]> granicus.if.org Git - apache/blob - modules/aaa/mod_authnz_ldap.c
PR51163: Resolve crashes when LDAP is used for authorization-only
[apache] / modules / aaa / mod_authnz_ldap.c
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 #include "ap_provider.h"
18 #include "httpd.h"
19 #include "http_config.h"
20 #include "ap_provider.h"
21 #include "http_core.h"
22 #include "http_log.h"
23 #include "http_protocol.h"
24 #include "http_request.h"
25 #include "util_ldap.h"
26
27 #include "mod_auth.h"
28
29 #include "apr_strings.h"
30 #include "apr_xlate.h"
31 #define APR_WANT_STRFUNC
32 #include "apr_want.h"
33 #include "apr_lib.h"
34
35 #include <ctype.h>
36
37 #if !APR_HAS_LDAP
38 #error mod_authnz_ldap requires APR-util to have LDAP support built in. To fix add --with-ldap to ./configure.
39 #endif
40
41 static char *default_attributes[3] = { "member", "uniqueMember", NULL };
42
43 typedef struct {
44     apr_pool_t *pool;               /* Pool that this config is allocated from */
45 #if APR_HAS_THREADS
46     apr_thread_mutex_t *lock;       /* Lock for this config */
47 #endif
48
49     /* These parameters are all derived from the AuthLDAPURL directive */
50     char *url;                      /* String representation of the URL */
51
52     char *host;                     /* Name of the LDAP server (or space separated list) */
53     int port;                       /* Port of the LDAP server */
54     char *basedn;                   /* Base DN to do all searches from */
55     char *attribute;                /* Attribute to search for */
56     char **attributes;              /* Array of all the attributes to return */
57     int scope;                      /* Scope of the search */
58     char *filter;                   /* Filter to further limit the search  */
59     deref_options deref;            /* how to handle alias dereferening */
60     char *binddn;                   /* DN to bind to server (can be NULL) */
61     char *bindpw;                   /* Password to bind to server (can be NULL) */
62     int bind_authoritative;         /* If true, will return errors when bind fails */
63
64     int user_is_dn;                 /* If true, connection->user is DN instead of userid */
65     char *remote_user_attribute;    /* If set, connection->user is this attribute instead of userid */
66     int compare_dn_on_server;       /* If true, will use server to do DN compare */
67
68     int have_ldap_url;              /* Set if we have found an LDAP url */
69
70     apr_array_header_t *groupattr;  /* List of Group attributes identifying user members. Default:"member uniqueMember" */
71     int group_attrib_is_dn;         /* If true, the group attribute is the DN, otherwise,
72                                         it's the exact string passed by the HTTP client */
73     char **sgAttributes;            /* Array of strings constructed (post-config) from subgroupattrs. Last entry is NULL. */
74     apr_array_header_t *subgroupclasses; /* List of object classes of sub-groups. Default:"groupOfNames groupOfUniqueNames" */
75     int maxNestingDepth;            /* Maximum recursive nesting depth permitted during subgroup processing. Default: 10 */
76
77     int secure;                     /* True if SSL connections are requested */
78     char *authz_prefix;             /* Prefix for environment variables added during authz */
79     int initial_bind_as_user;               /* true if we should try to bind (to lookup DN) directly with the basic auth username */
80     ap_regex_t *bind_regex;         /* basic auth -> bind'able username regex */
81     const char *bind_subst;         /* basic auth -> bind'able username substitution */
82     int search_as_user;             /* true if authz searches should be done with the users credentials (when we did authn) */
83     int compare_as_user;            /* true if authz compares should be done with the users credentials (when we did authn) */
84 } authn_ldap_config_t;
85
86 typedef struct {
87     char *dn;                       /* The saved dn from a successful search */
88     char *user;                     /* The username provided by the client */
89     const char **vals;              /* The additional values pulled during the DN search*/
90     char *password;                 /* if this module successfully authenticates, the basic auth password, else null */
91 } authn_ldap_request_t;
92
93 enum auth_ldap_phase {
94     LDAP_AUTHN, LDAP_AUTHZ
95 };
96
97 enum auth_ldap_optype {
98     LDAP_SEARCH, LDAP_COMPARE, LDAP_COMPARE_AND_SEARCH /* nested groups */
99 };
100
101 /* maximum group elements supported */
102 #define GROUPATTR_MAX_ELTS 10
103
104 module AP_MODULE_DECLARE_DATA authnz_ldap_module;
105
106 static APR_OPTIONAL_FN_TYPE(uldap_connection_close) *util_ldap_connection_close;
107 static APR_OPTIONAL_FN_TYPE(uldap_connection_find) *util_ldap_connection_find;
108 static APR_OPTIONAL_FN_TYPE(uldap_cache_comparedn) *util_ldap_cache_comparedn;
109 static APR_OPTIONAL_FN_TYPE(uldap_cache_compare) *util_ldap_cache_compare;
110 static APR_OPTIONAL_FN_TYPE(uldap_cache_check_subgroups) *util_ldap_cache_check_subgroups;
111 static APR_OPTIONAL_FN_TYPE(uldap_cache_checkuserid) *util_ldap_cache_checkuserid;
112 static APR_OPTIONAL_FN_TYPE(uldap_cache_getuserdn) *util_ldap_cache_getuserdn;
113 static APR_OPTIONAL_FN_TYPE(uldap_ssl_supported) *util_ldap_ssl_supported;
114
115 static apr_hash_t *charset_conversions = NULL;
116 static char *to_charset = NULL;           /* UTF-8 identifier derived from the charset.conv file */
117
118
119 /* Derive a code page ID give a language name or ID */
120 static char* derive_codepage_from_lang (apr_pool_t *p, char *language)
121 {
122     char *charset;
123
124     if (!language)          /* our default codepage */
125         return apr_pstrdup(p, "ISO-8859-1");
126
127     charset = (char*) apr_hash_get(charset_conversions, language, APR_HASH_KEY_STRING);
128
129     if (!charset) {
130         language[2] = '\0';
131         charset = (char*) apr_hash_get(charset_conversions, language, APR_HASH_KEY_STRING);
132     }
133
134     if (charset) {
135         charset = apr_pstrdup(p, charset);
136     }
137
138     return charset;
139 }
140
141 static apr_xlate_t* get_conv_set (request_rec *r)
142 {
143     char *lang_line = (char*)apr_table_get(r->headers_in, "accept-language");
144     char *lang;
145     apr_xlate_t *convset;
146
147     if (lang_line) {
148         lang_line = apr_pstrdup(r->pool, lang_line);
149         for (lang = lang_line;*lang;lang++) {
150             if ((*lang == ',') || (*lang == ';')) {
151                 *lang = '\0';
152                 break;
153             }
154         }
155         lang = derive_codepage_from_lang(r->pool, lang_line);
156
157         if (lang && (apr_xlate_open(&convset, to_charset, lang, r->pool) == APR_SUCCESS)) {
158             return convset;
159         }
160     }
161
162     return NULL;
163 }
164
165
166 static const char* authn_ldap_xlate_password(request_rec *r,
167                                              const char* sent_password)
168 {
169     apr_xlate_t *convset = NULL;
170     apr_size_t inbytes;
171     apr_size_t outbytes;
172     char *outbuf;
173
174     if (charset_conversions && (convset = get_conv_set(r)) ) {
175         inbytes = strlen(sent_password);
176         outbytes = (inbytes+1)*3;
177         outbuf = apr_pcalloc(r->pool, outbytes);
178
179         /* Convert the password to UTF-8. */
180         if (apr_xlate_conv_buffer(convset, sent_password, &inbytes, outbuf,
181                                   &outbytes) == APR_SUCCESS)
182             return outbuf;
183     }
184
185     return sent_password;
186 }
187
188
189 /*
190  * Build the search filter, or at least as much of the search filter that
191  * will fit in the buffer. We don't worry about the buffer not being able
192  * to hold the entire filter. If the buffer wasn't big enough to hold the
193  * filter, ldap_search_s will complain, but the only situation where this
194  * is likely to happen is if the client sent a really, really long
195  * username, most likely as part of an attack.
196  *
197  * The search filter consists of the filter provided with the URL,
198  * combined with a filter made up of the attribute provided with the URL,
199  * and the actual username passed by the HTTP client. For example, assume
200  * that the LDAP URL is
201  *
202  *   ldap://ldap.airius.com/ou=People, o=Airius?uid??(posixid=*)
203  *
204  * Further, assume that the userid passed by the client was `userj'.  The
205  * search filter will be (&(posixid=*)(uid=userj)).
206  */
207 #define FILTER_LENGTH MAX_STRING_LEN
208 static void authn_ldap_build_filter(char *filtbuf,
209                              request_rec *r,
210                              const char* sent_user,
211                              const char* sent_filter,
212                              authn_ldap_config_t *sec)
213 {
214     char *p, *q, *filtbuf_end;
215     char *user, *filter;
216     apr_xlate_t *convset = NULL;
217     apr_size_t inbytes;
218     apr_size_t outbytes;
219     char *outbuf;
220
221     if (sent_user != NULL) {
222         user = apr_pstrdup (r->pool, sent_user);
223     }
224     else
225         return;
226
227     if (sent_filter != NULL) {
228         filter = apr_pstrdup (r->pool, sent_filter);
229     }
230     else
231         filter = sec->filter;
232
233     if (charset_conversions) {
234         convset = get_conv_set(r);
235     }
236
237     if (convset) {
238         inbytes = strlen(user);
239         outbytes = (inbytes+1)*3;
240         outbuf = apr_pcalloc(r->pool, outbytes);
241
242         /* Convert the user name to UTF-8.  This is only valid for LDAP v3 */
243         if (apr_xlate_conv_buffer(convset, user, &inbytes, outbuf, &outbytes) == APR_SUCCESS) {
244             user = apr_pstrdup(r->pool, outbuf);
245         }
246     }
247
248     /*
249      * Create the first part of the filter, which consists of the
250      * config-supplied portions.
251      */
252     apr_snprintf(filtbuf, FILTER_LENGTH, "(&(%s)(%s=", filter, sec->attribute);
253
254     /*
255      * Now add the client-supplied username to the filter, ensuring that any
256      * LDAP filter metachars are escaped.
257      */
258     filtbuf_end = filtbuf + FILTER_LENGTH - 1;
259 #if APR_HAS_MICROSOFT_LDAPSDK
260     for (p = user, q=filtbuf + strlen(filtbuf);
261          *p && q < filtbuf_end; ) {
262         if (strchr("*()\\", *p) != NULL) {
263             if ( q + 3 >= filtbuf_end)
264               break;  /* Don't write part of escape sequence if we can't write all of it */
265             *q++ = '\\';
266             switch ( *p++ )
267             {
268               case '*':
269                 *q++ = '2';
270                 *q++ = 'a';
271                 break;
272               case '(':
273                 *q++ = '2';
274                 *q++ = '8';
275                 break;
276               case ')':
277                 *q++ = '2';
278                 *q++ = '9';
279                 break;
280               case '\\':
281                 *q++ = '5';
282                 *q++ = 'c';
283                 break;
284                         }
285         }
286         else
287             *q++ = *p++;
288     }
289 #else
290     for (p = user, q=filtbuf + strlen(filtbuf);
291          *p && q < filtbuf_end; *q++ = *p++) {
292         if (strchr("*()\\", *p) != NULL) {
293             *q++ = '\\';
294             if (q >= filtbuf_end) {
295               break;
296             }
297         }
298     }
299 #endif
300     *q = '\0';
301
302     /*
303      * Append the closing parens of the filter, unless doing so would
304      * overrun the buffer.
305      */
306     if (q + 2 <= filtbuf_end)
307         strcat(filtbuf, "))");
308 }
309
310 static void *create_authnz_ldap_dir_config(apr_pool_t *p, char *d)
311 {
312     authn_ldap_config_t *sec =
313         (authn_ldap_config_t *)apr_pcalloc(p, sizeof(authn_ldap_config_t));
314
315     sec->pool = p;
316 #if APR_HAS_THREADS
317     apr_thread_mutex_create(&sec->lock, APR_THREAD_MUTEX_DEFAULT, p);
318 #endif
319 /*
320     sec->authz_enabled = 1;
321 */
322     sec->groupattr = apr_array_make(p, GROUPATTR_MAX_ELTS,
323                                     sizeof(struct mod_auth_ldap_groupattr_entry_t));
324     sec->subgroupclasses = apr_array_make(p, GROUPATTR_MAX_ELTS,
325                                     sizeof(struct mod_auth_ldap_groupattr_entry_t));
326
327     sec->have_ldap_url = 0;
328     sec->url = "";
329     sec->host = NULL;
330     sec->binddn = NULL;
331     sec->bindpw = NULL;
332     sec->bind_authoritative = 1;
333     sec->deref = always;
334     sec->group_attrib_is_dn = 1;
335     sec->secure = -1;   /*Initialize to unset*/
336     sec->maxNestingDepth = 10;
337     sec->sgAttributes = apr_pcalloc(p, sizeof (char *) * GROUPATTR_MAX_ELTS + 1);
338
339     sec->user_is_dn = 0;
340     sec->remote_user_attribute = NULL;
341     sec->compare_dn_on_server = 0;
342
343     sec->authz_prefix = AUTHZ_PREFIX;
344
345     return sec;
346 }
347
348 static apr_status_t authnz_ldap_cleanup_connection_close(void *param)
349 {
350     util_ldap_connection_t *ldc = param;
351     util_ldap_connection_close(ldc);
352     return APR_SUCCESS;
353 }
354
355 static int set_request_vars(request_rec *r, enum auth_ldap_phase phase) { 
356     char *prefix = NULL;
357     int prefix_len;
358     int remote_user_attribute_set = 0;
359     authn_ldap_request_t *req =
360         (authn_ldap_request_t *)ap_get_module_config(r->request_config, &authnz_ldap_module);
361     authn_ldap_config_t *sec =
362         (authn_ldap_config_t *)ap_get_module_config(r->per_dir_config, &authnz_ldap_module);
363     const char **vals = req->vals;
364
365     prefix = (phase == LDAP_AUTHN) ? AUTHN_PREFIX : sec->authz_prefix;
366     prefix_len = strlen(prefix);
367
368     if (sec->attributes && vals) {
369         apr_table_t *e = r->subprocess_env;
370         int i = 0;
371         while (sec->attributes[i]) {
372             char *str = apr_pstrcat(r->pool, prefix, sec->attributes[i], NULL);
373             int j = prefix_len;
374             while (str[j]) {
375                 str[j] = apr_toupper(str[j]);
376                 j++;
377             }
378             apr_table_setn(e, str, vals[i] ? vals[i] : "");
379
380             /* handle remote_user_attribute, if set */
381             if ((phase == LDAP_AUTHN) &&
382                 sec->remote_user_attribute && 
383                 !strcmp(sec->remote_user_attribute, sec->attributes[i])) {
384                 r->user = (char *)apr_pstrdup(r->pool, vals[i]);
385                 remote_user_attribute_set = 1;
386             }
387             i++;
388         }
389     }
390     return remote_user_attribute_set;
391 }
392
393 static const char *ldap_determine_binddn(request_rec *r, const char *user) {
394     authn_ldap_config_t *sec =
395         (authn_ldap_config_t *)ap_get_module_config(r->per_dir_config, &authnz_ldap_module);
396     const char *result = user;
397     ap_regmatch_t regm[AP_MAX_REG_MATCH];
398
399     if (NULL == user || NULL == sec || !sec->bind_regex || !sec->bind_subst) {
400         return result;
401     }
402
403     if (!ap_regexec(sec->bind_regex, user, AP_MAX_REG_MATCH, regm, 0)) {
404         char *substituted = ap_pregsub(r->pool, sec->bind_subst, user, AP_MAX_REG_MATCH, regm);
405         if (NULL != substituted) {
406             result = substituted;
407         }
408     }
409
410     apr_table_set(r->subprocess_env, "LDAP_BINDASUSER", result);
411
412     return result;
413 }
414
415
416 /* Some LDAP servers restrict who can search or compare, and the hard-coded ID
417  * might be good for the DN lookup but not for later operations.
418  */
419 static util_ldap_connection_t *get_connection_for_authz(request_rec *r, enum auth_ldap_optype type) {
420     authn_ldap_request_t *req =
421         (authn_ldap_request_t *)ap_get_module_config(r->request_config, &authnz_ldap_module);
422     authn_ldap_config_t *sec =
423         (authn_ldap_config_t *)ap_get_module_config(r->per_dir_config, &authnz_ldap_module);
424
425     char *binddn = sec->binddn;
426     char *bindpw = sec->bindpw;
427
428     /* If the per-request config isn't set, we didn't authenticate this user, and leave the default credentials */
429     if (req && req->password &&
430          ((type == LDAP_SEARCH && sec->search_as_user)    ||
431           (type == LDAP_COMPARE && sec->compare_as_user)  ||
432           (type == LDAP_COMPARE_AND_SEARCH && sec->compare_as_user && sec->search_as_user))){
433             binddn = req->dn;
434             bindpw = req->password;
435     }
436
437     return util_ldap_connection_find(r, sec->host, sec->port,
438                                      binddn, bindpw,
439                                      sec->deref, sec->secure);
440 }
441 /*
442  * Authentication Phase
443  * --------------------
444  *
445  * This phase authenticates the credentials the user has sent with
446  * the request (ie the username and password are checked). This is done
447  * by making an attempt to bind to the LDAP server using this user's
448  * DN and the supplied password.
449  *
450  */
451 static authn_status authn_ldap_check_password(request_rec *r, const char *user,
452                                               const char *password)
453 {
454     int failures = 0;
455     char filtbuf[FILTER_LENGTH];
456     authn_ldap_config_t *sec =
457         (authn_ldap_config_t *)ap_get_module_config(r->per_dir_config, &authnz_ldap_module);
458
459     util_ldap_connection_t *ldc = NULL;
460     int result = 0;
461     int remote_user_attribute_set = 0;
462     const char *dn = NULL;
463     const char *utfpassword;
464
465     authn_ldap_request_t *req =
466         (authn_ldap_request_t *)apr_pcalloc(r->pool, sizeof(authn_ldap_request_t));
467     ap_set_module_config(r->request_config, &authnz_ldap_module, req);
468
469 /*
470     if (!sec->enabled) {
471         return AUTH_USER_NOT_FOUND;
472     }
473 */
474
475     /*
476      * Basic sanity checks before any LDAP operations even happen.
477      */
478     if (!sec->have_ldap_url) {
479         return AUTH_GENERAL_ERROR;
480     }
481
482 start_over:
483
484     /* There is a good AuthLDAPURL, right? */
485     if (sec->host) {
486         const char *binddn = sec->binddn;
487         const char *bindpw = sec->bindpw;
488         if (sec->initial_bind_as_user) {
489             bindpw = password;
490             binddn = ldap_determine_binddn(r, user);
491         }
492
493         ldc = util_ldap_connection_find(r, sec->host, sec->port,
494                                        binddn, bindpw,
495                                        sec->deref, sec->secure);
496     }
497     else {
498         ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r,
499                       "auth_ldap authenticate: no sec->host - weird...?");
500         return AUTH_GENERAL_ERROR;
501     }
502
503     ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
504                   "auth_ldap authenticate: using URL %s", sec->url);
505
506     /* Get the password that the client sent */
507     if (password == NULL) {
508         ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
509                       "auth_ldap authenticate: no password specified");
510         util_ldap_connection_close(ldc);
511         return AUTH_GENERAL_ERROR;
512     }
513
514     if (user == NULL) {
515         ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
516                       "auth_ldap authenticate: no user specified");
517         util_ldap_connection_close(ldc);
518         return AUTH_GENERAL_ERROR;
519     }
520
521     /* build the username filter */
522     authn_ldap_build_filter(filtbuf, r, user, NULL, sec);
523
524     ap_log_rerror(APLOG_MARK, APLOG_TRACE1, 0, r,
525                       "auth_ldap authenticate: final authn filter is %s", filtbuf);
526
527     /* convert password to utf-8 */
528     utfpassword = authn_ldap_xlate_password(r, password);
529
530     /* do the user search */
531     result = util_ldap_cache_checkuserid(r, ldc, sec->url, sec->basedn, sec->scope,
532                                          sec->attributes, filtbuf, utfpassword,
533                                          &dn, &(req->vals));
534     util_ldap_connection_close(ldc);
535
536     /* sanity check - if server is down, retry it up to 5 times */
537     if (AP_LDAP_IS_SERVER_DOWN(result)) {
538         if (failures++ <= 5) {
539             goto start_over;
540         }
541     }
542
543     /* handle bind failure */
544     if (result != LDAP_SUCCESS) {
545         if (!sec->bind_authoritative) {
546            ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
547                       "auth_ldap authenticate: user %s authentication failed; "
548                       "URI %s [%s][%s] (not authoritative)",
549                       user, r->uri, ldc->reason, ldap_err2string(result));
550            return AUTH_USER_NOT_FOUND;
551         }
552
553         ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r,
554                       "auth_ldap authenticate: "
555                       "user %s authentication failed; URI %s [%s][%s]",
556                       user, r->uri, ldc->reason, ldap_err2string(result));
557
558         return (LDAP_NO_SUCH_OBJECT == result) ? AUTH_USER_NOT_FOUND
559 #ifdef LDAP_SECURITY_ERROR
560                  : (LDAP_SECURITY_ERROR(result)) ? AUTH_DENIED
561 #else
562                  : (LDAP_INAPPROPRIATE_AUTH == result) ? AUTH_DENIED
563                  : (LDAP_INVALID_CREDENTIALS == result) ? AUTH_DENIED
564 #ifdef LDAP_INSUFFICIENT_ACCESS
565                  : (LDAP_INSUFFICIENT_ACCESS == result) ? AUTH_DENIED
566 #endif
567 #ifdef LDAP_INSUFFICIENT_RIGHTS
568                  : (LDAP_INSUFFICIENT_RIGHTS == result) ? AUTH_DENIED
569 #endif
570 #endif
571                  : AUTH_GENERAL_ERROR;
572     }
573
574     /* mark the user and DN */
575     req->dn = apr_pstrdup(r->pool, dn);
576     req->user = apr_pstrdup(r->pool, user);
577     req->password = apr_pstrdup(r->pool, password);
578     if (sec->user_is_dn) {
579         r->user = req->dn;
580     }
581
582     /* add environment variables */
583     remote_user_attribute_set = set_request_vars(r, LDAP_AUTHN);
584
585     /* sanity check */
586     if (sec->remote_user_attribute && !remote_user_attribute_set) {
587         ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r,
588                   "auth_ldap authenticate: "
589                   "REMOTE_USER was to be set with attribute '%s', "
590                   "but this attribute was not requested for in the "
591                   "LDAP query for the user. REMOTE_USER will fall "
592                   "back to username or DN as appropriate.",
593                   sec->remote_user_attribute);
594     }
595
596     ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
597                   "auth_ldap authenticate: accepting %s", user);
598
599     return AUTH_GRANTED;
600 }
601
602 static authz_status ldapuser_check_authorization(request_rec *r,
603                                                  const char *require_args,
604                                                  const void *parsed_require_args)
605 {
606     int result = 0;
607     authn_ldap_request_t *req =
608         (authn_ldap_request_t *)ap_get_module_config(r->request_config, &authnz_ldap_module);
609     authn_ldap_config_t *sec =
610         (authn_ldap_config_t *)ap_get_module_config(r->per_dir_config, &authnz_ldap_module);
611
612     util_ldap_connection_t *ldc = NULL;
613
614     const char *t;
615     char *w;
616
617     char filtbuf[FILTER_LENGTH];
618     const char *dn = NULL;
619
620     if (!r->user) {
621         return AUTHZ_DENIED_NO_USER;
622     }
623
624     if (!sec->have_ldap_url) {
625         return AUTHZ_DENIED;
626     }
627
628     if (sec->host) {
629         ldc = get_connection_for_authz(r, LDAP_COMPARE);
630         apr_pool_cleanup_register(r->pool, ldc,
631                                   authnz_ldap_cleanup_connection_close,
632                                   apr_pool_cleanup_null);
633     }
634     else {
635         ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r,
636                       "auth_ldap authorize: no sec->host - weird...?");
637         return AUTHZ_DENIED;
638     }
639
640     /*
641      * If we have been authenticated by some other module than mod_authnz_ldap,
642      * the req structure needed for authorization needs to be created
643      * and populated with the userid and DN of the account in LDAP
644      */
645
646
647     if (!strlen(r->user)) {
648         ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r,
649             "ldap authorize: Userid is blank, AuthType=%s",
650             r->ap_auth_type);
651     }
652
653     if(!req) {
654         ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
655             "ldap authorize: Creating LDAP req structure");
656
657         req = (authn_ldap_request_t *)apr_pcalloc(r->pool,
658             sizeof(authn_ldap_request_t));
659
660         /* Build the username filter */
661         authn_ldap_build_filter(filtbuf, r, r->user, NULL, sec);
662
663         /* Search for the user DN */
664         result = util_ldap_cache_getuserdn(r, ldc, sec->url, sec->basedn,
665              sec->scope, sec->attributes, filtbuf, &dn, &(req->vals));
666
667         /* Search failed, log error and return failure */
668         if(result != LDAP_SUCCESS) {
669             ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
670                 "auth_ldap authorise: User DN not found, %s", ldc->reason);
671             return AUTHZ_DENIED;
672         }
673
674         ap_set_module_config(r->request_config, &authnz_ldap_module, req);
675         req->dn = apr_pstrdup(r->pool, dn);
676         req->user = r->user;
677
678     }
679
680     if (req->dn == NULL || strlen(req->dn) == 0) {
681         ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
682                       "auth_ldap authorize: require user: user's DN has not "
683                       "been defined; failing authorization");
684         return AUTHZ_DENIED;
685     }
686
687     /*
688      * First do a whole-line compare, in case it's something like
689      *   require user Babs Jensen
690      */
691     result = util_ldap_cache_compare(r, ldc, sec->url, req->dn, sec->attribute, require_args);
692     switch(result) {
693         case LDAP_COMPARE_TRUE: {
694             ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
695                           "auth_ldap authorize: require user: authorization "
696                           "successful");
697             set_request_vars(r, LDAP_AUTHZ);
698             return AUTHZ_GRANTED;
699         }
700         default: {
701             ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
702                           "auth_ldap authorize: require user: "
703                           "authorization failed [%s][%s]",
704                           ldc->reason, ldap_err2string(result));
705         }
706     }
707
708     /*
709      * Now break apart the line and compare each word on it
710      */
711     t = require_args;
712     while ((w = ap_getword_conf(r->pool, &t)) && w[0]) {
713         result = util_ldap_cache_compare(r, ldc, sec->url, req->dn, sec->attribute, w);
714         switch(result) {
715             case LDAP_COMPARE_TRUE: {
716                 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
717                               "auth_ldap authorize: "
718                               "require user: authorization successful");
719                 set_request_vars(r, LDAP_AUTHZ);
720                 return AUTHZ_GRANTED;
721             }
722             default: {
723                 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
724                               "auth_ldap authorize: "
725                               "require user: authorization failed [%s][%s]",
726                               ldc->reason, ldap_err2string(result));
727             }
728         }
729     }
730
731     ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
732                   "auth_ldap authorize user: authorization denied for "
733                   "user %s to %s",
734                   r->user, r->uri);
735
736     return AUTHZ_DENIED;
737 }
738
739 static authz_status ldapgroup_check_authorization(request_rec *r,
740                                                   const char *require_args,
741                                                   const void *parsed_require_args)
742 {
743     int result = 0;
744     authn_ldap_request_t *req =
745         (authn_ldap_request_t *)ap_get_module_config(r->request_config, &authnz_ldap_module);
746     authn_ldap_config_t *sec =
747         (authn_ldap_config_t *)ap_get_module_config(r->per_dir_config, &authnz_ldap_module);
748
749     util_ldap_connection_t *ldc = NULL;
750
751     const char *t;
752
753     char filtbuf[FILTER_LENGTH];
754     const char *dn = NULL;
755     struct mod_auth_ldap_groupattr_entry_t *ent;
756     int i;
757
758     if (!r->user) {
759         return AUTHZ_DENIED_NO_USER;
760     }
761
762     if (!sec->have_ldap_url) {
763         return AUTHZ_DENIED;
764     }
765
766     if (sec->host) {
767         ldc = get_connection_for_authz(r, LDAP_COMPARE); /* for the top-level group only */
768         apr_pool_cleanup_register(r->pool, ldc,
769                                   authnz_ldap_cleanup_connection_close,
770                                   apr_pool_cleanup_null);
771     }
772     else {
773         ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r,
774                       "auth_ldap authorize: no sec->host - weird...?");
775         return AUTHZ_DENIED;
776     }
777
778     /*
779      * If there are no elements in the group attribute array, the default should be
780      * member and uniquemember; populate the array now.
781      */
782     if (sec->groupattr->nelts == 0) {
783         struct mod_auth_ldap_groupattr_entry_t *grp;
784 #if APR_HAS_THREADS
785         apr_thread_mutex_lock(sec->lock);
786 #endif
787         grp = apr_array_push(sec->groupattr);
788         grp->name = "member";
789         grp = apr_array_push(sec->groupattr);
790         grp->name = "uniqueMember";
791 #if APR_HAS_THREADS
792         apr_thread_mutex_unlock(sec->lock);
793 #endif
794     }
795
796     /*
797      * If there are no elements in the sub group classes array, the default
798      * should be groupOfNames and groupOfUniqueNames; populate the array now.
799      */
800     if (sec->subgroupclasses->nelts == 0) {
801         struct mod_auth_ldap_groupattr_entry_t *grp;
802 #if APR_HAS_THREADS
803         apr_thread_mutex_lock(sec->lock);
804 #endif
805         grp = apr_array_push(sec->subgroupclasses);
806         grp->name = "groupOfNames";
807         grp = apr_array_push(sec->subgroupclasses);
808         grp->name = "groupOfUniqueNames";
809 #if APR_HAS_THREADS
810         apr_thread_mutex_unlock(sec->lock);
811 #endif
812     }
813
814     /*
815      * If we have been authenticated by some other module than mod_auth_ldap,
816      * the req structure needed for authorization needs to be created
817      * and populated with the userid and DN of the account in LDAP
818      */
819
820     if (!strlen(r->user)) {
821         ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r,
822             "ldap authorize: Userid is blank, AuthType=%s",
823             r->ap_auth_type);
824     }
825
826     if(!req) {
827         ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
828             "ldap authorize: Creating LDAP req structure");
829
830         req = (authn_ldap_request_t *)apr_pcalloc(r->pool,
831             sizeof(authn_ldap_request_t));
832         /* Build the username filter */
833         authn_ldap_build_filter(filtbuf, r, r->user, NULL, sec);
834
835         /* Search for the user DN */
836         result = util_ldap_cache_getuserdn(r, ldc, sec->url, sec->basedn,
837              sec->scope, sec->attributes, filtbuf, &dn, &(req->vals));
838
839         /* Search failed, log error and return failure */
840         if(result != LDAP_SUCCESS) {
841             ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
842                 "auth_ldap authorise: User DN not found, %s", ldc->reason);
843             return AUTHZ_DENIED;
844         }
845
846         ap_set_module_config(r->request_config, &authnz_ldap_module, req);
847         req->dn = apr_pstrdup(r->pool, dn);
848         req->user = r->user;
849     }
850
851     ent = (struct mod_auth_ldap_groupattr_entry_t *) sec->groupattr->elts;
852
853     if (sec->group_attrib_is_dn) {
854         if (req->dn == NULL || strlen(req->dn) == 0) {
855             ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
856                           "auth_ldap authorize: require group: user's DN has "
857                           "not been defined; failing authorization for user %s",
858                           r->user);
859             return AUTHZ_DENIED;
860         }
861     }
862     else {
863         if (req->user == NULL || strlen(req->user) == 0) {
864             /* We weren't called in the authentication phase, so we didn't have a
865              * chance to set the user field. Do so now. */
866             req->user = r->user;
867         }
868     }
869
870     t = require_args;
871
872     ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
873                   "auth_ldap authorize: require group: testing for group "
874                   "membership in \"%s\"",
875                   t);
876
877     for (i = 0; i < sec->groupattr->nelts; i++) {
878         ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
879                       "auth_ldap authorize: require group: testing for %s: "
880                       "%s (%s)",
881                       ent[i].name,
882                       sec->group_attrib_is_dn ? req->dn : req->user, t);
883
884         result = util_ldap_cache_compare(r, ldc, sec->url, t, ent[i].name,
885                              sec->group_attrib_is_dn ? req->dn : req->user);
886         switch(result) {
887             case LDAP_COMPARE_TRUE: {
888                 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
889                               "auth_ldap authorize: require group: "
890                               "authorization successful (attribute %s) "
891                               "[%s][%d - %s]",
892                               ent[i].name, ldc->reason, result,
893                               ldap_err2string(result));
894                 set_request_vars(r, LDAP_AUTHZ);
895                 return AUTHZ_GRANTED;
896             }
897             case LDAP_NO_SUCH_ATTRIBUTE: 
898             case LDAP_COMPARE_FALSE: {
899                 /* nested groups need searches and compares, so grab a new handle */
900                 authnz_ldap_cleanup_connection_close(ldc);
901                 apr_pool_cleanup_kill(r->pool, ldc,authnz_ldap_cleanup_connection_close);
902
903                 ldc = get_connection_for_authz(r, LDAP_COMPARE_AND_SEARCH);
904                 apr_pool_cleanup_register(r->pool, ldc,
905                                           authnz_ldap_cleanup_connection_close,
906                                           apr_pool_cleanup_null);
907
908                 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
909                                "auth_ldap authorise: require group \"%s\": "
910                                "failed [%s][%d - %s], checking sub-groups",
911                                t, ldc->reason, result, ldap_err2string(result));
912
913                 result = util_ldap_cache_check_subgroups(r, ldc, sec->url, t, ent[i].name,
914                                                          sec->group_attrib_is_dn ? req->dn : req->user,
915                                                          sec->sgAttributes[0] ? sec->sgAttributes : default_attributes,
916                                                          sec->subgroupclasses,
917                                                          0, sec->maxNestingDepth);
918                 if(result == LDAP_COMPARE_TRUE) {
919                     ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
920                                   "auth_ldap authorise: require group "
921                                   "(sub-group): authorisation successful "
922                                   "(attribute %s) [%s][%d - %s]",
923                                   ent[i].name, ldc->reason, result,
924                                   ldap_err2string(result));
925                     set_request_vars(r, LDAP_AUTHZ);
926                     return AUTHZ_GRANTED;
927                 }
928                 else {
929                     ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
930                                   "auth_ldap authorise: require group "
931                                   "(sub-group) \"%s\": authorisation failed "
932                                   "[%s][%d - %s]",
933                                   t, ldc->reason, result,
934                                   ldap_err2string(result));
935                 }
936                 break;
937             }
938             default: {
939                 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
940                               "auth_ldap authorize: require group \"%s\": "
941                               "authorization failed [%s][%d - %s]",
942                               t, ldc->reason, result, ldap_err2string(result));
943             }
944         }
945     }
946
947     ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
948                   "auth_ldap authorize group: authorization denied for "
949                   "user %s to %s",
950                   r->user, r->uri);
951
952     return AUTHZ_DENIED;
953 }
954
955 static authz_status ldapdn_check_authorization(request_rec *r,
956                                                const char *require_args,
957                                                const void *parsed_require_args)
958 {
959     int result = 0;
960     authn_ldap_request_t *req =
961         (authn_ldap_request_t *)ap_get_module_config(r->request_config, &authnz_ldap_module);
962     authn_ldap_config_t *sec =
963         (authn_ldap_config_t *)ap_get_module_config(r->per_dir_config, &authnz_ldap_module);
964
965     util_ldap_connection_t *ldc = NULL;
966
967     const char *t;
968
969     char filtbuf[FILTER_LENGTH];
970     const char *dn = NULL;
971
972     if (!r->user) {
973         return AUTHZ_DENIED_NO_USER;
974     }
975
976     if (!sec->have_ldap_url) {
977         return AUTHZ_DENIED;
978     }
979
980     if (sec->host) {
981         ldc = get_connection_for_authz(r, LDAP_SEARCH); /* _comparedn is a searche */
982         apr_pool_cleanup_register(r->pool, ldc,
983                                   authnz_ldap_cleanup_connection_close,
984                                   apr_pool_cleanup_null);
985     }
986     else {
987         ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r,
988                       "auth_ldap authorize: no sec->host - weird...?");
989         return AUTHZ_DENIED;
990     }
991
992     /*
993      * If we have been authenticated by some other module than mod_auth_ldap,
994      * the req structure needed for authorization needs to be created
995      * and populated with the userid and DN of the account in LDAP
996      */
997
998     if (!strlen(r->user)) {
999         ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r,
1000             "ldap authorize: Userid is blank, AuthType=%s",
1001             r->ap_auth_type);
1002     }
1003
1004     if(!req) {
1005         ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1006             "ldap authorize: Creating LDAP req structure");
1007
1008         req = (authn_ldap_request_t *)apr_pcalloc(r->pool,
1009             sizeof(authn_ldap_request_t));
1010         /* Build the username filter */
1011         authn_ldap_build_filter(filtbuf, r, r->user, NULL, sec);
1012
1013         /* Search for the user DN */
1014         result = util_ldap_cache_getuserdn(r, ldc, sec->url, sec->basedn,
1015              sec->scope, sec->attributes, filtbuf, &dn, &(req->vals));
1016
1017         /* Search failed, log error and return failure */
1018         if(result != LDAP_SUCCESS) {
1019             ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1020                 "auth_ldap authorise: User DN not found with filter %s: %s", filtbuf, ldc->reason);
1021             return AUTHZ_DENIED;
1022         }
1023
1024         ap_set_module_config(r->request_config, &authnz_ldap_module, req);
1025         req->dn = apr_pstrdup(r->pool, dn);
1026         req->user = r->user;
1027     }
1028
1029     t = require_args;
1030
1031     if (req->dn == NULL || strlen(req->dn) == 0) {
1032         ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1033                       "auth_ldap authorize: require dn: user's DN has not "
1034                       "been defined; failing authorization");
1035         return AUTHZ_DENIED;
1036     }
1037
1038     result = util_ldap_cache_comparedn(r, ldc, sec->url, req->dn, t, sec->compare_dn_on_server);
1039     switch(result) {
1040         case LDAP_COMPARE_TRUE: {
1041             ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1042                           "auth_ldap authorize: "
1043                           "require dn: authorization successful");
1044             set_request_vars(r, LDAP_AUTHZ);
1045             return AUTHZ_GRANTED;
1046         }
1047         default: {
1048             ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1049                           "auth_ldap authorize: "
1050                           "require dn \"%s\": LDAP error [%s][%s]",
1051                           t, ldc->reason, ldap_err2string(result));
1052         }
1053     }
1054
1055
1056     ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1057                   "auth_ldap authorize dn: authorization denied for "
1058                   "user %s to %s",
1059                   r->user, r->uri);
1060
1061     return AUTHZ_DENIED;
1062 }
1063
1064 static authz_status ldapattribute_check_authorization(request_rec *r,
1065                                                       const char *require_args,
1066                                                       const void *parsed_require_args)
1067 {
1068     int result = 0;
1069     authn_ldap_request_t *req =
1070         (authn_ldap_request_t *)ap_get_module_config(r->request_config, &authnz_ldap_module);
1071     authn_ldap_config_t *sec =
1072         (authn_ldap_config_t *)ap_get_module_config(r->per_dir_config, &authnz_ldap_module);
1073
1074     util_ldap_connection_t *ldc = NULL;
1075
1076     const char *t;
1077     char *w, *value;
1078
1079     char filtbuf[FILTER_LENGTH];
1080     const char *dn = NULL;
1081
1082     if (!r->user) {
1083         return AUTHZ_DENIED_NO_USER;
1084     }
1085
1086     if (!sec->have_ldap_url) {
1087         return AUTHZ_DENIED;
1088     }
1089
1090     if (sec->host) {
1091         ldc = get_connection_for_authz(r, LDAP_COMPARE);
1092         apr_pool_cleanup_register(r->pool, ldc,
1093                                   authnz_ldap_cleanup_connection_close,
1094                                   apr_pool_cleanup_null);
1095     }
1096     else {
1097         ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r,
1098                       "auth_ldap authorize: no sec->host - weird...?");
1099         return AUTHZ_DENIED;
1100     }
1101
1102     /*
1103      * If we have been authenticated by some other module than mod_auth_ldap,
1104      * the req structure needed for authorization needs to be created
1105      * and populated with the userid and DN of the account in LDAP
1106      */
1107
1108     if (!strlen(r->user)) {
1109         ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r,
1110             "ldap authorize: Userid is blank, AuthType=%s",
1111             r->ap_auth_type);
1112     }
1113
1114     if(!req) {
1115         ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1116             "ldap authorize: Creating LDAP req structure");
1117
1118         req = (authn_ldap_request_t *)apr_pcalloc(r->pool,
1119             sizeof(authn_ldap_request_t));
1120         /* Build the username filter */
1121         authn_ldap_build_filter(filtbuf, r, r->user, NULL, sec);
1122
1123         /* Search for the user DN */
1124         result = util_ldap_cache_getuserdn(r, ldc, sec->url, sec->basedn,
1125              sec->scope, sec->attributes, filtbuf, &dn, &(req->vals));
1126
1127         /* Search failed, log error and return failure */
1128         if(result != LDAP_SUCCESS) {
1129             ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1130                 "auth_ldap authorise: User DN not found with filter %s: %s", filtbuf, ldc->reason);
1131             return AUTHZ_DENIED;
1132         }
1133
1134         ap_set_module_config(r->request_config, &authnz_ldap_module, req);
1135         req->dn = apr_pstrdup(r->pool, dn);
1136         req->user = r->user;
1137     }
1138
1139     if (req->dn == NULL || strlen(req->dn) == 0) {
1140         ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1141                       "auth_ldap authorize: require ldap-attribute: user's DN "
1142                       "has not been defined; failing authorization");
1143         return AUTHZ_DENIED;
1144     }
1145
1146     t = require_args;
1147     while (t[0]) {
1148         w = ap_getword(r->pool, &t, '=');
1149         value = ap_getword_conf(r->pool, &t);
1150
1151         ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1152                       "auth_ldap authorize: checking attribute %s has value %s",
1153                       w, value);
1154         result = util_ldap_cache_compare(r, ldc, sec->url, req->dn, w, value);
1155         switch(result) {
1156             case LDAP_COMPARE_TRUE: {
1157                 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1158                               "auth_ldap authorize: "
1159                               "require attribute: authorization successful");
1160                 set_request_vars(r, LDAP_AUTHZ);
1161                 return AUTHZ_GRANTED;
1162             }
1163             default: {
1164                 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1165                               "auth_ldap authorize: require attribute: "
1166                               "authorization failed [%s][%s]",
1167                               ldc->reason, ldap_err2string(result));
1168             }
1169         }
1170     }
1171
1172     ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1173                   "auth_ldap authorize attribute: authorization denied for "
1174                   "user %s to %s",
1175                   r->user, r->uri);
1176
1177     return AUTHZ_DENIED;
1178 }
1179
1180 static authz_status ldapfilter_check_authorization(request_rec *r,
1181                                                    const char *require_args,
1182                                                    const void *parsed_require_args)
1183 {
1184     int result = 0;
1185     authn_ldap_request_t *req =
1186         (authn_ldap_request_t *)ap_get_module_config(r->request_config, &authnz_ldap_module);
1187     authn_ldap_config_t *sec =
1188         (authn_ldap_config_t *)ap_get_module_config(r->per_dir_config, &authnz_ldap_module);
1189
1190     util_ldap_connection_t *ldc = NULL;
1191     const char *t;
1192
1193     char filtbuf[FILTER_LENGTH];
1194     const char *dn = NULL;
1195
1196     if (!r->user) {
1197         return AUTHZ_DENIED_NO_USER;
1198     }
1199
1200     if (!sec->have_ldap_url) {
1201         return AUTHZ_DENIED;
1202     }
1203
1204     if (sec->host) {
1205         ldc = get_connection_for_authz(r, LDAP_SEARCH);
1206         apr_pool_cleanup_register(r->pool, ldc,
1207                                   authnz_ldap_cleanup_connection_close,
1208                                   apr_pool_cleanup_null);
1209     }
1210     else {
1211         ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r,
1212                       "auth_ldap authorize: no sec->host - weird...?");
1213         return AUTHZ_DENIED;
1214     }
1215
1216     /*
1217      * If we have been authenticated by some other module than mod_auth_ldap,
1218      * the req structure needed for authorization needs to be created
1219      * and populated with the userid and DN of the account in LDAP
1220      */
1221
1222     if (!strlen(r->user)) {
1223         ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r,
1224             "ldap authorize: Userid is blank, AuthType=%s",
1225             r->ap_auth_type);
1226     }
1227
1228     if(!req) {
1229         ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1230             "ldap authorize: Creating LDAP req structure");
1231
1232         req = (authn_ldap_request_t *)apr_pcalloc(r->pool,
1233             sizeof(authn_ldap_request_t));
1234         /* Build the username filter */
1235         authn_ldap_build_filter(filtbuf, r, r->user, NULL, sec);
1236
1237         /* Search for the user DN */
1238         result = util_ldap_cache_getuserdn(r, ldc, sec->url, sec->basedn,
1239              sec->scope, sec->attributes, filtbuf, &dn, &(req->vals));
1240
1241         /* Search failed, log error and return failure */
1242         if(result != LDAP_SUCCESS) {
1243             ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1244                 "auth_ldap authorise: User DN not found with filter %s: %s", filtbuf, ldc->reason);
1245             return AUTHZ_DENIED;
1246         }
1247
1248         ap_set_module_config(r->request_config, &authnz_ldap_module, req);
1249         req->dn = apr_pstrdup(r->pool, dn);
1250         req->user = r->user;
1251     }
1252
1253     if (req->dn == NULL || strlen(req->dn) == 0) {
1254         ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1255                       "auth_ldap authorize: require ldap-filter: user's DN "
1256                       "has not been defined; failing authorization");
1257         return AUTHZ_DENIED;
1258     }
1259
1260     t = require_args;
1261
1262     if (t[0]) {
1263         ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1264                       "auth_ldap authorize: checking filter %s", t);
1265
1266         /* Build the username filter */
1267         authn_ldap_build_filter(filtbuf, r, req->user, t, sec);
1268
1269         /* Search for the user DN */
1270         result = util_ldap_cache_getuserdn(r, ldc, sec->url, sec->basedn,
1271              sec->scope, sec->attributes, filtbuf, &dn, &(req->vals));
1272
1273         /* Make sure that the filtered search returned the correct user dn */
1274         if (result == LDAP_SUCCESS) {
1275             ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1276                           "auth_ldap authorize: checking dn match %s", dn);
1277             if (sec->compare_as_user) {
1278                 /* ldap-filter is the only authz that requires a search and a compare */
1279                 apr_pool_cleanup_kill(r->pool, ldc, authnz_ldap_cleanup_connection_close);
1280                 authnz_ldap_cleanup_connection_close(ldc);
1281                 ldc = get_connection_for_authz(r, LDAP_COMPARE);
1282             }
1283             result = util_ldap_cache_comparedn(r, ldc, sec->url, req->dn, dn,
1284                                                sec->compare_dn_on_server);
1285         }
1286
1287         switch(result) {
1288             case LDAP_COMPARE_TRUE: {
1289                 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1290                               "auth_ldap authorize: require ldap-filter: "
1291                               "authorization successful");
1292                 set_request_vars(r, LDAP_AUTHZ);
1293                 return AUTHZ_GRANTED;
1294             }
1295             case LDAP_FILTER_ERROR: {
1296                 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1297                               "auth_ldap authorize: require ldap-filter: "
1298                               "%s authorization failed [%s][%s]",
1299                               filtbuf, ldc->reason, ldap_err2string(result));
1300                 break;
1301             }
1302             default: {
1303                 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1304                               "auth_ldap authorize: require ldap-filter: "
1305                               "authorization failed [%s][%s]",
1306                               ldc->reason, ldap_err2string(result));
1307             }
1308         }
1309     }
1310
1311     ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1312                   "auth_ldap authorize filter: authorization denied for "
1313                   "user %s to %s",
1314                   r->user, r->uri);
1315
1316     return AUTHZ_DENIED;
1317 }
1318
1319
1320 /*
1321  * Use the ldap url parsing routines to break up the ldap url into
1322  * host and port.
1323  */
1324 static const char *mod_auth_ldap_parse_url(cmd_parms *cmd,
1325                                     void *config,
1326                                     const char *url,
1327                                     const char *mode)
1328 {
1329     int rc;
1330     apr_ldap_url_desc_t *urld;
1331     apr_ldap_err_t *result;
1332
1333     authn_ldap_config_t *sec = config;
1334
1335     rc = apr_ldap_url_parse(cmd->pool, url, &(urld), &(result));
1336     if (rc != APR_SUCCESS) {
1337         return result->reason;
1338     }
1339     sec->url = apr_pstrdup(cmd->pool, url);
1340
1341     /* Set all the values, or at least some sane defaults */
1342     if (sec->host) {
1343         sec->host = apr_pstrcat(cmd->pool, urld->lud_host, " ", sec->host, NULL);
1344     }
1345     else {
1346         sec->host = urld->lud_host? apr_pstrdup(cmd->pool, urld->lud_host) : "localhost";
1347     }
1348     sec->basedn = urld->lud_dn? apr_pstrdup(cmd->pool, urld->lud_dn) : "";
1349     if (urld->lud_attrs && urld->lud_attrs[0]) {
1350         int i = 1;
1351         while (urld->lud_attrs[i]) {
1352             i++;
1353         }
1354         sec->attributes = apr_pcalloc(cmd->pool, sizeof(char *) * (i+1));
1355         i = 0;
1356         while (urld->lud_attrs[i]) {
1357             sec->attributes[i] = apr_pstrdup(cmd->pool, urld->lud_attrs[i]);
1358             i++;
1359         }
1360         sec->attribute = sec->attributes[0];
1361     }
1362     else {
1363         sec->attribute = "uid";
1364     }
1365
1366     sec->scope = urld->lud_scope == LDAP_SCOPE_ONELEVEL ?
1367         LDAP_SCOPE_ONELEVEL : LDAP_SCOPE_SUBTREE;
1368
1369     if (urld->lud_filter) {
1370         if (urld->lud_filter[0] == '(') {
1371             /*
1372              * Get rid of the surrounding parens; later on when generating the
1373              * filter, they'll be put back.
1374              */
1375             sec->filter = apr_pstrdup(cmd->pool, urld->lud_filter+1);
1376             sec->filter[strlen(sec->filter)-1] = '\0';
1377         }
1378         else {
1379             sec->filter = apr_pstrdup(cmd->pool, urld->lud_filter);
1380         }
1381     }
1382     else {
1383         sec->filter = "objectclass=*";
1384     }
1385
1386     if (mode) {
1387         if (0 == strcasecmp("NONE", mode)) {
1388             sec->secure = APR_LDAP_NONE;
1389         }
1390         else if (0 == strcasecmp("SSL", mode)) {
1391             sec->secure = APR_LDAP_SSL;
1392         }
1393         else if (0 == strcasecmp("TLS", mode) || 0 == strcasecmp("STARTTLS", mode)) {
1394             sec->secure = APR_LDAP_STARTTLS;
1395         }
1396         else {
1397             return "Invalid LDAP connection mode setting: must be one of NONE, "
1398                    "SSL, or TLS/STARTTLS";
1399         }
1400     }
1401
1402       /* "ldaps" indicates secure ldap connections desired
1403       */
1404     if (strncasecmp(url, "ldaps", 5) == 0)
1405     {
1406         sec->secure = APR_LDAP_SSL;
1407         sec->port = urld->lud_port? urld->lud_port : LDAPS_PORT;
1408     }
1409     else
1410     {
1411         sec->port = urld->lud_port? urld->lud_port : LDAP_PORT;
1412     }
1413
1414     sec->have_ldap_url = 1;
1415
1416     ap_log_error(APLOG_MARK, APLOG_TRACE1, 0, cmd->server,
1417                  "auth_ldap url parse: `%s', Host: %s, Port: %d, DN: %s, "
1418                  "attrib: %s, scope: %s, filter: %s, connection mode: %s",
1419                  url,
1420                  urld->lud_host,
1421                  urld->lud_port,
1422                  urld->lud_dn,
1423                  urld->lud_attrs? urld->lud_attrs[0] : "(null)",
1424                  (urld->lud_scope == LDAP_SCOPE_SUBTREE? "subtree" :
1425                   urld->lud_scope == LDAP_SCOPE_BASE? "base" :
1426                   urld->lud_scope == LDAP_SCOPE_ONELEVEL? "onelevel" : "unknown"),
1427                  urld->lud_filter,
1428                  sec->secure == APR_LDAP_SSL  ? "using SSL": "not using SSL"
1429                  );
1430
1431     return NULL;
1432 }
1433
1434 static const char *mod_auth_ldap_set_deref(cmd_parms *cmd, void *config, const char *arg)
1435 {
1436     authn_ldap_config_t *sec = config;
1437
1438     if (strcmp(arg, "never") == 0 || strcasecmp(arg, "off") == 0) {
1439         sec->deref = never;
1440     }
1441     else if (strcmp(arg, "searching") == 0) {
1442         sec->deref = searching;
1443     }
1444     else if (strcmp(arg, "finding") == 0) {
1445         sec->deref = finding;
1446     }
1447     else if (strcmp(arg, "always") == 0 || strcasecmp(arg, "on") == 0) {
1448         sec->deref = always;
1449     }
1450     else {
1451         return "Unrecognized value for AuthLDAPAliasDereference directive";
1452     }
1453     return NULL;
1454 }
1455
1456 static const char *mod_auth_ldap_add_subgroup_attribute(cmd_parms *cmd, void *config, const char *arg)
1457 {
1458     int i = 0;
1459
1460     authn_ldap_config_t *sec = config;
1461
1462     for (i = 0; sec->sgAttributes[i]; i++) {
1463         ;
1464     }
1465     if (i == GROUPATTR_MAX_ELTS)
1466         return "Too many AuthLDAPSubGroupAttribute values";
1467
1468     sec->sgAttributes[i] = apr_pstrdup(cmd->pool, arg);
1469
1470     return NULL;
1471 }
1472
1473 static const char *mod_auth_ldap_add_subgroup_class(cmd_parms *cmd, void *config, const char *arg)
1474 {
1475     struct mod_auth_ldap_groupattr_entry_t *new;
1476
1477     authn_ldap_config_t *sec = config;
1478
1479     if (sec->subgroupclasses->nelts > GROUPATTR_MAX_ELTS)
1480         return "Too many AuthLDAPSubGroupClass values";
1481
1482     new = apr_array_push(sec->subgroupclasses);
1483     new->name = apr_pstrdup(cmd->pool, arg);
1484
1485     return NULL;
1486 }
1487
1488 static const char *mod_auth_ldap_set_subgroup_maxdepth(cmd_parms *cmd,
1489                                                        void *config,
1490                                                        const char *max_depth)
1491 {
1492     authn_ldap_config_t *sec = config;
1493
1494     sec->maxNestingDepth = atol(max_depth);
1495
1496     return NULL;
1497 }
1498
1499 static const char *mod_auth_ldap_add_group_attribute(cmd_parms *cmd, void *config, const char *arg)
1500 {
1501     struct mod_auth_ldap_groupattr_entry_t *new;
1502
1503     authn_ldap_config_t *sec = config;
1504
1505     if (sec->groupattr->nelts > GROUPATTR_MAX_ELTS)
1506         return "Too many AuthLDAPGroupAttribute directives";
1507
1508     new = apr_array_push(sec->groupattr);
1509     new->name = apr_pstrdup(cmd->pool, arg);
1510
1511     return NULL;
1512 }
1513
1514 static const char *set_charset_config(cmd_parms *cmd, void *config, const char *arg)
1515 {
1516     ap_set_module_config(cmd->server->module_config, &authnz_ldap_module,
1517                          (void *)arg);
1518     return NULL;
1519 }
1520
1521 static const char *set_bind_pattern(cmd_parms *cmd, void *_cfg, const char *exp, const char *subst)
1522 {
1523     authn_ldap_config_t *sec = _cfg;
1524     ap_regex_t *regexp;
1525
1526     regexp = ap_pregcomp(cmd->pool, exp, AP_REG_EXTENDED);
1527
1528     if (!regexp) {
1529         return apr_pstrcat(cmd->pool, "AuthLDAPInitialBindPattern: cannot compile regular "
1530                                       "expression '", exp, "'", NULL);
1531     }
1532
1533     sec->bind_regex = regexp;
1534     sec->bind_subst = apr_pstrdup(cmd->pool, subst);
1535
1536     return NULL;
1537 }
1538
1539 static const command_rec authnz_ldap_cmds[] =
1540 {
1541     AP_INIT_TAKE12("AuthLDAPURL", mod_auth_ldap_parse_url, NULL, OR_AUTHCFG,
1542                   "URL to define LDAP connection. This should be an RFC 2255 compliant\n"
1543                   "URL of the form ldap://host[:port]/basedn[?attrib[?scope[?filter]]].\n"
1544                   "<ul>\n"
1545                   "<li>Host is the name of the LDAP server. Use a space separated list of hosts \n"
1546                   "to specify redundant servers.\n"
1547                   "<li>Port is optional, and specifies the port to connect to.\n"
1548                   "<li>basedn specifies the base DN to start searches from\n"
1549                   "<li>Attrib specifies what attribute to search for in the directory. If not "
1550                   "provided, it defaults to <b>uid</b>.\n"
1551                   "<li>Scope is the scope of the search, and can be either <b>sub</b> or "
1552                   "<b>one</b>. If not provided, the default is <b>sub</b>.\n"
1553                   "<li>Filter is a filter to use in the search. If not provided, "
1554                   "defaults to <b>(objectClass=*)</b>.\n"
1555                   "</ul>\n"
1556                   "Searches are performed using the attribute and the filter combined. "
1557                   "For example, assume that the\n"
1558                   "LDAP URL is <b>ldap://ldap.airius.com/ou=People, o=Airius?uid?sub?(posixid=*)</b>. "
1559                   "Searches will\n"
1560                   "be done using the filter <b>(&((posixid=*))(uid=<i>username</i>))</b>, "
1561                   "where <i>username</i>\n"
1562                   "is the user name passed by the HTTP client. The search will be a subtree "
1563                   "search on the branch <b>ou=People, o=Airius</b>."),
1564
1565     AP_INIT_TAKE1("AuthLDAPBindDN", ap_set_string_slot,
1566                   (void *)APR_OFFSETOF(authn_ldap_config_t, binddn), OR_AUTHCFG,
1567                   "DN to use to bind to LDAP server. If not provided, will do an anonymous bind."),
1568
1569     AP_INIT_TAKE1("AuthLDAPBindPassword", ap_set_string_slot,
1570                   (void *)APR_OFFSETOF(authn_ldap_config_t, bindpw), OR_AUTHCFG,
1571                   "Password to use to bind to LDAP server. If not provided, will do an anonymous bind."),
1572
1573     AP_INIT_FLAG("AuthLDAPBindAuthoritative", ap_set_flag_slot,
1574                   (void *)APR_OFFSETOF(authn_ldap_config_t, bind_authoritative), OR_AUTHCFG,
1575                   "Set to 'on' to return failures when user-specific bind fails - defaults to on."),
1576
1577     AP_INIT_FLAG("AuthLDAPRemoteUserIsDN", ap_set_flag_slot,
1578                  (void *)APR_OFFSETOF(authn_ldap_config_t, user_is_dn), OR_AUTHCFG,
1579                  "Set to 'on' to set the REMOTE_USER environment variable to be the full "
1580                  "DN of the remote user. By default, this is set to off, meaning that "
1581                  "the REMOTE_USER variable will contain whatever value the remote user sent."),
1582
1583     AP_INIT_TAKE1("AuthLDAPRemoteUserAttribute", ap_set_string_slot,
1584                  (void *)APR_OFFSETOF(authn_ldap_config_t, remote_user_attribute), OR_AUTHCFG,
1585                  "Override the user supplied username and place the "
1586                  "contents of this attribute in the REMOTE_USER "
1587                  "environment variable."),
1588
1589     AP_INIT_FLAG("AuthLDAPCompareDNOnServer", ap_set_flag_slot,
1590                  (void *)APR_OFFSETOF(authn_ldap_config_t, compare_dn_on_server), OR_AUTHCFG,
1591                  "Set to 'on' to force auth_ldap to do DN compares (for the \"require dn\" "
1592                  "directive) using the server, and set it 'off' to do the compares locally "
1593                  "(at the expense of possible false matches). See the documentation for "
1594                  "a complete description of this option."),
1595
1596     AP_INIT_ITERATE("AuthLDAPSubGroupAttribute", mod_auth_ldap_add_subgroup_attribute, NULL, OR_AUTHCFG,
1597                     "Attribute labels used to define sub-group (or nested group) membership in groups - "
1598                     "defaults to member and uniqueMember"),
1599
1600     AP_INIT_ITERATE("AuthLDAPSubGroupClass", mod_auth_ldap_add_subgroup_class, NULL, OR_AUTHCFG,
1601                      "LDAP objectClass values used to identify sub-group instances - "
1602                      "defaults to groupOfNames and groupOfUniqueNames"),
1603
1604     AP_INIT_TAKE1("AuthLDAPMaxSubGroupDepth", mod_auth_ldap_set_subgroup_maxdepth, NULL, OR_AUTHCFG,
1605                       "Maximum subgroup nesting depth to be evaluated - defaults to 10 (top-level group = 0)"),
1606
1607     AP_INIT_ITERATE("AuthLDAPGroupAttribute", mod_auth_ldap_add_group_attribute, NULL, OR_AUTHCFG,
1608                     "A list of attribute labels used to identify the user members of groups - defaults to "
1609                     "member and uniquemember"),
1610
1611     AP_INIT_FLAG("AuthLDAPGroupAttributeIsDN", ap_set_flag_slot,
1612                  (void *)APR_OFFSETOF(authn_ldap_config_t, group_attrib_is_dn), OR_AUTHCFG,
1613                  "If set to 'on', auth_ldap uses the DN that is retrieved from the server for"
1614                  "subsequent group comparisons. If set to 'off', auth_ldap uses the string"
1615                  "provided by the client directly. Defaults to 'on'."),
1616
1617     AP_INIT_TAKE1("AuthLDAPDereferenceAliases", mod_auth_ldap_set_deref, NULL, OR_AUTHCFG,
1618                   "Determines how aliases are handled during a search. Can be one of the"
1619                   "values \"never\", \"searching\", \"finding\", or \"always\". "
1620                   "Defaults to always."),
1621
1622     AP_INIT_TAKE1("AuthLDAPCharsetConfig", set_charset_config, NULL, RSRC_CONF,
1623                   "Character set conversion configuration file. If omitted, character set"
1624                   "conversion is disabled."),
1625
1626     AP_INIT_TAKE1("AuthLDAPAuthorizePrefix", ap_set_string_slot,
1627                   (void *)APR_OFFSETOF(authn_ldap_config_t, authz_prefix), OR_AUTHCFG,
1628                   "The prefix to add to environment variables set during "
1629                   "successful authorization, default '" AUTHZ_PREFIX "'"),
1630
1631     AP_INIT_FLAG("AuthLDAPInitialBindAsUser", ap_set_flag_slot,
1632                  (void *)APR_OFFSETOF(authn_ldap_config_t, initial_bind_as_user), OR_AUTHCFG,
1633                  "Set to 'on' to perform the initial DN lookup with the basic auth credentials "
1634                  "instead of anonymous or hard-coded credentials"),
1635
1636      AP_INIT_TAKE2("AuthLDAPInitialBindPattern", set_bind_pattern, NULL, OR_AUTHCFG,
1637                    "The regex and substitution to determine a username that can bind based on an HTTP basic auth username"),
1638
1639      AP_INIT_FLAG("AuthLDAPSearchAsUser", ap_set_flag_slot,
1640                   (void *)APR_OFFSETOF(authn_ldap_config_t, search_as_user), OR_AUTHCFG,
1641                    "Set to 'on' to perform authorization-based searches with the users credentials, when this module"
1642                    " has also performed authentication.  Does not affect nested groups lookup."),
1643      AP_INIT_FLAG("AuthLDAPCompareAsUser", ap_set_flag_slot,
1644                   (void *)APR_OFFSETOF(authn_ldap_config_t, compare_as_user), OR_AUTHCFG,
1645                   "Set to 'on' to perform authorization-based compares with the users credentials, when this module"
1646                   " has also performed authentication.  Does not affect nested groups lookups."),
1647     {NULL}
1648 };
1649
1650 static int authnz_ldap_post_config(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s)
1651 {
1652     ap_configfile_t *f;
1653     char l[MAX_STRING_LEN];
1654     const char *charset_confname = ap_get_module_config(s->module_config,
1655                                                       &authnz_ldap_module);
1656     apr_status_t status;
1657
1658     /*
1659     authn_ldap_config_t *sec = (authn_ldap_config_t *)
1660                                     ap_get_module_config(s->module_config,
1661                                                          &authnz_ldap_module);
1662
1663     if (sec->secure)
1664     {
1665         if (!util_ldap_ssl_supported(s))
1666         {
1667             ap_log_error(APLOG_MARK, APLOG_CRIT, 0, s,
1668                      "LDAP: SSL connections (ldaps://) not supported by utilLDAP");
1669             return(!OK);
1670         }
1671     }
1672     */
1673
1674     /* make sure that mod_ldap (util_ldap) is loaded */
1675     if (ap_find_linked_module("util_ldap.c") == NULL) {
1676         ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
1677                      "Module mod_ldap missing. Mod_ldap (aka. util_ldap) "
1678                      "must be loaded in order for mod_authnz_ldap to function properly");
1679         return HTTP_INTERNAL_SERVER_ERROR;
1680
1681     }
1682
1683     if (!charset_confname) {
1684         return OK;
1685     }
1686
1687     charset_confname = ap_server_root_relative(p, charset_confname);
1688     if (!charset_confname) {
1689         ap_log_error(APLOG_MARK, APLOG_ERR, APR_EBADPATH, s,
1690                      "Invalid charset conversion config path %s",
1691                      (const char *)ap_get_module_config(s->module_config,
1692                                                         &authnz_ldap_module));
1693         return HTTP_INTERNAL_SERVER_ERROR;
1694     }
1695     if ((status = ap_pcfg_openfile(&f, ptemp, charset_confname))
1696                 != APR_SUCCESS) {
1697         ap_log_error(APLOG_MARK, APLOG_ERR, status, s,
1698                      "could not open charset conversion config file %s.",
1699                      charset_confname);
1700         return HTTP_INTERNAL_SERVER_ERROR;
1701     }
1702
1703     charset_conversions = apr_hash_make(p);
1704
1705     while (!(ap_cfg_getline(l, MAX_STRING_LEN, f))) {
1706         const char *ll = l;
1707         char *lang;
1708
1709         if (l[0] == '#') {
1710             continue;
1711         }
1712         lang = ap_getword_conf(p, &ll);
1713         ap_str_tolower(lang);
1714
1715         if (ll[0]) {
1716             char *charset = ap_getword_conf(p, &ll);
1717             apr_hash_set(charset_conversions, lang, APR_HASH_KEY_STRING, charset);
1718         }
1719     }
1720     ap_cfg_closefile(f);
1721
1722     to_charset = derive_codepage_from_lang (p, "utf-8");
1723     if (to_charset == NULL) {
1724         ap_log_error(APLOG_MARK, APLOG_ERR, status, s,
1725                      "could not find the UTF-8 charset in the file %s.",
1726                      charset_confname);
1727         return HTTP_INTERNAL_SERVER_ERROR;
1728     }
1729
1730     return OK;
1731 }
1732
1733 static const authn_provider authn_ldap_provider =
1734 {
1735     &authn_ldap_check_password,
1736 };
1737
1738 static const authz_provider authz_ldapuser_provider =
1739 {
1740     &ldapuser_check_authorization,
1741     NULL,
1742 };
1743 static const authz_provider authz_ldapgroup_provider =
1744 {
1745     &ldapgroup_check_authorization,
1746     NULL,
1747 };
1748
1749 static const authz_provider authz_ldapdn_provider =
1750 {
1751     &ldapdn_check_authorization,
1752     NULL,
1753 };
1754
1755 static const authz_provider authz_ldapattribute_provider =
1756 {
1757     &ldapattribute_check_authorization,
1758     NULL,
1759 };
1760
1761 static const authz_provider authz_ldapfilter_provider =
1762 {
1763     &ldapfilter_check_authorization,
1764     NULL,
1765 };
1766
1767 static void ImportULDAPOptFn(void)
1768 {
1769     util_ldap_connection_close  = APR_RETRIEVE_OPTIONAL_FN(uldap_connection_close);
1770     util_ldap_connection_find   = APR_RETRIEVE_OPTIONAL_FN(uldap_connection_find);
1771     util_ldap_cache_comparedn   = APR_RETRIEVE_OPTIONAL_FN(uldap_cache_comparedn);
1772     util_ldap_cache_compare     = APR_RETRIEVE_OPTIONAL_FN(uldap_cache_compare);
1773     util_ldap_cache_checkuserid = APR_RETRIEVE_OPTIONAL_FN(uldap_cache_checkuserid);
1774     util_ldap_cache_getuserdn   = APR_RETRIEVE_OPTIONAL_FN(uldap_cache_getuserdn);
1775     util_ldap_ssl_supported     = APR_RETRIEVE_OPTIONAL_FN(uldap_ssl_supported);
1776     util_ldap_cache_check_subgroups = APR_RETRIEVE_OPTIONAL_FN(uldap_cache_check_subgroups);
1777 }
1778
1779 static void register_hooks(apr_pool_t *p)
1780 {
1781     /* Register authn provider */
1782     ap_register_auth_provider(p, AUTHN_PROVIDER_GROUP, "ldap",
1783                               AUTHN_PROVIDER_VERSION,
1784                               &authn_ldap_provider, AP_AUTH_INTERNAL_PER_CONF);
1785
1786     /* Register authz providers */
1787     ap_register_auth_provider(p, AUTHZ_PROVIDER_GROUP, "ldap-user",
1788                               AUTHZ_PROVIDER_VERSION,
1789                               &authz_ldapuser_provider,
1790                               AP_AUTH_INTERNAL_PER_CONF);
1791     ap_register_auth_provider(p, AUTHZ_PROVIDER_GROUP, "ldap-group",
1792                               AUTHZ_PROVIDER_VERSION,
1793                               &authz_ldapgroup_provider,
1794                               AP_AUTH_INTERNAL_PER_CONF);
1795     ap_register_auth_provider(p, AUTHZ_PROVIDER_GROUP, "ldap-dn",
1796                               AUTHZ_PROVIDER_VERSION,
1797                               &authz_ldapdn_provider,
1798                               AP_AUTH_INTERNAL_PER_CONF);
1799     ap_register_auth_provider(p, AUTHZ_PROVIDER_GROUP, "ldap-attribute",
1800                               AUTHZ_PROVIDER_VERSION,
1801                               &authz_ldapattribute_provider,
1802                               AP_AUTH_INTERNAL_PER_CONF);
1803     ap_register_auth_provider(p, AUTHZ_PROVIDER_GROUP, "ldap-filter",
1804                               AUTHZ_PROVIDER_VERSION,
1805                               &authz_ldapfilter_provider,
1806                               AP_AUTH_INTERNAL_PER_CONF);
1807
1808     ap_hook_post_config(authnz_ldap_post_config,NULL,NULL,APR_HOOK_MIDDLE);
1809
1810     ap_hook_optional_fn_retrieve(ImportULDAPOptFn,NULL,NULL,APR_HOOK_MIDDLE);
1811 }
1812
1813 AP_DECLARE_MODULE(authnz_ldap) =
1814 {
1815     STANDARD20_MODULE_STUFF,
1816     create_authnz_ldap_dir_config,   /* dir config creater */
1817     NULL,                            /* dir merger --- default is to override */
1818     NULL,                            /* server config */
1819     NULL,                            /* merge server config */
1820     authnz_ldap_cmds,                /* command apr_table_t */
1821     register_hooks                   /* register hooks */
1822 };