util_ldap.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /**
  17. * @file util_ldap.h
  18. * @brief Apache LDAP library
  19. */
  20. #ifndef UTIL_LDAP_H
  21. #define UTIL_LDAP_H
  22. /* APR header files */
  23. #include "apr.h"
  24. #include "apr_thread_mutex.h"
  25. #include "apr_thread_rwlock.h"
  26. #include "apr_tables.h"
  27. #include "apr_time.h"
  28. #include "apr_version.h"
  29. #if APR_MAJOR_VERSION < 2
  30. /* The LDAP API is currently only present in APR 1.x */
  31. #include "apr_ldap.h"
  32. #include "apr_ldap_rebind.h"
  33. #else
  34. #define APR_HAS_LDAP 0
  35. #endif
  36. #if APR_HAS_SHARED_MEMORY
  37. #include "apr_rmm.h"
  38. #include "apr_shm.h"
  39. #endif
  40. /* this whole thing disappears if LDAP is not enabled */
  41. #if APR_HAS_LDAP
  42. #if defined(LDAP_UNAVAILABLE) || APR_HAS_MICROSOFT_LDAPSDK
  43. #define AP_LDAP_IS_SERVER_DOWN(s) ((s) == LDAP_SERVER_DOWN \
  44. ||(s) == LDAP_UNAVAILABLE)
  45. #else
  46. #define AP_LDAP_IS_SERVER_DOWN(s) ((s) == LDAP_SERVER_DOWN)
  47. #endif
  48. /* Apache header files */
  49. #include "ap_config.h"
  50. #include "httpd.h"
  51. #include "http_config.h"
  52. #include "http_core.h"
  53. #include "http_log.h"
  54. #include "http_protocol.h"
  55. #include "http_request.h"
  56. #include "apr_optional.h"
  57. /* Create a set of LDAP_DECLARE macros with appropriate export
  58. * and import tags for the platform
  59. */
  60. #if !defined(WIN32)
  61. #define LDAP_DECLARE(type) type
  62. #define LDAP_DECLARE_NONSTD(type) type
  63. #define LDAP_DECLARE_DATA
  64. #elif defined(LDAP_DECLARE_STATIC)
  65. #define LDAP_DECLARE(type) type __stdcall
  66. #define LDAP_DECLARE_NONSTD(type) type
  67. #define LDAP_DECLARE_DATA
  68. #elif defined(LDAP_DECLARE_EXPORT)
  69. #define LDAP_DECLARE(type) __declspec(dllexport) type __stdcall
  70. #define LDAP_DECLARE_NONSTD(type) __declspec(dllexport) type
  71. #define LDAP_DECLARE_DATA __declspec(dllexport)
  72. #else
  73. #define LDAP_DECLARE(type) __declspec(dllimport) type __stdcall
  74. #define LDAP_DECLARE_NONSTD(type) __declspec(dllimport) type
  75. #define LDAP_DECLARE_DATA __declspec(dllimport)
  76. #endif
  77. #if APR_HAS_MICROSOFT_LDAPSDK
  78. #define timeval l_timeval
  79. #endif
  80. #ifdef __cplusplus
  81. extern "C" {
  82. #endif
  83. /*
  84. * LDAP Connections
  85. */
  86. /* Values that the deref member can have */
  87. typedef enum {
  88. never=LDAP_DEREF_NEVER,
  89. searching=LDAP_DEREF_SEARCHING,
  90. finding=LDAP_DEREF_FINDING,
  91. always=LDAP_DEREF_ALWAYS
  92. } deref_options;
  93. /* Structure representing an LDAP connection */
  94. typedef struct util_ldap_connection_t {
  95. LDAP *ldap;
  96. apr_pool_t *pool; /* Pool from which this connection is created */
  97. #if APR_HAS_THREADS
  98. apr_thread_mutex_t *lock; /* Lock to indicate this connection is in use */
  99. #endif
  100. const char *host; /* Name of the LDAP server (or space separated list) */
  101. int port; /* Port of the LDAP server */
  102. deref_options deref; /* how to handle alias dereferening */
  103. const char *binddn; /* DN to bind to server (can be NULL) */
  104. const char *bindpw; /* Password to bind to server (can be NULL) */
  105. int bound; /* Flag to indicate whether this connection is bound yet */
  106. int secure; /* SSL/TLS mode of the connection */
  107. apr_array_header_t *client_certs; /* Client certificates on this connection */
  108. const char *reason; /* Reason for an error failure */
  109. struct util_ldap_connection_t *next;
  110. struct util_ldap_state_t *st; /* The LDAP vhost config this connection belongs to */
  111. int keep; /* Will this connection be kept when it's unlocked */
  112. int ChaseReferrals; /* [on|off] (default = AP_LDAP_CHASEREFERRALS_ON)*/
  113. int ReferralHopLimit; /* # of referral hops to follow (default = AP_LDAP_DEFAULT_HOPLIMIT) */
  114. apr_time_t freed; /* the time this conn was placed back in the pool */
  115. apr_pool_t *rebind_pool; /* frequently cleared pool for rebind data */
  116. int must_rebind; /* The connection was last bound with other then binddn/bindpw */
  117. request_rec *r; /* request_rec used to find this util_ldap_connection_t */
  118. apr_time_t last_backend_conn; /* the approximate time of the last backend LDAP requst */
  119. } util_ldap_connection_t;
  120. typedef struct util_ldap_config_t {
  121. int ChaseReferrals;
  122. int ReferralHopLimit;
  123. apr_array_header_t *client_certs; /* Client certificates */
  124. } util_ldap_config_t;
  125. /* LDAP cache state information */
  126. typedef struct util_ldap_state_t {
  127. apr_pool_t *pool; /* pool from which this state is allocated */
  128. #if APR_HAS_THREADS
  129. apr_thread_mutex_t *mutex; /* mutex lock for the connection list */
  130. #endif
  131. apr_global_mutex_t *util_ldap_cache_lock;
  132. apr_size_t cache_bytes; /* Size (in bytes) of shared memory cache */
  133. char *cache_file; /* filename for shm */
  134. long search_cache_ttl; /* TTL for search cache */
  135. long search_cache_size; /* Size (in entries) of search cache */
  136. long compare_cache_ttl; /* TTL for compare cache */
  137. long compare_cache_size; /* Size (in entries) of compare cache */
  138. struct util_ldap_connection_t *connections;
  139. apr_array_header_t *global_certs; /* Global CA certificates */
  140. int ssl_supported;
  141. int secure;
  142. int secure_set;
  143. int verify_svr_cert;
  144. #if APR_HAS_SHARED_MEMORY
  145. apr_shm_t *cache_shm;
  146. apr_rmm_t *cache_rmm;
  147. #endif
  148. /* cache ald */
  149. void *util_ldap_cache;
  150. long connectionTimeout;
  151. struct timeval *opTimeout;
  152. int debug_level; /* SDK debug level */
  153. apr_interval_time_t connection_pool_ttl;
  154. int retries; /* number of retries for failed bind/search/compare */
  155. apr_interval_time_t retry_delay; /* delay between retries of failed bind/search/compare */
  156. } util_ldap_state_t;
  157. /* Used to store arrays of attribute labels/values. */
  158. struct mod_auth_ldap_groupattr_entry_t {
  159. char *name;
  160. };
  161. /**
  162. * Open a connection to an LDAP server
  163. * @param ldc A structure containing the expanded details of the server
  164. * to connect to. The handle to the LDAP connection is returned
  165. * as ldc->ldap.
  166. * @tip This function connects to the LDAP server and binds. It does not
  167. * connect if already connected (ldc->ldap != NULL). Does not bind
  168. * if already bound.
  169. * @return If successful LDAP_SUCCESS is returned.
  170. * @fn int util_ldap_connection_open(request_rec *r,
  171. * util_ldap_connection_t *ldc)
  172. */
  173. APR_DECLARE_OPTIONAL_FN(int,uldap_connection_open,(request_rec *r,
  174. util_ldap_connection_t *ldc));
  175. /**
  176. * Close a connection to an LDAP server
  177. * @param ldc A structure containing the expanded details of the server
  178. * that was connected.
  179. * @tip This function unbinds from the LDAP server, and clears ldc->ldap.
  180. * It is possible to rebind to this server again using the same ldc
  181. * structure, using apr_ldap_open_connection().
  182. * @fn util_ldap_close_connection(util_ldap_connection_t *ldc)
  183. */
  184. APR_DECLARE_OPTIONAL_FN(void,uldap_connection_close,(util_ldap_connection_t *ldc));
  185. /**
  186. * Unbind a connection to an LDAP server
  187. * @param ldc A structure containing the expanded details of the server
  188. * that was connected.
  189. * @tip This function unbinds the LDAP connection, and disconnects from
  190. * the server. It is used during error conditions, to bring the LDAP
  191. * connection back to a known state.
  192. * @fn apr_status_t util_ldap_connection_unbind(util_ldap_connection_t *ldc)
  193. */
  194. APR_DECLARE_OPTIONAL_FN(apr_status_t,uldap_connection_unbind,(void *param));
  195. /**
  196. * Find a connection in a list of connections
  197. * @param r The request record
  198. * @param host The hostname to connect to (multiple hosts space separated)
  199. * @param port The port to connect to
  200. * @param binddn The DN to bind with
  201. * @param bindpw The password to bind with
  202. * @param deref The dereferencing behavior
  203. * @param secure use SSL on the connection
  204. * @tip Once a connection is found and returned, a lock will be acquired to
  205. * lock that particular connection, so that another thread does not try and
  206. * use this connection while it is busy. Once you are finished with a connection,
  207. * apr_ldap_connection_close() must be called to release this connection.
  208. * @fn util_ldap_connection_t *util_ldap_connection_find(request_rec *r, const char *host, int port,
  209. * const char *binddn, const char *bindpw, deref_options deref,
  210. * int netscapessl, int starttls)
  211. */
  212. APR_DECLARE_OPTIONAL_FN(util_ldap_connection_t *,uldap_connection_find,(request_rec *r, const char *host, int port,
  213. const char *binddn, const char *bindpw, deref_options deref,
  214. int secure));
  215. /**
  216. * Compare two DNs for sameness
  217. * @param r The request record
  218. * @param ldc The LDAP connection being used.
  219. * @param url The URL of the LDAP connection - used for deciding which cache to use.
  220. * @param dn The first DN to compare.
  221. * @param reqdn The DN to compare the first DN to.
  222. * @param compare_dn_on_server Flag to determine whether the DNs should be checked using
  223. * LDAP calls or with a direct string comparison. A direct
  224. * string comparison is faster, but not as accurate - false
  225. * negative comparisons are possible.
  226. * @tip Two DNs can be equal and still fail a string comparison. Eg "dc=example,dc=com"
  227. * and "dc=example, dc=com". Use the compare_dn_on_server unless there are serious
  228. * performance issues.
  229. * @fn int util_ldap_cache_comparedn(request_rec *r, util_ldap_connection_t *ldc,
  230. * const char *url, const char *dn, const char *reqdn,
  231. * int compare_dn_on_server)
  232. */
  233. APR_DECLARE_OPTIONAL_FN(int,uldap_cache_comparedn,(request_rec *r, util_ldap_connection_t *ldc,
  234. const char *url, const char *dn, const char *reqdn,
  235. int compare_dn_on_server));
  236. /**
  237. * A generic LDAP compare function
  238. * @param r The request record
  239. * @param ldc The LDAP connection being used.
  240. * @param url The URL of the LDAP connection - used for deciding which cache to use.
  241. * @param dn The DN of the object in which we do the compare.
  242. * @param attrib The attribute within the object we are comparing for.
  243. * @param value The value of the attribute we are trying to compare for.
  244. * @tip Use this function to determine whether an attribute/value pair exists within an
  245. * object. Typically this would be used to determine LDAP top-level group
  246. * membership.
  247. * @fn int util_ldap_cache_compare(request_rec *r, util_ldap_connection_t *ldc,
  248. * const char *url, const char *dn, const char *attrib, const char *value)
  249. */
  250. APR_DECLARE_OPTIONAL_FN(int,uldap_cache_compare,(request_rec *r, util_ldap_connection_t *ldc,
  251. const char *url, const char *dn, const char *attrib, const char *value));
  252. /**
  253. * An LDAP function that checks if the specified user is a member of a subgroup.
  254. * @param r The request record
  255. * @param ldc The LDAP connection being used.
  256. * @param url The URL of the LDAP connection - used for deciding which cache to use.
  257. * @param dn The DN of the object in which we find subgroups to search within.
  258. * @param attrib The attribute within group objects that identify users.
  259. * @param value The user attribute value we are trying to compare for.
  260. * @param subgroupAttrs The attributes within group objects that identify subgroups.
  261. * Array of strings.
  262. * @param subgroupclasses The objectClass values used to identify groups (and
  263. * subgroups). apr_array_header_t *.
  264. * @param cur_subgroup_depth Current recursive depth during subgroup processing.
  265. * @param max_subgroup_depth Maximum depth of recursion allowed during subgroup
  266. * processing.
  267. * @tip Use this function to determine whether an attribute/value pair exists within a
  268. * starting group object or one of its nested subgroups. Typically this would be
  269. * used to determine LDAP nested group membership.
  270. * @deffunc int util_ldap_cache_check_subgroups(request_rec *r, util_ldap_connection_t
  271. * *ldc, const char *url, const char *dn,
  272. * const char *attrib, const char value,
  273. * char **subgroupAttrs, apr_array_header_t
  274. * *subgroupclasses, int cur_subgroup_depth, int
  275. * max_subgroup_depth )
  276. */
  277. APR_DECLARE_OPTIONAL_FN(int,uldap_cache_check_subgroups,(request_rec *r, util_ldap_connection_t *ldc,
  278. const char *url, const char *dn, const char *attrib, const char *value,
  279. char **subgroupAttrs, apr_array_header_t *subgroupclasses,
  280. int cur_subgroup_depth, int max_subgroup_depth));
  281. /**
  282. * Checks a username/password combination by binding to the LDAP server
  283. * @param r The request record
  284. * @param ldc The LDAP connection being used.
  285. * @param url The URL of the LDAP connection - used for deciding which cache to use.
  286. * @param basedn The Base DN to search for the user in.
  287. * @param scope LDAP scope of the search.
  288. * @param attrs LDAP attributes to return in search.
  289. * @param filter The user to search for in the form of an LDAP filter. This filter must return
  290. * exactly one user for the check to be successful.
  291. * @param bindpw The user password to bind as.
  292. * @param binddn The DN of the user will be returned in this variable.
  293. * @param retvals The values corresponding to the attributes requested in the attrs array.
  294. * @tip The filter supplied will be searched for. If a single entry is returned, an attempt
  295. * is made to bind as that user. If this bind succeeds, the user is not validated.
  296. * @fn int util_ldap_cache_checkuserid(request_rec *r, util_ldap_connection_t *ldc,
  297. * char *url, const char *basedn, int scope, char **attrs,
  298. * char *filter, char *bindpw, char **binddn, char ***retvals)
  299. */
  300. APR_DECLARE_OPTIONAL_FN(int,uldap_cache_checkuserid,(request_rec *r, util_ldap_connection_t *ldc,
  301. const char *url, const char *basedn, int scope, char **attrs,
  302. const char *filter, const char *bindpw, const char **binddn, const char ***retvals));
  303. /**
  304. * Searches for a specified user object in an LDAP directory
  305. * @param r The request record
  306. * @param ldc The LDAP connection being used.
  307. * @param url The URL of the LDAP connection - used for deciding which cache to use.
  308. * @param basedn The Base DN to search for the user in.
  309. * @param scope LDAP scope of the search.
  310. * @param attrs LDAP attributes to return in search.
  311. * @param filter The user to search for in the form of an LDAP filter. This filter must return
  312. * exactly one user for the check to be successful.
  313. * @param binddn The DN of the user will be returned in this variable.
  314. * @param retvals The values corresponding to the attributes requested in the attrs array.
  315. * @tip The filter supplied will be searched for. If a single entry is returned, an attempt
  316. * is made to bind as that user. If this bind succeeds, the user is not validated.
  317. * @fn int util_ldap_cache_getuserdn(request_rec *r, util_ldap_connection_t *ldc,
  318. * char *url, const char *basedn, int scope, char **attrs,
  319. * char *filter, char **binddn, char ***retvals)
  320. */
  321. APR_DECLARE_OPTIONAL_FN(int,uldap_cache_getuserdn,(request_rec *r, util_ldap_connection_t *ldc,
  322. const char *url, const char *basedn, int scope, char **attrs,
  323. const char *filter, const char **binddn, const char ***retvals));
  324. /**
  325. * Checks if SSL support is available in mod_ldap
  326. * @fn int util_ldap_ssl_supported(request_rec *r)
  327. */
  328. APR_DECLARE_OPTIONAL_FN(int,uldap_ssl_supported,(request_rec *r));
  329. /* from apr_ldap_cache.c */
  330. /**
  331. * Init the LDAP cache
  332. * @param pool The pool to use to initialise the cache
  333. * @param reqsize The size of the shared memory segment to request. A size
  334. * of zero requests the max size possible from
  335. * apr_shmem_init()
  336. * @fn void util_ldap_cache_init(apr_pool_t *p, util_ldap_state_t *st)
  337. * @return The status code returned is the status code of the
  338. * apr_smmem_init() call. Regardless of the status, the cache
  339. * will be set up at least for in-process or in-thread operation.
  340. */
  341. apr_status_t util_ldap_cache_init(apr_pool_t *pool, util_ldap_state_t *st);
  342. /* from apr_ldap_cache_mgr.c */
  343. /**
  344. * Display formatted stats for cache
  345. * @param The pool to allocate the returned string from
  346. * @tip This function returns a string allocated from the provided pool that describes
  347. * various stats about the cache.
  348. * @fn char *util_ald_cache_display(apr_pool_t *pool, util_ldap_state_t *st)
  349. */
  350. char *util_ald_cache_display(request_rec *r, util_ldap_state_t *st);
  351. #ifdef __cplusplus
  352. }
  353. #endif
  354. #endif /* APR_HAS_LDAP */
  355. #endif /* UTIL_LDAP_H */