]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/overlays/ppolicy.c
Plug memleak when query isn't added to cache
[openldap] / servers / slapd / overlays / ppolicy.c
index 03a19697f359940a0a85e4f0547f0e602c31996f..3403e8084743331a9df6019c64150b5b8a5f39ca 100644 (file)
@@ -1,7 +1,7 @@
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 2004-2005 The OpenLDAP Foundation.
+ * Copyright 2004-2006 The OpenLDAP Foundation.
  * Portions Copyright 2004-2005 Howard Chu, Symas Corporation.
  * Portions Copyright 2004 Hewlett-Packard Company.
  * All rights reserved.
@@ -357,30 +357,40 @@ ppolicy_get( Operation *op, Entry *e, PassPolicy *pp )
        pp->ad = slap_schema.si_ad_userPassword;
 #endif
 
-       if ((a = attr_find( pe->e_attrs, ad_pwdMinAge )))
-               pp->pwdMinAge = atoi(a->a_vals[0].bv_val );
-       if ((a = attr_find( pe->e_attrs, ad_pwdMaxAge )))
-               pp->pwdMaxAge = atoi(a->a_vals[0].bv_val );
-       if ((a = attr_find( pe->e_attrs, ad_pwdInHistory )))
-               pp->pwdInHistory = atoi(a->a_vals[0].bv_val );
-       if ((a = attr_find( pe->e_attrs, ad_pwdCheckQuality )))
-               pp->pwdCheckQuality = atoi(a->a_vals[0].bv_val );
-       if ((a = attr_find( pe->e_attrs, ad_pwdMinLength )))
-               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_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 )))
-               pp->pwdFailureCountInterval = atoi(a->a_vals[0].bv_val );
-       if ((a = attr_find( pe->e_attrs, ad_pwdLockoutDuration )))
-               pp->pwdLockoutDuration = atoi(a->a_vals[0].bv_val );
-
-       if ((a = attr_find( pe->e_attrs, ad_pwdCheckModule ))) {
-               strncpy(pp->pwdCheckModule, a->a_vals[0].bv_val,
-                       sizeof(pp->pwdCheckModule));
+       if ( ( a = attr_find( pe->e_attrs, ad_pwdMinAge ) )
+                       && lutil_atoi( &pp->pwdMinAge, a->a_vals[0].bv_val ) != 0 )
+               goto defaultpol;
+       if ( ( a = attr_find( pe->e_attrs, ad_pwdMaxAge ) )
+                       && lutil_atoi( &pp->pwdMaxAge, a->a_vals[0].bv_val ) != 0 )
+               goto defaultpol;
+       if ( ( a = attr_find( pe->e_attrs, ad_pwdInHistory ) )
+                       && lutil_atoi( &pp->pwdInHistory, a->a_vals[0].bv_val ) != 0 )
+               goto defaultpol;
+       if ( ( a = attr_find( pe->e_attrs, ad_pwdCheckQuality ) )
+                       && lutil_atoi( &pp->pwdCheckQuality, a->a_vals[0].bv_val ) != 0 )
+               goto defaultpol;
+       if ( ( a = attr_find( pe->e_attrs, ad_pwdMinLength ) )
+                       && lutil_atoi( &pp->pwdMinLength, a->a_vals[0].bv_val ) != 0 )
+               goto defaultpol;
+       if ( ( a = attr_find( pe->e_attrs, ad_pwdMaxFailure ) )
+                       && lutil_atoi( &pp->pwdMaxFailure, a->a_vals[0].bv_val ) != 0 )
+               goto defaultpol;
+       if ( ( a = attr_find( pe->e_attrs, ad_pwdGraceAuthNLimit ) )
+                       && lutil_atoi( &pp->pwdGraceAuthNLimit, a->a_vals[0].bv_val ) != 0 )
+               goto defaultpol;
+       if ( ( a = attr_find( pe->e_attrs, ad_pwdExpireWarning ) )
+                       && lutil_atoi( &pp->pwdExpireWarning, a->a_vals[0].bv_val ) != 0 )
+               goto defaultpol;
+       if ( ( a = attr_find( pe->e_attrs, ad_pwdFailureCountInterval ) )
+                       && lutil_atoi( &pp->pwdFailureCountInterval, a->a_vals[0].bv_val ) != 0 )
+               goto defaultpol;
+       if ( ( a = attr_find( pe->e_attrs, ad_pwdLockoutDuration ) )
+                       && lutil_atoi( &pp->pwdLockoutDuration, a->a_vals[0].bv_val ) != 0 )
+               goto defaultpol;
+
+       if ( ( a = attr_find( pe->e_attrs, ad_pwdCheckModule ) ) ) {
+               strncpy( pp->pwdCheckModule, a->a_vals[0].bv_val,
+                       sizeof(pp->pwdCheckModule) );
                pp->pwdCheckModule[sizeof(pp->pwdCheckModule)-1] = '\0';
        }
 
@@ -1102,38 +1112,39 @@ 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;
-                               }
+               }
+                       /*
+                        * 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;
 
-                               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;
+                       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 ) {
                        struct berval timestamp;
@@ -1155,10 +1166,11 @@ ppolicy_modify( Operation *op, SlapReply *rs )
 {
        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,
+       int                     i, rc, mod_pw_only, pwmod, pwmop = -1, deladd,
                                hsize = 0;
        PassPolicy              pp;
-       Modifications           *mods = NULL, *modtail, *ml, *delmod, *addmod;
+       Modifications           *mods = NULL, *modtail = NULL,
+                               *ml, *delmod, *addmod;
        Attribute               *pa, *ha, at;
        const char              *txt;
        pw_hist                 *tl = NULL, *p;
@@ -1168,16 +1180,104 @@ ppolicy_modify( Operation *op, SlapReply *rs )
                                *bv, cr[2];
        LDAPPasswordPolicyError pErr = PP_noError;
 
-       /* If this is a replica, assume the master checked everything */
-       if ( be_shadow_update( op ))
-               return SLAP_CB_CONTINUE;
-
        op->o_bd->bd_info = (BackendInfo *)on->on_info;
        rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &e );
        op->o_bd->bd_info = (BackendInfo *)on;
 
        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, got_del_fail = 0;
+               Attribute *a_grace, *a_lock, *a_fail;
+
+               a_grace = attr_find( e->e_attrs, ad_pwdGraceUseTime );
+               a_lock = attr_find( e->e_attrs, ad_pwdAccountLockedTime );
+               a_fail = attr_find( e->e_attrs, ad_pwdFailureTime );
+
+               for( prev = &op->oq_modify.rs_modlist, ml = *prev; ml; 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;
+                               } else
+                               if ( ml->sml_desc == ad_pwdFailureTime ) {
+                                       got_del_fail = 1;
+                                       if ( !a_fail )
+                                               drop = 1;
+                               }
+                               if ( drop ) {
+                                       *prev = ml->sml_next;
+                                       ml->sml_next = NULL;
+                                       slap_mods_free( ml, 1 );
+                                       continue;
+                               }
+                       }
+                       prev = &ml->sml_next;
+               }
+
+               /* If we're resetting the password, make sure grace, accountlock,
+                * and failure 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;
+                       }
+                       if ( a_fail && !got_del_fail ) {
+                               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_pwdFailureTime;
+                               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;
@@ -1202,12 +1302,13 @@ ppolicy_modify( Operation *op, SlapReply *rs )
 
        ppolicy_get( op, e, &pp );
 
-       for(ml = op->oq_modify.rs_modlist,
+       for ( ml = op->oq_modify.rs_modlist,
                        pwmod = 0, mod_pw_only = 1,
                        deladd = 0, delmod = NULL,
                        addmod = NULL,
                        zapReset = 1;
-               ml != NULL; modtail = ml, ml = ml->sml_next ) {
+               ml != NULL; modtail = ml, ml = ml->sml_next )
+       {
                if ( ml->sml_desc == pp.ad ) {
                        pwmod = 1;
                        pwmop = ml->sml_op;
@@ -1507,6 +1608,19 @@ do_modify:
                        modtail = mods;
                }
 
+               if (attr_find(e->e_attrs, ad_pwdFailureTime )) {
+                       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_pwdFailureTime;
+                       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 ) );
@@ -1669,6 +1783,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
@@ -1688,6 +1840,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 );
@@ -1764,6 +1930,7 @@ ppolicy_config(
                        return ( 1 );
                }
                pi->hash_passwords = 1;
+               return 0;
        }
        return SLAP_CONF_UNKNOWN;
 }
@@ -1775,7 +1942,7 @@ static char *extops[] = {
 
 static slap_overinst ppolicy;
 
-int ppolicy_init()
+int ppolicy_initialize()
 {
        LDAPAttributeType *at;
        const char *err;
@@ -1835,7 +2002,7 @@ int ppolicy_init()
 
 #if SLAPD_OVER_PPOLICY == SLAPD_MOD_DYNAMIC
 int init_module(int argc, char *argv[]) {
-       return ppolicy_init();
+       return ppolicy_initialize();
 }
 #endif