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>
96 * - If we add a check item then use the == operator. Based on an idea by Allister Maguire <amaguire@gnc.net.nz>
98 static const char rcsid[] = "$Id$";
100 #include "autoconf.h"
102 #include <sys/types.h>
103 #include <sys/socket.h>
104 #include <sys/time.h>
105 #include <netinet/in.h>
122 #include "libradius.h"
124 #include "conffile.h"
126 #include "rad_assert.h"
129 #define MAX_AUTH_QUERY_LEN 256
130 #define MAX_GROUP_STR_LEN 1024
134 * These are used in case ldap_search returns LDAP_SERVER_DOWN
135 * In that case we do conn->failed_conns++ and then check it:
136 * If conn->failed_conns <= MAX_FAILED_CONNS_START then we try
138 * conn->failed_conns is also checked on entrance in perform_search:
139 * If conn->failed_conns > MAX_FAILED_CONNS_START then we don't
140 * try to do anything and we just do conn->failed_conns++ and
141 * return RLM_MODULE_FAIL
142 * if conn->failed_conns >= MAX_FAILED_CONNS_END then we give it
143 * another chance and we set it to MAX_FAILED_CONNS_RESTART and
147 * We are assuming that the majority of the LDAP_SERVER_DOWN cases
148 * will either be an ldap connection timeout or a temporary ldap
150 * As a result we make a few attempts to reconnect hoping that the problem
151 * will soon go away. If it does not go away then we just return
152 * RLM_MODULE_FAIL on entrance in perform_search until conn->failed_conns
153 * gets to MAX_FAILED_CONNS_END. After that we give it one more chance by
154 * going back to MAX_FAILED_CONNS_RESTART
158 #define MAX_FAILED_CONNS_END 20
159 #define MAX_FAILED_CONNS_RESTART 4
160 #define MAX_FAILED_CONNS_START 5
162 /* linked list of mappings between RADIUS attributes and LDAP attributes */
163 struct TLDAP_RADIUS {
166 struct TLDAP_RADIUS* next;
168 typedef struct TLDAP_RADIUS TLDAP_RADIUS;
170 typedef struct ldap_conn {
175 pthread_mutex_t mutex;
178 #define MAX_SERVER_LINE 1024
184 struct timeval net_timeout;
185 struct timeval timeout;
198 char *default_profile;
203 char *dictionary_mapping;
204 char *groupname_attr;
205 char *groupmemb_filt;
206 char *groupmemb_attr;
208 TLDAP_RADIUS *check_item_map;
209 TLDAP_RADIUS *reply_item_map;
211 int ldap_debug; /* Debug flag for LDAP SDK */
212 char *xlat_name; /* name used to xlat */
215 static CONF_PARSER module_config[] = {
216 {"server", PW_TYPE_STRING_PTR, offsetof(ldap_instance,server), NULL, NULL},
217 {"port", PW_TYPE_INTEGER, offsetof(ldap_instance,port), NULL, "389"},
218 /* wait forever on network activity */
219 {"net_timeout", PW_TYPE_INTEGER, offsetof(ldap_instance,net_timeout.tv_sec), NULL, "10"},
220 /* wait forever for search results */
221 {"timeout", PW_TYPE_INTEGER, offsetof(ldap_instance,timeout.tv_sec), NULL, "20"},
222 /* allow server unlimited time for search (server-side limit) */
223 {"timelimit", PW_TYPE_INTEGER, offsetof(ldap_instance,timelimit), NULL, "20"},
224 {"ldap_cache_timeout", PW_TYPE_INTEGER, offsetof(ldap_instance,cache_timeout), NULL, "0"},
225 {"ldap_cache_size", PW_TYPE_INTEGER, offsetof(ldap_instance,cache_size), NULL, "0"},
226 {"identity", PW_TYPE_STRING_PTR, offsetof(ldap_instance,login), NULL, ""},
227 {"start_tls", PW_TYPE_BOOLEAN, offsetof(ldap_instance,start_tls), NULL, "no"},
228 {"password", PW_TYPE_STRING_PTR, offsetof(ldap_instance,password), NULL, ""},
229 {"basedn", PW_TYPE_STRING_PTR, offsetof(ldap_instance,basedn), NULL, NULL},
230 {"filter", PW_TYPE_STRING_PTR, offsetof(ldap_instance,filter), NULL, "(uid=%u)"},
231 {"default_profile", PW_TYPE_STRING_PTR, offsetof(ldap_instance,default_profile), NULL, NULL},
232 {"profile_attribute", PW_TYPE_STRING_PTR, offsetof(ldap_instance,profile_attr), NULL, NULL},
233 {"password_header", PW_TYPE_STRING_PTR, offsetof(ldap_instance,passwd_hdr), NULL, NULL},
234 {"password_attribute", PW_TYPE_STRING_PTR, offsetof(ldap_instance,passwd_attr), NULL, NULL},
235 /* LDAP attribute name that controls remote access */
236 {"access_attr", PW_TYPE_STRING_PTR, offsetof(ldap_instance,access_attr), NULL, NULL},
237 /* file with mapping between LDAP and RADIUS attributes */
238 {"groupname_attribute", PW_TYPE_STRING_PTR, offsetof(ldap_instance,groupname_attr), NULL, "cn"},
239 {"groupmembership_filter", PW_TYPE_STRING_PTR, offsetof(ldap_instance,groupmemb_filt), NULL, "(|(&(objectClass=GroupOfNames)(member=%{Ldap-UserDn}))(&(objectClass=GroupOfUniqueNames)(uniquemember=%{Ldap-UserDn})))"},
240 {"groupmembership_attribute", PW_TYPE_STRING_PTR, offsetof(ldap_instance,groupmemb_attr), NULL, NULL},
241 {"dictionary_mapping", PW_TYPE_STRING_PTR, offsetof(ldap_instance,dictionary_mapping), NULL, "${confdir}/ldap.attrmap"},
242 {"ldap_debug", PW_TYPE_INTEGER, offsetof(ldap_instance,ldap_debug), NULL, "0x0000"},
243 {"ldap_connections_number", PW_TYPE_INTEGER, offsetof(ldap_instance,num_conns), NULL, "5"},
244 {"compare_check_items", PW_TYPE_BOOLEAN, offsetof(ldap_instance,do_comp), NULL, "no"},
245 {"access_attr_used_for_allow", PW_TYPE_BOOLEAN, offsetof(ldap_instance,default_allow), NULL, "yes"},
247 {NULL, -1, 0, NULL, NULL}
250 #define ld_valid ld_options.ldo_valid
251 #define LDAP_VALID_SESSION 0x2
252 #define LDAP_VALID(ld) ( (ld)->ld_valid == LDAP_VALID_SESSION )
255 static void fieldcpy(char *, char **);
257 static VALUE_PAIR *ldap_pairget(LDAP *, LDAPMessage *, TLDAP_RADIUS *,VALUE_PAIR **,char);
258 static int ldap_groupcmp(void *, REQUEST *, VALUE_PAIR *, VALUE_PAIR *, VALUE_PAIR *, VALUE_PAIR **);
259 static int ldap_xlat(void *,REQUEST *, char *, char *,int, RADIUS_ESCAPE_STRING);
260 static LDAP *ldap_connect(void *instance, const char *, const char *, int, int *);
261 static int read_mappings(ldap_instance* inst);
263 static inline int ldap_get_conn(LDAP_CONN *conns,LDAP_CONN **ret,void *instance)
265 ldap_instance *inst = instance;
268 for(;i<inst->num_conns;i++){
269 if (conns[i].locked == 0 && pthread_mutex_trylock(&(conns[i].mutex)) == 0){
272 DEBUG("ldap_get_conn: Got Id: %d",i);
280 static inline void ldap_release_conn(int i, LDAP_CONN *conns)
282 DEBUG("ldap_release_conn: Release Id: %d",i);
284 pthread_mutex_unlock(&(conns[i].mutex));
287 /*************************************************************************
289 * Function: rlm_ldap_instantiate
291 * Purpose: Uses section of radiusd config file passed as parameter
292 * to create an instance of the module.
294 *************************************************************************/
296 ldap_instantiate(CONF_SECTION * conf, void **instance)
301 int reply_map_num = 0;
302 int check_map_num = 0;
303 int att_map[3] = {0,0,0};
307 inst = rad_malloc(sizeof *inst);
309 if (cf_section_parse(conf, inst, module_config) < 0) {
314 if (inst->server == NULL) {
315 radlog(L_ERR, "rlm_ldap: missing 'server' directive.");
320 inst->timeout.tv_usec = 0;
321 inst->net_timeout.tv_usec = 0;
322 /* workaround for servers which support LDAPS but not START TLS */
323 if(inst->port == LDAPS_PORT)
324 inst->tls_mode = LDAP_OPT_X_TLS_HARD;
326 inst->tls_mode = LDAP_OPT_X_TLS_TRY;
327 inst->reply_item_map = NULL;
328 inst->check_item_map = NULL;
331 paircompare_register(PW_LDAP_GROUP, PW_USER_NAME, ldap_groupcmp, inst);
332 DEBUG("conns: %p",inst->conns);
334 xlat_name = cf_section_name2(conf);
335 if (xlat_name == NULL) {
336 xlat_name = cf_section_name1(conf);
337 rad_assert(xlat_name != NULL); /* or all hell breaks loose */
339 inst->xlat_name = strdup(xlat_name);
340 xlat_register(xlat_name,ldap_xlat,inst);
342 if (inst->num_conns <= 0){
343 radlog(L_ERR, "rlm_ldap: Invalid ldap connections number passed.");
347 inst->conns = (LDAP_CONN *)malloc(sizeof(LDAP_CONN)*inst->num_conns);
348 if (inst->conns == NULL){
349 radlog(L_ERR, "rlm_ldap: Could not allocate memory. Aborting.");
353 for(;i<inst->num_conns;i++){
354 inst->conns[i].bound = 0;
355 inst->conns[i].locked = 0;
356 inst->conns[i].failed_conns = 0;
357 inst->conns[i].ld = NULL;
358 pthread_mutex_init(&inst->conns[i].mutex, NULL);
361 if (read_mappings(inst) != 0) {
362 radlog(L_ERR, "rlm_ldap: Reading dictionary mappings from file %s failed",
363 inst->dictionary_mapping);
364 radlog(L_ERR, "rlm_ldap: Proceeding with no mappings");
367 pair = inst->check_item_map;
372 check_map_num = (atts_num - 1);
373 pair = inst->reply_item_map;
378 reply_map_num = (atts_num - 1);
379 if (inst->profile_attr)
381 if (inst->passwd_attr)
383 if (inst->access_attr)
385 inst->atts = (char **)malloc(sizeof(char *)*(atts_num + 1));
386 if (inst->atts == NULL){
387 radlog(L_ERR, "rlm_ldap: Could not allocate memory. Aborting.");
391 pair = inst->check_item_map;
392 for(i=0;i<atts_num;i++){
393 if (i <= check_map_num ){
394 inst->atts[i] = pair->attr;
395 if (i == check_map_num)
396 pair = inst->reply_item_map;
400 else if (i <= reply_map_num){
401 inst->atts[i] = pair->attr;
405 if (inst->profile_attr && !att_map[0]){
406 inst->atts[i] = inst->profile_attr;
409 else if (inst->passwd_attr && !att_map[1]){
410 inst->atts[i] = inst->passwd_attr;
413 else if (inst->access_attr && !att_map[2]){
414 inst->atts[i] = inst->access_attr;
419 inst->atts[atts_num] = NULL;
421 DEBUG("conns: %p",inst->conns);
431 * read_mappings(...) reads a ldap<->radius mappings file to inst->reply_item_map and inst->check_item_map
434 #define MAX_LINE_LEN 160
435 #define GENERIC_ATTRIBUTE_ID "$GENERIC$"
438 read_mappings(ldap_instance* inst)
442 /* all buffers are of MAX_LINE_LEN so we can use sscanf without being afraid of buffer overflows */
443 char buf[MAX_LINE_LEN], itemType[MAX_LINE_LEN], radiusAttribute[MAX_LINE_LEN], ldapAttribute[MAX_LINE_LEN];
446 /* open the mappings file for reading */
448 filename = inst->dictionary_mapping;
449 DEBUG("rlm_ldap: reading ldap<->radius mappings from file %s", filename);
450 mapfile = fopen(filename, "r");
452 if (mapfile == NULL) {
453 radlog(L_ERR, "rlm_ldap: Opening file %s failed", filename);
454 return -1; /* error */
457 /* read file line by line. Note that if line length exceed MAX_LINE_LEN, line numbers will be mixed up */
461 while (fgets(buf, sizeof buf, mapfile)!=NULL) {
469 ptr = strchr(buf, '#');
473 if (buf[0] == 0) continue;
475 /* extract tokens from the string */
476 token_count = sscanf(buf, "%s %s %s", itemType, radiusAttribute, ldapAttribute);
478 if (token_count <= 0) /* no tokens */
481 if (token_count != 3) {
482 radlog(L_ERR, "rlm_ldap: Skipping %s line %i: %s", filename, linenumber, buf);
483 radlog(L_ERR, "rlm_ldap: Expected 3 tokens "
484 "(Item type, RADIUS Attribute and LDAP Attribute) but found only %i", token_count);
488 /* create new TLDAP_RADIUS list node */
489 pair = rad_malloc(sizeof(TLDAP_RADIUS));
491 pair->attr = strdup(ldapAttribute);
492 pair->radius_attr = strdup(radiusAttribute);
494 if ( (pair->attr == NULL) || (pair->radius_attr == NULL) ) {
495 radlog(L_ERR, "rlm_ldap: Out of memory");
496 if (pair->attr) free(pair->attr);
497 if (pair->radius_attr) free(pair->radius_attr);
503 /* push node to correct list */
504 if (strcasecmp(itemType, "checkItem") == 0) {
505 pair->next = inst->check_item_map;
506 inst->check_item_map = pair;
507 } else if (strcasecmp(itemType, "replyItem") == 0) {
508 pair->next = inst->reply_item_map;
509 inst->reply_item_map = pair;
511 radlog(L_ERR, "rlm_ldap: file %s: skipping line %i: unknown itemType %s",
512 filename, linenumber, itemType);
514 free(pair->radius_attr);
519 DEBUG("rlm_ldap: LDAP %s mapped to RADIUS %s",
520 pair->attr, pair->radius_attr);
525 return 0; /* success */
529 perform_search(void *instance, LDAP_CONN *conn, char *search_basedn, int scope, char *filter,
530 char **attrs, LDAPMessage ** result)
532 int res = RLM_MODULE_OK;
534 ldap_instance *inst = instance;
535 int search_retry = 0;
540 radlog(L_ERR, "rlm_ldap: NULL connection handle passed");
541 return RLM_MODULE_FAIL;
543 if (conn->failed_conns > MAX_FAILED_CONNS_START){
544 conn->failed_conns++;
545 if (conn->failed_conns >= MAX_FAILED_CONNS_END){
546 conn->failed_conns = MAX_FAILED_CONNS_RESTART;
551 if (!conn->bound || conn->ld == NULL) {
552 DEBUG2("rlm_ldap: attempting LDAP reconnection");
554 DEBUG2("rlm_ldap: closing existing LDAP connection");
555 if (inst->cache_timeout >0)
556 ldap_destroy_cache(conn->ld);
557 ldap_unbind_s(conn->ld);
559 if ((conn->ld = ldap_connect(instance, inst->login, inst->password, 0, &res)) == NULL) {
560 radlog(L_ERR, "rlm_ldap: (re)connection attempt failed");
561 if (search_retry == 0)
562 conn->failed_conns++;
563 return (RLM_MODULE_FAIL);
566 conn->failed_conns = 0;
568 DEBUG2("rlm_ldap: performing search in %s, with filter %s", search_basedn ? search_basedn : "(null)" , filter);
569 switch (ldap_search_st(conn->ld, search_basedn, scope, filter, attrs, 0, &(inst->timeout), result)) {
571 case LDAP_NO_SUCH_OBJECT:
573 case LDAP_SERVER_DOWN:
574 radlog(L_ERR, "rlm_ldap: ldap_search() failed: LDAP connection lost.");
575 conn->failed_conns++;
576 if (search_retry == 0){
577 if (conn->failed_conns <= MAX_FAILED_CONNS_START){
578 radlog(L_INFO, "rlm_ldap: Attempting reconnect");
581 ldap_msgfree(*result);
585 ldap_msgfree(*result);
586 return RLM_MODULE_FAIL;
587 case LDAP_INSUFFICIENT_ACCESS:
588 radlog(L_ERR, "rlm_ldap: ldap_search() failed: Insufficient access. Check the identity and password configuration directives.");
589 ldap_msgfree(*result);
590 return RLM_MODULE_FAIL;
592 radlog(L_ERR, "rlm_ldap: ldap_search() failed: Timed out while waiting for server to respond. Please increase the timeout.");
593 ldap_msgfree(*result);
594 return RLM_MODULE_FAIL;
595 case LDAP_TIMELIMIT_EXCEEDED:
597 case LDAP_UNAVAILABLE:
598 /* We don't need to reconnect in these cases so we don't set conn->bound */
599 ldap_get_option(conn->ld, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
600 radlog(L_ERR, "rlm_ldap: ldap_search() failed: %s", ldap_err2string(ldap_errno));
601 ldap_msgfree(*result);
602 return (RLM_MODULE_FAIL);
604 ldap_get_option(conn->ld, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
605 radlog(L_ERR, "rlm_ldap: ldap_search() failed: %s", ldap_err2string(ldap_errno));
607 ldap_msgfree(*result);
608 return (RLM_MODULE_FAIL);
611 if ((ldap_count_entries(conn->ld, *result)) != 1) {
612 DEBUG("rlm_ldap: object not found or got ambiguous search result");
613 res = RLM_MODULE_NOTFOUND;
614 ldap_msgfree(*result);
621 * ldap_groupcmp(). Implement the Ldap-Group == "group" filter
624 static int ldap_groupcmp(void *instance, REQUEST *req, VALUE_PAIR *request, VALUE_PAIR *check,
625 VALUE_PAIR *check_pairs, VALUE_PAIR **reply_pairs)
627 char filter[MAX_GROUP_STR_LEN];
628 char gr_filter[MAX_AUTH_QUERY_LEN];
630 LDAPMessage *result = NULL;
631 LDAPMessage *msg = NULL;
633 char *attrs[] = {"dn",NULL};
634 ldap_instance *inst = instance;
638 check_pairs = check_pairs;
639 reply_pairs = reply_pairs;
641 DEBUG("rlm_ldap: Entering ldap_groupcmp()");
643 if (check->strvalue == NULL || check->length == 0){
644 DEBUG("rlm_ldap::ldap_groupcmp: Illegal group name");
649 DEBUG("rlm_ldap::ldap_groupcmp: NULL request");
653 if (!radius_xlat(basedn, sizeof(basedn), inst->basedn, req, NULL)) {
654 DEBUG("rlm_ldap::ldap_groupcmp: unable to create basedn.");
658 if ((pairfind(req->packet->vps, PW_LDAP_USERDN)) == NULL){
659 char *user_dn = NULL;
661 if (!radius_xlat(filter, MAX_AUTH_QUERY_LEN, inst->filter, req, NULL)) {
662 DEBUG("rlm_ldap::ldap_groupcmp: unable to create filter");
665 if ((conn_id = ldap_get_conn(inst->conns,&conn,inst)) == -1){
666 radlog(L_ERR, "rlm_ldap: All ldap connections are in use");
669 if ((res = perform_search(inst, conn, basedn, LDAP_SCOPE_SUBTREE, filter, attrs, &result)) != RLM_MODULE_OK) {
670 DEBUG("rlm_ldap::ldap_groupcmp: search failed");
671 ldap_release_conn(conn_id,inst->conns);
674 if ((msg = ldap_first_entry(conn->ld, result)) == NULL) {
675 DEBUG("rlm_ldap::ldap_groupcmp: ldap_first_entry() failed");
676 ldap_release_conn(conn_id,inst->conns);
677 ldap_msgfree(result);
680 if ((user_dn = ldap_get_dn(conn->ld, msg)) == NULL) {
681 DEBUG("rlm_ldap:ldap_groupcmp:: ldap_get_dn() failed");
682 ldap_release_conn(conn_id,inst->conns);
683 ldap_msgfree(result);
686 ldap_release_conn(conn_id,inst->conns);
688 * Adding new attribute containing DN for LDAP object associated with
691 pairadd(&req->packet->vps, pairmake("Ldap-UserDn", user_dn, T_OP_EQ));
692 ldap_memfree(user_dn);
693 ldap_msgfree(result);
696 if(!radius_xlat(gr_filter, MAX_GROUP_STR_LEN, inst->groupmemb_filt, req, NULL)){
697 DEBUG("rlm_ldap::ldap_groupcmp: unable to create filter.");
701 snprintf(filter,MAX_GROUP_STR_LEN - 1, "(&(%s=%s)%s)",inst->groupname_attr,(char *)check->strvalue,gr_filter);
703 if ((conn_id = ldap_get_conn(inst->conns,&conn,inst)) == -1){
704 radlog(L_ERR, "rlm_ldap: All ldap connections are in use");
708 if ((res = perform_search(inst, conn, basedn, LDAP_SCOPE_SUBTREE, filter, attrs, &result)) != RLM_MODULE_OK){
709 if (res != RLM_MODULE_NOTFOUND){
710 DEBUG("rlm_ldap::ldap_groupcmp: Search returned error");
711 ldap_release_conn(conn_id,inst->conns);
715 if (res != RLM_MODULE_NOTFOUND)
716 ldap_msgfree(result);
717 ldap_release_conn(conn_id,inst->conns);
719 if (res == RLM_MODULE_NOTFOUND){
720 if (inst->groupmemb_attr == NULL){
721 DEBUG("rlm_ldap::ldap_groupcmp: Group %s not found or user is not a member.",(char *)check->strvalue);
726 char *group_attrs[] = {inst->groupmemb_attr,NULL};
728 user_dn = pairfind(req->packet->vps, PW_LDAP_USERDN);
729 if (user_dn != NULL){
732 snprintf(filter,MAX_GROUP_STR_LEN - 1, "(objectclass=*)");
733 if ((conn_id = ldap_get_conn(inst->conns,&conn,inst)) == -1){
734 radlog(L_ERR, "rlm_ldap: Add ldap connections are in use");
737 if ((res = perform_search(inst, conn, user_dn->strvalue, LDAP_SCOPE_BASE, filter, group_attrs, &result)) != RLM_MODULE_OK){
738 DEBUG("rlm_ldap::ldap_groupcmp: Search returned error");
739 ldap_release_conn(conn_id, inst->conns);
743 if ((msg = ldap_first_entry(conn->ld, result)) == NULL) {
744 DEBUG("rlm_ldap::ldap_groupcmp: ldap_first_entry() failed");
745 ldap_release_conn(conn_id,inst->conns);
746 ldap_msgfree(result);
749 if ((vals = ldap_get_values(conn->ld, msg, inst->groupmemb_attr)) != NULL) {
753 for (;i < ldap_count_values(vals);i++){
754 if (strchr(vals[i],',') != NULL){ /* This looks like a DN */
755 LDAPMessage *gr_result = NULL;
757 snprintf(filter,MAX_GROUP_STR_LEN - 1, "(%s=%s)",inst->groupname_attr,(char *)check->strvalue);
758 if ((res = perform_search(inst, conn, vals[i], LDAP_SCOPE_BASE, filter, attrs, &gr_result)) != RLM_MODULE_OK){
759 if (res != RLM_MODULE_NOTFOUND){
760 DEBUG("rlm_ldap::ldap_groupcmp: Search returned error");
761 ldap_msgfree(result);
762 ldap_value_free(vals);
763 ldap_release_conn(conn_id, inst->conns);
768 ldap_msgfree(gr_result);
774 if (strcmp(vals[i],(char *)check->strvalue) == 0){
780 ldap_value_free(vals);
781 ldap_msgfree(result);
783 DEBUG("rlm_ldap::groupcmp: Group %s not found or user not a member",
784 (char *)check->strvalue);
785 ldap_release_conn(conn_id,inst->conns);
790 DEBUG("rlm_ldap::ldap_groupcmp: ldap_get_values() failed");
791 ldap_release_conn(conn_id,inst->conns);
792 ldap_msgfree(result);
800 DEBUG("rlm_ldap::ldap_groupcmp: User found in group %s",(char *)check->strvalue);
801 ldap_release_conn(conn_id,inst->conns);
808 * Do an xlat on an LDAP URL
811 static int ldap_xlat(void *instance, REQUEST *request, char *fmt, char *out, int freespace,
812 RADIUS_ESCAPE_STRING func)
814 char url[MAX_STRING_LEN];
817 ldap_instance *inst = instance;
818 LDAPURLDesc *ldap_url;
819 LDAPMessage *result = NULL;
820 LDAPMessage *msg = NULL;
825 DEBUG("rlm_ldap: - ldap_xlat");
826 if (!radius_xlat(url, sizeof(url), fmt, request, func)) {
827 radlog (L_ERR, "rlm_ldap: Unable to create LDAP URL.\n");
830 if (!ldap_is_ldap_url(url)){
831 radlog (L_ERR, "rlm_ldap: String passed does not look like an LDAP URL.\n");
834 if (ldap_url_parse(url,&ldap_url)){
835 radlog (L_ERR, "rlm_ldap: LDAP URL parse failed.\n");
838 if (ldap_url->lud_attrs == NULL || ldap_url->lud_attrs[0] == NULL || \
839 ( ldap_url->lud_attrs[1] != NULL || ( ! strlen(ldap_url->lud_attrs[0]) || \
840 ! strcmp(ldap_url->lud_attrs[0],"*") ) ) ){
841 radlog (L_ERR, "rlm_ldap: Invalid Attribute(s) request.\n");
842 ldap_free_urldesc(ldap_url);
845 if (ldap_url->lud_host){
846 if (strncmp(inst->server,ldap_url->lud_host,strlen(inst->server)) != 0 || \
847 ldap_url->lud_port != inst->port){
848 DEBUG("rlm_ldap: Requested server/port is not known to this module instance.");
849 ldap_free_urldesc(ldap_url);
853 if ((conn_id = ldap_get_conn(inst->conns,&conn,inst)) == -1){
854 radlog(L_ERR, "rlm_ldap: All ldap connections are in use");
855 ldap_free_urldesc(ldap_url);
858 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){
859 if (res == RLM_MODULE_NOTFOUND){
860 DEBUG("rlm_ldap: Search returned not found");
861 ldap_free_urldesc(ldap_url);
862 ldap_release_conn(conn_id,inst->conns);
865 DEBUG("rlm_ldap: Search returned error");
866 ldap_free_urldesc(ldap_url);
867 ldap_release_conn(conn_id,inst->conns);
870 if ((msg = ldap_first_entry(conn->ld, result)) == NULL){
871 DEBUG("rlm_ldap: ldap_first_entry() failed");
872 ldap_msgfree(result);
873 ldap_free_urldesc(ldap_url);
874 ldap_release_conn(conn_id,inst->conns);
877 if ((vals = ldap_get_values(conn->ld, msg, ldap_url->lud_attrs[0])) != NULL) {
878 ret = strlen(vals[0]);
879 if (ret > freespace){
880 DEBUG("rlm_ldap: Insufficient string space");
881 ldap_free_urldesc(ldap_url);
882 ldap_value_free(vals);
883 ldap_msgfree(result);
884 ldap_release_conn(conn_id,inst->conns);
887 DEBUG("rlm_ldap: Adding attribute %s, value: %s",ldap_url->lud_attrs[0],vals[0]);
888 strncpy(out,vals[0],ret);
889 ldap_value_free(vals);
894 ldap_msgfree(result);
895 ldap_free_urldesc(ldap_url);
896 ldap_release_conn(conn_id,inst->conns);
898 DEBUG("rlm_ldap: - ldap_xlat end");
904 /******************************************************************************
906 * Function: rlm_ldap_authorize
908 * Purpose: Check if user is authorized for remote access
910 ******************************************************************************/
912 ldap_authorize(void *instance, REQUEST * request)
914 LDAPMessage *result = NULL;
915 LDAPMessage *msg = NULL;
916 LDAPMessage *def_msg = NULL;
917 LDAPMessage *def_attr_msg = NULL;
918 LDAPMessage *def_result = NULL;
919 LDAPMessage *def_attr_result = NULL;
920 ldap_instance *inst = instance;
921 char *user_dn = NULL;
922 char filter[MAX_AUTH_QUERY_LEN];
924 VALUE_PAIR *check_tmp;
925 VALUE_PAIR *reply_tmp;
927 VALUE_PAIR **check_pairs, **reply_pairs;
929 VALUE_PAIR *module_fmsg_vp;
930 VALUE_PAIR *user_profile;
931 char module_fmsg[MAX_STRING_LEN];
935 DEBUG("rlm_ldap: - authorize");
937 if (!request->username){
938 radlog(L_AUTH, "rlm_ldap: Attribute \"User-Name\" is required for authentication.\n");
939 return RLM_MODULE_INVALID;
942 check_pairs = &request->config_items;
943 reply_pairs = &request->reply->vps;
946 * Check for valid input, zero length names not permitted
948 if (request->username->strvalue == 0) {
949 radlog(L_ERR, "rlm_ldap: zero length username not permitted\n");
950 return RLM_MODULE_INVALID;
952 DEBUG("rlm_ldap: performing user authorization for %s",
953 request->username->strvalue);
955 if (!radius_xlat(filter, sizeof(filter), inst->filter,
957 radlog (L_ERR, "rlm_ldap: unable to create filter.\n");
958 return RLM_MODULE_INVALID;
961 if (!radius_xlat(basedn, sizeof(basedn), inst->basedn,
963 radlog (L_ERR, "rlm_ldap: unable to create basedn.\n");
964 return RLM_MODULE_INVALID;
967 if ((conn_id = ldap_get_conn(inst->conns,&conn,inst)) == -1){
968 radlog(L_ERR, "rlm_ldap: All ldap connections are in use");
969 return RLM_MODULE_FAIL;
971 if ((res = perform_search(instance, conn, basedn, LDAP_SCOPE_SUBTREE, filter, inst->atts, &result)) != RLM_MODULE_OK) {
972 DEBUG("rlm_ldap: search failed");
973 if (res == RLM_MODULE_NOTFOUND){
974 snprintf(module_fmsg,sizeof(module_fmsg),"rlm_ldap: User not found");
975 module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
976 pairadd(&request->packet->vps, module_fmsg_vp);
978 ldap_release_conn(conn_id,inst->conns);
981 if ((msg = ldap_first_entry(conn->ld, result)) == NULL) {
982 DEBUG("rlm_ldap: ldap_first_entry() failed");
983 ldap_msgfree(result);
984 ldap_release_conn(conn_id,inst->conns);
985 return RLM_MODULE_FAIL;
987 if ((user_dn = ldap_get_dn(conn->ld, msg)) == NULL) {
988 DEBUG("rlm_ldap: ldap_get_dn() failed");
989 ldap_msgfree(result);
990 ldap_release_conn(conn_id,inst->conns);
991 return RLM_MODULE_FAIL;
994 * Adding new attribute containing DN for LDAP object associated with
997 pairadd(&request->packet->vps, pairmake("Ldap-UserDn", user_dn, T_OP_EQ));
998 ldap_memfree(user_dn);
1001 /* Remote access is controled by attribute of the user object */
1002 if (inst->access_attr) {
1003 if ((vals = ldap_get_values(conn->ld, msg, inst->access_attr)) != NULL) {
1004 if (inst->default_allow){
1005 DEBUG("rlm_ldap: checking if remote access for %s is allowed by %s", request->username->strvalue, inst->access_attr);
1006 if (!strncmp(vals[0], "FALSE", 5)) {
1007 DEBUG("rlm_ldap: dialup access disabled");
1008 snprintf(module_fmsg,sizeof(module_fmsg),"rlm_ldap: Access Attribute denies access");
1009 module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
1010 pairadd(&request->packet->vps, module_fmsg_vp);
1011 ldap_msgfree(result);
1012 ldap_value_free(vals);
1013 ldap_release_conn(conn_id,inst->conns);
1014 return RLM_MODULE_USERLOCK;
1016 ldap_value_free(vals);
1019 DEBUG("rlm_ldap: %s attribute exists - access denied by default", inst->access_attr);
1020 snprintf(module_fmsg,sizeof(module_fmsg),"rlm_ldap: Access Attribute denies access");
1021 module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
1022 pairadd(&request->packet->vps, module_fmsg_vp);
1023 ldap_msgfree(result);
1024 ldap_value_free(vals);
1025 ldap_release_conn(conn_id,inst->conns);
1026 return RLM_MODULE_USERLOCK;
1029 if (inst->default_allow){
1030 DEBUG("rlm_ldap: no %s attribute - access denied by default", inst->access_attr);
1031 snprintf(module_fmsg,sizeof(module_fmsg),"rlm_ldap: Access Attribute denies access");
1032 module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
1033 pairadd(&request->packet->vps, module_fmsg_vp);
1034 ldap_msgfree(result);
1035 ldap_release_conn(conn_id,inst->conns);
1036 return RLM_MODULE_USERLOCK;
1040 if (inst->cache_timeout >0)
1041 ldap_enable_cache(conn->ld, inst->cache_timeout, inst->cache_size);
1044 * Check for the default profile entry. If it exists then add the
1045 * attributes it contains in the check and reply pairs
1048 user_profile = pairfind(request->config_items, PW_USER_PROFILE);
1049 if (inst->default_profile || user_profile){
1050 char *profile = inst->default_profile;
1052 strncpy(filter,"(objectclass=radiusprofile)",MAX_AUTH_QUERY_LEN);
1054 profile = user_profile->strvalue;
1055 if (profile && strlen(profile)){
1056 if ((res = perform_search(instance, conn,
1057 profile, LDAP_SCOPE_BASE,
1058 filter, inst->atts, &def_result)) == RLM_MODULE_OK){
1059 if ((def_msg = ldap_first_entry(conn->ld,def_result))){
1060 if ((check_tmp = ldap_pairget(conn->ld,def_msg,inst->check_item_map,check_pairs,1)))
1061 pairadd(check_pairs,check_tmp);
1062 if ((reply_tmp = ldap_pairget(conn->ld,def_msg,inst->reply_item_map,reply_pairs,0)))
1063 pairadd(reply_pairs,reply_tmp);
1065 ldap_msgfree(def_result);
1067 DEBUG("rlm_ldap: default_profile/user-profile search failed");
1072 * Check for the profile attribute. If it exists, we assume that it
1073 * contains the DN of an entry containg a profile for the user. That
1074 * way we can have different general profiles for various user groups
1075 * (students,faculty,staff etc)
1078 if (inst->profile_attr){
1079 if ((vals = ldap_get_values(conn->ld, msg, inst->profile_attr)) != NULL && strlen(vals[0])) {
1080 strncpy(filter,"(objectclass=radiusprofile)",MAX_AUTH_QUERY_LEN);
1081 if (inst->cache_timeout >0)
1082 ldap_enable_cache(conn->ld, inst->cache_timeout, inst->cache_size);
1083 if ((res = perform_search(instance, conn,
1084 vals[0], LDAP_SCOPE_BASE,
1085 filter, inst->atts, &def_attr_result)) == RLM_MODULE_OK){
1086 if ((def_attr_msg = ldap_first_entry(conn->ld,def_attr_result))){
1087 if ((check_tmp = ldap_pairget(conn->ld,def_attr_msg,inst->check_item_map,check_pairs,1)))
1088 pairadd(check_pairs,check_tmp);
1089 if ((reply_tmp = ldap_pairget(conn->ld,def_attr_msg,inst->reply_item_map,reply_pairs,0)))
1090 pairadd(reply_pairs,reply_tmp);
1092 ldap_msgfree(def_attr_result);
1094 DEBUG("rlm_ldap: profile_attribute search failed");
1095 ldap_value_free(vals);
1098 if (inst->cache_timeout >0)
1099 ldap_disable_cache(conn->ld);
1100 if (inst->passwd_attr && strlen(inst->passwd_attr)){
1101 VALUE_PAIR *passwd_item;
1103 if ((passwd_item = pairfind(request->config_items, PW_PASSWORD)) == NULL){
1105 char *passwd_val = NULL;
1108 if ((passwd_vals = ldap_get_values(conn->ld,msg,inst->passwd_attr)) != NULL){
1110 while(passwd_vals[i] != NULL){
1111 if (strlen(passwd_vals[i])){
1112 passwd_val = passwd_vals[i];
1114 if (inst->passwd_hdr && strlen(inst->passwd_hdr)){
1115 passwd_val = strstr(passwd_val,inst->passwd_hdr);
1116 if (passwd_val != NULL)
1117 passwd_val += strlen(inst->passwd_hdr);
1119 DEBUG("rlm_ldap: Password header not found in password %s for user %s", passwd_vals[0],request->username->strvalue);
1122 if ((passwd_item = paircreate(PW_PASSWORD,PW_TYPE_STRING)) == NULL){
1123 radlog(L_ERR|L_CONS, "no memory");
1124 ldap_value_free(passwd_vals);
1125 ldap_msgfree(result);
1126 ldap_release_conn(conn_id,inst->conns);
1127 return RLM_MODULE_FAIL;
1129 passwd_len = strlen(passwd_val);
1130 strncpy(passwd_item->strvalue,passwd_val,MAX_STRING_LEN - 1);
1131 passwd_item->length = (passwd_len > (MAX_STRING_LEN - 1)) ? (MAX_STRING_LEN - 1) : passwd_len;
1132 pairadd(&request->config_items,passwd_item);
1133 DEBUG("rlm_ldap: Added password %s in check items",passwd_item->strvalue);
1138 ldap_value_free(passwd_vals);
1145 DEBUG("rlm_ldap: looking for check items in directory...");
1147 if ((check_tmp = ldap_pairget(conn->ld, msg, inst->check_item_map,check_pairs,1)) != NULL)
1148 pairadd(check_pairs, check_tmp);
1151 DEBUG("rlm_ldap: looking for reply items in directory...");
1154 if ((reply_tmp = ldap_pairget(conn->ld, msg, inst->reply_item_map,reply_pairs,0)) != NULL)
1155 pairadd(reply_pairs, reply_tmp);
1157 if (inst->do_comp && paircmp(request,request->packet->vps,*check_pairs,reply_pairs) != 0){
1158 DEBUG("rlm_ldap: Pairs do not match. Rejecting user.");
1159 snprintf(module_fmsg,sizeof(module_fmsg),"rlm_ldap: Pairs do not match");
1160 module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
1161 pairadd(&request->packet->vps, module_fmsg_vp);
1162 ldap_msgfree(result);
1163 ldap_release_conn(conn_id,inst->conns);
1165 return RLM_MODULE_REJECT;
1169 * Module should default to LDAP authentication if no Auth-Type
1172 if (pairfind(*check_pairs, PW_AUTHTYPE) == NULL)
1173 pairadd(check_pairs, pairmake("Auth-Type", "LDAP", T_OP_EQ));
1176 DEBUG("rlm_ldap: user %s authorized to use remote access",
1177 request->username->strvalue);
1178 ldap_msgfree(result);
1179 ldap_release_conn(conn_id,inst->conns);
1181 return RLM_MODULE_OK;
1184 /*****************************************************************************
1186 * Function: rlm_ldap_authenticate
1188 * Purpose: Check the user's password against ldap database
1190 *****************************************************************************/
1192 ldap_authenticate(void *instance, REQUEST * request)
1195 LDAPMessage *result, *msg;
1196 ldap_instance *inst = instance;
1197 char *user_dn, *attrs[] = {"uid", NULL};
1198 char filter[MAX_AUTH_QUERY_LEN];
1201 VALUE_PAIR *vp_user_dn;
1202 VALUE_PAIR *module_fmsg_vp;
1203 char module_fmsg[MAX_STRING_LEN];
1207 DEBUG("rlm_ldap: - authenticate");
1209 * Ensure that we're being passed a plain-text password, and not
1213 if (!request->username) {
1214 radlog(L_AUTH, "rlm_ldap: Attribute \"User-Name\" is required for authentication.\n");
1215 return RLM_MODULE_INVALID;
1218 if (!request->password){
1219 radlog(L_AUTH, "rlm_ldap: Attribute \"User-Password\" is required for authentication.");
1220 return RLM_MODULE_INVALID;
1223 if(request->password->attribute != PW_PASSWORD) {
1224 radlog(L_AUTH, "rlm_ldap: Attribute \"User-Password\" is required for authentication. Cannot use \"%s\".", request->password->name);
1225 return RLM_MODULE_INVALID;
1228 if (request->password->length == 0) {
1229 radlog(L_ERR, "rlm_ldap: empty password supplied");
1230 return RLM_MODULE_INVALID;
1233 DEBUG("rlm_ldap: login attempt by \"%s\" with password \"%s\"",
1234 request->username->strvalue, request->password->strvalue);
1236 while((vp_user_dn = pairfind(request->packet->vps, PW_LDAP_USERDN)) == NULL) {
1237 if (!radius_xlat(filter, sizeof(filter), inst->filter,
1239 radlog (L_ERR, "rlm_ldap: unable to create filter.\n");
1240 return RLM_MODULE_INVALID;
1243 if (!radius_xlat(basedn, sizeof(basedn), inst->basedn,
1245 radlog (L_ERR, "rlm_ldap: unable to create basedn.\n");
1246 return RLM_MODULE_INVALID;
1249 if ((conn_id = ldap_get_conn(inst->conns,&conn,inst)) == -1){
1250 radlog(L_ERR, "rlm_ldap: All ldap connections are in use");
1251 return RLM_MODULE_FAIL;
1253 if ((res = perform_search(instance, conn, basedn, LDAP_SCOPE_SUBTREE, filter, attrs, &result)) != RLM_MODULE_OK) {
1254 if (res == RLM_MODULE_NOTFOUND){
1255 snprintf(module_fmsg,sizeof(module_fmsg),"rlm_ldap: User not found");
1256 module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
1257 pairadd(&request->packet->vps, module_fmsg_vp);
1259 ldap_release_conn(conn_id,inst->conns);
1262 if ((msg = ldap_first_entry(conn->ld, result)) == NULL) {
1263 ldap_msgfree(result);
1264 ldap_release_conn(conn_id,inst->conns);
1265 return RLM_MODULE_FAIL;
1267 if ((user_dn = ldap_get_dn(conn->ld, msg)) == NULL) {
1268 DEBUG("rlm_ldap: ldap_get_dn() failed");
1269 ldap_msgfree(result);
1270 ldap_release_conn(conn_id,inst->conns);
1271 return RLM_MODULE_FAIL;
1273 ldap_release_conn(conn_id,inst->conns);
1274 pairadd(&request->packet->vps, pairmake("Ldap-UserDn", user_dn, T_OP_EQ));
1275 ldap_memfree(user_dn);
1276 ldap_msgfree(result);
1279 user_dn = vp_user_dn->strvalue;
1281 DEBUG("rlm_ldap: user DN: %s", user_dn);
1283 ld_user = ldap_connect(instance, user_dn, request->password->strvalue,
1285 if (ld_user == NULL){
1286 snprintf(module_fmsg,sizeof(module_fmsg),"rlm_ldap: Bind as user failed");
1287 module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
1288 pairadd(&request->packet->vps, module_fmsg_vp);
1292 DEBUG("rlm_ldap: user %s authenticated succesfully",
1293 request->username->strvalue);
1294 ldap_unbind_s(ld_user);
1296 return RLM_MODULE_OK;
1300 ldap_connect(void *instance, const char *dn, const char *password, int auth, int *result)
1302 ldap_instance *inst = instance;
1304 int msgid, rc, ldap_version;
1308 DEBUG("rlm_ldap: (re)connect to %s:%d, authentication %d", inst->server, inst->port, auth);
1309 if ((ld = ldap_init(inst->server, inst->port)) == NULL) {
1310 radlog(L_ERR, "rlm_ldap: ldap_init() failed");
1311 *result = RLM_MODULE_FAIL;
1314 if (ldap_set_option(ld, LDAP_OPT_NETWORK_TIMEOUT, (void *) &(inst->net_timeout)) != LDAP_OPT_SUCCESS) {
1315 radlog(L_ERR, "rlm_ldap: Could not set LDAP_OPT_NETWORK_TIMEOUT %ld.%ld", inst->net_timeout.tv_sec, inst->net_timeout.tv_usec);
1317 if (ldap_set_option(ld, LDAP_OPT_TIMELIMIT, (void *) &(inst->timelimit)) != LDAP_OPT_SUCCESS) {
1318 radlog(L_ERR, "rlm_ldap: Could not set LDAP_OPT_TIMELIMIT %d", inst->timelimit);
1320 if (inst->ldap_debug && ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &(inst->ldap_debug)) != LDAP_OPT_SUCCESS) {
1321 radlog(L_ERR, "rlm_ldap: Could not set LDAP_OPT_DEBUG_LEVEL %d", inst->ldap_debug);
1323 #ifdef HAVE_LDAP_START_TLS
1324 if(inst->tls_mode) {
1325 DEBUG("rlm_ldap: setting TLS mode to %d", inst->tls_mode);
1326 if(ldap_set_option(ld, LDAP_OPT_X_TLS,
1327 (void *) &(inst->tls_mode)) != LDAP_OPT_SUCCESS) {
1328 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
1329 radlog(L_ERR, "rlm_ldap: could not set LDAP_OPT_X_TLS option %s", ldap_err2string(ldap_errno));
1333 if (inst->start_tls) {
1334 DEBUG("rlm_ldap: starting TLS");
1335 ldap_version = LDAP_VERSION3;
1336 if (ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &ldap_version) == LDAP_SUCCESS) {
1337 rc = ldap_start_tls_s(ld, NULL, NULL);
1338 if (rc != LDAP_SUCCESS) {
1339 DEBUG("rlm_ldap: ldap_start_tls_s()");
1340 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
1341 radlog(L_ERR, "rlm_ldap: could not start TLS %s", ldap_err2string(ldap_errno));
1342 *result = RLM_MODULE_FAIL;
1348 #endif /* HAVE_LDAP_START_TLS */
1350 DEBUG("rlm_ldap: bind as %s/%s to %s:%d", dn, password, inst->server, inst->port);
1351 msgid = ldap_bind(ld, dn, password,LDAP_AUTH_SIMPLE);
1353 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
1354 radlog(L_ERR, "rlm_ldap: %s bind to %s:%d failed: %s",
1355 dn, inst->server, inst->port,
1356 ldap_err2string(ldap_errno));
1357 *result = RLM_MODULE_FAIL;
1361 DEBUG("rlm_ldap: waiting for bind result ...");
1363 rc = ldap_result(ld, msgid, 1, &(inst->timeout), &res);
1366 DEBUG("rlm_ldap: ldap_result()");
1367 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
1368 radlog(L_ERR, "rlm_ldap: %s bind to %s:%d failed: %s",
1369 dn, inst->server, inst->port,
1370 (rc == 0) ? "timeout" : ldap_err2string(ldap_errno));
1371 *result = RLM_MODULE_FAIL;
1375 ldap_errno = ldap_result2error(ld, res, 1);
1376 switch (ldap_errno) {
1378 *result = RLM_MODULE_OK;
1381 case LDAP_INVALID_CREDENTIALS:
1383 *result = RLM_MODULE_REJECT;
1385 radlog(L_ERR, "rlm_ldap: LDAP login failed: check login, password settings in ldap section of radiusd.conf");
1386 *result = RLM_MODULE_FAIL;
1391 radlog(L_ERR,"rlm_ldap: %s bind to %s:%d failed %s",
1392 dn, inst->server, inst->port,
1393 ldap_err2string(ldap_errno));
1394 *result = RLM_MODULE_FAIL;
1397 if (*result != RLM_MODULE_OK) {
1404 /*****************************************************************************
1406 * Detach from the LDAP server and cleanup internal state.
1408 *****************************************************************************/
1410 ldap_detach(void *instance)
1412 ldap_instance *inst = instance;
1413 TLDAP_RADIUS *pair, *nextpair;
1416 free((char *) inst->server);
1418 free((char *) inst->login);
1420 free((char *) inst->password);
1422 free((char *) inst->basedn);
1423 if (inst->dictionary_mapping)
1424 free(inst->dictionary_mapping);
1426 free((char *) inst->filter);
1427 if (inst->passwd_hdr)
1428 free((char *) inst->passwd_hdr);
1429 if (inst->passwd_attr)
1430 free((char *) inst->passwd_attr);
1431 if (inst->groupname_attr)
1432 free((char *) inst->groupname_attr);
1433 if (inst->groupmemb_filt)
1434 free((char *) inst->groupmemb_filt);
1435 if (inst->groupmemb_attr)
1436 free((char *) inst->groupmemb_attr);
1440 for(;i<inst->num_conns;i++){
1441 if (inst->conns[i].ld){
1442 if (inst->cache_timeout >0)
1443 ldap_destroy_cache(inst->conns[i].ld);
1444 ldap_unbind_s(inst->conns[i].ld);
1446 pthread_mutex_destroy(&inst->conns[i].mutex);
1451 pair = inst->check_item_map;
1453 while (pair != NULL) {
1454 nextpair = pair->next;
1456 free(pair->radius_attr);
1461 pair = inst->reply_item_map;
1463 while (pair != NULL) {
1464 nextpair = pair->next;
1466 free(pair->radius_attr);
1474 while(inst->atts[i])
1475 free(inst->atts[i++]);
1479 paircompare_unregister(PW_LDAP_GROUP, ldap_groupcmp);
1480 xlat_unregister(inst->xlat_name,ldap_xlat);
1481 free(inst->xlat_name);
1490 fieldcpy(char *string, char **uptr)
1495 while (*ptr == ' ' || *ptr == '\t') {
1500 while (*ptr != '"' && *ptr != '\0' && *ptr != '\n') {
1510 while (*ptr != ' ' && *ptr != '\t' && *ptr != '\0' && *ptr != '\n' &&
1511 *ptr != '=' && *ptr != ',') {
1519 /*****************************************************************************
1520 * Get RADIUS attributes from LDAP object
1521 * ( according to draft-adoba-radius-05.txt
1522 * <http://www.ietf.org/internet-drafts/draft-adoba-radius-05.txt> )
1524 *****************************************************************************/
1527 ldap_pairget(LDAP * ld, LDAPMessage * entry,
1528 TLDAP_RADIUS * item_map, VALUE_PAIR **pairs,char is_check)
1534 TLDAP_RADIUS *element;
1536 int is_generic_attribute;
1538 VALUE_PAIR *pairlist = NULL;
1539 VALUE_PAIR *newpair = NULL;
1541 /* check if there is a mapping from this LDAP attribute to a RADIUS attribute */
1542 for (element = item_map; element != NULL; element = element->next) {
1543 if ((vals = ldap_get_values(ld,entry,element->attr)) != NULL){
1544 /* check whether this is a one-to-one-mapped ldap attribute or a generic
1545 attribute and set flag accordingly */
1547 if (strcasecmp(element->radius_attr, GENERIC_ATTRIBUTE_ID)==0)
1548 is_generic_attribute = 1;
1550 is_generic_attribute = 0;
1552 /* find out how many values there are for the attribute and extract all of them */
1554 vals_count = ldap_count_values(vals);
1556 for (vals_idx = 0; vals_idx < vals_count; vals_idx++) {
1557 ptr = vals[vals_idx];
1559 if (is_generic_attribute) {
1560 /* this is a generic attribute */
1561 LRAD_TOKEN dummy; /* makes pairread happy */
1563 /* not sure if using pairread here is ok ... */
1564 if ( (newpair = pairread(&ptr, &dummy)) != NULL) {
1565 DEBUG("rlm_ldap: extracted attribute %s from generic item %s",
1566 newpair->name, vals[vals_idx]);
1567 pairadd(&pairlist, newpair);
1569 radlog(L_ERR, "rlm_ldap: parsing %s failed: %s",
1570 element->attr, vals[vals_idx]);
1573 /* this is a one-to-one-mapped attribute */
1574 token = gettoken(&ptr, value, sizeof(value) - 1);
1575 if (token < T_EQSTART || token > T_EQEND) {
1576 token = (is_check) ? T_OP_CMP_EQ : T_OP_EQ;
1578 gettoken(&ptr, value, sizeof(value) - 1);
1580 if (value[0] == 0) {
1581 DEBUG("rlm_ldap: Attribute %s has no value", element->attr);
1584 DEBUG("rlm_ldap: Adding %s as %s, value %s & op=%d", element->attr, element->radius_attr, value, token);
1585 if ((newpair = pairmake(element->radius_attr, value, token)) == NULL)
1588 pairdelete(pairs,newpair->attribute);
1590 pairadd(&pairlist, newpair);
1593 ldap_value_free(vals);
1600 /* globally exported name */
1601 module_t rlm_ldap = {
1603 RLM_TYPE_THREAD_SAFE, /* type: reserved */
1604 NULL, /* initialization */
1605 ldap_instantiate, /* instantiation */
1607 ldap_authenticate, /* authentication */
1608 ldap_authorize, /* authorization */
1609 NULL, /* preaccounting */
1610 NULL, /* accounting */
1611 NULL, /* checksimul */
1612 NULL, /* pre-proxy */
1613 NULL, /* post-proxy */
1614 NULL /* post-auth */
1616 ldap_detach, /* detach */