If we don't re-connect, it's an error.
[freeradius.git] / src / modules / rlm_ldap / rlm_ldap.c
index b063e3e..d3fddbf 100644 (file)
@@ -1,10 +1,7 @@
 /*
- * 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-2012 The FreeRADIUS Server Project.
- *
- *   Copyright 2012 Alan DeKok <aland@freeradius.org>
- *   Copyright 2012 Arran Cudbard-Bell <a.cudbardb@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$")
 
@@ -66,16 +67,17 @@ typedef struct {
        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
@@ -329,7 +331,7 @@ typedef struct ldap_conn {
 } 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;
 
@@ -338,105 +340,170 @@ typedef struct rlm_ldap_result {
        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
 
 /** Create and return a new connection
  * This function is probably too big.
@@ -449,15 +516,13 @@ static void *ldap_conn_create(void *ctx)
        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",
@@ -467,7 +532,7 @@ static void *ldap_conn_create(void *ctx)
        } 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);
@@ -506,11 +571,13 @@ static void *ldap_conn_create(void *ctx)
                        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);
@@ -521,8 +588,7 @@ static void *ldap_conn_create(void *ctx)
        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",
@@ -594,22 +660,15 @@ static void *ldap_conn_create(void *ctx)
        }
 #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;
        }
 
@@ -625,7 +684,7 @@ 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;
 }
@@ -740,8 +799,6 @@ static int perform_search(ldap_instance *inst, REQUEST *request,
 {
        int             ldap_errno;
        int             count = 0;
-       int             reconnect = FALSE;
-       LDAP_CONN       *conn = *pconn;
        struct timeval  tv;
 
        /*
@@ -756,17 +813,15 @@ static int perform_search(ldap_instance *inst, REQUEST *request,
        /*
         *      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;
@@ -776,72 +831,28 @@ static int perform_search(ldap_instance *inst, REQUEST *request,
                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");
@@ -983,12 +994,12 @@ free_urldesc:
 }
 
 
-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";
@@ -999,7 +1010,7 @@ static char *get_userdn(LDAP_CONN **pconn, REQUEST *request, int *module_rcode)
 
        *module_rcode = RLM_MODULE_FAIL;
 
-       vp = pairfind(request->config_items, PW_LDAP_USERDN, 0);
+       vp = pairfind(request->config_items, PW_LDAP_USERDN, 0, TAG_ANY);
        if (vp) {
                *module_rcode = RLM_MODULE_OK;
                return vp->vp_strvalue;
@@ -1031,8 +1042,8 @@ static char *get_userdn(LDAP_CONN **pconn, REQUEST *request, int *module_rcode)
                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,
@@ -1041,8 +1052,8 @@ static char *get_userdn(LDAP_CONN **pconn, REQUEST *request, int *module_rcode)
                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,
@@ -1078,7 +1089,8 @@ static int ldap_groupcmp(void *instance, REQUEST *request,
                         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;
@@ -1309,7 +1321,7 @@ check_attr:
        ldap_msgfree(result);
        ldap_release_socket(inst, conn);
 
-       if (!found){
+       if (!found) {
                RDEBUG("User is not a member of specified group");
                return 1;
        }
@@ -1317,99 +1329,24 @@ check_attr:
        return 0;
 }
 
-/** Parse update section
- *
- * 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.
  *
  */
 static int ldap_detach(void *instance)
 {
        ldap_instance *inst = instance;
-
-       if (inst->postauth) free(inst->postauth);
-       if (inst->accounting) free(inst->accounting);
        
        fr_connection_pool_delete(inst->pool);
-       
+
        if (inst->user_map) {
                radius_mapfree(&inst->user_map);
        }
 
-       free(inst);
-
        return 0;
 }
 
 static int parse_sub_section(CONF_SECTION *parent, 
-                            UNUSED ldap_instance *inst,
+                            ldap_instance *inst,
                             ldap_acct_section_t **config,
                             rlm_components_t comp)
 {
@@ -1419,20 +1356,17 @@ static int parse_sub_section(CONF_SECTION *parent,
        
        cs = cf_section_sub_find(parent, name);
        if (!cs) {
-               radlog(L_INFO, "Couldn't find configuration for %s. "
-                      "Will return NOOP for calls from this section.", name);
+               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 = rad_calloc(sizeof(**config));
+       *config = talloc_zero(inst, ldap_acct_section_t);
        if (cf_section_parse(cs, *config, acct_section_config) < 0) {
-               radlog(L_ERR, "Failed parsing configuration for section %s",
-                      name);
-               
-               free(*config);
-               *config = NULL;
-               
+               radlog(L_ERR, "rlm_ldap (%s): Failed parsing configuration for "
+                      "section %s", inst->xlat_name, name);
                return -1;
        }
                
@@ -1441,6 +1375,65 @@ static int parse_sub_section(CONF_SECTION *parent,
        return 0;
 }
 
+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.
@@ -1448,9 +1441,10 @@ static int parse_sub_section(CONF_SECTION *parent,
 static int ldap_instantiate(CONF_SECTION * conf, void **instance)
 {
        ldap_instance *inst;
-       CONF_SECTION *cs;
 
-       inst = rad_calloc(sizeof *inst);
+       *instance = inst = talloc_zero(conf, ldap_instance);
+       if (!inst) return -1;
+
        inst->cs = conf;
 
        inst->chase_referrals = 2; /* use OpenLDAP defaults */
@@ -1460,8 +1454,6 @@ static int ldap_instantiate(CONF_SECTION * conf, void **instance)
        if (!inst->xlat_name) {
                inst->xlat_name = cf_section_name1(conf);
        }
-               
-       rad_assert(inst->xlat_name);
 
        /*
         *      If the configuration parameters can't be parsed, then fail.
@@ -1525,11 +1517,8 @@ static int ldap_instantiate(CONF_SECTION * conf, void **instance)
        /*
         *      Build the attribute map
         */
-       cs = cf_section_sub_find(conf, "update");
-       if (cs) {       
-               if (build_attrmap(cs, &(inst->user_map)) < 0) {
-                       goto error;
-               }
+       if (ldap_map_verify(inst, &(inst->user_map)) < 0) {
+               goto error;
        }
 
        /*
@@ -1537,7 +1526,7 @@ static int ldap_instantiate(CONF_SECTION * conf, void **instance)
         */
        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];
 
@@ -1571,10 +1560,9 @@ static int ldap_instantiate(CONF_SECTION * conf, void **instance)
                return -1;
        }
        
-       *instance = inst;
        return 0;
-       
-       error:
+
+error:
        ldap_detach(inst);
        return -1;
 }
@@ -1614,7 +1602,7 @@ 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;
@@ -1632,7 +1620,7 @@ static VALUE_PAIR *ldap_getvalue(REQUEST *request, const VALUE_PAIR_TMPL *dst,
         *      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]);
@@ -1647,7 +1635,7 @@ static VALUE_PAIR *ldap_getvalue(REQUEST *request, const VALUE_PAIR_TMPL *dst,
 
 static void xlat_attrsfree(const xlat_attrs_t *expanded)
 {
-       const VALUE_PAIR_MAP *map;
+       const value_pair_map_t *map;
        unsigned int total = 0;
        
        const char *name;
@@ -1657,44 +1645,81 @@ static void xlat_attrsfree(const xlat_attrs_t *expanded)
                name = expanded->attrs[total++];
                if (!name) return;
                
-               if (map->src->do_xlat) {
+               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;
@@ -1716,7 +1741,7 @@ static void do_attrmap(UNUSED ldap_instance *inst, REQUEST *request,
                       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;
@@ -1764,19 +1789,15 @@ static void do_check_reply(ldap_instance *inst, REQUEST *request)
        *       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?");
+               }
        }
 }
 
@@ -1826,7 +1847,7 @@ free_result:
 /** 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;
@@ -1868,14 +1889,15 @@ static int ldap_authorize(void *instance, REQUEST * request)
                       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);
@@ -1928,8 +1950,7 @@ static int ldap_authorize(void *instance, REQUEST * request)
        /*
         *      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;
        }
 
@@ -1955,13 +1976,12 @@ static int ldap_authorize(void *instance, REQUEST * 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 "
@@ -1972,7 +1992,7 @@ static int ldap_authorize(void *instance, REQUEST * request)
                        conn->rebound = TRUE;
                        module_rcode = ldap_bind_wrapper(&conn, user_dn,
                                                         vp->vp_strvalue,
-                                                        NULL, TRUE);
+                                                        TRUE);
                        if (module_rcode != RLM_MODULE_OK) {
                                goto free_result;
                        }
@@ -1997,7 +2017,7 @@ skip_edir:
        /*
         *      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;
 
@@ -2043,9 +2063,9 @@ free_socket:
 /** 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;
@@ -2072,10 +2092,10 @@ static int ldap_authenticate(void *instance, REQUEST * request)
                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;
        }
 
@@ -2086,12 +2106,12 @@ static int ldap_authenticate(void *instance, REQUEST * request)
                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.
         */
@@ -2107,7 +2127,7 @@ static int ldap_authenticate(void *instance, REQUEST * request)
        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);
        }
@@ -2119,16 +2139,16 @@ static int ldap_authenticate(void *instance, REQUEST * request)
 /** Modify user's object in LDAP
  *
  */
-static int user_modify(ldap_instance *inst, REQUEST *request,
-                      ldap_acct_section_t *section)
+static rlm_rcode_t user_modify(ldap_instance *inst, REQUEST *request,
+                              ldap_acct_section_t *section)
 {
-       int             module_rcode = RLM_MODULE_OK;
-       int             ldap_errno;
-       const char      *error_string;
+       rlm_rcode_t     module_rcode = RLM_MODULE_OK;
+       int             ldap_errno, rcode, msg_id;
+       LDAPMessage     *result = NULL;
        
-       LDAP_CONN       *conn;
+       LDAP_CONN       *conn = NULL;
        
-       LDAPMod         *mod_p[MAX_ATTRMAP], mod_s[MAX_ATTRMAP];
+       LDAPMod         *mod_p[MAX_ATTRMAP + 1], mod_s[MAX_ATTRMAP];
        LDAPMod         **modify = mod_p;
        
        char            *passed[MAX_ATTRMAP * 2];
@@ -2137,14 +2157,13 @@ static int user_modify(ldap_instance *inst, REQUEST *request,
        char            *expanded[MAX_ATTRMAP];
        int             last_exp = 0;
        
+       struct timeval  tv;
+       
        const char      *attr;
        const char      *value;
        
        const char      *user_dn;
 
-       conn = ldap_get_socket(inst);
-       if (!conn) return RLM_MODULE_FAIL;
-
        /*
         *      Build our set of modifications using the update sections in
         *      the config.
@@ -2215,8 +2234,21 @@ static int user_modify(ldap_instance *inst, REQUEST *request,
                        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;
+               }
+
                switch (cf_pair_value_type(cp))
                {
                        case T_BARE_WORD:
@@ -2231,28 +2263,26 @@ static int user_modify(ldap_instance *inst, REQUEST *request,
                                goto error;
                }
                
-               attr = cf_pair_attr(cp);
-               value = cf_pair_value(cp);
-               
-               if (do_xlat) {
-                       p = rad_malloc(1024);   
-                       
-                       if (radius_xlat(p, 1024, value, request,
-                                       NULL, NULL) == 0) {
-                               RDEBUG("xlat failed or expanded to empty "
-                                      "string, skipping attribute \"%s\"",
-                                      attr);
-                       
+               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;                               
+                               continue;
                        }
                        
                        expanded[last_exp++] = p;
-                       
                        passed[last_pass] = p;
+               /* 
+                *      Static strings
+                */
                } else {
-                       memcpy(&(passed[last_pass]), value,
+                       memcpy(&(passed[last_pass]), &value,
                               sizeof(passed[last_pass]));
                }
                
@@ -2262,51 +2292,66 @@ static int user_modify(ldap_instance *inst, REQUEST *request,
                                        
                last_pass += 2;
                
+               switch (op)
+               {
                /*
-                *      Now we know the value is ok, copy the pointers into
-                *      the ldapmod struct.
+                *  T_OP_EQ is *NOT* supported, it is impossible to
+                *  support because of the lack of transactions in LDAP
                 */
-               memcpy(&(mod_s[total].mod_type), &(attr),
-                      sizeof(mod_s[total].mod_type));
+               case T_OP_ADD:
+                       mod_s[total].mod_op = LDAP_MOD_ADD;
+                       break;
 
-               op = cf_pair_operator(cp);
-               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;
+               case T_OP_SET:
+                       mod_s[total].mod_op = LDAP_MOD_REPLACE;
                        break;
-                       case T_OP_SET:
-                               mod_s[total].mod_op = LDAP_MOD_REPLACE;
+
+               case T_OP_SUB:
+               case T_OP_CMP_FALSE:
+                       mod_s[total].mod_op = LDAP_MOD_DELETE;
                        break;
-                       case T_OP_SUB:
-                               mod_s[total].mod_op = LDAP_MOD_DELETE;
+
+#ifdef LDAP_MOD_INCREMENT
+               case T_OP_INCRM:
+                       mod_s[total].mod_op = LDAP_MOD_INCREMENT;
                        break;
-                       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;
+#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;
+       
        /*
         *      Perform all modifications as the default admin user.
         */
        if (conn->rebound) {
-               ldap_errno = ldap_bind_wrapper(&conn,
-                                              inst->login, inst->password,
-                                              NULL, TRUE);
+               ldap_errno = ldap_bind_wrapper(&conn, inst->login,
+                                              inst->password, TRUE);
                if (ldap_errno != RLM_MODULE_OK) {
                        goto error;
                }
@@ -2322,29 +2367,52 @@ static int user_modify(ldap_instance *inst, REQUEST *request,
        }
        
        RDEBUG2("Modifying user object with DN \"%s\"", user_dn);
-       
-       ldap_errno = ldap_modify_ext_s(conn->handle, user_dn, modify, NULL,
-                                      NULL);
-                            
-       if (ldap_errno < 0) {
-               ldap_get_option(conn->handle, LDAP_OPT_ERROR_NUMBER,
-                               &ldap_errno);
-                               
-               error_string = ldap_err2string(ldap_errno);
-
-               radlog(L_ERR, "rlm_ldap (%s): Modifying DN \"%s\" failed: %s",
-                      inst->xlat_name, user_dn, error_string);
-               
-       error:
-               module_rcode = RLM_MODULE_FAIL;
-               
-               goto release;
+       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:
-       
        /*
         *      Free up any buffers we allocated for xlat expansion
         */     
@@ -2352,13 +2420,14 @@ static int user_modify(ldap_instance *inst, REQUEST *request,
                free(expanded[i]);
        }
        
+
        ldap_release_socket(inst, conn);
        
        return module_rcode;
 }
 
 
-static int ldap_accounting(void *instance, REQUEST * request) {
+static rlm_rcode_t ldap_accounting(void *instance, REQUEST * request) {
        ldap_instance *inst = instance;         
 
        if (inst->accounting) {
@@ -2372,7 +2441,7 @@ static int ldap_accounting(void *instance, REQUEST * request) {
 /** Check the user's password against ldap database
  *
  */
-static int ldap_postauth(void *instance, REQUEST * request)
+static rlm_rcode_t ldap_postauth(void *instance, REQUEST * request)
 {
        ldap_instance   *inst = instance;