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-2012 The FreeRADIUS Server Project.
20 * Copyright 2012 Alan DeKok <aland@freeradius.org>
23 #include <freeradius-devel/ident.h>
26 #include <freeradius-devel/radiusd.h>
27 #include <freeradius-devel/modules.h>
28 #include <freeradius-devel/rad_assert.h>
36 #define MAX_ATTRMAP 128
37 #define MAX_ATTR_STR_LEN 256
38 #define MAX_FILTER_STR_LEN 1024
41 extern int nmasldap_get_password(LDAP *ld,char *objectDN, char *pwd, size_t *pwdSize);
47 fr_connection_pool_t *pool;
61 int ldap_debug; /* Debug flag for LDAP SDK */
63 const char *xlat_name; /* name used to xlat */
68 * RADIUS attribute to LDAP attribute maps
70 VALUE_PAIR_MAP *user_map; /* Applied to users and profiles */
73 * Access related configuration
76 int positive_access_attr;
82 char *default_profile;
89 char *groupmemb_filter;
93 * TLS items. We should really normalize these with the
103 char *tls_require_cert;
122 #ifdef LDAP_OPT_X_KEEPALIVE_IDLE
125 #ifdef LDAP_OPT_X_KEEPALIVE_PROBES
126 int keepalive_probes;
128 #ifdef LDAP_OPT_ERROR_NUMBER
129 int keepalive_interval;
134 /* The default setting for TLS Certificate Verification */
135 #define TLS_DEFAULT_VERIFY "allow"
140 static CONF_PARSER tls_config[] = {
141 {"start_tls", PW_TYPE_BOOLEAN,
142 offsetof(ldap_instance,start_tls), NULL, "no"},
143 {"cacertfile", PW_TYPE_FILENAME,
144 offsetof(ldap_instance,tls_cacertfile), NULL, NULL},
145 {"cacertdir", PW_TYPE_FILENAME,
146 offsetof(ldap_instance,tls_cacertdir), NULL, NULL},
147 {"certfile", PW_TYPE_FILENAME,
148 offsetof(ldap_instance,tls_certfile), NULL, NULL},
149 {"keyfile", PW_TYPE_FILENAME,
150 offsetof(ldap_instance,tls_keyfile), NULL, NULL},
151 {"randfile", PW_TYPE_STRING_PTR, /* OK if it changes on HUP */
152 offsetof(ldap_instance,tls_randfile), NULL, NULL},
153 {"require_cert", PW_TYPE_STRING_PTR,
154 offsetof(ldap_instance,tls_require_cert), NULL, TLS_DEFAULT_VERIFY},
155 { NULL, -1, 0, NULL, NULL }
159 static CONF_PARSER attr_config[] = {
163 /* LDAP attribute name that controls remote access */
164 {"access_attr", PW_TYPE_STRING_PTR,
165 offsetof(ldap_instance,access_attr), NULL, NULL},
166 {"positive_access_attr", PW_TYPE_BOOLEAN,
167 offsetof(ldap_instance,positive_access_attr), NULL, "yes"},
169 {"base_filter", PW_TYPE_STRING_PTR,
170 offsetof(ldap_instance,base_filter), NULL,
171 "(objectclass=radiusprofile)"},
172 {"default_profile", PW_TYPE_STRING_PTR,
173 offsetof(ldap_instance,default_profile), NULL, NULL},
174 {"profile_attribute", PW_TYPE_STRING_PTR,
175 offsetof(ldap_instance,profile_attr), NULL, NULL},
177 { NULL, -1, 0, NULL, NULL }
182 * Group configuration
184 static CONF_PARSER group_config[] = {
186 * Group checks. These could probably be done
187 * via dynamic xlat's.
189 {"name_attribute", PW_TYPE_STRING_PTR,
190 offsetof(ldap_instance,groupname_attr), NULL, "cn"},
191 {"membership_filter", PW_TYPE_STRING_PTR,
192 offsetof(ldap_instance,groupmemb_filter), NULL,
193 "(|(&(objectClass=GroupOfNames)(member=%{Ldap-UserDn}))"
194 "(&(objectClass=GroupOfUniqueNames)(uniquemember=%{Ldap-UserDn})))"},
195 {"membership_attribute", PW_TYPE_STRING_PTR,
196 offsetof(ldap_instance,groupmemb_attr), NULL, NULL},
199 { NULL, -1, 0, NULL, NULL }
203 * Various options that don't belong in the main configuration.
205 * Note that these overlap a bit with the connection pool code!
207 static CONF_PARSER option_config[] = {
209 * Debugging flags to the server
211 {"ldap_debug", PW_TYPE_INTEGER,
212 offsetof(ldap_instance,ldap_debug), NULL, "0x0000"},
214 {"chase_referrals", PW_TYPE_BOOLEAN,
215 offsetof(ldap_instance,chase_referrals), NULL, NULL},
217 {"rebind", PW_TYPE_BOOLEAN,
218 offsetof(ldap_instance,rebind), NULL, NULL},
220 /* timeout on network activity */
221 {"net_timeout", PW_TYPE_INTEGER,
222 offsetof(ldap_instance,net_timeout), NULL, "10"},
224 /* timeout for search results */
225 {"timeout", PW_TYPE_INTEGER,
226 offsetof(ldap_instance,timeout), NULL, "20"},
228 /* allow server unlimited time for search (server-side limit) */
229 {"timelimit", PW_TYPE_INTEGER,
230 offsetof(ldap_instance,timelimit), NULL, "20"},
232 #ifdef LDAP_OPT_X_KEEPALIVE_IDLE
233 {"idle", PW_TYPE_INTEGER,
234 offsetof(ldap_instance,keepalive_idle), NULL, "60"},
236 #ifdef LDAP_OPT_X_KEEPALIVE_PROBES
237 {"probes", PW_TYPE_INTEGER,
238 offsetof(ldap_instance,keepalive_probes), NULL, "3"},
240 #ifdef LDAP_OPT_ERROR_NUMBER
241 {"interval", PW_TYPE_INTEGER,
242 offsetof(ldap_instance,keepalive_interval), NULL, "30"},
244 { NULL, -1, 0, NULL, NULL }
248 static const CONF_PARSER module_config[] = {
249 {"server", PW_TYPE_STRING_PTR,
250 offsetof(ldap_instance,server), NULL, "localhost"},
251 {"port", PW_TYPE_INTEGER,
252 offsetof(ldap_instance,port), NULL, "389"},
254 {"password", PW_TYPE_STRING_PTR,
255 offsetof(ldap_instance,password), NULL, ""},
256 {"identity", PW_TYPE_STRING_PTR,
257 offsetof(ldap_instance,login), NULL, ""},
262 {"basedn", PW_TYPE_STRING_PTR,
263 offsetof(ldap_instance,basedn), NULL, "o=notexist"},
265 {"filter", PW_TYPE_STRING_PTR,
266 offsetof(ldap_instance,filter), NULL, "(uid=%u)"},
268 /* turn off the annoying warning if we don't expect a password */
269 {"expect_password", PW_TYPE_BOOLEAN,
270 offsetof(ldap_instance,expect_password), NULL, "yes"},
273 /* support for eDirectory Universal Password */
274 {"edir", PW_TYPE_BOOLEAN,
275 offsetof(ldap_instance,edir), NULL, NULL}, /* NULL defaults to "no" */
279 * Terrible things which should be deleted.
281 { "profiles", PW_TYPE_SUBSECTION, 0, NULL, (const void *) attr_config },
283 { "group", PW_TYPE_SUBSECTION, 0, NULL, (const void *) group_config },
285 { "options", PW_TYPE_SUBSECTION, 0, NULL,
286 (const void *) option_config },
288 { "tls", PW_TYPE_SUBSECTION, 0, NULL, (const void *) tls_config },
290 {NULL, -1, 0, NULL, NULL}
293 typedef struct ldap_conn {
300 typedef struct xlat_attrs {
301 const VALUE_PAIR_MAP *maps;
302 const char *attrs[MAX_ATTRMAP];
305 typedef struct rlm_ldap_result {
311 #if LDAP_SET_REBIND_PROC_ARGS == 3
313 * Rebind && chase referral stuff
315 static int ldap_rebind(LDAP *handle, LDAP_CONST char *url,
316 UNUSED ber_tag_t request, UNUSED ber_int_t msgid,
319 LDAP_CONN *conn = ctx;
321 conn->referred = TRUE;
322 conn->rebound = TRUE; /* not really, but oh well... */
323 rad_assert(handle == conn->handle);
325 DEBUG("rlm_ldap (%s): Rebinding to URL %s", conn->inst->xlat_name, url);
327 return ldap_bind_s(handle, conn->inst->login, conn->inst->password,
332 static int ldap_bind_wrapper(LDAP_CONN **pconn, const char *user,
333 const char *password,
334 const char **perror_str, int do_rebind)
336 int rcode, ldap_errno;
337 int module_rcode = RLM_MODULE_FAIL;
338 int reconnect = FALSE;
339 const char *error_string;
340 LDAP_CONN *conn = *pconn;
341 ldap_instance *inst = conn->inst;
342 LDAPMessage *result = NULL;
346 ldap_errno = ldap_bind(conn->handle, user, password, LDAP_AUTH_SIMPLE);
347 if (ldap_errno < 0) {
349 ldap_get_option(conn->handle, LDAP_OPT_ERROR_NUMBER,
351 error_string = ldap_err2string(ldap_errno);
353 if (do_rebind && !reconnect) {
354 conn = fr_connection_reconnect(inst->pool, conn);
356 if (!conn) return RLM_MODULE_FAIL;
361 if (perror_str) *perror_str = error_string;
363 #ifdef HAVE_LDAP_INITIALIZE
365 radlog(L_ERR, "rlm_ldap (%s): %s bind to %s failed: %s",
366 inst->xlat_name, user,
367 inst->server, error_string);
371 radlog(L_ERR, "rlm_ldap (%s): %s bind to %s:%d "
373 inst->xlat_name, user,
374 inst->server, inst->port,
378 return module_rcode; /* caller closes the connection */
381 DEBUG3("rlm_ldap (%s): Waiting for bind result...", inst->xlat_name);
383 tv.tv_sec = inst->timeout;
385 rcode = ldap_result(conn->handle, ldap_errno, 1, &tv, &result);
386 if (rcode < 0) goto get_error;
389 error_string = "timeout";
393 ldap_errno = ldap_result2error(conn->handle, result, 1);
394 switch (ldap_errno) {
398 case LDAP_INVALID_CREDENTIALS:
399 case LDAP_CONSTRAINT_VIOLATION:
400 rcode = RLM_MODULE_REJECT;
407 return RLM_MODULE_OK;
410 /*************************************************************************
412 * Function: ldap_conn_create
414 * Purpose: Create and return a new connection
415 * This function is probably too big.
417 *************************************************************************/
418 static void *ldap_conn_create(void *ctx)
421 int ldap_errno, ldap_version;
423 ldap_instance *inst = ctx;
425 LDAP_CONN *conn = NULL;
428 #ifdef HAVE_LDAP_INITIALIZE
430 DEBUG("rlm_ldap (%s): Connect to %s", inst->xlat_name,
433 ldap_errno = ldap_initialize(&handle, inst->server);
435 if (ldap_errno != LDAP_SUCCESS) {
436 radlog(L_ERR, "rlm_ldap (%s): ldap_initialize() "
438 inst->xlat_name, ldap_err2string(ldap_errno));
444 DEBUG("rlm_ldap (%s): Connect to %s:%d", inst->xlat_name,
445 inst->server, inst->port);
447 handle = ldap_init(inst->server, inst->port);
449 radlog(L_ERR, "rlm_ldap (%s): ldap_init() failed",
452 if (handle) ldap_unbind_s(handle);
458 * We now have a connection structure, but no actual TCP connection.
460 * Set a bunch of LDAP options, using common code.
462 #define do_ldap_option(_option, _name, _value) \
463 if (ldap_set_option(handle, _option, _value) != LDAP_OPT_SUCCESS) { \
464 ldap_get_option(handle, LDAP_OPT_ERROR_NUMBER, &ldap_errno); \
465 radlog(L_ERR, "rlm_ldap (%s): Could not set %s: %s", \
466 inst->xlat_name, _name, ldap_err2string(ldap_errno)); \
469 if (inst->ldap_debug) {
470 do_ldap_option(LDAP_OPT_DEBUG_LEVEL, "ldap_debug",
471 &(inst->ldap_debug));
475 * Leave "chase_referrals" unset to use the OpenLDAP
478 if (inst->chase_referrals != 2) {
479 if (inst->chase_referrals) {
480 do_ldap_option(LDAP_OPT_REFERRALS, "chase_referrals",
483 #if LDAP_SET_REBIND_PROC_ARGS == 3
484 if (inst->rebind == 1) {
485 ldap_set_rebind_proc(handle, ldap_rebind, inst);
489 do_ldap_option(LDAP_OPT_REFERRALS, "chase_referrals",
494 tv.tv_sec = inst->net_timeout;
496 do_ldap_option(LDAP_OPT_NETWORK_TIMEOUT, "net_timeout", &tv);
498 do_ldap_option(LDAP_OPT_TIMELIMIT, "timelimit",
501 ldap_version = LDAP_VERSION3;
502 do_ldap_option(LDAP_OPT_PROTOCOL_VERSION, "ldap_version",
505 #ifdef LDAP_OPT_X_KEEPALIVE_IDLE
506 do_ldap_option(LDAP_OPT_X_KEEPALIVE_IDLE, "keepalive idle",
507 &(inst->keepalive_idle));
510 #ifdef LDAP_OPT_X_KEEPALIVE_PROBES
511 do_ldap_option(LDAP_OPT_X_KEEPALIVE_PROBES, "keepalive probes",
512 &(inst->keepalive_probes));
515 #ifdef LDAP_OPT_X_KEEPALIVE_INTERVAL
516 do_ldap_option(LDAP_OPT_X_KEEPALIVE_INTERVAL, "keepalive interval",
517 &(inst->keepalive_interval));
520 #ifdef HAVE_LDAP_START_TLS
522 * Set all of the TLS options
524 if (inst->tls_mode) {
525 do_ldap_option(LDAP_OPT_X_TLS, "tls_mode", &(inst->tls_mode));
528 #define maybe_ldap_option(_option, _name, _value) \
529 if (_value) do_ldap_option(_option, _name, _value)
531 maybe_ldap_option(LDAP_OPT_X_TLS_CACERTFILE,
532 "cacertfile", inst->tls_cacertfile);
533 maybe_ldap_option(LDAP_OPT_X_TLS_CACERTDIR,
534 "cacertdir", inst->tls_cacertdir);
536 #ifdef HAVE_LDAP_INT_TLS_CONFIG
537 if (ldap_int_tls_config(NULL, LDAP_OPT_X_TLS_REQUIRE_CERT,
538 (inst->tls_require_cert)) != LDAP_OPT_SUCCESS) {
539 ldap_get_option(handle, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
540 radlog(L_ERR, "rlm_ldap (%s): could not set "
541 "LDAP_OPT_X_TLS_REQUIRE_CERT option to %s: %s",
543 inst->tls_require_cert,
544 ldap_err2string(ldap_errno));
548 maybe_ldap_option(LDAP_OPT_X_TLS_CERTFILE,
549 "certfile", inst->tls_certfile);
550 maybe_ldap_option(LDAP_OPT_X_TLS_KEYFILE,
551 "keyfile", inst->tls_keyfile);
552 maybe_ldap_option(LDAP_OPT_X_TLS_RANDOM_FILE,
553 "randfile", inst->tls_randfile);
556 * And finally start the TLS code.
558 if (inst->start_tls && (inst->port != 636)) {
559 ldap_errno = ldap_start_tls_s(handle, NULL, NULL);
560 if (ldap_errno != LDAP_SUCCESS) {
561 ldap_get_option(handle, LDAP_OPT_ERROR_NUMBER,
563 radlog(L_ERR, "rlm_ldap (%s): could not start TLS: %s",
565 ldap_err2string(ldap_errno));
569 #endif /* HAVE_LDAP_START_TLS */
571 conn = rad_malloc(sizeof(*conn));
573 conn->handle = handle;
574 conn->rebound = FALSE;
575 conn->referred = FALSE;
577 module_rcode = ldap_bind_wrapper(&conn, inst->login, inst->password,
579 if (module_rcode != RLM_MODULE_OK) {
580 radlog(L_ERR, "rlm_ldap (%s): failed binding to LDAP "
582 inst->xlat_name, error);
585 * FIXME: print "check config, morians!
594 /*************************************************************************
596 * Function: ldap_conn_delete
598 * Purpose: Close and delete a connection
600 *************************************************************************/
601 static int ldap_conn_delete(UNUSED void *ctx, void *connection)
603 LDAP_CONN *conn = connection;
605 ldap_unbind_s(conn->handle);
612 /*************************************************************************
614 * Function: ldap_get_socket
616 * Purpose: Gets an LDAP socket from the connection pool
618 *************************************************************************/
619 static LDAP_CONN *ldap_get_socket(ldap_instance *inst)
623 conn = fr_connection_get(inst->pool);
625 radlog(L_ERR, "rlm_ldap (%s): all ldap connections are in use",
633 /*************************************************************************
635 * Function: ldap_release_socket
637 * Purpose: Frees an LDAP socket back to the connection pool
639 *************************************************************************/
640 static void ldap_release_socket(ldap_instance *inst, LDAP_CONN *conn)
643 * Could have already been free'd due to a previous error.
648 * We chased a referral to another server.
650 * This connection is no longer part of the pool which is
651 * connected to and bound to the configured server.
654 * Note that we do NOT close it if it was bound to
655 * another user. Instead, we let the next caller do the
658 if (conn->referred) {
659 fr_connection_del(inst->pool, conn);
663 fr_connection_release(inst->pool, conn);
668 /*************************************************************************
670 * Function: ldap_escape_func
672 * Purpose: Converts "bad" strings into ones which are safe for LDAP
674 *************************************************************************/
675 static size_t ldap_escape_func(UNUSED REQUEST *request, char *out,
676 size_t outlen, const char *in, UNUSED void *arg)
682 * Encode unsafe characters.
685 ((in[0] == ' ') || (in[0] == '#'))) ||
686 (strchr(",+\"\\<>;*=()", *in))) {
687 static const char hex[] = "0123456789abcdef";
690 * Only 3 or less bytes available.
697 *(out++) = hex[((*in) >> 4) & 0x0f];
698 *(out++) = hex[(*in) & 0x0f];
706 * Only one byte left.
723 /*************************************************************************
725 * Function: perform_search
727 * Purpose: Do a search and get a response
729 *************************************************************************/
730 static int perform_search(ldap_instance *inst, REQUEST *request,
731 LDAP_CONN **pconn, const char *search_basedn,
732 int scope, const char *filter,
733 const char * const *attrs, LDAPMessage **presult)
737 int reconnect = FALSE;
738 LDAP_CONN *conn = *pconn;
742 * OpenLDAP library doesn't declare attrs array as const, but
743 * it really should be *sigh*.
746 memcpy(&search_attrs, &attrs, sizeof(attrs));
751 * Do all searches as the default admin user.
754 ldap_errno = ldap_bind_wrapper(pconn,
755 inst->login, inst->password,
757 if (ldap_errno != RLM_MODULE_OK) {
761 rad_assert(*pconn != NULL);
763 conn->rebound = FALSE;
766 tv.tv_sec = inst->timeout;
768 RDEBUG2("Performing search in '%s' with filter '%s'",
769 search_basedn ? search_basedn : "(null)" ,
773 ldap_errno = ldap_search_ext_s(conn->handle, search_basedn, scope,
774 filter, search_attrs, 0, NULL, NULL,
776 switch (ldap_errno) {
778 case LDAP_NO_SUCH_OBJECT:
781 case LDAP_SERVER_DOWN:
783 ldap_msgfree(*presult);
785 if (reconnect) return -1;
788 conn = fr_connection_reconnect(inst->pool, conn);
789 *pconn = conn; /* tell the caller we have a new connection */
790 if (!conn) return -1;
793 case LDAP_INSUFFICIENT_ACCESS:
794 radlog(L_ERR, "rlm_ldap (%s): Search failed: "
795 "Insufficient access. Check the identity and password "
796 "configuration directives", inst->xlat_name);
797 ldap_msgfree(*presult);
801 exec_trigger(NULL, inst->cs, "modules.ldap.timeout", TRUE);
802 radlog(L_ERR, "rlm_ldap (%s): Search failed: Timed out "
803 "while waiting for server to respond"
804 "Please increase the timeout", inst->xlat_name);
805 ldap_msgfree(*presult);
808 case LDAP_FILTER_ERROR:
809 radlog(L_ERR, "rlm_ldap (%s): Search failed: Bad search "
810 "filter: %s", inst->xlat_name,filter);
811 ldap_msgfree(*presult);
814 case LDAP_TIMELIMIT_EXCEEDED:
815 exec_trigger(NULL, inst->cs, "modules.ldap.timeout", TRUE);
818 case LDAP_UNAVAILABLE:
820 * Reconnect. There's an issue with the socket
823 ldap_get_option(conn->handle, LDAP_OPT_ERROR_NUMBER,
825 radlog(L_ERR, "rlm_ldap (%s): Search failed: %s",
826 inst->xlat_name, ldap_err2string(ldap_errno));
830 ldap_get_option(conn->handle, LDAP_OPT_ERROR_NUMBER,
832 radlog(L_ERR, "rlm_ldap (%s): Search failed: %s",
833 inst->xlat_name, ldap_err2string(ldap_errno));
834 ldap_msgfree(*presult);
838 count = ldap_count_entries(conn->handle, *presult);
840 ldap_msgfree(*presult);
841 RDEBUG("Search returned no results");
847 ldap_msgfree(*presult);
848 RDEBUG("Got ambiguous search result (%d results)", count);
856 /*************************************************************************
858 * Function: ldap_xlat
860 * Purpose: Expand an LDAP URL into a query, and return a string
861 * result from that query.
863 *************************************************************************/
864 static size_t ldap_xlat(void *instance, REQUEST *request, const char *fmt,
865 char *out, size_t freespace)
869 ldap_instance *inst = instance;
870 LDAPURLDesc *ldap_url;
871 LDAPMessage *result = NULL;
872 LDAPMessage *entry = NULL;
878 char buffer[MAX_FILTER_STR_LEN];
880 if (strchr(fmt, '%') != NULL) {
881 if (!radius_xlat(buffer, sizeof(buffer), fmt, request,
882 ldap_escape_func, NULL)) {
884 "rlm_ldap (%s): Unable to create LDAP URL",
893 if (!ldap_is_ldap_url(url)) {
894 radlog(L_ERR, "rlm_ldap (%s): String passed does not look "
895 "like an LDAP URL", inst->xlat_name);
899 if (ldap_url_parse(url, &ldap_url)){
900 radlog(L_ERR, "rlm_ldap (%s): Parsing LDAP URL failed",
906 * Nothing, empty string, "*" string, or got 2 things, die.
908 if (!ldap_url->lud_attrs || !ldap_url->lud_attrs[0] ||
909 !*ldap_url->lud_attrs[0] ||
910 (strcmp(ldap_url->lud_attrs[0], "*") == 0) ||
911 ldap_url->lud_attrs[1]) {
912 radlog(L_ERR, "rlm_ldap (%s): Bad attributes list in LDAP "
913 "URL. URL must specify exactly one attribute to "
920 if (ldap_url->lud_host &&
921 ((strncmp(inst->server, ldap_url->lud_host,
922 strlen(inst->server)) != 0) ||
923 (ldap_url->lud_port != inst->port))) {
924 RDEBUG("Requested server/port is \"%s:%i\"", ldap_url->lud_host,
930 conn = ldap_get_socket(inst);
931 if (!conn) goto free_urldesc;
933 memcpy(&attrs, &ldap_url->lud_attrs, sizeof(attrs));
935 rcode = perform_search(inst, request, &conn, ldap_url->lud_dn,
936 ldap_url->lud_scope, ldap_url->lud_filter, attrs,
940 RDEBUG("Search returned not found", inst->xlat_name);
947 entry = ldap_first_entry(conn->handle, result);
949 ldap_get_option(conn->handle, LDAP_OPT_RESULT_CODE,
951 radlog(L_ERR, "rlm_ldap (%s): Failed retrieving entry: %s",
953 ldap_err2string(ldap_errno));
957 vals = ldap_get_values(conn->handle, entry, ldap_url->lud_attrs[0]);
959 RDEBUG("No \"%s\" attributes found in specified object",
960 inst->xlat_name, ldap_url->lud_attrs[0]);
964 length = strlen(vals[0]);
965 if (length >= freespace){
970 strlcpy(out, vals[0], freespace);
973 ldap_value_free(vals);
975 ldap_msgfree(result);
977 ldap_release_socket(inst, conn);
979 ldap_free_urldesc(ldap_url);
985 static char *get_userdn(LDAP_CONN **pconn, REQUEST *request, int *module_rcode)
989 ldap_instance *inst = (*pconn)->inst;
990 LDAP *handle = (*pconn)->handle;
991 LDAPMessage *result, *entry;
993 static char firstattr[] = "uid";
995 const char *attrs[] = {firstattr, NULL};
996 char filter[MAX_FILTER_STR_LEN];
997 char basedn[MAX_FILTER_STR_LEN];
999 *module_rcode = RLM_MODULE_FAIL;
1001 vp = pairfind(request->config_items, PW_LDAP_USERDN, 0);
1002 if (vp) return vp->vp_strvalue;
1004 if (!radius_xlat(filter, sizeof(filter), inst->filter,
1005 request, ldap_escape_func, NULL)) {
1006 radlog(L_ERR, "rlm_ldap (%s): Unable to create filter",
1008 *module_rcode = RLM_MODULE_INVALID;
1012 if (!radius_xlat(basedn, sizeof(basedn), inst->basedn,
1013 request, ldap_escape_func, NULL)) {
1014 radlog(L_ERR, "rlm_ldap (%s): Unable to create basedn",
1016 *module_rcode = RLM_MODULE_INVALID;
1020 rcode = perform_search(inst, request, pconn, basedn, LDAP_SCOPE_SUBTREE,
1021 filter, attrs, &result);
1024 *module_rcode = RLM_MODULE_NOTFOUND;
1030 if ((entry = ldap_first_entry(handle, result)) == NULL) {
1031 ldap_get_option(handle, LDAP_OPT_RESULT_CODE,
1033 radlog(L_ERR, "rlm_ldap (%s): Failed retrieving entry: %s",
1035 ldap_err2string(ldap_errno));
1036 ldap_msgfree(result);
1040 if ((user_dn = ldap_get_dn(handle, entry)) == NULL) {
1041 ldap_get_option(handle, LDAP_OPT_RESULT_CODE,
1043 radlog(L_ERR, "rlm_ldap (%s): ldap_get_dn() failed: %s",
1045 ldap_err2string(ldap_errno));
1047 ldap_msgfree(result);
1051 vp = pairmake("LDAP-UserDn", user_dn, T_OP_EQ);
1053 ldap_memfree(user_dn);
1054 ldap_msgfree(result);
1058 pairadd(&request->config_items, vp);
1059 ldap_memfree(user_dn);
1060 ldap_msgfree(result);
1062 return vp->vp_strvalue;
1066 /*****************************************************************************
1068 * Perform LDAP-Group comparison checking
1070 *****************************************************************************/
1071 static int ldap_groupcmp(void *instance, REQUEST *request,
1072 UNUSED VALUE_PAIR *thing, VALUE_PAIR *check,
1073 UNUSED VALUE_PAIR *check_pairs,
1074 UNUSED VALUE_PAIR **reply_pairs)
1076 ldap_instance *inst = instance;
1077 int i, rcode, found, module_rcode;
1078 LDAPMessage *result = NULL;
1079 LDAPMessage *entry = NULL;
1081 int check_is_dn = FALSE, value_is_dn = FALSE;
1082 static char firstattr[] = "dn";
1083 const char *attrs[] = {firstattr, NULL};
1085 const char *group_attrs[] = {inst->groupmemb_attr, NULL};
1089 char gr_filter[MAX_FILTER_STR_LEN];
1090 char filter[MAX_FILTER_STR_LEN];
1091 char basedn[MAX_FILTER_STR_LEN];
1093 RDEBUG("Searching for user in group \"%s\"", check->vp_strvalue);
1095 if (check->length == 0) {
1096 RDEBUG("Cannot do comparison (group name is empty)");
1100 conn = ldap_get_socket(inst);
1101 if (!conn) return 1;
1104 * This is used in the default membership filter.
1106 user_dn = get_userdn(&conn, request, &module_rcode);
1108 ldap_release_socket(inst, conn);
1112 if (!inst->groupmemb_filter) goto check_attr;
1114 if (!radius_xlat(gr_filter, sizeof(gr_filter),
1115 inst->groupmemb_filter, request, ldap_escape_func,
1117 radlog(L_ERR, "rlm_ldap (%s): Failed creating group filter",
1123 * If it's a DN, use that.
1125 check_is_dn = strchr(check->vp_strvalue,',') == NULL ? FALSE : TRUE;
1128 strlcpy(filter, gr_filter, sizeof(filter));
1129 strlcpy(basedn, check->vp_strvalue, sizeof(basedn));
1131 snprintf(filter, sizeof(filter), "(&(%s=%s)%s)",
1132 inst->groupname_attr,
1133 check->vp_strvalue, gr_filter);
1136 * get_userdn does this, too. Oh well.
1138 if (!radius_xlat(basedn, sizeof(basedn), inst->basedn,
1139 request, ldap_escape_func, NULL)) {
1140 radlog(L_ERR, "rlm_ldap (%s): Failed creating basedn",
1146 rcode = perform_search(inst, request, &conn, basedn, LDAP_SCOPE_SUBTREE,
1147 filter, attrs, &result);
1149 ldap_release_socket(inst, conn);
1150 ldap_msgfree(result);
1152 RDEBUG("User found in group object");
1158 ldap_release_socket(inst, conn);
1162 /* else the search returned -2, for "not found" */
1165 * Else the search returned NOTFOUND. See if we're
1166 * configured to search for group membership using user
1169 if (!inst->groupmemb_attr) {
1170 ldap_release_socket(inst, conn);
1171 RDEBUG("Group object \"%s\" not found, or user is not a member",
1172 check->vp_strvalue);
1177 RDEBUG2("Checking user object membership (%s) attributes",
1178 inst->groupmemb_attr);
1180 snprintf(filter ,sizeof(filter), "(objectclass=*)");
1182 rcode = perform_search(inst, request, &conn, user_dn, LDAP_SCOPE_BASE,
1183 filter, group_attrs, &result);
1186 RDEBUG("Can't check membership attributes, user object "
1189 ldap_release_socket(inst, conn);
1193 entry = ldap_first_entry(conn->handle, result);
1195 ldap_get_option(conn->handle, LDAP_OPT_RESULT_CODE,
1197 radlog(L_ERR, "rlm_ldap (%s): Failed retrieving entry: %s",
1199 ldap_err2string(ldap_errno));
1201 ldap_release_socket(inst, conn);
1202 ldap_msgfree(result);
1206 vals = ldap_get_values(conn->handle, entry, inst->groupmemb_attr);
1208 RDEBUG("No group membership attribute(s) found in user object");
1209 ldap_release_socket(inst, conn);
1210 ldap_msgfree(result);
1215 * Loop over the list of groups the user is a member of,
1216 * looking for a match.
1219 for (i = 0; i < ldap_count_values(vals); i++) {
1220 LDAPMessage *gr_result = NULL;
1222 value_is_dn = strchr(vals[i], ',') == NULL ? FALSE : TRUE;
1224 RDEBUG2("Processing group membership value \"%s\"", vals[i]);
1227 * Both literal group names, do case sensitive comparison
1229 if (!check_is_dn && !value_is_dn) {
1230 if (strcmp(vals[i], check->vp_strvalue) == 0){
1231 RDEBUG("User found (membership value matches "
1242 * Both DNs, do case insensitive comparison
1244 if (check_is_dn && value_is_dn) {
1245 if (strcasecmp(vals[i], check->vp_strvalue) == 0){
1246 RDEBUG("User found (membership DN matches "
1257 * If the value is not a DN, or the check item is a DN
1258 * there's nothing more we can do.
1260 if (!value_is_dn && check_is_dn) continue;
1263 * We have a value which is a DN, and a check item which
1264 * specifies the name of a group, search using the value
1265 * DN for the group, and see if it has a groupname_attr
1266 * which matches our check val.
1268 RDEBUG2("Searching with membership DN and group name");
1270 snprintf(filter,sizeof(filter), "(%s=%s)",
1271 inst->groupname_attr, check->vp_strvalue);
1273 rcode = perform_search(inst, request, &conn, vals[i],
1274 LDAP_SCOPE_BASE, filter, attrs,
1277 ldap_msgfree(gr_result);
1279 /* Error occurred */
1281 ldap_value_free(vals);
1282 ldap_msgfree(result);
1283 ldap_release_socket(inst, conn);
1288 * Either the group DN wasn't found, or it didn't have the
1289 * correct name. Continue looping over the attributes.
1292 ldap_msgfree(gr_result);
1298 RDEBUG("User found (group name in membership DN matches check "
1304 ldap_value_free(vals);
1305 ldap_msgfree(result);
1306 ldap_release_socket(inst, conn);
1309 RDEBUG("User is not a member of specified group");
1317 * Verify that the ldap update section makes sense, and add attribute names
1318 * to array of attributes for efficient querying later.
1320 static int build_attrmap(CONF_SECTION *cs, VALUE_PAIR_MAP **head)
1322 const char *cs_list, *p;
1324 request_refs_t request_def = REQUEST_CURRENT;
1325 pair_lists_t list_def = PAIR_LIST_REQUEST;
1327 CONF_ITEM *ci = cf_sectiontoitem(cs);
1330 unsigned int total = 0;
1331 VALUE_PAIR_MAP **tail, *map;
1337 cs_list = p = cf_section_name2(cs);
1339 request_def = radius_request_name(&p, REQUEST_UNKNOWN);
1340 if (request_def == REQUEST_UNKNOWN) {
1341 cf_log_err(ci, "rlm_ldap: Default request specified "
1342 "in mapping section is invalid");
1346 list_def = fr_str2int(pair_lists, p, PAIR_LIST_UNKNOWN);
1347 if (list_def == PAIR_LIST_UNKNOWN) {
1348 cf_log_err(ci, "rlm_ldap: Default list specified "
1349 "in mapping section is invalid");
1354 for (ci = cf_item_find_next(cs, NULL);
1356 ci = cf_item_find_next(cs, ci)) {
1357 if (total++ == MAX_ATTRMAP) {
1358 cf_log_err(ci, "rlm_ldap: Attribute map size exceeded");
1362 if (!cf_item_is_pair(ci)) {
1363 cf_log_err(ci, "rlm_ldap: Entry is not in \"attribute ="
1364 " ldap-attribute\" format");
1368 cp = cf_itemtopair(ci);
1369 map = radius_cp2map(cp, REQUEST_CURRENT, list_def);
1375 tail = &(map->next);
1381 radius_mapfree(head);
1385 /*****************************************************************************
1387 * Detach from the LDAP server and cleanup internal state.
1389 *****************************************************************************/
1390 static int ldap_detach(void *instance)
1392 ldap_instance *inst = instance;
1394 fr_connection_pool_delete(inst->pool);
1396 if (inst->user_map) {
1397 radius_mapfree(&inst->user_map);
1405 /*************************************************************************
1407 * Function: rlm_ldap_instantiate
1409 * Purpose: Uses section of radiusd config file passed as parameter
1410 * to create an instance of the module.
1412 *************************************************************************/
1413 static int ldap_instantiate(CONF_SECTION * conf, void **instance)
1415 ldap_instance *inst;
1418 inst = rad_malloc(sizeof *inst);
1422 memset(inst, 0, sizeof(*inst));
1425 inst->chase_referrals = 2; /* use OpenLDAP defaults */
1428 if (cf_section_parse(conf, inst, module_config) < 0) {
1433 inst->xlat_name = cf_section_name2(conf);
1434 if (!inst->xlat_name) inst->xlat_name = cf_section_name1(conf);
1436 if (inst->server == NULL) {
1437 radlog(L_ERR, "rlm_ldap (%s): missing 'server' directive",
1444 * Check for URLs. If they're used and the library doesn't
1445 * support them, then complain.
1448 if (ldap_is_ldap_url(inst->server)) {
1449 #ifdef HAVE_LDAP_INITIALIZE
1453 radlog(L_ERR, "rlm_ldap (%s): 'server' directive is in URL "
1454 "form but ldap_initialize() is not available",
1461 /* workaround for servers which support LDAPS but not START TLS */
1462 if (inst->port == LDAPS_PORT || inst->tls_mode) {
1463 inst->tls_mode = LDAP_OPT_X_TLS_HARD;
1468 #if LDAP_SET_REBIND_PROC_ARGS != 3
1470 * The 2-argument rebind doesn't take an instance
1471 * variable. Our rebind function needs the instance
1472 * variable for the username, password, etc.
1474 if (inst->rebind == 1) {
1475 radlog(L_ERR, "rlm_ldap (%s): Cannot use 'rebind' directive "
1476 "as this version of libldap does not support the API "
1477 "that we need", inst->xlat-name);
1484 * Build the attribute map
1486 cs = cf_section_sub_find(conf, "update");
1488 if (build_attrmap(cs, &(inst->user_map)) < 0) {
1495 * Group comparison checks.
1497 paircompare_register(PW_LDAP_GROUP, PW_USER_NAME, ldap_groupcmp, inst);
1498 if (cf_section_name2(conf)) {
1503 snprintf(buffer, sizeof(buffer), "%s-Ldap-Group",
1505 memset(&flags, 0, sizeof(flags));
1507 dict_addattr(buffer, -1, 0, PW_TYPE_STRING, flags);
1508 da = dict_attrbyname(buffer);
1510 radlog(L_ERR, "rlm_ldap (%s): Failed creating "
1511 "attribute %s", inst->xlat_name, buffer);
1516 paircompare_register(da->attr, PW_USER_NAME, ldap_groupcmp,
1520 xlat_register(inst->xlat_name, ldap_xlat, inst);
1523 * Initialize the socket pool.
1525 inst->pool = fr_connection_pool_init(inst->cs, inst,
1538 static int check_access(ldap_instance *inst, REQUEST* request, LDAP_CONN *conn,
1544 vals = ldap_get_values(conn->handle, entry, inst->access_attr);
1546 if (inst->positive_access_attr) {
1547 if (strncmp(vals[0], "FALSE", 5) == 0) {
1548 RDEBUG("Dialup access disabled");
1555 RDEBUG("\"%s\" attribute exists - access denied by"
1556 " default", inst->access_attr);
1559 ldap_value_free(vals);
1561 } else if (inst->positive_access_attr) {
1562 RDEBUG("No %s attribute - access denied by default",
1573 static VALUE_PAIR *ldap_getvalue(REQUEST *request, const VALUE_PAIR_TMPL *dst,
1576 rlm_ldap_result_t *self = ctx;
1577 VALUE_PAIR *head, **tail, *vp;
1586 * Iterate over all the retrieved values,
1587 * don't try and be clever about changing operators
1588 * just use whatever was set in the attribute map.
1590 for (i = 0; i < self->count; i++) {
1591 vp = pairalloc(dst->da);
1594 pairparsevalue(vp, self->values[i]);
1604 static void xlat_attrsfree(const xlat_attrs_t *expanded)
1606 const VALUE_PAIR_MAP *map;
1607 unsigned int total = 0;
1611 for (map = expanded->maps; map != NULL; map = map->next)
1613 memcpy(&name, &(expanded->attrs[total++]), sizeof(name));
1617 if (map->src->do_xlat) {
1624 static int xlat_attrs(REQUEST *request, const VALUE_PAIR_MAP *maps,
1625 xlat_attrs_t *expanded)
1627 const VALUE_PAIR_MAP *map;
1628 unsigned int total = 0;
1633 for (map = maps; map != NULL; map = map->next)
1635 if (map->src->do_xlat) {
1636 buffer = rad_malloc(MAX_ATTR_STR_LEN);
1637 len = radius_xlat(buffer, MAX_ATTR_STR_LEN,
1638 map->src->name, request, NULL, NULL);
1641 RDEBUG("Expansion of LDAP attribute "
1642 "\"%s\" failed", map->src->name);
1644 expanded->attrs[total] = NULL;
1646 xlat_attrsfree(expanded);
1651 expanded->attrs[total++] = buffer;
1653 expanded->attrs[total++] = map->src->name;
1657 expanded->attrs[total] = NULL;
1658 expanded->maps = maps;
1664 /** Convert attribute map into valuepairs
1666 * Use the attribute map built earlier to convert LDAP values into valuepairs
1667 * and insert them into whichever list they need to go into.
1669 * This is *NOT* atomic, but there's no condition in which we should error
1672 static void do_attrmap(UNUSED ldap_instance *inst, REQUEST *request,
1673 LDAP *handle, const xlat_attrs_t *expanded,
1676 const VALUE_PAIR_MAP *map;
1677 unsigned int total = 0;
1679 rlm_ldap_result_t result;
1682 for (map = expanded->maps; map != NULL; map = map->next)
1684 name = expanded->attrs[total++];
1686 result.values = ldap_get_values(handle, entry, name);
1687 if (!result.values) {
1688 RDEBUG2("Attribute \"%s\" not found in LDAP object",
1695 * Find out how many values there are for the
1696 * attribute and extract all of them.
1698 result.count = ldap_count_values(result.values);
1701 * If something bad happened, just skip, this is probably
1702 * a case of the dst being incorrect for the current
1705 if (radius_map2request(request, map, name, ldap_getvalue,
1712 ldap_value_free(result.values);
1717 static void do_check_reply(ldap_instance *inst, REQUEST *request)
1720 * More warning messages for people who can't be bothered
1721 * to read the documentation.
1723 if (inst->expect_password && (debug_flag > 1)) {
1724 if (!pairfind(request->config_items,PW_CLEARTEXT_PASSWORD, 0) &&
1725 !pairfind(request->config_items,
1726 PW_NT_PASSWORD, 0) &&
1727 !pairfind(request->config_items,
1728 PW_USER_PASSWORD, 0) &&
1729 !pairfind(request->config_items,
1730 PW_PASSWORD_WITH_HEADER, 0) &&
1731 !pairfind(request->config_items,
1732 PW_CRYPT_PASSWORD, 0)) {
1733 RDEBUG("WARNING: No \"known good\" password "
1734 "was found in LDAP. Are you sure that "
1735 "the user is configured correctly?");
1741 static void apply_profile(ldap_instance *inst, REQUEST *request,
1742 LDAP_CONN **pconn, const char *profile,
1743 const xlat_attrs_t *expanded)
1746 LDAPMessage *result, *entry;
1748 LDAP *handle = (*pconn)->handle;
1749 char filter[MAX_FILTER_STR_LEN];
1751 if (!profile || !*profile) return;
1753 strlcpy(filter, inst->base_filter, sizeof(filter));
1755 rcode = perform_search(inst, request, pconn, profile, LDAP_SCOPE_BASE,
1756 filter, expanded->attrs, &result);
1760 RDEBUG("Profile \"%s\" not found", profile);
1765 entry = ldap_first_entry(handle, result);
1767 ldap_get_option(handle, LDAP_OPT_RESULT_CODE,
1769 radlog(L_ERR, "rlm_ldap (%s): Failed retrieving entry: %s",
1771 ldap_err2string(ldap_errno));
1776 do_attrmap(inst, request, handle, expanded, entry);
1779 ldap_msgfree(result);
1783 /******************************************************************************
1785 * Function: ldap_authorize
1787 * Purpose: Check if user is authorized for remote access
1789 ******************************************************************************/
1790 static int ldap_authorize(void *instance, REQUEST * request)
1793 int module_rcode = RLM_MODULE_OK;
1794 ldap_instance *inst = instance;
1799 LDAPMessage *result, *entry;
1801 char filter[MAX_FILTER_STR_LEN];
1802 char basedn[MAX_FILTER_STR_LEN];
1803 xlat_attrs_t expanded; /* faster that mallocing every time */
1805 if (!request->username) {
1806 RDEBUG2("Attribute \"User-Name\" is required for "
1808 return RLM_MODULE_NOOP;
1812 * Check for valid input, zero length names not permitted
1814 if (request->username->length == 0) {
1815 RDEBUG2("Zero length username not permitted");
1816 return RLM_MODULE_INVALID;
1819 if (!radius_xlat(filter, sizeof(filter), inst->filter,
1820 request, ldap_escape_func, NULL)) {
1821 radlog(L_ERR, "rlm_ldap (%s): Failed creating filter",
1823 return RLM_MODULE_INVALID;
1826 if (!radius_xlat(basedn, sizeof(basedn), inst->basedn,
1827 request, ldap_escape_func, NULL)) {
1828 radlog(L_ERR, "rlm_ldap (%s): Failed creating basedn",
1830 return RLM_MODULE_INVALID;
1833 conn = ldap_get_socket(inst);
1834 if (!conn) return RLM_MODULE_FAIL;
1836 if (xlat_attrs(request, inst->user_map, &expanded) < 0) {
1837 return RLM_MODULE_FAIL;
1840 rcode = perform_search(inst, request, &conn, basedn,
1841 LDAP_SCOPE_SUBTREE, filter, expanded.attrs,
1846 module_failure_msg(request,
1847 "rlm_ldap (%s): User object not "
1851 RDEBUG("User object not found", inst->xlat_name);
1853 module_rcode = RLM_MODULE_NOTFOUND;
1860 entry = ldap_first_entry(conn->handle, result);
1862 ldap_get_option(conn->handle, LDAP_OPT_RESULT_CODE,
1864 radlog(L_ERR, "rlm_ldap (%s): Failed retrieving entry: %s",
1866 ldap_err2string(ldap_errno));
1871 user_dn = ldap_get_dn(conn->handle, entry);
1873 ldap_get_option(conn->handle, LDAP_OPT_RESULT_CODE,
1875 radlog(L_ERR, "rlm_ldap (%s): ldap_get_dn() failed: %s",
1877 ldap_err2string(ldap_errno));
1881 RDEBUG2("User found at DN \"%s\"", user_dn);
1883 * Adding attribute containing the Users' DN.
1885 pairadd(&request->config_items,
1886 pairmake("Ldap-UserDn", user_dn, T_OP_EQ));
1890 * We already have a Cleartext-Password. Skip edir.
1892 if (inst->edir && pairfind(request->config_items,
1893 PW_CLEARTEXT_PASSWORD, 0)) {
1898 * Retrieve Universal Password if we use eDirectory
1905 bufsize = sizeof(buffer);
1907 /* retrive universal password */
1908 res = nmasldap_get_password(conn->handle, user_dn,
1911 RDEBUG2("Failed to retrieve eDirectory password. Check your configuration !");
1912 module_rcode = RLM_MODULE_NOOP;
1916 /* add Cleartext-Password attribute to the request */
1917 vp = radius_paircreate(request, &request->config_items,
1918 PW_CLEARTEXT_PASSWORD, 0, PW_TYPE_STRING);
1919 strlcpy(vp->vp_strvalue, buffer, sizeof(vp->vp_strvalue));
1920 vp->length = strlen(vp->vp_strvalue);
1922 RDEBUG2("Added eDirectory password in check items as %s = %s",
1923 vp->name, vp->vp_strvalue);
1929 ldap_memfree(user_dn);
1934 if (inst->access_attr) {
1935 if (check_access(inst, request, conn, entry) < 0) {
1936 module_rcode = RLM_MODULE_USERLOCK;
1942 * Apply ONE user profile, or a default user profile.
1944 vp = pairfind(request->config_items, PW_USER_PROFILE, 0);
1945 if (vp || inst->default_profile) {
1946 char *profile = inst->default_profile;
1948 if (vp) profile = vp->vp_strvalue;
1950 apply_profile(inst, request, &conn, profile, &expanded);
1954 * Apply a SET of user profiles.
1956 if (inst->profile_attr) {
1957 vals = ldap_get_values(conn->handle, entry, inst->profile_attr);
1961 for (i = 0; (vals[i] != NULL) && (*vals[i] != '\0');
1963 apply_profile(inst, request, &conn, vals[i],
1967 ldap_value_free(vals);
1971 if (inst->user_map) {
1972 do_attrmap(inst, request, conn->handle, &expanded, entry);
1973 do_check_reply(inst, request);
1977 xlat_attrsfree(&expanded);
1978 ldap_msgfree(result);
1980 ldap_release_socket(inst, conn);
1982 return module_rcode;
1986 /*****************************************************************************
1988 * Function: ldap_authenticate
1990 * Purpose: Check the user's password against ldap database
1992 *****************************************************************************/
1993 static int ldap_authenticate(void *instance, REQUEST * request)
1996 const char *user_dn;
1997 ldap_instance *inst = instance;
2001 * Ensure that we're being passed a plain-text password, and not
2005 if (!request->username) {
2006 radlog(L_AUTH, "rlm_ldap (%s): Attribute \"User-Name\" is "
2007 "required for authentication", inst->xlat_name);
2008 return RLM_MODULE_INVALID;
2011 if (!request->password) {
2012 radlog(L_AUTH, "rlm_ldap (%s): Attribute \"User-Password\" "
2013 "is required for authentication.", inst->xlat_name);
2014 RDEBUG2(" You have set \"Auth-Type := LDAP\" somewhere.");
2015 RDEBUG2(" *********************************************");
2016 RDEBUG2(" * THAT CONFIGURATION IS WRONG. DELETE IT. ");
2017 RDEBUG2(" * YOU ARE PREVENTING THE SERVER FROM WORKING.");
2018 RDEBUG2(" *********************************************");
2019 return RLM_MODULE_INVALID;
2022 if (request->password->attribute != PW_USER_PASSWORD) {
2023 radlog(L_AUTH, "rlm_ldap (%s): Attribute \"User-Password\" "
2024 "is required for authentication. Cannot use \"%s\".",
2025 inst->xlat_name, request->password->name);
2026 return RLM_MODULE_INVALID;
2029 if (request->password->length == 0) {
2030 module_failure_msg(request,
2031 "rlm_ldap (%s): Empty password supplied",
2033 return RLM_MODULE_INVALID;
2036 conn = ldap_get_socket(inst);
2037 if (!conn) return RLM_MODULE_FAIL;
2039 RDEBUG("Login attempt by \"%s\" with password \"%s\"",
2040 request->username->vp_strvalue, request->password->vp_strvalue);
2043 * Get the DN by doing a search.
2045 user_dn = get_userdn(&conn, request, &module_rcode);
2047 ldap_release_socket(inst, conn);
2048 return module_rcode;
2054 conn->rebound = TRUE;
2055 module_rcode = ldap_bind_wrapper(&conn, user_dn,
2056 request->password->vp_strvalue,
2058 if (module_rcode == RLM_MODULE_OK) {
2059 RDEBUG("Bind as user \"%s\" was successful", user_dn);
2062 ldap_release_socket(inst, conn);
2063 return module_rcode;
2068 /*****************************************************************************
2070 * Function: ldap_postauth
2072 * Purpose: Check the user's password against ldap database
2074 *****************************************************************************/
2075 static int ldap_postauth(void *instance, REQUEST * request)
2078 const char *user_dn;
2079 ldap_instance *inst = instance;
2084 * Ensure that we have a username and a
2085 * Cleartext-Password in the request
2087 if (!request->username) {
2088 radlog(L_AUTH, "rlm_ldap (%s): Attribute \"User-Name\" is "
2089 "required for authentication", inst->xlat_name);
2090 return RLM_MODULE_INVALID;
2093 vp = pairfind(request->config_items, PW_CLEARTEXT_PASSWORD, 0);
2095 radlog(L_AUTH, "rlm_ldap (%s): Attribute \"Cleartext-Password\" "
2096 "is required for authentication.", inst->xlat_name);
2097 return RLM_MODULE_INVALID;
2100 if (!*vp->vp_strvalue) {
2101 radlog(L_AUTH, "rlm_ldap (%s): Attribute \"Cleartext-Password\" "
2102 "is empty.", inst->xlat_name);
2103 return RLM_MODULE_INVALID;
2106 conn = ldap_get_socket(inst);
2107 if (!conn) return RLM_MODULE_FAIL;
2109 RDEBUG("Login attempt by \"%s\" with password \"%s\"",
2110 request->username->vp_strvalue, vp->vp_strvalue);
2113 * Get the DN by doing a search.
2115 user_dn = get_userdn(&conn, request, &module_rcode);
2117 ldap_release_socket(inst, conn);
2118 return module_rcode;
2124 conn->rebound = TRUE;
2125 module_rcode = ldap_bind_wrapper(&conn, user_dn,
2128 if (module_rcode == RLM_MODULE_OK) {
2129 RDEBUG("Bind as user \"%s\" was successful", user_dn);
2132 ldap_release_socket(inst, conn);
2133 return module_rcode;
2137 /* globally exported name */
2138 module_t rlm_ldap = {
2141 RLM_TYPE_THREAD_SAFE, /* type: reserved */
2142 ldap_instantiate, /* instantiation */
2143 ldap_detach, /* detach */
2145 ldap_authenticate, /* authentication */
2146 ldap_authorize, /* authorization */
2147 NULL, /* preaccounting */
2148 NULL, /* accounting */
2149 NULL, /* checksimul */
2150 NULL, /* pre-proxy */
2151 NULL, /* post-proxy */
2153 ldap_postauth /* post-auth */
2155 NULL /* post-auth */