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 */
96 #define REQUEST_ACCEPTED 0
97 #define REQUEST_CHALLENGED 1
98 #define REQUEST_REJECTED 2
99 #define MAX_CHALLENGE_LEN 128
101 int radLdapXtnNMASAuth( LDAP *, char *, char *, char *, char *, size_t *, char *, int * );
105 /* linked list of mappings between RADIUS attributes and LDAP attributes */
106 struct TLDAP_RADIUS {
110 struct TLDAP_RADIUS* next;
112 typedef struct TLDAP_RADIUS TLDAP_RADIUS;
114 typedef struct ldap_conn {
119 #ifdef HAVE_PTHREAD_H
120 pthread_mutex_t mutex;
128 struct timeval net_timeout;
129 struct timeval timeout;
144 char *default_profile;
150 char *dictionary_mapping;
151 char *groupname_attr;
152 char *groupmemb_filt;
153 char *groupmemb_attr;
155 TLDAP_RADIUS *check_item_map;
156 TLDAP_RADIUS *reply_item_map;
159 LDAP_CONN *apc_conns;
161 int ldap_debug; /* Debug flag for LDAP SDK */
162 char *xlat_name; /* name used to xlat */
163 char *tls_cacertfile;
168 char *tls_require_cert;
170 int edir_account_policy_check;
175 /* The default setting for TLS Certificate Verification */
176 #define TLS_DEFAULT_VERIFY "allow"
178 static CONF_PARSER tls_config[] = {
179 {"start_tls", PW_TYPE_BOOLEAN,
180 offsetof(ldap_instance,start_tls), NULL, "no"},
181 {"cacertfile", PW_TYPE_FILENAME,
182 offsetof(ldap_instance,tls_cacertfile), NULL, NULL},
183 {"cacertdir", PW_TYPE_FILENAME,
184 offsetof(ldap_instance,tls_cacertdir), NULL, NULL},
185 {"certfile", PW_TYPE_FILENAME,
186 offsetof(ldap_instance,tls_certfile), NULL, NULL},
187 {"keyfile", PW_TYPE_FILENAME,
188 offsetof(ldap_instance,tls_keyfile), NULL, NULL},
189 {"randfile", PW_TYPE_STRING_PTR, /* OK if it changes on HUP */
190 offsetof(ldap_instance,tls_randfile), NULL, NULL},
191 {"require_cert", PW_TYPE_STRING_PTR,
192 offsetof(ldap_instance,tls_require_cert), NULL, TLS_DEFAULT_VERIFY},
193 { NULL, -1, 0, NULL, NULL }
196 static const CONF_PARSER module_config[] = {
197 {"server", PW_TYPE_STRING_PTR,
198 offsetof(ldap_instance,server), NULL, "localhost"},
199 {"port", PW_TYPE_INTEGER,
200 offsetof(ldap_instance,port), NULL, "389"},
201 {"password", PW_TYPE_STRING_PTR,
202 offsetof(ldap_instance,password), NULL, ""},
203 {"identity", PW_TYPE_STRING_PTR,
204 offsetof(ldap_instance,login), NULL, ""},
209 /* wait forever on network activity */
210 {"net_timeout", PW_TYPE_INTEGER,
211 offsetof(ldap_instance,net_timeout.tv_sec), NULL, "10"},
212 /* wait forever for search results */
213 {"timeout", PW_TYPE_INTEGER,
214 offsetof(ldap_instance,timeout.tv_sec), NULL, "20"},
215 /* allow server unlimited time for search (server-side limit) */
216 {"timelimit", PW_TYPE_INTEGER,
217 offsetof(ldap_instance,timelimit), NULL, "20"},
220 * TLS configuration The first few are here for backwards
221 * compatibility. The last is the new subsection.
223 {"tls_mode", PW_TYPE_BOOLEAN,
224 offsetof(ldap_instance,tls_mode), NULL, "no"},
226 {"start_tls", PW_TYPE_BOOLEAN,
227 offsetof(ldap_instance,start_tls), NULL, "no"},
228 {"tls_cacertfile", PW_TYPE_FILENAME,
229 offsetof(ldap_instance,tls_cacertfile), NULL, NULL},
230 {"tls_cacertdir", PW_TYPE_FILENAME,
231 offsetof(ldap_instance,tls_cacertdir), NULL, NULL},
232 {"tls_certfile", PW_TYPE_FILENAME,
233 offsetof(ldap_instance,tls_certfile), NULL, NULL},
234 {"tls_keyfile", PW_TYPE_FILENAME,
235 offsetof(ldap_instance,tls_keyfile), NULL, NULL},
236 {"tls_randfile", PW_TYPE_STRING_PTR, /* OK if it changes on HUP */
237 offsetof(ldap_instance,tls_randfile), NULL, NULL},
238 {"tls_require_cert", PW_TYPE_STRING_PTR,
239 offsetof(ldap_instance,tls_require_cert), NULL, TLS_DEFAULT_VERIFY},
240 { "tls", PW_TYPE_SUBSECTION, 0, NULL, (const void *) tls_config },
245 {"basedn", PW_TYPE_STRING_PTR,
246 offsetof(ldap_instance,basedn), NULL, "o=notexist"},
247 {"filter", PW_TYPE_STRING_PTR,
248 offsetof(ldap_instance,filter), NULL, "(uid=%u)"},
249 {"base_filter", PW_TYPE_STRING_PTR,
250 offsetof(ldap_instance,base_filter), NULL, "(objectclass=radiusprofile)"},
251 {"default_profile", PW_TYPE_STRING_PTR,
252 offsetof(ldap_instance,default_profile), NULL, NULL},
253 {"profile_attribute", PW_TYPE_STRING_PTR,
254 offsetof(ldap_instance,profile_attr), NULL, NULL},
257 * Getting passwords from the database
259 {"password_header", PW_TYPE_STRING_PTR,
260 offsetof(ldap_instance,passwd_hdr), NULL, NULL},
261 {"password_attribute", PW_TYPE_STRING_PTR,
262 offsetof(ldap_instance,passwd_attr), NULL, NULL},
263 {"auto_header", PW_TYPE_BOOLEAN,
264 offsetof(ldap_instance,auto_header), NULL, "no"},
269 /* LDAP attribute name that controls remote access */
270 {"access_attr", PW_TYPE_STRING_PTR,
271 offsetof(ldap_instance,access_attr), NULL, NULL},
272 {"access_attr_used_for_allow", PW_TYPE_BOOLEAN,
273 offsetof(ldap_instance,default_allow), NULL, "yes"},
276 * Group checks. These could probably be done
277 * via dynamic xlat's.
279 {"groupname_attribute", PW_TYPE_STRING_PTR,
280 offsetof(ldap_instance,groupname_attr), NULL, "cn"},
281 {"groupmembership_filter", PW_TYPE_STRING_PTR,
282 offsetof(ldap_instance,groupmemb_filt), NULL, "(|(&(objectClass=GroupOfNames)(member=%{Ldap-UserDn}))(&(objectClass=GroupOfUniqueNames)(uniquemember=%{Ldap-UserDn})))"},
283 {"groupmembership_attribute", PW_TYPE_STRING_PTR,
284 offsetof(ldap_instance,groupmemb_attr), NULL, NULL},
286 /* file with mapping between LDAP and RADIUS attributes */
287 {"dictionary_mapping", PW_TYPE_FILENAME,
288 offsetof(ldap_instance,dictionary_mapping), NULL, "${confdir}/ldap.attrmap"},
291 * Debugging flags to the server
293 {"ldap_debug", PW_TYPE_INTEGER,
294 offsetof(ldap_instance,ldap_debug), NULL, "0x0000"},
295 {"ldap_connections_number", PW_TYPE_INTEGER,
296 offsetof(ldap_instance,num_conns), NULL, "5"},
297 {"compare_check_items", PW_TYPE_BOOLEAN,
298 offsetof(ldap_instance,do_comp), NULL, "no"},
299 {"do_xlat", PW_TYPE_BOOLEAN,
300 offsetof(ldap_instance,do_xlat), NULL, "yes"},
306 {"edir_account_policy_check", PW_TYPE_BOOLEAN,
307 offsetof(ldap_instance,edir_account_policy_check), NULL, "yes"},
310 {"set_auth_type", PW_TYPE_BOOLEAN, offsetof(ldap_instance,set_auth_type), NULL, "yes"},
311 {NULL, -1, 0, NULL, NULL}
314 #define ld_valid ld_options.ldo_valid
315 #define LDAP_VALID_SESSION 0x2
316 #define LDAP_VALID(ld) ( (ld)->ld_valid == LDAP_VALID_SESSION )
319 static void fieldcpy(char *, char **);
321 static VALUE_PAIR *ldap_pairget(LDAP *, LDAPMessage *, TLDAP_RADIUS *,VALUE_PAIR **,int);
322 static int ldap_groupcmp(void *, REQUEST *, VALUE_PAIR *, VALUE_PAIR *, VALUE_PAIR *, VALUE_PAIR **);
323 static int ldap_xlat(void *, REQUEST *, char *, char *, size_t, RADIUS_ESCAPE_STRING);
324 static LDAP *ldap_connect(void *instance, const char *, const char *, int, int *, char **);
325 static int read_mappings(ldap_instance* inst);
327 static inline int ldap_get_conn(LDAP_CONN *conns,LDAP_CONN **ret,void *instance)
329 ldap_instance *inst = instance;
332 for(i=0;i<inst->num_conns;i++){
333 DEBUG("rlm_ldap: ldap_get_conn: Checking Id: %d",i);
334 if ((pthread_mutex_trylock(&conns[i].mutex) == 0)) {
335 if (conns[i].locked == 1) {
336 /* connection is already being used */
337 pthread_mutex_unlock(&(conns[i].mutex));
340 /* found an unused connection */
343 DEBUG("rlm_ldap: ldap_get_conn: Got Id: %d",i);
351 static inline void ldap_release_conn(int i, LDAP_CONN *conns)
353 DEBUG("rlm_ldap: ldap_release_conn: Release Id: %d",i);
355 pthread_mutex_unlock(&(conns[i].mutex));
358 /*************************************************************************
360 * Function: rlm_ldap_instantiate
362 * Purpose: Uses section of radiusd config file passed as parameter
363 * to create an instance of the module.
365 *************************************************************************/
367 ldap_instantiate(CONF_SECTION * conf, void **instance)
372 int reply_map_num = 0;
373 int check_map_num = 0;
374 int att_map[3] = {0,0,0};
377 const char *xlat_name;
379 inst = rad_malloc(sizeof *inst);
383 memset(inst, 0, sizeof(*inst));
385 if (cf_section_parse(conf, inst, module_config) < 0) {
390 if (inst->server == NULL) {
391 radlog(L_ERR, "rlm_ldap: missing 'server' directive.");
392 free(inst); /* FIXME: detach */
396 if (ldap_is_ldap_url(inst->server)){
397 #ifdef HAVE_LDAP_INITIALIZE
401 radlog(L_ERR, "rlm_ldap: 'server' directive is in URL form but ldap_initialize() is not available.");
402 free(inst); /* FIXME: detach */
407 inst->timeout.tv_usec = 0;
408 inst->net_timeout.tv_usec = 0;
409 /* workaround for servers which support LDAPS but not START TLS */
410 if(inst->port == LDAPS_PORT || inst->tls_mode)
411 inst->tls_mode = LDAP_OPT_X_TLS_HARD;
414 inst->reply_item_map = NULL;
415 inst->check_item_map = NULL;
417 inst->failed_conns = 0;
419 DEBUG("rlm_ldap: Registering ldap_groupcmp for Ldap-Group");
420 paircompare_register(PW_LDAP_GROUP, PW_USER_NAME, ldap_groupcmp, inst);
421 memset(&flags, 0, sizeof(flags));
423 xlat_name = cf_section_name2(conf);
424 if (xlat_name != NULL){
429 * Allocate room for <instance>-Ldap-Group
431 group_name = rad_malloc((strlen(xlat_name) + 1 + 11) * sizeof(char));
432 sprintf(group_name,"%s-Ldap-Group",xlat_name);
433 DEBUG("rlm_ldap: Creating new attribute %s",group_name);
434 dict_addattr(group_name, 0, PW_TYPE_STRING, -1, flags);
435 dattr = dict_attrbyname(group_name);
437 radlog(L_ERR, "rlm_ldap: Failed to create attribute %s",group_name);
439 free(inst); /* FIXME: detach */
442 DEBUG("rlm_ldap: Registering ldap_groupcmp for %s",group_name);
443 paircompare_register(dattr->attr, PW_USER_NAME, ldap_groupcmp, inst);
447 xlat_name = cf_section_name1(conf);
448 rad_assert(xlat_name != NULL); /* or all hell breaks loose */
450 inst->xlat_name = strdup(xlat_name);
451 DEBUG("rlm_ldap: Registering ldap_xlat with xlat_name %s",xlat_name);
452 xlat_register(xlat_name,ldap_xlat,inst);
455 * Over-ride set_auth_type if there's no Auth-Type of our name.
456 * This automagically catches the case where LDAP is listed
457 * in "authorize", but not "authenticate".
459 if (inst->set_auth_type) {
460 DICT_VALUE *dv = dict_valbyname(PW_AUTH_TYPE, xlat_name);
462 DEBUG2("rlm_ldap: Over-riding set_auth_type, as we're not listed in the \"authenticate\" section.");
463 inst->set_auth_type = 0;
465 } /* else no need to look up the value */
469 * (LDAP_Instance, V1) attribute-value pair in the config
470 * items list means that the 'authorize' method of the
471 * instance 'V1' of the LDAP module has processed this
474 dict_addattr("LDAP-Instance", 0, PW_TYPE_STRING, -1, flags);
477 * ('eDir-APC', '1') in config items list
478 * Do not perform eDirectory account policy check (APC)
480 * ('eDir-APC', '2') in config items list
481 * Perform eDirectory APC
483 * ('eDir-APC', '3') in config items list
484 * eDirectory APC has been completed
486 dict_addattr("eDir-APC", 0, PW_TYPE_STRING, -1, flags);
488 * eDir-Auth-Option allows for a different NMAS Authentication method to be used instead of password
490 dict_addattr("eDir-Auth-Option", 0, PW_TYPE_STRING, -1, flags);
493 if (inst->num_conns <= 0){
494 radlog(L_ERR, "rlm_ldap: Invalid ldap connections number passed.");
495 free(inst); /* FIXME: detach */
498 inst->conns = malloc(sizeof(*(inst->conns))*inst->num_conns);
499 if (inst->conns == NULL){
500 radlog(L_ERR, "rlm_ldap: Could not allocate memory. Aborting.");
501 free(inst); /* FIXME: detach */
504 for(i = 0; i < inst->num_conns; i++){
505 inst->conns[i].bound = 0;
506 inst->conns[i].locked = 0;
507 inst->conns[i].failed_conns = 0;
508 inst->conns[i].ld = NULL;
509 pthread_mutex_init(&inst->conns[i].mutex, NULL);
514 * 'inst->apc_conns' is a separate connection pool to be
515 * used for performing eDirectory account policy check in
516 * the 'postauth' method. This avoids changing the
517 * (RADIUS server) credentials associated with the
518 * 'inst->conns' connection pool.
520 inst->apc_conns = malloc(sizeof(*(inst->apc_conns))*inst->num_conns);
521 if (inst->apc_conns == NULL){
522 radlog(L_ERR, "rlm_ldap: Could not allocate memory. Aborting.");
523 free(inst); /* FIXME: detach */
526 for(i = 0; i < inst->num_conns; i++){
527 inst->apc_conns[i].bound = 0;
528 inst->apc_conns[i].locked = 0;
529 inst->apc_conns[i].failed_conns = 0;
530 inst->apc_conns[i].ld = NULL;
531 pthread_mutex_init(&inst->apc_conns[i].mutex, NULL);
535 if (read_mappings(inst) != 0) {
536 radlog(L_ERR, "rlm_ldap: Reading dictionary mappings from file %s failed",
537 inst->dictionary_mapping);
538 free(inst); /* FIXME: detach */
541 if ((inst->check_item_map == NULL) &&
542 (inst->reply_item_map == NULL)) {
543 radlog(L_ERR, "rlm_ldap: dictionary mappings file %s did not contain any mappings",
544 inst->dictionary_mapping);
545 free(inst); /* FIXME: detach */
549 pair = inst->check_item_map;
554 check_map_num = (atts_num - 1);
555 pair = inst->reply_item_map;
560 reply_map_num = (atts_num - 1);
561 if (inst->profile_attr)
563 if (inst->passwd_attr)
565 if (inst->access_attr)
568 atts_num++; /* eDirectory Authentication Option attribute */
570 inst->atts = (char **)malloc(sizeof(char *)*(atts_num + 1));
571 if (inst->atts == NULL){
572 radlog(L_ERR, "rlm_ldap: Could not allocate memory. Aborting.");
573 free(inst); /* FIXME: detach */
576 pair = inst->check_item_map;
578 pair = inst->reply_item_map;
580 for(i=0;i<atts_num - 1;i++){
582 for(i=0;i<atts_num;i++){
584 if (i <= check_map_num ){
585 inst->atts[i] = pair->attr;
586 if (i == check_map_num)
587 pair = inst->reply_item_map;
591 else if (i <= reply_map_num){
592 inst->atts[i] = pair->attr;
596 if (inst->profile_attr && !att_map[0]){
597 inst->atts[i] = inst->profile_attr;
600 else if (inst->passwd_attr && !att_map[1]){
601 inst->atts[i] = inst->passwd_attr;
604 else if (inst->access_attr && !att_map[2]){
605 inst->atts[i] = inst->access_attr;
611 inst->atts[atts_num - 1] = "sasdefaultloginsequence";
613 inst->atts[atts_num] = NULL;
615 DEBUG("conns: %p",inst->conns);
625 * read_mappings(...) reads a ldap<->radius mappings file to
626 * inst->reply_item_map and inst->check_item_map
628 #define MAX_LINE_LEN 160
629 #define GENERIC_ATTRIBUTE_ID "$GENERIC$"
632 read_mappings(ldap_instance* inst)
638 * All buffers are of MAX_LINE_LEN so we can use sscanf
639 * without being afraid of buffer overflows
641 char buf[MAX_LINE_LEN], itemType[MAX_LINE_LEN];
642 char radiusAttribute[MAX_LINE_LEN], ldapAttribute[MAX_LINE_LEN];
645 char opstring[MAX_LINE_LEN];
647 /* open the mappings file for reading */
649 filename = inst->dictionary_mapping;
650 DEBUG("rlm_ldap: reading ldap<->radius mappings from file %s", filename);
651 mapfile = fopen(filename, "r");
653 if (mapfile == NULL) {
654 radlog(L_ERR, "rlm_ldap: Opening file %s failed: %s",
655 filename, strerror(errno));
656 return -1; /* error */
660 * read file line by line. Note that if line length
661 * exceeds MAX_LINE_LEN, line numbers will be mixed up
665 while (fgets(buf, sizeof buf, mapfile)!=NULL) {
673 ptr = strchr(buf, '#');
677 if (buf[0] == 0) continue;
679 /* extract tokens from the string */
680 token_count = sscanf(buf, "%s %s %s %s",
681 itemType, radiusAttribute,
682 ldapAttribute, opstring);
684 if (token_count <= 0) /* no tokens */
687 if ((token_count < 3) || (token_count > 4)) {
688 radlog(L_ERR, "rlm_ldap: Skipping %s line %i: %s",
689 filename, linenumber, buf);
690 radlog(L_ERR, "rlm_ldap: Expected 3 to 4 tokens "
691 "(Item type, RADIUS Attribute and LDAP Attribute) but found only %i", token_count);
695 if (token_count == 3) {
696 operator = T_OP_INVALID; /* use defaults */
701 operator = gettoken(&ptr, buf, sizeof(buf));
702 if ((operator < T_OP_ADD) || (operator > T_OP_CMP_EQ)) {
703 radlog(L_ERR, "rlm_ldap: file %s: skipping line %i: unknown or invalid operator %s",
704 filename, linenumber, opstring);
709 /* create new TLDAP_RADIUS list node */
710 pair = rad_malloc(sizeof(*pair));
712 pair->attr = strdup(ldapAttribute);
713 pair->radius_attr = strdup(radiusAttribute);
714 pair->operator = operator;
716 if ( (pair->attr == NULL) || (pair->radius_attr == NULL) ) {
717 radlog(L_ERR, "rlm_ldap: Out of memory");
718 if (pair->attr) free(pair->attr);
719 if (pair->radius_attr) free(pair->radius_attr);
725 /* push node to correct list */
726 if (strcasecmp(itemType, "checkItem") == 0) {
727 pair->next = inst->check_item_map;
728 inst->check_item_map = pair;
729 } else if (strcasecmp(itemType, "replyItem") == 0) {
730 pair->next = inst->reply_item_map;
731 inst->reply_item_map = pair;
733 radlog(L_ERR, "rlm_ldap: file %s: skipping line %i: unknown itemType %s",
734 filename, linenumber, itemType);
736 free(pair->radius_attr);
741 DEBUG("rlm_ldap: LDAP %s mapped to RADIUS %s",
742 pair->attr, pair->radius_attr);
747 return 0; /* success */
750 static int perform_search(void *instance, LDAP_CONN *conn,
751 char *search_basedn, int scope, char *filter,
752 char **attrs, LDAPMessage ** result)
754 int res = RLM_MODULE_OK;
756 ldap_instance *inst = instance;
757 int search_retry = 0;
762 radlog(L_ERR, "rlm_ldap: NULL connection handle passed");
763 return RLM_MODULE_FAIL;
765 if (conn->failed_conns > MAX_FAILED_CONNS_START){
766 conn->failed_conns++;
767 if (conn->failed_conns >= MAX_FAILED_CONNS_END){
768 conn->failed_conns = MAX_FAILED_CONNS_RESTART;
773 if (!conn->bound || conn->ld == NULL) {
774 DEBUG2("rlm_ldap: attempting LDAP reconnection");
776 DEBUG2("rlm_ldap: closing existing LDAP connection");
777 ldap_unbind_s(conn->ld);
779 if ((conn->ld = ldap_connect(instance, inst->login,
780 inst->password, 0, &res, NULL)) == NULL) {
781 radlog(L_ERR, "rlm_ldap: (re)connection attempt failed");
782 if (search_retry == 0)
783 conn->failed_conns++;
784 return (RLM_MODULE_FAIL);
787 conn->failed_conns = 0;
789 DEBUG2("rlm_ldap: performing search in %s, with filter %s",
790 search_basedn ? search_basedn : "(null)" , filter);
791 switch (ldap_search_st(conn->ld, search_basedn, scope, filter,
792 attrs, 0, &(inst->timeout), result)) {
794 case LDAP_NO_SUCH_OBJECT:
796 case LDAP_SERVER_DOWN:
797 radlog(L_ERR, "rlm_ldap: ldap_search() failed: LDAP connection lost.");
798 conn->failed_conns++;
799 if (search_retry == 0){
800 if (conn->failed_conns <= MAX_FAILED_CONNS_START){
801 radlog(L_INFO, "rlm_ldap: Attempting reconnect");
804 ldap_msgfree(*result);
808 ldap_msgfree(*result);
809 return RLM_MODULE_FAIL;
810 case LDAP_INSUFFICIENT_ACCESS:
811 radlog(L_ERR, "rlm_ldap: ldap_search() failed: Insufficient access. Check the identity and password configuration directives.");
812 ldap_msgfree(*result);
813 return RLM_MODULE_FAIL;
815 radlog(L_ERR, "rlm_ldap: ldap_search() failed: Timed out while waiting for server to respond. Please increase the timeout.");
816 ldap_msgfree(*result);
817 return RLM_MODULE_FAIL;
818 case LDAP_FILTER_ERROR:
819 radlog(L_ERR, "rlm_ldap: ldap_search() failed: Bad search filter: %s",filter);
820 ldap_msgfree(*result);
821 return RLM_MODULE_FAIL;
822 case LDAP_TIMELIMIT_EXCEEDED:
824 case LDAP_UNAVAILABLE:
825 /* We don't need to reconnect in these cases so we don't set conn->bound */
826 ldap_get_option(conn->ld, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
827 radlog(L_ERR, "rlm_ldap: ldap_search() failed: %s",
828 ldap_err2string(ldap_errno));
829 ldap_msgfree(*result);
830 return (RLM_MODULE_FAIL);
832 ldap_get_option(conn->ld, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
833 radlog(L_ERR, "rlm_ldap: ldap_search() failed: %s",
834 ldap_err2string(ldap_errno));
836 ldap_msgfree(*result);
837 return (RLM_MODULE_FAIL);
840 if ((ldap_count_entries(conn->ld, *result)) != 1) {
841 DEBUG("rlm_ldap: object not found or got ambiguous search result");
842 res = RLM_MODULE_NOTFOUND;
843 ldap_msgfree(*result);
850 * Translate the LDAP queries.
852 static int ldap_escape_func(char *out, int outlen, const char *in)
858 * Encode unsafe characters.
861 ((in[0] == ' ') || (in[0] == '#'))) ||
862 (strchr(",+\"\\<>;*=()", *in))) {
863 static const char hex[] = "0123456789abcdef";
866 * Only 3 or less bytes available.
873 *(out++) = hex[((*in) >> 4) & 0x0f];
874 *(out++) = hex[(*in) & 0x0f];
882 * Only one byte left.
900 * ldap_groupcmp(). Implement the Ldap-Group == "group" filter
902 static int ldap_groupcmp(void *instance, REQUEST *req,
903 UNUSED VALUE_PAIR *request, VALUE_PAIR *check,
904 UNUSED VALUE_PAIR *check_pairs,
905 UNUSED VALUE_PAIR **reply_pairs)
907 char filter[MAX_FILTER_STR_LEN];
908 char gr_filter[MAX_FILTER_STR_LEN];
910 LDAPMessage *result = NULL;
911 LDAPMessage *msg = NULL;
912 char basedn[MAX_FILTER_STR_LEN];
913 char *attrs[] = {"dn",NULL};
915 ldap_instance *inst = instance;
916 char *group_attrs[] = {inst->groupmemb_attr,NULL};
919 VALUE_PAIR *vp_user_dn;
920 VALUE_PAIR **request_pairs;
922 request_pairs = &req->config_items;
924 DEBUG("rlm_ldap: Entering ldap_groupcmp()");
926 if (check->vp_strvalue == NULL || check->length == 0){
927 DEBUG("rlm_ldap::ldap_groupcmp: Illegal group name");
932 DEBUG("rlm_ldap::ldap_groupcmp: NULL request");
936 if (!radius_xlat(basedn, sizeof(basedn), inst->basedn, req, ldap_escape_func)) {
937 DEBUG("rlm_ldap::ldap_groupcmp: unable to create basedn.");
941 while((vp_user_dn = pairfind(*request_pairs, PW_LDAP_USERDN)) == NULL){
942 char *user_dn = NULL;
944 if (!radius_xlat(filter, sizeof(filter), inst->filter,
945 req, ldap_escape_func)){
946 DEBUG("rlm_ldap::ldap_groupcmp: unable to create filter");
949 if ((conn_id = ldap_get_conn(inst->conns,&conn,inst)) == -1){
950 radlog(L_ERR, "rlm_ldap: All ldap connections are in use");
953 if ((res = perform_search(inst, conn, basedn, LDAP_SCOPE_SUBTREE,
954 filter, attrs, &result)) != RLM_MODULE_OK){
955 DEBUG("rlm_ldap::ldap_groupcmp: search failed");
956 ldap_release_conn(conn_id,inst->conns);
959 if ((msg = ldap_first_entry(conn->ld, result)) == NULL) {
960 DEBUG("rlm_ldap::ldap_groupcmp: ldap_first_entry() failed");
961 ldap_release_conn(conn_id,inst->conns);
962 ldap_msgfree(result);
965 if ((user_dn = ldap_get_dn(conn->ld, msg)) == NULL) {
966 DEBUG("rlm_ldap:ldap_groupcmp:: ldap_get_dn() failed");
967 ldap_release_conn(conn_id,inst->conns);
968 ldap_msgfree(result);
971 ldap_release_conn(conn_id,inst->conns);
974 * Adding new attribute containing DN for LDAP
975 * object associated with given username
977 pairadd(request_pairs, pairmake("Ldap-UserDn", user_dn,
979 ldap_memfree(user_dn);
980 ldap_msgfree(result);
983 if(!radius_xlat(gr_filter, sizeof(gr_filter),
984 inst->groupmemb_filt, req, ldap_escape_func)) {
985 DEBUG("rlm_ldap::ldap_groupcmp: unable to create filter.");
989 if (strchr((char *)check->vp_strvalue,',') != NULL) {
990 /* This looks like a DN */
991 snprintf(filter,sizeof(filter), "%s",gr_filter);
992 snprintf(basedn,sizeof(basedn), "%s",(char *)check->vp_strvalue);
994 snprintf(filter,sizeof(filter), "(&(%s=%s)%s)",
995 inst->groupname_attr,
996 (char *)check->vp_strvalue,gr_filter);
998 if ((conn_id = ldap_get_conn(inst->conns,&conn,inst)) == -1) {
999 radlog(L_ERR, "rlm_ldap: All ldap connections are in use");
1003 if ((res = perform_search(inst, conn, basedn, LDAP_SCOPE_SUBTREE,
1004 filter, attrs, &result)) == RLM_MODULE_OK) {
1005 DEBUG("rlm_ldap::ldap_groupcmp: User found in group %s",
1006 (char *)check->vp_strvalue);
1007 ldap_msgfree(result);
1008 ldap_release_conn(conn_id,inst->conns);
1012 ldap_release_conn(conn_id,inst->conns);
1014 if (res != RLM_MODULE_NOTFOUND ) {
1015 DEBUG("rlm_ldap::ldap_groupcmp: Search returned error");
1019 if (inst->groupmemb_attr == NULL){
1021 * Search returned NOTFOUND and searching for
1022 * membership using user object attributes is not
1023 * specified in config file
1025 DEBUG("rlm_ldap::ldap_groupcmp: Group %s not found or user is not a member.",(char *)check->vp_strvalue);
1029 snprintf(filter,sizeof(filter), "(objectclass=*)");
1030 if ((conn_id = ldap_get_conn(inst->conns,&conn,inst)) == -1){
1031 radlog(L_ERR, "rlm_ldap: Add ldap connections are in use");
1034 if ((res = perform_search(inst, conn, vp_user_dn->vp_strvalue,
1035 LDAP_SCOPE_BASE, filter, group_attrs,
1036 &result)) != RLM_MODULE_OK) {
1037 DEBUG("rlm_ldap::ldap_groupcmp: Search returned error");
1038 ldap_release_conn(conn_id, inst->conns);
1042 if ((msg = ldap_first_entry(conn->ld, result)) == NULL) {
1043 DEBUG("rlm_ldap::ldap_groupcmp: ldap_first_entry() failed");
1044 ldap_release_conn(conn_id,inst->conns);
1045 ldap_msgfree(result);
1048 if ((vals = ldap_get_values(conn->ld, msg,
1049 inst->groupmemb_attr)) != NULL) {
1053 for (;i < ldap_count_values(vals);i++){
1054 if (strchr(vals[i],',') != NULL){
1055 /* This looks like a DN */
1056 LDAPMessage *gr_result = NULL;
1057 snprintf(filter,sizeof(filter), "(%s=%s)",
1058 inst->groupname_attr,
1059 (char *)check->vp_strvalue);
1060 if ((res = perform_search(inst, conn, vals[i],
1061 LDAP_SCOPE_BASE, filter,
1062 attrs, &gr_result)) != RLM_MODULE_OK){
1063 if (res != RLM_MODULE_NOTFOUND) {
1064 DEBUG("rlm_ldap::ldap_groupcmp: Search returned error");
1065 ldap_value_free(vals);
1066 ldap_msgfree(result);
1067 ldap_release_conn(conn_id,inst->conns);
1071 ldap_msgfree(gr_result);
1076 if (strcmp(vals[i],(char *)check->vp_strvalue) == 0){
1082 ldap_value_free(vals);
1083 ldap_msgfree(result);
1085 DEBUG("rlm_ldap::groupcmp: Group %s not found or user not a member",
1086 (char *)check->vp_strvalue);
1087 ldap_release_conn(conn_id,inst->conns);
1091 DEBUG("rlm_ldap::ldap_groupcmp: ldap_get_values() failed");
1092 ldap_msgfree(result);
1093 ldap_release_conn(conn_id,inst->conns);
1097 DEBUG("rlm_ldap::ldap_groupcmp: User found in group %s",(char *)check->vp_strvalue);
1098 ldap_release_conn(conn_id,inst->conns);
1105 * Do an xlat on an LDAP URL
1107 static int ldap_xlat(void *instance, REQUEST *request, char *fmt,
1108 char *out, size_t freespace, RADIUS_ESCAPE_STRING func)
1110 char url[MAX_FILTER_STR_LEN];
1113 ldap_instance *inst = instance;
1114 LDAPURLDesc *ldap_url;
1115 LDAPMessage *result = NULL;
1116 LDAPMessage *msg = NULL;
1121 DEBUG("rlm_ldap: - ldap_xlat");
1122 if (!radius_xlat(url, sizeof(url), fmt, request, func)) {
1123 radlog (L_ERR, "rlm_ldap: Unable to create LDAP URL.\n");
1126 if (!ldap_is_ldap_url(url)){
1127 radlog (L_ERR, "rlm_ldap: String passed does not look like an LDAP URL.\n");
1130 if (ldap_url_parse(url,&ldap_url)){
1131 radlog (L_ERR, "rlm_ldap: LDAP URL parse failed.\n");
1134 if (ldap_url->lud_attrs == NULL || ldap_url->lud_attrs[0] == NULL ||
1135 ( ldap_url->lud_attrs[1] != NULL ||
1136 ( ! strlen(ldap_url->lud_attrs[0]) ||
1137 ! strcmp(ldap_url->lud_attrs[0],"*") ) ) ){
1138 radlog (L_ERR, "rlm_ldap: Invalid Attribute(s) request.\n");
1139 ldap_free_urldesc(ldap_url);
1142 if (ldap_url->lud_host){
1143 if (strncmp(inst->server,ldap_url->lud_host,
1144 strlen(inst->server)) != 0 ||
1145 ldap_url->lud_port != inst->port) {
1146 DEBUG("rlm_ldap: Requested server/port is not known to this module instance.");
1147 ldap_free_urldesc(ldap_url);
1151 if ((conn_id = ldap_get_conn(inst->conns,&conn,inst)) == -1){
1152 radlog(L_ERR, "rlm_ldap: All ldap connections are in use");
1153 ldap_free_urldesc(ldap_url);
1156 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){
1157 if (res == RLM_MODULE_NOTFOUND){
1158 DEBUG("rlm_ldap: Search returned not found");
1159 ldap_free_urldesc(ldap_url);
1160 ldap_release_conn(conn_id,inst->conns);
1163 DEBUG("rlm_ldap: Search returned error");
1164 ldap_free_urldesc(ldap_url);
1165 ldap_release_conn(conn_id,inst->conns);
1168 if ((msg = ldap_first_entry(conn->ld, result)) == NULL){
1169 DEBUG("rlm_ldap: ldap_first_entry() failed");
1170 ldap_msgfree(result);
1171 ldap_free_urldesc(ldap_url);
1172 ldap_release_conn(conn_id,inst->conns);
1175 if ((vals = ldap_get_values(conn->ld, msg, ldap_url->lud_attrs[0])) != NULL) {
1176 ret = strlen(vals[0]);
1177 if (ret > freespace){
1178 DEBUG("rlm_ldap: Insufficient string space");
1179 ldap_free_urldesc(ldap_url);
1180 ldap_value_free(vals);
1181 ldap_msgfree(result);
1182 ldap_release_conn(conn_id,inst->conns);
1185 DEBUG("rlm_ldap: Adding attribute %s, value: %s",ldap_url->lud_attrs[0],vals[0]);
1186 strlcpy(out,vals[0],ret);
1187 ldap_value_free(vals);
1192 ldap_msgfree(result);
1193 ldap_free_urldesc(ldap_url);
1194 ldap_release_conn(conn_id,inst->conns);
1196 DEBUG("rlm_ldap: - ldap_xlat end");
1203 * For auto-header discovery.
1205 static const LRAD_NAME_NUMBER header_names[] = {
1206 { "{clear}", PW_CLEARTEXT_PASSWORD },
1207 { "{cleartext}", PW_CLEARTEXT_PASSWORD },
1208 { "{md5}", PW_MD5_PASSWORD },
1209 { "{smd5}", PW_SMD5_PASSWORD },
1210 { "{crypt}", PW_CRYPT_PASSWORD },
1211 { "{sha}", PW_SHA_PASSWORD },
1212 { "{ssha}", PW_SSHA_PASSWORD },
1213 { "{nt}", PW_NT_PASSWORD },
1214 { "{ns-mta-md5}", PW_NS_MTA_MD5_PASSWORD },
1219 /******************************************************************************
1221 * Function: rlm_ldap_authorize
1223 * Purpose: Check if user is authorized for remote access
1225 ******************************************************************************/
1226 static int ldap_authorize(void *instance, REQUEST * request)
1228 LDAPMessage *result = NULL;
1229 LDAPMessage *msg = NULL;
1230 LDAPMessage *def_msg = NULL;
1231 LDAPMessage *def_attr_msg = NULL;
1232 LDAPMessage *def_result = NULL;
1233 LDAPMessage *def_attr_result = NULL;
1234 ldap_instance *inst = instance;
1235 char *user_dn = NULL;
1236 char filter[MAX_FILTER_STR_LEN];
1237 char basedn[MAX_FILTER_STR_LEN];
1238 VALUE_PAIR *check_tmp;
1239 VALUE_PAIR *reply_tmp;
1241 VALUE_PAIR **check_pairs, **reply_pairs;
1243 VALUE_PAIR *module_fmsg_vp;
1244 VALUE_PAIR *user_profile;
1245 char module_fmsg[MAX_STRING_LEN];
1248 int added_known_password = 0;
1250 DEBUG("rlm_ldap: - authorize");
1252 if (!request->username){
1253 radlog(L_AUTH, "rlm_ldap: Attribute \"User-Name\" is required for authentication.\n");
1254 return RLM_MODULE_INVALID;
1257 check_pairs = &request->config_items;
1258 reply_pairs = &request->reply->vps;
1261 * Check for valid input, zero length names not permitted
1263 if (request->username->vp_strvalue == 0) {
1264 radlog(L_ERR, "rlm_ldap: zero length username not permitted\n");
1265 return RLM_MODULE_INVALID;
1267 DEBUG("rlm_ldap: performing user authorization for %s",
1268 request->username->vp_strvalue);
1270 if (!radius_xlat(filter, sizeof(filter), inst->filter,
1271 request, ldap_escape_func)) {
1272 radlog (L_ERR, "rlm_ldap: unable to create filter.\n");
1273 return RLM_MODULE_INVALID;
1276 if (!radius_xlat(basedn, sizeof(basedn), inst->basedn,
1277 request, ldap_escape_func)) {
1278 radlog (L_ERR, "rlm_ldap: unable to create basedn.\n");
1279 return RLM_MODULE_INVALID;
1282 if ((conn_id = ldap_get_conn(inst->conns,&conn,inst)) == -1){
1283 radlog(L_ERR, "rlm_ldap: All ldap connections are in use");
1284 return RLM_MODULE_FAIL;
1286 if ((res = perform_search(instance, conn, basedn, LDAP_SCOPE_SUBTREE, filter, inst->atts, &result)) != RLM_MODULE_OK) {
1287 DEBUG("rlm_ldap: search failed");
1288 if (res == RLM_MODULE_NOTFOUND){
1289 snprintf(module_fmsg,sizeof(module_fmsg),"rlm_ldap: User not found");
1290 module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
1291 pairadd(&request->packet->vps, module_fmsg_vp);
1293 ldap_release_conn(conn_id,inst->conns);
1296 if ((msg = ldap_first_entry(conn->ld, result)) == NULL) {
1297 DEBUG("rlm_ldap: ldap_first_entry() failed");
1298 ldap_msgfree(result);
1299 ldap_release_conn(conn_id,inst->conns);
1300 return RLM_MODULE_FAIL;
1302 if ((user_dn = ldap_get_dn(conn->ld, msg)) == NULL) {
1303 DEBUG("rlm_ldap: ldap_get_dn() failed");
1304 ldap_msgfree(result);
1305 ldap_release_conn(conn_id,inst->conns);
1306 return RLM_MODULE_FAIL;
1309 * Adding new attribute containing DN for LDAP object associated with
1312 pairadd(check_pairs, pairmake("Ldap-UserDn", user_dn, T_OP_EQ));
1313 ldap_memfree(user_dn);
1316 /* Remote access is controled by attribute of the user object */
1317 if (inst->access_attr) {
1318 if ((vals = ldap_get_values(conn->ld, msg, inst->access_attr)) != NULL) {
1319 if (inst->default_allow){
1320 DEBUG("rlm_ldap: checking if remote access for %s is allowed by %s", request->username->vp_strvalue, inst->access_attr);
1321 if (!strncmp(vals[0], "FALSE", 5)) {
1322 DEBUG("rlm_ldap: dialup access disabled");
1323 snprintf(module_fmsg,sizeof(module_fmsg),"rlm_ldap: Access Attribute denies access");
1324 module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
1325 pairadd(&request->packet->vps, module_fmsg_vp);
1326 ldap_msgfree(result);
1327 ldap_value_free(vals);
1328 ldap_release_conn(conn_id,inst->conns);
1329 return RLM_MODULE_USERLOCK;
1331 ldap_value_free(vals);
1334 DEBUG("rlm_ldap: %s attribute exists - access denied by default", inst->access_attr);
1335 snprintf(module_fmsg,sizeof(module_fmsg),"rlm_ldap: Access Attribute denies access");
1336 module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
1337 pairadd(&request->packet->vps, module_fmsg_vp);
1338 ldap_msgfree(result);
1339 ldap_value_free(vals);
1340 ldap_release_conn(conn_id,inst->conns);
1341 return RLM_MODULE_USERLOCK;
1344 if (inst->default_allow){
1345 DEBUG("rlm_ldap: no %s attribute - access denied by default", inst->access_attr);
1346 snprintf(module_fmsg,sizeof(module_fmsg),"rlm_ldap: Access Attribute denies access");
1347 module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
1348 pairadd(&request->packet->vps, module_fmsg_vp);
1349 ldap_msgfree(result);
1350 ldap_release_conn(conn_id,inst->conns);
1351 return RLM_MODULE_USERLOCK;
1357 * Check for the default profile entry. If it exists then add the
1358 * attributes it contains in the check and reply pairs
1361 user_profile = pairfind(request->config_items, PW_USER_PROFILE);
1362 if (inst->default_profile || user_profile){
1363 char *profile = inst->default_profile;
1365 strlcpy(filter,inst->base_filter,sizeof(filter));
1367 profile = user_profile->vp_strvalue;
1368 if (profile && strlen(profile)){
1369 if ((res = perform_search(instance, conn,
1370 profile, LDAP_SCOPE_BASE,
1371 filter, inst->atts, &def_result)) == RLM_MODULE_OK){
1372 if ((def_msg = ldap_first_entry(conn->ld,def_result))){
1373 if ((check_tmp = ldap_pairget(conn->ld,def_msg,inst->check_item_map,check_pairs,1))) {
1375 pairxlatmove(request, check_pairs, &check_tmp);
1376 pairfree(&check_tmp);
1379 pairadd(check_pairs,check_tmp);
1381 if ((reply_tmp = ldap_pairget(conn->ld,def_msg,inst->reply_item_map,reply_pairs,0))) {
1383 pairxlatmove(request, reply_pairs, &reply_tmp);
1384 pairfree(&reply_tmp);
1387 pairadd(reply_pairs,reply_tmp);
1390 ldap_msgfree(def_result);
1392 DEBUG("rlm_ldap: default_profile/user-profile search failed");
1397 * Check for the profile attribute. If it exists, we assume that it
1398 * contains the DN of an entry containg a profile for the user. That
1399 * way we can have different general profiles for various user groups
1400 * (students,faculty,staff etc)
1403 if (inst->profile_attr){
1404 if ((vals = ldap_get_values(conn->ld, msg, inst->profile_attr)) != NULL) {
1406 strlcpy(filter,inst->base_filter,sizeof(filter));
1407 while(vals[i] != NULL && strlen(vals[i])){
1408 if ((res = perform_search(instance, conn,
1409 vals[i], LDAP_SCOPE_BASE,
1410 filter, inst->atts, &def_attr_result)) == RLM_MODULE_OK){
1411 if ((def_attr_msg = ldap_first_entry(conn->ld,def_attr_result))){
1412 if ((check_tmp = ldap_pairget(conn->ld,def_attr_msg,inst->check_item_map,check_pairs,1))) {
1414 pairxlatmove(request, check_pairs, &check_tmp);
1415 pairfree(&check_tmp);
1418 pairadd(check_pairs,check_tmp);
1420 if ((reply_tmp = ldap_pairget(conn->ld,def_attr_msg,inst->reply_item_map,reply_pairs,0))) {
1422 pairxlatmove(request, reply_pairs, &reply_tmp);
1423 pairfree(&reply_tmp);
1426 pairadd(reply_pairs,reply_tmp);
1429 ldap_msgfree(def_attr_result);
1431 DEBUG("rlm_ldap: profile_attribute search failed");
1434 ldap_value_free(vals);
1437 if (inst->passwd_attr && strlen(inst->passwd_attr)) {
1438 #ifdef NOVELL_UNIVERSAL_PASSWORD
1439 if (strcasecmp(inst->passwd_attr,"nspmPassword") != 0) {
1441 VALUE_PAIR *passwd_item;
1447 * Read the password from the DB, and
1448 * add it to the request.
1450 passwd_vals = ldap_get_values(conn->ld,msg,
1454 * Loop over what we received, and parse it.
1456 if (passwd_vals) for (i = 0;
1457 passwd_vals[i] != NULL;
1459 int attr = PW_USER_PASSWORD;
1461 if (strlen(passwd_vals[i]) == 0)
1464 value = passwd_vals[i];
1466 if (inst->auto_header) {
1470 p = strchr(value, '}');
1472 if ((p - value + 1) >= sizeof(autobuf))
1473 continue; /* paranoia */
1474 memcpy(autobuf, value, p - value + 1);
1475 autobuf[p - value + 1] = '\0';
1477 attr = lrad_str2int(header_names,
1479 if (!attr) continue;
1483 } else if (inst->passwd_hdr &&
1484 strlen(inst->passwd_hdr)) {
1485 if (strncasecmp(value,
1487 strlen(inst->passwd_hdr)) == 0) {
1488 value += strlen(inst->passwd_hdr);
1490 DEBUG("rlm_ldap: Password header not found in password %s for user %s", passwd_vals[0], request->username->vp_strvalue);
1493 if (!value) continue;
1496 passwd_item = radius_paircreate(request,
1497 &request->config_items,
1500 strlcpy(passwd_item->vp_strvalue, value,
1501 sizeof(passwd_item->vp_strvalue));
1502 passwd_item->length = strlen(passwd_item->vp_strvalue);
1503 DEBUG("rlm_ldap: Added %s = %s in check items",
1505 passwd_item->vp_strvalue);
1506 added_known_password = 1;
1508 ldap_value_free(passwd_vals);
1509 #ifdef NOVELL_UNIVERSAL_PASSWORD
1513 * Read Universal Password from eDirectory
1515 VALUE_PAIR *passwd_item;
1516 VALUE_PAIR *vp_user_dn;
1518 char *universal_password = NULL;
1519 int universal_password_len = UNIVERSAL_PASS_LEN;
1520 char *passwd_val = NULL;
1524 if ((passwd_item = pairfind(request->config_items, PW_CLEARTEXT_PASSWORD)) == NULL){
1526 universal_password = rad_malloc(universal_password_len);
1527 memset(universal_password, 0, universal_password_len);
1529 vp_user_dn = pairfind(request->config_items,PW_LDAP_USERDN);
1530 res = nmasldap_get_password(conn->ld,vp_user_dn->vp_strvalue,&universal_password_len,universal_password);
1533 passwd_val = universal_password;
1535 if (inst->passwd_hdr && strlen(inst->passwd_hdr)){
1536 passwd_val = strstr(passwd_val,inst->passwd_hdr);
1538 if (passwd_val != NULL)
1539 passwd_val += strlen((char*)inst->passwd_hdr);
1541 DEBUG("rlm_ldap: Password header not found in password %s for user %s ",passwd_val,request->username->vp_strvalue);
1545 passwd_item = radius_paircreate(request, &request->config_items, PW_CLEARTEXT_PASSWORD, PW_TYPE_STRING);
1546 strlcpy(passwd_item->vp_strvalue,passwd_val,sizeof(passwd_item->vp_strvalue));
1547 passwd_item->length = strlen(passwd_item->vp_strvalue);
1548 added_known_password = 1;
1553 VALUE_PAIR *vp_inst, *vp_apc;
1554 int inst_attr, apc_attr;
1556 dattr = dict_attrbyname("LDAP-Instance");
1557 inst_attr = dattr->attr;
1558 dattr = dict_attrbyname("eDir-APC");
1559 apc_attr = dattr->attr;
1561 vp_inst = pairfind(request->config_items, inst_attr);
1562 if(vp_inst == NULL){
1564 * The authorize method of no other LDAP module instance has
1565 * processed this request.
1567 vp_inset = radius_paircreate(request, &request->config_items, inst_attr, PW_TYPE_STRING);
1568 strlcpy(vp_inst->vp_strvalue, inst->xlat_name, sizeof(vp_inst->vp_strvalue));
1569 vp_inst->length = strlen(vp_inst->vp_strvalue);
1572 * Inform the authenticate / post-auth method about the presence
1573 * of UP in the config items list and whether eDirectory account
1574 * policy check is to be performed or not.
1576 vp_apc = radius_paircreate(request, &request->config_items, apc_attr, PW_TYPE_STRING);
1577 if(!inst->edir_account_policy_check){
1579 strcpy(vp_apc->vp_strvalue, "1");
1581 /* Perform eDirectory account-policy check */
1582 strcpy(vp_apc->vp_strvalue, "2");
1589 DEBUG("rlm_ldap: Added the eDirectory password %s in check items as %s",passwd_item->vp_strvalue,passwd_item->name);
1593 DEBUG("rlm_ldap: Error reading Universal Password.Return Code = %d",res);
1596 memset(universal_password, 0, universal_password_len);
1597 free(universal_password);
1605 VALUE_PAIR *vp_auth_opt;
1610 dattr = dict_attrbyname("eDir-Auth-Option");
1611 auth_opt_attr = dattr->attr;
1612 if(pairfind(*check_pairs, auth_opt_attr) == NULL){
1613 if ((auth_option = ldap_get_values(conn->ld, msg, "sasDefaultLoginSequence")) != NULL) {
1614 if ((vp_auth_opt = paircreate(auth_opt_attr, PW_TYPE_STRING)) == NULL){
1615 radlog(L_ERR, "rlm_ldap: Could not allocate memory. Aborting.");
1616 ldap_msgfree(result);
1617 ldap_release_conn(conn_id, inst->conns);
1619 strcpy(vp_auth_opt->strvalue, auth_option[0]);
1620 vp_auth_opt->length = strlen(auth_option[0]);
1621 pairadd(&request->config_items, vp_auth_opt);
1623 DEBUG("rlm_ldap: No default NMAS login sequence");
1629 DEBUG("rlm_ldap: looking for check items in directory...");
1631 if ((check_tmp = ldap_pairget(conn->ld, msg, inst->check_item_map,check_pairs,1)) != NULL) {
1633 pairxlatmove(request, check_pairs, &check_tmp);
1634 pairfree(&check_tmp);
1637 pairadd(check_pairs,check_tmp);
1641 DEBUG("rlm_ldap: looking for reply items in directory...");
1644 if ((reply_tmp = ldap_pairget(conn->ld, msg, inst->reply_item_map,reply_pairs,0)) != NULL) {
1646 pairxlatmove(request, reply_pairs, &reply_tmp);
1647 pairfree(&reply_tmp);
1650 pairadd(reply_pairs,reply_tmp);
1653 if (inst->do_comp && paircompare(request,request->packet->vps,*check_pairs,reply_pairs) != 0){
1655 /* Don't perform eDirectory APC if RADIUS authorize fails */
1660 dattr = dict_attrbyname("eDir-APC");
1661 apc_attr = dattr->attr;
1663 vp_apc = pairfind(request->config_items, apc_attr);
1665 vp_apc->vp_strvalue[0] = '1';
1668 DEBUG("rlm_ldap: Pairs do not match. Rejecting user.");
1669 snprintf(module_fmsg,sizeof(module_fmsg),"rlm_ldap: Pairs do not match");
1670 module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
1671 pairadd(&request->packet->vps, module_fmsg_vp);
1672 ldap_msgfree(result);
1673 ldap_release_conn(conn_id,inst->conns);
1675 return RLM_MODULE_REJECT;
1679 * Module should default to LDAP authentication if no Auth-Type
1680 * specified. Note that we do this ONLY if configured, AND we
1681 * set the Auth-Type to our module name, which allows multiple
1682 * ldap instances to work.
1684 if (inst->set_auth_type &&
1685 (pairfind(*check_pairs, PW_AUTH_TYPE) == NULL) &&
1686 request->password &&
1687 (request->password->attribute == PW_USER_PASSWORD) &&
1688 !added_known_password) {
1689 pairadd(check_pairs, pairmake("Auth-Type", inst->xlat_name, T_OP_EQ));
1690 DEBUG("rlm_ldap: Setting Auth-Type = %s", inst->xlat_name);
1693 DEBUG("rlm_ldap: user %s authorized to use remote access",
1694 request->username->vp_strvalue);
1695 ldap_msgfree(result);
1696 ldap_release_conn(conn_id,inst->conns);
1698 return RLM_MODULE_OK;
1701 /*****************************************************************************
1703 * Function: rlm_ldap_authenticate
1705 * Purpose: Check the user's password against ldap database
1707 *****************************************************************************/
1708 static int ldap_authenticate(void *instance, REQUEST * request)
1711 LDAPMessage *result, *msg;
1712 ldap_instance *inst = instance;
1713 char *user_dn, *attrs[] = {"uid", NULL};
1714 char filter[MAX_FILTER_STR_LEN];
1715 char basedn[MAX_FILTER_STR_LEN];
1717 VALUE_PAIR *vp_user_dn;
1718 VALUE_PAIR *module_fmsg_vp;
1719 char module_fmsg[MAX_STRING_LEN];
1726 DEBUG("rlm_ldap: - authenticate");
1729 * Ensure that we're being passed a plain-text password, and not
1733 if (!request->username) {
1734 radlog(L_AUTH, "rlm_ldap: Attribute \"User-Name\" is required for authentication.\n");
1735 return RLM_MODULE_INVALID;
1738 if (!request->password){
1739 radlog(L_AUTH, "rlm_ldap: Attribute \"User-Password\" is required for authentication.");
1740 DEBUG2(" You seem to have set \"Auth-Type := LDAP\" somewhere.");
1741 DEBUG2(" THAT CONFIGURATION IS WRONG. DELETE IT.");
1742 DEBUG2(" YOU ARE PREVENTING THE SERVER FROM WORKING PROPERLY.");
1743 return RLM_MODULE_INVALID;
1746 if(request->password->attribute != PW_USER_PASSWORD) {
1747 radlog(L_AUTH, "rlm_ldap: Attribute \"User-Password\" is required for authentication. Cannot use \"%s\".", request->password->name);
1748 return RLM_MODULE_INVALID;
1751 if (request->password->length == 0) {
1752 snprintf(module_fmsg,sizeof(module_fmsg),"rlm_ldap: empty password supplied");
1753 module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
1754 pairadd(&request->packet->vps, module_fmsg_vp);
1755 return RLM_MODULE_INVALID;
1759 * Check that we don't have any failed connections. If we do there's no real need
1760 * of runing. Also give it another chance if we have a lot of failed connections.
1762 if (inst->failed_conns > MAX_FAILED_CONNS_END)
1763 inst->failed_conns = 0;
1764 if (inst->failed_conns > MAX_FAILED_CONNS_START){
1765 inst->failed_conns++;
1766 return RLM_MODULE_FAIL;
1770 DEBUG("rlm_ldap: login attempt by \"%s\" with password \"%s\"",
1771 request->username->vp_strvalue, request->password->vp_strvalue);
1773 while ((vp_user_dn = pairfind(request->config_items,
1774 PW_LDAP_USERDN)) == NULL) {
1775 if (!radius_xlat(filter, sizeof(filter), inst->filter,
1776 request, ldap_escape_func)) {
1777 radlog (L_ERR, "rlm_ldap: unable to create filter.\n");
1778 return RLM_MODULE_INVALID;
1781 if (!radius_xlat(basedn, sizeof(basedn), inst->basedn,
1782 request, ldap_escape_func)) {
1783 radlog (L_ERR, "rlm_ldap: unable to create basedn.\n");
1784 return RLM_MODULE_INVALID;
1787 if ((conn_id = ldap_get_conn(inst->conns,&conn,inst)) == -1){
1788 radlog(L_ERR, "rlm_ldap: All ldap connections are in use");
1789 return RLM_MODULE_FAIL;
1791 if ((res = perform_search(instance, conn, basedn, LDAP_SCOPE_SUBTREE, filter, attrs, &result)) != RLM_MODULE_OK) {
1792 if (res == RLM_MODULE_NOTFOUND){
1793 snprintf(module_fmsg,sizeof(module_fmsg),"rlm_ldap: User not found");
1794 module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
1795 pairadd(&request->packet->vps, module_fmsg_vp);
1797 ldap_release_conn(conn_id,inst->conns);
1800 if ((msg = ldap_first_entry(conn->ld, result)) == NULL) {
1801 ldap_msgfree(result);
1802 ldap_release_conn(conn_id,inst->conns);
1803 return RLM_MODULE_FAIL;
1805 if ((user_dn = ldap_get_dn(conn->ld, msg)) == NULL) {
1806 DEBUG("rlm_ldap: ldap_get_dn() failed");
1807 ldap_msgfree(result);
1808 ldap_release_conn(conn_id,inst->conns);
1809 return RLM_MODULE_FAIL;
1811 ldap_release_conn(conn_id,inst->conns);
1812 pairadd(&request->config_items, pairmake("Ldap-UserDn", user_dn, T_OP_EQ));
1813 ldap_memfree(user_dn);
1814 ldap_msgfree(result);
1817 user_dn = vp_user_dn->vp_strvalue;
1819 DEBUG("rlm_ldap: user DN: %s", user_dn);
1822 ld_user = ldap_connect(instance, user_dn, request->password->vp_strvalue,
1825 /* Don't perform eDirectory APC again after attempting to bind here. */
1830 VALUE_PAIR *vp_auth_opt, *vp_state;
1833 char host_ipaddr[32];
1835 int auth_state = -1;
1836 char *challenge = NULL;
1837 int challenge_len = MAX_CHALLENGE_LEN;
1840 dattr = dict_attrbyname("eDir-APC");
1841 apc_attr = dattr->attr;
1842 vp_apc = pairfind(request->config_items, apc_attr);
1843 if(vp_apc && vp_apc->vp_strvalue[0] == '2')
1844 vp_apc->vp_strvalue[0] = '3';
1848 dattr = dict_attrbyname("eDir-Auth-Option");
1849 auth_opt_attr = dattr->attr;
1851 vp_auth_opt = pairfind(request->config_items, auth_opt_attr);
1855 DEBUG("rlm_ldap: ldap auth option = %s", vp_auth_opt->strvalue);
1856 strncpy(seq, vp_auth_opt->strvalue, vp_auth_opt->length);
1857 seq[vp_auth_opt->length] = '\0';
1858 if( strcmp(seq, "<No Default>") ){
1860 /* Get the client IP address to check for packet validity */
1861 inet_ntop(AF_INET, &request->packet->src_ipaddr, host_ipaddr, sizeof(host_ipaddr));
1863 /* challenge variable is used to receive the challenge from the
1864 * Token method (if any) and also to send the state attribute
1865 * in case the request packet is a reply to a challenge
1867 challenge = rad_malloc(MAX_CHALLENGE_LEN);
1869 /* If state attribute present in request it is a reply to challenge. */
1870 if((vp_state = pairfind(request->packet->vps, PW_STATE))!= NULL ){
1871 DEBUG("rlm_ldap: Response to Access-Challenge");
1872 strncpy(challenge, vp_state->strvalue, sizeof(challenge));
1873 challenge_len = vp_state->length;
1874 challenge[challenge_len] = 0;
1878 if ((conn_id = ldap_get_conn(inst->conns, &conn1, inst)) == -1){
1879 radlog(L_ERR, "rlm_ldap: All ldap connections are in use");
1880 res = RLM_MODULE_FAIL;
1884 radlog(L_ERR, "rlm_ldap: NULL connection handle passed");
1885 return RLM_MODULE_FAIL;
1888 if (conn1->failed_conns > MAX_FAILED_CONNS_START){
1889 conn1->failed_conns++;
1890 if (conn1->failed_conns >= MAX_FAILED_CONNS_END){
1891 conn1->failed_conns = MAX_FAILED_CONNS_RESTART;
1896 if (!conn1->bound || conn1->ld == NULL) {
1897 DEBUG2("rlm_ldap: attempting LDAP reconnection");
1899 DEBUG2("rlm_ldap: closing existing LDAP connection");
1900 ldap_unbind_s(conn1->ld);
1902 if ((conn1->ld = ldap_connect(instance, inst->login,inst->password, 0, &res, NULL)) == NULL) {
1903 radlog(L_ERR, "rlm_ldap: (re)connection attempt failed");
1904 conn1->failed_conns++;
1905 return (RLM_MODULE_FAIL);
1908 conn1->failed_conns = 0;
1910 DEBUG("rlm_ldap: Performing NMAS Authentication for user: %s, seq: %s \n", user_dn,seq);
1912 res = radLdapXtnNMASAuth(conn1->ld, user_dn, request->password->strvalue, seq, host_ipaddr, &challenge_len, challenge, &auth_state );
1916 ldap_release_conn(conn_id,inst->conns);
1917 if ( auth_state == -1)
1918 res = RLM_MODULE_FAIL;
1919 if ( auth_state != REQUEST_CHALLENGED){
1920 if (auth_state == REQUEST_ACCEPTED){
1921 DEBUG("rlm_ldap: user %s authenticated succesfully",request->username->strvalue);
1922 res = RLM_MODULE_OK;
1923 }else if(auth_state == REQUEST_REJECTED){
1924 DEBUG("rlm_ldap: user %s authentication failed",request->username->strvalue);
1925 res = RLM_MODULE_REJECT;
1928 /* Request challenged. Generate Reply-Message attribute with challenge data */
1929 pairadd(&request->reply->vps,pairmake("Reply-Message", challenge, T_OP_EQ));
1930 /* Generate state attribute */
1931 state = rad_malloc(MAX_CHALLENGE_LEN);
1932 (void) sprintf(state, "%s%s", challenge, challenge);
1933 vp_state = paircreate(PW_STATE, PW_TYPE_OCTETS);
1934 memcpy(vp_state->strvalue, state, strlen(state));
1935 vp_state->length = strlen(state);
1936 pairadd(&request->reply->vps, vp_state);
1938 /* Mark the packet as a Acceess-Challenge Packet */
1939 request->reply->code = PW_ACCESS_CHALLENGE;
1940 DEBUG("rlm_ldap: Sending Access-Challenge.");
1941 res = RLM_MODULE_HANDLED;
1946 case LDAP_SERVER_DOWN:
1947 radlog(L_ERR, "rlm_ldap: nmas authentication failed: LDAP connection lost."); conn->failed_conns++;
1948 if (conn->failed_conns <= MAX_FAILED_CONNS_START){
1949 radlog(L_INFO, "rlm_ldap: Attempting reconnect");
1955 return RLM_MODULE_FAIL;
1957 ldap_release_conn(conn_id,inst->conns);
1960 return RLM_MODULE_FAIL;
1966 ld_user = ldap_connect(instance, user_dn, request->password->strvalue,
1970 /* 'err' contains the LDAP connection error description */
1971 DEBUG("rlm_ldap: %s", err);
1972 pairadd(&request->reply->vps, pairmake("Reply-Message", err, T_OP_EQ));
1973 ldap_memfree((void *)err);
1977 if (ld_user == NULL){
1978 if (res == RLM_MODULE_REJECT){
1979 inst->failed_conns = 0;
1980 snprintf(module_fmsg,sizeof(module_fmsg),"rlm_ldap: Bind as user failed");
1981 module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
1982 pairadd(&request->packet->vps, module_fmsg_vp);
1984 if (res == RLM_MODULE_FAIL){
1985 DEBUG("rlm_ldap: ldap_connect() failed");
1986 inst->failed_conns++;
1991 DEBUG("rlm_ldap: user %s authenticated succesfully",
1992 request->username->vp_strvalue);
1993 ldap_unbind_s(ld_user);
1994 inst->failed_conns = 0;
1996 return RLM_MODULE_OK;
2000 /*****************************************************************************
2002 * Function: rlm_ldap_postauth
2004 * Purpose: Perform eDirectory account policy check and failed-login reporting
2007 *****************************************************************************/
2008 static int ldap_postauth(void *instance, REQUEST * request)
2010 int res = RLM_MODULE_FAIL;
2011 int inst_attr, apc_attr;
2012 char password[UNIVERSAL_PASS_LEN];
2013 ldap_instance *inst = instance;
2015 VALUE_PAIR *vp_inst, *vp_apc;
2018 dattr = dict_attrbyname("LDAP-Instance");
2019 inst_attr = dattr->attr;
2020 dattr = dict_attrbyname("eDir-APC");
2021 apc_attr = dattr->attr;
2023 vp_inst = pairfind(request->config_items, inst_attr);
2026 * Check if the password in the config items list is the user's UP which has
2027 * been read in the authorize method of this instance of the LDAP module.
2029 if((vp_inst == NULL) || strcmp(vp_inst->vp_strvalue, inst->xlat_name))
2030 return RLM_MODULE_NOOP;
2032 vp_apc = pairfind(request->config_items, apc_attr);
2034 switch(vp_apc->vp_strvalue[0]){
2036 /* Account policy check not enabled */
2038 /* Account policy check has been completed */
2039 res = RLM_MODULE_NOOP;
2043 int err, conn_id = -1;
2044 char *error_msg = NULL;
2045 VALUE_PAIR *vp_fdn, *vp_pwd;
2048 if (request->reply->code == PW_AUTHENTICATION_REJECT) {
2049 /* Bind to eDirectory as the RADIUS user with a wrong password. */
2050 vp_pwd = pairfind(request->config_items, PW_CLEARTEXT_PASSWORD);
2051 strcpy(password, vp_pwd->vp_strvalue);
2052 if (strlen(password) > 0) {
2053 if (password[0] != 'a') {
2059 strcpy(password, "dummy_password");
2061 res = RLM_MODULE_REJECT;
2063 /* Bind to eDirectory as the RADIUS user using the user's UP */
2064 vp_pwd = pairfind(request->config_items, PW_CLEARTEXT_PASSWORD);
2065 if (vp_pwd == NULL) {
2066 DEBUG("rlm_ldap: User's Universal Password not in config items list.");
2067 return RLM_MODULE_FAIL;
2069 strcpy(password, vp_pwd->vp_strvalue);
2072 if ((da = dict_attrbyname("Ldap-UserDn")) == NULL) {
2073 DEBUG("rlm_ldap: Attribute for user FDN not found in dictionary. Unable to proceed");
2074 return RLM_MODULE_FAIL;
2077 vp_fdn = pairfind(request->packet->vps, da->attr);
2078 if (vp_fdn == NULL) {
2079 DEBUG("rlm_ldap: User's FQDN not in config items list.");
2080 return RLM_MODULE_FAIL;
2083 if ((conn_id = ldap_get_conn(inst->apc_conns, &conn, inst)) == -1){
2084 radlog(L_ERR, "rlm_ldap: All ldap connections are in use");
2085 return RLM_MODULE_FAIL;
2089 * If there is an existing LDAP
2090 * connection to the directory,
2091 * bind over it. Otherwise,
2092 * establish a new connection.
2095 if (!conn->bound || conn->ld == NULL) {
2096 DEBUG2("rlm_ldap: attempting LDAP reconnection");
2098 DEBUG2("rlm_ldap: closing existing LDAP connection");
2099 ldap_unbind_s(conn->ld);
2101 if ((conn->ld = ldap_connect(instance, (char *)vp_fdn->vp_strvalue, password, 0, &res, &error_msg)) == NULL) {
2102 radlog(L_ERR, "rlm_ldap: eDirectory account policy check failed.");
2104 if (error_msg != NULL) {
2105 DEBUG("rlm_ldap: %s", error_msg);
2106 pairadd(&request->reply->vps, pairmake("Reply-Message", error_msg, T_OP_EQ));
2107 ldap_memfree((void *)error_msg);
2110 vp_apc->vp_strvalue[0] = '3';
2111 ldap_release_conn(conn_id, inst->apc_conns);
2112 return RLM_MODULE_REJECT;
2115 } else if((err = ldap_simple_bind_s(conn->ld, (char *)vp_fdn->vp_strvalue, password)) != LDAP_SUCCESS) {
2116 if (err == LDAP_SERVER_DOWN) {
2118 goto postauth_reconnect;
2120 DEBUG("rlm_ldap: eDirectory account policy check failed.");
2121 ldap_get_option(conn->ld, LDAP_OPT_ERROR_STRING, &error_msg);
2122 if (error_msg != NULL) {
2123 DEBUG("rlm_ldap: %s", error_msg);
2124 pairadd(&request->reply->vps, pairmake("Reply-Message", error_msg, T_OP_EQ));
2125 ldap_memfree((void *)error_msg);
2127 vp_apc->vp_strvalue[0] = '3';
2128 ldap_release_conn(conn_id, inst->apc_conns);
2129 return RLM_MODULE_REJECT;
2131 vp_apc->vp_strvalue[0] = '3';
2132 ldap_release_conn(conn_id, inst->apc_conns);
2133 return RLM_MODULE_OK;
2140 static LDAP *ldap_connect(void *instance, const char *dn, const char *password,
2141 int auth, int *result, char **err)
2143 ldap_instance *inst = instance;
2145 int msgid, rc, ldap_version;
2150 #ifdef HAVE_LDAP_INITIALIZE
2151 DEBUG("rlm_ldap: (re)connect to %s, authentication %d", inst->server, auth);
2152 if (ldap_initialize(&ld, inst->server) != LDAP_SUCCESS) {
2153 radlog(L_ERR, "rlm_ldap: ldap_initialize() failed");
2154 *result = RLM_MODULE_FAIL;
2159 DEBUG("rlm_ldap: (re)connect to %s:%d, authentication %d", inst->server, inst->port, auth);
2160 if ((ld = ldap_init(inst->server, inst->port)) == NULL) {
2161 radlog(L_ERR, "rlm_ldap: ldap_init() failed");
2162 *result = RLM_MODULE_FAIL;
2166 if (ldap_set_option(ld, LDAP_OPT_NETWORK_TIMEOUT,
2167 (void *) &(inst->net_timeout)) != LDAP_OPT_SUCCESS) {
2168 radlog(L_ERR, "rlm_ldap: Could not set LDAP_OPT_NETWORK_TIMEOUT %ld.%ld", inst->net_timeout.tv_sec, inst->net_timeout.tv_usec);
2171 if (ldap_set_option(ld, LDAP_OPT_TIMELIMIT,
2172 (void *) &(inst->timelimit)) != LDAP_OPT_SUCCESS) {
2173 radlog(L_ERR, "rlm_ldap: Could not set LDAP_OPT_TIMELIMIT %d", inst->timelimit);
2176 if (inst->ldap_debug && ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &(inst->ldap_debug)) != LDAP_OPT_SUCCESS) {
2177 radlog(L_ERR, "rlm_ldap: Could not set LDAP_OPT_DEBUG_LEVEL %d", inst->ldap_debug);
2180 ldap_version = LDAP_VERSION3;
2181 if (ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION,
2182 &ldap_version) != LDAP_OPT_SUCCESS) {
2183 radlog(L_ERR, "rlm_ldap: Could not set LDAP version to V3");
2186 #ifdef HAVE_LDAP_START_TLS
2187 if (inst->tls_mode) {
2188 DEBUG("rlm_ldap: setting TLS mode to %d", inst->tls_mode);
2189 if (ldap_set_option(ld, LDAP_OPT_X_TLS,
2190 (void *) &(inst->tls_mode)) != LDAP_OPT_SUCCESS) {
2191 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
2192 radlog(L_ERR, "rlm_ldap: could not set LDAP_OPT_X_TLS option %s", ldap_err2string(ldap_errno));
2196 if (inst->tls_cacertfile != NULL) {
2197 DEBUG("rlm_ldap: setting TLS CACert File to %s", inst->tls_cacertfile);
2199 if ( ldap_set_option( NULL, LDAP_OPT_X_TLS_CACERTFILE,
2200 (void *) inst->tls_cacertfile )
2201 != LDAP_OPT_SUCCESS) {
2202 radlog(L_ERR, "rlm_ldap: could not set "
2203 "LDAP_OPT_X_TLS_CACERTFILE option to %s", inst->tls_cacertfile);
2207 if (inst->tls_cacertdir != NULL) {
2208 DEBUG("rlm_ldap: setting TLS CACert Directory to %s", inst->tls_cacertdir);
2210 if ( ldap_set_option( NULL, LDAP_OPT_X_TLS_CACERTDIR,
2211 (void *) inst->tls_cacertdir )
2212 != LDAP_OPT_SUCCESS) {
2213 radlog(L_ERR, "rlm_ldap: could not set "
2214 "LDAP_OPT_X_TLS_CACERTDIR option to %s", inst->tls_cacertdir);
2218 if (strcmp(TLS_DEFAULT_VERIFY, inst->tls_require_cert ) != 0 ) {
2219 DEBUG("rlm_ldap: setting TLS Require Cert to %s",
2220 inst->tls_require_cert);
2224 #ifdef HAVE_LDAP_INT_TLS_CONFIG
2225 if (ldap_int_tls_config(NULL, LDAP_OPT_X_TLS_REQUIRE_CERT,
2226 (inst->tls_require_cert)) != LDAP_OPT_SUCCESS) {
2227 radlog(L_ERR, "rlm_ldap: could not set "
2228 "LDAP_OPT_X_TLS_REQUIRE_CERT option to %s",
2229 inst->tls_require_cert);
2233 if (inst->tls_certfile != NULL) {
2234 DEBUG("rlm_ldap: setting TLS Cert File to %s", inst->tls_certfile);
2236 if (ldap_set_option(NULL, LDAP_OPT_X_TLS_CERTFILE,
2237 (void *) inst->tls_certfile)
2238 != LDAP_OPT_SUCCESS) {
2239 radlog(L_ERR, "rlm_ldap: could not set "
2240 "LDAP_OPT_X_TLS_CERTFILE option to %s",
2241 inst->tls_certfile);
2245 if (inst->tls_keyfile != NULL) {
2246 DEBUG("rlm_ldap: setting TLS Key File to %s",
2249 if ( ldap_set_option( NULL, LDAP_OPT_X_TLS_KEYFILE,
2250 (void *) inst->tls_keyfile )
2251 != LDAP_OPT_SUCCESS) {
2252 radlog(L_ERR, "rlm_ldap: could not set "
2253 "LDAP_OPT_X_TLS_KEYFILE option to %s",
2258 if (inst->tls_randfile != NULL) {
2259 DEBUG("rlm_ldap: setting TLS Key File to %s",
2260 inst->tls_randfile);
2262 if (ldap_set_option(NULL, LDAP_OPT_X_TLS_RANDOM_FILE,
2263 (void *) inst->tls_randfile)
2264 != LDAP_OPT_SUCCESS) {
2265 radlog(L_ERR, "rlm_ldap: could not set "
2266 "LDAP_OPT_X_TLS_RANDOM_FILE option to %s",
2267 inst->tls_randfile);
2271 if (inst->start_tls) {
2272 DEBUG("rlm_ldap: starting TLS");
2273 rc = ldap_start_tls_s(ld, NULL, NULL);
2274 if (rc != LDAP_SUCCESS) {
2275 DEBUG("rlm_ldap: ldap_start_tls_s()");
2276 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER,
2278 radlog(L_ERR, "rlm_ldap: could not start TLS %s",
2279 ldap_err2string(ldap_errno));
2280 *result = RLM_MODULE_FAIL;
2285 #endif /* HAVE_LDAP_START_TLS */
2288 DEBUG("rlm_ldap: bind as %s/%s to %s",
2289 dn, password, inst->server);
2291 DEBUG("rlm_ldap: bind as %s/%s to %s:%d",
2292 dn, password, inst->server, inst->port);
2295 msgid = ldap_bind(ld, dn, password,LDAP_AUTH_SIMPLE);
2297 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
2299 ldap_get_option(ld, LDAP_OPT_ERROR_STRING, err);
2302 radlog(L_ERR, "rlm_ldap: %s bind to %s failed: %s",
2303 dn, inst->server, ldap_err2string(ldap_errno));
2305 radlog(L_ERR, "rlm_ldap: %s bind to %s:%d failed: %s",
2306 dn, inst->server, inst->port,
2307 ldap_err2string(ldap_errno));
2309 *result = RLM_MODULE_FAIL;
2313 DEBUG("rlm_ldap: waiting for bind result ...");
2315 rc = ldap_result(ld, msgid, 1, &(inst->timeout), &res);
2318 DEBUG("rlm_ldap: ldap_result()");
2319 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
2321 ldap_get_option(ld, LDAP_OPT_ERROR_STRING, err);
2324 radlog(L_ERR, "rlm_ldap: %s bind to %s failed: %s",
2325 dn, inst->server, (rc == 0) ? "timeout" : ldap_err2string(ldap_errno));
2327 radlog(L_ERR, "rlm_ldap: %s bind to %s:%d failed: %s",
2328 dn, inst->server, inst->port,
2329 (rc == 0) ? "timeout" : ldap_err2string(ldap_errno));
2331 *result = RLM_MODULE_FAIL;
2336 ldap_errno = ldap_result2error(ld, res, 1);
2337 switch (ldap_errno) {
2339 DEBUG("rlm_ldap: Bind was successful");
2340 *result = RLM_MODULE_OK;
2343 case LDAP_INVALID_CREDENTIALS:
2345 DEBUG("rlm_ldap: Bind failed with invalid credentials");
2346 *result = RLM_MODULE_REJECT;
2348 radlog(L_ERR, "rlm_ldap: LDAP login failed: check identity, password settings in ldap section of radiusd.conf");
2349 *result = RLM_MODULE_FAIL;
2352 ldap_get_option(ld, LDAP_OPT_ERROR_STRING, err);
2358 radlog(L_ERR,"rlm_ldap: %s bind to %s failed %s",
2359 dn, inst->server, ldap_err2string(ldap_errno));
2361 radlog(L_ERR,"rlm_ldap: %s bind to %s:%d failed %s",
2362 dn, inst->server, inst->port,
2363 ldap_err2string(ldap_errno));
2365 *result = RLM_MODULE_FAIL;
2367 ldap_get_option(ld, LDAP_OPT_ERROR_STRING, err);
2371 if (*result != RLM_MODULE_OK) {
2378 /*****************************************************************************
2380 * Detach from the LDAP server and cleanup internal state.
2382 *****************************************************************************/
2384 ldap_detach(void *instance)
2386 ldap_instance *inst = instance;
2387 TLDAP_RADIUS *pair, *nextpair;
2392 for (i = 0;i < inst->num_conns; i++) {
2393 if (inst->conns[i].ld){
2394 ldap_unbind_s(inst->conns[i].ld);
2396 pthread_mutex_destroy(&inst->conns[i].mutex);
2402 if (inst->apc_conns){
2405 for (i = 0; i < inst->num_conns; i++) {
2406 if (inst->apc_conns[i].ld){
2407 ldap_unbind_s(inst->apc_conns[i].ld);
2409 pthread_mutex_destroy(&inst->apc_conns[i].mutex);
2411 free(inst->apc_conns);
2415 pair = inst->check_item_map;
2417 while (pair != NULL) {
2418 nextpair = pair->next;
2420 free(pair->radius_attr);
2425 pair = inst->reply_item_map;
2427 while (pair != NULL) {
2428 nextpair = pair->next;
2430 free(pair->radius_attr);
2438 paircompare_unregister(PW_LDAP_GROUP, ldap_groupcmp);
2439 xlat_unregister(inst->xlat_name,ldap_xlat);
2440 free(inst->xlat_name);
2450 fieldcpy(char *string, char **uptr)
2455 while (*ptr == ' ' || *ptr == '\t') {
2460 while (*ptr != '"' && *ptr != '\0' && *ptr != '\n') {
2470 while (*ptr != ' ' && *ptr != '\t' && *ptr != '\0' && *ptr != '\n' &&
2471 *ptr != '=' && *ptr != ',') {
2481 * Copied from src/lib/token.c
2483 static const LRAD_NAME_NUMBER tokens[] = {
2484 { "=~", T_OP_REG_EQ, }, /* order is important! */
2485 { "!~", T_OP_REG_NE, },
2486 { "{", T_LCBRACE, },
2487 { "}", T_RCBRACE, },
2491 { "+=", T_OP_ADD, },
2492 { "-=", T_OP_SUB, },
2493 { ":=", T_OP_SET, },
2494 { "=*", T_OP_CMP_TRUE, },
2495 { "!*", T_OP_CMP_FALSE, },
2496 { "==", T_OP_CMP_EQ, },
2506 /*****************************************************************************
2507 * Get RADIUS attributes from LDAP object
2508 * ( according to draft-adoba-radius-05.txt
2509 * <http://www.ietf.org/internet-drafts/draft-adoba-radius-05.txt> )
2511 *****************************************************************************/
2512 static VALUE_PAIR *ldap_pairget(LDAP *ld, LDAPMessage *entry,
2513 TLDAP_RADIUS *item_map,
2514 VALUE_PAIR **pairs, int is_check)
2521 TLDAP_RADIUS *element;
2522 LRAD_TOKEN token, operator;
2523 int is_generic_attribute;
2524 char buf[MAX_STRING_LEN];
2525 VALUE_PAIR *pairlist = NULL;
2526 VALUE_PAIR *newpair = NULL;
2527 char do_xlat = FALSE;
2528 char print_buffer[2048];
2531 * check if there is a mapping from this LDAP attribute
2532 * to a RADIUS attribute
2534 for (element = item_map; element != NULL; element = element->next) {
2536 * No mapping, skip it.
2538 if ((vals = ldap_get_values(ld,entry,element->attr)) == NULL)
2542 * Check whether this is a one-to-one-mapped ldap
2543 * attribute or a generic attribute and set flag
2546 if (strcasecmp(element->radius_attr, GENERIC_ATTRIBUTE_ID)==0)
2547 is_generic_attribute = 1;
2549 is_generic_attribute = 0;
2552 * Find out how many values there are for the
2553 * attribute and extract all of them.
2555 vals_count = ldap_count_values(vals);
2557 for (vals_idx = 0; vals_idx < vals_count; vals_idx++) {
2558 value = vals[vals_idx];
2560 if (is_generic_attribute) {
2562 * This is a generic attribute.
2564 LRAD_TOKEN dummy; /* makes pairread happy */
2566 /* not sure if using pairread here is ok ... */
2567 if ( (newpair = pairread(&value, &dummy)) != NULL) {
2568 DEBUG("rlm_ldap: extracted attribute %s from generic item %s",
2569 newpair->name, vals[vals_idx]);
2570 pairadd(&pairlist, newpair);
2572 radlog(L_ERR, "rlm_ldap: parsing %s failed: %s",
2573 element->attr, vals[vals_idx]);
2577 * This is a one-to-one-mapped attribute
2580 operator = gettoken(&ptr, buf, sizeof(buf));
2581 if (operator < T_EQSTART || operator > T_EQEND) {
2582 /* no leading operator found */
2583 if (element->operator != T_OP_INVALID)
2584 operator = element->operator;
2586 operator = T_OP_CMP_EQ;
2590 /* the value is after the operator */
2595 * Do xlat if the *entire* string
2598 if ((value[0] == '\'' || value[0] == '"' ||
2600 (value[0] == value[strlen(value)-1])) {
2602 token = gettoken(&ptr, buf, sizeof(buf));
2604 /* take the unquoted string */
2605 case T_SINGLE_QUOTED_STRING:
2606 case T_DOUBLE_QUOTED_STRING:
2610 /* the value will be xlat'ed later */
2611 case T_BACK_QUOTED_STRING:
2616 /* keep the original string */
2621 if (value[0] == '\0') {
2622 DEBUG("rlm_ldap: Attribute %s has no value", element->attr);
2629 newpair = pairmake(element->radius_attr,
2630 do_xlat ? NULL : value,
2632 if (newpair == NULL) {
2633 radlog(L_ERR, "rlm_ldap: Failed to create the pair: %s", librad_errstr);
2638 newpair->flags.do_xlat = 1;
2639 strlcpy(newpair->vp_strvalue, buf,
2640 sizeof(newpair->vp_strvalue));
2641 newpair->length = 0;
2643 vp_prints(print_buffer, sizeof(print_buffer),
2645 DEBUG("rlm_ldap: LDAP attribute %s as RADIUS attribute %s",
2646 element->attr, print_buffer);
2650 * Add the pair into the packet.
2653 pairdelete(pairs, newpair->attribute);
2655 pairadd(&pairlist, newpair);
2658 ldap_value_free(vals);
2664 /* globally exported name */
2665 module_t rlm_ldap = {
2668 RLM_TYPE_THREAD_SAFE, /* type: reserved */
2669 ldap_instantiate, /* instantiation */
2670 ldap_detach, /* detach */
2672 ldap_authenticate, /* authentication */
2673 ldap_authorize, /* authorization */
2674 NULL, /* preaccounting */
2675 NULL, /* accounting */
2676 NULL, /* checksimul */
2677 NULL, /* pre-proxy */
2678 NULL, /* post-proxy */
2680 ldap_postauth /* post-auth */