X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=servers%2Fslapd%2Foverlays%2Fppolicy.c;h=e5b0045fcefc901d480b3c21054563596366c1fb;hb=a255e44bf0fcb703a3a5ac58117fd54a00727fd1;hp=48ecf6821905b65532addd5839c03fb4f7219008;hpb=3c5068bc1fa84fc5daf1e50d4f1a929cec91b7e9;p=openldap diff --git a/servers/slapd/overlays/ppolicy.c b/servers/slapd/overlays/ppolicy.c index 48ecf68219..e5b0045fce 100644 --- a/servers/slapd/overlays/ppolicy.c +++ b/servers/slapd/overlays/ppolicy.c @@ -1,7 +1,7 @@ /* $OpenLDAP$ */ /* This work is part of OpenLDAP Software . * - * Copyright 2004-2007 The OpenLDAP Foundation. + * Copyright 2004-2012 The OpenLDAP Foundation. * Portions Copyright 2004-2005 Howard Chu, Symas Corporation. * Portions Copyright 2004 Hewlett-Packard Company. * All rights reserved. @@ -31,7 +31,7 @@ #include #include "lutil.h" #include "slap.h" -#if SLAPD_MODULES +#ifdef SLAPD_MODULES #define LIBLTDL_DLL_IMPORT /* Win32: don't re-export libltdl's symbols */ #include #endif @@ -50,6 +50,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 */ + int forward_updates; /* use frontend for policy state updates */ } pp_info; /* Our per-connection info - note, it is not per-instance, it is @@ -61,6 +62,7 @@ typedef struct pw_conn { static pw_conn *pwcons; static int ppolicy_cid; +static int ov_count; typedef struct pass_policy { AttributeDescription *ad; /* attribute to which the policy applies */ @@ -213,7 +215,7 @@ static ConfigDriver ppolicy_cf_default; static ConfigTable ppolicycfg[] = { { "ppolicy_default", "policyDN", 2, 2, 0, - ARG_DN|ARG_MAGIC|PPOLICY_DEFAULT, ppolicy_cf_default, + ARG_DN|ARG_QUOTE|ARG_MAGIC|PPOLICY_DEFAULT, ppolicy_cf_default, "( OLcfgOvAt:12.1 NAME 'olcPPolicyDefault' " "DESC 'DN of a pwdPolicy object for uncustomized objects' " "SYNTAX OMsDN SINGLE-VALUE )", NULL, NULL }, @@ -223,6 +225,12 @@ static ConfigTable ppolicycfg[] = { "( OLcfgOvAt:12.2 NAME 'olcPPolicyHashCleartext' " "DESC 'Hash passwords on add or modify' " "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL }, + { "ppolicy_forward_updates", "on|off", 1, 2, 0, + ARG_ON_OFF|ARG_OFFSET, + (void *)offsetof(pp_info,forward_updates), + "( OLcfgOvAt:12.4 NAME 'olcPPolicyForwardUpdates' " + "DESC 'Allow policy state updates to be forwarded via updateref' " + "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL }, { "ppolicy_use_lockout", "on|off", 1, 2, 0, ARG_ON_OFF|ARG_OFFSET|PPOLICY_USE_LOCKOUT, (void *)offsetof(pp_info,use_lockout), @@ -238,7 +246,7 @@ static ConfigOCs ppolicyocs[] = { "DESC 'Password Policy configuration' " "SUP olcOverlayConfig " "MAY ( olcPPolicyDefault $ olcPPolicyHashCleartext $ " - "olcPPolicyUseLockout ) )", + "olcPPolicyUseLockout $ olcPPolicyForwardUpdates ) )", Cft_Overlay, ppolicycfg }, { NULL, 0, NULL } }; @@ -316,6 +324,9 @@ account_locked( Operation *op, Entry *e, assert(mod != NULL); + if ( !pp->pwdLockout ) + return 0; + if ( (la = attr_find( e->e_attrs, ad_pwdAccountLockedTime )) != NULL ) { BerVarray vals = la->a_nvals; @@ -358,21 +369,17 @@ account_locked( Operation *op, Entry *e, #define PPOLICY_EXPIRE 0x80L /* primitive + 0 */ #define PPOLICY_GRACE 0x81L /* primitive + 1 */ +static const char ppolicy_ctrl_oid[] = LDAP_CONTROL_PASSWORDPOLICYRESPONSE; + static LDAPControl * -create_passcontrol( int exptime, int grace, LDAPPasswordPolicyError err ) +create_passcontrol( Operation *op, int exptime, int grace, LDAPPasswordPolicyError err ) { - char berbuf[LBER_ELEMENT_SIZEOF], bb2[LBER_ELEMENT_SIZEOF]; - BerElement *ber = (BerElement *)berbuf, *b2 = (BerElement *)bb2; - LDAPControl *c; + BerElementBuffer berbuf, bb2; + BerElement *ber = (BerElement *) &berbuf, *b2 = (BerElement *) &bb2; + LDAPControl c = { 0 }, *cp; struct berval bv; - c = ch_calloc( sizeof( LDAPControl ), 1 ); - if ( c == NULL ) { - return NULL; - } - c->ldctl_oid = LDAP_CONTROL_PASSWORDPOLICYRESPONSE; - c->ldctl_iscritical = 0; - BER_BVZERO( &c->ldctl_value ); + BER_BVZERO( &c.ldctl_value ); ber_init2( ber, NULL, LBER_USE_DER ); ber_printf( ber, "{" /*}*/ ); @@ -398,12 +405,18 @@ create_passcontrol( int exptime, int grace, LDAPPasswordPolicyError err ) } ber_printf( ber, /*{*/ "N}" ); - if (ber_flatten2( ber, &(c->ldctl_value), 1 ) == LBER_DEFAULT) { - ch_free(c); - c = NULL; + if (ber_flatten2( ber, &c.ldctl_value, 0 ) == -1) { + return NULL; } + cp = op->o_tmpalloc( sizeof( LDAPControl ) + c.ldctl_value.bv_len, op->o_tmpmemctx ); + cp->ldctl_oid = (char *)ppolicy_ctrl_oid; + cp->ldctl_iscritical = 0; + cp->ldctl_value.bv_val = (char *)&cp[1]; + cp->ldctl_value.bv_len = c.ldctl_value.bv_len; + AC_MEMCPY( cp->ldctl_value.bv_val, c.ldctl_value.bv_val, c.ldctl_value.bv_len ); (void)ber_free_buf(ber); - return c; + + return cp; } static LDAPControl ** @@ -571,14 +584,19 @@ password_scheme( struct berval *cred, struct berval *sch ) } static int -check_password_quality( struct berval *cred, PassPolicy *pp, LDAPPasswordPolicyError *err, Entry *e ) +check_password_quality( struct berval *cred, PassPolicy *pp, LDAPPasswordPolicyError *err, Entry *e, char **txt ) { int rc = LDAP_SUCCESS, ok = LDAP_SUCCESS; - char *ptr = cred->bv_val; + char *ptr; struct berval sch; assert( cred != NULL ); assert( pp != NULL ); + assert( txt != NULL ); + + ptr = cred->bv_val; + + *txt = NULL; if ((cred->bv_len == 0) || (pp->pwdMinLength > cred->bv_len)) { rc = LDAP_CONSTRAINT_VIOLATION; @@ -618,7 +636,7 @@ check_password_quality( struct berval *cred, PassPolicy *pp, LDAPPasswordPolicyE rc = LDAP_SUCCESS; if (pp->pwdCheckModule[0]) { -#if SLAPD_MODULES +#ifdef SLAPD_MODULES lt_dlhandle mod; const char *err; @@ -630,6 +648,11 @@ check_password_quality( struct berval *cred, PassPolicy *pp, LDAPPasswordPolicyE pp->pwdCheckModule, err, 0 ); ok = LDAP_OTHER; /* internal error */ } else { + /* FIXME: the error message ought to be passed thru a + * struct berval, with preallocated buffer and size + * passed in. Module can still allocate a buffer for + * it if the provided one is too small. + */ int (*prog)( char *passwd, char **text, Entry *ent ); if ((prog = lt_dlsym( mod, "check_password" )) == NULL) { @@ -640,16 +663,13 @@ check_password_quality( struct berval *cred, PassPolicy *pp, LDAPPasswordPolicyE pp->pwdCheckModule, err, 0 ); ok = LDAP_OTHER; } else { - char *txt = NULL; - ldap_pvt_thread_mutex_lock( &chk_syntax_mutex ); - ok = prog( cred->bv_val, &txt, e ); + ok = prog( ptr, txt, e ); ldap_pvt_thread_mutex_unlock( &chk_syntax_mutex ); if (ok != LDAP_SUCCESS) { Debug(LDAP_DEBUG_ANY, "check_password_quality: module error: (%s) %s.[%d]\n", - pp->pwdCheckModule, txt ? txt : "", ok ); - free(txt); + pp->pwdCheckModule, *txt ? *txt : "", ok ); } } @@ -675,7 +695,7 @@ parse_pwdhistory( struct berval *bv, char **oid, time_t *oldtime, struct berval { char *ptr; struct berval nv, npw; - int i, j; + ber_len_t i, j; assert (bv && (bv->bv_len > 0) && (bv->bv_val) && oldtime && oldpw ); @@ -835,6 +855,7 @@ free_pwd_history_list( pw_hist **l ) typedef struct ppbind { slap_overinst *on; int send_ctrl; + int set_restrict; LDAPControl **oldctrls; Modifications *mod; LDAPPasswordPolicyError pErr; @@ -850,9 +871,8 @@ ctrls_cleanup( Operation *op, SlapReply *rs, LDAPControl **oldctrls ) assert( rs->sr_ctrls[0] != NULL ); for ( n = 0; rs->sr_ctrls[n]; n++ ) { - if ( rs->sr_ctrls[n]->ldctl_oid == LDAP_CONTROL_PASSWORDPOLICYRESPONSE ) { - ch_free( rs->sr_ctrls[n]->ldctl_value.bv_val ); - ch_free( rs->sr_ctrls[n] ); + if ( rs->sr_ctrls[n]->ldctl_oid == ppolicy_ctrl_oid ) { + op->o_tmpfree( rs->sr_ctrls[n], op->o_tmpmemctx ); rs->sr_ctrls[n] = (LDAPControl *)(-1); break; } @@ -918,6 +938,7 @@ ppolicy_bind_response( Operation *op, SlapReply *rs ) m->sml_flags = 0; m->sml_type = ad_pwdFailureTime->ad_cname; m->sml_desc = ad_pwdFailureTime; + m->sml_numvals = 1; m->sml_values = ch_calloc( sizeof(struct berval), 2 ); m->sml_nvalues = ch_calloc( sizeof(struct berval), 2 ); @@ -967,6 +988,7 @@ ppolicy_bind_response( Operation *op, SlapReply *rs ) m->sml_flags = 0; m->sml_type = ad_pwdAccountLockedTime->ad_cname; m->sml_desc = ad_pwdAccountLockedTime; + m->sml_numvals = 1; m->sml_values = ch_calloc( sizeof(struct berval), 2 ); m->sml_nvalues = ch_calloc( sizeof(struct berval), 2 ); ber_dupbv( &m->sml_values[0], ×tamp ); @@ -1002,8 +1024,10 @@ ppolicy_bind_response( Operation *op, SlapReply *rs ) * that we are disallowed from doing anything * other than change password. */ - ber_dupbv( &pwcons[op->o_conn->c_conn_idx].dn, - &op->o_conn->c_ndn ); + if ( ppb->set_restrict ) { + ber_dupbv( &pwcons[op->o_conn->c_conn_idx].dn, + &op->o_conn->c_ndn ); + } ppb->pErr = PP_changeAfterReset; @@ -1059,6 +1083,7 @@ grace: m->sml_flags = 0; m->sml_type = ad_pwdGraceUseTime->ad_cname; m->sml_desc = ad_pwdGraceUseTime; + m->sml_numvals = 1; m->sml_values = ch_calloc( sizeof(struct berval), 2 ); m->sml_nvalues = ch_calloc( sizeof(struct berval), 2 ); ber_dupbv( &m->sml_values[0], ×tamp ); @@ -1110,17 +1135,42 @@ locked: Operation op2 = *op; SlapReply r2 = { REP_RESULT }; slap_callback cb = { NULL, slap_null_cb, NULL, NULL }; + pp_info *pi = on->on_bi.bi_private; + LDAPControl c, *ca[2]; - /* FIXME: Need to handle replication of some (but not all) - * of the operational attributes... - */ op2.o_tag = LDAP_REQ_MODIFY; op2.o_callback = &cb; op2.orm_modlist = mod; + op2.orm_no_opattrs = 0; op2.o_dn = op->o_bd->be_rootdn; 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 ); + + /* If this server is a shadow and forward_updates is true, + * use the frontend to perform this modify. That will trigger + * the update referral, which can then be forwarded by the + * chain overlay. Obviously the updateref and chain overlay + * must be configured appropriately for this to be useful. + */ + if ( SLAP_SHADOW( op->o_bd ) && pi->forward_updates ) { + op2.o_bd = frontendDB; + + /* Must use Relax control since these are no-user-mod */ + op2.o_relax = SLAP_CONTROL_CRITICAL; + op2.o_ctrls = ca; + ca[0] = &c; + ca[1] = NULL; + BER_BVZERO( &c.ldctl_value ); + c.ldctl_iscritical = 1; + c.ldctl_oid = LDAP_CONTROL_RELAX; + } else { + /* If not forwarding, don't update opattrs and don't replicate */ + if ( SLAP_SINGLE_SHADOW( op->o_bd )) { + op2.orm_no_opattrs = 1; + op2.o_dont_replicate = 1; + } + op2.o_bd->bd_info = (BackendInfo *)on->on_info; + } + rc = op2.o_bd->be_modify( &op2, &r2 ); slap_mods_free( mod, 1 ); } @@ -1132,7 +1182,7 @@ locked: if ( ppb->pErr == PP_accountLocked && !pi->use_lockout ) { ppb->pErr = PP_noError; } - ctrl = create_passcontrol( warn, ngut, ppb->pErr ); + ctrl = create_passcontrol( op, warn, ngut, ppb->pErr ); ppb->oldctrls = add_passcontrol( op, rs, ctrl ); op->o_callback->sc_cleanup = ppolicy_ctrls_cleanup; } @@ -1170,6 +1220,7 @@ ppolicy_bind( Operation *op, SlapReply *rs ) ppb = (ppbind *)(cb+1); ppb->on = on; ppb->pErr = PP_noError; + ppb->set_restrict = 1; /* Setup a callback so we can munge the result */ @@ -1192,7 +1243,6 @@ ppolicy_bind( Operation *op, SlapReply *rs ) be_entry_release_r( op, e ); if ( rc ) { - /* This will be the Draft 8 response, Unwilling is bogus */ ppb->pErr = PP_accountLocked; send_ldap_error( op, rs, LDAP_INVALID_CREDENTIALS, NULL ); return rs->sr_err; @@ -1245,7 +1295,7 @@ ppolicy_restrict( "connection restricted to password changing only\n", 0, 0, 0); if ( send_ctrl ) { LDAPControl *ctrl = NULL; - ctrl = create_passcontrol( -1, -1, PP_changeAfterReset ); + ctrl = create_passcontrol( op, -1, -1, PP_changeAfterReset ); oldctrls = add_passcontrol( op, rs, ctrl ); } op->o_bd->bd_info = (BackendInfo *)on->on_info; @@ -1260,6 +1310,88 @@ ppolicy_restrict( return SLAP_CB_CONTINUE; } +static int +ppolicy_compare_response( + Operation *op, + SlapReply *rs ) +{ + /* map compare responses to bind responses */ + if ( rs->sr_err == LDAP_COMPARE_TRUE ) + rs->sr_err = LDAP_SUCCESS; + else if ( rs->sr_err == LDAP_COMPARE_FALSE ) + rs->sr_err = LDAP_INVALID_CREDENTIALS; + + ppolicy_bind_response( op, rs ); + + /* map back to compare */ + if ( rs->sr_err == LDAP_SUCCESS ) + rs->sr_err = LDAP_COMPARE_TRUE; + else if ( rs->sr_err == LDAP_INVALID_CREDENTIALS ) + rs->sr_err = LDAP_COMPARE_FALSE; + + return SLAP_CB_CONTINUE; +} + +static int +ppolicy_compare( + Operation *op, + SlapReply *rs ) +{ + slap_overinst *on = (slap_overinst *)op->o_bd->bd_info; + + if ( ppolicy_restrict( op, rs ) != SLAP_CB_CONTINUE ) + return rs->sr_err; + + /* Did we receive a password policy request control? + * Are we testing the userPassword? + */ + if ( op->o_ctrlflag[ppolicy_cid] && + op->orc_ava->aa_desc == slap_schema.si_ad_userPassword ) { + Entry *e; + int rc; + ppbind *ppb; + slap_callback *cb; + + op->o_bd->bd_info = (BackendInfo *)on->on_info; + rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &e ); + + if ( rc != LDAP_SUCCESS ) { + return SLAP_CB_CONTINUE; + } + + cb = op->o_tmpcalloc( sizeof(ppbind)+sizeof(slap_callback), + 1, op->o_tmpmemctx ); + ppb = (ppbind *)(cb+1); + ppb->on = on; + ppb->pErr = PP_noError; + ppb->send_ctrl = 1; + /* failures here don't lockout the connection */ + ppb->set_restrict = 0; + + /* Setup a callback so we can munge the result */ + + cb->sc_response = ppolicy_compare_response; + cb->sc_next = op->o_callback->sc_next; + cb->sc_private = ppb; + op->o_callback->sc_next = cb; + + op->o_bd->bd_info = (BackendInfo *)on; + ppolicy_get( op, e, &ppb->pp ); + + rc = account_locked( op, e, &ppb->pp, &ppb->mod ); + + op->o_bd->bd_info = (BackendInfo *)on->on_info; + be_entry_release_r( op, e ); + + if ( rc ) { + ppb->pErr = PP_accountLocked; + send_ldap_error( op, rs, LDAP_COMPARE_FALSE, NULL ); + return rs->sr_err; + } + } + return SLAP_CB_CONTINUE; +} + static int ppolicy_add( Operation *op, @@ -1300,21 +1432,25 @@ ppolicy_add( struct berval *bv = &(pa->a_vals[0]); int rc, send_ctrl = 0; LDAPPasswordPolicyError pErr = PP_noError; + char *txt; /* Did we receive a password policy request control? */ if ( op->o_ctrlflag[ppolicy_cid] ) { send_ctrl = 1; } - rc = check_password_quality( bv, &pp, &pErr, op->ora_e ); + rc = check_password_quality( bv, &pp, &pErr, op->ora_e, &txt ); if (rc != LDAP_SUCCESS) { LDAPControl **oldctrls = NULL; op->o_bd->bd_info = (BackendInfo *)on->on_info; if ( send_ctrl ) { LDAPControl *ctrl = NULL; - ctrl = create_passcontrol( -1, -1, pErr ); + ctrl = create_passcontrol( op, -1, -1, pErr ); oldctrls = add_passcontrol( op, rs, ctrl ); } - send_ldap_error( op, rs, rc, "Password fails quality checking policy" ); + send_ldap_error( op, rs, rc, txt ? txt : "Password fails quality checking policy" ); + if ( txt ) { + free( txt ); + } if ( send_ctrl ) { ctrls_cleanup( op, rs, oldctrls ); } @@ -1395,12 +1531,14 @@ ppolicy_modify( Operation *op, SlapReply *rs ) Attribute *pa, *ha, at; const char *txt; pw_hist *tl = NULL, *p; - int zapReset, send_ctrl = 0; + int zapReset, send_ctrl = 0, free_txt = 0; Entry *e; struct berval newpw = BER_BVNULL, oldpw = BER_BVNULL, *bv, cr[2]; LDAPPasswordPolicyError pErr = PP_noError; + LDAPControl *ctrl = NULL; LDAPControl **oldctrls = NULL; + int is_pwdexop = 0; op->o_bd->bd_info = (BackendInfo *)on->on_info; rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &e ); @@ -1466,6 +1604,7 @@ ppolicy_modify( Operation *op, SlapReply *rs ) ml->sml_flags = SLAP_MOD_INTERNAL; ml->sml_type.bv_val = NULL; ml->sml_desc = ad_pwdGraceUseTime; + ml->sml_numvals = 0; ml->sml_values = NULL; ml->sml_nvalues = NULL; ml->sml_next = NULL; @@ -1478,6 +1617,7 @@ ppolicy_modify( Operation *op, SlapReply *rs ) ml->sml_flags = SLAP_MOD_INTERNAL; ml->sml_type.bv_val = NULL; ml->sml_desc = ad_pwdAccountLockedTime; + ml->sml_numvals = 0; ml->sml_values = NULL; ml->sml_nvalues = NULL; ml->sml_next = NULL; @@ -1489,6 +1629,7 @@ ppolicy_modify( Operation *op, SlapReply *rs ) ml->sml_flags = SLAP_MOD_INTERNAL; ml->sml_type.bv_val = NULL; ml->sml_desc = ad_pwdFailureTime; + ml->sml_numvals = 0; ml->sml_values = NULL; ml->sml_nvalues = NULL; ml->sml_next = NULL; @@ -1512,11 +1653,12 @@ ppolicy_modify( Operation *op, SlapReply *rs ) slap_callback *sc; for ( sc = op->o_callback; sc; sc=sc->sc_next ) { - if ( sc->sc_response == slap_replog_cb && + if ( sc->sc_response == slap_null_cb && sc->sc_private ) { req_pwdexop_s *qpw = sc->sc_private; newpw = qpw->rs_new; oldpw = qpw->rs_old; + is_pwdexop = 1; break; } } @@ -1541,38 +1683,38 @@ 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)) { - addmod = ml; - - /* 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( addmod->sml_values != NULL ); - assert( !BER_BVISNULL( &addmod->sml_values[ 0 ] ) ); - if ( !BER_BVISNULL( &addmod->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 ); } } - } else if ( !is_at_operational( ml->sml_desc->ad_type ) ) { + } else if ( !(ml->sml_flags & SLAP_MOD_INTERNAL) && !is_at_operational( ml->sml_desc->ad_type ) ) { mod_pw_only = 0; /* modifying something other than password */ } @@ -1613,30 +1755,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; @@ -1658,6 +1788,23 @@ ppolicy_modify( Operation *op, SlapReply *rs ) if (be_isroot( op )) goto do_modify; + /* NOTE: according to draft-behera-ldap-password-policy + * pwdAllowUserChange == FALSE must only prevent pwd changes + * by the user the pwd belongs to (ITS#7021) */ + if (!pp.pwdAllowUserChange && dn_match(&op->o_req_ndn, &op->o_ndn)) { + 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 @@ -1668,6 +1815,7 @@ ppolicy_modify( Operation *op, SlapReply *rs ) ml->sml_flags = SLAP_MOD_INTERNAL; ml->sml_desc = pp.ad; ml->sml_type = pp.ad->ad_cname; + ml->sml_numvals = 1; ml->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) ); ber_dupbv( &ml->sml_values[0], &oldpw ); BER_BVZERO( &ml->sml_values[1] ); @@ -1687,14 +1835,10 @@ 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; - } - - if (pp.pwdMinAge > 0) { + /* 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 )) && + pp.pwdMinAge > 0) { time_t pwtime = (time_t)-1, now; int age; @@ -1715,8 +1859,6 @@ ppolicy_modify( Operation *op, SlapReply *rs ) /* * we have a password to check */ - const char *txt; - bv = oldpw.bv_val ? &oldpw : delmod->sml_values; /* FIXME: no access checking? */ rc = slap_passwd_check( op, NULL, pa, bv, &txt ); @@ -1750,15 +1892,21 @@ ppolicy_modify( Operation *op, SlapReply *rs ) bv = newpw.bv_val ? &newpw : &addmod->sml_values[0]; if (pp.pwdCheckQuality > 0) { - rc = check_password_quality( bv, &pp, &pErr, e ); + rc = check_password_quality( bv, &pp, &pErr, e, (char **)&txt ); if (rc != LDAP_SUCCESS) { rs->sr_err = rc; - rs->sr_text = "Password fails quality checking policy"; + if ( txt ) { + rs->sr_text = txt; + free_txt = 1; + } else { + rs->sr_text = "Password fails quality checking policy"; + } goto return_results; } } - if (pa) { + /* If pwdInHistory is zero, passwords may be reused */ + if (pa && pp.pwdInHistory > 0) { /* * Last check - the password history. */ @@ -1774,8 +1922,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. @@ -1828,22 +1974,26 @@ do_modify: timestamp.bv_len = sizeof(timebuf); slap_timestamp( &now, ×tamp ); - mods = (Modifications *) ch_calloc( sizeof( Modifications ), 1 ); - mods->sml_desc = ad_pwdChangedTime; + mods = NULL; if (pwmop != LDAP_MOD_DELETE) { + mods = (Modifications *) ch_calloc( sizeof( Modifications ), 1 ); mods->sml_op = LDAP_MOD_REPLACE; + mods->sml_numvals = 1; mods->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) ); ber_dupbv( &mods->sml_values[0], ×tamp ); BER_BVZERO( &mods->sml_values[1] ); assert( !BER_BVISNULL( &mods->sml_values[0] ) ); - - } else { + } else if (attr_find(e->e_attrs, ad_pwdChangedTime )) { + mods = (Modifications *) ch_calloc( sizeof( Modifications ), 1 ); mods->sml_op = LDAP_MOD_DELETE; } - mods->sml_flags = SLAP_MOD_INTERNAL; - mods->sml_next = NULL; - modtail->sml_next = mods; - modtail = mods; + if (mods) { + mods->sml_desc = ad_pwdChangedTime; + mods->sml_flags = SLAP_MOD_INTERNAL; + mods->sml_next = NULL; + modtail->sml_next = mods; + modtail = mods; + } if (attr_find(e->e_attrs, ad_pwdGraceUseTime )) { mods = (Modifications *) ch_calloc( sizeof( Modifications ), 1 ); @@ -1908,6 +2058,7 @@ do_modify: mods->sml_op = LDAP_MOD_DELETE; mods->sml_flags = SLAP_MOD_INTERNAL; mods->sml_desc = ad_pwdHistory; + mods->sml_numvals = hsize - pp.pwdInHistory + 1; mods->sml_values = ch_calloc( sizeof( struct berval ), hsize - pp.pwdInHistory + 2 ); BER_BVZERO( &mods->sml_values[ hsize - pp.pwdInHistory + 1 ] ); @@ -1939,6 +2090,7 @@ do_modify: mods->sml_type.bv_val = NULL; mods->sml_desc = ad_pwdHistory; mods->sml_nvalues = NULL; + mods->sml_numvals = 1; mods->sml_values = ch_calloc( sizeof( struct berval ), 2 ); mods->sml_values[ 1 ].bv_val = NULL; mods->sml_values[ 1 ].bv_len = 0; @@ -2000,14 +2152,25 @@ return_results: op->o_bd->bd_info = (BackendInfo *)on->on_info; be_entry_release_r( op, e ); if ( send_ctrl ) { - LDAPControl *ctrl = NULL; - - ctrl = create_passcontrol( -1, -1, pErr ); + ctrl = create_passcontrol( op, -1, -1, pErr ); oldctrls = add_passcontrol( op, rs, ctrl ); } send_ldap_result( op, rs ); + if ( free_txt ) { + free( (char *)txt ); + rs->sr_text = NULL; + } if ( send_ctrl ) { - ctrls_cleanup( op, rs, oldctrls ); + if ( is_pwdexop ) { + if ( rs->sr_flags & REP_CTRLS_MUSTBEFREED ) { + op->o_tmpfree( oldctrls, op->o_tmpmemctx ); + } + oldctrls = NULL; + rs->sr_flags |= REP_CTRLS_MUSTBEFREED; + + } else { + ctrls_cleanup( op, rs, oldctrls ); + } } return rs->sr_err; } @@ -2018,8 +2181,8 @@ ppolicy_parseCtrl( SlapReply *rs, LDAPControl *ctrl ) { - if ( ctrl->ldctl_value.bv_len ) { - rs->sr_text = "passwordPolicyRequest control value not empty"; + if ( !BER_BVISNULL( &ctrl->ldctl_value ) ) { + rs->sr_text = "passwordPolicyRequest control value not absent"; return LDAP_PROTOCOL_ERROR; } op->o_ctrlflag[ppolicy_cid] = ctrl->ldctl_iscritical @@ -2069,11 +2232,22 @@ attrNormalize( static int ppolicy_db_init( - BackendDB *be + BackendDB *be, + ConfigReply *cr ) { slap_overinst *on = (slap_overinst *) be->bd_info; + if ( SLAP_ISGLOBALOVERLAY( be ) ) { + /* do not allow slapo-ppolicy to be global by now (ITS#5858) */ + if ( cr ){ + snprintf( cr->msg, sizeof(cr->msg), + "slapo-ppolicy cannot be global" ); + Debug( LDAP_DEBUG_ANY, "%s\n", cr->msg, 0, 0 ); + } + return 1; + } + /* Has User Schema been initialized yet? */ if ( !pwd_UsSchema[0].ad[0] ) { const char *err; @@ -2082,7 +2256,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 ); + Debug( LDAP_DEBUG_ANY, "%s\n", cr->msg, 0, 0 ); + } return code; } } @@ -2104,29 +2283,45 @@ 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; } static int ppolicy_db_open( - BackendDB *be + BackendDB *be, + ConfigReply *cr ) { + ov_count++; return overlay_register_control( be, LDAP_CONTROL_PASSWORDPOLICYREQUEST ); } static int ppolicy_close( - BackendDB *be + BackendDB *be, + ConfigReply *cr ) { slap_overinst *on = (slap_overinst *) be->bd_info; pp_info *pi = on->on_bi.bi_private; - - free( pwcons ); + +#ifdef SLAP_CONFIG_DELETE + overlay_unregister_control( be, LDAP_CONTROL_PASSWORDPOLICYREQUEST ); +#endif /* SLAP_CONFIG_DELETE */ + + /* Perhaps backover should provide bi_destroy hooks... */ + ov_count--; + if ( ov_count <=0 && pwcons ) { + pwcons--; + free( pwcons ); + pwcons = NULL; + } free( pi->def_policy.bv_val ); free( pi ); @@ -2162,7 +2357,7 @@ int ppolicy_initialize() 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 ); + Debug( LDAP_DEBUG_ANY, "Failed to register control %d\n", code, 0, 0 ); return code; } @@ -2175,7 +2370,7 @@ int ppolicy_initialize() ppolicy.on_bi.bi_op_add = ppolicy_add; ppolicy.on_bi.bi_op_bind = ppolicy_bind; - ppolicy.on_bi.bi_op_compare = ppolicy_restrict; + ppolicy.on_bi.bi_op_compare = ppolicy_compare; ppolicy.on_bi.bi_op_delete = ppolicy_restrict; ppolicy.on_bi.bi_op_modify = ppolicy_modify; ppolicy.on_bi.bi_op_search = ppolicy_restrict;