]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/overlays/ppolicy.c
Fix cancel cleanup
[openldap] / servers / slapd / overlays / ppolicy.c
index e46cd1dcade8d7b5fe8204fb79704b27cb171f31..f572875fef781615c35a269607984e3babb6149f 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.
@@ -35,7 +35,9 @@
 #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>
@@ -49,6 +51,7 @@
 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 
@@ -59,6 +62,7 @@ typedef struct pw_conn {
 } pw_conn;
 
 static pw_conn *pwcons;
+static int ppolicy_cid;
 
 typedef struct pass_policy {
        AttributeDescription *ad; /* attribute to which the policy applies */
@@ -199,120 +203,16 @@ static struct schema_info pwd_UsSchema[] = {
 
 static ldap_pvt_thread_mutex_t chk_syntax_mutex;
 
-static time_t
-ppolicy_timegm( struct tm *t )
-{
-       static int moffset[12] = {
-               0, 31, 59, 90, 120,
-               151, 181, 212, 243,
-               273, 304, 334 }; 
-       time_t ret; 
-
-       /* t->tm_year is years since 1900 */
-       /* calculate days from years since 1970 (epoch) */ 
-       ret = t->tm_year - 70; 
-       ret *= 365L; 
-
-       /* count leap days in preceding years */ 
-       ret += ((t->tm_year -69) >> 2); 
-
-       /* calculate days from months */ 
-       ret += moffset[t->tm_mon]; 
-
-       /* add in this year's leap day, if any */ 
-       if (((t->tm_year & 3) == 0) && (t->tm_mon > 1)) { 
-               ret ++; 
-       } 
-
-       /* add in days in this month */ 
-       ret += (t->tm_mday - 1); 
-
-       /* convert to hours */ 
-       ret *= 24L; 
-       ret += t->tm_hour; 
-
-       /* convert to minutes */ 
-       ret *= 60L; 
-       ret += t->tm_min; 
-
-       /* convert to seconds */ 
-       ret *= 60L; 
-       ret += t->tm_sec; 
-
-       /* return the result */ 
-       return ret; 
-}
-
 static time_t
 parse_time( char *atm )
 {
-       struct tm tm;
-       char *ptr = atm;
+       struct lutil_tm tm;
+       struct lutil_timet tt;
        time_t ret = (time_t)-1;
-       int i = 0;
-
-       while (atm) {
-
-               /* Is the stamp reasonbly long? */
-               for (i=0; isdigit(atm[i]); i++);
-               if (i < sizeof("00000101000000")-1)
-                       break;
-
-               /*
-                * parse the time and return it's time_t value.
-                */
-               /* 4 digit year to year - 1900 */
-               tm.tm_year = *ptr++ - '0';
-               tm.tm_year *= 10; tm.tm_year += *ptr++ - '0';
-               tm.tm_year *= 10; tm.tm_year += *ptr++ - '0';
-               tm.tm_year *= 10; tm.tm_year += *ptr++ - '0';
-               tm.tm_year -= 1900;
-               /* month 01-12 to 0-11 */
-               tm.tm_mon = *ptr++ - '0';
-               tm.tm_mon *=10; tm.tm_mon += *ptr++ - '0';
-               if (tm.tm_mon < 1 || tm.tm_mon > 12) break;
-               tm.tm_mon--;
-
-               /* day of month 01-31 */
-               tm.tm_mday = *ptr++ - '0';
-               tm.tm_mday *=10; tm.tm_mday += *ptr++ - '0';
-               if (tm.tm_mday < 1 || tm.tm_mday > 31) break;
-
-               /* Hour 00-23 */
-               tm.tm_hour = *ptr++ - '0';
-               tm.tm_hour *=10; tm.tm_hour += *ptr++ - '0';
-               if (tm.tm_hour < 0 || tm.tm_hour > 23) break;
-
-               /* Minute 00-59 */
-               tm.tm_min = *ptr++ - '0';
-               tm.tm_min *=10; tm.tm_min += *ptr++ - '0';
-               if (tm.tm_min < 0 || tm.tm_min > 59) break;
-
-               /* Second 00-61 */
-               tm.tm_sec = *ptr++ - '0';
-               tm.tm_sec *=10; tm.tm_sec += *ptr++ - '0';
-               if (tm.tm_sec < 0 || tm.tm_sec > 61) break;
-
-               /* Fractions of seconds */
-               for (i = 0;isdigit(*ptr);) {
-                       i*=10; i+= *ptr++ - '0';
-               }
-
-               /*
-                * special case - if the lowest allowable GeneralizedTime is here, return
-                * this 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 (strncmp(atm, "00000101000000", sizeof("00000101000000")-1) == 0 &&
-                       i == 0 ) break;
-               
-               /* Must be UTC */
-               if (*ptr != 'Z') break;
-
-               /* FIXME: we don't check precision smaller than seconds. */
 
-               ret = ppolicy_timegm( &tm );
-               break;
+       if ( lutil_parsetime( atm, &tm ) == 0) {
+               lutil_tm2time( &tm, &tt );
+               ret = tt.tt_sec;
        }
        return ret;
 }
@@ -423,7 +323,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) );
 
@@ -446,8 +345,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;
@@ -501,13 +398,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;
 }
 
@@ -543,7 +438,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;
@@ -589,8 +484,9 @@ 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;
                
@@ -602,7 +498,7 @@ check_password_quality( struct berval *cred, PassPolicy *pp, LDAPPasswordPolicyE
                                pp->pwdCheckModule, err, 0 );
                        ok = LDAP_OTHER; /* internal error */
                } else {
-                       int (*prog)( char *passwd, char **text, void *arg );
+                       int (*prog)( char *passwd, char **text, Entry *ent );
 
                        if ((prog = lt_dlsym( mod, "check_password" )) == NULL) {
                                err = lt_dlerror();
@@ -615,7 +511,7 @@ check_password_quality( struct berval *cred, PassPolicy *pp, LDAPPasswordPolicyE
                                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 );
                                ldap_pvt_thread_mutex_unlock( &chk_syntax_mutex );
                                if (txt) {
                                        Debug(LDAP_DEBUG_ANY,
@@ -628,6 +524,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 */
        }
                
                    
@@ -1096,13 +996,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;
@@ -1143,12 +1038,8 @@ 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 ) {
@@ -1177,9 +1068,11 @@ 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;
@@ -1193,21 +1086,17 @@ ppolicy_add(
                 * 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 );
+               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 ) {
@@ -1221,9 +1110,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 ) && !be_isupdate( op )) {
+               if (( pp.pwdMaxAge || pp.pwdMinAge ) && !be_shadow_update( op )) {
                        struct berval timestamp;
                        char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
                        struct tm *ltm;
@@ -1247,12 +1167,12 @@ static int
 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,
                                hsize = 0;
        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;
@@ -1268,12 +1188,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
@@ -1459,7 +1375,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 );
@@ -1491,7 +1408,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";
@@ -1503,7 +1420,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.
@@ -1525,7 +1443,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;
                        
@@ -1537,7 +1456,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;
@@ -1697,7 +1616,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;
                        
@@ -1753,6 +1672,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;
 }
@@ -1786,6 +1706,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
@@ -1837,6 +1765,13 @@ ppolicy_config(
                }
                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;
 }
@@ -1875,8 +1810,8 @@ int ppolicy_init()
        }
 
        code = register_supported_control( LDAP_CONTROL_PASSWORDPOLICYREQUEST,
-               SLAP_CTRL_ADD|SLAP_CTRL_BIND|SLAP_CTRL_MODIFY, extops,
-               ppolicy_parseCtrl );
+               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 );
                return code;
@@ -1886,6 +1821,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;