2 * rlm_ldap.c LDAP authorization and authentication module.
5 * This module is based on LDAP patch to Cistron radiusd by James Golovich
6 * <james@wwnet.net>, which in turn was based mostly on a Mysql+Cistron patch
7 * from <oyarzun@wilmington.net>
9 * 17 Jan 2000, Adrian Pavlykevych <pam@polynet.lviv.ua>
10 * - OpenLDAP SDK porting, basic TLS support, LDAP authorization,
11 * fault tolerance with multiple LDAP server support
12 * 24 May 2000, Adrian Pavlykevych <pam@polynet.lviv.ua>
13 * - Converting to new configuration file format, futher improvements
14 * in fault tolerance, threaded operation
15 * 12 Dec 2000, Adrian Pavlykevych <pam@polynet.lviv.ua>
16 * - Added preliminary support for multiple instances
17 * - moved all instance configuration into dynamicly allocated structure
18 * - Removed connection maintenance thread and all attempts for multihreading
19 * the module itself. OpenLDAP SDK is not thread safe when used with shared
21 * - Added configuration option for defining LDAP attribute of user object,
22 * which controls remote access.
23 * 16 Feb 2001, Hannu Laurila <hannu.laurila@japo.fi>
24 * - LDAP<->RADIUS attribute mappings are now read from a file
25 * - Support for generic RADIUS check and reply attribute.
26 * Jun 2001, Kostas Kalevras <kkalev@noc.ntua.gr>
27 * - Fix: check and reply attributes from LDAP _replace_ existing ones
28 * - Added "default_profile" directive, which points to radiusProfile
29 * object, which contains default values for RADIUS users
30 * - Added "profile_attribute" directive, which specifies user object
31 * attribute pointing to radiusProfile object.
32 * Nov 2001, Kostas Kalevras <kkalev@noc.ntua.gr>
33 * - Added support for adding the user password to the check. Based on
34 * the password_header directive rlm_ldap will strip the
35 * password header if needed. This will make support for CHAP much easier.
36 * - Added module messages when we reject a user.
37 * - Added ldap_groupcmp to allow searching for user group membership.
38 * - Added ldap_xlat to allow ldap urls in xlat strings. Something like:
39 * %{ldap:ldap:///dc=company,dc=com?cn?sub?uid=user}
40 * Nov 2001, Gordon Tetlow <gordont@gnf.org>
41 * - Do an xlat on the access_group attribute.
42 * Dec 2001, Kostas Kalevras <kkalev@noc.ntua.gr>
43 * - Added ldap caching for the default/regular profiles and group entries.
44 * - Fixed a memory leak in ldap_xlat.
45 * - Removed dict_attrbyname from ldap_pairget. They are not needed.
46 * - Moved the radius_xlat's for filter and basedn in ldap_authenticate() to
48 * - Made the module thread safe. We create a connection pool and each thread
49 * will call ldap_get_conn to lock one of the ldap connections and release with
50 * a call to ldap_release_conn when it has finished.
51 * - Request only the user attributes that interest us (radius attributes,regular
52 * profile,user password and access attribute).
53 * Mar 2002, Kostas Kalevras <kkalev@noc.ntua.gr>
54 * - Fixed a bug where the ldap server will kill the idle connections from the ldap
55 * connection pool. We now check if ldap_search returns LDAP_SERVER_DOWN and try to
56 * reconnect if it does. Bug noted by Dan Perik <dan_perik-work@ntm.org.pg>
57 * May 2002, Kostas Kalevras <kkalev@noc.ntua.gr>
58 * - Instead of the Group attribute we now have the Ldap-Group attribute, to avoid
59 * collisions with other modules
60 * - If perform_search fails check the ld != NULL before using it. Based on a bug report
61 * by John <jhogenmiller@pennswoods.net>
62 * Jun 2002, Kostas Kalevras <kkalev@noc.ntua.gr>
63 * - Add the ability to do a paircmp on the check items. Add a compare_check_items boolean
64 * configuration directive which defaults to no. If it is set then we will do a compare
65 * - Add another configuration directive. access_attr_used_for_allow. If it is set to yes
66 * then the access_attr will be used to allow user access. If it is set to no then it will
67 * be used to deny user access.
68 * - Remember to free inst->atts in ldap_detach()
69 * - Add a forgotten ldap_free_urldesc in ldap_xlat()
70 * - Add a variable locked in the LDAP_CONN structure. We use this to avoid deadlocks. The mutex
71 * we are using is of type fast and can deadlock if the same thread tries to relock it. That
72 * could happen in case of calls to xlat.
73 * - When ldap_search returns NO_SUCH_OBJECT don't return fail but notfound
74 * Jul 2002, Kostas Kalevras <kkalev@noc.ntua.gr>
75 * - Fix the logic when we get an LDAP_SERVER_DOWN or we have conn->ld == NULL in perform_search
76 * - Try to minimize the penalty of having the ldap server go down. The comments before
77 * MAX_FAILED_CONNS_* definitions should explain things.
78 * - Check for a number of error codes from ldap_search and log corresponding error messages
79 * We should only reconnect when that can help things.
80 * - In ldap_groupcmp instead of first searching for the group object and then checking user
81 * group membership combine them in one ldap search operation. That should make group
82 * membership checks a lot faster.
83 * - Remember to do ldap_release_conn and ldap_msgfree when we do paircmp and the result is reject
84 * Aug 2002, Kostas Kalevras <kkalev@noc.ntua.gr>
85 * - Add support for group membership attribute inside the user entry in ldap_groupcmp. The attribute
86 * can either contain the name or the DN of the group. Added the groupmembership_attribute
87 * configuration directive
88 * - Move the ldap_{get,release}_conn in ldap_groupcmp so that we hold a connection for the minimum time.
89 * - Now that ldap_groupcmp is complete we really don't need access_group. Removed it.
90 * - Remember to free groupmembership_attribute in ldap_detach
91 * - Don't delete existing generic attributes in ldap_pairget when adding new ones. Since generic attributes
92 * have operators we don't need to try to be cleaver.
93 * Sep 2002, Kostas Kalevras <kkalev@noc.ntua.gr>
94 * - Fix a crash in ldap_pairget when the attribute value is larger than the buffer size
95 * Bug report by Stefan Radovanovici <sra@rtsffm.com>
97 static const char rcsid[] = "$Id$";
101 #include <sys/types.h>
102 #include <sys/socket.h>
103 #include <sys/time.h>
104 #include <netinet/in.h>
121 #include "libradius.h"
123 #include "conffile.h"
125 #include "rad_assert.h"
128 #define MAX_AUTH_QUERY_LEN 256
129 #define MAX_GROUP_STR_LEN 1024
133 * These are used in case ldap_search returns LDAP_SERVER_DOWN
134 * In that case we do conn->failed_conns++ and then check it:
135 * If conn->failed_conns <= MAX_FAILED_CONNS_START then we try
137 * conn->failed_conns is also checked on entrance in perform_search:
138 * If conn->failed_conns > MAX_FAILED_CONNS_START then we don't
139 * try to do anything and we just do conn->failed_conns++ and
140 * return RLM_MODULE_FAIL
141 * if conn->failed_conns >= MAX_FAILED_CONNS_END then we give it
142 * another chance and we set it to MAX_FAILED_CONNS_RESTART and
146 * We are assuming that the majority of the LDAP_SERVER_DOWN cases
147 * will either be an ldap connection timeout or a temporary ldap
149 * As a result we make a few attempts to reconnect hoping that the problem
150 * will soon go away. If it does not go away then we just return
151 * RLM_MODULE_FAIL on entrance in perform_search until conn->failed_conns
152 * gets to MAX_FAILED_CONNS_END. After that we give it one more chance by
153 * going back to MAX_FAILED_CONNS_RESTART
157 #define MAX_FAILED_CONNS_END 20
158 #define MAX_FAILED_CONNS_RESTART 4
159 #define MAX_FAILED_CONNS_START 5
161 /* linked list of mappings between RADIUS attributes and LDAP attributes */
162 struct TLDAP_RADIUS {
165 struct TLDAP_RADIUS* next;
167 typedef struct TLDAP_RADIUS TLDAP_RADIUS;
169 typedef struct ldap_conn {
174 pthread_mutex_t mutex;
177 #define MAX_SERVER_LINE 1024
183 struct timeval net_timeout;
184 struct timeval timeout;
197 char *default_profile;
202 char *dictionary_mapping;
203 char *groupname_attr;
204 char *groupmemb_filt;
205 char *groupmemb_attr;
207 TLDAP_RADIUS *check_item_map;
208 TLDAP_RADIUS *reply_item_map;
210 int ldap_debug; /* Debug flag for LDAP SDK */
211 char *xlat_name; /* name used to xlat */
214 static CONF_PARSER module_config[] = {
215 {"server", PW_TYPE_STRING_PTR, offsetof(ldap_instance,server), NULL, NULL},
216 {"port", PW_TYPE_INTEGER, offsetof(ldap_instance,port), NULL, "389"},
217 /* wait forever on network activity */
218 {"net_timeout", PW_TYPE_INTEGER, offsetof(ldap_instance,net_timeout.tv_sec), NULL, "10"},
219 /* wait forever for search results */
220 {"timeout", PW_TYPE_INTEGER, offsetof(ldap_instance,timeout.tv_sec), NULL, "20"},
221 /* allow server unlimited time for search (server-side limit) */
222 {"timelimit", PW_TYPE_INTEGER, offsetof(ldap_instance,timelimit), NULL, "20"},
223 {"ldap_cache_timeout", PW_TYPE_INTEGER, offsetof(ldap_instance,cache_timeout), NULL, "0"},
224 {"ldap_cache_size", PW_TYPE_INTEGER, offsetof(ldap_instance,cache_size), NULL, "0"},
225 {"identity", PW_TYPE_STRING_PTR, offsetof(ldap_instance,login), NULL, ""},
226 {"start_tls", PW_TYPE_BOOLEAN, offsetof(ldap_instance,start_tls), NULL, "no"},
227 {"password", PW_TYPE_STRING_PTR, offsetof(ldap_instance,password), NULL, ""},
228 {"basedn", PW_TYPE_STRING_PTR, offsetof(ldap_instance,basedn), NULL, NULL},
229 {"filter", PW_TYPE_STRING_PTR, offsetof(ldap_instance,filter), NULL, "(uid=%u)"},
230 {"default_profile", PW_TYPE_STRING_PTR, offsetof(ldap_instance,default_profile), NULL, NULL},
231 {"profile_attribute", PW_TYPE_STRING_PTR, offsetof(ldap_instance,profile_attr), NULL, NULL},
232 {"password_header", PW_TYPE_STRING_PTR, offsetof(ldap_instance,passwd_hdr), NULL, NULL},
233 {"password_attribute", PW_TYPE_STRING_PTR, offsetof(ldap_instance,passwd_attr), NULL, NULL},
234 /* LDAP attribute name that controls remote access */
235 {"access_attr", PW_TYPE_STRING_PTR, offsetof(ldap_instance,access_attr), NULL, NULL},
236 /* file with mapping between LDAP and RADIUS attributes */
237 {"groupname_attribute", PW_TYPE_STRING_PTR, offsetof(ldap_instance,groupname_attr), NULL, "cn"},
238 {"groupmembership_filter", PW_TYPE_STRING_PTR, offsetof(ldap_instance,groupmemb_filt), NULL, "(|(&(objectClass=GroupOfNames)(member=%{Ldap-UserDn}))(&(objectClass=GroupOfUniqueNames)(uniquemember=%{Ldap-UserDn})))"},
239 {"groupmembership_attribute", PW_TYPE_STRING_PTR, offsetof(ldap_instance,groupmemb_attr), NULL, NULL},
240 {"dictionary_mapping", PW_TYPE_STRING_PTR, offsetof(ldap_instance,dictionary_mapping), NULL, "${confdir}/ldap.attrmap"},
241 {"ldap_debug", PW_TYPE_INTEGER, offsetof(ldap_instance,ldap_debug), NULL, "0x0000"},
242 {"ldap_connections_number", PW_TYPE_INTEGER, offsetof(ldap_instance,num_conns), NULL, "5"},
243 {"compare_check_items", PW_TYPE_BOOLEAN, offsetof(ldap_instance,do_comp), NULL, "no"},
244 {"access_attr_used_for_allow", PW_TYPE_BOOLEAN, offsetof(ldap_instance,default_allow), NULL, "yes"},
246 {NULL, -1, 0, NULL, NULL}
249 #define ld_valid ld_options.ldo_valid
250 #define LDAP_VALID_SESSION 0x2
251 #define LDAP_VALID(ld) ( (ld)->ld_valid == LDAP_VALID_SESSION )
254 static void fieldcpy(char *, char **);
256 static VALUE_PAIR *ldap_pairget(LDAP *, LDAPMessage *, TLDAP_RADIUS *,VALUE_PAIR **);
257 static int ldap_groupcmp(void *, REQUEST *, VALUE_PAIR *, VALUE_PAIR *, VALUE_PAIR *, VALUE_PAIR **);
258 static int ldap_xlat(void *,REQUEST *, char *, char *,int, RADIUS_ESCAPE_STRING);
259 static LDAP *ldap_connect(void *instance, const char *, const char *, int, int *);
260 static int read_mappings(ldap_instance* inst);
262 static inline int ldap_get_conn(LDAP_CONN *conns,LDAP_CONN **ret,void *instance)
264 ldap_instance *inst = instance;
267 for(;i<inst->num_conns;i++){
268 if (conns[i].locked == 0 && pthread_mutex_trylock(&(conns[i].mutex)) == 0){
271 DEBUG("ldap_get_conn: Got Id: %d",i);
279 static inline void ldap_release_conn(int i, LDAP_CONN *conns)
281 DEBUG("ldap_release_conn: Release Id: %d",i);
283 pthread_mutex_unlock(&(conns[i].mutex));
286 /*************************************************************************
288 * Function: rlm_ldap_instantiate
290 * Purpose: Uses section of radiusd config file passed as parameter
291 * to create an instance of the module.
293 *************************************************************************/
295 ldap_instantiate(CONF_SECTION * conf, void **instance)
300 int reply_map_num = 0;
301 int check_map_num = 0;
302 int att_map[3] = {0,0,0};
306 inst = rad_malloc(sizeof *inst);
308 if (cf_section_parse(conf, inst, module_config) < 0) {
313 if (inst->server == NULL) {
314 radlog(L_ERR, "rlm_ldap: missing 'server' directive.");
319 inst->timeout.tv_usec = 0;
320 inst->net_timeout.tv_usec = 0;
321 /* workaround for servers which support LDAPS but not START TLS */
322 if(inst->port == LDAPS_PORT)
323 inst->tls_mode = LDAP_OPT_X_TLS_HARD;
325 inst->tls_mode = LDAP_OPT_X_TLS_TRY;
326 inst->reply_item_map = NULL;
327 inst->check_item_map = NULL;
330 paircompare_register(PW_LDAP_GROUP, PW_USER_NAME, ldap_groupcmp, inst);
331 DEBUG("conns: %p",inst->conns);
333 xlat_name = cf_section_name2(conf);
334 if (xlat_name == NULL) {
335 xlat_name = cf_section_name1(conf);
336 rad_assert(xlat_name != NULL); /* or all hell breaks loose */
338 inst->xlat_name = strdup(xlat_name);
339 xlat_register(xlat_name,ldap_xlat,inst);
341 if (inst->num_conns <= 0){
342 radlog(L_ERR, "rlm_ldap: Invalid ldap connections number passed.");
346 inst->conns = (LDAP_CONN *)malloc(sizeof(LDAP_CONN)*inst->num_conns);
347 if (inst->conns == NULL){
348 radlog(L_ERR, "rlm_ldap: Could not allocate memory. Aborting.");
352 for(;i<inst->num_conns;i++){
353 inst->conns[i].bound = 0;
354 inst->conns[i].locked = 0;
355 inst->conns[i].failed_conns = 0;
356 inst->conns[i].ld = NULL;
357 pthread_mutex_init(&inst->conns[i].mutex, NULL);
360 if (read_mappings(inst) != 0) {
361 radlog(L_ERR, "rlm_ldap: Reading dictionary mappings from file %s failed",
362 inst->dictionary_mapping);
363 radlog(L_ERR, "rlm_ldap: Proceeding with no mappings");
366 pair = inst->check_item_map;
371 check_map_num = (atts_num - 1);
372 pair = inst->reply_item_map;
377 reply_map_num = (atts_num - 1);
378 if (inst->profile_attr)
380 if (inst->passwd_attr)
382 if (inst->access_attr)
384 inst->atts = (char **)malloc(sizeof(char *)*(atts_num + 1));
385 if (inst->atts == NULL){
386 radlog(L_ERR, "rlm_ldap: Could not allocate memory. Aborting.");
390 pair = inst->check_item_map;
391 for(i=0;i<atts_num;i++){
392 if (i <= check_map_num ){
393 inst->atts[i] = pair->attr;
394 if (i == check_map_num)
395 pair = inst->reply_item_map;
399 else if (i <= reply_map_num){
400 inst->atts[i] = pair->attr;
404 if (inst->profile_attr && !att_map[0]){
405 inst->atts[i] = inst->profile_attr;
408 else if (inst->passwd_attr && !att_map[1]){
409 inst->atts[i] = inst->passwd_attr;
412 else if (inst->access_attr && !att_map[2]){
413 inst->atts[i] = inst->access_attr;
418 inst->atts[atts_num] = NULL;
420 DEBUG("conns: %p",inst->conns);
430 * read_mappings(...) reads a ldap<->radius mappings file to inst->reply_item_map and inst->check_item_map
433 #define MAX_LINE_LEN 160
434 #define GENERIC_ATTRIBUTE_ID "$GENERIC$"
437 read_mappings(ldap_instance* inst)
441 /* all buffers are of MAX_LINE_LEN so we can use sscanf without being afraid of buffer overflows */
442 char buf[MAX_LINE_LEN], itemType[MAX_LINE_LEN], radiusAttribute[MAX_LINE_LEN], ldapAttribute[MAX_LINE_LEN];
445 /* open the mappings file for reading */
447 filename = inst->dictionary_mapping;
448 DEBUG("rlm_ldap: reading ldap<->radius mappings from file %s", filename);
449 mapfile = fopen(filename, "r");
451 if (mapfile == NULL) {
452 radlog(L_ERR, "rlm_ldap: Opening file %s failed", filename);
453 return -1; /* error */
456 /* read file line by line. Note that if line length exceed MAX_LINE_LEN, line numbers will be mixed up */
460 while (fgets(buf, sizeof buf, mapfile)!=NULL) {
468 ptr = strchr(buf, '#');
472 if (buf[0] == 0) continue;
474 /* extract tokens from the string */
475 token_count = sscanf(buf, "%s %s %s", itemType, radiusAttribute, ldapAttribute);
477 if (token_count <= 0) /* no tokens */
480 if (token_count != 3) {
481 radlog(L_ERR, "rlm_ldap: Skipping %s line %i: %s", filename, linenumber, buf);
482 radlog(L_ERR, "rlm_ldap: Expected 3 tokens "
483 "(Item type, RADIUS Attribute and LDAP Attribute) but found only %i", token_count);
487 /* create new TLDAP_RADIUS list node */
488 pair = rad_malloc(sizeof(TLDAP_RADIUS));
490 pair->attr = strdup(ldapAttribute);
491 pair->radius_attr = strdup(radiusAttribute);
493 if ( (pair->attr == NULL) || (pair->radius_attr == NULL) ) {
494 radlog(L_ERR, "rlm_ldap: Out of memory");
495 if (pair->attr) free(pair->attr);
496 if (pair->radius_attr) free(pair->radius_attr);
502 /* push node to correct list */
503 if (strcasecmp(itemType, "checkItem") == 0) {
504 pair->next = inst->check_item_map;
505 inst->check_item_map = pair;
506 } else if (strcasecmp(itemType, "replyItem") == 0) {
507 pair->next = inst->reply_item_map;
508 inst->reply_item_map = pair;
510 radlog(L_ERR, "rlm_ldap: file %s: skipping line %i: unknown itemType %s",
511 filename, linenumber, itemType);
513 free(pair->radius_attr);
518 DEBUG("rlm_ldap: LDAP %s mapped to RADIUS %s",
519 pair->attr, pair->radius_attr);
524 return 0; /* success */
528 perform_search(void *instance, LDAP_CONN *conn, char *search_basedn, int scope, char *filter,
529 char **attrs, LDAPMessage ** result)
531 int res = RLM_MODULE_OK;
533 ldap_instance *inst = instance;
534 int search_retry = 0;
539 radlog(L_ERR, "rlm_ldap: NULL connection handle passed");
540 return RLM_MODULE_FAIL;
542 if (conn->failed_conns > MAX_FAILED_CONNS_START){
543 conn->failed_conns++;
544 if (conn->failed_conns >= MAX_FAILED_CONNS_END){
545 conn->failed_conns = MAX_FAILED_CONNS_RESTART;
550 if (!conn->bound || conn->ld == NULL) {
551 DEBUG2("rlm_ldap: attempting LDAP reconnection");
553 DEBUG2("rlm_ldap: closing existing LDAP connection");
554 if (inst->cache_timeout >0)
555 ldap_destroy_cache(conn->ld);
556 ldap_unbind_s(conn->ld);
558 if ((conn->ld = ldap_connect(instance, inst->login, inst->password, 0, &res)) == NULL) {
559 radlog(L_ERR, "rlm_ldap: (re)connection attempt failed");
560 if (search_retry == 0)
561 conn->failed_conns++;
562 return (RLM_MODULE_FAIL);
565 conn->failed_conns = 0;
567 DEBUG2("rlm_ldap: performing search in %s, with filter %s", search_basedn ? search_basedn : "(null)" , filter);
568 switch (ldap_search_st(conn->ld, search_basedn, scope, filter, attrs, 0, &(inst->timeout), result)) {
570 case LDAP_NO_SUCH_OBJECT:
572 case LDAP_SERVER_DOWN:
573 radlog(L_ERR, "rlm_ldap: ldap_search() failed: LDAP connection lost.");
574 conn->failed_conns++;
575 if (search_retry == 0){
576 if (conn->failed_conns <= MAX_FAILED_CONNS_START){
577 radlog(L_INFO, "rlm_ldap: Attempting reconnect");
580 ldap_msgfree(*result);
584 ldap_msgfree(*result);
585 return RLM_MODULE_FAIL;
586 case LDAP_INSUFFICIENT_ACCESS:
587 radlog(L_ERR, "rlm_ldap: ldap_search() failed: Insufficient access. Check the identity and password configuration directives.");
588 ldap_msgfree(*result);
589 return RLM_MODULE_FAIL;
591 radlog(L_ERR, "rlm_ldap: ldap_search() failed: Timed out while waiting for server to respond. Please increase the timeout.");
592 ldap_msgfree(*result);
593 return RLM_MODULE_FAIL;
594 case LDAP_TIMELIMIT_EXCEEDED:
596 case LDAP_UNAVAILABLE:
597 /* We don't need to reconnect in these cases so we don't set conn->bound */
598 ldap_get_option(conn->ld, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
599 radlog(L_ERR, "rlm_ldap: ldap_search() failed: %s", ldap_err2string(ldap_errno));
600 ldap_msgfree(*result);
601 return (RLM_MODULE_FAIL);
603 ldap_get_option(conn->ld, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
604 radlog(L_ERR, "rlm_ldap: ldap_search() failed: %s", ldap_err2string(ldap_errno));
606 ldap_msgfree(*result);
607 return (RLM_MODULE_FAIL);
610 if ((ldap_count_entries(conn->ld, *result)) != 1) {
611 DEBUG("rlm_ldap: object not found or got ambiguous search result");
612 res = RLM_MODULE_NOTFOUND;
613 ldap_msgfree(*result);
620 * ldap_groupcmp(). Implement the Ldap-Group == "group" filter
623 static int ldap_groupcmp(void *instance, REQUEST *req, VALUE_PAIR *request, VALUE_PAIR *check,
624 VALUE_PAIR *check_pairs, VALUE_PAIR **reply_pairs)
626 char filter[MAX_GROUP_STR_LEN];
627 char gr_filter[MAX_AUTH_QUERY_LEN];
629 LDAPMessage *result = NULL;
630 LDAPMessage *msg = NULL;
632 char *attrs[] = {"dn",NULL};
633 ldap_instance *inst = instance;
637 check_pairs = check_pairs;
638 reply_pairs = reply_pairs;
640 DEBUG("rlm_ldap: Entering ldap_groupcmp()");
642 if (check->strvalue == NULL || check->length == 0){
643 DEBUG("rlm_ldap::ldap_groupcmp: Illegal group name");
648 DEBUG("rlm_ldap::ldap_groupcmp: NULL request");
652 if (!radius_xlat(basedn, sizeof(basedn), inst->basedn, req, NULL)) {
653 DEBUG("rlm_ldap::ldap_groupcmp: unable to create basedn.");
657 if ((pairfind(req->packet->vps, PW_LDAP_USERDN)) == NULL){
658 char *user_dn = NULL;
660 if (!radius_xlat(filter, MAX_AUTH_QUERY_LEN, inst->filter, req, NULL)) {
661 DEBUG("rlm_ldap::ldap_groupcmp: unable to create filter");
664 if ((conn_id = ldap_get_conn(inst->conns,&conn,inst)) == -1){
665 radlog(L_ERR, "rlm_ldap: All ldap connections are in use");
668 if ((res = perform_search(inst, conn, basedn, LDAP_SCOPE_SUBTREE, filter, attrs, &result)) != RLM_MODULE_OK) {
669 DEBUG("rlm_ldap::ldap_groupcmp: search failed");
670 ldap_release_conn(conn_id,inst->conns);
673 if ((msg = ldap_first_entry(conn->ld, result)) == NULL) {
674 DEBUG("rlm_ldap::ldap_groupcmp: ldap_first_entry() failed");
675 ldap_release_conn(conn_id,inst->conns);
676 ldap_msgfree(result);
679 if ((user_dn = ldap_get_dn(conn->ld, msg)) == NULL) {
680 DEBUG("rlm_ldap:ldap_groupcmp:: ldap_get_dn() failed");
681 ldap_release_conn(conn_id,inst->conns);
682 ldap_msgfree(result);
685 ldap_release_conn(conn_id,inst->conns);
687 * Adding new attribute containing DN for LDAP object associated with
690 pairadd(&req->packet->vps, pairmake("Ldap-UserDn", user_dn, T_OP_EQ));
691 ldap_memfree(user_dn);
692 ldap_msgfree(result);
695 if(!radius_xlat(gr_filter, MAX_GROUP_STR_LEN, inst->groupmemb_filt, req, NULL)){
696 DEBUG("rlm_ldap::ldap_groupcmp: unable to create filter.");
700 snprintf(filter,MAX_GROUP_STR_LEN - 1, "(&(%s=%s)%s)",inst->groupname_attr,(char *)check->strvalue,gr_filter);
702 if ((conn_id = ldap_get_conn(inst->conns,&conn,inst)) == -1){
703 radlog(L_ERR, "rlm_ldap: All ldap connections are in use");
707 if ((res = perform_search(inst, conn, basedn, LDAP_SCOPE_SUBTREE, filter, attrs, &result)) != RLM_MODULE_OK){
708 if (res != RLM_MODULE_NOTFOUND){
709 DEBUG("rlm_ldap::ldap_groupcmp: Search returned error");
710 ldap_release_conn(conn_id,inst->conns);
714 if (res != RLM_MODULE_NOTFOUND)
715 ldap_msgfree(result);
716 ldap_release_conn(conn_id,inst->conns);
718 if (res == RLM_MODULE_NOTFOUND){
719 if (inst->groupmemb_attr == NULL){
720 DEBUG("rlm_ldap::ldap_groupcmp: Group %s not found or user is not a member.",(char *)check->strvalue);
725 char *group_attrs[] = {inst->groupmemb_attr,NULL};
727 user_dn = pairfind(req->packet->vps, PW_LDAP_USERDN);
728 if (user_dn != NULL){
731 snprintf(filter,MAX_GROUP_STR_LEN - 1, "(objectclass=*)");
732 if ((conn_id = ldap_get_conn(inst->conns,&conn,inst)) == -1){
733 radlog(L_ERR, "rlm_ldap: Add ldap connections are in use");
736 if ((res = perform_search(inst, conn, user_dn->strvalue, LDAP_SCOPE_BASE, filter, group_attrs, &result)) != RLM_MODULE_OK){
737 DEBUG("rlm_ldap::ldap_groupcmp: Search returned error");
738 ldap_release_conn(conn_id, inst->conns);
742 if ((msg = ldap_first_entry(conn->ld, result)) == NULL) {
743 DEBUG("rlm_ldap::ldap_groupcmp: ldap_first_entry() failed");
744 ldap_release_conn(conn_id,inst->conns);
745 ldap_msgfree(result);
748 if ((vals = ldap_get_values(conn->ld, msg, inst->groupmemb_attr)) != NULL) {
752 for (;i < ldap_count_values(vals);i++){
753 if (strchr(vals[i],',') != NULL){ /* This looks like a DN */
754 LDAPMessage *gr_result = NULL;
756 snprintf(filter,MAX_GROUP_STR_LEN - 1, "(%s=%s)",inst->groupname_attr,(char *)check->strvalue);
757 if ((res = perform_search(inst, conn, vals[i], LDAP_SCOPE_BASE, filter, attrs, &gr_result)) != RLM_MODULE_OK){
758 if (res != RLM_MODULE_NOTFOUND){
759 DEBUG("rlm_ldap::ldap_groupcmp: Search returned error");
760 ldap_msgfree(result);
761 ldap_value_free(vals);
762 ldap_release_conn(conn_id, inst->conns);
767 ldap_msgfree(gr_result);
773 if (strcmp(vals[i],(char *)check->strvalue) == 0){
779 ldap_value_free(vals);
780 ldap_msgfree(result);
782 DEBUG("rlm_ldap::groupcmp: Group %s not found or user not a member",
783 (char *)check->strvalue);
784 ldap_release_conn(conn_id,inst->conns);
789 DEBUG("rlm_ldap::ldap_groupcmp: ldap_get_values() failed");
790 ldap_release_conn(conn_id,inst->conns);
791 ldap_msgfree(result);
799 DEBUG("rlm_ldap::ldap_groupcmp: User found in group %s",(char *)check->strvalue);
800 ldap_release_conn(conn_id,inst->conns);
807 * Do an xlat on an LDAP URL
810 static int ldap_xlat(void *instance, REQUEST *request, char *fmt, char *out, int freespace,
811 RADIUS_ESCAPE_STRING func)
813 char url[MAX_STRING_LEN];
816 ldap_instance *inst = instance;
817 LDAPURLDesc *ldap_url;
818 LDAPMessage *result = NULL;
819 LDAPMessage *msg = NULL;
824 DEBUG("rlm_ldap: - ldap_xlat");
825 if (!radius_xlat(url, sizeof(url), fmt, request, func)) {
826 radlog (L_ERR, "rlm_ldap: Unable to create LDAP URL.\n");
829 if (!ldap_is_ldap_url(url)){
830 radlog (L_ERR, "rlm_ldap: String passed does not look like an LDAP URL.\n");
833 if (ldap_url_parse(url,&ldap_url)){
834 radlog (L_ERR, "rlm_ldap: LDAP URL parse failed.\n");
837 if (ldap_url->lud_attrs == NULL || ldap_url->lud_attrs[0] == NULL || \
838 ( ldap_url->lud_attrs[1] != NULL || ( ! strlen(ldap_url->lud_attrs[0]) || \
839 ! strcmp(ldap_url->lud_attrs[0],"*") ) ) ){
840 radlog (L_ERR, "rlm_ldap: Invalid Attribute(s) request.\n");
841 ldap_free_urldesc(ldap_url);
844 if (ldap_url->lud_host){
845 if (strncmp(inst->server,ldap_url->lud_host,strlen(inst->server)) != 0 || \
846 ldap_url->lud_port != inst->port){
847 DEBUG("rlm_ldap: Requested server/port is not known to this module instance.");
848 ldap_free_urldesc(ldap_url);
852 if ((conn_id = ldap_get_conn(inst->conns,&conn,inst)) == -1){
853 radlog(L_ERR, "rlm_ldap: All ldap connections are in use");
854 ldap_free_urldesc(ldap_url);
857 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){
858 if (res == RLM_MODULE_NOTFOUND){
859 DEBUG("rlm_ldap: Search returned not found");
860 ldap_free_urldesc(ldap_url);
861 ldap_release_conn(conn_id,inst->conns);
864 DEBUG("rlm_ldap: Search returned error");
865 ldap_free_urldesc(ldap_url);
866 ldap_release_conn(conn_id,inst->conns);
869 if ((msg = ldap_first_entry(conn->ld, result)) == NULL){
870 DEBUG("rlm_ldap: ldap_first_entry() failed");
871 ldap_msgfree(result);
872 ldap_free_urldesc(ldap_url);
873 ldap_release_conn(conn_id,inst->conns);
876 if ((vals = ldap_get_values(conn->ld, msg, ldap_url->lud_attrs[0])) != NULL) {
877 ret = strlen(vals[0]);
878 if (ret > freespace){
879 DEBUG("rlm_ldap: Insufficient string space");
880 ldap_free_urldesc(ldap_url);
881 ldap_value_free(vals);
882 ldap_msgfree(result);
883 ldap_release_conn(conn_id,inst->conns);
886 DEBUG("rlm_ldap: Adding attribute %s, value: %s",ldap_url->lud_attrs[0],vals[0]);
887 strncpy(out,vals[0],ret);
888 ldap_value_free(vals);
893 ldap_msgfree(result);
894 ldap_free_urldesc(ldap_url);
895 ldap_release_conn(conn_id,inst->conns);
897 DEBUG("rlm_ldap: - ldap_xlat end");
903 /******************************************************************************
905 * Function: rlm_ldap_authorize
907 * Purpose: Check if user is authorized for remote access
909 ******************************************************************************/
911 ldap_authorize(void *instance, REQUEST * request)
913 LDAPMessage *result = NULL;
914 LDAPMessage *msg = NULL;
915 LDAPMessage *def_msg = NULL;
916 LDAPMessage *def_attr_msg = NULL;
917 LDAPMessage *def_result = NULL;
918 LDAPMessage *def_attr_result = NULL;
919 ldap_instance *inst = instance;
920 char *user_dn = NULL;
921 char filter[MAX_AUTH_QUERY_LEN];
923 VALUE_PAIR *check_tmp;
924 VALUE_PAIR *reply_tmp;
926 VALUE_PAIR **check_pairs, **reply_pairs;
928 VALUE_PAIR *module_fmsg_vp;
929 VALUE_PAIR *user_profile;
930 char module_fmsg[MAX_STRING_LEN];
934 DEBUG("rlm_ldap: - authorize");
936 if (!request->username){
937 radlog(L_AUTH, "rlm_ldap: Attribute \"User-Name\" is required for authentication.\n");
938 return RLM_MODULE_INVALID;
941 check_pairs = &request->config_items;
942 reply_pairs = &request->reply->vps;
945 * Check for valid input, zero length names not permitted
947 if (request->username->strvalue == 0) {
948 radlog(L_ERR, "rlm_ldap: zero length username not permitted\n");
949 return RLM_MODULE_INVALID;
951 DEBUG("rlm_ldap: performing user authorization for %s",
952 request->username->strvalue);
954 if (!radius_xlat(filter, sizeof(filter), inst->filter,
956 radlog (L_ERR, "rlm_ldap: unable to create filter.\n");
957 return RLM_MODULE_INVALID;
960 if (!radius_xlat(basedn, sizeof(basedn), inst->basedn,
962 radlog (L_ERR, "rlm_ldap: unable to create basedn.\n");
963 return RLM_MODULE_INVALID;
966 if ((conn_id = ldap_get_conn(inst->conns,&conn,inst)) == -1){
967 radlog(L_ERR, "rlm_ldap: All ldap connections are in use");
968 return RLM_MODULE_FAIL;
970 if ((res = perform_search(instance, conn, basedn, LDAP_SCOPE_SUBTREE, filter, inst->atts, &result)) != RLM_MODULE_OK) {
971 DEBUG("rlm_ldap: search failed");
972 if (res == RLM_MODULE_NOTFOUND){
973 snprintf(module_fmsg,sizeof(module_fmsg),"rlm_ldap: User not found");
974 module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
975 pairadd(&request->packet->vps, module_fmsg_vp);
977 ldap_release_conn(conn_id,inst->conns);
980 if ((msg = ldap_first_entry(conn->ld, result)) == NULL) {
981 DEBUG("rlm_ldap: ldap_first_entry() failed");
982 ldap_msgfree(result);
983 ldap_release_conn(conn_id,inst->conns);
984 return RLM_MODULE_FAIL;
986 if ((user_dn = ldap_get_dn(conn->ld, msg)) == NULL) {
987 DEBUG("rlm_ldap: ldap_get_dn() failed");
988 ldap_msgfree(result);
989 ldap_release_conn(conn_id,inst->conns);
990 return RLM_MODULE_FAIL;
993 * Adding new attribute containing DN for LDAP object associated with
996 pairadd(&request->packet->vps, pairmake("Ldap-UserDn", user_dn, T_OP_EQ));
997 ldap_memfree(user_dn);
1000 /* Remote access is controled by attribute of the user object */
1001 if (inst->access_attr) {
1002 if ((vals = ldap_get_values(conn->ld, msg, inst->access_attr)) != NULL) {
1003 if (inst->default_allow){
1004 DEBUG("rlm_ldap: checking if remote access for %s is allowed by %s", request->username->strvalue, inst->access_attr);
1005 if (!strncmp(vals[0], "FALSE", 5)) {
1006 DEBUG("rlm_ldap: dialup access disabled");
1007 snprintf(module_fmsg,sizeof(module_fmsg),"rlm_ldap: Access Attribute denies access");
1008 module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
1009 pairadd(&request->packet->vps, module_fmsg_vp);
1010 ldap_msgfree(result);
1011 ldap_value_free(vals);
1012 ldap_release_conn(conn_id,inst->conns);
1013 return RLM_MODULE_USERLOCK;
1015 ldap_value_free(vals);
1018 DEBUG("rlm_ldap: %s attribute exists - access denied by default", inst->access_attr);
1019 snprintf(module_fmsg,sizeof(module_fmsg),"rlm_ldap: Access Attribute denies access");
1020 module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
1021 pairadd(&request->packet->vps, module_fmsg_vp);
1022 ldap_msgfree(result);
1023 ldap_value_free(vals);
1024 ldap_release_conn(conn_id,inst->conns);
1025 return RLM_MODULE_USERLOCK;
1028 if (inst->default_allow){
1029 DEBUG("rlm_ldap: no %s attribute - access denied by default", inst->access_attr);
1030 snprintf(module_fmsg,sizeof(module_fmsg),"rlm_ldap: Access Attribute denies access");
1031 module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
1032 pairadd(&request->packet->vps, module_fmsg_vp);
1033 ldap_msgfree(result);
1034 ldap_release_conn(conn_id,inst->conns);
1035 return RLM_MODULE_USERLOCK;
1039 if (inst->cache_timeout >0)
1040 ldap_enable_cache(conn->ld, inst->cache_timeout, inst->cache_size);
1043 * Check for the default profile entry. If it exists then add the
1044 * attributes it contains in the check and reply pairs
1047 user_profile = pairfind(request->config_items, PW_USER_PROFILE);
1048 if (inst->default_profile || user_profile){
1049 char *profile = inst->default_profile;
1051 strncpy(filter,"(objectclass=radiusprofile)",MAX_AUTH_QUERY_LEN);
1053 profile = user_profile->strvalue;
1054 if (profile && strlen(profile)){
1055 if ((res = perform_search(instance, conn,
1056 profile, LDAP_SCOPE_BASE,
1057 filter, inst->atts, &def_result)) == RLM_MODULE_OK){
1058 if ((def_msg = ldap_first_entry(conn->ld,def_result))){
1059 if ((check_tmp = ldap_pairget(conn->ld,def_msg,inst->check_item_map,check_pairs)))
1060 pairadd(check_pairs,check_tmp);
1061 if ((reply_tmp = ldap_pairget(conn->ld,def_msg,inst->reply_item_map,reply_pairs)))
1062 pairadd(reply_pairs,reply_tmp);
1064 ldap_msgfree(def_result);
1066 DEBUG("rlm_ldap: default_profile/user-profile search failed");
1071 * Check for the profile attribute. If it exists, we assume that it
1072 * contains the DN of an entry containg a profile for the user. That
1073 * way we can have different general profiles for various user groups
1074 * (students,faculty,staff etc)
1077 if (inst->profile_attr){
1078 if ((vals = ldap_get_values(conn->ld, msg, inst->profile_attr)) != NULL && strlen(vals[0])) {
1079 strncpy(filter,"(objectclass=radiusprofile)",MAX_AUTH_QUERY_LEN);
1080 if (inst->cache_timeout >0)
1081 ldap_enable_cache(conn->ld, inst->cache_timeout, inst->cache_size);
1082 if ((res = perform_search(instance, conn,
1083 vals[0], LDAP_SCOPE_BASE,
1084 filter, inst->atts, &def_attr_result)) == RLM_MODULE_OK){
1085 if ((def_attr_msg = ldap_first_entry(conn->ld,def_attr_result))){
1086 if ((check_tmp = ldap_pairget(conn->ld,def_attr_msg,inst->check_item_map,check_pairs)))
1087 pairadd(check_pairs,check_tmp);
1088 if ((reply_tmp = ldap_pairget(conn->ld,def_attr_msg,inst->reply_item_map,reply_pairs)))
1089 pairadd(reply_pairs,reply_tmp);
1091 ldap_msgfree(def_attr_result);
1093 DEBUG("rlm_ldap: profile_attribute search failed");
1094 ldap_value_free(vals);
1097 if (inst->cache_timeout >0)
1098 ldap_disable_cache(conn->ld);
1099 if (inst->passwd_attr && strlen(inst->passwd_attr)){
1100 VALUE_PAIR *passwd_item;
1102 if ((passwd_item = pairfind(request->config_items, PW_PASSWORD)) == NULL){
1104 char *passwd_val = NULL;
1107 if ((passwd_vals = ldap_get_values(conn->ld,msg,inst->passwd_attr)) != NULL){
1109 while(passwd_vals[i] != NULL){
1110 if (strlen(passwd_vals[i])){
1111 passwd_val = passwd_vals[i];
1113 if (inst->passwd_hdr && strlen(inst->passwd_hdr)){
1114 passwd_val = strstr(passwd_val,inst->passwd_hdr);
1115 if (passwd_val != NULL)
1116 passwd_val += strlen(inst->passwd_hdr);
1118 DEBUG("rlm_ldap: Password header not found in password %s for user %s", passwd_vals[0],request->username->strvalue);
1121 if ((passwd_item = paircreate(PW_PASSWORD,PW_TYPE_STRING)) == NULL){
1122 radlog(L_ERR|L_CONS, "no memory");
1123 ldap_value_free(passwd_vals);
1124 ldap_msgfree(result);
1125 ldap_release_conn(conn_id,inst->conns);
1126 return RLM_MODULE_FAIL;
1128 passwd_len = strlen(passwd_val);
1129 strncpy(passwd_item->strvalue,passwd_val,MAX_STRING_LEN - 1);
1130 passwd_item->length = (passwd_len > (MAX_STRING_LEN - 1)) ? (MAX_STRING_LEN - 1) : passwd_len;
1131 pairadd(&request->config_items,passwd_item);
1132 DEBUG("rlm_ldap: Added password %s in check items",passwd_item->strvalue);
1137 ldap_value_free(passwd_vals);
1144 DEBUG("rlm_ldap: looking for check items in directory...");
1146 if ((check_tmp = ldap_pairget(conn->ld, msg, inst->check_item_map,check_pairs)) != NULL)
1147 pairadd(check_pairs, check_tmp);
1150 DEBUG("rlm_ldap: looking for reply items in directory...");
1153 if ((reply_tmp = ldap_pairget(conn->ld, msg, inst->reply_item_map,reply_pairs)) != NULL)
1154 pairadd(reply_pairs, reply_tmp);
1156 if (inst->do_comp && paircmp(request,request->packet->vps,*check_pairs,reply_pairs) != 0){
1157 DEBUG("rlm_ldap: Pairs do not match. Rejecting user.");
1158 snprintf(module_fmsg,sizeof(module_fmsg),"rlm_ldap: Pairs do not match");
1159 module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
1160 pairadd(&request->packet->vps, module_fmsg_vp);
1161 ldap_msgfree(result);
1162 ldap_release_conn(conn_id,inst->conns);
1164 return RLM_MODULE_REJECT;
1168 * Module should default to LDAP authentication if no Auth-Type
1171 if (pairfind(*check_pairs, PW_AUTHTYPE) == NULL)
1172 pairadd(check_pairs, pairmake("Auth-Type", "LDAP", T_OP_EQ));
1175 DEBUG("rlm_ldap: user %s authorized to use remote access",
1176 request->username->strvalue);
1177 ldap_msgfree(result);
1178 ldap_release_conn(conn_id,inst->conns);
1180 return RLM_MODULE_OK;
1183 /*****************************************************************************
1185 * Function: rlm_ldap_authenticate
1187 * Purpose: Check the user's password against ldap database
1189 *****************************************************************************/
1191 ldap_authenticate(void *instance, REQUEST * request)
1194 LDAPMessage *result, *msg;
1195 ldap_instance *inst = instance;
1196 char *user_dn, *attrs[] = {"uid", NULL};
1197 char filter[MAX_AUTH_QUERY_LEN];
1200 VALUE_PAIR *vp_user_dn;
1201 VALUE_PAIR *module_fmsg_vp;
1202 char module_fmsg[MAX_STRING_LEN];
1206 DEBUG("rlm_ldap: - authenticate");
1208 * Ensure that we're being passed a plain-text password, and not
1212 if (!request->username) {
1213 radlog(L_AUTH, "rlm_ldap: Attribute \"User-Name\" is required for authentication.\n");
1214 return RLM_MODULE_INVALID;
1217 if (!request->password){
1218 radlog(L_AUTH, "rlm_ldap: Attribute \"User-Password\" is required for authentication.");
1219 return RLM_MODULE_INVALID;
1222 if(request->password->attribute != PW_PASSWORD) {
1223 radlog(L_AUTH, "rlm_ldap: Attribute \"User-Password\" is required for authentication. Cannot use \"%s\".", request->password->name);
1224 return RLM_MODULE_INVALID;
1227 if (request->password->length == 0) {
1228 radlog(L_ERR, "rlm_ldap: empty password supplied");
1229 return RLM_MODULE_INVALID;
1232 DEBUG("rlm_ldap: login attempt by \"%s\" with password \"%s\"",
1233 request->username->strvalue, request->password->strvalue);
1235 while((vp_user_dn = pairfind(request->packet->vps, PW_LDAP_USERDN)) == NULL) {
1236 if (!radius_xlat(filter, sizeof(filter), inst->filter,
1238 radlog (L_ERR, "rlm_ldap: unable to create filter.\n");
1239 return RLM_MODULE_INVALID;
1242 if (!radius_xlat(basedn, sizeof(basedn), inst->basedn,
1244 radlog (L_ERR, "rlm_ldap: unable to create basedn.\n");
1245 return RLM_MODULE_INVALID;
1248 if ((conn_id = ldap_get_conn(inst->conns,&conn,inst)) == -1){
1249 radlog(L_ERR, "rlm_ldap: All ldap connections are in use");
1250 return RLM_MODULE_FAIL;
1252 if ((res = perform_search(instance, conn, basedn, LDAP_SCOPE_SUBTREE, filter, attrs, &result)) != RLM_MODULE_OK) {
1253 if (res == RLM_MODULE_NOTFOUND){
1254 snprintf(module_fmsg,sizeof(module_fmsg),"rlm_ldap: User not found");
1255 module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
1256 pairadd(&request->packet->vps, module_fmsg_vp);
1258 ldap_release_conn(conn_id,inst->conns);
1261 if ((msg = ldap_first_entry(conn->ld, result)) == NULL) {
1262 ldap_msgfree(result);
1263 ldap_release_conn(conn_id,inst->conns);
1264 return RLM_MODULE_FAIL;
1266 if ((user_dn = ldap_get_dn(conn->ld, msg)) == NULL) {
1267 DEBUG("rlm_ldap: ldap_get_dn() failed");
1268 ldap_msgfree(result);
1269 ldap_release_conn(conn_id,inst->conns);
1270 return RLM_MODULE_FAIL;
1272 ldap_release_conn(conn_id,inst->conns);
1273 pairadd(&request->packet->vps, pairmake("Ldap-UserDn", user_dn, T_OP_EQ));
1274 ldap_memfree(user_dn);
1275 ldap_msgfree(result);
1278 user_dn = vp_user_dn->strvalue;
1280 DEBUG("rlm_ldap: user DN: %s", user_dn);
1282 ld_user = ldap_connect(instance, user_dn, request->password->strvalue,
1284 if (ld_user == NULL){
1285 snprintf(module_fmsg,sizeof(module_fmsg),"rlm_ldap: Bind as user failed");
1286 module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
1287 pairadd(&request->packet->vps, module_fmsg_vp);
1291 DEBUG("rlm_ldap: user %s authenticated succesfully",
1292 request->username->strvalue);
1293 ldap_unbind_s(ld_user);
1295 return RLM_MODULE_OK;
1299 ldap_connect(void *instance, const char *dn, const char *password, int auth, int *result)
1301 ldap_instance *inst = instance;
1303 int msgid, rc, ldap_version;
1307 DEBUG("rlm_ldap: (re)connect to %s:%d, authentication %d", inst->server, inst->port, auth);
1308 if ((ld = ldap_init(inst->server, inst->port)) == NULL) {
1309 radlog(L_ERR, "rlm_ldap: ldap_init() failed");
1310 *result = RLM_MODULE_FAIL;
1313 if (ldap_set_option(ld, LDAP_OPT_NETWORK_TIMEOUT, (void *) &(inst->net_timeout)) != LDAP_OPT_SUCCESS) {
1314 radlog(L_ERR, "rlm_ldap: Could not set LDAP_OPT_NETWORK_TIMEOUT %ld.%ld", inst->net_timeout.tv_sec, inst->net_timeout.tv_usec);
1316 if (ldap_set_option(ld, LDAP_OPT_TIMELIMIT, (void *) &(inst->timelimit)) != LDAP_OPT_SUCCESS) {
1317 radlog(L_ERR, "rlm_ldap: Could not set LDAP_OPT_TIMELIMIT %d", inst->timelimit);
1319 if (inst->ldap_debug && ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &(inst->ldap_debug)) != LDAP_OPT_SUCCESS) {
1320 radlog(L_ERR, "rlm_ldap: Could not set LDAP_OPT_DEBUG_LEVEL %d", inst->ldap_debug);
1322 #ifdef HAVE_LDAP_START_TLS
1323 if(inst->tls_mode) {
1324 DEBUG("rlm_ldap: setting TLS mode to %d", inst->tls_mode);
1325 if(ldap_set_option(ld, LDAP_OPT_X_TLS,
1326 (void *) &(inst->tls_mode)) != LDAP_OPT_SUCCESS) {
1327 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
1328 radlog(L_ERR, "rlm_ldap: could not set LDAP_OPT_X_TLS option %s", ldap_err2string(ldap_errno));
1332 if (inst->start_tls) {
1333 DEBUG("rlm_ldap: starting TLS");
1334 ldap_version = LDAP_VERSION3;
1335 if (ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &ldap_version) == LDAP_SUCCESS) {
1336 rc = ldap_start_tls_s(ld, NULL, NULL);
1337 if (rc != LDAP_SUCCESS) {
1338 DEBUG("rlm_ldap: ldap_start_tls_s()");
1339 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
1340 radlog(L_ERR, "rlm_ldap: could not start TLS %s", ldap_err2string(ldap_errno));
1341 *result = RLM_MODULE_FAIL;
1347 #endif /* HAVE_LDAP_START_TLS */
1349 DEBUG("rlm_ldap: bind as %s/%s to %s:%d", dn, password, inst->server, inst->port);
1350 msgid = ldap_bind(ld, dn, password,LDAP_AUTH_SIMPLE);
1352 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
1353 radlog(L_ERR, "rlm_ldap: %s bind to %s:%d failed: %s",
1354 dn, inst->server, inst->port,
1355 ldap_err2string(ldap_errno));
1356 *result = RLM_MODULE_FAIL;
1360 DEBUG("rlm_ldap: waiting for bind result ...");
1362 rc = ldap_result(ld, msgid, 1, &(inst->timeout), &res);
1365 DEBUG("rlm_ldap: ldap_result()");
1366 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
1367 radlog(L_ERR, "rlm_ldap: %s bind to %s:%d failed: %s",
1368 dn, inst->server, inst->port,
1369 (rc == 0) ? "timeout" : ldap_err2string(ldap_errno));
1370 *result = RLM_MODULE_FAIL;
1374 ldap_errno = ldap_result2error(ld, res, 1);
1375 switch (ldap_errno) {
1377 *result = RLM_MODULE_OK;
1380 case LDAP_INVALID_CREDENTIALS:
1382 *result = RLM_MODULE_REJECT;
1384 radlog(L_ERR, "rlm_ldap: LDAP login failed: check login, password settings in ldap section of radiusd.conf");
1385 *result = RLM_MODULE_FAIL;
1390 radlog(L_ERR,"rlm_ldap: %s bind to %s:%d failed %s",
1391 dn, inst->server, inst->port,
1392 ldap_err2string(ldap_errno));
1393 *result = RLM_MODULE_FAIL;
1396 if (*result != RLM_MODULE_OK) {
1403 /*****************************************************************************
1405 * Detach from the LDAP server and cleanup internal state.
1407 *****************************************************************************/
1409 ldap_detach(void *instance)
1411 ldap_instance *inst = instance;
1412 TLDAP_RADIUS *pair, *nextpair;
1415 free((char *) inst->server);
1417 free((char *) inst->login);
1419 free((char *) inst->password);
1421 free((char *) inst->basedn);
1422 if (inst->dictionary_mapping)
1423 free(inst->dictionary_mapping);
1425 free((char *) inst->filter);
1426 if (inst->passwd_hdr)
1427 free((char *) inst->passwd_hdr);
1428 if (inst->passwd_attr)
1429 free((char *) inst->passwd_attr);
1430 if (inst->groupname_attr)
1431 free((char *) inst->groupname_attr);
1432 if (inst->groupmemb_filt)
1433 free((char *) inst->groupmemb_filt);
1434 if (inst->groupmemb_attr)
1435 free((char *) inst->groupmemb_attr);
1439 for(;i<inst->num_conns;i++){
1440 if (inst->conns[i].ld){
1441 if (inst->cache_timeout >0)
1442 ldap_destroy_cache(inst->conns[i].ld);
1443 ldap_unbind_s(inst->conns[i].ld);
1445 pthread_mutex_destroy(&inst->conns[i].mutex);
1450 pair = inst->check_item_map;
1452 while (pair != NULL) {
1453 nextpair = pair->next;
1455 free(pair->radius_attr);
1460 pair = inst->reply_item_map;
1462 while (pair != NULL) {
1463 nextpair = pair->next;
1465 free(pair->radius_attr);
1473 while(inst->atts[i])
1474 free(inst->atts[i++]);
1478 paircompare_unregister(PW_LDAP_GROUP, ldap_groupcmp);
1479 xlat_unregister(inst->xlat_name,ldap_xlat);
1480 free(inst->xlat_name);
1489 fieldcpy(char *string, char **uptr)
1494 while (*ptr == ' ' || *ptr == '\t') {
1499 while (*ptr != '"' && *ptr != '\0' && *ptr != '\n') {
1509 while (*ptr != ' ' && *ptr != '\t' && *ptr != '\0' && *ptr != '\n' &&
1510 *ptr != '=' && *ptr != ',') {
1518 /*****************************************************************************
1519 * Get RADIUS attributes from LDAP object
1520 * ( according to draft-adoba-radius-05.txt
1521 * <http://www.ietf.org/internet-drafts/draft-adoba-radius-05.txt> )
1523 *****************************************************************************/
1526 ldap_pairget(LDAP * ld, LDAPMessage * entry,
1527 TLDAP_RADIUS * item_map, VALUE_PAIR **pairs)
1533 TLDAP_RADIUS *element;
1535 int is_generic_attribute;
1537 VALUE_PAIR *pairlist = NULL;
1538 VALUE_PAIR *newpair = NULL;
1540 /* check if there is a mapping from this LDAP attribute to a RADIUS attribute */
1541 for (element = item_map; element != NULL; element = element->next) {
1542 if ((vals = ldap_get_values(ld,entry,element->attr)) != NULL){
1543 /* check whether this is a one-to-one-mapped ldap attribute or a generic
1544 attribute and set flag accordingly */
1546 if (strcasecmp(element->radius_attr, GENERIC_ATTRIBUTE_ID)==0)
1547 is_generic_attribute = 1;
1549 is_generic_attribute = 0;
1551 /* find out how many values there are for the attribute and extract all of them */
1553 vals_count = ldap_count_values(vals);
1555 for (vals_idx = 0; vals_idx < vals_count; vals_idx++) {
1556 ptr = vals[vals_idx];
1558 if (is_generic_attribute) {
1559 /* this is a generic attribute */
1560 LRAD_TOKEN dummy; /* makes pairread happy */
1562 /* not sure if using pairread here is ok ... */
1563 if ( (newpair = pairread(&ptr, &dummy)) != NULL) {
1564 DEBUG("rlm_ldap: extracted attribute %s from generic item %s",
1565 newpair->name, vals[vals_idx]);
1566 pairadd(&pairlist, newpair);
1568 radlog(L_ERR, "rlm_ldap: parsing %s failed: %s",
1569 element->attr, vals[vals_idx]);
1572 /* this is a one-to-one-mapped attribute */
1573 token = gettoken(&ptr, value, sizeof(value) - 1);
1574 if (token < T_EQSTART || token > T_EQEND) {
1577 gettoken(&ptr, value, sizeof(value) - 1);
1579 if (value[0] == 0) {
1580 DEBUG("rlm_ldap: Attribute %s has no value", element->attr);
1583 DEBUG("rlm_ldap: Adding %s as %s, value %s & op=%d", element->attr, element->radius_attr, value, token);
1584 if ((newpair = pairmake(element->radius_attr, value, token)) == NULL)
1587 pairdelete(pairs,newpair->attribute);
1589 pairadd(&pairlist, newpair);
1592 ldap_value_free(vals);
1599 /* globally exported name */
1600 module_t rlm_ldap = {
1602 RLM_TYPE_THREAD_SAFE, /* type: reserved */
1603 NULL, /* initialization */
1604 ldap_instantiate, /* instantiation */
1606 ldap_authenticate, /* authentication */
1607 ldap_authorize, /* authorization */
1608 NULL, /* preaccounting */
1609 NULL, /* accounting */
1610 NULL, /* checksimul */
1611 NULL, /* pre-proxy */
1612 NULL, /* post-proxy */
1613 NULL /* post-auth */
1615 ldap_detach, /* detach */