2 * rlm_ldap.c LDAP authorization and authentication module.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18 * Copyright 1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,
19 * 2009,2010,2011,1012 The FreeRADIUS Server Project.
21 * Copyright 2012 Alan DeKok <aland@freeradius.org>
24 #include <freeradius-devel/ident.h>
27 #include <freeradius-devel/radiusd.h>
28 #include <freeradius-devel/modules.h>
29 #include <freeradius-devel/rad_assert.h>
37 #define MAX_ATTRMAP 128
38 #define MAX_ATTR_STR_LEN 256
39 #define MAX_FILTER_STR_LEN 1024
42 extern int nmasldap_get_password(LDAP *ld,char *objectDN, char *pwd, size_t *pwdSize);
48 fr_connection_pool_t *pool;
62 int ldap_debug; /* Debug flag for LDAP SDK */
64 const char *xlat_name; /* name used to xlat */
69 * RADIUS attribute to LDAP attribute maps
71 VALUE_PAIR_MAP *user_map; /* Applied to users and profiles */
74 * Access related configuration
77 int positive_access_attr;
83 char *default_profile;
90 char *groupmemb_filter;
94 * TLS items. We should really normalize these with the
104 char *tls_require_cert;
123 #ifdef LDAP_OPT_X_KEEPALIVE_IDLE
126 #ifdef LDAP_OPT_X_KEEPALIVE_PROBES
127 int keepalive_probes;
129 #ifdef LDAP_OPT_ERROR_NUMBER
130 int keepalive_interval;
135 /* The default setting for TLS Certificate Verification */
136 #define TLS_DEFAULT_VERIFY "allow"
141 static CONF_PARSER tls_config[] = {
142 {"start_tls", PW_TYPE_BOOLEAN,
143 offsetof(ldap_instance,start_tls), NULL, "no"},
144 {"cacertfile", PW_TYPE_FILENAME,
145 offsetof(ldap_instance,tls_cacertfile), NULL, NULL},
146 {"cacertdir", PW_TYPE_FILENAME,
147 offsetof(ldap_instance,tls_cacertdir), NULL, NULL},
148 {"certfile", PW_TYPE_FILENAME,
149 offsetof(ldap_instance,tls_certfile), NULL, NULL},
150 {"keyfile", PW_TYPE_FILENAME,
151 offsetof(ldap_instance,tls_keyfile), NULL, NULL},
152 {"randfile", PW_TYPE_STRING_PTR, /* OK if it changes on HUP */
153 offsetof(ldap_instance,tls_randfile), NULL, NULL},
154 {"require_cert", PW_TYPE_STRING_PTR,
155 offsetof(ldap_instance,tls_require_cert), NULL, TLS_DEFAULT_VERIFY},
156 { NULL, -1, 0, NULL, NULL }
160 static CONF_PARSER attr_config[] = {
164 /* LDAP attribute name that controls remote access */
165 {"access_attr", PW_TYPE_STRING_PTR,
166 offsetof(ldap_instance,access_attr), NULL, NULL},
167 {"positive_access_attr", PW_TYPE_BOOLEAN,
168 offsetof(ldap_instance,positive_access_attr), NULL, "yes"},
170 {"base_filter", PW_TYPE_STRING_PTR,
171 offsetof(ldap_instance,base_filter), NULL,
172 "(objectclass=radiusprofile)"},
173 {"default_profile", PW_TYPE_STRING_PTR,
174 offsetof(ldap_instance,default_profile), NULL, NULL},
175 {"profile_attribute", PW_TYPE_STRING_PTR,
176 offsetof(ldap_instance,profile_attr), NULL, NULL},
178 { NULL, -1, 0, NULL, NULL }
183 * Group configuration
185 static CONF_PARSER group_config[] = {
187 * Group checks. These could probably be done
188 * via dynamic xlat's.
190 {"name_attribute", PW_TYPE_STRING_PTR,
191 offsetof(ldap_instance,groupname_attr), NULL, "cn"},
192 {"membership_filter", PW_TYPE_STRING_PTR,
193 offsetof(ldap_instance,groupmemb_filter), NULL,
194 "(|(&(objectClass=GroupOfNames)(member=%{Ldap-UserDn}))"
195 "(&(objectClass=GroupOfUniqueNames)(uniquemember=%{Ldap-UserDn})))"},
196 {"membership_attribute", PW_TYPE_STRING_PTR,
197 offsetof(ldap_instance,groupmemb_attr), NULL, NULL},
200 { NULL, -1, 0, NULL, NULL }
204 * Various options that don't belong in the main configuration.
206 * Note that these overlap a bit with the connection pool code!
208 static CONF_PARSER option_config[] = {
210 * Debugging flags to the server
212 {"ldap_debug", PW_TYPE_INTEGER,
213 offsetof(ldap_instance,ldap_debug), NULL, "0x0000"},
215 {"chase_referrals", PW_TYPE_BOOLEAN,
216 offsetof(ldap_instance,chase_referrals), NULL, NULL},
218 {"rebind", PW_TYPE_BOOLEAN,
219 offsetof(ldap_instance,rebind), NULL, NULL},
221 /* timeout on network activity */
222 {"net_timeout", PW_TYPE_INTEGER,
223 offsetof(ldap_instance,net_timeout), NULL, "10"},
225 /* timeout for search results */
226 {"timeout", PW_TYPE_INTEGER,
227 offsetof(ldap_instance,timeout), NULL, "20"},
229 /* allow server unlimited time for search (server-side limit) */
230 {"timelimit", PW_TYPE_INTEGER,
231 offsetof(ldap_instance,timelimit), NULL, "20"},
233 #ifdef LDAP_OPT_X_KEEPALIVE_IDLE
234 {"idle", PW_TYPE_INTEGER,
235 offsetof(ldap_instance,keepalive_idle), NULL, "60"},
237 #ifdef LDAP_OPT_X_KEEPALIVE_PROBES
238 {"probes", PW_TYPE_INTEGER,
239 offsetof(ldap_instance,keepalive_probes), NULL, "3"},
241 #ifdef LDAP_OPT_ERROR_NUMBER
242 {"interval", PW_TYPE_INTEGER,
243 offsetof(ldap_instance,keepalive_interval), NULL, "30"},
245 { NULL, -1, 0, NULL, NULL }
249 static const CONF_PARSER module_config[] = {
250 {"server", PW_TYPE_STRING_PTR,
251 offsetof(ldap_instance,server), NULL, "localhost"},
252 {"port", PW_TYPE_INTEGER,
253 offsetof(ldap_instance,port), NULL, "389"},
255 {"password", PW_TYPE_STRING_PTR,
256 offsetof(ldap_instance,password), NULL, ""},
257 {"identity", PW_TYPE_STRING_PTR,
258 offsetof(ldap_instance,login), NULL, ""},
263 {"basedn", PW_TYPE_STRING_PTR,
264 offsetof(ldap_instance,basedn), NULL, "o=notexist"},
266 {"filter", PW_TYPE_STRING_PTR,
267 offsetof(ldap_instance,filter), NULL, "(uid=%u)"},
269 /* turn off the annoying warning if we don't expect a password */
270 {"expect_password", PW_TYPE_BOOLEAN,
271 offsetof(ldap_instance,expect_password), NULL, "yes"},
274 /* support for eDirectory Universal Password */
275 {"edir", PW_TYPE_BOOLEAN,
276 offsetof(ldap_instance,edir), NULL, NULL}, /* NULL defaults to "no" */
280 * Terrible things which should be deleted.
282 { "profiles", PW_TYPE_SUBSECTION, 0, NULL, (const void *) attr_config },
284 { "group", PW_TYPE_SUBSECTION, 0, NULL, (const void *) group_config },
286 { "options", PW_TYPE_SUBSECTION, 0, NULL,
287 (const void *) option_config },
289 { "tls", PW_TYPE_SUBSECTION, 0, NULL, (const void *) tls_config },
291 { "profiles", PW_TYPE_SUBSECTION, 0, NULL, (const void *) attr_config },
293 {NULL, -1, 0, NULL, NULL}
296 typedef struct ldap_conn {
303 typedef struct xlat_attrs {
304 const VALUE_PAIR_MAP *maps;
305 const char *attrs[MAX_ATTRMAP];
308 typedef struct rlm_ldap_result {
314 #if LDAP_SET_REBIND_PROC_ARGS == 3
316 * Rebind && chase referral stuff
318 static int ldap_rebind(LDAP *handle, LDAP_CONST char *url,
319 UNUSED ber_tag_t request, UNUSED ber_int_t msgid,
322 LDAP_CONN *conn = ctx;
324 conn->referred = TRUE;
325 conn->rebound = TRUE; /* not really, but oh well... */
326 rad_assert(handle == conn->handle);
328 DEBUG("rlm_ldap (%s): Rebinding to URL %s", conn->inst->xlat_name, url);
330 return ldap_bind_s(handle, conn->inst->login, conn->inst->password,
335 static int ldap_bind_wrapper(LDAP_CONN **pconn, const char *user,
336 const char *password,
337 const char **perror_str, int do_rebind)
339 int rcode, ldap_errno;
340 int module_rcode = RLM_MODULE_FAIL;
341 int reconnect = FALSE;
342 const char *error_string;
343 LDAP_CONN *conn = *pconn;
344 ldap_instance *inst = conn->inst;
345 LDAPMessage *result = NULL;
349 ldap_errno = ldap_bind(conn->handle, user, password, LDAP_AUTH_SIMPLE);
350 if (ldap_errno < 0) {
352 ldap_get_option(conn->handle, LDAP_OPT_ERROR_NUMBER,
354 error_string = ldap_err2string(ldap_errno);
356 if (do_rebind && !reconnect) {
357 conn = fr_connection_reconnect(inst->pool, conn);
359 if (!conn) return RLM_MODULE_FAIL;
364 if (perror_str) *perror_str = error_string;
366 #ifdef HAVE_LDAP_INITIALIZE
368 radlog(L_ERR, "rlm_ldap (%s): %s bind to %s failed: %s",
369 inst->xlat_name, user,
370 inst->server, error_string);
374 radlog(L_ERR, "rlm_ldap (%s): %s bind to %s:%d "
376 inst->xlat_name, user,
377 inst->server, inst->port,
381 return module_rcode; /* caller closes the connection */
384 DEBUG3("rlm_ldap (%s): Waiting for bind result...", inst->xlat_name);
386 tv.tv_sec = inst->timeout;
388 rcode = ldap_result(conn->handle, ldap_errno, 1, &tv, &result);
389 if (rcode < 0) goto get_error;
392 error_string = "timeout";
396 ldap_errno = ldap_result2error(conn->handle, result, 1);
397 switch (ldap_errno) {
401 case LDAP_INVALID_CREDENTIALS:
402 case LDAP_CONSTRAINT_VIOLATION:
403 rcode = RLM_MODULE_REJECT;
410 return RLM_MODULE_OK;
413 /*************************************************************************
415 * Function: ldap_conn_create
417 * Purpose: Create and return a new connection
418 * This function is probably too big.
420 *************************************************************************/
421 static void *ldap_conn_create(void *ctx)
424 int ldap_errno, ldap_version;
426 ldap_instance *inst = ctx;
428 LDAP_CONN *conn = NULL;
431 #ifdef HAVE_LDAP_INITIALIZE
433 DEBUG("rlm_ldap (%s): Connect to %s", inst->xlat_name,
436 ldap_errno = ldap_initialize(&handle, inst->server);
438 if (ldap_errno != LDAP_SUCCESS) {
439 radlog(L_ERR, "rlm_ldap (%s): ldap_initialize() "
441 inst->xlat_name, ldap_err2string(ldap_errno));
447 DEBUG("rlm_ldap (%s): Connect to %s:%d", inst->xlat_name,
448 inst->server, inst->port);
450 handle = ldap_init(inst->server, inst->port);
452 radlog(L_ERR, "rlm_ldap (%s): ldap_init() failed",
455 if (handle) ldap_unbind_s(handle);
461 * We now have a connection structure, but no actual TCP connection.
463 * Set a bunch of LDAP options, using common code.
465 #define do_ldap_option(_option, _name, _value) \
466 if (ldap_set_option(handle, _option, _value) != LDAP_OPT_SUCCESS) { \
467 ldap_get_option(handle, LDAP_OPT_ERROR_NUMBER, &ldap_errno); \
468 radlog(L_ERR, "rlm_ldap (%s): Could not set %s: %s", \
469 inst->xlat_name, _name, ldap_err2string(ldap_errno)); \
472 if (inst->ldap_debug) {
473 do_ldap_option(LDAP_OPT_DEBUG_LEVEL, "ldap_debug",
474 &(inst->ldap_debug));
478 * Leave "chase_referrals" unset to use the OpenLDAP
481 if (inst->chase_referrals != 2) {
482 if (inst->chase_referrals) {
483 do_ldap_option(LDAP_OPT_REFERRALS, "chase_referrals",
486 #if LDAP_SET_REBIND_PROC_ARGS == 3
487 if (inst->rebind == 1) {
488 ldap_set_rebind_proc(handle, ldap_rebind, inst);
492 do_ldap_option(LDAP_OPT_REFERRALS, "chase_referrals",
497 tv.tv_sec = inst->net_timeout;
499 do_ldap_option(LDAP_OPT_NETWORK_TIMEOUT, "net_timeout", &tv);
501 do_ldap_option(LDAP_OPT_TIMELIMIT, "timelimit",
504 ldap_version = LDAP_VERSION3;
505 do_ldap_option(LDAP_OPT_PROTOCOL_VERSION, "ldap_version",
508 #ifdef LDAP_OPT_X_KEEPALIVE_IDLE
509 do_ldap_option(LDAP_OPT_X_KEEPALIVE_IDLE, "keepalive idle",
510 &(inst->keepalive_idle));
513 #ifdef LDAP_OPT_X_KEEPALIVE_PROBES
514 do_ldap_option(LDAP_OPT_X_KEEPALIVE_PROBES, "keepalive probes",
515 &(inst->keepalive_probes));
518 #ifdef LDAP_OPT_X_KEEPALIVE_INTERVAL
519 do_ldap_option(LDAP_OPT_X_KEEPALIVE_INTERVAL, "keepalive interval",
520 &(inst->keepalive_interval));
523 #ifdef HAVE_LDAP_START_TLS
525 * Set all of the TLS options
527 if (inst->tls_mode) {
528 do_ldap_option(LDAP_OPT_X_TLS, "tls_mode", &(inst->tls_mode));
531 #define maybe_ldap_option(_option, _name, _value) \
532 if (_value) do_ldap_option(_option, _name, _value)
534 maybe_ldap_option(LDAP_OPT_X_TLS_CACERTFILE,
535 "cacertfile", inst->tls_cacertfile);
536 maybe_ldap_option(LDAP_OPT_X_TLS_CACERTDIR,
537 "cacertdir", inst->tls_cacertdir);
539 #ifdef HAVE_LDAP_INT_TLS_CONFIG
540 if (ldap_int_tls_config(NULL, LDAP_OPT_X_TLS_REQUIRE_CERT,
541 (inst->tls_require_cert)) != LDAP_OPT_SUCCESS) {
542 ldap_get_option(handle, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
543 radlog(L_ERR, "rlm_ldap (%s): could not set ",
544 "LDAP_OPT_X_TLS_REQUIRE_CERT option to %s: %s",
546 inst->tls_require_cert,
547 ldap_err2string(ldap_errno));
551 maybe_ldap_option(LDAP_OPT_X_TLS_CERTFILE,
552 "certfile", inst->tls_certfile);
553 maybe_ldap_option(LDAP_OPT_X_TLS_KEYFILE,
554 "keyfile", inst->tls_keyfile);
555 maybe_ldap_option(LDAP_OPT_X_TLS_RANDOM_FILE,
556 "randfile", inst->tls_randfile);
559 * And finally start the TLS code.
561 if (inst->start_tls && (inst->port != 636)) {
562 ldap_errno = ldap_start_tls_s(handle, NULL, NULL);
563 if (ldap_errno != LDAP_SUCCESS) {
564 ldap_get_option(handle, LDAP_OPT_ERROR_NUMBER,
566 radlog(L_ERR, "rlm_ldap (%s): could not start TLS: %s",
568 ldap_err2string(ldap_errno));
572 #endif /* HAVE_LDAP_START_TLS */
574 conn = rad_malloc(sizeof(*conn));
576 conn->handle = handle;
577 conn->rebound = FALSE;
578 conn->referred = FALSE;
580 module_rcode = ldap_bind_wrapper(&conn, inst->login, inst->password,
582 if (module_rcode != RLM_MODULE_OK) {
583 radlog(L_ERR, "rlm_ldap (%s): failed binding to LDAP "
585 inst->xlat_name, error);
588 * FIXME: print "check config, morians!
597 /*************************************************************************
599 * Function: ldap_conn_delete
601 * Purpose: Close and delete a connection
603 *************************************************************************/
604 static int ldap_conn_delete(UNUSED void *ctx, void *connection)
606 LDAP_CONN *conn = connection;
608 ldap_unbind_s(conn->handle);
615 /*************************************************************************
617 * Function: ldap_get_socket
619 * Purpose: Gets an LDAP socket from the connection pool
621 *************************************************************************/
622 static LDAP_CONN *ldap_get_socket(ldap_instance *inst)
626 conn = fr_connection_get(inst->pool);
628 radlog(L_ERR, "rlm_ldap (%s): all ldap connections are in use",
636 /*************************************************************************
638 * Function: ldap_release_socket
640 * Purpose: Frees an LDAP socket back to the connection pool
642 *************************************************************************/
643 static void ldap_release_socket(ldap_instance *inst, LDAP_CONN *conn)
646 * Could have already been free'd due to a previous error.
651 * We chased a referral to another server.
653 * This connection is no longer part of the pool which is
654 * connected to and bound to the configured server.
657 * Note that we do NOT close it if it was bound to
658 * another user. Instead, we let the next caller do the
661 if (conn->referred) {
662 fr_connection_del(inst->pool, conn);
666 fr_connection_release(inst->pool, conn);
671 /*************************************************************************
673 * Function: ldap_escape_func
675 * Purpose: Converts "bad" strings into ones which are safe for LDAP
677 *************************************************************************/
678 static size_t ldap_escape_func(UNUSED REQUEST *request, char *out,
679 size_t outlen, const char *in, UNUSED void *arg)
685 * Encode unsafe characters.
688 ((in[0] == ' ') || (in[0] == '#'))) ||
689 (strchr(",+\"\\<>;*=()", *in))) {
690 static const char hex[] = "0123456789abcdef";
693 * Only 3 or less bytes available.
700 *(out++) = hex[((*in) >> 4) & 0x0f];
701 *(out++) = hex[(*in) & 0x0f];
709 * Only one byte left.
726 /*************************************************************************
728 * Function: perform_search
730 * Purpose: Do a search and get a response
732 *************************************************************************/
733 static int perform_search(ldap_instance *inst, REQUEST *request,
734 LDAP_CONN **pconn, const char *search_basedn,
735 int scope, const char *filter,
736 const char * const *attrs, LDAPMessage **presult)
740 int reconnect = FALSE;
741 LDAP_CONN *conn = *pconn;
745 * OpenLDAP library doesn't declare attrs array as const, but
746 * it really should be *sigh*.
749 memcpy(&search_attrs, &attrs, sizeof(attrs));
754 * Do all searches as the default admin user.
757 ldap_errno = ldap_bind_wrapper(pconn,
758 inst->login, inst->password,
760 if (ldap_errno != RLM_MODULE_OK) {
764 rad_assert(*pconn != NULL);
766 conn->rebound = FALSE;
769 tv.tv_sec = inst->timeout;
771 RDEBUG2("Performing search in '%s' with filter '%s'",
772 search_basedn ? search_basedn : "(null)" ,
776 ldap_errno = ldap_search_ext_s(conn->handle, search_basedn, scope,
777 filter, search_attrs, 0, NULL, NULL,
779 switch (ldap_errno) {
781 case LDAP_NO_SUCH_OBJECT:
784 case LDAP_SERVER_DOWN:
786 ldap_msgfree(*presult);
788 if (reconnect) return -1;
791 conn = fr_connection_reconnect(inst->pool, conn);
792 *pconn = conn; /* tell the caller we have a new connection */
793 if (!conn) return -1;
796 case LDAP_INSUFFICIENT_ACCESS:
797 radlog(L_ERR, "rlm_ldap (%s): Search failed: "
798 "Insufficient access. Check the identity and password "
799 "configuration directives", inst->xlat_name);
800 ldap_msgfree(*presult);
804 exec_trigger(NULL, inst->cs, "modules.ldap.timeout", TRUE);
805 radlog(L_ERR, "rlm_ldap (%s): Search failed: Timed out "
806 "while waiting for server to respond"
807 "Please increase the timeout", inst->xlat_name);
808 ldap_msgfree(*presult);
811 case LDAP_FILTER_ERROR:
812 radlog(L_ERR, "rlm_ldap (%s): Search failed: Bad search "
813 "filter: %s", inst->xlat_name,filter);
814 ldap_msgfree(*presult);
817 case LDAP_TIMELIMIT_EXCEEDED:
818 exec_trigger(NULL, inst->cs, "modules.ldap.timeout", TRUE);
821 case LDAP_UNAVAILABLE:
823 * Reconnect. There's an issue with the socket
826 ldap_get_option(conn->handle, LDAP_OPT_ERROR_NUMBER,
828 radlog(L_ERR, "rlm_ldap (%s): Search failed: %s",
829 inst->xlat_name, ldap_err2string(ldap_errno));
833 ldap_get_option(conn->handle, LDAP_OPT_ERROR_NUMBER,
835 radlog(L_ERR, "rlm_ldap (%s): Search failed: %s",
836 inst->xlat_name, ldap_err2string(ldap_errno));
837 ldap_msgfree(*presult);
841 count = ldap_count_entries(conn->handle, *presult);
843 ldap_msgfree(*presult);
844 RDEBUG("Search returned no results");
850 ldap_msgfree(*presult);
851 RDEBUG("Got ambiguous search result (%d results)", count);
859 /*************************************************************************
861 * Function: ldap_xlat
863 * Purpose: Expand an LDAP URL into a query, and return a string
864 * result from that query.
866 *************************************************************************/
867 static size_t ldap_xlat(void *instance, REQUEST *request, const char *fmt,
868 char *out, size_t freespace)
872 ldap_instance *inst = instance;
873 LDAPURLDesc *ldap_url;
874 LDAPMessage *result = NULL;
875 LDAPMessage *entry = NULL;
881 char buffer[MAX_FILTER_STR_LEN];
883 if (strchr(fmt, '%') != NULL) {
884 if (!radius_xlat(buffer, sizeof(buffer), fmt, request,
885 ldap_escape_func, NULL)) {
887 "rlm_ldap (%s): Unable to create LDAP URL",
896 if (!ldap_is_ldap_url(url)) {
897 radlog(L_ERR, "rlm_ldap (%s): String passed does not look "
898 "like an LDAP URL", inst->xlat_name);
902 if (ldap_url_parse(url, &ldap_url)){
903 radlog(L_ERR, "rlm_ldap (%s): Parsing LDAP URL failed",
909 * Nothing, empty string, "*" string, or got 2 things, die.
911 if (!ldap_url->lud_attrs || !ldap_url->lud_attrs[0] ||
912 !*ldap_url->lud_attrs[0] ||
913 (strcmp(ldap_url->lud_attrs[0], "*") == 0) ||
914 ldap_url->lud_attrs[1]) {
915 radlog(L_ERR, "rlm_ldap (%s): Bad attributes list in LDAP "
916 "URL. URL must specify exactly one attribute to "
923 if (ldap_url->lud_host &&
924 ((strncmp(inst->server, ldap_url->lud_host,
925 strlen(inst->server)) != 0) ||
926 (ldap_url->lud_port != inst->port))) {
927 RDEBUG("Requested server/port is \"%s:%i\"", ldap_url->lud_host,
933 conn = ldap_get_socket(inst);
934 if (!conn) goto free_urldesc;
936 memcpy(&attrs, &ldap_url->lud_attrs, sizeof(attrs));
938 rcode = perform_search(inst, request, &conn, ldap_url->lud_dn,
939 ldap_url->lud_scope, ldap_url->lud_filter, attrs,
943 RDEBUG("Search returned not found", inst->xlat_name);
950 entry = ldap_first_entry(conn->handle, result);
952 ldap_get_option(conn->handle, LDAP_OPT_RESULT_CODE,
954 radlog(L_ERR, "rlm_ldap (%s): Failed retrieving entry: %s",
956 ldap_err2string(ldap_errno));
960 vals = ldap_get_values(conn->handle, entry, ldap_url->lud_attrs[0]);
962 RDEBUG("No \"%s\" attributes found in specified object",
963 inst->xlat_name, ldap_url->lud_attrs[0]);
967 length = strlen(vals[0]);
968 if (length >= freespace){
973 strlcpy(out, vals[0], freespace);
976 ldap_value_free(vals);
978 ldap_msgfree(result);
980 ldap_release_socket(inst, conn);
982 ldap_free_urldesc(ldap_url);
988 static char *get_userdn(LDAP_CONN **pconn, REQUEST *request, int *module_rcode)
992 ldap_instance *inst = (*pconn)->inst;
993 LDAP *handle = (*pconn)->handle;
994 LDAPMessage *result, *entry;
996 static char firstattr[] = "uid";
998 const char *attrs[] = {firstattr, NULL};
999 char filter[MAX_FILTER_STR_LEN];
1000 char basedn[MAX_FILTER_STR_LEN];
1002 *module_rcode = RLM_MODULE_FAIL;
1004 vp = pairfind(request->config_items, PW_LDAP_USERDN, 0);
1005 if (vp) return vp->vp_strvalue;
1007 if (!radius_xlat(filter, sizeof(filter), inst->filter,
1008 request, ldap_escape_func, NULL)) {
1009 radlog(L_ERR, "rlm_ldap (%s): Unable to create filter",
1011 *module_rcode = RLM_MODULE_INVALID;
1015 if (!radius_xlat(basedn, sizeof(basedn), inst->basedn,
1016 request, ldap_escape_func, NULL)) {
1017 radlog(L_ERR, "rlm_ldap (%s): Unable to create basedn",
1019 *module_rcode = RLM_MODULE_INVALID;
1023 rcode = perform_search(inst, request, pconn, basedn, LDAP_SCOPE_SUBTREE,
1024 filter, attrs, &result);
1027 *module_rcode = RLM_MODULE_NOTFOUND;
1033 if ((entry = ldap_first_entry(handle, result)) == NULL) {
1034 ldap_get_option(handle, LDAP_OPT_RESULT_CODE,
1036 radlog(L_ERR, "rlm_ldap (%s): Failed retrieving entry: %s",
1038 ldap_err2string(ldap_errno));
1039 ldap_msgfree(result);
1043 if ((user_dn = ldap_get_dn(handle, entry)) == NULL) {
1044 ldap_get_option(handle, LDAP_OPT_RESULT_CODE,
1046 radlog(L_ERR, "rlm_ldap (%s): ldap_get_dn() failed: %s",
1048 ldap_err2string(ldap_errno));
1050 ldap_msgfree(result);
1054 vp = pairmake("LDAP-UserDn", user_dn, T_OP_EQ);
1056 ldap_memfree(user_dn);
1057 ldap_msgfree(result);
1061 pairadd(&request->config_items, vp);
1062 ldap_memfree(user_dn);
1063 ldap_msgfree(result);
1065 return vp->vp_strvalue;
1069 /*****************************************************************************
1071 * Perform LDAP-Group comparison checking
1073 *****************************************************************************/
1074 static int ldap_groupcmp(void *instance, REQUEST *request,
1075 UNUSED VALUE_PAIR *thing, VALUE_PAIR *check,
1076 UNUSED VALUE_PAIR *check_pairs,
1077 UNUSED VALUE_PAIR **reply_pairs)
1079 ldap_instance *inst = instance;
1080 int i, rcode, found, module_rcode;
1081 LDAPMessage *result = NULL;
1082 LDAPMessage *entry = NULL;
1084 int check_is_dn = FALSE, value_is_dn = FALSE;
1085 static char firstattr[] = "dn";
1086 const char *attrs[] = {firstattr, NULL};
1088 const char *group_attrs[] = {inst->groupmemb_attr, NULL};
1092 char gr_filter[MAX_FILTER_STR_LEN];
1093 char filter[MAX_FILTER_STR_LEN];
1094 char basedn[MAX_FILTER_STR_LEN];
1096 RDEBUG("Searching for user in group \"%s\"", check->vp_strvalue);
1098 if (check->length == 0) {
1099 RDEBUG("Cannot do comparison (group name is empty)");
1103 conn = ldap_get_socket(inst);
1104 if (!conn) return 1;
1107 * This is used in the default membership filter.
1109 user_dn = get_userdn(&conn, request, &module_rcode);
1111 ldap_release_socket(inst, conn);
1115 if (!inst->groupmemb_filter) goto check_attr;
1117 if (!radius_xlat(gr_filter, sizeof(gr_filter),
1118 inst->groupmemb_filter, request, ldap_escape_func,
1120 radlog(L_ERR, "rlm_ldap (%s): Failed creating group filter",
1126 * If it's a DN, use that.
1128 check_is_dn = strchr(check->vp_strvalue,',') == NULL ? FALSE : TRUE;
1131 strlcpy(filter, gr_filter, sizeof(filter));
1132 strlcpy(basedn, check->vp_strvalue, sizeof(basedn));
1134 snprintf(filter, sizeof(filter), "(&(%s=%s)%s)",
1135 inst->groupname_attr,
1136 check->vp_strvalue, gr_filter);
1139 * get_userdn does this, too. Oh well.
1141 if (!radius_xlat(basedn, sizeof(basedn), inst->basedn,
1142 request, ldap_escape_func, NULL)) {
1143 radlog(L_ERR, "rlm_ldap (%s): Failed creating basedn",
1149 rcode = perform_search(inst, request, &conn, basedn, LDAP_SCOPE_SUBTREE,
1150 filter, attrs, &result);
1152 ldap_release_socket(inst, conn);
1153 ldap_msgfree(result);
1155 RDEBUG("User found in group object");
1161 ldap_release_socket(inst, conn);
1165 /* else the search returned -2, for "not found" */
1168 * Else the search returned NOTFOUND. See if we're
1169 * configured to search for group membership using user
1172 if (!inst->groupmemb_attr) {
1173 ldap_release_socket(inst, conn);
1174 RDEBUG("Group object \"%s\" not found, or user is not a member",
1175 check->vp_strvalue);
1180 RDEBUG2("Checking user object membership (%s) attributes",
1181 inst->groupmemb_attr);
1183 snprintf(filter ,sizeof(filter), "(objectclass=*)");
1185 rcode = perform_search(inst, request, &conn, user_dn, LDAP_SCOPE_BASE,
1186 filter, group_attrs, &result);
1189 RDEBUG("Can't check membership attributes, user object "
1192 ldap_release_socket(inst, conn);
1196 entry = ldap_first_entry(conn->handle, result);
1198 ldap_get_option(conn->handle, LDAP_OPT_RESULT_CODE,
1200 radlog(L_ERR, "rlm_ldap (%s): Failed retrieving entry: %s",
1202 ldap_err2string(ldap_errno));
1204 ldap_release_socket(inst, conn);
1205 ldap_msgfree(result);
1209 vals = ldap_get_values(conn->handle, entry, inst->groupmemb_attr);
1211 RDEBUG("No group membership attribute(s) found in user object");
1212 ldap_release_socket(inst, conn);
1213 ldap_msgfree(result);
1218 * Loop over the list of groups the user is a member of,
1219 * looking for a match.
1222 for (i = 0; i < ldap_count_values(vals); i++) {
1223 LDAPMessage *gr_result = NULL;
1225 value_is_dn = strchr(vals[i], ',') == NULL ? FALSE : TRUE;
1227 RDEBUG2("Processing group membership value \"%s\"", vals[i]);
1230 * Both literal group names, do case sensitive comparison
1232 if (!check_is_dn && !value_is_dn) {
1233 if (strcmp(vals[i], check->vp_strvalue) == 0){
1234 RDEBUG("User found (membership value matches "
1245 * Both DNs, do case insensitive comparison
1247 if (check_is_dn && value_is_dn) {
1248 if (strcasecmp(vals[i], check->vp_strvalue) == 0){
1249 RDEBUG("User found (membership DN matches "
1260 * If the value is not a DN, or the check item is a DN
1261 * there's nothing more we can do.
1263 if (!value_is_dn && check_is_dn) continue;
1266 * We have a value which is a DN, and a check item which
1267 * specifies the name of a group, search using the value
1268 * DN for the group, and see if it has a groupname_attr
1269 * which matches our check val.
1271 RDEBUG2("Searching with membership DN and group name");
1273 snprintf(filter,sizeof(filter), "(%s=%s)",
1274 inst->groupname_attr, check->vp_strvalue);
1276 rcode = perform_search(inst, request, &conn, vals[i],
1277 LDAP_SCOPE_BASE, filter, attrs,
1280 ldap_msgfree(gr_result);
1282 /* Error occurred */
1284 ldap_value_free(vals);
1285 ldap_msgfree(result);
1286 ldap_release_socket(inst, conn);
1291 * Either the group DN wasn't found, or it didn't have the
1292 * correct name. Continue looping over the attributes.
1295 ldap_msgfree(gr_result);
1301 RDEBUG("User found (group name in membership DN matches check "
1307 ldap_value_free(vals);
1308 ldap_msgfree(result);
1309 ldap_release_socket(inst, conn);
1312 RDEBUG("User is not a member of specified group");
1320 * Verify that the ldap update section makes sense, and add attribute names
1321 * to array of attributes for efficient querying later.
1323 static int build_attrmap(CONF_SECTION *cs, VALUE_PAIR_MAP **head)
1325 const char *cs_list, *p;
1327 request_refs_t request_def = REQUEST_CURRENT;
1328 pair_lists_t list_def = PAIR_LIST_REQUEST;
1330 CONF_ITEM *ci = cf_sectiontoitem(cs);
1333 unsigned int total = 0;
1334 VALUE_PAIR_MAP **tail, *map;
1340 cs_list = p = cf_section_name2(cs);
1342 request_def = radius_request_name(&p, REQUEST_UNKNOWN);
1343 if (request_def == REQUEST_UNKNOWN) {
1344 cf_log_err(ci, "rlm_ldap: Default request specified "
1345 "in mapping section is invalid");
1349 list_def = fr_str2int(pair_lists, p, PAIR_LIST_UNKNOWN);
1350 if (list_def == PAIR_LIST_UNKNOWN) {
1351 cf_log_err(ci, "rlm_ldap: Default list specified "
1352 "in mapping section is invalid");
1357 for (ci = cf_item_find_next(cs, NULL);
1359 ci = cf_item_find_next(cs, ci)) {
1360 if (total++ == MAX_ATTRMAP) {
1361 cf_log_err(ci, "rlm_ldap: Attribute map size exceeded");
1365 if (!cf_item_is_pair(ci)) {
1366 cf_log_err(ci, "rlm_ldap: Entry is not in \"attribute ="
1367 " ldap-attribute\" format");
1371 cp = cf_itemtopair(ci);
1372 map = radius_cp2map(cp, REQUEST_CURRENT, list_def);
1378 tail = &(map->next);
1384 radius_mapfree(head);
1388 /*****************************************************************************
1390 * Detach from the LDAP server and cleanup internal state.
1392 *****************************************************************************/
1393 static int ldap_detach(void *instance)
1395 ldap_instance *inst = instance;
1397 fr_connection_pool_delete(inst->pool);
1399 if (inst->user_map) {
1400 radius_mapfree(&inst->user_map);
1408 /*************************************************************************
1410 * Function: rlm_ldap_instantiate
1412 * Purpose: Uses section of radiusd config file passed as parameter
1413 * to create an instance of the module.
1415 *************************************************************************/
1416 static int ldap_instantiate(CONF_SECTION * conf, void **instance)
1418 ldap_instance *inst;
1421 inst = rad_malloc(sizeof *inst);
1425 memset(inst, 0, sizeof(*inst));
1428 inst->chase_referrals = 2; /* use OpenLDAP defaults */
1431 if (cf_section_parse(conf, inst, module_config) < 0) {
1436 inst->xlat_name = cf_section_name2(conf);
1437 if (!inst->xlat_name) inst->xlat_name = cf_section_name1(conf);
1439 if (inst->server == NULL) {
1440 radlog(L_ERR, "rlm_ldap (%s): missing 'server' directive",
1447 * Check for URLs. If they're used and the library doesn't
1448 * support them, then complain.
1451 if (ldap_is_ldap_url(inst->server)) {
1452 #ifdef HAVE_LDAP_INITIALIZE
1456 radlog(L_ERR, "rlm_ldap (%s): 'server' directive is in URL "
1457 "form but ldap_initialize() is not available",
1464 /* workaround for servers which support LDAPS but not START TLS */
1465 if (inst->port == LDAPS_PORT || inst->tls_mode) {
1466 inst->tls_mode = LDAP_OPT_X_TLS_HARD;
1471 #if LDAP_SET_REBIND_PROC_ARGS != 3
1473 * The 2-argument rebind doesn't take an instance
1474 * variable. Our rebind function needs the instance
1475 * variable for the username, password, etc.
1477 if (inst->rebind == 1) {
1478 radlog(L_ERR, "rlm_ldap (%s): Cannot use 'rebind' directive "
1479 "as this version of libldap does not support the API "
1480 "that we need", inst->xlat-name);
1487 * Build the attribute map
1489 cs = cf_section_sub_find(conf, "update");
1491 if (build_attrmap(cs, &(inst->user_map)) < 0) {
1498 * Group comparison checks.
1500 paircompare_register(PW_LDAP_GROUP, PW_USER_NAME, ldap_groupcmp, inst);
1501 if (cf_section_name2(conf)) {
1506 snprintf(buffer, sizeof(buffer), "%s-Ldap-Group",
1508 memset(&flags, 0, sizeof(flags));
1510 dict_addattr(buffer, -1, 0, PW_TYPE_STRING, flags);
1511 da = dict_attrbyname(buffer);
1513 radlog(L_ERR, "rlm_ldap (%s): Failed creating "
1514 "attribute %s", inst->xlat_name, buffer);
1519 paircompare_register(da->attr, PW_USER_NAME, ldap_groupcmp,
1523 xlat_register(inst->xlat_name, ldap_xlat, inst);
1526 * Initialize the socket pool.
1528 inst->pool = fr_connection_pool_init(inst->cs, inst,
1541 static int check_access(ldap_instance *inst, REQUEST* request, LDAP_CONN *conn,
1547 vals = ldap_get_values(conn->handle, entry, inst->access_attr);
1549 if (inst->positive_access_attr) {
1550 if (strncmp(vals[0], "FALSE", 5) == 0) {
1551 RDEBUG("Dialup access disabled");
1558 RDEBUG("\"%s\" attribute exists - access denied by"
1559 " default", inst->access_attr);
1562 ldap_value_free(vals);
1564 } else if (inst->positive_access_attr) {
1565 RDEBUG("No %s attribute - access denied by default",
1576 static VALUE_PAIR *ldap_getvalue(REQUEST *request, const VALUE_PAIR_TMPL *dst,
1579 rlm_ldap_result_t *self = ctx;
1580 VALUE_PAIR *head, **tail, *vp;
1589 * Iterate over all the retrieved values,
1590 * don't try and be clever about changing operators
1591 * just use whatever was set in the attribute map.
1593 for (i = 0; i < self->count; i++) {
1594 vp = pairalloc(dst->da);
1597 pairparsevalue(vp, self->values[i]);
1607 static void xlat_attrsfree(const xlat_attrs_t *expanded)
1609 const VALUE_PAIR_MAP *map;
1610 unsigned int total = 0;
1614 for (map = expanded->maps; map != NULL; map = map->next)
1616 memcpy(&name, &(expanded->attrs[total++]), sizeof(name));
1620 if (map->src->do_xlat) {
1627 static int xlat_attrs(REQUEST *request, const VALUE_PAIR_MAP *maps,
1628 xlat_attrs_t *expanded)
1630 const VALUE_PAIR_MAP *map;
1631 unsigned int total = 0;
1636 for (map = maps; map != NULL; map = map->next)
1638 if (map->src->do_xlat) {
1639 buffer = rad_malloc(MAX_ATTR_STR_LEN);
1640 len = radius_xlat(buffer, MAX_ATTR_STR_LEN,
1641 map->src->name, request, NULL, NULL);
1644 RDEBUG("Expansion of LDAP attribute "
1645 "\"%s\" failed", map->src->name);
1647 expanded->attrs[total] = NULL;
1649 xlat_attrsfree(expanded);
1654 expanded->attrs[total++] = buffer;
1656 expanded->attrs[total++] = map->src->name;
1660 expanded->attrs[total] = NULL;
1661 expanded->maps = maps;
1667 /** Convert attribute map into valuepairs
1669 * Use the attribute map built earlier to convert LDAP values into valuepairs
1670 * and insert them into whichever list they need to go into.
1672 * This is *NOT* atomic, but there's no condition in which we should error
1675 static void do_attrmap(UNUSED ldap_instance *inst, REQUEST *request,
1676 LDAP *handle, const xlat_attrs_t *expanded,
1679 const VALUE_PAIR_MAP *map;
1680 unsigned int total = 0;
1682 rlm_ldap_result_t result;
1685 for (map = expanded->maps; map != NULL; map = map->next)
1687 name = expanded->attrs[total++];
1689 result.values = ldap_get_values(handle, entry, name);
1690 if (!result.values) {
1691 RDEBUG2("Attribute \"%s\" not found in LDAP object",
1698 * Find out how many values there are for the
1699 * attribute and extract all of them.
1701 result.count = ldap_count_values(result.values);
1704 * If something bad happened, just skip, this is probably
1705 * a case of the dst being incorrect for the current
1708 if (radius_map2request(request, map, name, ldap_getvalue,
1715 ldap_value_free(result.values);
1720 static void do_check_reply(ldap_instance *inst, REQUEST *request)
1723 * More warning messages for people who can't be bothered
1724 * to read the documentation.
1726 if (inst->expect_password && (debug_flag > 1)) {
1727 if (!pairfind(request->config_items,PW_CLEARTEXT_PASSWORD, 0) &&
1728 !pairfind(request->config_items,
1729 PW_NT_PASSWORD, 0) &&
1730 !pairfind(request->config_items,
1731 PW_USER_PASSWORD, 0) &&
1732 !pairfind(request->config_items,
1733 PW_PASSWORD_WITH_HEADER, 0) &&
1734 !pairfind(request->config_items,
1735 PW_CRYPT_PASSWORD, 0)) {
1736 RDEBUG("WARNING: No \"known good\" password "
1737 "was found in LDAP. Are you sure that "
1738 "the user is configured correctly?");
1744 static void apply_profile(ldap_instance *inst, REQUEST *request,
1745 LDAP_CONN **pconn, const char *profile,
1746 const xlat_attrs_t *expanded)
1749 LDAPMessage *result, *entry;
1751 LDAP *handle = (*pconn)->handle;
1752 char filter[MAX_FILTER_STR_LEN];
1754 if (!profile || !*profile) return;
1756 strlcpy(filter, inst->base_filter, sizeof(filter));
1758 rcode = perform_search(inst, request, pconn, profile, LDAP_SCOPE_BASE,
1759 filter, expanded->attrs, &result);
1763 RDEBUG("Profile \"%s\" not found", profile);
1768 entry = ldap_first_entry(handle, result);
1770 ldap_get_option(handle, LDAP_OPT_RESULT_CODE,
1772 radlog(L_ERR, "rlm_ldap (%s): Failed retrieving entry: %s",
1774 ldap_err2string(ldap_errno));
1779 do_attrmap(inst, request, handle, expanded, entry);
1782 ldap_msgfree(result);
1786 /******************************************************************************
1788 * Function: ldap_authorize
1790 * Purpose: Check if user is authorized for remote access
1792 ******************************************************************************/
1793 static int ldap_authorize(void *instance, REQUEST * request)
1796 int module_rcode = RLM_MODULE_OK;
1797 ldap_instance *inst = instance;
1802 LDAPMessage *result, *entry;
1804 char filter[MAX_FILTER_STR_LEN];
1805 char basedn[MAX_FILTER_STR_LEN];
1806 xlat_attrs_t expanded; /* faster that mallocing every time */
1808 if (!request->username) {
1809 RDEBUG2("Attribute \"User-Name\" is required for "
1811 return RLM_MODULE_NOOP;
1815 * Check for valid input, zero length names not permitted
1817 if (request->username->length == 0) {
1818 RDEBUG2("Zero length username not permitted");
1819 return RLM_MODULE_INVALID;
1822 if (!radius_xlat(filter, sizeof(filter), inst->filter,
1823 request, ldap_escape_func, NULL)) {
1824 radlog(L_ERR, "rlm_ldap (%s): Failed creating filter",
1826 return RLM_MODULE_INVALID;
1829 if (!radius_xlat(basedn, sizeof(basedn), inst->basedn,
1830 request, ldap_escape_func, NULL)) {
1831 radlog(L_ERR, "rlm_ldap (%s): Failed creating basedn",
1833 return RLM_MODULE_INVALID;
1836 conn = ldap_get_socket(inst);
1837 if (!conn) return RLM_MODULE_FAIL;
1839 if (xlat_attrs(request, inst->user_map, &expanded) < 0) {
1840 return RLM_MODULE_FAIL;
1843 rcode = perform_search(inst, request, &conn, basedn,
1844 LDAP_SCOPE_SUBTREE, filter, expanded.attrs,
1849 module_failure_msg(request,
1850 "rlm_ldap (%s): User object not "
1854 RDEBUG("User object not found", inst->xlat_name);
1856 module_rcode = RLM_MODULE_NOTFOUND;
1863 entry = ldap_first_entry(conn->handle, result);
1865 ldap_get_option(conn->handle, LDAP_OPT_RESULT_CODE,
1867 radlog(L_ERR, "rlm_ldap (%s): Failed retrieving entry: %s",
1869 ldap_err2string(ldap_errno));
1874 user_dn = ldap_get_dn(conn->handle, entry);
1876 ldap_get_option(conn->handle, LDAP_OPT_RESULT_CODE,
1878 radlog(L_ERR, "rlm_ldap (%s): ldap_get_dn() failed: %s",
1880 ldap_err2string(ldap_errno));
1884 RDEBUG2("User found at DN \"%s\"", user_dn);
1886 * Adding attribute containing the Users' DN.
1888 pairadd(&request->config_items,
1889 pairmake("Ldap-UserDn", user_dn, T_OP_EQ));
1893 * We already have a Cleartext-Password. Skip edir.
1895 if (inst->edir && pairfind(request->config_items,
1896 PW_CLEARTEXT_PASSWORD, 0)) {
1901 * Retrieve Universal Password if we use eDirectory
1908 bufsize = sizeof(buffer);
1910 /* retrive universal password */
1911 res = nmasldap_get_password(conn->handle, user_dn,
1914 RDEBUG2("Failed to retrieve eDirectory password. Check your configuration !");
1915 module_rcode = RLM_MODULE_NOOP;
1919 /* add Cleartext-Password attribute to the request */
1920 vp = radius_paircreate(request, &request->config_items,
1921 PW_CLEARTEXT_PASSWORD, 0, PW_TYPE_STRING);
1922 strlcpy(vp->vp_strvalue, buffer, sizeof(vp->vp_strvalue));
1923 vp->length = strlen(vp->vp_strvalue);
1925 RDEBUG2("Added eDirectory password in check items as %s = %s",
1926 vp->name, vp->vp_strvalue);
1932 ldap_memfree(user_dn);
1937 if (inst->access_attr) {
1938 if (check_access(inst, request, conn, entry) < 0) {
1939 module_rcode = RLM_MODULE_USERLOCK;
1945 * Apply ONE user profile, or a default user profile.
1947 vp = pairfind(request->config_items, PW_USER_PROFILE, 0);
1948 if (vp || inst->default_profile) {
1949 char *profile = inst->default_profile;
1951 if (vp) profile = vp->vp_strvalue;
1953 apply_profile(inst, request, &conn, profile, &expanded);
1957 * Apply a SET of user profiles.
1959 if (inst->profile_attr) {
1960 vals = ldap_get_values(conn->handle, entry, inst->profile_attr);
1964 for (i = 0; (vals[i] != NULL) && (*vals[i] != '\0');
1966 apply_profile(inst, request, &conn, vals[i],
1970 ldap_value_free(vals);
1974 if (inst->user_map) {
1975 do_attrmap(inst, request, conn->handle, &expanded, entry);
1976 do_check_reply(inst, request);
1980 xlat_attrsfree(&expanded);
1981 ldap_msgfree(result);
1983 ldap_release_socket(inst, conn);
1985 return module_rcode;
1989 /*****************************************************************************
1991 * Function: ldap_authenticate
1993 * Purpose: Check the user's password against ldap database
1995 *****************************************************************************/
1996 static int ldap_authenticate(void *instance, REQUEST * request)
1999 const char *user_dn;
2000 ldap_instance *inst = instance;
2004 * Ensure that we're being passed a plain-text password, and not
2008 if (!request->username) {
2009 radlog(L_AUTH, "rlm_ldap (%s): Attribute \"User-Name\" is "
2010 "required for authentication", inst->xlat_name);
2011 return RLM_MODULE_INVALID;
2014 if (!request->password) {
2015 radlog(L_AUTH, "rlm_ldap (%s): Attribute \"User-Password\" "
2016 "is required for authentication.", inst->xlat_name);
2017 RDEBUG2(" You have set \"Auth-Type := LDAP\" somewhere.");
2018 RDEBUG2(" *********************************************");
2019 RDEBUG2(" * THAT CONFIGURATION IS WRONG. DELETE IT. ");
2020 RDEBUG2(" * YOU ARE PREVENTING THE SERVER FROM WORKING.");
2021 RDEBUG2(" *********************************************");
2022 return RLM_MODULE_INVALID;
2025 if (request->password->attribute != PW_USER_PASSWORD) {
2026 radlog(L_AUTH, "rlm_ldap (%s): Attribute \"User-Password\" "
2027 "is required for authentication. Cannot use \"%s\".",
2028 inst->xlat_name, request->password->name);
2029 return RLM_MODULE_INVALID;
2032 if (request->password->length == 0) {
2033 module_failure_msg(request,
2034 "rlm_ldap (%s): Empty password supplied",
2036 return RLM_MODULE_INVALID;
2039 conn = ldap_get_socket(inst);
2040 if (!conn) return RLM_MODULE_FAIL;
2042 RDEBUG("Login attempt by \"%s\" with password \"%s\"",
2043 request->username->vp_strvalue, request->password->vp_strvalue);
2046 * Get the DN by doing a search.
2048 user_dn = get_userdn(&conn, request, &module_rcode);
2050 ldap_release_socket(inst, conn);
2051 return module_rcode;
2057 conn->rebound = TRUE;
2058 module_rcode = ldap_bind_wrapper(&conn, user_dn,
2059 request->password->vp_strvalue,
2061 if (module_rcode == RLM_MODULE_OK) {
2062 RDEBUG("Bind as user \"%s\" was successful", user_dn);
2065 ldap_release_socket(inst, conn);
2066 return module_rcode;
2071 /*****************************************************************************
2073 * Function: ldap_postauth
2075 * Purpose: Check the user's password against ldap database
2077 *****************************************************************************/
2078 static int ldap_postauth(void *instance, REQUEST * request)
2081 const char *user_dn;
2082 ldap_instance *inst = instance;
2087 * Ensure that we have a username and a
2088 * Cleartext-Password in the request
2090 if (!request->username) {
2091 radlog(L_AUTH, "rlm_ldap (%s): Attribute \"User-Name\" is "
2092 "required for authentication", inst->xlat_name);
2093 return RLM_MODULE_INVALID;
2096 vp = pairfind(request->config_items, PW_CLEARTEXT_PASSWORD, 0);
2098 radlog(L_AUTH, "rlm_ldap (%s): Attribute \"Cleartext-Password\" "
2099 "is required for authentication.", inst->xlat_name);
2100 return RLM_MODULE_INVALID;
2103 if (!*vp->vp_strvalue) {
2104 radlog(L_AUTH, "rlm_ldap (%s): Attribute \"Cleartext-Password\" "
2105 "is empty.", inst->xlat_name);
2106 return RLM_MODULE_INVALID;
2109 conn = ldap_get_socket(inst);
2110 if (!conn) return RLM_MODULE_FAIL;
2112 RDEBUG("Login attempt by \"%s\" with password \"%s\"",
2113 request->username->vp_strvalue, vp->vp_strvalue);
2116 * Get the DN by doing a search.
2118 user_dn = get_userdn(&conn, request, &module_rcode);
2120 ldap_release_socket(inst, conn);
2121 return module_rcode;
2127 conn->rebound = TRUE;
2128 module_rcode = ldap_bind_wrapper(&conn, user_dn,
2131 if (module_rcode == RLM_MODULE_OK) {
2132 RDEBUG("Bind as user \"%s\" was successful", user_dn);
2135 ldap_release_socket(inst, conn);
2136 return module_rcode;
2140 /* globally exported name */
2141 module_t rlm_ldap = {
2144 RLM_TYPE_THREAD_SAFE, /* type: reserved */
2145 ldap_instantiate, /* instantiation */
2146 ldap_detach, /* detach */
2148 ldap_authenticate, /* authentication */
2149 ldap_authorize, /* authorization */
2150 NULL, /* preaccounting */
2151 NULL, /* accounting */
2152 NULL, /* checksimul */
2153 NULL, /* pre-proxy */
2154 NULL, /* post-proxy */
2156 ldap_postauth /* post-auth */
2158 NULL /* post-auth */