]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/overlays/ppolicy.c
pass ConfigReply into overlay_config() to allow progating error messages
[openldap] / servers / slapd / overlays / ppolicy.c
index 6b7668bf569116fde296617df8875671b6276379..6f96c02426106e5781df2d39288ea67b4b5c1ef2 100644 (file)
@@ -1,7 +1,7 @@
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 2004-2007 The OpenLDAP Foundation.
+ * Copyright 2004-2008 The OpenLDAP Foundation.
  * Portions Copyright 2004-2005 Howard Chu, Symas Corporation.
  * Portions Copyright 2004 Hewlett-Packard Company.
  * All rights reserved.
@@ -1550,35 +1550,35 @@ ppolicy_modify( Operation *op, SlapReply *rs )
                                delmod = ml;
                        }
 
-                       if ((deladd == 1) && ((ml->sml_op == LDAP_MOD_ADD) ||
-                                 (ml->sml_op == LDAP_MOD_REPLACE)))
-                       {
-                               deladd = 2;
-                       }
-
                        if ((ml->sml_op == LDAP_MOD_ADD) ||
                                (ml->sml_op == LDAP_MOD_REPLACE))
                        {
-                               /* FIXME: there's no easy way to ensure
-                                * that add does not cause multiple
-                                * userPassword values; one way (that 
-                                * would be consistent with the single
-                                * password constraint) would be to turn
-                                * add into replace); another would be
-                                * to disallow add.
-                                *
-                                * Let's check at least that a single value
-                                * is being added
-                                */
-                               assert( ml->sml_values != NULL );
-                               assert( !BER_BVISNULL( &ml->sml_values[ 0 ] ) );
-                               if ( !BER_BVISNULL( addmod || &ml->sml_values[ 1 ] ) ) {
-                                       rs->sr_err = LDAP_CONSTRAINT_VIOLATION; 
-                                       rs->sr_text = "Password policy only allows one password value";
-                                       goto return_results;
+                               if ( ml->sml_values && !BER_BVISNULL( &ml->sml_values[0] )) {
+                                       if ( deladd == 1 )
+                                               deladd = 2;
+
+                                       /* FIXME: there's no easy way to ensure
+                                        * that add does not cause multiple
+                                        * userPassword values; one way (that 
+                                        * would be consistent with the single
+                                        * password constraint) would be to turn
+                                        * add into replace); another would be
+                                        * to disallow add.
+                                        *
+                                        * Let's check at least that a single value
+                                        * is being added
+                                        */
+                                       if ( addmod || !BER_BVISNULL( &ml->sml_values[ 1 ] ) ) {
+                                               rs->sr_err = LDAP_CONSTRAINT_VIOLATION; 
+                                               rs->sr_text = "Password policy only allows one password value";
+                                               goto return_results;
+                                       }
+
+                                       addmod = ml;
+                               } else {
+                                       /* replace can have no values, add cannot */
+                                       assert( ml->sml_op == LDAP_MOD_REPLACE );
                                }
-
-                               addmod = ml;
                        }
 
                } else if ( !is_at_operational( ml->sml_desc->ad_type ) ) {
@@ -1622,30 +1622,18 @@ ppolicy_modify( Operation *op, SlapReply *rs )
         * if we have a "safe password modify policy", then we need to check if we're doing
         * a delete (with the old password), followed by an add (with the new password).
         *
-        * If we don't have this, then we fail with an error. We also skip all the checks if
+        * If we got just a delete with nothing else, just let it go. We also skip all the checks if
         * the root user is bound. Root can do anything, including avoid the policies.
         */
 
        if (!pwmod) goto do_modify;
 
-       /*
-        * Did we get a valid add mod?
-        */
-
-       if (!addmod) {
-               rs->sr_err = LDAP_OTHER;
-               rs->sr_text = "Internal Error";
-               Debug( LDAP_DEBUG_TRACE,
-                       "cannot locate modification supplying new password\n", 0, 0, 0 );
-               goto return_results;
-       }
-
        /*
         * Build the password history list in ascending time order
         * We need this, even if the user is root, in order to maintain
         * the pwdHistory operational attributes properly.
         */
-       if (pp.pwdInHistory > 0 && (ha = attr_find( e->e_attrs, ad_pwdHistory ))) {
+       if (addmod && pp.pwdInHistory > 0 && (ha = attr_find( e->e_attrs, ad_pwdHistory ))) {
                struct berval oldpw;
                time_t oldtime;
 
@@ -1667,6 +1655,20 @@ ppolicy_modify( Operation *op, SlapReply *rs )
 
        if (be_isroot( op )) goto do_modify;
 
+       if (!pp.pwdAllowUserChange) {
+               rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
+               rs->sr_text = "User alteration of password is not allowed";
+               pErr = PP_passwordModNotAllowed;
+               goto return_results;
+       }
+
+       /* Just deleting? */
+       if (!addmod) {
+               /* skip everything else */
+               pwmod = 0;
+               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 
         * let the matching value get found later
@@ -1697,13 +1699,6 @@ ppolicy_modify( Operation *op, SlapReply *rs )
                goto return_results;
        }
 
-       if (!pp.pwdAllowUserChange) {
-               rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
-               rs->sr_text = "User alteration of password is not allowed";
-               pErr = PP_passwordModNotAllowed;
-               goto return_results;
-       }
-
        /* Check age, but only if pwdReset is not TRUE */
        pa = attr_find( e->e_attrs, ad_pwdReset );
        if ((!pa || !bvmatch( &pa->a_nvals[0], &slap_true_bv )) &&
@@ -1771,7 +1766,8 @@ ppolicy_modify( Operation *op, SlapReply *rs )
                }
        }
 
-       if (pa) {
+       /* If pwdInHistory is zero, passwords may be reused */
+       if (pa && pp.pwdInHistory > 0) {
                /*
                 * Last check - the password history.
                 */
@@ -1787,8 +1783,6 @@ ppolicy_modify( Operation *op, SlapReply *rs )
                        goto return_results;
                }
        
-               if (pp.pwdInHistory < 1) goto do_modify;
-       
                /*
                 * Iterate through the password history, and fail on any
                 * password matches.
@@ -2099,7 +2093,12 @@ ppolicy_db_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 );
+                               if ( cr ){
+                                       snprintf( cr->msg, sizeof(cr->msg), 
+                                               "User Schema load failed for attribute \"%s\". Error code %d: %s",
+                                               pwd_UsSchema[i].def, code, err );
+                                       fprintf( stderr, "%s\n", cr->msg );
+                               }
                                return code;
                        }
                }
@@ -2121,8 +2120,11 @@ ppolicy_db_init(
 
        on->on_bi.bi_private = ch_calloc( sizeof(pp_info), 1 );
 
-       if ( dtblsize && !pwcons )
-               pwcons = ch_calloc(sizeof(pw_conn), dtblsize );
+       if ( dtblsize && !pwcons ) {
+               /* accommodate for c_conn_idx == -1 */
+               pwcons = ch_calloc( sizeof(pw_conn), dtblsize + 1 );
+               pwcons++;
+       }
 
        return 0;
 }
@@ -2149,6 +2151,7 @@ ppolicy_close(
        /* Perhaps backover should provide bi_destroy hooks... */
        ov_count--;
        if ( ov_count <=0 && pwcons ) {
+               pwcons--;
                free( pwcons );
                pwcons = NULL;
        }