2 * This program is is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License, version 2 if the
4 * License as published by the Free Software Foundation.
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
11 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software
13 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 * @brief LDAP authorization and authentication module.
21 * @copyright 1999-2013 The FreeRADIUS Server Project.
22 * @copyright 2012 Alan DeKok <aland@freeradius.org>
23 * @copyright 2012-2013 Arran Cudbard-Bell <a.cudbardb@freeradius.org>
25 #include <freeradius-devel/ident.h>
28 #include <freeradius-devel/radiusd.h>
29 #include <freeradius-devel/modules.h>
30 #include <freeradius-devel/rad_assert.h>
38 #define MAX_ATTRMAP 128
39 #define MAX_ATTR_STR_LEN 256
40 #define MAX_FILTER_STR_LEN 1024
43 extern int nmasldap_get_password(LDAP *ld,char *objectDN, char *pwd, size_t *pwdSize);
47 typedef struct ldap_acct_section {
50 const char *reference;
51 } ldap_acct_section_t;
56 fr_connection_pool_t *pool;
70 int ldap_debug; //!< Debug flag for the SDK.
72 const char *xlat_name; //!< Instance name.
77 * RADIUS attribute to LDAP attribute maps
79 value_pair_map_t *user_map; //!< Attribute map applied to users and
83 * Access related configuration
86 int positive_access_attr;
92 char *default_profile;
99 char *groupmemb_filter;
100 char *groupmemb_attr;
105 ldap_acct_section_t *postauth;
106 ldap_acct_section_t *accounting;
109 * TLS items. We should really normalize these with the
114 char *tls_cacertfile;
119 char *tls_require_cert;
139 #ifdef LDAP_OPT_X_KEEPALIVE_IDLE
142 #ifdef LDAP_OPT_X_KEEPALIVE_PROBES
143 int keepalive_probes;
145 #ifdef LDAP_OPT_ERROR_NUMBER
146 int keepalive_interval;
151 /* The default setting for TLS Certificate Verification */
152 #define TLS_DEFAULT_VERIFY "allow"
157 static CONF_PARSER tls_config[] = {
158 {"start_tls", PW_TYPE_BOOLEAN,
159 offsetof(ldap_instance,start_tls), NULL, "no"},
160 {"cacertfile", PW_TYPE_FILENAME,
161 offsetof(ldap_instance,tls_cacertfile), NULL, NULL},
162 {"cacertdir", PW_TYPE_FILENAME,
163 offsetof(ldap_instance,tls_cacertdir), NULL, NULL},
164 {"certfile", PW_TYPE_FILENAME,
165 offsetof(ldap_instance,tls_certfile), NULL, NULL},
166 {"keyfile", PW_TYPE_FILENAME,
167 offsetof(ldap_instance,tls_keyfile), NULL, NULL},
168 {"randfile", PW_TYPE_STRING_PTR, /* OK if it changes on HUP */
169 offsetof(ldap_instance,tls_randfile), NULL, NULL},
170 {"require_cert", PW_TYPE_STRING_PTR,
171 offsetof(ldap_instance,tls_require_cert), NULL, TLS_DEFAULT_VERIFY},
172 { NULL, -1, 0, NULL, NULL }
176 static CONF_PARSER attr_config[] = {
180 /* LDAP attribute name that controls remote access */
181 {"access_attr", PW_TYPE_STRING_PTR,
182 offsetof(ldap_instance,access_attr), NULL, NULL},
183 {"positive_access_attr", PW_TYPE_BOOLEAN,
184 offsetof(ldap_instance,positive_access_attr), NULL, "yes"},
186 {"base_filter", PW_TYPE_STRING_PTR,
187 offsetof(ldap_instance,base_filter), NULL,
188 "(objectclass=radiusprofile)"},
189 {"default_profile", PW_TYPE_STRING_PTR,
190 offsetof(ldap_instance,default_profile), NULL, NULL},
191 {"profile_attribute", PW_TYPE_STRING_PTR,
192 offsetof(ldap_instance,profile_attr), NULL, NULL},
194 { NULL, -1, 0, NULL, NULL }
199 * Group configuration
201 static CONF_PARSER group_config[] = {
203 * Group checks. These could probably be done
204 * via dynamic xlat's.
206 {"name_attribute", PW_TYPE_STRING_PTR,
207 offsetof(ldap_instance,groupname_attr), NULL, "cn"},
208 {"membership_filter", PW_TYPE_STRING_PTR,
209 offsetof(ldap_instance,groupmemb_filter), NULL,
210 "(|(&(objectClass=GroupOfNames)(member=%{Ldap-UserDn}))"
211 "(&(objectClass=GroupOfUniqueNames)(uniquemember=%{Ldap-UserDn})))"},
212 {"membership_attribute", PW_TYPE_STRING_PTR,
213 offsetof(ldap_instance,groupmemb_attr), NULL, NULL},
216 { NULL, -1, 0, NULL, NULL }
220 * Reference for accounting updates
222 static const CONF_PARSER acct_section_config[] = {
223 {"reference", PW_TYPE_STRING_PTR,
224 offsetof(ldap_acct_section_t, reference), NULL, "."},
225 {NULL, -1, 0, NULL, NULL}
229 * Various options that don't belong in the main configuration.
231 * Note that these overlap a bit with the connection pool code!
233 static CONF_PARSER option_config[] = {
235 * Debugging flags to the server
237 {"ldap_debug", PW_TYPE_INTEGER,
238 offsetof(ldap_instance,ldap_debug), NULL, "0x0000"},
240 {"chase_referrals", PW_TYPE_BOOLEAN,
241 offsetof(ldap_instance,chase_referrals), NULL, NULL},
243 {"rebind", PW_TYPE_BOOLEAN,
244 offsetof(ldap_instance,rebind), NULL, NULL},
246 /* timeout on network activity */
247 {"net_timeout", PW_TYPE_INTEGER,
248 offsetof(ldap_instance,net_timeout), NULL, "10"},
250 /* timeout for search results */
251 {"timeout", PW_TYPE_INTEGER,
252 offsetof(ldap_instance,timeout), NULL, "20"},
254 /* allow server unlimited time for search (server-side limit) */
255 {"timelimit", PW_TYPE_INTEGER,
256 offsetof(ldap_instance,timelimit), NULL, "20"},
258 #ifdef LDAP_OPT_X_KEEPALIVE_IDLE
259 {"idle", PW_TYPE_INTEGER,
260 offsetof(ldap_instance,keepalive_idle), NULL, "60"},
262 #ifdef LDAP_OPT_X_KEEPALIVE_PROBES
263 {"probes", PW_TYPE_INTEGER,
264 offsetof(ldap_instance,keepalive_probes), NULL, "3"},
266 #ifdef LDAP_OPT_ERROR_NUMBER
267 {"interval", PW_TYPE_INTEGER,
268 offsetof(ldap_instance,keepalive_interval), NULL, "30"},
270 { NULL, -1, 0, NULL, NULL }
274 static const CONF_PARSER module_config[] = {
275 {"server", PW_TYPE_STRING_PTR,
276 offsetof(ldap_instance,server), NULL, "localhost"},
277 {"port", PW_TYPE_INTEGER,
278 offsetof(ldap_instance,port), NULL, "389"},
280 {"password", PW_TYPE_STRING_PTR,
281 offsetof(ldap_instance,password), NULL, ""},
282 {"identity", PW_TYPE_STRING_PTR,
283 offsetof(ldap_instance,login), NULL, ""},
288 {"basedn", PW_TYPE_STRING_PTR,
289 offsetof(ldap_instance,basedn), NULL, "o=notexist"},
291 {"filter", PW_TYPE_STRING_PTR,
292 offsetof(ldap_instance,filter), NULL, "(uid=%u)"},
294 /* turn off the annoying warning if we don't expect a password */
295 {"expect_password", PW_TYPE_BOOLEAN,
296 offsetof(ldap_instance,expect_password), NULL, "yes"},
299 /* support for eDirectory Universal Password */
300 {"edir", PW_TYPE_BOOLEAN,
301 offsetof(ldap_instance,edir), NULL, NULL}, /* NULL defaults to "no" */
304 * Attempt to bind with the Cleartext password we got from eDirectory
305 * Universal password for additional authorization checks.
307 {"edir_autz", PW_TYPE_BOOLEAN,
308 offsetof(ldap_instance,edir_autz), NULL, NULL}, /* NULL defaults to "no" */
312 * Terrible things which should be deleted.
314 { "profiles", PW_TYPE_SUBSECTION, 0, NULL, (const void *) attr_config },
316 { "group", PW_TYPE_SUBSECTION, 0, NULL, (const void *) group_config },
318 { "options", PW_TYPE_SUBSECTION, 0, NULL,
319 (const void *) option_config },
321 { "tls", PW_TYPE_SUBSECTION, 0, NULL, (const void *) tls_config },
323 {NULL, -1, 0, NULL, NULL}
326 typedef struct ldap_conn {
333 typedef struct xlat_attrs {
334 const value_pair_map_t *maps;
335 const char *attrs[MAX_ATTRMAP];
338 typedef struct rlm_ldap_result {
344 LDAP_PROC_SUCCESS = 0,
345 LDAP_PROC_ERROR = -1,
346 LDAP_PROC_RETRY = -2,
347 LDAP_PROC_REJECT = -3
350 static ldap_rcode_t process_ldap_errno(ldap_instance *inst, LDAP_CONN **pconn,
351 const char *operation)
355 ldap_get_option((*pconn)->handle, LDAP_OPT_ERROR_NUMBER,
357 switch (ldap_errno) {
359 case LDAP_NO_SUCH_OBJECT:
360 return LDAP_PROC_SUCCESS;
362 case LDAP_INSUFFICIENT_ACCESS:
363 radlog(L_ERR, "rlm_ldap (%s): %s failed: Insufficient access. "
364 "Check the identity and password configuration "
365 "directives", inst->xlat_name, operation);
366 return LDAP_PROC_ERROR;
369 exec_trigger(NULL, inst->cs, "modules.ldap.timeout", TRUE);
370 radlog(L_ERR, "rlm_ldap (%s): %s failed: Timed out "
371 "while waiting for server to respond", inst->xlat_name,
373 return LDAP_PROC_ERROR;
375 case LDAP_FILTER_ERROR:
376 radlog(L_ERR, "rlm_ldap (%s): %s failed: Bad search "
377 "filter", inst->xlat_name, operation);
378 return LDAP_PROC_ERROR;
380 case LDAP_TIMELIMIT_EXCEEDED:
381 exec_trigger(NULL, inst->cs, "modules.ldap.timeout", TRUE);
385 case LDAP_UNAVAILABLE:
387 * Reconnect. There's an issue with the socket
390 radlog(L_ERR, "rlm_ldap (%s): %s failed: %s",
391 inst->xlat_name, operation, ldap_err2string(ldap_errno));
392 case LDAP_SERVER_DOWN:
393 return LDAP_PROC_RETRY;
395 case LDAP_INVALID_CREDENTIALS:
396 case LDAP_CONSTRAINT_VIOLATION:
397 return LDAP_PROC_REJECT;
399 case LDAP_OPERATIONS_ERROR:
400 DEBUGW("Please set 'chase_referrals=yes' and 'rebind=yes'");
401 DEBUGW("See the ldap module configuration for details");
405 radlog(L_ERR, "rlm_ldap (%s): %s failed: %s",
406 inst->xlat_name, operation, ldap_err2string(ldap_errno));
407 return LDAP_PROC_ERROR;
412 static int ldap_bind_wrapper(LDAP_CONN **pconn, const char *user,
413 const char *password, int retry)
416 int module_rcode = RLM_MODULE_OK;
417 LDAP_CONN *conn = *pconn;
418 ldap_instance *inst = conn->inst;
419 LDAPMessage *result = NULL;
423 msg_id = ldap_bind(conn->handle, user, password, LDAP_AUTH_SIMPLE);
424 if (msg_id < 0) goto get_error;
426 DEBUG3("rlm_ldap (%s): Waiting for bind result...", inst->xlat_name);
428 tv.tv_sec = inst->timeout;
431 rcode = ldap_result(conn->handle, msg_id, 1, &tv, &result);
432 ldap_msgfree(result);
435 switch (process_ldap_errno(inst, &conn, "Bind"))
437 case LDAP_PROC_SUCCESS:
439 case LDAP_PROC_REJECT:
440 module_rcode = RLM_MODULE_REJECT;
442 case LDAP_PROC_ERROR:
443 module_rcode = RLM_MODULE_FAIL;
445 #ifdef HAVE_LDAP_INITIALIZE
447 radlog(L_ERR, "rlm_ldap (%s): bind "
448 "with %s to %s failed",
449 inst->xlat_name, user,
454 radlog(L_ERR, "rlm_ldap (%s): bind "
455 "with %s to %s:%d failed",
456 inst->xlat_name, user,
457 inst->server, inst->port);
461 case LDAP_PROC_RETRY:
463 *pconn = fr_connection_reconnect(inst->pool, *pconn);
464 if (*pconn) goto retry;
467 module_rcode = RLM_MODULE_FAIL;
474 return module_rcode; /* caller closes the connection */
477 #if LDAP_SET_REBIND_PROC_ARGS == 3
479 * Rebind && chase referral stuff
481 static int ldap_rebind(LDAP *handle, LDAP_CONST char *url,
482 UNUSED ber_tag_t request, UNUSED ber_int_t msgid,
485 int rcode, ldap_errno;
486 LDAP_CONN *conn = ctx;
488 conn->referred = TRUE;
489 conn->rebound = TRUE; /* not really, but oh well... */
490 rad_assert(handle == conn->handle);
492 DEBUG("rlm_ldap (%s): Rebinding to URL %s", conn->inst->xlat_name, url);
495 rcode = ldap_bind_wrapper(&conn, conn->inst->login,
496 conn->inst->password, FALSE);
498 if (rcode == RLM_MODULE_OK) {
502 ldap_get_option(handle, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
508 /** Create and return a new connection
509 * This function is probably too big.
511 static void *ldap_conn_create(void *ctx)
514 int ldap_errno, ldap_version;
516 ldap_instance *inst = ctx;
518 LDAP_CONN *conn = NULL;
520 #ifdef HAVE_LDAP_INITIALIZE
522 DEBUG("rlm_ldap (%s): Connecting to %s", inst->xlat_name,
525 ldap_errno = ldap_initialize(&handle, inst->server);
526 if (ldap_errno != LDAP_SUCCESS) {
527 radlog(L_ERR, "rlm_ldap (%s): ldap_initialize() "
529 inst->xlat_name, ldap_err2string(ldap_errno));
535 DEBUG("rlm_ldap (%s): Connecting to %s:%d", inst->xlat_name,
536 inst->server, inst->port);
538 handle = ldap_init(inst->server, inst->port);
540 radlog(L_ERR, "rlm_ldap (%s): ldap_init() failed",
543 if (handle) ldap_unbind_s(handle);
549 * We now have a connection structure, but no actual TCP connection.
551 * Set a bunch of LDAP options, using common code.
553 #define do_ldap_option(_option, _name, _value) \
554 if (ldap_set_option(handle, _option, _value) != LDAP_OPT_SUCCESS) { \
555 ldap_get_option(handle, LDAP_OPT_ERROR_NUMBER, &ldap_errno); \
556 radlog(L_ERR, "rlm_ldap (%s): Could not set %s: %s", \
557 inst->xlat_name, _name, ldap_err2string(ldap_errno)); \
560 if (inst->ldap_debug) {
561 do_ldap_option(LDAP_OPT_DEBUG_LEVEL, "ldap_debug",
562 &(inst->ldap_debug));
566 * Leave "chase_referrals" unset to use the OpenLDAP
569 if (inst->chase_referrals != 2) {
570 if (inst->chase_referrals) {
571 do_ldap_option(LDAP_OPT_REFERRALS, "chase_referrals",
574 #if LDAP_SET_REBIND_PROC_ARGS == 3
575 if (inst->rebind == 1) {
576 ldap_set_rebind_proc(handle, ldap_rebind, inst);
580 do_ldap_option(LDAP_OPT_REFERRALS, "chase_referrals",
585 tv.tv_sec = inst->net_timeout;
587 do_ldap_option(LDAP_OPT_NETWORK_TIMEOUT, "net_timeout", &tv);
589 do_ldap_option(LDAP_OPT_TIMELIMIT, "timelimit", &(inst->timelimit));
591 ldap_version = LDAP_VERSION3;
592 do_ldap_option(LDAP_OPT_PROTOCOL_VERSION, "ldap_version",
595 #ifdef LDAP_OPT_X_KEEPALIVE_IDLE
596 do_ldap_option(LDAP_OPT_X_KEEPALIVE_IDLE, "keepalive idle",
597 &(inst->keepalive_idle));
600 #ifdef LDAP_OPT_X_KEEPALIVE_PROBES
601 do_ldap_option(LDAP_OPT_X_KEEPALIVE_PROBES, "keepalive probes",
602 &(inst->keepalive_probes));
605 #ifdef LDAP_OPT_X_KEEPALIVE_INTERVAL
606 do_ldap_option(LDAP_OPT_X_KEEPALIVE_INTERVAL, "keepalive interval",
607 &(inst->keepalive_interval));
610 #ifdef HAVE_LDAP_START_TLS
612 * Set all of the TLS options
614 if (inst->tls_mode) {
615 do_ldap_option(LDAP_OPT_X_TLS, "tls_mode", &(inst->tls_mode));
618 #define maybe_ldap_option(_option, _name, _value) \
619 if (_value) do_ldap_option(_option, _name, _value)
621 maybe_ldap_option(LDAP_OPT_X_TLS_CACERTFILE,
622 "cacertfile", inst->tls_cacertfile);
623 maybe_ldap_option(LDAP_OPT_X_TLS_CACERTDIR,
624 "cacertdir", inst->tls_cacertdir);
626 #ifdef HAVE_LDAP_INT_TLS_CONFIG
627 if (ldap_int_tls_config(NULL, LDAP_OPT_X_TLS_REQUIRE_CERT,
628 (inst->tls_require_cert)) != LDAP_OPT_SUCCESS) {
629 ldap_get_option(handle, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
630 radlog(L_ERR, "rlm_ldap (%s): could not set "
631 "LDAP_OPT_X_TLS_REQUIRE_CERT option to %s: %s",
633 inst->tls_require_cert,
634 ldap_err2string(ldap_errno));
638 maybe_ldap_option(LDAP_OPT_X_TLS_CERTFILE,
639 "certfile", inst->tls_certfile);
640 maybe_ldap_option(LDAP_OPT_X_TLS_KEYFILE,
641 "keyfile", inst->tls_keyfile);
642 maybe_ldap_option(LDAP_OPT_X_TLS_RANDOM_FILE,
643 "randfile", inst->tls_randfile);
646 * And finally start the TLS code.
648 if (inst->start_tls && (inst->port != 636)) {
649 ldap_errno = ldap_start_tls_s(handle, NULL, NULL);
650 if (ldap_errno != LDAP_SUCCESS) {
651 ldap_get_option(handle, LDAP_OPT_ERROR_NUMBER,
653 radlog(L_ERR, "rlm_ldap (%s): could not start TLS: %s",
655 ldap_err2string(ldap_errno));
659 #endif /* HAVE_LDAP_START_TLS */
661 conn = rad_malloc(sizeof(*conn));
663 conn->handle = handle;
664 conn->rebound = FALSE;
665 conn->referred = FALSE;
667 module_rcode = ldap_bind_wrapper(&conn, inst->login, inst->password,
669 if (module_rcode != RLM_MODULE_OK) {
677 /** Close and delete a connection
680 static int ldap_conn_delete(UNUSED void *ctx, void *connection)
682 LDAP_CONN *conn = connection;
684 ldap_unbind_s(conn->handle);
691 /** Gets an LDAP socket from the connection pool
694 static LDAP_CONN *ldap_get_socket(ldap_instance *inst)
698 conn = fr_connection_get(inst->pool);
700 radlog(L_ERR, "rlm_ldap (%s): all ldap connections are in use",
708 /** Frees an LDAP socket back to the connection pool
711 static void ldap_release_socket(ldap_instance *inst, LDAP_CONN *conn)
714 * Could have already been free'd due to a previous error.
719 * We chased a referral to another server.
721 * This connection is no longer part of the pool which is
722 * connected to and bound to the configured server.
725 * Note that we do NOT close it if it was bound to
726 * another user. Instead, we let the next caller do the
729 if (conn->referred) {
730 fr_connection_del(inst->pool, conn);
734 fr_connection_release(inst->pool, conn);
739 /* Converts "bad" strings into ones which are safe for LDAP
742 static size_t ldap_escape_func(UNUSED REQUEST *request, char *out,
743 size_t outlen, const char *in, UNUSED void *arg)
749 * Encode unsafe characters.
752 ((in[0] == ' ') || (in[0] == '#'))) ||
753 (strchr(",+\"\\<>;*=()", *in))) {
754 static const char hex[] = "0123456789abcdef";
757 * Only 3 or less bytes available.
764 *(out++) = hex[((*in) >> 4) & 0x0f];
765 *(out++) = hex[(*in) & 0x0f];
773 * Only one byte left.
790 /** Do a search and get a response
793 static int perform_search(ldap_instance *inst, REQUEST *request,
794 LDAP_CONN **pconn, const char *search_basedn,
795 int scope, const char *filter,
796 const char * const *attrs, LDAPMessage **presult)
800 LDAP_CONN *conn = *pconn;
804 * OpenLDAP library doesn't declare attrs array as const, but
805 * it really should be *sigh*.
808 memcpy(&search_attrs, &attrs, sizeof(attrs));
813 * Do all searches as the default admin user.
816 ldap_errno = ldap_bind_wrapper(pconn, inst->login,
817 inst->password, TRUE);
818 if (ldap_errno != RLM_MODULE_OK) {
822 rad_assert(*pconn != NULL);
824 conn->rebound = FALSE;
827 tv.tv_sec = inst->timeout;
829 RDEBUG2("Performing search in '%s' with filter '%s'",
830 search_basedn ? search_basedn : "(null)" ,
834 ldap_errno = ldap_search_ext_s(conn->handle, search_basedn, scope,
835 filter, search_attrs, 0, NULL, NULL,
837 if (ldap_errno != LDAP_SUCCESS) {
838 ldap_msgfree(*presult);
839 switch (process_ldap_errno(inst, pconn, "Search"))
841 case LDAP_PROC_SUCCESS:
843 case LDAP_PROC_REJECT:
844 case LDAP_PROC_ERROR:
846 case LDAP_PROC_RETRY:
847 conn = fr_connection_reconnect(inst->pool,
849 if (conn) goto retry;
856 count = ldap_count_entries(conn->handle, *presult);
858 ldap_msgfree(*presult);
859 RDEBUG("Search returned no results");
865 ldap_msgfree(*presult);
866 RDEBUG("Got ambiguous search result (%d results)", count);
874 /** Expand an LDAP URL into a query, and return a string result from that query.
877 static size_t ldap_xlat(void *instance, REQUEST *request, const char *fmt,
878 char *out, size_t freespace)
882 ldap_instance *inst = instance;
883 LDAPURLDesc *ldap_url;
884 LDAPMessage *result = NULL;
885 LDAPMessage *entry = NULL;
891 char buffer[MAX_FILTER_STR_LEN];
893 if (strchr(fmt, '%') != NULL) {
894 if (!radius_xlat(buffer, sizeof(buffer), fmt, request,
895 ldap_escape_func, NULL)) {
897 "rlm_ldap (%s): Unable to create LDAP URL",
906 if (!ldap_is_ldap_url(url)) {
907 radlog(L_ERR, "rlm_ldap (%s): String passed does not look "
908 "like an LDAP URL", inst->xlat_name);
912 if (ldap_url_parse(url, &ldap_url)){
913 radlog(L_ERR, "rlm_ldap (%s): Parsing LDAP URL failed",
919 * Nothing, empty string, "*" string, or got 2 things, die.
921 if (!ldap_url->lud_attrs || !ldap_url->lud_attrs[0] ||
922 !*ldap_url->lud_attrs[0] ||
923 (strcmp(ldap_url->lud_attrs[0], "*") == 0) ||
924 ldap_url->lud_attrs[1]) {
925 radlog(L_ERR, "rlm_ldap (%s): Bad attributes list in LDAP "
926 "URL. URL must specify exactly one attribute to "
933 if (ldap_url->lud_host &&
934 ((strncmp(inst->server, ldap_url->lud_host,
935 strlen(inst->server)) != 0) ||
936 (ldap_url->lud_port != inst->port))) {
937 RDEBUG("Requested server/port is \"%s:%i\"", ldap_url->lud_host,
943 conn = ldap_get_socket(inst);
944 if (!conn) goto free_urldesc;
946 memcpy(&attrs, &ldap_url->lud_attrs, sizeof(attrs));
948 rcode = perform_search(inst, request, &conn, ldap_url->lud_dn,
949 ldap_url->lud_scope, ldap_url->lud_filter, attrs,
953 RDEBUG("Search returned not found", inst->xlat_name);
960 entry = ldap_first_entry(conn->handle, result);
962 ldap_get_option(conn->handle, LDAP_OPT_RESULT_CODE,
964 radlog(L_ERR, "rlm_ldap (%s): Failed retrieving entry: %s",
966 ldap_err2string(ldap_errno));
970 vals = ldap_get_values(conn->handle, entry, ldap_url->lud_attrs[0]);
972 RDEBUG("No \"%s\" attributes found in specified object",
973 inst->xlat_name, ldap_url->lud_attrs[0]);
977 length = strlen(vals[0]);
978 if (length >= freespace){
983 strlcpy(out, vals[0], freespace);
986 ldap_value_free(vals);
988 ldap_msgfree(result);
990 ldap_release_socket(inst, conn);
992 ldap_free_urldesc(ldap_url);
998 static char *get_userdn(LDAP_CONN **pconn, REQUEST *request,
999 rlm_rcode_t *module_rcode)
1003 ldap_instance *inst = (*pconn)->inst;
1004 LDAPMessage *result, *entry;
1006 static char firstattr[] = "uid";
1008 const char *attrs[] = {firstattr, NULL};
1009 char filter[MAX_FILTER_STR_LEN];
1010 char basedn[MAX_FILTER_STR_LEN];
1012 *module_rcode = RLM_MODULE_FAIL;
1014 vp = pairfind(request->config_items, PW_LDAP_USERDN, 0, TAG_ANY);
1016 *module_rcode = RLM_MODULE_OK;
1017 return vp->vp_strvalue;
1020 if (!radius_xlat(filter, sizeof(filter), inst->filter,
1021 request, ldap_escape_func, NULL)) {
1022 radlog(L_ERR, "rlm_ldap (%s): Unable to create filter",
1024 *module_rcode = RLM_MODULE_INVALID;
1028 if (!radius_xlat(basedn, sizeof(basedn), inst->basedn,
1029 request, ldap_escape_func, NULL)) {
1030 radlog(L_ERR, "rlm_ldap (%s): Unable to create basedn",
1032 *module_rcode = RLM_MODULE_INVALID;
1036 rcode = perform_search(inst, request, pconn, basedn, LDAP_SCOPE_SUBTREE,
1037 filter, attrs, &result);
1040 *module_rcode = RLM_MODULE_NOTFOUND;
1046 if ((entry = ldap_first_entry((*pconn)->handle, result)) == NULL) {
1047 ldap_get_option((*pconn)->handle, LDAP_OPT_RESULT_CODE,
1049 radlog(L_ERR, "rlm_ldap (%s): Failed retrieving entry: %s",
1051 ldap_err2string(ldap_errno));
1052 ldap_msgfree(result);
1056 if ((user_dn = ldap_get_dn((*pconn)->handle, entry)) == NULL) {
1057 ldap_get_option((*pconn)->handle, LDAP_OPT_RESULT_CODE,
1059 radlog(L_ERR, "rlm_ldap (%s): ldap_get_dn() failed: %s",
1061 ldap_err2string(ldap_errno));
1063 ldap_msgfree(result);
1067 vp = pairmake("LDAP-UserDn", user_dn, T_OP_EQ);
1069 ldap_memfree(user_dn);
1070 ldap_msgfree(result);
1074 *module_rcode = RLM_MODULE_OK;
1076 pairadd(&request->config_items, vp);
1077 ldap_memfree(user_dn);
1078 ldap_msgfree(result);
1080 return vp->vp_strvalue;
1084 /** Perform LDAP-Group comparison checking
1087 static int ldap_groupcmp(void *instance, REQUEST *request,
1088 UNUSED VALUE_PAIR *thing, VALUE_PAIR *check,
1089 UNUSED VALUE_PAIR *check_pairs,
1090 UNUSED VALUE_PAIR **reply_pairs)
1092 ldap_instance *inst = instance;
1093 int i, rcode, found;
1094 rlm_rcode_t module_rcode;
1095 LDAPMessage *result = NULL;
1096 LDAPMessage *entry = NULL;
1098 int check_is_dn = FALSE, value_is_dn = FALSE;
1099 static char firstattr[] = "dn";
1100 const char *attrs[] = {firstattr, NULL};
1102 const char *group_attrs[] = {inst->groupmemb_attr, NULL};
1106 char gr_filter[MAX_FILTER_STR_LEN];
1107 char filter[MAX_FILTER_STR_LEN];
1108 char basedn[MAX_FILTER_STR_LEN];
1110 RDEBUG("Searching for user in group \"%s\"", check->vp_strvalue);
1112 if (check->length == 0) {
1113 RDEBUG("Cannot do comparison (group name is empty)");
1117 conn = ldap_get_socket(inst);
1118 if (!conn) return 1;
1121 * This is used in the default membership filter.
1123 user_dn = get_userdn(&conn, request, &module_rcode);
1125 ldap_release_socket(inst, conn);
1129 if (!inst->groupmemb_filter) goto check_attr;
1131 if (!radius_xlat(gr_filter, sizeof(gr_filter),
1132 inst->groupmemb_filter, request, ldap_escape_func,
1134 radlog(L_ERR, "rlm_ldap (%s): Failed creating group filter",
1140 * If it's a DN, use that.
1142 check_is_dn = strchr(check->vp_strvalue,',') == NULL ? FALSE : TRUE;
1145 strlcpy(filter, gr_filter, sizeof(filter));
1146 strlcpy(basedn, check->vp_strvalue, sizeof(basedn));
1148 snprintf(filter, sizeof(filter), "(&(%s=%s)%s)",
1149 inst->groupname_attr,
1150 check->vp_strvalue, gr_filter);
1153 * get_userdn does this, too. Oh well.
1155 if (!radius_xlat(basedn, sizeof(basedn), inst->basedn,
1156 request, ldap_escape_func, NULL)) {
1157 radlog(L_ERR, "rlm_ldap (%s): Failed creating basedn",
1163 rcode = perform_search(inst, request, &conn, basedn, LDAP_SCOPE_SUBTREE,
1164 filter, attrs, &result);
1166 ldap_release_socket(inst, conn);
1167 ldap_msgfree(result);
1169 RDEBUG("User found in group object");
1175 ldap_release_socket(inst, conn);
1179 /* else the search returned -2, for "not found" */
1182 * Else the search returned NOTFOUND. See if we're
1183 * configured to search for group membership using user
1186 if (!inst->groupmemb_attr) {
1187 ldap_release_socket(inst, conn);
1188 RDEBUG("Group object \"%s\" not found, or user is not a member",
1189 check->vp_strvalue);
1194 RDEBUG2("Checking user object membership (%s) attributes",
1195 inst->groupmemb_attr);
1197 snprintf(filter ,sizeof(filter), "(objectclass=*)");
1199 rcode = perform_search(inst, request, &conn, user_dn, LDAP_SCOPE_BASE,
1200 filter, group_attrs, &result);
1203 RDEBUG("Can't check membership attributes, user object "
1206 ldap_release_socket(inst, conn);
1210 entry = ldap_first_entry(conn->handle, result);
1212 ldap_get_option(conn->handle, LDAP_OPT_RESULT_CODE,
1214 radlog(L_ERR, "rlm_ldap (%s): Failed retrieving entry: %s",
1216 ldap_err2string(ldap_errno));
1218 ldap_release_socket(inst, conn);
1219 ldap_msgfree(result);
1223 vals = ldap_get_values(conn->handle, entry, inst->groupmemb_attr);
1225 RDEBUG("No group membership attribute(s) found in user object");
1226 ldap_release_socket(inst, conn);
1227 ldap_msgfree(result);
1232 * Loop over the list of groups the user is a member of,
1233 * looking for a match.
1236 for (i = 0; i < ldap_count_values(vals); i++) {
1237 LDAPMessage *gr_result = NULL;
1239 value_is_dn = strchr(vals[i], ',') == NULL ? FALSE : TRUE;
1241 RDEBUG2("Processing group membership value \"%s\"", vals[i]);
1244 * Both literal group names, do case sensitive comparison
1246 if (!check_is_dn && !value_is_dn) {
1247 if (strcmp(vals[i], check->vp_strvalue) == 0){
1248 RDEBUG("User found (membership value matches "
1259 * Both DNs, do case insensitive comparison
1261 if (check_is_dn && value_is_dn) {
1262 if (strcasecmp(vals[i], check->vp_strvalue) == 0){
1263 RDEBUG("User found (membership DN matches "
1274 * If the value is not a DN, or the check item is a DN
1275 * there's nothing more we can do.
1277 if (!value_is_dn && check_is_dn) continue;
1280 * We have a value which is a DN, and a check item which
1281 * specifies the name of a group, search using the value
1282 * DN for the group, and see if it has a groupname_attr
1283 * which matches our check val.
1285 RDEBUG2("Searching with membership DN and group name");
1287 snprintf(filter,sizeof(filter), "(%s=%s)",
1288 inst->groupname_attr, check->vp_strvalue);
1290 rcode = perform_search(inst, request, &conn, vals[i],
1291 LDAP_SCOPE_BASE, filter, attrs,
1294 ldap_msgfree(gr_result);
1296 /* Error occurred */
1298 ldap_value_free(vals);
1299 ldap_msgfree(result);
1300 ldap_release_socket(inst, conn);
1305 * Either the group DN wasn't found, or it didn't have the
1306 * correct name. Continue looping over the attributes.
1309 ldap_msgfree(gr_result);
1315 RDEBUG("User found (group name in membership DN matches check "
1321 ldap_value_free(vals);
1322 ldap_msgfree(result);
1323 ldap_release_socket(inst, conn);
1326 RDEBUG("User is not a member of specified group");
1333 /** Detach from the LDAP server and cleanup internal state.
1336 static int ldap_detach(void *instance)
1338 ldap_instance *inst = instance;
1340 fr_connection_pool_delete(inst->pool);
1342 if (inst->user_map) {
1343 radius_mapfree(&inst->user_map);
1349 static int parse_sub_section(CONF_SECTION *parent,
1350 ldap_instance *inst,
1351 ldap_acct_section_t **config,
1352 rlm_components_t comp)
1356 const char *name = section_type_value[comp].section;
1358 cs = cf_section_sub_find(parent, name);
1360 radlog(L_INFO, "rlm_ldap (%s): Couldn't find configuration for "
1361 "%s, will return NOOP for calls from this section",
1362 inst->xlat_name, name);
1367 *config = talloc_zero(inst, ldap_acct_section_t);
1368 if (cf_section_parse(cs, *config, acct_section_config) < 0) {
1369 radlog(L_ERR, "rlm_ldap (%s): Failed parsing configuration for "
1370 "section %s", inst->xlat_name, name);
1379 static int ldap_map_verify(ldap_instance *inst, value_pair_map_t **head)
1381 value_pair_map_t *map;
1383 if (radius_attrmap(inst->cs, head, PAIR_LIST_REPLY,
1384 PAIR_LIST_REQUEST, MAX_ATTRMAP) < 0) {
1388 * Attrmap only performs some basic validation checks, we need
1389 * to do rlm_ldap specific checks here.
1391 for (map = *head; map != NULL; map = map->next) {
1392 if (map->dst->type != VPT_TYPE_ATTR) {
1393 cf_log_err(map->ci, "Left operand must be an "
1399 if (map->src->type == VPT_TYPE_LIST) {
1400 cf_log_err(map->ci, "Right operand must not be "
1406 switch (map->src->type)
1409 * Only =, :=, += and -= operators are supported for
1412 case VPT_TYPE_LITERAL:
1423 cf_log_err(map->ci, "Operator \"%s\" not "
1424 "allowed for %s values",
1425 fr_int2str(fr_tokens, map->op,
1427 fr_int2str(vpt_types, map->src->type,
1439 * Uses section of radiusd config file passed as parameter to create an
1440 * instance of the module.
1442 static int ldap_instantiate(CONF_SECTION * conf, void **instance)
1444 ldap_instance *inst;
1446 *instance = inst = talloc_zero(conf, ldap_instance);
1447 if (!inst) return -1;
1451 inst->chase_referrals = 2; /* use OpenLDAP defaults */
1454 inst->xlat_name = cf_section_name2(conf);
1455 if (!inst->xlat_name) {
1456 inst->xlat_name = cf_section_name1(conf);
1460 * If the configuration parameters can't be parsed, then fail.
1462 if ((cf_section_parse(conf, inst, module_config) < 0) ||
1463 (parse_sub_section(conf, inst,
1465 RLM_COMPONENT_ACCT) < 0) ||
1466 (parse_sub_section(conf, inst,
1468 RLM_COMPONENT_POST_AUTH) < 0)) {
1469 radlog(L_ERR, "rlm_ldap (%s): Failed parsing configuration",
1474 if (inst->server == NULL) {
1475 radlog(L_ERR, "rlm_ldap (%s): Missing 'server' directive",
1481 * Check for URLs. If they're used and the library doesn't
1482 * support them, then complain.
1485 if (ldap_is_ldap_url(inst->server)) {
1486 #ifdef HAVE_LDAP_INITIALIZE
1490 radlog(L_ERR, "rlm_ldap (%s): 'server' directive is in URL "
1491 "form but ldap_initialize() is not available",
1497 /* workaround for servers which support LDAPS but not START TLS */
1498 if (inst->port == LDAPS_PORT || inst->tls_mode) {
1499 inst->tls_mode = LDAP_OPT_X_TLS_HARD;
1504 #if LDAP_SET_REBIND_PROC_ARGS != 3
1506 * The 2-argument rebind doesn't take an instance
1507 * variable. Our rebind function needs the instance
1508 * variable for the username, password, etc.
1510 if (inst->rebind == 1) {
1511 radlog(L_ERR, "rlm_ldap (%s): Cannot use 'rebind' directive "
1512 "as this version of libldap does not support the API "
1513 "that we need", inst->xlat-name);
1519 * Build the attribute map
1521 if (ldap_map_verify(inst, &(inst->user_map)) < 0) {
1526 * Group comparison checks.
1528 paircompare_register(PW_LDAP_GROUP, PW_USER_NAME, ldap_groupcmp, inst);
1529 if (cf_section_name2(conf)) {
1530 const DICT_ATTR *da;
1534 snprintf(buffer, sizeof(buffer), "%s-Ldap-Group",
1536 memset(&flags, 0, sizeof(flags));
1538 dict_addattr(buffer, -1, 0, PW_TYPE_STRING, flags);
1539 da = dict_attrbyname(buffer);
1541 radlog(L_ERR, "rlm_ldap (%s): Failed creating "
1542 "attribute %s", inst->xlat_name, buffer);
1546 paircompare_register(da->attr, PW_USER_NAME, ldap_groupcmp,
1550 xlat_register(inst->xlat_name, ldap_xlat, inst);
1553 * Initialize the socket pool.
1555 inst->pool = fr_connection_pool_init(inst->cs, inst,
1571 static int check_access(ldap_instance *inst, REQUEST* request, LDAP_CONN *conn,
1577 vals = ldap_get_values(conn->handle, entry, inst->access_attr);
1579 if (inst->positive_access_attr) {
1580 if (strncmp(vals[0], "FALSE", 5) == 0) {
1581 RDEBUG("Dialup access disabled");
1588 RDEBUG("\"%s\" attribute exists - access denied by"
1589 " default", inst->access_attr);
1592 ldap_value_free(vals);
1594 } else if (inst->positive_access_attr) {
1595 RDEBUG("No %s attribute - access denied by default",
1606 static VALUE_PAIR *ldap_getvalue(REQUEST *request, const value_pair_map_t *map,
1609 rlm_ldap_result_t *self = ctx;
1610 VALUE_PAIR *head, **tail, *vp;
1619 * Iterate over all the retrieved values,
1620 * don't try and be clever about changing operators
1621 * just use whatever was set in the attribute map.
1623 for (i = 0; i < self->count; i++) {
1624 vp = pairalloc(NULL, map->dst->da);
1627 pairparsevalue(vp, self->values[i]);
1637 static void xlat_attrsfree(const xlat_attrs_t *expanded)
1639 const value_pair_map_t *map;
1640 unsigned int total = 0;
1644 for (map = expanded->maps; map != NULL; map = map->next)
1646 name = expanded->attrs[total++];
1649 switch (map->src->type)
1662 static int xlat_attrs(REQUEST *request, const value_pair_map_t *maps,
1663 xlat_attrs_t *expanded)
1665 const value_pair_map_t *map;
1666 unsigned int total = 0;
1671 VALUE_PAIR *found, **from = NULL;
1674 for (map = maps; map != NULL; map = map->next)
1676 switch (map->src->type)
1679 buffer = rad_malloc(MAX_ATTR_STR_LEN);
1680 len = radius_xlat(buffer, MAX_ATTR_STR_LEN,
1681 map->src->name, request, NULL, NULL);
1684 RDEBUG("Expansion of LDAP attribute "
1685 "\"%s\" failed", map->src->name);
1690 expanded->attrs[total++] = buffer;
1696 if (radius_request(&context, map->src->request) == 0) {
1697 from = radius_list(context, map->src->list);
1699 if (!from) continue;
1701 found = pairfind(*from, map->src->da->attr,
1702 map->src->da->vendor, TAG_ANY);
1703 if (!found) continue;
1705 buffer = rad_malloc(MAX_ATTR_STR_LEN);
1706 strlcpy(buffer, found->vp_strvalue, MAX_ATTR_STR_LEN);
1708 expanded->attrs[total++] = buffer;
1711 case VPT_TYPE_LITERAL:
1712 expanded->attrs[total++] = map->src->name;
1717 expanded->attrs[total] = NULL;
1719 xlat_attrsfree(expanded);
1726 expanded->attrs[total] = NULL;
1727 expanded->maps = maps;
1733 /** Convert attribute map into valuepairs
1735 * Use the attribute map built earlier to convert LDAP values into valuepairs
1736 * and insert them into whichever list they need to go into.
1738 * This is *NOT* atomic, but there's no condition in which we should error
1741 static void do_attrmap(UNUSED ldap_instance *inst, REQUEST *request,
1742 LDAP *handle, const xlat_attrs_t *expanded,
1745 const value_pair_map_t *map;
1746 unsigned int total = 0;
1748 rlm_ldap_result_t result;
1751 for (map = expanded->maps; map != NULL; map = map->next)
1753 name = expanded->attrs[total++];
1755 result.values = ldap_get_values(handle, entry, name);
1756 if (!result.values) {
1757 RDEBUG2("Attribute \"%s\" not found in LDAP object",
1764 * Find out how many values there are for the
1765 * attribute and extract all of them.
1767 result.count = ldap_count_values(result.values);
1770 * If something bad happened, just skip, this is probably
1771 * a case of the dst being incorrect for the current
1774 if (radius_map2request(request, map, name, ldap_getvalue,
1781 ldap_value_free(result.values);
1786 static void do_check_reply(ldap_instance *inst, REQUEST *request)
1789 * More warning messages for people who can't be bothered
1790 * to read the documentation.
1792 if (inst->expect_password && (debug_flag > 1)) {
1793 if (!pairfind(request->config_items, PW_CLEARTEXT_PASSWORD, 0, TAG_ANY) &&
1794 !pairfind(request->config_items, PW_NT_PASSWORD, 0, TAG_ANY) &&
1795 !pairfind(request->config_items, PW_USER_PASSWORD, 0, TAG_ANY) &&
1796 !pairfind(request->config_items, PW_PASSWORD_WITH_HEADER, 0, TAG_ANY) &&
1797 !pairfind(request->config_items, PW_CRYPT_PASSWORD, 0, TAG_ANY)) {
1798 RDEBUGW("No \"known good\" password "
1799 "was found in LDAP. Are you sure that "
1800 "the user is configured correctly?");
1806 static void apply_profile(ldap_instance *inst, REQUEST *request,
1807 LDAP_CONN **pconn, const char *profile,
1808 const xlat_attrs_t *expanded)
1811 LDAPMessage *result, *entry;
1813 LDAP *handle = (*pconn)->handle;
1814 char filter[MAX_FILTER_STR_LEN];
1816 if (!profile || !*profile) return;
1818 strlcpy(filter, inst->base_filter, sizeof(filter));
1820 rcode = perform_search(inst, request, pconn, profile, LDAP_SCOPE_BASE,
1821 filter, expanded->attrs, &result);
1825 RDEBUG("Profile \"%s\" not found", profile);
1830 entry = ldap_first_entry(handle, result);
1832 ldap_get_option(handle, LDAP_OPT_RESULT_CODE,
1834 radlog(L_ERR, "rlm_ldap (%s): Failed retrieving entry: %s",
1836 ldap_err2string(ldap_errno));
1841 do_attrmap(inst, request, handle, expanded, entry);
1844 ldap_msgfree(result);
1848 /** Check if user is authorized for remote access
1851 static rlm_rcode_t ldap_authorize(void *instance, REQUEST * request)
1854 int module_rcode = RLM_MODULE_OK;
1855 ldap_instance *inst = instance;
1856 char *user_dn = NULL;
1860 LDAPMessage *result, *entry;
1862 char filter[MAX_FILTER_STR_LEN];
1863 char basedn[MAX_FILTER_STR_LEN];
1864 xlat_attrs_t expanded; /* faster that mallocing every time */
1866 if (!request->username) {
1867 RDEBUG2("Attribute \"User-Name\" is required for "
1869 return RLM_MODULE_NOOP;
1873 * Check for valid input, zero length names not permitted
1875 if (request->username->length == 0) {
1876 RDEBUG2("Zero length username not permitted");
1877 return RLM_MODULE_INVALID;
1880 if (!radius_xlat(filter, sizeof(filter), inst->filter,
1881 request, ldap_escape_func, NULL)) {
1882 radlog(L_ERR, "rlm_ldap (%s): Failed creating filter",
1884 return RLM_MODULE_INVALID;
1887 if (!radius_xlat(basedn, sizeof(basedn), inst->basedn,
1888 request, ldap_escape_func, NULL)) {
1889 radlog(L_ERR, "rlm_ldap (%s): Failed creating basedn",
1891 return RLM_MODULE_INVALID;
1894 if (xlat_attrs(request, inst->user_map, &expanded) < 0) {
1895 return RLM_MODULE_FAIL;
1899 conn = ldap_get_socket(inst);
1900 if (!conn) return RLM_MODULE_FAIL;
1902 rcode = perform_search(inst, request, &conn, basedn,
1903 LDAP_SCOPE_SUBTREE, filter, expanded.attrs,
1908 module_failure_msg(request,
1909 "rlm_ldap (%s): User object not "
1913 RDEBUG("User object not found", inst->xlat_name);
1915 module_rcode = RLM_MODULE_NOTFOUND;
1922 entry = ldap_first_entry(conn->handle, result);
1924 ldap_get_option(conn->handle, LDAP_OPT_RESULT_CODE,
1926 radlog(L_ERR, "rlm_ldap (%s): Failed retrieving entry: %s",
1928 ldap_err2string(ldap_errno));
1933 user_dn = ldap_get_dn(conn->handle, entry);
1935 ldap_get_option(conn->handle, LDAP_OPT_RESULT_CODE,
1937 radlog(L_ERR, "rlm_ldap (%s): ldap_get_dn() failed: %s",
1939 ldap_err2string(ldap_errno));
1943 RDEBUG2("User found at DN \"%s\"", user_dn);
1945 * Adding attribute containing the Users' DN.
1947 pairadd(&request->config_items,
1948 pairmake("Ldap-UserDn", user_dn, T_OP_EQ));
1952 * We already have a Cleartext-Password. Skip edir.
1954 if (inst->edir && pairfind(request->config_items, PW_CLEARTEXT_PASSWORD, 0, TAG_ANY)) {
1959 * Retrieve Universal Password if we use eDirectory
1966 bufsize = sizeof(buffer);
1968 /* retrive universal password */
1969 res = nmasldap_get_password(conn->handle, user_dn,
1972 RDEBUG2("Failed to retrieve eDirectory password. Check "
1973 "your configuration !");
1974 module_rcode = RLM_MODULE_NOOP;
1978 /* Add Cleartext-Password attribute to the request */
1979 vp = radius_paircreate(request, &request->config_items,
1980 PW_CLEARTEXT_PASSWORD, 0);
1981 strlcpy(vp->vp_strvalue, buffer, sizeof(vp->vp_strvalue));
1982 vp->length = strlen(vp->vp_strvalue);
1984 RDEBUG2("Added eDirectory password in check items as %s = %s",
1985 vp->da->name, vp->vp_strvalue);
1987 if (inst->edir_autz) {
1988 RDEBUG2("Binding as user for eDirectory authorization "
1993 conn->rebound = TRUE;
1994 module_rcode = ldap_bind_wrapper(&conn, user_dn,
1997 if (module_rcode != RLM_MODULE_OK) {
2001 RDEBUG("Bind as user \"%s\" was successful", user_dn);
2011 if (inst->access_attr) {
2012 if (check_access(inst, request, conn, entry) < 0) {
2013 module_rcode = RLM_MODULE_USERLOCK;
2019 * Apply ONE user profile, or a default user profile.
2021 vp = pairfind(request->config_items, PW_USER_PROFILE, 0, TAG_ANY);
2022 if (vp || inst->default_profile) {
2023 char *profile = inst->default_profile;
2025 if (vp) profile = vp->vp_strvalue;
2027 apply_profile(inst, request, &conn, profile, &expanded);
2031 * Apply a SET of user profiles.
2033 if (inst->profile_attr) {
2034 vals = ldap_get_values(conn->handle, entry, inst->profile_attr);
2038 for (i = 0; (vals[i] != NULL) && (*vals[i] != '\0');
2040 apply_profile(inst, request, &conn, vals[i],
2044 ldap_value_free(vals);
2048 if (inst->user_map) {
2049 do_attrmap(inst, request, conn->handle, &expanded, entry);
2050 do_check_reply(inst, request);
2054 if (user_dn) ldap_memfree(user_dn);
2055 xlat_attrsfree(&expanded);
2056 ldap_msgfree(result);
2058 ldap_release_socket(inst, conn);
2060 return module_rcode;
2064 /** Check the user's password against ldap database
2067 static rlm_rcode_t ldap_authenticate(void *instance, REQUEST * request)
2069 rlm_rcode_t module_rcode;
2070 const char *user_dn;
2071 ldap_instance *inst = instance;
2075 * Ensure that we're being passed a plain-text password, and not
2079 if (!request->username) {
2080 radlog(L_AUTH, "rlm_ldap (%s): Attribute \"User-Name\" is "
2081 "required for authentication", inst->xlat_name);
2082 return RLM_MODULE_INVALID;
2085 if (!request->password) {
2086 radlog(L_AUTH, "rlm_ldap (%s): Attribute \"User-Password\" "
2087 "is required for authentication.", inst->xlat_name);
2088 RDEBUG2(" You have set \"Auth-Type := LDAP\" somewhere.");
2089 RDEBUG2(" *********************************************");
2090 RDEBUG2(" * THAT CONFIGURATION IS WRONG. DELETE IT. ");
2091 RDEBUG2(" * YOU ARE PREVENTING THE SERVER FROM WORKING.");
2092 RDEBUG2(" *********************************************");
2093 return RLM_MODULE_INVALID;
2096 if (request->password->da->attr != PW_USER_PASSWORD) {
2097 radlog(L_AUTH, "rlm_ldap (%s): Attribute \"User-Password\" "
2098 "is required for authentication. Cannot use \"%s\".",
2099 inst->xlat_name, request->password->da->name);
2100 return RLM_MODULE_INVALID;
2103 if (request->password->length == 0) {
2104 module_failure_msg(request,
2105 "rlm_ldap (%s): Empty password supplied",
2107 return RLM_MODULE_INVALID;
2110 RDEBUG("Login attempt by \"%s\" with password \"%s\"",
2111 request->username->vp_strvalue, request->password->vp_strvalue);
2113 conn = ldap_get_socket(inst);
2114 if (!conn) return RLM_MODULE_FAIL;
2117 * Get the DN by doing a search.
2119 user_dn = get_userdn(&conn, request, &module_rcode);
2121 ldap_release_socket(inst, conn);
2122 return module_rcode;
2128 conn->rebound = TRUE;
2129 module_rcode = ldap_bind_wrapper(&conn, user_dn,
2130 request->password->vp_strvalue,
2132 if (module_rcode == RLM_MODULE_OK) {
2133 RDEBUG("Bind as user \"%s\" was successful", user_dn);
2136 ldap_release_socket(inst, conn);
2137 return module_rcode;
2140 /** Modify user's object in LDAP
2143 static rlm_rcode_t user_modify(ldap_instance *inst, REQUEST *request,
2144 ldap_acct_section_t *section)
2146 rlm_rcode_t module_rcode = RLM_MODULE_OK;
2147 int ldap_errno, rcode, msg_id;
2148 LDAPMessage *result = NULL;
2150 LDAP_CONN *conn = NULL;
2152 LDAPMod *mod_p[MAX_ATTRMAP + 1], mod_s[MAX_ATTRMAP];
2153 LDAPMod **modify = mod_p;
2155 char *passed[MAX_ATTRMAP * 2];
2156 int i, total = 0, last_pass = 0;
2158 char *expanded[MAX_ATTRMAP];
2166 const char *user_dn;
2169 * Build our set of modifications using the update sections in
2176 char path[MAX_STRING_LEN];
2180 rad_assert(section);
2183 * Locate the update section were going to be using
2185 if (section->reference[0] != '.') {
2189 if (!radius_xlat(p, (sizeof(path) - (p - path)) - 1,
2190 section->reference, request, NULL, NULL)) {
2194 ci = cf_reference_item(NULL, section->cs, path);
2199 if (!cf_item_is_section(ci)){
2200 radlog(L_ERR, "rlm_ldap (%s): Reference must resolve to a "
2201 "section", inst->xlat_name);
2206 cs = cf_section_sub_find(cf_itemtosection(ci), "update");
2208 radlog(L_ERR, "rlm_ldap (%s): Section must contain 'update' "
2216 * Iterate over all the pairs, building our mods array
2218 for (ci = cf_item_find_next(cs, NULL);
2220 ci = cf_item_find_next(cs, ci)) {
2221 int do_xlat = FALSE;
2223 if (total == MAX_ATTRMAP) {
2224 radlog(L_ERR, "rlm_ldap (%s): Modify map size exceeded",
2230 if (!cf_item_is_pair(ci)) {
2231 radlog(L_ERR, "rlm_ldap (%s): Entry is not in "
2232 "\"ldap-attribute = value\" format",
2239 * Retrieve all the information we need about the pair
2241 cp = cf_itemtopair(ci);
2242 value = cf_pair_value(cp);
2243 attr = cf_pair_attr(cp);
2244 op = cf_pair_operator(cp);
2246 if ((value == NULL) || (*value == '\0')) {
2247 RDEBUG("empty value string, "
2248 "skipping attribute \"%s\"", attr);
2253 switch (cf_pair_value_type(cp))
2256 case T_SINGLE_QUOTED_STRING:
2258 case T_BACK_QUOTED_STRING:
2259 case T_DOUBLE_QUOTED_STRING:
2267 if (op == T_OP_CMP_FALSE) {
2268 passed[last_pass] = NULL;
2269 } else if (do_xlat) {
2270 p = rad_malloc(1024);
2271 if (radius_xlat(p, 1024, value, request, NULL, NULL) <= 0) {
2272 RDEBUG("xlat failed or empty value string, "
2273 "skipping attribute \"%s\"", attr);
2280 expanded[last_exp++] = p;
2281 passed[last_pass] = p;
2286 memcpy(&(passed[last_pass]), &value,
2287 sizeof(passed[last_pass]));
2290 passed[last_pass + 1] = NULL;
2292 mod_s[total].mod_values = &(passed[last_pass]);
2299 * T_OP_EQ is *NOT* supported, it is impossible to
2300 * support because of the lack of transactions in LDAP
2303 mod_s[total].mod_op = LDAP_MOD_ADD;
2307 mod_s[total].mod_op = LDAP_MOD_REPLACE;
2311 case T_OP_CMP_FALSE:
2312 mod_s[total].mod_op = LDAP_MOD_DELETE;
2315 #ifdef LDAP_MOD_INCREMENT
2317 mod_s[total].mod_op = LDAP_MOD_INCREMENT;
2321 radlog(L_ERR, "rlm_ldap (%s): Operator '%s' "
2322 "is not supported for LDAP modify "
2323 "operations", inst->xlat_name,
2324 fr_int2str(fr_tokens, op, "¿unknown?"));
2330 * Now we know the value is ok, copy the pointers into
2331 * the ldapmod struct.
2333 memcpy(&(mod_s[total].mod_type), &(attr),
2334 sizeof(mod_s[total].mod_type));
2336 mod_p[total] = &(mod_s[total]);
2341 module_rcode = RLM_MODULE_NOOP;
2345 mod_p[total] = NULL;
2347 conn = ldap_get_socket(inst);
2348 if (!conn) return RLM_MODULE_FAIL;
2351 * Perform all modifications as the default admin user.
2353 if (conn->rebound) {
2354 ldap_errno = ldap_bind_wrapper(&conn, inst->login,
2355 inst->password, TRUE);
2356 if (ldap_errno != RLM_MODULE_OK) {
2360 rad_assert(conn != NULL);
2361 conn->rebound = FALSE;
2364 user_dn = get_userdn(&conn, request, &module_rcode);
2366 module_rcode = RLM_MODULE_NOTFOUND;
2370 RDEBUG2("Modifying user object with DN \"%s\"", user_dn);
2372 ldap_errno = ldap_modify_ext(conn->handle, user_dn, modify, NULL, NULL,
2374 if (ldap_errno != LDAP_SUCCESS) {
2375 switch (process_ldap_errno(inst, &conn, "Modify"))
2377 case LDAP_PROC_SUCCESS:
2379 case LDAP_PROC_REJECT:
2380 case LDAP_PROC_ERROR:
2382 case LDAP_PROC_RETRY:
2389 DEBUG3("rlm_ldap (%s): Waiting for modify result...", inst->xlat_name);
2391 tv.tv_sec = inst->timeout;
2395 rcode = ldap_result(conn->handle, msg_id, 1, &tv, &result);
2396 ldap_msgfree(result);
2398 switch (process_ldap_errno(inst, &conn, "Modify"))
2400 case LDAP_PROC_SUCCESS:
2402 case LDAP_PROC_REJECT:
2403 case LDAP_PROC_ERROR:
2405 module_rcode = RLM_MODULE_FAIL;
2407 case LDAP_PROC_RETRY:
2414 RDEBUG2("Modification successful!");
2418 * Free up any buffers we allocated for xlat expansion
2420 for (i = 0; i < last_exp; i++) {
2425 ldap_release_socket(inst, conn);
2427 return module_rcode;
2431 static rlm_rcode_t ldap_accounting(void *instance, REQUEST * request) {
2432 ldap_instance *inst = instance;
2434 if (inst->accounting) {
2435 return user_modify(inst, request, inst->accounting);
2438 return RLM_MODULE_NOOP;
2442 /** Check the user's password against ldap database
2445 static rlm_rcode_t ldap_postauth(void *instance, REQUEST * request)
2447 ldap_instance *inst = instance;
2449 if (inst->postauth) {
2450 return user_modify(inst, request, inst->postauth);
2453 return RLM_MODULE_NOOP;
2457 /* globally exported name */
2458 module_t rlm_ldap = {
2461 RLM_TYPE_THREAD_SAFE, /* type: reserved */
2462 ldap_instantiate, /* instantiation */
2463 ldap_detach, /* detach */
2465 ldap_authenticate, /* authentication */
2466 ldap_authorize, /* authorization */
2467 NULL, /* preaccounting */
2468 ldap_accounting, /* accounting */
2469 NULL, /* checksimul */
2470 NULL, /* pre-proxy */
2471 NULL, /* post-proxy */
2472 ldap_postauth /* post-auth */