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.
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.
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
19 * @brief LDAP authorization and authentication module.
21 * @author Arran Cudbard-Bell <a.cudbardb@freeradius.org>
22 * @author Alan DeKok <aland@freeradius.org>
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.
31 #include <freeradius-devel/rad_assert.h>
41 const FR_NAME_NUMBER ldap_scope[] = {
42 { "sub", LDAP_SCOPE_SUB },
43 { "one", LDAP_SCOPE_ONE },
44 { "base", LDAP_SCOPE_BASE },
52 static CONF_PARSER tls_config[] = {
53 {"start_tls", PW_TYPE_BOOLEAN, offsetof(ldap_instance_t, start_tls), NULL, "no"},
54 {"cacertfile", PW_TYPE_FILENAME, offsetof(ldap_instance_t, tls_cacertfile), NULL, NULL},
55 {"cacertdir", PW_TYPE_FILENAME, offsetof(ldap_instance_t, tls_cacertdir), NULL, NULL},
56 {"certfile", PW_TYPE_FILENAME, offsetof(ldap_instance_t, tls_certfile), NULL, NULL},
57 {"keyfile", PW_TYPE_FILENAME, offsetof(ldap_instance_t, tls_keyfile), NULL, NULL}, // OK if it changes on HUP
58 {"randfile", PW_TYPE_STRING_PTR, offsetof(ldap_instance_t, tls_randfile), NULL, NULL},
59 {"require_cert",PW_TYPE_STRING_PTR, offsetof(ldap_instance_t, tls_require_cert), NULL, TLS_DEFAULT_VERIFY},
61 { NULL, -1, 0, NULL, NULL }
65 static CONF_PARSER profile_config[] = {
66 {"profile_attribute", PW_TYPE_STRING_PTR, offsetof(ldap_instance_t, profile_attr), NULL, NULL},
67 {"default_profile", PW_TYPE_STRING_PTR, offsetof(ldap_instance_t, default_profile), NULL, NULL},
68 {"filter", PW_TYPE_STRING_PTR, offsetof(ldap_instance_t, profile_filter), NULL, NULL},
70 { NULL, -1, 0, NULL, NULL }
76 static CONF_PARSER user_config[] = {
77 {"filter", PW_TYPE_STRING_PTR, offsetof(ldap_instance_t, userobj_filter), NULL, "(uid=%u)"},
78 {"scope", PW_TYPE_STRING_PTR, offsetof(ldap_instance_t, userobj_scope_str), NULL, "sub"},
79 {"basedn", PW_TYPE_STRING_PTR, offsetof(ldap_instance_t,userobj_base_dn), NULL, NULL},
81 {"access_attribute", PW_TYPE_STRING_PTR, offsetof(ldap_instance_t, userobj_access_attr), NULL, NULL},
82 {"access_positive", PW_TYPE_BOOLEAN, offsetof(ldap_instance_t, access_positive), NULL, "yes"},
84 { NULL, -1, 0, NULL, NULL }
90 static CONF_PARSER group_config[] = {
91 {"filter", PW_TYPE_STRING_PTR, offsetof(ldap_instance_t, groupobj_filter), NULL, NULL},
92 {"scope", PW_TYPE_STRING_PTR, offsetof(ldap_instance_t, groupobj_scope_str), NULL, "sub"},
93 {"basedn", PW_TYPE_STRING_PTR, offsetof(ldap_instance_t, groupobj_base_dn), NULL, NULL},
95 {"name_attribute", PW_TYPE_STRING_PTR, offsetof(ldap_instance_t, groupobj_name_attr), NULL, "cn"},
96 {"membership_attribute", PW_TYPE_STRING_PTR, offsetof(ldap_instance_t, userobj_membership_attr), NULL, NULL},
97 {"membership_filter", PW_TYPE_STRING_PTR, offsetof(ldap_instance_t, groupobj_membership_filter), NULL, NULL},
98 {"cacheable_name", PW_TYPE_BOOLEAN, offsetof(ldap_instance_t, cacheable_group_name), NULL, "no"},
99 {"cacheable_dn", PW_TYPE_BOOLEAN, offsetof(ldap_instance_t, cacheable_group_dn), NULL, "no"},
101 { NULL, -1, 0, NULL, NULL }
105 * Reference for accounting updates
107 static const CONF_PARSER acct_section_config[] = {
108 {"reference", PW_TYPE_STRING_PTR, offsetof(ldap_acct_section_t, reference), NULL, "."},
110 {NULL, -1, 0, NULL, NULL}
114 * Various options that don't belong in the main configuration.
116 * Note that these overlap a bit with the connection pool code!
118 static CONF_PARSER option_config[] = {
120 * Debugging flags to the server
122 {"ldap_debug", PW_TYPE_INTEGER, offsetof(ldap_instance_t,ldap_debug), NULL, "0x0000"},
124 {"chase_referrals", PW_TYPE_BOOLEAN, offsetof(ldap_instance_t,chase_referrals), NULL, NULL},
126 {"rebind", PW_TYPE_BOOLEAN,offsetof(ldap_instance_t,rebind), NULL, NULL},
128 /* timeout on network activity */
129 {"net_timeout", PW_TYPE_INTEGER, offsetof(ldap_instance_t,net_timeout), NULL, "10"},
131 /* timeout for search results */
132 {"res_timeout", PW_TYPE_INTEGER, offsetof(ldap_instance_t,res_timeout), NULL, "20"},
134 /* allow server unlimited time for search (server-side limit) */
135 {"srv_timelimit", PW_TYPE_INTEGER, offsetof(ldap_instance_t,srv_timelimit), NULL, "20"},
137 #ifdef LDAP_OPT_X_KEEPALIVE_IDLE
138 {"idle", PW_TYPE_INTEGER, offsetof(ldap_instance_t,keepalive_idle), NULL, "60"},
140 #ifdef LDAP_OPT_X_KEEPALIVE_PROBES
141 {"probes", PW_TYPE_INTEGER, offsetof(ldap_instance_t,keepalive_probes), NULL, "3"},
143 #ifdef LDAP_OPT_X_KEEPALIVE_INTERVAL
144 {"interval", PW_TYPE_INTEGER, offsetof(ldap_instance_t,keepalive_interval), NULL, "30"},
147 { NULL, -1, 0, NULL, NULL }
151 static const CONF_PARSER module_config[] = {
152 {"server", PW_TYPE_STRING_PTR | PW_TYPE_REQUIRED, offsetof(ldap_instance_t,server), NULL, "localhost"},
153 {"port", PW_TYPE_INTEGER, offsetof(ldap_instance_t,port), NULL, "389"},
155 {"password", PW_TYPE_STRING_PTR, offsetof(ldap_instance_t,password), NULL, ""},
156 {"identity", PW_TYPE_STRING_PTR, offsetof(ldap_instance_t,admin_dn), NULL, ""},
158 {"basedn", PW_TYPE_STRING_PTR, offsetof(ldap_instance_t,base_dn), NULL, ""},
160 {"valuepair_attr", PW_TYPE_STRING_PTR, offsetof(ldap_instance_t, base_dn), NULL, NULL},
163 /* support for eDirectory Universal Password */
164 {"edir", PW_TYPE_BOOLEAN, offsetof(ldap_instance_t,edir), NULL, NULL}, /* NULL defaults to "no" */
167 * Attempt to bind with the Cleartext password we got from eDirectory
168 * Universal password for additional authorization checks.
170 {"edir_autz", PW_TYPE_BOOLEAN, offsetof(ldap_instance_t,edir_autz), NULL, NULL}, /* NULL defaults to "no" */
173 { "user", PW_TYPE_SUBSECTION, 0, NULL, (void const *) user_config },
175 { "group", PW_TYPE_SUBSECTION, 0, NULL, (void const *) group_config },
177 { "profiles", PW_TYPE_SUBSECTION, 0, NULL, (void const *) profile_config },
179 { "options", PW_TYPE_SUBSECTION, 0, NULL, (void const *) option_config },
181 { "tls", PW_TYPE_SUBSECTION, 0, NULL, (void const *) tls_config },
183 {NULL, -1, 0, NULL, NULL}
186 /** Expand an LDAP URL into a query, and return a string result from that query.
189 static size_t ldap_xlat(void *instance, REQUEST *request, char const *fmt,
190 char *out, size_t freespace)
194 ldap_instance_t *inst = instance;
195 LDAPURLDesc *ldap_url;
196 LDAPMessage *result = NULL;
197 LDAPMessage *entry = NULL;
206 if (!ldap_is_ldap_url(url)) {
207 RDEBUGE("String passed does not look like an LDAP URL");
211 if (ldap_url_parse(url, &ldap_url)){
212 RDEBUGE("Parsing LDAP URL failed");
217 * Nothing, empty string, "*" string, or got 2 things, die.
219 if (!ldap_url->lud_attrs || !ldap_url->lud_attrs[0] ||
220 !*ldap_url->lud_attrs[0] ||
221 (strcmp(ldap_url->lud_attrs[0], "*") == 0) ||
222 ldap_url->lud_attrs[1]) {
223 RDEBUGE("Bad attributes list in LDAP URL. URL must specify exactly one attribute to retrieve");
228 if (ldap_url->lud_host &&
229 ((strncmp(inst->server, ldap_url->lud_host, strlen(inst->server)) != 0) ||
230 (ldap_url->lud_port != inst->port))) {
231 RDEBUG("Requested server/port is \"%s:%i\"", ldap_url->lud_host, inst->port);
236 conn = rlm_ldap_get_socket(inst, request);
237 if (!conn) goto free_urldesc;
239 memcpy(&attrs, &ldap_url->lud_attrs, sizeof(attrs));
241 status = rlm_ldap_search(inst, request, &conn, ldap_url->lud_dn, ldap_url->lud_scope, ldap_url->lud_filter,
244 case LDAP_PROC_SUCCESS:
246 case LDAP_PROC_NO_RESULT:
247 RDEBUG("Search returned not found");
255 entry = ldap_first_entry(conn->handle, result);
257 ldap_get_option(conn->handle, LDAP_OPT_RESULT_CODE, &ldap_errno);
258 RDEBUGE("Failed retrieving entry: %s", ldap_err2string(ldap_errno));
262 vals = ldap_get_values(conn->handle, entry, ldap_url->lud_attrs[0]);
264 RDEBUG("No \"%s\" attributes found in specified object", ldap_url->lud_attrs[0]);
268 length = strlen(vals[0]);
269 if (length >= freespace){
274 strlcpy(out, vals[0], freespace);
277 ldap_value_free(vals);
279 ldap_msgfree(result);
281 rlm_ldap_release_socket(inst, conn);
283 ldap_free_urldesc(ldap_url);
288 /** Perform LDAP-Group comparison checking
290 * Attempts to match users to groups using a variety of methods.
292 * @param instance of the rlm_ldap module.
293 * @param request Current request.
294 * @param thing Unknown.
295 * @param check Which group to check for user membership.
296 * @param check_pairs Unknown.
297 * @param reply_pairs Unknown.
298 * @return 1 on failure (or if the user is not a member), else 0.
300 static int rlm_ldap_groupcmp(void *instance, REQUEST *request, UNUSED VALUE_PAIR *thing, VALUE_PAIR *check,
301 UNUSED VALUE_PAIR *check_pairs, UNUSED VALUE_PAIR **reply_pairs)
303 ldap_instance_t *inst = instance;
309 ldap_handle_t *conn = NULL;
312 RDEBUG("Searching for user in group \"%s\"", check->vp_strvalue);
314 if (check->length == 0) {
315 RDEBUG("Cannot do comparison (group name is empty)");
320 * Check if we can do cached membership verification
322 check_is_dn = rlm_ldap_is_dn(check->vp_strvalue);
323 if ((check_is_dn && inst->cacheable_group_dn) || (!check_is_dn && inst->cacheable_group_name)) {
324 switch(rlm_ldap_check_cached(inst, request, check)) {
325 case RLM_MODULE_NOTFOUND:
334 conn = rlm_ldap_get_socket(inst, request);
338 * This is used in the default membership filter.
340 user_dn = rlm_ldap_find_user(inst, request, &conn, NULL, false, NULL, &rcode);
342 rlm_ldap_release_socket(inst, conn);
349 * Check groupobj user membership
351 if (inst->groupobj_membership_filter) {
352 switch(rlm_ldap_check_groupobj_dynamic(inst, request, &conn, check)) {
353 case RLM_MODULE_NOTFOUND:
365 * Check userobj group membership
367 if (inst->userobj_membership_attr) {
368 switch(rlm_ldap_check_userobj_dynamic(inst, request, &conn, user_dn, check)) {
369 case RLM_MODULE_NOTFOUND:
382 rlm_ldap_release_socket(inst, conn);
386 RDEBUG("User is not a member of specified group");
394 /** Detach from the LDAP server and cleanup internal state.
397 static int mod_detach(void *instance)
399 ldap_instance_t *inst = instance;
401 fr_connection_pool_delete(inst->pool);
403 if (inst->user_map) {
404 talloc_free(inst->user_map);
410 /** Parse an accounting sub section.
412 * Allocate a new ldap_acct_section_t and write the config data into it.
414 * @param[in] inst rlm_ldap configuration.
415 * @param[in] parent of the config section.
416 * @param[out] config to write the sub section parameters to.
417 * @param[in] comp The section name were parsing the config for.
418 * @return 0 on success, else < 0 on failure.
420 static int parse_sub_section(ldap_instance_t *inst, CONF_SECTION *parent, ldap_acct_section_t **config,
421 rlm_components_t comp)
425 char const *name = section_type_value[comp].section;
427 cs = cf_section_sub_find(parent, name);
429 radlog(L_INFO, "rlm_ldap (%s): Couldn't find configuration for %s, will return NOOP for calls "
430 "from this section", inst->xlat_name, name);
435 *config = talloc_zero(inst, ldap_acct_section_t);
436 if (cf_section_parse(cs, *config, acct_section_config) < 0) {
437 LDAP_ERR("Failed parsing configuration for section %s", name);
447 /** Instantiate the module
449 * Creates a new instance of the module reading parameters from a configuration section.
451 * @param conf to parse.
452 * @param instance Where to write pointer to configuration data.
453 * @return 0 on success < 0 on failure.
455 static int mod_instantiate(CONF_SECTION *conf, void *instance)
457 ldap_instance_t *inst = instance;
461 inst->chase_referrals = 2; /* use OpenLDAP defaults */
464 inst->xlat_name = cf_section_name2(conf);
465 if (!inst->xlat_name) {
466 inst->xlat_name = cf_section_name1(conf);
470 * If the configuration parameters can't be parsed, then fail.
472 if ((parse_sub_section(inst, conf, &inst->accounting, RLM_COMPONENT_ACCT) < 0) ||
473 (parse_sub_section(inst, conf, &inst->postauth, RLM_COMPONENT_POST_AUTH) < 0)) {
474 LDAP_ERR("Failed parsing configuration");
480 * Sanity checks for cacheable groups code.
482 if (inst->cacheable_group_name && inst->groupobj_membership_filter && !inst->groupobj_name_attr) {
483 LDAP_ERR("Directive 'group.name_attribute' must be set if cacheable group names are enabled");
489 * Copy across values from base_dn to the object specific base_dn.
491 if (!inst->groupobj_base_dn) {
492 if (!inst->base_dn) {
493 LDAP_ERR("Must set 'base_dn' if there is no 'group_base_dn'");
498 inst->groupobj_base_dn = inst->base_dn;
501 if (!inst->userobj_base_dn) {
502 if (!inst->base_dn) {
503 LDAP_ERR("Must set 'base_dn' if there is no 'userobj_base_dn'");
508 inst->userobj_base_dn = inst->base_dn;
512 * Check for URLs. If they're used and the library doesn't support them, then complain.
515 if (ldap_is_ldap_url(inst->server)) {
516 #ifdef HAVE_LDAP_INITIALIZE
520 LDAP_ERR("'server' directive is in URL form but ldap_initialize() is not available");
526 * Workaround for servers which support LDAPS but not START TLS
528 if (inst->port == LDAPS_PORT || inst->tls_mode) {
529 inst->tls_mode = LDAP_OPT_X_TLS_HARD;
534 #if LDAP_SET_REBIND_PROC_ARGS != 3
536 * The 2-argument rebind doesn't take an instance variable. Our rebind function needs the instance
537 * variable for the username, password, etc.
539 if (inst->rebind == 1) {
540 LDAP_ERR("Cannot use 'rebind' directive as this version of libldap does not support the API "
548 * Convert scope strings to integers
550 inst->userobj_scope = fr_str2int(ldap_scope, inst->userobj_scope_str, -1);
551 if (inst->userobj_scope < 0) {
552 LDAP_ERR("Invalid 'user.scope' value '%s', expected 'sub', 'one' or 'base'",
553 inst->userobj_scope_str);
557 inst->groupobj_scope = fr_str2int(ldap_scope, inst->groupobj_scope_str, -1);
558 if (inst->groupobj_scope < 0) {
559 LDAP_ERR("Invalid 'group.scope' value '%s', expected 'sub', 'one' or 'base'",
560 inst->groupobj_scope_str);
565 * Build the attribute map
567 if (rlm_ldap_map_verify(inst, &(inst->user_map)) < 0) {
572 * Group comparison checks.
574 inst->group_da = dict_attrbyvalue(PW_LDAP_GROUP, 0);
575 paircompare_register(PW_LDAP_GROUP, PW_USER_NAME, rlm_ldap_groupcmp, inst);
576 if (cf_section_name2(conf)) {
580 snprintf(buffer, sizeof(buffer), "%s-Ldap-Group",
582 memset(&flags, 0, sizeof(flags));
584 dict_addattr(buffer, -1, 0, PW_TYPE_STRING, flags);
585 inst->group_da = dict_attrbyname(buffer);
586 if (!inst->group_da) {
587 LDAP_ERR("Failed creating attribute %s", buffer);
593 paircompare_register(inst->group_da->attr, PW_USER_NAME, rlm_ldap_groupcmp, inst);
596 xlat_register(inst->xlat_name, ldap_xlat, rlm_ldap_escape_func, inst);
599 * Initialize the socket pool.
601 inst->pool = fr_connection_pool_init(inst->cs, inst, mod_conn_create, NULL, mod_conn_delete, NULL);
612 /** Check the user's password against ldap directory
614 * @param instance rlm_ldap configuration.
615 * @param request Current request.
616 * @return one of the RLM_MODULE_* values.
618 static rlm_rcode_t mod_authenticate(void *instance, REQUEST *request)
623 ldap_instance_t *inst = instance;
627 * Ensure that we're being passed a plain-text password, and not
631 if (!request->username) {
632 RDEBUGE("Attribute \"User-Name\" is required for authentication");
634 return RLM_MODULE_INVALID;
637 if (!request->password ||
638 (request->password->da->attr != PW_USER_PASSWORD)) {
639 RDEBUGW("You have set \"Auth-Type := LDAP\" somewhere.");
640 RDEBUGW("*********************************************");
641 RDEBUGW("* THAT CONFIGURATION IS WRONG. DELETE IT. ");
642 RDEBUGW("* YOU ARE PREVENTING THE SERVER FROM WORKING.");
643 RDEBUGW("*********************************************");
645 RDEBUGE("Attribute \"User-Password\" is required for authentication.");
647 return RLM_MODULE_INVALID;
650 if (request->password->length == 0) {
651 RDEBUGE("Empty password supplied");
653 return RLM_MODULE_INVALID;
656 RDEBUG("Login attempt by \"%s\"", request->username->vp_strvalue);
658 conn = rlm_ldap_get_socket(inst, request);
659 if (!conn) return RLM_MODULE_FAIL;
662 * Get the DN by doing a search.
664 dn = rlm_ldap_find_user(inst, request, &conn, NULL, false, NULL, &rcode);
666 rlm_ldap_release_socket(inst, conn);
674 conn->rebound = true;
675 status = rlm_ldap_bind(inst, request, &conn, dn, request->password->vp_strvalue, true);
677 case LDAP_PROC_SUCCESS:
678 rcode = RLM_MODULE_OK;
679 RDEBUG("Bind as user \"%s\" was successful", dn);
682 case LDAP_PROC_NOT_PERMITTED:
683 rcode = RLM_MODULE_USERLOCK;
686 case LDAP_PROC_REJECT:
687 rcode = RLM_MODULE_REJECT;
690 case LDAP_PROC_BAD_DN:
691 rcode = RLM_MODULE_INVALID;
694 case LDAP_PROC_NO_RESULT:
695 rcode = RLM_MODULE_NOTFOUND;
699 rcode = RLM_MODULE_FAIL;
703 rlm_ldap_release_socket(inst, conn);
708 /** Check if user is authorized for remote access
711 static rlm_rcode_t mod_authorize(void *instance, REQUEST *request)
713 rlm_rcode_t rcode = RLM_MODULE_OK;
717 ldap_instance_t *inst = instance;
721 LDAPMessage *result, *entry;
722 char const *dn = NULL;
723 rlm_ldap_map_xlat_t expanded; /* faster that mallocing every time */
725 if (!request->username) {
726 RDEBUG2("Attribute \"User-Name\" is required for authorization.");
728 return RLM_MODULE_NOOP;
732 * Check for valid input, zero length names not permitted
734 if (request->username->length == 0) {
735 RDEBUG2("Zero length username not permitted");
737 return RLM_MODULE_INVALID;
740 if (rlm_ldap_map_xlat(request, inst->user_map, &expanded) < 0) {
741 return RLM_MODULE_FAIL;
744 conn = rlm_ldap_get_socket(inst, request);
745 if (!conn) return RLM_MODULE_FAIL;
748 * Add any additional attributes we need for checking access, memberships, and profiles
750 if (inst->userobj_access_attr) {
751 expanded.attrs[expanded.count++] = inst->userobj_access_attr;
754 if (inst->userobj_membership_attr && (inst->cacheable_group_dn || inst->cacheable_group_name)) {
755 expanded.attrs[expanded.count++] = inst->userobj_membership_attr;
758 if (inst->profile_attr) {
759 expanded.attrs[expanded.count++] = inst->profile_attr;
762 if (inst->valuepair_attr) {
763 expanded.attrs[expanded.count++] = inst->valuepair_attr;
766 expanded.attrs[expanded.count] = NULL;
768 dn = rlm_ldap_find_user(inst, request, &conn, expanded.attrs, true, &result, &rcode);
773 entry = ldap_first_entry(conn->handle, result);
775 ldap_get_option(conn->handle, LDAP_OPT_RESULT_CODE, &ldap_errno);
776 RDEBUGE("Failed retrieving entry: %s", ldap_err2string(ldap_errno));
784 if (inst->userobj_access_attr) {
785 rcode = rlm_ldap_check_access(inst, request, conn, entry);
786 if (rcode != RLM_MODULE_OK) {
792 * Check if we need to cache group memberships
794 if (inst->cacheable_group_dn || inst->cacheable_group_name) {
795 rcode = rlm_ldap_cacheable_userobj(inst, request, &conn, entry);
796 if (rcode != RLM_MODULE_OK) {
800 rcode = rlm_ldap_cacheable_groupobj(inst, request, &conn);
801 if (rcode != RLM_MODULE_OK) {
808 * We already have a Cleartext-Password. Skip edir.
810 if (pairfind(request->config_items, PW_CLEARTEXT_PASSWORD, 0, TAG_ANY)) {
815 * Retrieve Universal Password if we use eDirectory
820 size_t pass_size = sizeof(password);
823 * Retrive universal password
825 res = nmasldap_get_password(conn->handle, dn, password, &pass_size);
827 RDEBUGW("Failed to retrieve eDirectory password");
828 rcode = RLM_MODULE_NOOP;
834 * Add Cleartext-Password attribute to the request
836 vp = radius_paircreate(request, &request->config_items, PW_CLEARTEXT_PASSWORD, 0);
837 strlcpy(vp->vp_strvalue, password, sizeof(vp->vp_strvalue));
838 vp->length = pass_size;
840 RDEBUG2("Added eDirectory password in check items as %s = %s", vp->da->name, vp->vp_strvalue);
842 if (inst->edir_autz) {
843 RDEBUG2("Binding as user for eDirectory authorization checks");
847 conn->rebound = true;
848 status = rlm_ldap_bind(inst, request, &conn, dn, vp->vp_strvalue, true);
850 case LDAP_PROC_SUCCESS:
851 rcode = RLM_MODULE_OK;
852 RDEBUG("Bind as user \"%s\" was successful", dn);
855 case LDAP_PROC_NOT_PERMITTED:
856 rcode = RLM_MODULE_USERLOCK;
859 case LDAP_PROC_REJECT:
860 rcode = RLM_MODULE_REJECT;
863 case LDAP_PROC_BAD_DN:
864 rcode = RLM_MODULE_INVALID;
867 case LDAP_PROC_NO_RESULT:
868 rcode = RLM_MODULE_NOTFOUND;
872 rcode = RLM_MODULE_FAIL;
883 * Apply ONE user profile, or a default user profile.
885 vp = pairfind(request->config_items, PW_USER_PROFILE, 0, TAG_ANY);
886 if (vp || inst->default_profile) {
887 char const *profile = inst->default_profile;
889 if (vp) profile = vp->vp_strvalue;
891 rlm_ldap_map_profile(inst, request, &conn, profile, &expanded);
895 * Apply a SET of user profiles.
897 if (inst->profile_attr) {
898 vals = ldap_get_values(conn->handle, entry, inst->profile_attr);
900 for (i = 0; vals[i] != NULL; i++) {
901 rlm_ldap_map_profile(inst, request, &conn, vals[i], &expanded);
904 ldap_value_free(vals);
908 if (inst->user_map) {
909 rlm_ldap_map_do(inst, request, conn->handle, &expanded, entry);
910 rlm_ldap_check_reply(inst, request);
914 rlm_ldap_map_xlat_free(&expanded);
916 ldap_msgfree(result);
918 rlm_ldap_release_socket(inst, conn);
923 /** Modify user's object in LDAP
925 * Process a modifcation map to update a user object in the LDAP directory.
927 * @param inst rlm_ldap instance.
928 * @param request Current request.
929 * @param section that holds the map to process.
930 * @return one of the RLM_MODULE_* values.
932 static rlm_rcode_t user_modify(ldap_instance_t *inst, REQUEST *request, ldap_acct_section_t *section)
934 rlm_rcode_t rcode = RLM_MODULE_OK;
936 ldap_handle_t *conn = NULL;
938 LDAPMod *mod_p[LDAP_MAX_ATTRMAP + 1], mod_s[LDAP_MAX_ATTRMAP];
939 LDAPMod **modify = mod_p;
941 char *passed[LDAP_MAX_ATTRMAP * 2];
942 int i, total = 0, last_pass = 0;
944 char *expanded[LDAP_MAX_ATTRMAP];
952 * Build our set of modifications using the update sections in
959 char path[MAX_STRING_LEN];
966 * Locate the update section were going to be using
968 if (section->reference[0] != '.') {
972 if (radius_xlat(p, (sizeof(path) - (p - path)) - 1, request, section->reference, NULL, NULL) < 0) {
976 ci = cf_reference_item(NULL, section->cs, path);
981 if (!cf_item_is_section(ci)){
982 RDEBUGE("Reference must resolve to a section");
987 cs = cf_section_sub_find(cf_itemtosection(ci), "update");
989 RDEBUGE("Section must contain 'update' subsection");
995 * Iterate over all the pairs, building our mods array
997 for (ci = cf_item_find_next(cs, NULL); ci != NULL; ci = cf_item_find_next(cs, ci)) {
1000 if (total == LDAP_MAX_ATTRMAP) {
1001 RDEBUGE("Modify map size exceeded");
1006 if (!cf_item_is_pair(ci)) {
1007 RDEBUGE("Entry is not in \"ldap-attribute = value\" format");
1013 * Retrieve all the information we need about the pair
1015 cp = cf_itemtopair(ci);
1016 value = cf_pair_value(cp);
1017 attr = cf_pair_attr(cp);
1018 op = cf_pair_operator(cp);
1020 if (!value || (*value == '\0')) {
1021 RDEBUG("Empty value string, skipping attribute \"%s\"", attr);
1026 switch (cf_pair_value_type(cp))
1029 case T_SINGLE_QUOTED_STRING:
1031 case T_BACK_QUOTED_STRING:
1032 case T_DOUBLE_QUOTED_STRING:
1040 if (op == T_OP_CMP_FALSE) {
1041 passed[last_pass] = NULL;
1042 } else if (do_xlat) {
1045 if (radius_xlat(exp, 0, request, value, NULL, NULL) <= 0) {
1046 RDEBUG("Skipping attribute \"%s\"", attr);
1053 expanded[last_exp++] = exp;
1054 passed[last_pass] = exp;
1059 memcpy(&(passed[last_pass]), &value, sizeof(passed[last_pass]));
1062 passed[last_pass + 1] = NULL;
1064 mod_s[total].mod_values = &(passed[last_pass]);
1071 * T_OP_EQ is *NOT* supported, it is impossible to
1072 * support because of the lack of transactions in LDAP
1075 mod_s[total].mod_op = LDAP_MOD_ADD;
1079 mod_s[total].mod_op = LDAP_MOD_REPLACE;
1083 case T_OP_CMP_FALSE:
1084 mod_s[total].mod_op = LDAP_MOD_DELETE;
1087 #ifdef LDAP_MOD_INCREMENT
1089 mod_s[total].mod_op = LDAP_MOD_INCREMENT;
1093 RDEBUGE("Operator '%s' is not supported for LDAP modify operations",
1094 fr_int2str(fr_tokens, op, "¿unknown?"));
1100 * Now we know the value is ok, copy the pointers into
1101 * the ldapmod struct.
1103 memcpy(&(mod_s[total].mod_type), &(attr), sizeof(mod_s[total].mod_type));
1105 mod_p[total] = &(mod_s[total]);
1110 rcode = RLM_MODULE_NOOP;
1114 mod_p[total] = NULL;
1116 conn = rlm_ldap_get_socket(inst, request);
1117 if (!conn) return RLM_MODULE_FAIL;
1120 dn = rlm_ldap_find_user(inst, request, &conn, NULL, false, NULL, &rcode);
1121 if (!dn || (rcode != RLM_MODULE_OK)) {
1125 rcode = rlm_ldap_modify(inst, request, &conn, dn, modify);
1130 * Free up any buffers we allocated for xlat expansion
1132 for (i = 0; i < last_exp; i++) {
1133 talloc_free(expanded[i]);
1136 rlm_ldap_release_socket(inst, conn);
1141 static rlm_rcode_t mod_accounting(void *instance, REQUEST * request) {
1142 ldap_instance_t *inst = instance;
1144 if (inst->accounting) {
1145 return user_modify(inst, request, inst->accounting);
1148 return RLM_MODULE_NOOP;
1151 static rlm_rcode_t mod_post_auth(void *instance, REQUEST * request)
1153 ldap_instance_t *inst = instance;
1155 if (inst->postauth) {
1156 return user_modify(inst, request, inst->postauth);
1159 return RLM_MODULE_NOOP;
1163 /* globally exported name */
1164 module_t rlm_ldap = {
1167 RLM_TYPE_THREAD_SAFE, /* type: reserved */
1168 sizeof(ldap_instance_t),
1170 mod_instantiate, /* instantiation */
1171 mod_detach, /* detach */
1173 mod_authenticate, /* authentication */
1174 mod_authorize, /* authorization */
1175 NULL, /* preaccounting */
1176 mod_accounting, /* accounting */
1177 NULL, /* checksimul */
1178 NULL, /* pre-proxy */
1179 NULL, /* post-proxy */
1180 mod_post_auth /* post-auth */