]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/overlays/ppolicy.c
Mroe abandon checks for ITS#3671
[openldap] / servers / slapd / overlays / ppolicy.c
index e32585ccb28ff7f55330c1321f444102ffbb68cd..322b2b1009711ce4a8129938384275b48abffa2f 100644 (file)
@@ -1,7 +1,9 @@
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 2004 Howard Chu, Symas Corporation.
+ * Copyright 2004-2005 The OpenLDAP Foundation.
+ * Portions Copyright 2004 Howard Chu, Symas Corporation.
+ * Portions Copyright 2004 Hewlett-Packard Company.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * top-level directory of the distribution or, alternatively, at
  * <http://www.OpenLDAP.org/license.html>.
  */
+/* ACKNOWLEDGEMENTS:
+ * This work was developed by Howard Chu for inclusion in
+ * OpenLDAP Software, based on prior work by Neil Dunbar (HP).
+ * This work was sponsored by the Hewlett-Packard Company.
+ */
 
 #include "portable.h"
 
 /* This file implements "Password Policy for LDAP Directories",
- * based on draft behera-ldap-password-policy-07
+ * based on draft behera-ldap-password-policy-08
  */
 
 #ifdef SLAPD_OVER_PPOLICY
 #include <ldap.h>
 #include "lutil.h"
 #include "slap.h"
+#if SLAPD_MODULES
+#define LIBLTDL_DLL_IMPORT     /* Win32: don't re-export libltdl's symbols */
 #include <ltdl.h>
+#endif
 #include <ac/errno.h>
 #include <ac/time.h>
 #include <ac/string.h>
+#include <ac/ctype.h>
 
 #ifndef MODULE_NAME_SZ
 #define MODULE_NAME_SZ 256
@@ -41,6 +52,7 @@
 typedef struct pp_info {
        struct berval def_policy;       /* DN of default policy subentry */
        int use_lockout;                /* send AccountLocked result? */
+       int hash_passwords;             /* transparently hash cleartext pwds */
 } pp_info;
 
 /* Our per-connection info - note, it is not per-instance, it is 
@@ -51,6 +63,7 @@ typedef struct pw_conn {
 } pw_conn;
 
 static pw_conn *pwcons;
+static int ppolicy_cid;
 
 typedef struct pass_policy {
        AttributeDescription *ad; /* attribute to which the policy applies */
@@ -62,7 +75,7 @@ typedef struct pass_policy {
        int pwdMinLength; /* minimum number of chars in password */
        int pwdExpireWarning; /* number of seconds that warning controls are
                                                        sent before a password expires */
-       int pwdGraceLoginLimit; /* number of times you can log in with an
+       int pwdGraceAuthNLimit; /* number of times you can log in with an
                                                        expired password */
        int pwdLockout; /* 0 = do not lockout passwords, 1 = lock them out */
        int pwdLockoutDuration; /* time in seconds a password is locked out for */
@@ -89,8 +102,8 @@ typedef struct pw_hist {
 
 /* Operational attributes */
 static AttributeDescription *ad_pwdChangedTime, *ad_pwdAccountLockedTime,
-       *ad_pwdExpirationWarned, *ad_pwdFailureTime, *ad_pwdHistory,
-       *ad_pwdGraceUseTime, *ad_pwdReset, *ad_pwdPolicySubentry;
+       *ad_pwdFailureTime, *ad_pwdHistory, *ad_pwdGraceUseTime, *ad_pwdReset,
+       *ad_pwdPolicySubentry;
 
 static struct schema_info {
        char *def;
@@ -102,7 +115,7 @@ static struct schema_info {
                "EQUALITY generalizedTimeMatch "
                "ORDERING generalizedTimeOrderingMatch "
                "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
-               "SINGLE-VALUE USAGE directoryOperation )",
+               "SINGLE-VALUE USAGE directoryOperation NO-USER-MODIFICATION )",
                &ad_pwdChangedTime },
        {       "( 1.3.6.1.4.1.42.2.27.8.1.17 "
                "NAME ( 'pwdAccountLockedTime' ) "
@@ -112,14 +125,6 @@ static struct schema_info {
                "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
                "SINGLE-VALUE USAGE directoryOperation )",
                &ad_pwdAccountLockedTime },
-       {       "( 1.3.6.1.4.1.42.2.27.8.1.18 "
-               "NAME ( 'pwdExpirationWarned' ) "
-               "DESC 'The time the user was first warned about the coming expiration of the password' "
-               "EQUALITY generalizedTimeMatch "
-               "ORDERING generalizedTimeOrderingMatch "
-               "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
-               "SINGLE-VALUE USAGE directoryOperation )",
-               &ad_pwdExpirationWarned },
        {       "( 1.3.6.1.4.1.42.2.27.8.1.19 "
                "NAME ( 'pwdFailureTime' ) "
                "DESC 'The timestamps of the last consecutive authentication failures' "
@@ -133,14 +138,14 @@ static struct schema_info {
                "DESC 'The history of users passwords' "
                "EQUALITY octetStringMatch "
                "SYNTAX 1.3.6.1.4.1.1466.115.121.1.40 "
-               "USAGE directoryOperation )",
+               "USAGE directoryOperation NO-USER-MODIFICATION )",
                &ad_pwdHistory },
        {       "( 1.3.6.1.4.1.42.2.27.8.1.21 "
                "NAME ( 'pwdGraceUseTime' ) "
                "DESC 'The timestamps of the grace login once the password has expired' "
                "EQUALITY generalizedTimeMatch "
                "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
-               "USAGE directoryOperation )",
+               "USAGE directoryOperation NO-USER-MODIFICATION )",
                &ad_pwdGraceUseTime }, 
        {       "( 1.3.6.1.4.1.42.2.27.8.1.22 "
                "NAME ( 'pwdReset' ) "
@@ -162,7 +167,7 @@ static struct schema_info {
 /* User attributes */
 static AttributeDescription *ad_pwdMinAge, *ad_pwdMaxAge, *ad_pwdInHistory,
        *ad_pwdCheckQuality, *ad_pwdMinLength, *ad_pwdMaxFailure, 
-       *ad_pwdGraceLoginLimit, *ad_pwdExpireWarning, *ad_pwdLockoutDuration,
+       *ad_pwdGraceAuthNLimit, *ad_pwdExpireWarning, *ad_pwdLockoutDuration,
        *ad_pwdFailureCountInterval, *ad_pwdCheckModule, *ad_pwdLockout,
        *ad_pwdMustChange, *ad_pwdAllowUserChange, *ad_pwdSafeModify,
        *ad_pwdAttribute;
@@ -177,7 +182,7 @@ static struct schema_info pwd_UsSchema[] = {
        TAB(pwdCheckQuality),
        TAB(pwdMinLength),
        TAB(pwdMaxFailure),
-       TAB(pwdGraceLoginLimit),
+       TAB(pwdGraceAuthNLimit),
        TAB(pwdExpireWarning),
        TAB(pwdLockout),
        TAB(pwdLockoutDuration),
@@ -191,68 +196,18 @@ static struct schema_info pwd_UsSchema[] = {
 
 static ldap_pvt_thread_mutex_t chk_syntax_mutex;
 
-static time_t
-ppolicy_timegm( struct tm *t )
-{
-       static int moffset[12] = {
-               0, 31, 59, 90, 120,
-               151, 181, 212, 243,
-               273, 304, 334 }; 
-       time_t ret; 
-
-       /* t->tm_year is years since 1900 */
-       /* calculate days from years since 1970 (epoch) */ 
-       ret = t->tm_year - 70; 
-       ret *= 365L; 
-
-       /* count leap days in preceding years */ 
-       ret += ((t->tm_year -69) >> 2); 
-
-       /* calculate days from months */ 
-       ret += moffset[t->tm_mon]; 
-
-       /* add in this year's leap day, if any */ 
-       if (((t->tm_year & 3) == 0) && (t->tm_mon > 1)) { 
-               ret ++; 
-       } 
-
-       /* add in days in this month */ 
-       ret += (t->tm_mday - 1); 
-
-       /* convert to hours */ 
-       ret *= 24L; 
-       ret += t->tm_hour; 
-
-       /* convert to minutes */ 
-       ret *= 60L; 
-       ret += t->tm_min; 
-
-       /* convert to seconds */ 
-       ret *= 60L; 
-       ret += t->tm_sec; 
-
-       /* return the result */ 
-       return ret; 
-}
-
 static time_t
 parse_time( char *atm )
 {
-       struct tm tm;
+       struct lutil_tm tm;
+       struct lutil_timet tt;
+       time_t ret = (time_t)-1;
 
-       if (!atm) return (time_t)-1;
-
-       /*
-        * special case - if the lowest allowable GeneralizedTime is here, return
-        * this is as zero time. Note: this might also be the case if the value stored
-        * is equivalent to the start of the epoch (ie, Jan 1, 1970 at midnight.
-        */
-       if (strcmp(atm, "00000101000000Z") == 0) return (time_t)0;
-       /*
-        * else parse the time and return it's time_t value.
-        */
-       strptime( atm, "%Y%m%d%H%M%SZ", &tm );
-       return ppolicy_timegm( &tm );
+       if ( lutil_parsetime( atm, &tm ) == 0) {
+               lutil_tm2time( &tm, &tt );
+               ret = tt.tt_sec;
+       }
+       return ret;
 }
 
 static int
@@ -361,7 +316,6 @@ ppolicy_get( Operation *op, Entry *e, PassPolicy *pp )
        const char *text;
        AttributeDescription *ad;
        struct berval bv;
-       void *opr = op->o_private;
 
        memset( pp, 0, sizeof(PassPolicy) );
 
@@ -378,19 +332,12 @@ ppolicy_get( Operation *op, Entry *e, PassPolicy *pp )
        } else {
                vals = a->a_nvals;
                if (vals[0].bv_val == NULL) {
-#ifdef NEW_LOGGING
-                       LDAP_LOG( OPERATION, ERR,
-                               "ppolicy_get: NULL value for policySubEntry\n", 0, 0, 0 );
-#else
                        Debug( LDAP_DEBUG_ANY,
                                "ppolicy_get: NULL value for policySubEntry\n", 0, 0, 0 );
-#endif
                        goto defaultpol;
                }
        }
 
-       /* back-bdb stores lock info in o_private */
-       op->o_private = NULL;
        op->o_bd->bd_info = (BackendInfo *)on->on_info;
        rc = be_entry_get_rw( op, vals, NULL, NULL, 0, &pe );
        op->o_bd->bd_info = (BackendInfo *)on;
@@ -416,8 +363,8 @@ ppolicy_get( Operation *op, Entry *e, PassPolicy *pp )
                pp->pwdMinLength = atoi(a->a_vals[0].bv_val );
        if ((a = attr_find( pe->e_attrs, ad_pwdMaxFailure )))
                pp->pwdMaxFailure = atoi(a->a_vals[0].bv_val );
-       if ((a = attr_find( pe->e_attrs, ad_pwdGraceLoginLimit )))
-               pp->pwdGraceLoginLimit = atoi(a->a_vals[0].bv_val );
+       if ((a = attr_find( pe->e_attrs, ad_pwdGraceAuthNLimit )))
+               pp->pwdGraceAuthNLimit = atoi(a->a_vals[0].bv_val );
        if ((a = attr_find( pe->e_attrs, ad_pwdExpireWarning )))
                pp->pwdExpireWarning = atoi(a->a_vals[0].bv_val );
        if ((a = attr_find( pe->e_attrs, ad_pwdFailureCountInterval )))
@@ -444,18 +391,11 @@ ppolicy_get( Operation *op, Entry *e, PassPolicy *pp )
        be_entry_release_r( op, pe );
        op->o_bd->bd_info = (BackendInfo *)on;
 
-       op->o_private = opr;
        return;
 
 defaultpol:
-#ifdef NEW_LOGGING
-       LDAP_LOG( OPERATION, DETAIL1,
-               "ppolicy_get: using default policy\n", 0, 0, 0 );
-#else
        Debug( LDAP_DEBUG_ANY,
                "ppolicy_get: using default policy\n", 0, 0, 0 );
-#endif
-       op->o_private = opr;
        return;
 }
 
@@ -491,7 +431,7 @@ password_scheme( struct berval *cred, struct berval *sch )
 }
 
 static int
-check_password_quality( struct berval *cred, PassPolicy *pp, LDAPPasswordPolicyError *err )
+check_password_quality( struct berval *cred, PassPolicy *pp, LDAPPasswordPolicyError *err, Entry *e )
 {
        int rc = LDAP_SUCCESS, ok = LDAP_SUCCESS;
        char *ptr = cred->bv_val;
@@ -537,56 +477,39 @@ check_password_quality( struct berval *cred, PassPolicy *pp, LDAPPasswordPolicyE
        }
 
        rc = LDAP_SUCCESS;
-    
+
        if (pp->pwdCheckModule[0]) {
+#if SLAPD_MODULES
                lt_dlhandle mod;
                const char *err;
                
                if ((mod = lt_dlopen( pp->pwdCheckModule )) == NULL) {
                        err = lt_dlerror();
 
-#ifdef NEW_LOGGING
-                       LDAP_LOG( SLAPD, CRIT,
-                       "check_password_quality: lt_dlopen failed: (%s) %s.\n",
-                               pp->pwdCheckModule, err, 0 );
-#else
                        Debug(LDAP_DEBUG_ANY,
                        "check_password_quality: lt_dlopen failed: (%s) %s.\n",
                                pp->pwdCheckModule, err, 0 );
-#endif
                        ok = LDAP_OTHER; /* internal error */
                } else {
-                       int (*prog)( char *passwd, char **text, void *arg );
+                       int (*prog)( char *passwd, char **text, Entry *ent );
 
                        if ((prog = lt_dlsym( mod, "check_password" )) == NULL) {
                                err = lt_dlerror();
                            
-#ifdef NEW_LOGGING
-                               LDAP_LOG( SLAPD, CRIT,
-                                       "check_password_quality: lt_dlsym failed: (%s) %s.\n",
-                                       pp->pwdCheckModule, err, 0 );
-#else
                                Debug(LDAP_DEBUG_ANY,
                                        "check_password_quality: lt_dlsym failed: (%s) %s.\n",
                                        pp->pwdCheckModule, err, 0 );
-#endif
                                ok = LDAP_OTHER;
                        } else {
                                char *txt = NULL;
 
                                ldap_pvt_thread_mutex_lock( &chk_syntax_mutex );
-                               ok = prog( cred->bv_val, &txt, NULL );
+                               ok = prog( cred->bv_val, &txt, e );
                                ldap_pvt_thread_mutex_unlock( &chk_syntax_mutex );
                                if (txt) {
-#ifdef NEW_LOGGING
-                                       LDAP_LOG( SLAPD, CRIT,
-                                               "check_password_quality: module error (%s) %s[%d].\n",
-                                               pp->pwdCheckModule, txt, ok );
-#else
                                        Debug(LDAP_DEBUG_ANY,
                                                "check_password_quality: module error: (%s) %s.[%d]\n",
                                                pp->pwdCheckModule, txt, ok );
-#endif
                                        free(txt);
                                } else
                                        ok = LDAP_SUCCESS;
@@ -594,6 +517,10 @@ check_password_quality( struct berval *cred, PassPolicy *pp, LDAPPasswordPolicyE
                            
                        lt_dlclose( mod );
                }
+#else
+       Debug(LDAP_DEBUG_ANY, "check_password_quality: external modules not "
+               "supported. pwdCheckModule ignored.\n", 0, 0, 0);
+#endif /* SLAPD_MODULES */
        }
                
                    
@@ -893,16 +820,10 @@ ppolicy_bind_resp( Operation *op, SlapReply *rs )
                                 * missing if the DIT was established via
                                 * an import process.
                                 */
-#ifdef NEW_LOGGING
-                               LDAP_LOG( OPERATION, ERR,
-                                       "ppolicy_bind: Entry %s does not have valid pwdChangedTime attribute - assuming password expired\n",
-                                       e->e_name.bv_val, 0, 0);
-#else
                                Debug( LDAP_DEBUG_ANY,
                                        "ppolicy_bind: Entry %s does not have valid pwdChangedTime attribute - assuming password expired\n",
                                        e->e_name.bv_val, 0, 0);
                                
-#endif
                                pwExpired = 1;
                        } else {
                                /*
@@ -918,24 +839,18 @@ grace:
                if (!pwExpired) goto check_expiring_password;
                
                if ((a = attr_find( e->e_attrs, ad_pwdGraceUseTime )) == NULL)
-                       ngut = ppb->pp.pwdGraceLoginLimit;
+                       ngut = ppb->pp.pwdGraceAuthNLimit;
                else {
                        for(ngut=0; a->a_nvals[ngut].bv_val; ngut++);
-                       ngut = ppb->pp.pwdGraceLoginLimit - ngut;
+                       ngut = ppb->pp.pwdGraceAuthNLimit - ngut;
                }
 
                /*
                 * ngut is the number of remaining grace logins
                 */
-#ifdef NEW_LOGGING
-               LDAP_LOG( OPERATION, ERR,
-                       "ppolicy_bind: Entry %s has an expired password: %d grace logins\n",
-                       e->e_name.bv_val, ngut, 0);
-#else
                Debug( LDAP_DEBUG_ANY,
                        "ppolicy_bind: Entry %s has an expired password: %d grace logins\n",
                        e->e_name.bv_val, ngut, 0);
-#endif
                
                if (ngut < 1) {
                        ppb->pErr = PP_passwordExpired;
@@ -979,31 +894,14 @@ check_expiring_password:
                 */
                if (ppb->pp.pwdMaxAge - age < ppb->pp.pwdExpireWarning ) {
                        /*
-                        * Set the warning value, add expiration warned timestamp to the entry.
+                        * Set the warning value.
                         */
-                       if ((a = attr_find( e->e_attrs, ad_pwdExpirationWarned )) == NULL) {
-                               m = ch_calloc( sizeof(Modifications), 1 );
-                               m->sml_op = LDAP_MOD_ADD;
-                               m->sml_type = ad_pwdExpirationWarned->ad_cname;
-                               m->sml_desc = ad_pwdExpirationWarned;
-                               m->sml_values = ch_calloc( sizeof(struct berval), 2 );
-                               ber_str2bv( nowstr, 0, 1, &m->sml_values[0] );
-                               m->sml_next = mod;
-                               mod = m;
-                       }
-                       
                        warn = ppb->pp.pwdMaxAge - age; /* seconds left until expiry */
                        if (warn < 0) warn = 0; /* something weird here - why is pwExpired not set? */
                        
-#ifdef NEW_LOGGING
-                       LDAP_LOG( OPERATIONS, DETAIL1,
-                               "ppolicy_bind: Setting warning for password expiry for %s = %d seconds\n",
-                               op->o_req_dn.bv_val, warn, 0 );
-#else
                        Debug( LDAP_DEBUG_ANY,
                                "ppolicy_bind: Setting warning for password expiry for %s = %d seconds\n",
                                op->o_req_dn.bv_val, warn, 0 );
-#endif
                }
        }
 
@@ -1080,13 +978,8 @@ ppolicy_bind( Operation *op, SlapReply *rs )
                op->o_callback->sc_next = cb;
 
                /* Did we receive a password policy request control? */
-               for ( i=0; op->o_ctrls && op->o_ctrls[i]; i++ ) {
-                       if ( !strcmp( op->o_ctrls[i]->ldctl_oid,
-                               LDAP_CONTROL_PASSWORDPOLICYREQUEST ) )
-                       {
-                               ppb->send_ctrl = 1;
-                               break;
-                       }
+               if ( op->o_ctrlflag[ppolicy_cid] ) {
+                       ppb->send_ctrl = 1;
                }
 
                op->o_bd->bd_info = (BackendInfo *)on;
@@ -1127,22 +1020,13 @@ ppolicy_restrict(
        int i, send_ctrl = 0;
 
        /* Did we receive a password policy request control? */
-       for ( i=0; op->o_ctrls && op->o_ctrls[i]; i++ ) {
-               if ( !strcmp( op->o_ctrls[i]->ldctl_oid,
-                       LDAP_CONTROL_PASSWORDPOLICYREQUEST ) ) {
-                       send_ctrl = 1;
-                       break;
-               }
+       if ( op->o_ctrlflag[ppolicy_cid] ) {
+               send_ctrl = 1;
        }
 
        if ( op->o_conn && pwcons[op->o_conn->c_conn_idx].restrict ) {
-#ifdef NEW_LOGGING
-               LDAP_LOG( OPERATION, ENTRY,
-                       "connection restricted to password changing only\n", 0, 0, 0 );
-#else
                Debug( LDAP_DEBUG_TRACE,
                        "connection restricted to password changing only\n", 0, 0, 0);
-#endif
                if ( send_ctrl ) {
                        LDAPControl **ctrls = NULL;
 
@@ -1152,7 +1036,7 @@ ppolicy_restrict(
                        rs->sr_ctrls = ctrls;
                }
                op->o_bd->bd_info = (BackendInfo *)on->on_info;
-               send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
+               send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS, 
                        "Operations are restricted to bind/unbind/abandon/StartTLS/modify password" );
                return rs->sr_err;
        }
@@ -1166,9 +1050,11 @@ ppolicy_add(
        SlapReply *rs )
 {
        slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
+       pp_info *pi = on->on_bi.bi_private;
        PassPolicy pp;
        int pw;
        Attribute *pa;
+       const char *txt;
 
        if ( ppolicy_restrict( op, rs ) != SLAP_CB_CONTINUE )
                return rs->sr_err;
@@ -1182,21 +1068,17 @@ ppolicy_add(
                 * then we need to check that the password fits in with the
                 * security policy for the new entry.
                 */
-               ppolicy_get( op, op->oq_add.rs_e, &pp );
+               ppolicy_get( op, op->ora_e, &pp );
                if (pp.pwdCheckQuality > 0 && !be_isroot( op )) {
                        struct berval *bv = &(pa->a_vals[0]);
                        int rc, i, send_ctrl = 0; 
                        LDAPPasswordPolicyError pErr = PP_noError;
 
                        /* Did we receive a password policy request control? */
-                       for ( i=0; op->o_ctrls && op->o_ctrls[i]; i++ ) {
-                               if ( !strcmp( op->o_ctrls[i]->ldctl_oid,
-                                       LDAP_CONTROL_PASSWORDPOLICYREQUEST ) ) {
-                                       send_ctrl = 1;
-                                       break;
-                               }
+                       if ( op->o_ctrlflag[ppolicy_cid] ) {
+                               send_ctrl = 1;
                        }
-                       rc = check_password_quality( bv, &pp, &pErr );
+                       rc = check_password_quality( bv, &pp, &pErr, op->ora_e );
                        if (rc != LDAP_SUCCESS) {
                                op->o_bd->bd_info = (BackendInfo *)on->on_info;
                                if ( send_ctrl ) {
@@ -1210,9 +1092,40 @@ ppolicy_add(
                                send_ldap_error( op, rs, rc, "Password fails quality checking policy" );
                                return rs->sr_err;
                        }
+                           /*
+                            * A controversial bit. We hash cleartext
+                            * passwords provided via add and modify operations
+                            * You're not really supposed to do this, since
+                            * the X.500 model says "store attributes" as they
+                            * get provided. By default, this is what we do
+                            *
+                            * But if the hash_passwords flag is set, we hash
+                            * any cleartext password attribute values via the
+                            * default password hashing scheme.
+                            */
+                       if ((pi->hash_passwords) &&
+                               (password_scheme( &(pa->a_vals[0]), NULL ) != LDAP_SUCCESS)) {
+                               struct berval hpw;
+
+                               slap_passwd_hash( &(pa->a_vals[0]), &hpw, &txt );
+                               if (hpw.bv_val == NULL) {
+                                   /*
+                                    * hashing didn't work. Emit an error.
+                                    */
+                                       rs->sr_err = LDAP_OTHER;
+                                       rs->sr_text = txt;
+                                       send_ldap_error( op, rs, LDAP_OTHER, "Password hashing failed" );
+                                       return rs->sr_err;
+                               }
+
+                               memset( pa->a_vals[0].bv_val, 0, pa->a_vals[0].bv_len);
+                               ber_memfree( pa->a_vals[0].bv_val );
+                               pa->a_vals[0].bv_val = hpw.bv_val;
+                               pa->a_vals[0].bv_len = hpw.bv_len;
+                       }
                }
                /* If password aging is in effect, set the pwdChangedTime */
-               if ( pp.pwdMaxAge || pp.pwdMinAge ) {
+               if (( pp.pwdMaxAge || pp.pwdMinAge ) && !be_shadow_update( op )) {
                        struct berval timestamp;
                        char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
                        struct tm *ltm;
@@ -1235,17 +1148,19 @@ ppolicy_add(
 static int
 ppolicy_modify( Operation *op, SlapReply *rs )
 {
-       slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
-       int                             i, rc, mod_pw_only, pwmod, pwmop, deladd, hsize = 0;
+       slap_overinst           *on = (slap_overinst *)op->o_bd->bd_info;
+       pp_info                 *pi = on->on_bi.bi_private;
+       int                     i, rc, mod_pw_only, pwmod, pwmop, deladd,
+                               hsize = 0;
        PassPolicy              pp;
-       Modifications   *mods = NULL, *modtail, *ml, *delmod, *addmod;
+       Modifications           *mods = NULL, *modtail, *ml, *delmod, *addmod;
        Attribute               *pa, *ha, *ra, at;
-       int                             repl_user = be_isupdate( op->o_bd, &op->o_ndn );
        const char              *txt;
        pw_hist                 *tl = NULL, *p;
-       int                             zapReset, send_ctrl = 0;
+       int                     zapReset, send_ctrl = 0;
        Entry                   *e;
-       struct berval   newpw = { 0, NULL }, oldpw = { 0, NULL }, *bv, cr[2];
+       struct berval           newpw = BER_BVNULL, oldpw = BER_BVNULL,
+                               *bv, cr[2];
        LDAPPasswordPolicyError pErr = PP_noError;
 
        op->o_bd->bd_info = (BackendInfo *)on->on_info;
@@ -1255,12 +1170,8 @@ ppolicy_modify( Operation *op, SlapReply *rs )
        if ( rc != LDAP_SUCCESS ) return SLAP_CB_CONTINUE;
 
        /* Did we receive a password policy request control? */
-       for ( i=0; op->o_ctrls && op->o_ctrls[i]; i++ ) {
-               if ( !strcmp( op->o_ctrls[i]->ldctl_oid,
-                       LDAP_CONTROL_PASSWORDPOLICYREQUEST ) ) {
-                       send_ctrl = 1;
-                       break;
-               }
+       if ( op->o_ctrlflag[ppolicy_cid] ) {
+               send_ctrl = 1;
        }
 
        /* See if this is a pwdModify exop. If so, we can
@@ -1327,14 +1238,9 @@ ppolicy_modify( Operation *op, SlapReply *rs )
        }
        
        if (pwcons[op->o_conn->c_conn_idx].restrict && !mod_pw_only) {
-#ifdef NEW_LOGGING
-               LDAP_LOG( OPERATION, ENTRY,
-                       "connection restricted to password changing only\n", 0, 0, 0 );
-#else
                Debug( LDAP_DEBUG_TRACE,
                        "connection restricted to password changing only\n", 0, 0, 0 );
-#endif
-               rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
+               rs->sr_err = LDAP_INSUFFICIENT_ACCESS; 
                rs->sr_text = "Operations are restricted to bind/unbind/abandon/StartTLS/modify password";
                pErr = PP_changeAfterReset;
                goto return_results;
@@ -1357,13 +1263,8 @@ ppolicy_modify( Operation *op, SlapReply *rs )
        if (!addmod) {
                rs->sr_err = LDAP_OTHER;
                rs->sr_text = "Internal Error";
-#ifdef NEW_LOGGING
-               LDAP_LOG( OPERATION, ENTRY,
-                       "cannot locate modification supplying new password\n", 0, 0, 0 );
-#else
                Debug( LDAP_DEBUG_TRACE,
                        "cannot locate modification supplying new password\n", 0, 0, 0 );
-#endif
                goto return_results;
        }
 
@@ -1416,23 +1317,17 @@ ppolicy_modify( Operation *op, SlapReply *rs )
        }
 
        if (pp.pwdSafeModify && deladd != 2) {
-#ifdef NEW_LOGGING
-               LDAP_LOG( OPERATION, ENTRY,
-                       "change password must use DELETE followed by ADD/REPLACE\n",
-                       0, 0, 0 );
-#else
                Debug( LDAP_DEBUG_TRACE,
                        "change password must use DELETE followed by ADD/REPLACE\n",
                        0, 0, 0 );
-#endif
-               rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
+               rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
                rs->sr_text = "Must supply old password to be changed as well as new one";
                pErr = PP_mustSupplyOldPassword;
                goto return_results;
        }
 
        if (!pp.pwdAllowUserChange) {
-               rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
+               rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
                rs->sr_text = "User alteration of password is not allowed";
                pErr = PP_passwordModNotAllowed;
                goto return_results;
@@ -1447,7 +1342,7 @@ ppolicy_modify( Operation *op, SlapReply *rs )
                now = slap_get_time();
                age = (int)(now - pwtime);
                if ((pwtime != (time_t)-1) && (age < pp.pwdMinAge)) {
-                       rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
+                       rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
                        rs->sr_text = "Password is too young to change";
                        pErr = PP_passwordTooYoung;
                        goto return_results;
@@ -1462,15 +1357,11 @@ ppolicy_modify( Operation *op, SlapReply *rs )
                const char *txt;
                
                bv = oldpw.bv_val ? &oldpw : delmod->sml_values;
-               rc = slap_passwd_check( op->o_conn, pa, bv, &txt );
+               /* FIXME: no access checking? */
+               rc = slap_passwd_check( op, NULL, pa, bv, &txt );
                if (rc != LDAP_SUCCESS) {
-#ifdef NEW_LOGGING
-                       LDAP_LOG( OPERATION, ENTRY,
-                               "old password check failed: %s\n", txt, 0, 0 );
-#else
                        Debug( LDAP_DEBUG_TRACE,
                                "old password check failed: %s\n", txt, 0, 0 );
-#endif
                        
                        rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
                        rs->sr_text = "Must supply correct old password to change to new one";
@@ -1499,7 +1390,7 @@ ppolicy_modify( Operation *op, SlapReply *rs )
        bv = newpw.bv_val ? &newpw : addmod->sml_values;
        if (pp.pwdCheckQuality > 0) {
 
-               rc = check_password_quality( bv, &pp, &pErr );
+               rc = check_password_quality( bv, &pp, &pErr, e );
                if (rc != LDAP_SUCCESS) {
                        rs->sr_err = rc;
                        rs->sr_text = "Password fails quality checking policy";
@@ -1507,43 +1398,47 @@ ppolicy_modify( Operation *op, SlapReply *rs )
                }
        }
 
-       /*
-        * Last check - the password history.
-        */
-       if (slap_passwd_check( op->o_conn, pa, bv, &txt ) == LDAP_SUCCESS) {
+       if (pa) {
                /*
-                * This is bad - it means that the user is attempting
-                * to set the password to the same as the old one.
+                * Last check - the password history.
                 */
-               rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
-               rs->sr_text = "Password is not being changed from existing value";
-               pErr = PP_passwordInHistory;
-               goto return_results;
-       }
-
-       if (pp.pwdInHistory < 1) goto do_modify;
-
-       /*
-        * Iterate through the password history, and fail on any
-        * password matches.
-        */
-       at = *pa;
-       at.a_vals = cr;
-       cr[1].bv_val = NULL;
-       for(p=tl; p; p=p->next) {
-               cr[0] = p->pw;
-               rc = slap_passwd_check( op->o_conn, &at, bv, &txt );
-               
-               if (rc != LDAP_SUCCESS) continue;
-               
-               rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
-               rs->sr_text = "Password is in history of old passwords";
-               pErr = PP_passwordInHistory;
-               goto return_results;
+               /* FIXME: no access checking? */
+               if (slap_passwd_check( op, NULL, pa, bv, &txt ) == LDAP_SUCCESS) {
+                       /*
+                        * This is bad - it means that the user is attempting
+                        * to set the password to the same as the old one.
+                        */
+                       rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
+                       rs->sr_text = "Password is not being changed from existing value";
+                       pErr = PP_passwordInHistory;
+                       goto return_results;
+               }
+       
+               if (pp.pwdInHistory < 1) goto do_modify;
+       
+               /*
+                * Iterate through the password history, and fail on any
+                * password matches.
+                */
+               at = *pa;
+               at.a_vals = cr;
+               cr[1].bv_val = NULL;
+               for(p=tl; p; p=p->next) {
+                       cr[0] = p->pw;
+                       /* FIXME: no access checking? */
+                       rc = slap_passwd_check( op, NULL, &at, bv, &txt );
+                       
+                       if (rc != LDAP_SUCCESS) continue;
+                       
+                       rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
+                       rs->sr_text = "Password is in history of old passwords";
+                       pErr = PP_passwordInHistory;
+                       goto return_results;
+               }
        }
 
 do_modify:
-       if ((pwmod) && (!repl_user)) {
+       if ((pwmod) && (!be_shadow_update( op ))) {
                struct berval timestamp;
                char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
                struct tm *ltm;
@@ -1593,18 +1488,6 @@ do_modify:
                        modtail = mods;
                }
 
-               if (attr_find(e->e_attrs, ad_pwdExpirationWarned )) {
-                       mods = (Modifications *) ch_malloc( sizeof( Modifications ) );
-                       mods->sml_op = LDAP_MOD_DELETE;
-                       mods->sml_type.bv_val = NULL;
-                       mods->sml_desc = ad_pwdExpirationWarned;
-                       mods->sml_values = NULL;
-                       mods->sml_nvalues = NULL;
-                       mods->sml_next = NULL;
-                       modtail->sml_next = mods;
-                       modtail = mods;
-               }
-
                /* Delete the pwdReset attribute, since it's being reset */
                if ((zapReset) && (attr_find(e->e_attrs, ad_pwdReset ))) {
                        mods = (Modifications *) ch_malloc( sizeof( Modifications ) );
@@ -1681,13 +1564,8 @@ do_modify:
                                modtail->sml_next = mods;
                                modtail = mods;
                        } else {
-#ifdef NEW_LOGGING
-                               LDAP_LOG ( OPERATION, ERR, 
-                               "ppolicy_modify: password attr lookup failed\n", 0, 0, 0 );
-#else
                                Debug( LDAP_DEBUG_TRACE,
                                "ppolicy_modify: password attr lookup failed\n", 0, 0, 0 );
-#endif
                        }
                }
 
@@ -1708,7 +1586,7 @@ do_modify:
                 * leave it alone.
                 */
 
-               if ((addmod) && !newpw.bv_val && 
+               if ((pi->hash_passwords) && (addmod) && !newpw.bv_val && 
                        (password_scheme( &(addmod->sml_values[0]), NULL ) != LDAP_SUCCESS)) {
                        struct berval hpw, bv;
                        
@@ -1764,6 +1642,7 @@ ppolicy_parseCtrl(
                rs->sr_text = "passwordPolicyRequest control invalid criticality";
                return LDAP_PROTOCOL_ERROR;
        }
+       op->o_ctrlflag[ppolicy_cid] = SLAP_CONTROL_NONCRITICAL;
 
        return LDAP_SUCCESS;
 }
@@ -1775,6 +1654,20 @@ ppolicy_db_init(
 {
        slap_overinst *on = (slap_overinst *) be->bd_info;
 
+       /* Has User Schema been initialized yet? */
+       if ( !pwd_UsSchema[0].ad[0] ) {
+               const char *err;
+               int i, code;
+
+               for (i=0; pwd_UsSchema[i].def; i++) {
+                       code = slap_str2ad( pwd_UsSchema[i].def, pwd_UsSchema[i].ad, &err );
+                       if ( code ) {
+                               fprintf( stderr, "User Schema Load failed %d: %s\n", code, err );
+                               return code;
+                       }
+               }
+       }
+
        on->on_bi.bi_private = ch_calloc( sizeof(pp_info), 1 );
 
        if ( dtblsize && !pwcons )
@@ -1783,6 +1676,14 @@ ppolicy_db_init(
        return 0;
 }
 
+static int
+ppolicy_db_open(
+    BackendDB *be
+)
+{
+       return overlay_register_control( be, LDAP_CONTROL_PASSWORDPOLICYREQUEST );
+}
+
 static int
 ppolicy_close(
        BackendDB *be
@@ -1825,6 +1726,7 @@ ppolicy_config(
                        return 1;
                }
                return 0;
+
        } else if ( strcasecmp( argv[0], "ppolicy_use_lockout" ) == 0 ) {
                if ( argc != 1 ) {
                        fprintf( stderr, "%s: line %d: ppolicy_use_lockout "
@@ -1832,6 +1734,14 @@ ppolicy_config(
                        return ( 1 );
                }
                pi->use_lockout = 1;
+               return 0;
+       } else if ( strcasecmp( argv[0], "ppolicy_hash_cleartext" ) == 0 ) {
+               if ( argc != 1 ) {
+                       fprintf( stderr, "%s: line %d: ppolicy_hash_cleartext "
+                               "takes no arguments\n", fname, lineno );
+                       return ( 1 );
+               }
+               pi->hash_passwords = 1;
        }
        return SLAP_CONF_UNKNOWN;
 }
@@ -1846,9 +1756,8 @@ static slap_overinst ppolicy;
 int ppolicy_init()
 {
        LDAPAttributeType *at;
-       int code;
        const char *err;
-       int i;
+       int i, code;
 
        for (i=0; pwd_OpSchema[i].def; i++) {
                at = ldap_str2attributetype( pwd_OpSchema[i].def, &code, &err,
@@ -1858,7 +1767,7 @@ int ppolicy_init()
                                ldap_scherr2str(code), err );
                        return code;
                }
-               code = at_add( at, &err );
+               code = at_add( at, 0, NULL, &err );
                if ( !code ) {
                        slap_str2ad( at->at_names[0], pwd_OpSchema[i].ad, &err );
                }
@@ -1870,17 +1779,9 @@ int ppolicy_init()
                }
        }
 
-       for (i=0; pwd_UsSchema[i].def; i++) {
-               code = slap_str2ad( pwd_UsSchema[i].def, pwd_UsSchema[i].ad, &err );
-               if ( code ) {
-                       fprintf( stderr, "User Schema Load failed %d: %s\n", code, err );
-                       return code;
-               }
-       }
-
        code = register_supported_control( LDAP_CONTROL_PASSWORDPOLICYREQUEST,
-               SLAP_CTRL_ADD|SLAP_CTRL_BIND|SLAP_CTRL_MODIFY, extops,
-               ppolicy_parseCtrl );
+               SLAP_CTRL_ADD|SLAP_CTRL_BIND|SLAP_CTRL_MODIFY|SLAP_CTRL_HIDE, extops,
+               ppolicy_parseCtrl, &ppolicy_cid );
        if ( code != LDAP_SUCCESS ) {
                fprintf( stderr, "Failed to register control %d\n", code );
                return code;
@@ -1890,6 +1791,7 @@ int ppolicy_init()
 
        ppolicy.on_bi.bi_type = "ppolicy";
        ppolicy.on_bi.bi_db_init = ppolicy_db_init;
+       ppolicy.on_bi.bi_db_open = ppolicy_db_open;
        ppolicy.on_bi.bi_db_config = ppolicy_config;
        ppolicy.on_bi.bi_db_close = ppolicy_close;