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>
21 * Copyright 2012 Arran Cudbard-Bell <a.cudbardb@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
42 extern int nmasldap_get_password(LDAP *ld,char *objectDN, char *pwd, size_t *pwdSize);
46 typedef struct ldap_acct_section {
49 const char *reference;
50 } ldap_acct_section_t;
55 fr_connection_pool_t *pool;
69 int ldap_debug; /* Debug flag for LDAP SDK */
71 const char *xlat_name; /* name used to xlat */
76 * RADIUS attribute to LDAP attribute maps
78 VALUE_PAIR_MAP *user_map; /* Applied to users and profiles */
81 * Access related configuration
84 int positive_access_attr;
90 char *default_profile;
97 char *groupmemb_filter;
103 ldap_acct_section_t *postauth;
104 ldap_acct_section_t *accounting;
107 * TLS items. We should really normalize these with the
112 char *tls_cacertfile;
117 char *tls_require_cert;
137 #ifdef LDAP_OPT_X_KEEPALIVE_IDLE
140 #ifdef LDAP_OPT_X_KEEPALIVE_PROBES
141 int keepalive_probes;
143 #ifdef LDAP_OPT_ERROR_NUMBER
144 int keepalive_interval;
149 /* The default setting for TLS Certificate Verification */
150 #define TLS_DEFAULT_VERIFY "allow"
155 static CONF_PARSER tls_config[] = {
156 {"start_tls", PW_TYPE_BOOLEAN,
157 offsetof(ldap_instance,start_tls), NULL, "no"},
158 {"cacertfile", PW_TYPE_FILENAME,
159 offsetof(ldap_instance,tls_cacertfile), NULL, NULL},
160 {"cacertdir", PW_TYPE_FILENAME,
161 offsetof(ldap_instance,tls_cacertdir), NULL, NULL},
162 {"certfile", PW_TYPE_FILENAME,
163 offsetof(ldap_instance,tls_certfile), NULL, NULL},
164 {"keyfile", PW_TYPE_FILENAME,
165 offsetof(ldap_instance,tls_keyfile), NULL, NULL},
166 {"randfile", PW_TYPE_STRING_PTR, /* OK if it changes on HUP */
167 offsetof(ldap_instance,tls_randfile), NULL, NULL},
168 {"require_cert", PW_TYPE_STRING_PTR,
169 offsetof(ldap_instance,tls_require_cert), NULL, TLS_DEFAULT_VERIFY},
170 { NULL, -1, 0, NULL, NULL }
174 static CONF_PARSER attr_config[] = {
178 /* LDAP attribute name that controls remote access */
179 {"access_attr", PW_TYPE_STRING_PTR,
180 offsetof(ldap_instance,access_attr), NULL, NULL},
181 {"positive_access_attr", PW_TYPE_BOOLEAN,
182 offsetof(ldap_instance,positive_access_attr), NULL, "yes"},
184 {"base_filter", PW_TYPE_STRING_PTR,
185 offsetof(ldap_instance,base_filter), NULL,
186 "(objectclass=radiusprofile)"},
187 {"default_profile", PW_TYPE_STRING_PTR,
188 offsetof(ldap_instance,default_profile), NULL, NULL},
189 {"profile_attribute", PW_TYPE_STRING_PTR,
190 offsetof(ldap_instance,profile_attr), NULL, NULL},
192 { NULL, -1, 0, NULL, NULL }
197 * Group configuration
199 static CONF_PARSER group_config[] = {
201 * Group checks. These could probably be done
202 * via dynamic xlat's.
204 {"name_attribute", PW_TYPE_STRING_PTR,
205 offsetof(ldap_instance,groupname_attr), NULL, "cn"},
206 {"membership_filter", PW_TYPE_STRING_PTR,
207 offsetof(ldap_instance,groupmemb_filter), NULL,
208 "(|(&(objectClass=GroupOfNames)(member=%{Ldap-UserDn}))"
209 "(&(objectClass=GroupOfUniqueNames)(uniquemember=%{Ldap-UserDn})))"},
210 {"membership_attribute", PW_TYPE_STRING_PTR,
211 offsetof(ldap_instance,groupmemb_attr), NULL, NULL},
214 { NULL, -1, 0, NULL, NULL }
218 * Reference for accounting updates
220 static const CONF_PARSER acct_section_config[] = {
221 {"reference", PW_TYPE_STRING_PTR,
222 offsetof(ldap_acct_section_t, reference), NULL, "."},
223 {NULL, -1, 0, NULL, NULL}
227 * Various options that don't belong in the main configuration.
229 * Note that these overlap a bit with the connection pool code!
231 static CONF_PARSER option_config[] = {
233 * Debugging flags to the server
235 {"ldap_debug", PW_TYPE_INTEGER,
236 offsetof(ldap_instance,ldap_debug), NULL, "0x0000"},
238 {"chase_referrals", PW_TYPE_BOOLEAN,
239 offsetof(ldap_instance,chase_referrals), NULL, NULL},
241 {"rebind", PW_TYPE_BOOLEAN,
242 offsetof(ldap_instance,rebind), NULL, NULL},
244 /* timeout on network activity */
245 {"net_timeout", PW_TYPE_INTEGER,
246 offsetof(ldap_instance,net_timeout), NULL, "10"},
248 /* timeout for search results */
249 {"timeout", PW_TYPE_INTEGER,
250 offsetof(ldap_instance,timeout), NULL, "20"},
252 /* allow server unlimited time for search (server-side limit) */
253 {"timelimit", PW_TYPE_INTEGER,
254 offsetof(ldap_instance,timelimit), NULL, "20"},
256 #ifdef LDAP_OPT_X_KEEPALIVE_IDLE
257 {"idle", PW_TYPE_INTEGER,
258 offsetof(ldap_instance,keepalive_idle), NULL, "60"},
260 #ifdef LDAP_OPT_X_KEEPALIVE_PROBES
261 {"probes", PW_TYPE_INTEGER,
262 offsetof(ldap_instance,keepalive_probes), NULL, "3"},
264 #ifdef LDAP_OPT_ERROR_NUMBER
265 {"interval", PW_TYPE_INTEGER,
266 offsetof(ldap_instance,keepalive_interval), NULL, "30"},
268 { NULL, -1, 0, NULL, NULL }
272 static const CONF_PARSER module_config[] = {
273 {"server", PW_TYPE_STRING_PTR,
274 offsetof(ldap_instance,server), NULL, "localhost"},
275 {"port", PW_TYPE_INTEGER,
276 offsetof(ldap_instance,port), NULL, "389"},
278 {"password", PW_TYPE_STRING_PTR,
279 offsetof(ldap_instance,password), NULL, ""},
280 {"identity", PW_TYPE_STRING_PTR,
281 offsetof(ldap_instance,login), NULL, ""},
286 {"basedn", PW_TYPE_STRING_PTR,
287 offsetof(ldap_instance,basedn), NULL, "o=notexist"},
289 {"filter", PW_TYPE_STRING_PTR,
290 offsetof(ldap_instance,filter), NULL, "(uid=%u)"},
292 /* turn off the annoying warning if we don't expect a password */
293 {"expect_password", PW_TYPE_BOOLEAN,
294 offsetof(ldap_instance,expect_password), NULL, "yes"},
297 /* support for eDirectory Universal Password */
298 {"edir", PW_TYPE_BOOLEAN,
299 offsetof(ldap_instance,edir), NULL, NULL}, /* NULL defaults to "no" */
302 * Attempt to bind with the Cleartext password we got from eDirectory
303 * Universal password for additional authorization checks.
305 {"edir_autz", PW_TYPE_BOOLEAN,
306 offsetof(ldap_instance,edir_autz), NULL, NULL}, /* NULL defaults to "no" */
310 * Terrible things which should be deleted.
312 { "profiles", PW_TYPE_SUBSECTION, 0, NULL, (const void *) attr_config },
314 { "group", PW_TYPE_SUBSECTION, 0, NULL, (const void *) group_config },
316 { "options", PW_TYPE_SUBSECTION, 0, NULL,
317 (const void *) option_config },
319 { "tls", PW_TYPE_SUBSECTION, 0, NULL, (const void *) tls_config },
321 {NULL, -1, 0, NULL, NULL}
324 typedef struct ldap_conn {
331 typedef struct xlat_attrs {
332 const VALUE_PAIR_MAP *maps;
333 const char *attrs[MAX_ATTRMAP];
336 typedef struct rlm_ldap_result {
341 #define LDAP_PROC_SUCCESS 0
342 #define LDAP_PROC_ERROR -1
343 #define LDAP_PROC_RETRY -2
344 #define LDAP_PROC_REJECT -3
346 static int process_ldap_errno(ldap_instance *inst, LDAP_CONN **pconn,
347 const char *operation)
351 ldap_get_option((*pconn)->handle, LDAP_OPT_ERROR_NUMBER,
353 switch (ldap_errno) {
355 case LDAP_NO_SUCH_OBJECT:
356 return LDAP_PROC_SUCCESS;
358 case LDAP_SERVER_DOWN:
360 *pconn = fr_connection_reconnect(inst->pool, *pconn);
361 if (!*pconn) return -1;
362 return LDAP_PROC_RETRY;
364 case LDAP_INSUFFICIENT_ACCESS:
365 radlog(L_ERR, "rlm_ldap (%s): %s failed: Insufficient access. "
366 "Check the identity and password configuration "
367 "directives", inst->xlat_name, operation);
368 return LDAP_PROC_ERROR;
371 exec_trigger(NULL, inst->cs, "modules.ldap.timeout", TRUE);
372 radlog(L_ERR, "rlm_ldap (%s): %s failed: Timed out "
373 "while waiting for server to respond", inst->xlat_name,
375 return LDAP_PROC_ERROR;
377 case LDAP_FILTER_ERROR:
378 radlog(L_ERR, "rlm_ldap (%s): %s failed: Bad search "
379 "filter", inst->xlat_name, operation);
380 return LDAP_PROC_ERROR;
382 case LDAP_TIMELIMIT_EXCEEDED:
383 exec_trigger(NULL, inst->cs, "modules.ldap.timeout", TRUE);
386 case LDAP_UNAVAILABLE:
388 * Reconnect. There's an issue with the socket
391 radlog(L_ERR, "rlm_ldap (%s): %s failed: %s",
392 inst->xlat_name, operation, ldap_err2string(ldap_errno));
395 case LDAP_INVALID_CREDENTIALS:
396 case LDAP_CONSTRAINT_VIOLATION:
397 return LDAP_PROC_REJECT;
400 radlog(L_ERR, "rlm_ldap (%s): %s failed: %s",
401 inst->xlat_name, operation, ldap_err2string(ldap_errno));
402 return LDAP_PROC_ERROR;
407 static int ldap_bind_wrapper(LDAP_CONN **pconn, const char *user,
408 const char *password, int retry)
411 int module_rcode = RLM_MODULE_OK;
412 LDAP_CONN *conn = *pconn;
413 ldap_instance *inst = conn->inst;
414 LDAPMessage *result = NULL;
418 msg_id = ldap_bind(conn->handle, user, password, LDAP_AUTH_SIMPLE);
419 if (msg_id < 0) goto get_error;
421 DEBUG3("rlm_ldap (%s): Waiting for bind result...", inst->xlat_name);
423 tv.tv_sec = inst->timeout;
426 rcode = ldap_result(conn->handle, msg_id, 1, &tv, &result);
427 ldap_msgfree(result);
430 switch (process_ldap_errno(inst, &conn, "Bind"))
432 case LDAP_PROC_SUCCESS:
434 case LDAP_PROC_REJECT:
435 module_rcode = RLM_MODULE_REJECT;
437 case LDAP_PROC_ERROR:
438 module_rcode = RLM_MODULE_FAIL;
440 #ifdef HAVE_LDAP_INITIALIZE
442 radlog(L_ERR, "rlm_ldap (%s): bind "
443 "with %s to %s failed",
444 inst->xlat_name, user,
449 radlog(L_ERR, "rlm_ldap (%s): bind "
450 "with %s to %s:%d failed",
451 inst->xlat_name, user,
452 inst->server, inst->port);
456 case LDAP_PROC_RETRY:
457 if (retry) goto bind;
459 module_rcode = RLM_MODULE_FAIL;
466 return module_rcode; /* caller closes the connection */
469 #if LDAP_SET_REBIND_PROC_ARGS == 3
471 * Rebind && chase referral stuff
473 static int ldap_rebind(LDAP *handle, LDAP_CONST char *url,
474 UNUSED ber_tag_t request, UNUSED ber_int_t msgid,
477 int rcode, ldap_errno;
478 LDAP_CONN *conn = ctx;
480 conn->referred = TRUE;
481 conn->rebound = TRUE; /* not really, but oh well... */
482 rad_assert(handle == conn->handle);
484 DEBUG("rlm_ldap (%s): Rebinding to URL %s", conn->inst->xlat_name, url);
487 rcode = ldap_bind_wrapper(&conn, conn->inst->login,
488 conn->inst->password, FALSE);
490 if (rcode == RLM_MODULE_OK) {
494 ldap_get_option(handle, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
500 /** Create and return a new connection
501 * This function is probably too big.
503 static void *ldap_conn_create(void *ctx)
506 int ldap_errno, ldap_version;
508 ldap_instance *inst = ctx;
510 LDAP_CONN *conn = NULL;
512 #ifdef HAVE_LDAP_INITIALIZE
514 DEBUG("rlm_ldap (%s): Connecting to %s", inst->xlat_name,
517 ldap_errno = ldap_initialize(&handle, inst->server);
518 if (ldap_errno != LDAP_SUCCESS) {
519 radlog(L_ERR, "rlm_ldap (%s): ldap_initialize() "
521 inst->xlat_name, ldap_err2string(ldap_errno));
527 DEBUG("rlm_ldap (%s): Connecting to %s:%d", inst->xlat_name,
528 inst->server, inst->port);
530 handle = ldap_init(inst->server, inst->port);
532 radlog(L_ERR, "rlm_ldap (%s): ldap_init() failed",
535 if (handle) ldap_unbind_s(handle);
541 * We now have a connection structure, but no actual TCP connection.
543 * Set a bunch of LDAP options, using common code.
545 #define do_ldap_option(_option, _name, _value) \
546 if (ldap_set_option(handle, _option, _value) != LDAP_OPT_SUCCESS) { \
547 ldap_get_option(handle, LDAP_OPT_ERROR_NUMBER, &ldap_errno); \
548 radlog(L_ERR, "rlm_ldap (%s): Could not set %s: %s", \
549 inst->xlat_name, _name, ldap_err2string(ldap_errno)); \
552 if (inst->ldap_debug) {
553 do_ldap_option(LDAP_OPT_DEBUG_LEVEL, "ldap_debug",
554 &(inst->ldap_debug));
558 * Leave "chase_referrals" unset to use the OpenLDAP
561 if (inst->chase_referrals != 2) {
562 if (inst->chase_referrals) {
563 do_ldap_option(LDAP_OPT_REFERRALS, "chase_referrals",
566 #if LDAP_SET_REBIND_PROC_ARGS == 3
567 if (inst->rebind == 1) {
568 ldap_set_rebind_proc(handle, ldap_rebind, inst);
572 do_ldap_option(LDAP_OPT_REFERRALS, "chase_referrals",
577 tv.tv_sec = inst->net_timeout;
579 do_ldap_option(LDAP_OPT_NETWORK_TIMEOUT, "net_timeout", &tv);
581 do_ldap_option(LDAP_OPT_TIMELIMIT, "timelimit", &(inst->timelimit));
583 ldap_version = LDAP_VERSION3;
584 do_ldap_option(LDAP_OPT_PROTOCOL_VERSION, "ldap_version",
587 #ifdef LDAP_OPT_X_KEEPALIVE_IDLE
588 do_ldap_option(LDAP_OPT_X_KEEPALIVE_IDLE, "keepalive idle",
589 &(inst->keepalive_idle));
592 #ifdef LDAP_OPT_X_KEEPALIVE_PROBES
593 do_ldap_option(LDAP_OPT_X_KEEPALIVE_PROBES, "keepalive probes",
594 &(inst->keepalive_probes));
597 #ifdef LDAP_OPT_X_KEEPALIVE_INTERVAL
598 do_ldap_option(LDAP_OPT_X_KEEPALIVE_INTERVAL, "keepalive interval",
599 &(inst->keepalive_interval));
602 #ifdef HAVE_LDAP_START_TLS
604 * Set all of the TLS options
606 if (inst->tls_mode) {
607 do_ldap_option(LDAP_OPT_X_TLS, "tls_mode", &(inst->tls_mode));
610 #define maybe_ldap_option(_option, _name, _value) \
611 if (_value) do_ldap_option(_option, _name, _value)
613 maybe_ldap_option(LDAP_OPT_X_TLS_CACERTFILE,
614 "cacertfile", inst->tls_cacertfile);
615 maybe_ldap_option(LDAP_OPT_X_TLS_CACERTDIR,
616 "cacertdir", inst->tls_cacertdir);
618 #ifdef HAVE_LDAP_INT_TLS_CONFIG
619 if (ldap_int_tls_config(NULL, LDAP_OPT_X_TLS_REQUIRE_CERT,
620 (inst->tls_require_cert)) != LDAP_OPT_SUCCESS) {
621 ldap_get_option(handle, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
622 radlog(L_ERR, "rlm_ldap (%s): could not set "
623 "LDAP_OPT_X_TLS_REQUIRE_CERT option to %s: %s",
625 inst->tls_require_cert,
626 ldap_err2string(ldap_errno));
630 maybe_ldap_option(LDAP_OPT_X_TLS_CERTFILE,
631 "certfile", inst->tls_certfile);
632 maybe_ldap_option(LDAP_OPT_X_TLS_KEYFILE,
633 "keyfile", inst->tls_keyfile);
634 maybe_ldap_option(LDAP_OPT_X_TLS_RANDOM_FILE,
635 "randfile", inst->tls_randfile);
638 * And finally start the TLS code.
640 if (inst->start_tls && (inst->port != 636)) {
641 ldap_errno = ldap_start_tls_s(handle, NULL, NULL);
642 if (ldap_errno != LDAP_SUCCESS) {
643 ldap_get_option(handle, LDAP_OPT_ERROR_NUMBER,
645 radlog(L_ERR, "rlm_ldap (%s): could not start TLS: %s",
647 ldap_err2string(ldap_errno));
651 #endif /* HAVE_LDAP_START_TLS */
653 conn = rad_malloc(sizeof(*conn));
655 conn->handle = handle;
656 conn->rebound = FALSE;
657 conn->referred = FALSE;
659 module_rcode = ldap_bind_wrapper(&conn, inst->login, inst->password,
661 if (module_rcode != RLM_MODULE_OK) {
669 /** Close and delete a connection
672 static int ldap_conn_delete(UNUSED void *ctx, void *connection)
674 LDAP_CONN *conn = connection;
676 ldap_unbind_s(conn->handle);
683 /** Gets an LDAP socket from the connection pool
686 static LDAP_CONN *ldap_get_socket(ldap_instance *inst)
690 conn = fr_connection_get(inst->pool);
692 radlog(L_ERR, "rlm_ldap (%s): all ldap connections are in use",
700 /** Frees an LDAP socket back to the connection pool
703 static void ldap_release_socket(ldap_instance *inst, LDAP_CONN *conn)
706 * Could have already been free'd due to a previous error.
711 * We chased a referral to another server.
713 * This connection is no longer part of the pool which is
714 * connected to and bound to the configured server.
717 * Note that we do NOT close it if it was bound to
718 * another user. Instead, we let the next caller do the
721 if (conn->referred) {
722 fr_connection_del(inst->pool, conn);
726 fr_connection_release(inst->pool, conn);
731 /* Converts "bad" strings into ones which are safe for LDAP
734 static size_t ldap_escape_func(UNUSED REQUEST *request, char *out,
735 size_t outlen, const char *in, UNUSED void *arg)
741 * Encode unsafe characters.
744 ((in[0] == ' ') || (in[0] == '#'))) ||
745 (strchr(",+\"\\<>;*=()", *in))) {
746 static const char hex[] = "0123456789abcdef";
749 * Only 3 or less bytes available.
756 *(out++) = hex[((*in) >> 4) & 0x0f];
757 *(out++) = hex[(*in) & 0x0f];
765 * Only one byte left.
782 /** Do a search and get a response
785 static int perform_search(ldap_instance *inst, REQUEST *request,
786 LDAP_CONN **pconn, const char *search_basedn,
787 int scope, const char *filter,
788 const char * const *attrs, LDAPMessage **presult)
792 LDAP_CONN *conn = *pconn;
796 * OpenLDAP library doesn't declare attrs array as const, but
797 * it really should be *sigh*.
800 memcpy(&search_attrs, &attrs, sizeof(attrs));
805 * Do all searches as the default admin user.
808 ldap_errno = ldap_bind_wrapper(pconn, inst->login,
809 inst->password, TRUE);
810 if (ldap_errno != RLM_MODULE_OK) {
814 rad_assert(*pconn != NULL);
816 conn->rebound = FALSE;
819 tv.tv_sec = inst->timeout;
821 RDEBUG2("Performing search in '%s' with filter '%s'",
822 search_basedn ? search_basedn : "(null)" ,
826 ldap_errno = ldap_search_ext_s(conn->handle, search_basedn, scope,
827 filter, search_attrs, 0, NULL, NULL,
829 if (ldap_errno != LDAP_SUCCESS) {
830 ldap_msgfree(*presult);
831 switch (process_ldap_errno(inst, pconn, "Search"))
833 case LDAP_PROC_SUCCESS:
835 case LDAP_PROC_REJECT:
836 case LDAP_PROC_ERROR:
838 case LDAP_PROC_RETRY:
846 count = ldap_count_entries(conn->handle, *presult);
848 ldap_msgfree(*presult);
849 RDEBUG("Search returned no results");
855 ldap_msgfree(*presult);
856 RDEBUG("Got ambiguous search result (%d results)", count);
864 /** Expand an LDAP URL into a query, and return a string result from that query.
867 static size_t ldap_xlat(void *instance, REQUEST *request, const char *fmt,
868 char *out, size_t freespace)
872 ldap_instance *inst = instance;
873 LDAPURLDesc *ldap_url;
874 LDAPMessage *result = NULL;
875 LDAPMessage *entry = NULL;
881 char buffer[MAX_FILTER_STR_LEN];
883 if (strchr(fmt, '%') != NULL) {
884 if (!radius_xlat(buffer, sizeof(buffer), fmt, request,
885 ldap_escape_func, NULL)) {
887 "rlm_ldap (%s): Unable to create LDAP URL",
896 if (!ldap_is_ldap_url(url)) {
897 radlog(L_ERR, "rlm_ldap (%s): String passed does not look "
898 "like an LDAP URL", inst->xlat_name);
902 if (ldap_url_parse(url, &ldap_url)){
903 radlog(L_ERR, "rlm_ldap (%s): Parsing LDAP URL failed",
909 * Nothing, empty string, "*" string, or got 2 things, die.
911 if (!ldap_url->lud_attrs || !ldap_url->lud_attrs[0] ||
912 !*ldap_url->lud_attrs[0] ||
913 (strcmp(ldap_url->lud_attrs[0], "*") == 0) ||
914 ldap_url->lud_attrs[1]) {
915 radlog(L_ERR, "rlm_ldap (%s): Bad attributes list in LDAP "
916 "URL. URL must specify exactly one attribute to "
923 if (ldap_url->lud_host &&
924 ((strncmp(inst->server, ldap_url->lud_host,
925 strlen(inst->server)) != 0) ||
926 (ldap_url->lud_port != inst->port))) {
927 RDEBUG("Requested server/port is \"%s:%i\"", ldap_url->lud_host,
933 conn = ldap_get_socket(inst);
934 if (!conn) goto free_urldesc;
936 memcpy(&attrs, &ldap_url->lud_attrs, sizeof(attrs));
938 rcode = perform_search(inst, request, &conn, ldap_url->lud_dn,
939 ldap_url->lud_scope, ldap_url->lud_filter, attrs,
943 RDEBUG("Search returned not found", inst->xlat_name);
950 entry = ldap_first_entry(conn->handle, result);
952 ldap_get_option(conn->handle, LDAP_OPT_RESULT_CODE,
954 radlog(L_ERR, "rlm_ldap (%s): Failed retrieving entry: %s",
956 ldap_err2string(ldap_errno));
960 vals = ldap_get_values(conn->handle, entry, ldap_url->lud_attrs[0]);
962 RDEBUG("No \"%s\" attributes found in specified object",
963 inst->xlat_name, ldap_url->lud_attrs[0]);
967 length = strlen(vals[0]);
968 if (length >= freespace){
973 strlcpy(out, vals[0], freespace);
976 ldap_value_free(vals);
978 ldap_msgfree(result);
980 ldap_release_socket(inst, conn);
982 ldap_free_urldesc(ldap_url);
988 static char *get_userdn(LDAP_CONN **pconn, REQUEST *request, int *module_rcode)
992 ldap_instance *inst = (*pconn)->inst;
993 LDAPMessage *result, *entry;
995 static char firstattr[] = "uid";
997 const char *attrs[] = {firstattr, NULL};
998 char filter[MAX_FILTER_STR_LEN];
999 char basedn[MAX_FILTER_STR_LEN];
1001 *module_rcode = RLM_MODULE_FAIL;
1003 vp = pairfind(request->config_items, PW_LDAP_USERDN, 0);
1005 *module_rcode = RLM_MODULE_OK;
1006 return vp->vp_strvalue;
1009 if (!radius_xlat(filter, sizeof(filter), inst->filter,
1010 request, ldap_escape_func, NULL)) {
1011 radlog(L_ERR, "rlm_ldap (%s): Unable to create filter",
1013 *module_rcode = RLM_MODULE_INVALID;
1017 if (!radius_xlat(basedn, sizeof(basedn), inst->basedn,
1018 request, ldap_escape_func, NULL)) {
1019 radlog(L_ERR, "rlm_ldap (%s): Unable to create basedn",
1021 *module_rcode = RLM_MODULE_INVALID;
1025 rcode = perform_search(inst, request, pconn, basedn, LDAP_SCOPE_SUBTREE,
1026 filter, attrs, &result);
1029 *module_rcode = RLM_MODULE_NOTFOUND;
1035 if ((entry = ldap_first_entry((*pconn)->handle, result)) == NULL) {
1036 ldap_get_option((*pconn)->handle, LDAP_OPT_RESULT_CODE,
1038 radlog(L_ERR, "rlm_ldap (%s): Failed retrieving entry: %s",
1040 ldap_err2string(ldap_errno));
1041 ldap_msgfree(result);
1045 if ((user_dn = ldap_get_dn((*pconn)->handle, entry)) == NULL) {
1046 ldap_get_option((*pconn)->handle, LDAP_OPT_RESULT_CODE,
1048 radlog(L_ERR, "rlm_ldap (%s): ldap_get_dn() failed: %s",
1050 ldap_err2string(ldap_errno));
1052 ldap_msgfree(result);
1056 vp = pairmake("LDAP-UserDn", user_dn, T_OP_EQ);
1058 ldap_memfree(user_dn);
1059 ldap_msgfree(result);
1063 *module_rcode = RLM_MODULE_OK;
1065 pairadd(&request->config_items, vp);
1066 ldap_memfree(user_dn);
1067 ldap_msgfree(result);
1069 return vp->vp_strvalue;
1073 /** Perform LDAP-Group comparison checking
1076 static int ldap_groupcmp(void *instance, REQUEST *request,
1077 UNUSED VALUE_PAIR *thing, VALUE_PAIR *check,
1078 UNUSED VALUE_PAIR *check_pairs,
1079 UNUSED VALUE_PAIR **reply_pairs)
1081 ldap_instance *inst = instance;
1082 int i, rcode, found, module_rcode;
1083 LDAPMessage *result = NULL;
1084 LDAPMessage *entry = NULL;
1086 int check_is_dn = FALSE, value_is_dn = FALSE;
1087 static char firstattr[] = "dn";
1088 const char *attrs[] = {firstattr, NULL};
1090 const char *group_attrs[] = {inst->groupmemb_attr, NULL};
1094 char gr_filter[MAX_FILTER_STR_LEN];
1095 char filter[MAX_FILTER_STR_LEN];
1096 char basedn[MAX_FILTER_STR_LEN];
1098 RDEBUG("Searching for user in group \"%s\"", check->vp_strvalue);
1100 if (check->length == 0) {
1101 RDEBUG("Cannot do comparison (group name is empty)");
1105 conn = ldap_get_socket(inst);
1106 if (!conn) return 1;
1109 * This is used in the default membership filter.
1111 user_dn = get_userdn(&conn, request, &module_rcode);
1113 ldap_release_socket(inst, conn);
1117 if (!inst->groupmemb_filter) goto check_attr;
1119 if (!radius_xlat(gr_filter, sizeof(gr_filter),
1120 inst->groupmemb_filter, request, ldap_escape_func,
1122 radlog(L_ERR, "rlm_ldap (%s): Failed creating group filter",
1128 * If it's a DN, use that.
1130 check_is_dn = strchr(check->vp_strvalue,',') == NULL ? FALSE : TRUE;
1133 strlcpy(filter, gr_filter, sizeof(filter));
1134 strlcpy(basedn, check->vp_strvalue, sizeof(basedn));
1136 snprintf(filter, sizeof(filter), "(&(%s=%s)%s)",
1137 inst->groupname_attr,
1138 check->vp_strvalue, gr_filter);
1141 * get_userdn does this, too. Oh well.
1143 if (!radius_xlat(basedn, sizeof(basedn), inst->basedn,
1144 request, ldap_escape_func, NULL)) {
1145 radlog(L_ERR, "rlm_ldap (%s): Failed creating basedn",
1151 rcode = perform_search(inst, request, &conn, basedn, LDAP_SCOPE_SUBTREE,
1152 filter, attrs, &result);
1154 ldap_release_socket(inst, conn);
1155 ldap_msgfree(result);
1157 RDEBUG("User found in group object");
1163 ldap_release_socket(inst, conn);
1167 /* else the search returned -2, for "not found" */
1170 * Else the search returned NOTFOUND. See if we're
1171 * configured to search for group membership using user
1174 if (!inst->groupmemb_attr) {
1175 ldap_release_socket(inst, conn);
1176 RDEBUG("Group object \"%s\" not found, or user is not a member",
1177 check->vp_strvalue);
1182 RDEBUG2("Checking user object membership (%s) attributes",
1183 inst->groupmemb_attr);
1185 snprintf(filter ,sizeof(filter), "(objectclass=*)");
1187 rcode = perform_search(inst, request, &conn, user_dn, LDAP_SCOPE_BASE,
1188 filter, group_attrs, &result);
1191 RDEBUG("Can't check membership attributes, user object "
1194 ldap_release_socket(inst, conn);
1198 entry = ldap_first_entry(conn->handle, result);
1200 ldap_get_option(conn->handle, LDAP_OPT_RESULT_CODE,
1202 radlog(L_ERR, "rlm_ldap (%s): Failed retrieving entry: %s",
1204 ldap_err2string(ldap_errno));
1206 ldap_release_socket(inst, conn);
1207 ldap_msgfree(result);
1211 vals = ldap_get_values(conn->handle, entry, inst->groupmemb_attr);
1213 RDEBUG("No group membership attribute(s) found in user object");
1214 ldap_release_socket(inst, conn);
1215 ldap_msgfree(result);
1220 * Loop over the list of groups the user is a member of,
1221 * looking for a match.
1224 for (i = 0; i < ldap_count_values(vals); i++) {
1225 LDAPMessage *gr_result = NULL;
1227 value_is_dn = strchr(vals[i], ',') == NULL ? FALSE : TRUE;
1229 RDEBUG2("Processing group membership value \"%s\"", vals[i]);
1232 * Both literal group names, do case sensitive comparison
1234 if (!check_is_dn && !value_is_dn) {
1235 if (strcmp(vals[i], check->vp_strvalue) == 0){
1236 RDEBUG("User found (membership value matches "
1247 * Both DNs, do case insensitive comparison
1249 if (check_is_dn && value_is_dn) {
1250 if (strcasecmp(vals[i], check->vp_strvalue) == 0){
1251 RDEBUG("User found (membership DN matches "
1262 * If the value is not a DN, or the check item is a DN
1263 * there's nothing more we can do.
1265 if (!value_is_dn && check_is_dn) continue;
1268 * We have a value which is a DN, and a check item which
1269 * specifies the name of a group, search using the value
1270 * DN for the group, and see if it has a groupname_attr
1271 * which matches our check val.
1273 RDEBUG2("Searching with membership DN and group name");
1275 snprintf(filter,sizeof(filter), "(%s=%s)",
1276 inst->groupname_attr, check->vp_strvalue);
1278 rcode = perform_search(inst, request, &conn, vals[i],
1279 LDAP_SCOPE_BASE, filter, attrs,
1282 ldap_msgfree(gr_result);
1284 /* Error occurred */
1286 ldap_value_free(vals);
1287 ldap_msgfree(result);
1288 ldap_release_socket(inst, conn);
1293 * Either the group DN wasn't found, or it didn't have the
1294 * correct name. Continue looping over the attributes.
1297 ldap_msgfree(gr_result);
1303 RDEBUG("User found (group name in membership DN matches check "
1309 ldap_value_free(vals);
1310 ldap_msgfree(result);
1311 ldap_release_socket(inst, conn);
1314 RDEBUG("User is not a member of specified group");
1321 /** Parse update section
1323 * Verify that the ldap update section makes sense, and add attribute names
1324 * to array of attributes for efficient querying later.
1326 static int build_attrmap(CONF_SECTION *cs, VALUE_PAIR_MAP **head)
1328 const char *cs_list, *p;
1330 request_refs_t request_def = REQUEST_CURRENT;
1331 pair_lists_t list_def = PAIR_LIST_REQUEST;
1333 CONF_ITEM *ci = cf_sectiontoitem(cs);
1336 unsigned int total = 0;
1337 VALUE_PAIR_MAP **tail, *map;
1343 cs_list = p = cf_section_name2(cs);
1345 request_def = radius_request_name(&p, REQUEST_UNKNOWN);
1346 if (request_def == REQUEST_UNKNOWN) {
1347 cf_log_err(ci, "rlm_ldap: Default request specified "
1348 "in mapping section is invalid");
1352 list_def = fr_str2int(pair_lists, p, PAIR_LIST_UNKNOWN);
1353 if (list_def == PAIR_LIST_UNKNOWN) {
1354 cf_log_err(ci, "rlm_ldap: Default list specified "
1355 "in mapping section is invalid");
1360 for (ci = cf_item_find_next(cs, NULL);
1362 ci = cf_item_find_next(cs, ci)) {
1363 if (total++ == MAX_ATTRMAP) {
1364 cf_log_err(ci, "rlm_ldap: Attribute map size exceeded");
1368 if (!cf_item_is_pair(ci)) {
1369 cf_log_err(ci, "rlm_ldap: Entry is not in \"attribute ="
1370 " ldap-attribute\" format");
1374 cp = cf_itemtopair(ci);
1375 map = radius_cp2map(cp, REQUEST_CURRENT, list_def);
1381 tail = &(map->next);
1387 radius_mapfree(head);
1391 /** Detach from the LDAP server and cleanup internal state.
1394 static int ldap_detach(void *instance)
1396 ldap_instance *inst = instance;
1398 if (inst->postauth) free(inst->postauth);
1399 if (inst->accounting) free(inst->accounting);
1401 fr_connection_pool_delete(inst->pool);
1403 if (inst->user_map) {
1404 radius_mapfree(&inst->user_map);
1412 static int parse_sub_section(CONF_SECTION *parent,
1413 ldap_instance *inst,
1414 ldap_acct_section_t **config,
1415 rlm_components_t comp)
1419 const char *name = section_type_value[comp].section;
1421 cs = cf_section_sub_find(parent, name);
1423 radlog(L_INFO, "rlm_ldap (%s): Couldn't find configuration for "
1424 "%s, will return NOOP for calls from this section",
1425 inst->xlat_name, name);
1430 *config = rad_calloc(sizeof(**config));
1431 if (cf_section_parse(cs, *config, acct_section_config) < 0) {
1432 radlog(L_ERR, "rlm_ldap (%s): Failed parsing configuration for "
1433 "section %s", inst->xlat_name, name);
1447 * Uses section of radiusd config file passed as parameter to create an
1448 * instance of the module.
1450 static int ldap_instantiate(CONF_SECTION * conf, void **instance)
1452 ldap_instance *inst;
1455 inst = rad_calloc(sizeof *inst);
1458 inst->chase_referrals = 2; /* use OpenLDAP defaults */
1461 inst->xlat_name = cf_section_name2(conf);
1462 if (!inst->xlat_name) {
1463 inst->xlat_name = cf_section_name1(conf);
1466 rad_assert(inst->xlat_name);
1469 * If the configuration parameters can't be parsed, then fail.
1471 if ((cf_section_parse(conf, inst, module_config) < 0) ||
1472 (parse_sub_section(conf, inst,
1474 RLM_COMPONENT_ACCT) < 0) ||
1475 (parse_sub_section(conf, inst,
1477 RLM_COMPONENT_POST_AUTH) < 0)) {
1478 radlog(L_ERR, "rlm_ldap (%s): Failed parsing configuration",
1483 if (inst->server == NULL) {
1484 radlog(L_ERR, "rlm_ldap (%s): Missing 'server' directive",
1490 * Check for URLs. If they're used and the library doesn't
1491 * support them, then complain.
1494 if (ldap_is_ldap_url(inst->server)) {
1495 #ifdef HAVE_LDAP_INITIALIZE
1499 radlog(L_ERR, "rlm_ldap (%s): 'server' directive is in URL "
1500 "form but ldap_initialize() is not available",
1506 /* workaround for servers which support LDAPS but not START TLS */
1507 if (inst->port == LDAPS_PORT || inst->tls_mode) {
1508 inst->tls_mode = LDAP_OPT_X_TLS_HARD;
1513 #if LDAP_SET_REBIND_PROC_ARGS != 3
1515 * The 2-argument rebind doesn't take an instance
1516 * variable. Our rebind function needs the instance
1517 * variable for the username, password, etc.
1519 if (inst->rebind == 1) {
1520 radlog(L_ERR, "rlm_ldap (%s): Cannot use 'rebind' directive "
1521 "as this version of libldap does not support the API "
1522 "that we need", inst->xlat-name);
1528 * Build the attribute map
1530 cs = cf_section_sub_find(conf, "update");
1532 if (build_attrmap(cs, &(inst->user_map)) < 0) {
1538 * Group comparison checks.
1540 paircompare_register(PW_LDAP_GROUP, PW_USER_NAME, ldap_groupcmp, inst);
1541 if (cf_section_name2(conf)) {
1546 snprintf(buffer, sizeof(buffer), "%s-Ldap-Group",
1548 memset(&flags, 0, sizeof(flags));
1550 dict_addattr(buffer, -1, 0, PW_TYPE_STRING, flags);
1551 da = dict_attrbyname(buffer);
1553 radlog(L_ERR, "rlm_ldap (%s): Failed creating "
1554 "attribute %s", inst->xlat_name, buffer);
1558 paircompare_register(da->attr, PW_USER_NAME, ldap_groupcmp,
1562 xlat_register(inst->xlat_name, ldap_xlat, inst);
1565 * Initialize the socket pool.
1567 inst->pool = fr_connection_pool_init(inst->cs, inst,
1584 static int check_access(ldap_instance *inst, REQUEST* request, LDAP_CONN *conn,
1590 vals = ldap_get_values(conn->handle, entry, inst->access_attr);
1592 if (inst->positive_access_attr) {
1593 if (strncmp(vals[0], "FALSE", 5) == 0) {
1594 RDEBUG("Dialup access disabled");
1601 RDEBUG("\"%s\" attribute exists - access denied by"
1602 " default", inst->access_attr);
1605 ldap_value_free(vals);
1607 } else if (inst->positive_access_attr) {
1608 RDEBUG("No %s attribute - access denied by default",
1619 static VALUE_PAIR *ldap_getvalue(REQUEST *request, const VALUE_PAIR_TMPL *dst,
1622 rlm_ldap_result_t *self = ctx;
1623 VALUE_PAIR *head, **tail, *vp;
1632 * Iterate over all the retrieved values,
1633 * don't try and be clever about changing operators
1634 * just use whatever was set in the attribute map.
1636 for (i = 0; i < self->count; i++) {
1637 vp = pairalloc(dst->da);
1640 pairparsevalue(vp, self->values[i]);
1650 static void xlat_attrsfree(const xlat_attrs_t *expanded)
1652 const VALUE_PAIR_MAP *map;
1653 unsigned int total = 0;
1657 for (map = expanded->maps; map != NULL; map = map->next)
1659 name = expanded->attrs[total++];
1662 if (map->src->do_xlat) {
1669 static int xlat_attrs(REQUEST *request, const VALUE_PAIR_MAP *maps,
1670 xlat_attrs_t *expanded)
1672 const VALUE_PAIR_MAP *map;
1673 unsigned int total = 0;
1678 for (map = maps; map != NULL; map = map->next)
1680 if (map->src->do_xlat) {
1681 buffer = rad_malloc(MAX_ATTR_STR_LEN);
1682 len = radius_xlat(buffer, MAX_ATTR_STR_LEN,
1683 map->src->name, request, NULL, NULL);
1686 RDEBUG("Expansion of LDAP attribute "
1687 "\"%s\" failed", map->src->name);
1689 expanded->attrs[total] = NULL;
1691 xlat_attrsfree(expanded);
1696 expanded->attrs[total++] = buffer;
1698 expanded->attrs[total++] = map->src->name;
1702 expanded->attrs[total] = NULL;
1703 expanded->maps = maps;
1709 /** Convert attribute map into valuepairs
1711 * Use the attribute map built earlier to convert LDAP values into valuepairs
1712 * and insert them into whichever list they need to go into.
1714 * This is *NOT* atomic, but there's no condition in which we should error
1717 static void do_attrmap(UNUSED ldap_instance *inst, REQUEST *request,
1718 LDAP *handle, const xlat_attrs_t *expanded,
1721 const VALUE_PAIR_MAP *map;
1722 unsigned int total = 0;
1724 rlm_ldap_result_t result;
1727 for (map = expanded->maps; map != NULL; map = map->next)
1729 name = expanded->attrs[total++];
1731 result.values = ldap_get_values(handle, entry, name);
1732 if (!result.values) {
1733 RDEBUG2("Attribute \"%s\" not found in LDAP object",
1740 * Find out how many values there are for the
1741 * attribute and extract all of them.
1743 result.count = ldap_count_values(result.values);
1746 * If something bad happened, just skip, this is probably
1747 * a case of the dst being incorrect for the current
1750 if (radius_map2request(request, map, name, ldap_getvalue,
1757 ldap_value_free(result.values);
1762 static void do_check_reply(ldap_instance *inst, REQUEST *request)
1765 * More warning messages for people who can't be bothered
1766 * to read the documentation.
1768 if (inst->expect_password && (debug_flag > 1)) {
1769 if (!pairfind(request->config_items,PW_CLEARTEXT_PASSWORD, 0) &&
1770 !pairfind(request->config_items,
1771 PW_NT_PASSWORD, 0) &&
1772 !pairfind(request->config_items,
1773 PW_USER_PASSWORD, 0) &&
1774 !pairfind(request->config_items,
1775 PW_PASSWORD_WITH_HEADER, 0) &&
1776 !pairfind(request->config_items,
1777 PW_CRYPT_PASSWORD, 0)) {
1778 RDEBUG("WARNING: No \"known good\" password "
1779 "was found in LDAP. Are you sure that "
1780 "the user is configured correctly?");
1786 static void apply_profile(ldap_instance *inst, REQUEST *request,
1787 LDAP_CONN **pconn, const char *profile,
1788 const xlat_attrs_t *expanded)
1791 LDAPMessage *result, *entry;
1793 LDAP *handle = (*pconn)->handle;
1794 char filter[MAX_FILTER_STR_LEN];
1796 if (!profile || !*profile) return;
1798 strlcpy(filter, inst->base_filter, sizeof(filter));
1800 rcode = perform_search(inst, request, pconn, profile, LDAP_SCOPE_BASE,
1801 filter, expanded->attrs, &result);
1805 RDEBUG("Profile \"%s\" not found", profile);
1810 entry = ldap_first_entry(handle, result);
1812 ldap_get_option(handle, LDAP_OPT_RESULT_CODE,
1814 radlog(L_ERR, "rlm_ldap (%s): Failed retrieving entry: %s",
1816 ldap_err2string(ldap_errno));
1821 do_attrmap(inst, request, handle, expanded, entry);
1824 ldap_msgfree(result);
1828 /** Check if user is authorized for remote access
1831 static int ldap_authorize(void *instance, REQUEST * request)
1834 int module_rcode = RLM_MODULE_OK;
1835 ldap_instance *inst = instance;
1836 char *user_dn = NULL;
1840 LDAPMessage *result, *entry;
1842 char filter[MAX_FILTER_STR_LEN];
1843 char basedn[MAX_FILTER_STR_LEN];
1844 xlat_attrs_t expanded; /* faster that mallocing every time */
1846 if (!request->username) {
1847 RDEBUG2("Attribute \"User-Name\" is required for "
1849 return RLM_MODULE_NOOP;
1853 * Check for valid input, zero length names not permitted
1855 if (request->username->length == 0) {
1856 RDEBUG2("Zero length username not permitted");
1857 return RLM_MODULE_INVALID;
1860 if (!radius_xlat(filter, sizeof(filter), inst->filter,
1861 request, ldap_escape_func, NULL)) {
1862 radlog(L_ERR, "rlm_ldap (%s): Failed creating filter",
1864 return RLM_MODULE_INVALID;
1867 if (!radius_xlat(basedn, sizeof(basedn), inst->basedn,
1868 request, ldap_escape_func, NULL)) {
1869 radlog(L_ERR, "rlm_ldap (%s): Failed creating basedn",
1871 return RLM_MODULE_INVALID;
1874 if (xlat_attrs(request, inst->user_map, &expanded) < 0) {
1875 return RLM_MODULE_FAIL;
1879 conn = ldap_get_socket(inst);
1880 if (!conn) return RLM_MODULE_FAIL;
1882 rcode = perform_search(inst, request, &conn, basedn,
1883 LDAP_SCOPE_SUBTREE, filter, expanded.attrs,
1888 module_failure_msg(request,
1889 "rlm_ldap (%s): User object not "
1893 RDEBUG("User object not found", inst->xlat_name);
1895 module_rcode = RLM_MODULE_NOTFOUND;
1902 entry = ldap_first_entry(conn->handle, result);
1904 ldap_get_option(conn->handle, LDAP_OPT_RESULT_CODE,
1906 radlog(L_ERR, "rlm_ldap (%s): Failed retrieving entry: %s",
1908 ldap_err2string(ldap_errno));
1913 user_dn = ldap_get_dn(conn->handle, entry);
1915 ldap_get_option(conn->handle, LDAP_OPT_RESULT_CODE,
1917 radlog(L_ERR, "rlm_ldap (%s): ldap_get_dn() failed: %s",
1919 ldap_err2string(ldap_errno));
1923 RDEBUG2("User found at DN \"%s\"", user_dn);
1925 * Adding attribute containing the Users' DN.
1927 pairadd(&request->config_items,
1928 pairmake("Ldap-UserDn", user_dn, T_OP_EQ));
1932 * We already have a Cleartext-Password. Skip edir.
1934 if (inst->edir && pairfind(request->config_items,
1935 PW_CLEARTEXT_PASSWORD, 0)) {
1940 * Retrieve Universal Password if we use eDirectory
1947 bufsize = sizeof(buffer);
1949 /* retrive universal password */
1950 res = nmasldap_get_password(conn->handle, user_dn,
1953 RDEBUG2("Failed to retrieve eDirectory password. Check "
1954 "your configuration !");
1955 module_rcode = RLM_MODULE_NOOP;
1959 /* Add Cleartext-Password attribute to the request */
1960 vp = radius_paircreate(request, &request->config_items,
1961 PW_CLEARTEXT_PASSWORD, 0,
1963 strlcpy(vp->vp_strvalue, buffer, sizeof(vp->vp_strvalue));
1964 vp->length = strlen(vp->vp_strvalue);
1966 RDEBUG2("Added eDirectory password in check items as %s = %s",
1967 vp->name, vp->vp_strvalue);
1969 if (inst->edir_autz) {
1970 RDEBUG2("Binding as user for eDirectory authorization "
1975 conn->rebound = TRUE;
1976 module_rcode = ldap_bind_wrapper(&conn, user_dn,
1979 if (module_rcode != RLM_MODULE_OK) {
1983 RDEBUG("Bind as user \"%s\" was successful", user_dn);
1993 if (inst->access_attr) {
1994 if (check_access(inst, request, conn, entry) < 0) {
1995 module_rcode = RLM_MODULE_USERLOCK;
2001 * Apply ONE user profile, or a default user profile.
2003 vp = pairfind(request->config_items, PW_USER_PROFILE, 0);
2004 if (vp || inst->default_profile) {
2005 char *profile = inst->default_profile;
2007 if (vp) profile = vp->vp_strvalue;
2009 apply_profile(inst, request, &conn, profile, &expanded);
2013 * Apply a SET of user profiles.
2015 if (inst->profile_attr) {
2016 vals = ldap_get_values(conn->handle, entry, inst->profile_attr);
2020 for (i = 0; (vals[i] != NULL) && (*vals[i] != '\0');
2022 apply_profile(inst, request, &conn, vals[i],
2026 ldap_value_free(vals);
2030 if (inst->user_map) {
2031 do_attrmap(inst, request, conn->handle, &expanded, entry);
2032 do_check_reply(inst, request);
2036 if (user_dn) ldap_memfree(user_dn);
2037 xlat_attrsfree(&expanded);
2038 ldap_msgfree(result);
2040 ldap_release_socket(inst, conn);
2042 return module_rcode;
2046 /** Check the user's password against ldap database
2049 static int ldap_authenticate(void *instance, REQUEST * request)
2052 const char *user_dn;
2053 ldap_instance *inst = instance;
2057 * Ensure that we're being passed a plain-text password, and not
2061 if (!request->username) {
2062 radlog(L_AUTH, "rlm_ldap (%s): Attribute \"User-Name\" is "
2063 "required for authentication", inst->xlat_name);
2064 return RLM_MODULE_INVALID;
2067 if (!request->password) {
2068 radlog(L_AUTH, "rlm_ldap (%s): Attribute \"User-Password\" "
2069 "is required for authentication.", inst->xlat_name);
2070 RDEBUG2(" You have set \"Auth-Type := LDAP\" somewhere.");
2071 RDEBUG2(" *********************************************");
2072 RDEBUG2(" * THAT CONFIGURATION IS WRONG. DELETE IT. ");
2073 RDEBUG2(" * YOU ARE PREVENTING THE SERVER FROM WORKING.");
2074 RDEBUG2(" *********************************************");
2075 return RLM_MODULE_INVALID;
2078 if (request->password->attribute != PW_USER_PASSWORD) {
2079 radlog(L_AUTH, "rlm_ldap (%s): Attribute \"User-Password\" "
2080 "is required for authentication. Cannot use \"%s\".",
2081 inst->xlat_name, request->password->name);
2082 return RLM_MODULE_INVALID;
2085 if (request->password->length == 0) {
2086 module_failure_msg(request,
2087 "rlm_ldap (%s): Empty password supplied",
2089 return RLM_MODULE_INVALID;
2092 RDEBUG("Login attempt by \"%s\" with password \"%s\"",
2093 request->username->vp_strvalue, request->password->vp_strvalue);
2095 conn = ldap_get_socket(inst);
2096 if (!conn) return RLM_MODULE_FAIL;
2099 * Get the DN by doing a search.
2101 user_dn = get_userdn(&conn, request, &module_rcode);
2103 ldap_release_socket(inst, conn);
2104 return module_rcode;
2110 conn->rebound = TRUE;
2111 module_rcode = ldap_bind_wrapper(&conn, user_dn,
2112 request->password->vp_strvalue,
2114 if (module_rcode == RLM_MODULE_OK) {
2115 RDEBUG("Bind as user \"%s\" was successful", user_dn);
2118 ldap_release_socket(inst, conn);
2119 return module_rcode;
2122 /** Modify user's object in LDAP
2125 static int user_modify(ldap_instance *inst, REQUEST *request,
2126 ldap_acct_section_t *section)
2128 int module_rcode = RLM_MODULE_OK;
2129 int ldap_errno, rcode, msg_id;
2130 LDAPMessage *result = NULL;
2134 LDAPMod *mod_p[MAX_ATTRMAP + 1], mod_s[MAX_ATTRMAP];
2135 LDAPMod **modify = mod_p;
2137 char *passed[MAX_ATTRMAP * 2];
2138 int i, total = 0, last_pass = 0;
2140 char *expanded[MAX_ATTRMAP];
2148 const char *user_dn;
2151 * Build our set of modifications using the update sections in
2158 char path[MAX_STRING_LEN];
2162 rad_assert(section);
2165 * Locate the update section were going to be using
2167 if (section->reference[0] != '.') {
2171 if (!radius_xlat(p, (sizeof(path) - (p - path)) - 1,
2172 section->reference, request, NULL, NULL)) {
2176 ci = cf_reference_item(NULL, section->cs, path);
2181 if (!cf_item_is_section(ci)){
2182 radlog(L_ERR, "rlm_ldap (%s): Reference must resolve to a "
2183 "section", inst->xlat_name);
2188 cs = cf_section_sub_find(cf_itemtosection(ci), "update");
2190 radlog(L_ERR, "rlm_ldap (%s): Section must contain 'update' "
2198 * Iterate over all the pairs, building our mods array
2200 for (ci = cf_item_find_next(cs, NULL);
2202 ci = cf_item_find_next(cs, ci)) {
2203 int do_xlat = FALSE;
2205 if (total == MAX_ATTRMAP) {
2206 radlog(L_ERR, "rlm_ldap (%s): Modify map size exceeded",
2212 if (!cf_item_is_pair(ci)) {
2213 radlog(L_ERR, "rlm_ldap (%s): Entry is not in "
2214 "\"ldap-attribute = value\" format",
2221 * Retrieve all the information we need about the pair
2223 cp = cf_itemtopair(ci);
2224 value = cf_pair_value(cp);
2225 attr = cf_pair_attr(cp);
2226 op = cf_pair_operator(cp);
2228 if ((value == NULL) || (*value == '\0')) {
2229 RDEBUG("empty value string, "
2230 "skipping attribute \"%s\"", attr);
2235 switch (cf_pair_value_type(cp))
2238 case T_SINGLE_QUOTED_STRING:
2240 case T_BACK_QUOTED_STRING:
2241 case T_DOUBLE_QUOTED_STRING:
2249 if (op == T_OP_CMP_FALSE) {
2250 passed[last_pass] = NULL;
2251 } else if (do_xlat) {
2252 p = rad_malloc(1024);
2253 radius_xlat(p, 1024, value, request, NULL, NULL);
2257 RDEBUG("xlat failed or empty value string, "
2258 "skipping attribute \"%s\"", attr);
2265 expanded[last_exp++] = p;
2266 passed[last_pass] = p;
2271 memcpy(&(passed[last_pass]), &value,
2272 sizeof(passed[last_pass]));
2275 passed[last_pass + 1] = NULL;
2277 mod_s[total].mod_values = &(passed[last_pass]);
2284 * T_OP_EQ is *NOT* supported, it is impossible to
2285 * support because of the lack of transactions in LDAP
2288 mod_s[total].mod_op = LDAP_MOD_ADD;
2291 mod_s[total].mod_op = LDAP_MOD_REPLACE;
2294 case T_OP_CMP_FALSE:
2295 mod_s[total].mod_op = LDAP_MOD_DELETE;
2297 #ifdef LDAP_MOD_INCREMENT
2299 mod_s[total].mod_op = LDAP_MOD_INCREMENT;
2303 radlog(L_ERR, "rlm_ldap (%s): Operator '%s' "
2304 "is not supported for LDAP modify "
2305 "operations", inst->xlat_name,
2306 fr_int2str(fr_tokens, op, "¿unknown?"));
2312 * Now we know the value is ok, copy the pointers into
2313 * the ldapmod struct.
2315 memcpy(&(mod_s[total].mod_type), &(attr),
2316 sizeof(mod_s[total].mod_type));
2318 mod_p[total] = &(mod_s[total]);
2323 module_rcode = RLM_MODULE_NOOP;
2327 mod_p[total] = NULL;
2329 conn = ldap_get_socket(inst);
2330 if (!conn) return RLM_MODULE_FAIL;
2333 * Perform all modifications as the default admin user.
2335 if (conn->rebound) {
2336 ldap_errno = ldap_bind_wrapper(&conn,
2337 inst->login, inst->password,
2339 if (ldap_errno != RLM_MODULE_OK) {
2343 rad_assert(conn != NULL);
2344 conn->rebound = FALSE;
2347 user_dn = get_userdn(&conn, request, &module_rcode);
2349 module_rcode = RLM_MODULE_NOTFOUND;
2353 RDEBUG2("Modifying user object with DN \"%s\"", user_dn);
2355 ldap_errno = ldap_modify_ext(conn->handle, user_dn, modify, NULL, NULL,
2357 if (ldap_errno != LDAP_SUCCESS) {
2358 switch (process_ldap_errno(inst, &conn, "Modify"))
2360 case LDAP_PROC_SUCCESS:
2362 case LDAP_PROC_REJECT:
2363 case LDAP_PROC_ERROR:
2365 case LDAP_PROC_RETRY:
2372 DEBUG3("rlm_ldap (%s): Waiting for modify result...", inst->xlat_name);
2374 tv.tv_sec = inst->timeout;
2378 rcode = ldap_result(conn->handle, msg_id, 1, &tv, &result);
2379 ldap_msgfree(result);
2381 switch (process_ldap_errno(inst, &conn, "Modify"))
2383 case LDAP_PROC_SUCCESS:
2385 case LDAP_PROC_REJECT:
2386 case LDAP_PROC_ERROR:
2388 module_rcode = RLM_MODULE_FAIL;
2390 case LDAP_PROC_RETRY:
2397 RDEBUG2("Modification successful!");
2401 * Free up any buffers we allocated for xlat expansion
2403 for (i = 0; i < last_exp; i++) {
2408 ldap_release_socket(inst, conn);
2410 return module_rcode;
2414 static int ldap_accounting(void *instance, REQUEST * request) {
2415 ldap_instance *inst = instance;
2417 if (inst->accounting) {
2418 return user_modify(inst, request, inst->accounting);
2421 return RLM_MODULE_NOOP;
2425 /** Check the user's password against ldap database
2428 static int ldap_postauth(void *instance, REQUEST * request)
2430 ldap_instance *inst = instance;
2432 if (inst->postauth) {
2433 return user_modify(inst, request, inst->postauth);
2436 return RLM_MODULE_NOOP;
2440 /* globally exported name */
2441 module_t rlm_ldap = {
2444 RLM_TYPE_THREAD_SAFE, /* type: reserved */
2445 ldap_instantiate, /* instantiation */
2446 ldap_detach, /* detach */
2448 ldap_authenticate, /* authentication */
2449 ldap_authorize, /* authorization */
2450 NULL, /* preaccounting */
2451 ldap_accounting, /* accounting */
2452 NULL, /* checksimul */
2453 NULL, /* pre-proxy */
2454 NULL, /* post-proxy */
2455 ldap_postauth /* post-auth */