]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/overlays/ppolicy.c
More schema tweaks
[openldap] / servers / slapd / overlays / ppolicy.c
index 4b29520517dfc4e7b08e6e7d73abfa5102bbc7fb..0d87840a166290f1b081f1b0f791b3d658088c9b 100644 (file)
@@ -2,7 +2,7 @@
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
  * Copyright 2004-2005 The OpenLDAP Foundation.
- * Portions Copyright 2004 Howard Chu, Symas Corporation.
+ * Portions Copyright 2004-2005 Howard Chu, Symas Corporation.
  * Portions Copyright 2004 Hewlett-Packard Company.
  * All rights reserved.
  *
@@ -23,7 +23,7 @@
 #include "portable.h"
 
 /* This file implements "Password Policy for LDAP Directories",
- * based on draft behera-ldap-password-policy-08
+ * based on draft behera-ldap-password-policy-09
  */
 
 #ifdef SLAPD_OVER_PPOLICY
@@ -111,7 +111,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 NO-USER-MODIFICATION )",
+               "SINGLE-VALUE NO-USER-MODIFICATION USAGE directoryOperation )",
                &ad_pwdChangedTime },
        {       "( 1.3.6.1.4.1.42.2.27.8.1.17 "
                "NAME ( 'pwdAccountLockedTime' ) "
@@ -119,7 +119,12 @@ 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 "
+#if 0
+               /* Not until MANAGEDIT control is released */
+               "NO-USER-MODIFICATION "
+#endif
+               "USAGE directoryOperation )",
                &ad_pwdAccountLockedTime },
        {       "( 1.3.6.1.4.1.42.2.27.8.1.19 "
                "NAME ( 'pwdFailureTime' ) "
@@ -127,21 +132,21 @@ static struct schema_info {
                "EQUALITY generalizedTimeMatch "
                "ORDERING generalizedTimeOrderingMatch "
                "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
-               "USAGE directoryOperation )",
+               "NO-USER-MODIFICATION USAGE directoryOperation )",
                &ad_pwdFailureTime },
        {       "( 1.3.6.1.4.1.42.2.27.8.1.20 "
                "NAME ( 'pwdHistory' ) "
                "DESC 'The history of users passwords' "
                "EQUALITY octetStringMatch "
                "SYNTAX 1.3.6.1.4.1.1466.115.121.1.40 "
-               "USAGE directoryOperation NO-USER-MODIFICATION )",
+               "NO-USER-MODIFICATION USAGE directoryOperation )",
                &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 NO-USER-MODIFICATION )",
+               "NO-USER-MODIFICATION USAGE directoryOperation )",
                &ad_pwdGraceUseTime }, 
        {       "( 1.3.6.1.4.1.42.2.27.8.1.22 "
                "NAME ( 'pwdReset' ) "
@@ -155,7 +160,12 @@ static struct schema_info {
                "DESC 'The pwdPolicy subentry in effect for this object' "
                "EQUALITY distinguishedNameMatch "
                "SYNTAX 1.3.6.1.4.1.1466.115.121.1.12 "
-               "SINGLE-VALUE USAGE directoryOperation )",
+               "SINGLE-VALUE "
+#if 0
+               /* Not until MANAGEDIT control is released */
+               "NO-USER-MODIFICATION "
+#endif
+               "USAGE directoryOperation )",
                &ad_pwdPolicySubentry },
        { NULL, NULL }
 };
@@ -211,10 +221,8 @@ account_locked( Operation *op, Entry *e,
                PassPolicy *pp, Modifications **mod ) 
 {
        Attribute       *la;
-       int rc;
-       Entry *de;
 
-       assert(mod);
+       assert(mod != NULL);
 
        if ( (la = attr_find( e->e_attrs, ad_pwdAccountLockedTime )) != NULL ) {
                BerVarray vals = la->a_nvals;
@@ -225,7 +233,6 @@ account_locked( Operation *op, Entry *e,
                 */
                if (vals[0].bv_val != NULL) {
                        time_t then, now;
-                       struct berval bv;
                        Modifications *m;
 
                        if (!pp->pwdLockoutDuration)
@@ -310,12 +317,11 @@ ppolicy_get( Operation *op, Entry *e, PassPolicy *pp )
        pp_info *pi = on->on_bi.bi_private;
        Attribute *a;
        BerVarray vals;
-       int i, rc, nent;
+       int rc;
        Entry *pe = NULL;
-       AttributeDescription *oca = slap_schema.si_ad_objectClass;
+#if 0
        const char *text;
-       AttributeDescription *ad;
-       struct berval bv;
+#endif
 
        memset( pp, 0, sizeof(PassPolicy) );
 
@@ -432,7 +438,6 @@ check_password_quality( struct berval *cred, PassPolicy *pp, LDAPPasswordPolicyE
 {
        int rc = LDAP_SUCCESS, ok = LDAP_SUCCESS;
        char *ptr = cred->bv_val;
-       char *modpath;
        struct berval sch;
 
        assert( cred != NULL );
@@ -681,10 +686,9 @@ ppolicy_bind_resp( Operation *op, SlapReply *rs )
        slap_overinst *on = ppb->on;
        Modifications *mod = ppb->mod, *m;
        int pwExpired = 0;
-       int ngut = -1, warn = -1, age, rc, i;
+       int ngut = -1, warn = -1, age, rc;
        Attribute *a;
-       time_t now, then, pwtime = (time_t)-1;
-       const char *txt;
+       time_t now, pwtime = (time_t)-1;
        char nowstr[ LDAP_LUTIL_GENTIME_BUFSIZE ];
        struct berval timestamp;
        BackendInfo *bi = op->o_bd->bd_info;
@@ -925,7 +929,7 @@ locked:
                op2.o_ndn = op->o_bd->be_rootndn;
                op2.o_bd->bd_info = (BackendInfo *)on->on_info;
                rc = op->o_bd->be_modify( &op2, &r2 );
-               slap_mods_free( mod );
+               slap_mods_free( mod, 1 );
        }
 
        if ( ppb->send_ctrl ) {
@@ -950,10 +954,13 @@ ppolicy_bind( Operation *op, SlapReply *rs )
 {
        slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
 
+       /* Reset lockout status on all Bind requests */
+       pwcons[op->o_conn->c_conn_idx].restricted = 0;
+
        /* Root bypasses policy */
        if ( !be_isroot_dn( op->o_bd, &op->o_req_ndn )) {
                Entry *e;
-               int i, rc;
+               int rc;
                ppbind *ppb;
                slap_callback *cb;
 
@@ -1004,9 +1011,9 @@ ppolicy_bind( Operation *op, SlapReply *rs )
 
 /* Reset the restricted flag for the next session on this connection */
 static int
-ppolicy_unbind( Operation *op, SlapReply *rs )
+ppolicy_connection_destroy( BackendDB *bd, Connection *conn )
 {
-       pwcons[op->o_conn->c_conn_idx].restricted = 0;
+       pwcons[conn->c_conn_idx].restricted = 0;
        return SLAP_CB_CONTINUE;
 }
 
@@ -1017,7 +1024,7 @@ ppolicy_restrict(
        SlapReply *rs )
 {
        slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
-       int i, send_ctrl = 0;
+       int send_ctrl = 0;
 
        /* Did we receive a password policy request control? */
        if ( op->o_ctrlflag[ppolicy_cid] ) {
@@ -1052,13 +1059,16 @@ ppolicy_add(
        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;
 
+       /* If this is a replica, assume the master checked everything */
+       if ( be_shadow_update( op ))
+               return SLAP_CB_CONTINUE;
+
        /* Check for password in entry */
        if ((pa = attr_find( op->oq_add.rs_e->e_attrs,
                slap_schema.si_ad_userPassword )))
@@ -1071,7 +1081,7 @@ ppolicy_add(
                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; 
+                       int rc, send_ctrl = 0;
                        LDAPPasswordPolicyError pErr = PP_noError;
 
                        /* Did we receive a password policy request control? */
@@ -1125,7 +1135,7 @@ ppolicy_add(
                        }
                }
                /* If password aging is in effect, set the pwdChangedTime */
-               if (( pp.pwdMaxAge || pp.pwdMinAge ) && !be_shadow_update( op )) {
+               if ( pp.pwdMaxAge || pp.pwdMinAge ) {
                        struct berval timestamp;
                        char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
                        time_t now = slap_get_time();
@@ -1149,7 +1159,7 @@ ppolicy_modify( Operation *op, SlapReply *rs )
                                hsize = 0;
        PassPolicy              pp;
        Modifications           *mods = NULL, *modtail, *ml, *delmod, *addmod;
-       Attribute               *pa, *ha, *ra, at;
+       Attribute               *pa, *ha, at;
        const char              *txt;
        pw_hist                 *tl = NULL, *p;
        int                     zapReset, send_ctrl = 0;
@@ -1164,6 +1174,80 @@ ppolicy_modify( Operation *op, SlapReply *rs )
 
        if ( rc != LDAP_SUCCESS ) return SLAP_CB_CONTINUE;
 
+       /* If this is a replica, we may need to tweak some of the
+        * master's modifications. Otherwise, just pass it through.
+        */
+       if ( be_shadow_update( op )) {
+               Modifications **prev;
+               int got_del_grace = 0, got_del_lock = 0, got_pw = 0;
+               Attribute *a_grace, *a_lock;
+
+               a_grace = attr_find( e->e_attrs, ad_pwdGraceUseTime );
+               a_lock = attr_find( e->e_attrs, ad_pwdAccountLockedTime );
+
+               for( prev = &op->oq_modify.rs_modlist, ml = *prev; ml;
+                       prev = &ml->sml_next, ml = *prev ) {
+
+                       if ( ml->sml_desc == slap_schema.si_ad_userPassword )
+                               got_pw = 1;
+
+                       /* If we're deleting an attr that didn't exist,
+                        * drop this delete op
+                        */
+                       if ( ml->sml_op == LDAP_MOD_DELETE ) {
+                               int drop = 0;
+
+                               if ( ml->sml_desc == ad_pwdGraceUseTime ) {
+                                       got_del_grace = 1;
+                                       if ( !a_grace )
+                                               drop = 1;
+                               } else
+                               if ( ml->sml_desc == ad_pwdAccountLockedTime ) {
+                                       got_del_lock = 1;
+                                       if ( !a_lock )
+                                               drop = 1;
+                               }
+                               if ( drop ) {
+                                       *prev = ml->sml_next;
+                                       ml->sml_next = NULL;
+                                       slap_mods_free( ml, 1 );
+                               }
+                       }
+               }
+
+               /* If we're resetting the password, make sure grace and accountlock
+                * also get removed.
+                */
+               if ( got_pw ) {
+                       if ( a_grace && !got_del_grace ) {
+                               ml = (Modifications *) ch_malloc( sizeof( Modifications ) );
+                               ml->sml_op = LDAP_MOD_DELETE;
+                               ml->sml_flags = SLAP_MOD_INTERNAL;
+                               ml->sml_type.bv_val = NULL;
+                               ml->sml_desc = ad_pwdGraceUseTime;
+                               ml->sml_values = NULL;
+                               ml->sml_nvalues = NULL;
+                               ml->sml_next = NULL;
+                               *prev = ml;
+                               prev = &ml->sml_next;
+                       }
+                       if ( a_lock && !got_del_lock ) {
+                               ml = (Modifications *) ch_malloc( sizeof( Modifications ) );
+                               ml->sml_op = LDAP_MOD_DELETE;
+                               ml->sml_flags = SLAP_MOD_INTERNAL;
+                               ml->sml_type.bv_val = NULL;
+                               ml->sml_desc = ad_pwdAccountLockedTime;
+                               ml->sml_values = NULL;
+                               ml->sml_nvalues = NULL;
+                               ml->sml_next = NULL;
+                               *prev = ml;
+                       }
+               }
+               op->o_bd->bd_info = (BackendInfo *)on->on_info;
+               be_entry_release_r( op, e );
+               return SLAP_CB_CONTINUE;
+       }
+
        /* Did we receive a password policy request control? */
        if ( op->o_ctrlflag[ppolicy_cid] ) {
                send_ctrl = 1;
@@ -1271,7 +1355,6 @@ ppolicy_modify( Operation *op, SlapReply *rs )
        if (pp.pwdInHistory > 0 && (ha = attr_find( e->e_attrs, ad_pwdHistory ))) {
                struct berval oldpw;
                time_t oldtime;
-               char *oid;
 
                for(i=0; ha->a_nvals[i].bv_val; i++) {
                        rc = parse_pwdhistory( &(ha->a_nvals[i]), NULL,
@@ -1434,11 +1517,10 @@ ppolicy_modify( Operation *op, SlapReply *rs )
        }
 
 do_modify:
-       if ((pwmod) && (!be_shadow_update( op ))) {
+       if (pwmod) {
                struct berval timestamp;
                char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
                time_t now = slap_get_time();
-               Attribute *ga;
                
                /*
                 * keep the necessary pwd.. operational attributes
@@ -1458,7 +1540,7 @@ do_modify:
                        ber_dupbv( &mods->sml_values[0], &timestamp );
                        mods->sml_values[1].bv_len = 0;
                        mods->sml_values[1].bv_val = NULL;
-                       assert( mods->sml_values[0].bv_val );
+                       assert( mods->sml_values[0].bv_val != NULL );
                } else {
                        mods->sml_op = LDAP_MOD_DELETE;
                        mods->sml_values = NULL;
@@ -1482,6 +1564,19 @@ do_modify:
                        modtail = mods;
                }
 
+               if (attr_find(e->e_attrs, ad_pwdAccountLockedTime )) {
+                       mods = (Modifications *) ch_malloc( sizeof( Modifications ) );
+                       mods->sml_op = LDAP_MOD_DELETE;
+                       mods->sml_flags = SLAP_MOD_INTERNAL;
+                       mods->sml_type.bv_val = NULL;
+                       mods->sml_desc = ad_pwdAccountLockedTime;
+                       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 ) );
@@ -1644,6 +1739,44 @@ ppolicy_parseCtrl(
        return LDAP_SUCCESS;
 }
 
+static int
+attrPretty(
+       Syntax *syntax,
+       struct berval *val,
+       struct berval *out,
+       void *ctx )
+{
+       AttributeDescription *ad = NULL;
+       const char *err;
+       int code;
+
+       code = slap_bv2ad( val, &ad, &err );
+       if ( !code ) {
+               ber_dupbv_x( out, &ad->ad_type->sat_cname, ctx );
+       }
+       return code;
+}
+
+static int
+attrNormalize(
+       slap_mask_t use,
+       Syntax *syntax,
+       MatchingRule *mr,
+       struct berval *val,
+       struct berval *out,
+       void *ctx )
+{
+       AttributeDescription *ad = NULL;
+       const char *err;
+       int code;
+
+       code = slap_bv2ad( val, &ad, &err );
+       if ( !code ) {
+               ber_str2bv_x( ad->ad_type->sat_oid, 0, 1, out, ctx );
+       }
+       return code;
+}
+
 static int
 ppolicy_db_init(
        BackendDB *be
@@ -1663,6 +1796,20 @@ ppolicy_db_init(
                                return code;
                        }
                }
+               {
+                       Syntax *syn;
+                       MatchingRule *mr;
+
+                       syn = ch_malloc( sizeof( Syntax ));
+                       *syn = *ad_pwdAttribute->ad_type->sat_syntax;
+                       syn->ssyn_pretty = attrPretty;
+                       ad_pwdAttribute->ad_type->sat_syntax = syn;
+
+                       mr = ch_malloc( sizeof( MatchingRule ));
+                       *mr = *ad_pwdAttribute->ad_type->sat_equality;
+                       mr->smr_normalize = attrNormalize;
+                       ad_pwdAttribute->ad_type->sat_equality = mr;
+               }
        }
 
        on->on_bi.bi_private = ch_calloc( sizeof(pp_info), 1 );
@@ -1739,6 +1886,7 @@ ppolicy_config(
                        return ( 1 );
                }
                pi->hash_passwords = 1;
+               return 0;
        }
        return SLAP_CONF_UNKNOWN;
 }
@@ -1774,6 +1922,11 @@ int ppolicy_init()
                                scherr2str(code), err );
                        return code;
                }
+               /* Allow Manager to set these as needed */
+               if ( is_at_no_user_mod( (*pwd_OpSchema[i].ad)->ad_type )) {
+                       (*pwd_OpSchema[i].ad)->ad_type->sat_flags |=
+                               SLAP_AT_MANAGEABLE;
+               }
        }
 
        code = register_supported_control( LDAP_CONTROL_PASSWORDPOLICYREQUEST,
@@ -1794,11 +1947,11 @@ int ppolicy_init()
 
        ppolicy.on_bi.bi_op_add = ppolicy_add;
        ppolicy.on_bi.bi_op_bind = ppolicy_bind;
-       ppolicy.on_bi.bi_op_unbind = ppolicy_unbind;
        ppolicy.on_bi.bi_op_compare = ppolicy_restrict;
        ppolicy.on_bi.bi_op_delete = ppolicy_restrict;
        ppolicy.on_bi.bi_op_modify = ppolicy_modify;
        ppolicy.on_bi.bi_op_search = ppolicy_restrict;
+       ppolicy.on_bi.bi_connection_destroy = ppolicy_connection_destroy;
 
        return overlay_register( &ppolicy );
 }