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