ldapconnection.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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 __LDAPCONNECTION_HPP
  14. #define __LDAPCONNECTION_HPP
  15. #include <stdlib.h>
  16. #include "thirdparty.h"
  17. #include "jiface.hpp"
  18. #include "jliball.hpp"
  19. #include "seclib.hpp"
  20. #ifdef _WIN32
  21. #include <windows.h>
  22. #include <winldap.h>
  23. #include <winber.h>
  24. #include <rpc.h>
  25. #include <rpcdce.h>
  26. #include "dsgetdc.h"
  27. #include <lm.h>
  28. #else
  29. #define LDAP_DEPRECATED 1
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include <ldap_cdefs.h>
  33. #include <ldap.h>
  34. #endif
  35. #ifdef _WIN32
  36. #ifndef LDAPSECURITY_EXPORTS
  37. #define LDAPSECURITY_API __declspec(dllimport)
  38. #else
  39. #define LDAPSECURITY_API __declspec(dllexport)
  40. #endif//LDAPSECURITY_EXPORTS
  41. #else
  42. #define LDAPSECURITY_API
  43. #endif //_WIN32
  44. #ifdef _WIN32
  45. /*from Winldap.h
  46. WINLDAPAPI ULONG LDAPAPI ldap_compare_ext_s(
  47. LDAP *ld,
  48. const PCHAR dn,
  49. const PCHAR Attr,
  50. const PCHAR Value, // either value or Data is not null, not both
  51. struct berval *Data,
  52. PLDAPControlA *ServerControls,
  53. PLDAPControlA *ClientControls
  54. );
  55. */
  56. #define LDAP_COMPARE_EXT_S(ld,dn,attr,bval,data,svrctrls,clientctrls) ldap_compare_ext_s(ld,(const PCHAR)dn,(const PCHAR)attr,(const PCHAR)bval,(struct berval *)data,svrctrls,clientctrls)
  57. #define LDAP_UNBIND(ld) ldap_unbind(ld)
  58. #define LDAP_INIT(ld,uri) ldap_init(ld, uri);
  59. #else
  60. /* from openLDAP ldap.h
  61. ldap_compare_ext_s LDAP_P((
  62. LDAP *ld,
  63. LDAP_CONST char *dn,
  64. LDAP_CONST char *attr,
  65. struct berval *bvalue,
  66. LDAPControl **serverctrls,
  67. LDAPControl **clientctrls ));
  68. */
  69. #define LDAP_COMPARE_EXT_S(ld,dn,attr,bval,svrctrls,clientctrls,msgnum) ldap_compare_ext_s(ld,(const char*)dn,(const char*)attr,(struct berval *)bval,svrctrls,clientctrls)
  70. #define LDAP_UNBIND(ld) ldap_unbind_ext(ld,0,0)
  71. #define LDAP_INIT(ld,uri) ldap_initialize(ld, uri);
  72. #endif
  73. #ifdef _WIN32
  74. typedef struct l_timeval TIMEVAL;
  75. #else
  76. typedef struct timeval TIMEVAL;
  77. #endif
  78. #define LDAPTIMEOUT 60 //20 second connection/search timeout
  79. #define DEFAULT_LDAP_POOL_SIZE 10
  80. // 1 for ActiveDirectory, 2 for iPlanet, 3 for openLdap
  81. enum LdapServerType
  82. {
  83. LDAPSERVER_UNKNOWN = 0,
  84. ACTIVE_DIRECTORY = 1,
  85. IPLANET = 2,
  86. OPEN_LDAP = 3
  87. };
  88. enum ACT_TYPE
  89. {
  90. USER_ACT = 0,
  91. GROUP_ACT = 1
  92. };
  93. enum UserField
  94. {
  95. UFUserID = 0,
  96. UFName = 1,
  97. UFFullName = 2,
  98. UFPasswordExpiration = 3,
  99. UFterm = 4,
  100. UFreverse = 256,
  101. UFnocase = 512,
  102. UFnumeric = 1024
  103. };
  104. enum GroupField
  105. {
  106. GFName = 0,
  107. GFManagedBy = 1,
  108. GFDesc = 2,
  109. GFterm = 3,
  110. GFreverse = 256,
  111. GFnocase = 512,
  112. GFnumeric = 1024
  113. };
  114. #define RF_NONE 0x00
  115. #define RF_RT_FILE_SCOPE_FILE 0x01
  116. #define RF_RT_MODULE_NO_REPOSITORY 0x02
  117. enum ResourceField
  118. {
  119. RFName = 0,
  120. RFDesc = 1,
  121. RFterm = 2,
  122. RFreverse = 256,
  123. RFnocase = 512,
  124. RFnumeric = 1024
  125. };
  126. extern LDAPSECURITY_API const char* getUserFieldNames(UserField feild);
  127. extern LDAPSECURITY_API const char* getGroupFieldNames(GroupField feild);
  128. extern LDAPSECURITY_API const char* getResourceFieldNames(ResourceField feild);
  129. typedef IIteratorOf<IPropertyTree> ISecItemIterator;
  130. interface IPermissionProcessor;
  131. interface ILdapConnection : extends IInterface
  132. {
  133. virtual LDAP* getLd() = 0;
  134. };
  135. interface ILdapConnectionPool : extends IInterface
  136. {
  137. virtual ILdapConnection* getConnection() = 0;
  138. virtual ILdapConnection* getSSLConnection() = 0;
  139. };
  140. interface ILdapConfig : extends IInterface
  141. {
  142. virtual LdapServerType getServerType() = 0;
  143. virtual const char * getCfgServerType() const = 0;
  144. virtual StringBuffer& getLdapHost(StringBuffer& hostbuf) = 0;
  145. virtual void markDown(const char* ldaphost) = 0;
  146. virtual int getLdapPort() = 0;
  147. virtual int getLdapSecurePort() = 0;
  148. virtual const char* getProtocol() = 0;
  149. virtual const char* getBasedn() = 0;
  150. virtual const char* getDomain() = 0;
  151. virtual const char* getAuthMethod() = 0;
  152. virtual const char* getUserBasedn() = 0;
  153. virtual const char* getGroupBasedn() = 0;
  154. virtual const char* getResourceBasedn(SecResourceType rtype) = 0;
  155. virtual const char* getTemplateName() = 0;
  156. virtual const char* getSysUser() = 0;
  157. virtual const char* getSysUserDn() = 0;
  158. virtual const char* getSysUserCommonName() = 0;
  159. virtual const char* getSysUserPassword() = 0;
  160. virtual const char* getSysUserBasedn() = 0;
  161. virtual bool sysuserSpecified() = 0;
  162. virtual int getMaxConnections() = 0;
  163. virtual void setResourceBasedn(const char* rbasedn, SecResourceType rtype = RT_DEFAULT) = 0;
  164. };
  165. class CPermission : public CInterface, implements IInterface
  166. {
  167. StringBuffer m_account_name;
  168. ACT_TYPE m_account_type;
  169. int m_allows;
  170. int m_denies;
  171. public:
  172. IMPLEMENT_IINTERFACE
  173. CPermission(const char* account_name, ACT_TYPE account_type, int allows, int denies)
  174. {
  175. m_account_name.append(account_name);
  176. m_account_type = account_type;
  177. m_allows = allows;
  178. m_denies = denies;
  179. }
  180. const char* getAccount_name() {return m_account_name.str();}
  181. ACT_TYPE getAccount_type() {return m_account_type;}
  182. int getAllows() {return m_allows;}
  183. int getDenies() {return m_denies;}
  184. void setAllows(int allows) { m_allows = allows;}
  185. void setDenies(int denies) { m_denies = denies;}
  186. };
  187. class CPermissionAction : public CInterface, implements IInterface
  188. {
  189. public:
  190. StringBuffer m_action;
  191. StringBuffer m_basedn;
  192. SecResourceType m_rtype;
  193. StringBuffer m_rname;
  194. StringBuffer m_account_name;
  195. ACT_TYPE m_account_type;
  196. int m_allows;
  197. int m_denies;
  198. IMPLEMENT_IINTERFACE
  199. };
  200. interface ILdapClient : extends IInterface
  201. {
  202. virtual void init(IPermissionProcessor* pp) = 0;
  203. virtual LdapServerType getServerType() = 0;
  204. virtual bool authenticate(ISecUser& user) = 0;
  205. virtual bool authorize(SecResourceType rtype, ISecUser&, IArrayOf<ISecResource>& resources) = 0;
  206. virtual bool addResources(SecResourceType rtype, ISecUser& user, IArrayOf<ISecResource>& resources, SecPermissionType ptype, const char* basedn) = 0;
  207. virtual bool addUser(ISecUser& user) = 0;
  208. virtual void getGroups(const char *user, StringArray& groups) = 0;
  209. virtual bool getUserInfo(ISecUser& user, const char* infotype = NULL) = 0;
  210. virtual ISecUser* lookupUser(unsigned uid) = 0;
  211. virtual bool lookupAccount(MemoryBuffer& sidbuf, StringBuffer& account_name, ACT_TYPE& act_type) = 0;
  212. virtual void lookupSid(const char* act_name, MemoryBuffer& act_sid, ACT_TYPE act_type) = 0;
  213. virtual void setPermissionProcessor(IPermissionProcessor* pp) = 0;
  214. virtual bool retrieveUsers(IUserArray& users) = 0;
  215. virtual bool retrieveUsers(const char* searchstr, IUserArray& users) = 0;
  216. virtual IPropertyTreeIterator* getUserIterator(const char* userName) = 0;
  217. virtual ISecItemIterator* getUsersSorted(const char* userName, UserField* sortOrder, const unsigned pageStartFrom, const unsigned pageSize,
  218. unsigned *total, __int64 *cachehint) = 0;
  219. virtual void getAllGroups(StringArray & groups, StringArray & managedBy, StringArray & descriptions) = 0;
  220. virtual IPropertyTreeIterator* getGroupIterator() = 0;
  221. virtual ISecItemIterator* getGroupsSorted(GroupField* sortOrder, const unsigned pageStartFrom, const unsigned pageSize,
  222. unsigned *total, __int64 *cachehint) = 0;
  223. virtual IPropertyTreeIterator* getGroupMemberIterator(const char* groupName) = 0;
  224. virtual ISecItemIterator* getGroupMembersSorted(const char* groupName, UserField* sortOrder, const unsigned pageStartFrom, const unsigned pageSize,
  225. unsigned *total, __int64 *cachehint) = 0;
  226. virtual void setResourceBasedn(const char* rbasedn, SecResourceType rtype = RT_DEFAULT) = 0;
  227. virtual ILdapConfig* getLdapConfig() = 0;
  228. virtual bool userInGroup(const char* userdn, const char* groupdn) = 0;
  229. virtual bool updateUserPassword(ISecUser& user, const char* newPassword, const char* currPassword = 0) = 0;
  230. virtual bool updateUser(const char* type, ISecUser& user) = 0;
  231. virtual bool updateUserPassword(const char* username, const char* newPassword) = 0;
  232. virtual bool getResources(SecResourceType rtype, const char * basedn, const char* prefix, IArrayOf<ISecResource>& resources) = 0;
  233. virtual bool getResourcesEx(SecResourceType rtype, const char * basedn, const char* prefix, const char* searchstr, IArrayOf<ISecResource>& resources) = 0;
  234. virtual IPropertyTreeIterator* getResourceIterator(SecResourceType rtype, const char * basedn, const char* prefix,
  235. const char* resourceName, unsigned extraNameFilter) = 0;
  236. virtual ISecItemIterator* getResourcesSorted(SecResourceType rtype, const char * basedn, const char* resourceName, unsigned extraNameFilter,
  237. ResourceField* sortOrder, const unsigned pageStartFrom, const unsigned pageSize, unsigned *total, __int64 *cachehint) = 0;
  238. virtual bool getPermissionsArray(const char* basedn, SecResourceType rtype, const char* name, IArrayOf<CPermission>& permissions) = 0;
  239. virtual bool changePermission(CPermissionAction& action) = 0;
  240. virtual void changeUserGroup(const char* action, const char* username, const char* groupname) = 0;
  241. virtual bool deleteUser(ISecUser* user) = 0;
  242. virtual void addGroup(const char* groupname, const char * groupOwner, const char * groupDesc) = 0;
  243. virtual void deleteGroup(const char* groupname) = 0;
  244. virtual void getGroupMembers(const char* groupname, StringArray & users) = 0;
  245. virtual void deleteResource(SecResourceType rtype, const char* name, const char* basedn) = 0;
  246. virtual void renameResource(SecResourceType rtype, const char* oldname, const char* newname, const char* basedn) = 0;
  247. virtual void copyResource(SecResourceType rtype, const char* oldname, const char* newname, const char* basedn) = 0;
  248. virtual void normalizeDn(const char* dn, StringBuffer& ndn) = 0;
  249. virtual bool isSuperUser(ISecUser* user) = 0;
  250. virtual int countEntries(const char* basedn, const char* objectClass, int limit) = 0;
  251. virtual int countUsers(const char* searchstr, int limit) = 0;
  252. virtual int countResources(const char* basedn, const char* searchstr, int limit) = 0;
  253. virtual ILdapConfig* queryConfig() = 0;
  254. virtual const char* getPasswordStorageScheme() = 0;
  255. virtual bool createUserScope(ISecUser& user) = 0;
  256. virtual aindex_t getManagedFileScopes(IArrayOf<ISecResource>& scopes) = 0;
  257. virtual int queryDefaultPermission(ISecUser& user) = 0;
  258. };
  259. ILdapClient* createLdapClient(IPropertyTree* cfg);
  260. #ifdef _WIN32
  261. bool verifyServerCert(LDAP* ld, PCCERT_CONTEXT pServerCert);
  262. #endif
  263. //--------------------------------------------
  264. // This helper class ensures memory allocated by
  265. // calls to ldap_get_values_len gets freed
  266. //--------------------------------------------
  267. class CLDAPGetValuesLenWrapper
  268. {
  269. private:
  270. struct berval** bvalues;
  271. unsigned numValues;
  272. public:
  273. CLDAPGetValuesLenWrapper()
  274. {
  275. bvalues = NULL;
  276. numValues = 0;
  277. }
  278. CLDAPGetValuesLenWrapper(LDAP *ld, LDAPMessage *msg, const char * attr)
  279. {
  280. bvalues = NULL;
  281. retrieveBValues(ld,msg,attr);
  282. }
  283. ~CLDAPGetValuesLenWrapper()
  284. {
  285. if (bvalues)
  286. ldap_value_free_len(bvalues);
  287. }
  288. inline bool hasValues() { return bvalues != NULL && *bvalues != NULL; }
  289. inline berval **queryBValues() { return bvalues; }
  290. inline const char * queryCharValue(unsigned which){ return which < numValues ? (*(bvalues[which])).bv_val : NULL; }
  291. //Delayed call to ldap_get_values_len
  292. void retrieveBValues(LDAP *ld, LDAPMessage *msg, const char * attr)
  293. {
  294. if (bvalues)
  295. ldap_value_free_len(bvalues);
  296. #ifdef _WIN32
  297. bvalues = ldap_get_values_len(ld, msg, (const PCHAR)attr);
  298. #else
  299. bvalues = ldap_get_values_len(ld, msg, attr);
  300. #endif
  301. for (numValues = 0; bvalues && bvalues[numValues]; numValues++);
  302. }
  303. };
  304. #endif