]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/overlays/ppolicy.c
Mroe abandon checks for ITS#3671
[openldap] / servers / slapd / overlays / ppolicy.c
index fc441f6d318cf5ef2b88d832ef39aa9bb0371b5f..322b2b1009711ce4a8129938384275b48abffa2f 100644 (file)
@@ -1,7 +1,7 @@
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 2004 The OpenLDAP Foundation.
+ * Copyright 2004-2005 The OpenLDAP Foundation.
  * Portions Copyright 2004 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-07
+ * based on draft behera-ldap-password-policy-08
  */
 
 #ifdef SLAPD_OVER_PPOLICY
@@ -36,6 +36,7 @@
 #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>
@@ -74,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 */
@@ -101,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;
@@ -114,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' ) "
@@ -124,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' "
@@ -145,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' ) "
@@ -174,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;
@@ -189,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),
@@ -323,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) );
 
@@ -346,8 +338,6 @@ ppolicy_get( Operation *op, Entry *e, PassPolicy *pp )
                }
        }
 
-       /* 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;
@@ -373,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 )))
@@ -401,13 +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:
        Debug( LDAP_DEBUG_ANY,
                "ppolicy_get: using default policy\n", 0, 0, 0 );
-       op->o_private = opr;
        return;
 }
 
@@ -851,10 +839,10 @@ 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;
                }
 
                /*
@@ -906,19 +894,8 @@ 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? */
                        
@@ -1059,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;
        }
@@ -1148,7 +1125,7 @@ ppolicy_add(
                        }
                }
                /* If password aging is in effect, set the pwdChangedTime */
-               if (( pp.pwdMaxAge || pp.pwdMinAge ) && !be_isupdate( op )) {
+               if (( pp.pwdMaxAge || pp.pwdMinAge ) && !be_shadow_update( op )) {
                        struct berval timestamp;
                        char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
                        struct tm *ltm;
@@ -1178,7 +1155,6 @@ ppolicy_modify( Operation *op, SlapReply *rs )
        PassPolicy              pp;
        Modifications           *mods = NULL, *modtail, *ml, *delmod, *addmod;
        Attribute               *pa, *ha, *ra, at;
-       int                     repl_user = be_isupdate( op );
        const char              *txt;
        pw_hist                 *tl = NULL, *p;
        int                     zapReset, send_ctrl = 0;
@@ -1264,7 +1240,7 @@ ppolicy_modify( Operation *op, SlapReply *rs )
        if (pwcons[op->o_conn->c_conn_idx].restrict && !mod_pw_only) {
                Debug( LDAP_DEBUG_TRACE,
                        "connection restricted to password changing only\n", 0, 0, 0 );
-               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;
@@ -1344,14 +1320,14 @@ ppolicy_modify( Operation *op, SlapReply *rs )
                Debug( LDAP_DEBUG_TRACE,
                        "change password must use DELETE followed by ADD/REPLACE\n",
                        0, 0, 0 );
-               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;
@@ -1366,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;
@@ -1381,7 +1357,8 @@ 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) {
                        Debug( LDAP_DEBUG_TRACE,
                                "old password check failed: %s\n", txt, 0, 0 );
@@ -1425,7 +1402,8 @@ ppolicy_modify( Operation *op, SlapReply *rs )
                /*
                 * Last check - the password history.
                 */
-               if (slap_passwd_check( op->o_conn, pa, bv, &txt ) == LDAP_SUCCESS) {
+               /* 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.
@@ -1447,7 +1425,8 @@ ppolicy_modify( Operation *op, SlapReply *rs )
                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 );
+                       /* FIXME: no access checking? */
+                       rc = slap_passwd_check( op, NULL, &at, bv, &txt );
                        
                        if (rc != LDAP_SUCCESS) continue;
                        
@@ -1459,7 +1438,7 @@ ppolicy_modify( Operation *op, SlapReply *rs )
        }
 
 do_modify:
-       if ((pwmod) && (!repl_user)) {
+       if ((pwmod) && (!be_shadow_update( op ))) {
                struct berval timestamp;
                char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
                struct tm *ltm;
@@ -1509,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 ) );
@@ -1709,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
@@ -1792,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 );
                }
@@ -1805,7 +1780,7 @@ int ppolicy_init()
        }
 
        code = register_supported_control( LDAP_CONTROL_PASSWORDPOLICYREQUEST,
-               SLAP_CTRL_ADD|SLAP_CTRL_BIND|SLAP_CTRL_MODIFY, extops,
+               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 );
@@ -1816,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;