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>
97 * - Only add a failure message for bind as user failed in ldap_authenticate if the result of ldap_connect was
99 * - Make tls_mode a configurable option. Patch from John <jhogenmiller@pennswoods.net>
100 * - Allow multiple regular profiles for an entry
101 * Oct 2002, Kostas Kalevras <kkalev@noc.ntua.gr>
102 * - Disable cache after searching for the default profile
103 * - Use the MAX_FAILED_CONNS_* in ldap_authenticate() when calling ldap_connect()
104 * Nov 2002, Kostas Kalevras <kkalev@noc.ntua.gr>
105 * - Set LDAP version to V3 before binding. Now freeradius should work with openldap21
106 * Dec 2002, Kostas Kalevras <kkalev@noc.ntua.gr>
107 * - Set default values for the server and basedn parameters
108 * Feb 2003, Kostas Kalevras <kkalev@noc.ntua.gr>
109 * - Add support for ldap_initialize. That way we can specify the server as an ldap url.
110 * Based on ideas from Derrik Pates <dpates@dsdk12.net>
111 * Mar 2003, Kostas Kalevras <kkalev@noc.ntua.gr>
112 * - Add an ldap_escape_func. Escape the * character from the filter so that we can avoid
113 * the trivial DoS of username=*
114 * - Remove the caching code. It does not exist in openldap21.
115 * Based on a report from Mike Denka <mdenk@whidbey.net>
116 * May 2003, Kostas Kalevras <kkalev@noc.ntua.gr>
117 * - Don't do a double free on the attribute maps. Bug noted by Derrik Pates <dpates@dsdk12.net>
118 * - Apply a patch from Alexander M. Pravking <fduch@antar.bryansk.ru> to do an xlat on the
119 * retrieved attributes.
121 static const char rcsid[] = "$Id$";
123 #include "autoconf.h"
125 #include <sys/types.h>
126 #include <sys/socket.h>
127 #include <sys/time.h>
128 #include <netinet/in.h>
145 #include "libradius.h"
147 #include "conffile.h"
149 #include "rad_assert.h"
151 #define MAX_FILTER_STR_LEN 1024
155 * These are used in case ldap_search returns LDAP_SERVER_DOWN
156 * In that case we do conn->failed_conns++ and then check it:
157 * If conn->failed_conns <= MAX_FAILED_CONNS_START then we try
159 * conn->failed_conns is also checked on entrance in perform_search:
160 * If conn->failed_conns > MAX_FAILED_CONNS_START then we don't
161 * try to do anything and we just do conn->failed_conns++ and
162 * return RLM_MODULE_FAIL
163 * if conn->failed_conns >= MAX_FAILED_CONNS_END then we give it
164 * another chance and we set it to MAX_FAILED_CONNS_RESTART and
168 * We are assuming that the majority of the LDAP_SERVER_DOWN cases
169 * will either be an ldap connection timeout or a temporary ldap
171 * As a result we make a few attempts to reconnect hoping that the problem
172 * will soon go away. If it does not go away then we just return
173 * RLM_MODULE_FAIL on entrance in perform_search until conn->failed_conns
174 * gets to MAX_FAILED_CONNS_END. After that we give it one more chance by
175 * going back to MAX_FAILED_CONNS_RESTART
179 #define MAX_FAILED_CONNS_END 20
180 #define MAX_FAILED_CONNS_RESTART 4
181 #define MAX_FAILED_CONNS_START 5
183 /* linked list of mappings between RADIUS attributes and LDAP attributes */
184 struct TLDAP_RADIUS {
187 struct TLDAP_RADIUS* next;
189 typedef struct TLDAP_RADIUS TLDAP_RADIUS;
191 typedef struct ldap_conn {
196 pthread_mutex_t mutex;
203 struct timeval net_timeout;
204 struct timeval timeout;
217 char *default_profile;
222 char *dictionary_mapping;
223 char *groupname_attr;
224 char *groupmemb_filt;
225 char *groupmemb_attr;
227 TLDAP_RADIUS *check_item_map;
228 TLDAP_RADIUS *reply_item_map;
230 int ldap_debug; /* Debug flag for LDAP SDK */
231 char *xlat_name; /* name used to xlat */
234 static CONF_PARSER module_config[] = {
235 {"server", PW_TYPE_STRING_PTR, offsetof(ldap_instance,server), NULL, "localhost"},
236 {"port", PW_TYPE_INTEGER, offsetof(ldap_instance,port), NULL, "389"},
237 /* wait forever on network activity */
238 {"net_timeout", PW_TYPE_INTEGER, offsetof(ldap_instance,net_timeout.tv_sec), NULL, "10"},
239 /* wait forever for search results */
240 {"timeout", PW_TYPE_INTEGER, offsetof(ldap_instance,timeout.tv_sec), NULL, "20"},
241 /* allow server unlimited time for search (server-side limit) */
242 {"timelimit", PW_TYPE_INTEGER, offsetof(ldap_instance,timelimit), NULL, "20"},
243 {"identity", PW_TYPE_STRING_PTR, offsetof(ldap_instance,login), NULL, ""},
244 {"start_tls", PW_TYPE_BOOLEAN, offsetof(ldap_instance,start_tls), NULL, "no"},
245 {"password", PW_TYPE_STRING_PTR, offsetof(ldap_instance,password), NULL, ""},
246 {"basedn", PW_TYPE_STRING_PTR, offsetof(ldap_instance,basedn), NULL, "o=notexist"},
247 {"filter", PW_TYPE_STRING_PTR, offsetof(ldap_instance,filter), NULL, "(uid=%u)"},
248 {"default_profile", PW_TYPE_STRING_PTR, offsetof(ldap_instance,default_profile), NULL, NULL},
249 {"profile_attribute", PW_TYPE_STRING_PTR, offsetof(ldap_instance,profile_attr), NULL, NULL},
250 {"password_header", PW_TYPE_STRING_PTR, offsetof(ldap_instance,passwd_hdr), NULL, NULL},
251 {"password_attribute", PW_TYPE_STRING_PTR, offsetof(ldap_instance,passwd_attr), NULL, NULL},
252 /* LDAP attribute name that controls remote access */
253 {"access_attr", PW_TYPE_STRING_PTR, offsetof(ldap_instance,access_attr), NULL, NULL},
254 /* file with mapping between LDAP and RADIUS attributes */
255 {"groupname_attribute", PW_TYPE_STRING_PTR, offsetof(ldap_instance,groupname_attr), NULL, "cn"},
256 {"groupmembership_filter", PW_TYPE_STRING_PTR, offsetof(ldap_instance,groupmemb_filt), NULL, "(|(&(objectClass=GroupOfNames)(member=%{Ldap-UserDn}))(&(objectClass=GroupOfUniqueNames)(uniquemember=%{Ldap-UserDn})))"},
257 {"groupmembership_attribute", PW_TYPE_STRING_PTR, offsetof(ldap_instance,groupmemb_attr), NULL, NULL},
258 {"dictionary_mapping", PW_TYPE_STRING_PTR, offsetof(ldap_instance,dictionary_mapping), NULL, "${confdir}/ldap.attrmap"},
259 {"ldap_debug", PW_TYPE_INTEGER, offsetof(ldap_instance,ldap_debug), NULL, "0x0000"},
260 {"ldap_connections_number", PW_TYPE_INTEGER, offsetof(ldap_instance,num_conns), NULL, "5"},
261 {"compare_check_items", PW_TYPE_BOOLEAN, offsetof(ldap_instance,do_comp), NULL, "no"},
262 {"access_attr_used_for_allow", PW_TYPE_BOOLEAN, offsetof(ldap_instance,default_allow), NULL, "yes"},
264 {NULL, -1, 0, NULL, NULL}
267 #define ld_valid ld_options.ldo_valid
268 #define LDAP_VALID_SESSION 0x2
269 #define LDAP_VALID(ld) ( (ld)->ld_valid == LDAP_VALID_SESSION )
272 static void fieldcpy(char *, char **);
274 static VALUE_PAIR *ldap_pairget(LDAP *, LDAPMessage *, TLDAP_RADIUS *,VALUE_PAIR **,char);
275 static int ldap_groupcmp(void *, REQUEST *, VALUE_PAIR *, VALUE_PAIR *, VALUE_PAIR *, VALUE_PAIR **);
276 static int ldap_xlat(void *,REQUEST *, char *, char *,int, RADIUS_ESCAPE_STRING);
277 static LDAP *ldap_connect(void *instance, const char *, const char *, int, int *);
278 static int read_mappings(ldap_instance* inst);
280 static inline int ldap_get_conn(LDAP_CONN *conns,LDAP_CONN **ret,void *instance)
282 ldap_instance *inst = instance;
285 for(;i<inst->num_conns;i++){
286 if (conns[i].locked == 0 && pthread_mutex_trylock(&(conns[i].mutex)) == 0){
289 DEBUG("ldap_get_conn: Got Id: %d",i);
297 static inline void ldap_release_conn(int i, LDAP_CONN *conns)
299 DEBUG("ldap_release_conn: Release Id: %d",i);
301 pthread_mutex_unlock(&(conns[i].mutex));
304 /*************************************************************************
306 * Function: rlm_ldap_instantiate
308 * Purpose: Uses section of radiusd config file passed as parameter
309 * to create an instance of the module.
311 *************************************************************************/
313 ldap_instantiate(CONF_SECTION * conf, void **instance)
318 int reply_map_num = 0;
319 int check_map_num = 0;
320 int att_map[3] = {0,0,0};
324 inst = rad_malloc(sizeof *inst);
328 memset(inst, 0, sizeof(*inst));
330 if (cf_section_parse(conf, inst, module_config) < 0) {
335 if (inst->server == NULL) {
336 radlog(L_ERR, "rlm_ldap: missing 'server' directive.");
341 #ifdef HAVE_LDAP_INITIALIZE
342 if (ldap_is_ldap_url(inst->server)){
348 inst->timeout.tv_usec = 0;
349 inst->net_timeout.tv_usec = 0;
350 /* workaround for servers which support LDAPS but not START TLS */
351 if(inst->port == LDAPS_PORT)
352 inst->tls_mode = LDAP_OPT_X_TLS_HARD;
355 inst->reply_item_map = NULL;
356 inst->check_item_map = NULL;
358 inst->failed_conns = 0;
360 paircompare_register(PW_LDAP_GROUP, PW_USER_NAME, ldap_groupcmp, inst);
361 DEBUG("conns: %p",inst->conns);
363 xlat_name = cf_section_name2(conf);
364 if (xlat_name == NULL) {
365 xlat_name = cf_section_name1(conf);
366 rad_assert(xlat_name != NULL); /* or all hell breaks loose */
368 inst->xlat_name = strdup(xlat_name);
369 xlat_register(xlat_name,ldap_xlat,inst);
371 if (inst->num_conns <= 0){
372 radlog(L_ERR, "rlm_ldap: Invalid ldap connections number passed.");
376 inst->conns = (LDAP_CONN *)malloc(sizeof(LDAP_CONN)*inst->num_conns);
377 if (inst->conns == NULL){
378 radlog(L_ERR, "rlm_ldap: Could not allocate memory. Aborting.");
382 for(;i<inst->num_conns;i++){
383 inst->conns[i].bound = 0;
384 inst->conns[i].locked = 0;
385 inst->conns[i].failed_conns = 0;
386 inst->conns[i].ld = NULL;
387 pthread_mutex_init(&inst->conns[i].mutex, NULL);
390 if (read_mappings(inst) != 0) {
391 radlog(L_ERR, "rlm_ldap: Reading dictionary mappings from file %s failed",
392 inst->dictionary_mapping);
393 radlog(L_ERR, "rlm_ldap: Proceeding with no mappings");
396 pair = inst->check_item_map;
401 check_map_num = (atts_num - 1);
402 pair = inst->reply_item_map;
407 reply_map_num = (atts_num - 1);
408 if (inst->profile_attr)
410 if (inst->passwd_attr)
412 if (inst->access_attr)
414 inst->atts = (char **)malloc(sizeof(char *)*(atts_num + 1));
415 if (inst->atts == NULL){
416 radlog(L_ERR, "rlm_ldap: Could not allocate memory. Aborting.");
420 pair = inst->check_item_map;
421 for(i=0;i<atts_num;i++){
422 if (i <= check_map_num ){
423 inst->atts[i] = pair->attr;
424 if (i == check_map_num)
425 pair = inst->reply_item_map;
429 else if (i <= reply_map_num){
430 inst->atts[i] = pair->attr;
434 if (inst->profile_attr && !att_map[0]){
435 inst->atts[i] = inst->profile_attr;
438 else if (inst->passwd_attr && !att_map[1]){
439 inst->atts[i] = inst->passwd_attr;
442 else if (inst->access_attr && !att_map[2]){
443 inst->atts[i] = inst->access_attr;
448 inst->atts[atts_num] = NULL;
450 DEBUG("conns: %p",inst->conns);
460 * read_mappings(...) reads a ldap<->radius mappings file to inst->reply_item_map and inst->check_item_map
463 #define MAX_LINE_LEN 160
464 #define GENERIC_ATTRIBUTE_ID "$GENERIC$"
467 read_mappings(ldap_instance* inst)
471 /* all buffers are of MAX_LINE_LEN so we can use sscanf without being afraid of buffer overflows */
472 char buf[MAX_LINE_LEN], itemType[MAX_LINE_LEN], radiusAttribute[MAX_LINE_LEN], ldapAttribute[MAX_LINE_LEN];
475 /* open the mappings file for reading */
477 filename = inst->dictionary_mapping;
478 DEBUG("rlm_ldap: reading ldap<->radius mappings from file %s", filename);
479 mapfile = fopen(filename, "r");
481 if (mapfile == NULL) {
482 radlog(L_ERR, "rlm_ldap: Opening file %s failed", filename);
483 return -1; /* error */
486 /* read file line by line. Note that if line length exceed MAX_LINE_LEN, line numbers will be mixed up */
490 while (fgets(buf, sizeof buf, mapfile)!=NULL) {
498 ptr = strchr(buf, '#');
502 if (buf[0] == 0) continue;
504 /* extract tokens from the string */
505 token_count = sscanf(buf, "%s %s %s", itemType, radiusAttribute, ldapAttribute);
507 if (token_count <= 0) /* no tokens */
510 if (token_count != 3) {
511 radlog(L_ERR, "rlm_ldap: Skipping %s line %i: %s", filename, linenumber, buf);
512 radlog(L_ERR, "rlm_ldap: Expected 3 tokens "
513 "(Item type, RADIUS Attribute and LDAP Attribute) but found only %i", token_count);
517 /* create new TLDAP_RADIUS list node */
518 pair = rad_malloc(sizeof(TLDAP_RADIUS));
520 pair->attr = strdup(ldapAttribute);
521 pair->radius_attr = strdup(radiusAttribute);
523 if ( (pair->attr == NULL) || (pair->radius_attr == NULL) ) {
524 radlog(L_ERR, "rlm_ldap: Out of memory");
525 if (pair->attr) free(pair->attr);
526 if (pair->radius_attr) free(pair->radius_attr);
532 /* push node to correct list */
533 if (strcasecmp(itemType, "checkItem") == 0) {
534 pair->next = inst->check_item_map;
535 inst->check_item_map = pair;
536 } else if (strcasecmp(itemType, "replyItem") == 0) {
537 pair->next = inst->reply_item_map;
538 inst->reply_item_map = pair;
540 radlog(L_ERR, "rlm_ldap: file %s: skipping line %i: unknown itemType %s",
541 filename, linenumber, itemType);
543 free(pair->radius_attr);
548 DEBUG("rlm_ldap: LDAP %s mapped to RADIUS %s",
549 pair->attr, pair->radius_attr);
554 return 0; /* success */
558 perform_search(void *instance, LDAP_CONN *conn, char *search_basedn, int scope, char *filter,
559 char **attrs, LDAPMessage ** result)
561 int res = RLM_MODULE_OK;
563 ldap_instance *inst = instance;
564 int search_retry = 0;
569 radlog(L_ERR, "rlm_ldap: NULL connection handle passed");
570 return RLM_MODULE_FAIL;
572 if (conn->failed_conns > MAX_FAILED_CONNS_START){
573 conn->failed_conns++;
574 if (conn->failed_conns >= MAX_FAILED_CONNS_END){
575 conn->failed_conns = MAX_FAILED_CONNS_RESTART;
580 if (!conn->bound || conn->ld == NULL) {
581 DEBUG2("rlm_ldap: attempting LDAP reconnection");
583 DEBUG2("rlm_ldap: closing existing LDAP connection");
584 ldap_unbind_s(conn->ld);
586 if ((conn->ld = ldap_connect(instance, inst->login, inst->password, 0, &res)) == NULL) {
587 radlog(L_ERR, "rlm_ldap: (re)connection attempt failed");
588 if (search_retry == 0)
589 conn->failed_conns++;
590 return (RLM_MODULE_FAIL);
593 conn->failed_conns = 0;
595 DEBUG2("rlm_ldap: performing search in %s, with filter %s", search_basedn ? search_basedn : "(null)" , filter);
596 switch (ldap_search_st(conn->ld, search_basedn, scope, filter, attrs, 0, &(inst->timeout), result)) {
598 case LDAP_NO_SUCH_OBJECT:
600 case LDAP_SERVER_DOWN:
601 radlog(L_ERR, "rlm_ldap: ldap_search() failed: LDAP connection lost.");
602 conn->failed_conns++;
603 if (search_retry == 0){
604 if (conn->failed_conns <= MAX_FAILED_CONNS_START){
605 radlog(L_INFO, "rlm_ldap: Attempting reconnect");
608 ldap_msgfree(*result);
612 ldap_msgfree(*result);
613 return RLM_MODULE_FAIL;
614 case LDAP_INSUFFICIENT_ACCESS:
615 radlog(L_ERR, "rlm_ldap: ldap_search() failed: Insufficient access. Check the identity and password configuration directives.");
616 ldap_msgfree(*result);
617 return RLM_MODULE_FAIL;
619 radlog(L_ERR, "rlm_ldap: ldap_search() failed: Timed out while waiting for server to respond. Please increase the timeout.");
620 ldap_msgfree(*result);
621 return RLM_MODULE_FAIL;
622 case LDAP_TIMELIMIT_EXCEEDED:
624 case LDAP_UNAVAILABLE:
625 /* We don't need to reconnect in these cases so we don't set conn->bound */
626 ldap_get_option(conn->ld, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
627 radlog(L_ERR, "rlm_ldap: ldap_search() failed: %s", ldap_err2string(ldap_errno));
628 ldap_msgfree(*result);
629 return (RLM_MODULE_FAIL);
631 ldap_get_option(conn->ld, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
632 radlog(L_ERR, "rlm_ldap: ldap_search() failed: %s", ldap_err2string(ldap_errno));
634 ldap_msgfree(*result);
635 return (RLM_MODULE_FAIL);
638 if ((ldap_count_entries(conn->ld, *result)) != 1) {
639 DEBUG("rlm_ldap: object not found or got ambiguous search result");
640 res = RLM_MODULE_NOTFOUND;
641 ldap_msgfree(*result);
648 * Translate the LDAP queries.
650 static int ldap_escape_func(char *out, int outlen, const char *in)
656 * Only one byte left.
662 if (strchr("*", *in)) {
669 * Else it's a nice character.
682 * ldap_groupcmp(). Implement the Ldap-Group == "group" filter
685 static int ldap_groupcmp(void *instance, REQUEST *req, VALUE_PAIR *request, VALUE_PAIR *check,
686 VALUE_PAIR *check_pairs, VALUE_PAIR **reply_pairs)
688 char filter[MAX_FILTER_STR_LEN];
689 char gr_filter[MAX_FILTER_STR_LEN];
691 LDAPMessage *result = NULL;
692 LDAPMessage *msg = NULL;
693 char basedn[MAX_FILTER_STR_LEN];
694 char *attrs[] = {"dn",NULL};
696 ldap_instance *inst = instance;
697 char *group_attrs[] = {inst->groupmemb_attr,NULL};
700 VALUE_PAIR *vp_user_dn;
701 VALUE_PAIR **request_pairs;
703 request_pairs = &req->packet->vps;
705 DEBUG("rlm_ldap: Entering ldap_groupcmp()");
707 if (check->strvalue == NULL || check->length == 0){
708 DEBUG("rlm_ldap::ldap_groupcmp: Illegal group name");
713 DEBUG("rlm_ldap::ldap_groupcmp: NULL request");
717 if (!radius_xlat(basedn, sizeof(basedn), inst->basedn, req, NULL)) {
718 DEBUG("rlm_ldap::ldap_groupcmp: unable to create basedn.");
722 while((vp_user_dn = pairfind(*request_pairs, PW_LDAP_USERDN)) == NULL){
723 char *user_dn = NULL;
725 if (!radius_xlat(filter, sizeof(filter), inst->filter,
726 req, ldap_escape_func)){
727 DEBUG("rlm_ldap::ldap_groupcmp: unable to create filter");
730 if ((conn_id = ldap_get_conn(inst->conns,&conn,inst)) == -1){
731 radlog(L_ERR, "rlm_ldap: All ldap connections are in use");
734 if ((res = perform_search(inst, conn, basedn, LDAP_SCOPE_SUBTREE,
735 filter, attrs, &result)) != RLM_MODULE_OK){
736 DEBUG("rlm_ldap::ldap_groupcmp: search failed");
737 ldap_release_conn(conn_id,inst->conns);
740 if ((msg = ldap_first_entry(conn->ld, result)) == NULL) {
741 DEBUG("rlm_ldap::ldap_groupcmp: ldap_first_entry() failed");
742 ldap_release_conn(conn_id,inst->conns);
743 ldap_msgfree(result);
746 if ((user_dn = ldap_get_dn(conn->ld, msg)) == NULL) {
747 DEBUG("rlm_ldap:ldap_groupcmp:: ldap_get_dn() failed");
748 ldap_release_conn(conn_id,inst->conns);
749 ldap_msgfree(result);
752 ldap_release_conn(conn_id,inst->conns);
754 * Adding new attribute containing DN for LDAP object associated with
757 pairadd(request_pairs, pairmake("Ldap-UserDn", user_dn, T_OP_EQ));
758 ldap_memfree(user_dn);
759 ldap_msgfree(result);
762 if(!radius_xlat(gr_filter, sizeof(gr_filter), inst->groupmemb_filt, req, NULL)){
763 DEBUG("rlm_ldap::ldap_groupcmp: unable to create filter.");
767 if (strchr((char *)check->strvalue,',') != NULL) {
768 /* This looks like a DN */
769 snprintf(filter,sizeof(filter), "%s",gr_filter);
770 snprintf(basedn,sizeof(basedn), "%s",(char *)check->strvalue);
772 snprintf(filter,sizeof(filter), "(&(%s=%s)%s)",inst->groupname_attr,(char *)check->strvalue,gr_filter);
774 if ((conn_id = ldap_get_conn(inst->conns,&conn,inst)) == -1){
775 radlog(L_ERR, "rlm_ldap: All ldap connections are in use");
779 if ((res = perform_search(inst, conn, basedn, LDAP_SCOPE_SUBTREE,
780 filter, attrs, &result)) == RLM_MODULE_OK){
781 DEBUG("rlm_ldap::ldap_groupcmp: User found in group %s",
782 (char *)check->strvalue);
783 ldap_msgfree(result);
784 ldap_release_conn(conn_id,inst->conns);
788 ldap_release_conn(conn_id,inst->conns);
790 if (res != RLM_MODULE_NOTFOUND ) {
791 DEBUG("rlm_ldap::ldap_groupcmp: Search returned error");
795 if (inst->groupmemb_attr == NULL){
796 /* search returned NOTFOUND and searching for membership
797 * using user object attributes is not specified in config
800 DEBUG("rlm_ldap::ldap_groupcmp: Group %s not found or user is not a member.",(char *)check->strvalue);
804 snprintf(filter,sizeof(filter), "(objectclass=*)");
805 if ((conn_id = ldap_get_conn(inst->conns,&conn,inst)) == -1){
806 radlog(L_ERR, "rlm_ldap: Add ldap connections are in use");
809 if ((res = perform_search(inst, conn, vp_user_dn->strvalue, LDAP_SCOPE_BASE,
810 filter, group_attrs,&result)) != RLM_MODULE_OK){
811 DEBUG("rlm_ldap::ldap_groupcmp: Search returned error");
812 ldap_release_conn(conn_id, inst->conns);
816 if ((msg = ldap_first_entry(conn->ld, result)) == NULL) {
817 DEBUG("rlm_ldap::ldap_groupcmp: ldap_first_entry() failed");
818 ldap_release_conn(conn_id,inst->conns);
819 ldap_msgfree(result);
822 if ((vals = ldap_get_values(conn->ld, msg, inst->groupmemb_attr)) != NULL) {
825 for (;i < ldap_count_values(vals);i++){
826 if (strchr(vals[i],',') != NULL){
827 /* This looks like a DN */
828 LDAPMessage *gr_result = NULL;
829 snprintf(filter,sizeof(filter), "(%s=%s)",
830 inst->groupname_attr,
831 (char *)check->strvalue);
832 if ((res = perform_search(inst, conn, vals[i],
833 LDAP_SCOPE_BASE, filter,
834 attrs, &gr_result)) != RLM_MODULE_OK){
835 if (res != RLM_MODULE_NOTFOUND){
836 DEBUG("rlm_ldap::ldap_groupcmp: \
837 Search returned error");
838 ldap_value_free(vals);
839 ldap_msgfree(result);
840 ldap_release_conn(conn_id,inst->conns);
844 ldap_msgfree(gr_result);
849 if (strcmp(vals[i],(char *)check->strvalue) == 0){
855 ldap_value_free(vals);
856 ldap_msgfree(result);
858 DEBUG("rlm_ldap::groupcmp: Group %s not found \
859 or user not a member",
860 (char *)check->strvalue);
861 ldap_release_conn(conn_id,inst->conns);
865 DEBUG("rlm_ldap::ldap_groupcmp: ldap_get_values() failed");
866 ldap_msgfree(result);
867 ldap_release_conn(conn_id,inst->conns);
871 DEBUG("rlm_ldap::ldap_groupcmp: User found in group %s",(char *)check->strvalue);
872 ldap_release_conn(conn_id,inst->conns);
879 * Do an xlat on an LDAP URL
882 static int ldap_xlat(void *instance, REQUEST *request, char *fmt, char *out, int freespace,
883 RADIUS_ESCAPE_STRING func)
885 char url[MAX_FILTER_STR_LEN];
888 ldap_instance *inst = instance;
889 LDAPURLDesc *ldap_url;
890 LDAPMessage *result = NULL;
891 LDAPMessage *msg = NULL;
896 DEBUG("rlm_ldap: - ldap_xlat");
897 if (!radius_xlat(url, sizeof(url), fmt, request, func)) {
898 radlog (L_ERR, "rlm_ldap: Unable to create LDAP URL.\n");
901 if (!ldap_is_ldap_url(url)){
902 radlog (L_ERR, "rlm_ldap: String passed does not look like an LDAP URL.\n");
905 if (ldap_url_parse(url,&ldap_url)){
906 radlog (L_ERR, "rlm_ldap: LDAP URL parse failed.\n");
909 if (ldap_url->lud_attrs == NULL || ldap_url->lud_attrs[0] == NULL || \
910 ( ldap_url->lud_attrs[1] != NULL || ( ! strlen(ldap_url->lud_attrs[0]) || \
911 ! strcmp(ldap_url->lud_attrs[0],"*") ) ) ){
912 radlog (L_ERR, "rlm_ldap: Invalid Attribute(s) request.\n");
913 ldap_free_urldesc(ldap_url);
916 if (ldap_url->lud_host){
917 if (strncmp(inst->server,ldap_url->lud_host,strlen(inst->server)) != 0 || \
918 ldap_url->lud_port != inst->port){
919 DEBUG("rlm_ldap: Requested server/port is not known to this module instance.");
920 ldap_free_urldesc(ldap_url);
924 if ((conn_id = ldap_get_conn(inst->conns,&conn,inst)) == -1){
925 radlog(L_ERR, "rlm_ldap: All ldap connections are in use");
926 ldap_free_urldesc(ldap_url);
929 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){
930 if (res == RLM_MODULE_NOTFOUND){
931 DEBUG("rlm_ldap: Search returned not found");
932 ldap_free_urldesc(ldap_url);
933 ldap_release_conn(conn_id,inst->conns);
936 DEBUG("rlm_ldap: Search returned error");
937 ldap_free_urldesc(ldap_url);
938 ldap_release_conn(conn_id,inst->conns);
941 if ((msg = ldap_first_entry(conn->ld, result)) == NULL){
942 DEBUG("rlm_ldap: ldap_first_entry() failed");
943 ldap_msgfree(result);
944 ldap_free_urldesc(ldap_url);
945 ldap_release_conn(conn_id,inst->conns);
948 if ((vals = ldap_get_values(conn->ld, msg, ldap_url->lud_attrs[0])) != NULL) {
949 ret = strlen(vals[0]);
950 if (ret > freespace){
951 DEBUG("rlm_ldap: Insufficient string space");
952 ldap_free_urldesc(ldap_url);
953 ldap_value_free(vals);
954 ldap_msgfree(result);
955 ldap_release_conn(conn_id,inst->conns);
958 DEBUG("rlm_ldap: Adding attribute %s, value: %s",ldap_url->lud_attrs[0],vals[0]);
959 strncpy(out,vals[0],ret);
960 ldap_value_free(vals);
965 ldap_msgfree(result);
966 ldap_free_urldesc(ldap_url);
967 ldap_release_conn(conn_id,inst->conns);
969 DEBUG("rlm_ldap: - ldap_xlat end");
975 /******************************************************************************
977 * Function: rlm_ldap_authorize
979 * Purpose: Check if user is authorized for remote access
981 ******************************************************************************/
983 ldap_authorize(void *instance, REQUEST * request)
985 LDAPMessage *result = NULL;
986 LDAPMessage *msg = NULL;
987 LDAPMessage *def_msg = NULL;
988 LDAPMessage *def_attr_msg = NULL;
989 LDAPMessage *def_result = NULL;
990 LDAPMessage *def_attr_result = NULL;
991 ldap_instance *inst = instance;
992 char *user_dn = NULL;
993 char filter[MAX_FILTER_STR_LEN];
994 char basedn[MAX_FILTER_STR_LEN];
995 VALUE_PAIR *check_tmp;
996 VALUE_PAIR *reply_tmp;
998 VALUE_PAIR **check_pairs, **reply_pairs;
1000 VALUE_PAIR *module_fmsg_vp;
1001 VALUE_PAIR *user_profile;
1002 char module_fmsg[MAX_STRING_LEN];
1006 DEBUG("rlm_ldap: - authorize");
1008 if (!request->username){
1009 radlog(L_AUTH, "rlm_ldap: Attribute \"User-Name\" is required for authentication.\n");
1010 return RLM_MODULE_INVALID;
1013 check_pairs = &request->config_items;
1014 reply_pairs = &request->reply->vps;
1017 * Check for valid input, zero length names not permitted
1019 if (request->username->strvalue == 0) {
1020 radlog(L_ERR, "rlm_ldap: zero length username not permitted\n");
1021 return RLM_MODULE_INVALID;
1023 DEBUG("rlm_ldap: performing user authorization for %s",
1024 request->username->strvalue);
1026 if (!radius_xlat(filter, sizeof(filter), inst->filter,
1027 request, ldap_escape_func)) {
1028 radlog (L_ERR, "rlm_ldap: unable to create filter.\n");
1029 return RLM_MODULE_INVALID;
1032 if (!radius_xlat(basedn, sizeof(basedn), inst->basedn,
1034 radlog (L_ERR, "rlm_ldap: unable to create basedn.\n");
1035 return RLM_MODULE_INVALID;
1038 if ((conn_id = ldap_get_conn(inst->conns,&conn,inst)) == -1){
1039 radlog(L_ERR, "rlm_ldap: All ldap connections are in use");
1040 return RLM_MODULE_FAIL;
1042 if ((res = perform_search(instance, conn, basedn, LDAP_SCOPE_SUBTREE, filter, inst->atts, &result)) != RLM_MODULE_OK) {
1043 DEBUG("rlm_ldap: search failed");
1044 if (res == RLM_MODULE_NOTFOUND){
1045 snprintf(module_fmsg,sizeof(module_fmsg),"rlm_ldap: User not found");
1046 module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
1047 pairadd(&request->packet->vps, module_fmsg_vp);
1049 ldap_release_conn(conn_id,inst->conns);
1052 if ((msg = ldap_first_entry(conn->ld, result)) == NULL) {
1053 DEBUG("rlm_ldap: ldap_first_entry() failed");
1054 ldap_msgfree(result);
1055 ldap_release_conn(conn_id,inst->conns);
1056 return RLM_MODULE_FAIL;
1058 if ((user_dn = ldap_get_dn(conn->ld, msg)) == NULL) {
1059 DEBUG("rlm_ldap: ldap_get_dn() failed");
1060 ldap_msgfree(result);
1061 ldap_release_conn(conn_id,inst->conns);
1062 return RLM_MODULE_FAIL;
1065 * Adding new attribute containing DN for LDAP object associated with
1068 pairadd(&request->packet->vps, pairmake("Ldap-UserDn", user_dn, T_OP_EQ));
1069 ldap_memfree(user_dn);
1072 /* Remote access is controled by attribute of the user object */
1073 if (inst->access_attr) {
1074 if ((vals = ldap_get_values(conn->ld, msg, inst->access_attr)) != NULL) {
1075 if (inst->default_allow){
1076 DEBUG("rlm_ldap: checking if remote access for %s is allowed by %s", request->username->strvalue, inst->access_attr);
1077 if (!strncmp(vals[0], "FALSE", 5)) {
1078 DEBUG("rlm_ldap: dialup access disabled");
1079 snprintf(module_fmsg,sizeof(module_fmsg),"rlm_ldap: Access Attribute denies access");
1080 module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
1081 pairadd(&request->packet->vps, module_fmsg_vp);
1082 ldap_msgfree(result);
1083 ldap_value_free(vals);
1084 ldap_release_conn(conn_id,inst->conns);
1085 return RLM_MODULE_USERLOCK;
1087 ldap_value_free(vals);
1090 DEBUG("rlm_ldap: %s attribute exists - access denied by default", inst->access_attr);
1091 snprintf(module_fmsg,sizeof(module_fmsg),"rlm_ldap: Access Attribute denies access");
1092 module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
1093 pairadd(&request->packet->vps, module_fmsg_vp);
1094 ldap_msgfree(result);
1095 ldap_value_free(vals);
1096 ldap_release_conn(conn_id,inst->conns);
1097 return RLM_MODULE_USERLOCK;
1100 if (inst->default_allow){
1101 DEBUG("rlm_ldap: no %s attribute - access denied by default", inst->access_attr);
1102 snprintf(module_fmsg,sizeof(module_fmsg),"rlm_ldap: Access Attribute denies access");
1103 module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
1104 pairadd(&request->packet->vps, module_fmsg_vp);
1105 ldap_msgfree(result);
1106 ldap_release_conn(conn_id,inst->conns);
1107 return RLM_MODULE_USERLOCK;
1113 * Check for the default profile entry. If it exists then add the
1114 * attributes it contains in the check and reply pairs
1117 user_profile = pairfind(request->config_items, PW_USER_PROFILE);
1118 if (inst->default_profile || user_profile){
1119 char *profile = inst->default_profile;
1121 strNcpy(filter,"(objectclass=radiusprofile)",sizeof(filter));
1123 profile = user_profile->strvalue;
1124 if (profile && strlen(profile)){
1125 if ((res = perform_search(instance, conn,
1126 profile, LDAP_SCOPE_BASE,
1127 filter, inst->atts, &def_result)) == RLM_MODULE_OK){
1128 if ((def_msg = ldap_first_entry(conn->ld,def_result))){
1129 if ((check_tmp = ldap_pairget(conn->ld,def_msg,inst->check_item_map,check_pairs,1))) {
1130 /*pairadd(check_pairs,check_tmp);*/
1131 pairxlatmove(request, check_pairs, &check_tmp);
1132 pairfree(&check_tmp);
1134 if ((reply_tmp = ldap_pairget(conn->ld,def_msg,inst->reply_item_map,reply_pairs,0))) {
1135 /*pairadd(reply_pairs,reply_tmp);*/
1136 pairxlatmove(request, reply_pairs, &reply_tmp);
1137 pairfree(&reply_tmp);
1140 ldap_msgfree(def_result);
1142 DEBUG("rlm_ldap: default_profile/user-profile search failed");
1147 * Check for the profile attribute. If it exists, we assume that it
1148 * contains the DN of an entry containg a profile for the user. That
1149 * way we can have different general profiles for various user groups
1150 * (students,faculty,staff etc)
1153 if (inst->profile_attr){
1154 if ((vals = ldap_get_values(conn->ld, msg, inst->profile_attr)) != NULL) {
1156 strNcpy(filter,"(objectclass=radiusprofile)",sizeof(filter));
1157 while(vals[i] != NULL && strlen(vals[i])){
1158 if ((res = perform_search(instance, conn,
1159 vals[i], LDAP_SCOPE_BASE,
1160 filter, inst->atts, &def_attr_result)) == RLM_MODULE_OK){
1161 if ((def_attr_msg = ldap_first_entry(conn->ld,def_attr_result))){
1162 if ((check_tmp = ldap_pairget(conn->ld,def_attr_msg,inst->check_item_map,check_pairs,1))) {
1163 /*pairadd(check_pairs,check_tmp);*/
1164 pairxlatmove(request, check_pairs, &check_tmp);
1165 pairfree(&check_tmp);
1167 if ((reply_tmp = ldap_pairget(conn->ld,def_attr_msg,inst->reply_item_map,reply_pairs,0))) {
1168 /*pairadd(reply_pairs,reply_tmp);*/
1169 pairxlatmove(request, reply_pairs, &reply_tmp);
1170 pairfree(&reply_tmp);
1173 ldap_msgfree(def_attr_result);
1175 DEBUG("rlm_ldap: profile_attribute search failed");
1178 ldap_value_free(vals);
1181 if (inst->passwd_attr && strlen(inst->passwd_attr)){
1182 VALUE_PAIR *passwd_item;
1184 if ((passwd_item = pairfind(request->config_items, PW_PASSWORD)) == NULL){
1186 char *passwd_val = NULL;
1189 if ((passwd_vals = ldap_get_values(conn->ld,msg,inst->passwd_attr)) != NULL){
1191 while(passwd_vals[i] != NULL){
1192 if (strlen(passwd_vals[i])){
1193 passwd_val = passwd_vals[i];
1195 if (inst->passwd_hdr && strlen(inst->passwd_hdr)){
1196 passwd_val = strstr(passwd_val,inst->passwd_hdr);
1197 if (passwd_val != NULL)
1198 passwd_val += strlen(inst->passwd_hdr);
1200 DEBUG("rlm_ldap: Password header not found in password %s for user %s", passwd_vals[0],request->username->strvalue);
1203 if ((passwd_item = paircreate(PW_PASSWORD,PW_TYPE_STRING)) == NULL){
1204 radlog(L_ERR|L_CONS, "no memory");
1205 ldap_value_free(passwd_vals);
1206 ldap_msgfree(result);
1207 ldap_release_conn(conn_id,inst->conns);
1208 return RLM_MODULE_FAIL;
1210 passwd_len = strlen(passwd_val);
1211 strncpy(passwd_item->strvalue,passwd_val,MAX_STRING_LEN - 1);
1212 passwd_item->length = (passwd_len > (MAX_STRING_LEN - 1)) ? (MAX_STRING_LEN - 1) : passwd_len;
1213 pairadd(&request->config_items,passwd_item);
1214 DEBUG("rlm_ldap: Added password %s in check items",passwd_item->strvalue);
1219 ldap_value_free(passwd_vals);
1226 DEBUG("rlm_ldap: looking for check items in directory...");
1228 if ((check_tmp = ldap_pairget(conn->ld, msg, inst->check_item_map,check_pairs,1)) != NULL) {
1229 /*pairadd(check_pairs,check_tmp);*/
1230 pairxlatmove(request, check_pairs, &check_tmp);
1231 pairfree(&check_tmp);
1235 DEBUG("rlm_ldap: looking for reply items in directory...");
1238 if ((reply_tmp = ldap_pairget(conn->ld, msg, inst->reply_item_map,reply_pairs,0)) != NULL) {
1239 /*pairadd(reply_pairs,reply_tmp);*/
1240 pairxlatmove(request, reply_pairs, &reply_tmp);
1241 pairfree(&reply_tmp);
1244 if (inst->do_comp && paircmp(request,request->packet->vps,*check_pairs,reply_pairs) != 0){
1245 DEBUG("rlm_ldap: Pairs do not match. Rejecting user.");
1246 snprintf(module_fmsg,sizeof(module_fmsg),"rlm_ldap: Pairs do not match");
1247 module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
1248 pairadd(&request->packet->vps, module_fmsg_vp);
1249 ldap_msgfree(result);
1250 ldap_release_conn(conn_id,inst->conns);
1252 return RLM_MODULE_REJECT;
1256 * Module should default to LDAP authentication if no Auth-Type
1259 if (pairfind(*check_pairs, PW_AUTHTYPE) == NULL)
1260 pairadd(check_pairs, pairmake("Auth-Type", "LDAP", T_OP_EQ));
1263 DEBUG("rlm_ldap: user %s authorized to use remote access",
1264 request->username->strvalue);
1265 ldap_msgfree(result);
1266 ldap_release_conn(conn_id,inst->conns);
1268 return RLM_MODULE_OK;
1271 /*****************************************************************************
1273 * Function: rlm_ldap_authenticate
1275 * Purpose: Check the user's password against ldap database
1277 *****************************************************************************/
1279 ldap_authenticate(void *instance, REQUEST * request)
1282 LDAPMessage *result, *msg;
1283 ldap_instance *inst = instance;
1284 char *user_dn, *attrs[] = {"uid", NULL};
1285 char filter[MAX_FILTER_STR_LEN];
1286 char basedn[MAX_FILTER_STR_LEN];
1288 VALUE_PAIR *vp_user_dn;
1289 VALUE_PAIR *module_fmsg_vp;
1290 char module_fmsg[MAX_STRING_LEN];
1294 DEBUG("rlm_ldap: - authenticate");
1297 * Ensure that we're being passed a plain-text password, and not
1301 if (!request->username) {
1302 radlog(L_AUTH, "rlm_ldap: Attribute \"User-Name\" is required for authentication.\n");
1303 return RLM_MODULE_INVALID;
1306 if (!request->password){
1307 radlog(L_AUTH, "rlm_ldap: Attribute \"User-Password\" is required for authentication.");
1308 return RLM_MODULE_INVALID;
1311 if(request->password->attribute != PW_PASSWORD) {
1312 radlog(L_AUTH, "rlm_ldap: Attribute \"User-Password\" is required for authentication. Cannot use \"%s\".", request->password->name);
1313 return RLM_MODULE_INVALID;
1316 if (request->password->length == 0) {
1317 radlog(L_ERR, "rlm_ldap: empty password supplied");
1318 return RLM_MODULE_INVALID;
1322 * Check that we don't have any failed connections. If we do there's no real need
1323 * of runing. Also give it another chance if we have a lot of failed connections.
1325 if (inst->failed_conns > MAX_FAILED_CONNS_END)
1326 inst->failed_conns = 0;
1327 if (inst->failed_conns > MAX_FAILED_CONNS_START){
1328 inst->failed_conns++;
1329 return RLM_MODULE_FAIL;
1333 DEBUG("rlm_ldap: login attempt by \"%s\" with password \"%s\"",
1334 request->username->strvalue, request->password->strvalue);
1336 while((vp_user_dn = pairfind(request->packet->vps, PW_LDAP_USERDN)) == NULL) {
1337 if (!radius_xlat(filter, sizeof(filter), inst->filter,
1339 radlog (L_ERR, "rlm_ldap: unable to create filter.\n");
1340 return RLM_MODULE_INVALID;
1343 if (!radius_xlat(basedn, sizeof(basedn), inst->basedn,
1345 radlog (L_ERR, "rlm_ldap: unable to create basedn.\n");
1346 return RLM_MODULE_INVALID;
1349 if ((conn_id = ldap_get_conn(inst->conns,&conn,inst)) == -1){
1350 radlog(L_ERR, "rlm_ldap: All ldap connections are in use");
1351 return RLM_MODULE_FAIL;
1353 if ((res = perform_search(instance, conn, basedn, LDAP_SCOPE_SUBTREE, filter, attrs, &result)) != RLM_MODULE_OK) {
1354 if (res == RLM_MODULE_NOTFOUND){
1355 snprintf(module_fmsg,sizeof(module_fmsg),"rlm_ldap: User not found");
1356 module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
1357 pairadd(&request->packet->vps, module_fmsg_vp);
1359 ldap_release_conn(conn_id,inst->conns);
1362 if ((msg = ldap_first_entry(conn->ld, result)) == NULL) {
1363 ldap_msgfree(result);
1364 ldap_release_conn(conn_id,inst->conns);
1365 return RLM_MODULE_FAIL;
1367 if ((user_dn = ldap_get_dn(conn->ld, msg)) == NULL) {
1368 DEBUG("rlm_ldap: ldap_get_dn() failed");
1369 ldap_msgfree(result);
1370 ldap_release_conn(conn_id,inst->conns);
1371 return RLM_MODULE_FAIL;
1373 ldap_release_conn(conn_id,inst->conns);
1374 pairadd(&request->packet->vps, pairmake("Ldap-UserDn", user_dn, T_OP_EQ));
1375 ldap_memfree(user_dn);
1376 ldap_msgfree(result);
1379 user_dn = vp_user_dn->strvalue;
1381 DEBUG("rlm_ldap: user DN: %s", user_dn);
1383 ld_user = ldap_connect(instance, user_dn, request->password->strvalue,
1385 if (ld_user == NULL){
1386 if (res == RLM_MODULE_REJECT){
1387 inst->failed_conns = 0;
1388 snprintf(module_fmsg,sizeof(module_fmsg),"rlm_ldap: Bind as user failed");
1389 module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
1390 pairadd(&request->packet->vps, module_fmsg_vp);
1392 if (res == RLM_MODULE_FAIL){
1393 DEBUG("rlm_ldap: ldap_connect() failed");
1394 inst->failed_conns++;
1399 DEBUG("rlm_ldap: user %s authenticated succesfully",
1400 request->username->strvalue);
1401 ldap_unbind_s(ld_user);
1402 inst->failed_conns = 0;
1404 return RLM_MODULE_OK;
1408 ldap_connect(void *instance, const char *dn, const char *password, int auth, int *result)
1410 ldap_instance *inst = instance;
1412 int msgid, rc, ldap_version;
1417 #ifdef HAVE_LDAP_INITIALIZE
1418 DEBUG("rlm_ldap: (re)connect to %s, authentication %d", inst->server, auth);
1419 if (ldap_initialize(&ld, inst->server) != LDAP_SUCCESS) {
1420 radlog(L_ERR, "rlm_ldap: ldap_initialize() failed");
1421 *result = RLM_MODULE_FAIL;
1427 DEBUG("rlm_ldap: (re)connect to %s:%d, authentication %d", inst->server, inst->port, auth);
1428 if ((ld = ldap_init(inst->server, inst->port)) == NULL) {
1429 radlog(L_ERR, "rlm_ldap: ldap_init() failed");
1430 *result = RLM_MODULE_FAIL;
1434 if (ldap_set_option(ld, LDAP_OPT_NETWORK_TIMEOUT, (void *) &(inst->net_timeout)) != LDAP_OPT_SUCCESS) {
1435 radlog(L_ERR, "rlm_ldap: Could not set LDAP_OPT_NETWORK_TIMEOUT %ld.%ld", inst->net_timeout.tv_sec, inst->net_timeout.tv_usec);
1437 if (ldap_set_option(ld, LDAP_OPT_TIMELIMIT, (void *) &(inst->timelimit)) != LDAP_OPT_SUCCESS) {
1438 radlog(L_ERR, "rlm_ldap: Could not set LDAP_OPT_TIMELIMIT %d", inst->timelimit);
1440 if (inst->ldap_debug && ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &(inst->ldap_debug)) != LDAP_OPT_SUCCESS) {
1441 radlog(L_ERR, "rlm_ldap: Could not set LDAP_OPT_DEBUG_LEVEL %d", inst->ldap_debug);
1443 ldap_version = LDAP_VERSION3;
1444 if (ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &ldap_version) != LDAP_OPT_SUCCESS) {
1445 radlog(L_ERR, "rlm_ldap: Could not set LDAP version to V3");
1447 #ifdef HAVE_LDAP_START_TLS
1448 if(inst->tls_mode) {
1449 DEBUG("rlm_ldap: setting TLS mode to %d", inst->tls_mode);
1450 if(ldap_set_option(ld, LDAP_OPT_X_TLS,
1451 (void *) &(inst->tls_mode)) != LDAP_OPT_SUCCESS) {
1452 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
1453 radlog(L_ERR, "rlm_ldap: could not set LDAP_OPT_X_TLS option %s", ldap_err2string(ldap_errno));
1456 if (inst->start_tls) {
1457 DEBUG("rlm_ldap: starting TLS");
1458 rc = ldap_start_tls_s(ld, NULL, NULL);
1459 if (rc != LDAP_SUCCESS) {
1460 DEBUG("rlm_ldap: ldap_start_tls_s()");
1461 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
1462 radlog(L_ERR, "rlm_ldap: could not start TLS %s", ldap_err2string(ldap_errno));
1463 *result = RLM_MODULE_FAIL;
1468 #endif /* HAVE_LDAP_START_TLS */
1471 DEBUG("rlm_ldap: bind as %s/%s to %s", dn, password, inst->server);
1474 DEBUG("rlm_ldap: bind as %s/%s to %s:%d", dn, password, inst->server, inst->port);
1476 msgid = ldap_bind(ld, dn, password,LDAP_AUTH_SIMPLE);
1478 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
1480 radlog(L_ERR, "rlm_ldap: %s bind to %s failed: %s",
1481 dn, inst->server, ldap_err2string(ldap_errno));
1483 radlog(L_ERR, "rlm_ldap: %s bind to %s:%d failed: %s",
1484 dn, inst->server, inst->port,
1485 ldap_err2string(ldap_errno));
1486 *result = RLM_MODULE_FAIL;
1490 DEBUG("rlm_ldap: waiting for bind result ...");
1492 rc = ldap_result(ld, msgid, 1, &(inst->timeout), &res);
1495 DEBUG("rlm_ldap: ldap_result()");
1496 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
1498 radlog(L_ERR, "rlm_ldap: %s bind to %s failed: %s",
1499 dn, inst->server, (rc == 0) ? "timeout" : ldap_err2string(ldap_errno));
1501 radlog(L_ERR, "rlm_ldap: %s bind to %s:%d failed: %s",
1502 dn, inst->server, inst->port,
1503 (rc == 0) ? "timeout" : ldap_err2string(ldap_errno));
1504 *result = RLM_MODULE_FAIL;
1508 ldap_errno = ldap_result2error(ld, res, 1);
1509 switch (ldap_errno) {
1511 *result = RLM_MODULE_OK;
1514 case LDAP_INVALID_CREDENTIALS:
1516 *result = RLM_MODULE_REJECT;
1518 radlog(L_ERR, "rlm_ldap: LDAP login failed: check login, password settings in ldap section of radiusd.conf");
1519 *result = RLM_MODULE_FAIL;
1525 radlog(L_ERR,"rlm_ldap: %s bind to %s failed %s",
1526 dn, inst->server, ldap_err2string(ldap_errno));
1528 radlog(L_ERR,"rlm_ldap: %s bind to %s:%d failed %s",
1529 dn, inst->server, inst->port,
1530 ldap_err2string(ldap_errno));
1531 *result = RLM_MODULE_FAIL;
1534 if (*result != RLM_MODULE_OK) {
1541 /*****************************************************************************
1543 * Detach from the LDAP server and cleanup internal state.
1545 *****************************************************************************/
1547 ldap_detach(void *instance)
1549 ldap_instance *inst = instance;
1550 TLDAP_RADIUS *pair, *nextpair;
1553 free((char *) inst->server);
1555 free((char *) inst->login);
1557 free((char *) inst->password);
1559 free((char *) inst->basedn);
1560 if (inst->dictionary_mapping)
1561 free(inst->dictionary_mapping);
1563 free((char *) inst->filter);
1564 if (inst->passwd_hdr)
1565 free((char *) inst->passwd_hdr);
1566 if (inst->passwd_attr)
1567 free((char *) inst->passwd_attr);
1568 if (inst->groupname_attr)
1569 free((char *) inst->groupname_attr);
1570 if (inst->groupmemb_filt)
1571 free((char *) inst->groupmemb_filt);
1572 if (inst->groupmemb_attr)
1573 free((char *) inst->groupmemb_attr);
1574 if (inst->access_attr)
1575 free((char *) inst->access_attr);
1576 if (inst->profile_attr)
1577 free((char *) inst->profile_attr);
1581 for(;i<inst->num_conns;i++){
1582 if (inst->conns[i].ld){
1583 ldap_unbind_s(inst->conns[i].ld);
1585 pthread_mutex_destroy(&inst->conns[i].mutex);
1590 pair = inst->check_item_map;
1592 while (pair != NULL) {
1593 nextpair = pair->next;
1595 free(pair->radius_attr);
1600 pair = inst->reply_item_map;
1602 while (pair != NULL) {
1603 nextpair = pair->next;
1605 free(pair->radius_attr);
1613 paircompare_unregister(PW_LDAP_GROUP, ldap_groupcmp);
1614 xlat_unregister(inst->xlat_name,ldap_xlat);
1615 free(inst->xlat_name);
1624 fieldcpy(char *string, char **uptr)
1629 while (*ptr == ' ' || *ptr == '\t') {
1634 while (*ptr != '"' && *ptr != '\0' && *ptr != '\n') {
1644 while (*ptr != ' ' && *ptr != '\t' && *ptr != '\0' && *ptr != '\n' &&
1645 *ptr != '=' && *ptr != ',') {
1653 /*****************************************************************************
1654 * Get RADIUS attributes from LDAP object
1655 * ( according to draft-adoba-radius-05.txt
1656 * <http://www.ietf.org/internet-drafts/draft-adoba-radius-05.txt> )
1658 *****************************************************************************/
1661 ldap_pairget(LDAP * ld, LDAPMessage * entry,
1662 TLDAP_RADIUS * item_map, VALUE_PAIR **pairs,char is_check)
1668 TLDAP_RADIUS *element;
1670 int is_generic_attribute;
1672 VALUE_PAIR *pairlist = NULL;
1673 VALUE_PAIR *newpair = NULL;
1675 /* check if there is a mapping from this LDAP attribute to a RADIUS attribute */
1676 for (element = item_map; element != NULL; element = element->next) {
1677 if ((vals = ldap_get_values(ld,entry,element->attr)) != NULL){
1678 /* check whether this is a one-to-one-mapped ldap attribute or a generic
1679 attribute and set flag accordingly */
1681 if (strcasecmp(element->radius_attr, GENERIC_ATTRIBUTE_ID)==0)
1682 is_generic_attribute = 1;
1684 is_generic_attribute = 0;
1686 /* find out how many values there are for the attribute and extract all of them */
1688 vals_count = ldap_count_values(vals);
1690 for (vals_idx = 0; vals_idx < vals_count; vals_idx++) {
1691 ptr = vals[vals_idx];
1693 if (is_generic_attribute) {
1694 /* this is a generic attribute */
1695 LRAD_TOKEN dummy; /* makes pairread happy */
1697 /* not sure if using pairread here is ok ... */
1698 if ( (newpair = pairread(&ptr, &dummy)) != NULL) {
1699 DEBUG("rlm_ldap: extracted attribute %s from generic item %s",
1700 newpair->name, vals[vals_idx]);
1701 pairadd(&pairlist, newpair);
1703 radlog(L_ERR, "rlm_ldap: parsing %s failed: %s",
1704 element->attr, vals[vals_idx]);
1707 /* this is a one-to-one-mapped attribute */
1708 token = gettoken(&ptr, value, sizeof(value) - 1);
1709 if (token < T_EQSTART || token > T_EQEND) {
1710 token = (is_check) ? T_OP_CMP_EQ : T_OP_EQ;
1712 gettoken(&ptr, value, sizeof(value) - 1);
1714 if (value[0] == 0) {
1715 DEBUG("rlm_ldap: Attribute %s has no value", element->attr);
1718 DEBUG("rlm_ldap: Adding %s as %s, value %s & op=%d", element->attr, element->radius_attr, value, token);
1719 if ((newpair = pairmake(element->radius_attr, value, token)) == NULL)
1722 pairdelete(pairs,newpair->attribute);
1724 pairadd(&pairlist, newpair);
1727 ldap_value_free(vals);
1734 /* globally exported name */
1735 module_t rlm_ldap = {
1737 RLM_TYPE_THREAD_SAFE, /* type: reserved */
1738 NULL, /* initialization */
1739 ldap_instantiate, /* instantiation */
1741 ldap_authenticate, /* authentication */
1742 ldap_authorize, /* authorization */
1743 NULL, /* preaccounting */
1744 NULL, /* accounting */
1745 NULL, /* checksimul */
1746 NULL, /* pre-proxy */
1747 NULL, /* post-proxy */
1748 NULL /* post-auth */
1750 ldap_detach, /* detach */