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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "ap_provider.h"
19 #include "http_config.h"
20 #include "ap_provider.h"
21 #include "http_core.h"
23 #include "http_protocol.h"
24 #include "http_request.h"
25 #include "util_ldap.h"
29 #include "apr_strings.h"
30 #include "apr_xlate.h"
31 #define APR_WANT_STRFUNC
38 #error mod_authnz_ldap requires APR-util to have LDAP support built in. To fix add --with-ldap to ./configure.
41 static char *default_attributes[3] = { "member", "uniqueMember", NULL };
44 apr_pool_t *pool; /* Pool that this config is allocated from */
46 apr_thread_mutex_t *lock; /* Lock for this config */
49 /* These parameters are all derived from the AuthLDAPURL directive */
50 char *url; /* String representation of the URL */
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 */
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 */
68 int have_ldap_url; /* Set if we have found an LDAP url */
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 */
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;
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;
93 enum auth_ldap_phase {
94 LDAP_AUTHN, LDAP_AUTHZ
97 enum auth_ldap_optype {
98 LDAP_SEARCH, LDAP_COMPARE, LDAP_COMPARE_AND_SEARCH /* nested groups */
101 /* maximum group elements supported */
102 #define GROUPATTR_MAX_ELTS 10
104 module AP_MODULE_DECLARE_DATA authnz_ldap_module;
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;
115 static apr_hash_t *charset_conversions = NULL;
116 static char *to_charset = NULL; /* UTF-8 identifier derived from the charset.conv file */
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)
125 if (!language) /* our default codepage */
126 return apr_pstrdup(p, "ISO-8859-1");
128 lang_len = strlen(language);
130 charset = (char*) apr_hash_get(charset_conversions, language, APR_HASH_KEY_STRING);
134 charset = (char*) apr_hash_get(charset_conversions, language, APR_HASH_KEY_STRING);
138 charset = apr_pstrdup(p, charset);
144 static apr_xlate_t* get_conv_set (request_rec *r)
146 char *lang_line = (char*)apr_table_get(r->headers_in, "accept-language");
148 apr_xlate_t *convset;
151 lang_line = apr_pstrdup(r->pool, lang_line);
152 for (lang = lang_line;*lang;lang++) {
153 if ((*lang == ',') || (*lang == ';')) {
158 lang = derive_codepage_from_lang(r->pool, lang_line);
160 if (lang && (apr_xlate_open(&convset, to_charset, lang, r->pool) == APR_SUCCESS)) {
169 static const char* authn_ldap_xlate_password(request_rec *r,
170 const char* sent_password)
172 apr_xlate_t *convset = NULL;
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);
182 /* Convert the password to UTF-8. */
183 if (apr_xlate_conv_buffer(convset, sent_password, &inbytes, outbuf,
184 &outbytes) == APR_SUCCESS)
188 return sent_password;
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.
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
205 * ldap://ldap.airius.com/ou=People, o=Airius?uid??(posixid=*)
207 * Further, assume that the userid passed by the client was `userj'. The
208 * search filter will be (&(posixid=*)(uid=userj)).
210 #define FILTER_LENGTH MAX_STRING_LEN
211 static void authn_ldap_build_filter(char *filtbuf,
213 const char* sent_user,
214 const char* sent_filter,
215 authn_ldap_config_t *sec)
217 char *p, *q, *filtbuf_end;
219 apr_xlate_t *convset = NULL;
224 if (sent_user != NULL) {
225 user = apr_pstrdup (r->pool, sent_user);
230 if (sent_filter != NULL) {
231 filter = apr_pstrdup (r->pool, sent_filter);
234 filter = sec->filter;
236 if (charset_conversions) {
237 convset = get_conv_set(r);
241 inbytes = strlen(user);
242 outbytes = (inbytes+1)*3;
243 outbuf = apr_pcalloc(r->pool, outbytes);
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);
252 * Create the first part of the filter, which consists of the
253 * config-supplied portions.
255 apr_snprintf(filtbuf, FILTER_LENGTH, "(&(%s)(%s=", filter, sec->attribute);
258 * Now add the client-supplied username to the filter, ensuring that any
259 * LDAP filter metachars are escaped.
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 */
293 for (p = user, q=filtbuf + strlen(filtbuf);
294 *p && q < filtbuf_end; *q++ = *p++) {
295 if (strchr("*()\\", *p) != NULL) {
297 if (q >= filtbuf_end) {
306 * Append the closing parens of the filter, unless doing so would
307 * overrun the buffer.
309 if (q + 2 <= filtbuf_end)
310 strcat(filtbuf, "))");
313 static void *create_authnz_ldap_dir_config(apr_pool_t *p, char *d)
315 authn_ldap_config_t *sec =
316 (authn_ldap_config_t *)apr_pcalloc(p, sizeof(authn_ldap_config_t));
320 apr_thread_mutex_create(&sec->lock, APR_THREAD_MUTEX_DEFAULT, p);
323 sec->authz_enabled = 1;
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));
330 sec->have_ldap_url = 0;
335 sec->bind_authoritative = 1;
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);
343 sec->remote_user_attribute = NULL;
344 sec->compare_dn_on_server = 0;
346 sec->authz_prefix = AUTHZ_PREFIX;
351 static apr_status_t authnz_ldap_cleanup_connection_close(void *param)
353 util_ldap_connection_t *ldc = param;
354 util_ldap_connection_close(ldc);
358 static int set_request_vars(request_rec *r, enum auth_ldap_phase phase) {
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;
368 prefix = (phase == LDAP_AUTHN) ? AUTHN_PREFIX : sec->authz_prefix;
369 prefix_len = strlen(prefix);
371 if (sec->attributes && vals) {
372 apr_table_t *e = r->subprocess_env;
374 while (sec->attributes[i]) {
375 char *str = apr_pstrcat(r->pool, prefix, sec->attributes[i], NULL);
378 str[j] = apr_toupper(str[j]);
381 apr_table_setn(e, str, vals[i] ? vals[i] : "");
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;
393 return remote_user_attribute_set;
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];
402 if (NULL == user || NULL == sec || !sec->bind_regex || !sec->bind_subst) {
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;
413 apr_table_set(r->subprocess_env, "LDAP_BINDASUSER", result);
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.
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);
428 char *binddn = sec->binddn;
429 char *bindpw = sec->bindpw;
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))){
437 bindpw = req->password;
440 return util_ldap_connection_find(r, sec->host, sec->port,
442 sec->deref, sec->secure);
445 * Authentication Phase
446 * --------------------
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.
454 static authn_status authn_ldap_check_password(request_rec *r, const char *user,
455 const char *password)
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);
462 util_ldap_connection_t *ldc = NULL;
464 int remote_user_attribute_set = 0;
465 const char *dn = NULL;
466 const char *utfpassword;
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);
474 return AUTH_USER_NOT_FOUND;
479 * Basic sanity checks before any LDAP operations even happen.
481 if (!sec->have_ldap_url) {
482 return AUTH_GENERAL_ERROR;
487 /* There is a good AuthLDAPURL, right? */
489 const char *binddn = sec->binddn;
490 const char *bindpw = sec->bindpw;
491 if (sec->initial_bind_as_user) {
493 binddn = ldap_determine_binddn(r, user);
496 ldc = util_ldap_connection_find(r, sec->host, sec->port,
498 sec->deref, sec->secure);
501 ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r,
502 "auth_ldap authenticate: no sec->host - weird...?");
503 return AUTH_GENERAL_ERROR;
506 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
507 "auth_ldap authenticate: using URL %s", sec->url);
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;
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;
524 /* build the username filter */
525 authn_ldap_build_filter(filtbuf, r, user, NULL, sec);
527 /* convert password to utf-8 */
528 utfpassword = authn_ldap_xlate_password(r, password);
530 /* do the user search */
531 result = util_ldap_cache_checkuserid(r, ldc, sec->url, sec->basedn, sec->scope,
532 sec->attributes, filtbuf, utfpassword,
534 util_ldap_connection_close(ldc);
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) {
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;
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));
558 return (LDAP_NO_SUCH_OBJECT == result) ? AUTH_USER_NOT_FOUND
559 #ifdef LDAP_SECURITY_ERROR
560 : (LDAP_SECURITY_ERROR(result)) ? AUTH_DENIED
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
567 #ifdef LDAP_INSUFFICIENT_RIGHTS
568 : (LDAP_INSUFFICIENT_RIGHTS == result) ? AUTH_DENIED
571 : AUTH_GENERAL_ERROR;
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) {
582 /* add environment variables */
583 remote_user_attribute_set = set_request_vars(r, LDAP_AUTHN);
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);
596 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
597 "auth_ldap authenticate: accepting %s", user);
602 static authz_status ldapuser_check_authorization(request_rec *r,
603 const char *require_args)
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);
611 util_ldap_connection_t *ldc = NULL;
616 char filtbuf[FILTER_LENGTH];
617 const char *dn = NULL;
619 if (!sec->have_ldap_url) {
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);
630 ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r,
631 "auth_ldap authorize: no sec->host - weird...?");
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
641 /* Check that we have a userid to start with */
643 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
644 "access to %s failed, reason: no authenticated user", r->uri);
648 if (!strlen(r->user)) {
649 ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r,
650 "ldap authorize: Userid is blank, AuthType=%s",
655 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
656 "ldap authorize: Creating LDAP req structure");
658 req = (authn_ldap_request_t *)apr_pcalloc(r->pool,
659 sizeof(authn_ldap_request_t));
661 /* Build the username filter */
662 authn_ldap_build_filter(filtbuf, r, r->user, NULL, sec);
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));
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);
675 ap_set_module_config(r->request_config, &authnz_ldap_module, req);
676 req->dn = apr_pstrdup(r->pool, dn);
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");
689 * First do a whole-line compare, in case it's something like
690 * require user Babs Jensen
692 result = util_ldap_cache_compare(r, ldc, sec->url, req->dn, sec->attribute, require_args);
694 case LDAP_COMPARE_TRUE: {
695 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
696 "auth_ldap authorize: require user: authorization "
698 set_request_vars(r, LDAP_AUTHZ);
699 return AUTHZ_GRANTED;
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));
710 * Now break apart the line and compare each word on it
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);
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;
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));
732 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
733 "auth_ldap authorize user: authorization denied for "
740 static authz_status ldapgroup_check_authorization(request_rec *r,
741 const char *require_args)
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);
749 util_ldap_connection_t *ldc = NULL;
753 char filtbuf[FILTER_LENGTH];
754 const char *dn = NULL;
755 struct mod_auth_ldap_groupattr_entry_t *ent;
758 if (!sec->have_ldap_url) {
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);
769 ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r,
770 "auth_ldap authorize: no sec->host - weird...?");
775 * If there are no elements in the group attribute array, the default should be
776 * member and uniquemember; populate the array now.
778 if (sec->groupattr->nelts == 0) {
779 struct mod_auth_ldap_groupattr_entry_t *grp;
781 apr_thread_mutex_lock(sec->lock);
783 grp = apr_array_push(sec->groupattr);
784 grp->name = "member";
785 grp = apr_array_push(sec->groupattr);
786 grp->name = "uniqueMember";
788 apr_thread_mutex_unlock(sec->lock);
793 * If there are no elements in the sub group classes array, the default
794 * should be groupOfNames and groupOfUniqueNames; populate the array now.
796 if (sec->subgroupclasses->nelts == 0) {
797 struct mod_auth_ldap_groupattr_entry_t *grp;
799 apr_thread_mutex_lock(sec->lock);
801 grp = apr_array_push(sec->subgroupclasses);
802 grp->name = "groupOfNames";
803 grp = apr_array_push(sec->subgroupclasses);
804 grp->name = "groupOfUniqueNames";
806 apr_thread_mutex_unlock(sec->lock);
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
816 /* Check that we have a userid to start with */
818 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
819 "access to %s failed, reason: no authenticated user", r->uri);
823 if (!strlen(r->user)) {
824 ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r,
825 "ldap authorize: Userid is blank, AuthType=%s",
830 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
831 "ldap authorize: Creating LDAP req structure");
833 /* Build the username filter */
834 authn_ldap_build_filter(filtbuf, r, r->user, NULL, sec);
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));
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);
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);
854 ent = (struct mod_auth_ldap_groupattr_entry_t *) sec->groupattr->elts;
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",
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. */
875 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
876 "auth_ldap authorize: require group: testing for group "
877 "membership in \"%s\"",
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: "
885 sec->group_attrib_is_dn ? req->dn : req->user, t);
887 result = util_ldap_cache_compare(r, ldc, sec->url, t, ent[i].name,
888 sec->group_attrib_is_dn ? req->dn : req->user);
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) "
895 ent[i].name, ldc->reason, result,
896 ldap_err2string(result));
897 set_request_vars(r, LDAP_AUTHZ);
898 return AUTHZ_GRANTED;
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);
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);
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));
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;
932 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
933 "auth_ldap authorise: require group "
934 "(sub-group) \"%s\": authorisation failed "
936 t, ldc->reason, result,
937 ldap_err2string(result));
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));
950 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
951 "auth_ldap authorize group: authorization denied for "
958 static authz_status ldapdn_check_authorization(request_rec *r,
959 const char *require_args)
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);
967 util_ldap_connection_t *ldc = NULL;
971 char filtbuf[FILTER_LENGTH];
972 const char *dn = NULL;
974 if (!sec->have_ldap_url) {
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);
985 ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r,
986 "auth_ldap authorize: no sec->host - weird...?");
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
996 /* Check that we have a userid to start with */
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;
1003 if (!strlen(r->user)) {
1004 ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r,
1005 "ldap authorize: Userid is blank, AuthType=%s",
1010 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1011 "ldap authorize: Creating LDAP req structure");
1013 /* Build the username filter */
1014 authn_ldap_build_filter(filtbuf, r, r->user, NULL, sec);
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));
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;
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;
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;
1043 result = util_ldap_cache_comparedn(r, ldc, sec->url, req->dn, t, sec->compare_dn_on_server);
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;
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));
1061 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1062 "auth_ldap authorize dn: authorization denied for "
1066 return AUTHZ_DENIED;
1069 static authz_status ldapattribute_check_authorization(request_rec *r,
1070 const char *require_args)
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);
1078 util_ldap_connection_t *ldc = NULL;
1083 char filtbuf[FILTER_LENGTH];
1084 const char *dn = NULL;
1086 if (!sec->have_ldap_url) {
1087 return AUTHZ_DENIED;
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);
1097 ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r,
1098 "auth_ldap authorize: no sec->host - weird...?");
1099 return AUTHZ_DENIED;
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
1108 /* Check that we have a userid to start with */
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;
1115 if (!strlen(r->user)) {
1116 ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r,
1117 "ldap authorize: Userid is blank, AuthType=%s",
1122 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1123 "ldap authorize: Creating LDAP req structure");
1125 /* Build the username filter */
1126 authn_ldap_build_filter(filtbuf, r, r->user, NULL, sec);
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));
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;
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;
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;
1155 w = ap_getword(r->pool, &t, '=');
1156 value = ap_getword_conf(r->pool, &t);
1158 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1159 "auth_ldap authorize: checking attribute %s has value %s",
1161 result = util_ldap_cache_compare(r, ldc, sec->url, req->dn, w, value);
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;
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));
1179 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1180 "auth_ldap authorize attribute: authorization denied for "
1184 return AUTHZ_DENIED;
1187 static authz_status ldapfilter_check_authorization(request_rec *r,
1188 const char *require_args)
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);
1196 util_ldap_connection_t *ldc = NULL;
1199 char filtbuf[FILTER_LENGTH];
1200 const char *dn = NULL;
1202 if (!sec->have_ldap_url) {
1203 return AUTHZ_DENIED;
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);
1213 ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r,
1214 "auth_ldap authorize: no sec->host - weird...?");
1215 return AUTHZ_DENIED;
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
1224 /* Check that we have a userid to start with */
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;
1231 if (!strlen(r->user)) {
1232 ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r,
1233 "ldap authorize: Userid is blank, AuthType=%s",
1238 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1239 "ldap authorize: Creating LDAP req structure");
1241 /* Build the username filter */
1242 authn_ldap_build_filter(filtbuf, r, r->user, NULL, sec);
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));
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;
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;
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;
1272 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1273 "auth_ldap authorize: checking filter %s", t);
1275 /* Build the username filter */
1276 authn_ldap_build_filter(filtbuf, r, req->user, t, sec);
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));
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);
1292 result = util_ldap_cache_comparedn(r, ldc, sec->url, req->dn, dn,
1293 sec->compare_dn_on_server);
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;
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));
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));
1320 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1321 "auth_ldap authorize filter: authorization denied for "
1325 return AUTHZ_DENIED;
1330 * Use the ldap url parsing routines to break up the ldap url into
1333 static const char *mod_auth_ldap_parse_url(cmd_parms *cmd,
1339 apr_ldap_url_desc_t *urld;
1340 apr_ldap_err_t *result;
1342 authn_ldap_config_t *sec = config;
1344 rc = apr_ldap_url_parse(cmd->pool, url, &(urld), &(result));
1345 if (rc != APR_SUCCESS) {
1346 return result->reason;
1348 sec->url = apr_pstrdup(cmd->pool, url);
1350 /* Set all the values, or at least some sane defaults */
1352 sec->host = apr_pstrcat(cmd->pool, urld->lud_host, " ", sec->host, NULL);
1355 sec->host = urld->lud_host? apr_pstrdup(cmd->pool, urld->lud_host) : "localhost";
1357 sec->basedn = urld->lud_dn? apr_pstrdup(cmd->pool, urld->lud_dn) : "";
1358 if (urld->lud_attrs && urld->lud_attrs[0]) {
1360 while (urld->lud_attrs[i]) {
1363 sec->attributes = apr_pcalloc(cmd->pool, sizeof(char *) * (i+1));
1365 while (urld->lud_attrs[i]) {
1366 sec->attributes[i] = apr_pstrdup(cmd->pool, urld->lud_attrs[i]);
1369 sec->attribute = sec->attributes[0];
1372 sec->attribute = "uid";
1375 sec->scope = urld->lud_scope == LDAP_SCOPE_ONELEVEL ?
1376 LDAP_SCOPE_ONELEVEL : LDAP_SCOPE_SUBTREE;
1378 if (urld->lud_filter) {
1379 if (urld->lud_filter[0] == '(') {
1381 * Get rid of the surrounding parens; later on when generating the
1382 * filter, they'll be put back.
1384 sec->filter = apr_pstrdup(cmd->pool, urld->lud_filter+1);
1385 sec->filter[strlen(sec->filter)-1] = '\0';
1388 sec->filter = apr_pstrdup(cmd->pool, urld->lud_filter);
1392 sec->filter = "objectclass=*";
1396 if (0 == strcasecmp("NONE", mode)) {
1397 sec->secure = APR_LDAP_NONE;
1399 else if (0 == strcasecmp("SSL", mode)) {
1400 sec->secure = APR_LDAP_SSL;
1402 else if (0 == strcasecmp("TLS", mode) || 0 == strcasecmp("STARTTLS", mode)) {
1403 sec->secure = APR_LDAP_STARTTLS;
1406 return "Invalid LDAP connection mode setting: must be one of NONE, "
1407 "SSL, or TLS/STARTTLS";
1411 /* "ldaps" indicates secure ldap connections desired
1413 if (strncasecmp(url, "ldaps", 5) == 0)
1415 sec->secure = APR_LDAP_SSL;
1416 sec->port = urld->lud_port? urld->lud_port : LDAPS_PORT;
1420 sec->port = urld->lud_port? urld->lud_port : LDAP_PORT;
1423 sec->have_ldap_url = 1;
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",
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"),
1437 sec->secure == APR_LDAP_SSL ? "using SSL": "not using SSL"
1443 static const char *mod_auth_ldap_set_deref(cmd_parms *cmd, void *config, const char *arg)
1445 authn_ldap_config_t *sec = config;
1447 if (strcmp(arg, "never") == 0 || strcasecmp(arg, "off") == 0) {
1450 else if (strcmp(arg, "searching") == 0) {
1451 sec->deref = searching;
1453 else if (strcmp(arg, "finding") == 0) {
1454 sec->deref = finding;
1456 else if (strcmp(arg, "always") == 0 || strcasecmp(arg, "on") == 0) {
1457 sec->deref = always;
1460 return "Unrecognized value for AuthLDAPAliasDereference directive";
1465 static const char *mod_auth_ldap_add_subgroup_attribute(cmd_parms *cmd, void *config, const char *arg)
1469 authn_ldap_config_t *sec = config;
1471 for (i = 0; sec->sgAttributes[i]; i++) {
1474 if (i == GROUPATTR_MAX_ELTS)
1475 return "Too many AuthLDAPSubGroupAttribute values";
1477 sec->sgAttributes[i] = apr_pstrdup(cmd->pool, arg);
1482 static const char *mod_auth_ldap_add_subgroup_class(cmd_parms *cmd, void *config, const char *arg)
1484 struct mod_auth_ldap_groupattr_entry_t *new;
1486 authn_ldap_config_t *sec = config;
1488 if (sec->subgroupclasses->nelts > GROUPATTR_MAX_ELTS)
1489 return "Too many AuthLDAPSubGroupClass values";
1491 new = apr_array_push(sec->subgroupclasses);
1492 new->name = apr_pstrdup(cmd->pool, arg);
1497 static const char *mod_auth_ldap_set_subgroup_maxdepth(cmd_parms *cmd,
1499 const char *max_depth)
1501 authn_ldap_config_t *sec = config;
1503 sec->maxNestingDepth = atol(max_depth);
1508 static const char *mod_auth_ldap_add_group_attribute(cmd_parms *cmd, void *config, const char *arg)
1510 struct mod_auth_ldap_groupattr_entry_t *new;
1512 authn_ldap_config_t *sec = config;
1514 if (sec->groupattr->nelts > GROUPATTR_MAX_ELTS)
1515 return "Too many AuthLDAPGroupAttribute directives";
1517 new = apr_array_push(sec->groupattr);
1518 new->name = apr_pstrdup(cmd->pool, arg);
1523 static const char *set_charset_config(cmd_parms *cmd, void *config, const char *arg)
1525 ap_set_module_config(cmd->server->module_config, &authnz_ldap_module,
1530 static const char *set_bind_pattern(cmd_parms *cmd, void *_cfg, const char *exp, const char *subst)
1532 authn_ldap_config_t *sec = _cfg;
1535 regexp = ap_pregcomp(cmd->pool, exp, AP_REG_EXTENDED);
1538 return apr_pstrcat(cmd->pool, "AuthLDAPInitialBindPattern: cannot compile regular "
1539 "expression '", exp, "'", NULL);
1542 sec->bind_regex = regexp;
1543 sec->bind_subst = apr_pstrdup(cmd->pool, subst);
1548 static const command_rec authnz_ldap_cmds[] =
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"
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"
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>. "
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>."),
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."),
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."),
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."),
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."),
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."),
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."),
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"),
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"),
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)"),
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"),
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'."),
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."),
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."),
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 "'"),
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"),
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"),
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."),
1659 static int authnz_ldap_post_config(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s)
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;
1668 authn_ldap_config_t *sec = (authn_ldap_config_t *)
1669 ap_get_module_config(s->module_config,
1670 &authnz_ldap_module);
1674 if (!util_ldap_ssl_supported(s))
1676 ap_log_error(APLOG_MARK, APLOG_CRIT, 0, s,
1677 "LDAP: SSL connections (ldaps://) not supported by utilLDAP");
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;
1692 if (!charset_confname) {
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;
1704 if ((status = ap_pcfg_openfile(&f, ptemp, charset_confname))
1706 ap_log_error(APLOG_MARK, APLOG_ERR, status, s,
1707 "could not open charset conversion config file %s.",
1709 return HTTP_INTERNAL_SERVER_ERROR;
1712 charset_conversions = apr_hash_make(p);
1714 while (!(ap_cfg_getline(l, MAX_STRING_LEN, f))) {
1721 lang = ap_getword_conf(p, &ll);
1722 ap_str_tolower(lang);
1725 char *charset = ap_getword_conf(p, &ll);
1726 apr_hash_set(charset_conversions, lang, APR_HASH_KEY_STRING, charset);
1729 ap_cfg_closefile(f);
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.",
1736 return HTTP_INTERNAL_SERVER_ERROR;
1742 static const authn_provider authn_ldap_provider =
1744 &authn_ldap_check_password,
1747 static const authz_provider authz_ldapuser_provider =
1749 &ldapuser_check_authorization,
1751 static const authz_provider authz_ldapgroup_provider =
1753 &ldapgroup_check_authorization,
1756 static const authz_provider authz_ldapdn_provider =
1758 &ldapdn_check_authorization,
1761 static const authz_provider authz_ldapattribute_provider =
1763 &ldapattribute_check_authorization,
1766 static const authz_provider authz_ldapfilter_provider =
1768 &ldapfilter_check_authorization,
1771 static void ImportULDAPOptFn(void)
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);
1783 static void register_hooks(apr_pool_t *p)
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);
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);
1812 ap_hook_post_config(authnz_ldap_post_config,NULL,NULL,APR_HOOK_MIDDLE);
1814 ap_hook_optional_fn_retrieve(ImportULDAPOptFn,NULL,NULL,APR_HOOK_MIDDLE);
1817 AP_DECLARE_MODULE(authnz_ldap) =
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 */