]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/overlays/ppolicy.c
Fix overlay aux_operational, add contextCSN as operational attribute
[openldap] / servers / slapd / overlays / ppolicy.c
index b8bce27efa590667d3d2bb6a96a26c3226102654..c22b295072bd277ea0f696be2ed2cc20e8e0e8b8 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 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"
 
 #include <ldap.h>
 #include "lutil.h"
 #include "slap.h"
+#if SLAPD_MODULES
 #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
@@ -40,6 +50,8 @@
 /* Per-instance configuration information */
 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 
@@ -50,8 +62,7 @@ typedef struct pw_conn {
 } pw_conn;
 
 static pw_conn *pwcons;
-
-static long gmtoff;    /* Offset of local time from GMT */
+static int ppolicy_cid;
 
 typedef struct pass_policy {
        AttributeDescription *ad; /* attribute to which the policy applies */
@@ -195,23 +206,15 @@ static ldap_pvt_thread_mutex_t chk_syntax_mutex;
 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. This will be -1 if the
-        * text isn't a valid time string.
-        */
-       strptime( atm, "%Y%m%d%H%M%SZ", &tm );
-       tm.tm_isdst = 0;
-       return mktime(&tm) - gmtoff;
+       if ( lutil_parsetime( atm, &tm ) == 0) {
+               lutil_tm2time( &tm, &tt );
+               ret = tt.tt_sec;
+       }
+       return ret;
 }
 
 static int
@@ -324,6 +327,9 @@ ppolicy_get( Operation *op, Entry *e, PassPolicy *pp )
 
        memset( pp, 0, sizeof(PassPolicy) );
 
+       /* Users can change their own password by default */
+       pp->pwdAllowUserChange = 1;
+
        if ((a = attr_find( e->e_attrs, ad_pwdPolicySubentry )) == NULL) {
                /*
                 * entry has no password policy assigned - use default
@@ -334,13 +340,8 @@ 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;
                }
        }
@@ -404,13 +405,8 @@ ppolicy_get( Operation *op, Entry *e, PassPolicy *pp )
        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;
 }
@@ -447,7 +443,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;
@@ -493,56 +489,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, Attribute *attrs );
 
                        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 ? e->e_attrs : NULL );
                                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;
@@ -550,6 +529,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 */
        }
                
                    
@@ -849,16 +832,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 {
                                /*
@@ -883,15 +860,9 @@ grace:
                /*
                 * 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;
@@ -951,15 +922,9 @@ check_expiring_password:
                        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
                }
        }
 
@@ -988,7 +953,12 @@ locked:
 
        if ( ppb->send_ctrl ) {
                LDAPControl **ctrls = NULL;
+               pp_info *pi = on->on_bi.bi_private;
 
+               /* Do we really want to tell that the account is locked? */
+               if ( ppb->pErr == PP_accountLocked && !pi->use_lockout ) {
+                       ppb->pErr = PP_noError;
+               }
                ctrls = ch_calloc( sizeof( LDAPControl *) , 2 );
                ctrls[0] = create_passcontrol( warn, ngut, ppb->pErr );
                ctrls[1] = NULL;
@@ -1004,7 +974,7 @@ ppolicy_bind( Operation *op, SlapReply *rs )
        slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
 
        /* Root bypasses policy */
-       if ( !be_isroot( op->o_bd, &op->o_req_ndn )) {
+       if ( !be_isroot_dn( op->o_bd, &op->o_req_ndn )) {
                Entry *e;
                int i, rc;
                ppbind *ppb;
@@ -1031,11 +1001,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;
@@ -1076,22 +1043,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;
 
@@ -1115,35 +1073,35 @@ 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;
 
        /* Check for password in entry */
-       if ((pa = attr_find( op->oq_add.rs_e->e_attrs, slap_schema.si_ad_userPassword ))) {
+       if ((pa = attr_find( op->oq_add.rs_e->e_attrs,
+               slap_schema.si_ad_userPassword )))
+       {
                /*
                 * new entry contains a password - if we're not the root user
                 * 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 );
-               if (pp.pwdCheckQuality > 0 && !be_isroot( op->o_bd, &op->o_ndn )) {
+               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 ) {
@@ -1157,9 +1115,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_isupdate( op )) {
                        struct berval timestamp;
                        char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
                        struct tm *ltm;
@@ -1182,17 +1171,20 @@ 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 );
+       int                     repl_user = be_isupdate( op );
        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;
@@ -1202,12 +1194,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
@@ -1274,13 +1262,8 @@ 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_text = "Operations are restricted to bind/unbind/abandon/StartTLS/modify password";
                pErr = PP_changeAfterReset;
@@ -1304,13 +1287,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;
        }
 
@@ -1319,7 +1297,7 @@ ppolicy_modify( Operation *op, SlapReply *rs )
         * We need this, even if the user is root, in order to maintain
         * the pwdHistory operational attributes properly.
         */
-       if (ha = attr_find( e->e_attrs, ad_pwdHistory )) {
+       if (pp.pwdInHistory > 0 && (ha = attr_find( e->e_attrs, ad_pwdHistory ))) {
                struct berval oldpw;
                time_t oldtime;
                char *oid;
@@ -1337,10 +1315,10 @@ ppolicy_modify( Operation *op, SlapReply *rs )
                                oldpw.bv_len = 0;
                        }
                }
+               for(p=tl; p; p=p->next, hsize++); /* count history size */
        }
-       for(p=tl, hsize=0; p; p=p->next, hsize++); /* count history size */
 
-       if (be_isroot( op->o_bd, &op->o_ndn)) goto do_modify;
+       if (be_isroot( op )) goto do_modify;
 
        /* This is a pwdModify exop that provided the old pw.
         * We need to create a Delete mod for this old pw and 
@@ -1363,15 +1341,9 @@ 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_text = "Must supply old password to be changed as well as new one";
                pErr = PP_mustSupplyOldPassword;
@@ -1401,7 +1373,8 @@ ppolicy_modify( Operation *op, SlapReply *rs )
                }
        }
 
-       if (delmod && (pa = attr_find( e->e_attrs, pp.ad )) != NULL) {
+       /* pa is used in password history check below, be sure it's set */
+       if ((pa = attr_find( e->e_attrs, pp.ad )) != NULL && delmod) {
                /*
                 * we have a password to check
                 */
@@ -1410,13 +1383,8 @@ ppolicy_modify( Operation *op, SlapReply *rs )
                bv = oldpw.bv_val ? &oldpw : delmod->sml_values;
                rc = slap_passwd_check( op->o_conn, 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";
@@ -1445,7 +1413,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";
@@ -1453,39 +1421,41 @@ 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;
+               if (slap_passwd_check( op->o_conn, 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;
+                       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;
+               }
        }
 
 do_modify:
@@ -1564,75 +1534,72 @@ do_modify:
                        modtail = mods;
                }
 
-               if (hsize >= pp.pwdInHistory) {
+               if (pp.pwdInHistory > 0) {
+                       if (hsize >= pp.pwdInHistory) {
+                               /*
+                                * We use the >= operator, since we are going to add
+                                * the existing password attribute value into the
+                                * history - thus the cardinality of history values is
+                                * about to rise by one.
+                                *
+                                * If this would push it over the limit of history
+                                * values (remembering - the password policy could have
+                                * changed since the password was last altered), we must
+                                * delete at least 1 value from the pwdHistory list.
+                                *
+                                * In fact, we delete '(#pwdHistory attrs - max pwd
+                                * history length) + 1' values, starting with the oldest.
+                                * This is easily evaluated, since the linked list is
+                                * created in ascending time order.
+                                */
+                               mods = (Modifications *) ch_malloc( sizeof( Modifications ) );
+                               mods->sml_op = LDAP_MOD_DELETE;
+                               mods->sml_type.bv_val = NULL;
+                               mods->sml_desc = ad_pwdHistory;
+                               mods->sml_nvalues = NULL;
+                               mods->sml_values = ch_calloc( sizeof( struct berval ),
+                                                                                          hsize - pp.pwdInHistory + 2 );
+                               mods->sml_values[ hsize - pp.pwdInHistory + 1 ].bv_val = NULL;
+                               mods->sml_values[ hsize - pp.pwdInHistory + 1 ].bv_len = 0;
+                               for(i=0,p=tl; i < (hsize - pp.pwdInHistory + 1); i++, p=p->next) {
+                                       mods->sml_values[i].bv_val = NULL;
+                                       mods->sml_values[i].bv_len = 0;
+                                       ber_dupbv( &(mods->sml_values[i]), &p->bv );
+                               }
+                               mods->sml_next = NULL;
+                               modtail->sml_next = mods;
+                               modtail = mods;
+                       }
+                       free_pwd_history_list( &tl );
+
                        /*
-                        * We use the >= operator, since we are going to add a
-                        * the existing password attribute value into the
-                        * history - thus the cardinality of history values is
-                        * about to rise by one.
+                        * Now add the existing password into the history list.
+                        * This will be executed even if the operation is to delete
+                        * the password entirely.
                         *
-                        * If this would push it over the limit of history
-                        * values (remembering - the password policy could have
-                        * changed since the password was last altered), we must
-                        * delete at least 1 value from the pwdHistory list.
-                        *
-                        * In fact, we delete '(#pwdHistory attrs - max pwd
-                        * history length) + 1' values, starting with the oldest.
-                        * This is easily evaluated, since the linked list is
-                        * created in ascending time order.
+                        * This isn't in the spec explicitly, but it seems to make
+                        * sense that the password history list is the list of all
+                        * previous passwords - even if they were deleted. Thus, if
+                        * someone tries to add a historical password at some future
+                        * point, it will fail.
                         */
-                       mods = (Modifications *) ch_malloc( sizeof( Modifications ) );
-                       mods->sml_op = LDAP_MOD_DELETE;
-                       mods->sml_type.bv_val = NULL;
-                       mods->sml_desc = ad_pwdHistory;
-                       mods->sml_nvalues = NULL;
-                       mods->sml_values = ch_calloc( sizeof( struct berval ),
-                                                                                  hsize - pp.pwdInHistory + 2 );
-                       mods->sml_values[ hsize - pp.pwdInHistory + 1 ].bv_val = NULL;
-                       mods->sml_values[ hsize - pp.pwdInHistory + 1 ].bv_len = 0;
-                       for(i=0,p=tl; i < (hsize - pp.pwdInHistory + 1); i++, p=p->next) {
-                               mods->sml_values[i].bv_val = NULL;
-                               mods->sml_values[i].bv_len = 0;
-                               ber_dupbv( &(mods->sml_values[i]), &p->bv );
+                       if ((pa = attr_find( e->e_attrs, pp.ad )) != NULL) {
+                               mods = (Modifications *) ch_malloc( sizeof( Modifications ) );
+                               mods->sml_op = LDAP_MOD_ADD;
+                               mods->sml_type.bv_val = NULL;
+                               mods->sml_desc = ad_pwdHistory;
+                               mods->sml_nvalues = NULL;
+                               mods->sml_values = ch_calloc( sizeof( struct berval ), 2 );
+                               mods->sml_values[ 1 ].bv_val = NULL;
+                               mods->sml_values[ 1 ].bv_len = 0;
+                               make_pwd_history_value( timebuf, &mods->sml_values[0], pa );
+                               mods->sml_next = NULL;
+                               modtail->sml_next = mods;
+                               modtail = mods;
+                       } else {
+                               Debug( LDAP_DEBUG_TRACE,
+                               "ppolicy_modify: password attr lookup failed\n", 0, 0, 0 );
                        }
-                       mods->sml_next = NULL;
-                       modtail->sml_next = mods;
-                       modtail = mods;
-               }
-               free_pwd_history_list( &tl );
-
-               /*
-                * Now add the existing password into the history list.
-                * This will be executed even if the operation is to delete
-                * the password entirely.
-                *
-                * This isn't in the spec explicitly, but it seems to make
-                * sense that the password history list is the list of all
-                * previous passwords - even if they were deleted. Thus, if
-                * someone tries to add a historical password at some future
-                * point, it will fail.
-                */
-               if ((pa = attr_find( e->e_attrs, pp.ad )) != NULL) {
-                       mods = (Modifications *) ch_malloc( sizeof( Modifications ) );
-                       mods->sml_op = LDAP_MOD_ADD;
-                       mods->sml_type.bv_val = NULL;
-                       mods->sml_desc = ad_pwdHistory;
-                       mods->sml_nvalues = NULL;
-                       mods->sml_values = ch_calloc( sizeof( struct berval ), 2 );
-                       mods->sml_values[ 1 ].bv_val = NULL;
-                       mods->sml_values[ 1 ].bv_len = 0;
-                       make_pwd_history_value( timebuf, &mods->sml_values[0], pa );
-                       mods->sml_next = NULL;
-                       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
                }
 
                /*
@@ -1652,7 +1619,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;
                        
@@ -1708,6 +1675,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;
 }
@@ -1719,6 +1687,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 )
@@ -1769,6 +1751,22 @@ 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 "
+                               "takes no arguments\n", fname, lineno );
+                       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;
 }
@@ -1783,9 +1781,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,
@@ -1807,39 +1804,14 @@ 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 );
+               ppolicy_parseCtrl, &ppolicy_cid );
        if ( code != LDAP_SUCCESS ) {
                fprintf( stderr, "Failed to register control %d\n", code );
                return code;
        }
 
-       /* Find out our offset from GMT since mktime() always assumes
-        * its input is local time
-        */
-       {
-               time_t gmt, lcl, now = slap_get_time();
-               struct tm *tmp;
-
-               tmp = gmtime( &now );
-               gmt = mktime( tmp );
-
-               tmp = localtime( &now );
-               tmp->tm_isdst = 0;
-               lcl = mktime( tmp );
-
-               gmtoff = gmt - lcl;
-       }
-
        ldap_pvt_thread_mutex_init( &chk_syntax_mutex );
 
        ppolicy.on_bi.bi_type = "ppolicy";