/*
- * rlm_ldap.c LDAP authorization and authentication module.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
+ * This program is is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License, version 2 if the
+ * License as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
+ */
+
+/**
+ * $Id$
+ * @file rlm_ldap.c
+ * @brief LDAP authorization and authentication module.
*
- * Copyright 1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,
- * 2009,2010,2011,1012 The FreeRADIUS Server Project.
- *
- * Copyright 2012 Alan DeKok <aland@freeradius.org>
+ * @copyright 1999-2013 The FreeRADIUS Server Project.
+ * @copyright 2012 Alan DeKok <aland@freeradius.org>
+ * @copyright 2012-2013 Arran Cudbard-Bell <a.cudbardb@freeradius.org>
*/
-
#include <freeradius-devel/ident.h>
RCSID("$Id$")
#endif
+typedef struct ldap_acct_section {
+ CONF_SECTION *cs;
+
+ const char *reference;
+} ldap_acct_section_t;
+
+
typedef struct {
CONF_SECTION *cs;
fr_connection_pool_t *pool;
int chase_referrals;
int rebind;
- int ldap_debug; /* Debug flag for LDAP SDK */
+ int ldap_debug; //!< Debug flag for the SDK.
- const char *xlat_name; /* name used to xlat */
+ const char *xlat_name; //!< Instance name.
int expect_password;
/*
* RADIUS attribute to LDAP attribute maps
*/
- VALUE_PAIR_MAP *user_map; /* Applied to users and profiles */
+ value_pair_map_t *user_map; //!< Attribute map applied to users and
+ //!< profiles.
/*
* Access related configuration
char *groupname_attr;
char *groupmemb_filter;
char *groupmemb_attr;
+
+ /*
+ * Accounting
+ */
+ ldap_acct_section_t *postauth;
+ ldap_acct_section_t *accounting;
/*
* TLS items. We should really normalize these with the
* eDir support
*/
int edir;
+ int edir_autz;
#endif
/*
* For keep-alives.
};
/*
+ * Reference for accounting updates
+ */
+static const CONF_PARSER acct_section_config[] = {
+ {"reference", PW_TYPE_STRING_PTR,
+ offsetof(ldap_acct_section_t, reference), NULL, "."},
+ {NULL, -1, 0, NULL, NULL}
+};
+
+/*
* Various options that don't belong in the main configuration.
*
* Note that these overlap a bit with the connection pool code!
/* support for eDirectory Universal Password */
{"edir", PW_TYPE_BOOLEAN,
offsetof(ldap_instance,edir), NULL, NULL}, /* NULL defaults to "no" */
+
+ /*
+ * Attempt to bind with the Cleartext password we got from eDirectory
+ * Universal password for additional authorization checks.
+ */
+ {"edir_autz", PW_TYPE_BOOLEAN,
+ offsetof(ldap_instance,edir_autz), NULL, NULL}, /* NULL defaults to "no" */
#endif
/*
{ "tls", PW_TYPE_SUBSECTION, 0, NULL, (const void *) tls_config },
- { "profiles", PW_TYPE_SUBSECTION, 0, NULL, (const void *) attr_config },
-
{NULL, -1, 0, NULL, NULL}
};
} LDAP_CONN;
typedef struct xlat_attrs {
- const VALUE_PAIR_MAP *maps;
+ const value_pair_map_t *maps;
const char *attrs[MAX_ATTRMAP];
} xlat_attrs_t;
int count;
} rlm_ldap_result_t;
+typedef enum {
+ LDAP_PROC_SUCCESS = 0,
+ LDAP_PROC_ERROR = -1,
+ LDAP_PROC_RETRY = -2,
+ LDAP_PROC_REJECT = -3
+} ldap_rcode_t;
-#if LDAP_SET_REBIND_PROC_ARGS == 3
-/*
- * Rebind && chase referral stuff
- */
-static int ldap_rebind(LDAP *handle, LDAP_CONST char *url,
- UNUSED ber_tag_t request, UNUSED ber_int_t msgid,
- void *ctx )
+static ldap_rcode_t process_ldap_errno(ldap_instance *inst, LDAP_CONN **pconn,
+ const char *operation)
{
- LDAP_CONN *conn = ctx;
+ int ldap_errno;
+
+ ldap_get_option((*pconn)->handle, LDAP_OPT_ERROR_NUMBER,
+ &ldap_errno);
+ switch (ldap_errno) {
+ case LDAP_SUCCESS:
+ case LDAP_NO_SUCH_OBJECT:
+ return LDAP_PROC_SUCCESS;
- conn->referred = TRUE;
- conn->rebound = TRUE; /* not really, but oh well... */
- rad_assert(handle == conn->handle);
+ case LDAP_INSUFFICIENT_ACCESS:
+ radlog(L_ERR, "rlm_ldap (%s): %s failed: Insufficient access. "
+ "Check the identity and password configuration "
+ "directives", inst->xlat_name, operation);
+ return LDAP_PROC_ERROR;
+
+ case LDAP_TIMEOUT:
+ exec_trigger(NULL, inst->cs, "modules.ldap.timeout", TRUE);
+ radlog(L_ERR, "rlm_ldap (%s): %s failed: Timed out "
+ "while waiting for server to respond", inst->xlat_name,
+ operation);
+ return LDAP_PROC_ERROR;
- DEBUG("rlm_ldap (%s): Rebinding to URL %s", conn->inst->xlat_name, url);
-
- return ldap_bind_s(handle, conn->inst->login, conn->inst->password,
- LDAP_AUTH_SIMPLE);
+ case LDAP_FILTER_ERROR:
+ radlog(L_ERR, "rlm_ldap (%s): %s failed: Bad search "
+ "filter", inst->xlat_name, operation);
+ return LDAP_PROC_ERROR;
+
+ case LDAP_TIMELIMIT_EXCEEDED:
+ exec_trigger(NULL, inst->cs, "modules.ldap.timeout", TRUE);
+ /* FALL-THROUGH */
+
+ case LDAP_BUSY:
+ case LDAP_UNAVAILABLE:
+ /*
+ * Reconnect. There's an issue with the socket
+ * or LDAP server.
+ */
+ radlog(L_ERR, "rlm_ldap (%s): %s failed: %s",
+ inst->xlat_name, operation, ldap_err2string(ldap_errno));
+ case LDAP_SERVER_DOWN:
+ return LDAP_PROC_RETRY;
+
+ case LDAP_INVALID_CREDENTIALS:
+ case LDAP_CONSTRAINT_VIOLATION:
+ return LDAP_PROC_REJECT;
+
+ case LDAP_OPERATIONS_ERROR:
+ DEBUGW("Please set 'chase_referrals=yes' and 'rebind=yes'");
+ DEBUGW("See the ldap module configuration for details");
+ /* FALL-THROUGH */
+
+ default:
+ radlog(L_ERR, "rlm_ldap (%s): %s failed: %s",
+ inst->xlat_name, operation, ldap_err2string(ldap_errno));
+ return LDAP_PROC_ERROR;
+ }
}
-#endif
+
static int ldap_bind_wrapper(LDAP_CONN **pconn, const char *user,
- const char *password,
- const char **perror_str, int do_rebind)
+ const char *password, int retry)
{
- int rcode, ldap_errno;
- int module_rcode = RLM_MODULE_FAIL;
- int reconnect = FALSE;
- const char *error_string;
+ int rcode, msg_id;
+ int module_rcode = RLM_MODULE_OK;
LDAP_CONN *conn = *pconn;
ldap_instance *inst = conn->inst;
LDAPMessage *result = NULL;
struct timeval tv;
-redo:
- ldap_errno = ldap_bind(conn->handle, user, password, LDAP_AUTH_SIMPLE);
- if (ldap_errno < 0) {
- get_error:
- ldap_get_option(conn->handle, LDAP_OPT_ERROR_NUMBER,
- &ldap_errno);
- error_string = ldap_err2string(ldap_errno);
+retry:
+ msg_id = ldap_bind(conn->handle, user, password, LDAP_AUTH_SIMPLE);
+ if (msg_id < 0) goto get_error;
- if (do_rebind && !reconnect) {
- conn = fr_connection_reconnect(inst->pool, conn);
- *pconn = conn;
- if (!conn) return RLM_MODULE_FAIL;
- goto redo;
- }
+ DEBUG3("rlm_ldap (%s): Waiting for bind result...", inst->xlat_name);
- print_error:
- if (perror_str) *perror_str = error_string;
+ tv.tv_sec = inst->timeout;
+ tv.tv_usec = 0;
+ rcode = ldap_result(conn->handle, msg_id, 1, &tv, &result);
+ ldap_msgfree(result);
+ if (rcode <= 0) {
+get_error:
+ switch (process_ldap_errno(inst, &conn, "Bind"))
+ {
+ case LDAP_PROC_SUCCESS:
+ break;
+ case LDAP_PROC_REJECT:
+ module_rcode = RLM_MODULE_REJECT;
+ goto error;
+ case LDAP_PROC_ERROR:
+ module_rcode = RLM_MODULE_FAIL;
+error:
#ifdef HAVE_LDAP_INITIALIZE
- if (inst->is_url) {
- radlog(L_ERR, "rlm_ldap (%s): %s bind to %s failed: %s",
- inst->xlat_name, user,
- inst->server, error_string);
- } else
+ if (inst->is_url) {
+ radlog(L_ERR, "rlm_ldap (%s): bind "
+ "with %s to %s failed",
+ inst->xlat_name, user,
+ inst->server);
+ } else
#endif
- {
- radlog(L_ERR, "rlm_ldap (%s): %s bind to %s:%d "
- "failed: %s",
- inst->xlat_name, user,
- inst->server, inst->port,
- error_string);
- }
-
- return module_rcode; /* caller closes the connection */
+ {
+ radlog(L_ERR, "rlm_ldap (%s): bind "
+ "with %s to %s:%d failed",
+ inst->xlat_name, user,
+ inst->server, inst->port);
+ }
+
+ break;
+ case LDAP_PROC_RETRY:
+ if (retry) {
+ *pconn = fr_connection_reconnect(inst->pool, *pconn);
+ if (*pconn) goto retry;
+ }
+
+ module_rcode = RLM_MODULE_FAIL;
+ break;
+ default:
+ rad_assert(0);
+ }
}
- DEBUG3("rlm_ldap (%s): Waiting for bind result...", inst->xlat_name);
-
- tv.tv_sec = inst->timeout;
- tv.tv_usec = 0;
- rcode = ldap_result(conn->handle, ldap_errno, 1, &tv, &result);
- if (rcode < 0) goto get_error;
+ return module_rcode; /* caller closes the connection */
+}
- if (rcode == 0) {
- error_string = "timeout";
- goto print_error;
- }
+#if LDAP_SET_REBIND_PROC_ARGS == 3
+/*
+ * Rebind && chase referral stuff
+ */
+static int ldap_rebind(LDAP *handle, LDAP_CONST char *url,
+ UNUSED ber_tag_t request, UNUSED ber_int_t msgid,
+ void *ctx )
+{
+ int rcode, ldap_errno;
+ LDAP_CONN *conn = ctx;
- ldap_errno = ldap_result2error(conn->handle, result, 1);
- switch (ldap_errno) {
- case LDAP_SUCCESS:
- break;
+ conn->referred = TRUE;
+ conn->rebound = TRUE; /* not really, but oh well... */
+ rad_assert(handle == conn->handle);
- case LDAP_INVALID_CREDENTIALS:
- case LDAP_CONSTRAINT_VIOLATION:
- rcode = RLM_MODULE_REJECT;
- /* FALL-THROUGH */
+ DEBUG("rlm_ldap (%s): Rebinding to URL %s", conn->inst->xlat_name, url);
+
- default:
- goto get_error;
+ rcode = ldap_bind_wrapper(&conn, conn->inst->login,
+ conn->inst->password, FALSE);
+
+ if (rcode == RLM_MODULE_OK) {
+ return LDAP_SUCCESS;
}
-
- return RLM_MODULE_OK;
+
+ ldap_get_option(handle, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
+
+ return ldap_errno;
}
+#endif
-/*************************************************************************
- *
- * Function: ldap_conn_create
- *
- * Purpose: Create and return a new connection
- * This function is probably too big.
- *
- *************************************************************************/
+/** Create and return a new connection
+ * This function is probably too big.
+ */
static void *ldap_conn_create(void *ctx)
{
int module_rcode;
ldap_instance *inst = ctx;
LDAP *handle = NULL;
LDAP_CONN *conn = NULL;
- const char *error;
#ifdef HAVE_LDAP_INITIALIZE
if (inst->is_url) {
- DEBUG("rlm_ldap (%s): Connect to %s", inst->xlat_name,
+ DEBUG("rlm_ldap (%s): Connecting to %s", inst->xlat_name,
inst->server);
ldap_errno = ldap_initialize(&handle, inst->server);
-
if (ldap_errno != LDAP_SUCCESS) {
radlog(L_ERR, "rlm_ldap (%s): ldap_initialize() "
"failed: %s",
} else
#endif
{
- DEBUG("rlm_ldap (%s): Connect to %s:%d", inst->xlat_name,
+ DEBUG("rlm_ldap (%s): Connecting to %s:%d", inst->xlat_name,
inst->server, inst->port);
handle = ldap_init(inst->server, inst->port);
do_ldap_option(LDAP_OPT_REFERRALS, "chase_referrals",
LDAP_OPT_ON);
-#if LDAP_SET_REBIND_PROC_ARGS == 3
if (inst->rebind == 1) {
+#if LDAP_SET_REBIND_PROC_ARGS == 3
ldap_set_rebind_proc(handle, ldap_rebind, inst);
- }
+#else
+ DEBUGW("The flag 'rebind = yes' is not supported by the system LDAP library. Ignoring.");
#endif
+ }
} else {
do_ldap_option(LDAP_OPT_REFERRALS, "chase_referrals",
LDAP_OPT_OFF);
tv.tv_usec = 0;
do_ldap_option(LDAP_OPT_NETWORK_TIMEOUT, "net_timeout", &tv);
- do_ldap_option(LDAP_OPT_TIMELIMIT, "timelimit",
- &(inst->timelimit));
+ do_ldap_option(LDAP_OPT_TIMELIMIT, "timelimit", &(inst->timelimit));
ldap_version = LDAP_VERSION3;
do_ldap_option(LDAP_OPT_PROTOCOL_VERSION, "ldap_version",
if (ldap_int_tls_config(NULL, LDAP_OPT_X_TLS_REQUIRE_CERT,
(inst->tls_require_cert)) != LDAP_OPT_SUCCESS) {
ldap_get_option(handle, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
- radlog(L_ERR, "rlm_ldap (%s): could not set ",
+ radlog(L_ERR, "rlm_ldap (%s): could not set "
"LDAP_OPT_X_TLS_REQUIRE_CERT option to %s: %s",
inst->xlat_name,
inst->tls_require_cert,
}
#endif /* HAVE_LDAP_START_TLS */
- conn = rad_malloc(sizeof(*conn));
+ conn = talloc(NULL, LDAP_CONN);
conn->inst = inst;
conn->handle = handle;
conn->rebound = FALSE;
conn->referred = FALSE;
module_rcode = ldap_bind_wrapper(&conn, inst->login, inst->password,
- &error, FALSE);
+ FALSE);
if (module_rcode != RLM_MODULE_OK) {
- radlog(L_ERR, "rlm_ldap (%s): failed binding to LDAP "
- "server: %s",
- inst->xlat_name, error);
-
- /*
- * FIXME: print "check config, morians!
- */
goto conn_fail;
}
}
-/*************************************************************************
+/** Close and delete a connection
*
- * Function: ldap_conn_delete
- *
- * Purpose: Close and delete a connection
- *
- *************************************************************************/
+ */
static int ldap_conn_delete(UNUSED void *ctx, void *connection)
{
LDAP_CONN *conn = connection;
ldap_unbind_s(conn->handle);
- free(conn);
+ talloc_free(conn);
return 0;
}
-/*************************************************************************
- *
- * Function: ldap_get_socket
+/** Gets an LDAP socket from the connection pool
*
- * Purpose: Gets an LDAP socket from the connection pool
- *
- *************************************************************************/
+ */
static LDAP_CONN *ldap_get_socket(ldap_instance *inst)
{
LDAP_CONN *conn;
return conn;
}
-/*************************************************************************
- *
- * Function: ldap_release_socket
- *
- * Purpose: Frees an LDAP socket back to the connection pool
+/** Frees an LDAP socket back to the connection pool
*
- *************************************************************************/
+ */
static void ldap_release_socket(ldap_instance *inst, LDAP_CONN *conn)
{
/*
}
-/*************************************************************************
- *
- * Function: ldap_escape_func
+/* Converts "bad" strings into ones which are safe for LDAP
*
- * Purpose: Converts "bad" strings into ones which are safe for LDAP
- *
- *************************************************************************/
+ */
static size_t ldap_escape_func(UNUSED REQUEST *request, char *out,
size_t outlen, const char *in, UNUSED void *arg)
{
return len;
}
-/*************************************************************************
+/** Do a search and get a response
*
- * Function: perform_search
- *
- * Purpose: Do a search and get a response
- *
- *************************************************************************/
+ */
static int perform_search(ldap_instance *inst, REQUEST *request,
LDAP_CONN **pconn, const char *search_basedn,
int scope, const char *filter,
{
int ldap_errno;
int count = 0;
- int reconnect = FALSE;
- LDAP_CONN *conn = *pconn;
struct timeval tv;
/*
/*
* Do all searches as the default admin user.
*/
- if (conn->rebound) {
- ldap_errno = ldap_bind_wrapper(pconn,
- inst->login, inst->password,
- NULL, TRUE);
+ if ((*pconn)->rebound) {
+ ldap_errno = ldap_bind_wrapper(pconn, inst->login,
+ inst->password, TRUE);
if (ldap_errno != RLM_MODULE_OK) {
return -1;
}
- rad_assert(*pconn != NULL);
- conn = *pconn;
- conn->rebound = FALSE;
+ rad_assert(*pconn);
+ (*pconn)->rebound = FALSE;
}
tv.tv_sec = inst->timeout;
filter);
retry:
- ldap_errno = ldap_search_ext_s(conn->handle, search_basedn, scope,
+ ldap_errno = ldap_search_ext_s((*pconn)->handle, search_basedn, scope,
filter, search_attrs, 0, NULL, NULL,
&tv, 0, presult);
- switch (ldap_errno) {
- case LDAP_SUCCESS:
- case LDAP_NO_SUCH_OBJECT:
- break;
-
- case LDAP_SERVER_DOWN:
- do_reconnect:
- ldap_msgfree(*presult);
-
- if (reconnect) return -1;
- reconnect = TRUE;
-
- conn = fr_connection_reconnect(inst->pool, conn);
- *pconn = conn; /* tell the caller we have a new connection */
- if (!conn) return -1;
- goto retry;
-
- case LDAP_INSUFFICIENT_ACCESS:
- radlog(L_ERR, "rlm_ldap (%s): Search failed: "
- "Insufficient access. Check the identity and password "
- "configuration directives", inst->xlat_name);
- ldap_msgfree(*presult);
- return -1;
-
- case LDAP_TIMEOUT:
- exec_trigger(NULL, inst->cs, "modules.ldap.timeout", TRUE);
- radlog(L_ERR, "rlm_ldap (%s): Search failed: Timed out "
- "while waiting for server to respond"
- "Please increase the timeout", inst->xlat_name);
- ldap_msgfree(*presult);
- return -1;
-
- case LDAP_FILTER_ERROR:
- radlog(L_ERR, "rlm_ldap (%s): Search failed: Bad search "
- "filter: %s", inst->xlat_name,filter);
- ldap_msgfree(*presult);
- return -1;
-
- case LDAP_TIMELIMIT_EXCEEDED:
- exec_trigger(NULL, inst->cs, "modules.ldap.timeout", TRUE);
-
- case LDAP_BUSY:
- case LDAP_UNAVAILABLE:
- /*
- * Reconnect. There's an issue with the socket
- * or LDAP server.
- */
- ldap_get_option(conn->handle, LDAP_OPT_ERROR_NUMBER,
- &ldap_errno);
- radlog(L_ERR, "rlm_ldap (%s): Search failed: %s",
- inst->xlat_name, ldap_err2string(ldap_errno));
- goto do_reconnect;
-
- default:
- ldap_get_option(conn->handle, LDAP_OPT_ERROR_NUMBER,
- &ldap_errno);
- radlog(L_ERR, "rlm_ldap (%s): Search failed: %s",
- inst->xlat_name, ldap_err2string(ldap_errno));
+ if (ldap_errno != LDAP_SUCCESS) {
ldap_msgfree(*presult);
- return -1;
+ switch (process_ldap_errno(inst, pconn, "Search"))
+ {
+ case LDAP_PROC_SUCCESS:
+ break;
+ case LDAP_PROC_REJECT:
+ case LDAP_PROC_ERROR:
+ return -1;
+ case LDAP_PROC_RETRY:
+ *pconn = fr_connection_reconnect(inst->pool, *pconn);
+ if (*pconn) goto retry;
+ return -1;
+ default:
+ rad_assert(0);
+ }
}
-
- count = ldap_count_entries(conn->handle, *presult);
+
+ count = ldap_count_entries((*pconn)->handle, *presult);
if (count == 0) {
ldap_msgfree(*presult);
RDEBUG("Search returned no results");
return 0;
}
-/*************************************************************************
- *
- * Function: ldap_xlat
- *
- * Purpose: Expand an LDAP URL into a query, and return a string
- * result from that query.
+/** Expand an LDAP URL into a query, and return a string result from that query.
*
- *************************************************************************/
+ */
static size_t ldap_xlat(void *instance, REQUEST *request, const char *fmt,
char *out, size_t freespace)
{
}
-static char *get_userdn(LDAP_CONN **pconn, REQUEST *request, int *module_rcode)
+static char *get_userdn(LDAP_CONN **pconn, REQUEST *request,
+ rlm_rcode_t *module_rcode)
{
int rcode;
VALUE_PAIR *vp;
ldap_instance *inst = (*pconn)->inst;
- LDAP *handle = (*pconn)->handle;
LDAPMessage *result, *entry;
int ldap_errno;
static char firstattr[] = "uid";
*module_rcode = RLM_MODULE_FAIL;
- vp = pairfind(request->config_items, PW_LDAP_USERDN, 0);
- if (vp) return vp->vp_strvalue;
-
+ vp = pairfind(request->config_items, PW_LDAP_USERDN, 0, TAG_ANY);
+ if (vp) {
+ *module_rcode = RLM_MODULE_OK;
+ return vp->vp_strvalue;
+ }
+
if (!radius_xlat(filter, sizeof(filter), inst->filter,
request, ldap_escape_func, NULL)) {
radlog(L_ERR, "rlm_ldap (%s): Unable to create filter",
return NULL;
}
- if ((entry = ldap_first_entry(handle, result)) == NULL) {
- ldap_get_option(handle, LDAP_OPT_RESULT_CODE,
+ if ((entry = ldap_first_entry((*pconn)->handle, result)) == NULL) {
+ ldap_get_option((*pconn)->handle, LDAP_OPT_RESULT_CODE,
&ldap_errno);
radlog(L_ERR, "rlm_ldap (%s): Failed retrieving entry: %s",
inst->xlat_name,
return NULL;
}
- if ((user_dn = ldap_get_dn(handle, entry)) == NULL) {
- ldap_get_option(handle, LDAP_OPT_RESULT_CODE,
+ if ((user_dn = ldap_get_dn((*pconn)->handle, entry)) == NULL) {
+ ldap_get_option((*pconn)->handle, LDAP_OPT_RESULT_CODE,
&ldap_errno);
radlog(L_ERR, "rlm_ldap (%s): ldap_get_dn() failed: %s",
inst->xlat_name,
return NULL;
}
+ *module_rcode = RLM_MODULE_OK;
+
pairadd(&request->config_items, vp);
ldap_memfree(user_dn);
ldap_msgfree(result);
}
-/*****************************************************************************
+/** Perform LDAP-Group comparison checking
*
- * Perform LDAP-Group comparison checking
- *
- *****************************************************************************/
+ */
static int ldap_groupcmp(void *instance, REQUEST *request,
UNUSED VALUE_PAIR *thing, VALUE_PAIR *check,
UNUSED VALUE_PAIR *check_pairs,
UNUSED VALUE_PAIR **reply_pairs)
{
ldap_instance *inst = instance;
- int i, rcode, found, module_rcode;
+ int i, rcode, found;
+ rlm_rcode_t module_rcode;
LDAPMessage *result = NULL;
LDAPMessage *entry = NULL;
int ldap_errno;
ldap_msgfree(result);
ldap_release_socket(inst, conn);
- if (!found){
+ if (!found) {
RDEBUG("User is not a member of specified group");
return 1;
}
return 0;
}
-/*
- * Verify that the ldap update section makes sense, and add attribute names
- * to array of attributes for efficient querying later.
- */
-static int build_attrmap(CONF_SECTION *cs, VALUE_PAIR_MAP **head)
-{
- const char *cs_list, *p;
-
- request_refs_t request_def = REQUEST_CURRENT;
- pair_lists_t list_def = PAIR_LIST_REQUEST;
-
- CONF_ITEM *ci = cf_sectiontoitem(cs);
- CONF_PAIR *cp;
-
- unsigned int total = 0;
- VALUE_PAIR_MAP **tail, *map;
- *head = NULL;
- tail = head;
-
- if (!cs) return 0;
-
- cs_list = p = cf_section_name2(cs);
- if (cs_list) {
- request_def = radius_request_name(&p, REQUEST_UNKNOWN);
- if (request_def == REQUEST_UNKNOWN) {
- cf_log_err(ci, "rlm_ldap: Default request specified "
- "in mapping section is invalid");
- return -1;
- }
-
- list_def = fr_str2int(pair_lists, p, PAIR_LIST_UNKNOWN);
- if (list_def == PAIR_LIST_UNKNOWN) {
- cf_log_err(ci, "rlm_ldap: Default list specified "
- "in mapping section is invalid");
- return -1;
- }
- }
-
- for (ci = cf_item_find_next(cs, NULL);
- ci != NULL;
- ci = cf_item_find_next(cs, ci)) {
- if (total++ == MAX_ATTRMAP) {
- cf_log_err(ci, "rlm_ldap: Attribute map size exceeded");
- goto error;
- }
-
- if (!cf_item_is_pair(ci)) {
- cf_log_err(ci, "rlm_ldap: Entry is not in \"attribute ="
- " ldap-attribute\" format");
- goto error;
- }
-
- cp = cf_itemtopair(ci);
- map = radius_cp2map(cp, REQUEST_CURRENT, list_def);
- if (!map) {
- goto error;
- }
-
- *tail = map;
- tail = &(map->next);
- }
-
- return 0;
-
- error:
- radius_mapfree(head);
- return -1;
-}
-
-/*****************************************************************************
- *
- * Detach from the LDAP server and cleanup internal state.
+/** Detach from the LDAP server and cleanup internal state.
*
- *****************************************************************************/
+ */
static int ldap_detach(void *instance)
{
ldap_instance *inst = instance;
-
- fr_connection_pool_delete(inst->pool);
+ fr_connection_pool_delete(inst->pool);
+
if (inst->user_map) {
radius_mapfree(&inst->user_map);
}
- free(inst);
-
return 0;
}
-/*************************************************************************
- *
- * Function: rlm_ldap_instantiate
- *
- * Purpose: Uses section of radiusd config file passed as parameter
- * to create an instance of the module.
- *
- *************************************************************************/
-static int ldap_instantiate(CONF_SECTION * conf, void **instance)
+static int parse_sub_section(CONF_SECTION *parent,
+ ldap_instance *inst,
+ ldap_acct_section_t **config,
+ rlm_components_t comp)
{
- ldap_instance *inst;
CONF_SECTION *cs;
- inst = rad_malloc(sizeof *inst);
- if (!inst) {
+ const char *name = section_type_value[comp].section;
+
+ cs = cf_section_sub_find(parent, name);
+ if (!cs) {
+ radlog(L_INFO, "rlm_ldap (%s): Couldn't find configuration for "
+ "%s, will return NOOP for calls from this section",
+ inst->xlat_name, name);
+
+ return 0;
+ }
+
+ *config = talloc_zero(inst, ldap_acct_section_t);
+ if (cf_section_parse(cs, *config, acct_section_config) < 0) {
+ radlog(L_ERR, "rlm_ldap (%s): Failed parsing configuration for "
+ "section %s", inst->xlat_name, name);
return -1;
}
- memset(inst, 0, sizeof(*inst));
- inst->cs = conf;
+
+ (*config)->cs = cs;
- inst->chase_referrals = 2; /* use OpenLDAP defaults */
- inst->rebind = 2;
+ return 0;
+}
- if (cf_section_parse(conf, inst, module_config) < 0) {
- free(inst);
+static int ldap_map_verify(ldap_instance *inst, value_pair_map_t **head)
+{
+ value_pair_map_t *map;
+
+ if (radius_attrmap(inst->cs, head, PAIR_LIST_REPLY,
+ PAIR_LIST_REQUEST, MAX_ATTRMAP) < 0) {
return -1;
}
+ /*
+ * Attrmap only performs some basic validation checks, we need
+ * to do rlm_ldap specific checks here.
+ */
+ for (map = *head; map != NULL; map = map->next) {
+ if (map->dst->type != VPT_TYPE_ATTR) {
+ cf_log_err(map->ci, "Left operand must be an "
+ "attribute ref");
+
+ return -1;
+ }
+
+ if (map->src->type == VPT_TYPE_LIST) {
+ cf_log_err(map->ci, "Right operand must not be "
+ "a list");
+
+ return -1;
+ }
+
+ switch (map->src->type)
+ {
+ /*
+ * Only =, :=, += and -= operators are supported for
+ * cache entries.
+ */
+ case VPT_TYPE_LITERAL:
+ case VPT_TYPE_XLAT:
+ case VPT_TYPE_ATTR:
+ switch (map->op) {
+ case T_OP_SET:
+ case T_OP_EQ:
+ case T_OP_SUB:
+ case T_OP_ADD:
+ break;
+
+ default:
+ cf_log_err(map->ci, "Operator \"%s\" not "
+ "allowed for %s values",
+ fr_int2str(fr_tokens, map->op,
+ "¿unknown?"),
+ fr_int2str(vpt_types, map->src->type,
+ "¿unknown?"));
+ return -1;
+ }
+ default:
+ break;
+ }
+ }
+ return 0;
+}
+
+/** Parses config
+ * Uses section of radiusd config file passed as parameter to create an
+ * instance of the module.
+ */
+static int ldap_instantiate(CONF_SECTION * conf, void **instance)
+{
+ ldap_instance *inst;
+
+ *instance = inst = talloc_zero(conf, ldap_instance);
+ if (!inst) return -1;
+
+ inst->cs = conf;
+
+ inst->chase_referrals = 2; /* use OpenLDAP defaults */
+ inst->rebind = 2;
inst->xlat_name = cf_section_name2(conf);
- if (!inst->xlat_name) inst->xlat_name = cf_section_name1(conf);
+ if (!inst->xlat_name) {
+ inst->xlat_name = cf_section_name1(conf);
+ }
+
+ /*
+ * If the configuration parameters can't be parsed, then fail.
+ */
+ if ((cf_section_parse(conf, inst, module_config) < 0) ||
+ (parse_sub_section(conf, inst,
+ &inst->accounting,
+ RLM_COMPONENT_ACCT) < 0) ||
+ (parse_sub_section(conf, inst,
+ &inst->postauth,
+ RLM_COMPONENT_POST_AUTH) < 0)) {
+ radlog(L_ERR, "rlm_ldap (%s): Failed parsing configuration",
+ inst->xlat_name);
+ goto error;
+ }
if (inst->server == NULL) {
- radlog(L_ERR, "rlm_ldap (%s): missing 'server' directive",
+ radlog(L_ERR, "rlm_ldap (%s): Missing 'server' directive",
inst->xlat_name);
- ldap_detach(inst);
- return -1;
+ goto error;
}
/*
radlog(L_ERR, "rlm_ldap (%s): 'server' directive is in URL "
"form but ldap_initialize() is not available",
inst->xlat_name);
- ldap_detach(inst);
- return -1;
+ goto error;
#endif
}
radlog(L_ERR, "rlm_ldap (%s): Cannot use 'rebind' directive "
"as this version of libldap does not support the API "
"that we need", inst->xlat-name);
- ldap_detach(inst);
- return -1;
+ goto error;
}
#endif
/*
* Build the attribute map
*/
- cs = cf_section_sub_find(conf, "update");
- if (cs) {
- if (build_attrmap(cs, &(inst->user_map)) < 0) {
- ldap_detach(inst);
- return -1;
- }
+ if (ldap_map_verify(inst, &(inst->user_map)) < 0) {
+ goto error;
}
/*
*/
paircompare_register(PW_LDAP_GROUP, PW_USER_NAME, ldap_groupcmp, inst);
if (cf_section_name2(conf)) {
- DICT_ATTR *da;
+ const DICT_ATTR *da;
ATTR_FLAGS flags;
char buffer[256];
if (!da) {
radlog(L_ERR, "rlm_ldap (%s): Failed creating "
"attribute %s", inst->xlat_name, buffer);
- ldap_detach(inst);
- return -1;
+ goto error;
}
paircompare_register(da->attr, PW_USER_NAME, ldap_groupcmp,
return -1;
}
- *instance = inst;
return 0;
+
+error:
+ ldap_detach(inst);
+ return -1;
}
static int check_access(ldap_instance *inst, REQUEST* request, LDAP_CONN *conn,
}
-static VALUE_PAIR *ldap_getvalue(REQUEST *request, const VALUE_PAIR_TMPL *dst,
+static VALUE_PAIR *ldap_getvalue(REQUEST *request, const value_pair_map_t *map,
void *ctx)
{
rlm_ldap_result_t *self = ctx;
* just use whatever was set in the attribute map.
*/
for (i = 0; i < self->count; i++) {
- vp = pairalloc(dst->da);
+ vp = pairalloc(NULL, map->dst->da);
rad_assert(vp);
pairparsevalue(vp, self->values[i]);
static void xlat_attrsfree(const xlat_attrs_t *expanded)
{
- const VALUE_PAIR_MAP *map;
+ const value_pair_map_t *map;
unsigned int total = 0;
- char *name;
+ const char *name;
for (map = expanded->maps; map != NULL; map = map->next)
{
- memcpy(&name, &(expanded->attrs[total++]), sizeof(name));
-
+ name = expanded->attrs[total++];
if (!name) return;
- if (map->src->do_xlat) {
- free(name);
+ switch (map->src->type)
+ {
+ case VPT_TYPE_XLAT:
+ case VPT_TYPE_ATTR:
+ rad_cfree(name);
+ break;
+ default:
+ break;
}
}
}
-static int xlat_attrs(REQUEST *request, const VALUE_PAIR_MAP *maps,
+static int xlat_attrs(REQUEST *request, const value_pair_map_t *maps,
xlat_attrs_t *expanded)
{
- const VALUE_PAIR_MAP *map;
+ const value_pair_map_t *map;
unsigned int total = 0;
size_t len;
char *buffer;
+ VALUE_PAIR *found, **from = NULL;
+ REQUEST *context;
+
for (map = maps; map != NULL; map = map->next)
{
- if (map->src->do_xlat) {
+ switch (map->src->type)
+ {
+ case VPT_TYPE_XLAT:
buffer = rad_malloc(MAX_ATTR_STR_LEN);
len = radius_xlat(buffer, MAX_ATTR_STR_LEN,
map->src->name, request, NULL, NULL);
- if (!len) {
+ if (len <= 0) {
RDEBUG("Expansion of LDAP attribute "
"\"%s\" failed", map->src->name);
- expanded->attrs[total] = NULL;
-
- xlat_attrsfree(expanded);
-
- return -1;
+ goto error;
}
expanded->attrs[total++] = buffer;
- } else {
+ break;
+
+ case VPT_TYPE_ATTR:
+ context = request;
+
+ if (radius_request(&context, map->src->request) == 0) {
+ from = radius_list(context, map->src->list);
+ }
+ if (!from) continue;
+
+ found = pairfind(*from, map->src->da->attr,
+ map->src->da->vendor, TAG_ANY);
+ if (!found) continue;
+
+ buffer = rad_malloc(MAX_ATTR_STR_LEN);
+ strlcpy(buffer, found->vp_strvalue, MAX_ATTR_STR_LEN);
+
+ expanded->attrs[total++] = buffer;
+ break;
+
+ case VPT_TYPE_LITERAL:
expanded->attrs[total++] = map->src->name;
+ break;
+ default:
+ rad_assert(0);
+ error:
+ expanded->attrs[total] = NULL;
+
+ xlat_attrsfree(expanded);
+
+ return -1;
}
+
}
expanded->attrs[total] = NULL;
LDAP *handle, const xlat_attrs_t *expanded,
LDAPMessage *entry)
{
- const VALUE_PAIR_MAP *map;
+ const value_pair_map_t *map;
unsigned int total = 0;
rlm_ldap_result_t result;
* to read the documentation.
*/
if (inst->expect_password && (debug_flag > 1)) {
- if (!pairfind(request->config_items,PW_CLEARTEXT_PASSWORD, 0) &&
- !pairfind(request->config_items,
- PW_NT_PASSWORD, 0) &&
- !pairfind(request->config_items,
- PW_USER_PASSWORD, 0) &&
- !pairfind(request->config_items,
- PW_PASSWORD_WITH_HEADER, 0) &&
- !pairfind(request->config_items,
- PW_CRYPT_PASSWORD, 0)) {
- RDEBUG("WARNING: No \"known good\" password "
- "was found in LDAP. Are you sure that "
- "the user is configured correctly?");
- }
+ if (!pairfind(request->config_items, PW_CLEARTEXT_PASSWORD, 0, TAG_ANY) &&
+ !pairfind(request->config_items, PW_NT_PASSWORD, 0, TAG_ANY) &&
+ !pairfind(request->config_items, PW_USER_PASSWORD, 0, TAG_ANY) &&
+ !pairfind(request->config_items, PW_PASSWORD_WITH_HEADER, 0, TAG_ANY) &&
+ !pairfind(request->config_items, PW_CRYPT_PASSWORD, 0, TAG_ANY)) {
+ RDEBUGW("No \"known good\" password "
+ "was found in LDAP. Are you sure that "
+ "the user is configured correctly?");
+ }
}
}
}
-/******************************************************************************
- *
- * Function: ldap_authorize
+/** Check if user is authorized for remote access
*
- * Purpose: Check if user is authorized for remote access
- *
- ******************************************************************************/
-static int ldap_authorize(void *instance, REQUEST * request)
+ */
+static rlm_rcode_t ldap_authorize(void *instance, REQUEST * request)
{
int rcode;
int module_rcode = RLM_MODULE_OK;
ldap_instance *inst = instance;
- char *user_dn;
+ char *user_dn = NULL;
char **vals;
VALUE_PAIR *vp;
LDAP_CONN *conn;
inst->xlat_name);
return RLM_MODULE_INVALID;
}
-
- conn = ldap_get_socket(inst);
- if (!conn) return RLM_MODULE_FAIL;
if (xlat_attrs(request, inst->user_map, &expanded) < 0) {
return RLM_MODULE_FAIL;
}
+
+ conn = ldap_get_socket(inst);
+ if (!conn) return RLM_MODULE_FAIL;
+
rcode = perform_search(inst, request, &conn, basedn,
LDAP_SCOPE_SUBTREE, filter, expanded.attrs,
&result);
/*
* We already have a Cleartext-Password. Skip edir.
*/
- if (inst->edir && pairfind(request->config_items,
- PW_CLEARTEXT_PASSWORD, 0)) {
+ if (inst->edir && pairfind(request->config_items, PW_CLEARTEXT_PASSWORD, 0, TAG_ANY)) {
goto skip_edir;
}
res = nmasldap_get_password(conn->handle, user_dn,
buffer, &bufsize);
if (res != 0) {
- RDEBUG2("Failed to retrieve eDirectory password. Check your configuration !");
+ RDEBUG2("Failed to retrieve eDirectory password. Check "
+ "your configuration !");
module_rcode = RLM_MODULE_NOOP;
goto free_result;
}
- /* add Cleartext-Password attribute to the request */
+ /* Add Cleartext-Password attribute to the request */
vp = radius_paircreate(request, &request->config_items,
- PW_CLEARTEXT_PASSWORD, 0, PW_TYPE_STRING);
+ PW_CLEARTEXT_PASSWORD, 0);
strlcpy(vp->vp_strvalue, buffer, sizeof(vp->vp_strvalue));
vp->length = strlen(vp->vp_strvalue);
RDEBUG2("Added eDirectory password in check items as %s = %s",
- vp->name, vp->vp_strvalue);
+ vp->da->name, vp->vp_strvalue);
+
+ if (inst->edir_autz) {
+ RDEBUG2("Binding as user for eDirectory authorization "
+ "checks");
+ /*
+ * Bind as the user
+ */
+ conn->rebound = TRUE;
+ module_rcode = ldap_bind_wrapper(&conn, user_dn,
+ vp->vp_strvalue,
+ TRUE);
+ if (module_rcode != RLM_MODULE_OK) {
+ goto free_result;
+ }
+
+ RDEBUG("Bind as user \"%s\" was successful", user_dn);
+ }
}
skip_edir:
#endif
- ldap_memfree(user_dn);
-
/*
* Check for access.
*/
/*
* Apply ONE user profile, or a default user profile.
*/
- vp = pairfind(request->config_items, PW_USER_PROFILE, 0);
+ vp = pairfind(request->config_items, PW_USER_PROFILE, 0, TAG_ANY);
if (vp || inst->default_profile) {
char *profile = inst->default_profile;
}
free_result:
+ if (user_dn) ldap_memfree(user_dn);
xlat_attrsfree(&expanded);
ldap_msgfree(result);
free_socket:
}
-/*****************************************************************************
- *
- * Function: ldap_authenticate
- *
- * Purpose: Check the user's password against ldap database
+/** Check the user's password against ldap database
*
- *****************************************************************************/
-static int ldap_authenticate(void *instance, REQUEST * request)
+ */
+static rlm_rcode_t ldap_authenticate(void *instance, REQUEST * request)
{
- int module_rcode;
+ rlm_rcode_t module_rcode;
const char *user_dn;
ldap_instance *inst = instance;
LDAP_CONN *conn;
return RLM_MODULE_INVALID;
}
- if (request->password->attribute != PW_USER_PASSWORD) {
+ if (request->password->da->attr != PW_USER_PASSWORD) {
radlog(L_AUTH, "rlm_ldap (%s): Attribute \"User-Password\" "
"is required for authentication. Cannot use \"%s\".",
- inst->xlat_name, request->password->name);
+ inst->xlat_name, request->password->da->name);
return RLM_MODULE_INVALID;
}
return RLM_MODULE_INVALID;
}
- conn = ldap_get_socket(inst);
- if (!conn) return RLM_MODULE_FAIL;
-
RDEBUG("Login attempt by \"%s\" with password \"%s\"",
request->username->vp_strvalue, request->password->vp_strvalue);
+ conn = ldap_get_socket(inst);
+ if (!conn) return RLM_MODULE_FAIL;
+
/*
* Get the DN by doing a search.
*/
conn->rebound = TRUE;
module_rcode = ldap_bind_wrapper(&conn, user_dn,
request->password->vp_strvalue,
- NULL, TRUE);
+ TRUE);
if (module_rcode == RLM_MODULE_OK) {
RDEBUG("Bind as user \"%s\" was successful", user_dn);
}
return module_rcode;
}
-
-#ifdef WITH_EDIR
-/*****************************************************************************
- *
- * Function: ldap_postauth
+/** Modify user's object in LDAP
*
- * Purpose: Check the user's password against ldap database
- *
- *****************************************************************************/
-static int ldap_postauth(void *instance, REQUEST * request)
+ */
+static rlm_rcode_t user_modify(ldap_instance *inst, REQUEST *request,
+ ldap_acct_section_t *section)
{
- int module_rcode;
+ rlm_rcode_t module_rcode = RLM_MODULE_OK;
+ int ldap_errno, rcode, msg_id;
+ LDAPMessage *result = NULL;
+
+ LDAP_CONN *conn = NULL;
+
+ LDAPMod *mod_p[MAX_ATTRMAP + 1], mod_s[MAX_ATTRMAP];
+ LDAPMod **modify = mod_p;
+
+ char *passed[MAX_ATTRMAP * 2];
+ int i, total = 0, last_pass = 0;
+
+ char *expanded[MAX_ATTRMAP];
+ int last_exp = 0;
+
+ struct timeval tv;
+
+ const char *attr;
+ const char *value;
+
const char *user_dn;
- ldap_instance *inst = instance;
- LDAP_CONN *conn;
- VALUE_PAIR *vp;
/*
- * Ensure that we have a username and a
- * Cleartext-Password in the request
+ * Build our set of modifications using the update sections in
+ * the config.
*/
- if (!request->username) {
- radlog(L_AUTH, "rlm_ldap (%s): Attribute \"User-Name\" is "
- "required for authentication", inst->xlat_name);
- return RLM_MODULE_INVALID;
- }
+ CONF_ITEM *ci;
+ CONF_PAIR *cp;
+ CONF_SECTION *cs;
+ FR_TOKEN op;
+ char path[MAX_STRING_LEN];
+
+ char *p = path;
- vp = pairfind(request->config_items, PW_CLEARTEXT_PASSWORD, 0);
- if (!vp) {
- radlog(L_AUTH, "rlm_ldap (%s): Attribute \"Cleartext-Password\" "
- "is required for authentication.", inst->xlat_name);
- return RLM_MODULE_INVALID;
+ rad_assert(section);
+
+ /*
+ * Locate the update section were going to be using
+ */
+ if (section->reference[0] != '.') {
+ *p++ = '.';
+ }
+
+ if (!radius_xlat(p, (sizeof(path) - (p - path)) - 1,
+ section->reference, request, NULL, NULL)) {
+ goto error;
}
- if (!*vp->vp_strvalue) {
- radlog(L_AUTH, "rlm_ldap (%s): Attribute \"Cleartext-Password\" "
- "is empty.", inst->xlat_name);
- return RLM_MODULE_INVALID;
+ ci = cf_reference_item(NULL, section->cs, path);
+ if (!ci) {
+ goto error;
}
+
+ if (!cf_item_is_section(ci)){
+ radlog(L_ERR, "rlm_ldap (%s): Reference must resolve to a "
+ "section", inst->xlat_name);
+
+ goto error;
+ }
+
+ cs = cf_section_sub_find(cf_itemtosection(ci), "update");
+ if (!cs) {
+ radlog(L_ERR, "rlm_ldap (%s): Section must contain 'update' "
+ "subsection",
+ inst->xlat_name);
+
+ goto error;
+ }
+
+ /*
+ * Iterate over all the pairs, building our mods array
+ */
+ for (ci = cf_item_find_next(cs, NULL);
+ ci != NULL;
+ ci = cf_item_find_next(cs, ci)) {
+ int do_xlat = FALSE;
+
+ if (total == MAX_ATTRMAP) {
+ radlog(L_ERR, "rlm_ldap (%s): Modify map size exceeded",
+ inst->xlat_name);
+
+ goto error;
+ }
+
+ if (!cf_item_is_pair(ci)) {
+ radlog(L_ERR, "rlm_ldap (%s): Entry is not in "
+ "\"ldap-attribute = value\" format",
+ inst->xlat_name);
+
+ goto error;
+ }
+
+ /*
+ * Retrieve all the information we need about the pair
+ */
+ cp = cf_itemtopair(ci);
+ value = cf_pair_value(cp);
+ attr = cf_pair_attr(cp);
+ op = cf_pair_operator(cp);
+
+ if ((value == NULL) || (*value == '\0')) {
+ RDEBUG("empty value string, "
+ "skipping attribute \"%s\"", attr);
+
+ continue;
+ }
- conn = ldap_get_socket(inst);
- if (!conn) return RLM_MODULE_FAIL;
+ switch (cf_pair_value_type(cp))
+ {
+ case T_BARE_WORD:
+ case T_SINGLE_QUOTED_STRING:
+ break;
+ case T_BACK_QUOTED_STRING:
+ case T_DOUBLE_QUOTED_STRING:
+ do_xlat = TRUE;
+ break;
+ default:
+ rad_assert(0);
+ goto error;
+ }
+
+ if (op == T_OP_CMP_FALSE) {
+ passed[last_pass] = NULL;
+ } else if (do_xlat) {
+ p = rad_malloc(1024);
+ if (radius_xlat(p, 1024, value, request, NULL, NULL) <= 0) {
+ RDEBUG("xlat failed or empty value string, "
+ "skipping attribute \"%s\"", attr);
+
+ free(p);
+
+ continue;
+ }
+
+ expanded[last_exp++] = p;
+ passed[last_pass] = p;
+ /*
+ * Static strings
+ */
+ } else {
+ memcpy(&(passed[last_pass]), &value,
+ sizeof(passed[last_pass]));
+ }
+
+ passed[last_pass + 1] = NULL;
+
+ mod_s[total].mod_values = &(passed[last_pass]);
+
+ last_pass += 2;
+
+ switch (op)
+ {
+ /*
+ * T_OP_EQ is *NOT* supported, it is impossible to
+ * support because of the lack of transactions in LDAP
+ */
+ case T_OP_ADD:
+ mod_s[total].mod_op = LDAP_MOD_ADD;
+ break;
- RDEBUG("Login attempt by \"%s\" with password \"%s\"",
- request->username->vp_strvalue, vp->vp_strvalue);
+ case T_OP_SET:
+ mod_s[total].mod_op = LDAP_MOD_REPLACE;
+ break;
+
+ case T_OP_SUB:
+ case T_OP_CMP_FALSE:
+ mod_s[total].mod_op = LDAP_MOD_DELETE;
+ break;
+#ifdef LDAP_MOD_INCREMENT
+ case T_OP_INCRM:
+ mod_s[total].mod_op = LDAP_MOD_INCREMENT;
+ break;
+#endif
+ default:
+ radlog(L_ERR, "rlm_ldap (%s): Operator '%s' "
+ "is not supported for LDAP modify "
+ "operations", inst->xlat_name,
+ fr_int2str(fr_tokens, op, "¿unknown?"));
+
+ goto error;
+ }
+
+ /*
+ * Now we know the value is ok, copy the pointers into
+ * the ldapmod struct.
+ */
+ memcpy(&(mod_s[total].mod_type), &(attr),
+ sizeof(mod_s[total].mod_type));
+
+ mod_p[total] = &(mod_s[total]);
+ total++;
+ }
+
+ if (total == 0) {
+ module_rcode = RLM_MODULE_NOOP;
+ goto release;
+ }
+
+ mod_p[total] = NULL;
+
+ conn = ldap_get_socket(inst);
+ if (!conn) return RLM_MODULE_FAIL;
+
/*
- * Get the DN by doing a search.
+ * Perform all modifications as the default admin user.
*/
+ if (conn->rebound) {
+ ldap_errno = ldap_bind_wrapper(&conn, inst->login,
+ inst->password, TRUE);
+ if (ldap_errno != RLM_MODULE_OK) {
+ goto error;
+ }
+
+ rad_assert(conn != NULL);
+ conn->rebound = FALSE;
+ }
+
user_dn = get_userdn(&conn, request, &module_rcode);
if (!user_dn) {
- ldap_release_socket(inst, conn);
- return module_rcode;
+ module_rcode = RLM_MODULE_NOTFOUND;
+ goto release;
}
+
+ RDEBUG2("Modifying user object with DN \"%s\"", user_dn);
+ retry:
+ ldap_errno = ldap_modify_ext(conn->handle, user_dn, modify, NULL, NULL,
+ &msg_id);
+ if (ldap_errno != LDAP_SUCCESS) {
+ switch (process_ldap_errno(inst, &conn, "Modify"))
+ {
+ case LDAP_PROC_SUCCESS:
+ break;
+ case LDAP_PROC_REJECT:
+ case LDAP_PROC_ERROR:
+ goto error;
+ case LDAP_PROC_RETRY:
+ goto retry;
+ default:
+ rad_assert(0);
+ }
+ }
+
+ DEBUG3("rlm_ldap (%s): Waiting for modify result...", inst->xlat_name);
+ tv.tv_sec = inst->timeout;
+ tv.tv_usec = 0;
+
+ result:
+ rcode = ldap_result(conn->handle, msg_id, 1, &tv, &result);
+ ldap_msgfree(result);
+ if (rcode <= 0) {
+ switch (process_ldap_errno(inst, &conn, "Modify"))
+ {
+ case LDAP_PROC_SUCCESS:
+ break;
+ case LDAP_PROC_REJECT:
+ case LDAP_PROC_ERROR:
+ error:
+ module_rcode = RLM_MODULE_FAIL;
+ goto release;
+ case LDAP_PROC_RETRY:
+ goto result;
+ default:
+ rad_assert(0);
+ }
+ }
+
+ RDEBUG2("Modification successful!");
+
+ release:
/*
- * Bind as the user
- */
- conn->rebound = TRUE;
- module_rcode = ldap_bind_wrapper(&conn, user_dn,
- vp->vp_strvalue,
- NULL, TRUE);
- if (module_rcode == RLM_MODULE_OK) {
- RDEBUG("Bind as user \"%s\" was successful", user_dn);
+ * Free up any buffers we allocated for xlat expansion
+ */
+ for (i = 0; i < last_exp; i++) {
+ free(expanded[i]);
}
+
ldap_release_socket(inst, conn);
+
return module_rcode;
}
-#endif
+
+
+static rlm_rcode_t ldap_accounting(void *instance, REQUEST * request) {
+ ldap_instance *inst = instance;
+
+ if (inst->accounting) {
+ return user_modify(inst, request, inst->accounting);
+ }
+
+ return RLM_MODULE_NOOP;
+}
+
+
+/** Check the user's password against ldap database
+ *
+ */
+static rlm_rcode_t ldap_postauth(void *instance, REQUEST * request)
+{
+ ldap_instance *inst = instance;
+
+ if (inst->postauth) {
+ return user_modify(inst, request, inst->postauth);
+ }
+
+ return RLM_MODULE_NOOP;
+}
+
/* globally exported name */
module_t rlm_ldap = {
ldap_authenticate, /* authentication */
ldap_authorize, /* authorization */
NULL, /* preaccounting */
- NULL, /* accounting */
+ ldap_accounting, /* accounting */
NULL, /* checksimul */
NULL, /* pre-proxy */
NULL, /* post-proxy */
-#ifdef WITH_EDIR
ldap_postauth /* post-auth */
-#else
- NULL /* post-auth */
-#endif
},
};