If we add a check item then use the == operator. Based on an idea by Allister Maguire...
[freeradius.git] / src / modules / rlm_ldap / rlm_ldap.c
1 /*
2  * rlm_ldap.c LDAP authorization and authentication module.
3  * 
4  * 
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>
8  * 
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
20  *        LDAP connection.
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
47  *        the right place.
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  */
98 static const char rcsid[] = "$Id$";
99
100 #include "autoconf.h"
101
102 #include        <sys/types.h>
103 #include        <sys/socket.h>
104 #include        <sys/time.h>
105 #include        <netinet/in.h>
106
107 #include        <stdio.h>
108 #include        <stdlib.h>
109 #include        <netdb.h>
110 #include        <pwd.h>
111 #include        <time.h>
112 #include        <ctype.h>
113 #include        <string.h>
114
115 #include        <lber.h>
116 #include        <ldap.h>
117
118 #include        <errno.h>
119 #include        <unistd.h>
120 #include        <pthread.h>
121
122 #include        "libradius.h"
123 #include        "radiusd.h"
124 #include        "conffile.h"
125 #include        "modules.h"
126 #include        "rad_assert.h"
127
128
129 #define MAX_AUTH_QUERY_LEN      256
130 #define MAX_GROUP_STR_LEN       1024
131 #define TIMELIMIT 5
132
133 /*
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
137  * to reconnect
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
144  * try to reconnect.
145  *
146  *
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
149  * server problem.
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
155  *
156  */
157
158 #define MAX_FAILED_CONNS_END            20
159 #define MAX_FAILED_CONNS_RESTART        4
160 #define MAX_FAILED_CONNS_START          5
161
162 /* linked list of mappings between RADIUS attributes and LDAP attributes */
163 struct TLDAP_RADIUS {
164         char*                 attr;
165         char*                 radius_attr;
166         struct TLDAP_RADIUS*  next;
167 };
168 typedef struct TLDAP_RADIUS TLDAP_RADIUS;
169
170 typedef struct ldap_conn {
171         LDAP            *ld;
172         char            bound;
173         char            locked;
174         int             failed_conns;
175         pthread_mutex_t mutex;
176 } LDAP_CONN;
177
178 #define MAX_SERVER_LINE 1024
179
180 typedef struct {
181         char           *server;
182         int             port;
183         int             timelimit;
184         struct timeval  net_timeout;
185         struct timeval  timeout;
186         int             debug;
187         int             tls_mode;
188         int             start_tls;
189         int             num_conns;
190         int             cache_timeout;
191         int             cache_size;
192         int             do_comp;
193         int             default_allow;
194         char           *login;
195         char           *password;
196         char           *filter;
197         char           *basedn;
198         char           *default_profile;
199         char           *profile_attr;
200         char           *access_attr;
201         char           *passwd_hdr;
202         char           *passwd_attr;
203         char           *dictionary_mapping;
204         char           *groupname_attr;
205         char           *groupmemb_filt;
206         char           *groupmemb_attr;
207         char            **atts;
208         TLDAP_RADIUS   *check_item_map;
209         TLDAP_RADIUS   *reply_item_map;
210         LDAP_CONN       *conns;
211         int             ldap_debug; /* Debug flag for LDAP SDK */
212         char            *xlat_name; /* name used to xlat */
213 }               ldap_instance;
214
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"},
246
247         {NULL, -1, 0, NULL, NULL}
248 };
249
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 )
253
254 #ifdef FIELDCPY
255 static void     fieldcpy(char *, char **);
256 #endif
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);
262
263 static inline int ldap_get_conn(LDAP_CONN *conns,LDAP_CONN **ret,void *instance)
264 {
265         ldap_instance *inst = instance;
266         register int i = 0;
267
268         for(;i<inst->num_conns;i++){
269                 if (conns[i].locked == 0 && pthread_mutex_trylock(&(conns[i].mutex)) == 0){
270                         *ret = &conns[i];
271                         conns[i].locked = 1;
272                         DEBUG("ldap_get_conn: Got Id: %d",i);
273                         return i;
274                 }
275         }
276
277         return -1;
278 }
279         
280 static inline void ldap_release_conn(int i, LDAP_CONN *conns)
281 {
282         DEBUG("ldap_release_conn: Release Id: %d",i);
283         conns[i].locked = 0;
284         pthread_mutex_unlock(&(conns[i].mutex));
285 }
286
287 /*************************************************************************
288  *
289  *      Function: rlm_ldap_instantiate
290  *
291  *      Purpose: Uses section of radiusd config file passed as parameter
292  *               to create an instance of the module.
293  *
294  *************************************************************************/
295 static int 
296 ldap_instantiate(CONF_SECTION * conf, void **instance)
297 {
298         ldap_instance  *inst;
299         int i = 0;
300         int atts_num = 0;
301         int reply_map_num = 0;
302         int check_map_num = 0;
303         int att_map[3] = {0,0,0};
304         TLDAP_RADIUS *pair;
305         char *xlat_name;
306
307         inst = rad_malloc(sizeof *inst);
308
309         if (cf_section_parse(conf, inst, module_config) < 0) {
310                 free(inst);
311                 return -1;
312         }
313
314         if (inst->server == NULL) {
315                 radlog(L_ERR, "rlm_ldap: missing 'server' directive.");
316                 free(inst);
317                 return -1;
318         }
319  
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;
325          else
326                 inst->tls_mode = LDAP_OPT_X_TLS_TRY;
327         inst->reply_item_map = NULL;
328         inst->check_item_map = NULL;
329         inst->conns = NULL;
330
331         paircompare_register(PW_LDAP_GROUP, PW_USER_NAME, ldap_groupcmp, inst);
332         DEBUG("conns: %p",inst->conns);
333
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 */
338         }
339         inst->xlat_name = strdup(xlat_name);
340         xlat_register(xlat_name,ldap_xlat,inst);
341
342         if (inst->num_conns <= 0){
343                 radlog(L_ERR, "rlm_ldap: Invalid ldap connections number passed.");
344                 free(inst);
345                 return -1;
346         }
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.");
350                 free(inst);
351                 return -1;
352         }
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);
359         }       
360
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");
365         }
366         
367         pair = inst->check_item_map;
368         while(pair != NULL){
369                 atts_num++;
370                 pair = pair->next;
371         }
372         check_map_num = (atts_num - 1);
373         pair = inst->reply_item_map;
374         while(pair != NULL){
375                 atts_num++;
376                 pair = pair->next;
377         }
378         reply_map_num = (atts_num - 1);
379         if (inst->profile_attr)
380                 atts_num++;
381         if (inst->passwd_attr)
382                 atts_num++;
383         if (inst->access_attr)
384                 atts_num++;
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.");
388                 free(inst);
389                 return -1;
390         }
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;
397                         else
398                                 pair = pair->next;
399                 }
400                 else if (i <= reply_map_num){
401                         inst->atts[i] = pair->attr;
402                         pair = pair->next;
403                 }
404                 else{
405                         if (inst->profile_attr && !att_map[0]){
406                                 inst->atts[i] = inst->profile_attr;
407                                 att_map[0] = 1;
408                         }
409                         else if (inst->passwd_attr && !att_map[1]){
410                                 inst->atts[i] = inst->passwd_attr;
411                                 att_map[1] = 1;
412                         }
413                         else if (inst->access_attr && !att_map[2]){
414                                 inst->atts[i] = inst->access_attr;
415                                 att_map[2] = 1;
416                         }
417                 }
418         }
419         inst->atts[atts_num] = NULL;            
420
421         DEBUG("conns: %p",inst->conns);
422
423         *instance = inst;
424
425
426         return 0;
427 }
428
429
430 /*
431  * read_mappings(...) reads a ldap<->radius mappings file to inst->reply_item_map and inst->check_item_map
432  */
433
434 #define MAX_LINE_LEN 160
435 #define GENERIC_ATTRIBUTE_ID "$GENERIC$"
436
437 static int
438 read_mappings(ldap_instance* inst)
439 {
440         FILE* mapfile;
441         char *filename;
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];
444         int linenumber;
445
446         /* open the mappings file for reading */
447
448         filename = inst->dictionary_mapping;
449         DEBUG("rlm_ldap: reading ldap<->radius mappings from file %s", filename);
450         mapfile = fopen(filename, "r");
451
452         if (mapfile == NULL) {
453                 radlog(L_ERR, "rlm_ldap: Opening file %s failed", filename);
454                 return -1; /* error */
455         }
456
457         /* read file line by line. Note that if line length exceed MAX_LINE_LEN, line numbers will be mixed up */
458
459         linenumber = 0;
460
461         while (fgets(buf, sizeof buf, mapfile)!=NULL) {
462                 char* ptr;
463                 int token_count;
464                 TLDAP_RADIUS* pair;
465
466                 linenumber++;
467
468                 /* strip comments */
469                 ptr = strchr(buf, '#');
470                 if (ptr) *ptr = 0;
471                 
472                 /* empty line */
473                 if (buf[0] == 0) continue;
474                 
475                 /* extract tokens from the string */            
476                 token_count = sscanf(buf, "%s %s %s", itemType, radiusAttribute, ldapAttribute);
477
478                 if (token_count <= 0) /* no tokens */                   
479                         continue;
480
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);
485                         continue;
486                 }
487
488                 /* create new TLDAP_RADIUS list node */
489                 pair = rad_malloc(sizeof(TLDAP_RADIUS));
490
491                 pair->attr = strdup(ldapAttribute);
492                 pair->radius_attr = strdup(radiusAttribute);
493
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);
498                         free(pair);
499                         fclose(mapfile);
500                         return -1;
501                 }
502                         
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;
510                 } else {
511                         radlog(L_ERR, "rlm_ldap: file %s: skipping line %i: unknown itemType %s", 
512                                filename, linenumber, itemType);
513                         free(pair->attr);
514                         free(pair->radius_attr);
515                         free(pair);
516                         continue;
517                 }
518
519                 DEBUG("rlm_ldap: LDAP %s mapped to RADIUS %s",
520                       pair->attr, pair->radius_attr);
521         }
522         
523         fclose(mapfile);
524
525         return 0; /* success */
526 }
527
528 static int 
529 perform_search(void *instance, LDAP_CONN *conn, char *search_basedn, int scope, char *filter, 
530                 char **attrs, LDAPMessage ** result)
531 {
532         int             res = RLM_MODULE_OK;
533         int             ldap_errno = 0;
534         ldap_instance  *inst = instance;
535         int             search_retry = 0;
536
537         *result = NULL;
538
539         if (!conn){
540                 radlog(L_ERR, "rlm_ldap: NULL connection handle passed");
541                 return RLM_MODULE_FAIL;
542         }
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;
547                         conn->bound = 0;
548                 }
549         }
550 retry:
551         if (!conn->bound || conn->ld == NULL) {
552                 DEBUG2("rlm_ldap: attempting LDAP reconnection");
553                 if (conn->ld){
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);
558                 }
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);
564                 }
565                 conn->bound = 1;
566                 conn->failed_conns = 0;
567         }
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)) {
570         case LDAP_SUCCESS:
571         case LDAP_NO_SUCH_OBJECT:
572                 break;
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");
579                                 search_retry = 1;
580                                 conn->bound = 0;
581                                 ldap_msgfree(*result);  
582                                 goto retry;
583                         }
584                 }
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;
591         case LDAP_TIMEOUT:
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:
596         case LDAP_BUSY:
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);
603         default:
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));
606                 conn->bound = 0;
607                 ldap_msgfree(*result);  
608                 return (RLM_MODULE_FAIL);
609         }
610
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);  
615         }
616         return res;
617 }
618
619
620 /*
621  * ldap_groupcmp(). Implement the Ldap-Group == "group" filter
622  */
623
624 static int ldap_groupcmp(void *instance, REQUEST *req, VALUE_PAIR *request, VALUE_PAIR *check,
625                 VALUE_PAIR *check_pairs, VALUE_PAIR **reply_pairs)
626 {
627         char            filter[MAX_GROUP_STR_LEN];
628         char            gr_filter[MAX_AUTH_QUERY_LEN];
629         int             res;
630         LDAPMessage     *result = NULL;
631         LDAPMessage     *msg = NULL;
632         char            basedn[1024];
633         char            *attrs[] = {"dn",NULL};
634         ldap_instance   *inst = instance;
635         LDAP_CONN       *conn;
636         int             conn_id = -1;
637
638         check_pairs = check_pairs;
639         reply_pairs = reply_pairs;
640
641         DEBUG("rlm_ldap: Entering ldap_groupcmp()");
642
643         if (check->strvalue == NULL || check->length == 0){
644                 DEBUG("rlm_ldap::ldap_groupcmp: Illegal group name");
645                 return 1;
646         }
647
648         if (req == NULL){
649                 DEBUG("rlm_ldap::ldap_groupcmp: NULL request");
650                 return 1;
651         }
652
653         if (!radius_xlat(basedn, sizeof(basedn), inst->basedn, req, NULL)) {
654                 DEBUG("rlm_ldap::ldap_groupcmp: unable to create basedn.");
655                 return 1;
656         }
657
658         if ((pairfind(req->packet->vps, PW_LDAP_USERDN)) == NULL){
659                 char            *user_dn = NULL;
660
661                 if (!radius_xlat(filter, MAX_AUTH_QUERY_LEN, inst->filter, req, NULL)) {
662                         DEBUG("rlm_ldap::ldap_groupcmp: unable to create filter");
663                         return 1;
664                 }
665                 if ((conn_id = ldap_get_conn(inst->conns,&conn,inst)) == -1){
666                         radlog(L_ERR, "rlm_ldap: All ldap connections are in use");
667                         return 1;
668                 }
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);
672                         return 1;
673                 }
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);
678                         return 1;
679                 }
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);
684                         return 1;
685                 }
686                 ldap_release_conn(conn_id,inst->conns);
687                 /*
688                 * Adding new attribute containing DN for LDAP object associated with
689                 * given username
690                 */
691                 pairadd(&req->packet->vps, pairmake("Ldap-UserDn", user_dn, T_OP_EQ));
692                 ldap_memfree(user_dn);
693                 ldap_msgfree(result);
694         }
695
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.");
698                 return 1;
699         }
700
701         snprintf(filter,MAX_GROUP_STR_LEN - 1, "(&(%s=%s)%s)",inst->groupname_attr,(char *)check->strvalue,gr_filter);
702
703         if ((conn_id = ldap_get_conn(inst->conns,&conn,inst)) == -1){
704                 radlog(L_ERR, "rlm_ldap: All ldap connections are in use");
705                 return 1;
706         }
707
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);
712                         return 1;
713                 }
714         }
715         if (res != RLM_MODULE_NOTFOUND)
716                 ldap_msgfree(result);
717         ldap_release_conn(conn_id,inst->conns);
718
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);
722                         return 1;
723                 }
724                 else{
725                         VALUE_PAIR *user_dn;
726                         char *group_attrs[] = {inst->groupmemb_attr,NULL};
727
728                         user_dn = pairfind(req->packet->vps, PW_LDAP_USERDN);
729                         if (user_dn != NULL){
730                                 char **vals;
731
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");
735                                         return 1;
736                                 }
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);
740                                         return 1;
741                                 }               
742
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);
747                                         return 1;
748                                 }
749                                 if ((vals = ldap_get_values(conn->ld, msg, inst->groupmemb_attr)) != NULL) {
750                                         unsigned int i = 0;
751                                         char found = 0;
752
753                                         for (;i < ldap_count_values(vals);i++){
754                                                 if (strchr(vals[i],',') != NULL){ /* This looks like a DN */
755                                                         LDAPMessage *gr_result = NULL;
756
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);
764                                                                         return 1;
765                                                                 }
766                                                         }
767                                                         else{
768                                                                 ldap_msgfree(gr_result);
769                                                                 found = 1;
770                                                                 break;
771                                                         }
772                                                 }
773                                                 else{
774                                                         if (strcmp(vals[i],(char *)check->strvalue) == 0){
775                                                                 found = 1;
776                                                                 break;
777                                                         }
778                                                 }
779                                         }
780                                         ldap_value_free(vals);
781                                         ldap_msgfree(result);
782                                         if (found == 0){
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);
786                                                 return 1;
787                                         }
788                                 }
789                                 else{
790                                         DEBUG("rlm_ldap::ldap_groupcmp: ldap_get_values() failed");
791                                         ldap_release_conn(conn_id,inst->conns);
792                                         ldap_msgfree(result);
793                                         return 1;
794                                 }
795                         }
796                 }
797         }
798
799
800         DEBUG("rlm_ldap::ldap_groupcmp: User found in group %s",(char *)check->strvalue);
801         ldap_release_conn(conn_id,inst->conns);
802
803         return 0;
804 }
805
806 /*
807  * ldap_xlat()
808  * Do an xlat on an LDAP URL
809  */
810
811 static int ldap_xlat(void *instance, REQUEST *request, char *fmt, char *out, int freespace,
812                                 RADIUS_ESCAPE_STRING func)
813 {
814         char url[MAX_STRING_LEN];
815         int res;
816         int ret = 0;
817         ldap_instance *inst = instance;
818         LDAPURLDesc *ldap_url;
819         LDAPMessage *result = NULL;
820         LDAPMessage *msg = NULL;
821         char **vals;
822         int conn_id = -1;
823         LDAP_CONN *conn;
824
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");
828                 return 0;
829         }
830         if (!ldap_is_ldap_url(url)){
831                 radlog (L_ERR, "rlm_ldap: String passed does not look like an LDAP URL.\n");
832                 return 0;
833         }
834         if (ldap_url_parse(url,&ldap_url)){
835                 radlog (L_ERR, "rlm_ldap: LDAP URL parse failed.\n");
836                 return 0;
837         }
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);
843                 return 0;
844         }
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);
850                         return 0;
851                 }
852         }
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);
856                 return 0;
857         }
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);
863                         return 0;
864                 }
865                 DEBUG("rlm_ldap: Search returned error");
866                 ldap_free_urldesc(ldap_url);
867                 ldap_release_conn(conn_id,inst->conns);
868                 return 0;
869         }
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);
875                 return 0;
876         }
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);
885                         return 0;
886                 }
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);
890         }
891         else
892                 ret = 0;
893
894         ldap_msgfree(result);
895         ldap_free_urldesc(ldap_url);
896         ldap_release_conn(conn_id,inst->conns);
897
898         DEBUG("rlm_ldap: - ldap_xlat end");
899
900         return ret;
901 }
902
903
904 /******************************************************************************
905  *
906  *      Function: rlm_ldap_authorize
907  *
908  *      Purpose: Check if user is authorized for remote access
909  *
910  ******************************************************************************/
911 static int 
912 ldap_authorize(void *instance, REQUEST * request)
913 {
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];
923         char            basedn[1024];
924         VALUE_PAIR      *check_tmp;
925         VALUE_PAIR      *reply_tmp;
926         int             res;
927         VALUE_PAIR      **check_pairs, **reply_pairs;
928         char            **vals;
929         VALUE_PAIR      *module_fmsg_vp;
930         VALUE_PAIR      *user_profile;
931         char            module_fmsg[MAX_STRING_LEN];
932         LDAP_CONN       *conn;
933         int             conn_id = -1;
934
935         DEBUG("rlm_ldap: - authorize");
936
937         if (!request->username){
938                 radlog(L_AUTH, "rlm_ldap: Attribute \"User-Name\" is required for authentication.\n");
939                 return RLM_MODULE_INVALID;
940         }
941
942         check_pairs = &request->config_items;
943         reply_pairs = &request->reply->vps;
944
945         /*
946          * Check for valid input, zero length names not permitted
947          */
948         if (request->username->strvalue == 0) {
949                 radlog(L_ERR, "rlm_ldap: zero length username not permitted\n");
950                 return RLM_MODULE_INVALID;
951         }
952         DEBUG("rlm_ldap: performing user authorization for %s",
953                request->username->strvalue);
954
955         if (!radius_xlat(filter, sizeof(filter), inst->filter,
956                          request, NULL)) {
957                 radlog (L_ERR, "rlm_ldap: unable to create filter.\n");
958                 return RLM_MODULE_INVALID;
959         }
960
961         if (!radius_xlat(basedn, sizeof(basedn), inst->basedn,
962                          request, NULL)) {
963                 radlog (L_ERR, "rlm_ldap: unable to create basedn.\n");
964                 return RLM_MODULE_INVALID;
965         }
966
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;
970         }
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);
977                 }
978                 ldap_release_conn(conn_id,inst->conns);
979                 return (res);
980         }
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;
986         }
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;
992         }
993         /*
994          * Adding new attribute containing DN for LDAP object associated with
995          * given username
996          */
997         pairadd(&request->packet->vps, pairmake("Ldap-UserDn", user_dn, T_OP_EQ));
998         ldap_memfree(user_dn);
999
1000
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;
1015                                 }
1016                                 ldap_value_free(vals);
1017                         }
1018                         else{
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;
1027                         }
1028                 } else {
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;
1037                         }
1038                 }
1039         }
1040         if (inst->cache_timeout >0)
1041                 ldap_enable_cache(conn->ld, inst->cache_timeout, inst->cache_size);
1042
1043         /*
1044          * Check for the default profile entry. If it exists then add the 
1045          * attributes it contains in the check and reply pairs
1046          */
1047
1048         user_profile = pairfind(request->config_items, PW_USER_PROFILE);
1049         if (inst->default_profile || user_profile){
1050                 char *profile = inst->default_profile;
1051
1052                 strncpy(filter,"(objectclass=radiusprofile)",MAX_AUTH_QUERY_LEN);
1053                 if (user_profile)
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);
1064                                 }
1065                                 ldap_msgfree(def_result);
1066                         } else 
1067                                 DEBUG("rlm_ldap: default_profile/user-profile search failed");
1068                 }
1069         }
1070
1071         /*
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)
1076          */
1077
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);
1091                                 }
1092                                 ldap_msgfree(def_attr_result);
1093                         } else 
1094                                 DEBUG("rlm_ldap: profile_attribute search failed");
1095                         ldap_value_free(vals);
1096                 }
1097         }
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;
1102
1103                 if ((passwd_item = pairfind(request->config_items, PW_PASSWORD)) == NULL){
1104                         char **passwd_vals;
1105                         char *passwd_val = NULL;
1106                         int passwd_len;
1107
1108                         if ((passwd_vals = ldap_get_values(conn->ld,msg,inst->passwd_attr)) != NULL){
1109                                 unsigned int i=0;
1110                                 while(passwd_vals[i] != NULL){
1111                                         if (strlen(passwd_vals[i])){
1112                                                 passwd_val = passwd_vals[i];
1113
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);
1118                                                         else
1119                                                                 DEBUG("rlm_ldap: Password header not found in password %s for user %s", passwd_vals[0],request->username->strvalue);
1120                                                 }
1121                                                 if (passwd_val){
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;
1128                                                         }
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);
1134                                                 }
1135                                         }
1136                                         i++;
1137                                 }
1138                                 ldap_value_free(passwd_vals);
1139                         }
1140                 }
1141         }
1142
1143
1144
1145         DEBUG("rlm_ldap: looking for check items in directory...");
1146
1147         if ((check_tmp = ldap_pairget(conn->ld, msg, inst->check_item_map,check_pairs,1)) != NULL)
1148                 pairadd(check_pairs, check_tmp);
1149
1150
1151         DEBUG("rlm_ldap: looking for reply items in directory...");
1152
1153
1154         if ((reply_tmp = ldap_pairget(conn->ld, msg, inst->reply_item_map,reply_pairs,0)) != NULL)
1155                 pairadd(reply_pairs, reply_tmp);
1156
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);
1164
1165                 return RLM_MODULE_REJECT;
1166         }
1167
1168         /*
1169          * Module should default to LDAP authentication if no Auth-Type
1170          * specified
1171          */
1172         if (pairfind(*check_pairs, PW_AUTHTYPE) == NULL)
1173                 pairadd(check_pairs, pairmake("Auth-Type", "LDAP", T_OP_EQ));
1174
1175
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);
1180
1181         return RLM_MODULE_OK;
1182 }
1183
1184 /*****************************************************************************
1185  *
1186  *      Function: rlm_ldap_authenticate
1187  *
1188  *      Purpose: Check the user's password against ldap database
1189  *
1190  *****************************************************************************/
1191 static int 
1192 ldap_authenticate(void *instance, REQUEST * request)
1193 {
1194         LDAP           *ld_user;
1195         LDAPMessage    *result, *msg;
1196         ldap_instance  *inst = instance;
1197         char           *user_dn, *attrs[] = {"uid", NULL};
1198         char            filter[MAX_AUTH_QUERY_LEN];
1199         char            basedn[1024];
1200         int             res;
1201         VALUE_PAIR     *vp_user_dn;
1202         VALUE_PAIR      *module_fmsg_vp;
1203         char            module_fmsg[MAX_STRING_LEN];
1204         LDAP_CONN       *conn;
1205         int             conn_id = -1;
1206
1207         DEBUG("rlm_ldap: - authenticate");
1208         /*
1209          * Ensure that we're being passed a plain-text password, and not
1210          * anything else.
1211          */
1212
1213         if (!request->username) {
1214                 radlog(L_AUTH, "rlm_ldap: Attribute \"User-Name\" is required for authentication.\n");
1215                 return RLM_MODULE_INVALID;
1216         }
1217
1218         if (!request->password){
1219                 radlog(L_AUTH, "rlm_ldap: Attribute \"User-Password\" is required for authentication.");
1220                 return RLM_MODULE_INVALID;
1221         }
1222
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;
1226         }
1227
1228         if (request->password->length == 0) {
1229                 radlog(L_ERR, "rlm_ldap: empty password supplied");
1230                 return RLM_MODULE_INVALID;
1231         }
1232
1233         DEBUG("rlm_ldap: login attempt by \"%s\" with password \"%s\"", 
1234                request->username->strvalue, request->password->strvalue);
1235
1236         while((vp_user_dn = pairfind(request->packet->vps, PW_LDAP_USERDN)) == NULL) {
1237                 if (!radius_xlat(filter, sizeof(filter), inst->filter,
1238                                 request, NULL)) {
1239                         radlog (L_ERR, "rlm_ldap: unable to create filter.\n"); 
1240                         return RLM_MODULE_INVALID;
1241                 }
1242
1243                 if (!radius_xlat(basedn, sizeof(basedn), inst->basedn,
1244                                 request, NULL)) {
1245                         radlog (L_ERR, "rlm_ldap: unable to create basedn.\n");
1246                         return RLM_MODULE_INVALID;
1247                 }
1248
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;
1252                 }
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);
1258                         }
1259                         ldap_release_conn(conn_id,inst->conns);
1260                         return (res);
1261                 }
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;
1266                 }
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;
1272                 }
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);
1277         }
1278
1279         user_dn = vp_user_dn->strvalue;
1280
1281         DEBUG("rlm_ldap: user DN: %s", user_dn);
1282
1283         ld_user = ldap_connect(instance, user_dn, request->password->strvalue,
1284                                1, &res);
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);
1289                 return (res);
1290         }
1291
1292         DEBUG("rlm_ldap: user %s authenticated succesfully",
1293               request->username->strvalue);
1294         ldap_unbind_s(ld_user);
1295
1296         return RLM_MODULE_OK;
1297 }
1298
1299 static LDAP    *
1300 ldap_connect(void *instance, const char *dn, const char *password, int auth, int *result)
1301 {
1302         ldap_instance  *inst = instance;
1303         LDAP           *ld;
1304         int             msgid, rc, ldap_version;
1305         int             ldap_errno = 0;
1306         LDAPMessage    *res;
1307
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;
1312                 return (NULL);
1313         }
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);
1316         }
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);
1319         }
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);
1322         }
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));
1330                 }
1331         }
1332
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;
1343                                 ldap_unbind_s(ld);
1344                                 return (NULL);
1345                         }
1346                 }
1347         }
1348 #endif /* HAVE_LDAP_START_TLS */
1349
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);
1352         if (msgid == -1) {
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;
1358                 ldap_unbind_s(ld);
1359                 return (NULL);
1360         }
1361         DEBUG("rlm_ldap: waiting for bind result ...");
1362
1363         rc = ldap_result(ld, msgid, 1, &(inst->timeout), &res);
1364
1365         if(rc < 1) {
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;
1372                 ldap_unbind_s(ld);
1373                 return (NULL);
1374         }
1375         ldap_errno = ldap_result2error(ld, res, 1);
1376         switch (ldap_errno) {
1377         case LDAP_SUCCESS:
1378                 *result = RLM_MODULE_OK;
1379                 break;
1380
1381         case LDAP_INVALID_CREDENTIALS:
1382                 if (auth) 
1383                         *result = RLM_MODULE_REJECT;
1384                 else {
1385                         radlog(L_ERR, "rlm_ldap: LDAP login failed: check login, password settings in ldap section of radiusd.conf");
1386                         *result = RLM_MODULE_FAIL;
1387                 }               
1388                 break;
1389                 
1390         default:
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;
1395         }
1396
1397         if (*result != RLM_MODULE_OK) {
1398                 ldap_unbind_s(ld);
1399                 ld = NULL;
1400         }
1401         return ld;
1402 }
1403
1404 /*****************************************************************************
1405  *
1406  *      Detach from the LDAP server and cleanup internal state.
1407  *
1408  *****************************************************************************/
1409 static int 
1410 ldap_detach(void *instance)
1411 {
1412         ldap_instance  *inst = instance;
1413         TLDAP_RADIUS *pair, *nextpair;
1414
1415         if (inst->server)
1416                 free((char *) inst->server);
1417         if (inst->login)
1418                 free((char *) inst->login);
1419         if (inst->password)
1420                 free((char *) inst->password);
1421         if (inst->basedn)
1422                 free((char *) inst->basedn);
1423         if (inst->dictionary_mapping)
1424                 free(inst->dictionary_mapping);
1425         if (inst->filter)
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);
1437         if (inst->conns){
1438                 int i=0;
1439
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);
1445                         }
1446                         pthread_mutex_destroy(&inst->conns[i].mutex);
1447                 }
1448                 free(inst->conns);
1449         }
1450
1451         pair = inst->check_item_map;
1452         
1453         while (pair != NULL) {
1454                 nextpair = pair->next;
1455                 free(pair->attr);
1456                 free(pair->radius_attr);
1457                 free(pair);
1458                 pair = nextpair;
1459         }
1460
1461         pair = inst->reply_item_map;
1462
1463         while (pair != NULL) {
1464                 nextpair = pair->next;
1465                 free(pair->attr);
1466                 free(pair->radius_attr);
1467                 free(pair);
1468                 pair = nextpair;
1469         }
1470
1471         if (inst->atts){
1472                 int i = 0;
1473
1474                 while(inst->atts[i])
1475                         free(inst->atts[i++]);
1476                 free(inst->atts);
1477         }
1478
1479         paircompare_unregister(PW_LDAP_GROUP, ldap_groupcmp);
1480         xlat_unregister(inst->xlat_name,ldap_xlat);
1481         free(inst->xlat_name);
1482
1483         free(inst);
1484
1485         return 0;
1486 }
1487
1488 #ifdef FIELDCPY
1489 static void 
1490 fieldcpy(char *string, char **uptr)
1491 {
1492         char           *ptr;
1493
1494         ptr = *uptr;
1495         while (*ptr == ' ' || *ptr == '\t') {
1496                 ptr++;
1497         }
1498         if (*ptr == '"') {
1499                 ptr++;
1500                 while (*ptr != '"' && *ptr != '\0' && *ptr != '\n') {
1501                         *string++ = *ptr++;
1502                 }
1503                 *string = '\0';
1504                 if (*ptr == '"') {
1505                         ptr++;
1506                 }
1507                 *uptr = ptr;
1508                 return;
1509         }
1510         while (*ptr != ' ' && *ptr != '\t' && *ptr != '\0' && *ptr != '\n' &&
1511                *ptr != '=' && *ptr != ',') {
1512                 *string++ = *ptr++;
1513         }
1514         *string = '\0';
1515         *uptr = ptr;
1516         return;
1517 }
1518 #endif
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> )
1523  *
1524  *****************************************************************************/
1525
1526 static VALUE_PAIR *
1527 ldap_pairget(LDAP * ld, LDAPMessage * entry,
1528              TLDAP_RADIUS * item_map, VALUE_PAIR **pairs,char is_check)
1529 {
1530         char          **vals;
1531         int             vals_count;
1532         int             vals_idx;
1533         char           *ptr;
1534         TLDAP_RADIUS   *element;
1535         LRAD_TOKEN      token;
1536         int             is_generic_attribute;
1537         char            value[256];
1538         VALUE_PAIR     *pairlist = NULL;
1539         VALUE_PAIR     *newpair = NULL;
1540
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 */
1546
1547                         if (strcasecmp(element->radius_attr, GENERIC_ATTRIBUTE_ID)==0)
1548                                 is_generic_attribute = 1;
1549                         else
1550                                 is_generic_attribute = 0;
1551
1552                         /* find out how many values there are for the attribute and extract all of them */
1553
1554                         vals_count = ldap_count_values(vals);
1555
1556                         for (vals_idx = 0; vals_idx < vals_count; vals_idx++) {
1557                                 ptr = vals[vals_idx];
1558
1559                                 if (is_generic_attribute) {
1560                                         /* this is a generic attribute */
1561                                         LRAD_TOKEN dummy; /* makes pairread happy */
1562                                         
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);
1568                                         } else {
1569                                                 radlog(L_ERR, "rlm_ldap: parsing %s failed: %s", 
1570                                                        element->attr, vals[vals_idx]);
1571                                         }
1572                                 } else {
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;
1577                                         } else {
1578                                                 gettoken(&ptr, value, sizeof(value) - 1);
1579                                         }
1580                                         if (value[0] == 0) {
1581                                                 DEBUG("rlm_ldap: Attribute %s has no value", element->attr);
1582                                                 break;
1583                                         }
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)
1586                                                 continue;
1587                                         if (! vals_idx){
1588                                                 pairdelete(pairs,newpair->attribute);
1589                                         }
1590                                         pairadd(&pairlist, newpair);
1591                                 }
1592                         }
1593                         ldap_value_free(vals);
1594                 }
1595         }
1596
1597         return (pairlist);
1598 }
1599
1600 /* globally exported name */
1601 module_t        rlm_ldap = {
1602         "LDAP",
1603         RLM_TYPE_THREAD_SAFE,   /* type: reserved        */
1604         NULL,                   /* initialization        */
1605         ldap_instantiate,       /* instantiation         */
1606         {
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             */
1615         },
1616         ldap_detach,            /* detach                */
1617         NULL,                   /* destroy               */
1618 };