1 /* Copyright 2001-2004 The Apache Software Foundation
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
17 * mod_auth_ldap.c: LDAP authentication module
19 * Original code from auth_ldap module for Apache v1.3:
20 * Copyright 1998, 1999 Enbridge Pipelines Inc.
21 * Copyright 1999-2001 Dave Carrigan
25 #include <apr_strings.h>
26 #include <apr_xlate.h>
27 #define APR_WANT_STRFUNC
30 #include "ap_config.h"
38 #include "http_config.h"
39 #include "http_core.h"
41 #include "http_protocol.h"
42 #include "http_request.h"
43 #include "util_ldap.h"
46 #error mod_auth_ldap requires APR-util to have LDAP support built in
49 /* per directory configuration */
51 apr_pool_t *pool; /* Pool that this config is allocated from */
53 apr_thread_mutex_t *lock; /* Lock for this config */
55 int auth_authoritative; /* Is this auth method the one and only? */
56 int enabled; /* Is auth_ldap enabled in this directory? */
58 /* These parameters are all derived from the AuthLDAPURL directive */
59 char *url; /* String representation of the URL */
61 char *host; /* Name of the LDAP server (or space separated list) */
62 int port; /* Port of the LDAP server */
63 char *basedn; /* Base DN to do all searches from */
64 char *attribute; /* Attribute to search for */
65 char **attributes; /* Array of all the attributes to return */
66 int scope; /* Scope of the search */
67 char *filter; /* Filter to further limit the search */
68 deref_options deref; /* how to handle alias dereferening */
69 char *binddn; /* DN to bind to server (can be NULL) */
70 char *bindpw; /* Password to bind to server (can be NULL) */
72 int frontpage_hack; /* Hack for frontpage support */
73 int user_is_dn; /* If true, connection->user is DN instead of userid */
74 int compare_dn_on_server; /* If true, will use server to do DN compare */
76 int have_ldap_url; /* Set if we have found an LDAP url */
78 apr_array_header_t *groupattr; /* List of Group attributes */
79 int group_attrib_is_dn; /* If true, the group attribute is the DN, otherwise,
80 it's the exact string passed by the HTTP client */
82 int secure; /* True if SSL connections are requested */
83 } mod_auth_ldap_config_t;
85 typedef struct mod_auth_ldap_request_t {
86 char *dn; /* The saved dn from a successful search */
87 char *user; /* The username provided by the client */
88 } mod_auth_ldap_request_t;
90 /* maximum group elements supported */
91 #define GROUPATTR_MAX_ELTS 10
93 struct mod_auth_ldap_groupattr_entry_t {
97 module AP_MODULE_DECLARE_DATA auth_ldap_module;
99 /* function prototypes */
100 void mod_auth_ldap_build_filter(char *filtbuf,
102 mod_auth_ldap_config_t *sec);
103 int mod_auth_ldap_check_user_id(request_rec *r);
104 int mod_auth_ldap_auth_checker(request_rec *r);
105 void *mod_auth_ldap_create_dir_config(apr_pool_t *p, char *d);
107 /* ---------------------------------------- */
109 static apr_hash_t *charset_conversions = NULL;
110 static char *to_charset = NULL; /* UTF-8 identifier derived from the charset.conv file */
112 /* Derive a code page ID give a language name or ID */
113 static char* derive_codepage_from_lang (apr_pool_t *p, char *language)
118 if (!language) /* our default codepage */
119 return apr_pstrdup(p, "ISO-8859-1");
121 lang_len = strlen(language);
123 charset = (char*) apr_hash_get(charset_conversions, language, APR_HASH_KEY_STRING);
127 charset = (char*) apr_hash_get(charset_conversions, language, APR_HASH_KEY_STRING);
131 charset = apr_pstrdup(p, charset);
137 static apr_xlate_t* get_conv_set (request_rec *r)
139 char *lang_line = (char*)apr_table_get(r->headers_in, "accept-language");
141 apr_xlate_t *convset;
144 lang_line = apr_pstrdup(r->pool, lang_line);
145 for (lang = lang_line;*lang;lang++) {
146 if ((*lang == ',') || (*lang == ';')) {
151 lang = derive_codepage_from_lang(r->pool, lang_line);
153 if (lang && (apr_xlate_open(&convset, to_charset, lang, r->pool) == APR_SUCCESS)) {
163 * Build the search filter, or at least as much of the search filter that
164 * will fit in the buffer. We don't worry about the buffer not being able
165 * to hold the entire filter. If the buffer wasn't big enough to hold the
166 * filter, ldap_search_s will complain, but the only situation where this
167 * is likely to happen is if the client sent a really, really long
168 * username, most likely as part of an attack.
170 * The search filter consists of the filter provided with the URL,
171 * combined with a filter made up of the attribute provided with the URL,
172 * and the actual username passed by the HTTP client. For example, assume
173 * that the LDAP URL is
175 * ldap://ldap.airius.com/ou=People, o=Airius?uid??(posixid=*)
177 * Further, assume that the userid passed by the client was `userj'. The
178 * search filter will be (&(posixid=*)(uid=userj)).
180 #define FILTER_LENGTH MAX_STRING_LEN
181 void mod_auth_ldap_build_filter(char *filtbuf,
183 mod_auth_ldap_config_t *sec)
185 char *p, *q, *filtbuf_end;
187 apr_xlate_t *convset = NULL;
192 if (r->user != NULL) {
193 user = apr_pstrdup (r->pool, r->user);
198 if (charset_conversions) {
199 convset = get_conv_set(r);
203 inbytes = strlen(user);
204 outbytes = (inbytes+1)*3;
205 outbuf = apr_pcalloc(r->pool, outbytes);
207 /* Convert the user name to UTF-8. This is only valid for LDAP v3 */
208 if (apr_xlate_conv_buffer(convset, user, &inbytes, outbuf, &outbytes) == APR_SUCCESS) {
209 user = apr_pstrdup(r->pool, outbuf);
214 * Create the first part of the filter, which consists of the
215 * config-supplied portions.
217 apr_snprintf(filtbuf, FILTER_LENGTH, "(&(%s)(%s=", sec->filter, sec->attribute);
220 * Now add the client-supplied username to the filter, ensuring that any
221 * LDAP filter metachars are escaped.
223 filtbuf_end = filtbuf + FILTER_LENGTH - 1;
224 for (p = user, q=filtbuf + strlen(filtbuf);
225 *p && q < filtbuf_end; *q++ = *p++) {
226 #if APR_HAS_MICROSOFT_LDAPSDK
227 /* Note: The Microsoft SDK escapes for us, so is not necessary */
229 if (strchr("*()\\", *p) != NULL) {
231 if (q >= filtbuf_end) {
240 * Append the closing parens of the filter, unless doing so would
241 * overrun the buffer.
243 if (q + 2 <= filtbuf_end)
244 strcat(filtbuf, "))");
247 static apr_status_t mod_auth_ldap_cleanup_connection_close(void *param)
249 util_ldap_connection_t *ldc = param;
250 util_ldap_connection_close(ldc);
256 * Authentication Phase
257 * --------------------
259 * This phase authenticates the credentials the user has sent with
260 * the request (ie the username and password are checked). This is done
261 * by making an attempt to bind to the LDAP server using this user's
262 * DN and the supplied password.
265 int mod_auth_ldap_check_user_id(request_rec *r)
268 const char **vals = NULL;
269 char filtbuf[FILTER_LENGTH];
270 mod_auth_ldap_config_t *sec =
271 (mod_auth_ldap_config_t *)ap_get_module_config(r->per_dir_config, &auth_ldap_module);
273 util_ldap_connection_t *ldc = NULL;
276 const char *dn = NULL;
278 mod_auth_ldap_request_t *req =
279 (mod_auth_ldap_request_t *)apr_pcalloc(r->pool, sizeof(mod_auth_ldap_request_t));
280 ap_set_module_config(r->request_config, &auth_ldap_module, req);
287 * Basic sanity checks before any LDAP operations even happen.
289 if (!sec->have_ldap_url) {
295 /* There is a good AuthLDAPURL, right? */
297 ldc = util_ldap_connection_find(r, sec->host, sec->port,
298 sec->binddn, sec->bindpw, sec->deref,
302 ap_log_rerror(APLOG_MARK, APLOG_WARNING|APLOG_NOERRNO, 0, r,
303 "[%d] auth_ldap authenticate: no sec->host - weird...?", getpid());
304 return sec->auth_authoritative? HTTP_UNAUTHORIZED : DECLINED;
307 ap_log_rerror(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, r,
308 "[%d] auth_ldap authenticate: using URL %s", getpid(), sec->url);
310 /* Get the password that the client sent */
311 if ((result = ap_get_basic_auth_pw(r, &sent_pw))) {
312 ap_log_rerror(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, r,
313 "[%d] auth_ldap authenticate: "
314 "ap_get_basic_auth_pw() returns %d", getpid(), result);
315 util_ldap_connection_close(ldc);
319 if (r->user == NULL) {
320 ap_log_rerror(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, r,
321 "[%d] auth_ldap authenticate: no user specified", getpid());
322 util_ldap_connection_close(ldc);
323 return sec->auth_authoritative? HTTP_UNAUTHORIZED : DECLINED;
326 /* build the username filter */
327 mod_auth_ldap_build_filter(filtbuf, r, sec);
329 /* do the user search */
330 result = util_ldap_cache_checkuserid(r, ldc, sec->url, sec->basedn, sec->scope,
331 sec->attributes, filtbuf, sent_pw, &dn, &vals);
332 util_ldap_connection_close(ldc);
334 /* sanity check - if server is down, retry it up to 5 times */
335 if (result == LDAP_SERVER_DOWN) {
336 if (failures++ <= 5) {
341 /* handle bind failure */
342 if (result != LDAP_SUCCESS) {
343 ap_log_rerror(APLOG_MARK, APLOG_WARNING|APLOG_NOERRNO, 0, r,
344 "[%d] auth_ldap authenticate: "
345 "user %s authentication failed; URI %s [%s][%s]",
346 getpid(), r->user, r->uri, ldc->reason, ldap_err2string(result));
347 if ((LDAP_INVALID_CREDENTIALS == result) || sec->auth_authoritative) {
348 ap_note_basic_auth_failure(r);
349 return HTTP_UNAUTHORIZED;
356 /* mark the user and DN */
357 req->dn = apr_pstrdup(r->pool, dn);
359 if (sec->user_is_dn) {
363 /* add environment variables */
364 if (sec->attributes && vals) {
365 apr_table_t *e = r->subprocess_env;
367 while (sec->attributes[i]) {
368 char *str = apr_pstrcat(r->pool, "AUTHENTICATE_", sec->attributes[i], NULL);
371 if (str[j] >= 'a' && str[j] <= 'z') {
372 str[j] = str[j] - ('a' - 'A');
376 apr_table_setn(e, str, vals[i]);
381 ap_log_rerror(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, r,
382 "[%d] auth_ldap authenticate: accepting %s", getpid(), r->user);
389 * Authorisation Phase
390 * -------------------
392 * After checking whether the username and password are correct, we need
393 * to check whether that user is authorised to view this resource. The
394 * require directive is used to do this:
396 * require valid-user Any authenticated is allowed in.
397 * require user <username> This particular user is allowed in.
398 * require group <groupname> The user must be a member of this group
399 * in order to be allowed in.
400 * require dn <dn> The user must have the following DN in the
401 * LDAP tree to be let in.
404 int mod_auth_ldap_auth_checker(request_rec *r)
407 mod_auth_ldap_request_t *req =
408 (mod_auth_ldap_request_t *)ap_get_module_config(r->request_config,
410 mod_auth_ldap_config_t *sec =
411 (mod_auth_ldap_config_t *)ap_get_module_config(r->per_dir_config,
414 util_ldap_connection_t *ldc = NULL;
415 int m = r->method_number;
417 const apr_array_header_t *reqs_arr = ap_requires(r);
418 require_line *reqs = reqs_arr ? (require_line *)reqs_arr->elts : NULL;
423 int method_restricted = 0;
429 if (!sec->have_ldap_url) {
434 ldc = util_ldap_connection_find(r, sec->host, sec->port,
435 sec->binddn, sec->bindpw, sec->deref,
437 apr_pool_cleanup_register(r->pool, ldc,
438 mod_auth_ldap_cleanup_connection_close,
439 apr_pool_cleanup_null);
442 ap_log_rerror(APLOG_MARK, APLOG_WARNING|APLOG_NOERRNO, 0, r,
443 "[%d] auth_ldap authorise: no sec->host - weird...?", getpid());
444 return sec->auth_authoritative? HTTP_UNAUTHORIZED : DECLINED;
448 * If there are no elements in the group attribute array, the default should be
449 * member and uniquemember; populate the array now.
451 if (sec->groupattr->nelts == 0) {
452 struct mod_auth_ldap_groupattr_entry_t *grp;
454 apr_thread_mutex_lock(sec->lock);
456 grp = apr_array_push(sec->groupattr);
457 grp->name = "member";
458 grp = apr_array_push(sec->groupattr);
459 grp->name = "uniquemember";
461 apr_thread_mutex_unlock(sec->lock);
466 ap_log_rerror(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, r,
467 "[%d] auth_ldap authorise: no requirements array", getpid());
468 return sec->auth_authoritative? HTTP_UNAUTHORIZED : DECLINED;
471 /* Loop through the requirements array until there's no elements
472 * left, or something causes a return from inside the loop */
473 for(x=0; x < reqs_arr->nelts; x++) {
474 if (! (reqs[x].method_mask & (1 << m))) {
477 method_restricted = 1;
479 t = reqs[x].requirement;
480 w = ap_getword_white(r->pool, &t);
482 if (strcmp(w, "valid-user") == 0) {
484 * Valid user will always be true if we authenticated with ldap,
485 * but when using front page, valid user should only be true if
486 * he exists in the frontpage password file. This hack will get
487 * auth_ldap to look up the user in the the pw file to really be
488 * sure that he's valid. Naturally, it requires mod_auth to be
489 * compiled in, but if mod_auth wasn't in there, then the need
490 * for this hack wouldn't exist anyway.
492 if (sec->frontpage_hack) {
493 ap_log_rerror(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, r,
494 "[%d] auth_ldap authorise: "
495 "deferring authorisation to mod_auth (FP Hack)",
500 ap_log_rerror(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, r,
501 "[%d] auth_ldap authorise: "
502 "successful authorisation because user "
503 "is valid-user", getpid());
507 else if (strcmp(w, "user") == 0) {
508 if (req->dn == NULL || strlen(req->dn) == 0) {
509 ap_log_rerror(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, r,
510 "[%d] auth_ldap authorise: "
511 "require user: user's DN has not been defined; failing authorisation",
513 return sec->auth_authoritative? HTTP_UNAUTHORIZED : DECLINED;
516 * First do a whole-line compare, in case it's something like
517 * require user Babs Jensen
519 result = util_ldap_cache_compare(r, ldc, sec->url, req->dn, sec->attribute, t);
521 case LDAP_COMPARE_TRUE: {
522 ap_log_rerror(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, r,
523 "[%d] auth_ldap authorise: "
524 "require user: authorisation successful", getpid());
528 ap_log_rerror(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, r,
529 "[%d] auth_ldap authorise: require user: "
530 "authorisation failed [%s][%s]", getpid(),
531 ldc->reason, ldap_err2string(result));
535 * Now break apart the line and compare each word on it
538 w = ap_getword_conf(r->pool, &t);
539 result = util_ldap_cache_compare(r, ldc, sec->url, req->dn, sec->attribute, w);
541 case LDAP_COMPARE_TRUE: {
542 ap_log_rerror(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, r,
543 "[%d] auth_ldap authorise: "
544 "require user: authorisation successful", getpid());
548 ap_log_rerror(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, r,
549 "[%d] auth_ldap authorise: "
550 "require user: authorisation failed [%s][%s]",
551 getpid(), ldc->reason, ldap_err2string(result));
556 else if (strcmp(w, "dn") == 0) {
557 if (req->dn == NULL || strlen(req->dn) == 0) {
558 ap_log_rerror(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, r,
559 "[%d] auth_ldap authorise: "
560 "require dn: user's DN has not been defined; failing authorisation",
562 return sec->auth_authoritative? HTTP_UNAUTHORIZED : DECLINED;
565 result = util_ldap_cache_comparedn(r, ldc, sec->url, req->dn, t, sec->compare_dn_on_server);
567 case LDAP_COMPARE_TRUE: {
568 ap_log_rerror(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, r,
569 "[%d] auth_ldap authorise: "
570 "require dn: authorisation successful", getpid());
574 ap_log_rerror(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, r,
575 "[%d] auth_ldap authorise: "
576 "require dn \"%s\": LDAP error [%s][%s]",
577 getpid(), t, ldc->reason, ldap_err2string(result));
581 else if (strcmp(w, "group") == 0) {
582 struct mod_auth_ldap_groupattr_entry_t *ent = (struct mod_auth_ldap_groupattr_entry_t *) sec->groupattr->elts;
585 if (sec->group_attrib_is_dn) {
586 if (req->dn == NULL || strlen(req->dn) == 0) {
587 ap_log_rerror(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, r,
588 "[%d] auth_ldap authorise: require group: user's DN has not been defined; failing authorisation",
590 return sec->auth_authoritative? HTTP_UNAUTHORIZED : DECLINED;
594 if (req->user == NULL || strlen(req->user) == 0) {
595 /* We weren't called in the authentication phase, so we didn't have a
596 * chance to set the user field. Do so now. */
601 ap_log_rerror(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, r,
602 "[%d] auth_ldap authorise: require group: testing for group membership in \"%s\"",
605 for (i = 0; i < sec->groupattr->nelts; i++) {
606 ap_log_rerror(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, r,
607 "[%d] auth_ldap authorise: require group: testing for %s: %s (%s)", getpid(),
608 ent[i].name, sec->group_attrib_is_dn ? req->dn : req->user, t);
610 result = util_ldap_cache_compare(r, ldc, sec->url, t, ent[i].name,
611 sec->group_attrib_is_dn ? req->dn : req->user);
613 case LDAP_COMPARE_TRUE: {
614 ap_log_rerror(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, r,
615 "[%d] auth_ldap authorise: require group: "
616 "authorisation successful (attribute %s) [%s][%s]",
617 getpid(), ent[i].name, ldc->reason, ldap_err2string(result));
621 ap_log_rerror(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, r,
622 "[%d] auth_ldap authorise: require group \"%s\": "
623 "authorisation failed [%s][%s]",
624 getpid(), t, ldc->reason, ldap_err2string(result));
631 if (!method_restricted) {
632 ap_log_rerror(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, r,
633 "[%d] auth_ldap authorise: agreeing because non-restricted",
638 if (!sec->auth_authoritative) {
639 ap_log_rerror(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, r,
640 "[%d] auth_ldap authorise: declining to authorise", getpid());
644 ap_log_rerror(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, r,
645 "[%d] auth_ldap authorise: authorisation denied", getpid());
646 ap_note_basic_auth_failure (r);
648 return HTTP_UNAUTHORIZED;
652 /* ---------------------------------------- */
653 /* config directives */
656 void *mod_auth_ldap_create_dir_config(apr_pool_t *p, char *d)
658 mod_auth_ldap_config_t *sec =
659 (mod_auth_ldap_config_t *)apr_pcalloc(p, sizeof(mod_auth_ldap_config_t));
663 apr_thread_mutex_create(&sec->lock, APR_THREAD_MUTEX_DEFAULT, p);
665 sec->auth_authoritative = 1;
667 sec->groupattr = apr_array_make(p, GROUPATTR_MAX_ELTS,
668 sizeof(struct mod_auth_ldap_groupattr_entry_t));
670 sec->have_ldap_url = 0;
676 sec->group_attrib_is_dn = 1;
678 sec->frontpage_hack = 0;
682 sec->compare_dn_on_server = 0;
688 * Use the ldap url parsing routines to break up the ldap url into
691 static const char *mod_auth_ldap_parse_url(cmd_parms *cmd,
696 apr_ldap_url_desc_t *urld;
698 mod_auth_ldap_config_t *sec = config;
700 ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0,
701 cmd->server, "[%d] auth_ldap url parse: `%s'",
704 result = apr_ldap_url_parse(url, &(urld));
705 if (result != LDAP_SUCCESS) {
707 case LDAP_URL_ERR_NOTLDAP:
708 return "LDAP URL does not begin with ldap://";
709 case LDAP_URL_ERR_NODN:
710 return "LDAP URL does not have a DN";
711 case LDAP_URL_ERR_BADSCOPE:
712 return "LDAP URL has an invalid scope";
713 case LDAP_URL_ERR_MEM:
714 return "Out of memory parsing LDAP URL";
716 return "Could not parse LDAP URL";
719 sec->url = apr_pstrdup(cmd->pool, url);
721 ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0,
722 cmd->server, "[%d] auth_ldap url parse: Host: %s", getpid(), urld->lud_host);
723 ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0,
724 cmd->server, "[%d] auth_ldap url parse: Port: %d", getpid(), urld->lud_port);
725 ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0,
726 cmd->server, "[%d] auth_ldap url parse: DN: %s", getpid(), urld->lud_dn);
727 ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0,
728 cmd->server, "[%d] auth_ldap url parse: attrib: %s", getpid(), urld->lud_attrs? urld->lud_attrs[0] : "(null)");
729 ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0,
730 cmd->server, "[%d] auth_ldap url parse: scope: %s", getpid(),
731 (urld->lud_scope == LDAP_SCOPE_SUBTREE? "subtree" :
732 urld->lud_scope == LDAP_SCOPE_BASE? "base" :
733 urld->lud_scope == LDAP_SCOPE_ONELEVEL? "onelevel" : "unknown"));
734 ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0,
735 cmd->server, "[%d] auth_ldap url parse: filter: %s", getpid(), urld->lud_filter);
737 /* Set all the values, or at least some sane defaults */
739 char *p = apr_palloc(cmd->pool, strlen(sec->host) + strlen(urld->lud_host) + 2);
740 strcpy(p, urld->lud_host);
742 strcat(p, sec->host);
746 sec->host = urld->lud_host? apr_pstrdup(cmd->pool, urld->lud_host) : "localhost";
748 sec->basedn = urld->lud_dn? apr_pstrdup(cmd->pool, urld->lud_dn) : "";
749 if (urld->lud_attrs && urld->lud_attrs[0]) {
751 while (urld->lud_attrs[i]) {
754 sec->attributes = apr_pcalloc(cmd->pool, sizeof(char *) * (i+1));
756 while (urld->lud_attrs[i]) {
757 sec->attributes[i] = apr_pstrdup(cmd->pool, urld->lud_attrs[i]);
760 sec->attribute = sec->attributes[0];
763 sec->attribute = "uid";
766 sec->scope = urld->lud_scope == LDAP_SCOPE_ONELEVEL ?
767 LDAP_SCOPE_ONELEVEL : LDAP_SCOPE_SUBTREE;
769 if (urld->lud_filter) {
770 if (urld->lud_filter[0] == '(') {
772 * Get rid of the surrounding parens; later on when generating the
773 * filter, they'll be put back.
775 sec->filter = apr_pstrdup(cmd->pool, urld->lud_filter+1);
776 sec->filter[strlen(sec->filter)-1] = '\0';
779 sec->filter = apr_pstrdup(cmd->pool, urld->lud_filter);
783 sec->filter = "objectclass=*";
786 /* "ldaps" indicates secure ldap connections desired
788 if (strncasecmp(url, "ldaps", 5) == 0)
791 sec->port = urld->lud_port? urld->lud_port : LDAPS_PORT;
792 ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, cmd->server,
793 "LDAP: auth_ldap using SSL connections");
798 sec->port = urld->lud_port? urld->lud_port : LDAP_PORT;
799 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, cmd->server,
800 "LDAP: auth_ldap not using SSL connections");
803 sec->have_ldap_url = 1;
804 apr_ldap_free_urldesc(urld);
808 static const char *mod_auth_ldap_set_deref(cmd_parms *cmd, void *config, const char *arg)
810 mod_auth_ldap_config_t *sec = config;
812 if (strcmp(arg, "never") == 0 || strcasecmp(arg, "off") == 0) {
815 else if (strcmp(arg, "searching") == 0) {
816 sec->deref = searching;
818 else if (strcmp(arg, "finding") == 0) {
819 sec->deref = finding;
821 else if (strcmp(arg, "always") == 0 || strcasecmp(arg, "on") == 0) {
825 return "Unrecognized value for AuthLDAPAliasDereference directive";
830 static const char *mod_auth_ldap_add_group_attribute(cmd_parms *cmd, void *config, const char *arg)
832 struct mod_auth_ldap_groupattr_entry_t *new;
834 mod_auth_ldap_config_t *sec = config;
836 if (sec->groupattr->nelts > GROUPATTR_MAX_ELTS)
837 return "Too many AuthLDAPGroupAttribute directives";
839 new = apr_array_push(sec->groupattr);
840 new->name = apr_pstrdup(cmd->pool, arg);
845 static const char *set_charset_config(cmd_parms *cmd, void *config, const char *arg)
847 ap_set_module_config(cmd->server->module_config, &auth_ldap_module,
853 command_rec mod_auth_ldap_cmds[] = {
854 AP_INIT_TAKE1("AuthLDAPURL", mod_auth_ldap_parse_url, NULL, OR_AUTHCFG,
855 "URL to define LDAP connection. This should be an RFC 2255 complaint\n"
856 "URL of the form ldap://host[:port]/basedn[?attrib[?scope[?filter]]].\n"
858 "<li>Host is the name of the LDAP server. Use a space separated list of hosts \n"
859 "to specify redundant servers.\n"
860 "<li>Port is optional, and specifies the port to connect to.\n"
861 "<li>basedn specifies the base DN to start searches from\n"
862 "<li>Attrib specifies what attribute to search for in the directory. If not "
863 "provided, it defaults to <b>uid</b>.\n"
864 "<li>Scope is the scope of the search, and can be either <b>sub</b> or "
865 "<b>one</b>. If not provided, the default is <b>sub</b>.\n"
866 "<li>Filter is a filter to use in the search. If not provided, "
867 "defaults to <b>(objectClass=*)</b>.\n"
869 "Searches are performed using the attribute and the filter combined. "
870 "For example, assume that the\n"
871 "LDAP URL is <b>ldap://ldap.airius.com/ou=People, o=Airius?uid?sub?(posixid=*)</b>. "
873 "be done using the filter <b>(&((posixid=*))(uid=<i>username</i>))</b>, "
874 "where <i>username</i>\n"
875 "is the user name passed by the HTTP client. The search will be a subtree "
876 "search on the branch <b>ou=People, o=Airius</b>."),
878 AP_INIT_TAKE1("AuthLDAPBindDN", ap_set_string_slot,
879 (void *)APR_OFFSETOF(mod_auth_ldap_config_t, binddn), OR_AUTHCFG,
880 "DN to use to bind to LDAP server. If not provided, will do an anonymous bind."),
882 AP_INIT_TAKE1("AuthLDAPBindPassword", ap_set_string_slot,
883 (void *)APR_OFFSETOF(mod_auth_ldap_config_t, bindpw), OR_AUTHCFG,
884 "Password to use to bind to LDAP server. If not provided, will do an anonymous bind."),
886 AP_INIT_FLAG("AuthLDAPRemoteUserIsDN", ap_set_flag_slot,
887 (void *)APR_OFFSETOF(mod_auth_ldap_config_t, user_is_dn), OR_AUTHCFG,
888 "Set to 'on' to set the REMOTE_USER environment variable to be the full "
889 "DN of the remote user. By default, this is set to off, meaning that "
890 "the REMOTE_USER variable will contain whatever value the remote user sent."),
892 AP_INIT_FLAG("AuthLDAPAuthoritative", ap_set_flag_slot,
893 (void *)APR_OFFSETOF(mod_auth_ldap_config_t, auth_authoritative), OR_AUTHCFG,
894 "Set to 'off' to allow access control to be passed along to lower modules if "
895 "the UserID and/or group is not known to this module"),
897 AP_INIT_FLAG("AuthLDAPCompareDNOnServer", ap_set_flag_slot,
898 (void *)APR_OFFSETOF(mod_auth_ldap_config_t, compare_dn_on_server), OR_AUTHCFG,
899 "Set to 'on' to force auth_ldap to do DN compares (for the \"require dn\" "
900 "directive) using the server, and set it 'off' to do the compares locally "
901 "(at the expense of possible false matches). See the documentation for "
902 "a complete description of this option."),
904 AP_INIT_ITERATE("AuthLDAPGroupAttribute", mod_auth_ldap_add_group_attribute, NULL, OR_AUTHCFG,
905 "A list of attributes used to define group membership - defaults to "
906 "member and uniquemember"),
908 AP_INIT_FLAG("AuthLDAPGroupAttributeIsDN", ap_set_flag_slot,
909 (void *)APR_OFFSETOF(mod_auth_ldap_config_t, group_attrib_is_dn), OR_AUTHCFG,
910 "If set to 'on', auth_ldap uses the DN that is retrieved from the server for"
911 "subsequent group comparisons. If set to 'off', auth_ldap uses the string"
912 "provided by the client directly. Defaults to 'on'."),
914 AP_INIT_TAKE1("AuthLDAPDereferenceAliases", mod_auth_ldap_set_deref, NULL, OR_AUTHCFG,
915 "Determines how aliases are handled during a search. Can bo one of the"
916 "values \"never\", \"searching\", \"finding\", or \"always\". "
917 "Defaults to always."),
919 AP_INIT_FLAG("AuthLDAPEnabled", ap_set_flag_slot,
920 (void *)APR_OFFSETOF(mod_auth_ldap_config_t, enabled), OR_AUTHCFG,
921 "Set to off to disable auth_ldap, even if it's been enabled in a higher tree"),
923 AP_INIT_FLAG("AuthLDAPFrontPageHack", ap_set_flag_slot,
924 (void *)APR_OFFSETOF(mod_auth_ldap_config_t, frontpage_hack), OR_AUTHCFG,
925 "Set to 'on' to support Microsoft FrontPage"),
927 AP_INIT_TAKE1("AuthLDAPCharsetConfig", set_charset_config, NULL, RSRC_CONF,
928 "Character set conversion configuration file. If omitted, character set"
929 "conversion is disabled."),
934 static int auth_ldap_post_config(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s)
937 char l[MAX_STRING_LEN];
938 const char *charset_confname = ap_get_module_config(s->module_config,
943 mod_auth_ldap_config_t *sec = (mod_auth_ldap_config_t *)
944 ap_get_module_config(s->module_config,
949 if (!util_ldap_ssl_supported(s))
951 ap_log_error(APLOG_MARK, APLOG_CRIT, 0, s,
952 "LDAP: SSL connections (ldaps://) not supported by utilLDAP");
958 /* make sure that mod_ldap (util_ldap) is loaded */
959 if (ap_find_linked_module("util_ldap.c") == NULL) {
960 ap_log_error(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, 0, s,
961 "Module mod_ldap missing. Mod_ldap (aka. util_ldap) "
962 "must be loaded in order for mod_auth_ldap to function properly");
963 return HTTP_INTERNAL_SERVER_ERROR;
967 if (!charset_confname) {
971 charset_confname = ap_server_root_relative(p, charset_confname);
972 if (!charset_confname) {
973 ap_log_error(APLOG_MARK, APLOG_ERR, APR_EBADPATH, s,
974 "Invalid charset conversion config path %s",
975 (const char *)ap_get_module_config(s->module_config,
977 return HTTP_INTERNAL_SERVER_ERROR;
979 if ((status = ap_pcfg_openfile(&f, ptemp, charset_confname))
981 ap_log_error(APLOG_MARK, APLOG_ERR, status, s,
982 "could not open charset conversion config file %s.",
984 return HTTP_INTERNAL_SERVER_ERROR;
987 charset_conversions = apr_hash_make(p);
989 while (!(ap_cfg_getline(l, MAX_STRING_LEN, f))) {
996 lang = ap_getword_conf(p, &ll);
997 ap_str_tolower(lang);
1000 char *charset = ap_getword_conf(p, &ll);
1001 apr_hash_set(charset_conversions, lang, APR_HASH_KEY_STRING, charset);
1004 ap_cfg_closefile(f);
1006 to_charset = derive_codepage_from_lang (p, "utf-8");
1007 if (to_charset == NULL) {
1008 ap_log_error(APLOG_MARK, APLOG_ERR, status, s,
1009 "could not find the UTF-8 charset in the file %s.",
1011 return HTTP_INTERNAL_SERVER_ERROR;
1017 static void mod_auth_ldap_register_hooks(apr_pool_t *p)
1019 ap_hook_post_config(auth_ldap_post_config,NULL,NULL,APR_HOOK_MIDDLE);
1020 ap_hook_check_user_id(mod_auth_ldap_check_user_id, NULL, NULL, APR_HOOK_MIDDLE);
1021 ap_hook_auth_checker(mod_auth_ldap_auth_checker, NULL, NULL, APR_HOOK_MIDDLE);
1024 module auth_ldap_module = {
1025 STANDARD20_MODULE_STUFF,
1026 mod_auth_ldap_create_dir_config, /* dir config creater */
1027 NULL, /* dir merger --- default is to override */
1028 NULL, /* server config */
1029 NULL, /* merge server config */
1030 mod_auth_ldap_cmds, /* command table */
1031 mod_auth_ldap_register_hooks, /* set up request processing hooks */