If we don't re-connect, it's an error.
[freeradius.git] / src / modules / rlm_ldap / rlm_ldap.c
index 7dc3ba3..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 2004,2006 The FreeRADIUS Server Project.
+ * @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$")
 
-#include <freeradius-devel/radiusd.h>
-#include <freeradius-devel/modules.h>
+#include       <freeradius-devel/radiusd.h>
+#include       <freeradius-devel/modules.h>
 #include       <freeradius-devel/rad_assert.h>
 
-#include       <pwd.h>
+#include       <stdarg.h>
 #include       <ctype.h>
 
 #include       <lber.h>
-#include        <ldap.h>
+#include       <ldap.h>
 
-#ifndef HAVE_PTHREAD_H
-/*
- *      This is a lot simpler than putting ifdef's around
- *      every use of the pthread functions.
- */
-#define pthread_mutex_lock(a)
-#define pthread_mutex_trylock(a) (0)
-#define pthread_mutex_unlock(a)
-#define pthread_mutex_init(a,b)
-#define pthread_mutex_destroy(a)
-#else
-#include       <pthread.h>
-#endif
+#define MAX_ATTRMAP            128
+#define MAX_ATTR_STR_LEN       256
+#define MAX_FILTER_STR_LEN     1024
 
+#ifdef WITH_EDIR
+extern int nmasldap_get_password(LDAP *ld,char *objectDN, char *pwd, size_t *pwdSize);
 
-#define MAX_FILTER_STR_LEN     1024
-#define TIMELIMIT 5
+#endif
 
-/*
- * These are used in case ldap_search returns LDAP_SERVER_DOWN
- * In that case we do conn->failed_conns++ and then check it:
- * If conn->failed_conns <= MAX_FAILED_CONNS_START then we try
- * to reconnect
- * conn->failed_conns is also checked on entrance in perform_search:
- * If conn->failed_conns > MAX_FAILED_CONNS_START then we don't
- * try to do anything and we just do conn->failed_conns++ and
- * return RLM_MODULE_FAIL
- * if conn->failed_conns >= MAX_FAILED_CONNS_END then we give it
- * another chance and we set it to MAX_FAILED_CONNS_RESTART and
- * try to reconnect.
- *
- *
- * We are assuming that the majority of the LDAP_SERVER_DOWN cases
- * will either be an ldap connection timeout or a temporary ldap
- * server problem.
- * As a result we make a few attempts to reconnect hoping that the problem
- * will soon go away. If it does not go away then we just return
- * RLM_MODULE_FAIL on entrance in perform_search until conn->failed_conns
- * gets to MAX_FAILED_CONNS_END. After that we give it one more chance by
- * going back to MAX_FAILED_CONNS_RESTART
- *
- */
+typedef struct ldap_acct_section {
+       CONF_SECTION    *cs;
+       
+       const char *reference;
+} ldap_acct_section_t;
 
-#define MAX_FAILED_CONNS_END           20
-#define MAX_FAILED_CONNS_RESTART       4
-#define MAX_FAILED_CONNS_START         5
 
-#ifdef NOVELL_UNIVERSAL_PASSWORD
+typedef struct {
+       CONF_SECTION    *cs;
+       fr_connection_pool_t *pool;
 
-/* Universal Password Length */
-#define UNIVERSAL_PASS_LEN 256
+       char            *server;
+       int             port;
 
-int nmasldap_get_password(
-       LDAP     *ld,
-       char     *objectDN,
-       size_t   *pwdSize,      /* in bytes */
-       char     *pwd );
+       char            *login;
+       char            *password;
 
-#endif
+       char            *filter;
+       char            *basedn;
 
-#ifdef NOVELL
+       int             chase_referrals;
+       int             rebind;
 
-#define REQUEST_ACCEPTED   0
-#define REQUEST_CHALLENGED 1
-#define REQUEST_REJECTED   2
-#define MAX_CHALLENGE_LEN  128
+       int             ldap_debug; //!< Debug flag for the SDK.
 
-int radLdapXtnNMASAuth( LDAP *, char *, char *, char *, char *, size_t *, char *, int * );
+       const char      *xlat_name; //!< Instance name.
 
-#endif
+       int             expect_password;
+       
+       /*
+        *      RADIUS attribute to LDAP attribute maps
+        */
+       value_pair_map_t *user_map; //!< Attribute map applied to users and
+                                   //!< profiles.
+       
+       /*
+        *      Access related configuration
+        */
+       char            *access_attr;
+       int             positive_access_attr;
 
-/* linked list of mappings between RADIUS attributes and LDAP attributes */
-struct TLDAP_RADIUS {
-       char*                 attr;
-       char*                 radius_attr;
-       FR_TOKEN              operator;
-       struct TLDAP_RADIUS*  next;
-};
-typedef struct TLDAP_RADIUS TLDAP_RADIUS;
+       /*
+        *      Profiles
+        */
+       char            *base_filter;
+       char            *default_profile;
+       char            *profile_attr;
 
-typedef struct ldap_conn {
-       LDAP            *ld;
-       char            bound;
-       char            locked;
-       int             failed_conns;
-       int             uses;
-#ifdef HAVE_PTHREAD_H
-       pthread_mutex_t mutex;
-#endif
-} LDAP_CONN;
+       /*
+        *      Group checking.
+        */
+       char            *groupname_attr;
+       char            *groupmemb_filter;
+       char            *groupmemb_attr;
+       
+       /*
+        *      Accounting
+        */
+       ldap_acct_section_t *postauth;
+       ldap_acct_section_t *accounting;
 
-typedef struct {
-       CONF_SECTION   *cs;
-       char           *server;
-       int             port;
-       int             timelimit;
-       int             max_uses;
-       int             net_timeout;
-       int             timeout;
-       int             debug;
-       int             tls_mode;
+       /*
+        *      TLS items.  We should really normalize these with the
+        *      TLS code in 3.0.
+        */
+       int             tls_mode;
        int             start_tls;
-       int             num_conns;
-       int             do_comp;
-       int             do_xlat;
-       int             default_allow;
-       int             failed_conns;
-       int             is_url;
-       int             chase_referrals;
-       int             rebind;
-       char           *login;
-       char           *password;
-       char           *filter;
-       char           *base_filter;
-       char           *basedn;
-       char           *default_profile;
-       char           *profile_attr;
-       char           *access_attr;
-       char           *passwd_attr;
-       char           *dictionary_mapping;
-       char           *groupname_attr;
-       char           *groupmemb_filt;
-       char           *groupmemb_attr;
-       char            **atts;
-       TLDAP_RADIUS   *check_item_map;
-       TLDAP_RADIUS   *reply_item_map;
-       LDAP_CONN       *conns;
-#ifdef NOVELL
-       LDAP_CONN *apc_conns;
-#endif
-       int             ldap_debug; /* Debug flag for LDAP SDK */
-       char            *xlat_name; /* name used to xlat */
-       char            *auth_type;
        char            *tls_cacertfile;
        char            *tls_cacertdir;
        char            *tls_certfile;
        char            *tls_keyfile;
        char            *tls_randfile;
        char            *tls_require_cert;
-#ifdef NOVELL
-       int              edir_account_policy_check;
-#endif
-       int              set_auth_type;
 
        /*
+        *      Options
+        */
+       int             timelimit;
+       int             net_timeout;
+       int             timeout;
+       int             is_url;
+
+#ifdef WITH_EDIR
+       /*
+        *      eDir support
+        */
+       int             edir;
+       int             edir_autz;
+#endif
+       /*
         *      For keep-alives.
         */
 #ifdef LDAP_OPT_X_KEEPALIVE_IDLE
@@ -193,22 +151,9 @@ typedef struct {
 /* The default setting for TLS Certificate Verification */
 #define TLS_DEFAULT_VERIFY "allow"
 
-#if defined(LDAP_OPT_X_KEEPALIVE_IDLE) || defined(LDAP_OPT_X_KEEPALIVE_PROBES) || defined (LDAP_OPT_ERROR_NUMBER)
-static CONF_PARSER keepalive_config[] = {
-#ifdef LDAP_OPT_X_KEEPALIVE_IDLE
-       {"idle", PW_TYPE_INTEGER, offsetof(ldap_instance,keepalive_idle), NULL, "60"},
-#endif
-#ifdef LDAP_OPT_X_KEEPALIVE_PROBES
-       {"probes", PW_TYPE_INTEGER, offsetof(ldap_instance,keepalive_probes), NULL, "3"},
-#endif
-#ifdef LDAP_OPT_ERROR_NUMBER
-       {"interval", PW_TYPE_INTEGER, offsetof(ldap_instance,keepalive_interval), NULL, "30"},
-#endif
-
-       { NULL, -1, 0, NULL, NULL }
-};
-#endif                         /* KEEPALIVE */
-
+/*
+ *     TLS Configuration
+ */
 static CONF_PARSER tls_config[] = {
        {"start_tls", PW_TYPE_BOOLEAN,
         offsetof(ldap_instance,start_tls), NULL, "no"},
@@ -227,739 +172,578 @@ static CONF_PARSER tls_config[] = {
        { NULL, -1, 0, NULL, NULL }
 };
 
-static const CONF_PARSER module_config[] = {
-       {"server", PW_TYPE_STRING_PTR,
-        offsetof(ldap_instance,server), NULL, "localhost"},
-       {"port", PW_TYPE_INTEGER,
-        offsetof(ldap_instance,port), NULL, "389"},
-       {"password", PW_TYPE_STRING_PTR,
-        offsetof(ldap_instance,password), NULL, ""},
-       {"identity", PW_TYPE_STRING_PTR,
-        offsetof(ldap_instance,login), NULL, ""},
-
-       /*
-        *      Timeouts & stuff.
-        */
-       /* wait forever on network activity */
-       {"net_timeout", PW_TYPE_INTEGER,
-        offsetof(ldap_instance,net_timeout), NULL, "10"},
-       /* wait forever for search results */
-       {"timeout", PW_TYPE_INTEGER,
-        offsetof(ldap_instance,timeout), NULL, "20"},
-       /* allow server unlimited time for search (server-side limit) */
-       {"timelimit", PW_TYPE_INTEGER,
-        offsetof(ldap_instance,timelimit), NULL, "20"},
-       /* how many times the connection can be used before being re-established */     
-       {"max_uses", PW_TYPE_INTEGER,
-        offsetof(ldap_instance,max_uses), NULL, "0"},
 
+static CONF_PARSER attr_config[] = {
        /*
-        *      TLS configuration  The first few are here for backwards
-        *      compatibility.  The last is the new subsection.
+        *      Access limitations
         */
-       {"tls_mode", PW_TYPE_BOOLEAN | PW_TYPE_DEPRECATED,
-        offsetof(ldap_instance,tls_mode), NULL, "no"},
-
-       {"start_tls", PW_TYPE_BOOLEAN | PW_TYPE_DEPRECATED,
-        offsetof(ldap_instance,start_tls), NULL, "no"},
-       {"tls_cacertfile", PW_TYPE_FILENAME | PW_TYPE_DEPRECATED,
-        offsetof(ldap_instance,tls_cacertfile), NULL, NULL},
-       {"tls_cacertdir", PW_TYPE_FILENAME | PW_TYPE_DEPRECATED,
-        offsetof(ldap_instance,tls_cacertdir), NULL, NULL},
-       {"tls_certfile", PW_TYPE_FILENAME | PW_TYPE_DEPRECATED,
-        offsetof(ldap_instance,tls_certfile), NULL, NULL},
-       {"tls_keyfile", PW_TYPE_FILENAME | PW_TYPE_DEPRECATED,
-        offsetof(ldap_instance,tls_keyfile), NULL, NULL},
-       {"tls_randfile", PW_TYPE_STRING_PTR | PW_TYPE_DEPRECATED, /* OK if it changes on HUP */
-        offsetof(ldap_instance,tls_randfile), NULL, NULL},
-       {"tls_require_cert", PW_TYPE_STRING_PTR | PW_TYPE_DEPRECATED,
-        offsetof(ldap_instance,tls_require_cert), NULL, TLS_DEFAULT_VERIFY},
-       { "tls", PW_TYPE_SUBSECTION, 0, NULL, (const void *) tls_config },
+       /* LDAP attribute name that controls remote access */
+       {"access_attr", PW_TYPE_STRING_PTR,
+        offsetof(ldap_instance,access_attr), NULL, NULL},
+       {"positive_access_attr", PW_TYPE_BOOLEAN,
+        offsetof(ldap_instance,positive_access_attr), NULL, "yes"},
 
-       /*
-        *      DN's and filters.
-        */
-       {"basedn", PW_TYPE_STRING_PTR,
-        offsetof(ldap_instance,basedn), NULL, "o=notexist"},
-       {"filter", PW_TYPE_STRING_PTR,
-        offsetof(ldap_instance,filter), NULL, "(uid=%u)"},
        {"base_filter", PW_TYPE_STRING_PTR,
-        offsetof(ldap_instance,base_filter), NULL, "(objectclass=radiusprofile)"},
+        offsetof(ldap_instance,base_filter), NULL,
+        "(objectclass=radiusprofile)"},
        {"default_profile", PW_TYPE_STRING_PTR,
         offsetof(ldap_instance,default_profile), NULL, NULL},
        {"profile_attribute", PW_TYPE_STRING_PTR,
         offsetof(ldap_instance,profile_attr), NULL, NULL},
 
-       /*
-        *      Getting passwords from the database
-        */
-       {"password_attribute", PW_TYPE_STRING_PTR,
-        offsetof(ldap_instance,passwd_attr), NULL, NULL},
+       { NULL, -1, 0, NULL, NULL }
+};
 
-       /*
-        *      Access limitations
-        */
-       /* LDAP attribute name that controls remote access */
-       {"access_attr", PW_TYPE_STRING_PTR,
-        offsetof(ldap_instance,access_attr), NULL, NULL},
-       {"access_attr_used_for_allow", PW_TYPE_BOOLEAN,
-        offsetof(ldap_instance,default_allow), NULL, "yes"},
-       {"chase_referrals", PW_TYPE_BOOLEAN,
-        offsetof(ldap_instance,chase_referrals), NULL, NULL},
-       {"rebind", PW_TYPE_BOOLEAN,
-        offsetof(ldap_instance,rebind), NULL, NULL},
 
+/*
+ *     Group configuration
+ */
+static CONF_PARSER group_config[] = {
        /*
         *      Group checks.  These could probably be done
         *      via dynamic xlat's.
         */
-       {"groupname_attribute", PW_TYPE_STRING_PTR,
+       {"name_attribute", PW_TYPE_STRING_PTR,
         offsetof(ldap_instance,groupname_attr), NULL, "cn"},
-       {"groupmembership_filter", PW_TYPE_STRING_PTR,
-        offsetof(ldap_instance,groupmemb_filt), NULL, "(|(&(objectClass=GroupOfNames)(member=%{Ldap-UserDn}))(&(objectClass=GroupOfUniqueNames)(uniquemember=%{Ldap-UserDn})))"},
-       {"groupmembership_attribute", PW_TYPE_STRING_PTR,
+       {"membership_filter", PW_TYPE_STRING_PTR,
+        offsetof(ldap_instance,groupmemb_filter), NULL,
+        "(|(&(objectClass=GroupOfNames)(member=%{Ldap-UserDn}))"
+        "(&(objectClass=GroupOfUniqueNames)(uniquemember=%{Ldap-UserDn})))"},
+       {"membership_attribute", PW_TYPE_STRING_PTR,
         offsetof(ldap_instance,groupmemb_attr), NULL, NULL},
 
-       /* file with mapping between LDAP and RADIUS attributes */
-       {"dictionary_mapping", PW_TYPE_FILENAME,
-        offsetof(ldap_instance,dictionary_mapping), NULL, "${confdir}/ldap.attrmap"},
 
+       { NULL, -1, 0, NULL, NULL }
+};
+
+/*
+ *     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!
+ */
+static CONF_PARSER option_config[] = {
        /*
         *      Debugging flags to the server
         */
        {"ldap_debug", PW_TYPE_INTEGER,
         offsetof(ldap_instance,ldap_debug), NULL, "0x0000"},
-       {"ldap_connections_number", PW_TYPE_INTEGER,
-        offsetof(ldap_instance,num_conns), NULL, "5"},
-       {"compare_check_items", PW_TYPE_BOOLEAN,
-        offsetof(ldap_instance,do_comp), NULL, "no"},
-       {"do_xlat", PW_TYPE_BOOLEAN,
-        offsetof(ldap_instance,do_xlat), NULL, "yes"},
-
-#ifdef NOVELL
+
+       {"chase_referrals", PW_TYPE_BOOLEAN,
+        offsetof(ldap_instance,chase_referrals), NULL, NULL},
+
+       {"rebind", PW_TYPE_BOOLEAN,
+        offsetof(ldap_instance,rebind), NULL, NULL},
+
+       /* timeout on network activity */
+       {"net_timeout", PW_TYPE_INTEGER,
+        offsetof(ldap_instance,net_timeout), NULL, "10"},
+
+       /* timeout for search results */
+       {"timeout", PW_TYPE_INTEGER,
+        offsetof(ldap_instance,timeout), NULL, "20"},
+
+       /* allow server unlimited time for search (server-side limit) */
+       {"timelimit", PW_TYPE_INTEGER,
+        offsetof(ldap_instance,timelimit), NULL, "20"},
+
+#ifdef LDAP_OPT_X_KEEPALIVE_IDLE
+       {"idle", PW_TYPE_INTEGER,
+        offsetof(ldap_instance,keepalive_idle), NULL, "60"},
+#endif
+#ifdef LDAP_OPT_X_KEEPALIVE_PROBES
+       {"probes", PW_TYPE_INTEGER,
+        offsetof(ldap_instance,keepalive_probes), NULL, "3"},
+#endif
+#ifdef LDAP_OPT_ERROR_NUMBER
+       {"interval", PW_TYPE_INTEGER, 
+        offsetof(ldap_instance,keepalive_interval), NULL, "30"},
+#endif
+       { NULL, -1, 0, NULL, NULL }
+};
+
+
+static const CONF_PARSER module_config[] = {
+       {"server", PW_TYPE_STRING_PTR,
+        offsetof(ldap_instance,server), NULL, "localhost"},
+       {"port", PW_TYPE_INTEGER,
+        offsetof(ldap_instance,port), NULL, "389"},
+
+       {"password", PW_TYPE_STRING_PTR,
+        offsetof(ldap_instance,password), NULL, ""},
+       {"identity", PW_TYPE_STRING_PTR,
+        offsetof(ldap_instance,login), NULL, ""},
+
        /*
-        *      Novell magic.
+        *      DN's and filters.
         */
-       {"edir_account_policy_check", PW_TYPE_BOOLEAN,
-        offsetof(ldap_instance,edir_account_policy_check), NULL, "yes"},
-#endif
+       {"basedn", PW_TYPE_STRING_PTR,
+        offsetof(ldap_instance,basedn), NULL, "o=notexist"},
 
-       {"set_auth_type", PW_TYPE_BOOLEAN, offsetof(ldap_instance,set_auth_type), NULL, "yes"},
+       {"filter", PW_TYPE_STRING_PTR,
+        offsetof(ldap_instance,filter), NULL, "(uid=%u)"},
 
-#if defined(LDAP_OPT_X_KEEPALIVE_IDLE) || defined(LDAP_OPT_X_KEEPALIVE_PROBES) || defined (LDAP_OPT_ERROR_NUMBER)
-       { "keepalive", PW_TYPE_SUBSECTION, 0, NULL, (const void *) keepalive_config },
+       /* turn off the annoying warning if we don't expect a password */
+       {"expect_password", PW_TYPE_BOOLEAN,
+        offsetof(ldap_instance,expect_password), NULL, "yes"},
+        
+#ifdef WITH_EDIR
+       /* 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
 
+       /*
+        *      Terrible things which should be deleted.
+        */
+       { "profiles", PW_TYPE_SUBSECTION, 0, NULL, (const void *) attr_config },
+
+       { "group", PW_TYPE_SUBSECTION, 0, NULL, (const void *) group_config },
+
+       { "options", PW_TYPE_SUBSECTION, 0, NULL,
+        (const void *) option_config },
+
+       { "tls", PW_TYPE_SUBSECTION, 0, NULL, (const void *) tls_config },
+
        {NULL, -1, 0, NULL, NULL}
 };
 
-#define ld_valid                ld_options.ldo_valid
-#define LDAP_VALID_SESSION      0x2
-#define LDAP_VALID(ld)  ( (ld)->ld_valid == LDAP_VALID_SESSION )
+typedef struct ldap_conn {
+       LDAP    *handle;
+       int     rebound;
+       int     referred;
+       ldap_instance *inst;
+} LDAP_CONN;
 
-#ifdef FIELDCPY
-static void     fieldcpy(char *, char **);
-#endif
-static VALUE_PAIR *ldap_pairget(LDAP *, LDAPMessage *, TLDAP_RADIUS *,VALUE_PAIR **,int, ldap_instance *);
-static int ldap_groupcmp(void *, REQUEST *, VALUE_PAIR *, VALUE_PAIR *, VALUE_PAIR *, VALUE_PAIR **);
-static size_t ldap_xlat(void *, REQUEST *, const char *, char *, size_t);
-static LDAP    *ldap_connect(void *instance, const char *, const char *, int, int *, char **);
-static int     read_mappings(ldap_instance* inst);
-
-static inline int ldap_get_conn(LDAP_CONN *conns,LDAP_CONN **ret,
-                               ldap_instance *inst)
+typedef struct xlat_attrs {
+       const value_pair_map_t *maps;
+       const char *attrs[MAX_ATTRMAP];
+} xlat_attrs_t;
+
+typedef struct rlm_ldap_result {
+       char    **values;
+       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;
+
+static ldap_rcode_t process_ldap_errno(ldap_instance *inst, LDAP_CONN **pconn,
+                             const char *operation)
 {
-       register int i = 0;
-
-       for(i=0;i<inst->num_conns;i++){
-               DEBUG("  [%s] ldap_get_conn: Checking Id: %d",
-                     inst->xlat_name, i);
-               if ((pthread_mutex_trylock(&conns[i].mutex) == 0)) {
-                       if (conns[i].locked == 1) {
-                               /* connection is already being used */
-                               pthread_mutex_unlock(&(conns[i].mutex));
-                               continue;
-                       }
-                       /* found an unused connection */
-                       *ret = &conns[i];
-                       conns[i].uses++;
-                       conns[i].locked = 1;
-                       DEBUG("  [%s] ldap_get_conn: Got Id: %d",
-                             inst->xlat_name, i);
-                       return i;
-               }
-       }
+       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;
 
-       return -1;
-}
+       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;
 
-static inline void ldap_release_conn(int i, ldap_instance *inst)
-                                    
-{
-       LDAP_CONN *conns = inst->conns;
+       case LDAP_FILTER_ERROR:
+               radlog(L_ERR, "rlm_ldap (%s): %s failed: Bad search "
+                      "filter", inst->xlat_name, operation);
+               return LDAP_PROC_ERROR;
 
-       DEBUG("  [%s] ldap_release_conn: Release Id: %d", inst->xlat_name, i);
-       if ((inst->max_uses > 0) && (conns[i].uses >= inst->max_uses)) {
-               if (conns[i].ld){
-                       DEBUG("  [%s] ldap_release_conn: Hit max usage limit, closing Id: %d", inst->xlat_name, i);
-                       ldap_unbind_s(conns[i].ld);
-                       
-                       conns[i].ld = NULL;     
-               }
-               conns[i].bound = 0;
-               conns[i].uses = 0;
+       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;
        }
-       conns[i].locked = 0;
-       pthread_mutex_unlock(&(conns[i].mutex));
 }
 
-#ifdef NOVELL
-static inline void ldap_release_apc_conn(int i, ldap_instance *inst)
-                                    
+
+static int ldap_bind_wrapper(LDAP_CONN **pconn, const char *user,
+                            const char *password, int retry)
 {
-       LDAP_CONN *conns = inst->apc_conns;
+       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;
 
-       DEBUG("  [%s] ldap_release_conn: Release Id: %d", inst->xlat_name, i);
-       conns[i].locked = 0;
-       pthread_mutex_unlock(&(conns[i].mutex));
-}
-#endif
+retry:
+       msg_id = ldap_bind(conn->handle, user, password, LDAP_AUTH_SIMPLE);
+       if (msg_id < 0) goto get_error;
 
-/*************************************************************************
- *
- *     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)
-{
-       ldap_instance  *inst;
-       int i = 0;
-       int atts_num = 0;
-       int reply_map_num = 0;
-       int check_map_num = 0;
-       int att_map[3] = {0,0,0};
-       TLDAP_RADIUS *pair;
-       ATTR_FLAGS flags;
-       const char *xlat_name;
-
-       inst = rad_malloc(sizeof *inst);
-       if (!inst) {
-               return -1;
-       }
-       memset(inst, 0, sizeof(*inst));
-       inst->chase_referrals = 2; /* use OpenLDAP defaults */
-       inst->rebind = 2;
-       inst->cs = conf;
+       DEBUG3("rlm_ldap (%s): Waiting for bind result...", inst->xlat_name);
 
-       if (cf_section_parse(conf, inst, module_config) < 0) {
-               free(inst);
-               return -1;
-       }
+       tv.tv_sec = inst->timeout;
+       tv.tv_usec = 0;
 
-       if (inst->server == NULL) {
-               radlog(L_ERR, "rlm_ldap: missing 'server' directive.");
-               free(inst);     /* FIXME: detach */
-               return -1;
-       }
-       inst->is_url = 0;
-       if (ldap_is_ldap_url(inst->server)){
+       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
-               inst->is_url = 1;
-               inst->port = 0;
-#else
-               radlog(L_ERR, "rlm_ldap: 'server' directive is in URL form but ldap_initialize() is not available.");
-               free(inst);     /* FIXME: detach */
-               return -1;
+                               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): 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);
+               }       
        }
 
-       /* workaround for servers which support LDAPS but not START TLS */
-       if(inst->port == LDAPS_PORT || inst->tls_mode)
-               inst->tls_mode = LDAP_OPT_X_TLS_HARD;
-       else
-               inst->tls_mode = 0;
-       inst->reply_item_map = NULL;
-       inst->check_item_map = NULL;
-       inst->conns = NULL;
-       inst->failed_conns = 0;
+       return module_rcode; /* caller closes the connection */
+}
 
-#if LDAP_SET_REBIND_PROC_ARGS != 3
-       /*
-        *      The 2-argument rebind doesn't take an instance
-        *      variable.  Our rebind function needs the instance
-        *      variable for the username, password, etc.
-        */
-       if (inst->rebind == 1) {
-               radlog(L_ERR, "rlm_ldap: Cannot use 'rebind' directive as this version of libldap does not support the API that we need.");
-               free(inst);
-               return -1;
-       }
-#endif
+#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;
 
-       DEBUG("rlm_ldap: Registering ldap_groupcmp for Ldap-Group");
-       paircompare_register(PW_LDAP_GROUP, PW_USER_NAME, ldap_groupcmp, inst);
-       memset(&flags, 0, sizeof(flags));
+       conn->referred = TRUE;
+       conn->rebound = TRUE;   /* not really, but oh well... */
+       rad_assert(handle == conn->handle);
 
-       xlat_name = cf_section_name2(conf);
-       if (xlat_name != NULL){
-               char *group_name;
-               DICT_ATTR *dattr;
+       DEBUG("rlm_ldap (%s): Rebinding to URL %s", conn->inst->xlat_name, url);
+       
 
-               /*
-                * Allocate room for <instance>-Ldap-Group
-                */
-               group_name = rad_malloc((strlen(xlat_name) + 1 + 11) * sizeof(char));
-               sprintf(group_name,"%s-Ldap-Group",xlat_name);
-               DEBUG("rlm_ldap: Creating new attribute %s",group_name);
-               dict_addattr(group_name, -1, 0, PW_TYPE_STRING, flags);
-               dattr = dict_attrbyname(group_name);
-               if (dattr == NULL){
-                       radlog(L_ERR, "rlm_ldap: Failed to create attribute %s",group_name);
-                       free(group_name);
-                       free(inst);     /* FIXME: detach */
-                       return -1;
-               }
-               DEBUG("rlm_ldap: Registering ldap_groupcmp for %s",group_name);
-               paircompare_register(dattr->attr, PW_USER_NAME, ldap_groupcmp, inst);
-               free(group_name);
+       rcode = ldap_bind_wrapper(&conn, conn->inst->login,
+                                 conn->inst->password, FALSE);
+       
+       if (rcode == RLM_MODULE_OK) {
+               return LDAP_SUCCESS;
        }
-       else {
-               xlat_name = cf_section_name1(conf);
-               rad_assert(xlat_name != NULL); /* or all hell breaks loose */
-       }
-       inst->xlat_name = strdup(xlat_name);
-       DEBUG("rlm_ldap: Registering ldap_xlat with xlat_name %s",xlat_name);
-       xlat_register(xlat_name,ldap_xlat,inst);
+       
+       ldap_get_option(handle, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
+                       
+       return ldap_errno;
+}
+#endif
 
-       /*
-        *      Over-ride set_auth_type if there's no Auth-Type of our name.
-        *      This automagically catches the case where LDAP is listed
-        *      in "authorize", but not "authenticate".
-        */
-       if (inst->set_auth_type) {
-         DICT_VALUE *dv = dict_valbyname(PW_AUTH_TYPE, 0, xlat_name);
+/** Create and return a new connection
+ * This function is probably too big.
+ */
+static void *ldap_conn_create(void *ctx)
+{
+       int module_rcode;
+       int ldap_errno, ldap_version;
+       struct timeval tv;
+       ldap_instance *inst = ctx;
+       LDAP *handle = NULL;
+       LDAP_CONN *conn = NULL;
 
-               /*
-                *      No section of *my* name, but maybe there's an
-                *      LDAP section...
-                */
-               if (!dv) dv = dict_valbyname(PW_AUTH_TYPE, 0, "LDAP");
-               if (!dv) {
-                       DEBUG2("rlm_ldap: Over-riding set_auth_type, as there is no module %s listed in the \"authenticate\" section.", xlat_name);
-                       inst->set_auth_type = 0;
-               } else {
-                       inst->auth_type = dv->name; /* doesn't change on HUP */
+#ifdef HAVE_LDAP_INITIALIZE
+       if (inst->is_url) {
+               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",
+                              inst->xlat_name, ldap_err2string(ldap_errno));
+                       goto conn_fail;
                }
-       } /* else no need to look up the value */
+       } else
+#endif
+       {
+               DEBUG("rlm_ldap (%s): Connecting to %s:%d", inst->xlat_name,
+                     inst->server, inst->port);
 
-#ifdef NOVELL
-       /*
-        *      (LDAP_Instance, V1) attribute-value pair in the config
-        *      items list means that the 'authorize' method of the
-        *      instance 'V1' of the LDAP module has processed this
-        *      request.
-        */
-       dict_addattr("LDAP-Instance", -1, 0, PW_TYPE_STRING, flags);
+               handle = ldap_init(inst->server, inst->port);
+               if (!handle) {
+                       radlog(L_ERR, "rlm_ldap (%s): ldap_init() failed",
+                              inst->xlat_name);
+               conn_fail:
+                       if (handle) ldap_unbind_s(handle);
+                       return NULL;
+               }
+       }
 
        /*
-        *      ('eDir-APC', '1') in config items list
-        *      Do not perform eDirectory account policy check (APC)
-        *
-        *      ('eDir-APC', '2') in config items list
-        *      Perform eDirectory APC
+        *      We now have a connection structure, but no actual TCP connection.
         *
-        *      ('eDir-APC', '3') in config items list
-        *      eDirectory APC has been completed
+        *      Set a bunch of LDAP options, using common code.
         */
-       dict_addattr("eDir-APC", -1, 0, PW_TYPE_STRING, flags);
-       /*
-        *      eDir-Auth-Option allows for a different NMAS Authentication method to be used instead of password
-        */
-       dict_addattr("eDir-Auth-Option", -1, 0, PW_TYPE_STRING, flags);
-#endif
-
-       if (inst->num_conns <= 0){
-               radlog(L_ERR, "rlm_ldap: Invalid ldap connections number passed.");
-               free(inst);     /* FIXME: detach */
-               return -1;
-       }
-       inst->conns = malloc(sizeof(*(inst->conns))*inst->num_conns);
-       if (inst->conns == NULL){
-               radlog(L_ERR, "rlm_ldap: Could not allocate memory. Aborting.");
-               free(inst);     /* FIXME: detach */
-               return -1;
+#define do_ldap_option(_option, _name, _value) \
+       if (ldap_set_option(handle, _option, _value) != LDAP_OPT_SUCCESS) { \
+               ldap_get_option(handle, LDAP_OPT_ERROR_NUMBER, &ldap_errno); \
+               radlog(L_ERR, "rlm_ldap (%s): Could not set %s: %s", \
+                      inst->xlat_name, _name, ldap_err2string(ldap_errno)); \
        }
-       for(i = 0; i < inst->num_conns; i++){
-               inst->conns[i].bound = 0;
-               inst->conns[i].locked = 0;
-               inst->conns[i].failed_conns = 0;
-               inst->conns[i].ld = NULL;
-               pthread_mutex_init(&inst->conns[i].mutex, NULL);
+               
+       if (inst->ldap_debug) {
+               do_ldap_option(LDAP_OPT_DEBUG_LEVEL, "ldap_debug",
+                              &(inst->ldap_debug));
        }
 
-#ifdef NOVELL
        /*
-        *      'inst->apc_conns' is a separate connection pool to be
-        *      used for performing eDirectory account policy check in
-        *      the 'postauth' method. This avoids changing the
-        *      (RADIUS server) credentials associated with the
-        *      'inst->conns' connection pool.
+        *      Leave "chase_referrals" unset to use the OpenLDAP
+        *      default.
         */
-       inst->apc_conns = malloc(sizeof(*(inst->apc_conns))*inst->num_conns);
-       if (inst->apc_conns == NULL){
-               radlog(L_ERR, "rlm_ldap: Could not allocate memory. Aborting.");
-               free(inst);     /* FIXME: detach */
-               return -1;
-       }
-       for(i = 0; i < inst->num_conns; i++){
-               inst->apc_conns[i].bound = 0;
-               inst->apc_conns[i].locked = 0;
-               inst->apc_conns[i].failed_conns = 0;
-               inst->apc_conns[i].ld = NULL;
-               pthread_mutex_init(&inst->apc_conns[i].mutex, NULL);
-       }
-#endif
-
-       if (read_mappings(inst) != 0) {
-               radlog(L_ERR, "rlm_ldap: Reading dictionary mappings from file %s failed",
-                      inst->dictionary_mapping);
-               free(inst);     /* FIXME: detach */
-               return -1;
-       }
-       if ((inst->check_item_map == NULL) &&
-           (inst->reply_item_map == NULL)) {
-               radlog(L_ERR, "rlm_ldap: dictionary mappings file %s did not contain any mappings",
-                       inst->dictionary_mapping);
-               free(inst);     /* FIXME: detach */
-               return -1;
-       }
-
-       pair = inst->check_item_map;
-       while(pair != NULL){
-               atts_num++;
-               pair = pair->next;
-       }
-       check_map_num = (atts_num - 1);
-       pair = inst->reply_item_map;
-       while(pair != NULL){
-               atts_num++;
-               pair = pair->next;
-       }
-       reply_map_num = (atts_num - 1);
-       if (inst->profile_attr)
-               atts_num++;
-       if (inst->passwd_attr)
-               atts_num++;
-       if (inst->access_attr)
-               atts_num++;
-#ifdef NOVELL
-               atts_num++;     /* eDirectory Authentication Option attribute */
-#endif
-       inst->atts = (char **)malloc(sizeof(char *)*(atts_num + 1));
-       if (inst->atts == NULL){
-               radlog(L_ERR, "rlm_ldap: Could not allocate memory. Aborting.");
-               free(inst);     /* FIXME: detach */
-               return -1;
-       }
-       pair = inst->check_item_map;
-       if (pair == NULL)
-               pair = inst->reply_item_map;
-#ifdef NOVELL
-       for(i=0;i<atts_num - 1;i++){
+       if (inst->chase_referrals != 2) {
+               if (inst->chase_referrals) {
+                       do_ldap_option(LDAP_OPT_REFERRALS, "chase_referrals",
+                                      LDAP_OPT_ON);
+                       
+                       if (inst->rebind == 1) {
+#if LDAP_SET_REBIND_PROC_ARGS == 3
+                               ldap_set_rebind_proc(handle, ldap_rebind, inst);
 #else
-       for(i=0;i<atts_num;i++){
+                               DEBUGW("The flag 'rebind = yes' is not supported by the system LDAP library.  Ignoring.");
 #endif
-               if (i <= check_map_num ){
-                       inst->atts[i] = pair->attr;
-                       if (i == check_map_num)
-                               pair = inst->reply_item_map;
-                       else
-                               pair = pair->next;
-               }
-               else if (i <= reply_map_num){
-                       inst->atts[i] = pair->attr;
-                       pair = pair->next;
-               }
-               else{
-                       if (inst->profile_attr && !att_map[0]){
-                               inst->atts[i] = inst->profile_attr;
-                               att_map[0] = 1;
-                       }
-                       else if (inst->passwd_attr && !att_map[1]){
-                               inst->atts[i] = inst->passwd_attr;
-                               att_map[1] = 1;
-                       }
-                       else if (inst->access_attr && !att_map[2]){
-                               inst->atts[i] = inst->access_attr;
-                               att_map[2] = 1;
                        }
+               } else {
+                       do_ldap_option(LDAP_OPT_REFERRALS, "chase_referrals",
+                                      LDAP_OPT_OFF);
                }
        }
-#ifdef NOVELL
-       {
-               static char ts[] = "sasdefaultloginsequence";
-               inst->atts[atts_num - 1] = ts;
-       }
-#endif
-       inst->atts[atts_num] = NULL;
-
-       DEBUG("conns: %p",inst->conns);
 
-       *instance = inst;
+       tv.tv_sec = inst->net_timeout;
+       tv.tv_usec = 0;
+       do_ldap_option(LDAP_OPT_NETWORK_TIMEOUT, "net_timeout", &tv);
 
+       do_ldap_option(LDAP_OPT_TIMELIMIT, "timelimit", &(inst->timelimit));
 
-       return 0;
-}
+       ldap_version = LDAP_VERSION3;
+       do_ldap_option(LDAP_OPT_PROTOCOL_VERSION, "ldap_version",
+                      &ldap_version);
 
+#ifdef LDAP_OPT_X_KEEPALIVE_IDLE
+       do_ldap_option(LDAP_OPT_X_KEEPALIVE_IDLE, "keepalive idle",
+                      &(inst->keepalive_idle));
+#endif
 
-/*
- *     read_mappings(...) reads a ldap<->radius mappings file to
- *     inst->reply_item_map and inst->check_item_map
- */
-#define MAX_LINE_LEN 160
-#define GENERIC_ATTRIBUTE_ID "$GENERIC$"
+#ifdef LDAP_OPT_X_KEEPALIVE_PROBES
+       do_ldap_option(LDAP_OPT_X_KEEPALIVE_PROBES, "keepalive probes",
+                      &(inst->keepalive_probes));
+#endif
 
-static int
-read_mappings(ldap_instance* inst)
-{
-       FILE* mapfile;
-       char *filename;
+#ifdef LDAP_OPT_X_KEEPALIVE_INTERVAL
+       do_ldap_option(LDAP_OPT_X_KEEPALIVE_INTERVAL, "keepalive interval",
+                      &(inst->keepalive_interval));
+#endif
 
+#ifdef HAVE_LDAP_START_TLS
        /*
-        *      All buffers are of MAX_LINE_LEN so we can use sscanf
-        *      without being afraid of buffer overflows
+        *      Set all of the TLS options
         */
-       char buf[MAX_LINE_LEN], itemType[MAX_LINE_LEN];
-       char radiusAttribute[MAX_LINE_LEN], ldapAttribute[MAX_LINE_LEN];
-       int linenumber;
-       FR_TOKEN operator;
-       char opstring[MAX_LINE_LEN];
+       if (inst->tls_mode) {
+               do_ldap_option(LDAP_OPT_X_TLS, "tls_mode", &(inst->tls_mode));
+       }
 
-       /* open the mappings file for reading */
+#define maybe_ldap_option(_option, _name, _value) \
+       if (_value) do_ldap_option(_option, _name, _value)
 
-       filename = inst->dictionary_mapping;
-       DEBUG("rlm_ldap: reading ldap<->radius mappings from file %s", filename);
-       mapfile = fopen(filename, "r");
+       maybe_ldap_option(LDAP_OPT_X_TLS_CACERTFILE,
+                         "cacertfile", inst->tls_cacertfile);
+       maybe_ldap_option(LDAP_OPT_X_TLS_CACERTDIR,
+                         "cacertdir", inst->tls_cacertdir);
 
-       if (mapfile == NULL) {
-               radlog(L_ERR, "rlm_ldap: Opening file %s failed: %s",
-                      filename, strerror(errno));
-               return -1; /* error */
+#ifdef HAVE_LDAP_INT_TLS_CONFIG
+       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 "
+                      "LDAP_OPT_X_TLS_REQUIRE_CERT option to %s: %s",
+                      inst->xlat_name, 
+                      inst->tls_require_cert,
+                      ldap_err2string(ldap_errno));
        }
+#endif
+
+       maybe_ldap_option(LDAP_OPT_X_TLS_CERTFILE,
+                         "certfile", inst->tls_certfile);
+       maybe_ldap_option(LDAP_OPT_X_TLS_KEYFILE,
+                         "keyfile", inst->tls_keyfile);
+       maybe_ldap_option(LDAP_OPT_X_TLS_RANDOM_FILE,
+                         "randfile", inst->tls_randfile);
 
        /*
-        *      read file line by line. Note that if line length
-        *      exceeds MAX_LINE_LEN, line numbers will be mixed up
+        *      And finally start the TLS code.
         */
-       linenumber = 0;
-
-       while (fgets(buf, sizeof buf, mapfile)!=NULL) {
-               char* ptr;
-               int token_count;
-               TLDAP_RADIUS* pair;
-
-               linenumber++;
-
-               /* strip comments */
-               ptr = strchr(buf, '#');
-               if (ptr) *ptr = 0;
+       if (inst->start_tls && (inst->port != 636)) {
+               ldap_errno = ldap_start_tls_s(handle, NULL, NULL);
+               if (ldap_errno != LDAP_SUCCESS) {
+                       ldap_get_option(handle, LDAP_OPT_ERROR_NUMBER,
+                                       &ldap_errno);
+                       radlog(L_ERR, "rlm_ldap (%s): could not start TLS: %s",
+                              inst->xlat_name,
+                              ldap_err2string(ldap_errno));
+                       goto conn_fail;
+               }
+       }
+#endif /* HAVE_LDAP_START_TLS */
 
-               /* empty line */
-               if (buf[0] == 0) continue;
+       conn = talloc(NULL, LDAP_CONN);
+       conn->inst = inst;
+       conn->handle = handle;
+       conn->rebound = FALSE;
+       conn->referred = FALSE;
 
-               /* extract tokens from the string */
-               token_count = sscanf(buf, "%s %s %s %s",
-                                    itemType, radiusAttribute,
-                                    ldapAttribute, opstring);
+       module_rcode = ldap_bind_wrapper(&conn, inst->login, inst->password,
+                                        FALSE);
+       if (module_rcode != RLM_MODULE_OK) {
+               goto conn_fail;
+       }
 
-               if (token_count <= 0) /* no tokens */
-                       continue;
+       return conn;
+}
 
-               if ((token_count < 3) || (token_count > 4)) {
-                       radlog(L_ERR, "rlm_ldap: Skipping %s line %i: %s",
-                              filename, linenumber, buf);
-                       radlog(L_ERR, "rlm_ldap: Expected 3 to 4 tokens "
-                              "(Item type, RADIUS Attribute and LDAP Attribute) but found only %i", token_count);
-                       continue;
-               }
 
-               if (token_count == 3) {
-                       operator = T_OP_INVALID; /* use defaults */
-               } else {
-                       ptr = opstring;
-                       operator = gettoken((void*)&ptr, buf, sizeof(buf));
-                       if ((operator < T_OP_ADD) || (operator > T_OP_CMP_EQ)) {
-                               radlog(L_ERR, "rlm_ldap: file %s: skipping line %i: unknown or invalid operator %s",
-                                      filename, linenumber, opstring);
-                               continue;
-                       }
-               }
+/** Close and delete a connection
+ *
+ */
+static int ldap_conn_delete(UNUSED void *ctx, void *connection)
+{
+       LDAP_CONN *conn = connection;
 
-               /* create new TLDAP_RADIUS list node */
-               pair = rad_malloc(sizeof(*pair));
+       ldap_unbind_s(conn->handle);
+       talloc_free(conn);
 
-               pair->attr = strdup(ldapAttribute);
-               pair->radius_attr = strdup(radiusAttribute);
-               pair->operator = operator;
+       return 0;
+}
 
-               if ( (pair->attr == NULL) || (pair->radius_attr == NULL) ) {
-                       radlog(L_ERR, "rlm_ldap: Out of memory");
-                       if (pair->attr) free(pair->attr);
-                       if (pair->radius_attr) free(pair->radius_attr);
-                       free(pair);
-                       fclose(mapfile);
-                       return -1;
-               }
 
-               /* push node to correct list */
-               if (strcasecmp(itemType, "checkItem") == 0) {
-                       pair->next = inst->check_item_map;
-                       inst->check_item_map = pair;
-               } else if (strcasecmp(itemType, "replyItem") == 0) {
-                       pair->next = inst->reply_item_map;
-                       inst->reply_item_map = pair;
-               } else {
-                       radlog(L_ERR, "rlm_ldap: file %s: skipping line %i: unknown itemType %s",
-                              filename, linenumber, itemType);
-                       free(pair->attr);
-                       free(pair->radius_attr);
-                       free(pair);
-                       continue;
-               }
+/** Gets an LDAP socket from the connection pool
+ *
+ */
+static LDAP_CONN *ldap_get_socket(ldap_instance *inst)
+{
+       LDAP_CONN *conn;
 
-               DEBUG("rlm_ldap: LDAP %s mapped to RADIUS %s",
-                     pair->attr, pair->radius_attr);
+       conn = fr_connection_get(inst->pool);
+       if (!conn) {
+               radlog(L_ERR, "rlm_ldap (%s): all ldap connections are in use",
+                      inst->xlat_name);
+               return NULL;
        }
 
-       fclose(mapfile);
-
-       return 0; /* success */
+       return conn;
 }
 
-static int perform_search(void *instance, LDAP_CONN *conn,
-                         char *search_basedn, int scope, char *filter,
-                         char **attrs, LDAPMessage ** result)
+/** Frees an LDAP socket back to the connection pool
+ *
+ */
+static void ldap_release_socket(ldap_instance *inst, LDAP_CONN *conn)
 {
-       int             res = RLM_MODULE_OK;
-       int             ldap_errno = 0;
-       ldap_instance  *inst = instance;
-       int             search_retry = 0;
-       struct timeval  tv;
-
-       *result = NULL;
-
-       if (!conn){
-               radlog(L_ERR, "  [%s] NULL connection handle passed",
-                       inst->xlat_name);
-               return RLM_MODULE_FAIL;
-       }
-       if (conn->failed_conns > MAX_FAILED_CONNS_START){
-               conn->failed_conns++;
-               if (conn->failed_conns >= MAX_FAILED_CONNS_END){
-                       conn->failed_conns = MAX_FAILED_CONNS_RESTART;
-                       conn->bound = 0;
-               }
-       }
-retry:
-       if (!conn->bound || conn->ld == NULL) {
-               DEBUG2("  [%s] attempting LDAP reconnection", inst->xlat_name);
-               if (conn->ld){
-                       DEBUG2("  [%s] closing existing LDAP connection",
-                               inst->xlat_name);
-                       ldap_unbind_s(conn->ld);
-               }
-               if ((conn->ld = ldap_connect(instance, inst->login,
-                                            inst->password, 0, &res, NULL)) == NULL) {
-                       radlog(L_ERR, "  [%s] (re)connection attempt failed",
-                               inst->xlat_name);
-                       if (search_retry == 0)
-                               conn->failed_conns++;
-                       return (RLM_MODULE_FAIL);
-               }
-               conn->bound = 1;
-               conn->failed_conns = 0;
-       }
-
-       tv.tv_sec = inst->timeout;
-       tv.tv_usec = 0;
-       DEBUG2("  [%s] performing search in %s, with filter %s", inst->xlat_name, 
-              search_basedn ? search_basedn : "(null)" , filter);
-       switch (ldap_search_st(conn->ld, search_basedn, scope, filter,
-                              attrs, 0, &tv, result)) {
-       case LDAP_SUCCESS:
-       case LDAP_NO_SUCH_OBJECT:
-               break;
-       case LDAP_SERVER_DOWN:
-               radlog(L_ERR, "  [%s] ldap_search() failed: LDAP connection lost.", inst->xlat_name);
-               conn->failed_conns++;
-               if (search_retry == 0){
-                       if (conn->failed_conns <= MAX_FAILED_CONNS_START){
-                               radlog(L_INFO, "  [%s] Attempting reconnect", inst->xlat_name);
-                               search_retry = 1;
-                               conn->bound = 0;
-                               ldap_msgfree(*result);
-                               goto retry;
-                       }
-               }
-               ldap_msgfree(*result);
-               return RLM_MODULE_FAIL;
-       case LDAP_INSUFFICIENT_ACCESS:
-               radlog(L_ERR, "  [%s] ldap_search() failed: Insufficient access. Check the identity and password configuration directives.", inst->xlat_name);
-               ldap_msgfree(*result);
-               return RLM_MODULE_FAIL;
-       case LDAP_TIMEOUT:
-               exec_trigger(NULL, inst->cs, "modules.ldap.timeout", TRUE);
-               radlog(L_ERR, "  [%s] ldap_search() failed: Timed out while waiting for server to respond. Please increase the timeout.", inst->xlat_name);
-               ldap_msgfree(*result);
-               return RLM_MODULE_FAIL;
-       case LDAP_FILTER_ERROR:
-               radlog(L_ERR, "  [%s] ldap_search() failed: Bad search filter: %s", inst->xlat_name,filter);
-               ldap_msgfree(*result);
-               return RLM_MODULE_FAIL;
-       case LDAP_TIMELIMIT_EXCEEDED:
-               exec_trigger(NULL, inst->cs, "modules.ldap.timeout", TRUE);
+       /*
+        *      Could have already been free'd due to a previous error.
+        */
+       if (!conn) return;
 
-       case LDAP_BUSY:
-       case LDAP_UNAVAILABLE:
-               /* We don't need to reconnect in these cases so we don't set conn->bound */
-               ldap_get_option(conn->ld, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
-               radlog(L_ERR, "  [%s] ldap_search() failed: %s", inst->xlat_name,
-                      ldap_err2string(ldap_errno));
-               ldap_msgfree(*result);
-               return (RLM_MODULE_FAIL);
-       default:
-               ldap_get_option(conn->ld, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
-               radlog(L_ERR, "  [%s] ldap_search() failed: %s", inst->xlat_name,
-                      ldap_err2string(ldap_errno));
-               conn->bound = 0;
-               ldap_msgfree(*result);
-               return (RLM_MODULE_FAIL);
+       /*
+        *      We chased a referral to another server.
+        *
+        *      This connection is no longer part of the pool which is
+        *      connected to and bound to the configured server.
+        *      Close it.
+        *
+        *      Note that we do NOT close it if it was bound to
+        *      another user.  Instead, we let the next caller do the
+        *      rebind.
+        */
+       if (conn->referred) {
+               fr_connection_del(inst->pool, conn);
+               return;
        }
 
-       ldap_errno = ldap_count_entries(conn->ld, *result);
-       if (ldap_errno != 1) {
-               if (ldap_errno == 0) {
-                       DEBUG("  [%s] object not found", inst->xlat_name);
-               } else {
-                       DEBUG("  [%s] got ambiguous search result (%d results)", inst->xlat_name, ldap_errno);
-               }
-               res = RLM_MODULE_NOTFOUND;
-               ldap_msgfree(*result);
-       }
-       return res;
+       fr_connection_release(inst->pool, conn);
+       return;
 }
 
 
-/*
- *     Translate the LDAP queries.
+/* 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, void *arg)
+static size_t ldap_escape_func(UNUSED REQUEST *request, char *out,
+                              size_t outlen, const char *in, UNUSED void *arg)
 {
-        ldap_instance   *inst = arg;
        size_t len = 0;
 
        while (in[0]) {
@@ -1005,1853 +789,1674 @@ static size_t ldap_escape_func(UNUSED REQUEST *request, char *out, size_t outlen
        return len;
 }
 
-/*
- *     ldap_groupcmp(). Implement the Ldap-Group == "group" filter
+/** Do a search and get a response
+ *
  */
-static int ldap_groupcmp(void *instance, REQUEST *req,
-                        UNUSED VALUE_PAIR *request, VALUE_PAIR *check,
-                        UNUSED VALUE_PAIR *check_pairs,
-                        UNUSED VALUE_PAIR **reply_pairs)
+static int perform_search(ldap_instance *inst, REQUEST *request,
+                         LDAP_CONN **pconn, const char *search_basedn,
+                         int scope, const char *filter, 
+                         const char * const *attrs, LDAPMessage **presult)
 {
-        char            filter[MAX_FILTER_STR_LEN];
-        char            gr_filter[MAX_FILTER_STR_LEN];
-        int             res;
-        LDAPMessage     *result = NULL;
-        LDAPMessage     *msg = NULL;
-        char            basedn[MAX_FILTER_STR_LEN];
-       static char     firstattr[] = "dn";
-       char            *attrs[] = {firstattr,NULL};
-       char            **vals;
-        ldap_instance   *inst = instance;
-       char            *group_attrs[] = {inst->groupmemb_attr,NULL};
-       LDAP_CONN       *conn;
-       int             conn_id = -1;
-       VALUE_PAIR      *vp_user_dn;
-       VALUE_PAIR      **request_pairs;
-
-       request_pairs = &req->config_items;
-
-       DEBUG("  [%s] Entering ldap_groupcmp()", inst->xlat_name);
-
-       if (check->vp_strvalue == NULL || check->length == 0){
-                DEBUG("rlm_ldap::ldap_groupcmp: Illegal group name");
-                return 1;
-        }
-
-        if (req == NULL){
-                DEBUG("rlm_ldap::ldap_groupcmp: NULL request");
-                return 1;
-        }
-
-        if (!radius_xlat(basedn, sizeof(basedn), inst->basedn, req, ldap_escape_func, inst)) {
-                DEBUG("rlm_ldap::ldap_groupcmp: unable to create basedn.");
-                return 1;
-        }
-
-        while((vp_user_dn = pairfind(*request_pairs, PW_LDAP_USERDN, 0)) == NULL){
-                char            *user_dn = NULL;
-
-                if (!radius_xlat(filter, sizeof(filter), inst->filter,
-                                       req, ldap_escape_func, inst)){
-                        DEBUG("rlm_ldap::ldap_groupcmp: unable to create filter");
-                        return 1;
-                }
-               if ((conn_id = ldap_get_conn(inst->conns,&conn,inst)) == -1){
-                       radlog(L_ERR, "  [%s] All ldap connections are in use", inst->xlat_name);
-                       return 1;
-               }
-                if ((res = perform_search(inst, conn, basedn, LDAP_SCOPE_SUBTREE,
-                                       filter, attrs, &result)) != RLM_MODULE_OK){
-                        DEBUG("rlm_ldap::ldap_groupcmp: search failed");
-                       ldap_release_conn(conn_id,inst);
-                        return 1;
-                }
-                if ((msg = ldap_first_entry(conn->ld, result)) == NULL) {
-                        DEBUG("rlm_ldap::ldap_groupcmp: ldap_first_entry() failed");
-                       ldap_release_conn(conn_id,inst);
-                        ldap_msgfree(result);
-                        return 1;
-                }
-                if ((user_dn = ldap_get_dn(conn->ld, msg)) == NULL) {
-                        DEBUG("rlm_ldap:ldap_groupcmp:: ldap_get_dn() failed");
-                       ldap_release_conn(conn_id,inst);
-                        ldap_msgfree(result);
-                        return 1;
-                }
-               ldap_release_conn(conn_id,inst);
-
-                /*
-                *      Adding new attribute containing DN for LDAP
-                *      object associated with given username
-                */
-                pairadd(request_pairs, pairmake("Ldap-UserDn", user_dn,
-                                               T_OP_EQ));
-                ldap_memfree(user_dn);
-                ldap_msgfree(result);
-        }
-
-        if(!radius_xlat(gr_filter, sizeof(gr_filter),
-                       inst->groupmemb_filt, req, ldap_escape_func, inst)) {
-                DEBUG("rlm_ldap::ldap_groupcmp: unable to create filter.");
-                return 1;
-        }
-
-       if (strchr((char *)check->vp_strvalue,',') != NULL) {
-               /* This looks like a DN */
-               strlcpy(filter, gr_filter, sizeof(filter));
-               strlcpy(basedn, check->vp_strvalue, sizeof(basedn));
-       } else
-               snprintf(filter,sizeof(filter), "(&(%s=%s)%s)",
-                        inst->groupname_attr,
-                        (char *)check->vp_strvalue,gr_filter);
+       int             ldap_errno;
+       int             count = 0;
+       struct timeval  tv;
 
-       if ((conn_id = ldap_get_conn(inst->conns,&conn,inst)) == -1) {
-               radlog(L_ERR, "  [%s] All ldap connections are in use", inst->xlat_name);
-               return 1;
-       }
+       /*
+        *      OpenLDAP library doesn't declare attrs array as const, but
+        *      it really should be *sigh*.
+        */
+       char **search_attrs;
+       memcpy(&search_attrs, &attrs, sizeof(attrs));
 
-       if ((res = perform_search(inst, conn, basedn, LDAP_SCOPE_SUBTREE,
-                               filter, attrs, &result)) == RLM_MODULE_OK) {
-               DEBUG("rlm_ldap::ldap_groupcmp: User found in group %s",
-                               (char *)check->vp_strvalue);
-               ldap_msgfree(result);
-               ldap_release_conn(conn_id,inst);
-               return 0;
-       }
+       *presult = NULL;
 
-       ldap_release_conn(conn_id,inst);
+       /*
+        *      Do all searches as the default admin user.
+        */
+       if ((*pconn)->rebound) {
+               ldap_errno = ldap_bind_wrapper(pconn, inst->login,
+                                              inst->password, TRUE);
+               if (ldap_errno != RLM_MODULE_OK) {
+                       return -1;
+               }
 
-       if (res != RLM_MODULE_NOTFOUND ) {
-               DEBUG("rlm_ldap::ldap_groupcmp: Search returned error");
-               return 1;
+               rad_assert(*pconn);
+               (*pconn)->rebound = FALSE;
        }
 
-       if (inst->groupmemb_attr == NULL){
-               /*
-                *      Search returned NOTFOUND and searching for
-                *      membership using user object attributes is not
-                *      specified in config file
-                */
-               DEBUG("rlm_ldap::ldap_groupcmp: Group %s not found or user is not a member.",(char *)check->vp_strvalue);
-               return 1;
-       }
+       tv.tv_sec = inst->timeout;
+       tv.tv_usec = 0;
+       RDEBUG2("Performing search in '%s' with filter '%s'",
+               search_basedn ? search_basedn : "(null)" ,
+               filter);
 
-       snprintf(filter,sizeof(filter), "(objectclass=*)");
-       if ((conn_id = ldap_get_conn(inst->conns,&conn,inst)) == -1){
-               radlog(L_ERR, "  [%s] Add ldap connections are in use", inst->xlat_name);
-               return 1;
+retry:
+       ldap_errno = ldap_search_ext_s((*pconn)->handle, search_basedn, scope,
+                                      filter, search_attrs, 0, NULL, NULL,
+                                      &tv, 0, presult);
+       if (ldap_errno != LDAP_SUCCESS) {
+               ldap_msgfree(*presult);
+               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);
+               }
        }
-       if ((res = perform_search(inst, conn, vp_user_dn->vp_strvalue,
-                                 LDAP_SCOPE_BASE, filter, group_attrs,
-                                 &result)) != RLM_MODULE_OK) {
-               DEBUG("rlm_ldap::ldap_groupcmp: Search returned error");
-               ldap_release_conn(conn_id, inst);
-               return 1;
+               
+       count = ldap_count_entries((*pconn)->handle, *presult);
+       if (count == 0) {
+               ldap_msgfree(*presult);
+               RDEBUG("Search returned no results");
+               
+               return -2;
        }
 
-       if ((msg = ldap_first_entry(conn->ld, result)) == NULL) {
-               DEBUG("rlm_ldap::ldap_groupcmp: ldap_first_entry() failed");
-               ldap_release_conn(conn_id,inst);
-               ldap_msgfree(result);
-               return 1;
+       if (count != 1) {
+               ldap_msgfree(*presult);
+               RDEBUG("Got ambiguous search result (%d results)", count);
+                     
+               return -2;
        }
-       if ((vals = ldap_get_values(conn->ld, msg,
-                                   inst->groupmemb_attr)) != NULL) {
-               int i = 0;
-               char found = 0;
-
-               for (;i < ldap_count_values(vals);i++){
-                       if (strchr(vals[i],',') != NULL){
-                               /* This looks like a DN */
-                               LDAPMessage *gr_result = NULL;
-                               snprintf(filter,sizeof(filter), "(%s=%s)",
-                                       inst->groupname_attr,
-                                       (char *)check->vp_strvalue);
-                               if ((res = perform_search(inst, conn, vals[i],
-                                               LDAP_SCOPE_BASE, filter,
-                                               attrs, &gr_result)) != RLM_MODULE_OK){
-                                       if (res != RLM_MODULE_NOTFOUND) {
-                                               DEBUG("rlm_ldap::ldap_groupcmp: Search returned error");
-                                               ldap_value_free(vals);
-                                               ldap_msgfree(result);
-                                               ldap_release_conn(conn_id,inst);
-                                               return 1;
-                                       }
-                               } else {
-                                       ldap_msgfree(gr_result);
-                                       found = 1;
-                                       break;
-                               }
-                       } else {
-                               if (strcmp(vals[i],(char *)check->vp_strvalue) == 0){
-                                       found = 1;
-                                       break;
-                               }
-                       }
-               }
-               ldap_value_free(vals);
-               ldap_msgfree(result);
-               if (found == 0){
-                       DEBUG("rlm_ldap::groupcmp: Group %s not found or user not a member",
-                               (char *)check->vp_strvalue);
-                       ldap_release_conn(conn_id,inst);
-                       return 1;
-               }
-       } else {
-                       DEBUG("rlm_ldap::ldap_groupcmp: ldap_get_values() failed");
-                       ldap_msgfree(result);
-                       ldap_release_conn(conn_id,inst);
-                       return 1;
-       }
-
-       DEBUG("rlm_ldap::ldap_groupcmp: User found in group %s",(char *)check->vp_strvalue);
-       ldap_release_conn(conn_id,inst);
 
-        return 0;
+       return 0;
 }
 
-/*
- * ldap_xlat()
- * Do an xlat on an LDAP URL
+/** 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)
+                       char *out, size_t freespace)
 {
-       char url[MAX_FILTER_STR_LEN];
-       int res;
-       size_t ret = 0;
+       int rcode;
+       size_t length = 0;
        ldap_instance *inst = instance;
        LDAPURLDesc *ldap_url;
        LDAPMessage *result = NULL;
-       LDAPMessage *msg = NULL;
+       LDAPMessage *entry = NULL;
        char **vals;
-       int conn_id = -1;
        LDAP_CONN *conn;
-
-       DEBUG("  [%s] - ldap_xlat", inst->xlat_name);
-       if (!radius_xlat(url, sizeof(url), fmt, request, ldap_escape_func, inst)) {
-               radlog (L_ERR, "  [%s] Unable to create LDAP URL.\n", inst->xlat_name);
-               return 0;
-       }
-       if (!ldap_is_ldap_url(url)){
-               radlog (L_ERR, "  [%s] String passed does not look like an LDAP URL.\n", inst->xlat_name);
-               return 0;
-       }
-       if (ldap_url_parse(url,&ldap_url)){
-               radlog (L_ERR, "  [%s] LDAP URL parse failed.\n", inst->xlat_name);
-               return 0;
-       }
-       if (ldap_url->lud_attrs == NULL || ldap_url->lud_attrs[0] == NULL ||
-           ( ldap_url->lud_attrs[1] != NULL ||
-             ( !*ldap_url->lud_attrs[0] ||
-               ! strcmp(ldap_url->lud_attrs[0],"*") ) ) ){
-               radlog (L_ERR, "  [%s] Invalid Attribute(s) request.\n", inst->xlat_name);
-               ldap_free_urldesc(ldap_url);
-               return 0;
-       }
-       if (ldap_url->lud_host){
-               if (strncmp(inst->server,ldap_url->lud_host,
-                           strlen(inst->server)) != 0 ||
-                   ldap_url->lud_port != inst->port) {
-                       DEBUG("  [%s] Requested server/port is not known to this module instance.", inst->xlat_name);
-                       ldap_free_urldesc(ldap_url);
+       int ldap_errno;
+       const char *url;
+       const char **attrs;
+       char buffer[MAX_FILTER_STR_LEN];
+
+       if (strchr(fmt, '%') != NULL) {
+               if (!radius_xlat(buffer, sizeof(buffer), fmt, request,
+                                ldap_escape_func, NULL)) {
+                       radlog(L_ERR,
+                              "rlm_ldap (%s): Unable to create LDAP URL", 
+                              inst->xlat_name);
                        return 0;
                }
+               url = buffer;
+       } else {
+               url = fmt;
        }
-       if ((conn_id = ldap_get_conn(inst->conns,&conn,inst)) == -1){
-               radlog(L_ERR, "  [%s] All ldap connections are in use", inst->xlat_name);
-               ldap_free_urldesc(ldap_url);
+
+       if (!ldap_is_ldap_url(url)) {
+               radlog(L_ERR, "rlm_ldap (%s): String passed does not look "
+                      "like an LDAP URL", inst->xlat_name);
                return 0;
        }
-       if ((res = perform_search(inst, conn, ldap_url->lud_dn, ldap_url->lud_scope, ldap_url->lud_filter, ldap_url->lud_attrs, &result)) != RLM_MODULE_OK){
-               if (res == RLM_MODULE_NOTFOUND){
-                       DEBUG("  [%s] Search returned not found", inst->xlat_name);
-                       ldap_free_urldesc(ldap_url);
-                       ldap_release_conn(conn_id,inst);
-                       return 0;
-               }
-               DEBUG("  [%s] Search returned error", inst->xlat_name);
-               ldap_free_urldesc(ldap_url);
-               ldap_release_conn(conn_id,inst);
+
+       if (ldap_url_parse(url, &ldap_url)){
+               radlog(L_ERR, "rlm_ldap (%s): Parsing LDAP URL failed",
+                      inst->xlat_name);
                return 0;
        }
-       if ((msg = ldap_first_entry(conn->ld, result)) == NULL){
-               DEBUG("  [%s] ldap_first_entry() failed", inst->xlat_name);
-               ldap_msgfree(result);
-               ldap_free_urldesc(ldap_url);
-               ldap_release_conn(conn_id,inst);
-               return 0;
+
+       /*
+        *      Nothing, empty string, "*" string, or got 2 things, die.
+        */
+       if (!ldap_url->lud_attrs || !ldap_url->lud_attrs[0] ||
+           !*ldap_url->lud_attrs[0] ||
+           (strcmp(ldap_url->lud_attrs[0], "*") == 0) ||
+           ldap_url->lud_attrs[1]) {
+               radlog(L_ERR, "rlm_ldap (%s): Bad attributes list in LDAP "
+                      "URL. URL must specify exactly one attribute to "
+                      "retrieve",
+                      inst->xlat_name);
+                      
+               goto free_urldesc;
+       }
+
+       if (ldap_url->lud_host &&
+           ((strncmp(inst->server, ldap_url->lud_host,
+                     strlen(inst->server)) != 0) ||
+            (ldap_url->lud_port != inst->port))) {
+               RDEBUG("Requested server/port is \"%s:%i\"", ldap_url->lud_host,
+                      inst->port);
+               
+               goto free_urldesc;
+       }
+
+       conn = ldap_get_socket(inst);
+       if (!conn) goto free_urldesc;
+
+       memcpy(&attrs, &ldap_url->lud_attrs, sizeof(attrs));
+       
+       rcode = perform_search(inst, request, &conn, ldap_url->lud_dn, 
+                              ldap_url->lud_scope, ldap_url->lud_filter, attrs,
+                              &result);
+       if (rcode < 0) {
+               if (rcode == -2) {
+                       RDEBUG("Search returned not found", inst->xlat_name);
+                       goto free_socket;
+               }
+
+               goto free_socket;
+       }
+
+       entry = ldap_first_entry(conn->handle, result);
+       if (!entry) {
+               ldap_get_option(conn->handle, LDAP_OPT_RESULT_CODE,
+                               &ldap_errno);
+               radlog(L_ERR, "rlm_ldap (%s): Failed retrieving entry: %s", 
+                      inst->xlat_name,
+                      ldap_err2string(ldap_errno));
+               goto free_result;
        }
-       if ((vals = ldap_get_values(conn->ld, msg, ldap_url->lud_attrs[0])) != NULL) {
-               ret = strlen(vals[0]);
-               if (ret >= freespace){
-                       DEBUG("  [%s] Insufficient string space", inst->xlat_name);
-                       ldap_free_urldesc(ldap_url);
-                       ldap_value_free(vals);
-                       ldap_msgfree(result);
-                       ldap_release_conn(conn_id,inst);
-                       return 0;
-               }
-               DEBUG("  [%s] Adding attribute %s, value: %s", inst->xlat_name,ldap_url->lud_attrs[0],vals[0]);
-               strlcpy(out,vals[0],freespace);
-               ldap_value_free(vals);
+
+       vals = ldap_get_values(conn->handle, entry, ldap_url->lud_attrs[0]);
+       if (!vals) {
+               RDEBUG("No \"%s\" attributes found in specified object",
+                      inst->xlat_name, ldap_url->lud_attrs[0]);
+               goto free_result;
        }
-       else
-               ret = 0;
 
-       ldap_msgfree(result);
-       ldap_free_urldesc(ldap_url);
-       ldap_release_conn(conn_id,inst);
+       length = strlen(vals[0]);
+       if (length >= freespace){
 
-       DEBUG("  [%s] - ldap_xlat end", inst->xlat_name);
+               goto free_vals;
+       }
 
-       return ret;
-}
+       strlcpy(out, vals[0], freespace);
 
+free_vals:
+       ldap_value_free(vals);
+free_result:
+       ldap_msgfree(result);
+free_socket:
+       ldap_release_socket(inst, conn);
+free_urldesc:
+       ldap_free_urldesc(ldap_url);
 
-/*
- *     For auto-header discovery.
- */
-static const FR_NAME_NUMBER header_names[] = {
-       { "{clear}",    PW_CLEARTEXT_PASSWORD },
-       { "{cleartext}", PW_CLEARTEXT_PASSWORD },
-       { "{md5}",      PW_MD5_PASSWORD },
-       { "{BASE64_MD5}",       PW_MD5_PASSWORD },
-       { "{smd5}",     PW_SMD5_PASSWORD },
-       { "{crypt}",    PW_CRYPT_PASSWORD },
-       { "{sha}",      PW_SHA_PASSWORD },
-       { "{ssha}",     PW_SSHA_PASSWORD },
-       { "{nt}",       PW_NT_PASSWORD },
-       { "{ns-mta-md5}", PW_NS_MTA_MD5_PASSWORD },
-       { NULL, 0 }
-};
+       return length;
+}
 
 
-/******************************************************************************
- *
- *      Function: rlm_ldap_authorize
- *
- *      Purpose: Check if user is authorized for remote access
- *
- ******************************************************************************/
-static int ldap_authorize(void *instance, REQUEST * request)
+static char *get_userdn(LDAP_CONN **pconn, REQUEST *request,
+                       rlm_rcode_t *module_rcode)
 {
-       LDAPMessage     *result = NULL;
-       LDAPMessage     *msg = NULL;
-       LDAPMessage     *def_msg = NULL;
-       LDAPMessage     *def_attr_msg = NULL;
-       LDAPMessage     *def_result = NULL;
-       LDAPMessage     *def_attr_result = NULL;
-       ldap_instance   *inst = instance;
-       char            *user_dn = NULL;
-       char            filter[MAX_FILTER_STR_LEN];
-       char            basedn[MAX_FILTER_STR_LEN];
-       VALUE_PAIR      *check_tmp;
-       VALUE_PAIR      *reply_tmp;
-       int             res;
-       VALUE_PAIR      **check_pairs, **reply_pairs;
-       char            **vals;
-       VALUE_PAIR      *module_fmsg_vp;
-       VALUE_PAIR      *user_profile;
-       char            module_fmsg[MAX_STRING_LEN];
-       LDAP_CONN       *conn;
-       int             conn_id = -1;
-       int             added_known_password = 0;
-
-       if (!request->username){
-               RDEBUG2("Attribute \"User-Name\" is required for authorization.\n");
-               return RLM_MODULE_NOOP;
-       }
+       int             rcode;
+       VALUE_PAIR      *vp;
+       ldap_instance   *inst = (*pconn)->inst;
+       LDAPMessage     *result, *entry;
+       int             ldap_errno;
+       static char     firstattr[] = "uid";
+       char            *user_dn;
+       const char      *attrs[] = {firstattr, NULL};
+       char            filter[MAX_FILTER_STR_LEN];     
+       char            basedn[MAX_FILTER_STR_LEN];     
 
-       check_pairs = &request->config_items;
-       reply_pairs = &request->reply->vps;
+       *module_rcode = RLM_MODULE_FAIL;
 
-       /*
-        * Check for valid input, zero length names not permitted
-        */
-       if (request->username->vp_strvalue == 0) {
-               DEBUG2("zero length username not permitted\n");
-               return RLM_MODULE_INVALID;
+       vp = pairfind(request->config_items, PW_LDAP_USERDN, 0, TAG_ANY);
+       if (vp) {
+               *module_rcode = RLM_MODULE_OK;
+               return vp->vp_strvalue;
        }
-       RDEBUG("performing user authorization for %s",
-              request->username->vp_strvalue);
-
+       
        if (!radius_xlat(filter, sizeof(filter), inst->filter,
-                        request, ldap_escape_func, inst)) {
-               radlog(L_ERR, "  [%s] unable to create filter.\n", inst->xlat_name);
-               return RLM_MODULE_INVALID;
+                        request, ldap_escape_func, NULL)) {
+               radlog(L_ERR, "rlm_ldap (%s): Unable to create filter",
+                      inst->xlat_name);
+               *module_rcode = RLM_MODULE_INVALID;
+               return NULL;
        }
 
        if (!radius_xlat(basedn, sizeof(basedn), inst->basedn,
-                        request, ldap_escape_func, inst)) {
-               radlog(L_ERR, "  [%s] unable to create basedn.\n", inst->xlat_name);
-               return RLM_MODULE_INVALID;
+                        request, ldap_escape_func, NULL)) {
+               radlog(L_ERR, "rlm_ldap (%s): Unable to create basedn",
+                      inst->xlat_name);
+               *module_rcode = RLM_MODULE_INVALID;
+               return NULL;
        }
 
-       if ((conn_id = ldap_get_conn(inst->conns,&conn,inst)) == -1){
-               radlog(L_ERR, "  [%s] All ldap connections are in use", inst->xlat_name);
-               return RLM_MODULE_FAIL;
-       }
-       if ((res = perform_search(instance, conn, basedn, LDAP_SCOPE_SUBTREE, filter, inst->atts, &result)) != RLM_MODULE_OK) {
-               RDEBUG("search failed");
-               if (res == RLM_MODULE_NOTFOUND){
-                       snprintf(module_fmsg,sizeof(module_fmsg),"  [%s] User not found", inst->xlat_name);
-                       module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
-                       pairadd(&request->packet->vps, module_fmsg_vp);
+       rcode = perform_search(inst, request, pconn, basedn, LDAP_SCOPE_SUBTREE,
+                              filter, attrs, &result);
+       if (rcode < 0) {
+               if (rcode == -2) {
+                       *module_rcode = RLM_MODULE_NOTFOUND;
                }
-               ldap_release_conn(conn_id,inst);
-               return (res);
+
+               return NULL;
        }
-       if ((msg = ldap_first_entry(conn->ld, result)) == NULL) {
-               RDEBUG("ldap_first_entry() failed");
+
+       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,
+                      ldap_err2string(ldap_errno));
                ldap_msgfree(result);
-               ldap_release_conn(conn_id,inst);
-               return RLM_MODULE_FAIL;
+               return NULL;
        }
-       if ((user_dn = ldap_get_dn(conn->ld, msg)) == NULL) {
-               RDEBUG("ldap_get_dn() failed");
+
+       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,
+                      ldap_err2string(ldap_errno));
+                      
                ldap_msgfree(result);
-               ldap_release_conn(conn_id,inst);
-               return RLM_MODULE_FAIL;
+               return NULL;
        }
-       /*
-        * Adding new attribute containing DN for LDAP object associated with
-        * given username
-        */
-       pairadd(check_pairs, pairmake("Ldap-UserDn", user_dn, T_OP_EQ));
+
+       vp = pairmake("LDAP-UserDn", user_dn, T_OP_EQ);
+       if (!vp) {
+               ldap_memfree(user_dn);
+               ldap_msgfree(result);
+               return NULL;
+       }
+       
+       *module_rcode = RLM_MODULE_OK;
+       
+       pairadd(&request->config_items, vp);
        ldap_memfree(user_dn);
+       ldap_msgfree(result);
 
+       return vp->vp_strvalue;
+}
 
-       /* Remote access is controled by attribute of the user object */
-       if (inst->access_attr) {
-               if ((vals = ldap_get_values(conn->ld, msg, inst->access_attr)) != NULL) {
-                       if (inst->default_allow){
-                               RDEBUG("checking if remote access for %s is allowed by %s", request->username->vp_strvalue, inst->access_attr);
-                               if (!strncmp(vals[0], "FALSE", 5)) {
-                                       RDEBUG("dialup access disabled");
-                                       snprintf(module_fmsg,sizeof(module_fmsg),"  [%s] Access Attribute denies access", inst->xlat_name);
-                                       module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
-                                       pairadd(&request->packet->vps, module_fmsg_vp);
-                                       ldap_msgfree(result);
-                                       ldap_value_free(vals);
-                                       ldap_release_conn(conn_id,inst);
-                                       return RLM_MODULE_USERLOCK;
-                               }
-                               ldap_value_free(vals);
-                       }
-                       else{
-                               RDEBUG("%s attribute exists - access denied by default", inst->access_attr);
-                               snprintf(module_fmsg,sizeof(module_fmsg),"  [%s] Access Attribute denies access", inst->xlat_name);
-                               module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
-                               pairadd(&request->packet->vps, module_fmsg_vp);
-                               ldap_msgfree(result);
-                               ldap_value_free(vals);
-                               ldap_release_conn(conn_id,inst);
-                               return RLM_MODULE_USERLOCK;
-                       }
-               } else {
-                       if (inst->default_allow){
-                               RDEBUG("no %s attribute - access denied by default", inst->access_attr);
-                               snprintf(module_fmsg,sizeof(module_fmsg),"  [%s] Access Attribute denies access", inst->xlat_name);
-                               module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
-                               pairadd(&request->packet->vps, module_fmsg_vp);
-                               ldap_msgfree(result);
-                               ldap_release_conn(conn_id,inst);
-                               return RLM_MODULE_USERLOCK;
-                       }
-               }
+
+/** 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;
+       rlm_rcode_t     module_rcode;
+       LDAPMessage     *result = NULL;
+       LDAPMessage     *entry = NULL;
+       int             ldap_errno;
+       int             check_is_dn = FALSE, value_is_dn = FALSE;
+       static char     firstattr[] = "dn";
+       const char      *attrs[] = {firstattr, NULL};
+       char            **vals;
+       const char      *group_attrs[] = {inst->groupmemb_attr, NULL};
+       LDAP_CONN       *conn;
+       char            *user_dn;
+
+       char            gr_filter[MAX_FILTER_STR_LEN];
+       char            filter[MAX_FILTER_STR_LEN];
+       char            basedn[MAX_FILTER_STR_LEN];
+
+       RDEBUG("Searching for user in group \"%s\"", check->vp_strvalue);
+
+       if (check->length == 0) {
+               RDEBUG("Cannot do comparison (group name is empty)");
+               return 1;
        }
 
+       conn = ldap_get_socket(inst);
+       if (!conn) return 1;
+
        /*
-        * Check for the default profile entry. If it exists then add the
-        * attributes it contains in the check and reply pairs
+        *      This is used in the default membership filter.
         */
+       user_dn = get_userdn(&conn, request, &module_rcode);
+       if (!user_dn) {
+               ldap_release_socket(inst, conn);
+               return 1;
+       }
 
-       user_profile = pairfind(request->config_items, PW_USER_PROFILE, 0);
-       if (inst->default_profile || user_profile){
-               char *profile = inst->default_profile;
+       if (!inst->groupmemb_filter) goto check_attr;
 
-               strlcpy(filter,inst->base_filter,sizeof(filter));
-               if (user_profile)
-                       profile = user_profile->vp_strvalue;
-               if (profile && *profile){
-                       if ((res = perform_search(instance, conn,
-                               profile, LDAP_SCOPE_BASE,
-                               filter, inst->atts, &def_result)) == RLM_MODULE_OK){
-                               if ((def_msg = ldap_first_entry(conn->ld,def_result))){
-                                       if ((check_tmp = ldap_pairget(conn->ld,def_msg,inst->check_item_map,check_pairs,1, inst))) {
-                                               if (inst->do_xlat){
-                                                       pairxlatmove(request, check_pairs, &check_tmp);
-                                                       pairfree(&check_tmp);
-                                               }
-                                               else
-                                                       pairadd(check_pairs,check_tmp);
-                                       }
-                                       if ((reply_tmp = ldap_pairget(conn->ld,def_msg,inst->reply_item_map,reply_pairs,0, inst))) {
-                                               if (inst->do_xlat){
-                                                       pairxlatmove(request, reply_pairs, &reply_tmp);
-                                                       pairfree(&reply_tmp);
-                                               }
-                                               else
-                                                       pairadd(reply_pairs,reply_tmp);
-                                       }
-                               }
-                               ldap_msgfree(def_result);
-                       } else
-                               RDEBUG("default_profile/user-profile search failed");
-               }
+       if (!radius_xlat(gr_filter, sizeof(gr_filter),
+                        inst->groupmemb_filter, request, ldap_escape_func,
+                        NULL)) {
+               radlog(L_ERR, "rlm_ldap (%s): Failed creating group filter",
+                      inst->xlat_name);
+               return 1;
        }
 
        /*
-        * Check for the profile attribute. If it exists, we assume that it
-        * contains the DN of an entry containg a profile for the user. That
-        * way we can have different general profiles for various user groups
-        * (students,faculty,staff etc)
+        *      If it's a DN, use that.
         */
+       check_is_dn = strchr(check->vp_strvalue,',') == NULL ? FALSE : TRUE;
+       
+       if (check_is_dn) {
+               strlcpy(filter, gr_filter, sizeof(filter));
+               strlcpy(basedn, check->vp_strvalue, sizeof(basedn));    
+       } else {
+               snprintf(filter, sizeof(filter), "(&(%s=%s)%s)",
+                        inst->groupname_attr,
+                        check->vp_strvalue, gr_filter);
 
-       if (inst->profile_attr){
-               if ((vals = ldap_get_values(conn->ld, msg, inst->profile_attr)) != NULL) {
-                       unsigned int i=0;
-                       strlcpy(filter,inst->base_filter,sizeof(filter));
-                       while(vals[i] && *vals[i]){
-                               if ((res = perform_search(instance, conn,
-                                       vals[i], LDAP_SCOPE_BASE,
-                                       filter, inst->atts, &def_attr_result)) == RLM_MODULE_OK){
-                                       if ((def_attr_msg = ldap_first_entry(conn->ld,def_attr_result))){
-                                               if ((check_tmp = ldap_pairget(conn->ld,def_attr_msg,inst->check_item_map,check_pairs,1, inst))) {
-                                                       if (inst->do_xlat){
-                                                               pairxlatmove(request, check_pairs, &check_tmp);
-                                                               pairfree(&check_tmp);
-                                                       }
-                                                       else
-                                                               pairadd(check_pairs,check_tmp);
-                                               }
-                                               if ((reply_tmp = ldap_pairget(conn->ld,def_attr_msg,inst->reply_item_map,reply_pairs,0, inst))) {
-                                                       if (inst->do_xlat){
-                                                               pairxlatmove(request, reply_pairs, &reply_tmp);
-                                                               pairfree(&reply_tmp);
-                                                       }
-                                                       else
-                                                               pairadd(reply_pairs,reply_tmp);
-                                               }
-                                       }
-                                       ldap_msgfree(def_attr_result);
-                               } else
-                                       RDEBUG("profile_attribute search failed");
-                               i++;
-                       }
-                       ldap_value_free(vals);
+               /*
+                *      get_userdn does this, too.  Oh well.
+                */
+               if (!radius_xlat(basedn, sizeof(basedn), inst->basedn,
+                                request, ldap_escape_func, NULL)) {
+                       radlog(L_ERR, "rlm_ldap (%s): Failed creating basedn",
+                              inst->xlat_name);
+                       return 1;
                }
        }
-       if (inst->passwd_attr && *inst->passwd_attr) {
-#ifdef NOVELL_UNIVERSAL_PASSWORD
-               if (strcasecmp(inst->passwd_attr,"nspmPassword") != 0) {
-#endif
-                       VALUE_PAIR *passwd_item;
-                       char **passwd_vals;
-                       char *value = NULL;
-                       int i;
 
-                       /*
-                        *      Read the password from the DB, and
-                        *      add it to the request.
-                        */
-                       passwd_vals = ldap_get_values(conn->ld,msg,
-                                                     inst->passwd_attr);
+       rcode = perform_search(inst, request, &conn, basedn, LDAP_SCOPE_SUBTREE,
+                              filter, attrs, &result);
+       if (rcode == 0) {
+               ldap_release_socket(inst, conn);
+               ldap_msgfree(result);
+                       
+               RDEBUG("User found in group object");
+               
+               return 0;
+       }
 
-                       /*
-                        *      Loop over what we received, and parse it.
-                        */
-                       if (passwd_vals) for (i = 0;
-                                             passwd_vals[i] != NULL;
-                                             i++) {
-                               int attr = PW_USER_PASSWORD;
-
-                               if (!*passwd_vals[i])
-                                       continue;
-
-                               value = passwd_vals[i];
-                               if (!value) continue;
-
-                               passwd_item = radius_paircreate(request,
-                                                               &request->config_items,
-                                                               attr, 0,
-                                                               PW_TYPE_STRING);
-                               strlcpy(passwd_item->vp_strvalue, value,
-                                       sizeof(passwd_item->vp_strvalue));
-                               passwd_item->length = strlen(passwd_item->vp_strvalue);
-                               RDEBUG("Added %s = %s in check items",
-                                     passwd_item->name,
-                                     passwd_item->vp_strvalue);
-                               added_known_password = 1;
-                       }
-                       ldap_value_free(passwd_vals);
-#ifdef NOVELL_UNIVERSAL_PASSWORD
-               }
-               else{
-               /*
-               * Read Universal Password from eDirectory
-               */
-                       VALUE_PAIR      *passwd_item;
-                       VALUE_PAIR      *vp_user_dn;
-                       char            *universal_password = NULL;
-                       size_t          universal_password_len = UNIVERSAL_PASS_LEN;
-                       char            *passwd_val = NULL;
-
-                       res = 0;
-
-                       if ((passwd_item = pairfind(request->config_items, PW_CLEARTEXT_PASSWORD, 0)) == NULL){
-
-                               universal_password = rad_malloc(universal_password_len);
-                               memset(universal_password, 0, universal_password_len);
-
-                               vp_user_dn = pairfind(request->config_items,PW_LDAP_USERDN, 0);
-                               res = nmasldap_get_password(conn->ld,vp_user_dn->vp_strvalue,&universal_password_len,universal_password);
-
-                               if (res == 0){
-                                       passwd_val = universal_password;
-                                       if (passwd_val){
-                                               passwd_item = radius_paircreate(request, &request->config_items, PW_CLEARTEXT_PASSWORD, 0, PW_TYPE_STRING);
-                                               strlcpy(passwd_item->vp_strvalue,passwd_val,sizeof(passwd_item->vp_strvalue));
-                                               passwd_item->length = strlen(passwd_item->vp_strvalue);
-                                               added_known_password = 1;
-
-#ifdef NOVELL
-                                               {
-                                                       DICT_ATTR *dattr;
-                                                       VALUE_PAIR      *vp_inst, *vp_apc;
-                                                       int inst_attr, apc_attr;
-
-                                                       dattr = dict_attrbyname("LDAP-Instance");
-                                                       inst_attr = dattr->attr;
-                                                       dattr = dict_attrbyname("eDir-APC");
-                                                       apc_attr = dattr->attr;
-
-                                                       vp_inst = pairfind(request->config_items, inst_attr, 0);
-                                                       if(vp_inst == NULL){
-                                                               /*
-                                                                * The authorize method of no other LDAP module instance has
-                                                                * processed this request.
-                                                                */
-                                                               vp_inst = radius_paircreate(request, &request->config_items, inst_attr, 0, PW_TYPE_STRING);
-                                                               strlcpy(vp_inst->vp_strvalue, inst->xlat_name, sizeof(vp_inst->vp_strvalue));
-                                                               vp_inst->length = strlen(vp_inst->vp_strvalue);
-
-                                                               /*
-                                                                * Inform the authenticate / post-auth method about the presence
-                                                                * of UP in the config items list and whether eDirectory account
-                                                                * policy check is to be performed or not.
-                                                                */
-                                                               vp_apc = radius_paircreate(request, &request->config_items, apc_attr, 0, PW_TYPE_STRING);
-                                                               if(!inst->edir_account_policy_check){
-                                                                       /* Do nothing */
-                                                                       strcpy(vp_apc->vp_strvalue, "1");
-                                                               }else{
-                                                                       /* Perform eDirectory account-policy check */
-                                                                       strcpy(vp_apc->vp_strvalue, "2");
-                                                               }
-                                                               vp_apc->length = 1;
-                                                       }
-                                               }
-#endif
+       if (rcode == -1) {
+               ldap_release_socket(inst, conn);
+               return 1;
+       }
 
-                                               RDEBUG("Added the eDirectory password %s in check items as %s",passwd_item->vp_strvalue,passwd_item->name);
-                                       }
-                               }
-                               else {
-                                       RDEBUG("Error reading Universal Password.Return Code = %d",res);
-                               }
+       /* else the search returned -2, for "not found" */
 
-                               memset(universal_password, 0, universal_password_len);
-                               free(universal_password);
-                       }
-               }
-#endif
+       /*
+        *      Else the search returned NOTFOUND.  See if we're
+        *      configured to search for group membership using user
+        *      object attribute.
+        */
+       if (!inst->groupmemb_attr) {
+               ldap_release_socket(inst, conn);
+               RDEBUG("Group object \"%s\" not found, or user is not a member",
+                      check->vp_strvalue);
+               return 1;
        }
 
-#ifdef NOVELL
-       {
-               VALUE_PAIR      *vp_auth_opt;
-               DICT_ATTR       *dattr;
-               char            **auth_option;
-               int             auth_opt_attr;
-
-               dattr = dict_attrbyname("eDir-Auth-Option");
-               auth_opt_attr = dattr->attr;
-               if(pairfind(*check_pairs, auth_opt_attr, 0) == NULL){
-                       if ((auth_option = ldap_get_values(conn->ld, msg, "sasDefaultLoginSequence")) != NULL) {
-                               if ((vp_auth_opt = paircreate(auth_opt_attr, 0, PW_TYPE_STRING)) == NULL){
-                                       radlog(L_ERR, "  [%s] Could not allocate memory. Aborting.", inst->xlat_name);
-                                       ldap_msgfree(result);
-                                       ldap_release_conn(conn_id, inst);
-                               }
-                               strcpy(vp_auth_opt->vp_strvalue, auth_option[0]);
-                               vp_auth_opt->length = strlen(auth_option[0]);
-                               pairadd(&request->config_items, vp_auth_opt);
-                       }else{
-                               RDEBUG("No default NMAS login sequence");
-                       }
-               }
-       }
-#endif
+check_attr:
+       RDEBUG2("Checking user object membership (%s) attributes",
+               inst->groupmemb_attr);
 
-       RDEBUG("looking for check items in directory...");
+       snprintf(filter ,sizeof(filter), "(objectclass=*)");
 
-       if ((check_tmp = ldap_pairget(conn->ld, msg, inst->check_item_map,check_pairs,1, inst)) != NULL) {
-               if (inst->do_xlat){
-                       pairxlatmove(request, check_pairs, &check_tmp);
-                       pairfree(&check_tmp);
+       rcode = perform_search(inst, request, &conn, user_dn, LDAP_SCOPE_BASE,
+                              filter, group_attrs, &result);
+       if (rcode < 0) {
+               if (rcode == -2) {
+                       RDEBUG("Can't check membership attributes, user object "
+                              "not found");
                }
-               else
-                       pairadd(check_pairs,check_tmp);
+               ldap_release_socket(inst, conn);
+               return 1;
        }
 
+       entry = ldap_first_entry(conn->handle, result);
+       if (!entry) {
+               ldap_get_option(conn->handle, LDAP_OPT_RESULT_CODE,
+                               &ldap_errno);
+               radlog(L_ERR, "rlm_ldap (%s): Failed retrieving entry: %s", 
+                      inst->xlat_name,
+                      ldap_err2string(ldap_errno));
+                              
+               ldap_release_socket(inst, conn);
+               ldap_msgfree(result);
+               return 1;
+       }
 
-       RDEBUG("looking for reply items in directory...");
+       vals = ldap_get_values(conn->handle, entry, inst->groupmemb_attr);
+       if (!vals) {
+               RDEBUG("No group membership attribute(s) found in user object");
+               ldap_release_socket(inst, conn);
+               ldap_msgfree(result);
+               return 1;
+       }
+
+       /*
+        *      Loop over the list of groups the user is a member of,
+        *      looking for a match.
+        */
+       found = FALSE;
+       for (i = 0; i < ldap_count_values(vals); i++) {
+               LDAPMessage *gr_result = NULL;
+               
+               value_is_dn = strchr(vals[i], ',') == NULL ? FALSE : TRUE;
+               
+               RDEBUG2("Processing group membership value \"%s\"", vals[i]);
 
+               /*
+                *      Both literal group names, do case sensitive comparison
+                */
+               if (!check_is_dn && !value_is_dn) {
+                       if (strcmp(vals[i], check->vp_strvalue) == 0){
+                               RDEBUG("User found (membership value matches "
+                                      "check value)");
+                              
+                               found = TRUE;
+                               break;
+                       }
+                       
+                       continue;
+               }
 
-       if ((reply_tmp = ldap_pairget(conn->ld, msg, inst->reply_item_map,reply_pairs,0, inst)) != NULL) {
-               if (inst->do_xlat){
-                       pairxlatmove(request, reply_pairs, &reply_tmp);
-                       pairfree(&reply_tmp);
+               /*
+                *      Both DNs, do case insensitive comparison
+                */
+               if (check_is_dn && value_is_dn) {
+                       if (strcasecmp(vals[i], check->vp_strvalue) == 0){
+                               RDEBUG("User found (membership DN matches "
+                                      "check DN)");
+                              
+                               found = TRUE;
+                               break;
+                       }
+                       
+                       continue;
                }
-               else
-                       pairadd(reply_pairs,reply_tmp);
-       }
+               
+               /*
+                *      If the value is not a DN, or the check item is a DN
+                *      there's nothing more we can do.
+                */
+               if (!value_is_dn && check_is_dn) continue;
 
-       if (inst->do_comp && paircompare(request,request->packet->vps,*check_pairs,reply_pairs) != 0){
-#ifdef NOVELL
-               /* Don't perform eDirectory APC if RADIUS authorize fails */
-               int apc_attr;
-               VALUE_PAIR *vp_apc;
-               DICT_ATTR *dattr;
+               /*
+                *      We have a value which is a DN, and a check item which
+                *      specifies the name of a group, search using the value
+                *      DN for the group, and see if it has a groupname_attr
+                *      which matches our check val.
+                */
+               RDEBUG2("Searching with membership DN and group name");
 
-               dattr = dict_attrbyname("eDir-APC");
-               apc_attr = dattr->attr;
+               snprintf(filter,sizeof(filter), "(%s=%s)",
+                        inst->groupname_attr, check->vp_strvalue);
 
-               vp_apc = pairfind(request->config_items, apc_attr, 0);
-               if(vp_apc)
-                       vp_apc->vp_strvalue[0] = '1';
-#endif
+               rcode = perform_search(inst, request, &conn, vals[i],
+                                      LDAP_SCOPE_BASE, filter, attrs,
+                                      &gr_result);
+                                      
+               ldap_msgfree(gr_result);
 
-               RDEBUG("Pairs do not match. Rejecting user.");
-               snprintf(module_fmsg,sizeof(module_fmsg),"  [%s] Pairs do not match", inst->xlat_name);
-               module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
-               pairadd(&request->packet->vps, module_fmsg_vp);
-               ldap_msgfree(result);
-               ldap_release_conn(conn_id,inst);
+               /* Error occurred */
+               if (rcode == -1) {
+                       ldap_value_free(vals);
+                       ldap_msgfree(result);
+                       ldap_release_socket(inst, conn);
+                       return 1;
+               }
+               
+               /*
+                *      Either the group DN wasn't found, or it didn't have the
+                *      correct name. Continue looping over the attributes.
+                */
+               if (rcode == -2) {
+                       ldap_msgfree(gr_result);
+                       continue;
+               }
 
-               return RLM_MODULE_REJECT;
-       }
-       
-       /*
-       *       More warning messages for people who can't be bothered
-       *       to read the documentation.
-       */
-       if (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)) {
-                      DEBUG("WARNING: No \"known good\" password was found in LDAP.  Are you sure that the user is configured correctly?");
-              }
-       }
+               found = TRUE;
 
-       /*
-        * Module should default to LDAP authentication if no Auth-Type
-        * specified.  Note that we do this ONLY if configured, AND we
-        * set the Auth-Type to our module name, which allows multiple
-        * ldap instances to work.
-        */
-       if (inst->set_auth_type &&
-           (pairfind(*check_pairs, PW_AUTH_TYPE, 0) == NULL) &&
-           request->password &&
-           (request->password->attribute == PW_USER_PASSWORD) &&
-           !added_known_password) {
-               pairadd(check_pairs, pairmake("Auth-Type", inst->auth_type, T_OP_EQ));
-               RDEBUG("Setting Auth-Type = %s", inst->auth_type);
+               RDEBUG("User found (group name in membership DN matches check "
+                      "value)");
+
+               break;
        }
 
-       RDEBUG("user %s authorized to use remote access",
-             request->username->vp_strvalue);
+       ldap_value_free(vals);
        ldap_msgfree(result);
-       ldap_release_conn(conn_id,inst);
+       ldap_release_socket(inst, conn);
+
+       if (!found) {
+               RDEBUG("User is not a member of specified group");
+               return 1;
+       }
 
-       return RLM_MODULE_OK;
+       return 0;
 }
 
-/*****************************************************************************
- *
- *     Function: rlm_ldap_authenticate
- *
- *     Purpose: Check the user's password against ldap database
+/** Detach from the LDAP server and cleanup internal state.
  *
- *****************************************************************************/
-static int ldap_authenticate(void *instance, REQUEST * request)
+ */
+static int ldap_detach(void *instance)
 {
-       LDAP           *ld_user;
-       LDAPMessage    *result, *msg;
-       ldap_instance  *inst = instance;
-       static char     firstattr[] = "uid";
-       char           *user_dn, *attrs[] = {firstattr, NULL};
-       char            filter[MAX_FILTER_STR_LEN];
-       char            basedn[MAX_FILTER_STR_LEN];
-       int             res;
-       VALUE_PAIR     *vp_user_dn;
-       VALUE_PAIR      *module_fmsg_vp;
-       char            module_fmsg[MAX_STRING_LEN];
-       LDAP_CONN       *conn;
-       int             conn_id = -1;
-#ifdef NOVELL
-       char            *err = NULL;
-#endif
-
-       /*
-        * Ensure that we're being passed a plain-text password, and not
-        * anything else.
-        */
+       ldap_instance *inst = instance;
+       
+       fr_connection_pool_delete(inst->pool);
 
-       if (!request->username) {
-               radlog(L_AUTH, "  [%s] Attribute \"User-Name\" is required for authentication.\n", inst->xlat_name);
-               return RLM_MODULE_INVALID;
+       if (inst->user_map) {
+               radius_mapfree(&inst->user_map);
        }
 
-       if (!request->password){
-               radlog(L_AUTH, "  [%s] Attribute \"User-Password\" is required for authentication.", inst->xlat_name);
-               DEBUG2("  You seem to have set \"Auth-Type := LDAP\" somewhere.");
-               DEBUG2("  THAT CONFIGURATION IS WRONG.  DELETE IT.");
-               DEBUG2("  YOU ARE PREVENTING THE SERVER FROM WORKING PROPERLY.");
-               return RLM_MODULE_INVALID;
-       }
+       return 0;
+}
 
-       if(request->password->attribute != PW_USER_PASSWORD) {
-               radlog(L_AUTH, "  [%s] Attribute \"User-Password\" is required for authentication. Cannot use \"%s\".", inst->xlat_name, request->password->name);
-               return RLM_MODULE_INVALID;
+static int parse_sub_section(CONF_SECTION *parent, 
+                            ldap_instance *inst,
+                            ldap_acct_section_t **config,
+                            rlm_components_t comp)
+{
+       CONF_SECTION *cs;
+
+       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;
        }
-
-       if (request->password->length == 0) {
-               snprintf(module_fmsg,sizeof(module_fmsg),"  [%s] empty password supplied", inst->xlat_name);
-               module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
-               pairadd(&request->packet->vps, module_fmsg_vp);
-               return RLM_MODULE_INVALID;
+       
+       *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;
        }
+               
+       (*config)->cs = cs;
+
+       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;
+       }
        /*
-        * Check that we don't have any failed connections. If we do there's no real need
-        * of runing. Also give it another chance if we have a lot of failed connections.
+        *      Attrmap only performs some basic validation checks, we need
+        *      to do rlm_ldap specific checks here.
         */
-       if (inst->failed_conns > MAX_FAILED_CONNS_END)
-               inst->failed_conns = 0;
-       if (inst->failed_conns > MAX_FAILED_CONNS_START){
-               inst->failed_conns++;
-               return RLM_MODULE_FAIL;
-       }
-
-
-       RDEBUG("login attempt by \"%s\" with password \"%s\"",
-              request->username->vp_strvalue, request->password->vp_strvalue);
-
-       while ((vp_user_dn = pairfind(request->config_items,
-                                     PW_LDAP_USERDN, 0)) == NULL) {
-               if (!radius_xlat(filter, sizeof(filter), inst->filter,
-                               request, ldap_escape_func, inst)) {
-                       radlog(L_ERR, "  [%s] unable to create filter.\n", inst->xlat_name);
-                       return RLM_MODULE_INVALID;
-               }
-
-               if (!radius_xlat(basedn, sizeof(basedn), inst->basedn,
-                               request, ldap_escape_func, inst)) {
-                       radlog(L_ERR, "  [%s] unable to create basedn.\n", inst->xlat_name);
-                       return RLM_MODULE_INVALID;
+       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 ((conn_id = ldap_get_conn(inst->conns,&conn,inst)) == -1){
-                       radlog(L_ERR, "  [%s] All ldap connections are in use", inst->xlat_name);
-                       return RLM_MODULE_FAIL;
+               
+               if (map->src->type == VPT_TYPE_LIST) {
+                       cf_log_err(map->ci, "Right operand must not be "
+                                    "a list");
+                       
+                       return -1;
                }
-               if ((res = perform_search(instance, conn, basedn, LDAP_SCOPE_SUBTREE, filter, attrs, &result)) != RLM_MODULE_OK) {
-                       if (res == RLM_MODULE_NOTFOUND){
-                               snprintf(module_fmsg,sizeof(module_fmsg),"  [%s] User not found", inst->xlat_name);
-                               module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
-                               pairadd(&request->packet->vps, module_fmsg_vp);
+               
+               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;
                        }
-                       ldap_release_conn(conn_id,inst);
-                       return (res);
-               }
-               if ((msg = ldap_first_entry(conn->ld, result)) == NULL) {
-                       ldap_msgfree(result);
-                       ldap_release_conn(conn_id,inst);
-                       return RLM_MODULE_FAIL;
-               }
-               if ((user_dn = ldap_get_dn(conn->ld, msg)) == NULL) {
-                       RDEBUG("ldap_get_dn() failed");
-                       ldap_msgfree(result);
-                       ldap_release_conn(conn_id,inst);
-                       return RLM_MODULE_FAIL;
+               default:
+                       break;
                }
-               ldap_release_conn(conn_id,inst);
-               pairadd(&request->config_items, pairmake("Ldap-UserDn", user_dn, T_OP_EQ));
-               ldap_memfree(user_dn);
-               ldap_msgfree(result);
        }
+       return 0;
+}
 
-       user_dn = vp_user_dn->vp_strvalue;
+/** 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;
 
-       RDEBUG("user DN: %s", user_dn);
+       *instance = inst = talloc_zero(conf, ldap_instance);
+       if (!inst) return -1;
 
-#ifndef NOVELL
-       ld_user = ldap_connect(instance, user_dn, request->password->vp_strvalue,
-                              1, &res, NULL);
-#else
-       /* Don't perform eDirectory APC again after attempting to bind here. */
-       {
-               int apc_attr;
-               DICT_ATTR *dattr;
-               VALUE_PAIR *vp_apc;
-               VALUE_PAIR      *vp_auth_opt, *vp_state;
-               int auth_opt_attr;
-               char seq[256];
-               char host_ipaddr[32];
-               LDAP_CONN       *conn1;
-               int auth_state = -1;
-               char            *challenge = NULL;
-               size_t          challenge_len = MAX_CHALLENGE_LEN;
-               char            *state = NULL;
-
-               dattr = dict_attrbyname("eDir-APC");
-               apc_attr = dattr->attr;
-               vp_apc = pairfind(request->config_items, apc_attr, 0);
-               if(vp_apc && vp_apc->vp_strvalue[0] == '2')
-                       vp_apc->vp_strvalue[0] = '3';
-
-               res = 0;
-
-               dattr = dict_attrbyname("eDir-Auth-Option");
-               auth_opt_attr = dattr->attr;
-
-               vp_auth_opt = pairfind(request->config_items, auth_opt_attr, 0);
-
-               if(vp_auth_opt )
-               {
-                       RDEBUG("ldap auth option = %s", vp_auth_opt->vp_strvalue);
-                       strncpy(seq, vp_auth_opt->vp_strvalue, vp_auth_opt->length);
-                       seq[vp_auth_opt->length] = '\0';
-                       if( strcasecmp(seq, "<No Default>") ){
-
-                               /* Get the client IP address to check for packet validity */
-                               inet_ntop(AF_INET, &request->packet->src_ipaddr, host_ipaddr, sizeof(host_ipaddr));
-
-                               /* challenge variable is used to receive the challenge from the
-                                * Token method (if any) and also to send the state attribute
-                                * in case the request packet is a reply to a challenge
-                                */
-                               challenge = rad_malloc(MAX_CHALLENGE_LEN);
-
-                               /*  If state attribute present in request it is a reply to challenge. */
-                               if((vp_state = pairfind(request->packet->vps, PW_STATE, 0))!= NULL ){
-                                       RDEBUG("Response to Access-Challenge");
-                                       strncpy(challenge, vp_state->vp_strvalue, sizeof(challenge));
-                                       challenge_len = vp_state->length;
-                                       challenge[challenge_len] = 0;
-                                       auth_state = -2;
-                               }
+       inst->cs = conf;
 
-                               if ((conn_id = ldap_get_conn(inst->conns, &conn1, inst)) == -1){
-                                       radlog(L_ERR, "  [%s] All ldap connections are in use", inst->xlat_name);
-                                       res =  RLM_MODULE_FAIL;
-                               }
+       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(!conn1){
-                                       radlog(L_ERR, "  [%s] NULL connection handle passed", inst->xlat_name);
-                                       return RLM_MODULE_FAIL;
-                               }
+       /*
+        *      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 (conn1->failed_conns > MAX_FAILED_CONNS_START){
-                                       conn1->failed_conns++;
-                                       if (conn1->failed_conns >= MAX_FAILED_CONNS_END){
-                                               conn1->failed_conns = MAX_FAILED_CONNS_RESTART;
-                                               conn1->bound = 0;
-                                       }
-                               }
-retry:
-                               if (!conn1->bound || conn1->ld == NULL) {
-                                       DEBUG2("  [%s] attempting LDAP reconnection", inst->xlat_name);
-                                       if (conn1->ld){
-                                               DEBUG2("  [%s] closing existing LDAP connection", inst->xlat_name);
-                                               ldap_unbind_s(conn1->ld);
-                                       }
-                                       if ((conn1->ld = ldap_connect(instance, inst->login,inst->password, 0, &res, NULL)) == NULL) {
-                                               radlog(L_ERR, "  [%s] (re)connection attempt failed", inst->xlat_name);
-                                               conn1->failed_conns++;
-                                               return (RLM_MODULE_FAIL);
-                                       }
-                                       conn1->bound = 1;
-                                       conn1->failed_conns = 0;
-                               }
-                               RDEBUG("Performing NMAS Authentication for user: %s, seq: %s \n", user_dn,seq);
-
-                               res = radLdapXtnNMASAuth(conn1->ld, user_dn, request->password->vp_strvalue, seq, host_ipaddr, &challenge_len, challenge, &auth_state );
-
-                               switch(res){
-                                       case LDAP_SUCCESS:
-                                               ldap_release_conn(conn_id,inst);
-                                               if ( auth_state == -1)
-                                                       res = RLM_MODULE_FAIL;
-                                               if ( auth_state != REQUEST_CHALLENGED){
-                                                       if (auth_state == REQUEST_ACCEPTED){
-                                                               RDEBUG("user %s authenticated succesfully",request->username->vp_strvalue);
-                                                               res = RLM_MODULE_OK;
-                                                       }else if(auth_state == REQUEST_REJECTED){
-                                                               RDEBUG("user %s authentication failed",request->username->vp_strvalue);
-                                                               res = RLM_MODULE_REJECT;
-                                                       }
-                                               }else{
-                                                       /* Request challenged. Generate Reply-Message attribute with challenge data */
-                                                       pairadd(&request->reply->vps,pairmake("Reply-Message", challenge, T_OP_EQ));
-                                                       /* Generate state attribute */
-                                                       state = rad_malloc(MAX_CHALLENGE_LEN);
-                                                       (void) sprintf(state, "%s%s", challenge, challenge);
-                                                       vp_state = paircreate(PW_STATE, 0, PW_TYPE_OCTETS);
-                                                       memcpy(vp_state->vp_strvalue, state, strlen(state));
-                                                       vp_state->length = strlen(state);
-                                                       pairadd(&request->reply->vps, vp_state);
-                                                       free(state);
-                                                       /* Mark the packet as a Acceess-Challenge Packet */
-                                                       request->reply->code = PW_ACCESS_CHALLENGE;
-                                                       RDEBUG("Sending Access-Challenge.");
-                                                       res = RLM_MODULE_HANDLED;
-                                               }
-                                               if(challenge)
-                                                       free(challenge);
-                                               return res;
-                                       case LDAP_SERVER_DOWN:
-                                               radlog(L_ERR, "  [%s] nmas authentication failed: LDAP connection lost.", inst->xlat_name);                                                conn->failed_conns++;
-                                               if (conn->failed_conns <= MAX_FAILED_CONNS_START){
-                                                       radlog(L_INFO, "  [%s] Attempting reconnect", inst->xlat_name);
-                                                       conn->bound = 0;
-                                                       goto retry;
-                                               }
-                                               if(challenge)
-                                                       free(challenge);
-                                               return RLM_MODULE_FAIL;
-                                       default:
-                                               ldap_release_conn(conn_id,inst);
-                                               if(challenge)
-                                                       free(challenge);
-                                               return RLM_MODULE_FAIL;
-                               }
-                       }
-               }
-       }
+       if (inst->server == NULL) {
+               radlog(L_ERR, "rlm_ldap (%s): Missing 'server' directive",
+                      inst->xlat_name);
+               goto error;
+       }
 
-       ld_user = ldap_connect(instance, user_dn, request->password->vp_strvalue,
-                       1, &res, &err);
+       /*
+        *      Check for URLs.  If they're used and the library doesn't
+        *      support them, then complain.
+        */
+       inst->is_url = 0;
+       if (ldap_is_ldap_url(inst->server)) {
+#ifdef HAVE_LDAP_INITIALIZE
+               inst->is_url = 1;
+               inst->port = 0;
+#else
+               radlog(L_ERR, "rlm_ldap (%s): 'server' directive is in URL "
+                      "form but ldap_initialize() is not available",
+                      inst->xlat_name);
+               goto error;
+#endif
+       }
 
-       if(err != NULL){
-               /* 'err' contains the LDAP connection error description */
-               RDEBUG("%s", err);
-               pairadd(&request->reply->vps, pairmake("Reply-Message", err, T_OP_EQ));
-               ldap_memfree((void *)err);
+       /* workaround for servers which support LDAPS but not START TLS */
+       if (inst->port == LDAPS_PORT || inst->tls_mode) {
+               inst->tls_mode = LDAP_OPT_X_TLS_HARD;
+       } else {
+               inst->tls_mode = 0;
+       }
+
+#if LDAP_SET_REBIND_PROC_ARGS != 3
+       /*
+        *      The 2-argument rebind doesn't take an instance
+        *      variable.  Our rebind function needs the instance
+        *      variable for the username, password, etc.
+        */
+       if (inst->rebind == 1) {
+               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);
+               goto error;
        }
 #endif
 
-       if (ld_user == NULL){
-               if (res == RLM_MODULE_REJECT){
-                       inst->failed_conns = 0;
-                       snprintf(module_fmsg,sizeof(module_fmsg),"  [%s] Bind as user failed", inst->xlat_name);
-                       module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
-                       pairadd(&request->packet->vps, module_fmsg_vp);
-               }
-               if (res == RLM_MODULE_FAIL){
-                       RDEBUG("ldap_connect() failed");
-                       inst->failed_conns++;
-               }
-               return (res);
+       /*
+        *      Build the attribute map
+        */
+       if (ldap_map_verify(inst, &(inst->user_map)) < 0) {
+               goto error;
        }
 
-       RDEBUG("user %s authenticated succesfully",
-             request->username->vp_strvalue);
-       ldap_unbind_s(ld_user);
-       inst->failed_conns = 0;
+       /*
+        *      Group comparison checks.
+        */
+       paircompare_register(PW_LDAP_GROUP, PW_USER_NAME, ldap_groupcmp, inst); 
+       if (cf_section_name2(conf)) {
+               const DICT_ATTR *da;
+               ATTR_FLAGS flags;
+               char buffer[256];
 
-       return RLM_MODULE_OK;
-}
+               snprintf(buffer, sizeof(buffer), "%s-Ldap-Group",
+                        inst->xlat_name);
+               memset(&flags, 0, sizeof(flags));
 
-#ifdef NOVELL
-/*****************************************************************************
- *
- *     Function: rlm_ldap_postauth
- *
- *     Purpose: Perform eDirectory account policy check and failed-login reporting
- *     to eDirectory.
- *
- *****************************************************************************/
-static int ldap_postauth(void *instance, REQUEST * request)
-{
-       int res = RLM_MODULE_FAIL;
-       int inst_attr, apc_attr;
-       char password[UNIVERSAL_PASS_LEN];
-       ldap_instance  *inst = instance;
-       LDAP_CONN       *conn;
-       VALUE_PAIR *vp_inst, *vp_apc;
-       DICT_ATTR *dattr;
+               dict_addattr(buffer, -1, 0, PW_TYPE_STRING, flags);
+               da = dict_attrbyname(buffer);
+               if (!da) {
+                       radlog(L_ERR, "rlm_ldap (%s): Failed creating "
+                              "attribute %s", inst->xlat_name, buffer);
+                       goto error;
+               }
 
-       dattr = dict_attrbyname("LDAP-Instance");
-       inst_attr = dattr->attr;
-       dattr = dict_attrbyname("eDir-APC");
-       apc_attr = dattr->attr;
+               paircompare_register(da->attr, PW_USER_NAME, ldap_groupcmp,
+                                    inst);
+       }
 
-       vp_inst = pairfind(request->config_items, inst_attr, 0);
+       xlat_register(inst->xlat_name, ldap_xlat, inst);
 
        /*
-        * Check if the password in the config items list is the user's UP which has
-        * been read in the authorize method of this instance of the LDAP module.
+        *      Initialize the socket pool.
         */
-       if((vp_inst == NULL) || strcmp(vp_inst->vp_strvalue, inst->xlat_name))
-               return RLM_MODULE_NOOP;
+       inst->pool = fr_connection_pool_init(inst->cs, inst,
+                                            ldap_conn_create,
+                                            NULL,
+                                            ldap_conn_delete);
+       if (!inst->pool) {
+               ldap_detach(inst);
+               return -1;
+       }
+       
+       return 0;
 
-       vp_apc = pairfind(request->config_items, apc_attr, 0);
+error:
+       ldap_detach(inst);
+       return -1;
+}
 
-       switch(vp_apc->vp_strvalue[0]){
-               case '1':
-                       /* Account policy check not enabled */
-               case '3':
-                       /* Account policy check has been completed */
-                       res = RLM_MODULE_NOOP;
-                       break;
-               case '2':
-                       {
-                               int err, conn_id = -1;
-                               char *error_msg = NULL;
-                               VALUE_PAIR *vp_fdn, *vp_pwd;
-                               DICT_ATTR *da;
-
-                               if (request->reply->code == PW_AUTHENTICATION_REJECT) {
-                                 /* Bind to eDirectory as the RADIUS user with a wrong password. */
-                                 vp_pwd = pairfind(request->config_items, PW_CLEARTEXT_PASSWORD, 0);
-                                 if (vp_pwd && *vp_pwd->vp_strvalue) {
-                                         strcpy(password, vp_pwd->vp_strvalue);
-                                         if (password[0] != 'a') {
-                                                 password[0] = 'a';
-                                         } else {
-                                                 password[0] = 'b';
-                                         }
-                                 } else {
-                                         strcpy(password, "dummy_password");
-                                 }
-                                 res = RLM_MODULE_REJECT;
-                               } else {
-                                       /* Bind to eDirectory as the RADIUS user using the user's UP */
-                                       vp_pwd = pairfind(request->config_items, PW_CLEARTEXT_PASSWORD, 0);
-                                       if (vp_pwd == NULL) {
-                                               RDEBUG("User's Universal Password not in config items list.");
-                                               return RLM_MODULE_FAIL;
-                                       }
-                                       strcpy(password, vp_pwd->vp_strvalue);
-                               }
+static int check_access(ldap_instance *inst, REQUEST* request, LDAP_CONN *conn,
+                       LDAPMessage *entry)
+{
+       int rcode = -1;
+       char **vals = NULL;
 
-                               if ((da = dict_attrbyname("Ldap-UserDn")) == NULL) {
-                                       RDEBUG("Attribute for user FDN not found in dictionary. Unable to proceed");
-                                       return RLM_MODULE_FAIL;
-                               }
+       vals = ldap_get_values(conn->handle, entry, inst->access_attr);
+       if (vals) {
+               if (inst->positive_access_attr) {
+                       if (strncmp(vals[0], "FALSE", 5) == 0) {
+                               RDEBUG("Dialup access disabled");
 
-                               vp_fdn = pairfind(request->config_items, da->attr, 0);
-                               if (vp_fdn == NULL) {
-                                       RDEBUG("User's FQDN not in config items list.");
-                                       return RLM_MODULE_FAIL;
-                               }
+                       } else {
+                               rcode = 0;
+                       }
 
-                               if ((conn_id = ldap_get_conn(inst->apc_conns, &conn, inst)) == -1){
-                                       radlog(L_ERR, "  [%s] All ldap connections are in use", inst->xlat_name);
-                                       return RLM_MODULE_FAIL;
-                               }
+               } else {
+                       RDEBUG("\"%s\" attribute exists - access denied by"
+                              " default", inst->access_attr);
+               }
 
-                               /*
-                                *      If there is an existing LDAP
-                                *      connection to the directory,
-                                *      bind over it. Otherwise,
-                                *      establish a new connection.
-                                */
-                       postauth_reconnect:
-                               if (!conn->bound || conn->ld == NULL) {
-                                       DEBUG2("  [%s] attempting LDAP reconnection", inst->xlat_name);
-                                       if (conn->ld){
-                                               DEBUG2("  [%s] closing existing LDAP connection", inst->xlat_name);
-                                               ldap_unbind_s(conn->ld);
-                                       }
-                                       if ((conn->ld = ldap_connect(instance, (char *)vp_fdn->vp_strvalue, password, 0, &res, &error_msg)) == NULL) {
-                                               radlog(L_ERR, "  [%s] eDirectory account policy check failed.", inst->xlat_name);
-
-                                               if (error_msg != NULL) {
-                                                       RDEBUG("%s", error_msg);
-                                                       pairadd(&request->reply->vps, pairmake("Reply-Message", error_msg, T_OP_EQ));
-                                                       ldap_memfree((void *)error_msg);
-                                               }
-
-                                               vp_apc->vp_strvalue[0] = '3';
-                                               ldap_release_apc_conn(conn_id, inst);
-                                               return RLM_MODULE_REJECT;
-                                       }
-                                       conn->bound = 1;
-                               } else if((err = ldap_simple_bind_s(conn->ld, (char *)vp_fdn->vp_strvalue, password)) != LDAP_SUCCESS) {
-                                       if (err == LDAP_SERVER_DOWN) {
-                                               conn->bound = 0;
-                                               goto postauth_reconnect;
-                                       }
-                                       RDEBUG("eDirectory account policy check failed.");
-                                       ldap_get_option(conn->ld, LDAP_OPT_ERROR_STRING, &error_msg);
-                                       if (error_msg != NULL) {
-                                               RDEBUG("%s", error_msg);
-                                               pairadd(&request->reply->vps, pairmake("Reply-Message", error_msg, T_OP_EQ));
-                                               ldap_memfree((void *)error_msg);
-                                       }
-                                       vp_apc->vp_strvalue[0] = '3';
-                                       ldap_release_apc_conn(conn_id, inst);
-                                       return RLM_MODULE_REJECT;
-                               }
-                               vp_apc->vp_strvalue[0] = '3';
-                               ldap_release_apc_conn(conn_id, inst);
-                               return RLM_MODULE_OK;
-                       }
+               ldap_value_free(vals);
+
+       } else if (inst->positive_access_attr) {
+               RDEBUG("No %s attribute - access denied by default",
+                      inst->access_attr);
+
+       } else {
+               rcode = 0;
        }
-       return res;
+
+       return rcode;
 }
-#endif
 
-#if LDAP_SET_REBIND_PROC_ARGS == 3
-static int ldap_rebind(LDAP *ld, LDAP_CONST char *url,
-                      UNUSED ber_tag_t request, UNUSED ber_int_t msgid,
-                      void *params )
+
+static VALUE_PAIR *ldap_getvalue(REQUEST *request, const value_pair_map_t *map,
+                                void *ctx)
 {
-       ldap_instance   *inst = params;
+       rlm_ldap_result_t *self = ctx;
+       VALUE_PAIR *head, **tail, *vp;
+       int i;
+       
+       request = request;
+       
+       head = NULL;
+       tail = &head;
+       
+       /*
+        *      Iterate over all the retrieved values,
+        *      don't try and be clever about changing operators
+        *      just use whatever was set in the attribute map. 
+        */
+       for (i = 0; i < self->count; i++) {
+               vp = pairalloc(NULL, map->dst->da);
+               rad_assert(vp);
 
-       DEBUG("  [%s] rebind to URL %s", inst->xlat_name,url);
-       return ldap_bind_s(ld, inst->login, inst->password, LDAP_AUTH_SIMPLE);
+               pairparsevalue(vp, self->values[i]);
+               
+               *tail = vp;
+               tail = &(vp->next);
+       }
+       
+       return head;            
 }
-#endif
 
-static LDAP *ldap_connect(void *instance, const char *dn, const char *password,
-                         int auth, int *result, char **err)
-{
-       ldap_instance  *inst = instance;
-       LDAP           *ld = NULL;
-       int             msgid, rc, ldap_version;
-       int             ldap_errno = 0;
-       LDAPMessage    *res;
-       struct timeval tv;
 
-       if (inst->is_url){
-#ifdef HAVE_LDAP_INITIALIZE
-               DEBUG("  [%s] (re)connect to %s, authentication %d", inst->xlat_name, inst->server, auth);
-               if (ldap_initialize(&ld, inst->server) != LDAP_SUCCESS) {
-                       exec_trigger(NULL, inst->cs, "modules.ldap.fail", FALSE);
-                       radlog(L_ERR, "  [%s] ldap_initialize() failed", inst->xlat_name);
-                       *result = RLM_MODULE_FAIL;
-                       return (NULL);
-               }
-#endif
-       } else {
-               DEBUG("  [%s] (re)connect to %s:%d, authentication %d", inst->xlat_name, inst->server, inst->port, auth);
-               if ((ld = ldap_init(inst->server, inst->port)) == NULL) {
-                       exec_trigger(NULL, inst->cs, "modules.ldap.fail", FALSE);
-                       radlog(L_ERR, "  [%s] ldap_init() failed", inst->xlat_name);
-                       *result = RLM_MODULE_FAIL;
-                       return (NULL);
+static void xlat_attrsfree(const xlat_attrs_t *expanded)
+{
+       const value_pair_map_t *map;
+       unsigned int total = 0;
+       
+       const char *name;
+       
+       for (map = expanded->maps; map != NULL; map = map->next)
+       {
+               name = expanded->attrs[total++];
+               if (!name) return;
+               
+               switch (map->src->type)
+               {
+               case VPT_TYPE_XLAT:             
+               case VPT_TYPE_ATTR:
+                       rad_cfree(name);
+                       break;
+               default:
+                       break;
                }
        }
+}
 
-       tv.tv_sec = inst->net_timeout;
-       tv.tv_usec = 0;
-       if (ldap_set_option(ld, LDAP_OPT_NETWORK_TIMEOUT,
-                           (void *) &tv) != LDAP_OPT_SUCCESS) {
-               ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
-               radlog(L_ERR, "  [%s] Could not set LDAP_OPT_NETWORK_TIMEOUT %d: %s", inst->xlat_name, inst->net_timeout, ldap_err2string(ldap_errno));
-       }
 
-       /*
-        *      Leave "chase_referrals" unset to use the OpenLDAP
-        *      default.
-        */
-       if (inst->chase_referrals != 2) {
-               if (inst->chase_referrals) {
-                       rc=ldap_set_option(ld, LDAP_OPT_REFERRALS,
-                                          LDAP_OPT_ON);
-                       
-#if LDAP_SET_REBIND_PROC_ARGS == 3
-                       if (inst->rebind == 1) {
-                               ldap_set_rebind_proc(ld, ldap_rebind,
-                                                    inst);
-                       }
-#endif
-               } else {
-                       rc=ldap_set_option(ld, LDAP_OPT_REFERRALS,
-                                          LDAP_OPT_OFF);
-               }
-               if (rc != LDAP_OPT_SUCCESS) {
-                       ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
-                       radlog(L_ERR, "  [%s] Could not set LDAP_OPT_REFERRALS=%d  %s", inst->xlat_name, inst->chase_referrals, ldap_err2string(ldap_errno));
-               }
-       }
+static int xlat_attrs(REQUEST *request, const value_pair_map_t *maps,
+                     xlat_attrs_t *expanded)
+{
+       const value_pair_map_t *map;
+       unsigned int total = 0;
+       
+       size_t len;
+       char *buffer;
 
-       if (ldap_set_option(ld, LDAP_OPT_TIMELIMIT,
-                           (void *) &(inst->timelimit)) != LDAP_OPT_SUCCESS) {
-               ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
-               radlog(L_ERR, "  [%s] Could not set LDAP_OPT_TIMELIMIT %d: %s", inst->xlat_name, inst->timelimit, ldap_err2string(ldap_errno));
-       }
+       VALUE_PAIR *found, **from = NULL;
+       REQUEST *context;
 
-       if (inst->ldap_debug && ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &(inst->ldap_debug)) != LDAP_OPT_SUCCESS) {
-               ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
-               radlog(L_ERR, "  [%s] Could not set LDAP_OPT_DEBUG_LEVEL %d: %s", inst->xlat_name, inst->ldap_debug, ldap_err2string(ldap_errno));
+       for (map = maps; map != NULL; map = map->next)
+       {
+               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 <= 0) {
+                               RDEBUG("Expansion of LDAP attribute "
+                                      "\"%s\" failed", map->src->name);
+                                      
+                               goto error;
+                       }
+                       
+                       expanded->attrs[total++] = buffer;
+                       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;
+       expanded->maps = maps;
+       
+       return 0;
+}
 
-       ldap_version = LDAP_VERSION3;
-       if (ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION,
-                           &ldap_version) != LDAP_OPT_SUCCESS) {
-               ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
-               radlog(L_ERR, "  [%s] Could not set LDAP version to V3: %s", inst->xlat_name, ldap_err2string(ldap_errno));
-       }
 
-#ifdef LDAP_OPT_X_KEEPALIVE_IDLE
-       if (ldap_set_option(ld, LDAP_OPT_X_KEEPALIVE_IDLE,
-                           (void *) &(inst->keepalive_idle)) != LDAP_OPT_SUCCESS) {
-               ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
-               radlog(L_ERR, "  [%s] Could not set LDAP_OPT_X_KEEPALIVE_IDLE %d: %s", inst->xlat_name, inst->keepalive_idle, ldap_err2string(ldap_errno));
-       }
-#endif
+/** Convert attribute map into valuepairs
+ *
+ * Use the attribute map built earlier to convert LDAP values into valuepairs
+ * and insert them into whichever list they need to go into.
+ *
+ * This is *NOT* atomic, but there's no condition in which we should error
+ * out...
+ */
+static void do_attrmap(UNUSED ldap_instance *inst, REQUEST *request,
+                      LDAP *handle, const xlat_attrs_t *expanded,
+                      LDAPMessage *entry)
+{
+       const value_pair_map_t  *map;
+       unsigned int            total = 0;
+       
+       rlm_ldap_result_t       result;
+       const char              *name;
 
-#ifdef LDAP_OPT_X_KEEPALIVE_PROBES
-       if (ldap_set_option(ld, LDAP_OPT_X_KEEPALIVE_PROBES,
-                           (void *) &(inst->keepalive_probes)) != LDAP_OPT_SUCCESS) {
-               ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
-               radlog(L_ERR, "  [%s] Could not set LDAP_OPT_X_KEEPALIVE_PROBES %d: %s", inst->xlat_name, inst->keepalive_probes, ldap_err2string(ldap_errno));
+       for (map = expanded->maps; map != NULL; map = map->next)
+       {
+               name = expanded->attrs[total++];
+               
+               result.values = ldap_get_values(handle, entry, name);
+               if (!result.values) {
+                       RDEBUG2("Attribute \"%s\" not found in LDAP object",
+                               name);
+                               
+                       goto next;
+               }
+               
+               /*
+                *      Find out how many values there are for the
+                *      attribute and extract all of them.
+                */
+               result.count = ldap_count_values(result.values);
+               
+               /*
+                *      If something bad happened, just skip, this is probably
+                *      a case of the dst being incorrect for the current
+                *      request context
+                */
+               if (radius_map2request(request, map, name, ldap_getvalue,
+                                      &result) < 0) {
+                       goto next;
+               }
+               
+               next:
+               
+               ldap_value_free(result.values);
        }
-#endif
+}
 
-#ifdef LDAP_OPT_X_KEEPALIVE_INTERVAL
-       if (ldap_set_option(ld, LDAP_OPT_X_KEEPALIVE_INTERVAL,
-                           (void *) &(inst->keepalive_interval)) != LDAP_OPT_SUCCESS) {
-               ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
-               radlog(L_ERR, "  [%s] Could not set LDAP_OPT_X_KEEPALIVE_INTERVAL %d: %s", inst->xlat_name, inst->keepalive_interval, ldap_err2string(ldap_errno));
-       }
-#endif
 
-#ifdef HAVE_LDAP_START_TLS
-        if (inst->tls_mode) {
-               DEBUG("  [%s] setting TLS mode to %d", inst->xlat_name, inst->tls_mode);
-               if (ldap_set_option(ld, LDAP_OPT_X_TLS,
-                                   (void *) &(inst->tls_mode)) != LDAP_OPT_SUCCESS) {
-                       ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
-                       radlog(L_ERR, "  [%s] could not set LDAP_OPT_X_TLS option %s:", inst->xlat_name, ldap_err2string(ldap_errno));
+static void do_check_reply(ldap_instance *inst, REQUEST *request)
+{
+       /*
+       *       More warning messages for people who can't be bothered
+       *       to read the documentation.
+       */
+       if (inst->expect_password && (debug_flag > 1)) {
+               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?");
                }
-       }
+       }
+}
 
-       if (inst->tls_cacertfile != NULL) {
-               DEBUG("  [%s] setting TLS CACert File to %s", inst->xlat_name, inst->tls_cacertfile);
 
-               if ( ldap_set_option( NULL, LDAP_OPT_X_TLS_CACERTFILE,
-                                     (void *) inst->tls_cacertfile )
-                    != LDAP_OPT_SUCCESS) {
-                       ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
-                       radlog(L_ERR, "  [%s] could not set "
-                              "LDAP_OPT_X_TLS_CACERTFILE option to %s: %s",
-                              inst->xlat_name, 
-                              inst->tls_cacertfile,
-                              ldap_err2string(ldap_errno));
-               }
-       }
+static void apply_profile(ldap_instance *inst, REQUEST *request,
+                         LDAP_CONN **pconn, const char *profile,
+                         const xlat_attrs_t *expanded)
+{
+       int rcode;
+       LDAPMessage     *result, *entry;
+       int             ldap_errno;
+       LDAP            *handle = (*pconn)->handle;
+       char            filter[MAX_FILTER_STR_LEN];
 
-       if (inst->tls_cacertdir != NULL) {
-               DEBUG("  [%s] setting TLS CACert Directory to %s", inst->xlat_name, inst->tls_cacertdir);
+       if (!profile || !*profile) return;
 
-               if ( ldap_set_option( NULL, LDAP_OPT_X_TLS_CACERTDIR,
-                                     (void *) inst->tls_cacertdir )
-                    != LDAP_OPT_SUCCESS) {
-                       ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
-                       radlog(L_ERR, "  [%s] could not set "
-                              "LDAP_OPT_X_TLS_CACERTDIR option to %s: %s",
-                              inst->xlat_name, 
-                              inst->tls_cacertdir,
-                              ldap_err2string(ldap_errno));
+       strlcpy(filter, inst->base_filter, sizeof(filter));
+
+       rcode = perform_search(inst, request, pconn, profile, LDAP_SCOPE_BASE,
+                              filter, expanded->attrs, &result);
+               
+       if (rcode < 0) {
+               if (rcode == -2) {
+                       RDEBUG("Profile \"%s\" not found", profile);
                }
+               goto free_result;
        }
 
-       if (strcmp(TLS_DEFAULT_VERIFY, inst->tls_require_cert ) != 0 ) {
-               DEBUG("  [%s] setting TLS Require Cert to %s", inst->xlat_name,
-                     inst->tls_require_cert);
+       entry = ldap_first_entry(handle, result);
+       if (!entry) {
+               ldap_get_option(handle, LDAP_OPT_RESULT_CODE,
+                               &ldap_errno);
+               radlog(L_ERR, "rlm_ldap (%s): Failed retrieving entry: %s", 
+                      inst->xlat_name,
+                      ldap_err2string(ldap_errno));
+                      
+               goto free_result;
        }
+       
+       do_attrmap(inst, request, handle, expanded, entry);
 
+free_result:
+       ldap_msgfree(result);
+}
 
-#ifdef HAVE_LDAP_INT_TLS_CONFIG
-       if (ldap_int_tls_config(NULL, LDAP_OPT_X_TLS_REQUIRE_CERT,
-                               (inst->tls_require_cert)) != LDAP_OPT_SUCCESS) {
-               ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
-               radlog(L_ERR, "  [%s] could not set ", 
-                      "LDAP_OPT_X_TLS_REQUIRE_CERT option to %s: %s",
-                      inst->xlat_name, 
-                      inst->tls_require_cert,
-                      ldap_err2string(ldap_errno));
-       }
-#endif
 
-       if (inst->tls_certfile != NULL) {
-               DEBUG("  [%s] setting TLS Cert File to %s", inst->xlat_name, inst->tls_certfile);
-
-               if (ldap_set_option(NULL, LDAP_OPT_X_TLS_CERTFILE,
-                                   (void *) inst->tls_certfile)
-                   != LDAP_OPT_SUCCESS) {
-                       ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
-                       radlog(L_ERR, "  [%s] could not set "
-                              "LDAP_OPT_X_TLS_CERTFILE option to %s: %s",
-                              inst->xlat_name, 
-                              inst->tls_certfile,
-                              ldap_err2string(ldap_errno));
-               }
+/** Check if user is authorized for remote access
+ *
+ */
+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 = NULL;
+       char            **vals;
+       VALUE_PAIR      *vp;
+       LDAP_CONN       *conn;
+       LDAPMessage     *result, *entry;
+       int             ldap_errno;
+       char            filter[MAX_FILTER_STR_LEN];
+       char            basedn[MAX_FILTER_STR_LEN];
+       xlat_attrs_t    expanded; /* faster that mallocing every time */
+       
+       if (!request->username) {
+               RDEBUG2("Attribute \"User-Name\" is required for "
+                       "authorization.");
+               return RLM_MODULE_NOOP;
        }
 
-       if (inst->tls_keyfile != NULL) {
-               DEBUG("  [%s] setting TLS Key File to %s", inst->xlat_name,
-                     inst->tls_keyfile);
-
-               if ( ldap_set_option( NULL, LDAP_OPT_X_TLS_KEYFILE,
-                                     (void *) inst->tls_keyfile )
-                    != LDAP_OPT_SUCCESS) {
-                       ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
-                       radlog(L_ERR, "  [%s] could not set "
-                              "LDAP_OPT_X_TLS_KEYFILE option to %s: %s",
-                              inst->xlat_name, 
-                              inst->tls_keyfile, ldap_err2string(ldap_errno));
-               }
+       /*
+        *      Check for valid input, zero length names not permitted
+        */
+       if (request->username->length == 0) {
+               RDEBUG2("Zero length username not permitted");
+               return RLM_MODULE_INVALID;
        }
 
-       if (inst->tls_randfile != NULL) {
-               DEBUG("  [%s] setting TLS Key File to %s", inst->xlat_name,
-                     inst->tls_randfile);
+       if (!radius_xlat(filter, sizeof(filter), inst->filter,
+                        request, ldap_escape_func, NULL)) {
+               radlog(L_ERR, "rlm_ldap (%s): Failed creating filter",
+                      inst->xlat_name);
+               return RLM_MODULE_INVALID;
+       }
 
-               if (ldap_set_option(NULL, LDAP_OPT_X_TLS_RANDOM_FILE,
-                                   (void *) inst->tls_randfile)
-                   != LDAP_OPT_SUCCESS) {
-                       ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
-                       radlog(L_ERR, "  [%s] could not set "
-                              "LDAP_OPT_X_TLS_RANDOM_FILE option to %s: %s",
-                              inst->xlat_name,
-                              inst->tls_randfile, ldap_err2string(ldap_errno));
-               }
+       if (!radius_xlat(basedn, sizeof(basedn), inst->basedn,
+                        request, ldap_escape_func, NULL)) {
+               radlog(L_ERR, "rlm_ldap (%s): Failed creating basedn",
+                      inst->xlat_name);
+               return RLM_MODULE_INVALID;
+       }
+       
+       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);
+       
+       if (rcode < 0) {
+               if (rcode == -2) {
+                       module_failure_msg(request,
+                                          "rlm_ldap (%s): User object not "
+                                          " found",
+                                          inst->xlat_name);
+                                          
+                       RDEBUG("User object not found", inst->xlat_name);
+                              
+                       module_rcode = RLM_MODULE_NOTFOUND;
+                       goto free_socket;
+               }
+
+               goto free_socket;
+       }
+
+       entry = ldap_first_entry(conn->handle, result);
+       if (!entry) {
+               ldap_get_option(conn->handle, LDAP_OPT_RESULT_CODE,
+                               &ldap_errno);
+               radlog(L_ERR, "rlm_ldap (%s): Failed retrieving entry: %s", 
+                      inst->xlat_name,
+                      ldap_err2string(ldap_errno));
+                      
+               goto free_result;
        }
 
-       if (inst->start_tls) {
-               DEBUG("  [%s] starting TLS", inst->xlat_name);
-               rc = ldap_start_tls_s(ld, NULL, NULL);
-               if (rc != LDAP_SUCCESS) {
-                       DEBUG("  [%s] ldap_start_tls_s()", inst->xlat_name);
-                       ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER,
-                                       &ldap_errno);
-                       radlog(L_ERR, "  [%s] could not start TLS %s", inst->xlat_name,
-                              ldap_err2string(ldap_errno));
-                       *result = RLM_MODULE_FAIL;
-                       ldap_unbind_s(ld);
-                       exec_trigger(NULL, inst->cs, "modules.ldap.fail", FALSE);
-                       return (NULL);
-               }
+       user_dn = ldap_get_dn(conn->handle, entry);
+       if (!user_dn) {
+               ldap_get_option(conn->handle, LDAP_OPT_RESULT_CODE,
+                               &ldap_errno);
+               radlog(L_ERR, "rlm_ldap (%s): ldap_get_dn() failed: %s",
+                      inst->xlat_name,
+                      ldap_err2string(ldap_errno));
+               goto free_result;
        }
-#endif /* HAVE_LDAP_START_TLS */
+       
+       RDEBUG2("User found at DN \"%s\"", user_dn);
+       /*
+        *      Adding attribute containing the Users' DN.
+        */
+       pairadd(&request->config_items,
+               pairmake("Ldap-UserDn", user_dn, T_OP_EQ));
 
-       if (inst->is_url){
-               DEBUG("  [%s] bind as %s/%s to %s", inst->xlat_name,
-                     dn, password, inst->server);
-       } else {
-               DEBUG("  [%s] bind as %s/%s to %s:%d", inst->xlat_name,
-                     dn, password, inst->server, inst->port);
+#ifdef WITH_EDIR
+       /*
+        *      We already have a Cleartext-Password.  Skip edir.
+        */
+       if (inst->edir && pairfind(request->config_items, PW_CLEARTEXT_PASSWORD, 0, TAG_ANY)) {
+               goto skip_edir;
        }
 
-       msgid = ldap_bind(ld, dn, password,LDAP_AUTH_SIMPLE);
-       if (msgid == -1) {
-               ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
-               if(err != NULL){
-                       ldap_get_option(ld, LDAP_OPT_ERROR_STRING, err);
-               }
-               if (inst->is_url) {
-                       radlog(L_ERR, "  [%s] %s bind to %s failed: %s", inst->xlat_name,
-                               dn, inst->server, ldap_err2string(ldap_errno));
-               } else {
-                       radlog(L_ERR, "  [%s] %s bind to %s:%d failed: %s", inst->xlat_name,
-                               dn, inst->server, inst->port,
-                               ldap_err2string(ldap_errno));
+       /*
+        *      Retrieve Universal Password if we use eDirectory
+        */
+       if (inst->edir) {
+               int res = 0;
+               size_t bufsize;
+               char buffer[256];
+
+               bufsize = sizeof(buffer);
+
+               /* retrive universal password */
+               res = nmasldap_get_password(conn->handle, user_dn,
+                                           buffer, &bufsize);
+               if (res != 0) {
+                       RDEBUG2("Failed to retrieve eDirectory password. Check "
+                               "your configuration !");
+                       module_rcode = RLM_MODULE_NOOP;
+                       goto free_result;
+               }
+
+               /* Add Cleartext-Password attribute to the request */
+               vp = radius_paircreate(request, &request->config_items,
+                                      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->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);
                }
-               *result = RLM_MODULE_FAIL;
-               ldap_unbind_s(ld);
-               return (NULL);
        }
-       DEBUG("  [%s] waiting for bind result ...", inst->xlat_name);
 
-       tv.tv_sec = inst->timeout;
-       tv.tv_usec = 0;
-       rc = ldap_result(ld, msgid, 1, &tv, &res);
+skip_edir:
+#endif
 
-       if (rc < 1) {
-               DEBUG("  [%s] ldap_result()", inst->xlat_name);
-               ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ldap_errno);
-               if(err != NULL){
-                       ldap_get_option(ld, LDAP_OPT_ERROR_STRING, err);
-               }
-               if (inst->is_url) {
-                       radlog(L_ERR, "  [%s] %s bind to %s failed: %s", inst->xlat_name,
-                               dn, inst->server, (rc == 0) ? "timeout" : ldap_err2string(ldap_errno));
-               } else {
-                       radlog(L_ERR, "  [%s] %s bind to %s:%d failed: %s", inst->xlat_name,
-                              dn, inst->server, inst->port,
-                               (rc == 0) ? "timeout" : ldap_err2string(ldap_errno));
+       /*
+        *      Check for access.
+        */
+       if (inst->access_attr) {
+               if (check_access(inst, request, conn, entry) < 0) {
+                       module_rcode = RLM_MODULE_USERLOCK;
+                       goto free_result;
                }
-               *result = RLM_MODULE_FAIL;
-               ldap_unbind_s(ld);
-               return (NULL);
        }
 
-       ldap_errno = ldap_result2error(ld, res, 1);
-       switch (ldap_errno) {
-       case LDAP_SUCCESS:
-               DEBUG("  [%s] Bind was successful", inst->xlat_name);
-               *result = RLM_MODULE_OK;
-               break;
+       /*
+        *      Apply ONE user profile, or a default user profile.
+        */
+       vp = pairfind(request->config_items, PW_USER_PROFILE, 0, TAG_ANY);
+       if (vp || inst->default_profile) {
+               char *profile = inst->default_profile;
 
-       case LDAP_INVALID_CREDENTIALS:
-               if (auth){
-                       DEBUG("  [%s] Bind failed with invalid credentials", inst->xlat_name);
-                       *result = RLM_MODULE_REJECT;
-               } else {
-                       radlog(L_ERR, "  [%s] LDAP login failed: check identity, password settings in ldap section of radiusd.conf", inst->xlat_name);
-                       *result = RLM_MODULE_FAIL;
-               }
-               if(err != NULL){
-                       ldap_get_option(ld, LDAP_OPT_ERROR_STRING, err);
-               }
-               break;
+               if (vp) profile = vp->vp_strvalue;
 
-       case LDAP_CONSTRAINT_VIOLATION:
-               DEBUG("rlm_ldap: Bind failed with constraint violation");
-               *result = RLM_MODULE_REJECT;
-               if(err != NULL){
-                       ldap_get_option(ld, LDAP_OPT_ERROR_STRING, err);
-               }
-               break;
+               apply_profile(inst, request, &conn, profile, &expanded);
+       }
 
-       default:
-               if (inst->is_url) {
-                       radlog(L_ERR,"  [%s] %s bind to %s failed %s", inst->xlat_name,
-                               dn, inst->server, ldap_err2string(ldap_errno));
-               } else {
-                       radlog(L_ERR,"  [%s] %s bind to %s:%d failed %s", inst->xlat_name,
-                               dn, inst->server, inst->port,
-                               ldap_err2string(ldap_errno));
-               }
-               *result = RLM_MODULE_FAIL;
-               if(err != NULL){
-                       ldap_get_option(ld, LDAP_OPT_ERROR_STRING, err);
+       /*
+        *      Apply a SET of user profiles.
+        */
+       if (inst->profile_attr) {
+               vals = ldap_get_values(conn->handle, entry, inst->profile_attr);
+               if (vals != NULL) {
+                       int i;
+       
+                       for (i = 0; (vals[i] != NULL) && (*vals[i] != '\0');
+                            i++) {
+                               apply_profile(inst, request, &conn, vals[i],
+                                             &expanded);
+                       }
+       
+                       ldap_value_free(vals);
                }
        }
 
-       if (*result != RLM_MODULE_OK) {
-               ldap_unbind_s(ld);
-               ld = NULL;
+       if (inst->user_map) {
+               do_attrmap(inst, request, conn->handle, &expanded, entry);
+               do_check_reply(inst, request);
        }
-       return ld;
+       
+free_result:
+       if (user_dn) ldap_memfree(user_dn);
+       xlat_attrsfree(&expanded);
+       ldap_msgfree(result);
+free_socket:
+       ldap_release_socket(inst, conn);
+
+       return module_rcode;
 }
 
-/*****************************************************************************
- *
- *     Detach from the LDAP server and cleanup internal state.
+
+/** Check the user's password against ldap database
  *
- *****************************************************************************/
-static int
-ldap_detach(void *instance)
+ */
+static rlm_rcode_t ldap_authenticate(void *instance, REQUEST * request)
 {
-       ldap_instance  *inst = instance;
-       TLDAP_RADIUS *pair, *nextpair;
-
-       if (inst->conns) {
-               int i;
+       rlm_rcode_t     module_rcode;
+       const char      *user_dn;
+       ldap_instance   *inst = instance;
+       LDAP_CONN       *conn;
 
-               for (i = 0;i < inst->num_conns; i++) {
-                       if (inst->conns[i].locked) return -1;
+       /*
+        * Ensure that we're being passed a plain-text password, and not
+        * anything else.
+        */
 
-                       if (inst->conns[i].ld){
-                               ldap_unbind_s(inst->conns[i].ld);
-                       }
-                       pthread_mutex_destroy(&inst->conns[i].mutex);
-               }
-               free(inst->conns);
+       if (!request->username) {
+               radlog(L_AUTH, "rlm_ldap (%s): Attribute \"User-Name\" is "
+                      "required for authentication", inst->xlat_name);
+               return RLM_MODULE_INVALID;
        }
 
-#ifdef NOVELL
-       if (inst->apc_conns){
-               int i;
-
-               for (i = 0; i < inst->num_conns; i++) {
-                       if (inst->apc_conns[i].locked) return -1;
-
-                       if (inst->apc_conns[i].ld){
-                               ldap_unbind_s(inst->apc_conns[i].ld);
-                       }
-                       pthread_mutex_destroy(&inst->apc_conns[i].mutex);
-               }
-               free(inst->apc_conns);
+       if (!request->password) {
+               radlog(L_AUTH, "rlm_ldap (%s): Attribute \"User-Password\" "
+                      "is required for authentication.", inst->xlat_name);
+               RDEBUG2("  You have set \"Auth-Type := LDAP\" somewhere.");
+               RDEBUG2("  *********************************************");
+               RDEBUG2("  * THAT CONFIGURATION IS WRONG.  DELETE IT.   ");
+               RDEBUG2("  * YOU ARE PREVENTING THE SERVER FROM WORKING.");
+               RDEBUG2("  *********************************************");
+               return RLM_MODULE_INVALID;
        }
-#endif
-
-       pair = inst->check_item_map;
 
-       while (pair != NULL) {
-               nextpair = pair->next;
-               free(pair->attr);
-               free(pair->radius_attr);
-               free(pair);
-               pair = nextpair;
+       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->da->name);
+               return RLM_MODULE_INVALID;
        }
 
-       pair = inst->reply_item_map;
-
-       while (pair != NULL) {
-               nextpair = pair->next;
-               free(pair->attr);
-               free(pair->radius_attr);
-               free(pair);
-               pair = nextpair;
+       if (request->password->length == 0) {
+               module_failure_msg(request,
+                                  "rlm_ldap (%s): Empty password supplied",
+                                  inst->xlat_name);
+               return RLM_MODULE_INVALID;
        }
 
-       if (inst->atts)
-               free(inst->atts);
-
-       paircompare_unregister(PW_LDAP_GROUP, ldap_groupcmp);
-       xlat_unregister(inst->xlat_name,ldap_xlat, instance);
-       free(inst->xlat_name);
-
-       free(inst);
-
-       return 0;
-}
-
+       RDEBUG("Login attempt by \"%s\" with password \"%s\"",
+              request->username->vp_strvalue, request->password->vp_strvalue);
 
-#ifdef FIELDCPY
-static void
-fieldcpy(char *string, char **uptr)
-{
-       char           *ptr;
+       conn = ldap_get_socket(inst);
+       if (!conn) return RLM_MODULE_FAIL;
 
-       ptr = *uptr;
-       while (*ptr == ' ' || *ptr == '\t') {
-               ptr++;
-       }
-       if (*ptr == '"') {
-               ptr++;
-               while (*ptr != '"' && *ptr != '\0' && *ptr != '\n') {
-                       *string++ = *ptr++;
-               }
-               *string = '\0';
-               if (*ptr == '"') {
-                       ptr++;
-               }
-               *uptr = ptr;
-               return;
+       /*
+        *      Get the DN by doing a search.
+        */
+       user_dn = get_userdn(&conn, request, &module_rcode);
+       if (!user_dn) {
+               ldap_release_socket(inst, conn);
+               return module_rcode;
        }
-       while (*ptr != ' ' && *ptr != '\t' && *ptr != '\0' && *ptr != '\n' &&
-              *ptr != '=' && *ptr != ',') {
-               *string++ = *ptr++;
+
+       /*
+        *      Bind as the user
+        */
+       conn->rebound = TRUE;
+       module_rcode = ldap_bind_wrapper(&conn, user_dn,
+                                        request->password->vp_strvalue,
+                                        TRUE);
+       if (module_rcode == RLM_MODULE_OK) {
+               RDEBUG("Bind as user \"%s\" was successful", user_dn);
        }
-       *string = '\0';
-       *uptr = ptr;
-       return;
-}
-#endif
 
-/*
- *     Copied from src/lib/token.c
- */
-static const FR_NAME_NUMBER tokens[] = {
-       { "=~", T_OP_REG_EQ,    }, /* order is important! */
-       { "!~", T_OP_REG_NE,    },
-       { "{",  T_LCBRACE,      },
-       { "}",  T_RCBRACE,      },
-       { "(",  T_LBRACE,       },
-       { ")",  T_RBRACE,       },
-       { ",",  T_COMMA,        },
-       { "+=", T_OP_ADD,       },
-       { "-=", T_OP_SUB,       },
-       { ":=", T_OP_SET,       },
-       { "=*", T_OP_CMP_TRUE,  },
-       { "!*", T_OP_CMP_FALSE, },
-       { "==", T_OP_CMP_EQ,    },
-       { "=",  T_OP_EQ,        },
-       { "!=", T_OP_NE,        },
-       { ">=", T_OP_GE,        },
-       { ">",  T_OP_GT,        },
-       { "<=", T_OP_LE,        },
-       { "<",  T_OP_LT,        },
-       { NULL, 0}
-};
+       ldap_release_socket(inst, conn);
+       return module_rcode;
+}
 
-/*****************************************************************************
- *     Get RADIUS attributes from LDAP object
- *     ( according to draft-adoba-radius-05.txt
- *       <http://www.ietf.org/internet-drafts/draft-adoba-radius-05.txt> )
+/** Modify user's object in LDAP
  *
- *****************************************************************************/
-static VALUE_PAIR *ldap_pairget(LDAP *ld, LDAPMessage *entry,
-                               TLDAP_RADIUS *item_map,
-                               VALUE_PAIR **pairs, int is_check,
-                               ldap_instance *inst)
+ */
+static rlm_rcode_t user_modify(ldap_instance *inst, REQUEST *request,
+                              ldap_acct_section_t *section)
 {
-       char          **vals;
-       int             vals_count;
-       int             vals_idx;
-       const char      *ptr;
-       const char     *value;
-       TLDAP_RADIUS   *element;
-       FR_TOKEN      token, operator;
-       int             is_generic_attribute;
-       char            buf[MAX_STRING_LEN];
-       VALUE_PAIR     *pairlist = NULL;
-       VALUE_PAIR     *newpair = NULL;
-       char            do_xlat = FALSE;
-       char            print_buffer[2048];
+       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;
 
        /*
-        *      check if there is a mapping from this LDAP attribute
-        *      to a RADIUS attribute
+        *      Build our set of modifications using the update sections in
+        *      the config.
+        */
+       CONF_ITEM       *ci;
+       CONF_PAIR       *cp;
+       CONF_SECTION    *cs;
+       FR_TOKEN        op;
+       char            path[MAX_STRING_LEN];
+       
+       char            *p = path;
+
+       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;     
+       }
+
+       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 (element = item_map; element != NULL; element = element->next) {
+       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;
+               }
+       
                /*
-                *      No mapping, skip it.
+                *      Retrieve all the information we need about the pair
                 */
-               if ((vals = ldap_get_values(ld,entry,element->attr)) == NULL)
+               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:
+                       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)
+               {
                /*
-                *      Check whether this is a one-to-one-mapped ldap
-                *      attribute or a generic attribute and set flag
-                *      accordingly.
+                *  T_OP_EQ is *NOT* supported, it is impossible to
+                *  support because of the lack of transactions in LDAP
                 */
-               if (strcasecmp(element->radius_attr, GENERIC_ATTRIBUTE_ID)==0)
-                       is_generic_attribute = 1;
-               else
-                       is_generic_attribute = 0;
+               case T_OP_ADD:
+                       mod_s[total].mod_op = LDAP_MOD_ADD;
+                       break;
+
+               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;
+               }
+               
                /*
-                *      Find out how many values there are for the
-                *      attribute and extract all of them.
+                *      Now we know the value is ok, copy the pointers into
+                *      the ldapmod struct.
                 */
-               vals_count = ldap_count_values(vals);
-
-               for (vals_idx = 0; vals_idx < vals_count; vals_idx++) {
-                       value = vals[vals_idx];
-
-                       if (is_generic_attribute) {
-                               /*
-                                *      This is a generic attribute.
-                                */
-                               FR_TOKEN dummy; /* makes pairread happy */
-
-                               /* not sure if using pairread here is ok ... */
-                               if ( (newpair = pairread(&value, &dummy)) != NULL) {
-                                       DEBUG("  [%s] extracted attribute %s from generic item %s", inst->xlat_name,
-                                             newpair->name, vals[vals_idx]);
-                                       pairadd(&pairlist, newpair);
-                               } else {
-                                       radlog(L_ERR, "  [%s] parsing %s failed: %s", inst->xlat_name,
-                                              element->attr, vals[vals_idx]);
-                               }
-                       } else {
-                               /*
-                                *      This is a one-to-one-mapped attribute
-                                */
-                               ptr = value;
-                               operator = gettoken(&ptr, buf, sizeof(buf));
-                               if (operator < T_EQSTART || operator > T_EQEND) {
-                                       /* no leading operator found */
-                                       if (element->operator != T_OP_INVALID)
-                                               operator = element->operator;
-                                       else if (is_check)
-                                               operator = T_OP_CMP_EQ;
-                                       else
-                                               operator = T_OP_EQ;
-                               } else {
-                                       /* the value is after the operator */
-                                       value = ptr;
-                               }
+               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, TRUE);
+               if (ldap_errno != RLM_MODULE_OK) {
+                       goto error;
+               }
 
-                               /*
-                                *      Do xlat if the *entire* string
-                                *      is quoted.
-                                */
-                               if ((value[0] == '\'' || value[0] == '"' ||
-                                    value[0] == '`') &&
-                                   (value[0] == value[strlen(value)-1])) {
-                                       ptr = value;
-                                       token = gettoken(&ptr, buf, sizeof(buf));
-                                       switch (token) {
-                                       /* take the unquoted string */
-                                       case T_SINGLE_QUOTED_STRING:
-                                       case T_DOUBLE_QUOTED_STRING:
-                                               value = buf;
-                                               break;
-
-                                       /* the value will be xlat'ed later */
-                                       case T_BACK_QUOTED_STRING:
-                                               value = buf;
-                                               do_xlat = TRUE;
-                                               break;
-
-                                       /* keep the original string */
-                                       default:
-                                               break;
-                                       }
-                               }
-                               if (value[0] == '\0') {
-                                       DEBUG("  [%s] Attribute %s has no value", inst->xlat_name, element->attr);
-                                       continue;
-                               }
+               rad_assert(conn != NULL);
+               conn->rebound = FALSE;
+       }
 
-                               /*
-                                *      Create the pair.
-                                */
-                               if (do_xlat) {
-                                       newpair = pairmake_xlat(element->radius_attr,
-                                                               value,
-                                                               operator);
-                               } else {
-                                       newpair = pairmake(element->radius_attr,
-                                                          value,
-                                                          operator);
-                               }
-                               if (newpair == NULL) {
-                                       radlog(L_ERR, "  [%s] Failed to create the pair: %s", inst->xlat_name, fr_strerror());
-                                       continue;
-                               }
+       user_dn = get_userdn(&conn, request, &module_rcode);
+       if (!user_dn) {
+               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);
 
-                               vp_prints(print_buffer, sizeof(print_buffer),
-                                         newpair);
-                               DEBUG("  [%s] %s -> %s", inst->xlat_name,
-                                     element->attr, print_buffer);
+       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
+        */     
+       for (i = 0; i < last_exp; i++) {
+               free(expanded[i]);
+       }
+       
 
+       ldap_release_socket(inst, conn);
+       
+       return module_rcode;
+}
 
-                               /*
-                                *      Add the pair into the packet.
-                                */
-                               if (!vals_idx){
-                                 pairdelete(pairs, newpair->attribute, newpair->vendor);
-                               }
-                               pairadd(&pairlist, newpair);
-                       }
-               }
-               ldap_value_free(vals);
+
+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 (pairlist);
+       return RLM_MODULE_NOOP;
 }
 
+
 /* globally exported name */
 module_t rlm_ldap = {
        RLM_MODULE_INIT,
-       "LDAP",
+       "ldap",
        RLM_TYPE_THREAD_SAFE,   /* type: reserved        */
        ldap_instantiate,       /* instantiation         */
        ldap_detach,            /* detach                */
@@ -2859,14 +2464,10 @@ 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 NOVELL
-               ldap_postauth           /* post-auth             */
-#else
-               NULL
-#endif
+               ldap_postauth           /* post-auth */
        },
 };