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_INTEGER, -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];
1222 DEBUG("rlm_ldap: - authorize");
1224 if (!request->username){
1225 radlog(L_AUTH, "rlm_ldap: Attribute \"User-Name\" is required for authentication.\n");
1226 return RLM_MODULE_INVALID;
1229 check_pairs = &request->config_items;
1230 reply_pairs = &request->reply->vps;
1233 * Check for valid input, zero length names not permitted
1235 if (request->username->vp_strvalue == 0) {
1236 radlog(L_ERR, "rlm_ldap: zero length username not permitted\n");
1237 return RLM_MODULE_INVALID;
1239 DEBUG("rlm_ldap: performing user authorization for %s",
1240 request->username->vp_strvalue);
1242 if (!radius_xlat(filter, sizeof(filter), inst->filter,
1243 request, ldap_escape_func)) {
1244 radlog (L_ERR, "rlm_ldap: unable to create filter.\n");
1245 return RLM_MODULE_INVALID;
1248 if (!radius_xlat(basedn, sizeof(basedn), inst->basedn,
1249 request, ldap_escape_func)) {
1250 radlog (L_ERR, "rlm_ldap: unable to create basedn.\n");
1251 return RLM_MODULE_INVALID;
1254 if ((conn_id = ldap_get_conn(inst->conns,&conn,inst)) == -1){
1255 radlog(L_ERR, "rlm_ldap: All ldap connections are in use");
1256 return RLM_MODULE_FAIL;
1258 if ((res = perform_search(instance, conn, basedn, LDAP_SCOPE_SUBTREE, filter, inst->atts, &result)) != RLM_MODULE_OK) {
1259 DEBUG("rlm_ldap: search failed");
1260 if (res == RLM_MODULE_NOTFOUND){
1261 snprintf(module_fmsg,sizeof(module_fmsg),"rlm_ldap: User not found");
1262 module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
1263 pairadd(&request->packet->vps, module_fmsg_vp);
1265 ldap_release_conn(conn_id,inst->conns);
1268 if ((msg = ldap_first_entry(conn->ld, result)) == NULL) {
1269 DEBUG("rlm_ldap: ldap_first_entry() failed");
1270 ldap_msgfree(result);
1271 ldap_release_conn(conn_id,inst->conns);
1272 return RLM_MODULE_FAIL;
1274 if ((user_dn = ldap_get_dn(conn->ld, msg)) == NULL) {
1275 DEBUG("rlm_ldap: ldap_get_dn() failed");
1276 ldap_msgfree(result);
1277 ldap_release_conn(conn_id,inst->conns);
1278 return RLM_MODULE_FAIL;
1281 * Adding new attribute containing DN for LDAP object associated with
1284 pairadd(check_pairs, pairmake("Ldap-UserDn", user_dn, T_OP_EQ));
1285 ldap_memfree(user_dn);
1288 /* Remote access is controled by attribute of the user object */
1289 if (inst->access_attr) {
1290 if ((vals = ldap_get_values(conn->ld, msg, inst->access_attr)) != NULL) {
1291 if (inst->default_allow){
1292 DEBUG("rlm_ldap: checking if remote access for %s is allowed by %s", request->username->vp_strvalue, inst->access_attr);
1293 if (!strncmp(vals[0], "FALSE", 5)) {
1294 DEBUG("rlm_ldap: dialup access disabled");
1295 snprintf(module_fmsg,sizeof(module_fmsg),"rlm_ldap: Access Attribute denies access");
1296 module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
1297 pairadd(&request->packet->vps, module_fmsg_vp);
1298 ldap_msgfree(result);
1299 ldap_value_free(vals);
1300 ldap_release_conn(conn_id,inst->conns);
1301 return RLM_MODULE_USERLOCK;
1303 ldap_value_free(vals);
1306 DEBUG("rlm_ldap: %s attribute exists - access denied by default", inst->access_attr);
1307 snprintf(module_fmsg,sizeof(module_fmsg),"rlm_ldap: Access Attribute denies access");
1308 module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
1309 pairadd(&request->packet->vps, module_fmsg_vp);
1310 ldap_msgfree(result);
1311 ldap_value_free(vals);
1312 ldap_release_conn(conn_id,inst->conns);
1313 return RLM_MODULE_USERLOCK;
1316 if (inst->default_allow){
1317 DEBUG("rlm_ldap: no %s attribute - access denied by default", inst->access_attr);
1318 snprintf(module_fmsg,sizeof(module_fmsg),"rlm_ldap: Access Attribute denies access");
1319 module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
1320 pairadd(&request->packet->vps, module_fmsg_vp);
1321 ldap_msgfree(result);
1322 ldap_release_conn(conn_id,inst->conns);
1323 return RLM_MODULE_USERLOCK;
1329 * Check for the default profile entry. If it exists then add the
1330 * attributes it contains in the check and reply pairs
1333 user_profile = pairfind(request->config_items, PW_USER_PROFILE);
1334 if (inst->default_profile || user_profile){
1335 char *profile = inst->default_profile;
1337 strlcpy(filter,inst->base_filter,sizeof(filter));
1339 profile = user_profile->vp_strvalue;
1340 if (profile && strlen(profile)){
1341 if ((res = perform_search(instance, conn,
1342 profile, LDAP_SCOPE_BASE,
1343 filter, inst->atts, &def_result)) == RLM_MODULE_OK){
1344 if ((def_msg = ldap_first_entry(conn->ld,def_result))){
1345 if ((check_tmp = ldap_pairget(conn->ld,def_msg,inst->check_item_map,check_pairs,1))) {
1347 pairxlatmove(request, check_pairs, &check_tmp);
1348 pairfree(&check_tmp);
1351 pairadd(check_pairs,check_tmp);
1353 if ((reply_tmp = ldap_pairget(conn->ld,def_msg,inst->reply_item_map,reply_pairs,0))) {
1355 pairxlatmove(request, reply_pairs, &reply_tmp);
1356 pairfree(&reply_tmp);
1359 pairadd(reply_pairs,reply_tmp);
1362 ldap_msgfree(def_result);
1364 DEBUG("rlm_ldap: default_profile/user-profile search failed");
1369 * Check for the profile attribute. If it exists, we assume that it
1370 * contains the DN of an entry containg a profile for the user. That
1371 * way we can have different general profiles for various user groups
1372 * (students,faculty,staff etc)
1375 if (inst->profile_attr){
1376 if ((vals = ldap_get_values(conn->ld, msg, inst->profile_attr)) != NULL) {
1378 strlcpy(filter,inst->base_filter,sizeof(filter));
1379 while(vals[i] != NULL && strlen(vals[i])){
1380 if ((res = perform_search(instance, conn,
1381 vals[i], LDAP_SCOPE_BASE,
1382 filter, inst->atts, &def_attr_result)) == RLM_MODULE_OK){
1383 if ((def_attr_msg = ldap_first_entry(conn->ld,def_attr_result))){
1384 if ((check_tmp = ldap_pairget(conn->ld,def_attr_msg,inst->check_item_map,check_pairs,1))) {
1386 pairxlatmove(request, check_pairs, &check_tmp);
1387 pairfree(&check_tmp);
1390 pairadd(check_pairs,check_tmp);
1392 if ((reply_tmp = ldap_pairget(conn->ld,def_attr_msg,inst->reply_item_map,reply_pairs,0))) {
1394 pairxlatmove(request, reply_pairs, &reply_tmp);
1395 pairfree(&reply_tmp);
1398 pairadd(reply_pairs,reply_tmp);
1401 ldap_msgfree(def_attr_result);
1403 DEBUG("rlm_ldap: profile_attribute search failed");
1406 ldap_value_free(vals);
1409 if (inst->passwd_attr && strlen(inst->passwd_attr)) {
1410 #ifdef NOVELL_UNIVERSAL_PASSWORD
1411 if (strcasecmp(inst->passwd_attr,"nspmPassword") != 0) {
1413 VALUE_PAIR *passwd_item;
1419 * Read the password from the DB, and
1420 * add it to the request.
1422 passwd_vals = ldap_get_values(conn->ld,msg,
1426 * Loop over what we received, and parse it.
1428 if (passwd_vals) for (i = 0;
1429 passwd_vals[i] != NULL;
1431 int attr = PW_USER_PASSWORD;
1433 if (strlen(passwd_vals[i]) == 0)
1436 value = passwd_vals[i];
1438 if (inst->auto_header) {
1442 p = strchr(value, '}');
1444 if ((p - value + 1) >= sizeof(autobuf))
1445 continue; /* paranoia */
1446 memcpy(autobuf, value, p - value + 1);
1447 autobuf[p - value + 1] = '\0';
1449 attr = lrad_str2int(header_names,
1451 if (!attr) continue;
1455 } else if (inst->passwd_hdr &&
1456 strlen(inst->passwd_hdr)) {
1457 if (strncasecmp(value,
1459 strlen(inst->passwd_hdr)) == 0) {
1460 value += strlen(inst->passwd_hdr);
1462 DEBUG("rlm_ldap: Password header not found in password %s for user %s", passwd_vals[0], request->username->vp_strvalue);
1465 if (!value) continue;
1468 passwd_item = paircreate(attr, PW_TYPE_STRING);
1470 radlog(L_ERR|L_CONS, "no memory");
1471 ldap_value_free(passwd_vals);
1472 ldap_msgfree(result);
1473 ldap_release_conn(conn_id,inst->conns);
1474 return RLM_MODULE_FAIL;
1476 strlcpy(passwd_item->vp_strvalue, value,
1477 sizeof(passwd_item->vp_strvalue));
1478 passwd_item->length = strlen(passwd_item->vp_strvalue);
1479 pairadd(&request->config_items,passwd_item);
1480 DEBUG("rlm_ldap: Added %s = %s in check items",
1482 passwd_item->vp_strvalue);
1484 ldap_value_free(passwd_vals);
1485 #ifdef NOVELL_UNIVERSAL_PASSWORD
1489 * Read Universal Password from eDirectory
1491 VALUE_PAIR *passwd_item;
1492 VALUE_PAIR *vp_user_dn;
1494 char *universal_password = NULL;
1495 int universal_password_len = UNIVERSAL_PASS_LEN;
1496 char *passwd_val = NULL;
1500 if ((passwd_item = pairfind(request->config_items, PW_CLEARTEXT_PASSWORD)) == NULL){
1502 universal_password = rad_malloc(universal_password_len);
1503 memset(universal_password, 0, universal_password_len);
1505 vp_user_dn = pairfind(request->config_items,PW_LDAP_USERDN);
1506 res = nmasldap_get_password(conn->ld,vp_user_dn->vp_strvalue,&universal_password_len,universal_password);
1509 passwd_val = universal_password;
1511 if (inst->passwd_hdr && strlen(inst->passwd_hdr)){
1512 passwd_val = strstr(passwd_val,inst->passwd_hdr);
1514 if (passwd_val != NULL)
1515 passwd_val += strlen((char*)inst->passwd_hdr);
1517 DEBUG("rlm_ldap: Password header not found in password %s for user %s ",passwd_val,request->username->vp_strvalue);
1521 if ((passwd_item = paircreate(PW_CLEARTEXT_PASSWORD,PW_TYPE_STRING)) == NULL){
1522 radlog(L_ERR, "rlm_ldap: Could not allocate memory. Aborting.");
1523 ldap_msgfree(result);
1524 ldap_release_conn(conn_id,inst->conns);
1525 memset(universal_password, 0, universal_password_len);
1526 free(universal_password);
1527 return RLM_MODULE_FAIL;
1530 passwd_len = strlen(passwd_val);
1531 strlcpy(passwd_item->vp_strvalue,passwd_val,sizeof(passwd_item->vp_strvalue));
1532 passwd_item->length = (passwd_len > (MAX_STRING_LEN - 1)) ? (MAX_STRING_LEN - 1) : passwd_len;
1533 pairadd(&request->config_items,passwd_item);
1538 VALUE_PAIR *vp_inst, *vp_apc;
1539 int inst_attr, apc_attr;
1541 dattr = dict_attrbyname("LDAP-Instance");
1542 inst_attr = dattr->attr;
1543 dattr = dict_attrbyname("eDir-APC");
1544 apc_attr = dattr->attr;
1546 vp_inst = pairfind(request->config_items, inst_attr);
1547 if(vp_inst == NULL){
1549 * The authorize method of no other LDAP module instance has
1550 * processed this request.
1552 if ((vp_inst = paircreate(inst_attr, PW_TYPE_STRING)) == NULL){
1553 radlog(L_ERR, "rlm_ldap: Could not allocate memory. Aborting.");
1554 ldap_msgfree(result);
1555 ldap_release_conn(conn_id, inst->conns);
1556 memset(universal_password, 0, universal_password_len);
1557 free(universal_password);
1558 return RLM_MODULE_FAIL;
1560 strcpy(vp_inst->vp_strvalue, inst->xlat_name);
1561 vp_inst->length = strlen(vp_inst->vp_strvalue);
1562 pairadd(&request->config_items, vp_inst);
1565 * Inform the authenticate / post-auth method about the presence
1566 * of UP in the config items list and whether eDirectory account
1567 * policy check is to be performed or not.
1569 if ((vp_apc = paircreate(apc_attr, PW_TYPE_INTEGER)) == NULL){
1570 radlog(L_ERR, "rlm_ldap: Could not allocate memory. Aborting.");
1571 ldap_msgfree(result);
1572 ldap_release_conn(conn_id, inst->conns);
1573 memset(universal_password, 0, universal_password_len);
1574 free(universal_password);
1575 return RLM_MODULE_FAIL;
1578 if(!inst->edir_account_policy_check){
1580 strcpy(vp_apc->vp_strvalue, "1");
1582 /* Perform eDirectory account-policy check */
1583 strcpy(vp_apc->vp_strvalue, "2");
1586 pairadd(&request->config_items, vp_apc);
1591 DEBUG("rlm_ldap: Added the eDirectory password %s in check items as %s",passwd_item->vp_strvalue,passwd_item->name);
1595 DEBUG("rlm_ldap: Error reading Universal Password.Return Code = %d",res);
1598 memset(universal_password, 0, universal_password_len);
1599 free(universal_password);
1607 DEBUG("rlm_ldap: looking for check items in directory...");
1609 if ((check_tmp = ldap_pairget(conn->ld, msg, inst->check_item_map,check_pairs,1)) != NULL) {
1611 pairxlatmove(request, check_pairs, &check_tmp);
1612 pairfree(&check_tmp);
1615 pairadd(check_pairs,check_tmp);
1619 DEBUG("rlm_ldap: looking for reply items in directory...");
1622 if ((reply_tmp = ldap_pairget(conn->ld, msg, inst->reply_item_map,reply_pairs,0)) != NULL) {
1624 pairxlatmove(request, reply_pairs, &reply_tmp);
1625 pairfree(&reply_tmp);
1628 pairadd(reply_pairs,reply_tmp);
1631 if (inst->do_comp && paircompare(request,request->packet->vps,*check_pairs,reply_pairs) != 0){
1633 /* Don't perform eDirectory APC if RADIUS authorize fails */
1638 dattr = dict_attrbyname("eDir-APC");
1639 apc_attr = dattr->attr;
1641 vp_apc = pairfind(request->config_items, apc_attr);
1643 vp_apc->vp_strvalue[0] = '1';
1646 DEBUG("rlm_ldap: Pairs do not match. Rejecting user.");
1647 snprintf(module_fmsg,sizeof(module_fmsg),"rlm_ldap: Pairs do not match");
1648 module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
1649 pairadd(&request->packet->vps, module_fmsg_vp);
1650 ldap_msgfree(result);
1651 ldap_release_conn(conn_id,inst->conns);
1653 return RLM_MODULE_REJECT;
1657 * Module should default to LDAP authentication if no Auth-Type
1658 * specified. Note that we do this ONLY if configured, AND we
1659 * set the Auth-Type to our module name, which allows multiple
1660 * ldap instances to work.
1662 if (inst->set_auth_type &&
1663 (pairfind(*check_pairs, PW_AUTH_TYPE) == NULL) &&
1664 request->password &&
1665 (request->password->attribute == PW_USER_PASSWORD)) {
1666 pairadd(check_pairs, pairmake("Auth-Type", inst->xlat_name, T_OP_EQ));
1667 DEBUG("rlm_ldap: Setting Auth-Type = %s", inst->xlat_name);
1670 DEBUG("rlm_ldap: user %s authorized to use remote access",
1671 request->username->vp_strvalue);
1672 ldap_msgfree(result);
1673 ldap_release_conn(conn_id,inst->conns);
1675 return RLM_MODULE_OK;
1678 /*****************************************************************************
1680 * Function: rlm_ldap_authenticate
1682 * Purpose: Check the user's password against ldap database
1684 *****************************************************************************/
1685 static int ldap_authenticate(void *instance, REQUEST * request)
1688 LDAPMessage *result, *msg;
1689 ldap_instance *inst = instance;
1690 char *user_dn, *attrs[] = {"uid", NULL};
1691 char filter[MAX_FILTER_STR_LEN];
1692 char basedn[MAX_FILTER_STR_LEN];
1694 VALUE_PAIR *vp_user_dn;
1695 VALUE_PAIR *module_fmsg_vp;
1696 char module_fmsg[MAX_STRING_LEN];
1703 DEBUG("rlm_ldap: - authenticate");
1706 * Ensure that we're being passed a plain-text password, and not
1710 if (!request->username) {
1711 radlog(L_AUTH, "rlm_ldap: Attribute \"User-Name\" is required for authentication.\n");
1712 return RLM_MODULE_INVALID;
1715 if (!request->password){
1716 radlog(L_AUTH, "rlm_ldap: Attribute \"User-Password\" is required for authentication.");
1717 DEBUG2(" You seem to have set \"Auth-Type := LDAP\" somewhere.");
1718 DEBUG2(" THAT CONFIGURATION IS WRONG. DELETE IT.");
1719 DEBUG2(" YOU ARE PREVENTING THE SERVER FROM WORKING PROPERLY.");
1720 return RLM_MODULE_INVALID;
1723 if(request->password->attribute != PW_USER_PASSWORD) {
1724 radlog(L_AUTH, "rlm_ldap: Attribute \"User-Password\" is required for authentication. Cannot use \"%s\".", request->password->name);
1725 return RLM_MODULE_INVALID;
1728 if (request->password->length == 0) {
1729 snprintf(module_fmsg,sizeof(module_fmsg),"rlm_ldap: empty password supplied");
1730 module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
1731 pairadd(&request->packet->vps, module_fmsg_vp);
1732 return RLM_MODULE_INVALID;
1736 * Check that we don't have any failed connections. If we do there's no real need
1737 * of runing. Also give it another chance if we have a lot of failed connections.
1739 if (inst->failed_conns > MAX_FAILED_CONNS_END)
1740 inst->failed_conns = 0;
1741 if (inst->failed_conns > MAX_FAILED_CONNS_START){
1742 inst->failed_conns++;
1743 return RLM_MODULE_FAIL;
1747 DEBUG("rlm_ldap: login attempt by \"%s\" with password \"%s\"",
1748 request->username->vp_strvalue, request->password->vp_strvalue);
1750 while ((vp_user_dn = pairfind(request->config_items,
1751 PW_LDAP_USERDN)) == NULL) {
1752 if (!radius_xlat(filter, sizeof(filter), inst->filter,
1753 request, ldap_escape_func)) {
1754 radlog (L_ERR, "rlm_ldap: unable to create filter.\n");
1755 return RLM_MODULE_INVALID;
1758 if (!radius_xlat(basedn, sizeof(basedn), inst->basedn,
1759 request, ldap_escape_func)) {
1760 radlog (L_ERR, "rlm_ldap: unable to create basedn.\n");
1761 return RLM_MODULE_INVALID;
1764 if ((conn_id = ldap_get_conn(inst->conns,&conn,inst)) == -1){
1765 radlog(L_ERR, "rlm_ldap: All ldap connections are in use");
1766 return RLM_MODULE_FAIL;
1768 if ((res = perform_search(instance, conn, basedn, LDAP_SCOPE_SUBTREE, filter, attrs, &result)) != RLM_MODULE_OK) {
1769 if (res == RLM_MODULE_NOTFOUND){
1770 snprintf(module_fmsg,sizeof(module_fmsg),"rlm_ldap: User not found");
1771 module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
1772 pairadd(&request->packet->vps, module_fmsg_vp);
1774 ldap_release_conn(conn_id,inst->conns);
1777 if ((msg = ldap_first_entry(conn->ld, result)) == NULL) {
1778 ldap_msgfree(result);
1779 ldap_release_conn(conn_id,inst->conns);
1780 return RLM_MODULE_FAIL;
1782 if ((user_dn = ldap_get_dn(conn->ld, msg)) == NULL) {
1783 DEBUG("rlm_ldap: ldap_get_dn() failed");
1784 ldap_msgfree(result);
1785 ldap_release_conn(conn_id,inst->conns);
1786 return RLM_MODULE_FAIL;
1788 ldap_release_conn(conn_id,inst->conns);
1789 pairadd(&request->config_items, pairmake("Ldap-UserDn", user_dn, T_OP_EQ));
1790 ldap_memfree(user_dn);
1791 ldap_msgfree(result);
1794 user_dn = vp_user_dn->vp_strvalue;
1796 DEBUG("rlm_ldap: user DN: %s", user_dn);
1799 ld_user = ldap_connect(instance, user_dn, request->password->vp_strvalue,
1803 ld_user = ldap_connect(instance, user_dn, request->password->vp_strvalue,
1807 /* 'err' contains the LDAP connection error description */
1808 DEBUG("rlm_ldap: %s", err);
1809 pairadd(&request->reply->vps, pairmake("Reply-Message", err, T_OP_EQ));
1810 ldap_memfree((void *)err);
1813 /* Don't perform eDirectory APC again after attempting to bind here. */
1819 dattr = dict_attrbyname("eDir-APC");
1820 apc_attr = dattr->attr;
1821 vp_apc = pairfind(request->config_items, apc_attr);
1822 if(vp_apc && vp_apc->vp_strvalue[0] == '2')
1823 vp_apc->vp_strvalue[0] = '3';
1827 if (ld_user == NULL){
1828 if (res == RLM_MODULE_REJECT){
1829 inst->failed_conns = 0;
1830 snprintf(module_fmsg,sizeof(module_fmsg),"rlm_ldap: Bind as user failed");
1831 module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
1832 pairadd(&request->packet->vps, module_fmsg_vp);
1834 if (res == RLM_MODULE_FAIL){
1835 DEBUG("rlm_ldap: ldap_connect() failed");
1836 inst->failed_conns++;
1841 DEBUG("rlm_ldap: user %s authenticated succesfully",
1842 request->username->vp_strvalue);
1843 ldap_unbind_s(ld_user);
1844 inst->failed_conns = 0;
1846 return RLM_MODULE_OK;
1850 /*****************************************************************************
1852 * Function: rlm_ldap_postauth
1854 * Purpose: Perform eDirectory account policy check and failed-login reporting
1857 *****************************************************************************/
1858 static int ldap_postauth(void *instance, REQUEST * request)
1860 int res = RLM_MODULE_FAIL;
1861 int inst_attr, apc_attr;
1862 char password[UNIVERSAL_PASS_LEN];
1863 ldap_instance *inst = instance;
1865 VALUE_PAIR *vp_inst, *vp_apc;
1868 dattr = dict_attrbyname("LDAP-Instance");
1869 inst_attr = dattr->attr;
1870 dattr = dict_attrbyname("eDir-APC");
1871 apc_attr = dattr->attr;
1873 vp_inst = pairfind(request->config_items, inst_attr);
1876 * Check if the password in the config items list is the user's UP which has
1877 * been read in the authorize method of this instance of the LDAP module.
1879 if((vp_inst == NULL) || strcmp(vp_inst->vp_strvalue, inst->xlat_name))
1880 return RLM_MODULE_NOOP;
1882 vp_apc = pairfind(request->config_items, apc_attr);
1884 switch(vp_apc->vp_strvalue[0]){
1886 /* Account policy check not enabled */
1888 /* Account policy check has been completed */
1889 res = RLM_MODULE_NOOP;
1893 int err, conn_id = -1;
1894 char *error_msg = NULL;
1895 VALUE_PAIR *vp_fdn, *vp_pwd;
1898 if (request->reply->code == PW_AUTHENTICATION_REJECT) {
1899 /* Bind to eDirectory as the RADIUS user with a wrong password. */
1900 vp_pwd = pairfind(request->config_items, PW_CLEARTEXT_PASSWORD);
1901 strcpy(password, vp_pwd->vp_strvalue);
1902 if (strlen(password) > 0) {
1903 if (password[0] != 'a') {
1909 strcpy(password, "dummy_password");
1911 res = RLM_MODULE_REJECT;
1913 /* Bind to eDirectory as the RADIUS user using the user's UP */
1914 vp_pwd = pairfind(request->config_items, PW_CLEARTEXT_PASSWORD);
1915 if (vp_pwd == NULL) {
1916 DEBUG("rlm_ldap: User's Universal Password not in config items list.");
1917 return RLM_MODULE_FAIL;
1919 strcpy(password, vp_pwd->vp_strvalue);
1922 if ((da = dict_attrbyname("Ldap-UserDn")) == NULL) {
1923 DEBUG("rlm_ldap: Attribute for user FDN not found in dictionary. Unable to proceed");
1924 return RLM_MODULE_FAIL;
1927 vp_fdn = pairfind(request->packet->vps, da->attr);
1928 if (vp_fdn == NULL) {
1929 DEBUG("rlm_ldap: User's FQDN not in config items list.");
1930 return RLM_MODULE_FAIL;
1933 if ((conn_id = ldap_get_conn(inst->apc_conns, &conn, inst)) == -1){
1934 radlog(L_ERR, "rlm_ldap: All ldap connections are in use");
1935 return RLM_MODULE_FAIL;
1939 * If there is an existing LDAP
1940 * connection to the directory,
1941 * bind over it. Otherwise,
1942 * establish a new connection.
1945 if (!conn->bound || conn->ld == NULL) {
1946 DEBUG2("rlm_ldap: attempting LDAP reconnection");
1948 DEBUG2("rlm_ldap: closing existing LDAP connection");
1949 ldap_unbind_s(conn->ld);
1951 if ((conn->ld = ldap_connect(instance, (char *)vp_fdn->vp_strvalue, password, 0, &res, &error_msg)) == NULL) {
1952 radlog(L_ERR, "rlm_ldap: eDirectory account policy check failed.");
1954 if (error_msg != NULL) {
1955 DEBUG("rlm_ldap: %s", error_msg);
1956 pairadd(&request->reply->vps, pairmake("Reply-Message", error_msg, T_OP_EQ));
1957 ldap_memfree((void *)error_msg);
1960 vp_apc->vp_strvalue[0] = '3';
1961 ldap_release_conn(conn_id, inst->apc_conns);
1962 return RLM_MODULE_REJECT;
1965 } else if((err = ldap_simple_bind_s(conn->ld, (char *)vp_fdn->vp_strvalue, password)) != LDAP_SUCCESS) {
1966 if (err == LDAP_SERVER_DOWN) {
1968 goto postauth_reconnect;
1970 DEBUG("rlm_ldap: eDirectory account policy check failed.");
1971 ldap_get_option(conn->ld, LDAP_OPT_ERROR_STRING, &error_msg);
1972 if (error_msg != NULL) {
1973 DEBUG("rlm_ldap: %s", error_msg);
1974 pairadd(&request->reply->vps, pairmake("Reply-Message", error_msg, T_OP_EQ));
1975 ldap_memfree((void *)error_msg);
1977 vp_apc->vp_strvalue[0] = '3';
1978 ldap_release_conn(conn_id, inst->apc_conns);
1979 return RLM_MODULE_REJECT;
1981 vp_apc->vp_strvalue[0] = '3';
1982 ldap_release_conn(conn_id, inst->apc_conns);
1983 return RLM_MODULE_OK;
1990 static LDAP *ldap_connect(void *instance, const char *dn, const char *password,
1991 int auth, int *result, char **err)
1993 ldap_instance *inst = instance;
1995 int msgid, rc, ldap_version;
2000 #ifdef HAVE_LDAP_INITIALIZE
2001 DEBUG("rlm_ldap: (re)connect to %s, authentication %d", inst->server, auth);
2002 if (ldap_initialize(&ld, inst->server) != LDAP_SUCCESS) {
2003 radlog(L_ERR, "rlm_ldap: ldap_initialize() failed");
2004 *result = RLM_MODULE_FAIL;
2009 DEBUG("rlm_ldap: (re)connect to %s:%d, authentication %d", inst->server, inst->port, auth);
2010 if ((ld = ldap_init(inst->server, inst->port)) == NULL) {
2011 radlog(L_ERR, "rlm_ldap: ldap_init() failed");
2012 *result = RLM_MODULE_FAIL;
2016 if (ldap_set_option(ld, LDAP_OPT_NETWORK_TIMEOUT,
2017 (void *) &(inst->net_timeout)) != LDAP_OPT_SUCCESS) {
2018 radlog(L_ERR, "rlm_ldap: Could not set LDAP_OPT_NETWORK_TIMEOUT %ld.%ld", inst->net_timeout.tv_sec, inst->net_timeout.tv_usec);
2021 if (ldap_set_option(ld, LDAP_OPT_TIMELIMIT,
2022 (void *) &(inst->timelimit)) != LDAP_OPT_SUCCESS) {
2023 radlog(L_ERR, "rlm_ldap: Could not set LDAP_OPT_TIMELIMIT %d", inst->timelimit);
2026 if (inst->ldap_debug && ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &(inst->ldap_debug)) != LDAP_OPT_SUCCESS) {
2027 radlog(L_ERR, "rlm_ldap: Could not set LDAP_OPT_DEBUG_LEVEL %d", inst->ldap_debug);
2030 ldap_version = LDAP_VERSION3;
2031 if (ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION,
2032 &ldap_version) != LDAP_OPT_SUCCESS) {
2033 radlog(L_ERR, "rlm_ldap: Could not set LDAP version to V3");
2036 #ifdef HAVE_LDAP_START_TLS
2037 if (inst->tls_mode) {
2038 DEBUG("rlm_ldap: setting TLS mode to %d", inst->tls_mode);
2039 if (ldap_set_option(ld, LDAP_OPT_X_TLS,
2040 (void *) &(inst->tls_mode)) != LDAP_OPT_SUCCESS) {
2041 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
2042 radlog(L_ERR, "rlm_ldap: could not set LDAP_OPT_X_TLS option %s", ldap_err2string(ldap_errno));
2046 if (inst->tls_cacertfile != NULL) {
2047 DEBUG("rlm_ldap: setting TLS CACert File to %s", inst->tls_cacertfile);
2049 if ( ldap_set_option( NULL, LDAP_OPT_X_TLS_CACERTFILE,
2050 (void *) inst->tls_cacertfile )
2051 != LDAP_OPT_SUCCESS) {
2052 radlog(L_ERR, "rlm_ldap: could not set "
2053 "LDAP_OPT_X_TLS_CACERTFILE option to %s", inst->tls_cacertfile);
2057 if (inst->tls_cacertdir != NULL) {
2058 DEBUG("rlm_ldap: setting TLS CACert Directory to %s", inst->tls_cacertdir);
2060 if ( ldap_set_option( NULL, LDAP_OPT_X_TLS_CACERTDIR,
2061 (void *) inst->tls_cacertdir )
2062 != LDAP_OPT_SUCCESS) {
2063 radlog(L_ERR, "rlm_ldap: could not set "
2064 "LDAP_OPT_X_TLS_CACERTDIR option to %s", inst->tls_cacertdir);
2068 if (strcmp(TLS_DEFAULT_VERIFY, inst->tls_require_cert ) != 0 ) {
2069 DEBUG("rlm_ldap: setting TLS Require Cert to %s",
2070 inst->tls_require_cert);
2074 #ifdef HAVE_LDAP_INT_TLS_CONFIG
2075 if (ldap_int_tls_config(NULL, LDAP_OPT_X_TLS_REQUIRE_CERT,
2076 (inst->tls_require_cert)) != LDAP_OPT_SUCCESS) {
2077 radlog(L_ERR, "rlm_ldap: could not set "
2078 "LDAP_OPT_X_TLS_REQUIRE_CERT option to %s",
2079 inst->tls_require_cert);
2083 if (inst->tls_certfile != NULL) {
2084 DEBUG("rlm_ldap: setting TLS Cert File to %s", inst->tls_certfile);
2086 if (ldap_set_option(NULL, LDAP_OPT_X_TLS_CERTFILE,
2087 (void *) inst->tls_certfile)
2088 != LDAP_OPT_SUCCESS) {
2089 radlog(L_ERR, "rlm_ldap: could not set "
2090 "LDAP_OPT_X_TLS_CERTFILE option to %s",
2091 inst->tls_certfile);
2095 if (inst->tls_keyfile != NULL) {
2096 DEBUG("rlm_ldap: setting TLS Key File to %s",
2099 if ( ldap_set_option( NULL, LDAP_OPT_X_TLS_KEYFILE,
2100 (void *) inst->tls_keyfile )
2101 != LDAP_OPT_SUCCESS) {
2102 radlog(L_ERR, "rlm_ldap: could not set "
2103 "LDAP_OPT_X_TLS_KEYFILE option to %s",
2108 if (inst->tls_randfile != NULL) {
2109 DEBUG("rlm_ldap: setting TLS Key File to %s",
2110 inst->tls_randfile);
2112 if (ldap_set_option(NULL, LDAP_OPT_X_TLS_RANDOM_FILE,
2113 (void *) inst->tls_randfile)
2114 != LDAP_OPT_SUCCESS) {
2115 radlog(L_ERR, "rlm_ldap: could not set "
2116 "LDAP_OPT_X_TLS_RANDOM_FILE option to %s",
2117 inst->tls_randfile);
2121 if (inst->start_tls) {
2122 DEBUG("rlm_ldap: starting TLS");
2123 rc = ldap_start_tls_s(ld, NULL, NULL);
2124 if (rc != LDAP_SUCCESS) {
2125 DEBUG("rlm_ldap: ldap_start_tls_s()");
2126 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER,
2128 radlog(L_ERR, "rlm_ldap: could not start TLS %s",
2129 ldap_err2string(ldap_errno));
2130 *result = RLM_MODULE_FAIL;
2135 #endif /* HAVE_LDAP_START_TLS */
2138 DEBUG("rlm_ldap: bind as %s/%s to %s",
2139 dn, password, inst->server);
2141 DEBUG("rlm_ldap: bind as %s/%s to %s:%d",
2142 dn, password, inst->server, inst->port);
2145 msgid = ldap_bind(ld, dn, password,LDAP_AUTH_SIMPLE);
2147 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
2149 ldap_get_option(ld, LDAP_OPT_ERROR_STRING, err);
2152 radlog(L_ERR, "rlm_ldap: %s bind to %s failed: %s",
2153 dn, inst->server, ldap_err2string(ldap_errno));
2155 radlog(L_ERR, "rlm_ldap: %s bind to %s:%d failed: %s",
2156 dn, inst->server, inst->port,
2157 ldap_err2string(ldap_errno));
2159 *result = RLM_MODULE_FAIL;
2163 DEBUG("rlm_ldap: waiting for bind result ...");
2165 rc = ldap_result(ld, msgid, 1, &(inst->timeout), &res);
2168 DEBUG("rlm_ldap: ldap_result()");
2169 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
2171 ldap_get_option(ld, LDAP_OPT_ERROR_STRING, err);
2174 radlog(L_ERR, "rlm_ldap: %s bind to %s failed: %s",
2175 dn, inst->server, (rc == 0) ? "timeout" : ldap_err2string(ldap_errno));
2177 radlog(L_ERR, "rlm_ldap: %s bind to %s:%d failed: %s",
2178 dn, inst->server, inst->port,
2179 (rc == 0) ? "timeout" : ldap_err2string(ldap_errno));
2181 *result = RLM_MODULE_FAIL;
2186 ldap_errno = ldap_result2error(ld, res, 1);
2187 switch (ldap_errno) {
2189 DEBUG("rlm_ldap: Bind was successful");
2190 *result = RLM_MODULE_OK;
2193 case LDAP_INVALID_CREDENTIALS:
2195 DEBUG("rlm_ldap: Bind failed with invalid credentials");
2196 *result = RLM_MODULE_REJECT;
2198 radlog(L_ERR, "rlm_ldap: LDAP login failed: check identity, password settings in ldap section of radiusd.conf");
2199 *result = RLM_MODULE_FAIL;
2202 ldap_get_option(ld, LDAP_OPT_ERROR_STRING, err);
2208 radlog(L_ERR,"rlm_ldap: %s bind to %s failed %s",
2209 dn, inst->server, ldap_err2string(ldap_errno));
2211 radlog(L_ERR,"rlm_ldap: %s bind to %s:%d failed %s",
2212 dn, inst->server, inst->port,
2213 ldap_err2string(ldap_errno));
2215 *result = RLM_MODULE_FAIL;
2217 ldap_get_option(ld, LDAP_OPT_ERROR_STRING, err);
2221 if (*result != RLM_MODULE_OK) {
2228 /*****************************************************************************
2230 * Detach from the LDAP server and cleanup internal state.
2232 *****************************************************************************/
2234 ldap_detach(void *instance)
2236 ldap_instance *inst = instance;
2237 TLDAP_RADIUS *pair, *nextpair;
2242 for (i = 0;i < inst->num_conns; i++) {
2243 if (inst->conns[i].ld){
2244 ldap_unbind_s(inst->conns[i].ld);
2246 pthread_mutex_destroy(&inst->conns[i].mutex);
2252 if (inst->apc_conns){
2255 for (i = 0; i < inst->num_conns; i++) {
2256 if (inst->apc_conns[i].ld){
2257 ldap_unbind_s(inst->apc_conns[i].ld);
2259 pthread_mutex_destroy(&inst->apc_conns[i].mutex);
2261 free(inst->apc_conns);
2265 pair = inst->check_item_map;
2267 while (pair != NULL) {
2268 nextpair = pair->next;
2270 free(pair->radius_attr);
2275 pair = inst->reply_item_map;
2277 while (pair != NULL) {
2278 nextpair = pair->next;
2280 free(pair->radius_attr);
2288 paircompare_unregister(PW_LDAP_GROUP, ldap_groupcmp);
2289 xlat_unregister(inst->xlat_name,ldap_xlat);
2290 free(inst->xlat_name);
2300 fieldcpy(char *string, char **uptr)
2305 while (*ptr == ' ' || *ptr == '\t') {
2310 while (*ptr != '"' && *ptr != '\0' && *ptr != '\n') {
2320 while (*ptr != ' ' && *ptr != '\t' && *ptr != '\0' && *ptr != '\n' &&
2321 *ptr != '=' && *ptr != ',') {
2331 * Copied from src/lib/token.c
2333 static const LRAD_NAME_NUMBER tokens[] = {
2334 { "=~", T_OP_REG_EQ, }, /* order is important! */
2335 { "!~", T_OP_REG_NE, },
2336 { "{", T_LCBRACE, },
2337 { "}", T_RCBRACE, },
2341 { "+=", T_OP_ADD, },
2342 { "-=", T_OP_SUB, },
2343 { ":=", T_OP_SET, },
2344 { "=*", T_OP_CMP_TRUE, },
2345 { "!*", T_OP_CMP_FALSE, },
2346 { "==", T_OP_CMP_EQ, },
2356 /*****************************************************************************
2357 * Get RADIUS attributes from LDAP object
2358 * ( according to draft-adoba-radius-05.txt
2359 * <http://www.ietf.org/internet-drafts/draft-adoba-radius-05.txt> )
2361 *****************************************************************************/
2362 static VALUE_PAIR *ldap_pairget(LDAP *ld, LDAPMessage *entry,
2363 TLDAP_RADIUS *item_map,
2364 VALUE_PAIR **pairs, int is_check)
2371 TLDAP_RADIUS *element;
2372 LRAD_TOKEN token, operator;
2373 int is_generic_attribute;
2374 char buf[MAX_STRING_LEN];
2375 VALUE_PAIR *pairlist = NULL;
2376 VALUE_PAIR *newpair = NULL;
2377 char do_xlat = FALSE;
2380 * check if there is a mapping from this LDAP attribute
2381 * to a RADIUS attribute
2383 for (element = item_map; element != NULL; element = element->next) {
2385 * No mapping, skip it.
2387 if ((vals = ldap_get_values(ld,entry,element->attr)) == NULL)
2391 * Check whether this is a one-to-one-mapped ldap
2392 * attribute or a generic attribute and set flag
2395 if (strcasecmp(element->radius_attr, GENERIC_ATTRIBUTE_ID)==0)
2396 is_generic_attribute = 1;
2398 is_generic_attribute = 0;
2401 * Find out how many values there are for the
2402 * attribute and extract all of them.
2404 vals_count = ldap_count_values(vals);
2406 for (vals_idx = 0; vals_idx < vals_count; vals_idx++) {
2407 value = vals[vals_idx];
2409 if (is_generic_attribute) {
2411 * This is a generic attribute.
2413 LRAD_TOKEN dummy; /* makes pairread happy */
2415 /* not sure if using pairread here is ok ... */
2416 if ( (newpair = pairread(&value, &dummy)) != NULL) {
2417 DEBUG("rlm_ldap: extracted attribute %s from generic item %s",
2418 newpair->name, vals[vals_idx]);
2419 pairadd(&pairlist, newpair);
2421 radlog(L_ERR, "rlm_ldap: parsing %s failed: %s",
2422 element->attr, vals[vals_idx]);
2426 * This is a one-to-one-mapped attribute
2429 operator = gettoken(&ptr, buf, sizeof(buf));
2430 if (operator < T_EQSTART || operator > T_EQEND) {
2431 /* no leading operator found */
2432 if (element->operator != T_OP_INVALID)
2433 operator = element->operator;
2435 operator = T_OP_CMP_EQ;
2439 /* the value is after the operator */
2444 * Do xlat if the *entire* string
2447 if ((value[0] == '\'' || value[0] == '"' ||
2449 (value[0] == value[strlen(value)-1])) {
2451 token = gettoken(&ptr, buf, sizeof(buf));
2453 /* take the unquoted string */
2454 case T_SINGLE_QUOTED_STRING:
2455 case T_DOUBLE_QUOTED_STRING:
2459 /* the value will be xlat'ed later */
2460 case T_BACK_QUOTED_STRING:
2465 /* keep the original string */
2470 if (value[0] == '\0') {
2471 DEBUG("rlm_ldap: Attribute %s has no value", element->attr);
2475 DEBUG("rlm_ldap: Adding LDAP attribute %s as RADIUS attribute %s %s %s",
2476 element->attr, element->radius_attr,
2477 lrad_int2str(tokens, operator, "?"),
2483 newpair = pairmake(element->radius_attr,
2484 do_xlat ? NULL : value,
2486 if (newpair == NULL) {
2487 radlog(L_ERR, "rlm_ldap: Failed to create the pair: %s", librad_errstr);
2491 newpair->flags.do_xlat = 1;
2492 strlcpy(newpair->vp_strvalue, buf,
2493 sizeof(newpair->vp_strvalue));
2494 newpair->length = 0;
2498 * Add the pair into the packet.
2501 pairdelete(pairs, newpair->attribute);
2503 pairadd(&pairlist, newpair);
2506 ldap_value_free(vals);
2512 /* globally exported name */
2513 module_t rlm_ldap = {
2516 RLM_TYPE_THREAD_SAFE, /* type: reserved */
2517 ldap_instantiate, /* instantiation */
2518 ldap_detach, /* detach */
2520 ldap_authenticate, /* authentication */
2521 ldap_authorize, /* authorization */
2522 NULL, /* preaccounting */
2523 NULL, /* accounting */
2524 NULL, /* checksimul */
2525 NULL, /* pre-proxy */
2526 NULL, /* post-proxy */
2528 ldap_postauth /* post-auth */