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 2004,2006 The FreeRADIUS Server Project.
21 #include <freeradius-devel/ident.h>
24 #include <freeradius-devel/radiusd.h>
25 #include <freeradius-devel/modules.h>
26 #include <freeradius-devel/rad_assert.h>
34 #ifndef HAVE_PTHREAD_H
36 * This is a lot simpler than putting ifdef's around
37 * every use of the pthread functions.
39 #define pthread_mutex_lock(a)
40 #define pthread_mutex_trylock(a) (0)
41 #define pthread_mutex_unlock(a)
42 #define pthread_mutex_init(a,b)
43 #define pthread_mutex_destroy(a)
49 #define MAX_FILTER_STR_LEN 1024
53 * These are used in case ldap_search returns LDAP_SERVER_DOWN
54 * In that case we do conn->failed_conns++ and then check it:
55 * If conn->failed_conns <= MAX_FAILED_CONNS_START then we try
57 * conn->failed_conns is also checked on entrance in perform_search:
58 * If conn->failed_conns > MAX_FAILED_CONNS_START then we don't
59 * try to do anything and we just do conn->failed_conns++ and
60 * return RLM_MODULE_FAIL
61 * if conn->failed_conns >= MAX_FAILED_CONNS_END then we give it
62 * another chance and we set it to MAX_FAILED_CONNS_RESTART and
66 * We are assuming that the majority of the LDAP_SERVER_DOWN cases
67 * will either be an ldap connection timeout or a temporary ldap
69 * As a result we make a few attempts to reconnect hoping that the problem
70 * will soon go away. If it does not go away then we just return
71 * RLM_MODULE_FAIL on entrance in perform_search until conn->failed_conns
72 * gets to MAX_FAILED_CONNS_END. After that we give it one more chance by
73 * going back to MAX_FAILED_CONNS_RESTART
77 #define MAX_FAILED_CONNS_END 20
78 #define MAX_FAILED_CONNS_RESTART 4
79 #define MAX_FAILED_CONNS_START 5
81 #ifdef NOVELL_UNIVERSAL_PASSWORD
83 /* Universal Password Length */
84 #define UNIVERSAL_PASS_LEN 256
86 int nmasldap_get_password(
89 size_t *pwdSize, /* in bytes */
93 /* linked list of mappings between RADIUS attributes and LDAP attributes */
98 struct TLDAP_RADIUS* next;
100 typedef struct TLDAP_RADIUS TLDAP_RADIUS;
102 typedef struct ldap_conn {
107 #ifdef HAVE_PTHREAD_H
108 pthread_mutex_t mutex;
116 struct timeval net_timeout;
117 struct timeval timeout;
132 char *default_profile;
138 char *dictionary_mapping;
139 char *groupname_attr;
140 char *groupmemb_filt;
141 char *groupmemb_attr;
143 TLDAP_RADIUS *check_item_map;
144 TLDAP_RADIUS *reply_item_map;
147 LDAP_CONN *apc_conns;
149 int ldap_debug; /* Debug flag for LDAP SDK */
150 char *xlat_name; /* name used to xlat */
151 char *tls_cacertfile;
156 char *tls_require_cert;
158 int edir_account_policy_check;
163 /* The default setting for TLS Certificate Verification */
164 #define TLS_DEFAULT_VERIFY "allow"
166 static CONF_PARSER tls_config[] = {
167 {"start_tls", PW_TYPE_BOOLEAN,
168 offsetof(ldap_instance,start_tls), NULL, "no"},
169 {"cacertfile", PW_TYPE_FILENAME,
170 offsetof(ldap_instance,tls_cacertfile), NULL, NULL},
171 {"cacertdir", PW_TYPE_FILENAME,
172 offsetof(ldap_instance,tls_cacertdir), NULL, NULL},
173 {"certfile", PW_TYPE_FILENAME,
174 offsetof(ldap_instance,tls_certfile), NULL, NULL},
175 {"keyfile", PW_TYPE_FILENAME,
176 offsetof(ldap_instance,tls_keyfile), NULL, NULL},
177 {"randfile", PW_TYPE_STRING_PTR, /* OK if it changes on HUP */
178 offsetof(ldap_instance,tls_randfile), NULL, NULL},
179 {"require_cert", PW_TYPE_STRING_PTR,
180 offsetof(ldap_instance,tls_require_cert), NULL, TLS_DEFAULT_VERIFY},
181 { NULL, -1, 0, NULL, NULL }
184 static const CONF_PARSER module_config[] = {
185 {"server", PW_TYPE_STRING_PTR,
186 offsetof(ldap_instance,server), NULL, "localhost"},
187 {"port", PW_TYPE_INTEGER,
188 offsetof(ldap_instance,port), NULL, "389"},
189 {"password", PW_TYPE_STRING_PTR,
190 offsetof(ldap_instance,password), NULL, ""},
191 {"identity", PW_TYPE_STRING_PTR,
192 offsetof(ldap_instance,login), NULL, ""},
197 /* wait forever on network activity */
198 {"net_timeout", PW_TYPE_INTEGER,
199 offsetof(ldap_instance,net_timeout.tv_sec), NULL, "10"},
200 /* wait forever for search results */
201 {"timeout", PW_TYPE_INTEGER,
202 offsetof(ldap_instance,timeout.tv_sec), NULL, "20"},
203 /* allow server unlimited time for search (server-side limit) */
204 {"timelimit", PW_TYPE_INTEGER,
205 offsetof(ldap_instance,timelimit), NULL, "20"},
208 * TLS configuration The first few are here for backwards
209 * compatibility. The last is the new subsection.
211 {"tls_mode", PW_TYPE_BOOLEAN,
212 offsetof(ldap_instance,tls_mode), NULL, "no"},
214 {"start_tls", PW_TYPE_BOOLEAN,
215 offsetof(ldap_instance,start_tls), NULL, "no"},
216 {"tls_cacertfile", PW_TYPE_FILENAME,
217 offsetof(ldap_instance,tls_cacertfile), NULL, NULL},
218 {"tls_cacertdir", PW_TYPE_FILENAME,
219 offsetof(ldap_instance,tls_cacertdir), NULL, NULL},
220 {"tls_certfile", PW_TYPE_FILENAME,
221 offsetof(ldap_instance,tls_certfile), NULL, NULL},
222 {"tls_keyfile", PW_TYPE_FILENAME,
223 offsetof(ldap_instance,tls_keyfile), NULL, NULL},
224 {"tls_randfile", PW_TYPE_STRING_PTR, /* OK if it changes on HUP */
225 offsetof(ldap_instance,tls_randfile), NULL, NULL},
226 {"tls_require_cert", PW_TYPE_STRING_PTR,
227 offsetof(ldap_instance,tls_require_cert), NULL, TLS_DEFAULT_VERIFY},
228 { "tls", PW_TYPE_SUBSECTION, 0, NULL, (const void *) tls_config },
233 {"basedn", PW_TYPE_STRING_PTR,
234 offsetof(ldap_instance,basedn), NULL, "o=notexist"},
235 {"filter", PW_TYPE_STRING_PTR,
236 offsetof(ldap_instance,filter), NULL, "(uid=%u)"},
237 {"base_filter", PW_TYPE_STRING_PTR,
238 offsetof(ldap_instance,base_filter), NULL, "(objectclass=radiusprofile)"},
239 {"default_profile", PW_TYPE_STRING_PTR,
240 offsetof(ldap_instance,default_profile), NULL, NULL},
241 {"profile_attribute", PW_TYPE_STRING_PTR,
242 offsetof(ldap_instance,profile_attr), NULL, NULL},
245 * Getting passwords from the database
247 {"password_header", PW_TYPE_STRING_PTR,
248 offsetof(ldap_instance,passwd_hdr), NULL, NULL},
249 {"password_attribute", PW_TYPE_STRING_PTR,
250 offsetof(ldap_instance,passwd_attr), NULL, NULL},
251 {"auto_header", PW_TYPE_BOOLEAN,
252 offsetof(ldap_instance,auto_header), NULL, "no"},
257 /* LDAP attribute name that controls remote access */
258 {"access_attr", PW_TYPE_STRING_PTR,
259 offsetof(ldap_instance,access_attr), NULL, NULL},
260 {"access_attr_used_for_allow", PW_TYPE_BOOLEAN,
261 offsetof(ldap_instance,default_allow), NULL, "yes"},
264 * Group checks. These could probably be done
265 * via dynamic xlat's.
267 {"groupname_attribute", PW_TYPE_STRING_PTR,
268 offsetof(ldap_instance,groupname_attr), NULL, "cn"},
269 {"groupmembership_filter", PW_TYPE_STRING_PTR,
270 offsetof(ldap_instance,groupmemb_filt), NULL, "(|(&(objectClass=GroupOfNames)(member=%{Ldap-UserDn}))(&(objectClass=GroupOfUniqueNames)(uniquemember=%{Ldap-UserDn})))"},
271 {"groupmembership_attribute", PW_TYPE_STRING_PTR,
272 offsetof(ldap_instance,groupmemb_attr), NULL, NULL},
274 /* file with mapping between LDAP and RADIUS attributes */
275 {"dictionary_mapping", PW_TYPE_FILENAME,
276 offsetof(ldap_instance,dictionary_mapping), NULL, "${confdir}/ldap.attrmap"},
279 * Debugging flags to the server
281 {"ldap_debug", PW_TYPE_INTEGER,
282 offsetof(ldap_instance,ldap_debug), NULL, "0x0000"},
283 {"ldap_connections_number", PW_TYPE_INTEGER,
284 offsetof(ldap_instance,num_conns), NULL, "5"},
285 {"compare_check_items", PW_TYPE_BOOLEAN,
286 offsetof(ldap_instance,do_comp), NULL, "no"},
287 {"do_xlat", PW_TYPE_BOOLEAN,
288 offsetof(ldap_instance,do_xlat), NULL, "yes"},
294 {"edir_account_policy_check", PW_TYPE_BOOLEAN,
295 offsetof(ldap_instance,edir_account_policy_check), NULL, "yes"},
298 {"set_auth_type", PW_TYPE_BOOLEAN, offsetof(ldap_instance,set_auth_type), NULL, "yes"},
299 {NULL, -1, 0, NULL, NULL}
302 #define ld_valid ld_options.ldo_valid
303 #define LDAP_VALID_SESSION 0x2
304 #define LDAP_VALID(ld) ( (ld)->ld_valid == LDAP_VALID_SESSION )
307 static void fieldcpy(char *, char **);
309 static VALUE_PAIR *ldap_pairget(LDAP *, LDAPMessage *, TLDAP_RADIUS *,VALUE_PAIR **,int);
310 static int ldap_groupcmp(void *, REQUEST *, VALUE_PAIR *, VALUE_PAIR *, VALUE_PAIR *, VALUE_PAIR **);
311 static int ldap_xlat(void *, REQUEST *, char *, char *, size_t, RADIUS_ESCAPE_STRING);
312 static LDAP *ldap_connect(void *instance, const char *, const char *, int, int *, char **);
313 static int read_mappings(ldap_instance* inst);
315 static inline int ldap_get_conn(LDAP_CONN *conns,LDAP_CONN **ret,void *instance)
317 ldap_instance *inst = instance;
320 for(i=0;i<inst->num_conns;i++){
321 DEBUG("rlm_ldap: ldap_get_conn: Checking Id: %d",i);
322 if ((pthread_mutex_trylock(&conns[i].mutex) == 0)) {
323 if (conns[i].locked == 1) {
324 /* connection is already being used */
325 pthread_mutex_unlock(&(conns[i].mutex));
328 /* found an unused connection */
331 DEBUG("rlm_ldap: ldap_get_conn: Got Id: %d",i);
339 static inline void ldap_release_conn(int i, LDAP_CONN *conns)
341 DEBUG("rlm_ldap: ldap_release_conn: Release Id: %d",i);
343 pthread_mutex_unlock(&(conns[i].mutex));
346 /*************************************************************************
348 * Function: rlm_ldap_instantiate
350 * Purpose: Uses section of radiusd config file passed as parameter
351 * to create an instance of the module.
353 *************************************************************************/
355 ldap_instantiate(CONF_SECTION * conf, void **instance)
360 int reply_map_num = 0;
361 int check_map_num = 0;
362 int att_map[3] = {0,0,0};
365 const char *xlat_name;
367 inst = rad_malloc(sizeof *inst);
371 memset(inst, 0, sizeof(*inst));
373 if (cf_section_parse(conf, inst, module_config) < 0) {
378 if (inst->server == NULL) {
379 radlog(L_ERR, "rlm_ldap: missing 'server' directive.");
380 free(inst); /* FIXME: detach */
384 if (ldap_is_ldap_url(inst->server)){
385 #ifdef HAVE_LDAP_INITIALIZE
389 radlog(L_ERR, "rlm_ldap: 'server' directive is in URL form but ldap_initialize() is not available.");
390 free(inst); /* FIXME: detach */
395 inst->timeout.tv_usec = 0;
396 inst->net_timeout.tv_usec = 0;
397 /* workaround for servers which support LDAPS but not START TLS */
398 if(inst->port == LDAPS_PORT || inst->tls_mode)
399 inst->tls_mode = LDAP_OPT_X_TLS_HARD;
402 inst->reply_item_map = NULL;
403 inst->check_item_map = NULL;
405 inst->failed_conns = 0;
407 DEBUG("rlm_ldap: Registering ldap_groupcmp for Ldap-Group");
408 paircompare_register(PW_LDAP_GROUP, PW_USER_NAME, ldap_groupcmp, inst);
409 memset(&flags, 0, sizeof(flags));
411 xlat_name = cf_section_name2(conf);
412 if (xlat_name != NULL){
417 * Allocate room for <instance>-Ldap-Group
419 group_name = rad_malloc((strlen(xlat_name) + 1 + 11) * sizeof(char));
420 sprintf(group_name,"%s-Ldap-Group",xlat_name);
421 DEBUG("rlm_ldap: Creating new attribute %s",group_name);
422 dict_addattr(group_name, 0, PW_TYPE_STRING, -1, flags);
423 dattr = dict_attrbyname(group_name);
425 radlog(L_ERR, "rlm_ldap: Failed to create attribute %s",group_name);
427 free(inst); /* FIXME: detach */
430 DEBUG("rlm_ldap: Registering ldap_groupcmp for %s",group_name);
431 paircompare_register(dattr->attr, PW_USER_NAME, ldap_groupcmp, inst);
435 xlat_name = cf_section_name1(conf);
436 rad_assert(xlat_name != NULL); /* or all hell breaks loose */
438 inst->xlat_name = strdup(xlat_name);
439 DEBUG("rlm_ldap: Registering ldap_xlat with xlat_name %s",xlat_name);
440 xlat_register(xlat_name,ldap_xlat,inst);
443 * Over-ride set_auth_type if there's no Auth-Type of our name.
444 * This automagically catches the case where LDAP is listed
445 * in "authorize", but not "authenticate".
447 if (inst->set_auth_type) {
448 DICT_VALUE *dv = dict_valbyname(PW_AUTH_TYPE, xlat_name);
450 DEBUG2("rlm_ldap: Over-riding set_auth_type, as we're not listed in the \"authenticate\" section.");
451 inst->set_auth_type = 0;
453 } /* else no need to look up the value */
457 * (LDAP_Instance, V1) attribute-value pair in the config
458 * items list means that the 'authorize' method of the
459 * instance 'V1' of the LDAP module has processed this
462 dict_addattr("LDAP-Instance", 0, PW_TYPE_STRING, -1, flags);
465 * ('eDir-APC', '1') in config items list
466 * Do not perform eDirectory account policy check (APC)
468 * ('eDir-APC', '2') in config items list
469 * Perform eDirectory APC
471 * ('eDir-APC', '3') in config items list
472 * eDirectory APC has been completed
474 dict_addattr("eDir-APC", 0, PW_TYPE_STRING, -1, flags);
477 if (inst->num_conns <= 0){
478 radlog(L_ERR, "rlm_ldap: Invalid ldap connections number passed.");
479 free(inst); /* FIXME: detach */
482 inst->conns = malloc(sizeof(*(inst->conns))*inst->num_conns);
483 if (inst->conns == NULL){
484 radlog(L_ERR, "rlm_ldap: Could not allocate memory. Aborting.");
485 free(inst); /* FIXME: detach */
488 for(i = 0; i < inst->num_conns; i++){
489 inst->conns[i].bound = 0;
490 inst->conns[i].locked = 0;
491 inst->conns[i].failed_conns = 0;
492 inst->conns[i].ld = NULL;
493 pthread_mutex_init(&inst->conns[i].mutex, NULL);
498 * 'inst->apc_conns' is a separate connection pool to be
499 * used for performing eDirectory account policy check in
500 * the 'postauth' method. This avoids changing the
501 * (RADIUS server) credentials associated with the
502 * 'inst->conns' connection pool.
504 inst->apc_conns = malloc(sizeof(*(inst->apc_conns))*inst->num_conns);
505 if (inst->apc_conns == NULL){
506 radlog(L_ERR, "rlm_ldap: Could not allocate memory. Aborting.");
507 free(inst); /* FIXME: detach */
510 for(i = 0; i < inst->num_conns; i++){
511 inst->apc_conns[i].bound = 0;
512 inst->apc_conns[i].locked = 0;
513 inst->apc_conns[i].failed_conns = 0;
514 inst->apc_conns[i].ld = NULL;
515 pthread_mutex_init(&inst->apc_conns[i].mutex, NULL);
519 if (read_mappings(inst) != 0) {
520 radlog(L_ERR, "rlm_ldap: Reading dictionary mappings from file %s failed",
521 inst->dictionary_mapping);
522 free(inst); /* FIXME: detach */
525 if ((inst->check_item_map == NULL) &&
526 (inst->reply_item_map == NULL)) {
527 radlog(L_ERR, "rlm_ldap: dictionary mappings file %s did not contain any mappings",
528 inst->dictionary_mapping);
529 free(inst); /* FIXME: detach */
533 pair = inst->check_item_map;
538 check_map_num = (atts_num - 1);
539 pair = inst->reply_item_map;
544 reply_map_num = (atts_num - 1);
545 if (inst->profile_attr)
547 if (inst->passwd_attr)
549 if (inst->access_attr)
551 inst->atts = (char **)malloc(sizeof(char *)*(atts_num + 1));
552 if (inst->atts == NULL){
553 radlog(L_ERR, "rlm_ldap: Could not allocate memory. Aborting.");
554 free(inst); /* FIXME: detach */
557 pair = inst->check_item_map;
559 pair = inst->reply_item_map;
560 for(i=0;i<atts_num;i++){
561 if (i <= check_map_num ){
562 inst->atts[i] = pair->attr;
563 if (i == check_map_num)
564 pair = inst->reply_item_map;
568 else if (i <= reply_map_num){
569 inst->atts[i] = pair->attr;
573 if (inst->profile_attr && !att_map[0]){
574 inst->atts[i] = inst->profile_attr;
577 else if (inst->passwd_attr && !att_map[1]){
578 inst->atts[i] = inst->passwd_attr;
581 else if (inst->access_attr && !att_map[2]){
582 inst->atts[i] = inst->access_attr;
587 inst->atts[atts_num] = NULL;
589 DEBUG("conns: %p",inst->conns);
599 * read_mappings(...) reads a ldap<->radius mappings file to
600 * inst->reply_item_map and inst->check_item_map
602 #define MAX_LINE_LEN 160
603 #define GENERIC_ATTRIBUTE_ID "$GENERIC$"
606 read_mappings(ldap_instance* inst)
612 * All buffers are of MAX_LINE_LEN so we can use sscanf
613 * without being afraid of buffer overflows
615 char buf[MAX_LINE_LEN], itemType[MAX_LINE_LEN];
616 char radiusAttribute[MAX_LINE_LEN], ldapAttribute[MAX_LINE_LEN];
619 char opstring[MAX_LINE_LEN];
621 /* open the mappings file for reading */
623 filename = inst->dictionary_mapping;
624 DEBUG("rlm_ldap: reading ldap<->radius mappings from file %s", filename);
625 mapfile = fopen(filename, "r");
627 if (mapfile == NULL) {
628 radlog(L_ERR, "rlm_ldap: Opening file %s failed", filename);
629 return -1; /* error */
633 * read file line by line. Note that if line length
634 * exceeds MAX_LINE_LEN, line numbers will be mixed up
638 while (fgets(buf, sizeof buf, mapfile)!=NULL) {
646 ptr = strchr(buf, '#');
650 if (buf[0] == 0) continue;
652 /* extract tokens from the string */
653 token_count = sscanf(buf, "%s %s %s %s",
654 itemType, radiusAttribute,
655 ldapAttribute, opstring);
657 if (token_count <= 0) /* no tokens */
660 if ((token_count < 3) || (token_count > 4)) {
661 radlog(L_ERR, "rlm_ldap: Skipping %s line %i: %s",
662 filename, linenumber, buf);
663 radlog(L_ERR, "rlm_ldap: Expected 3 to 4 tokens "
664 "(Item type, RADIUS Attribute and LDAP Attribute) but found only %i", token_count);
668 if (token_count == 3) {
669 operator = T_OP_INVALID; /* use defaults */
674 operator = gettoken(&ptr, buf, sizeof(buf));
675 if ((operator < T_OP_ADD) || (operator > T_OP_CMP_EQ)) {
676 radlog(L_ERR, "rlm_ldap: file %s: skipping line %i: unknown or invalid operator %s",
677 filename, linenumber, opstring);
682 /* create new TLDAP_RADIUS list node */
683 pair = rad_malloc(sizeof(*pair));
685 pair->attr = strdup(ldapAttribute);
686 pair->radius_attr = strdup(radiusAttribute);
687 pair->operator = operator;
689 if ( (pair->attr == NULL) || (pair->radius_attr == NULL) ) {
690 radlog(L_ERR, "rlm_ldap: Out of memory");
691 if (pair->attr) free(pair->attr);
692 if (pair->radius_attr) free(pair->radius_attr);
698 /* push node to correct list */
699 if (strcasecmp(itemType, "checkItem") == 0) {
700 pair->next = inst->check_item_map;
701 inst->check_item_map = pair;
702 } else if (strcasecmp(itemType, "replyItem") == 0) {
703 pair->next = inst->reply_item_map;
704 inst->reply_item_map = pair;
706 radlog(L_ERR, "rlm_ldap: file %s: skipping line %i: unknown itemType %s",
707 filename, linenumber, itemType);
709 free(pair->radius_attr);
714 DEBUG("rlm_ldap: LDAP %s mapped to RADIUS %s",
715 pair->attr, pair->radius_attr);
720 return 0; /* success */
723 static int perform_search(void *instance, LDAP_CONN *conn,
724 char *search_basedn, int scope, char *filter,
725 char **attrs, LDAPMessage ** result)
727 int res = RLM_MODULE_OK;
729 ldap_instance *inst = instance;
730 int search_retry = 0;
735 radlog(L_ERR, "rlm_ldap: NULL connection handle passed");
736 return RLM_MODULE_FAIL;
738 if (conn->failed_conns > MAX_FAILED_CONNS_START){
739 conn->failed_conns++;
740 if (conn->failed_conns >= MAX_FAILED_CONNS_END){
741 conn->failed_conns = MAX_FAILED_CONNS_RESTART;
746 if (!conn->bound || conn->ld == NULL) {
747 DEBUG2("rlm_ldap: attempting LDAP reconnection");
749 DEBUG2("rlm_ldap: closing existing LDAP connection");
750 ldap_unbind_s(conn->ld);
752 if ((conn->ld = ldap_connect(instance, inst->login,
753 inst->password, 0, &res, NULL)) == NULL) {
754 radlog(L_ERR, "rlm_ldap: (re)connection attempt failed");
755 if (search_retry == 0)
756 conn->failed_conns++;
757 return (RLM_MODULE_FAIL);
760 conn->failed_conns = 0;
762 DEBUG2("rlm_ldap: performing search in %s, with filter %s",
763 search_basedn ? search_basedn : "(null)" , filter);
764 switch (ldap_search_st(conn->ld, search_basedn, scope, filter,
765 attrs, 0, &(inst->timeout), result)) {
767 case LDAP_NO_SUCH_OBJECT:
769 case LDAP_SERVER_DOWN:
770 radlog(L_ERR, "rlm_ldap: ldap_search() failed: LDAP connection lost.");
771 conn->failed_conns++;
772 if (search_retry == 0){
773 if (conn->failed_conns <= MAX_FAILED_CONNS_START){
774 radlog(L_INFO, "rlm_ldap: Attempting reconnect");
777 ldap_msgfree(*result);
781 ldap_msgfree(*result);
782 return RLM_MODULE_FAIL;
783 case LDAP_INSUFFICIENT_ACCESS:
784 radlog(L_ERR, "rlm_ldap: ldap_search() failed: Insufficient access. Check the identity and password configuration directives.");
785 ldap_msgfree(*result);
786 return RLM_MODULE_FAIL;
788 radlog(L_ERR, "rlm_ldap: ldap_search() failed: Timed out while waiting for server to respond. Please increase the timeout.");
789 ldap_msgfree(*result);
790 return RLM_MODULE_FAIL;
791 case LDAP_FILTER_ERROR:
792 radlog(L_ERR, "rlm_ldap: ldap_search() failed: Bad search filter: %s",filter);
793 ldap_msgfree(*result);
794 return RLM_MODULE_FAIL;
795 case LDAP_TIMELIMIT_EXCEEDED:
797 case LDAP_UNAVAILABLE:
798 /* We don't need to reconnect in these cases so we don't set conn->bound */
799 ldap_get_option(conn->ld, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
800 radlog(L_ERR, "rlm_ldap: ldap_search() failed: %s",
801 ldap_err2string(ldap_errno));
802 ldap_msgfree(*result);
803 return (RLM_MODULE_FAIL);
805 ldap_get_option(conn->ld, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
806 radlog(L_ERR, "rlm_ldap: ldap_search() failed: %s",
807 ldap_err2string(ldap_errno));
809 ldap_msgfree(*result);
810 return (RLM_MODULE_FAIL);
813 if ((ldap_count_entries(conn->ld, *result)) != 1) {
814 DEBUG("rlm_ldap: object not found or got ambiguous search result");
815 res = RLM_MODULE_NOTFOUND;
816 ldap_msgfree(*result);
823 * Translate the LDAP queries.
825 static int ldap_escape_func(char *out, int outlen, const char *in)
831 * Encode unsafe characters.
834 ((in[0] == ' ') || (in[0] == '#'))) ||
835 (strchr(",+\"\\<>;*=()", *in))) {
836 static const char hex[] = "0123456789abcdef";
839 * Only 3 or less bytes available.
846 *(out++) = hex[((*in) >> 4) & 0x0f];
847 *(out++) = hex[(*in) & 0x0f];
855 * Only one byte left.
873 * ldap_groupcmp(). Implement the Ldap-Group == "group" filter
875 static int ldap_groupcmp(void *instance, REQUEST *req,
876 UNUSED VALUE_PAIR *request, VALUE_PAIR *check,
877 UNUSED VALUE_PAIR *check_pairs,
878 UNUSED VALUE_PAIR **reply_pairs)
880 char filter[MAX_FILTER_STR_LEN];
881 char gr_filter[MAX_FILTER_STR_LEN];
883 LDAPMessage *result = NULL;
884 LDAPMessage *msg = NULL;
885 char basedn[MAX_FILTER_STR_LEN];
886 char *attrs[] = {"dn",NULL};
888 ldap_instance *inst = instance;
889 char *group_attrs[] = {inst->groupmemb_attr,NULL};
892 VALUE_PAIR *vp_user_dn;
893 VALUE_PAIR **request_pairs;
895 request_pairs = &req->config_items;
897 DEBUG("rlm_ldap: Entering ldap_groupcmp()");
899 if (check->vp_strvalue == NULL || check->length == 0){
900 DEBUG("rlm_ldap::ldap_groupcmp: Illegal group name");
905 DEBUG("rlm_ldap::ldap_groupcmp: NULL request");
909 if (!radius_xlat(basedn, sizeof(basedn), inst->basedn, req, ldap_escape_func)) {
910 DEBUG("rlm_ldap::ldap_groupcmp: unable to create basedn.");
914 while((vp_user_dn = pairfind(*request_pairs, PW_LDAP_USERDN)) == NULL){
915 char *user_dn = NULL;
917 if (!radius_xlat(filter, sizeof(filter), inst->filter,
918 req, ldap_escape_func)){
919 DEBUG("rlm_ldap::ldap_groupcmp: unable to create filter");
922 if ((conn_id = ldap_get_conn(inst->conns,&conn,inst)) == -1){
923 radlog(L_ERR, "rlm_ldap: All ldap connections are in use");
926 if ((res = perform_search(inst, conn, basedn, LDAP_SCOPE_SUBTREE,
927 filter, attrs, &result)) != RLM_MODULE_OK){
928 DEBUG("rlm_ldap::ldap_groupcmp: search failed");
929 ldap_release_conn(conn_id,inst->conns);
932 if ((msg = ldap_first_entry(conn->ld, result)) == NULL) {
933 DEBUG("rlm_ldap::ldap_groupcmp: ldap_first_entry() failed");
934 ldap_release_conn(conn_id,inst->conns);
935 ldap_msgfree(result);
938 if ((user_dn = ldap_get_dn(conn->ld, msg)) == NULL) {
939 DEBUG("rlm_ldap:ldap_groupcmp:: ldap_get_dn() failed");
940 ldap_release_conn(conn_id,inst->conns);
941 ldap_msgfree(result);
944 ldap_release_conn(conn_id,inst->conns);
947 * Adding new attribute containing DN for LDAP
948 * object associated with given username
950 pairadd(request_pairs, pairmake("Ldap-UserDn", user_dn,
952 ldap_memfree(user_dn);
953 ldap_msgfree(result);
956 if(!radius_xlat(gr_filter, sizeof(gr_filter),
957 inst->groupmemb_filt, req, ldap_escape_func)) {
958 DEBUG("rlm_ldap::ldap_groupcmp: unable to create filter.");
962 if (strchr((char *)check->vp_strvalue,',') != NULL) {
963 /* This looks like a DN */
964 snprintf(filter,sizeof(filter), "%s",gr_filter);
965 snprintf(basedn,sizeof(basedn), "%s",(char *)check->vp_strvalue);
967 snprintf(filter,sizeof(filter), "(&(%s=%s)%s)",
968 inst->groupname_attr,
969 (char *)check->vp_strvalue,gr_filter);
971 if ((conn_id = ldap_get_conn(inst->conns,&conn,inst)) == -1) {
972 radlog(L_ERR, "rlm_ldap: All ldap connections are in use");
976 if ((res = perform_search(inst, conn, basedn, LDAP_SCOPE_SUBTREE,
977 filter, attrs, &result)) == RLM_MODULE_OK) {
978 DEBUG("rlm_ldap::ldap_groupcmp: User found in group %s",
979 (char *)check->vp_strvalue);
980 ldap_msgfree(result);
981 ldap_release_conn(conn_id,inst->conns);
985 ldap_release_conn(conn_id,inst->conns);
987 if (res != RLM_MODULE_NOTFOUND ) {
988 DEBUG("rlm_ldap::ldap_groupcmp: Search returned error");
992 if (inst->groupmemb_attr == NULL){
994 * Search returned NOTFOUND and searching for
995 * membership using user object attributes is not
996 * specified in config file
998 DEBUG("rlm_ldap::ldap_groupcmp: Group %s not found or user is not a member.",(char *)check->vp_strvalue);
1002 snprintf(filter,sizeof(filter), "(objectclass=*)");
1003 if ((conn_id = ldap_get_conn(inst->conns,&conn,inst)) == -1){
1004 radlog(L_ERR, "rlm_ldap: Add ldap connections are in use");
1007 if ((res = perform_search(inst, conn, vp_user_dn->vp_strvalue,
1008 LDAP_SCOPE_BASE, filter, group_attrs,
1009 &result)) != RLM_MODULE_OK) {
1010 DEBUG("rlm_ldap::ldap_groupcmp: Search returned error");
1011 ldap_release_conn(conn_id, inst->conns);
1015 if ((msg = ldap_first_entry(conn->ld, result)) == NULL) {
1016 DEBUG("rlm_ldap::ldap_groupcmp: ldap_first_entry() failed");
1017 ldap_release_conn(conn_id,inst->conns);
1018 ldap_msgfree(result);
1021 if ((vals = ldap_get_values(conn->ld, msg,
1022 inst->groupmemb_attr)) != NULL) {
1026 for (;i < ldap_count_values(vals);i++){
1027 if (strchr(vals[i],',') != NULL){
1028 /* This looks like a DN */
1029 LDAPMessage *gr_result = NULL;
1030 snprintf(filter,sizeof(filter), "(%s=%s)",
1031 inst->groupname_attr,
1032 (char *)check->vp_strvalue);
1033 if ((res = perform_search(inst, conn, vals[i],
1034 LDAP_SCOPE_BASE, filter,
1035 attrs, &gr_result)) != RLM_MODULE_OK){
1036 if (res != RLM_MODULE_NOTFOUND) {
1037 DEBUG("rlm_ldap::ldap_groupcmp: Search returned error");
1038 ldap_value_free(vals);
1039 ldap_msgfree(result);
1040 ldap_release_conn(conn_id,inst->conns);
1044 ldap_msgfree(gr_result);
1049 if (strcmp(vals[i],(char *)check->vp_strvalue) == 0){
1055 ldap_value_free(vals);
1056 ldap_msgfree(result);
1058 DEBUG("rlm_ldap::groupcmp: Group %s not found or user not a member",
1059 (char *)check->vp_strvalue);
1060 ldap_release_conn(conn_id,inst->conns);
1064 DEBUG("rlm_ldap::ldap_groupcmp: ldap_get_values() failed");
1065 ldap_msgfree(result);
1066 ldap_release_conn(conn_id,inst->conns);
1070 DEBUG("rlm_ldap::ldap_groupcmp: User found in group %s",(char *)check->vp_strvalue);
1071 ldap_release_conn(conn_id,inst->conns);
1078 * Do an xlat on an LDAP URL
1080 static int ldap_xlat(void *instance, REQUEST *request, char *fmt,
1081 char *out, size_t freespace, RADIUS_ESCAPE_STRING func)
1083 char url[MAX_FILTER_STR_LEN];
1086 ldap_instance *inst = instance;
1087 LDAPURLDesc *ldap_url;
1088 LDAPMessage *result = NULL;
1089 LDAPMessage *msg = NULL;
1094 DEBUG("rlm_ldap: - ldap_xlat");
1095 if (!radius_xlat(url, sizeof(url), fmt, request, func)) {
1096 radlog (L_ERR, "rlm_ldap: Unable to create LDAP URL.\n");
1099 if (!ldap_is_ldap_url(url)){
1100 radlog (L_ERR, "rlm_ldap: String passed does not look like an LDAP URL.\n");
1103 if (ldap_url_parse(url,&ldap_url)){
1104 radlog (L_ERR, "rlm_ldap: LDAP URL parse failed.\n");
1107 if (ldap_url->lud_attrs == NULL || ldap_url->lud_attrs[0] == NULL ||
1108 ( ldap_url->lud_attrs[1] != NULL ||
1109 ( ! strlen(ldap_url->lud_attrs[0]) ||
1110 ! strcmp(ldap_url->lud_attrs[0],"*") ) ) ){
1111 radlog (L_ERR, "rlm_ldap: Invalid Attribute(s) request.\n");
1112 ldap_free_urldesc(ldap_url);
1115 if (ldap_url->lud_host){
1116 if (strncmp(inst->server,ldap_url->lud_host,
1117 strlen(inst->server)) != 0 ||
1118 ldap_url->lud_port != inst->port) {
1119 DEBUG("rlm_ldap: Requested server/port is not known to this module instance.");
1120 ldap_free_urldesc(ldap_url);
1124 if ((conn_id = ldap_get_conn(inst->conns,&conn,inst)) == -1){
1125 radlog(L_ERR, "rlm_ldap: All ldap connections are in use");
1126 ldap_free_urldesc(ldap_url);
1129 if ((res = perform_search(inst, conn, ldap_url->lud_dn, ldap_url->lud_scope, ldap_url->lud_filter, ldap_url->lud_attrs, &result)) != RLM_MODULE_OK){
1130 if (res == RLM_MODULE_NOTFOUND){
1131 DEBUG("rlm_ldap: Search returned not found");
1132 ldap_free_urldesc(ldap_url);
1133 ldap_release_conn(conn_id,inst->conns);
1136 DEBUG("rlm_ldap: Search returned error");
1137 ldap_free_urldesc(ldap_url);
1138 ldap_release_conn(conn_id,inst->conns);
1141 if ((msg = ldap_first_entry(conn->ld, result)) == NULL){
1142 DEBUG("rlm_ldap: ldap_first_entry() failed");
1143 ldap_msgfree(result);
1144 ldap_free_urldesc(ldap_url);
1145 ldap_release_conn(conn_id,inst->conns);
1148 if ((vals = ldap_get_values(conn->ld, msg, ldap_url->lud_attrs[0])) != NULL) {
1149 ret = strlen(vals[0]);
1150 if (ret > freespace){
1151 DEBUG("rlm_ldap: Insufficient string space");
1152 ldap_free_urldesc(ldap_url);
1153 ldap_value_free(vals);
1154 ldap_msgfree(result);
1155 ldap_release_conn(conn_id,inst->conns);
1158 DEBUG("rlm_ldap: Adding attribute %s, value: %s",ldap_url->lud_attrs[0],vals[0]);
1159 strlcpy(out,vals[0],ret);
1160 ldap_value_free(vals);
1165 ldap_msgfree(result);
1166 ldap_free_urldesc(ldap_url);
1167 ldap_release_conn(conn_id,inst->conns);
1169 DEBUG("rlm_ldap: - ldap_xlat end");
1176 * For auto-header discovery.
1178 static const LRAD_NAME_NUMBER header_names[] = {
1179 { "{clear}", PW_CLEARTEXT_PASSWORD },
1180 { "{cleartext}", PW_CLEARTEXT_PASSWORD },
1181 { "{md5}", PW_MD5_PASSWORD },
1182 { "{smd5}", PW_SMD5_PASSWORD },
1183 { "{crypt}", PW_CRYPT_PASSWORD },
1184 { "{sha}", PW_SHA_PASSWORD },
1185 { "{ssha}", PW_SSHA_PASSWORD },
1186 { "{nt}", PW_NT_PASSWORD },
1187 { "{ns-mta-md5}", PW_NS_MTA_MD5_PASSWORD },
1192 /******************************************************************************
1194 * Function: rlm_ldap_authorize
1196 * Purpose: Check if user is authorized for remote access
1198 ******************************************************************************/
1199 static int ldap_authorize(void *instance, REQUEST * request)
1201 LDAPMessage *result = NULL;
1202 LDAPMessage *msg = NULL;
1203 LDAPMessage *def_msg = NULL;
1204 LDAPMessage *def_attr_msg = NULL;
1205 LDAPMessage *def_result = NULL;
1206 LDAPMessage *def_attr_result = NULL;
1207 ldap_instance *inst = instance;
1208 char *user_dn = NULL;
1209 char filter[MAX_FILTER_STR_LEN];
1210 char basedn[MAX_FILTER_STR_LEN];
1211 VALUE_PAIR *check_tmp;
1212 VALUE_PAIR *reply_tmp;
1214 VALUE_PAIR **check_pairs, **reply_pairs;
1216 VALUE_PAIR *module_fmsg_vp;
1217 VALUE_PAIR *user_profile;
1218 char module_fmsg[MAX_STRING_LEN];
1221 int added_known_password = 0;
1223 DEBUG("rlm_ldap: - authorize");
1225 if (!request->username){
1226 radlog(L_AUTH, "rlm_ldap: Attribute \"User-Name\" is required for authentication.\n");
1227 return RLM_MODULE_INVALID;
1230 check_pairs = &request->config_items;
1231 reply_pairs = &request->reply->vps;
1234 * Check for valid input, zero length names not permitted
1236 if (request->username->vp_strvalue == 0) {
1237 radlog(L_ERR, "rlm_ldap: zero length username not permitted\n");
1238 return RLM_MODULE_INVALID;
1240 DEBUG("rlm_ldap: performing user authorization for %s",
1241 request->username->vp_strvalue);
1243 if (!radius_xlat(filter, sizeof(filter), inst->filter,
1244 request, ldap_escape_func)) {
1245 radlog (L_ERR, "rlm_ldap: unable to create filter.\n");
1246 return RLM_MODULE_INVALID;
1249 if (!radius_xlat(basedn, sizeof(basedn), inst->basedn,
1250 request, ldap_escape_func)) {
1251 radlog (L_ERR, "rlm_ldap: unable to create basedn.\n");
1252 return RLM_MODULE_INVALID;
1255 if ((conn_id = ldap_get_conn(inst->conns,&conn,inst)) == -1){
1256 radlog(L_ERR, "rlm_ldap: All ldap connections are in use");
1257 return RLM_MODULE_FAIL;
1259 if ((res = perform_search(instance, conn, basedn, LDAP_SCOPE_SUBTREE, filter, inst->atts, &result)) != RLM_MODULE_OK) {
1260 DEBUG("rlm_ldap: search failed");
1261 if (res == RLM_MODULE_NOTFOUND){
1262 snprintf(module_fmsg,sizeof(module_fmsg),"rlm_ldap: User not found");
1263 module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
1264 pairadd(&request->packet->vps, module_fmsg_vp);
1266 ldap_release_conn(conn_id,inst->conns);
1269 if ((msg = ldap_first_entry(conn->ld, result)) == NULL) {
1270 DEBUG("rlm_ldap: ldap_first_entry() failed");
1271 ldap_msgfree(result);
1272 ldap_release_conn(conn_id,inst->conns);
1273 return RLM_MODULE_FAIL;
1275 if ((user_dn = ldap_get_dn(conn->ld, msg)) == NULL) {
1276 DEBUG("rlm_ldap: ldap_get_dn() failed");
1277 ldap_msgfree(result);
1278 ldap_release_conn(conn_id,inst->conns);
1279 return RLM_MODULE_FAIL;
1282 * Adding new attribute containing DN for LDAP object associated with
1285 pairadd(check_pairs, pairmake("Ldap-UserDn", user_dn, T_OP_EQ));
1286 ldap_memfree(user_dn);
1289 /* Remote access is controled by attribute of the user object */
1290 if (inst->access_attr) {
1291 if ((vals = ldap_get_values(conn->ld, msg, inst->access_attr)) != NULL) {
1292 if (inst->default_allow){
1293 DEBUG("rlm_ldap: checking if remote access for %s is allowed by %s", request->username->vp_strvalue, inst->access_attr);
1294 if (!strncmp(vals[0], "FALSE", 5)) {
1295 DEBUG("rlm_ldap: dialup access disabled");
1296 snprintf(module_fmsg,sizeof(module_fmsg),"rlm_ldap: Access Attribute denies access");
1297 module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
1298 pairadd(&request->packet->vps, module_fmsg_vp);
1299 ldap_msgfree(result);
1300 ldap_value_free(vals);
1301 ldap_release_conn(conn_id,inst->conns);
1302 return RLM_MODULE_USERLOCK;
1304 ldap_value_free(vals);
1307 DEBUG("rlm_ldap: %s attribute exists - access denied by default", inst->access_attr);
1308 snprintf(module_fmsg,sizeof(module_fmsg),"rlm_ldap: Access Attribute denies access");
1309 module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
1310 pairadd(&request->packet->vps, module_fmsg_vp);
1311 ldap_msgfree(result);
1312 ldap_value_free(vals);
1313 ldap_release_conn(conn_id,inst->conns);
1314 return RLM_MODULE_USERLOCK;
1317 if (inst->default_allow){
1318 DEBUG("rlm_ldap: no %s attribute - access denied by default", inst->access_attr);
1319 snprintf(module_fmsg,sizeof(module_fmsg),"rlm_ldap: Access Attribute denies access");
1320 module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
1321 pairadd(&request->packet->vps, module_fmsg_vp);
1322 ldap_msgfree(result);
1323 ldap_release_conn(conn_id,inst->conns);
1324 return RLM_MODULE_USERLOCK;
1330 * Check for the default profile entry. If it exists then add the
1331 * attributes it contains in the check and reply pairs
1334 user_profile = pairfind(request->config_items, PW_USER_PROFILE);
1335 if (inst->default_profile || user_profile){
1336 char *profile = inst->default_profile;
1338 strlcpy(filter,inst->base_filter,sizeof(filter));
1340 profile = user_profile->vp_strvalue;
1341 if (profile && strlen(profile)){
1342 if ((res = perform_search(instance, conn,
1343 profile, LDAP_SCOPE_BASE,
1344 filter, inst->atts, &def_result)) == RLM_MODULE_OK){
1345 if ((def_msg = ldap_first_entry(conn->ld,def_result))){
1346 if ((check_tmp = ldap_pairget(conn->ld,def_msg,inst->check_item_map,check_pairs,1))) {
1348 pairxlatmove(request, check_pairs, &check_tmp);
1349 pairfree(&check_tmp);
1352 pairadd(check_pairs,check_tmp);
1354 if ((reply_tmp = ldap_pairget(conn->ld,def_msg,inst->reply_item_map,reply_pairs,0))) {
1356 pairxlatmove(request, reply_pairs, &reply_tmp);
1357 pairfree(&reply_tmp);
1360 pairadd(reply_pairs,reply_tmp);
1363 ldap_msgfree(def_result);
1365 DEBUG("rlm_ldap: default_profile/user-profile search failed");
1370 * Check for the profile attribute. If it exists, we assume that it
1371 * contains the DN of an entry containg a profile for the user. That
1372 * way we can have different general profiles for various user groups
1373 * (students,faculty,staff etc)
1376 if (inst->profile_attr){
1377 if ((vals = ldap_get_values(conn->ld, msg, inst->profile_attr)) != NULL) {
1379 strlcpy(filter,inst->base_filter,sizeof(filter));
1380 while(vals[i] != NULL && strlen(vals[i])){
1381 if ((res = perform_search(instance, conn,
1382 vals[i], LDAP_SCOPE_BASE,
1383 filter, inst->atts, &def_attr_result)) == RLM_MODULE_OK){
1384 if ((def_attr_msg = ldap_first_entry(conn->ld,def_attr_result))){
1385 if ((check_tmp = ldap_pairget(conn->ld,def_attr_msg,inst->check_item_map,check_pairs,1))) {
1387 pairxlatmove(request, check_pairs, &check_tmp);
1388 pairfree(&check_tmp);
1391 pairadd(check_pairs,check_tmp);
1393 if ((reply_tmp = ldap_pairget(conn->ld,def_attr_msg,inst->reply_item_map,reply_pairs,0))) {
1395 pairxlatmove(request, reply_pairs, &reply_tmp);
1396 pairfree(&reply_tmp);
1399 pairadd(reply_pairs,reply_tmp);
1402 ldap_msgfree(def_attr_result);
1404 DEBUG("rlm_ldap: profile_attribute search failed");
1407 ldap_value_free(vals);
1410 if (inst->passwd_attr && strlen(inst->passwd_attr)) {
1411 #ifdef NOVELL_UNIVERSAL_PASSWORD
1412 if (strcasecmp(inst->passwd_attr,"nspmPassword") != 0) {
1414 VALUE_PAIR *passwd_item;
1420 * Read the password from the DB, and
1421 * add it to the request.
1423 passwd_vals = ldap_get_values(conn->ld,msg,
1427 * Loop over what we received, and parse it.
1429 if (passwd_vals) for (i = 0;
1430 passwd_vals[i] != NULL;
1432 int attr = PW_USER_PASSWORD;
1434 if (strlen(passwd_vals[i]) == 0)
1437 value = passwd_vals[i];
1439 if (inst->auto_header) {
1443 p = strchr(value, '}');
1445 if ((p - value + 1) >= sizeof(autobuf))
1446 continue; /* paranoia */
1447 memcpy(autobuf, value, p - value + 1);
1448 autobuf[p - value + 1] = '\0';
1450 attr = lrad_str2int(header_names,
1452 if (!attr) continue;
1456 } else if (inst->passwd_hdr &&
1457 strlen(inst->passwd_hdr)) {
1458 if (strncasecmp(value,
1460 strlen(inst->passwd_hdr)) == 0) {
1461 value += strlen(inst->passwd_hdr);
1463 DEBUG("rlm_ldap: Password header not found in password %s for user %s", passwd_vals[0], request->username->vp_strvalue);
1466 if (!value) continue;
1469 passwd_item = radius_paircreate(request,
1470 &request->config_items,
1473 strlcpy(passwd_item->vp_strvalue, value,
1474 sizeof(passwd_item->vp_strvalue));
1475 passwd_item->length = strlen(passwd_item->vp_strvalue);
1476 DEBUG("rlm_ldap: Added %s = %s in check items",
1478 passwd_item->vp_strvalue);
1479 added_known_password = 1;
1481 ldap_value_free(passwd_vals);
1482 #ifdef NOVELL_UNIVERSAL_PASSWORD
1486 * Read Universal Password from eDirectory
1488 VALUE_PAIR *passwd_item;
1489 VALUE_PAIR *vp_user_dn;
1491 char *universal_password = NULL;
1492 int universal_password_len = UNIVERSAL_PASS_LEN;
1493 char *passwd_val = NULL;
1497 if ((passwd_item = pairfind(request->config_items, PW_CLEARTEXT_PASSWORD)) == NULL){
1499 universal_password = rad_malloc(universal_password_len);
1500 memset(universal_password, 0, universal_password_len);
1502 vp_user_dn = pairfind(request->config_items,PW_LDAP_USERDN);
1503 res = nmasldap_get_password(conn->ld,vp_user_dn->vp_strvalue,&universal_password_len,universal_password);
1506 passwd_val = universal_password;
1508 if (inst->passwd_hdr && strlen(inst->passwd_hdr)){
1509 passwd_val = strstr(passwd_val,inst->passwd_hdr);
1511 if (passwd_val != NULL)
1512 passwd_val += strlen((char*)inst->passwd_hdr);
1514 DEBUG("rlm_ldap: Password header not found in password %s for user %s ",passwd_val,request->username->vp_strvalue);
1518 passwd_item = radius_paircreate(request, &request->config_items, PW_CLEARTEXT_PASSWORD, PW_TYPE_STRING);
1519 strlcpy(passwd_item->vp_strvalue,passwd_val,sizeof(passwd_item->vp_strvalue));
1520 passwd_item->length = strlen(passwd_item->vp_strvalue);
1521 added_known_password = 1;
1526 VALUE_PAIR *vp_inst, *vp_apc;
1527 int inst_attr, apc_attr;
1529 dattr = dict_attrbyname("LDAP-Instance");
1530 inst_attr = dattr->attr;
1531 dattr = dict_attrbyname("eDir-APC");
1532 apc_attr = dattr->attr;
1534 vp_inst = pairfind(request->config_items, inst_attr);
1535 if(vp_inst == NULL){
1537 * The authorize method of no other LDAP module instance has
1538 * processed this request.
1540 vp_inset = radius_paircreate(request, &request->config_items, inst_attr, PW_TYPE_STRING);
1541 strlcpy(vp_inst->vp_strvalue, inst->xlat_name, sizeof(vp_inst->vp_strvalue));
1542 vp_inst->length = strlen(vp_inst->vp_strvalue);
1545 * Inform the authenticate / post-auth method about the presence
1546 * of UP in the config items list and whether eDirectory account
1547 * policy check is to be performed or not.
1549 vp_apc = radius_paircreate(request, &request->config_items, apc_attr, PW_TYPE_STRING);
1550 if(!inst->edir_account_policy_check){
1552 strcpy(vp_apc->vp_strvalue, "1");
1554 /* Perform eDirectory account-policy check */
1555 strcpy(vp_apc->vp_strvalue, "2");
1562 DEBUG("rlm_ldap: Added the eDirectory password %s in check items as %s",passwd_item->vp_strvalue,passwd_item->name);
1566 DEBUG("rlm_ldap: Error reading Universal Password.Return Code = %d",res);
1569 memset(universal_password, 0, universal_password_len);
1570 free(universal_password);
1578 DEBUG("rlm_ldap: looking for check items in directory...");
1580 if ((check_tmp = ldap_pairget(conn->ld, msg, inst->check_item_map,check_pairs,1)) != NULL) {
1582 pairxlatmove(request, check_pairs, &check_tmp);
1583 pairfree(&check_tmp);
1586 pairadd(check_pairs,check_tmp);
1590 DEBUG("rlm_ldap: looking for reply items in directory...");
1593 if ((reply_tmp = ldap_pairget(conn->ld, msg, inst->reply_item_map,reply_pairs,0)) != NULL) {
1595 pairxlatmove(request, reply_pairs, &reply_tmp);
1596 pairfree(&reply_tmp);
1599 pairadd(reply_pairs,reply_tmp);
1602 if (inst->do_comp && paircompare(request,request->packet->vps,*check_pairs,reply_pairs) != 0){
1604 /* Don't perform eDirectory APC if RADIUS authorize fails */
1609 dattr = dict_attrbyname("eDir-APC");
1610 apc_attr = dattr->attr;
1612 vp_apc = pairfind(request->config_items, apc_attr);
1614 vp_apc->vp_strvalue[0] = '1';
1617 DEBUG("rlm_ldap: Pairs do not match. Rejecting user.");
1618 snprintf(module_fmsg,sizeof(module_fmsg),"rlm_ldap: Pairs do not match");
1619 module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
1620 pairadd(&request->packet->vps, module_fmsg_vp);
1621 ldap_msgfree(result);
1622 ldap_release_conn(conn_id,inst->conns);
1624 return RLM_MODULE_REJECT;
1628 * Module should default to LDAP authentication if no Auth-Type
1629 * specified. Note that we do this ONLY if configured, AND we
1630 * set the Auth-Type to our module name, which allows multiple
1631 * ldap instances to work.
1633 if (inst->set_auth_type &&
1634 (pairfind(*check_pairs, PW_AUTH_TYPE) == NULL) &&
1635 request->password &&
1636 (request->password->attribute == PW_USER_PASSWORD) &&
1637 !added_known_password) {
1638 pairadd(check_pairs, pairmake("Auth-Type", inst->xlat_name, T_OP_EQ));
1639 DEBUG("rlm_ldap: Setting Auth-Type = %s", inst->xlat_name);
1642 DEBUG("rlm_ldap: user %s authorized to use remote access",
1643 request->username->vp_strvalue);
1644 ldap_msgfree(result);
1645 ldap_release_conn(conn_id,inst->conns);
1647 return RLM_MODULE_OK;
1650 /*****************************************************************************
1652 * Function: rlm_ldap_authenticate
1654 * Purpose: Check the user's password against ldap database
1656 *****************************************************************************/
1657 static int ldap_authenticate(void *instance, REQUEST * request)
1660 LDAPMessage *result, *msg;
1661 ldap_instance *inst = instance;
1662 char *user_dn, *attrs[] = {"uid", NULL};
1663 char filter[MAX_FILTER_STR_LEN];
1664 char basedn[MAX_FILTER_STR_LEN];
1666 VALUE_PAIR *vp_user_dn;
1667 VALUE_PAIR *module_fmsg_vp;
1668 char module_fmsg[MAX_STRING_LEN];
1675 DEBUG("rlm_ldap: - authenticate");
1678 * Ensure that we're being passed a plain-text password, and not
1682 if (!request->username) {
1683 radlog(L_AUTH, "rlm_ldap: Attribute \"User-Name\" is required for authentication.\n");
1684 return RLM_MODULE_INVALID;
1687 if (!request->password){
1688 radlog(L_AUTH, "rlm_ldap: Attribute \"User-Password\" is required for authentication.");
1689 DEBUG2(" You seem to have set \"Auth-Type := LDAP\" somewhere.");
1690 DEBUG2(" THAT CONFIGURATION IS WRONG. DELETE IT.");
1691 DEBUG2(" YOU ARE PREVENTING THE SERVER FROM WORKING PROPERLY.");
1692 return RLM_MODULE_INVALID;
1695 if(request->password->attribute != PW_USER_PASSWORD) {
1696 radlog(L_AUTH, "rlm_ldap: Attribute \"User-Password\" is required for authentication. Cannot use \"%s\".", request->password->name);
1697 return RLM_MODULE_INVALID;
1700 if (request->password->length == 0) {
1701 snprintf(module_fmsg,sizeof(module_fmsg),"rlm_ldap: empty password supplied");
1702 module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
1703 pairadd(&request->packet->vps, module_fmsg_vp);
1704 return RLM_MODULE_INVALID;
1708 * Check that we don't have any failed connections. If we do there's no real need
1709 * of runing. Also give it another chance if we have a lot of failed connections.
1711 if (inst->failed_conns > MAX_FAILED_CONNS_END)
1712 inst->failed_conns = 0;
1713 if (inst->failed_conns > MAX_FAILED_CONNS_START){
1714 inst->failed_conns++;
1715 return RLM_MODULE_FAIL;
1719 DEBUG("rlm_ldap: login attempt by \"%s\" with password \"%s\"",
1720 request->username->vp_strvalue, request->password->vp_strvalue);
1722 while ((vp_user_dn = pairfind(request->config_items,
1723 PW_LDAP_USERDN)) == NULL) {
1724 if (!radius_xlat(filter, sizeof(filter), inst->filter,
1725 request, ldap_escape_func)) {
1726 radlog (L_ERR, "rlm_ldap: unable to create filter.\n");
1727 return RLM_MODULE_INVALID;
1730 if (!radius_xlat(basedn, sizeof(basedn), inst->basedn,
1731 request, ldap_escape_func)) {
1732 radlog (L_ERR, "rlm_ldap: unable to create basedn.\n");
1733 return RLM_MODULE_INVALID;
1736 if ((conn_id = ldap_get_conn(inst->conns,&conn,inst)) == -1){
1737 radlog(L_ERR, "rlm_ldap: All ldap connections are in use");
1738 return RLM_MODULE_FAIL;
1740 if ((res = perform_search(instance, conn, basedn, LDAP_SCOPE_SUBTREE, filter, attrs, &result)) != RLM_MODULE_OK) {
1741 if (res == RLM_MODULE_NOTFOUND){
1742 snprintf(module_fmsg,sizeof(module_fmsg),"rlm_ldap: User not found");
1743 module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
1744 pairadd(&request->packet->vps, module_fmsg_vp);
1746 ldap_release_conn(conn_id,inst->conns);
1749 if ((msg = ldap_first_entry(conn->ld, result)) == NULL) {
1750 ldap_msgfree(result);
1751 ldap_release_conn(conn_id,inst->conns);
1752 return RLM_MODULE_FAIL;
1754 if ((user_dn = ldap_get_dn(conn->ld, msg)) == NULL) {
1755 DEBUG("rlm_ldap: ldap_get_dn() failed");
1756 ldap_msgfree(result);
1757 ldap_release_conn(conn_id,inst->conns);
1758 return RLM_MODULE_FAIL;
1760 ldap_release_conn(conn_id,inst->conns);
1761 pairadd(&request->config_items, pairmake("Ldap-UserDn", user_dn, T_OP_EQ));
1762 ldap_memfree(user_dn);
1763 ldap_msgfree(result);
1766 user_dn = vp_user_dn->vp_strvalue;
1768 DEBUG("rlm_ldap: user DN: %s", user_dn);
1771 ld_user = ldap_connect(instance, user_dn, request->password->vp_strvalue,
1775 ld_user = ldap_connect(instance, user_dn, request->password->vp_strvalue,
1779 /* 'err' contains the LDAP connection error description */
1780 DEBUG("rlm_ldap: %s", err);
1781 pairadd(&request->reply->vps, pairmake("Reply-Message", err, T_OP_EQ));
1782 ldap_memfree((void *)err);
1785 /* Don't perform eDirectory APC again after attempting to bind here. */
1791 dattr = dict_attrbyname("eDir-APC");
1792 apc_attr = dattr->attr;
1793 vp_apc = pairfind(request->config_items, apc_attr);
1794 if(vp_apc && vp_apc->vp_strvalue[0] == '2')
1795 vp_apc->vp_strvalue[0] = '3';
1799 if (ld_user == NULL){
1800 if (res == RLM_MODULE_REJECT){
1801 inst->failed_conns = 0;
1802 snprintf(module_fmsg,sizeof(module_fmsg),"rlm_ldap: Bind as user failed");
1803 module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
1804 pairadd(&request->packet->vps, module_fmsg_vp);
1806 if (res == RLM_MODULE_FAIL){
1807 DEBUG("rlm_ldap: ldap_connect() failed");
1808 inst->failed_conns++;
1813 DEBUG("rlm_ldap: user %s authenticated succesfully",
1814 request->username->vp_strvalue);
1815 ldap_unbind_s(ld_user);
1816 inst->failed_conns = 0;
1818 return RLM_MODULE_OK;
1822 /*****************************************************************************
1824 * Function: rlm_ldap_postauth
1826 * Purpose: Perform eDirectory account policy check and failed-login reporting
1829 *****************************************************************************/
1830 static int ldap_postauth(void *instance, REQUEST * request)
1832 int res = RLM_MODULE_FAIL;
1833 int inst_attr, apc_attr;
1834 char password[UNIVERSAL_PASS_LEN];
1835 ldap_instance *inst = instance;
1837 VALUE_PAIR *vp_inst, *vp_apc;
1840 dattr = dict_attrbyname("LDAP-Instance");
1841 inst_attr = dattr->attr;
1842 dattr = dict_attrbyname("eDir-APC");
1843 apc_attr = dattr->attr;
1845 vp_inst = pairfind(request->config_items, inst_attr);
1848 * Check if the password in the config items list is the user's UP which has
1849 * been read in the authorize method of this instance of the LDAP module.
1851 if((vp_inst == NULL) || strcmp(vp_inst->vp_strvalue, inst->xlat_name))
1852 return RLM_MODULE_NOOP;
1854 vp_apc = pairfind(request->config_items, apc_attr);
1856 switch(vp_apc->vp_strvalue[0]){
1858 /* Account policy check not enabled */
1860 /* Account policy check has been completed */
1861 res = RLM_MODULE_NOOP;
1865 int err, conn_id = -1;
1866 char *error_msg = NULL;
1867 VALUE_PAIR *vp_fdn, *vp_pwd;
1870 if (request->reply->code == PW_AUTHENTICATION_REJECT) {
1871 /* Bind to eDirectory as the RADIUS user with a wrong password. */
1872 vp_pwd = pairfind(request->config_items, PW_CLEARTEXT_PASSWORD);
1873 strcpy(password, vp_pwd->vp_strvalue);
1874 if (strlen(password) > 0) {
1875 if (password[0] != 'a') {
1881 strcpy(password, "dummy_password");
1883 res = RLM_MODULE_REJECT;
1885 /* Bind to eDirectory as the RADIUS user using the user's UP */
1886 vp_pwd = pairfind(request->config_items, PW_CLEARTEXT_PASSWORD);
1887 if (vp_pwd == NULL) {
1888 DEBUG("rlm_ldap: User's Universal Password not in config items list.");
1889 return RLM_MODULE_FAIL;
1891 strcpy(password, vp_pwd->vp_strvalue);
1894 if ((da = dict_attrbyname("Ldap-UserDn")) == NULL) {
1895 DEBUG("rlm_ldap: Attribute for user FDN not found in dictionary. Unable to proceed");
1896 return RLM_MODULE_FAIL;
1899 vp_fdn = pairfind(request->packet->vps, da->attr);
1900 if (vp_fdn == NULL) {
1901 DEBUG("rlm_ldap: User's FQDN not in config items list.");
1902 return RLM_MODULE_FAIL;
1905 if ((conn_id = ldap_get_conn(inst->apc_conns, &conn, inst)) == -1){
1906 radlog(L_ERR, "rlm_ldap: All ldap connections are in use");
1907 return RLM_MODULE_FAIL;
1911 * If there is an existing LDAP
1912 * connection to the directory,
1913 * bind over it. Otherwise,
1914 * establish a new connection.
1917 if (!conn->bound || conn->ld == NULL) {
1918 DEBUG2("rlm_ldap: attempting LDAP reconnection");
1920 DEBUG2("rlm_ldap: closing existing LDAP connection");
1921 ldap_unbind_s(conn->ld);
1923 if ((conn->ld = ldap_connect(instance, (char *)vp_fdn->vp_strvalue, password, 0, &res, &error_msg)) == NULL) {
1924 radlog(L_ERR, "rlm_ldap: eDirectory account policy check failed.");
1926 if (error_msg != NULL) {
1927 DEBUG("rlm_ldap: %s", error_msg);
1928 pairadd(&request->reply->vps, pairmake("Reply-Message", error_msg, T_OP_EQ));
1929 ldap_memfree((void *)error_msg);
1932 vp_apc->vp_strvalue[0] = '3';
1933 ldap_release_conn(conn_id, inst->apc_conns);
1934 return RLM_MODULE_REJECT;
1937 } else if((err = ldap_simple_bind_s(conn->ld, (char *)vp_fdn->vp_strvalue, password)) != LDAP_SUCCESS) {
1938 if (err == LDAP_SERVER_DOWN) {
1940 goto postauth_reconnect;
1942 DEBUG("rlm_ldap: eDirectory account policy check failed.");
1943 ldap_get_option(conn->ld, LDAP_OPT_ERROR_STRING, &error_msg);
1944 if (error_msg != NULL) {
1945 DEBUG("rlm_ldap: %s", error_msg);
1946 pairadd(&request->reply->vps, pairmake("Reply-Message", error_msg, T_OP_EQ));
1947 ldap_memfree((void *)error_msg);
1949 vp_apc->vp_strvalue[0] = '3';
1950 ldap_release_conn(conn_id, inst->apc_conns);
1951 return RLM_MODULE_REJECT;
1953 vp_apc->vp_strvalue[0] = '3';
1954 ldap_release_conn(conn_id, inst->apc_conns);
1955 return RLM_MODULE_OK;
1962 static LDAP *ldap_connect(void *instance, const char *dn, const char *password,
1963 int auth, int *result, char **err)
1965 ldap_instance *inst = instance;
1967 int msgid, rc, ldap_version;
1972 #ifdef HAVE_LDAP_INITIALIZE
1973 DEBUG("rlm_ldap: (re)connect to %s, authentication %d", inst->server, auth);
1974 if (ldap_initialize(&ld, inst->server) != LDAP_SUCCESS) {
1975 radlog(L_ERR, "rlm_ldap: ldap_initialize() failed");
1976 *result = RLM_MODULE_FAIL;
1981 DEBUG("rlm_ldap: (re)connect to %s:%d, authentication %d", inst->server, inst->port, auth);
1982 if ((ld = ldap_init(inst->server, inst->port)) == NULL) {
1983 radlog(L_ERR, "rlm_ldap: ldap_init() failed");
1984 *result = RLM_MODULE_FAIL;
1988 if (ldap_set_option(ld, LDAP_OPT_NETWORK_TIMEOUT,
1989 (void *) &(inst->net_timeout)) != LDAP_OPT_SUCCESS) {
1990 radlog(L_ERR, "rlm_ldap: Could not set LDAP_OPT_NETWORK_TIMEOUT %ld.%ld", inst->net_timeout.tv_sec, inst->net_timeout.tv_usec);
1993 if (ldap_set_option(ld, LDAP_OPT_TIMELIMIT,
1994 (void *) &(inst->timelimit)) != LDAP_OPT_SUCCESS) {
1995 radlog(L_ERR, "rlm_ldap: Could not set LDAP_OPT_TIMELIMIT %d", inst->timelimit);
1998 if (inst->ldap_debug && ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &(inst->ldap_debug)) != LDAP_OPT_SUCCESS) {
1999 radlog(L_ERR, "rlm_ldap: Could not set LDAP_OPT_DEBUG_LEVEL %d", inst->ldap_debug);
2002 ldap_version = LDAP_VERSION3;
2003 if (ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION,
2004 &ldap_version) != LDAP_OPT_SUCCESS) {
2005 radlog(L_ERR, "rlm_ldap: Could not set LDAP version to V3");
2008 #ifdef HAVE_LDAP_START_TLS
2009 if (inst->tls_mode) {
2010 DEBUG("rlm_ldap: setting TLS mode to %d", inst->tls_mode);
2011 if (ldap_set_option(ld, LDAP_OPT_X_TLS,
2012 (void *) &(inst->tls_mode)) != LDAP_OPT_SUCCESS) {
2013 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
2014 radlog(L_ERR, "rlm_ldap: could not set LDAP_OPT_X_TLS option %s", ldap_err2string(ldap_errno));
2018 if (inst->tls_cacertfile != NULL) {
2019 DEBUG("rlm_ldap: setting TLS CACert File to %s", inst->tls_cacertfile);
2021 if ( ldap_set_option( NULL, LDAP_OPT_X_TLS_CACERTFILE,
2022 (void *) inst->tls_cacertfile )
2023 != LDAP_OPT_SUCCESS) {
2024 radlog(L_ERR, "rlm_ldap: could not set "
2025 "LDAP_OPT_X_TLS_CACERTFILE option to %s", inst->tls_cacertfile);
2029 if (inst->tls_cacertdir != NULL) {
2030 DEBUG("rlm_ldap: setting TLS CACert Directory to %s", inst->tls_cacertdir);
2032 if ( ldap_set_option( NULL, LDAP_OPT_X_TLS_CACERTDIR,
2033 (void *) inst->tls_cacertdir )
2034 != LDAP_OPT_SUCCESS) {
2035 radlog(L_ERR, "rlm_ldap: could not set "
2036 "LDAP_OPT_X_TLS_CACERTDIR option to %s", inst->tls_cacertdir);
2040 if (strcmp(TLS_DEFAULT_VERIFY, inst->tls_require_cert ) != 0 ) {
2041 DEBUG("rlm_ldap: setting TLS Require Cert to %s",
2042 inst->tls_require_cert);
2046 #ifdef HAVE_LDAP_INT_TLS_CONFIG
2047 if (ldap_int_tls_config(NULL, LDAP_OPT_X_TLS_REQUIRE_CERT,
2048 (inst->tls_require_cert)) != LDAP_OPT_SUCCESS) {
2049 radlog(L_ERR, "rlm_ldap: could not set "
2050 "LDAP_OPT_X_TLS_REQUIRE_CERT option to %s",
2051 inst->tls_require_cert);
2055 if (inst->tls_certfile != NULL) {
2056 DEBUG("rlm_ldap: setting TLS Cert File to %s", inst->tls_certfile);
2058 if (ldap_set_option(NULL, LDAP_OPT_X_TLS_CERTFILE,
2059 (void *) inst->tls_certfile)
2060 != LDAP_OPT_SUCCESS) {
2061 radlog(L_ERR, "rlm_ldap: could not set "
2062 "LDAP_OPT_X_TLS_CERTFILE option to %s",
2063 inst->tls_certfile);
2067 if (inst->tls_keyfile != NULL) {
2068 DEBUG("rlm_ldap: setting TLS Key File to %s",
2071 if ( ldap_set_option( NULL, LDAP_OPT_X_TLS_KEYFILE,
2072 (void *) inst->tls_keyfile )
2073 != LDAP_OPT_SUCCESS) {
2074 radlog(L_ERR, "rlm_ldap: could not set "
2075 "LDAP_OPT_X_TLS_KEYFILE option to %s",
2080 if (inst->tls_randfile != NULL) {
2081 DEBUG("rlm_ldap: setting TLS Key File to %s",
2082 inst->tls_randfile);
2084 if (ldap_set_option(NULL, LDAP_OPT_X_TLS_RANDOM_FILE,
2085 (void *) inst->tls_randfile)
2086 != LDAP_OPT_SUCCESS) {
2087 radlog(L_ERR, "rlm_ldap: could not set "
2088 "LDAP_OPT_X_TLS_RANDOM_FILE option to %s",
2089 inst->tls_randfile);
2093 if (inst->start_tls) {
2094 DEBUG("rlm_ldap: starting TLS");
2095 rc = ldap_start_tls_s(ld, NULL, NULL);
2096 if (rc != LDAP_SUCCESS) {
2097 DEBUG("rlm_ldap: ldap_start_tls_s()");
2098 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER,
2100 radlog(L_ERR, "rlm_ldap: could not start TLS %s",
2101 ldap_err2string(ldap_errno));
2102 *result = RLM_MODULE_FAIL;
2107 #endif /* HAVE_LDAP_START_TLS */
2110 DEBUG("rlm_ldap: bind as %s/%s to %s",
2111 dn, password, inst->server);
2113 DEBUG("rlm_ldap: bind as %s/%s to %s:%d",
2114 dn, password, inst->server, inst->port);
2117 msgid = ldap_bind(ld, dn, password,LDAP_AUTH_SIMPLE);
2119 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
2121 ldap_get_option(ld, LDAP_OPT_ERROR_STRING, err);
2124 radlog(L_ERR, "rlm_ldap: %s bind to %s failed: %s",
2125 dn, inst->server, ldap_err2string(ldap_errno));
2127 radlog(L_ERR, "rlm_ldap: %s bind to %s:%d failed: %s",
2128 dn, inst->server, inst->port,
2129 ldap_err2string(ldap_errno));
2131 *result = RLM_MODULE_FAIL;
2135 DEBUG("rlm_ldap: waiting for bind result ...");
2137 rc = ldap_result(ld, msgid, 1, &(inst->timeout), &res);
2140 DEBUG("rlm_ldap: ldap_result()");
2141 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
2143 ldap_get_option(ld, LDAP_OPT_ERROR_STRING, err);
2146 radlog(L_ERR, "rlm_ldap: %s bind to %s failed: %s",
2147 dn, inst->server, (rc == 0) ? "timeout" : ldap_err2string(ldap_errno));
2149 radlog(L_ERR, "rlm_ldap: %s bind to %s:%d failed: %s",
2150 dn, inst->server, inst->port,
2151 (rc == 0) ? "timeout" : ldap_err2string(ldap_errno));
2153 *result = RLM_MODULE_FAIL;
2158 ldap_errno = ldap_result2error(ld, res, 1);
2159 switch (ldap_errno) {
2161 DEBUG("rlm_ldap: Bind was successful");
2162 *result = RLM_MODULE_OK;
2165 case LDAP_INVALID_CREDENTIALS:
2167 DEBUG("rlm_ldap: Bind failed with invalid credentials");
2168 *result = RLM_MODULE_REJECT;
2170 radlog(L_ERR, "rlm_ldap: LDAP login failed: check identity, password settings in ldap section of radiusd.conf");
2171 *result = RLM_MODULE_FAIL;
2174 ldap_get_option(ld, LDAP_OPT_ERROR_STRING, err);
2180 radlog(L_ERR,"rlm_ldap: %s bind to %s failed %s",
2181 dn, inst->server, ldap_err2string(ldap_errno));
2183 radlog(L_ERR,"rlm_ldap: %s bind to %s:%d failed %s",
2184 dn, inst->server, inst->port,
2185 ldap_err2string(ldap_errno));
2187 *result = RLM_MODULE_FAIL;
2189 ldap_get_option(ld, LDAP_OPT_ERROR_STRING, err);
2193 if (*result != RLM_MODULE_OK) {
2200 /*****************************************************************************
2202 * Detach from the LDAP server and cleanup internal state.
2204 *****************************************************************************/
2206 ldap_detach(void *instance)
2208 ldap_instance *inst = instance;
2209 TLDAP_RADIUS *pair, *nextpair;
2214 for (i = 0;i < inst->num_conns; i++) {
2215 if (inst->conns[i].ld){
2216 ldap_unbind_s(inst->conns[i].ld);
2218 pthread_mutex_destroy(&inst->conns[i].mutex);
2224 if (inst->apc_conns){
2227 for (i = 0; i < inst->num_conns; i++) {
2228 if (inst->apc_conns[i].ld){
2229 ldap_unbind_s(inst->apc_conns[i].ld);
2231 pthread_mutex_destroy(&inst->apc_conns[i].mutex);
2233 free(inst->apc_conns);
2237 pair = inst->check_item_map;
2239 while (pair != NULL) {
2240 nextpair = pair->next;
2242 free(pair->radius_attr);
2247 pair = inst->reply_item_map;
2249 while (pair != NULL) {
2250 nextpair = pair->next;
2252 free(pair->radius_attr);
2260 paircompare_unregister(PW_LDAP_GROUP, ldap_groupcmp);
2261 xlat_unregister(inst->xlat_name,ldap_xlat);
2262 free(inst->xlat_name);
2272 fieldcpy(char *string, char **uptr)
2277 while (*ptr == ' ' || *ptr == '\t') {
2282 while (*ptr != '"' && *ptr != '\0' && *ptr != '\n') {
2292 while (*ptr != ' ' && *ptr != '\t' && *ptr != '\0' && *ptr != '\n' &&
2293 *ptr != '=' && *ptr != ',') {
2303 * Copied from src/lib/token.c
2305 static const LRAD_NAME_NUMBER tokens[] = {
2306 { "=~", T_OP_REG_EQ, }, /* order is important! */
2307 { "!~", T_OP_REG_NE, },
2308 { "{", T_LCBRACE, },
2309 { "}", T_RCBRACE, },
2313 { "+=", T_OP_ADD, },
2314 { "-=", T_OP_SUB, },
2315 { ":=", T_OP_SET, },
2316 { "=*", T_OP_CMP_TRUE, },
2317 { "!*", T_OP_CMP_FALSE, },
2318 { "==", T_OP_CMP_EQ, },
2328 /*****************************************************************************
2329 * Get RADIUS attributes from LDAP object
2330 * ( according to draft-adoba-radius-05.txt
2331 * <http://www.ietf.org/internet-drafts/draft-adoba-radius-05.txt> )
2333 *****************************************************************************/
2334 static VALUE_PAIR *ldap_pairget(LDAP *ld, LDAPMessage *entry,
2335 TLDAP_RADIUS *item_map,
2336 VALUE_PAIR **pairs, int is_check)
2343 TLDAP_RADIUS *element;
2344 LRAD_TOKEN token, operator;
2345 int is_generic_attribute;
2346 char buf[MAX_STRING_LEN];
2347 VALUE_PAIR *pairlist = NULL;
2348 VALUE_PAIR *newpair = NULL;
2349 char do_xlat = FALSE;
2350 char print_buffer[2048];
2353 * check if there is a mapping from this LDAP attribute
2354 * to a RADIUS attribute
2356 for (element = item_map; element != NULL; element = element->next) {
2358 * No mapping, skip it.
2360 if ((vals = ldap_get_values(ld,entry,element->attr)) == NULL)
2364 * Check whether this is a one-to-one-mapped ldap
2365 * attribute or a generic attribute and set flag
2368 if (strcasecmp(element->radius_attr, GENERIC_ATTRIBUTE_ID)==0)
2369 is_generic_attribute = 1;
2371 is_generic_attribute = 0;
2374 * Find out how many values there are for the
2375 * attribute and extract all of them.
2377 vals_count = ldap_count_values(vals);
2379 for (vals_idx = 0; vals_idx < vals_count; vals_idx++) {
2380 value = vals[vals_idx];
2382 if (is_generic_attribute) {
2384 * This is a generic attribute.
2386 LRAD_TOKEN dummy; /* makes pairread happy */
2388 /* not sure if using pairread here is ok ... */
2389 if ( (newpair = pairread(&value, &dummy)) != NULL) {
2390 DEBUG("rlm_ldap: extracted attribute %s from generic item %s",
2391 newpair->name, vals[vals_idx]);
2392 pairadd(&pairlist, newpair);
2394 radlog(L_ERR, "rlm_ldap: parsing %s failed: %s",
2395 element->attr, vals[vals_idx]);
2399 * This is a one-to-one-mapped attribute
2402 operator = gettoken(&ptr, buf, sizeof(buf));
2403 if (operator < T_EQSTART || operator > T_EQEND) {
2404 /* no leading operator found */
2405 if (element->operator != T_OP_INVALID)
2406 operator = element->operator;
2408 operator = T_OP_CMP_EQ;
2412 /* the value is after the operator */
2417 * Do xlat if the *entire* string
2420 if ((value[0] == '\'' || value[0] == '"' ||
2422 (value[0] == value[strlen(value)-1])) {
2424 token = gettoken(&ptr, buf, sizeof(buf));
2426 /* take the unquoted string */
2427 case T_SINGLE_QUOTED_STRING:
2428 case T_DOUBLE_QUOTED_STRING:
2432 /* the value will be xlat'ed later */
2433 case T_BACK_QUOTED_STRING:
2438 /* keep the original string */
2443 if (value[0] == '\0') {
2444 DEBUG("rlm_ldap: Attribute %s has no value", element->attr);
2451 newpair = pairmake(element->radius_attr,
2452 do_xlat ? NULL : value,
2454 if (newpair == NULL) {
2455 radlog(L_ERR, "rlm_ldap: Failed to create the pair: %s", librad_errstr);
2460 newpair->flags.do_xlat = 1;
2461 strlcpy(newpair->vp_strvalue, buf,
2462 sizeof(newpair->vp_strvalue));
2463 newpair->length = 0;
2465 vp_prints(print_buffer, sizeof(print_buffer),
2467 DEBUG("rlm_ldap: LDAP attribute %s as RADIUS attribute %s",
2468 element->attr, print_buffer);
2472 * Add the pair into the packet.
2475 pairdelete(pairs, newpair->attribute);
2477 pairadd(&pairlist, newpair);
2480 ldap_value_free(vals);
2486 /* globally exported name */
2487 module_t rlm_ldap = {
2490 RLM_TYPE_THREAD_SAFE, /* type: reserved */
2491 ldap_instantiate, /* instantiation */
2492 ldap_detach, /* detach */
2494 ldap_authenticate, /* authentication */
2495 ldap_authorize, /* authorization */
2496 NULL, /* preaccounting */
2497 NULL, /* accounting */
2498 NULL, /* checksimul */
2499 NULL, /* pre-proxy */
2500 NULL, /* post-proxy */
2502 ldap_postauth /* post-auth */