basesecurity.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #ifndef BASESECURITY_INCL
  14. #define BASESECURITY_INCL
  15. #pragma warning (disable : 4786)
  16. #pragma warning (disable : 4018)
  17. #include <stdlib.h>
  18. #include "seclib.hpp"
  19. #include "jliball.hpp"
  20. #include "SecureUser.hpp"
  21. #include "SecurityResource.hpp"
  22. // to avoid warning about macro max/min
  23. #undef max
  24. #undef min
  25. #include "caching.hpp"
  26. #include "SecurityResourceList.hpp"
  27. #include <map>
  28. #include <string>
  29. typedef MapStringTo<int> MapStrToInt;
  30. const char* const def_ExpirationDate = "ExpirationDate";
  31. typedef std::map<std::string, CSecurityResourceList* > MapStrToResList;
  32. typedef std::map<std::string, bool> IPList;
  33. class UserInfo : public CInterface
  34. {
  35. private:
  36. ISecUser* _UserInfo;
  37. MapStrToResList _resList;
  38. unsigned _timeCreated;
  39. unsigned _timeOut;
  40. public:
  41. IMPLEMENT_IINTERFACE;
  42. UserInfo(ISecUser& userInfo, unsigned TimeoutPeriod=60000)
  43. {
  44. _UserInfo = &userInfo;
  45. if(_UserInfo)
  46. _UserInfo->Link();
  47. _timeCreated = msTick();
  48. _timeOut = TimeoutPeriod;
  49. }
  50. virtual ~UserInfo()
  51. {
  52. MapStrToResList::iterator pos;
  53. for(pos=_resList.begin();pos!=_resList.end();){
  54. pos->second->Release();
  55. pos++;
  56. }
  57. if(_UserInfo)
  58. _UserInfo->Release();
  59. }
  60. ISecResourceList* queryList(const char* ListName)
  61. {
  62. return (ISecResourceList*)_resList[ListName];
  63. }
  64. void appendList(const char* ListName, ISecResourceList* list)
  65. {
  66. if(list && ListName)
  67. _resList[ListName] = (CSecurityResourceList*)list;
  68. }
  69. virtual bool IsValid()
  70. {
  71. return (msTick() > (_timeOut + _timeCreated)) ? false : true;
  72. }
  73. void CopyTo(ISecUser & sec_user)
  74. {
  75. if(_UserInfo)
  76. _UserInfo->copyTo(sec_user);
  77. }
  78. };
  79. typedef std::map<std::string, UserInfo*> MapStrToUsers;
  80. class CBaseSecurityManager : public CInterface,
  81. implements ISecManager
  82. {
  83. private:
  84. Owned<ISecAuthenticEvents> m_subscriber;
  85. StringBuffer m_dbserver;
  86. StringBuffer m_dbuser;
  87. StringBuffer m_dbpassword;
  88. int m_poolsize;
  89. SecPasswordEncoding m_dbpasswordEncoding;
  90. MapStrToInt m_usermap;
  91. Mutex m_usermap_mutex;
  92. MapStrToUsers m_userList;
  93. Owned<IProperties> m_extraparams;
  94. CPermissionsCache m_permissionsCache;
  95. unsigned m_passwordExpirationWarningDays;
  96. protected:
  97. CriticalSection crit;
  98. Owned<IPropertyTree> m_config;
  99. CriticalSection _cache_Section;
  100. IPList m_safeIPList;
  101. bool m_enableIPRoaming;
  102. bool m_enableOTP;
  103. public:
  104. IMPLEMENT_IINTERFACE
  105. CBaseSecurityManager(const char *serviceName, const char *config);
  106. CBaseSecurityManager(const char *serviceName, IPropertyTree *config);
  107. virtual ~CBaseSecurityManager();
  108. //interface ISecManager : extends IInterface
  109. ISecUser * createUser(const char * user_name);
  110. ISecResourceList * createResourceList(const char * rlname);
  111. bool subscribe(ISecAuthenticEvents & events);
  112. bool unsubscribe(ISecAuthenticEvents & events);
  113. bool virtual authorize(ISecUser & sec_user, ISecResourceList * Resources);
  114. bool authorizeEx(SecResourceType rtype, ISecUser& sec_user, ISecResourceList * Resources)
  115. {
  116. return authorize(sec_user, Resources);
  117. }
  118. int authorizeEx(SecResourceType rtype, ISecUser& sec_user, const char* resourcename)
  119. {
  120. if(!resourcename || !*resourcename)
  121. return SecAccess_Full;
  122. Owned<ISecResourceList> rlist;
  123. rlist.setown(createResourceList("resources"));
  124. rlist->addResource(resourcename);
  125. bool ok = authorizeEx(rtype, sec_user, rlist.get());
  126. if(ok)
  127. return rlist->queryResource(0)->getAccessFlags();
  128. else
  129. return -1;
  130. }
  131. virtual int getAccessFlagsEx(SecResourceType rtype, ISecUser& sec_user, const char* resourcename)
  132. {
  133. UNIMPLEMENTED;
  134. }
  135. virtual int authorizeFileScope(ISecUser & user, const char * filescope)
  136. {
  137. UNIMPLEMENTED;
  138. }
  139. virtual bool authorizeFileScope(ISecUser & user, ISecResourceList * resources)
  140. {
  141. UNIMPLEMENTED;
  142. }
  143. virtual int authorizeWorkunitScope(ISecUser & user, const char * filescope)
  144. {
  145. UNIMPLEMENTED;
  146. }
  147. virtual bool authorizeWorkunitScope(ISecUser & user, ISecResourceList * resources)
  148. {
  149. UNIMPLEMENTED;
  150. }
  151. virtual bool ValidateSourceIP(ISecUser & user,IPList& SafeIPList)
  152. {
  153. return true;
  154. }
  155. bool addResourcesEx(SecResourceType rtype, ISecUser& sec_user, ISecResourceList * resources, SecPermissionType ptype = PT_ADMINISTRATORS_ONLY, const char* basedn=NULL)
  156. {
  157. return addResources(sec_user, resources);
  158. }
  159. bool addResourceEx(SecResourceType rtype, ISecUser& user, const char* resourcename, SecPermissionType ptype = PT_ADMINISTRATORS_ONLY, const char* basedn=NULL)
  160. {
  161. Owned<ISecResourceList> rlist;
  162. rlist.setown(createResourceList("resources"));
  163. rlist->addResource(resourcename);
  164. return addResourcesEx(rtype, user, rlist.get(), ptype, basedn);
  165. }
  166. bool addResources(ISecUser & user, ISecResourceList * resources);
  167. bool updateResources(ISecUser & user, ISecResourceList * resources);
  168. virtual bool updateSettings(ISecUser &user, ISecPropertyList* resources) ;
  169. virtual bool getResources(SecResourceType rtype, const char * basedn, IArrayOf<ISecResource> & resources)
  170. {
  171. UNIMPLEMENTED;
  172. }
  173. virtual bool addUser(ISecUser & user);
  174. virtual ISecUser * lookupUser(unsigned uid)
  175. {
  176. return NULL;
  177. }
  178. virtual ISecUser * findUser(const char * username)
  179. {
  180. return NULL;
  181. }
  182. virtual bool initUser(ISecUser& user)
  183. {
  184. return false;
  185. }
  186. virtual ISecUserIterator * getAllUsers()
  187. {
  188. return NULL;
  189. }
  190. virtual void setExtraParam(const char * name, const char * value)
  191. {
  192. if(name == NULL || name[0] == '\0')
  193. return;
  194. if (!m_extraparams)
  195. m_extraparams.setown(createProperties(false));
  196. m_extraparams->setProp(name, value);
  197. }
  198. virtual IAuthMap * createAuthMap(IPropertyTree * authconfig) {return NULL;}
  199. virtual IAuthMap * createFeatureMap(IPropertyTree * authconfig) {return NULL;}
  200. virtual IAuthMap * createSettingMap(IPropertyTree * authconfig) {return NULL;}
  201. virtual bool updateUserPassword(ISecUser& user, const char* newPassword, const char* currPassword = NULL);
  202. virtual bool IsPasswordExpired(ISecUser& user){return false;}
  203. void getAllGroups(StringArray & groups, StringArray & managedBy, StringArray & descriptions) { UNIMPLEMENTED;}
  204. virtual void deleteResource(SecResourceType rtype, const char * name, const char * basedn)
  205. {
  206. UNIMPLEMENTED;
  207. }
  208. virtual void renameResource(SecResourceType rtype, const char * oldname, const char * newname, const char * basedn)
  209. {
  210. //UNIMPLEMENTED;
  211. }
  212. virtual void copyResource(SecResourceType rtype, const char * oldname, const char * newname, const char * basedn)
  213. {
  214. UNIMPLEMENTED;
  215. }
  216. virtual void cacheSwitch(SecResourceType rtype, bool on)
  217. {
  218. UNIMPLEMENTED;
  219. }
  220. virtual SecUserStatus getUserStatus(const char* StatusFlag)
  221. {
  222. UNIMPLEMENTED;
  223. }
  224. virtual SecPasswordEncoding getPasswordEncoding()
  225. {
  226. return m_dbpasswordEncoding;
  227. }
  228. virtual bool authTypeRequired(SecResourceType rtype) {return false;};
  229. virtual const char* getDescription()
  230. {
  231. return NULL;
  232. }
  233. virtual unsigned getPasswordExpirationWarningDays()
  234. {
  235. return m_passwordExpirationWarningDays;
  236. }
  237. virtual bool createUserScopes() {UNIMPLEMENTED; return false;}
  238. virtual aindex_t getManagedFileScopes(IArrayOf<ISecResource>& scopes) {UNIMPLEMENTED; }
  239. virtual int queryDefaultPermission(ISecUser& user) {UNIMPLEMENTED; }
  240. virtual bool clearPermissionsCache(ISecUser& user) {return false;}
  241. virtual bool authenticateUser(ISecUser & user, bool &superUser) {return false;}
  242. protected:
  243. const char* getServer(){return m_dbserver.str();}
  244. const char* getUser(){return m_dbuser.str();}
  245. const char* getPassword(){return m_dbpassword.str();}
  246. int getPoolsize() { return m_poolsize;}
  247. void setUserMap(const char* user,int uid){synchronized block(m_usermap_mutex); m_usermap.setValue(user, uid);}
  248. int getUserID(ISecUser& user);
  249. void logon_failed(const char* user, const char* msg);
  250. int findUser(const char* user,const char* realm);
  251. void init(const char *serviceName, IPropertyTree *config);
  252. void EncodePassword(StringBuffer& password);
  253. bool ValidateResources(ISecUser & sec_user,IArrayOf<ISecResource>& rlist);
  254. bool updateSettings(ISecUser & sec_user,IArrayOf<ISecResource>& rlist);
  255. virtual bool ValidateUser(ISecUser & sec_user);
  256. virtual bool ValidateResources(ISecUser & sec_user, ISecResourceList * Resources);
  257. virtual StringBuffer& buildAuthenticateQuery(const char* user,const char* password,const char* realm, StringBuffer& SQLQuery){return SQLQuery;}
  258. virtual bool dbValidateResource(ISecResource& res,int usernum,const char* realm)
  259. {
  260. return false;
  261. }
  262. virtual bool dbValidateSetting(ISecResource& res,int usernum,const char* realm)
  263. {
  264. return false;
  265. }
  266. virtual bool dbValidateSetting(ISecResource& res,ISecUser& User)
  267. {
  268. return false;
  269. }
  270. virtual bool dbUpdateResource(ISecResource& res,int usernum,const char* Realm)
  271. {
  272. return false;
  273. }
  274. virtual StringBuffer& ExecuteScalar(const char* Query,const char* FieldName, StringBuffer & ReturnVal){return ReturnVal;}
  275. virtual bool dbauthenticate(StringBuffer &user, StringBuffer &password,StringBuffer &realm,StringBuffer& status,int& ExpirationDate)
  276. {
  277. return false;
  278. }
  279. virtual bool dbauthenticate(ISecUser& User, StringBuffer& SQLQuery)
  280. {
  281. return false;
  282. }
  283. virtual int dbLookupUser(const char* user,const char* realm)
  284. {
  285. return 0;
  286. }
  287. virtual bool dbUpdatePasswrd(const char* user,const char* realm,const char* password)
  288. {
  289. return false;
  290. }
  291. virtual StringBuffer& dbGetEffectiveAccess(int usernum, const char * resource, const char * member, const char * objclass,StringBuffer& returnValue)
  292. {
  293. returnValue.appendf("%d",-1);
  294. return returnValue;
  295. }
  296. bool IsIPRestricted(ISecUser& User);
  297. virtual bool IsPasswordValid(ISecUser& sec_user);
  298. virtual void dbConnect()
  299. {
  300. }
  301. virtual void dbDisconnect()
  302. {
  303. }
  304. virtual bool inHierarchy(const char* username)
  305. {
  306. return false;
  307. }
  308. virtual bool inHierarchy(int usernum)
  309. {
  310. return false;
  311. }
  312. virtual bool validateSecurityQuestion(ISecUser* user, const char* token)
  313. {
  314. return false;
  315. }
  316. virtual int validateOTP(ISecUser* user, const char* cookie)
  317. {
  318. return 0;
  319. }
  320. };
  321. #endif // BASESECURITY_INCL
  322. //#endif