2 * rlm_ldap.c LDAP authorization and authentication module.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18 * Copyright 1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,
19 * 2009,2010,2011,1012 The FreeRADIUS Server Project.
21 * Copyright 2012 Alan DeKok <aland@freeradius.org>
24 #include <freeradius-devel/ident.h>
27 #include <freeradius-devel/radiusd.h>
28 #include <freeradius-devel/modules.h>
29 #include <freeradius-devel/rad_assert.h>
37 #define MAX_ATTRMAP 128
38 #define MAX_ATTR_STR_LEN 256
39 #define MAX_FILTER_STR_LEN 1024
43 fr_connection_pool_t *pool;
57 int ldap_debug; /* Debug flag for LDAP SDK */
59 const char *xlat_name; /* name used to xlat */
64 * RADIUS attribute to LDAP attribute maps
66 VALUE_PAIR_MAP *user_map; /* Applied to users and profiles */
71 * Access related configuration
74 int positive_access_attr;
80 char *default_profile;
87 char *groupmemb_filter;
91 * TLS items. We should really normalize these with the
101 char *tls_require_cert;
114 #ifdef LDAP_OPT_X_KEEPALIVE_IDLE
117 #ifdef LDAP_OPT_X_KEEPALIVE_PROBES
118 int keepalive_probes;
120 #ifdef LDAP_OPT_ERROR_NUMBER
121 int keepalive_interval;
126 /* The default setting for TLS Certificate Verification */
127 #define TLS_DEFAULT_VERIFY "allow"
132 static CONF_PARSER tls_config[] = {
133 {"start_tls", PW_TYPE_BOOLEAN,
134 offsetof(ldap_instance,start_tls), NULL, "no"},
135 {"cacertfile", PW_TYPE_FILENAME,
136 offsetof(ldap_instance,tls_cacertfile), NULL, NULL},
137 {"cacertdir", PW_TYPE_FILENAME,
138 offsetof(ldap_instance,tls_cacertdir), NULL, NULL},
139 {"certfile", PW_TYPE_FILENAME,
140 offsetof(ldap_instance,tls_certfile), NULL, NULL},
141 {"keyfile", PW_TYPE_FILENAME,
142 offsetof(ldap_instance,tls_keyfile), NULL, NULL},
143 {"randfile", PW_TYPE_STRING_PTR, /* OK if it changes on HUP */
144 offsetof(ldap_instance,tls_randfile), NULL, NULL},
145 {"require_cert", PW_TYPE_STRING_PTR,
146 offsetof(ldap_instance,tls_require_cert), NULL, TLS_DEFAULT_VERIFY},
147 { NULL, -1, 0, NULL, NULL }
151 static CONF_PARSER attr_config[] = {
155 /* LDAP attribute name that controls remote access */
156 {"access_attr", PW_TYPE_STRING_PTR,
157 offsetof(ldap_instance,access_attr), NULL, NULL},
158 {"positive_access_attr", PW_TYPE_BOOLEAN,
159 offsetof(ldap_instance,positive_access_attr), NULL, "yes"},
161 {"base_filter", PW_TYPE_STRING_PTR,
162 offsetof(ldap_instance,base_filter), NULL,
163 "(objectclass=radiusprofile)"},
164 {"default_profile", PW_TYPE_STRING_PTR,
165 offsetof(ldap_instance,default_profile), NULL, NULL},
166 {"profile_attribute", PW_TYPE_STRING_PTR,
167 offsetof(ldap_instance,profile_attr), NULL, NULL},
169 { NULL, -1, 0, NULL, NULL }
174 * Group configuration
176 static CONF_PARSER group_config[] = {
178 * Group checks. These could probably be done
179 * via dynamic xlat's.
181 {"name_attribute", PW_TYPE_STRING_PTR,
182 offsetof(ldap_instance,groupname_attr), NULL, "cn"},
183 {"membership_filter", PW_TYPE_STRING_PTR,
184 offsetof(ldap_instance,groupmemb_filter), NULL,
185 "(|(&(objectClass=GroupOfNames)(member=%{Ldap-UserDn}))"
186 "(&(objectClass=GroupOfUniqueNames)(uniquemember=%{Ldap-UserDn})))"},
187 {"membership_attribute", PW_TYPE_STRING_PTR,
188 offsetof(ldap_instance,groupmemb_attr), NULL, NULL},
191 { NULL, -1, 0, NULL, NULL }
195 * Various options that don't belong in the main configuration.
197 * Note that these overlap a bit with the connection pool code!
199 static CONF_PARSER option_config[] = {
201 * Debugging flags to the server
203 {"ldap_debug", PW_TYPE_INTEGER,
204 offsetof(ldap_instance,ldap_debug), NULL, "0x0000"},
206 {"chase_referrals", PW_TYPE_BOOLEAN,
207 offsetof(ldap_instance,chase_referrals), NULL, NULL},
209 {"rebind", PW_TYPE_BOOLEAN,
210 offsetof(ldap_instance,rebind), NULL, NULL},
212 /* timeout on network activity */
213 {"net_timeout", PW_TYPE_INTEGER,
214 offsetof(ldap_instance,net_timeout), NULL, "10"},
216 /* timeout for search results */
217 {"timeout", PW_TYPE_INTEGER,
218 offsetof(ldap_instance,timeout), NULL, "20"},
220 /* allow server unlimited time for search (server-side limit) */
221 {"timelimit", PW_TYPE_INTEGER,
222 offsetof(ldap_instance,timelimit), NULL, "20"},
224 #ifdef LDAP_OPT_X_KEEPALIVE_IDLE
225 {"idle", PW_TYPE_INTEGER,
226 offsetof(ldap_instance,keepalive_idle), NULL, "60"},
228 #ifdef LDAP_OPT_X_KEEPALIVE_PROBES
229 {"probes", PW_TYPE_INTEGER,
230 offsetof(ldap_instance,keepalive_probes), NULL, "3"},
232 #ifdef LDAP_OPT_ERROR_NUMBER
233 {"interval", PW_TYPE_INTEGER,
234 offsetof(ldap_instance,keepalive_interval), NULL, "30"},
236 { NULL, -1, 0, NULL, NULL }
240 static const CONF_PARSER module_config[] = {
241 {"server", PW_TYPE_STRING_PTR,
242 offsetof(ldap_instance,server), NULL, "localhost"},
243 {"port", PW_TYPE_INTEGER,
244 offsetof(ldap_instance,port), NULL, "389"},
246 {"password", PW_TYPE_STRING_PTR,
247 offsetof(ldap_instance,password), NULL, ""},
248 {"identity", PW_TYPE_STRING_PTR,
249 offsetof(ldap_instance,login), NULL, ""},
254 {"basedn", PW_TYPE_STRING_PTR,
255 offsetof(ldap_instance,basedn), NULL, "o=notexist"},
257 {"filter", PW_TYPE_STRING_PTR,
258 offsetof(ldap_instance,filter), NULL, "(uid=%u)"},
260 /* turn off the annoying warning if we don't expect a password */
261 {"expect_password", PW_TYPE_BOOLEAN,
262 offsetof(ldap_instance,expect_password), NULL, "yes"},
265 * Terrible things which should be deleted.
267 {"do_xlat", PW_TYPE_BOOLEAN,
268 offsetof(ldap_instance,do_xlat), NULL, "yes"},
270 { "profiles", PW_TYPE_SUBSECTION, 0, NULL, (const void *) attr_config },
272 { "group", PW_TYPE_SUBSECTION, 0, NULL, (const void *) group_config },
274 { "options", PW_TYPE_SUBSECTION, 0, NULL,
275 (const void *) option_config },
277 { "tls", PW_TYPE_SUBSECTION, 0, NULL, (const void *) tls_config },
279 { "profiles", PW_TYPE_SUBSECTION, 0, NULL, (const void *) attr_config },
281 {NULL, -1, 0, NULL, NULL}
284 typedef struct ldap_conn {
291 typedef struct xlat_attrs {
292 const VALUE_PAIR_MAP *maps;
293 const char *attrs[MAX_ATTRMAP];
296 typedef struct rlm_ldap_result {
302 #if LDAP_SET_REBIND_PROC_ARGS == 3
304 * Rebind && chase referral stuff
306 static int ldap_rebind(LDAP *handle, LDAP_CONST char *url,
307 UNUSED ber_tag_t request, UNUSED ber_int_t msgid,
310 LDAP_CONN *conn = ctx;
312 conn->referred = TRUE;
313 conn->rebound = TRUE; /* not really, but oh well... */
314 rad_assert(handle == conn->handle);
316 DEBUG("rlm_ldap (%s): Rebinding to URL %s", conn->inst->xlat_name, url);
318 return ldap_bind_s(handle, conn->inst->login, conn->inst->password,
323 static int ldap_bind_wrapper(LDAP_CONN **pconn, const char *user,
324 const char *password,
325 const char **perror_str, int do_rebind)
327 int rcode, ldap_errno;
328 int module_rcode = RLM_MODULE_FAIL;
329 int reconnect = FALSE;
330 const char *error_string;
331 LDAP_CONN *conn = *pconn;
332 ldap_instance *inst = conn->inst;
333 LDAPMessage *result = NULL;
337 ldap_errno = ldap_bind(conn->handle, user, password, LDAP_AUTH_SIMPLE);
338 if (ldap_errno < 0) {
340 ldap_get_option(conn->handle, LDAP_OPT_ERROR_NUMBER,
342 error_string = ldap_err2string(ldap_errno);
344 if (do_rebind && !reconnect) {
345 conn = fr_connection_reconnect(inst->pool, conn);
347 if (!conn) return RLM_MODULE_FAIL;
352 if (perror_str) *perror_str = error_string;
354 #ifdef HAVE_LDAP_INITIALIZE
356 radlog(L_ERR, "rlm_ldap (%s): %s bind to %s failed: %s",
357 inst->xlat_name, user,
358 inst->server, error_string);
362 radlog(L_ERR, "rlm_ldap (%s): %s bind to %s:%d "
364 inst->xlat_name, user,
365 inst->server, inst->port,
369 return module_rcode; /* caller closes the connection */
372 DEBUG3("rlm_ldap (%s): Waiting for bind result...", inst->xlat_name);
374 tv.tv_sec = inst->timeout;
376 rcode = ldap_result(conn->handle, ldap_errno, 1, &tv, &result);
377 if (rcode < 0) goto get_error;
380 error_string = "timeout";
384 ldap_errno = ldap_result2error(conn->handle, result, 1);
385 switch (ldap_errno) {
389 case LDAP_INVALID_CREDENTIALS:
390 case LDAP_CONSTRAINT_VIOLATION:
391 rcode = RLM_MODULE_REJECT;
398 return RLM_MODULE_OK;
401 /*************************************************************************
403 * Function: ldap_conn_create
405 * Purpose: Create and return a new connection
406 * This function is probably too big.
408 *************************************************************************/
409 static void *ldap_conn_create(void *ctx)
412 int ldap_errno, ldap_version;
414 ldap_instance *inst = ctx;
416 LDAP_CONN *conn = NULL;
419 #ifdef HAVE_LDAP_INITIALIZE
421 DEBUG("rlm_ldap (%s): Connect to %s", inst->xlat_name,
424 ldap_errno = ldap_initialize(&handle, inst->server);
426 if (ldap_errno != LDAP_SUCCESS) {
427 radlog(L_ERR, "rlm_ldap (%s): ldap_initialize() "
429 inst->xlat_name, ldap_err2string(ldap_errno));
435 DEBUG("rlm_ldap (%s): Connect to %s:%d", inst->xlat_name,
436 inst->server, inst->port);
438 handle = ldap_init(inst->server, inst->port);
440 radlog(L_ERR, "rlm_ldap (%s): ldap_init() failed",
443 if (handle) ldap_unbind_s(handle);
449 * We now have a connection structure, but no actual TCP connection.
451 * Set a bunch of LDAP options, using common code.
453 #define do_ldap_option(_option, _name, _value) \
454 if (ldap_set_option(handle, _option, _value) != LDAP_OPT_SUCCESS) { \
455 ldap_get_option(handle, LDAP_OPT_ERROR_NUMBER, &ldap_errno); \
456 radlog(L_ERR, "rlm_ldap (%s): Could not set %s: %s", \
457 inst->xlat_name, _name, ldap_err2string(ldap_errno)); \
460 if (inst->ldap_debug) {
461 do_ldap_option(LDAP_OPT_DEBUG_LEVEL, "ldap_debug",
462 &(inst->ldap_debug));
466 * Leave "chase_referrals" unset to use the OpenLDAP
469 if (inst->chase_referrals != 2) {
470 if (inst->chase_referrals) {
471 do_ldap_option(LDAP_OPT_REFERRALS, "chase_referrals",
474 #if LDAP_SET_REBIND_PROC_ARGS == 3
475 if (inst->rebind == 1) {
476 ldap_set_rebind_proc(handle, ldap_rebind, inst);
480 do_ldap_option(LDAP_OPT_REFERRALS, "chase_referrals",
485 tv.tv_sec = inst->net_timeout;
487 do_ldap_option(LDAP_OPT_NETWORK_TIMEOUT, "net_timeout", &tv);
489 do_ldap_option(LDAP_OPT_TIMELIMIT, "timelimit",
492 ldap_version = LDAP_VERSION3;
493 do_ldap_option(LDAP_OPT_PROTOCOL_VERSION, "ldap_version",
496 #ifdef LDAP_OPT_X_KEEPALIVE_IDLE
497 do_ldap_option(LDAP_OPT_X_KEEPALIVE_IDLE, "keepalive idle",
498 &(inst->keepalive_idle));
501 #ifdef LDAP_OPT_X_KEEPALIVE_PROBES
502 do_ldap_option(LDAP_OPT_X_KEEPALIVE_PROBES, "keepalive probes",
503 &(inst->keepalive_probes));
506 #ifdef LDAP_OPT_X_KEEPALIVE_INTERVAL
507 do_ldap_option(LDAP_OPT_X_KEEPALIVE_INTERVAL, "keepalive interval",
508 &(inst->keepalive_interval));
511 #ifdef HAVE_LDAP_START_TLS
513 * Set all of the TLS options
515 if (inst->tls_mode) {
516 do_ldap_option(LDAP_OPT_X_TLS, "tls_mode", &(inst->tls_mode));
519 #define maybe_ldap_option(_option, _name, _value) \
520 if (_value) do_ldap_option(_option, _name, _value)
522 maybe_ldap_option(LDAP_OPT_X_TLS_CACERTFILE,
523 "cacertfile", inst->tls_cacertfile);
524 maybe_ldap_option(LDAP_OPT_X_TLS_CACERTDIR,
525 "cacertdir", inst->tls_cacertdir);
527 #ifdef HAVE_LDAP_INT_TLS_CONFIG
528 if (ldap_int_tls_config(NULL, LDAP_OPT_X_TLS_REQUIRE_CERT,
529 (inst->tls_require_cert)) != LDAP_OPT_SUCCESS) {
530 ldap_get_option(handle, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
531 radlog(L_ERR, "rlm_ldap (%s): could not set ",
532 "LDAP_OPT_X_TLS_REQUIRE_CERT option to %s: %s",
534 inst->tls_require_cert,
535 ldap_err2string(ldap_errno));
539 maybe_ldap_option(LDAP_OPT_X_TLS_CERTFILE,
540 "certfile", inst->tls_certfile);
541 maybe_ldap_option(LDAP_OPT_X_TLS_KEYFILE,
542 "keyfile", inst->tls_keyfile);
543 maybe_ldap_option(LDAP_OPT_X_TLS_RANDOM_FILE,
544 "randfile", inst->tls_randfile);
547 * And finally start the TLS code.
549 if (inst->start_tls && (inst->port != 636)) {
550 ldap_errno = ldap_start_tls_s(handle, NULL, NULL);
551 if (ldap_errno != LDAP_SUCCESS) {
552 ldap_get_option(handle, LDAP_OPT_ERROR_NUMBER,
554 radlog(L_ERR, "rlm_ldap (%s): could not start TLS: %s",
556 ldap_err2string(ldap_errno));
560 #endif /* HAVE_LDAP_START_TLS */
562 conn = rad_malloc(sizeof(*conn));
564 conn->handle = handle;
565 conn->rebound = FALSE;
566 conn->referred = FALSE;
568 module_rcode = ldap_bind_wrapper(&conn, inst->login, inst->password,
570 if (module_rcode != RLM_MODULE_OK) {
571 radlog(L_ERR, "rlm_ldap (%s): failed binding to LDAP "
573 inst->xlat_name, error);
576 * FIXME: print "check config, morians!
585 /*************************************************************************
587 * Function: ldap_conn_delete
589 * Purpose: Close and delete a connection
591 *************************************************************************/
592 static int ldap_conn_delete(UNUSED void *ctx, void *connection)
594 LDAP_CONN *conn = connection;
596 ldap_unbind_s(conn->handle);
603 /*************************************************************************
605 * Function: ldap_get_socket
607 * Purpose: Gets an LDAP socket from the connection pool
609 *************************************************************************/
610 static LDAP_CONN *ldap_get_socket(ldap_instance *inst)
614 conn = fr_connection_get(inst->pool);
616 radlog(L_ERR, "rlm_ldap (%s): all ldap connections are in use",
624 /*************************************************************************
626 * Function: ldap_release_socket
628 * Purpose: Frees an LDAP socket back to the connection pool
630 *************************************************************************/
631 static void ldap_release_socket(ldap_instance *inst, LDAP_CONN *conn)
634 * Could have already been free'd due to a previous error.
639 * We chased a referral to another server.
641 * This connection is no longer part of the pool which is
642 * connected to and bound to the configured server.
645 * Note that we do NOT close it if it was bound to
646 * another user. Instead, we let the next caller do the
649 if (conn->referred) {
650 fr_connection_del(inst->pool, conn);
654 fr_connection_release(inst->pool, conn);
659 /*************************************************************************
661 * Function: ldap_escape_func
663 * Purpose: Converts "bad" strings into ones which are safe for LDAP
665 *************************************************************************/
666 static size_t ldap_escape_func(UNUSED REQUEST *request, char *out,
667 size_t outlen, const char *in, UNUSED void *arg)
673 * Encode unsafe characters.
676 ((in[0] == ' ') || (in[0] == '#'))) ||
677 (strchr(",+\"\\<>;*=()", *in))) {
678 static const char hex[] = "0123456789abcdef";
681 * Only 3 or less bytes available.
688 *(out++) = hex[((*in) >> 4) & 0x0f];
689 *(out++) = hex[(*in) & 0x0f];
697 * Only one byte left.
714 /*************************************************************************
716 * Function: perform_search
718 * Purpose: Do a search and get a response
720 *************************************************************************/
721 static int perform_search(ldap_instance *inst, REQUEST *request,
722 LDAP_CONN **pconn, const char *search_basedn,
723 int scope, const char *filter,
724 const char * const *attrs, LDAPMessage **presult)
728 int reconnect = FALSE;
729 LDAP_CONN *conn = *pconn;
733 * OpenLDAP library doesn't declare attrs array as const, but
734 * it really should be *sigh*.
737 memcpy(&search_attrs, &attrs, sizeof(attrs));
742 * Do all searches as the default admin user.
745 ldap_errno = ldap_bind_wrapper(pconn,
746 inst->login, inst->password,
748 if (ldap_errno != RLM_MODULE_OK) {
752 rad_assert(*pconn != NULL);
754 conn->rebound = FALSE;
757 tv.tv_sec = inst->timeout;
759 RDEBUG2("Performing search in '%s' with filter '%s'",
760 search_basedn ? search_basedn : "(null)" ,
764 ldap_errno = ldap_search_ext_s(conn->handle, search_basedn, scope,
765 filter, search_attrs, 0, NULL, NULL,
767 switch (ldap_errno) {
769 case LDAP_NO_SUCH_OBJECT:
772 case LDAP_SERVER_DOWN:
774 ldap_msgfree(*presult);
776 if (reconnect) return -1;
779 conn = fr_connection_reconnect(inst->pool, conn);
780 *pconn = conn; /* tell the caller we have a new connection */
781 if (!conn) return -1;
784 case LDAP_INSUFFICIENT_ACCESS:
785 radlog(L_ERR, "rlm_ldap (%s): Search failed: "
786 "Insufficient access. Check the identity and password "
787 "configuration directives", inst->xlat_name);
788 ldap_msgfree(*presult);
792 exec_trigger(NULL, inst->cs, "modules.ldap.timeout", TRUE);
793 radlog(L_ERR, "rlm_ldap (%s): Search failed: Timed out "
794 "while waiting for server to respond"
795 "Please increase the timeout", inst->xlat_name);
796 ldap_msgfree(*presult);
799 case LDAP_FILTER_ERROR:
800 radlog(L_ERR, "rlm_ldap (%s): Search failed: Bad search "
801 "filter: %s", inst->xlat_name,filter);
802 ldap_msgfree(*presult);
805 case LDAP_TIMELIMIT_EXCEEDED:
806 exec_trigger(NULL, inst->cs, "modules.ldap.timeout", TRUE);
809 case LDAP_UNAVAILABLE:
811 * Reconnect. There's an issue with the socket
814 ldap_get_option(conn->handle, LDAP_OPT_ERROR_NUMBER,
816 radlog(L_ERR, "rlm_ldap (%s): Search failed: %s",
817 inst->xlat_name, ldap_err2string(ldap_errno));
821 ldap_get_option(conn->handle, LDAP_OPT_ERROR_NUMBER,
823 radlog(L_ERR, "rlm_ldap (%s): Search failed: %s",
824 inst->xlat_name, ldap_err2string(ldap_errno));
825 ldap_msgfree(*presult);
829 count = ldap_count_entries(conn->handle, *presult);
831 ldap_msgfree(*presult);
832 RDEBUG("Search returned no results");
838 ldap_msgfree(*presult);
839 RDEBUG("Got ambiguous search result (%d results)", count);
847 /*************************************************************************
849 * Function: ldap_xlat
851 * Purpose: Expand an LDAP URL into a query, and return a string
852 * result from that query.
854 *************************************************************************/
855 static size_t ldap_xlat(void *instance, REQUEST *request, const char *fmt,
856 char *out, size_t freespace)
860 ldap_instance *inst = instance;
861 LDAPURLDesc *ldap_url;
862 LDAPMessage *result = NULL;
863 LDAPMessage *entry = NULL;
869 char buffer[MAX_FILTER_STR_LEN];
871 if (strchr(fmt, '%') != NULL) {
872 if (!radius_xlat(buffer, sizeof(buffer), fmt, request,
873 ldap_escape_func, NULL)) {
875 "rlm_ldap (%s): Unable to create LDAP URL",
884 if (!ldap_is_ldap_url(url)) {
885 radlog(L_ERR, "rlm_ldap (%s): String passed does not look "
886 "like an LDAP URL", inst->xlat_name);
890 if (ldap_url_parse(url, &ldap_url)){
891 radlog(L_ERR, "rlm_ldap (%s): Parsing LDAP URL failed",
897 * Nothing, empty string, "*" string, or got 2 things, die.
899 if (!ldap_url->lud_attrs || !ldap_url->lud_attrs[0] ||
900 !*ldap_url->lud_attrs[0] ||
901 (strcmp(ldap_url->lud_attrs[0], "*") == 0) ||
902 ldap_url->lud_attrs[1]) {
903 radlog(L_ERR, "rlm_ldap (%s): Bad attributes list in LDAP "
904 "URL. URL must specify exactly one attribute to "
911 if (ldap_url->lud_host &&
912 ((strncmp(inst->server, ldap_url->lud_host,
913 strlen(inst->server)) != 0) ||
914 (ldap_url->lud_port != inst->port))) {
915 RDEBUG("Requested server/port is \"%s:%i\"", ldap_url->lud_host,
921 conn = ldap_get_socket(inst);
922 if (!conn) goto free_urldesc;
924 memcpy(&attrs, &ldap_url->lud_attrs, sizeof(attrs));
926 rcode = perform_search(inst, request, &conn, ldap_url->lud_dn,
927 ldap_url->lud_scope, ldap_url->lud_filter, attrs,
931 RDEBUG("Search returned not found", inst->xlat_name);
938 entry = ldap_first_entry(conn->handle, result);
940 ldap_get_option(conn->handle, LDAP_OPT_RESULT_CODE,
942 radlog(L_ERR, "rlm_ldap (%s): Failed retrieving entry: %s",
944 ldap_err2string(ldap_errno));
948 vals = ldap_get_values(conn->handle, entry, ldap_url->lud_attrs[0]);
950 RDEBUG("No \"%s\" attributes found in specified object",
951 inst->xlat_name, ldap_url->lud_attrs[0]);
955 length = strlen(vals[0]);
956 if (length >= freespace){
961 strlcpy(out, vals[0], freespace);
964 ldap_value_free(vals);
966 ldap_msgfree(result);
968 ldap_release_socket(inst, conn);
970 ldap_free_urldesc(ldap_url);
976 static char *get_userdn(LDAP_CONN **pconn, REQUEST *request, int *module_rcode)
980 ldap_instance *inst = (*pconn)->inst;
981 LDAP *handle = (*pconn)->handle;
982 LDAPMessage *result, *entry;
984 static char firstattr[] = "uid";
986 const char *attrs[] = {firstattr, NULL};
987 char filter[MAX_FILTER_STR_LEN];
988 char basedn[MAX_FILTER_STR_LEN];
990 *module_rcode = RLM_MODULE_FAIL;
992 vp = pairfind(request->config_items, PW_LDAP_USERDN, 0);
993 if (vp) return vp->vp_strvalue;
995 if (!radius_xlat(filter, sizeof(filter), inst->filter,
996 request, ldap_escape_func, NULL)) {
997 radlog(L_ERR, "rlm_ldap (%s): Unable to create filter",
999 *module_rcode = RLM_MODULE_INVALID;
1003 if (!radius_xlat(basedn, sizeof(basedn), inst->basedn,
1004 request, ldap_escape_func, NULL)) {
1005 radlog(L_ERR, "rlm_ldap (%s): Unable to create basedn",
1007 *module_rcode = RLM_MODULE_INVALID;
1011 rcode = perform_search(inst, request, pconn, basedn, LDAP_SCOPE_SUBTREE,
1012 filter, attrs, &result);
1015 *module_rcode = RLM_MODULE_NOTFOUND;
1021 if ((entry = ldap_first_entry(handle, result)) == NULL) {
1022 ldap_get_option(handle, LDAP_OPT_RESULT_CODE,
1024 radlog(L_ERR, "rlm_ldap (%s): Failed retrieving entry: %s",
1026 ldap_err2string(ldap_errno));
1027 ldap_msgfree(result);
1031 if ((user_dn = ldap_get_dn(handle, entry)) == NULL) {
1032 ldap_get_option(handle, LDAP_OPT_RESULT_CODE,
1034 radlog(L_ERR, "rlm_ldap (%s): ldap_get_dn() failed: %s",
1036 ldap_err2string(ldap_errno));
1038 ldap_msgfree(result);
1042 vp = pairmake("LDAP-UserDn", user_dn, T_OP_EQ);
1044 ldap_memfree(user_dn);
1045 ldap_msgfree(result);
1049 pairadd(&request->config_items, vp);
1050 ldap_memfree(user_dn);
1051 ldap_msgfree(result);
1053 return vp->vp_strvalue;
1057 /*****************************************************************************
1059 * Perform LDAP-Group comparison checking
1061 *****************************************************************************/
1062 static int ldap_groupcmp(void *instance, REQUEST *request,
1063 UNUSED VALUE_PAIR *thing, VALUE_PAIR *check,
1064 UNUSED VALUE_PAIR *check_pairs,
1065 UNUSED VALUE_PAIR **reply_pairs)
1067 ldap_instance *inst = instance;
1068 int i, rcode, found, module_rcode;
1069 LDAPMessage *result = NULL;
1070 LDAPMessage *entry = NULL;
1072 int check_is_dn = FALSE, value_is_dn = FALSE;
1073 static char firstattr[] = "dn";
1074 const char *attrs[] = {firstattr, NULL};
1076 const char *group_attrs[] = {inst->groupmemb_attr, NULL};
1080 char gr_filter[MAX_FILTER_STR_LEN];
1081 char filter[MAX_FILTER_STR_LEN];
1082 char basedn[MAX_FILTER_STR_LEN];
1084 RDEBUG("Searching for user in group \"%s\"", check->vp_strvalue);
1086 if (check->length == 0) {
1087 RDEBUG("Cannot do comparison (group name is empty)");
1091 conn = ldap_get_socket(inst);
1092 if (!conn) return 1;
1095 * This is used in the default membership filter.
1097 user_dn = get_userdn(&conn, request, &module_rcode);
1099 ldap_release_socket(inst, conn);
1103 if (!inst->groupmemb_filter) goto check_attr;
1105 if (!radius_xlat(gr_filter, sizeof(gr_filter),
1106 inst->groupmemb_filter, request, ldap_escape_func,
1108 radlog(L_ERR, "rlm_ldap (%s): Failed creating group filter",
1114 * If it's a DN, use that.
1116 check_is_dn = strchr(check->vp_strvalue,',') == NULL ? FALSE : TRUE;
1119 strlcpy(filter, gr_filter, sizeof(filter));
1120 strlcpy(basedn, check->vp_strvalue, sizeof(basedn));
1122 snprintf(filter, sizeof(filter), "(&(%s=%s)%s)",
1123 inst->groupname_attr,
1124 check->vp_strvalue, gr_filter);
1127 * get_userdn does this, too. Oh well.
1129 if (!radius_xlat(basedn, sizeof(basedn), inst->basedn,
1130 request, ldap_escape_func, NULL)) {
1131 radlog(L_ERR, "rlm_ldap (%s): Failed creating basedn",
1137 rcode = perform_search(inst, request, &conn, basedn, LDAP_SCOPE_SUBTREE,
1138 filter, attrs, &result);
1140 ldap_release_socket(inst, conn);
1141 ldap_msgfree(result);
1143 RDEBUG("User found in group object");
1149 ldap_release_socket(inst, conn);
1153 /* else the search returned -2, for "not found" */
1156 * Else the search returned NOTFOUND. See if we're
1157 * configured to search for group membership using user
1160 if (!inst->groupmemb_attr) {
1161 ldap_release_socket(inst, conn);
1162 RDEBUG("Group object \"%s\" not found, or user is not a member",
1163 check->vp_strvalue);
1168 RDEBUG2("Checking user object membership (%s) attributes",
1169 inst->groupmemb_attr);
1171 snprintf(filter ,sizeof(filter), "(objectclass=*)");
1173 rcode = perform_search(inst, request, &conn, user_dn, LDAP_SCOPE_BASE,
1174 filter, group_attrs, &result);
1177 RDEBUG("Can't check membership attributes, user object "
1180 ldap_release_socket(inst, conn);
1184 entry = ldap_first_entry(conn->handle, result);
1186 ldap_get_option(conn->handle, LDAP_OPT_RESULT_CODE,
1188 radlog(L_ERR, "rlm_ldap (%s): Failed retrieving entry: %s",
1190 ldap_err2string(ldap_errno));
1192 ldap_release_socket(inst, conn);
1193 ldap_msgfree(result);
1197 vals = ldap_get_values(conn->handle, entry, inst->groupmemb_attr);
1199 RDEBUG("No group membership attribute(s) found in user object");
1200 ldap_release_socket(inst, conn);
1201 ldap_msgfree(result);
1206 * Loop over the list of groups the user is a member of,
1207 * looking for a match.
1210 for (i = 0; i < ldap_count_values(vals); i++) {
1211 LDAPMessage *gr_result = NULL;
1213 value_is_dn = strchr(vals[i], ',') == NULL ? FALSE : TRUE;
1215 RDEBUG2("Processing group membership value \"%s\"", vals[i]);
1218 * Both literal group names, do case sensitive comparison
1220 if (!check_is_dn && !value_is_dn) {
1221 if (strcmp(vals[i], check->vp_strvalue) == 0){
1222 RDEBUG("User found (membership value matches "
1233 * Both DNs, do case insensitive comparison
1235 if (check_is_dn && value_is_dn) {
1236 if (strcasecmp(vals[i], check->vp_strvalue) == 0){
1237 RDEBUG("User found (membership DN matches "
1248 * If the value is not a DN, or the check item is a DN
1249 * there's nothing more we can do.
1251 if (!value_is_dn && check_is_dn) continue;
1254 * We have a value which is a DN, and a check item which
1255 * specifies the name of a group, search using the value
1256 * DN for the group, and see if it has a groupname_attr
1257 * which matches our check val.
1259 RDEBUG2("Searching with membership DN and group name");
1261 snprintf(filter,sizeof(filter), "(%s=%s)",
1262 inst->groupname_attr, check->vp_strvalue);
1264 rcode = perform_search(inst, request, &conn, vals[i],
1265 LDAP_SCOPE_BASE, filter, attrs,
1268 ldap_msgfree(gr_result);
1270 /* Error occurred */
1272 ldap_value_free(vals);
1273 ldap_msgfree(result);
1274 ldap_release_socket(inst, conn);
1279 * Either the group DN wasn't found, or it didn't have the
1280 * correct name. Continue looping over the attributes.
1283 ldap_msgfree(gr_result);
1289 RDEBUG("User found (group name in membership DN matches check "
1295 ldap_value_free(vals);
1296 ldap_msgfree(result);
1297 ldap_release_socket(inst, conn);
1300 RDEBUG("User is not a member of specified group");
1308 * Verify that the ldap update section makes sense, and add attribute names
1309 * to array of attributes for efficient querying later.
1311 static int build_attrmap(CONF_SECTION *cs, VALUE_PAIR_MAP **head)
1313 const char *cs_list, *p;
1315 request_refs_t request_def = REQUEST_CURRENT;
1316 pair_lists_t list_def = PAIR_LIST_REQUEST;
1318 CONF_ITEM *ci = cf_sectiontoitem(cs);
1321 unsigned int total = 0;
1322 VALUE_PAIR_MAP **tail, *map;
1328 cs_list = p = cf_section_name2(cs);
1330 request_def = radius_request_name(&p, REQUEST_UNKNOWN);
1331 if (request_def == REQUEST_UNKNOWN) {
1332 cf_log_err(ci, "rlm_ldap: Default request specified "
1333 "in mapping section is invalid");
1337 list_def = fr_str2int(pair_lists, p, PAIR_LIST_UNKNOWN);
1338 if (list_def == PAIR_LIST_UNKNOWN) {
1339 cf_log_err(ci, "rlm_ldap: Default list specified "
1340 "in mapping section is invalid");
1345 for (ci = cf_item_find_next(cs, NULL);
1347 ci = cf_item_find_next(cs, ci)) {
1348 if (total++ == MAX_ATTRMAP) {
1349 cf_log_err(ci, "rlm_ldap: Attribute map size exceeded");
1353 if (!cf_item_is_pair(ci)) {
1354 cf_log_err(ci, "rlm_ldap: Entry is not in \"attribute ="
1355 " ldap-attribute\" format");
1359 cp = cf_itemtopair(ci);
1360 map = radius_cp2map(cp, REQUEST_CURRENT, list_def);
1366 tail = &(map->next);
1372 radius_mapfree(head);
1376 /*****************************************************************************
1378 * Detach from the LDAP server and cleanup internal state.
1380 *****************************************************************************/
1381 static int ldap_detach(void *instance)
1383 ldap_instance *inst = instance;
1385 fr_connection_pool_delete(inst->pool);
1387 if (inst->user_map) {
1388 radius_mapfree(&inst->user_map);
1396 /*************************************************************************
1398 * Function: rlm_ldap_instantiate
1400 * Purpose: Uses section of radiusd config file passed as parameter
1401 * to create an instance of the module.
1403 *************************************************************************/
1404 static int ldap_instantiate(CONF_SECTION * conf, void **instance)
1406 ldap_instance *inst;
1409 inst = rad_malloc(sizeof *inst);
1413 memset(inst, 0, sizeof(*inst));
1416 inst->chase_referrals = 2; /* use OpenLDAP defaults */
1419 if (cf_section_parse(conf, inst, module_config) < 0) {
1424 inst->xlat_name = cf_section_name2(conf);
1425 if (!inst->xlat_name) inst->xlat_name = cf_section_name1(conf);
1427 if (inst->server == NULL) {
1428 radlog(L_ERR, "rlm_ldap (%s): missing 'server' directive",
1435 * Check for URLs. If they're used and the library doesn't
1436 * support them, then complain.
1439 if (ldap_is_ldap_url(inst->server)) {
1440 #ifdef HAVE_LDAP_INITIALIZE
1444 radlog(L_ERR, "rlm_ldap (%s): 'server' directive is in URL "
1445 "form but ldap_initialize() is not available",
1452 /* workaround for servers which support LDAPS but not START TLS */
1453 if (inst->port == LDAPS_PORT || inst->tls_mode) {
1454 inst->tls_mode = LDAP_OPT_X_TLS_HARD;
1459 #if LDAP_SET_REBIND_PROC_ARGS != 3
1461 * The 2-argument rebind doesn't take an instance
1462 * variable. Our rebind function needs the instance
1463 * variable for the username, password, etc.
1465 if (inst->rebind == 1) {
1466 radlog(L_ERR, "rlm_ldap (%s): Cannot use 'rebind' directive "
1467 "as this version of libldap does not support the API "
1468 "that we need", inst->xlat-name);
1475 * Build the attribute map
1477 cs = cf_section_sub_find(conf, "update");
1479 if (build_attrmap(cs, &(inst->user_map)) < 0) {
1486 * Group comparison checks.
1488 paircompare_register(PW_LDAP_GROUP, PW_USER_NAME, ldap_groupcmp, inst);
1489 if (cf_section_name2(conf)) {
1494 snprintf(buffer, sizeof(buffer), "%s-Ldap-Group",
1496 memset(&flags, 0, sizeof(flags));
1498 dict_addattr(buffer, -1, 0, PW_TYPE_STRING, flags);
1499 da = dict_attrbyname(buffer);
1501 radlog(L_ERR, "rlm_ldap (%s): Failed creating "
1502 "attribute %s", inst->xlat_name, buffer);
1507 paircompare_register(da->attr, PW_USER_NAME, ldap_groupcmp,
1511 xlat_register(inst->xlat_name, ldap_xlat, inst);
1514 * Initialize the socket pool.
1516 inst->pool = fr_connection_pool_init(inst->cs, inst,
1530 static void module_failure_msg(VALUE_PAIR **vps, const char *fmt, ...)
1536 vp = paircreate(PW_MODULE_FAILURE_MESSAGE, 0, PW_TYPE_STRING);
1542 vsnprintf(vp->vp_strvalue, sizeof(vp->vp_strvalue), fmt, ap);
1548 static int check_access(ldap_instance *inst, REQUEST* request, LDAP_CONN *conn,
1554 vals = ldap_get_values(conn->handle, entry, inst->access_attr);
1556 if (inst->positive_access_attr) {
1557 if (strncmp(vals[0], "FALSE", 5) == 0) {
1558 RDEBUG("Dialup access disabled");
1565 RDEBUG("\"%s\" attribute exists - access denied by"
1566 " default", inst->access_attr);
1569 ldap_value_free(vals);
1571 } else if (inst->positive_access_attr) {
1572 RDEBUG("No %s attribute - access denied by default",
1583 static VALUE_PAIR *ldap_getvalue(REQUEST *request, const VALUE_PAIR_TMPL *dst,
1586 rlm_ldap_result_t *self = ctx;
1587 VALUE_PAIR *head, **tail, *vp;
1596 * Iterate over all the retrieved values,
1597 * don't try and be clever about changing operators
1598 * just use whatever was set in the attribute map.
1600 for (i = 0; i < self->count; i++) {
1601 vp = pairalloc(dst->da);
1604 pairparsevalue(vp, self->values[i]);
1614 static void xlat_attrsfree(const xlat_attrs_t *expanded)
1616 const VALUE_PAIR_MAP *map;
1617 unsigned int total = 0;
1621 for (map = expanded->maps; map != NULL; map = map->next)
1623 memcpy(&name, &(expanded->attrs[total++]), sizeof(name));
1627 if (map->src.do_xlat) {
1634 static int xlat_attrs(REQUEST *request, const VALUE_PAIR_MAP *maps,
1635 xlat_attrs_t *expanded)
1637 const VALUE_PAIR_MAP *map;
1638 unsigned int total = 0;
1643 for (map = maps; map != NULL; map = map->next)
1645 if (map->src.do_xlat) {
1646 buffer = rad_malloc(MAX_ATTR_STR_LEN);
1647 len = radius_xlat(buffer, MAX_ATTR_STR_LEN,
1648 map->src.name, request, NULL, NULL);
1651 RDEBUG("Expansion of LDAP attribute "
1652 "\"%s\" failed", map->src.name);
1654 expanded->attrs[total] = NULL;
1656 xlat_attrsfree(expanded);
1661 expanded->attrs[total++] = buffer;
1663 expanded->attrs[total++] = map->src.name;
1667 expanded->attrs[total] = NULL;
1668 expanded->maps = maps;
1674 /** Convert attribute map into valuepairs
1676 * Use the attribute map built earlier to convert LDAP values into valuepairs
1677 * and insert them into whichever list they need to go into.
1679 * This is *NOT* atomic, but there's no condition in which we should error
1682 static void do_attrmap(UNUSED ldap_instance *inst, REQUEST *request,
1683 LDAP *handle, const xlat_attrs_t *expanded,
1686 const VALUE_PAIR_MAP *map;
1687 unsigned int total = 0;
1689 rlm_ldap_result_t result;
1692 for (map = expanded->maps; map != NULL; map = map->next)
1694 name = expanded->attrs[total++];
1696 result.values = ldap_get_values(handle, entry, name);
1697 if (!result.values) {
1698 DEBUG2("Attribute \"%s\" not found in LDAP object",
1705 * Find out how many values there are for the
1706 * attribute and extract all of them.
1708 result.count = ldap_count_values(result.values);
1711 * If something bad happened, just skip, this is probably
1712 * a case of the dst being incorrect for the current
1715 if (radius_map2request(request, map, name, ldap_getvalue,
1722 ldap_value_free(result.values);
1727 static void do_check_reply(ldap_instance *inst, REQUEST *request)
1730 * More warning messages for people who can't be bothered
1731 * to read the documentation.
1733 if (inst->expect_password && (debug_flag > 1)) {
1734 if (!pairfind(request->config_items,PW_CLEARTEXT_PASSWORD, 0) &&
1735 !pairfind(request->config_items,
1736 PW_NT_PASSWORD, 0) &&
1737 !pairfind(request->config_items,
1738 PW_USER_PASSWORD, 0) &&
1739 !pairfind(request->config_items,
1740 PW_PASSWORD_WITH_HEADER, 0) &&
1741 !pairfind(request->config_items,
1742 PW_CRYPT_PASSWORD, 0)) {
1743 RDEBUG("WARNING: No \"known good\" password "
1744 "was found in LDAP. Are you sure that "
1745 "the user is configured correctly?");
1751 static void apply_profile(ldap_instance *inst, REQUEST *request,
1752 LDAP_CONN **pconn, const char *profile,
1753 const xlat_attrs_t *expanded)
1756 LDAPMessage *result, *entry;
1758 LDAP *handle = (*pconn)->handle;
1759 char filter[MAX_FILTER_STR_LEN];
1761 if (!profile || !*profile) return;
1763 strlcpy(filter, inst->base_filter, sizeof(filter));
1765 rcode = perform_search(inst, request, pconn, profile, LDAP_SCOPE_BASE,
1766 filter, expanded->attrs, &result);
1770 RDEBUG("Profile \"%s\" not found", profile);
1775 entry = ldap_first_entry(handle, result);
1777 ldap_get_option(handle, LDAP_OPT_RESULT_CODE,
1779 radlog(L_ERR, "rlm_ldap (%s): Failed retrieving entry: %s",
1781 ldap_err2string(ldap_errno));
1786 do_attrmap(inst, request, handle, expanded, entry);
1789 ldap_msgfree(result);
1793 /******************************************************************************
1795 * Function: ldap_authorize
1797 * Purpose: Check if user is authorized for remote access
1799 ******************************************************************************/
1800 static int ldap_authorize(void *instance, REQUEST * request)
1803 int module_rcode = RLM_MODULE_OK;
1804 ldap_instance *inst = instance;
1809 LDAPMessage *result, *entry;
1811 char filter[MAX_FILTER_STR_LEN];
1812 char basedn[MAX_FILTER_STR_LEN];
1813 xlat_attrs_t expanded; /* faster that mallocing every time */
1815 if (!request->username) {
1816 RDEBUG2("Attribute \"User-Name\" is required for "
1818 return RLM_MODULE_NOOP;
1822 * Check for valid input, zero length names not permitted
1824 if (request->username->length == 0) {
1825 RDEBUG2("Zero length username not permitted");
1826 return RLM_MODULE_INVALID;
1829 if (!radius_xlat(filter, sizeof(filter), inst->filter,
1830 request, ldap_escape_func, NULL)) {
1831 radlog(L_ERR, "rlm_ldap (%s): Failed creating filter",
1833 return RLM_MODULE_INVALID;
1836 if (!radius_xlat(basedn, sizeof(basedn), inst->basedn,
1837 request, ldap_escape_func, NULL)) {
1838 radlog(L_ERR, "rlm_ldap (%s): Failed creating basedn",
1840 return RLM_MODULE_INVALID;
1843 conn = ldap_get_socket(inst);
1844 if (!conn) return RLM_MODULE_FAIL;
1846 if (xlat_attrs(request, inst->user_map, &expanded) < 0) {
1847 return RLM_MODULE_FAIL;
1850 rcode = perform_search(inst, request, &conn, basedn,
1851 LDAP_SCOPE_SUBTREE, filter, expanded.attrs,
1856 module_failure_msg(&request->packet->vps,
1857 "rlm_ldap (%s): User object not "
1861 RDEBUG("User object not found", inst->xlat_name);
1863 module_rcode = RLM_MODULE_NOTFOUND;
1870 entry = ldap_first_entry(conn->handle, result);
1872 ldap_get_option(conn->handle, LDAP_OPT_RESULT_CODE,
1874 radlog(L_ERR, "rlm_ldap (%s): Failed retrieving entry: %s",
1876 ldap_err2string(ldap_errno));
1881 user_dn = ldap_get_dn(conn->handle, entry);
1883 ldap_get_option(conn->handle, LDAP_OPT_RESULT_CODE,
1885 radlog(L_ERR, "rlm_ldap (%s): ldap_get_dn() failed: %s",
1887 ldap_err2string(ldap_errno));
1891 RDEBUG2("User found at DN \"%s\"", user_dn);
1893 * Adding attribute containing the Users' DN.
1895 pairadd(&request->config_items,
1896 pairmake("Ldap-UserDn", user_dn, T_OP_EQ));
1897 ldap_memfree(user_dn);
1902 if (inst->access_attr) {
1903 if (check_access(inst, request, conn, entry) < 0) {
1904 module_rcode = RLM_MODULE_USERLOCK;
1910 * Apply ONE user profile, or a default user profile.
1912 vp = pairfind(request->config_items, PW_USER_PROFILE, 0);
1913 if (vp || inst->default_profile) {
1914 char *profile = inst->default_profile;
1916 if (vp) profile = vp->vp_strvalue;
1918 apply_profile(inst, request, &conn, profile, &expanded);
1922 * Apply a SET of user profiles.
1924 if (inst->profile_attr) {
1925 vals = ldap_get_values(conn->handle, entry, inst->profile_attr);
1929 for (i = 0; (vals[i] != NULL) && (*vals[i] != '\0');
1931 apply_profile(inst, request, &conn, vals[i],
1935 ldap_value_free(vals);
1939 if (inst->user_map) {
1940 do_attrmap(inst, request, conn->handle, &expanded, entry);
1941 do_check_reply(inst, request);
1945 xlat_attrsfree(&expanded);
1946 ldap_msgfree(result);
1948 ldap_release_socket(inst, conn);
1950 return module_rcode;
1954 /*****************************************************************************
1956 * Function: ldap_authenticate
1958 * Purpose: Check the user's password against ldap database
1960 *****************************************************************************/
1961 static int ldap_authenticate(void *instance, REQUEST * request)
1964 const char *user_dn;
1965 ldap_instance *inst = instance;
1969 * Ensure that we're being passed a plain-text password, and not
1973 if (!request->username) {
1974 radlog(L_AUTH, "rlm_ldap (%s): Attribute \"User-Name\" is "
1975 "required for authentication", inst->xlat_name);
1976 return RLM_MODULE_INVALID;
1979 if (!request->password) {
1980 radlog(L_AUTH, "rlm_ldap (%s): Attribute \"User-Password\" "
1981 "is required for authentication.", inst->xlat_name);
1982 RDEBUG2(" You have set \"Auth-Type := LDAP\" somewhere.");
1983 RDEBUG2(" *********************************************");
1984 RDEBUG2(" * THAT CONFIGURATION IS WRONG. DELETE IT. ");
1985 RDEBUG2(" * YOU ARE PREVENTING THE SERVER FROM WORKING.");
1986 RDEBUG2(" *********************************************");
1987 return RLM_MODULE_INVALID;
1990 if (request->password->attribute != PW_USER_PASSWORD) {
1991 radlog(L_AUTH, "rlm_ldap (%s): Attribute \"User-Password\" "
1992 "is required for authentication. Cannot use \"%s\".",
1993 inst->xlat_name, request->password->name);
1994 return RLM_MODULE_INVALID;
1997 if (request->password->length == 0) {
1998 module_failure_msg(&request->packet->vps,
1999 "rlm_ldap (%s): Empty password supplied",
2001 return RLM_MODULE_INVALID;
2004 conn = ldap_get_socket(inst);
2005 if (!conn) return RLM_MODULE_FAIL;
2007 RDEBUG("Login attempt by \"%s\" with password \"%s\"",
2008 request->username->vp_strvalue, request->password->vp_strvalue);
2011 * Get the DN by doing a search.
2013 user_dn = get_userdn(&conn, request, &module_rcode);
2015 ldap_release_socket(inst, conn);
2016 return module_rcode;
2022 conn->rebound = TRUE;
2023 module_rcode = ldap_bind_wrapper(&conn, user_dn,
2024 request->password->vp_strvalue,
2026 if (module_rcode == RLM_MODULE_OK) {
2027 RDEBUG("Bind as user \"%s\" was successful", user_dn);
2030 ldap_release_socket(inst, conn);
2031 return module_rcode;
2035 /* globally exported name */
2036 module_t rlm_ldap = {
2039 RLM_TYPE_THREAD_SAFE, /* type: reserved */
2040 ldap_instantiate, /* instantiation */
2041 ldap_detach, /* detach */
2043 ldap_authenticate, /* authentication */
2044 ldap_authorize, /* authorization */
2045 NULL, /* preaccounting */
2046 NULL, /* accounting */
2047 NULL, /* checksimul */
2048 NULL, /* pre-proxy */
2049 NULL, /* post-proxy */