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 {
343 #define LDAP_PROC_SUCCESS 0
344 #define LDAP_PROC_ERROR -1
345 #define LDAP_PROC_RETRY -2
346 #define LDAP_PROC_REJECT -3
348 static int process_ldap_errno(ldap_instance *inst, LDAP_CONN **pconn,
349 const char *operation)
353 ldap_get_option((*pconn)->handle, LDAP_OPT_ERROR_NUMBER,
355 switch (ldap_errno) {
357 case LDAP_NO_SUCH_OBJECT:
358 return LDAP_PROC_SUCCESS;
360 case LDAP_SERVER_DOWN:
362 *pconn = fr_connection_reconnect(inst->pool, *pconn);
363 if (!*pconn) return -1;
364 return LDAP_PROC_RETRY;
366 case LDAP_INSUFFICIENT_ACCESS:
367 radlog(L_ERR, "rlm_ldap (%s): %s failed: Insufficient access. "
368 "Check the identity and password configuration "
369 "directives", inst->xlat_name, operation);
370 return LDAP_PROC_ERROR;
373 exec_trigger(NULL, inst->cs, "modules.ldap.timeout", TRUE);
374 radlog(L_ERR, "rlm_ldap (%s): %s failed: Timed out "
375 "while waiting for server to respond", inst->xlat_name,
377 return LDAP_PROC_ERROR;
379 case LDAP_FILTER_ERROR:
380 radlog(L_ERR, "rlm_ldap (%s): %s failed: Bad search "
381 "filter", inst->xlat_name, operation);
382 return LDAP_PROC_ERROR;
384 case LDAP_TIMELIMIT_EXCEEDED:
385 exec_trigger(NULL, inst->cs, "modules.ldap.timeout", TRUE);
389 case LDAP_UNAVAILABLE:
391 * Reconnect. There's an issue with the socket
394 radlog(L_ERR, "rlm_ldap (%s): %s failed: %s",
395 inst->xlat_name, operation, ldap_err2string(ldap_errno));
398 case LDAP_INVALID_CREDENTIALS:
399 case LDAP_CONSTRAINT_VIOLATION:
400 return LDAP_PROC_REJECT;
403 radlog(L_ERR, "rlm_ldap (%s): %s failed: %s",
404 inst->xlat_name, operation, ldap_err2string(ldap_errno));
405 return LDAP_PROC_ERROR;
410 static int ldap_bind_wrapper(LDAP_CONN **pconn, const char *user,
411 const char *password, int retry)
414 int module_rcode = RLM_MODULE_OK;
415 LDAP_CONN *conn = *pconn;
416 ldap_instance *inst = conn->inst;
417 LDAPMessage *result = NULL;
421 msg_id = ldap_bind(conn->handle, user, password, LDAP_AUTH_SIMPLE);
422 if (msg_id < 0) goto get_error;
424 DEBUG3("rlm_ldap (%s): Waiting for bind result...", inst->xlat_name);
426 tv.tv_sec = inst->timeout;
429 rcode = ldap_result(conn->handle, msg_id, 1, &tv, &result);
430 ldap_msgfree(result);
433 switch (process_ldap_errno(inst, &conn, "Bind"))
435 case LDAP_PROC_SUCCESS:
437 case LDAP_PROC_REJECT:
438 module_rcode = RLM_MODULE_REJECT;
440 case LDAP_PROC_ERROR:
441 module_rcode = RLM_MODULE_FAIL;
443 #ifdef HAVE_LDAP_INITIALIZE
445 radlog(L_ERR, "rlm_ldap (%s): bind "
446 "with %s to %s failed",
447 inst->xlat_name, user,
452 radlog(L_ERR, "rlm_ldap (%s): bind "
453 "with %s to %s:%d failed",
454 inst->xlat_name, user,
455 inst->server, inst->port);
459 case LDAP_PROC_RETRY:
460 if (retry) goto bind;
462 module_rcode = RLM_MODULE_FAIL;
469 return module_rcode; /* caller closes the connection */
472 #if LDAP_SET_REBIND_PROC_ARGS == 3
474 * Rebind && chase referral stuff
476 static int ldap_rebind(LDAP *handle, LDAP_CONST char *url,
477 UNUSED ber_tag_t request, UNUSED ber_int_t msgid,
480 int rcode, ldap_errno;
481 LDAP_CONN *conn = ctx;
483 conn->referred = TRUE;
484 conn->rebound = TRUE; /* not really, but oh well... */
485 rad_assert(handle == conn->handle);
487 DEBUG("rlm_ldap (%s): Rebinding to URL %s", conn->inst->xlat_name, url);
490 rcode = ldap_bind_wrapper(&conn, conn->inst->login,
491 conn->inst->password, FALSE);
493 if (rcode == RLM_MODULE_OK) {
497 ldap_get_option(handle, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
503 /** Create and return a new connection
504 * This function is probably too big.
506 static void *ldap_conn_create(void *ctx)
509 int ldap_errno, ldap_version;
511 ldap_instance *inst = ctx;
513 LDAP_CONN *conn = NULL;
515 #ifdef HAVE_LDAP_INITIALIZE
517 DEBUG("rlm_ldap (%s): Connecting to %s", inst->xlat_name,
520 ldap_errno = ldap_initialize(&handle, inst->server);
521 if (ldap_errno != LDAP_SUCCESS) {
522 radlog(L_ERR, "rlm_ldap (%s): ldap_initialize() "
524 inst->xlat_name, ldap_err2string(ldap_errno));
530 DEBUG("rlm_ldap (%s): Connecting to %s:%d", inst->xlat_name,
531 inst->server, inst->port);
533 handle = ldap_init(inst->server, inst->port);
535 radlog(L_ERR, "rlm_ldap (%s): ldap_init() failed",
538 if (handle) ldap_unbind_s(handle);
544 * We now have a connection structure, but no actual TCP connection.
546 * Set a bunch of LDAP options, using common code.
548 #define do_ldap_option(_option, _name, _value) \
549 if (ldap_set_option(handle, _option, _value) != LDAP_OPT_SUCCESS) { \
550 ldap_get_option(handle, LDAP_OPT_ERROR_NUMBER, &ldap_errno); \
551 radlog(L_ERR, "rlm_ldap (%s): Could not set %s: %s", \
552 inst->xlat_name, _name, ldap_err2string(ldap_errno)); \
555 if (inst->ldap_debug) {
556 do_ldap_option(LDAP_OPT_DEBUG_LEVEL, "ldap_debug",
557 &(inst->ldap_debug));
561 * Leave "chase_referrals" unset to use the OpenLDAP
564 if (inst->chase_referrals != 2) {
565 if (inst->chase_referrals) {
566 do_ldap_option(LDAP_OPT_REFERRALS, "chase_referrals",
569 #if LDAP_SET_REBIND_PROC_ARGS == 3
570 if (inst->rebind == 1) {
571 ldap_set_rebind_proc(handle, ldap_rebind, inst);
575 do_ldap_option(LDAP_OPT_REFERRALS, "chase_referrals",
580 tv.tv_sec = inst->net_timeout;
582 do_ldap_option(LDAP_OPT_NETWORK_TIMEOUT, "net_timeout", &tv);
584 do_ldap_option(LDAP_OPT_TIMELIMIT, "timelimit", &(inst->timelimit));
586 ldap_version = LDAP_VERSION3;
587 do_ldap_option(LDAP_OPT_PROTOCOL_VERSION, "ldap_version",
590 #ifdef LDAP_OPT_X_KEEPALIVE_IDLE
591 do_ldap_option(LDAP_OPT_X_KEEPALIVE_IDLE, "keepalive idle",
592 &(inst->keepalive_idle));
595 #ifdef LDAP_OPT_X_KEEPALIVE_PROBES
596 do_ldap_option(LDAP_OPT_X_KEEPALIVE_PROBES, "keepalive probes",
597 &(inst->keepalive_probes));
600 #ifdef LDAP_OPT_X_KEEPALIVE_INTERVAL
601 do_ldap_option(LDAP_OPT_X_KEEPALIVE_INTERVAL, "keepalive interval",
602 &(inst->keepalive_interval));
605 #ifdef HAVE_LDAP_START_TLS
607 * Set all of the TLS options
609 if (inst->tls_mode) {
610 do_ldap_option(LDAP_OPT_X_TLS, "tls_mode", &(inst->tls_mode));
613 #define maybe_ldap_option(_option, _name, _value) \
614 if (_value) do_ldap_option(_option, _name, _value)
616 maybe_ldap_option(LDAP_OPT_X_TLS_CACERTFILE,
617 "cacertfile", inst->tls_cacertfile);
618 maybe_ldap_option(LDAP_OPT_X_TLS_CACERTDIR,
619 "cacertdir", inst->tls_cacertdir);
621 #ifdef HAVE_LDAP_INT_TLS_CONFIG
622 if (ldap_int_tls_config(NULL, LDAP_OPT_X_TLS_REQUIRE_CERT,
623 (inst->tls_require_cert)) != LDAP_OPT_SUCCESS) {
624 ldap_get_option(handle, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
625 radlog(L_ERR, "rlm_ldap (%s): could not set "
626 "LDAP_OPT_X_TLS_REQUIRE_CERT option to %s: %s",
628 inst->tls_require_cert,
629 ldap_err2string(ldap_errno));
633 maybe_ldap_option(LDAP_OPT_X_TLS_CERTFILE,
634 "certfile", inst->tls_certfile);
635 maybe_ldap_option(LDAP_OPT_X_TLS_KEYFILE,
636 "keyfile", inst->tls_keyfile);
637 maybe_ldap_option(LDAP_OPT_X_TLS_RANDOM_FILE,
638 "randfile", inst->tls_randfile);
641 * And finally start the TLS code.
643 if (inst->start_tls && (inst->port != 636)) {
644 ldap_errno = ldap_start_tls_s(handle, NULL, NULL);
645 if (ldap_errno != LDAP_SUCCESS) {
646 ldap_get_option(handle, LDAP_OPT_ERROR_NUMBER,
648 radlog(L_ERR, "rlm_ldap (%s): could not start TLS: %s",
650 ldap_err2string(ldap_errno));
654 #endif /* HAVE_LDAP_START_TLS */
656 conn = rad_malloc(sizeof(*conn));
658 conn->handle = handle;
659 conn->rebound = FALSE;
660 conn->referred = FALSE;
662 module_rcode = ldap_bind_wrapper(&conn, inst->login, inst->password,
664 if (module_rcode != RLM_MODULE_OK) {
672 /** Close and delete a connection
675 static int ldap_conn_delete(UNUSED void *ctx, void *connection)
677 LDAP_CONN *conn = connection;
679 ldap_unbind_s(conn->handle);
686 /** Gets an LDAP socket from the connection pool
689 static LDAP_CONN *ldap_get_socket(ldap_instance *inst)
693 conn = fr_connection_get(inst->pool);
695 radlog(L_ERR, "rlm_ldap (%s): all ldap connections are in use",
703 /** Frees an LDAP socket back to the connection pool
706 static void ldap_release_socket(ldap_instance *inst, LDAP_CONN *conn)
709 * Could have already been free'd due to a previous error.
714 * We chased a referral to another server.
716 * This connection is no longer part of the pool which is
717 * connected to and bound to the configured server.
720 * Note that we do NOT close it if it was bound to
721 * another user. Instead, we let the next caller do the
724 if (conn->referred) {
725 fr_connection_del(inst->pool, conn);
729 fr_connection_release(inst->pool, conn);
734 /* Converts "bad" strings into ones which are safe for LDAP
737 static size_t ldap_escape_func(UNUSED REQUEST *request, char *out,
738 size_t outlen, const char *in, UNUSED void *arg)
744 * Encode unsafe characters.
747 ((in[0] == ' ') || (in[0] == '#'))) ||
748 (strchr(",+\"\\<>;*=()", *in))) {
749 static const char hex[] = "0123456789abcdef";
752 * Only 3 or less bytes available.
759 *(out++) = hex[((*in) >> 4) & 0x0f];
760 *(out++) = hex[(*in) & 0x0f];
768 * Only one byte left.
785 /** Do a search and get a response
788 static int perform_search(ldap_instance *inst, REQUEST *request,
789 LDAP_CONN **pconn, const char *search_basedn,
790 int scope, const char *filter,
791 const char * const *attrs, LDAPMessage **presult)
795 LDAP_CONN *conn = *pconn;
799 * OpenLDAP library doesn't declare attrs array as const, but
800 * it really should be *sigh*.
803 memcpy(&search_attrs, &attrs, sizeof(attrs));
808 * Do all searches as the default admin user.
811 ldap_errno = ldap_bind_wrapper(pconn, inst->login,
812 inst->password, TRUE);
813 if (ldap_errno != RLM_MODULE_OK) {
817 rad_assert(*pconn != NULL);
819 conn->rebound = FALSE;
822 tv.tv_sec = inst->timeout;
824 RDEBUG2("Performing search in '%s' with filter '%s'",
825 search_basedn ? search_basedn : "(null)" ,
829 ldap_errno = ldap_search_ext_s(conn->handle, search_basedn, scope,
830 filter, search_attrs, 0, NULL, NULL,
832 if (ldap_errno != LDAP_SUCCESS) {
833 ldap_msgfree(*presult);
834 switch (process_ldap_errno(inst, pconn, "Search"))
836 case LDAP_PROC_SUCCESS:
838 case LDAP_PROC_REJECT:
839 case LDAP_PROC_ERROR:
841 case LDAP_PROC_RETRY:
849 count = ldap_count_entries(conn->handle, *presult);
851 ldap_msgfree(*presult);
852 RDEBUG("Search returned no results");
858 ldap_msgfree(*presult);
859 RDEBUG("Got ambiguous search result (%d results)", count);
867 /** Expand an LDAP URL into a query, and return a string result from that query.
870 static size_t ldap_xlat(void *instance, REQUEST *request, const char *fmt,
871 char *out, size_t freespace)
875 ldap_instance *inst = instance;
876 LDAPURLDesc *ldap_url;
877 LDAPMessage *result = NULL;
878 LDAPMessage *entry = NULL;
884 char buffer[MAX_FILTER_STR_LEN];
886 if (strchr(fmt, '%') != NULL) {
887 if (!radius_xlat(buffer, sizeof(buffer), fmt, request,
888 ldap_escape_func, NULL)) {
890 "rlm_ldap (%s): Unable to create LDAP URL",
899 if (!ldap_is_ldap_url(url)) {
900 radlog(L_ERR, "rlm_ldap (%s): String passed does not look "
901 "like an LDAP URL", inst->xlat_name);
905 if (ldap_url_parse(url, &ldap_url)){
906 radlog(L_ERR, "rlm_ldap (%s): Parsing LDAP URL failed",
912 * Nothing, empty string, "*" string, or got 2 things, die.
914 if (!ldap_url->lud_attrs || !ldap_url->lud_attrs[0] ||
915 !*ldap_url->lud_attrs[0] ||
916 (strcmp(ldap_url->lud_attrs[0], "*") == 0) ||
917 ldap_url->lud_attrs[1]) {
918 radlog(L_ERR, "rlm_ldap (%s): Bad attributes list in LDAP "
919 "URL. URL must specify exactly one attribute to "
926 if (ldap_url->lud_host &&
927 ((strncmp(inst->server, ldap_url->lud_host,
928 strlen(inst->server)) != 0) ||
929 (ldap_url->lud_port != inst->port))) {
930 RDEBUG("Requested server/port is \"%s:%i\"", ldap_url->lud_host,
936 conn = ldap_get_socket(inst);
937 if (!conn) goto free_urldesc;
939 memcpy(&attrs, &ldap_url->lud_attrs, sizeof(attrs));
941 rcode = perform_search(inst, request, &conn, ldap_url->lud_dn,
942 ldap_url->lud_scope, ldap_url->lud_filter, attrs,
946 RDEBUG("Search returned not found", inst->xlat_name);
953 entry = ldap_first_entry(conn->handle, result);
955 ldap_get_option(conn->handle, LDAP_OPT_RESULT_CODE,
957 radlog(L_ERR, "rlm_ldap (%s): Failed retrieving entry: %s",
959 ldap_err2string(ldap_errno));
963 vals = ldap_get_values(conn->handle, entry, ldap_url->lud_attrs[0]);
965 RDEBUG("No \"%s\" attributes found in specified object",
966 inst->xlat_name, ldap_url->lud_attrs[0]);
970 length = strlen(vals[0]);
971 if (length >= freespace){
976 strlcpy(out, vals[0], freespace);
979 ldap_value_free(vals);
981 ldap_msgfree(result);
983 ldap_release_socket(inst, conn);
985 ldap_free_urldesc(ldap_url);
991 static char *get_userdn(LDAP_CONN **pconn, REQUEST *request,
992 rlm_rcode_t *module_rcode)
996 ldap_instance *inst = (*pconn)->inst;
997 LDAPMessage *result, *entry;
999 static char firstattr[] = "uid";
1001 const char *attrs[] = {firstattr, NULL};
1002 char filter[MAX_FILTER_STR_LEN];
1003 char basedn[MAX_FILTER_STR_LEN];
1005 *module_rcode = RLM_MODULE_FAIL;
1007 vp = pairfind(request->config_items, PW_LDAP_USERDN, 0, TAG_ANY);
1009 *module_rcode = RLM_MODULE_OK;
1010 return vp->vp_strvalue;
1013 if (!radius_xlat(filter, sizeof(filter), inst->filter,
1014 request, ldap_escape_func, NULL)) {
1015 radlog(L_ERR, "rlm_ldap (%s): Unable to create filter",
1017 *module_rcode = RLM_MODULE_INVALID;
1021 if (!radius_xlat(basedn, sizeof(basedn), inst->basedn,
1022 request, ldap_escape_func, NULL)) {
1023 radlog(L_ERR, "rlm_ldap (%s): Unable to create basedn",
1025 *module_rcode = RLM_MODULE_INVALID;
1029 rcode = perform_search(inst, request, pconn, basedn, LDAP_SCOPE_SUBTREE,
1030 filter, attrs, &result);
1033 *module_rcode = RLM_MODULE_NOTFOUND;
1039 if ((entry = ldap_first_entry((*pconn)->handle, result)) == NULL) {
1040 ldap_get_option((*pconn)->handle, LDAP_OPT_RESULT_CODE,
1042 radlog(L_ERR, "rlm_ldap (%s): Failed retrieving entry: %s",
1044 ldap_err2string(ldap_errno));
1045 ldap_msgfree(result);
1049 if ((user_dn = ldap_get_dn((*pconn)->handle, entry)) == NULL) {
1050 ldap_get_option((*pconn)->handle, LDAP_OPT_RESULT_CODE,
1052 radlog(L_ERR, "rlm_ldap (%s): ldap_get_dn() failed: %s",
1054 ldap_err2string(ldap_errno));
1056 ldap_msgfree(result);
1060 vp = pairmake("LDAP-UserDn", user_dn, T_OP_EQ);
1062 ldap_memfree(user_dn);
1063 ldap_msgfree(result);
1067 *module_rcode = RLM_MODULE_OK;
1069 pairadd(&request->config_items, vp);
1070 ldap_memfree(user_dn);
1071 ldap_msgfree(result);
1073 return vp->vp_strvalue;
1077 /** Perform LDAP-Group comparison checking
1080 static int ldap_groupcmp(void *instance, REQUEST *request,
1081 UNUSED VALUE_PAIR *thing, VALUE_PAIR *check,
1082 UNUSED VALUE_PAIR *check_pairs,
1083 UNUSED VALUE_PAIR **reply_pairs)
1085 ldap_instance *inst = instance;
1086 int i, rcode, found;
1087 rlm_rcode_t module_rcode;
1088 LDAPMessage *result = NULL;
1089 LDAPMessage *entry = NULL;
1091 int check_is_dn = FALSE, value_is_dn = FALSE;
1092 static char firstattr[] = "dn";
1093 const char *attrs[] = {firstattr, NULL};
1095 const char *group_attrs[] = {inst->groupmemb_attr, NULL};
1099 char gr_filter[MAX_FILTER_STR_LEN];
1100 char filter[MAX_FILTER_STR_LEN];
1101 char basedn[MAX_FILTER_STR_LEN];
1103 RDEBUG("Searching for user in group \"%s\"", check->vp_strvalue);
1105 if (check->length == 0) {
1106 RDEBUG("Cannot do comparison (group name is empty)");
1110 conn = ldap_get_socket(inst);
1111 if (!conn) return 1;
1114 * This is used in the default membership filter.
1116 user_dn = get_userdn(&conn, request, &module_rcode);
1118 ldap_release_socket(inst, conn);
1122 if (!inst->groupmemb_filter) goto check_attr;
1124 if (!radius_xlat(gr_filter, sizeof(gr_filter),
1125 inst->groupmemb_filter, request, ldap_escape_func,
1127 radlog(L_ERR, "rlm_ldap (%s): Failed creating group filter",
1133 * If it's a DN, use that.
1135 check_is_dn = strchr(check->vp_strvalue,',') == NULL ? FALSE : TRUE;
1138 strlcpy(filter, gr_filter, sizeof(filter));
1139 strlcpy(basedn, check->vp_strvalue, sizeof(basedn));
1141 snprintf(filter, sizeof(filter), "(&(%s=%s)%s)",
1142 inst->groupname_attr,
1143 check->vp_strvalue, gr_filter);
1146 * get_userdn does this, too. Oh well.
1148 if (!radius_xlat(basedn, sizeof(basedn), inst->basedn,
1149 request, ldap_escape_func, NULL)) {
1150 radlog(L_ERR, "rlm_ldap (%s): Failed creating basedn",
1156 rcode = perform_search(inst, request, &conn, basedn, LDAP_SCOPE_SUBTREE,
1157 filter, attrs, &result);
1159 ldap_release_socket(inst, conn);
1160 ldap_msgfree(result);
1162 RDEBUG("User found in group object");
1168 ldap_release_socket(inst, conn);
1172 /* else the search returned -2, for "not found" */
1175 * Else the search returned NOTFOUND. See if we're
1176 * configured to search for group membership using user
1179 if (!inst->groupmemb_attr) {
1180 ldap_release_socket(inst, conn);
1181 RDEBUG("Group object \"%s\" not found, or user is not a member",
1182 check->vp_strvalue);
1187 RDEBUG2("Checking user object membership (%s) attributes",
1188 inst->groupmemb_attr);
1190 snprintf(filter ,sizeof(filter), "(objectclass=*)");
1192 rcode = perform_search(inst, request, &conn, user_dn, LDAP_SCOPE_BASE,
1193 filter, group_attrs, &result);
1196 RDEBUG("Can't check membership attributes, user object "
1199 ldap_release_socket(inst, conn);
1203 entry = ldap_first_entry(conn->handle, result);
1205 ldap_get_option(conn->handle, LDAP_OPT_RESULT_CODE,
1207 radlog(L_ERR, "rlm_ldap (%s): Failed retrieving entry: %s",
1209 ldap_err2string(ldap_errno));
1211 ldap_release_socket(inst, conn);
1212 ldap_msgfree(result);
1216 vals = ldap_get_values(conn->handle, entry, inst->groupmemb_attr);
1218 RDEBUG("No group membership attribute(s) found in user object");
1219 ldap_release_socket(inst, conn);
1220 ldap_msgfree(result);
1225 * Loop over the list of groups the user is a member of,
1226 * looking for a match.
1229 for (i = 0; i < ldap_count_values(vals); i++) {
1230 LDAPMessage *gr_result = NULL;
1232 value_is_dn = strchr(vals[i], ',') == NULL ? FALSE : TRUE;
1234 RDEBUG2("Processing group membership value \"%s\"", vals[i]);
1237 * Both literal group names, do case sensitive comparison
1239 if (!check_is_dn && !value_is_dn) {
1240 if (strcmp(vals[i], check->vp_strvalue) == 0){
1241 RDEBUG("User found (membership value matches "
1252 * Both DNs, do case insensitive comparison
1254 if (check_is_dn && value_is_dn) {
1255 if (strcasecmp(vals[i], check->vp_strvalue) == 0){
1256 RDEBUG("User found (membership DN matches "
1267 * If the value is not a DN, or the check item is a DN
1268 * there's nothing more we can do.
1270 if (!value_is_dn && check_is_dn) continue;
1273 * We have a value which is a DN, and a check item which
1274 * specifies the name of a group, search using the value
1275 * DN for the group, and see if it has a groupname_attr
1276 * which matches our check val.
1278 RDEBUG2("Searching with membership DN and group name");
1280 snprintf(filter,sizeof(filter), "(%s=%s)",
1281 inst->groupname_attr, check->vp_strvalue);
1283 rcode = perform_search(inst, request, &conn, vals[i],
1284 LDAP_SCOPE_BASE, filter, attrs,
1287 ldap_msgfree(gr_result);
1289 /* Error occurred */
1291 ldap_value_free(vals);
1292 ldap_msgfree(result);
1293 ldap_release_socket(inst, conn);
1298 * Either the group DN wasn't found, or it didn't have the
1299 * correct name. Continue looping over the attributes.
1302 ldap_msgfree(gr_result);
1308 RDEBUG("User found (group name in membership DN matches check "
1314 ldap_value_free(vals);
1315 ldap_msgfree(result);
1316 ldap_release_socket(inst, conn);
1319 RDEBUG("User is not a member of specified group");
1326 /** Detach from the LDAP server and cleanup internal state.
1329 static int ldap_detach(void *instance)
1331 ldap_instance *inst = instance;
1333 if (inst->postauth) free(inst->postauth);
1334 if (inst->accounting) free(inst->accounting);
1336 fr_connection_pool_delete(inst->pool);
1338 if (inst->user_map) {
1339 radius_mapfree(&inst->user_map);
1347 static int parse_sub_section(CONF_SECTION *parent,
1348 ldap_instance *inst,
1349 ldap_acct_section_t **config,
1350 rlm_components_t comp)
1354 const char *name = section_type_value[comp].section;
1356 cs = cf_section_sub_find(parent, name);
1358 radlog(L_INFO, "rlm_ldap (%s): Couldn't find configuration for "
1359 "%s, will return NOOP for calls from this section",
1360 inst->xlat_name, name);
1365 *config = rad_calloc(sizeof(**config));
1366 if (cf_section_parse(cs, *config, acct_section_config) < 0) {
1367 radlog(L_ERR, "rlm_ldap (%s): Failed parsing configuration for "
1368 "section %s", inst->xlat_name, name);
1381 static int ldap_map_verify(ldap_instance *inst, value_pair_map_t **head)
1383 value_pair_map_t *map;
1385 if (radius_attrmap(inst->cs, head, PAIR_LIST_REPLY,
1386 PAIR_LIST_REQUEST, MAX_ATTRMAP) < 0) {
1390 * Attrmap only performs some basic validation checks, we need
1391 * to do rlm_ldap specific checks here.
1393 for (map = *head; map != NULL; map = map->next) {
1394 if (map->dst->type != VPT_TYPE_ATTR) {
1395 cf_log_err(map->ci, "Left operand must be an "
1401 if (map->src->type == VPT_TYPE_LIST) {
1402 cf_log_err(map->ci, "Right operand must not be "
1408 switch (map->src->type)
1411 * Only =, :=, += and -= operators are supported for
1414 case VPT_TYPE_LITERAL:
1425 cf_log_err(map->ci, "Operator \"%s\" not "
1426 "allowed for %s values",
1427 fr_int2str(fr_tokens, map->op,
1429 fr_int2str(vpt_types, map->src->type,
1441 * Uses section of radiusd config file passed as parameter to create an
1442 * instance of the module.
1444 static int ldap_instantiate(CONF_SECTION * conf, void **instance)
1446 ldap_instance *inst;
1448 inst = rad_calloc(sizeof *inst);
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);
1459 rad_assert(inst->xlat_name);
1462 * If the configuration parameters can't be parsed, then fail.
1464 if ((cf_section_parse(conf, inst, module_config) < 0) ||
1465 (parse_sub_section(conf, inst,
1467 RLM_COMPONENT_ACCT) < 0) ||
1468 (parse_sub_section(conf, inst,
1470 RLM_COMPONENT_POST_AUTH) < 0)) {
1471 radlog(L_ERR, "rlm_ldap (%s): Failed parsing configuration",
1476 if (inst->server == NULL) {
1477 radlog(L_ERR, "rlm_ldap (%s): Missing 'server' directive",
1483 * Check for URLs. If they're used and the library doesn't
1484 * support them, then complain.
1487 if (ldap_is_ldap_url(inst->server)) {
1488 #ifdef HAVE_LDAP_INITIALIZE
1492 radlog(L_ERR, "rlm_ldap (%s): 'server' directive is in URL "
1493 "form but ldap_initialize() is not available",
1499 /* workaround for servers which support LDAPS but not START TLS */
1500 if (inst->port == LDAPS_PORT || inst->tls_mode) {
1501 inst->tls_mode = LDAP_OPT_X_TLS_HARD;
1506 #if LDAP_SET_REBIND_PROC_ARGS != 3
1508 * The 2-argument rebind doesn't take an instance
1509 * variable. Our rebind function needs the instance
1510 * variable for the username, password, etc.
1512 if (inst->rebind == 1) {
1513 radlog(L_ERR, "rlm_ldap (%s): Cannot use 'rebind' directive "
1514 "as this version of libldap does not support the API "
1515 "that we need", inst->xlat-name);
1521 * Build the attribute map
1523 if (ldap_map_verify(inst, &(inst->user_map)) < 0) {
1528 * Group comparison checks.
1530 paircompare_register(PW_LDAP_GROUP, PW_USER_NAME, ldap_groupcmp, inst);
1531 if (cf_section_name2(conf)) {
1532 const DICT_ATTR *da;
1536 snprintf(buffer, sizeof(buffer), "%s-Ldap-Group",
1538 memset(&flags, 0, sizeof(flags));
1540 dict_addattr(buffer, -1, 0, PW_TYPE_STRING, flags);
1541 da = dict_attrbyname(buffer);
1543 radlog(L_ERR, "rlm_ldap (%s): Failed creating "
1544 "attribute %s", inst->xlat_name, buffer);
1548 paircompare_register(da->attr, PW_USER_NAME, ldap_groupcmp,
1552 xlat_register(inst->xlat_name, ldap_xlat, inst);
1555 * Initialize the socket pool.
1557 inst->pool = fr_connection_pool_init(inst->cs, inst,
1574 static int check_access(ldap_instance *inst, REQUEST* request, LDAP_CONN *conn,
1580 vals = ldap_get_values(conn->handle, entry, inst->access_attr);
1582 if (inst->positive_access_attr) {
1583 if (strncmp(vals[0], "FALSE", 5) == 0) {
1584 RDEBUG("Dialup access disabled");
1591 RDEBUG("\"%s\" attribute exists - access denied by"
1592 " default", inst->access_attr);
1595 ldap_value_free(vals);
1597 } else if (inst->positive_access_attr) {
1598 RDEBUG("No %s attribute - access denied by default",
1609 static VALUE_PAIR *ldap_getvalue(REQUEST *request, const value_pair_map_t *map,
1612 rlm_ldap_result_t *self = ctx;
1613 VALUE_PAIR *head, **tail, *vp;
1622 * Iterate over all the retrieved values,
1623 * don't try and be clever about changing operators
1624 * just use whatever was set in the attribute map.
1626 for (i = 0; i < self->count; i++) {
1627 vp = pairalloc(map->dst->da);
1630 pairparsevalue(vp, self->values[i]);
1640 static void xlat_attrsfree(const xlat_attrs_t *expanded)
1642 const value_pair_map_t *map;
1643 unsigned int total = 0;
1647 for (map = expanded->maps; map != NULL; map = map->next)
1649 name = expanded->attrs[total++];
1652 switch (map->src->type)
1665 static int xlat_attrs(REQUEST *request, const value_pair_map_t *maps,
1666 xlat_attrs_t *expanded)
1668 const value_pair_map_t *map;
1669 unsigned int total = 0;
1674 VALUE_PAIR *found, **from = NULL;
1677 for (map = maps; map != NULL; map = map->next)
1679 switch (map->src->type)
1682 buffer = rad_malloc(MAX_ATTR_STR_LEN);
1683 len = radius_xlat(buffer, MAX_ATTR_STR_LEN,
1684 map->src->name, request, NULL, NULL);
1687 RDEBUG("Expansion of LDAP attribute "
1688 "\"%s\" failed", map->src->name);
1693 expanded->attrs[total++] = buffer;
1699 if (radius_request(&context, map->src->request) == 0) {
1700 from = radius_list(context, map->src->list);
1702 if (!from) continue;
1704 found = pairfind(*from, map->src->da->attr,
1705 map->src->da->vendor, TAG_ANY);
1706 if (!found) continue;
1708 buffer = rad_malloc(MAX_ATTR_STR_LEN);
1709 strlcpy(buffer, found->vp_strvalue, MAX_ATTR_STR_LEN);
1711 expanded->attrs[total++] = buffer;
1714 case VPT_TYPE_LITERAL:
1715 expanded->attrs[total++] = map->src->name;
1720 expanded->attrs[total] = NULL;
1722 xlat_attrsfree(expanded);
1729 expanded->attrs[total] = NULL;
1730 expanded->maps = maps;
1736 /** Convert attribute map into valuepairs
1738 * Use the attribute map built earlier to convert LDAP values into valuepairs
1739 * and insert them into whichever list they need to go into.
1741 * This is *NOT* atomic, but there's no condition in which we should error
1744 static void do_attrmap(UNUSED ldap_instance *inst, REQUEST *request,
1745 LDAP *handle, const xlat_attrs_t *expanded,
1748 const value_pair_map_t *map;
1749 unsigned int total = 0;
1751 rlm_ldap_result_t result;
1754 for (map = expanded->maps; map != NULL; map = map->next)
1756 name = expanded->attrs[total++];
1758 result.values = ldap_get_values(handle, entry, name);
1759 if (!result.values) {
1760 RDEBUG2("Attribute \"%s\" not found in LDAP object",
1767 * Find out how many values there are for the
1768 * attribute and extract all of them.
1770 result.count = ldap_count_values(result.values);
1773 * If something bad happened, just skip, this is probably
1774 * a case of the dst being incorrect for the current
1777 if (radius_map2request(request, map, name, ldap_getvalue,
1784 ldap_value_free(result.values);
1789 static void do_check_reply(ldap_instance *inst, REQUEST *request)
1792 * More warning messages for people who can't be bothered
1793 * to read the documentation.
1795 if (inst->expect_password && (debug_flag > 1)) {
1796 if (!pairfind(request->config_items, PW_CLEARTEXT_PASSWORD, 0, TAG_ANY) &&
1797 !pairfind(request->config_items, PW_NT_PASSWORD, 0, TAG_ANY) &&
1798 !pairfind(request->config_items, PW_USER_PASSWORD, 0, TAG_ANY) &&
1799 !pairfind(request->config_items, PW_PASSWORD_WITH_HEADER, 0, TAG_ANY) &&
1800 !pairfind(request->config_items, PW_CRYPT_PASSWORD, 0, TAG_ANY)) {
1801 RDEBUG("WARNING: No \"known good\" password "
1802 "was found in LDAP. Are you sure that "
1803 "the user is configured correctly?");
1809 static void apply_profile(ldap_instance *inst, REQUEST *request,
1810 LDAP_CONN **pconn, const char *profile,
1811 const xlat_attrs_t *expanded)
1814 LDAPMessage *result, *entry;
1816 LDAP *handle = (*pconn)->handle;
1817 char filter[MAX_FILTER_STR_LEN];
1819 if (!profile || !*profile) return;
1821 strlcpy(filter, inst->base_filter, sizeof(filter));
1823 rcode = perform_search(inst, request, pconn, profile, LDAP_SCOPE_BASE,
1824 filter, expanded->attrs, &result);
1828 RDEBUG("Profile \"%s\" not found", profile);
1833 entry = ldap_first_entry(handle, result);
1835 ldap_get_option(handle, LDAP_OPT_RESULT_CODE,
1837 radlog(L_ERR, "rlm_ldap (%s): Failed retrieving entry: %s",
1839 ldap_err2string(ldap_errno));
1844 do_attrmap(inst, request, handle, expanded, entry);
1847 ldap_msgfree(result);
1851 /** Check if user is authorized for remote access
1854 static rlm_rcode_t ldap_authorize(void *instance, REQUEST * request)
1857 int module_rcode = RLM_MODULE_OK;
1858 ldap_instance *inst = instance;
1859 char *user_dn = NULL;
1863 LDAPMessage *result, *entry;
1865 char filter[MAX_FILTER_STR_LEN];
1866 char basedn[MAX_FILTER_STR_LEN];
1867 xlat_attrs_t expanded; /* faster that mallocing every time */
1869 if (!request->username) {
1870 RDEBUG2("Attribute \"User-Name\" is required for "
1872 return RLM_MODULE_NOOP;
1876 * Check for valid input, zero length names not permitted
1878 if (request->username->length == 0) {
1879 RDEBUG2("Zero length username not permitted");
1880 return RLM_MODULE_INVALID;
1883 if (!radius_xlat(filter, sizeof(filter), inst->filter,
1884 request, ldap_escape_func, NULL)) {
1885 radlog(L_ERR, "rlm_ldap (%s): Failed creating filter",
1887 return RLM_MODULE_INVALID;
1890 if (!radius_xlat(basedn, sizeof(basedn), inst->basedn,
1891 request, ldap_escape_func, NULL)) {
1892 radlog(L_ERR, "rlm_ldap (%s): Failed creating basedn",
1894 return RLM_MODULE_INVALID;
1897 if (xlat_attrs(request, inst->user_map, &expanded) < 0) {
1898 return RLM_MODULE_FAIL;
1902 conn = ldap_get_socket(inst);
1903 if (!conn) return RLM_MODULE_FAIL;
1905 rcode = perform_search(inst, request, &conn, basedn,
1906 LDAP_SCOPE_SUBTREE, filter, expanded.attrs,
1911 module_failure_msg(request,
1912 "rlm_ldap (%s): User object not "
1916 RDEBUG("User object not found", inst->xlat_name);
1918 module_rcode = RLM_MODULE_NOTFOUND;
1925 entry = ldap_first_entry(conn->handle, result);
1927 ldap_get_option(conn->handle, LDAP_OPT_RESULT_CODE,
1929 radlog(L_ERR, "rlm_ldap (%s): Failed retrieving entry: %s",
1931 ldap_err2string(ldap_errno));
1936 user_dn = ldap_get_dn(conn->handle, entry);
1938 ldap_get_option(conn->handle, LDAP_OPT_RESULT_CODE,
1940 radlog(L_ERR, "rlm_ldap (%s): ldap_get_dn() failed: %s",
1942 ldap_err2string(ldap_errno));
1946 RDEBUG2("User found at DN \"%s\"", user_dn);
1948 * Adding attribute containing the Users' DN.
1950 pairadd(&request->config_items,
1951 pairmake("Ldap-UserDn", user_dn, T_OP_EQ));
1955 * We already have a Cleartext-Password. Skip edir.
1957 if (inst->edir && pairfind(request->config_items, PW_CLEARTEXT_PASSWORD, 0, TAG_ANY)) {
1962 * Retrieve Universal Password if we use eDirectory
1969 bufsize = sizeof(buffer);
1971 /* retrive universal password */
1972 res = nmasldap_get_password(conn->handle, user_dn,
1975 RDEBUG2("Failed to retrieve eDirectory password. Check "
1976 "your configuration !");
1977 module_rcode = RLM_MODULE_NOOP;
1981 /* Add Cleartext-Password attribute to the request */
1982 vp = radius_paircreate(request, &request->config_items,
1983 PW_CLEARTEXT_PASSWORD, 0);
1984 strlcpy(vp->vp_strvalue, buffer, sizeof(vp->vp_strvalue));
1985 vp->length = strlen(vp->vp_strvalue);
1987 RDEBUG2("Added eDirectory password in check items as %s = %s",
1988 vp->da->name, vp->vp_strvalue);
1990 if (inst->edir_autz) {
1991 RDEBUG2("Binding as user for eDirectory authorization "
1996 conn->rebound = TRUE;
1997 module_rcode = ldap_bind_wrapper(&conn, user_dn,
2000 if (module_rcode != RLM_MODULE_OK) {
2004 RDEBUG("Bind as user \"%s\" was successful", user_dn);
2014 if (inst->access_attr) {
2015 if (check_access(inst, request, conn, entry) < 0) {
2016 module_rcode = RLM_MODULE_USERLOCK;
2022 * Apply ONE user profile, or a default user profile.
2024 vp = pairfind(request->config_items, PW_USER_PROFILE, 0, TAG_ANY);
2025 if (vp || inst->default_profile) {
2026 char *profile = inst->default_profile;
2028 if (vp) profile = vp->vp_strvalue;
2030 apply_profile(inst, request, &conn, profile, &expanded);
2034 * Apply a SET of user profiles.
2036 if (inst->profile_attr) {
2037 vals = ldap_get_values(conn->handle, entry, inst->profile_attr);
2041 for (i = 0; (vals[i] != NULL) && (*vals[i] != '\0');
2043 apply_profile(inst, request, &conn, vals[i],
2047 ldap_value_free(vals);
2051 if (inst->user_map) {
2052 do_attrmap(inst, request, conn->handle, &expanded, entry);
2053 do_check_reply(inst, request);
2057 if (user_dn) ldap_memfree(user_dn);
2058 xlat_attrsfree(&expanded);
2059 ldap_msgfree(result);
2061 ldap_release_socket(inst, conn);
2063 return module_rcode;
2067 /** Check the user's password against ldap database
2070 static rlm_rcode_t ldap_authenticate(void *instance, REQUEST * request)
2072 rlm_rcode_t module_rcode;
2073 const char *user_dn;
2074 ldap_instance *inst = instance;
2078 * Ensure that we're being passed a plain-text password, and not
2082 if (!request->username) {
2083 radlog(L_AUTH, "rlm_ldap (%s): Attribute \"User-Name\" is "
2084 "required for authentication", inst->xlat_name);
2085 return RLM_MODULE_INVALID;
2088 if (!request->password) {
2089 radlog(L_AUTH, "rlm_ldap (%s): Attribute \"User-Password\" "
2090 "is required for authentication.", inst->xlat_name);
2091 RDEBUG2(" You have set \"Auth-Type := LDAP\" somewhere.");
2092 RDEBUG2(" *********************************************");
2093 RDEBUG2(" * THAT CONFIGURATION IS WRONG. DELETE IT. ");
2094 RDEBUG2(" * YOU ARE PREVENTING THE SERVER FROM WORKING.");
2095 RDEBUG2(" *********************************************");
2096 return RLM_MODULE_INVALID;
2099 if (request->password->da->attr != PW_USER_PASSWORD) {
2100 radlog(L_AUTH, "rlm_ldap (%s): Attribute \"User-Password\" "
2101 "is required for authentication. Cannot use \"%s\".",
2102 inst->xlat_name, request->password->da->name);
2103 return RLM_MODULE_INVALID;
2106 if (request->password->length == 0) {
2107 module_failure_msg(request,
2108 "rlm_ldap (%s): Empty password supplied",
2110 return RLM_MODULE_INVALID;
2113 RDEBUG("Login attempt by \"%s\" with password \"%s\"",
2114 request->username->vp_strvalue, request->password->vp_strvalue);
2116 conn = ldap_get_socket(inst);
2117 if (!conn) return RLM_MODULE_FAIL;
2120 * Get the DN by doing a search.
2122 user_dn = get_userdn(&conn, request, &module_rcode);
2124 ldap_release_socket(inst, conn);
2125 return module_rcode;
2131 conn->rebound = TRUE;
2132 module_rcode = ldap_bind_wrapper(&conn, user_dn,
2133 request->password->vp_strvalue,
2135 if (module_rcode == RLM_MODULE_OK) {
2136 RDEBUG("Bind as user \"%s\" was successful", user_dn);
2139 ldap_release_socket(inst, conn);
2140 return module_rcode;
2143 /** Modify user's object in LDAP
2146 static rlm_rcode_t user_modify(ldap_instance *inst, REQUEST *request,
2147 ldap_acct_section_t *section)
2149 rlm_rcode_t module_rcode = RLM_MODULE_OK;
2150 int ldap_errno, rcode, msg_id;
2151 LDAPMessage *result = NULL;
2153 LDAP_CONN *conn = NULL;
2155 LDAPMod *mod_p[MAX_ATTRMAP + 1], mod_s[MAX_ATTRMAP];
2156 LDAPMod **modify = mod_p;
2158 char *passed[MAX_ATTRMAP * 2];
2159 int i, total = 0, last_pass = 0;
2161 char *expanded[MAX_ATTRMAP];
2169 const char *user_dn;
2172 * Build our set of modifications using the update sections in
2179 char path[MAX_STRING_LEN];
2183 rad_assert(section);
2186 * Locate the update section were going to be using
2188 if (section->reference[0] != '.') {
2192 if (!radius_xlat(p, (sizeof(path) - (p - path)) - 1,
2193 section->reference, request, NULL, NULL)) {
2197 ci = cf_reference_item(NULL, section->cs, path);
2202 if (!cf_item_is_section(ci)){
2203 radlog(L_ERR, "rlm_ldap (%s): Reference must resolve to a "
2204 "section", inst->xlat_name);
2209 cs = cf_section_sub_find(cf_itemtosection(ci), "update");
2211 radlog(L_ERR, "rlm_ldap (%s): Section must contain 'update' "
2219 * Iterate over all the pairs, building our mods array
2221 for (ci = cf_item_find_next(cs, NULL);
2223 ci = cf_item_find_next(cs, ci)) {
2224 int do_xlat = FALSE;
2226 if (total == MAX_ATTRMAP) {
2227 radlog(L_ERR, "rlm_ldap (%s): Modify map size exceeded",
2233 if (!cf_item_is_pair(ci)) {
2234 radlog(L_ERR, "rlm_ldap (%s): Entry is not in "
2235 "\"ldap-attribute = value\" format",
2242 * Retrieve all the information we need about the pair
2244 cp = cf_itemtopair(ci);
2245 value = cf_pair_value(cp);
2246 attr = cf_pair_attr(cp);
2247 op = cf_pair_operator(cp);
2249 if ((value == NULL) || (*value == '\0')) {
2250 RDEBUG("empty value string, "
2251 "skipping attribute \"%s\"", attr);
2256 switch (cf_pair_value_type(cp))
2259 case T_SINGLE_QUOTED_STRING:
2261 case T_BACK_QUOTED_STRING:
2262 case T_DOUBLE_QUOTED_STRING:
2270 if (op == T_OP_CMP_FALSE) {
2271 passed[last_pass] = NULL;
2272 } else if (do_xlat) {
2273 p = rad_malloc(1024);
2274 if (radius_xlat(p, 1024, value, request, NULL, NULL) <= 0) {
2275 RDEBUG("xlat failed or empty value string, "
2276 "skipping attribute \"%s\"", attr);
2283 expanded[last_exp++] = p;
2284 passed[last_pass] = p;
2289 memcpy(&(passed[last_pass]), &value,
2290 sizeof(passed[last_pass]));
2293 passed[last_pass + 1] = NULL;
2295 mod_s[total].mod_values = &(passed[last_pass]);
2302 * T_OP_EQ is *NOT* supported, it is impossible to
2303 * support because of the lack of transactions in LDAP
2306 mod_s[total].mod_op = LDAP_MOD_ADD;
2310 mod_s[total].mod_op = LDAP_MOD_REPLACE;
2314 case T_OP_CMP_FALSE:
2315 mod_s[total].mod_op = LDAP_MOD_DELETE;
2318 #ifdef LDAP_MOD_INCREMENT
2320 mod_s[total].mod_op = LDAP_MOD_INCREMENT;
2324 radlog(L_ERR, "rlm_ldap (%s): Operator '%s' "
2325 "is not supported for LDAP modify "
2326 "operations", inst->xlat_name,
2327 fr_int2str(fr_tokens, op, "¿unknown?"));
2333 * Now we know the value is ok, copy the pointers into
2334 * the ldapmod struct.
2336 memcpy(&(mod_s[total].mod_type), &(attr),
2337 sizeof(mod_s[total].mod_type));
2339 mod_p[total] = &(mod_s[total]);
2344 module_rcode = RLM_MODULE_NOOP;
2348 mod_p[total] = NULL;
2350 conn = ldap_get_socket(inst);
2351 if (!conn) return RLM_MODULE_FAIL;
2354 * Perform all modifications as the default admin user.
2356 if (conn->rebound) {
2357 ldap_errno = ldap_bind_wrapper(&conn, inst->login,
2358 inst->password, TRUE);
2359 if (ldap_errno != RLM_MODULE_OK) {
2363 rad_assert(conn != NULL);
2364 conn->rebound = FALSE;
2367 user_dn = get_userdn(&conn, request, &module_rcode);
2369 module_rcode = RLM_MODULE_NOTFOUND;
2373 RDEBUG2("Modifying user object with DN \"%s\"", user_dn);
2375 ldap_errno = ldap_modify_ext(conn->handle, user_dn, modify, NULL, NULL,
2377 if (ldap_errno != LDAP_SUCCESS) {
2378 switch (process_ldap_errno(inst, &conn, "Modify"))
2380 case LDAP_PROC_SUCCESS:
2382 case LDAP_PROC_REJECT:
2383 case LDAP_PROC_ERROR:
2385 case LDAP_PROC_RETRY:
2392 DEBUG3("rlm_ldap (%s): Waiting for modify result...", inst->xlat_name);
2394 tv.tv_sec = inst->timeout;
2398 rcode = ldap_result(conn->handle, msg_id, 1, &tv, &result);
2399 ldap_msgfree(result);
2401 switch (process_ldap_errno(inst, &conn, "Modify"))
2403 case LDAP_PROC_SUCCESS:
2405 case LDAP_PROC_REJECT:
2406 case LDAP_PROC_ERROR:
2408 module_rcode = RLM_MODULE_FAIL;
2410 case LDAP_PROC_RETRY:
2417 RDEBUG2("Modification successful!");
2421 * Free up any buffers we allocated for xlat expansion
2423 for (i = 0; i < last_exp; i++) {
2428 ldap_release_socket(inst, conn);
2430 return module_rcode;
2434 static rlm_rcode_t ldap_accounting(void *instance, REQUEST * request) {
2435 ldap_instance *inst = instance;
2437 if (inst->accounting) {
2438 return user_modify(inst, request, inst->accounting);
2441 return RLM_MODULE_NOOP;
2445 /** Check the user's password against ldap database
2448 static rlm_rcode_t ldap_postauth(void *instance, REQUEST * request)
2450 ldap_instance *inst = instance;
2452 if (inst->postauth) {
2453 return user_modify(inst, request, inst->postauth);
2456 return RLM_MODULE_NOOP;
2460 /* globally exported name */
2461 module_t rlm_ldap = {
2464 RLM_TYPE_THREAD_SAFE, /* type: reserved */
2465 ldap_instantiate, /* instantiation */
2466 ldap_detach, /* detach */
2468 ldap_authenticate, /* authentication */
2469 ldap_authorize, /* authorization */
2470 NULL, /* preaccounting */
2471 ldap_accounting, /* accounting */
2472 NULL, /* checksimul */
2473 NULL, /* pre-proxy */
2474 NULL, /* post-proxy */
2475 ldap_postauth /* post-auth */