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 },
49 #ifdef LDAP_OPT_X_TLS_NEVER
50 const FR_NAME_NUMBER ldap_tls_require_cert[] = {
51 { "never", LDAP_OPT_X_TLS_NEVER },
52 { "demand", LDAP_OPT_X_TLS_DEMAND },
53 { "allow", LDAP_OPT_X_TLS_ALLOW },
54 { "try", LDAP_OPT_X_TLS_TRY },
55 { "hard", LDAP_OPT_X_TLS_HARD }, /* oh yes, just like that */
64 static CONF_PARSER tls_config[] = {
65 {"start_tls", PW_TYPE_BOOLEAN, offsetof(ldap_instance_t, start_tls), NULL, "no"},
66 {"cacertfile", PW_TYPE_FILENAME, offsetof(ldap_instance_t, tls_cacertfile), NULL, NULL},
67 {"cacertdir", PW_TYPE_FILENAME, offsetof(ldap_instance_t, tls_cacertdir), NULL, NULL},
68 {"certfile", PW_TYPE_FILENAME, offsetof(ldap_instance_t, tls_certfile), NULL, NULL},
69 {"keyfile", PW_TYPE_FILENAME, offsetof(ldap_instance_t, tls_keyfile), NULL, NULL}, // OK if it changes on HUP
70 {"randfile", PW_TYPE_STRING_PTR, offsetof(ldap_instance_t, tls_randfile), NULL, NULL},
71 {"require_cert", PW_TYPE_STRING_PTR, offsetof(ldap_instance_t, tls_require_cert_str), NULL, NULL},
73 { NULL, -1, 0, NULL, NULL }
77 static CONF_PARSER profile_config[] = {
78 {"profile_attribute", PW_TYPE_STRING_PTR, offsetof(ldap_instance_t, profile_attr), NULL, NULL},
79 {"default_profile", PW_TYPE_STRING_PTR, offsetof(ldap_instance_t, default_profile), NULL, NULL},
80 {"filter", PW_TYPE_STRING_PTR, offsetof(ldap_instance_t, profile_filter), NULL, NULL},
82 { NULL, -1, 0, NULL, NULL }
88 static CONF_PARSER user_config[] = {
89 {"filter", PW_TYPE_STRING_PTR, offsetof(ldap_instance_t, userobj_filter), NULL, "(uid=%u)"},
90 {"scope", PW_TYPE_STRING_PTR, offsetof(ldap_instance_t, userobj_scope_str), NULL, "sub"},
91 {"basedn", PW_TYPE_STRING_PTR, offsetof(ldap_instance_t,userobj_base_dn), NULL, NULL},
93 {"access_attribute", PW_TYPE_STRING_PTR, offsetof(ldap_instance_t, userobj_access_attr), NULL, NULL},
94 {"access_positive", PW_TYPE_BOOLEAN, offsetof(ldap_instance_t, access_positive), NULL, "yes"},
96 { NULL, -1, 0, NULL, NULL }
100 * Group configuration
102 static CONF_PARSER group_config[] = {
103 {"filter", PW_TYPE_STRING_PTR, offsetof(ldap_instance_t, groupobj_filter), NULL, NULL},
104 {"scope", PW_TYPE_STRING_PTR, offsetof(ldap_instance_t, groupobj_scope_str), NULL, "sub"},
105 {"basedn", PW_TYPE_STRING_PTR, offsetof(ldap_instance_t, groupobj_base_dn), NULL, NULL},
107 {"name_attribute", PW_TYPE_STRING_PTR, offsetof(ldap_instance_t, groupobj_name_attr), NULL, "cn"},
108 {"membership_attribute", PW_TYPE_STRING_PTR, offsetof(ldap_instance_t, userobj_membership_attr), NULL, NULL},
109 {"membership_filter", PW_TYPE_STRING_PTR, offsetof(ldap_instance_t, groupobj_membership_filter), NULL, NULL},
110 {"cacheable_name", PW_TYPE_BOOLEAN, offsetof(ldap_instance_t, cacheable_group_name), NULL, "no"},
111 {"cacheable_dn", PW_TYPE_BOOLEAN, offsetof(ldap_instance_t, cacheable_group_dn), NULL, "no"},
113 { NULL, -1, 0, NULL, NULL }
117 * Reference for accounting updates
119 static const CONF_PARSER acct_section_config[] = {
120 {"reference", PW_TYPE_STRING_PTR, offsetof(ldap_acct_section_t, reference), NULL, "."},
122 {NULL, -1, 0, NULL, NULL}
126 * Various options that don't belong in the main configuration.
128 * Note that these overlap a bit with the connection pool code!
130 static CONF_PARSER option_config[] = {
132 * Debugging flags to the server
134 {"ldap_debug", PW_TYPE_INTEGER, offsetof(ldap_instance_t,ldap_debug), NULL, "0x0000"},
136 {"chase_referrals", PW_TYPE_BOOLEAN, offsetof(ldap_instance_t,chase_referrals), NULL, NULL},
138 {"rebind", PW_TYPE_BOOLEAN,offsetof(ldap_instance_t,rebind), NULL, NULL},
140 /* timeout on network activity */
141 {"net_timeout", PW_TYPE_INTEGER, offsetof(ldap_instance_t,net_timeout), NULL, "10"},
143 /* timeout for search results */
144 {"res_timeout", PW_TYPE_INTEGER, offsetof(ldap_instance_t,res_timeout), NULL, "20"},
146 /* allow server unlimited time for search (server-side limit) */
147 {"srv_timelimit", PW_TYPE_INTEGER, offsetof(ldap_instance_t,srv_timelimit), NULL, "20"},
149 #ifdef LDAP_OPT_X_KEEPALIVE_IDLE
150 {"idle", PW_TYPE_INTEGER, offsetof(ldap_instance_t,keepalive_idle), NULL, "60"},
152 #ifdef LDAP_OPT_X_KEEPALIVE_PROBES
153 {"probes", PW_TYPE_INTEGER, offsetof(ldap_instance_t,keepalive_probes), NULL, "3"},
155 #ifdef LDAP_OPT_X_KEEPALIVE_INTERVAL
156 {"interval", PW_TYPE_INTEGER, offsetof(ldap_instance_t,keepalive_interval), NULL, "30"},
159 { NULL, -1, 0, NULL, NULL }
163 static const CONF_PARSER module_config[] = {
164 {"server", PW_TYPE_STRING_PTR | PW_TYPE_REQUIRED, offsetof(ldap_instance_t,server), NULL, "localhost"},
165 {"port", PW_TYPE_INTEGER, offsetof(ldap_instance_t,port), NULL, "389"},
167 {"password", PW_TYPE_STRING_PTR, offsetof(ldap_instance_t,password), NULL, ""},
168 {"identity", PW_TYPE_STRING_PTR, offsetof(ldap_instance_t,admin_dn), NULL, ""},
170 {"basedn", PW_TYPE_STRING_PTR, offsetof(ldap_instance_t,base_dn), NULL, ""},
172 {"valuepair_attr", PW_TYPE_STRING_PTR, offsetof(ldap_instance_t, base_dn), NULL, NULL},
175 /* support for eDirectory Universal Password */
176 {"edir", PW_TYPE_BOOLEAN, offsetof(ldap_instance_t,edir), NULL, NULL}, /* NULL defaults to "no" */
179 * Attempt to bind with the Cleartext password we got from eDirectory
180 * Universal password for additional authorization checks.
182 {"edir_autz", PW_TYPE_BOOLEAN, offsetof(ldap_instance_t,edir_autz), NULL, NULL}, /* NULL defaults to "no" */
185 { "user", PW_TYPE_SUBSECTION, 0, NULL, (void const *) user_config },
187 { "group", PW_TYPE_SUBSECTION, 0, NULL, (void const *) group_config },
189 { "profiles", PW_TYPE_SUBSECTION, 0, NULL, (void const *) profile_config },
191 { "options", PW_TYPE_SUBSECTION, 0, NULL, (void const *) option_config },
193 { "tls", PW_TYPE_SUBSECTION, 0, NULL, (void const *) tls_config },
195 {NULL, -1, 0, NULL, NULL}
198 /** Expand an LDAP URL into a query, and return a string result from that query.
201 static size_t ldap_xlat(void *instance, REQUEST *request, char const *fmt,
202 char *out, size_t freespace)
206 ldap_instance_t *inst = instance;
207 LDAPURLDesc *ldap_url;
208 LDAPMessage *result = NULL;
209 LDAPMessage *entry = NULL;
218 if (!ldap_is_ldap_url(url)) {
219 REDEBUG("String passed does not look like an LDAP URL");
223 if (ldap_url_parse(url, &ldap_url)){
224 REDEBUG("Parsing LDAP URL failed");
229 * Nothing, empty string, "*" string, or got 2 things, die.
231 if (!ldap_url->lud_attrs || !ldap_url->lud_attrs[0] ||
232 !*ldap_url->lud_attrs[0] ||
233 (strcmp(ldap_url->lud_attrs[0], "*") == 0) ||
234 ldap_url->lud_attrs[1]) {
235 REDEBUG("Bad attributes list in LDAP URL. URL must specify exactly one attribute to retrieve");
240 if (ldap_url->lud_host &&
241 ((strncmp(inst->server, ldap_url->lud_host, strlen(inst->server)) != 0) ||
242 (ldap_url->lud_port != inst->port))) {
243 RDEBUG("Requested server/port is \"%s:%i\"", ldap_url->lud_host, inst->port);
248 conn = rlm_ldap_get_socket(inst, request);
249 if (!conn) goto free_urldesc;
251 memcpy(&attrs, &ldap_url->lud_attrs, sizeof(attrs));
253 status = rlm_ldap_search(inst, request, &conn, ldap_url->lud_dn, ldap_url->lud_scope, ldap_url->lud_filter,
256 case LDAP_PROC_SUCCESS:
258 case LDAP_PROC_NO_RESULT:
259 RDEBUG("Search returned not found");
267 entry = ldap_first_entry(conn->handle, result);
269 ldap_get_option(conn->handle, LDAP_OPT_RESULT_CODE, &ldap_errno);
270 REDEBUG("Failed retrieving entry: %s", ldap_err2string(ldap_errno));
274 vals = ldap_get_values(conn->handle, entry, ldap_url->lud_attrs[0]);
276 RDEBUG("No \"%s\" attributes found in specified object", ldap_url->lud_attrs[0]);
280 length = strlen(vals[0]);
281 if (length >= freespace){
286 strlcpy(out, vals[0], freespace);
289 ldap_value_free(vals);
291 ldap_msgfree(result);
293 rlm_ldap_release_socket(inst, conn);
295 ldap_free_urldesc(ldap_url);
300 /** Perform LDAP-Group comparison checking
302 * Attempts to match users to groups using a variety of methods.
304 * @param instance of the rlm_ldap module.
305 * @param request Current request.
306 * @param thing Unknown.
307 * @param check Which group to check for user membership.
308 * @param check_pairs Unknown.
309 * @param reply_pairs Unknown.
310 * @return 1 on failure (or if the user is not a member), else 0.
312 static int rlm_ldap_groupcmp(void *instance, REQUEST *request, UNUSED VALUE_PAIR *thing, VALUE_PAIR *check,
313 UNUSED VALUE_PAIR *check_pairs, UNUSED VALUE_PAIR **reply_pairs)
315 ldap_instance_t *inst = instance;
321 ldap_handle_t *conn = NULL;
324 RDEBUG("Searching for user in group \"%s\"", check->vp_strvalue);
326 if (check->length == 0) {
327 RDEBUG("Cannot do comparison (group name is empty)");
332 * Check if we can do cached membership verification
334 check_is_dn = rlm_ldap_is_dn(check->vp_strvalue);
335 if ((check_is_dn && inst->cacheable_group_dn) || (!check_is_dn && inst->cacheable_group_name)) {
336 switch(rlm_ldap_check_cached(inst, request, check)) {
337 case RLM_MODULE_NOTFOUND:
346 conn = rlm_ldap_get_socket(inst, request);
350 * This is used in the default membership filter.
352 user_dn = rlm_ldap_find_user(inst, request, &conn, NULL, false, NULL, &rcode);
354 rlm_ldap_release_socket(inst, conn);
361 * Check groupobj user membership
363 if (inst->groupobj_membership_filter) {
364 switch(rlm_ldap_check_groupobj_dynamic(inst, request, &conn, check)) {
365 case RLM_MODULE_NOTFOUND:
377 * Check userobj group membership
379 if (inst->userobj_membership_attr) {
380 switch(rlm_ldap_check_userobj_dynamic(inst, request, &conn, user_dn, check)) {
381 case RLM_MODULE_NOTFOUND:
394 rlm_ldap_release_socket(inst, conn);
398 RDEBUG("User is not a member of specified group");
406 /** Detach from the LDAP server and cleanup internal state.
409 static int mod_detach(void *instance)
411 ldap_instance_t *inst = instance;
413 fr_connection_pool_delete(inst->pool);
415 if (inst->user_map) {
416 talloc_free(inst->user_map);
422 /** Parse an accounting sub section.
424 * Allocate a new ldap_acct_section_t and write the config data into it.
426 * @param[in] inst rlm_ldap configuration.
427 * @param[in] parent of the config section.
428 * @param[out] config to write the sub section parameters to.
429 * @param[in] comp The section name were parsing the config for.
430 * @return 0 on success, else < 0 on failure.
432 static int parse_sub_section(ldap_instance_t *inst, CONF_SECTION *parent, ldap_acct_section_t **config,
433 rlm_components_t comp)
437 char const *name = section_type_value[comp].section;
439 cs = cf_section_sub_find(parent, name);
441 INFO("rlm_ldap (%s): Couldn't find configuration for %s, will return NOOP for calls "
442 "from this section", inst->xlat_name, name);
447 *config = talloc_zero(inst, ldap_acct_section_t);
448 if (cf_section_parse(cs, *config, acct_section_config) < 0) {
449 LDAP_ERR("Failed parsing configuration for section %s", name);
459 /** Instantiate the module
461 * Creates a new instance of the module reading parameters from a configuration section.
463 * @param conf to parse.
464 * @param instance Where to write pointer to configuration data.
465 * @return 0 on success < 0 on failure.
467 static int mod_instantiate(CONF_SECTION *conf, void *instance)
469 ldap_instance_t *inst = instance;
473 inst->chase_referrals = 2; /* use OpenLDAP defaults */
476 inst->xlat_name = cf_section_name2(conf);
477 if (!inst->xlat_name) {
478 inst->xlat_name = cf_section_name1(conf);
482 * If the configuration parameters can't be parsed, then fail.
484 if ((parse_sub_section(inst, conf, &inst->accounting, RLM_COMPONENT_ACCT) < 0) ||
485 (parse_sub_section(inst, conf, &inst->postauth, RLM_COMPONENT_POST_AUTH) < 0)) {
486 LDAP_ERR("Failed parsing configuration");
492 * Sanity checks for cacheable groups code.
494 if (inst->cacheable_group_name && inst->groupobj_membership_filter && !inst->groupobj_name_attr) {
495 LDAP_ERR("Directive 'group.name_attribute' must be set if cacheable group names are enabled");
501 * Copy across values from base_dn to the object specific base_dn.
503 if (!inst->groupobj_base_dn) {
504 if (!inst->base_dn) {
505 LDAP_ERR("Must set 'base_dn' if there is no 'group_base_dn'");
510 inst->groupobj_base_dn = inst->base_dn;
513 if (!inst->userobj_base_dn) {
514 if (!inst->base_dn) {
515 LDAP_ERR("Must set 'base_dn' if there is no 'userobj_base_dn'");
520 inst->userobj_base_dn = inst->base_dn;
524 * Check for URLs. If they're used and the library doesn't support them, then complain.
527 if (ldap_is_ldap_url(inst->server)) {
528 #ifdef HAVE_LDAP_INITIALIZE
532 LDAP_ERR("'server' directive is in URL form but ldap_initialize() is not available");
538 * Workaround for servers which support LDAPS but not START TLS
540 if (inst->port == LDAPS_PORT || inst->tls_mode) {
541 inst->tls_mode = LDAP_OPT_X_TLS_HARD;
546 #if LDAP_SET_REBIND_PROC_ARGS != 3
548 * The 2-argument rebind doesn't take an instance variable. Our rebind function needs the instance
549 * variable for the username, password, etc.
551 if (inst->rebind == true) {
552 LDAP_ERR("Cannot use 'rebind' directive as this version of libldap does not support the API "
560 * Convert scope strings to enumerated constants
562 inst->userobj_scope = fr_str2int(ldap_scope, inst->userobj_scope_str, -1);
563 if (inst->userobj_scope < 0) {
564 LDAP_ERR("Invalid 'user.scope' value \"%s\", expected 'sub', 'one' or 'base'",
565 inst->userobj_scope_str);
569 inst->groupobj_scope = fr_str2int(ldap_scope, inst->groupobj_scope_str, -1);
570 if (inst->groupobj_scope < 0) {
571 LDAP_ERR("Invalid 'group.scope' value \"%s\", expected 'sub', 'one' or 'base'",
572 inst->groupobj_scope_str);
576 if (inst->tls_require_cert_str) {
577 #ifdef LDAP_OPT_X_TLS_NEVER
579 * Convert cert strictness to enumerated constants
581 inst->tls_require_cert = fr_str2int(ldap_tls_require_cert, inst->tls_require_cert_str, -1);
582 if (inst->tls_require_cert < 0) {
583 LDAP_ERR("Invalid 'tls.require_cert' value \"%s\", expected 'never', 'demand', 'allow', "
584 "'try' or 'hard'", inst->tls_require_cert_str);
588 LDAP_DBGW("Modifying 'tls.require_cert' is not supported by current version of libldap.
589 "Please upgrade libldap and rebuild this module");
593 * Build the attribute map
595 if (rlm_ldap_map_verify(inst, &(inst->user_map)) < 0) {
600 * Group comparison checks.
602 inst->group_da = dict_attrbyvalue(PW_LDAP_GROUP, 0);
603 paircompare_register(PW_LDAP_GROUP, PW_USER_NAME, rlm_ldap_groupcmp, inst);
604 if (cf_section_name2(conf)) {
608 snprintf(buffer, sizeof(buffer), "%s-Ldap-Group",
610 memset(&flags, 0, sizeof(flags));
612 dict_addattr(buffer, -1, 0, PW_TYPE_STRING, flags);
613 inst->group_da = dict_attrbyname(buffer);
614 if (!inst->group_da) {
615 LDAP_ERR("Failed creating attribute %s", buffer);
621 paircompare_register(inst->group_da->attr, PW_USER_NAME, rlm_ldap_groupcmp, inst);
624 xlat_register(inst->xlat_name, ldap_xlat, rlm_ldap_escape_func, inst);
627 * Initialize the socket pool.
629 inst->pool = fr_connection_pool_init(inst->cs, inst, mod_conn_create, NULL, mod_conn_delete, NULL);
640 /** Check the user's password against ldap directory
642 * @param instance rlm_ldap configuration.
643 * @param request Current request.
644 * @return one of the RLM_MODULE_* values.
646 static rlm_rcode_t mod_authenticate(void *instance, REQUEST *request)
651 ldap_instance_t *inst = instance;
655 * Ensure that we're being passed a plain-text password, and not
659 if (!request->username) {
660 REDEBUG("Attribute \"User-Name\" is required for authentication");
662 return RLM_MODULE_INVALID;
665 if (!request->password ||
666 (request->password->da->attr != PW_USER_PASSWORD)) {
667 RWDEBUG("You have set \"Auth-Type := LDAP\" somewhere.");
668 RWDEBUG("*********************************************");
669 RWDEBUG("* THAT CONFIGURATION IS WRONG. DELETE IT. ");
670 RWDEBUG("* YOU ARE PREVENTING THE SERVER FROM WORKING.");
671 RWDEBUG("*********************************************");
673 REDEBUG("Attribute \"User-Password\" is required for authentication.");
675 return RLM_MODULE_INVALID;
678 if (request->password->length == 0) {
679 REDEBUG("Empty password supplied");
681 return RLM_MODULE_INVALID;
684 RDEBUG("Login attempt by \"%s\"", request->username->vp_strvalue);
686 conn = rlm_ldap_get_socket(inst, request);
687 if (!conn) return RLM_MODULE_FAIL;
690 * Get the DN by doing a search.
692 dn = rlm_ldap_find_user(inst, request, &conn, NULL, false, NULL, &rcode);
694 rlm_ldap_release_socket(inst, conn);
702 conn->rebound = true;
703 status = rlm_ldap_bind(inst, request, &conn, dn, request->password->vp_strvalue, true);
705 case LDAP_PROC_SUCCESS:
706 rcode = RLM_MODULE_OK;
707 RDEBUG("Bind as user \"%s\" was successful", dn);
710 case LDAP_PROC_NOT_PERMITTED:
711 rcode = RLM_MODULE_USERLOCK;
714 case LDAP_PROC_REJECT:
715 rcode = RLM_MODULE_REJECT;
718 case LDAP_PROC_BAD_DN:
719 rcode = RLM_MODULE_INVALID;
722 case LDAP_PROC_NO_RESULT:
723 rcode = RLM_MODULE_NOTFOUND;
727 rcode = RLM_MODULE_FAIL;
731 rlm_ldap_release_socket(inst, conn);
736 /** Check if user is authorized for remote access
739 static rlm_rcode_t mod_authorize(void *instance, REQUEST *request)
741 rlm_rcode_t rcode = RLM_MODULE_OK;
745 ldap_instance_t *inst = instance;
749 LDAPMessage *result, *entry;
750 char const *dn = NULL;
751 rlm_ldap_map_xlat_t expanded; /* faster that mallocing every time */
753 if (!request->username) {
754 RDEBUG2("Attribute \"User-Name\" is required for authorization.");
756 return RLM_MODULE_NOOP;
760 * Check for valid input, zero length names not permitted
762 if (request->username->length == 0) {
763 RDEBUG2("Zero length username not permitted");
765 return RLM_MODULE_INVALID;
768 if (rlm_ldap_map_xlat(request, inst->user_map, &expanded) < 0) {
769 return RLM_MODULE_FAIL;
772 conn = rlm_ldap_get_socket(inst, request);
773 if (!conn) return RLM_MODULE_FAIL;
776 * Add any additional attributes we need for checking access, memberships, and profiles
778 if (inst->userobj_access_attr) {
779 expanded.attrs[expanded.count++] = inst->userobj_access_attr;
782 if (inst->userobj_membership_attr && (inst->cacheable_group_dn || inst->cacheable_group_name)) {
783 expanded.attrs[expanded.count++] = inst->userobj_membership_attr;
786 if (inst->profile_attr) {
787 expanded.attrs[expanded.count++] = inst->profile_attr;
790 if (inst->valuepair_attr) {
791 expanded.attrs[expanded.count++] = inst->valuepair_attr;
794 expanded.attrs[expanded.count] = NULL;
796 dn = rlm_ldap_find_user(inst, request, &conn, expanded.attrs, true, &result, &rcode);
801 entry = ldap_first_entry(conn->handle, result);
803 ldap_get_option(conn->handle, LDAP_OPT_RESULT_CODE, &ldap_errno);
804 REDEBUG("Failed retrieving entry: %s", ldap_err2string(ldap_errno));
812 if (inst->userobj_access_attr) {
813 rcode = rlm_ldap_check_access(inst, request, conn, entry);
814 if (rcode != RLM_MODULE_OK) {
820 * Check if we need to cache group memberships
822 if (inst->cacheable_group_dn || inst->cacheable_group_name) {
823 rcode = rlm_ldap_cacheable_userobj(inst, request, &conn, entry);
824 if (rcode != RLM_MODULE_OK) {
828 rcode = rlm_ldap_cacheable_groupobj(inst, request, &conn);
829 if (rcode != RLM_MODULE_OK) {
836 * We already have a Cleartext-Password. Skip edir.
838 if (pairfind(request->config_items, PW_CLEARTEXT_PASSWORD, 0, TAG_ANY)) {
843 * Retrieve Universal Password if we use eDirectory
848 size_t pass_size = sizeof(password);
851 * Retrive universal password
853 res = nmasldap_get_password(conn->handle, dn, password, &pass_size);
855 RWDEBUG("Failed to retrieve eDirectory password");
856 rcode = RLM_MODULE_NOOP;
862 * Add Cleartext-Password attribute to the request
864 vp = radius_paircreate(request, &request->config_items, PW_CLEARTEXT_PASSWORD, 0);
865 pairstrcpy(vp, password);
866 vp->length = pass_size;
868 RDEBUG2("Added eDirectory password in check items as %s = %s", vp->da->name, vp->vp_strvalue);
870 if (inst->edir_autz) {
871 RDEBUG2("Binding as user for eDirectory authorization checks");
875 conn->rebound = true;
876 status = rlm_ldap_bind(inst, request, &conn, dn, vp->vp_strvalue, true);
878 case LDAP_PROC_SUCCESS:
879 rcode = RLM_MODULE_OK;
880 RDEBUG("Bind as user \"%s\" was successful", dn);
883 case LDAP_PROC_NOT_PERMITTED:
884 rcode = RLM_MODULE_USERLOCK;
887 case LDAP_PROC_REJECT:
888 rcode = RLM_MODULE_REJECT;
891 case LDAP_PROC_BAD_DN:
892 rcode = RLM_MODULE_INVALID;
895 case LDAP_PROC_NO_RESULT:
896 rcode = RLM_MODULE_NOTFOUND;
900 rcode = RLM_MODULE_FAIL;
911 * Apply ONE user profile, or a default user profile.
913 vp = pairfind(request->config_items, PW_USER_PROFILE, 0, TAG_ANY);
914 if (vp || inst->default_profile) {
915 char const *profile = inst->default_profile;
917 if (vp) profile = vp->vp_strvalue;
919 rlm_ldap_map_profile(inst, request, &conn, profile, &expanded);
923 * Apply a SET of user profiles.
925 if (inst->profile_attr) {
926 vals = ldap_get_values(conn->handle, entry, inst->profile_attr);
928 for (i = 0; vals[i] != NULL; i++) {
929 rlm_ldap_map_profile(inst, request, &conn, vals[i], &expanded);
932 ldap_value_free(vals);
936 if (inst->user_map) {
937 rlm_ldap_map_do(inst, request, conn->handle, &expanded, entry);
938 rlm_ldap_check_reply(inst, request);
942 rlm_ldap_map_xlat_free(&expanded);
944 ldap_msgfree(result);
946 rlm_ldap_release_socket(inst, conn);
951 /** Modify user's object in LDAP
953 * Process a modifcation map to update a user object in the LDAP directory.
955 * @param inst rlm_ldap instance.
956 * @param request Current request.
957 * @param section that holds the map to process.
958 * @return one of the RLM_MODULE_* values.
960 static rlm_rcode_t user_modify(ldap_instance_t *inst, REQUEST *request, ldap_acct_section_t *section)
962 rlm_rcode_t rcode = RLM_MODULE_OK;
964 ldap_handle_t *conn = NULL;
966 LDAPMod *mod_p[LDAP_MAX_ATTRMAP + 1], mod_s[LDAP_MAX_ATTRMAP];
967 LDAPMod **modify = mod_p;
969 char *passed[LDAP_MAX_ATTRMAP * 2];
970 int i, total = 0, last_pass = 0;
972 char *expanded[LDAP_MAX_ATTRMAP];
980 * Build our set of modifications using the update sections in
987 char path[MAX_STRING_LEN];
994 * Locate the update section were going to be using
996 if (section->reference[0] != '.') {
1000 if (radius_xlat(p, (sizeof(path) - (p - path)) - 1, request, section->reference, NULL, NULL) < 0) {
1004 ci = cf_reference_item(NULL, section->cs, path);
1009 if (!cf_item_is_section(ci)){
1010 REDEBUG("Reference must resolve to a section");
1015 cs = cf_section_sub_find(cf_itemtosection(ci), "update");
1017 REDEBUG("Section must contain 'update' subsection");
1023 * Iterate over all the pairs, building our mods array
1025 for (ci = cf_item_find_next(cs, NULL); ci != NULL; ci = cf_item_find_next(cs, ci)) {
1026 int do_xlat = false;
1028 if (total == LDAP_MAX_ATTRMAP) {
1029 REDEBUG("Modify map size exceeded");
1034 if (!cf_item_is_pair(ci)) {
1035 REDEBUG("Entry is not in \"ldap-attribute = value\" format");
1041 * Retrieve all the information we need about the pair
1043 cp = cf_itemtopair(ci);
1044 value = cf_pair_value(cp);
1045 attr = cf_pair_attr(cp);
1046 op = cf_pair_operator(cp);
1048 if (!value || (*value == '\0')) {
1049 RDEBUG("Empty value string, skipping attribute \"%s\"", attr);
1054 switch (cf_pair_value_type(cp))
1057 case T_SINGLE_QUOTED_STRING:
1059 case T_BACK_QUOTED_STRING:
1060 case T_DOUBLE_QUOTED_STRING:
1068 if (op == T_OP_CMP_FALSE) {
1069 passed[last_pass] = NULL;
1070 } else if (do_xlat) {
1073 if (radius_xlat(exp, 0, request, value, NULL, NULL) <= 0) {
1074 RDEBUG("Skipping attribute \"%s\"", attr);
1081 expanded[last_exp++] = exp;
1082 passed[last_pass] = exp;
1087 memcpy(&(passed[last_pass]), &value, sizeof(passed[last_pass]));
1090 passed[last_pass + 1] = NULL;
1092 mod_s[total].mod_values = &(passed[last_pass]);
1099 * T_OP_EQ is *NOT* supported, it is impossible to
1100 * support because of the lack of transactions in LDAP
1103 mod_s[total].mod_op = LDAP_MOD_ADD;
1107 mod_s[total].mod_op = LDAP_MOD_REPLACE;
1111 case T_OP_CMP_FALSE:
1112 mod_s[total].mod_op = LDAP_MOD_DELETE;
1115 #ifdef LDAP_MOD_INCREMENT
1117 mod_s[total].mod_op = LDAP_MOD_INCREMENT;
1121 REDEBUG("Operator '%s' is not supported for LDAP modify operations",
1122 fr_int2str(fr_tokens, op, "¿unknown?"));
1128 * Now we know the value is ok, copy the pointers into
1129 * the ldapmod struct.
1131 memcpy(&(mod_s[total].mod_type), &(attr), sizeof(mod_s[total].mod_type));
1133 mod_p[total] = &(mod_s[total]);
1138 rcode = RLM_MODULE_NOOP;
1142 mod_p[total] = NULL;
1144 conn = rlm_ldap_get_socket(inst, request);
1145 if (!conn) return RLM_MODULE_FAIL;
1148 dn = rlm_ldap_find_user(inst, request, &conn, NULL, false, NULL, &rcode);
1149 if (!dn || (rcode != RLM_MODULE_OK)) {
1153 rcode = rlm_ldap_modify(inst, request, &conn, dn, modify);
1158 * Free up any buffers we allocated for xlat expansion
1160 for (i = 0; i < last_exp; i++) {
1161 talloc_free(expanded[i]);
1164 rlm_ldap_release_socket(inst, conn);
1169 static rlm_rcode_t mod_accounting(void *instance, REQUEST * request) {
1170 ldap_instance_t *inst = instance;
1172 if (inst->accounting) {
1173 return user_modify(inst, request, inst->accounting);
1176 return RLM_MODULE_NOOP;
1179 static rlm_rcode_t mod_post_auth(void *instance, REQUEST * request)
1181 ldap_instance_t *inst = instance;
1183 if (inst->postauth) {
1184 return user_modify(inst, request, inst->postauth);
1187 return RLM_MODULE_NOOP;
1191 /* globally exported name */
1192 module_t rlm_ldap = {
1195 RLM_TYPE_THREAD_SAFE, /* type: reserved */
1196 sizeof(ldap_instance_t),
1198 mod_instantiate, /* instantiation */
1199 mod_detach, /* detach */
1201 mod_authenticate, /* authentication */
1202 mod_authorize, /* authorization */
1203 NULL, /* preaccounting */
1204 mod_accounting, /* accounting */
1205 NULL, /* checksimul */
1206 NULL, /* pre-proxy */
1207 NULL, /* post-proxy */
1208 mod_post_auth /* post-auth */