More fixes
[freeradius.git] / src / modules / rlm_ldap / ldap.c
1 /*
2  *   This program is is free software; you can redistribute it and/or modify
3  *   it under the terms of the GNU General Public License, version 2 if the
4  *   License as published by the Free Software Foundation.
5  *
6  *   This program is distributed in the hope that it will be useful,
7  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
8  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9  *   GNU General Public License for more details.
10  *
11  *   You should have received a copy of the GNU General Public License
12  *   along with this program; if not, write to the Free Software
13  *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
14  */
15  
16 /**
17  * $Id$
18  * @file ldap.c
19  * @brief LDAP module library functions.
20  *
21  * @author Arran Cudbard-Bell <a.cudbardb@freeradius.org>
22  * @copyright 2013 Network RADIUS SARL <info@networkradius.com>
23  * @copyright 2013 The FreeRADIUS Server Project.
24  */
25 #include        <freeradius-devel/radiusd.h>
26 #include        <freeradius-devel/modules.h>
27 #include        <freeradius-devel/rad_assert.h>
28
29 #include        <stdarg.h>
30 #include        <ctype.h>
31
32 #include        <lber.h>
33 #include        <ldap.h>
34 #include        "ldap.h"
35
36
37
38 /** Converts "bad" strings into ones which are safe for LDAP
39  *
40  * This is a callback for xlat operations.
41  *
42  * Will escape any characters in input strings that would cause the string to be interpreted as part of a DN and or
43  * filter. Escape sequence is @verbatim \<hex><hex> @endverbatim
44  *
45  * @param request The current request.
46  * @param out Pointer to output buffer.
47  * @param outlen Size of the output buffer.
48  * @param in Raw unescaped string.
49  * @param arg Any additional arguments (unused).
50  */
51 size_t rlm_ldap_escape_func(UNUSED REQUEST *request, char *out, size_t outlen, const char *in, UNUSED void *arg)
52 {
53         static const char encode[] = ",+\"\\<>;*=()";
54         static const char hextab[] = "0123456789abcdef";
55         size_t left = outlen;
56         
57         if (*in && ((*in == ' ') || (*in == '#'))) {
58                 goto encode;
59         }
60         
61         while (*in) {
62                 /*
63                  *      Encode unsafe characters.
64                  */
65                 if (memchr(encode, *in, sizeof(encode) - 1)) {
66                         encode:
67
68                         /*
69                          *      Only 3 or less bytes available.
70                          */
71                         if (left <= 3) break;
72
73                         *out++ = '\\';
74                         *out++ = hextab[(*in >> 4) & 0x0f];
75                         *out++ = hextab[*in & 0x0f];
76                         in++;
77                         left -= 3;
78
79                         continue;
80                 }
81
82                 if (left <= 1) break;
83
84                 /*
85                  *      Doesn't need encoding
86                  */
87                 *out++ = *in++;
88                 left--;
89         }
90         
91         *out = '\0';
92         
93         return outlen - left;
94 }
95
96 /** Check whether a string is a DN
97  *
98  * @param str to check.
99  * @return true if string is a DN, else false.
100  */
101 int rlm_ldap_is_dn(const char *str)
102 {
103         return strrchr(str, ',') == NULL ? FALSE : TRUE;
104 }
105
106 /** Find the place at which the two DN strings diverge
107  * 
108  * Returns the length of the non matching string in full.
109  *
110  * @param full DN.
111  * @param part Partial DN as returned by ldap_parse_result.
112  * @return the length of the portion of full which wasn't matched or -1 on error.
113  */
114 static size_t rlm_ldap_common_dn(const char *full, const char *part)
115 {
116         size_t f_len, p_len, i;
117         
118         if (!full) {
119                 return -1;
120         }
121         
122         f_len = strlen(full);
123         
124         if (!part) {
125                 return -1;
126         }
127         
128         p_len = strlen(part);
129         if (!p_len) {
130                 return f_len;
131         }
132         
133         if ((f_len < p_len) || !f_len) {
134                 return -1; 
135         }
136
137         for (i = 0; i < p_len; i++) {
138                 if (part[p_len - i] != full[f_len - i]) {
139                         return -1; 
140                 }
141         }
142
143         return f_len - p_len;
144 }
145
146 /** Combine and expand filters
147  *
148  * @param request Current request.
149  * @param out Where to write the expanded string.
150  * @param outlen Length of output buffer.
151  * @param sub Array of subfilters (may contain NULLs).
152  * @param sublen Number of potential subfilters in array.
153  * @return length of expanded data.
154  */
155 ssize_t rlm_ldap_xlat_filter(REQUEST *request, char *out, size_t outlen, const char **sub, size_t sublen)
156 {
157         char buffer[LDAP_MAX_FILTER_STR_LEN + 1];
158         const char *in;
159         char *p = NULL;
160         
161         size_t len;
162         
163         unsigned int i;
164         int cnt = 0;
165         
166         /*
167          *      Figure out how many filter elements we need to integrate
168          */
169         for (i = 0; i < sublen; i++) {
170                 if (sub[i] && *sub[i]) {
171                         in = sub[i];
172                         cnt++;
173                 } 
174         }
175
176         if (!cnt) {
177                 out[0] = '\0';
178                 return 0;
179         }
180
181         p = buffer;
182         if (cnt > 1) {
183                 if (outlen < 3) {
184                         goto oob;
185                 }
186                 
187                 p[len++] = '(';
188                 p[len++] = '&';
189                 
190                 for (i = 0; i < sublen; i++) {
191                         if (sub[i] && (*sub[i] != '\0')) {
192                                 len += strlcpy(p + len, sub[i], outlen - len);
193                                 
194                                 if (len >= outlen) {
195                                         oob:
196                                         RDEBUGE("Out of buffer space creating filter");
197                                         
198                                         return -1;
199                                 }
200                         } 
201                 }
202                 
203                 if ((outlen - len) < 2) {
204                         goto oob;
205                 }
206                 
207                 p[len++] = ')';
208                 p[len] = '\0';
209                 
210                 in = buffer;
211         }
212
213         len = radius_xlat(out, outlen, in, request, rlm_ldap_escape_func, NULL);
214         if (!len) {
215                 RDEBUGE("Failed creating filter");
216                 
217                 return -1;
218         }
219         
220         return len;
221 }
222
223 /** Parse response from LDAP server dealing with any errors
224  *
225  * Should be called after an LDAP operation. Will check result of operation and if it was successful, then attempt 
226  * to retrieve and parse the result.
227  *
228  * Will also produce extended error output including any messages the server sent, and information about partial 
229  * DN matches.
230  *
231  * @param[in] inst of LDAP module.
232  * @param[in] conn Current connection.
233  * @param[in] msgid returned from last operation.
234  * @param[in] dn Last search or bind DN.
235  * @param[out] result Where to write result, if NULL result will be freed.
236  * @param[out] error Where to write the error string, may be NULL, must not be freed.
237  * @param[out] extra Where to write additional error string to, may be NULL (faster) or must be freed 
238  *      (with talloc_free).
239  * @return One of the LDAP_PROC_* codes.
240  */
241 static ldap_rcode_t rlm_ldap_result(const ldap_instance_t *inst, const ldap_handle_t *conn, int msgid, const char *dn,
242                                     LDAPMessage **result, const char **error, char **extra)
243 {
244         ldap_rcode_t status = LDAP_PROC_SUCCESS;
245
246         int lib_errno = LDAP_SUCCESS;   // errno returned by the library.
247         int srv_errno = LDAP_SUCCESS;   // errno in the result message.
248         
249         char *part_dn = NULL;           // Partial DN match.
250         char *our_err = NULL;           // Our extended error message.
251         char *srv_err = NULL;           // Server's extended error message.
252         char *p, *a;
253
254         int freeit = FALSE;             // Whether the message should be freed after being processed.
255         int len;
256         
257         struct timeval tv;              // Holds timeout values.
258         
259         LDAPMessage *tmp_msg;           // Temporary message pointer storage if we weren't provided with one.
260         
261         const char *tmp_err;            // Temporary error pointer storage if we weren't provided with one.
262         
263         if (!error) {
264                 error = &tmp_err;
265         }
266         *error = NULL;
267         
268         if (extra) {
269                 *extra = NULL;
270         }
271         
272         /*
273          *      We always need the result, but our caller may not
274          */
275         if (!result) {
276                 result = &tmp_msg;
277                 freeit = TRUE;
278         }
279         
280         *result = NULL;
281         
282         /*
283          *      Check if there was an error sending the request
284          */
285         ldap_get_option(conn->handle, LDAP_OPT_ERROR_NUMBER,
286                         &lib_errno);
287         if (lib_errno != LDAP_SUCCESS) {
288                 goto process_error;
289         }
290         
291         memset(&tv, 0, sizeof(tv));
292         tv.tv_sec = inst->res_timeout;
293         
294         /*
295          *      Now retrieve the result and check for errors
296          *      ldap_result returns -1 on error, and 0 on timeout
297          */
298         lib_errno = ldap_result(conn->handle, msgid, 1, &tv, result);
299         if (lib_errno == 0) {
300                 lib_errno = LDAP_TIMEOUT;
301                 
302                 goto process_error;
303         }
304         
305         if (lib_errno == -1) {
306                 ldap_get_option(conn->handle, LDAP_OPT_ERROR_NUMBER,
307                                 &lib_errno);
308                 goto process_error;
309         }
310         
311         /*
312          *      Parse the result and check for errors sent by the server
313          */
314         lib_errno = ldap_parse_result(conn->handle, *result,
315                                       &srv_errno,
316                                       extra ? &part_dn : NULL,
317                                       extra ? &srv_err : NULL,
318                                       NULL, NULL, freeit);
319         if (freeit) {
320                 *result = NULL;
321         }
322         
323         if (lib_errno != LDAP_SUCCESS) {
324                 ldap_get_option(conn->handle, LDAP_OPT_ERROR_NUMBER,
325                                 &lib_errno);
326                 goto process_error;
327         }
328         
329         process_error:
330         
331         if ((lib_errno == LDAP_SUCCESS) && (srv_errno != LDAP_SUCCESS)) {
332                 lib_errno = srv_errno;
333         } else if ((lib_errno != LDAP_SUCCESS) && (srv_errno == LDAP_SUCCESS)) {
334                 srv_errno = lib_errno;
335         }
336         
337         switch (lib_errno) {
338         case LDAP_SUCCESS:
339                 *error = "Success";
340                 
341                 break;
342
343         case LDAP_NO_SUCH_OBJECT:
344                 *error = "The specified DN wasn't found, check base_dn and identity";
345                 
346                 status = LDAP_PROC_BAD_DN;
347                 
348                 if (!extra) break;
349                 
350                 /* 
351                  *      Build our own internal diagnostic string
352                  */
353                 len = rlm_ldap_common_dn(dn, part_dn);
354                 if (len < 0) break;
355                 
356                 our_err = talloc_asprintf(conn, "Match stopped here: [%.*s]%s", len, dn, part_dn ? part_dn : "");
357
358                 goto error_string;
359
360         case LDAP_INSUFFICIENT_ACCESS:
361                 *error = "Insufficient access. Check the identity and password configuration directives";
362                 
363                 status = LDAP_PROC_NOT_PERMITTED;
364                 break;
365                 
366         case LDAP_UNWILLING_TO_PERFORM:
367                 *error = "Server was unwilling to perform";
368         
369                 status = LDAP_PROC_NOT_PERMITTED;
370                 break;
371                 
372         case LDAP_TIMEOUT:
373                 exec_trigger(NULL, inst->cs, "modules.ldap.timeout", TRUE);
374                 
375                 *error = "Timed out while waiting for server to respond";
376                        
377                 status = LDAP_PROC_ERROR;
378                 break;
379                 
380         case LDAP_FILTER_ERROR:
381                 *error = "Bad search filter";
382
383                 status = LDAP_PROC_ERROR;
384                 break;
385                 
386         case LDAP_TIMELIMIT_EXCEEDED:
387                 exec_trigger(NULL, inst->cs, "modules.ldap.timeout", TRUE);
388                 
389                 *error = "Time limit exceeded";
390                 /* FALL-THROUGH */
391
392         case LDAP_BUSY:
393         case LDAP_UNAVAILABLE:
394         case LDAP_SERVER_DOWN:
395                 status = LDAP_PROC_RETRY;
396                 
397                 goto error_string;
398                 
399         case LDAP_INVALID_CREDENTIALS:
400         case LDAP_CONSTRAINT_VIOLATION:
401                 status = LDAP_PROC_REJECT;
402                 
403                 goto error_string;
404                 
405         case LDAP_OPERATIONS_ERROR:
406                 *error = "Please set 'chase_referrals=yes' and 'rebind=yes'. See the ldap module configuration "
407                          "for details.";
408                          
409                 /* FALL-THROUGH */
410         default:
411                 status = LDAP_PROC_ERROR;
412                 
413                 error_string:
414                 
415                 if (!*error) {
416                         *error = ldap_err2string(lib_errno);
417                 }
418                 
419                 if (!extra || ((lib_errno == srv_errno) && !our_err && !srv_err)) {
420                         break;
421                 }
422                 
423                 /*
424                  *      Output the error codes from the library and server
425                  */
426                 p = talloc_zero_array(conn, char, 1);
427                 if (!p) break;
428
429                 if (lib_errno != srv_errno) {
430                         a = talloc_asprintf_append(p, "LDAP lib error: %s (%u), srv error: %s (%u). ", 
431                                                    ldap_err2string(lib_errno), lib_errno,
432                                                    ldap_err2string(srv_errno), srv_errno);
433                         if (!a) {
434                                 talloc_free(p);
435                                 break;
436                         }
437                         
438                         p = a;
439                 }
440
441                 if (our_err) {
442                         a = talloc_asprintf_append_buffer(p, "%s. ", our_err);
443                         if (!a) {
444                                 talloc_free(p);
445                                 break;
446                         }
447                         
448                         p = a;
449                 }
450                 
451                 if (srv_err) {
452                         a = talloc_asprintf_append_buffer(p, "Server said: %s. ", srv_err);
453                         if (!a) {
454                                 talloc_free(p);
455                                 break;
456                         }
457                         
458                         p = a;
459                 }
460                 
461                 *extra = p;
462                 
463                 break;
464         }
465         
466         /*
467          *      Cleanup memory
468          */
469         if (srv_err) {
470                 ldap_memfree(srv_err);
471         }
472         
473         if (part_dn) {
474                 ldap_memfree(part_dn);
475         }
476         
477         if (our_err) {
478                 talloc_free(our_err);
479         }
480         
481         if ((lib_errno || srv_errno) && *result) {
482                 ldap_msgfree(*result);
483                 *result = NULL;
484         }
485         
486         return status;
487 }
488
489 /** Bind to the LDAP directory as a user
490  *
491  * Performs a simple bind to the LDAP directory, and handles any errors that occur.
492  *
493  * @param[in] inst rlm_ldap configuration.
494  * @param[in] request Current request, this may be NULL, in which case all debug logging is done with radlog.
495  * @param[in,out] pconn to use. May change as this function calls functions which auto re-connect.
496  * @param[in] dn of the user, may be NULL to bind anonymously.
497  * @param[in] password of the user, may be NULL if no password is specified.
498  * @param[in] retry if the server is down.
499  * @return one of the LDAP_PROC_* values.
500  */
501 ldap_rcode_t rlm_ldap_bind(const ldap_instance_t *inst, REQUEST *request, ldap_handle_t **pconn, const char *dn,
502                            const char *password, int retry)
503 {
504         ldap_rcode_t    status;
505         
506         int             msgid;
507         
508         const char      *error = NULL;
509         char            *extra = NULL;
510
511         rad_assert(*pconn && (*pconn)->handle);
512         
513         /*
514          *      Bind as anonymous user
515          */
516         if (!dn) dn = "";
517
518 retry:
519         msgid = ldap_bind((*pconn)->handle, dn, password, LDAP_AUTH_SIMPLE);
520         /* We got a valid message ID */
521         if (msgid >= 0) {
522                 if (request) {
523                         RDEBUG2("Waiting for bind result...");
524                 } else {
525                         DEBUG2("rlm_ldap (%s): Waiting for bind result...", inst->xlat_name);
526                 }
527         }
528
529         status = rlm_ldap_result(inst, *pconn, msgid, dn, NULL, &error, &extra);
530         switch (status) {
531         case LDAP_PROC_SUCCESS:
532                 break;
533         case LDAP_PROC_NOT_PERMITTED:
534                 LDAP_ERR_REQ("Bind was not permitted: %s", error);
535                 LDAP_EXT_REQ();
536                 
537                 break;
538
539         case LDAP_PROC_REJECT:
540                 LDAP_ERR_REQ("Bind credentials incorrect: %s", error);
541                 LDAP_EXT_REQ();
542
543                 break;
544
545         case LDAP_PROC_RETRY:
546                 if (retry) {
547                         *pconn = fr_connection_reconnect(inst->pool, *pconn);
548                         if (*pconn) {
549                                 LDAP_DBGW_REQ("Bind with %s to %s:%d failed: %s. Got new socket, retrying...",
550                                               dn, inst->server, inst->port, error);
551                                 
552                                 talloc_free(extra); /* don't leak debug info */
553                                 
554                                 goto retry;
555                         }
556                 };
557                 
558                 status = LDAP_PROC_ERROR;
559                 
560                 /*
561                  *      Were not allowed to retry, or there are no more
562                  *      sockets, treat this as a hard failure.
563                  */
564                 /* FALL-THROUGH */
565         default:
566 #ifdef HAVE_LDAP_INITIALIZE
567                 if (inst->is_url) {
568                         LDAP_ERR_REQ("Bind with %s to %s failed: %s", dn, inst->server, error);
569                 } else
570 #endif
571                 {
572                         LDAP_ERR_REQ("Bind with %s to %s:%d failed: %s", dn, inst->server,
573                                      inst->port, error);
574                 }
575                 LDAP_EXT_REQ();
576                 
577                 break;
578         }
579
580         if (extra) {
581                 talloc_free(extra);
582         }
583         
584         return status; /* caller closes the connection */
585 }
586
587
588 /** Search for something in the LDAP directory
589  *
590  * Binds as the administrative user and performs a search, dealing with any errors.
591  *
592  * @param[in] inst rlm_ldap configuration.
593  * @param[in] request Current request.
594  * @param[in,out] pconn to use. May change as this function calls functions which auto re-connect.
595  * @param[in] dn to use as base for the search.
596  * @param[in] scope to use (LDAP_SCOPE_BASE, LDAP_SCOPE_ONE, LDAP_SCOPE_SUB).
597  * @param[in] filter to use, should be pre-escaped.
598  * @param[in] attrs to retrieve.
599  * @param[out] result Where to store the result. Must be freed with ldap_msgfree if LDAP_PROC_SUCCESS is returned.
600  *      May be NULL in which case result will be automatically freed after use.
601  * @return One of the LDAP_PROC_* values.
602  */
603 ldap_rcode_t rlm_ldap_search(const ldap_instance_t *inst, REQUEST *request, ldap_handle_t **pconn,
604                              const char *dn, int scope, const char *filter, const char * const *attrs,
605                              LDAPMessage **result)
606 {
607         ldap_rcode_t    status;
608         
609         int             msgid;          // Message id returned by
610                                         // ldap_search_ext.
611                                 
612         int             count = 0;      // Number of results we got.
613         
614         struct timeval  tv;             // Holds timeout values.
615         
616         const char      *error = NULL;
617         char            *extra = NULL;
618
619         rad_assert(*pconn && (*pconn)->handle);
620         
621         /*
622          *      OpenLDAP library doesn't declare attrs array as const, but
623          *      it really should be *sigh*.
624          */
625         char **search_attrs;
626         memcpy(&search_attrs, &attrs, sizeof(attrs));
627
628         /*
629          *      Do all searches as the admin user.
630          */
631         if ((*pconn)->rebound) {
632                 status = rlm_ldap_bind(inst, request, pconn, inst->admin_dn, inst->password, TRUE);
633                 if (status != LDAP_PROC_SUCCESS) {
634                         return LDAP_PROC_ERROR;
635                 }
636
637                 rad_assert(*pconn);
638                 
639                 (*pconn)->rebound = FALSE;
640         }
641
642         RDEBUG2("Performing search in '%s' with filter '%s'", dn, filter);
643
644         /*
645          *      If LDAP search produced an error it should also be logged
646          *      to the ld. result should pick it up without us
647          *      having to pass it explicitly.
648          */
649         memset(&tv, 0, sizeof(tv));
650         tv.tv_sec = inst->res_timeout;
651 retry:  
652         (void) ldap_search_ext((*pconn)->handle, dn, scope, filter, search_attrs, 0, NULL, NULL, &tv, 0, &msgid);
653
654         RDEBUG2("Waiting for search result...");               
655         status = rlm_ldap_result(inst, *pconn, msgid, dn, result, &error, &extra);                     
656         switch (status) {
657                 case LDAP_PROC_SUCCESS:
658                         break;
659                 case LDAP_PROC_RETRY:
660                         *pconn = fr_connection_reconnect(inst->pool, *pconn);
661                         if (*pconn) {
662                                 RDEBUGW("Search failed: %s. Got new socket, retrying...", error);
663                                 
664                                 talloc_free(extra); /* don't leak debug info */
665                                 
666                                 goto retry;
667                         }
668                         
669                         status = LDAP_PROC_ERROR;
670                         
671                         /* FALL-THROUGH */
672                 default:
673                         RDEBUGE("Failed performing search: %s", error);
674                         if (extra) RDEBUGE("%s", extra);
675
676                         goto finish;
677         }
678         
679         if (result) {   
680                 count = ldap_count_entries((*pconn)->handle, *result);
681                 if (count == 0) {
682                         ldap_msgfree(*result);
683                         *result = NULL;
684                 
685                         RDEBUG("Search returned no results");
686                 
687                         status = LDAP_PROC_NO_RESULT;
688                 }
689         }
690         
691         finish:
692         if (extra) {
693                 talloc_free(extra);
694         }
695         
696         return status;
697 }
698
699 /** Modify something in the LDAP directory
700  *
701  * Binds as the administrative user and attempts to modify an LDAP object.
702  *
703  * @param[in] inst rlm_ldap configuration.
704  * @param[in] request Current request.
705  * @param[in,out] pconn to use. May change as this function calls functions which auto re-connect.
706  * @param[in] dn of the object to modify.
707  * @param[in] mods to make, see 'man ldap_modify' for more information.
708  * @return One of the LDAP_PROC_* values.
709  */
710 ldap_rcode_t rlm_ldap_modify(const ldap_instance_t *inst, REQUEST *request, ldap_handle_t **pconn,
711                              const char *dn, LDAPMod *mods[])
712 {
713         ldap_rcode_t    status;
714         
715         int             msgid;          // Message id returned by ldap_search_ext.
716         
717         const char      *error = NULL;
718         char            *extra = NULL;                     
719
720         rad_assert(*pconn && (*pconn)->handle);
721                 
722         /*
723          *      Perform all modifications as the admin user.
724          */
725         if ((*pconn)->rebound) {
726                 status = rlm_ldap_bind(inst, request, pconn, inst->admin_dn, inst->password, TRUE);
727                 if (status != LDAP_PROC_SUCCESS) {
728                         return LDAP_PROC_ERROR;
729                 }
730
731                 rad_assert(*pconn);
732                 
733                 (*pconn)->rebound = FALSE;
734         }
735         
736         RDEBUG2("Modifying object with DN \"%s\"", dn);
737         retry:
738         (void) ldap_modify_ext((*pconn)->handle, dn, mods, NULL, NULL, &msgid);
739         
740         RDEBUG2("Waiting for modify result...");
741         status = rlm_ldap_result(inst, *pconn, msgid, dn, NULL, &error, &extra);
742         switch (status) {
743                 case LDAP_PROC_SUCCESS:
744                         break;
745                 case LDAP_PROC_RETRY:
746                         *pconn = fr_connection_reconnect(inst->pool, *pconn);
747                         if (*pconn) {
748                                 RDEBUGW("Modify failed: %s. Got new socket, retrying...", error);
749                                 
750                                 talloc_free(extra); /* don't leak debug info */
751                                 
752                                 goto retry;
753                         }
754                         
755                         status = LDAP_PROC_ERROR;
756                         
757                         /* FALL-THROUGH */
758                 default:
759                         RDEBUGE("Failed modifying object: %s", error);
760                         RDEBUGE("%s", extra);
761                         
762                         goto finish;
763         }                    
764         
765         finish:
766         if (extra) {
767                 talloc_free(extra);
768         }
769         
770         return status;
771 }
772
773 /** Retrieve the DN of a user object
774  *
775  * Retrieves the DN of a user and adds it to the control list as LDAP-UserDN. Will also retrieve any attributes
776  * passed and return the result in *result.
777  *
778  * This potentially allows for all authorization and authentication checks to be performed in one ldap search
779  * operation, which is a big bonus given the number of crappy, slow *cough*AD*cough* LDAP directory servers out there.
780  * 
781  * @param[in] inst rlm_ldap configuration.
782  * @param[in] request Current request.
783  * @param[in,out] pconn to use. May change as this function calls functions which auto re-connect.
784  * @param[in] attrs Additional attributes to retrieve, may be NULL.
785  * @param[in] force Query even if the User-DN already exists.
786  * @param[out] result Where to write the result, may be NULL in which case result is discarded.
787  * @param[out] rcode The status of the operation, one of the RLM_MODULE_* codes.
788  * @return The user's DN or NULL on error.
789  */
790 const char *rlm_ldap_find_user(const ldap_instance_t *inst, REQUEST *request, ldap_handle_t **pconn,
791                                const char *attrs[], int force, LDAPMessage **result, rlm_rcode_t *rcode)
792 {
793         static const char *tmp_attrs[] = { NULL };
794         
795         ldap_rcode_t    status;
796         VALUE_PAIR      *vp = NULL;
797         LDAPMessage     *tmp_msg = NULL, *entry = NULL;
798         int             ldap_errno;
799         char            *dn = NULL;
800         char            filter[LDAP_MAX_FILTER_STR_LEN];        
801         char            base_dn[LDAP_MAX_DN_STR_LEN];
802         
803         int freeit = FALSE;                                     //!< Whether the message should
804                                                                 //!< be freed after being processed.
805
806         *rcode = RLM_MODULE_FAIL;
807
808         if (!result) {
809                 result = &tmp_msg;
810                 freeit = TRUE;
811         }
812         *result = NULL;
813         
814         if (!attrs) {
815                 memset(&attrs, 0, sizeof(tmp_attrs));
816         }
817         
818         /*
819          *      If the caller isn't looking for the result we can just return the current userdn value.
820          */
821         if (!force) {
822                 vp = pairfind(request->config_items, PW_LDAP_USERDN, 0, TAG_ANY);
823                 if (vp) {
824                         RDEBUG("Using user DN from request \"%s\"", vp->vp_strvalue);
825                         *rcode = RLM_MODULE_OK;
826                         return vp->vp_strvalue;
827                 }
828         }
829         
830         /*
831          *      Perform all searches as the admin user.
832          */
833         if ((*pconn)->rebound) {
834                 status = rlm_ldap_bind(inst, request, pconn, inst->admin_dn, inst->password, TRUE);
835                 if (status != LDAP_PROC_SUCCESS) {
836                         *rcode = RLM_MODULE_FAIL;
837                         return NULL;
838                 }
839
840                 rad_assert(*pconn);
841                 
842                 (*pconn)->rebound = FALSE;
843         }
844         
845         if (!radius_xlat(filter, sizeof(filter), inst->userobj_filter, request, rlm_ldap_escape_func, NULL)) {
846                 RDEBUGE("Unable to create filter");
847                 
848                 *rcode = RLM_MODULE_INVALID;
849                 return NULL;
850         }
851
852         if (!radius_xlat(base_dn, sizeof(base_dn), inst->userobj_base_dn, request, rlm_ldap_escape_func, NULL)) {
853                 RDEBUGE("Unable to create base_dn");
854                 
855                 *rcode = RLM_MODULE_INVALID;
856                 return NULL;
857         }
858
859         status = rlm_ldap_search(inst, request, pconn, base_dn, inst->userobj_scope, filter, attrs, result);
860         switch (status) {
861                 case LDAP_PROC_SUCCESS:
862                         break;
863                 case LDAP_PROC_NO_RESULT:
864                         *rcode = RLM_MODULE_NOTFOUND;
865                         return NULL;
866                 default:
867                         *rcode = RLM_MODULE_FAIL;
868                         return NULL;
869         }
870         
871         rad_assert(*pconn);
872
873         entry = ldap_first_entry((*pconn)->handle, *result);
874         if (!entry) {
875                 ldap_get_option((*pconn)->handle, LDAP_OPT_RESULT_CODE, &ldap_errno);
876                 RDEBUGE("Failed retrieving entry: %s", 
877                         ldap_err2string(ldap_errno));
878                          
879                 goto finish;
880         }
881
882         dn = ldap_get_dn((*pconn)->handle, entry);
883         if (!dn) {
884                 ldap_get_option((*pconn)->handle, LDAP_OPT_RESULT_CODE, &ldap_errno);
885                                 
886                 RDEBUGE("Retrieving object DN from entry failed: %s",
887                         ldap_err2string(ldap_errno));
888                        
889                 goto finish;
890         }
891         
892         RDEBUG("User object found at DN \"%s\"", dn);
893         vp = pairmake(request, &request->config_items, "LDAP-UserDN", dn, T_OP_EQ);
894         if (vp) {       
895                 *rcode = RLM_MODULE_OK;
896         }
897         
898         finish:
899         ldap_memfree(dn);
900         
901         if ((freeit || (*rcode != RLM_MODULE_OK)) && *result) {
902                 ldap_msgfree(*result);
903                 *result = NULL;
904         }
905
906         return vp ? vp->vp_strvalue : NULL;
907 }
908
909 /** Check for presence of access attribute in result
910  *
911  * @param[in] inst rlm_ldap configuration.
912  * @param[in] request Current request.
913  * @param[in] conn used to retrieve access attributes.
914  * @param[in] entry retrieved by rlm_ldap_find_user or rlm_ldap_search.
915  * @return RLM_MODULE_USERLOCK if the user was denied access, else RLM_MODULE_OK.
916  */
917 rlm_rcode_t rlm_ldap_check_access(const ldap_instance_t *inst, REQUEST *request,
918                                   const ldap_handle_t *conn, LDAPMessage *entry)
919 {
920         rlm_rcode_t rcode = RLM_MODULE_OK;
921         char **vals = NULL;
922
923         vals = ldap_get_values(conn->handle, entry, inst->userobj_access_attr);
924         if (vals) {
925                 if (inst->access_positive && (strncmp(vals[0], "FALSE", 5) == 0)) {
926                         RDEBUG("\"%s\" attribute exists but is set to 'false' - user locked out");
927                         rcode = RLM_MODULE_USERLOCK;
928                 } else {
929                         RDEBUG("\"%s\" attribute exists - user locked out", inst->userobj_access_attr);
930                         rcode = RLM_MODULE_USERLOCK;
931                 }
932
933                 ldap_value_free(vals);
934         } else if (inst->access_positive) {
935                 RDEBUG("No \"%s\" attribute - user locked out", inst->userobj_access_attr);
936                 rcode = RLM_MODULE_USERLOCK;
937         }
938
939         return rcode;
940 }
941
942 /** Verify we got a password from the search
943  *
944  * Checks to see if after the LDAP to RADIUS mapping has been completed that a reference password.
945  *
946  * @param inst rlm_ldap configuration.
947  * @param request Current request.
948  */
949 void rlm_ldap_check_reply(const ldap_instance_t *inst, REQUEST *request)
950 {
951        /*
952         *       More warning messages for people who can't be bothered to read the documentation.
953         *
954         *       Expect_password is set when we process the mapping, and is only true if there was a mapping between
955         *       an LDAP attribute and a password reference attribute in the control list.
956         */
957         if (inst->expect_password && (debug_flag > 1)) {
958                 if (!pairfind(request->config_items, PW_CLEARTEXT_PASSWORD, 0, TAG_ANY) &&
959                     !pairfind(request->config_items, PW_NT_PASSWORD, 0, TAG_ANY) &&
960                     !pairfind(request->config_items, PW_USER_PASSWORD, 0, TAG_ANY) &&
961                     !pairfind(request->config_items, PW_PASSWORD_WITH_HEADER, 0, TAG_ANY) &&
962                     !pairfind(request->config_items, PW_CRYPT_PASSWORD, 0, TAG_ANY)) {
963                         RDEBUGW("No \"reference\" password added. Ensure the admin user has permission to "
964                                 "read the password attribute");
965                         RDEBUGW("PAP authentication will *NOT* work with Active Directory (if that is what you "
966                                 "were trying to configure)");
967                 }
968        }
969 }
970
971 #if LDAP_SET_REBIND_PROC_ARGS == 3
972 /** Callback for OpenLDAP to rebind and chase referrals
973  *
974  * Called by OpenLDAP when it receives a referral and has to rebind.
975  *
976  * @param handle to rebind.
977  * @param url to bind to.
978  * @param request that triggered the rebind.
979  * @param msgid that triggered the rebind.
980  * @param ctx rlm_ldap configuration.
981  */
982 static int rlm_ldap_rebind(LDAP *handle, LDAP_CONST char *url, UNUSED ber_tag_t request, UNUSED ber_int_t msgid,
983                            void *ctx)
984 {
985         ldap_rcode_t status;
986         ldap_handle_t *conn = ctx;
987         
988         int ldap_errno;
989
990         conn->referred = TRUE;
991         conn->rebound = TRUE;   /* not really, but oh well... */
992         rad_assert(handle == conn->handle);
993
994         DEBUG("rlm_ldap (%s): Rebinding to URL %s", conn->inst->xlat_name, url);
995
996         status = rlm_ldap_bind(conn->inst, NULL, &conn, conn->inst->admin_dn, conn->inst->password, FALSE);
997         if (status != LDAP_PROC_SUCCESS) {
998                 ldap_get_option(handle, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
999                         
1000                 return ldap_errno;
1001         }
1002         
1003
1004         return LDAP_SUCCESS;
1005 }
1006 #endif
1007
1008 /** Create and return a new connection
1009  *
1010  * Create a new ldap connection and allocate memory for a new rlm_handle_t
1011  *
1012  * @param ctx rlm_ldap instance.
1013  * @return A new connection handle or NULL on error.
1014  */
1015 void *rlm_ldap_conn_create(void *ctx)
1016 {
1017         ldap_rcode_t status;
1018         
1019         int ldap_errno, ldap_version;
1020         struct timeval tv;
1021         
1022         ldap_instance_t *inst = ctx;
1023         LDAP *handle = NULL;
1024         ldap_handle_t *conn = NULL;
1025
1026 #ifdef HAVE_LDAP_INITIALIZE
1027         if (inst->is_url) {
1028                 DEBUG("rlm_ldap (%s): Connecting to %s", inst->xlat_name, inst->server);
1029                 
1030                 ldap_errno = ldap_initialize(&handle, inst->server);
1031                 if (ldap_errno != LDAP_SUCCESS) {
1032                         LDAP_ERR("ldap_initialize failed: %s", ldap_err2string(ldap_errno));
1033                         goto error;
1034                 }
1035         } else
1036 #endif
1037         {
1038                 DEBUG("rlm_ldap (%s): Connecting to %s:%d", inst->xlat_name, inst->server, inst->port);
1039
1040                 handle = ldap_init(inst->server, inst->port);
1041                 if (!handle) {
1042                         LDAP_ERR("ldap_init() failed");
1043                         goto error;
1044                 }
1045         }
1046
1047         /*
1048          *      We now have a connection structure, but no actual TCP connection.
1049          *
1050          *      Set a bunch of LDAP options, using common code.
1051          */
1052 #define do_ldap_option(_option, _name, _value) \
1053         if (ldap_set_option(handle, _option, _value) != LDAP_OPT_SUCCESS) { \
1054                 ldap_get_option(handle, LDAP_OPT_ERROR_NUMBER, &ldap_errno); \
1055                 LDAP_ERR("Could not set %s: %s", _name, ldap_err2string(ldap_errno)); \
1056         }
1057                 
1058         if (inst->ldap_debug) {
1059                 do_ldap_option(LDAP_OPT_DEBUG_LEVEL, "ldap_debug", &(inst->ldap_debug));
1060         }
1061
1062         /*
1063          *      Leave "chase_referrals" unset to use the OpenLDAP default.
1064          */
1065         if (inst->chase_referrals != 2) {
1066                 if (inst->chase_referrals) {
1067                         do_ldap_option(LDAP_OPT_REFERRALS, "chase_referrals", LDAP_OPT_ON);
1068                         
1069                         if (inst->rebind == 1) {
1070 #if LDAP_SET_REBIND_PROC_ARGS == 3
1071                                 ldap_set_rebind_proc(handle, rlm_ldap_rebind, inst);
1072 #else
1073                                 DEBUGW("The flag 'rebind = yes' is not supported by the system LDAP library. "
1074                                        "Ignoring.");
1075 #endif
1076                         }
1077                 } else {
1078                         do_ldap_option(LDAP_OPT_REFERRALS, "chase_referrals", LDAP_OPT_OFF);
1079                 }
1080         }
1081
1082         memset(&tv, 0, sizeof(tv));
1083         tv.tv_sec = inst->net_timeout;
1084
1085         do_ldap_option(LDAP_OPT_NETWORK_TIMEOUT, "net_timeout", &tv);
1086
1087         do_ldap_option(LDAP_OPT_TIMELIMIT, "srv_timelimit", &(inst->srv_timelimit));
1088
1089         ldap_version = LDAP_VERSION3;
1090         do_ldap_option(LDAP_OPT_PROTOCOL_VERSION, "ldap_version", &ldap_version);
1091
1092 #ifdef LDAP_OPT_X_KEEPALIVE_IDLE
1093         do_ldap_option(LDAP_OPT_X_KEEPALIVE_IDLE, "keepalive idle", &(inst->keepalive_idle));
1094 #endif
1095
1096 #ifdef LDAP_OPT_X_KEEPALIVE_PROBES
1097         do_ldap_option(LDAP_OPT_X_KEEPALIVE_PROBES, "keepalive probes", &(inst->keepalive_probes));
1098 #endif
1099
1100 #ifdef LDAP_OPT_X_KEEPALIVE_INTERVAL
1101         do_ldap_option(LDAP_OPT_X_KEEPALIVE_INTERVAL, "keepalive interval", &(inst->keepalive_interval));
1102 #endif
1103
1104 #ifdef HAVE_LDAP_START_TLS
1105         /*
1106          *      Set all of the TLS options
1107          */
1108         if (inst->tls_mode) {
1109                 do_ldap_option(LDAP_OPT_X_TLS, "tls_mode", &(inst->tls_mode));
1110         }
1111
1112 #  define maybe_ldap_option(_option, _name, _value) \
1113         if (_value) do_ldap_option(_option, _name, _value)
1114
1115         maybe_ldap_option(LDAP_OPT_X_TLS_CACERTFILE, "cacertfile", inst->tls_cacertfile);
1116         maybe_ldap_option(LDAP_OPT_X_TLS_CACERTDIR, "cacertdir", inst->tls_cacertdir);
1117
1118 #  ifdef HAVE_LDAP_INT_TLS_CONFIG
1119         if (ldap_int_tls_config(NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, inst->tls_require_cert) != LDAP_OPT_SUCCESS) {
1120                 ldap_get_option(handle, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
1121                 
1122                 LDAP_ERR("Could not set LDAP_OPT_X_TLS_REQUIRE_CERT option to %s: %s", inst->tls_require_cert,
1123                          ldap_err2string(ldap_errno));
1124         }
1125 #  endif
1126
1127         /*
1128          *      Set certificate options
1129          */
1130         maybe_ldap_option(LDAP_OPT_X_TLS_CERTFILE, "certfile", inst->tls_certfile);
1131         maybe_ldap_option(LDAP_OPT_X_TLS_KEYFILE, "keyfile", inst->tls_keyfile);
1132         maybe_ldap_option(LDAP_OPT_X_TLS_RANDOM_FILE, "randfile", inst->tls_randfile);
1133
1134         /*
1135          *      And finally start the TLS code.
1136          */
1137         if (inst->start_tls) {
1138                 if (inst->port == 636) {
1139                         DEBUGW("Told to Start TLS on LDAPS port this will probably fail, please correct the "
1140                                "configuration");
1141                 }
1142                 
1143                 if (ldap_start_tls_s(handle, NULL, NULL) != LDAP_SUCCESS) {
1144                         ldap_get_option(handle, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
1145
1146                         LDAP_ERR("Could not start TLS: %s", ldap_err2string(ldap_errno));
1147                         goto error;
1148                 }
1149         }
1150 #endif /* HAVE_LDAP_START_TLS */
1151
1152         /*
1153          *      Allocate memory for the handle.
1154          */
1155         conn = talloc_zero(ctx, ldap_handle_t);
1156         conn->inst = inst;
1157         conn->handle = handle;
1158         conn->rebound = FALSE;
1159         conn->referred = FALSE;
1160
1161         status = rlm_ldap_bind(inst, NULL, &conn, inst->admin_dn, inst->password, FALSE);
1162         if (status != LDAP_PROC_SUCCESS) {
1163                 goto error;
1164         }
1165
1166         return conn;
1167         
1168         error:
1169         if (handle) ldap_unbind_s(handle);
1170         if (conn) talloc_free(conn);
1171         
1172         return NULL;
1173 }
1174
1175
1176 /** Close and delete a connection
1177  *
1178  * Unbinds the LDAP connection, informing the server and freeing any memory, then releases the memory used by the 
1179  * connection handle.
1180  *
1181  * @param ctx unused.
1182  * @param connection to destroy.
1183  * @return always indicates success.
1184  */
1185 int rlm_ldap_conn_delete(UNUSED void *ctx, void *connection)
1186 {
1187         ldap_handle_t *conn = connection;
1188
1189         ldap_unbind_s(conn->handle);
1190         talloc_free(conn);
1191
1192         return 0;
1193 }
1194
1195
1196 /** Gets an LDAP socket from the connection pool
1197  *
1198  * Retrieve a socket from the connection pool, or NULL on error (of if no sockets are available).
1199  *
1200  * @param inst rlm_ldap configuration.
1201  * @param request Current request.
1202  */
1203 ldap_handle_t *rlm_ldap_get_socket(const ldap_instance_t *inst, REQUEST *request)
1204 {
1205         ldap_handle_t *conn;
1206
1207         conn = fr_connection_get(inst->pool);
1208         if (!conn) {
1209                 RDEBUGE("All ldap connections are in use");
1210                 
1211                 return NULL;
1212         }
1213
1214         return conn;
1215 }
1216
1217 /** Frees an LDAP socket back to the connection pool
1218  *
1219  * If the socket was rebound chasing a referral onto another server then we destroy it.
1220  * If the socket was rebound to another user on the same server, we let the next caller rebind it.
1221  *
1222  * @param inst rlm_ldap configuration.
1223  * @param conn to release.
1224  */
1225 void rlm_ldap_release_socket(const ldap_instance_t *inst, ldap_handle_t *conn)
1226 {
1227         /*
1228          *      Could have already been free'd due to a previous error.
1229          */
1230         if (!conn) return;
1231
1232         /*
1233          *      We chased a referral to another server.
1234          *
1235          *      This connection is no longer part of the pool which is connected to and bound to the configured server.
1236          *      Close it.
1237          *
1238          *      Note that we do NOT close it if it was bound to another user.  Instead, we let the next caller do the
1239          *      rebind.
1240          */
1241         if (conn->referred) {
1242                 fr_connection_del(inst->pool, conn);
1243                 return;
1244         }
1245
1246         fr_connection_release(inst->pool, conn);
1247         return;
1248 }