ldapsecurity.ipp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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 __LDAPSECURITY_IPP_
  14. #define __LDAPSECURITY_IPP_
  15. #pragma warning(disable:4786)
  16. #include "permissions.ipp"
  17. #include "aci.ipp"
  18. #include "caching.hpp"
  19. #undef new
  20. #include <map>
  21. #include <string>
  22. #if defined(_DEBUG) && defined(_WIN32) && !defined(USING_MPATROL)
  23. #define new new(_NORMAL_BLOCK, __FILE__, __LINE__)
  24. #endif
  25. #include "seclib.hpp"
  26. #ifndef LDAPSECURITY_EXPORTS
  27. #define LDAPSECURITY_API DECL_IMPORT
  28. #else
  29. #define LDAPSECURITY_API DECL_EXPORT
  30. #endif
  31. class LDAPSECURITY_API CLdapSecUser : implements ISecUser, implements ISecCredentials, public CInterface
  32. {
  33. private:
  34. StringAttr m_realm;
  35. StringAttr m_name;
  36. StringAttr m_fullname;
  37. StringAttr m_firstname;
  38. StringAttr m_lastname;
  39. StringAttr m_pw;
  40. StringAttr m_employeeID;
  41. StringAttr m_distinguishedName;
  42. StringAttr m_Fqdn;
  43. StringAttr m_Peer;
  44. authStatus m_authenticateStatus;
  45. CDateTime m_passwordExpiration;//local time
  46. unsigned m_userid;
  47. MemoryBuffer m_usersid;
  48. BufferArray m_groupsids;
  49. bool m_posixenabled;
  50. StringAttr m_gidnumber;
  51. StringAttr m_uidnumber;
  52. StringAttr m_homedirectory;
  53. StringAttr m_loginshell;
  54. bool m_sudoersenabled;
  55. bool m_insudoers;
  56. StringAttr m_sudoHost;
  57. StringAttr m_sudoCommand;
  58. StringAttr m_sudoOption;
  59. unsigned m_sessionToken;//User's ESP session token
  60. StringBuffer m_signature;//User's digital signature
  61. public:
  62. IMPLEMENT_IINTERFACE
  63. CLdapSecUser(const char *name, const char *pw);
  64. virtual ~CLdapSecUser();
  65. //non-interfaced functions
  66. void setUserID(unsigned userid);
  67. void setUserSid(int sidlen, const char* sid);
  68. MemoryBuffer& getUserSid();
  69. //interface ISecUser
  70. const char * getName();
  71. bool setName(const char * name);
  72. virtual const char * getFullName();
  73. virtual bool setFullName(const char * name);
  74. virtual const char * getFirstName();
  75. virtual bool setFirstName(const char * fname);
  76. virtual const char * getLastName();
  77. virtual bool setLastName(const char * lname);
  78. virtual const char * getEmployeeID();
  79. virtual bool setEmployeeID(const char * emplID);
  80. virtual const char * getDistinguishedName();
  81. virtual bool setDistinguishedName(const char * dn);
  82. const char * getRealm();
  83. bool setRealm(const char * name);
  84. ISecCredentials & credentials();
  85. virtual unsigned getUserID();
  86. virtual void copyTo(ISecUser& source);
  87. virtual const char * getFqdn();
  88. virtual bool setFqdn(const char * Fqdn);
  89. virtual const char *getPeer();
  90. virtual bool setPeer(const char *Peer);
  91. virtual SecUserStatus getStatus(){return SecUserStatus_Unknown;}
  92. virtual bool setStatus(SecUserStatus Status){return false;}
  93. virtual CDateTime& getPasswordExpiration(CDateTime& expirationDate)
  94. {
  95. expirationDate.set(m_passwordExpiration);
  96. return expirationDate;
  97. }
  98. virtual bool setPasswordExpiration(CDateTime& expirationDate)
  99. {
  100. m_passwordExpiration.set(expirationDate);
  101. return true;
  102. }
  103. virtual int getPasswordDaysRemaining()
  104. {
  105. if (m_passwordExpiration.isNull())
  106. return scPasswordNeverExpires;//-2 if never expires
  107. CDateTime expiry(m_passwordExpiration);
  108. CDateTime now;
  109. now.setNow();
  110. now.adjustTime(now.queryUtcToLocalDelta());
  111. if (expiry <= now)
  112. return scPasswordExpired;//-1 if already expired
  113. expiry.setTime(0,0,0,0);
  114. now.setTime(23,59,59);
  115. int numDays = 0;
  116. while (expiry > now)
  117. {
  118. ++numDays;
  119. now.adjustTime(24*60);
  120. }
  121. return numDays;
  122. }
  123. authStatus getAuthenticateStatus() { return m_authenticateStatus; }
  124. void setAuthenticateStatus(authStatus status){ m_authenticateStatus = status; }
  125. ISecUser * clone();
  126. virtual void setProperty(const char* name, const char* value){}
  127. virtual const char* getProperty(const char* name){ return "";}
  128. virtual void setPropertyInt(const char* name, int value){}
  129. virtual int getPropertyInt(const char* name){ return 0;}
  130. IPropertyIterator * getPropertyIterator() const override { return nullptr;}
  131. //interface ISecCredentials
  132. bool setPassword(const char * pw);
  133. const char* getPassword();
  134. bool setEncodedPassword(SecPasswordEncoding enc, void * pw, unsigned length, void * salt, unsigned saltlen);
  135. void setSessionToken(unsigned token);
  136. unsigned getSessionToken();
  137. void setSignature(const char * signature);
  138. const char * getSignature();
  139. // Posix specific fields
  140. virtual void setGidnumber(const char* gidnumber)
  141. {
  142. m_gidnumber.set(gidnumber);
  143. }
  144. virtual const char* getGidnumber()
  145. {
  146. return m_gidnumber.get();
  147. }
  148. virtual void setUidnumber(const char* uidnumber)
  149. {
  150. m_uidnumber.set(uidnumber);
  151. }
  152. virtual const char* getUidnumber()
  153. {
  154. return m_uidnumber.get();
  155. }
  156. virtual void setHomedirectory(const char* homedir)
  157. {
  158. m_homedirectory.set(homedir);
  159. }
  160. virtual const char* getHomedirectory()
  161. {
  162. return m_homedirectory.get();
  163. }
  164. virtual void setLoginshell(const char* loginshell)
  165. {
  166. m_loginshell.set(loginshell);
  167. }
  168. virtual const char* getLoginshell()
  169. {
  170. return m_loginshell.get();
  171. }
  172. virtual void setPosixenabled(bool enabled)
  173. {
  174. m_posixenabled = enabled;
  175. }
  176. virtual bool getPosixenabled()
  177. {
  178. return m_posixenabled;
  179. }
  180. // Sudoers specific fields
  181. virtual void setSudoersEnabled(bool enabled)
  182. {
  183. m_sudoersenabled = enabled;
  184. }
  185. virtual bool getSudoersEnabled()
  186. {
  187. return m_sudoersenabled;
  188. }
  189. virtual void setInSudoers(bool in)
  190. {
  191. m_insudoers = in;
  192. }
  193. virtual bool getInSudoers()
  194. {
  195. return m_insudoers;
  196. }
  197. virtual void setSudoHost(const char* host)
  198. {
  199. m_sudoHost.set(host);
  200. }
  201. virtual const char* getSudoHost()
  202. {
  203. return m_sudoHost.get();
  204. }
  205. virtual void setSudoCommand(const char* cmd)
  206. {
  207. m_sudoCommand.set(cmd);
  208. }
  209. virtual const char* getSudoCommand()
  210. {
  211. return m_sudoCommand.get();
  212. }
  213. virtual void setSudoOption(const char* option)
  214. {
  215. m_sudoOption.set(option);
  216. }
  217. virtual const char* getSudoOption()
  218. {
  219. return m_sudoOption.get();
  220. }
  221. };
  222. class CLdapSecResource : implements ISecResource, public CInterface
  223. {
  224. private:
  225. StringAttr m_name;
  226. SecAccessFlags m_access;
  227. SecAccessFlags m_required_access;
  228. Owned<IProperties> m_parameters;
  229. StringBuffer m_description;
  230. StringBuffer m_value;
  231. SecResourceType m_resourcetype;
  232. public:
  233. IMPLEMENT_IINTERFACE
  234. CLdapSecResource(const char *name);
  235. void addAccess(SecAccessFlags flags);
  236. void setAccessFlags(SecAccessFlags flags);
  237. virtual void setRequiredAccessFlags(SecAccessFlags flags);
  238. virtual SecAccessFlags getRequiredAccessFlags();
  239. //interface ISecResource : extends IInterface
  240. virtual const char * getName();
  241. virtual SecAccessFlags getAccessFlags();
  242. virtual int addParameter(const char* name, const char* value);
  243. virtual const char * getParameter(const char * name);
  244. virtual IPropertyIterator * getParameterIterator() const override;
  245. virtual void setDescription(const char* description);
  246. virtual const char* getDescription();
  247. virtual void setValue(const char* value);
  248. virtual const char* getValue();
  249. virtual ISecResource * clone();
  250. virtual SecResourceType getResourceType();
  251. virtual void setResourceType(SecResourceType resourcetype);
  252. virtual void copy(ISecResource* from);
  253. virtual StringBuffer& toString(StringBuffer& s)
  254. {
  255. s.appendf("%s: %s (value: %s, rqr'ed access: %d, type: %s)", m_name.get(), m_description.str(),
  256. m_value.str(), m_required_access, resTypeDesc(m_resourcetype));
  257. return s;
  258. }
  259. };
  260. class CLdapSecResourceList : implements ISecResourceList, public CInterface
  261. {
  262. private:
  263. bool m_complete;
  264. StringAttr m_name;
  265. IArrayOf<ISecResource> m_rlist;
  266. std::map<std::string, ISecResource*> m_rmap;
  267. public:
  268. IMPLEMENT_IINTERFACE
  269. CLdapSecResourceList(const char *name);
  270. void setAuthorizationComplete(bool value);
  271. IArrayOf<ISecResource>& getResourceList();
  272. //interface ISecResourceList : extends IInterface
  273. bool isAuthorizationComplete();
  274. virtual ISecResourceList * clone();
  275. virtual bool copyTo(ISecResourceList& destination);
  276. void clear();
  277. ISecResource* addResource(const char * name);
  278. virtual void addResource(ISecResource * resource);
  279. bool addCustomResource(const char * name, const char * config);
  280. ISecResource * getResource(const char * Resource);
  281. virtual int count();
  282. virtual const char* getName();
  283. virtual ISecResource * queryResource(unsigned seq);
  284. virtual ISecPropertyIterator * getPropertyItr();
  285. virtual ISecProperty* findProperty(const char* name);
  286. virtual StringBuffer& toString(StringBuffer& s)
  287. {
  288. s.appendf("name=%s, count=%d.", m_name.get(), count());
  289. for (int i=0; i<count(); i++)
  290. {
  291. s.appendf("\nItem %d: ",i+1);
  292. queryResource(i)->toString(s);
  293. }
  294. return s;
  295. }
  296. };
  297. class LDAPSECURITY_API CLdapSecManager : implements ISecManager, public CInterface
  298. {
  299. private:
  300. Owned<ILdapClient> m_ldap_client;
  301. Owned<IPermissionProcessor> m_pp;
  302. Owned<IPropertyTree> m_cfg;
  303. Owned<ISecAuthenticEvents> m_subscriber;
  304. StringBuffer m_server;
  305. void init(const char *serviceName, IPropertyTree* cfg);
  306. IUserArray m_user_array;
  307. Monitor m_monitor;
  308. Owned<IProperties> m_extraparams;
  309. Owned<CPermissionsCache> m_permissionsCache;
  310. bool m_cache_off[RT_SCOPE_MAX];
  311. bool m_usercache_off;
  312. bool authenticate(ISecUser* user);
  313. StringBuffer m_description;
  314. unsigned m_passwordExpirationWarningDays;
  315. bool m_checkViewPermissions;
  316. public:
  317. IMPLEMENT_IINTERFACE
  318. CLdapSecManager(const char *serviceName, const char *config);
  319. CLdapSecManager(const char *serviceName, IPropertyTree &config);
  320. virtual ~CLdapSecManager();
  321. //interface ISecManager : extends IInterface
  322. ISecUser * createUser(const char * user_name);
  323. ISecResourceList * createResourceList(const char * rlname);
  324. bool subscribe(ISecAuthenticEvents & events);
  325. bool unsubscribe(ISecAuthenticEvents & events);
  326. bool authorize(ISecUser& sec_user, ISecResourceList * Resources, IEspSecureContext* secureContext);
  327. bool authorizeEx(SecResourceType rtype, ISecUser& sec_user, ISecResourceList * Resources, IEspSecureContext* secureContext = NULL);
  328. SecAccessFlags authorizeEx(SecResourceType rtype, ISecUser& sec_user, const char* resourcename, IEspSecureContext* secureContext = NULL);
  329. virtual SecAccessFlags authorizeFileScope(ISecUser & user, const char * filescope);
  330. virtual bool authorizeFileScope(ISecUser & user, ISecResourceList * resources);
  331. virtual SecAccessFlags authorizeWorkunitScope(ISecUser & user, const char * wuscope);
  332. virtual bool authorizeViewScope(ISecUser & user, StringArray & filenames, StringArray & columnnames);
  333. virtual bool authorizeWorkunitScope(ISecUser & user, ISecResourceList * resources);
  334. virtual bool addResources(ISecUser& sec_user, ISecResourceList * resources);
  335. virtual SecAccessFlags getAccessFlagsEx(SecResourceType rtype, ISecUser & user, const char * resourcename);
  336. virtual bool addResourcesEx(SecResourceType rtype, ISecUser &user, ISecResourceList* resources, SecPermissionType ptype = PT_DEFAULT, const char* basedn = NULL);
  337. virtual bool addResourceEx(SecResourceType rtype, ISecUser& user, const char* resourcename, SecPermissionType ptype = PT_DEFAULT, const char* basedn = NULL);
  338. virtual bool updateResources(ISecUser& sec_user, ISecResourceList * resources){return false;}
  339. virtual bool addUser(ISecUser & user);
  340. virtual ISecUser * lookupUser(unsigned uid);
  341. virtual ISecUser * findUser(const char * username);
  342. virtual ISecUserIterator * getAllUsers();
  343. virtual void searchUsers(const char* searchstr, IUserArray& users);
  344. virtual ISecItemIterator* getUsersSorted(const char* userName, UserField* sortOrder, const unsigned pageStartFrom, const unsigned pageSize, unsigned* total, __int64* cacheHint);
  345. virtual void getAllUsers(IUserArray& users);
  346. virtual void setExtraParam(const char * name, const char * value);
  347. virtual IAuthMap * createAuthMap(IPropertyTree * authconfig);
  348. virtual IAuthMap * createFeatureMap(IPropertyTree * authconfig);
  349. virtual IAuthMap * createSettingMap(struct IPropertyTree *){return 0;}
  350. virtual bool updateSettings(ISecUser & User,ISecPropertyList * settings, IEspSecureContext* secureContext){return false;}
  351. virtual bool updateUserPassword(ISecUser& user, const char* newPassword, const char* currPassword = 0);
  352. virtual bool updateUser(const char* type, ISecUser& user);
  353. virtual bool updateUserPassword(const char* username, const char* newPassword);
  354. virtual bool initUser(ISecUser& user){return false;}
  355. virtual bool getResources(SecResourceType rtype, const char * basedn, IArrayOf<ISecResource>& resources);
  356. virtual bool getResourcesEx(SecResourceType rtype, const char * basedn, const char * searchstr, IArrayOf<ISecResource>& resources);
  357. virtual ISecItemIterator* getResourcesSorted(SecResourceType rtype, const char* basedn, const char* resourceName, unsigned extraNameFilter,
  358. ResourceField* sortOrder, const unsigned pageStartFrom, const unsigned pageSize, unsigned* total, __int64* cacheHint);
  359. virtual ISecItemIterator* getResourcePermissionsSorted(const char* name, enum ACCOUNT_TYPE_REQ accountType, const char* baseDN, const char* rtype, const char* prefix,
  360. ResourcePermissionField* sortOrder, const unsigned pageStartFrom, const unsigned pageSize, unsigned* total, __int64* cacheHint);
  361. virtual void cacheSwitch(SecResourceType rtype, bool on);
  362. virtual bool getPermissionsArray(const char* basedn, SecResourceType rtype, const char* name, IArrayOf<CPermission>& permissions);
  363. virtual void getAllGroups(StringArray & groups, StringArray & managedBy, StringArray & descriptions);
  364. virtual ISecItemIterator* getGroupsSorted(GroupField* sortOrder, const unsigned pageStartFrom, const unsigned pageSize, unsigned* total, __int64* cacheHint);
  365. virtual ISecItemIterator* getGroupMembersSorted(const char* groupName, UserField* sortOrder, const unsigned pageStartFrom, const unsigned pageSize, unsigned* total, __int64* cacheHint);
  366. virtual void getGroups(const char* username, StringArray & groups);
  367. virtual bool changePermission(CPermissionAction& action);
  368. virtual void changeUserGroup(const char* action, const char* username, const char* groupname);
  369. virtual bool deleteUser(ISecUser* user);
  370. virtual void addGroup(const char* groupname, const char * groupOwner, const char * groupDesc);
  371. virtual void deleteGroup(const char* groupname);
  372. virtual void getGroupMembers(const char* groupname, StringArray & users);
  373. virtual void deleteResource(SecResourceType rtype, const char * name, const char * basedn);
  374. virtual void renameResource(SecResourceType rtype, const char * oldname, const char * newname, const char * basedn);
  375. virtual void copyResource(SecResourceType rtype, const char * oldname, const char * newname, const char * basedn);
  376. virtual bool authorizeEx(SecResourceType rtype, ISecUser& sec_user, ISecResourceList * Resources, bool doAuthentication);
  377. virtual SecAccessFlags authorizeEx(SecResourceType rtype, ISecUser& sec_user, const char* resourcename, bool doAuthentication);
  378. virtual void normalizeDn(const char* dn, StringBuffer& ndn);
  379. virtual bool isSuperUser(ISecUser* user);
  380. virtual ILdapConfig* queryConfig();
  381. virtual int countResources(const char* basedn, const char* searchstr, int limit);
  382. virtual int countUsers(const char* searchstr, int limit);
  383. virtual bool authTypeRequired(SecResourceType rtype) {return true;};
  384. virtual bool getUserInfo(ISecUser& user, const char* infotype = NULL);
  385. virtual LdapServerType getLdapServerType()
  386. {
  387. if(m_ldap_client)
  388. return m_ldap_client->getServerType();
  389. else
  390. return ACTIVE_DIRECTORY;
  391. }
  392. virtual const char* getPasswordStorageScheme()
  393. {
  394. if(m_ldap_client)
  395. return m_ldap_client->getPasswordStorageScheme();
  396. else
  397. return NULL;
  398. }
  399. virtual const char* getDescription()
  400. {
  401. return m_description.str();
  402. }
  403. virtual unsigned getPasswordExpirationWarningDays()
  404. {
  405. return m_passwordExpirationWarningDays;
  406. }
  407. virtual bool getCheckViewPermissions()
  408. {
  409. return m_checkViewPermissions;
  410. }
  411. virtual bool createUserScopes();
  412. virtual aindex_t getManagedFileScopes(IArrayOf<ISecResource>& scopes);
  413. virtual SecAccessFlags queryDefaultPermission(ISecUser& user);
  414. virtual bool clearPermissionsCache(ISecUser &user);
  415. virtual bool authenticateUser(ISecUser & user, bool * superUser);
  416. virtual secManagerType querySecMgrType() { return SMT_LDAP; }
  417. inline virtual const char* querySecMgrTypeName() { return "LdapSecurity"; }
  418. virtual bool logoutUser(ISecUser & user);
  419. //Data View related interfaces
  420. virtual void createView(const char * viewName, const char * viewDescription);
  421. virtual void deleteView(const char * viewName);
  422. virtual void queryAllViews(StringArray & viewNames, StringArray & viewDescriptions, StringArray & viewManagedBy);
  423. virtual void addViewColumns(const char * viewName, StringArray & files, StringArray & columns);
  424. virtual void removeViewColumns(const char * viewName, StringArray & files, StringArray & columns);
  425. virtual void queryViewColumns(const char * viewName, StringArray & files, StringArray & columns);
  426. virtual void addViewMembers(const char * viewName, StringArray & viewUsers, StringArray & viewGroups);
  427. virtual void removeViewMembers(const char * viewName, StringArray & viewUsers, StringArray & viewGroups);
  428. virtual void queryViewMembers(const char * viewName, StringArray & viewUsers, StringArray & viewGroups);
  429. virtual bool userInView(const char * user, const char* viewName);
  430. };
  431. #endif