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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Copyright 2004 The FreeRADIUS Server Project.
20 static const char rcsid[] = "$Id$";
24 #include <sys/types.h>
25 #include <sys/socket.h>
27 #include <netinet/in.h>
44 #include "libradius.h"
48 #include "rad_assert.h"
50 #ifndef HAVE_PTHREAD_H
52 * This is a lot simpler than putting ifdef's around
53 * every use of the pthread functions.
55 #define pthread_mutex_lock(a)
56 #define pthread_mutex_unlock(a)
57 #define pthread_mutex_init(a,b)
58 #define pthread_mutex_destroy(a)
62 #define MAX_FILTER_STR_LEN 1024
66 * These are used in case ldap_search returns LDAP_SERVER_DOWN
67 * In that case we do conn->failed_conns++ and then check it:
68 * If conn->failed_conns <= MAX_FAILED_CONNS_START then we try
70 * conn->failed_conns is also checked on entrance in perform_search:
71 * If conn->failed_conns > MAX_FAILED_CONNS_START then we don't
72 * try to do anything and we just do conn->failed_conns++ and
73 * return RLM_MODULE_FAIL
74 * if conn->failed_conns >= MAX_FAILED_CONNS_END then we give it
75 * another chance and we set it to MAX_FAILED_CONNS_RESTART and
79 * We are assuming that the majority of the LDAP_SERVER_DOWN cases
80 * will either be an ldap connection timeout or a temporary ldap
82 * As a result we make a few attempts to reconnect hoping that the problem
83 * will soon go away. If it does not go away then we just return
84 * RLM_MODULE_FAIL on entrance in perform_search until conn->failed_conns
85 * gets to MAX_FAILED_CONNS_END. After that we give it one more chance by
86 * going back to MAX_FAILED_CONNS_RESTART
90 #define MAX_FAILED_CONNS_END 20
91 #define MAX_FAILED_CONNS_RESTART 4
92 #define MAX_FAILED_CONNS_START 5
94 #ifdef NOVELL_UNIVERSAL_PASSWORD
96 /* Universal Password Length */
97 #define UNIVERSAL_PASS_LEN 256
99 int nmasldap_get_password(
102 size_t *pwdSize, // in bytes
106 /* linked list of mappings between RADIUS attributes and LDAP attributes */
107 struct TLDAP_RADIUS {
111 struct TLDAP_RADIUS* next;
113 typedef struct TLDAP_RADIUS TLDAP_RADIUS;
115 typedef struct ldap_conn {
120 #ifdef HAVE_PTHREAD_H
121 pthread_mutex_t mutex;
129 struct timeval net_timeout;
130 struct timeval timeout;
145 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;
158 int ldap_debug; /* Debug flag for LDAP SDK */
159 char *xlat_name; /* name used to xlat */
160 char *tls_cacertfile;
165 char *tls_require_cert;
168 /* The default setting for TLS Certificate Verification */
169 #define TLS_DEFAULT_VERIFY "allow"
171 static CONF_PARSER module_config[] = {
172 {"server", PW_TYPE_STRING_PTR, offsetof(ldap_instance,server), NULL, "localhost"},
173 {"port", PW_TYPE_INTEGER, offsetof(ldap_instance,port), NULL, "389"},
174 /* wait forever on network activity */
175 {"net_timeout", PW_TYPE_INTEGER, offsetof(ldap_instance,net_timeout.tv_sec), NULL, "10"},
176 /* wait forever for search results */
177 {"timeout", PW_TYPE_INTEGER, offsetof(ldap_instance,timeout.tv_sec), NULL, "20"},
178 /* allow server unlimited time for search (server-side limit) */
179 {"timelimit", PW_TYPE_INTEGER, offsetof(ldap_instance,timelimit), NULL, "20"},
180 {"identity", PW_TYPE_STRING_PTR, offsetof(ldap_instance,login), NULL, ""},
181 {"tls_mode", PW_TYPE_BOOLEAN, offsetof(ldap_instance,tls_mode), NULL, "no"},
182 {"start_tls", PW_TYPE_BOOLEAN, offsetof(ldap_instance,start_tls), NULL, "no"},
183 {"tls_cacertfile", PW_TYPE_STRING_PTR, offsetof(ldap_instance,tls_cacertfile), NULL, NULL},
184 {"tls_cacertdir", PW_TYPE_STRING_PTR, offsetof(ldap_instance,tls_cacertdir), NULL, NULL},
185 {"tls_certfile", PW_TYPE_STRING_PTR, offsetof(ldap_instance,tls_certfile), NULL, NULL},
186 {"tls_keyfile", PW_TYPE_STRING_PTR, offsetof(ldap_instance,tls_keyfile), NULL, NULL},
187 {"tls_randfile", PW_TYPE_STRING_PTR, offsetof(ldap_instance,tls_randfile), NULL, NULL},
188 {"tls_require_cert", PW_TYPE_STRING_PTR, offsetof(ldap_instance,tls_require_cert), NULL, TLS_DEFAULT_VERIFY},
189 {"password", PW_TYPE_STRING_PTR, offsetof(ldap_instance,password), NULL, ""},
190 {"basedn", PW_TYPE_STRING_PTR, offsetof(ldap_instance,basedn), NULL, "o=notexist"},
191 {"filter", PW_TYPE_STRING_PTR, offsetof(ldap_instance,filter), NULL, "(uid=%u)"},
192 {"base_filter", PW_TYPE_STRING_PTR, offsetof(ldap_instance,base_filter), NULL, "(objectclass=radiusprofile)"},
193 {"default_profile", PW_TYPE_STRING_PTR, offsetof(ldap_instance,default_profile), NULL, NULL},
194 {"profile_attribute", PW_TYPE_STRING_PTR, offsetof(ldap_instance,profile_attr), NULL, NULL},
195 {"password_header", PW_TYPE_STRING_PTR, offsetof(ldap_instance,passwd_hdr), NULL, NULL},
196 {"password_attribute", PW_TYPE_STRING_PTR, offsetof(ldap_instance,passwd_attr), NULL, NULL},
197 /* LDAP attribute name that controls remote access */
198 {"access_attr", PW_TYPE_STRING_PTR, offsetof(ldap_instance,access_attr), NULL, NULL},
199 /* file with mapping between LDAP and RADIUS attributes */
200 {"groupname_attribute", PW_TYPE_STRING_PTR, offsetof(ldap_instance,groupname_attr), NULL, "cn"},
201 {"groupmembership_filter", PW_TYPE_STRING_PTR, offsetof(ldap_instance,groupmemb_filt), NULL, "(|(&(objectClass=GroupOfNames)(member=%{Ldap-UserDn}))(&(objectClass=GroupOfUniqueNames)(uniquemember=%{Ldap-UserDn})))"},
202 {"groupmembership_attribute", PW_TYPE_STRING_PTR, offsetof(ldap_instance,groupmemb_attr), NULL, NULL},
203 {"dictionary_mapping", PW_TYPE_STRING_PTR, offsetof(ldap_instance,dictionary_mapping), NULL, "${confdir}/ldap.attrmap"},
204 {"ldap_debug", PW_TYPE_INTEGER, offsetof(ldap_instance,ldap_debug), NULL, "0x0000"},
205 {"ldap_connections_number", PW_TYPE_INTEGER, offsetof(ldap_instance,num_conns), NULL, "5"},
206 {"compare_check_items", PW_TYPE_BOOLEAN, offsetof(ldap_instance,do_comp), NULL, "no"},
207 {"access_attr_used_for_allow", PW_TYPE_BOOLEAN, offsetof(ldap_instance,default_allow), NULL, "yes"},
208 {"do_xlat", PW_TYPE_BOOLEAN, offsetof(ldap_instance,do_xlat), NULL, "yes"},
210 {NULL, -1, 0, NULL, NULL}
213 #define ld_valid ld_options.ldo_valid
214 #define LDAP_VALID_SESSION 0x2
215 #define LDAP_VALID(ld) ( (ld)->ld_valid == LDAP_VALID_SESSION )
218 static void fieldcpy(char *, char **);
220 static VALUE_PAIR *ldap_pairget(LDAP *, LDAPMessage *, TLDAP_RADIUS *,VALUE_PAIR **,char);
221 static int ldap_groupcmp(void *, REQUEST *, VALUE_PAIR *, VALUE_PAIR *, VALUE_PAIR *, VALUE_PAIR **);
222 static int ldap_xlat(void *,REQUEST *, char *, char *,int, RADIUS_ESCAPE_STRING);
223 static LDAP *ldap_connect(void *instance, const char *, const char *, int, int *);
224 static int read_mappings(ldap_instance* inst);
226 static inline int ldap_get_conn(LDAP_CONN *conns,LDAP_CONN **ret,void *instance)
228 ldap_instance *inst = instance;
231 for(i=0;i<inst->num_conns;i++){
232 DEBUG("rlm_ldap: ldap_get_conn: Checking Id: %d",i);
233 if ((conns[i].locked == 0) &&
234 (pthread_mutex_trylock(&(conns[i].mutex)) == 0)) {
237 DEBUG("rlm_ldap: ldap_get_conn: Got Id: %d",i);
245 static inline void ldap_release_conn(int i, LDAP_CONN *conns)
247 DEBUG("rlm_ldap: ldap_release_conn: Release Id: %d",i);
249 pthread_mutex_unlock(&(conns[i].mutex));
252 /*************************************************************************
254 * Function: rlm_ldap_instantiate
256 * Purpose: Uses section of radiusd config file passed as parameter
257 * to create an instance of the module.
259 *************************************************************************/
261 ldap_instantiate(CONF_SECTION * conf, void **instance)
266 int reply_map_num = 0;
267 int check_map_num = 0;
268 int att_map[3] = {0,0,0};
273 inst = rad_malloc(sizeof *inst);
277 memset(inst, 0, sizeof(*inst));
279 if (cf_section_parse(conf, inst, module_config) < 0) {
284 if (inst->server == NULL) {
285 radlog(L_ERR, "rlm_ldap: missing 'server' directive.");
290 if (ldap_is_ldap_url(inst->server)){
291 #ifdef HAVE_LDAP_INITIALIZE
295 radlog(L_ERR, "rlm_ldap: 'server' directive is in URL form but ldap_initialize() is not available.");
301 inst->timeout.tv_usec = 0;
302 inst->net_timeout.tv_usec = 0;
303 /* workaround for servers which support LDAPS but not START TLS */
304 if(inst->port == LDAPS_PORT || inst->tls_mode)
305 inst->tls_mode = LDAP_OPT_X_TLS_HARD;
308 inst->reply_item_map = NULL;
309 inst->check_item_map = NULL;
311 inst->failed_conns = 0;
313 DEBUG("rlm_ldap: Registering ldap_groupcmp for Ldap-Group");
314 paircompare_register(PW_LDAP_GROUP, PW_USER_NAME, ldap_groupcmp, inst);
316 xlat_name = cf_section_name2(conf);
317 if (xlat_name != NULL){
322 * Allocate room for <instance>-Ldap-Group
324 group_name = malloc((strlen(xlat_name) + 1 + 11) * sizeof(char));
325 rad_assert(group_name != NULL);
326 sprintf(group_name,"%s-Ldap-Group",xlat_name);
327 DEBUG("rlm_ldap: Creating new attribute %s",group_name);
328 dict_addattr(group_name, 0, PW_TYPE_STRING, -1, flags);
329 dattr = dict_attrbyname(group_name);
331 radlog(L_ERR, "rlm_ldap: Failed to create attribute %s",group_name);
335 DEBUG("rlm_ldap: Registering ldap_groupcmp for %s",group_name);
336 paircompare_register(dattr->attr, PW_USER_NAME, ldap_groupcmp, inst);
339 xlat_name = cf_section_name1(conf);
340 rad_assert(xlat_name != NULL); /* or all hell breaks loose */
342 inst->xlat_name = strdup(xlat_name);
343 DEBUG("rlm_ldap: Registering ldap_xlat with xlat_name %s",xlat_name);
344 xlat_register(xlat_name,ldap_xlat,inst);
346 if (inst->num_conns <= 0){
347 radlog(L_ERR, "rlm_ldap: Invalid ldap connections number passed.");
351 inst->conns = (LDAP_CONN *)malloc(sizeof(LDAP_CONN)*inst->num_conns);
352 if (inst->conns == NULL){
353 radlog(L_ERR, "rlm_ldap: Could not allocate memory. Aborting.");
357 for(;i<inst->num_conns;i++){
358 inst->conns[i].bound = 0;
359 inst->conns[i].locked = 0;
360 inst->conns[i].failed_conns = 0;
361 inst->conns[i].ld = NULL;
362 pthread_mutex_init(&inst->conns[i].mutex, NULL);
365 if (read_mappings(inst) != 0) {
366 radlog(L_ERR, "rlm_ldap: Reading dictionary mappings from file %s failed",
367 inst->dictionary_mapping);
371 if ((inst->check_item_map == NULL) &&
372 (inst->reply_item_map == NULL)) {
373 radlog(L_ERR, "rlm_ldap: dictionary mappings file %s did not contain any mappings",
374 inst->dictionary_mapping);
379 pair = inst->check_item_map;
384 check_map_num = (atts_num - 1);
385 pair = inst->reply_item_map;
390 reply_map_num = (atts_num - 1);
391 if (inst->profile_attr)
393 if (inst->passwd_attr)
395 if (inst->access_attr)
397 inst->atts = (char **)malloc(sizeof(char *)*(atts_num + 1));
398 if (inst->atts == NULL){
399 radlog(L_ERR, "rlm_ldap: Could not allocate memory. Aborting.");
403 pair = inst->check_item_map;
405 pair = inst->reply_item_map;
406 for(i=0;i<atts_num;i++){
407 if (i <= check_map_num ){
408 inst->atts[i] = pair->attr;
409 if (i == check_map_num)
410 pair = inst->reply_item_map;
414 else if (i <= reply_map_num){
415 inst->atts[i] = pair->attr;
419 if (inst->profile_attr && !att_map[0]){
420 inst->atts[i] = inst->profile_attr;
423 else if (inst->passwd_attr && !att_map[1]){
424 inst->atts[i] = inst->passwd_attr;
427 else if (inst->access_attr && !att_map[2]){
428 inst->atts[i] = inst->access_attr;
433 inst->atts[atts_num] = NULL;
435 DEBUG("conns: %p",inst->conns);
445 * read_mappings(...) reads a ldap<->radius mappings file to inst->reply_item_map and inst->check_item_map
448 #define MAX_LINE_LEN 160
449 #define GENERIC_ATTRIBUTE_ID "$GENERIC$"
452 read_mappings(ldap_instance* inst)
456 /* all buffers are of MAX_LINE_LEN so we can use sscanf without being afraid of buffer overflows */
457 char buf[MAX_LINE_LEN], itemType[MAX_LINE_LEN];
458 char radiusAttribute[MAX_LINE_LEN], ldapAttribute[MAX_LINE_LEN];
461 char opstring[MAX_LINE_LEN];
463 /* open the mappings file for reading */
465 filename = inst->dictionary_mapping;
466 DEBUG("rlm_ldap: reading ldap<->radius mappings from file %s", filename);
467 mapfile = fopen(filename, "r");
469 if (mapfile == NULL) {
470 radlog(L_ERR, "rlm_ldap: Opening file %s failed", filename);
471 return -1; /* error */
474 /* read file line by line. Note that if line length exceed MAX_LINE_LEN, line numbers will be mixed up */
478 while (fgets(buf, sizeof buf, mapfile)!=NULL) {
486 ptr = strchr(buf, '#');
490 if (buf[0] == 0) continue;
492 /* extract tokens from the string */
493 token_count = sscanf(buf, "%s %s %s %s",
494 itemType, radiusAttribute,
495 ldapAttribute, opstring);
497 if (token_count <= 0) /* no tokens */
500 if ((token_count < 3) || (token_count > 4)) {
501 radlog(L_ERR, "rlm_ldap: Skipping %s line %i: %s",
502 filename, linenumber, buf);
503 radlog(L_ERR, "rlm_ldap: Expected 3 to 4 tokens "
504 "(Item type, RADIUS Attribute and LDAP Attribute) but found only %i", token_count);
508 if (token_count == 3) {
509 operator = T_OP_INVALID; /* use defaults */
514 operator = gettoken(&ptr, buf, sizeof(buf));
515 if ((operator < T_OP_ADD) || (operator > T_OP_CMP_EQ)) {
516 radlog(L_ERR, "rlm_ldap: file %s: skipping line %i: unknown or invalid operator %s",
517 filename, linenumber, opstring);
519 free(pair->radius_attr);
525 /* create new TLDAP_RADIUS list node */
526 pair = rad_malloc(sizeof(TLDAP_RADIUS));
528 pair->attr = strdup(ldapAttribute);
529 pair->radius_attr = strdup(radiusAttribute);
530 pair->operator = operator;
532 if ( (pair->attr == NULL) || (pair->radius_attr == NULL) ) {
533 radlog(L_ERR, "rlm_ldap: Out of memory");
534 if (pair->attr) free(pair->attr);
535 if (pair->radius_attr) free(pair->radius_attr);
541 /* push node to correct list */
542 if (strcasecmp(itemType, "checkItem") == 0) {
543 pair->next = inst->check_item_map;
544 inst->check_item_map = pair;
545 } else if (strcasecmp(itemType, "replyItem") == 0) {
546 pair->next = inst->reply_item_map;
547 inst->reply_item_map = pair;
549 radlog(L_ERR, "rlm_ldap: file %s: skipping line %i: unknown itemType %s",
550 filename, linenumber, itemType);
552 free(pair->radius_attr);
557 DEBUG("rlm_ldap: LDAP %s mapped to RADIUS %s",
558 pair->attr, pair->radius_attr);
563 return 0; /* success */
567 perform_search(void *instance, LDAP_CONN *conn, char *search_basedn, int scope, char *filter,
568 char **attrs, LDAPMessage ** result)
570 int res = RLM_MODULE_OK;
572 ldap_instance *inst = instance;
573 int search_retry = 0;
578 radlog(L_ERR, "rlm_ldap: NULL connection handle passed");
579 return RLM_MODULE_FAIL;
581 if (conn->failed_conns > MAX_FAILED_CONNS_START){
582 conn->failed_conns++;
583 if (conn->failed_conns >= MAX_FAILED_CONNS_END){
584 conn->failed_conns = MAX_FAILED_CONNS_RESTART;
589 if (!conn->bound || conn->ld == NULL) {
590 DEBUG2("rlm_ldap: attempting LDAP reconnection");
592 DEBUG2("rlm_ldap: closing existing LDAP connection");
593 ldap_unbind_s(conn->ld);
595 if ((conn->ld = ldap_connect(instance, inst->login,
596 inst->password, 0, &res)) == NULL) {
597 radlog(L_ERR, "rlm_ldap: (re)connection attempt failed");
598 if (search_retry == 0)
599 conn->failed_conns++;
600 return (RLM_MODULE_FAIL);
603 conn->failed_conns = 0;
605 DEBUG2("rlm_ldap: performing search in %s, with filter %s",
606 search_basedn ? search_basedn : "(null)" , filter);
607 switch (ldap_search_st(conn->ld, search_basedn, scope, filter,
608 attrs, 0, &(inst->timeout), result)) {
610 case LDAP_NO_SUCH_OBJECT:
612 case LDAP_SERVER_DOWN:
613 radlog(L_ERR, "rlm_ldap: ldap_search() failed: LDAP connection lost.");
614 conn->failed_conns++;
615 if (search_retry == 0){
616 if (conn->failed_conns <= MAX_FAILED_CONNS_START){
617 radlog(L_INFO, "rlm_ldap: Attempting reconnect");
620 ldap_msgfree(*result);
624 ldap_msgfree(*result);
625 return RLM_MODULE_FAIL;
626 case LDAP_INSUFFICIENT_ACCESS:
627 radlog(L_ERR, "rlm_ldap: ldap_search() failed: Insufficient access. Check the identity and password configuration directives.");
628 ldap_msgfree(*result);
629 return RLM_MODULE_FAIL;
631 radlog(L_ERR, "rlm_ldap: ldap_search() failed: Timed out while waiting for server to respond. Please increase the timeout.");
632 ldap_msgfree(*result);
633 return RLM_MODULE_FAIL;
634 case LDAP_FILTER_ERROR:
635 radlog(L_ERR, "rlm_ldap: ldap_search() failed: Bad search filter: %s",filter);
636 ldap_msgfree(*result);
637 return RLM_MODULE_FAIL;
638 case LDAP_TIMELIMIT_EXCEEDED:
640 case LDAP_UNAVAILABLE:
641 /* We don't need to reconnect in these cases so we don't set conn->bound */
642 ldap_get_option(conn->ld, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
643 radlog(L_ERR, "rlm_ldap: ldap_search() failed: %s",
644 ldap_err2string(ldap_errno));
645 ldap_msgfree(*result);
646 return (RLM_MODULE_FAIL);
648 ldap_get_option(conn->ld, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
649 radlog(L_ERR, "rlm_ldap: ldap_search() failed: %s",
650 ldap_err2string(ldap_errno));
652 ldap_msgfree(*result);
653 return (RLM_MODULE_FAIL);
656 if ((ldap_count_entries(conn->ld, *result)) != 1) {
657 DEBUG("rlm_ldap: object not found or got ambiguous search result");
658 res = RLM_MODULE_NOTFOUND;
659 ldap_msgfree(*result);
666 * Translate the LDAP queries.
668 static int ldap_escape_func(char *out, int outlen, const char *in)
674 * Only one byte left.
680 if (strchr("*", *in)) {
687 * Else it's a nice character.
700 * ldap_groupcmp(). Implement the Ldap-Group == "group" filter
703 static int ldap_groupcmp(void *instance, REQUEST *req, VALUE_PAIR *request,
704 VALUE_PAIR *check, VALUE_PAIR *check_pairs,
705 VALUE_PAIR **reply_pairs)
707 char filter[MAX_FILTER_STR_LEN];
708 char gr_filter[MAX_FILTER_STR_LEN];
710 LDAPMessage *result = NULL;
711 LDAPMessage *msg = NULL;
712 char basedn[MAX_FILTER_STR_LEN];
713 char *attrs[] = {"dn",NULL};
715 ldap_instance *inst = instance;
716 char *group_attrs[] = {inst->groupmemb_attr,NULL};
719 VALUE_PAIR *vp_user_dn;
720 VALUE_PAIR **request_pairs;
722 request_pairs = &req->config_items;
724 DEBUG("rlm_ldap: Entering ldap_groupcmp()");
726 if (check->strvalue == NULL || check->length == 0){
727 DEBUG("rlm_ldap::ldap_groupcmp: Illegal group name");
732 DEBUG("rlm_ldap::ldap_groupcmp: NULL request");
736 if (!radius_xlat(basedn, sizeof(basedn), inst->basedn, req, NULL)) {
737 DEBUG("rlm_ldap::ldap_groupcmp: unable to create basedn.");
741 while((vp_user_dn = pairfind(*request_pairs, PW_LDAP_USERDN)) == NULL){
742 char *user_dn = NULL;
744 if (!radius_xlat(filter, sizeof(filter), inst->filter,
745 req, ldap_escape_func)){
746 DEBUG("rlm_ldap::ldap_groupcmp: unable to create filter");
749 if ((conn_id = ldap_get_conn(inst->conns,&conn,inst)) == -1){
750 radlog(L_ERR, "rlm_ldap: All ldap connections are in use");
753 if ((res = perform_search(inst, conn, basedn, LDAP_SCOPE_SUBTREE,
754 filter, attrs, &result)) != RLM_MODULE_OK){
755 DEBUG("rlm_ldap::ldap_groupcmp: search failed");
756 ldap_release_conn(conn_id,inst->conns);
759 if ((msg = ldap_first_entry(conn->ld, result)) == NULL) {
760 DEBUG("rlm_ldap::ldap_groupcmp: ldap_first_entry() failed");
761 ldap_release_conn(conn_id,inst->conns);
762 ldap_msgfree(result);
765 if ((user_dn = ldap_get_dn(conn->ld, msg)) == NULL) {
766 DEBUG("rlm_ldap:ldap_groupcmp:: ldap_get_dn() failed");
767 ldap_release_conn(conn_id,inst->conns);
768 ldap_msgfree(result);
771 ldap_release_conn(conn_id,inst->conns);
773 * Adding new attribute containing DN for LDAP object associated with
776 pairadd(request_pairs, pairmake("Ldap-UserDn", user_dn,
778 ldap_memfree(user_dn);
779 ldap_msgfree(result);
782 if(!radius_xlat(gr_filter, sizeof(gr_filter),
783 inst->groupmemb_filt, req, NULL)) {
784 DEBUG("rlm_ldap::ldap_groupcmp: unable to create filter.");
788 if (strchr((char *)check->strvalue,',') != NULL) {
789 /* This looks like a DN */
790 snprintf(filter,sizeof(filter), "%s",gr_filter);
791 snprintf(basedn,sizeof(basedn), "%s",(char *)check->strvalue);
793 snprintf(filter,sizeof(filter), "(&(%s=%s)%s)",
794 inst->groupname_attr,
795 (char *)check->strvalue,gr_filter);
797 if ((conn_id = ldap_get_conn(inst->conns,&conn,inst)) == -1) {
798 radlog(L_ERR, "rlm_ldap: All ldap connections are in use");
802 if ((res = perform_search(inst, conn, basedn, LDAP_SCOPE_SUBTREE,
803 filter, attrs, &result)) == RLM_MODULE_OK) {
804 DEBUG("rlm_ldap::ldap_groupcmp: User found in group %s",
805 (char *)check->strvalue);
806 ldap_msgfree(result);
807 ldap_release_conn(conn_id,inst->conns);
811 ldap_release_conn(conn_id,inst->conns);
813 if (res != RLM_MODULE_NOTFOUND ) {
814 DEBUG("rlm_ldap::ldap_groupcmp: Search returned error");
818 if (inst->groupmemb_attr == NULL){
819 /* search returned NOTFOUND and searching for membership
820 * using user object attributes is not specified in config
823 DEBUG("rlm_ldap::ldap_groupcmp: Group %s not found or user is not a member.",(char *)check->strvalue);
827 snprintf(filter,sizeof(filter), "(objectclass=*)");
828 if ((conn_id = ldap_get_conn(inst->conns,&conn,inst)) == -1){
829 radlog(L_ERR, "rlm_ldap: Add ldap connections are in use");
832 if ((res = perform_search(inst, conn, vp_user_dn->strvalue,
833 LDAP_SCOPE_BASE, filter, group_attrs,
834 &result)) != RLM_MODULE_OK) {
835 DEBUG("rlm_ldap::ldap_groupcmp: Search returned error");
836 ldap_release_conn(conn_id, inst->conns);
840 if ((msg = ldap_first_entry(conn->ld, result)) == NULL) {
841 DEBUG("rlm_ldap::ldap_groupcmp: ldap_first_entry() failed");
842 ldap_release_conn(conn_id,inst->conns);
843 ldap_msgfree(result);
846 if ((vals = ldap_get_values(conn->ld, msg,
847 inst->groupmemb_attr)) != NULL) {
851 for (;i < ldap_count_values(vals);i++){
852 if (strchr(vals[i],',') != NULL){
853 /* This looks like a DN */
854 LDAPMessage *gr_result = NULL;
855 snprintf(filter,sizeof(filter), "(%s=%s)",
856 inst->groupname_attr,
857 (char *)check->strvalue);
858 if ((res = perform_search(inst, conn, vals[i],
859 LDAP_SCOPE_BASE, filter,
860 attrs, &gr_result)) != RLM_MODULE_OK){
861 if (res != RLM_MODULE_NOTFOUND) {
862 DEBUG("rlm_ldap::ldap_groupcmp: Search returned error");
863 ldap_value_free(vals);
864 ldap_msgfree(result);
865 ldap_release_conn(conn_id,inst->conns);
869 ldap_msgfree(gr_result);
874 if (strcmp(vals[i],(char *)check->strvalue) == 0){
880 ldap_value_free(vals);
881 ldap_msgfree(result);
883 DEBUG("rlm_ldap::groupcmp: Group %s not found or user not a member",
884 (char *)check->strvalue);
885 ldap_release_conn(conn_id,inst->conns);
889 DEBUG("rlm_ldap::ldap_groupcmp: ldap_get_values() failed");
890 ldap_msgfree(result);
891 ldap_release_conn(conn_id,inst->conns);
895 DEBUG("rlm_ldap::ldap_groupcmp: User found in group %s",(char *)check->strvalue);
896 ldap_release_conn(conn_id,inst->conns);
903 * Do an xlat on an LDAP URL
906 static int ldap_xlat(void *instance, REQUEST *request, char *fmt, char *out, int freespace,
907 RADIUS_ESCAPE_STRING func)
909 char url[MAX_FILTER_STR_LEN];
912 ldap_instance *inst = instance;
913 LDAPURLDesc *ldap_url;
914 LDAPMessage *result = NULL;
915 LDAPMessage *msg = NULL;
920 DEBUG("rlm_ldap: - ldap_xlat");
921 if (!radius_xlat(url, sizeof(url), fmt, request, func)) {
922 radlog (L_ERR, "rlm_ldap: Unable to create LDAP URL.\n");
925 if (!ldap_is_ldap_url(url)){
926 radlog (L_ERR, "rlm_ldap: String passed does not look like an LDAP URL.\n");
929 if (ldap_url_parse(url,&ldap_url)){
930 radlog (L_ERR, "rlm_ldap: LDAP URL parse failed.\n");
933 if (ldap_url->lud_attrs == NULL || ldap_url->lud_attrs[0] == NULL ||
934 ( ldap_url->lud_attrs[1] != NULL ||
935 ( ! strlen(ldap_url->lud_attrs[0]) ||
936 ! strcmp(ldap_url->lud_attrs[0],"*") ) ) ){
937 radlog (L_ERR, "rlm_ldap: Invalid Attribute(s) request.\n");
938 ldap_free_urldesc(ldap_url);
941 if (ldap_url->lud_host){
942 if (strncmp(inst->server,ldap_url->lud_host,
943 strlen(inst->server)) != 0 ||
944 ldap_url->lud_port != inst->port) {
945 DEBUG("rlm_ldap: Requested server/port is not known to this module instance.");
946 ldap_free_urldesc(ldap_url);
950 if ((conn_id = ldap_get_conn(inst->conns,&conn,inst)) == -1){
951 radlog(L_ERR, "rlm_ldap: All ldap connections are in use");
952 ldap_free_urldesc(ldap_url);
955 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){
956 if (res == RLM_MODULE_NOTFOUND){
957 DEBUG("rlm_ldap: Search returned not found");
958 ldap_free_urldesc(ldap_url);
959 ldap_release_conn(conn_id,inst->conns);
962 DEBUG("rlm_ldap: Search returned error");
963 ldap_free_urldesc(ldap_url);
964 ldap_release_conn(conn_id,inst->conns);
967 if ((msg = ldap_first_entry(conn->ld, result)) == NULL){
968 DEBUG("rlm_ldap: ldap_first_entry() failed");
969 ldap_msgfree(result);
970 ldap_free_urldesc(ldap_url);
971 ldap_release_conn(conn_id,inst->conns);
974 if ((vals = ldap_get_values(conn->ld, msg, ldap_url->lud_attrs[0])) != NULL) {
975 ret = strlen(vals[0]);
976 if (ret > freespace){
977 DEBUG("rlm_ldap: Insufficient string space");
978 ldap_free_urldesc(ldap_url);
979 ldap_value_free(vals);
980 ldap_msgfree(result);
981 ldap_release_conn(conn_id,inst->conns);
984 DEBUG("rlm_ldap: Adding attribute %s, value: %s",ldap_url->lud_attrs[0],vals[0]);
985 strncpy(out,vals[0],ret);
986 ldap_value_free(vals);
991 ldap_msgfree(result);
992 ldap_free_urldesc(ldap_url);
993 ldap_release_conn(conn_id,inst->conns);
995 DEBUG("rlm_ldap: - ldap_xlat end");
1001 /******************************************************************************
1003 * Function: rlm_ldap_authorize
1005 * Purpose: Check if user is authorized for remote access
1007 ******************************************************************************/
1009 ldap_authorize(void *instance, REQUEST * request)
1011 LDAPMessage *result = NULL;
1012 LDAPMessage *msg = NULL;
1013 LDAPMessage *def_msg = NULL;
1014 LDAPMessage *def_attr_msg = NULL;
1015 LDAPMessage *def_result = NULL;
1016 LDAPMessage *def_attr_result = NULL;
1017 ldap_instance *inst = instance;
1018 char *user_dn = NULL;
1019 char filter[MAX_FILTER_STR_LEN];
1020 char basedn[MAX_FILTER_STR_LEN];
1021 VALUE_PAIR *check_tmp;
1022 VALUE_PAIR *reply_tmp;
1024 VALUE_PAIR **check_pairs, **reply_pairs;
1026 VALUE_PAIR *module_fmsg_vp;
1027 VALUE_PAIR *user_profile;
1028 char module_fmsg[MAX_STRING_LEN];
1032 DEBUG("rlm_ldap: - authorize");
1034 if (!request->username){
1035 radlog(L_AUTH, "rlm_ldap: Attribute \"User-Name\" is required for authentication.\n");
1036 return RLM_MODULE_INVALID;
1039 check_pairs = &request->config_items;
1040 reply_pairs = &request->reply->vps;
1043 * Check for valid input, zero length names not permitted
1045 if (request->username->strvalue == 0) {
1046 radlog(L_ERR, "rlm_ldap: zero length username not permitted\n");
1047 return RLM_MODULE_INVALID;
1049 DEBUG("rlm_ldap: performing user authorization for %s",
1050 request->username->strvalue);
1052 if (!radius_xlat(filter, sizeof(filter), inst->filter,
1053 request, ldap_escape_func)) {
1054 radlog (L_ERR, "rlm_ldap: unable to create filter.\n");
1055 return RLM_MODULE_INVALID;
1058 if (!radius_xlat(basedn, sizeof(basedn), inst->basedn,
1060 radlog (L_ERR, "rlm_ldap: unable to create basedn.\n");
1061 return RLM_MODULE_INVALID;
1064 if ((conn_id = ldap_get_conn(inst->conns,&conn,inst)) == -1){
1065 radlog(L_ERR, "rlm_ldap: All ldap connections are in use");
1066 return RLM_MODULE_FAIL;
1068 if ((res = perform_search(instance, conn, basedn, LDAP_SCOPE_SUBTREE, filter, inst->atts, &result)) != RLM_MODULE_OK) {
1069 DEBUG("rlm_ldap: search failed");
1070 if (res == RLM_MODULE_NOTFOUND){
1071 snprintf(module_fmsg,sizeof(module_fmsg),"rlm_ldap: User not found");
1072 module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
1073 pairadd(&request->packet->vps, module_fmsg_vp);
1075 ldap_release_conn(conn_id,inst->conns);
1078 if ((msg = ldap_first_entry(conn->ld, result)) == NULL) {
1079 DEBUG("rlm_ldap: ldap_first_entry() failed");
1080 ldap_msgfree(result);
1081 ldap_release_conn(conn_id,inst->conns);
1082 return RLM_MODULE_FAIL;
1084 if ((user_dn = ldap_get_dn(conn->ld, msg)) == NULL) {
1085 DEBUG("rlm_ldap: ldap_get_dn() failed");
1086 ldap_msgfree(result);
1087 ldap_release_conn(conn_id,inst->conns);
1088 return RLM_MODULE_FAIL;
1091 * Adding new attribute containing DN for LDAP object associated with
1094 pairadd(check_pairs, pairmake("Ldap-UserDn", user_dn, T_OP_EQ));
1095 ldap_memfree(user_dn);
1098 /* Remote access is controled by attribute of the user object */
1099 if (inst->access_attr) {
1100 if ((vals = ldap_get_values(conn->ld, msg, inst->access_attr)) != NULL) {
1101 if (inst->default_allow){
1102 DEBUG("rlm_ldap: checking if remote access for %s is allowed by %s", request->username->strvalue, inst->access_attr);
1103 if (!strncmp(vals[0], "FALSE", 5)) {
1104 DEBUG("rlm_ldap: dialup access disabled");
1105 snprintf(module_fmsg,sizeof(module_fmsg),"rlm_ldap: Access Attribute denies access");
1106 module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
1107 pairadd(&request->packet->vps, module_fmsg_vp);
1108 ldap_msgfree(result);
1109 ldap_value_free(vals);
1110 ldap_release_conn(conn_id,inst->conns);
1111 return RLM_MODULE_USERLOCK;
1113 ldap_value_free(vals);
1116 DEBUG("rlm_ldap: %s attribute exists - access denied by default", inst->access_attr);
1117 snprintf(module_fmsg,sizeof(module_fmsg),"rlm_ldap: Access Attribute denies access");
1118 module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
1119 pairadd(&request->packet->vps, module_fmsg_vp);
1120 ldap_msgfree(result);
1121 ldap_value_free(vals);
1122 ldap_release_conn(conn_id,inst->conns);
1123 return RLM_MODULE_USERLOCK;
1126 if (inst->default_allow){
1127 DEBUG("rlm_ldap: no %s attribute - access denied by default", inst->access_attr);
1128 snprintf(module_fmsg,sizeof(module_fmsg),"rlm_ldap: Access Attribute denies access");
1129 module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
1130 pairadd(&request->packet->vps, module_fmsg_vp);
1131 ldap_msgfree(result);
1132 ldap_release_conn(conn_id,inst->conns);
1133 return RLM_MODULE_USERLOCK;
1139 * Check for the default profile entry. If it exists then add the
1140 * attributes it contains in the check and reply pairs
1143 user_profile = pairfind(request->config_items, PW_USER_PROFILE);
1144 if (inst->default_profile || user_profile){
1145 char *profile = inst->default_profile;
1147 strNcpy(filter,inst->base_filter,sizeof(filter));
1149 profile = user_profile->strvalue;
1150 if (profile && strlen(profile)){
1151 if ((res = perform_search(instance, conn,
1152 profile, LDAP_SCOPE_BASE,
1153 filter, inst->atts, &def_result)) == RLM_MODULE_OK){
1154 if ((def_msg = ldap_first_entry(conn->ld,def_result))){
1155 if ((check_tmp = ldap_pairget(conn->ld,def_msg,inst->check_item_map,check_pairs,1))) {
1157 pairxlatmove(request, check_pairs, &check_tmp);
1158 pairfree(&check_tmp);
1161 pairadd(check_pairs,check_tmp);
1163 if ((reply_tmp = ldap_pairget(conn->ld,def_msg,inst->reply_item_map,reply_pairs,0))) {
1165 pairxlatmove(request, reply_pairs, &reply_tmp);
1166 pairfree(&reply_tmp);
1169 pairadd(reply_pairs,reply_tmp);
1172 ldap_msgfree(def_result);
1174 DEBUG("rlm_ldap: default_profile/user-profile search failed");
1179 * Check for the profile attribute. If it exists, we assume that it
1180 * contains the DN of an entry containg a profile for the user. That
1181 * way we can have different general profiles for various user groups
1182 * (students,faculty,staff etc)
1185 if (inst->profile_attr){
1186 if ((vals = ldap_get_values(conn->ld, msg, inst->profile_attr)) != NULL) {
1188 strNcpy(filter,inst->base_filter,sizeof(filter));
1189 while(vals[i] != NULL && strlen(vals[i])){
1190 if ((res = perform_search(instance, conn,
1191 vals[i], LDAP_SCOPE_BASE,
1192 filter, inst->atts, &def_attr_result)) == RLM_MODULE_OK){
1193 if ((def_attr_msg = ldap_first_entry(conn->ld,def_attr_result))){
1194 if ((check_tmp = ldap_pairget(conn->ld,def_attr_msg,inst->check_item_map,check_pairs,1))) {
1196 pairxlatmove(request, check_pairs, &check_tmp);
1197 pairfree(&check_tmp);
1200 pairadd(check_pairs,check_tmp);
1202 if ((reply_tmp = ldap_pairget(conn->ld,def_attr_msg,inst->reply_item_map,reply_pairs,0))) {
1204 pairxlatmove(request, reply_pairs, &reply_tmp);
1205 pairfree(&reply_tmp);
1208 pairadd(reply_pairs,reply_tmp);
1211 ldap_msgfree(def_attr_result);
1213 DEBUG("rlm_ldap: profile_attribute search failed");
1216 ldap_value_free(vals);
1219 if (inst->passwd_attr && strlen(inst->passwd_attr)){
1220 #ifdef NOVELL_UNIVERSAL_PASSWORD
1221 if(strcasecmp(inst->passwd_attr,"nspmPassword")!= 0){
1223 VALUE_PAIR *passwd_item;
1225 if ((passwd_item = pairfind(request->config_items, PW_PASSWORD)) == NULL){
1227 char *passwd_val = NULL;
1230 if ((passwd_vals = ldap_get_values(conn->ld,msg,inst->passwd_attr)) != NULL){
1232 while(passwd_vals[i] != NULL){
1233 if (strlen(passwd_vals[i])){
1234 passwd_val = passwd_vals[i];
1236 if (inst->passwd_hdr && strlen(inst->passwd_hdr)){
1237 passwd_val = strstr(passwd_val,inst->passwd_hdr);
1238 if (passwd_val != NULL)
1239 passwd_val += strlen(inst->passwd_hdr);
1241 DEBUG("rlm_ldap: Password header not found in password %s for user %s", passwd_vals[0],request->username->strvalue);
1244 if ((passwd_item = paircreate(PW_PASSWORD,PW_TYPE_STRING)) == NULL){
1245 radlog(L_ERR|L_CONS, "no memory");
1246 ldap_value_free(passwd_vals);
1247 ldap_msgfree(result);
1248 ldap_release_conn(conn_id,inst->conns);
1249 return RLM_MODULE_FAIL;
1251 passwd_len = strlen(passwd_val);
1252 strncpy(passwd_item->strvalue,passwd_val,MAX_STRING_LEN - 1);
1253 passwd_item->length = (passwd_len > (MAX_STRING_LEN - 1)) ? (MAX_STRING_LEN - 1) : passwd_len;
1254 pairadd(&request->config_items,passwd_item);
1255 DEBUG("rlm_ldap: Added password %s in check items",passwd_item->strvalue);
1260 ldap_value_free(passwd_vals);
1263 #ifdef NOVELL_UNIVERSAL_PASSWORD
1267 * Read Universal Password from eDirectory
1269 VALUE_PAIR *passwd_item;
1270 VALUE_PAIR *vp_user_dn;
1272 char *universal_password = NULL;
1273 int universal_password_len = UNIVERSAL_PASS_LEN;
1274 char *passwd_val = NULL;
1278 if ((passwd_item = pairfind(request->config_items, PW_PASSWORD)) == NULL){
1280 universal_password = rad_malloc(universal_password_len);
1281 memset(universal_password, 0, universal_password_len);
1283 vp_user_dn = pairfind(request->config_items,PW_LDAP_USERDN);
1284 res = nmasldap_get_password(conn->ld,vp_user_dn->strvalue,&universal_password_len,universal_password);
1287 passwd_val = universal_password;
1289 if (inst->passwd_hdr && strlen(inst->passwd_hdr)){
1290 passwd_val = strstr(passwd_val,inst->passwd_hdr);
1292 if (passwd_val != NULL)
1293 passwd_val += strlen((char*)inst->passwd_hdr);
1295 DEBUG("rlm_ldap: Password header not found in password %s for user %s ",passwd_val,request->username->strvalue);
1299 if ((passwd_item = paircreate(PW_PASSWORD,PW_TYPE_STRING)) == NULL){
1300 radlog(L_ERR, "rlm_ldap: Could not allocate memory. Aborting.");
1301 ldap_msgfree(result);
1302 ldap_release_conn(conn_id,inst->conns);
1303 memset(universal_password, 0, universal_password_len);
1304 free(universal_password);
1305 return RLM_MODULE_FAIL;
1308 passwd_len = strlen(passwd_val);
1309 strncpy(passwd_item->strvalue,passwd_val,MAX_STRING_LEN - 1);
1310 passwd_item->length = (passwd_len > (MAX_STRING_LEN - 1)) ? (MAX_STRING_LEN - 1) : passwd_len;
1311 pairadd(&request->config_items,passwd_item);
1312 DEBUG("rlm_ldap: Added the eDirectory password in check items");
1316 DEBUG("rlm_ldap: Error reading Universal Password.Return Code = %d",res);
1319 memset(universal_password, 0, universal_password_len);
1320 free(universal_password);
1328 DEBUG("rlm_ldap: looking for check items in directory...");
1330 if ((check_tmp = ldap_pairget(conn->ld, msg, inst->check_item_map,check_pairs,1)) != NULL) {
1332 pairxlatmove(request, check_pairs, &check_tmp);
1333 pairfree(&check_tmp);
1336 pairadd(check_pairs,check_tmp);
1340 DEBUG("rlm_ldap: looking for reply items in directory...");
1343 if ((reply_tmp = ldap_pairget(conn->ld, msg, inst->reply_item_map,reply_pairs,0)) != NULL) {
1345 pairxlatmove(request, reply_pairs, &reply_tmp);
1346 pairfree(&reply_tmp);
1349 pairadd(reply_pairs,reply_tmp);
1352 if (inst->do_comp && paircmp(request,request->packet->vps,*check_pairs,reply_pairs) != 0){
1353 DEBUG("rlm_ldap: Pairs do not match. Rejecting user.");
1354 snprintf(module_fmsg,sizeof(module_fmsg),"rlm_ldap: Pairs do not match");
1355 module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
1356 pairadd(&request->packet->vps, module_fmsg_vp);
1357 ldap_msgfree(result);
1358 ldap_release_conn(conn_id,inst->conns);
1360 return RLM_MODULE_REJECT;
1364 * Module should default to LDAP authentication if no Auth-Type
1367 if ((pairfind(*check_pairs, PW_AUTH_TYPE) == NULL) &&
1368 request->password &&
1369 (request->password->attribute == PW_USER_PASSWORD))
1370 pairadd(check_pairs, pairmake("Auth-Type", "LDAP", T_OP_EQ));
1373 DEBUG("rlm_ldap: user %s authorized to use remote access",
1374 request->username->strvalue);
1375 ldap_msgfree(result);
1376 ldap_release_conn(conn_id,inst->conns);
1378 return RLM_MODULE_OK;
1381 /*****************************************************************************
1383 * Function: rlm_ldap_authenticate
1385 * Purpose: Check the user's password against ldap database
1387 *****************************************************************************/
1389 ldap_authenticate(void *instance, REQUEST * request)
1392 LDAPMessage *result, *msg;
1393 ldap_instance *inst = instance;
1394 char *user_dn, *attrs[] = {"uid", NULL};
1395 char filter[MAX_FILTER_STR_LEN];
1396 char basedn[MAX_FILTER_STR_LEN];
1398 VALUE_PAIR *vp_user_dn;
1399 VALUE_PAIR *module_fmsg_vp;
1400 char module_fmsg[MAX_STRING_LEN];
1404 DEBUG("rlm_ldap: - authenticate");
1407 * Ensure that we're being passed a plain-text password, and not
1411 if (!request->username) {
1412 radlog(L_AUTH, "rlm_ldap: Attribute \"User-Name\" is required for authentication.\n");
1413 return RLM_MODULE_INVALID;
1416 if (!request->password){
1417 radlog(L_AUTH, "rlm_ldap: Attribute \"User-Password\" is required for authentication.");
1418 DEBUG2(" You seem to have set \"Auth-Type := LDAP\" somewhere.");
1419 DEBUG2(" THAT CONFIGURATION IS WRONG. DELETE IT.");
1420 DEBUG2(" YOU ARE PREVENTING THE SERVER FROM WORKING PROPERLY.");
1421 return RLM_MODULE_INVALID;
1424 if(request->password->attribute != PW_PASSWORD) {
1425 radlog(L_AUTH, "rlm_ldap: Attribute \"User-Password\" is required for authentication. Cannot use \"%s\".", request->password->name);
1426 return RLM_MODULE_INVALID;
1429 if (request->password->length == 0) {
1430 snprintf(module_fmsg,sizeof(module_fmsg),"rlm_ldap: empty password supplied");
1431 module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
1432 pairadd(&request->packet->vps, module_fmsg_vp);
1433 return RLM_MODULE_INVALID;
1437 * Check that we don't have any failed connections. If we do there's no real need
1438 * of runing. Also give it another chance if we have a lot of failed connections.
1440 if (inst->failed_conns > MAX_FAILED_CONNS_END)
1441 inst->failed_conns = 0;
1442 if (inst->failed_conns > MAX_FAILED_CONNS_START){
1443 inst->failed_conns++;
1444 return RLM_MODULE_FAIL;
1448 DEBUG("rlm_ldap: login attempt by \"%s\" with password \"%s\"",
1449 request->username->strvalue, request->password->strvalue);
1451 while ((vp_user_dn = pairfind(request->config_items,
1452 PW_LDAP_USERDN)) == NULL) {
1453 if (!radius_xlat(filter, sizeof(filter), inst->filter,
1455 radlog (L_ERR, "rlm_ldap: unable to create filter.\n");
1456 return RLM_MODULE_INVALID;
1459 if (!radius_xlat(basedn, sizeof(basedn), inst->basedn,
1461 radlog (L_ERR, "rlm_ldap: unable to create basedn.\n");
1462 return RLM_MODULE_INVALID;
1465 if ((conn_id = ldap_get_conn(inst->conns,&conn,inst)) == -1){
1466 radlog(L_ERR, "rlm_ldap: All ldap connections are in use");
1467 return RLM_MODULE_FAIL;
1469 if ((res = perform_search(instance, conn, basedn, LDAP_SCOPE_SUBTREE, filter, attrs, &result)) != RLM_MODULE_OK) {
1470 if (res == RLM_MODULE_NOTFOUND){
1471 snprintf(module_fmsg,sizeof(module_fmsg),"rlm_ldap: User not found");
1472 module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
1473 pairadd(&request->packet->vps, module_fmsg_vp);
1475 ldap_release_conn(conn_id,inst->conns);
1478 if ((msg = ldap_first_entry(conn->ld, result)) == NULL) {
1479 ldap_msgfree(result);
1480 ldap_release_conn(conn_id,inst->conns);
1481 return RLM_MODULE_FAIL;
1483 if ((user_dn = ldap_get_dn(conn->ld, msg)) == NULL) {
1484 DEBUG("rlm_ldap: ldap_get_dn() failed");
1485 ldap_msgfree(result);
1486 ldap_release_conn(conn_id,inst->conns);
1487 return RLM_MODULE_FAIL;
1489 ldap_release_conn(conn_id,inst->conns);
1490 pairadd(&request->config_items, pairmake("Ldap-UserDn", user_dn, T_OP_EQ));
1491 ldap_memfree(user_dn);
1492 ldap_msgfree(result);
1495 user_dn = vp_user_dn->strvalue;
1497 DEBUG("rlm_ldap: user DN: %s", user_dn);
1499 ld_user = ldap_connect(instance, user_dn, request->password->strvalue,
1501 if (ld_user == NULL){
1502 if (res == RLM_MODULE_REJECT){
1503 inst->failed_conns = 0;
1504 snprintf(module_fmsg,sizeof(module_fmsg),"rlm_ldap: Bind as user failed");
1505 module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
1506 pairadd(&request->packet->vps, module_fmsg_vp);
1508 if (res == RLM_MODULE_FAIL){
1509 DEBUG("rlm_ldap: ldap_connect() failed");
1510 inst->failed_conns++;
1515 DEBUG("rlm_ldap: user %s authenticated succesfully",
1516 request->username->strvalue);
1517 ldap_unbind_s(ld_user);
1518 inst->failed_conns = 0;
1520 return RLM_MODULE_OK;
1524 ldap_connect(void *instance, const char *dn, const char *password, int auth, int *result)
1526 ldap_instance *inst = instance;
1528 int msgid, rc, ldap_version;
1533 #ifdef HAVE_LDAP_INITIALIZE
1534 DEBUG("rlm_ldap: (re)connect to %s, authentication %d", inst->server, auth);
1535 if (ldap_initialize(&ld, inst->server) != LDAP_SUCCESS) {
1536 radlog(L_ERR, "rlm_ldap: ldap_initialize() failed");
1537 *result = RLM_MODULE_FAIL;
1542 DEBUG("rlm_ldap: (re)connect to %s:%d, authentication %d", inst->server, inst->port, auth);
1543 if ((ld = ldap_init(inst->server, inst->port)) == NULL) {
1544 radlog(L_ERR, "rlm_ldap: ldap_init() failed");
1545 *result = RLM_MODULE_FAIL;
1549 if (ldap_set_option(ld, LDAP_OPT_NETWORK_TIMEOUT,
1550 (void *) &(inst->net_timeout)) != LDAP_OPT_SUCCESS) {
1551 radlog(L_ERR, "rlm_ldap: Could not set LDAP_OPT_NETWORK_TIMEOUT %ld.%ld", inst->net_timeout.tv_sec, inst->net_timeout.tv_usec);
1554 if (ldap_set_option(ld, LDAP_OPT_TIMELIMIT, (void *) &(inst->timelimit)) != LDAP_OPT_SUCCESS) {
1555 radlog(L_ERR, "rlm_ldap: Could not set LDAP_OPT_TIMELIMIT %d", inst->timelimit);
1558 if (inst->ldap_debug && ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &(inst->ldap_debug)) != LDAP_OPT_SUCCESS) {
1559 radlog(L_ERR, "rlm_ldap: Could not set LDAP_OPT_DEBUG_LEVEL %d", inst->ldap_debug);
1562 ldap_version = LDAP_VERSION3;
1563 if (ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &ldap_version) != LDAP_OPT_SUCCESS) {
1564 radlog(L_ERR, "rlm_ldap: Could not set LDAP version to V3");
1567 #ifdef HAVE_LDAP_START_TLS
1568 if(inst->tls_mode) {
1569 DEBUG("rlm_ldap: setting TLS mode to %d", inst->tls_mode);
1570 if(ldap_set_option(ld, LDAP_OPT_X_TLS,
1571 (void *) &(inst->tls_mode)) != LDAP_OPT_SUCCESS) {
1572 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
1573 radlog(L_ERR, "rlm_ldap: could not set LDAP_OPT_X_TLS option %s", ldap_err2string(ldap_errno));
1577 if(inst->tls_cacertfile != NULL) {
1578 DEBUG("rlm_ldap: setting TLS CACert File to %s", inst->tls_cacertfile);
1580 if ( ldap_set_option( NULL, LDAP_OPT_X_TLS_CACERTFILE,
1581 (void *) inst->tls_cacertfile )
1582 != LDAP_OPT_SUCCESS) {
1583 radlog(L_ERR, "rlm_ldap: could not set "
1584 "LDAP_OPT_X_TLS_CACERTFILE option to %s", inst->tls_cacertfile);
1588 if(inst->tls_cacertdir != NULL) {
1589 DEBUG("rlm_ldap: setting TLS CACert File to %s", inst->tls_cacertdir);
1591 if ( ldap_set_option( NULL, LDAP_OPT_X_TLS_CACERTDIR,
1592 (void *) inst->tls_cacertdir )
1593 != LDAP_OPT_SUCCESS) {
1594 radlog(L_ERR, "rlm_ldap: could not set "
1595 "LDAP_OPT_X_TLS_CACERTDIR option to %s", inst->tls_cacertdir);
1599 if( strcmp( TLS_DEFAULT_VERIFY, inst->tls_require_cert ) != 0 ) {
1600 DEBUG("rlm_ldap: setting TLS Require Cert to %s",
1601 inst->tls_require_cert);
1605 #ifdef HAVE_INT_TLS_CONFIG
1606 if ( ldap_int_tls_config( NULL, LDAP_OPT_X_TLS_REQUIRE_CERT,
1607 (inst->tls_require_cert) )
1608 != LDAP_OPT_SUCCESS) {
1609 radlog(L_ERR, "rlm_ldap: could not set "
1610 "LDAP_OPT_X_TLS_REQUIRE_CERT option to %s",
1611 inst->tls_require_cert);
1615 if(inst->tls_certfile != NULL) {
1616 DEBUG("rlm_ldap: setting TLS Cert File to %s", inst->tls_certfile);
1618 if ( ldap_set_option( NULL, LDAP_OPT_X_TLS_CERTFILE,
1619 (void *) inst->tls_certfile )
1620 != LDAP_OPT_SUCCESS) {
1621 radlog(L_ERR, "rlm_ldap: could not set "
1622 "LDAP_OPT_X_TLS_CERTFILE option to %s",
1623 inst->tls_certfile);
1627 if (inst->tls_keyfile != NULL) {
1628 DEBUG("rlm_ldap: setting TLS Key File to %s",
1631 if ( ldap_set_option( NULL, LDAP_OPT_X_TLS_KEYFILE,
1632 (void *) inst->tls_keyfile )
1633 != LDAP_OPT_SUCCESS) {
1634 radlog(L_ERR, "rlm_ldap: could not set "
1635 "LDAP_OPT_X_TLS_KEYFILE option to %s",
1640 if (inst->tls_randfile != NULL) {
1641 DEBUG("rlm_ldap: setting TLS Key File to %s",
1642 inst->tls_randfile);
1644 if (ldap_set_option( NULL, LDAP_OPT_X_TLS_RANDOM_FILE,
1645 (void *) inst->tls_randfile)
1646 != LDAP_OPT_SUCCESS) {
1647 radlog(L_ERR, "rlm_ldap: could not set "
1648 "LDAP_OPT_X_TLS_RANDOM_FILE option to %s",
1649 inst->tls_randfile);
1653 if (inst->start_tls) {
1654 DEBUG("rlm_ldap: starting TLS");
1655 rc = ldap_start_tls_s(ld, NULL, NULL);
1656 if (rc != LDAP_SUCCESS) {
1657 DEBUG("rlm_ldap: ldap_start_tls_s()");
1658 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER,
1660 radlog(L_ERR, "rlm_ldap: could not start TLS %s",
1661 ldap_err2string(ldap_errno));
1662 *result = RLM_MODULE_FAIL;
1667 #endif /* HAVE_LDAP_START_TLS */
1670 DEBUG("rlm_ldap: bind as %s/%s to %s",
1671 dn, password, inst->server);
1673 DEBUG("rlm_ldap: bind as %s/%s to %s:%d",
1674 dn, password, inst->server, inst->port);
1677 msgid = ldap_bind(ld, dn, password,LDAP_AUTH_SIMPLE);
1679 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
1681 radlog(L_ERR, "rlm_ldap: %s bind to %s failed: %s",
1682 dn, inst->server, ldap_err2string(ldap_errno));
1684 radlog(L_ERR, "rlm_ldap: %s bind to %s:%d failed: %s",
1685 dn, inst->server, inst->port,
1686 ldap_err2string(ldap_errno));
1688 *result = RLM_MODULE_FAIL;
1692 DEBUG("rlm_ldap: waiting for bind result ...");
1694 rc = ldap_result(ld, msgid, 1, &(inst->timeout), &res);
1697 DEBUG("rlm_ldap: ldap_result()");
1698 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
1700 radlog(L_ERR, "rlm_ldap: %s bind to %s failed: %s",
1701 dn, inst->server, (rc == 0) ? "timeout" : ldap_err2string(ldap_errno));
1703 radlog(L_ERR, "rlm_ldap: %s bind to %s:%d failed: %s",
1704 dn, inst->server, inst->port,
1705 (rc == 0) ? "timeout" : ldap_err2string(ldap_errno));
1707 *result = RLM_MODULE_FAIL;
1712 ldap_errno = ldap_result2error(ld, res, 1);
1713 switch (ldap_errno) {
1715 DEBUG("rlm_ldap: Bind was successful");
1716 *result = RLM_MODULE_OK;
1719 case LDAP_INVALID_CREDENTIALS:
1721 DEBUG("rlm_ldap: Bind failed with invalid credentials");
1722 *result = RLM_MODULE_REJECT;
1724 radlog(L_ERR, "rlm_ldap: LDAP login failed: check identity, password settings in ldap section of radiusd.conf");
1725 *result = RLM_MODULE_FAIL;
1731 radlog(L_ERR,"rlm_ldap: %s bind to %s failed %s",
1732 dn, inst->server, ldap_err2string(ldap_errno));
1734 radlog(L_ERR,"rlm_ldap: %s bind to %s:%d failed %s",
1735 dn, inst->server, inst->port,
1736 ldap_err2string(ldap_errno));
1738 *result = RLM_MODULE_FAIL;
1741 if (*result != RLM_MODULE_OK) {
1748 /*****************************************************************************
1750 * Detach from the LDAP server and cleanup internal state.
1752 *****************************************************************************/
1754 ldap_detach(void *instance)
1756 ldap_instance *inst = instance;
1757 TLDAP_RADIUS *pair, *nextpair;
1760 free((char *) inst->server);
1762 free((char *) inst->login);
1764 free((char *) inst->password);
1766 free((char *) inst->basedn);
1767 if (inst->dictionary_mapping)
1768 free(inst->dictionary_mapping);
1770 free((char *) inst->filter);
1771 if (inst->base_filter)
1772 free((char *) inst->base_filter);
1773 if (inst->passwd_hdr)
1774 free((char *) inst->passwd_hdr);
1775 if (inst->passwd_attr)
1776 free((char *) inst->passwd_attr);
1777 if (inst->groupname_attr)
1778 free((char *) inst->groupname_attr);
1779 if (inst->groupmemb_filt)
1780 free((char *) inst->groupmemb_filt);
1781 if (inst->groupmemb_attr)
1782 free((char *) inst->groupmemb_attr);
1783 if (inst->access_attr)
1784 free((char *) inst->access_attr);
1785 if (inst->profile_attr)
1786 free((char *) inst->profile_attr);
1791 for(i = 0;i < inst->num_conns; i++){
1792 if (inst->conns[i].ld){
1793 ldap_unbind_s(inst->conns[i].ld);
1795 pthread_mutex_destroy(&inst->conns[i].mutex);
1800 pair = inst->check_item_map;
1802 while (pair != NULL) {
1803 nextpair = pair->next;
1805 free(pair->radius_attr);
1810 pair = inst->reply_item_map;
1812 while (pair != NULL) {
1813 nextpair = pair->next;
1815 free(pair->radius_attr);
1823 paircompare_unregister(PW_LDAP_GROUP, ldap_groupcmp);
1824 xlat_unregister(inst->xlat_name,ldap_xlat);
1825 free(inst->xlat_name);
1834 fieldcpy(char *string, char **uptr)
1839 while (*ptr == ' ' || *ptr == '\t') {
1844 while (*ptr != '"' && *ptr != '\0' && *ptr != '\n') {
1854 while (*ptr != ' ' && *ptr != '\t' && *ptr != '\0' && *ptr != '\n' &&
1855 *ptr != '=' && *ptr != ',') {
1863 /*****************************************************************************
1864 * Get RADIUS attributes from LDAP object
1865 * ( according to draft-adoba-radius-05.txt
1866 * <http://www.ietf.org/internet-drafts/draft-adoba-radius-05.txt> )
1868 *****************************************************************************/
1871 ldap_pairget(LDAP * ld, LDAPMessage * entry,
1872 TLDAP_RADIUS * item_map, VALUE_PAIR **pairs,char is_check)
1878 TLDAP_RADIUS *element;
1880 int is_generic_attribute;
1882 VALUE_PAIR *pairlist = NULL;
1883 VALUE_PAIR *newpair = NULL;
1885 /* check if there is a mapping from this LDAP attribute to a RADIUS attribute */
1886 for (element = item_map; element != NULL; element = element->next) {
1887 if ((vals = ldap_get_values(ld,entry,element->attr)) != NULL) {
1888 /* check whether this is a one-to-one-mapped ldap attribute or a generic
1889 attribute and set flag accordingly */
1891 if (strcasecmp(element->radius_attr, GENERIC_ATTRIBUTE_ID)==0)
1892 is_generic_attribute = 1;
1894 is_generic_attribute = 0;
1896 /* find out how many values there are for the attribute and extract all of them */
1898 vals_count = ldap_count_values(vals);
1900 for (vals_idx = 0; vals_idx < vals_count; vals_idx++) {
1901 ptr = vals[vals_idx];
1903 if (is_generic_attribute) {
1904 /* this is a generic attribute */
1905 LRAD_TOKEN dummy; /* makes pairread happy */
1907 /* not sure if using pairread here is ok ... */
1908 if ( (newpair = pairread(&ptr, &dummy)) != NULL) {
1909 DEBUG("rlm_ldap: extracted attribute %s from generic item %s",
1910 newpair->name, vals[vals_idx]);
1911 pairadd(&pairlist, newpair);
1913 radlog(L_ERR, "rlm_ldap: parsing %s failed: %s",
1914 element->attr, vals[vals_idx]);
1917 /* this is a one-to-one-mapped attribute */
1918 token = gettoken(&ptr, value, sizeof(value) - 1);
1919 if (token < T_EQSTART || token > T_EQEND) {
1921 token = T_OP_CMP_EQ;
1926 if (element->operator != T_OP_INVALID) {
1927 token = element->operator;
1930 gettoken(&ptr, value, sizeof(value) - 1);
1932 if (value[0] == 0) {
1933 DEBUG("rlm_ldap: Attribute %s has no value", element->attr);
1936 DEBUG("rlm_ldap: Adding %s as %s, value %s & op=%d", element->attr, element->radius_attr, value, token);
1937 if ((newpair = pairmake(element->radius_attr, value, token)) == NULL)
1940 pairdelete(pairs,newpair->attribute);
1942 pairadd(&pairlist, newpair);
1945 ldap_value_free(vals);
1952 /* globally exported name */
1953 module_t rlm_ldap = {
1955 RLM_TYPE_THREAD_SAFE, /* type: reserved */
1956 NULL, /* initialization */
1957 ldap_instantiate, /* instantiation */
1959 ldap_authenticate, /* authentication */
1960 ldap_authorize, /* authorization */
1961 NULL, /* preaccounting */
1962 NULL, /* accounting */
1963 NULL, /* checksimul */
1964 NULL, /* pre-proxy */
1965 NULL, /* post-proxy */
1966 NULL /* post-auth */
1968 ldap_detach, /* detach */