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 {"filter", PW_TYPE_STRING_PTR, offsetof(ldap_instance_t, profile_filter), NULL, "(&)"}, //!< Correct filter for
81 {"attribute", PW_TYPE_STRING_PTR, offsetof(ldap_instance_t, profile_attr), NULL, NULL},
82 {"default", PW_TYPE_STRING_PTR, offsetof(ldap_instance_t, default_profile), NULL, NULL},
84 { NULL, -1, 0, NULL, NULL }
90 static CONF_PARSER user_config[] = {
91 {"filter", PW_TYPE_STRING_PTR, offsetof(ldap_instance_t, userobj_filter), NULL, "(uid=%u)"},
92 {"scope", PW_TYPE_STRING_PTR, offsetof(ldap_instance_t, userobj_scope_str), NULL, "sub"},
93 {"base_dn", PW_TYPE_STRING_PTR | PW_TYPE_REQUIRED, offsetof(ldap_instance_t,userobj_base_dn), NULL, NULL},
95 {"access_attribute", PW_TYPE_STRING_PTR, offsetof(ldap_instance_t, userobj_access_attr), NULL, NULL},
96 {"access_positive", PW_TYPE_BOOLEAN, offsetof(ldap_instance_t, access_positive), NULL, "yes"},
98 { NULL, -1, 0, NULL, NULL }
102 * Group configuration
104 static CONF_PARSER group_config[] = {
105 {"filter", PW_TYPE_STRING_PTR, offsetof(ldap_instance_t, groupobj_filter), NULL, NULL},
106 {"scope", PW_TYPE_STRING_PTR, offsetof(ldap_instance_t, groupobj_scope_str), NULL, "sub"},
107 {"base_dn", PW_TYPE_STRING_PTR, offsetof(ldap_instance_t, groupobj_base_dn), NULL, NULL},
109 {"name_attribute", PW_TYPE_STRING_PTR, offsetof(ldap_instance_t, groupobj_name_attr), NULL, "cn"},
110 {"membership_attribute", PW_TYPE_STRING_PTR, offsetof(ldap_instance_t, userobj_membership_attr), NULL, NULL},
111 {"membership_filter", PW_TYPE_STRING_PTR, offsetof(ldap_instance_t, groupobj_membership_filter), NULL, NULL},
112 {"cacheable_name", PW_TYPE_BOOLEAN, offsetof(ldap_instance_t, cacheable_group_name), NULL, "no"},
113 {"cacheable_dn", PW_TYPE_BOOLEAN, offsetof(ldap_instance_t, cacheable_group_dn), NULL, "no"},
115 { NULL, -1, 0, NULL, NULL }
119 * Client configuration
121 static CONF_PARSER client_attributes[] = {
122 {"identifier", PW_TYPE_STRING_PTR, offsetof(ldap_instance_t, clientobj_identifier), NULL, "host"},
123 {"shortname", PW_TYPE_STRING_PTR, offsetof(ldap_instance_t, clientobj_shortname), NULL, "cn"},
124 {"nas_type", PW_TYPE_STRING_PTR, offsetof(ldap_instance_t, clientobj_type), NULL, NULL},
125 {"secret", PW_TYPE_STRING_PTR, offsetof(ldap_instance_t, clientobj_secret), NULL, NULL},
126 {"virtual_server", PW_TYPE_STRING_PTR, offsetof(ldap_instance_t, clientobj_server), NULL, NULL},
127 {"require_message_authenticator", PW_TYPE_STRING_PTR, offsetof(ldap_instance_t, clientobj_require_ma),
130 { NULL, -1, 0, NULL, NULL }
133 static CONF_PARSER client_config[] = {
134 {"filter", PW_TYPE_STRING_PTR, offsetof(ldap_instance_t, clientobj_filter), NULL, NULL},
135 {"scope", PW_TYPE_STRING_PTR, offsetof(ldap_instance_t, clientobj_scope_str), NULL, "sub"},
136 {"base_dn", PW_TYPE_STRING_PTR, offsetof(ldap_instance_t, clientobj_base_dn), NULL, NULL},
137 {"attributes", PW_TYPE_SUBSECTION, 0, NULL, (void const *) client_attributes},
139 { NULL, -1, 0, NULL, NULL }
143 * Reference for accounting updates
145 static const CONF_PARSER acct_section_config[] = {
146 {"reference", PW_TYPE_STRING_PTR, offsetof(ldap_acct_section_t, reference), NULL, "."},
148 {NULL, -1, 0, NULL, NULL}
152 * Various options that don't belong in the main configuration.
154 * Note that these overlap a bit with the connection pool code!
156 static CONF_PARSER option_config[] = {
158 * Debugging flags to the server
160 {"ldap_debug", PW_TYPE_INTEGER, offsetof(ldap_instance_t,ldap_debug), NULL, "0x0000"},
162 {"chase_referrals", PW_TYPE_BOOLEAN, offsetof(ldap_instance_t,chase_referrals), NULL, NULL},
164 {"rebind", PW_TYPE_BOOLEAN,offsetof(ldap_instance_t,rebind), NULL, NULL},
166 /* timeout on network activity */
167 {"net_timeout", PW_TYPE_INTEGER, offsetof(ldap_instance_t,net_timeout), NULL, "10"},
169 /* timeout for search results */
170 {"res_timeout", PW_TYPE_INTEGER, offsetof(ldap_instance_t,res_timeout), NULL, "20"},
172 /* allow server unlimited time for search (server-side limit) */
173 {"srv_timelimit", PW_TYPE_INTEGER, offsetof(ldap_instance_t,srv_timelimit), NULL, "20"},
175 #ifdef LDAP_OPT_X_KEEPALIVE_IDLE
176 {"idle", PW_TYPE_INTEGER, offsetof(ldap_instance_t,keepalive_idle), NULL, "60"},
178 #ifdef LDAP_OPT_X_KEEPALIVE_PROBES
179 {"probes", PW_TYPE_INTEGER, offsetof(ldap_instance_t,keepalive_probes), NULL, "3"},
181 #ifdef LDAP_OPT_X_KEEPALIVE_INTERVAL
182 {"interval", PW_TYPE_INTEGER, offsetof(ldap_instance_t,keepalive_interval), NULL, "30"},
185 { NULL, -1, 0, NULL, NULL }
189 static const CONF_PARSER module_config[] = {
190 {"server", PW_TYPE_STRING_PTR | PW_TYPE_REQUIRED, offsetof(ldap_instance_t,server), NULL, "localhost"},
191 {"port", PW_TYPE_INTEGER, offsetof(ldap_instance_t,port), NULL, "389"},
193 {"password", PW_TYPE_STRING_PTR, offsetof(ldap_instance_t,password), NULL, ""},
194 {"identity", PW_TYPE_STRING_PTR, offsetof(ldap_instance_t,admin_dn), NULL, ""},
196 {"valuepair_attr", PW_TYPE_STRING_PTR, offsetof(ldap_instance_t, valuepair_attr), NULL, NULL},
199 /* support for eDirectory Universal Password */
200 {"edir", PW_TYPE_BOOLEAN, offsetof(ldap_instance_t,edir), NULL, NULL}, /* NULL defaults to "no" */
203 * Attempt to bind with the Cleartext password we got from eDirectory
204 * Universal password for additional authorization checks.
206 {"edir_autz", PW_TYPE_BOOLEAN, offsetof(ldap_instance_t,edir_autz), NULL, NULL}, /* NULL defaults to "no" */
209 {"read_clients", PW_TYPE_BOOLEAN, offsetof(ldap_instance_t,do_clients), NULL, NULL}, /* NULL defaults to "no" */
211 { "user", PW_TYPE_SUBSECTION, 0, NULL, (void const *) user_config },
213 { "group", PW_TYPE_SUBSECTION, 0, NULL, (void const *) group_config },
215 { "client", PW_TYPE_SUBSECTION, 0, NULL, (void const *) client_config },
217 { "profile", PW_TYPE_SUBSECTION, 0, NULL, (void const *) profile_config },
219 { "options", PW_TYPE_SUBSECTION, 0, NULL, (void const *) option_config },
221 { "tls", PW_TYPE_SUBSECTION, 0, NULL, (void const *) tls_config },
223 {NULL, -1, 0, NULL, NULL}
226 /** Expand an LDAP URL into a query, and return a string result from that query.
229 static size_t ldap_xlat(void *instance, REQUEST *request, char const *fmt,
230 char *out, size_t freespace)
234 ldap_instance_t *inst = instance;
235 LDAPURLDesc *ldap_url;
236 LDAPMessage *result = NULL;
237 LDAPMessage *entry = NULL;
246 if (!ldap_is_ldap_url(url)) {
247 REDEBUG("String passed does not look like an LDAP URL");
251 if (ldap_url_parse(url, &ldap_url)){
252 REDEBUG("Parsing LDAP URL failed");
257 * Nothing, empty string, "*" string, or got 2 things, die.
259 if (!ldap_url->lud_attrs || !ldap_url->lud_attrs[0] ||
260 !*ldap_url->lud_attrs[0] ||
261 (strcmp(ldap_url->lud_attrs[0], "*") == 0) ||
262 ldap_url->lud_attrs[1]) {
263 REDEBUG("Bad attributes list in LDAP URL. URL must specify exactly one attribute to retrieve");
268 if (ldap_url->lud_host &&
269 ((strncmp(inst->server, ldap_url->lud_host, strlen(inst->server)) != 0) ||
270 (ldap_url->lud_port != inst->port))) {
271 RDEBUG("Requested server/port is \"%s:%i\"", ldap_url->lud_host, inst->port);
276 conn = rlm_ldap_get_socket(inst, request);
277 if (!conn) goto free_urldesc;
279 memcpy(&attrs, &ldap_url->lud_attrs, sizeof(attrs));
281 status = rlm_ldap_search(inst, request, &conn, ldap_url->lud_dn, ldap_url->lud_scope, ldap_url->lud_filter,
284 case LDAP_PROC_SUCCESS:
286 case LDAP_PROC_NO_RESULT:
287 RDEBUG("Search returned not found");
295 entry = ldap_first_entry(conn->handle, result);
297 ldap_get_option(conn->handle, LDAP_OPT_RESULT_CODE, &ldap_errno);
298 REDEBUG("Failed retrieving entry: %s", ldap_err2string(ldap_errno));
302 vals = ldap_get_values(conn->handle, entry, ldap_url->lud_attrs[0]);
304 RDEBUG("No \"%s\" attributes found in specified object", ldap_url->lud_attrs[0]);
308 length = strlen(vals[0]);
309 if (length >= freespace){
314 strlcpy(out, vals[0], freespace);
317 ldap_value_free(vals);
319 ldap_msgfree(result);
321 rlm_ldap_release_socket(inst, conn);
323 ldap_free_urldesc(ldap_url);
328 /** Perform LDAP-Group comparison checking
330 * Attempts to match users to groups using a variety of methods.
332 * @param instance of the rlm_ldap module.
333 * @param request Current request.
334 * @param thing Unknown.
335 * @param check Which group to check for user membership.
336 * @param check_pairs Unknown.
337 * @param reply_pairs Unknown.
338 * @return 1 on failure (or if the user is not a member), else 0.
340 static int rlm_ldap_groupcmp(void *instance, REQUEST *request, UNUSED VALUE_PAIR *thing, VALUE_PAIR *check,
341 UNUSED VALUE_PAIR *check_pairs, UNUSED VALUE_PAIR **reply_pairs)
343 ldap_instance_t *inst = instance;
349 ldap_handle_t *conn = NULL;
352 if (!inst->groupobj_base_dn) {
353 REDEBUG("Directive 'group.base_dn' must be set'");
358 RDEBUG("Searching for user in group \"%s\"", check->vp_strvalue);
360 if (check->length == 0) {
361 RDEBUG("Cannot do comparison (group name is empty)");
366 * Check if we can do cached membership verification
368 check_is_dn = rlm_ldap_is_dn(check->vp_strvalue);
369 if ((check_is_dn && inst->cacheable_group_dn) || (!check_is_dn && inst->cacheable_group_name)) {
370 switch(rlm_ldap_check_cached(inst, request, check)) {
371 case RLM_MODULE_NOTFOUND:
380 conn = rlm_ldap_get_socket(inst, request);
384 * This is used in the default membership filter.
386 user_dn = rlm_ldap_find_user(inst, request, &conn, NULL, false, NULL, &rcode);
388 rlm_ldap_release_socket(inst, conn);
395 * Check groupobj user membership
397 if (inst->groupobj_membership_filter) {
398 switch(rlm_ldap_check_groupobj_dynamic(inst, request, &conn, check)) {
399 case RLM_MODULE_NOTFOUND:
411 * Check userobj group membership
413 if (inst->userobj_membership_attr) {
414 switch(rlm_ldap_check_userobj_dynamic(inst, request, &conn, user_dn, check)) {
415 case RLM_MODULE_NOTFOUND:
428 rlm_ldap_release_socket(inst, conn);
432 RDEBUG("User is not a member of specified group");
440 /** Detach from the LDAP server and cleanup internal state.
443 static int mod_detach(void *instance)
445 ldap_instance_t *inst = instance;
447 fr_connection_pool_delete(inst->pool);
449 if (inst->user_map) {
450 talloc_free(inst->user_map);
456 /** Parse an accounting sub section.
458 * Allocate a new ldap_acct_section_t and write the config data into it.
460 * @param[in] inst rlm_ldap configuration.
461 * @param[in] parent of the config section.
462 * @param[out] config to write the sub section parameters to.
463 * @param[in] comp The section name were parsing the config for.
464 * @return 0 on success, else < 0 on failure.
466 static int parse_sub_section(ldap_instance_t *inst, CONF_SECTION *parent, ldap_acct_section_t **config,
467 rlm_components_t comp)
471 char const *name = section_type_value[comp].section;
473 cs = cf_section_sub_find(parent, name);
475 INFO("rlm_ldap (%s): Couldn't find configuration for %s, will return NOOP for calls "
476 "from this section", inst->xlat_name, name);
481 *config = talloc_zero(inst, ldap_acct_section_t);
482 if (cf_section_parse(cs, *config, acct_section_config) < 0) {
483 LDAP_ERR("Failed parsing configuration for section %s", name);
493 /** Instantiate the module
495 * Creates a new instance of the module reading parameters from a configuration section.
497 * @param conf to parse.
498 * @param instance Where to write pointer to configuration data.
499 * @return 0 on success < 0 on failure.
501 static int mod_instantiate(CONF_SECTION *conf, void *instance)
503 ldap_instance_t *inst = instance;
507 inst->chase_referrals = 2; /* use OpenLDAP defaults */
510 inst->xlat_name = cf_section_name2(conf);
511 if (!inst->xlat_name) {
512 inst->xlat_name = cf_section_name1(conf);
516 * If the configuration parameters can't be parsed, then fail.
518 if ((parse_sub_section(inst, conf, &inst->accounting, RLM_COMPONENT_ACCT) < 0) ||
519 (parse_sub_section(inst, conf, &inst->postauth, RLM_COMPONENT_POST_AUTH) < 0)) {
520 LDAP_ERR("Failed parsing configuration");
526 * Sanity checks for cacheable groups code.
528 if (inst->cacheable_group_name && inst->groupobj_membership_filter) {
529 if (!inst->groupobj_name_attr) {
530 LDAP_ERR("Directive 'group.name_attribute' must be set if cacheable group names are enabled");
535 if (!inst->groupobj_base_dn) {
536 LDAP_ERR("Directive 'group.base_dn' must be set if cacheable group names are enabled");
543 * Check for URLs. If they're used and the library doesn't support them, then complain.
546 if (ldap_is_ldap_url(inst->server)) {
547 #ifdef HAVE_LDAP_INITIALIZE
551 LDAP_ERR("Directive 'server' is in URL form but ldap_initialize() is not available");
557 * Workaround for servers which support LDAPS but not START TLS
559 if (inst->port == LDAPS_PORT || inst->tls_mode) {
560 inst->tls_mode = LDAP_OPT_X_TLS_HARD;
565 #if LDAP_SET_REBIND_PROC_ARGS != 3
567 * The 2-argument rebind doesn't take an instance variable. Our rebind function needs the instance
568 * variable for the username, password, etc.
570 if (inst->rebind == true) {
571 LDAP_ERR("Cannot use 'rebind' directive as this version of libldap does not support the API "
579 * Convert scope strings to enumerated constants
581 inst->userobj_scope = fr_str2int(ldap_scope, inst->userobj_scope_str, -1);
582 if (inst->userobj_scope < 0) {
583 LDAP_ERR("Invalid 'user.scope' value \"%s\", expected 'sub', 'one' or 'base'",
584 inst->userobj_scope_str);
588 inst->groupobj_scope = fr_str2int(ldap_scope, inst->groupobj_scope_str, -1);
589 if (inst->groupobj_scope < 0) {
590 LDAP_ERR("Invalid 'group.scope' value \"%s\", expected 'sub', 'one' or 'base'",
591 inst->groupobj_scope_str);
595 inst->clientobj_scope = fr_str2int(ldap_scope, inst->clientobj_scope_str, -1);
596 if (inst->clientobj_scope < 0) {
597 LDAP_ERR("Invalid 'client.scope' value \"%s\", expected 'sub', 'one' or 'base'",
598 inst->clientobj_scope_str);
602 if (inst->tls_require_cert_str) {
603 #ifdef LDAP_OPT_X_TLS_NEVER
605 * Convert cert strictness to enumerated constants
607 inst->tls_require_cert = fr_str2int(ldap_tls_require_cert, inst->tls_require_cert_str, -1);
608 if (inst->tls_require_cert < 0) {
609 LDAP_ERR("Invalid 'tls.require_cert' value \"%s\", expected 'never', 'demand', 'allow', "
610 "'try' or 'hard'", inst->tls_require_cert_str);
614 LDAP_ERR("Modifying 'tls.require_cert' is not supported by current version of libldap. "
615 "Please upgrade libldap and rebuild this module");
621 * Build the attribute map
623 if (rlm_ldap_map_verify(inst, &(inst->user_map)) < 0) {
628 * Group comparison checks.
630 inst->group_da = dict_attrbyvalue(PW_LDAP_GROUP, 0);
631 paircompare_register(PW_LDAP_GROUP, PW_USER_NAME, rlm_ldap_groupcmp, inst);
632 if (cf_section_name2(conf)) {
636 snprintf(buffer, sizeof(buffer), "%s-Ldap-Group",
638 memset(&flags, 0, sizeof(flags));
640 dict_addattr(buffer, -1, 0, PW_TYPE_STRING, flags);
641 inst->group_da = dict_attrbyname(buffer);
642 if (!inst->group_da) {
643 LDAP_ERR("Failed creating attribute %s", buffer);
648 paircompare_register(inst->group_da->attr, PW_USER_NAME, rlm_ldap_groupcmp, inst);
651 xlat_register(inst->xlat_name, ldap_xlat, rlm_ldap_escape_func, inst);
654 * Initialize the socket pool.
656 inst->pool = fr_connection_pool_init(inst->cs, inst, mod_conn_create, NULL, mod_conn_delete, NULL);
662 * Bulk load dynamic clients.
664 if (inst->do_clients) {
665 if (rlm_ldap_load_clients(inst) < 0) {
666 LDAP_ERR("Error loading clients");
678 /** Check the user's password against ldap directory
680 * @param instance rlm_ldap configuration.
681 * @param request Current request.
682 * @return one of the RLM_MODULE_* values.
684 static rlm_rcode_t mod_authenticate(void *instance, REQUEST *request)
689 ldap_instance_t *inst = instance;
693 * Ensure that we're being passed a plain-text password, and not
697 if (!request->username) {
698 REDEBUG("Attribute \"User-Name\" is required for authentication");
700 return RLM_MODULE_INVALID;
703 if (!request->password ||
704 (request->password->da->attr != PW_USER_PASSWORD)) {
705 RWDEBUG("You have set \"Auth-Type := LDAP\" somewhere.");
706 RWDEBUG("*********************************************");
707 RWDEBUG("* THAT CONFIGURATION IS WRONG. DELETE IT. ");
708 RWDEBUG("* YOU ARE PREVENTING THE SERVER FROM WORKING.");
709 RWDEBUG("*********************************************");
711 REDEBUG("Attribute \"User-Password\" is required for authentication.");
713 return RLM_MODULE_INVALID;
716 if (request->password->length == 0) {
717 REDEBUG("Empty password supplied");
719 return RLM_MODULE_INVALID;
722 RDEBUG("Login attempt by \"%s\"", request->username->vp_strvalue);
724 conn = rlm_ldap_get_socket(inst, request);
725 if (!conn) return RLM_MODULE_FAIL;
728 * Get the DN by doing a search.
730 dn = rlm_ldap_find_user(inst, request, &conn, NULL, false, NULL, &rcode);
732 rlm_ldap_release_socket(inst, conn);
740 conn->rebound = true;
741 status = rlm_ldap_bind(inst, request, &conn, dn, request->password->vp_strvalue, true);
743 case LDAP_PROC_SUCCESS:
744 rcode = RLM_MODULE_OK;
745 RDEBUG("Bind as user \"%s\" was successful", dn);
748 case LDAP_PROC_NOT_PERMITTED:
749 rcode = RLM_MODULE_USERLOCK;
752 case LDAP_PROC_REJECT:
753 rcode = RLM_MODULE_REJECT;
756 case LDAP_PROC_BAD_DN:
757 rcode = RLM_MODULE_INVALID;
760 case LDAP_PROC_NO_RESULT:
761 rcode = RLM_MODULE_NOTFOUND;
765 rcode = RLM_MODULE_FAIL;
769 rlm_ldap_release_socket(inst, conn);
774 /** Check if user is authorized for remote access
777 static rlm_rcode_t mod_authorize(void *instance, REQUEST *request)
779 rlm_rcode_t rcode = RLM_MODULE_OK;
783 ldap_instance_t *inst = instance;
787 LDAPMessage *result, *entry;
788 char const *dn = NULL;
789 rlm_ldap_map_xlat_t expanded; /* faster that mallocing every time */
791 if (!request->username) {
792 RDEBUG2("Attribute \"User-Name\" is required for authorization.");
794 return RLM_MODULE_NOOP;
798 * Check for valid input, zero length names not permitted
800 if (request->username->length == 0) {
801 RDEBUG2("Zero length username not permitted");
803 return RLM_MODULE_INVALID;
806 if (rlm_ldap_map_xlat(request, inst->user_map, &expanded) < 0) {
807 return RLM_MODULE_FAIL;
810 conn = rlm_ldap_get_socket(inst, request);
811 if (!conn) return RLM_MODULE_FAIL;
814 * Add any additional attributes we need for checking access, memberships, and profiles
816 if (inst->userobj_access_attr) {
817 expanded.attrs[expanded.count++] = inst->userobj_access_attr;
820 if (inst->userobj_membership_attr && (inst->cacheable_group_dn || inst->cacheable_group_name)) {
821 expanded.attrs[expanded.count++] = inst->userobj_membership_attr;
824 if (inst->profile_attr) {
825 expanded.attrs[expanded.count++] = inst->profile_attr;
828 if (inst->valuepair_attr) {
829 expanded.attrs[expanded.count++] = inst->valuepair_attr;
832 expanded.attrs[expanded.count] = NULL;
834 dn = rlm_ldap_find_user(inst, request, &conn, expanded.attrs, true, &result, &rcode);
839 entry = ldap_first_entry(conn->handle, result);
841 ldap_get_option(conn->handle, LDAP_OPT_RESULT_CODE, &ldap_errno);
842 REDEBUG("Failed retrieving entry: %s", ldap_err2string(ldap_errno));
850 if (inst->userobj_access_attr) {
851 rcode = rlm_ldap_check_access(inst, request, conn, entry);
852 if (rcode != RLM_MODULE_OK) {
858 * Check if we need to cache group memberships
860 if (inst->cacheable_group_dn || inst->cacheable_group_name) {
861 rcode = rlm_ldap_cacheable_userobj(inst, request, &conn, entry);
862 if (rcode != RLM_MODULE_OK) {
866 rcode = rlm_ldap_cacheable_groupobj(inst, request, &conn);
867 if (rcode != RLM_MODULE_OK) {
874 * We already have a Cleartext-Password. Skip edir.
876 if (pairfind(request->config_items, PW_CLEARTEXT_PASSWORD, 0, TAG_ANY)) {
881 * Retrieve Universal Password if we use eDirectory
886 size_t pass_size = sizeof(password);
889 * Retrive universal password
891 res = nmasldap_get_password(conn->handle, dn, password, &pass_size);
893 RWDEBUG("Failed to retrieve eDirectory password");
894 rcode = RLM_MODULE_NOOP;
900 * Add Cleartext-Password attribute to the request
902 vp = radius_paircreate(request, &request->config_items, PW_CLEARTEXT_PASSWORD, 0);
903 pairstrcpy(vp, password);
904 vp->length = pass_size;
906 RDEBUG2("Added eDirectory password in check items as %s = %s", vp->da->name, vp->vp_strvalue);
908 if (inst->edir_autz) {
909 RDEBUG2("Binding as user for eDirectory authorization checks");
913 conn->rebound = true;
914 status = rlm_ldap_bind(inst, request, &conn, dn, vp->vp_strvalue, true);
916 case LDAP_PROC_SUCCESS:
917 rcode = RLM_MODULE_OK;
918 RDEBUG("Bind as user \"%s\" was successful", dn);
921 case LDAP_PROC_NOT_PERMITTED:
922 rcode = RLM_MODULE_USERLOCK;
925 case LDAP_PROC_REJECT:
926 rcode = RLM_MODULE_REJECT;
929 case LDAP_PROC_BAD_DN:
930 rcode = RLM_MODULE_INVALID;
933 case LDAP_PROC_NO_RESULT:
934 rcode = RLM_MODULE_NOTFOUND;
938 rcode = RLM_MODULE_FAIL;
949 * Apply ONE user profile, or a default user profile.
951 vp = pairfind(request->config_items, PW_USER_PROFILE, 0, TAG_ANY);
952 if (vp || inst->default_profile) {
953 char const *profile = vp ? vp->vp_strvalue : inst->default_profile;
955 rlm_ldap_map_profile(inst, request, &conn, profile, &expanded);
959 * Apply a SET of user profiles.
961 if (inst->profile_attr) {
962 vals = ldap_get_values(conn->handle, entry, inst->profile_attr);
964 for (i = 0; vals[i] != NULL; i++) {
965 rlm_ldap_map_profile(inst, request, &conn, vals[i], &expanded);
968 ldap_value_free(vals);
972 if (inst->user_map) {
973 rlm_ldap_map_do(inst, request, conn->handle, &expanded, entry);
974 rlm_ldap_check_reply(inst, request);
978 rlm_ldap_map_xlat_free(&expanded);
980 ldap_msgfree(result);
982 rlm_ldap_release_socket(inst, conn);
987 /** Modify user's object in LDAP
989 * Process a modifcation map to update a user object in the LDAP directory.
991 * @param inst rlm_ldap instance.
992 * @param request Current request.
993 * @param section that holds the map to process.
994 * @return one of the RLM_MODULE_* values.
996 static rlm_rcode_t user_modify(ldap_instance_t *inst, REQUEST *request, ldap_acct_section_t *section)
998 rlm_rcode_t rcode = RLM_MODULE_OK;
1000 ldap_handle_t *conn = NULL;
1002 LDAPMod *mod_p[LDAP_MAX_ATTRMAP + 1], mod_s[LDAP_MAX_ATTRMAP];
1003 LDAPMod **modify = mod_p;
1005 char *passed[LDAP_MAX_ATTRMAP * 2];
1006 int i, total = 0, last_pass = 0;
1008 char *expanded[LDAP_MAX_ATTRMAP];
1016 * Build our set of modifications using the update sections in
1023 char path[MAX_STRING_LEN];
1027 rad_assert(section);
1030 * Locate the update section were going to be using
1032 if (section->reference[0] != '.') {
1036 if (radius_xlat(p, (sizeof(path) - (p - path)) - 1, request, section->reference, NULL, NULL) < 0) {
1040 ci = cf_reference_item(NULL, section->cs, path);
1045 if (!cf_item_is_section(ci)){
1046 REDEBUG("Reference must resolve to a section");
1051 cs = cf_section_sub_find(cf_itemtosection(ci), "update");
1053 REDEBUG("Section must contain 'update' subsection");
1059 * Iterate over all the pairs, building our mods array
1061 for (ci = cf_item_find_next(cs, NULL); ci != NULL; ci = cf_item_find_next(cs, ci)) {
1062 int do_xlat = false;
1064 if (total == LDAP_MAX_ATTRMAP) {
1065 REDEBUG("Modify map size exceeded");
1070 if (!cf_item_is_pair(ci)) {
1071 REDEBUG("Entry is not in \"ldap-attribute = value\" format");
1077 * Retrieve all the information we need about the pair
1079 cp = cf_itemtopair(ci);
1080 value = cf_pair_value(cp);
1081 attr = cf_pair_attr(cp);
1082 op = cf_pair_operator(cp);
1084 if (!value || (*value == '\0')) {
1085 RDEBUG("Empty value string, skipping attribute \"%s\"", attr);
1090 switch (cf_pair_value_type(cp))
1093 case T_SINGLE_QUOTED_STRING:
1095 case T_BACK_QUOTED_STRING:
1096 case T_DOUBLE_QUOTED_STRING:
1104 if (op == T_OP_CMP_FALSE) {
1105 passed[last_pass] = NULL;
1106 } else if (do_xlat) {
1109 if (radius_xlat(exp, 0, request, value, NULL, NULL) <= 0) {
1110 RDEBUG("Skipping attribute \"%s\"", attr);
1117 expanded[last_exp++] = exp;
1118 passed[last_pass] = exp;
1123 memcpy(&(passed[last_pass]), &value, sizeof(passed[last_pass]));
1126 passed[last_pass + 1] = NULL;
1128 mod_s[total].mod_values = &(passed[last_pass]);
1135 * T_OP_EQ is *NOT* supported, it is impossible to
1136 * support because of the lack of transactions in LDAP
1139 mod_s[total].mod_op = LDAP_MOD_ADD;
1143 mod_s[total].mod_op = LDAP_MOD_REPLACE;
1147 case T_OP_CMP_FALSE:
1148 mod_s[total].mod_op = LDAP_MOD_DELETE;
1151 #ifdef LDAP_MOD_INCREMENT
1153 mod_s[total].mod_op = LDAP_MOD_INCREMENT;
1157 REDEBUG("Operator '%s' is not supported for LDAP modify operations",
1158 fr_int2str(fr_tokens, op, "¿unknown?"));
1164 * Now we know the value is ok, copy the pointers into
1165 * the ldapmod struct.
1167 memcpy(&(mod_s[total].mod_type), &(attr), sizeof(mod_s[total].mod_type));
1169 mod_p[total] = &(mod_s[total]);
1174 rcode = RLM_MODULE_NOOP;
1178 mod_p[total] = NULL;
1180 conn = rlm_ldap_get_socket(inst, request);
1181 if (!conn) return RLM_MODULE_FAIL;
1184 dn = rlm_ldap_find_user(inst, request, &conn, NULL, false, NULL, &rcode);
1185 if (!dn || (rcode != RLM_MODULE_OK)) {
1189 rcode = rlm_ldap_modify(inst, request, &conn, dn, modify);
1194 * Free up any buffers we allocated for xlat expansion
1196 for (i = 0; i < last_exp; i++) {
1197 talloc_free(expanded[i]);
1200 rlm_ldap_release_socket(inst, conn);
1205 static rlm_rcode_t mod_accounting(void *instance, REQUEST * request) {
1206 ldap_instance_t *inst = instance;
1208 if (inst->accounting) {
1209 return user_modify(inst, request, inst->accounting);
1212 return RLM_MODULE_NOOP;
1215 static rlm_rcode_t mod_post_auth(void *instance, REQUEST * request)
1217 ldap_instance_t *inst = instance;
1219 if (inst->postauth) {
1220 return user_modify(inst, request, inst->postauth);
1223 return RLM_MODULE_NOOP;
1227 /* globally exported name */
1228 module_t rlm_ldap = {
1231 RLM_TYPE_THREAD_SAFE, /* type: reserved */
1232 sizeof(ldap_instance_t),
1234 mod_instantiate, /* instantiation */
1235 mod_detach, /* detach */
1237 mod_authenticate, /* authentication */
1238 mod_authorize, /* authorization */
1239 NULL, /* preaccounting */
1240 mod_accounting, /* accounting */
1241 NULL, /* checksimul */
1242 NULL, /* pre-proxy */
1243 NULL, /* post-proxy */
1244 mod_post_auth /* post-auth */