Add more version checks for modules
[freeradius.git] / src / modules / rlm_ldap / rlm_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 rlm_ldap.c
19  * @brief LDAP authorization and authentication module.
20  *
21  * @author Arran Cudbard-Bell <a.cudbardb@freeradius.org>
22  * @author Alan DeKok <aland@freeradius.org>
23  *
24  * @copyright 2013 Network RADIUS SARL <info@networkradius.com>
25  * @copyright 2012-2013 Arran Cudbard-Bell <a.cudbardb@freeradius.org>
26  * @copyright 2012 Alan DeKok <aland@freeradius.org>
27  * @copyright 1999-2013 The FreeRADIUS Server Project.
28  */
29 RCSID("$Id$")
30
31 #include        <freeradius-devel/rad_assert.h>
32
33 #include        <stdarg.h>
34 #include        <ctype.h>
35
36 #include        "ldap.h"
37
38 /*
39  *      Scopes
40  */
41 FR_NAME_NUMBER const ldap_scope[] = {
42         { "sub",        LDAP_SCOPE_SUB  },
43         { "one",        LDAP_SCOPE_ONE  },
44         { "base",       LDAP_SCOPE_BASE },
45 #ifdef LDAP_SCOPE_CHILDREN
46         { "children",   LDAP_SCOPE_CHILDREN },
47 #endif
48         {  NULL , -1 }
49 };
50
51 #ifdef LDAP_OPT_X_TLS_NEVER
52 FR_NAME_NUMBER const ldap_tls_require_cert[] = {
53         { "never",      LDAP_OPT_X_TLS_NEVER    },
54         { "demand",     LDAP_OPT_X_TLS_DEMAND   },
55         { "allow",      LDAP_OPT_X_TLS_ALLOW    },
56         { "try",        LDAP_OPT_X_TLS_TRY      },
57         { "hard",       LDAP_OPT_X_TLS_HARD     },      /* oh yes, just like that */
58
59         {  NULL , -1 }
60 };
61 #endif
62
63 FR_NAME_NUMBER const ldap_dereference[] = {
64         { "never",      LDAP_DEREF_NEVER        },
65         { "searching",  LDAP_DEREF_SEARCHING    },
66         { "finding",    LDAP_DEREF_FINDING      },
67         { "always",     LDAP_DEREF_ALWAYS       },
68
69         {  NULL , -1 }
70 };
71
72 /*
73  *      TLS Configuration
74  */
75 static CONF_PARSER tls_config[] = {
76         /*
77          *      Deprecated attributes
78          */
79         { "cacertfile", FR_CONF_OFFSET(PW_TYPE_FILE_INPUT | PW_TYPE_DEPRECATED, ldap_instance_t, tls_ca_file), NULL },
80         { "ca_file", FR_CONF_OFFSET(PW_TYPE_FILE_INPUT, ldap_instance_t, tls_ca_file), NULL },
81
82         { "cacertdir", FR_CONF_OFFSET(PW_TYPE_FILE_INPUT | PW_TYPE_DEPRECATED, ldap_instance_t, tls_ca_path), NULL },
83         { "ca_path", FR_CONF_OFFSET(PW_TYPE_FILE_INPUT, ldap_instance_t, tls_ca_path), NULL },
84
85         { "certfile", FR_CONF_OFFSET(PW_TYPE_FILE_INPUT | PW_TYPE_DEPRECATED, ldap_instance_t, tls_certificate_file), NULL },
86         { "certificate_file", FR_CONF_OFFSET(PW_TYPE_FILE_INPUT, ldap_instance_t, tls_certificate_file), NULL },
87
88         { "keyfile", FR_CONF_OFFSET(PW_TYPE_FILE_INPUT | PW_TYPE_DEPRECATED, ldap_instance_t, tls_private_key_file), NULL }, // OK if it changes on HUP
89         { "private_key_file", FR_CONF_OFFSET(PW_TYPE_FILE_INPUT, ldap_instance_t, tls_private_key_file), NULL }, // OK if it changes on HUP
90
91         { "randfile", FR_CONF_OFFSET(PW_TYPE_FILE_INPUT | PW_TYPE_DEPRECATED, ldap_instance_t, tls_random_file), NULL },
92         { "random_file", FR_CONF_OFFSET(PW_TYPE_FILE_INPUT, ldap_instance_t, tls_random_file), NULL },
93
94         /*
95          *      LDAP Specific TLS attributes
96          */
97         { "start_tls", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, ldap_instance_t, start_tls), "no" },
98         { "require_cert", FR_CONF_OFFSET(PW_TYPE_STRING, ldap_instance_t, tls_require_cert_str), NULL },
99
100         { NULL, -1, 0, NULL, NULL }
101 };
102
103
104 static CONF_PARSER profile_config[] = {
105         { "filter", FR_CONF_OFFSET(PW_TYPE_STRING, ldap_instance_t, profile_filter), "(&)" },   //!< Correct filter for
106                                                                                                 //!< when the DN is
107                                                                                                 //!< known.
108         { "attribute", FR_CONF_OFFSET(PW_TYPE_STRING, ldap_instance_t, profile_attr), NULL },
109         { "default", FR_CONF_OFFSET(PW_TYPE_STRING, ldap_instance_t, default_profile), NULL },
110
111         { NULL, -1, 0, NULL, NULL }
112 };
113
114 /*
115  *      User configuration
116  */
117 static CONF_PARSER user_config[] = {
118         { "filter", FR_CONF_OFFSET(PW_TYPE_STRING, ldap_instance_t, userobj_filter), "(uid=%u)" },
119         { "scope", FR_CONF_OFFSET(PW_TYPE_STRING, ldap_instance_t, userobj_scope_str), "sub" },
120         { "base_dn", FR_CONF_OFFSET(PW_TYPE_STRING, ldap_instance_t, userobj_base_dn), "" },
121
122         { "access_attribute", FR_CONF_OFFSET(PW_TYPE_STRING, ldap_instance_t, userobj_access_attr), NULL },
123         { "access_positive", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, ldap_instance_t, access_positive), "yes" },
124
125         { NULL, -1, 0, NULL, NULL }
126 };
127
128 /*
129  *      Group configuration
130  */
131 static CONF_PARSER group_config[] = {
132         { "filter", FR_CONF_OFFSET(PW_TYPE_STRING, ldap_instance_t, groupobj_filter), NULL },
133         { "scope", FR_CONF_OFFSET(PW_TYPE_STRING, ldap_instance_t, groupobj_scope_str), "sub" },
134         { "base_dn", FR_CONF_OFFSET(PW_TYPE_STRING, ldap_instance_t, groupobj_base_dn), "" },
135
136         { "name_attribute", FR_CONF_OFFSET(PW_TYPE_STRING, ldap_instance_t, groupobj_name_attr), "cn" },
137         { "membership_attribute", FR_CONF_OFFSET(PW_TYPE_STRING, ldap_instance_t, userobj_membership_attr), NULL },
138         { "membership_filter", FR_CONF_OFFSET(PW_TYPE_STRING, ldap_instance_t, groupobj_membership_filter), NULL },
139         { "cacheable_name", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, ldap_instance_t, cacheable_group_name), "no" },
140         { "cacheable_dn", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, ldap_instance_t, cacheable_group_dn), "no" },
141         { "cache_attribute", FR_CONF_OFFSET(PW_TYPE_STRING, ldap_instance_t, cache_attribute), NULL },
142
143         { NULL, -1, 0, NULL, NULL }
144 };
145
146 /*
147  *      Client configuration
148  */
149 static CONF_PARSER client_attribute[] = {
150         { "identifier", FR_CONF_OFFSET(PW_TYPE_STRING, ldap_instance_t, clientobj_identifier), "host" },
151         { "shortname", FR_CONF_OFFSET(PW_TYPE_STRING, ldap_instance_t, clientobj_shortname), "cn" },
152         { "nas_type", FR_CONF_OFFSET(PW_TYPE_STRING, ldap_instance_t, clientobj_type), NULL },
153         { "secret", FR_CONF_OFFSET(PW_TYPE_STRING, ldap_instance_t, clientobj_secret), NULL },
154         { "virtual_server", FR_CONF_OFFSET(PW_TYPE_STRING, ldap_instance_t, clientobj_server), NULL },
155         { "require_message_authenticator", FR_CONF_OFFSET(PW_TYPE_STRING, ldap_instance_t, clientobj_require_ma), NULL },
156
157         { NULL, -1, 0, NULL, NULL }
158 };
159
160 static CONF_PARSER client_config[] = {
161         { "filter", FR_CONF_OFFSET(PW_TYPE_STRING, ldap_instance_t, clientobj_filter), NULL },
162         { "scope", FR_CONF_OFFSET(PW_TYPE_STRING, ldap_instance_t, clientobj_scope_str), "sub" },
163         { "base_dn", FR_CONF_OFFSET(PW_TYPE_STRING, ldap_instance_t, clientobj_base_dn), "" },
164         { "attribute", FR_CONF_POINTER(PW_TYPE_SUBSECTION, NULL), (void const *) client_attribute },
165
166         { NULL, -1, 0, NULL, NULL }
167 };
168
169 /*
170  *      Reference for accounting updates
171  */
172 static const CONF_PARSER acct_section_config[] = {
173         { "reference", FR_CONF_OFFSET(PW_TYPE_STRING, ldap_acct_section_t, reference), "." },
174
175         {NULL, -1, 0, NULL, NULL}
176 };
177
178 /*
179  *      Various options that don't belong in the main configuration.
180  *
181  *      Note that these overlap a bit with the connection pool code!
182  */
183 static CONF_PARSER option_config[] = {
184         /*
185          *      Debugging flags to the server
186          */
187         { "ldap_debug", FR_CONF_OFFSET(PW_TYPE_INTEGER, ldap_instance_t, ldap_debug), "0x0000" },
188
189         { "dereference", FR_CONF_OFFSET(PW_TYPE_STRING, ldap_instance_t, dereference_str), NULL },
190
191         { "chase_referrals", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, ldap_instance_t, chase_referrals), NULL },
192
193         { "rebind", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, ldap_instance_t, rebind), NULL },
194
195 #ifdef LDAP_OPT_NETWORK_TIMEOUT
196         /* timeout on network activity */
197         { "net_timeout", FR_CONF_OFFSET(PW_TYPE_INTEGER, ldap_instance_t, net_timeout), "10" },
198 #endif
199
200         /* timeout for search results */
201         { "res_timeout", FR_CONF_OFFSET(PW_TYPE_INTEGER, ldap_instance_t, res_timeout), "20" },
202
203         /* allow server unlimited time for search (server-side limit) */
204         { "srv_timelimit", FR_CONF_OFFSET(PW_TYPE_INTEGER, ldap_instance_t, srv_timelimit), "20" },
205
206 #ifdef LDAP_OPT_X_KEEPALIVE_IDLE
207         { "idle", FR_CONF_OFFSET(PW_TYPE_INTEGER, ldap_instance_t, keepalive_idle), "60" },
208 #endif
209 #ifdef LDAP_OPT_X_KEEPALIVE_PROBES
210         { "probes", FR_CONF_OFFSET(PW_TYPE_INTEGER, ldap_instance_t, keepalive_probes), "3" },
211 #endif
212 #ifdef LDAP_OPT_X_KEEPALIVE_INTERVAL
213         { "interval", FR_CONF_OFFSET(PW_TYPE_INTEGER, ldap_instance_t, keepalive_interval), "30" },
214 #endif
215
216         { NULL, -1, 0, NULL, NULL }
217 };
218
219
220 static const CONF_PARSER module_config[] = {
221         { "server", FR_CONF_OFFSET(PW_TYPE_STRING | PW_TYPE_REQUIRED, ldap_instance_t, server), "localhost" },
222         { "port", FR_CONF_OFFSET(PW_TYPE_SHORT, ldap_instance_t, port), "389" },
223
224         { "password", FR_CONF_OFFSET(PW_TYPE_STRING | PW_TYPE_SECRET, ldap_instance_t, password), "" },
225         { "identity", FR_CONF_OFFSET(PW_TYPE_STRING, ldap_instance_t, admin_dn), "" },
226
227         { "valuepair_attribute", FR_CONF_OFFSET(PW_TYPE_STRING, ldap_instance_t, valuepair_attr), NULL },
228
229 #ifdef WITH_EDIR
230         /* support for eDirectory Universal Password */
231         { "edir", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, ldap_instance_t, edir), NULL }, /* NULL defaults to "no" */
232
233         /*
234          *      Attempt to bind with the cleartext password we got from eDirectory
235          *      Universal password for additional authorization checks.
236          */
237         { "edir_autz", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, ldap_instance_t, edir_autz), NULL }, /* NULL defaults to "no" */
238 #endif
239
240         { "read_clients", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, ldap_instance_t, do_clients), NULL }, /* NULL defaults to "no" */
241
242         { "user", FR_CONF_POINTER(PW_TYPE_SUBSECTION, NULL), (void const *) user_config },
243
244         { "group", FR_CONF_POINTER(PW_TYPE_SUBSECTION, NULL), (void const *) group_config },
245
246         { "client", FR_CONF_POINTER(PW_TYPE_SUBSECTION, NULL), (void const *) client_config },
247
248         { "profile", FR_CONF_POINTER(PW_TYPE_SUBSECTION, NULL), (void const *) profile_config },
249
250         { "options", FR_CONF_POINTER(PW_TYPE_SUBSECTION, NULL), (void const *) option_config },
251
252         { "tls", FR_CONF_POINTER(PW_TYPE_SUBSECTION, NULL), (void const *) tls_config },
253
254         {NULL, -1, 0, NULL, NULL}
255 };
256
257 /** Expand an LDAP URL into a query, and return a string result from that query.
258  *
259  */
260 static ssize_t ldap_xlat(void *instance, REQUEST *request, char const *fmt, char *out, size_t freespace)
261 {
262         ldap_rcode_t status;
263         size_t len = 0;
264         ldap_instance_t *inst = instance;
265         LDAPURLDesc *ldap_url;
266         LDAPMessage *result = NULL;
267         LDAPMessage *entry = NULL;
268         char **vals;
269         ldap_handle_t *conn;
270         int ldap_errno;
271         char const *url;
272         char const **attrs;
273
274         url = fmt;
275
276         if (!ldap_is_ldap_url(url)) {
277                 REDEBUG("String passed does not look like an LDAP URL");
278                 return -1;
279         }
280
281         if (ldap_url_parse(url, &ldap_url)){
282                 REDEBUG("Parsing LDAP URL failed");
283                 return -1;
284         }
285
286         /*
287          *      Nothing, empty string, "*" string, or got 2 things, die.
288          */
289         if (!ldap_url->lud_attrs || !ldap_url->lud_attrs[0] ||
290             !*ldap_url->lud_attrs[0] ||
291             (strcmp(ldap_url->lud_attrs[0], "*") == 0) ||
292             ldap_url->lud_attrs[1]) {
293                 REDEBUG("Bad attributes list in LDAP URL. URL must specify exactly one attribute to retrieve");
294
295                 goto free_urldesc;
296         }
297
298         if (ldap_url->lud_host &&
299             ((strncmp(inst->server, ldap_url->lud_host, strlen(inst->server)) != 0) ||
300              ((uint32_t) ldap_url->lud_port != inst->port))) {
301                 RDEBUG("Requested server/port is \"%s:%i\"", ldap_url->lud_host, inst->port);
302
303                 goto free_urldesc;
304         }
305
306         conn = rlm_ldap_get_socket(inst, request);
307         if (!conn) goto free_urldesc;
308
309         memcpy(&attrs, &ldap_url->lud_attrs, sizeof(attrs));
310
311         status = rlm_ldap_search(inst, request, &conn, ldap_url->lud_dn, ldap_url->lud_scope, ldap_url->lud_filter,
312                                  attrs, &result);
313         switch (status) {
314                 case LDAP_PROC_SUCCESS:
315                         break;
316                 case LDAP_PROC_NO_RESULT:
317                         RDEBUG("Search returned not found");
318                 default:
319                         goto free_socket;
320         }
321
322         rad_assert(conn);
323         rad_assert(result);
324
325         entry = ldap_first_entry(conn->handle, result);
326         if (!entry) {
327                 ldap_get_option(conn->handle, LDAP_OPT_RESULT_CODE, &ldap_errno);
328                 REDEBUG("Failed retrieving entry: %s", ldap_err2string(ldap_errno));
329                 len = -1;
330                 goto free_result;
331         }
332
333         vals = ldap_get_values(conn->handle, entry, ldap_url->lud_attrs[0]);
334         if (!vals) {
335                 RDEBUG("No \"%s\" attributes found in specified object", ldap_url->lud_attrs[0]);
336                 goto free_result;
337         }
338
339         len = strlen(vals[0]);
340         if (len >= freespace){
341                 goto free_vals;
342         }
343
344         strlcpy(out, vals[0], freespace);
345
346 free_vals:
347         ldap_value_free(vals);
348 free_result:
349         ldap_msgfree(result);
350 free_socket:
351         rlm_ldap_release_socket(inst, conn);
352 free_urldesc:
353         ldap_free_urldesc(ldap_url);
354
355         return len;
356 }
357
358 /** Perform LDAP-Group comparison checking
359  *
360  * Attempts to match users to groups using a variety of methods.
361  *
362  * @param instance of the rlm_ldap module.
363  * @param request Current request.
364  * @param thing Unknown.
365  * @param check Which group to check for user membership.
366  * @param check_pairs Unknown.
367  * @param reply_pairs Unknown.
368  * @return 1 on failure (or if the user is not a member), else 0.
369  */
370 static int rlm_ldap_groupcmp(void *instance, REQUEST *request, UNUSED VALUE_PAIR *thing, VALUE_PAIR *check,
371                              UNUSED VALUE_PAIR *check_pairs, UNUSED VALUE_PAIR **reply_pairs)
372 {
373         ldap_instance_t *inst = instance;
374         rlm_rcode_t     rcode;
375
376         bool            found = false;
377         bool            check_is_dn;
378
379         ldap_handle_t   *conn = NULL;
380         char const      *user_dn;
381
382         rad_assert(inst->groupobj_base_dn);
383
384         RDEBUG("Searching for user in group \"%s\"", check->vp_strvalue);
385
386         if (check->length == 0) {
387                 RDEBUG("Cannot do comparison (group name is empty)");
388                 return 1;
389         }
390
391         /*
392          *      Check if we can do cached membership verification
393          */
394         check_is_dn = rlm_ldap_is_dn(check->vp_strvalue);
395         if ((check_is_dn && inst->cacheable_group_dn) || (!check_is_dn && inst->cacheable_group_name)) {
396                 switch(rlm_ldap_check_cached(inst, request, check)) {
397                         case RLM_MODULE_NOTFOUND:
398                                 found = false;
399                                 goto finish;
400                         case RLM_MODULE_OK:
401                                 found = true;
402                                 goto finish;
403                         /*
404                          *      Fallback to dynamic search on failure
405                          */
406                         case RLM_MODULE_FAIL:
407                         case RLM_MODULE_INVALID:
408                         default:
409                                 break;
410                 }
411         }
412
413         conn = rlm_ldap_get_socket(inst, request);
414         if (!conn) return 1;
415
416         /*
417          *      This is used in the default membership filter.
418          */
419         user_dn = rlm_ldap_find_user(inst, request, &conn, NULL, false, NULL, &rcode);
420         if (!user_dn) {
421                 rlm_ldap_release_socket(inst, conn);
422                 return 1;
423         }
424
425         rad_assert(conn);
426
427         /*
428          *      Check groupobj user membership
429          */
430         if (inst->groupobj_membership_filter) {
431                 switch(rlm_ldap_check_groupobj_dynamic(inst, request, &conn, check)) {
432                         case RLM_MODULE_NOTFOUND:
433                                 break;
434                         case RLM_MODULE_OK:
435                                 found = true;
436                         default:
437                                 goto finish;
438                 }
439         }
440
441         rad_assert(conn);
442
443         /*
444          *      Check userobj group membership
445          */
446         if (inst->userobj_membership_attr) {
447                 switch(rlm_ldap_check_userobj_dynamic(inst, request, &conn, user_dn, check)) {
448                         case RLM_MODULE_NOTFOUND:
449                                 break;
450                         case RLM_MODULE_OK:
451                                 found = true;
452                         default:
453                                 goto finish;
454                 }
455         }
456
457         rad_assert(conn);
458
459         finish:
460         if (conn) {
461                 rlm_ldap_release_socket(inst, conn);
462         }
463
464         if (!found) {
465                 RDEBUG("User is not a member of specified group");
466
467                 return 1;
468         }
469
470         return 0;
471 }
472
473 /** Detach from the LDAP server and cleanup internal state.
474  *
475  */
476 static int mod_detach(void *instance)
477 {
478         ldap_instance_t *inst = instance;
479
480         fr_connection_pool_delete(inst->pool);
481
482         if (inst->user_map) {
483                 talloc_free(inst->user_map);
484         }
485
486         return 0;
487 }
488
489 /** Parse an accounting sub section.
490  *
491  * Allocate a new ldap_acct_section_t and write the config data into it.
492  *
493  * @param[in] inst rlm_ldap configuration.
494  * @param[in] parent of the config section.
495  * @param[out] config to write the sub section parameters to.
496  * @param[in] comp The section name were parsing the config for.
497  * @return 0 on success, else < 0 on failure.
498  */
499 static int parse_sub_section(ldap_instance_t *inst, CONF_SECTION *parent, ldap_acct_section_t **config,
500                              rlm_components_t comp)
501 {
502         CONF_SECTION *cs;
503
504         char const *name = section_type_value[comp].section;
505
506         cs = cf_section_sub_find(parent, name);
507         if (!cs) {
508                 INFO("rlm_ldap (%s): Couldn't find configuration for %s, will return NOOP for calls "
509                        "from this section", inst->xlat_name, name);
510
511                 return 0;
512         }
513
514         *config = talloc_zero(inst, ldap_acct_section_t);
515         if (cf_section_parse(cs, *config, acct_section_config) < 0) {
516                 LDAP_ERR("Failed parsing configuration for section %s", name);
517
518                 return -1;
519         }
520
521         (*config)->cs = cs;
522
523         return 0;
524 }
525
526 /** Instantiate the module
527  *
528  * Creates a new instance of the module reading parameters from a configuration section.
529  *
530  * @param conf to parse.
531  * @param instance Where to write pointer to configuration data.
532  * @return 0 on success < 0 on failure.
533  */
534 static int mod_instantiate(CONF_SECTION *conf, void *instance)
535 {
536         static bool version_done;
537
538         CONF_SECTION *options;
539         ldap_instance_t *inst = instance;
540
541         inst->cs = conf;
542
543         options = cf_section_sub_find(conf, "options");
544         if (!options || !cf_pair_find(options, "chase_referrals")) {
545                 inst->chase_referrals_unset = true;      /* use OpenLDAP defaults */
546         }
547
548         inst->xlat_name = cf_section_name2(conf);
549         if (!inst->xlat_name) {
550                 inst->xlat_name = cf_section_name1(conf);
551         }
552
553         /*
554          *      Get version info from the LDAP API.
555          */
556         if (!version_done) {
557                 LDAPAPIInfo info;
558
559                 version_done = true;
560
561                 if (ldap_get_option(NULL, LDAP_OPT_API_INFO, &info) == LDAP_SUCCESS) {
562                         if (strcmp(info.ldapai_vendor_name, LDAP_VENDOR_NAME) != 0) {
563                                 WARN("rlm_ldap: libldap vendor changed since the server was built");
564                                 WARN("rlm_ldap: linked: %s built: %s", info.ldapai_vendor_name, LDAP_VENDOR_NAME);
565                         }
566
567                         if (info.ldapai_vendor_version != LDAP_VENDOR_VERSION) {
568                                 WARN("rlm_ldap: libldap version changed since the server was built");
569                                 WARN("rlm_ldap: linked: %i built: %i",
570                                      info.ldapai_vendor_version, LDAP_VENDOR_VERSION);
571                         }
572
573                         INFO("rlm_ldap: libldap vendor: %s version: %i", info.ldapai_vendor_name,
574                              info.ldapai_vendor_version);
575                         ldap_memfree(info.ldapai_extensions);
576                 }
577         }
578
579         /*
580          *      If the configuration parameters can't be parsed, then fail.
581          */
582         if ((parse_sub_section(inst, conf, &inst->accounting, RLM_COMPONENT_ACCT) < 0) ||
583             (parse_sub_section(inst, conf, &inst->postauth, RLM_COMPONENT_POST_AUTH) < 0)) {
584                 LDAP_ERR("Failed parsing configuration");
585
586                 goto error;
587         }
588
589         /*
590          *      Sanity checks for cacheable groups code.
591          */
592         if (inst->cacheable_group_name && inst->groupobj_membership_filter) {
593                 if (!inst->groupobj_name_attr) {
594                         LDAP_ERR("Directive 'group.name_attribute' must be set if cacheable group names are enabled");
595
596                         goto error;
597                 }
598         }
599
600         /*
601          *      Check for URLs.  If they're used and the library doesn't support them, then complain.
602          */
603         inst->is_url = 0;
604         if (ldap_is_ldap_url(inst->server)) {
605 #ifdef HAVE_LDAP_INITIALIZE
606                 inst->is_url = 1;
607                 inst->port = 0;
608 #else
609                 LDAP_ERR("Directive 'server' is in URL form but ldap_initialize() is not available");
610                 goto error;
611 #endif
612         }
613
614 #ifdef LDAP_OPT_X_TLS_NEVER
615         /*
616          *      Workaround for servers which support LDAPS but not START TLS
617          */
618         if (inst->port == LDAPS_PORT || inst->tls_mode) {
619                 inst->tls_mode = LDAP_OPT_X_TLS_HARD;
620         } else {
621                 inst->tls_mode = 0;
622         }
623 #endif
624
625         /*
626          *      Convert dereference strings to enumerated constants
627          */
628         if (inst->dereference_str) {
629                 inst->dereference = fr_str2int(ldap_dereference, inst->dereference_str, -1);
630                 if (inst->dereference < 0) {
631                         LDAP_ERR("Invalid 'dereference' value \"%s\", expected 'never', 'searching', "
632                                  "'finding' or 'always'", inst->dereference_str);
633                         goto error;
634                 }
635         }
636
637 #if LDAP_SET_REBIND_PROC_ARGS != 3
638         /*
639          *      The 2-argument rebind doesn't take an instance variable.  Our rebind function needs the instance
640          *      variable for the username, password, etc.
641          */
642         if (inst->rebind == true) {
643                 LDAP_ERR("Cannot use 'rebind' directive as this version of libldap does not support the API "
644                          "that we need");
645
646                 goto error;
647         }
648 #endif
649
650         /*
651          *      Convert scope strings to enumerated constants
652          */
653         inst->userobj_scope = fr_str2int(ldap_scope, inst->userobj_scope_str, -1);
654         if (inst->userobj_scope < 0) {
655                 LDAP_ERR("Invalid 'user.scope' value \"%s\", expected 'sub', 'one'"
656 #ifdef LDAP_SCOPE_CHILDREN
657                          ", 'base' or 'children'"
658 #else
659                          " or 'base'"
660 #endif
661                          , inst->userobj_scope_str);
662                 goto error;
663         }
664
665         inst->groupobj_scope = fr_str2int(ldap_scope, inst->groupobj_scope_str, -1);
666         if (inst->groupobj_scope < 0) {
667                 LDAP_ERR("Invalid 'group.scope' value \"%s\", expected 'sub', 'one'"
668 #ifdef LDAP_SCOPE_CHILDREN
669                          ", 'base' or 'children'"
670 #else
671                          " or 'base'"
672 #endif
673                          , inst->groupobj_scope_str);
674                 goto error;
675         }
676
677         inst->clientobj_scope = fr_str2int(ldap_scope, inst->clientobj_scope_str, -1);
678         if (inst->clientobj_scope < 0) {
679                 LDAP_ERR("Invalid 'client.scope' value \"%s\", expected 'sub', 'one'"
680 #ifdef LDAP_SCOPE_CHILDREN
681                          ", 'base' or 'children'"
682 #else
683                          " or 'base'"
684 #endif
685                          , inst->clientobj_scope_str);
686                 goto error;
687         }
688
689         if (inst->tls_require_cert_str) {
690 #ifdef LDAP_OPT_X_TLS_NEVER
691                 /*
692                  *      Convert cert strictness to enumerated constants
693                  */
694                 inst->tls_require_cert = fr_str2int(ldap_tls_require_cert, inst->tls_require_cert_str, -1);
695                 if (inst->tls_require_cert < 0) {
696                         LDAP_ERR("Invalid 'tls.require_cert' value \"%s\", expected 'never', 'demand', 'allow', "
697                                  "'try' or 'hard'", inst->tls_require_cert_str);
698                         goto error;
699                 }
700 #else
701                 LDAP_ERR("Modifying 'tls.require_cert' is not supported by current version of libldap. "
702                          "Please upgrade or substitute current libldap and rebuild this module");
703
704                 goto error;
705 #endif
706         }
707         /*
708          *      Build the attribute map
709          */
710         if (rlm_ldap_map_verify(inst, &(inst->user_map)) < 0) {
711                 goto error;
712         }
713
714         /*
715          *      Group comparison checks.
716          */
717         if (cf_section_name2(conf)) {
718                 static ATTR_FLAGS flags;
719                 char buffer[256];
720
721                 snprintf(buffer, sizeof(buffer), "%s-Ldap-Group", inst->xlat_name);
722                 if (dict_addattr(buffer, -1, 0, PW_TYPE_STRING, flags) < 0) {
723                         LDAP_ERR("Error creating group attribute: %s", fr_strerror());
724
725                         return -1;
726                 }
727                 inst->group_da = dict_attrbyname(buffer);
728                 if (!inst->group_da) {
729                         LDAP_ERR("Failed creating attribute %s", buffer);
730
731                         goto error;
732                 }
733
734                 paircompare_register(inst->group_da, dict_attrbyvalue(PW_USER_NAME, 0), false, rlm_ldap_groupcmp, inst);
735         /*
736          *      Were the default instance
737          */
738         } else {
739                 inst->group_da = dict_attrbyvalue(PW_LDAP_GROUP, 0);
740                 paircompare_register(dict_attrbyvalue(PW_LDAP_GROUP, 0), dict_attrbyvalue(PW_USER_NAME, 0),
741                                 false, rlm_ldap_groupcmp, inst);
742         }
743
744         xlat_register(inst->xlat_name, ldap_xlat, rlm_ldap_escape_func, inst);
745
746         /*
747          *      Setup the cache attribute
748          */
749         if (inst->cache_attribute) {
750                 static ATTR_FLAGS flags;
751
752                 if (dict_addattr(inst->cache_attribute, -1, 0, PW_TYPE_STRING, flags) < 0) {
753                         LDAP_ERR("Error creating cache attribute: %s", fr_strerror());
754
755                         return -1;
756                 }
757                 inst->cache_da = dict_attrbyname(inst->cache_attribute);
758         } else {
759                 inst->cache_da = inst->group_da;        /* Default to the group_da */
760         }
761
762         /*
763          *      Initialize the socket pool.
764          */
765         inst->pool = fr_connection_pool_init(inst->cs, inst, mod_conn_create, NULL, mod_conn_delete, NULL);
766         if (!inst->pool) {
767                 return -1;
768         }
769
770         /*
771          *      Bulk load dynamic clients.
772          */
773         if (inst->do_clients) {
774                 if (rlm_ldap_load_clients(inst) < 0) {
775                         LDAP_ERR("Error loading clients");
776
777                         return -1;
778                 }
779         }
780
781         return 0;
782
783 error:
784         return -1;
785 }
786
787 static rlm_rcode_t CC_HINT(nonnull) mod_authenticate(void *instance, REQUEST *request)
788 {
789         rlm_rcode_t     rcode;
790         ldap_rcode_t    status;
791         char const      *dn;
792         ldap_instance_t *inst = instance;
793         ldap_handle_t   *conn;
794
795         /*
796          * Ensure that we're being passed a plain-text password, and not
797          * anything else.
798          */
799
800         if (!request->username) {
801                 REDEBUG("Attribute \"User-Name\" is required for authentication");
802
803                 return RLM_MODULE_INVALID;
804         }
805
806         if (!request->password ||
807             (request->password->da->attr != PW_USER_PASSWORD)) {
808                 RWDEBUG("You have set \"Auth-Type := LDAP\" somewhere");
809                 RWDEBUG("*********************************************");
810                 RWDEBUG("* THAT CONFIGURATION IS WRONG.  DELETE IT.   ");
811                 RWDEBUG("* YOU ARE PREVENTING THE SERVER FROM WORKING");
812                 RWDEBUG("*********************************************");
813
814                 REDEBUG("Attribute \"User-Password\" is required for authentication");
815
816                 return RLM_MODULE_INVALID;
817         }
818
819         if (request->password->length == 0) {
820                 REDEBUG("Empty password supplied");
821
822                 return RLM_MODULE_INVALID;
823         }
824
825         RDEBUG("Login attempt by \"%s\"", request->username->vp_strvalue);
826
827         conn = rlm_ldap_get_socket(inst, request);
828         if (!conn) return RLM_MODULE_FAIL;
829
830         /*
831          *      Get the DN by doing a search.
832          */
833         dn = rlm_ldap_find_user(inst, request, &conn, NULL, false, NULL, &rcode);
834         if (!dn) {
835                 rlm_ldap_release_socket(inst, conn);
836
837                 return rcode;
838         }
839
840         /*
841          *      Bind as the user
842          */
843         conn->rebound = true;
844         status = rlm_ldap_bind(inst, request, &conn, dn, request->password->vp_strvalue, true);
845         switch (status) {
846         case LDAP_PROC_SUCCESS:
847                 rcode = RLM_MODULE_OK;
848                 RDEBUG("Bind as user \"%s\" was successful", dn);
849                 break;
850
851         case LDAP_PROC_NOT_PERMITTED:
852                 rcode = RLM_MODULE_USERLOCK;
853                 break;
854
855         case LDAP_PROC_REJECT:
856                 rcode = RLM_MODULE_REJECT;
857                 break;
858
859         case LDAP_PROC_BAD_DN:
860                 rcode = RLM_MODULE_INVALID;
861                 break;
862
863         case LDAP_PROC_NO_RESULT:
864                 rcode = RLM_MODULE_NOTFOUND;
865                 break;
866
867         default:
868                 rcode = RLM_MODULE_FAIL;
869                 break;
870         };
871
872         rlm_ldap_release_socket(inst, conn);
873
874         return rcode;
875 }
876
877 static rlm_rcode_t CC_HINT(nonnull) mod_authorize(void *instance, REQUEST *request)
878 {
879         rlm_rcode_t     rcode = RLM_MODULE_OK;
880         ldap_rcode_t    status;
881         int             ldap_errno;
882         int             i;
883         ldap_instance_t *inst = instance;
884         char            **vals;
885         VALUE_PAIR      *vp;
886         ldap_handle_t   *conn;
887         LDAPMessage     *result, *entry;
888         char const      *dn = NULL;
889         rlm_ldap_map_xlat_t     expanded; /* faster than mallocing every time */
890
891         if (!request->username) {
892                 RDEBUG2("Attribute \"User-Name\" is required for authorization");
893
894                 return RLM_MODULE_NOOP;
895         }
896
897         /*
898          *      Check for valid input, zero length names not permitted
899          */
900         if (request->username->length == 0) {
901                 RDEBUG2("Zero length username not permitted");
902
903                 return RLM_MODULE_INVALID;
904         }
905
906         if (rlm_ldap_map_xlat(request, inst->user_map, &expanded) < 0) {
907                 return RLM_MODULE_FAIL;
908         }
909
910         conn = rlm_ldap_get_socket(inst, request);
911         if (!conn) return RLM_MODULE_FAIL;
912
913         /*
914          *      Add any additional attributes we need for checking access, memberships, and profiles
915          */
916         if (inst->userobj_access_attr) {
917                 expanded.attrs[expanded.count++] = inst->userobj_access_attr;
918         }
919
920         if (inst->userobj_membership_attr && (inst->cacheable_group_dn || inst->cacheable_group_name)) {
921                 expanded.attrs[expanded.count++] = inst->userobj_membership_attr;
922         }
923
924         if (inst->profile_attr) {
925                 expanded.attrs[expanded.count++] = inst->profile_attr;
926         }
927
928         if (inst->valuepair_attr) {
929                 expanded.attrs[expanded.count++] = inst->valuepair_attr;
930         }
931
932         expanded.attrs[expanded.count] = NULL;
933
934         dn = rlm_ldap_find_user(inst, request, &conn, expanded.attrs, true, &result, &rcode);
935         if (!dn) {
936                 goto finish;
937         }
938
939         entry = ldap_first_entry(conn->handle, result);
940         if (!entry) {
941                 ldap_get_option(conn->handle, LDAP_OPT_RESULT_CODE, &ldap_errno);
942                 REDEBUG("Failed retrieving entry: %s", ldap_err2string(ldap_errno));
943
944                 goto finish;
945         }
946
947         /*
948          *      Check for access.
949          */
950         if (inst->userobj_access_attr) {
951                 rcode = rlm_ldap_check_access(inst, request, conn, entry);
952                 if (rcode != RLM_MODULE_OK) {
953                         goto finish;
954                 }
955         }
956
957         /*
958          *      Check if we need to cache group memberships
959          */
960         if (inst->cacheable_group_dn || inst->cacheable_group_name) {
961                 if (inst->userobj_membership_attr) {
962                         rcode = rlm_ldap_cacheable_userobj(inst, request, &conn, entry, inst->userobj_membership_attr);
963                         if (rcode != RLM_MODULE_OK) {
964                                 goto finish;
965                         }
966                 }
967
968                 rcode = rlm_ldap_cacheable_groupobj(inst, request, &conn);
969                 if (rcode != RLM_MODULE_OK) {
970                         goto finish;
971                 }
972         }
973
974 #ifdef WITH_EDIR
975         /*
976          *      We already have a Cleartext-Password.  Skip edir.
977          */
978         if (pairfind(request->config_items, PW_CLEARTEXT_PASSWORD, 0, TAG_ANY)) {
979                 goto skip_edir;
980         }
981
982         /*
983          *      Retrieve Universal Password if we use eDirectory
984          */
985         if (inst->edir) {
986                 int res = 0;
987                 char password[256];
988                 size_t pass_size = sizeof(password);
989
990                 /*
991                  *      Retrive universal password
992                  */
993                 res = nmasldap_get_password(conn->handle, dn, password, &pass_size);
994                 if (res != 0) {
995                         REDEBUG("Failed to retrieve eDirectory password: (%i) %s", res, edir_errstr(res));
996                         rcode = RLM_MODULE_FAIL;
997
998                         goto finish;
999                 }
1000
1001                 /*
1002                  *      Add Cleartext-Password attribute to the request
1003                  */
1004                 vp = radius_paircreate(request, &request->config_items, PW_CLEARTEXT_PASSWORD, 0);
1005                 pairstrcpy(vp, password);
1006                 vp->length = pass_size;
1007
1008                 if (RDEBUG_ENABLED3) {
1009                         RDEBUG3("Added eDirectory password.  control:%s += '%s'", vp->da->name, vp->vp_strvalue);
1010                 } else {
1011                         RDEBUG2("Added eDirectory password");
1012                 }
1013
1014                 if (inst->edir_autz) {
1015                         RDEBUG2("Binding as user for eDirectory authorization checks");
1016                         /*
1017                          *      Bind as the user
1018                          */
1019                         conn->rebound = true;
1020                         status = rlm_ldap_bind(inst, request, &conn, dn, vp->vp_strvalue, true);
1021                         switch (status) {
1022                         case LDAP_PROC_SUCCESS:
1023                                 rcode = RLM_MODULE_OK;
1024                                 RDEBUG("Bind as user '%s' was successful", dn);
1025                                 break;
1026
1027                         case LDAP_PROC_NOT_PERMITTED:
1028                                 rcode = RLM_MODULE_USERLOCK;
1029                                 goto finish;
1030
1031                         case LDAP_PROC_REJECT:
1032                                 rcode = RLM_MODULE_REJECT;
1033                                 goto finish;
1034
1035                         case LDAP_PROC_BAD_DN:
1036                                 rcode = RLM_MODULE_INVALID;
1037                                 goto finish;
1038
1039                         case LDAP_PROC_NO_RESULT:
1040                                 rcode = RLM_MODULE_NOTFOUND;
1041                                 goto finish;
1042
1043                         default:
1044                                 rcode = RLM_MODULE_FAIL;
1045                                 goto finish;
1046                         };
1047                 }
1048         }
1049
1050 skip_edir:
1051 #endif
1052
1053         /*
1054          *      Apply ONE user profile, or a default user profile.
1055          */
1056         if (inst->default_profile) {
1057                 char profile[1024];
1058
1059                 if (radius_xlat(profile, sizeof(profile), request, inst->default_profile, NULL, NULL) < 0) {
1060                         REDEBUG("Failed creating default profile string");
1061
1062                         rcode = RLM_MODULE_INVALID;
1063                         goto finish;
1064                 }
1065
1066                 rlm_ldap_map_profile(inst, request, &conn, profile, &expanded);
1067         }
1068
1069         /*
1070          *      Apply a SET of user profiles.
1071          */
1072         if (inst->profile_attr) {
1073                 vals = ldap_get_values(conn->handle, entry, inst->profile_attr);
1074                 if (vals != NULL) {
1075                         for (i = 0; vals[i] != NULL; i++) {
1076                                 rlm_ldap_map_profile(inst, request, &conn, vals[i], &expanded);
1077                         }
1078
1079                         ldap_value_free(vals);
1080                 }
1081         }
1082
1083         if (inst->user_map || inst->valuepair_attr) {
1084                 RDEBUG("Processing user attributes");
1085                 rlm_ldap_map_do(inst, request, conn->handle, &expanded, entry);
1086                 rlm_ldap_check_reply(inst, request);
1087         }
1088
1089 finish:
1090         rlm_ldap_map_xlat_free(&expanded);
1091         if (result) {
1092                 ldap_msgfree(result);
1093         }
1094         rlm_ldap_release_socket(inst, conn);
1095
1096         return rcode;
1097 }
1098
1099 /** Modify user's object in LDAP
1100  *
1101  * Process a modifcation map to update a user object in the LDAP directory.
1102  *
1103  * @param inst rlm_ldap instance.
1104  * @param request Current request.
1105  * @param section that holds the map to process.
1106  * @return one of the RLM_MODULE_* values.
1107  */
1108 static rlm_rcode_t user_modify(ldap_instance_t *inst, REQUEST *request, ldap_acct_section_t *section)
1109 {
1110         rlm_rcode_t     rcode = RLM_MODULE_OK;
1111         ldap_rcode_t    status;
1112
1113         ldap_handle_t   *conn = NULL;
1114
1115         LDAPMod         *mod_p[LDAP_MAX_ATTRMAP + 1], mod_s[LDAP_MAX_ATTRMAP];
1116         LDAPMod         **modify = mod_p;
1117
1118         char            *passed[LDAP_MAX_ATTRMAP * 2];
1119         int             i, total = 0, last_pass = 0;
1120
1121         char            *expanded[LDAP_MAX_ATTRMAP];
1122         int             last_exp = 0;
1123
1124         char const      *attr;
1125         char const      *value;
1126
1127         char const      *dn;
1128         /*
1129          *      Build our set of modifications using the update sections in
1130          *      the config.
1131          */
1132         CONF_ITEM       *ci;
1133         CONF_PAIR       *cp;
1134         CONF_SECTION    *cs;
1135         FR_TOKEN        op;
1136         char            path[MAX_STRING_LEN];
1137
1138         char            *p = path;
1139
1140         rad_assert(section);
1141
1142         /*
1143          *      Locate the update section were going to be using
1144          */
1145         if (section->reference[0] != '.') {
1146                 *p++ = '.';
1147         }
1148
1149         if (radius_xlat(p, (sizeof(path) - (p - path)) - 1, request, section->reference, NULL, NULL) < 0) {
1150                 goto error;
1151         }
1152
1153         ci = cf_reference_item(NULL, section->cs, path);
1154         if (!ci) {
1155                 goto error;
1156         }
1157
1158         if (!cf_item_is_section(ci)){
1159                 REDEBUG("Reference must resolve to a section");
1160
1161                 goto error;
1162         }
1163
1164         cs = cf_section_sub_find(cf_itemtosection(ci), "update");
1165         if (!cs) {
1166                 REDEBUG("Section must contain 'update' subsection");
1167
1168                 goto error;
1169         }
1170
1171         /*
1172          *      Iterate over all the pairs, building our mods array
1173          */
1174         for (ci = cf_item_find_next(cs, NULL); ci != NULL; ci = cf_item_find_next(cs, ci)) {
1175                 bool do_xlat = false;
1176
1177                 if (total == LDAP_MAX_ATTRMAP) {
1178                         REDEBUG("Modify map size exceeded");
1179
1180                         goto error;
1181                 }
1182
1183                 if (!cf_item_is_pair(ci)) {
1184                         REDEBUG("Entry is not in \"ldap-attribute = value\" format");
1185
1186                         goto error;
1187                 }
1188
1189                 /*
1190                  *      Retrieve all the information we need about the pair
1191                  */
1192                 cp = cf_itemtopair(ci);
1193                 value = cf_pair_value(cp);
1194                 attr = cf_pair_attr(cp);
1195                 op = cf_pair_operator(cp);
1196
1197                 if (!value || (*value == '\0')) {
1198                         RDEBUG("Empty value string, skipping attribute \"%s\"", attr);
1199
1200                         continue;
1201                 }
1202
1203                 switch (cf_pair_value_type(cp)) {
1204                 case T_BARE_WORD:
1205                 case T_SINGLE_QUOTED_STRING:
1206                         break;
1207
1208                 case T_BACK_QUOTED_STRING:
1209                 case T_DOUBLE_QUOTED_STRING:
1210                         do_xlat = true;
1211                         break;
1212
1213                 default:
1214                         rad_assert(0);
1215                         goto error;
1216                 }
1217
1218                 if (op == T_OP_CMP_FALSE) {
1219                         passed[last_pass] = NULL;
1220                 } else if (do_xlat) {
1221                         char *exp = NULL;
1222
1223                         if (radius_axlat(&exp, request, value, NULL, NULL) <= 0) {
1224                                 RDEBUG("Skipping attribute \"%s\"", attr);
1225
1226                                 talloc_free(exp);
1227
1228                                 continue;
1229                         }
1230
1231                         expanded[last_exp++] = exp;
1232                         passed[last_pass] = exp;
1233                 /*
1234                  *      Static strings
1235                  */
1236                 } else {
1237                         memcpy(&(passed[last_pass]), &value, sizeof(passed[last_pass]));
1238                 }
1239
1240                 passed[last_pass + 1] = NULL;
1241
1242                 mod_s[total].mod_values = &(passed[last_pass]);
1243
1244                 last_pass += 2;
1245
1246                 switch (op) {
1247                 /*
1248                  *  T_OP_EQ is *NOT* supported, it is impossible to
1249                  *  support because of the lack of transactions in LDAP
1250                  */
1251                 case T_OP_ADD:
1252                         mod_s[total].mod_op = LDAP_MOD_ADD;
1253                         break;
1254
1255                 case T_OP_SET:
1256                         mod_s[total].mod_op = LDAP_MOD_REPLACE;
1257                         break;
1258
1259                 case T_OP_SUB:
1260                 case T_OP_CMP_FALSE:
1261                         mod_s[total].mod_op = LDAP_MOD_DELETE;
1262                         break;
1263
1264 #ifdef LDAP_MOD_INCREMENT
1265                 case T_OP_INCRM:
1266                         mod_s[total].mod_op = LDAP_MOD_INCREMENT;
1267                         break;
1268 #endif
1269                 default:
1270                         REDEBUG("Operator '%s' is not supported for LDAP modify operations",
1271                                 fr_int2str(fr_tokens, op, "<INVALID>"));
1272
1273                         goto error;
1274                 }
1275
1276                 /*
1277                  *      Now we know the value is ok, copy the pointers into
1278                  *      the ldapmod struct.
1279                  */
1280                 memcpy(&(mod_s[total].mod_type), &attr, sizeof(mod_s[total].mod_type));
1281
1282                 mod_p[total] = &(mod_s[total]);
1283                 total++;
1284         }
1285
1286         if (total == 0) {
1287                 rcode = RLM_MODULE_NOOP;
1288                 goto release;
1289         }
1290
1291         mod_p[total] = NULL;
1292
1293         conn = rlm_ldap_get_socket(inst, request);
1294         if (!conn) return RLM_MODULE_FAIL;
1295
1296
1297         dn = rlm_ldap_find_user(inst, request, &conn, NULL, false, NULL, &rcode);
1298         if (!dn || (rcode != RLM_MODULE_OK)) {
1299                 goto error;
1300         }
1301
1302         status = rlm_ldap_modify(inst, request, &conn, dn, modify);
1303         switch (status) {
1304         case LDAP_PROC_SUCCESS:
1305                 break;
1306
1307         case LDAP_PROC_REJECT:
1308         case LDAP_PROC_BAD_DN:
1309                 rcode = RLM_MODULE_INVALID;
1310                 break;
1311
1312         default:
1313                 rcode = RLM_MODULE_FAIL;
1314                 break;
1315         };
1316
1317         release:
1318         error:
1319         /*
1320          *      Free up any buffers we allocated for xlat expansion
1321          */
1322         for (i = 0; i < last_exp; i++) {
1323                 talloc_free(expanded[i]);
1324         }
1325
1326         rlm_ldap_release_socket(inst, conn);
1327
1328         return rcode;
1329 }
1330
1331 static rlm_rcode_t CC_HINT(nonnull) mod_accounting(void *instance, REQUEST * request) {
1332         ldap_instance_t *inst = instance;
1333
1334         if (inst->accounting) {
1335                 return user_modify(inst, request, inst->accounting);
1336         }
1337
1338         return RLM_MODULE_NOOP;
1339 }
1340
1341 static rlm_rcode_t CC_HINT(nonnull) mod_post_auth(void *instance, REQUEST * request)
1342 {
1343         ldap_instance_t *inst = instance;
1344
1345         if (inst->postauth) {
1346                 return user_modify(inst, request, inst->postauth);
1347         }
1348
1349         return RLM_MODULE_NOOP;
1350 }
1351
1352
1353 /* globally exported name */
1354 module_t rlm_ldap = {
1355         RLM_MODULE_INIT,
1356         "ldap",
1357         RLM_TYPE_THREAD_SAFE,   /* type: reserved        */
1358         sizeof(ldap_instance_t),
1359         module_config,
1360         mod_instantiate,        /* instantiation         */
1361         mod_detach,             /* detach                */
1362         {
1363                 mod_authenticate,       /* authentication        */
1364                 mod_authorize,          /* authorization         */
1365                 NULL,                   /* preaccounting         */
1366                 mod_accounting,         /* accounting            */
1367                 NULL,                   /* checksimul            */
1368                 NULL,                   /* pre-proxy             */
1369                 NULL,                   /* post-proxy            */
1370                 mod_post_auth           /* post-auth */
1371         },
1372 };