📄 ppolicy.c
字号:
int ngut = -1, warn = -1, age, rc; Attribute *a; time_t now, pwtime = (time_t)-1; char nowstr[ LDAP_LUTIL_GENTIME_BUFSIZE ]; struct berval timestamp; BackendInfo *bi = op->o_bd->bd_info; Entry *e; /* If we already know it's locked, just get on with it */ if ( ppb->pErr != PP_noError ) { goto locked; } 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 = bi; if ( rc != LDAP_SUCCESS ) { return SLAP_CB_CONTINUE; } now = slap_get_time(); /* stored for later consideration */ timestamp.bv_val = nowstr; timestamp.bv_len = sizeof(nowstr); slap_timestamp( &now, ×tamp ); if ( rs->sr_err == LDAP_INVALID_CREDENTIALS ) { int i = 0, fc = 0; m = ch_calloc( sizeof(Modifications), 1 ); m->sml_op = LDAP_MOD_ADD; m->sml_flags = 0; m->sml_type = ad_pwdFailureTime->ad_cname; m->sml_desc = ad_pwdFailureTime; 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 ); ber_dupbv( &m->sml_nvalues[0], ×tamp ); m->sml_next = mod; mod = m; /* * Count the pwdFailureTimes - if it's * greater than the policy pwdMaxFailure, * then lock the account. */ if ((a = attr_find( e->e_attrs, ad_pwdFailureTime )) != NULL) { for(i=0; a->a_nvals[i].bv_val; i++) { /* * If the interval is 0, then failures * stay on the record until explicitly * reset by successful authentication. */ if (ppb->pp.pwdFailureCountInterval == 0) { fc++; } else if (now <= parse_time(a->a_nvals[i].bv_val) + ppb->pp.pwdFailureCountInterval) { fc++; } /* * We only count those failures * which are not due to expire. */ } } if ((ppb->pp.pwdMaxFailure > 0) && (fc >= ppb->pp.pwdMaxFailure - 1)) { /* * We subtract 1 from the failure max * because the new failure entry hasn't * made it to the entry yet. */ m = ch_calloc( sizeof(Modifications), 1 ); m->sml_op = LDAP_MOD_REPLACE; m->sml_flags = 0; m->sml_type = ad_pwdAccountLockedTime->ad_cname; m->sml_desc = ad_pwdAccountLockedTime; 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 ); ber_dupbv( &m->sml_nvalues[0], ×tamp ); m->sml_next = mod; mod = m; } } else if ( rs->sr_err == LDAP_SUCCESS ) { if ((a = attr_find( e->e_attrs, ad_pwdChangedTime )) != NULL) pwtime = parse_time( a->a_nvals[0].bv_val ); /* delete all pwdFailureTimes */ if ( attr_find( e->e_attrs, ad_pwdFailureTime )) { m = ch_calloc( sizeof(Modifications), 1 ); m->sml_op = LDAP_MOD_DELETE; m->sml_flags = 0; m->sml_type = ad_pwdFailureTime->ad_cname; m->sml_desc = ad_pwdFailureTime; m->sml_next = mod; mod = m; } /* * check to see if the password must be changed */ if ( ppb->pp.pwdMustChange && (a = attr_find( e->e_attrs, ad_pwdReset )) && bvmatch( &a->a_nvals[0], &slap_true_bv ) ) { /* * need to inject client controls here to give * more information. For the moment, we ensure * 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 ); ppb->pErr = PP_changeAfterReset; } else { /* * the password does not need to be changed, so * we now check whether the password has expired. * * We can skip this bit if passwords don't age in * the policy. Also, if there was no pwdChangedTime * attribute in the entry, the password never expires. */ if (ppb->pp.pwdMaxAge == 0) goto grace; if (pwtime != (time_t)-1) { /* * Check: was the last change time of * the password older than the maximum age * allowed. (Ignore case 2 from I-D, it's just silly.) */ if (now - pwtime > ppb->pp.pwdMaxAge ) pwExpired = 1; } }grace: if (!pwExpired) goto check_expiring_password; if ((a = attr_find( e->e_attrs, ad_pwdGraceUseTime )) == NULL) ngut = ppb->pp.pwdGraceAuthNLimit; else { for(ngut=0; a->a_nvals[ngut].bv_val; ngut++); ngut = ppb->pp.pwdGraceAuthNLimit - ngut; } /* * ngut is the number of remaining grace logins */ Debug( LDAP_DEBUG_ANY, "ppolicy_bind: Entry %s has an expired password: %d grace logins\n", e->e_name.bv_val, ngut, 0); if (ngut < 1) { ppb->pErr = PP_passwordExpired; rs->sr_err = LDAP_INVALID_CREDENTIALS; goto done; } /* * Add a grace user time to the entry */ m = ch_calloc( sizeof(Modifications), 1 ); m->sml_op = LDAP_MOD_ADD; m->sml_flags = 0; m->sml_type = ad_pwdGraceUseTime->ad_cname; m->sml_desc = ad_pwdGraceUseTime; 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 ); ber_dupbv( &m->sml_nvalues[0], ×tamp ); m->sml_next = mod; mod = m;check_expiring_password: /* * Now we need to check to see * if it is about to expire, and if so, should the user * be warned about it in the password policy control. * * If the password has expired, and we're in the grace period, then * we don't need to do this bit. Similarly, if we don't have password * aging, then there's no need to do this bit either. */ if ((ppb->pp.pwdMaxAge < 1) || (pwExpired) || (ppb->pp.pwdExpireWarning < 1)) goto done; age = (int)(now - pwtime); /* * We know that there is a password Change Time attribute - if * there wasn't, then the pwdExpired value would be true, unless * there is no password aging - and if there is no password aging, * then this section isn't called anyway - you can't have an * expiring password if there's no limit to expire. */ if (ppb->pp.pwdMaxAge - age < ppb->pp.pwdExpireWarning ) { /* * Set the warning value. */ warn = ppb->pp.pwdMaxAge - age; /* seconds left until expiry */ if (warn < 0) warn = 0; /* something weird here - why is pwExpired not set? */ Debug( LDAP_DEBUG_ANY, "ppolicy_bind: Setting warning for password expiry for %s = %d seconds\n", op->o_req_dn.bv_val, warn, 0 ); } }done: op->o_bd->bd_info = (BackendInfo *)on->on_info; be_entry_release_r( op, e );locked: if ( mod ) { Operation op2 = *op; SlapReply r2 = { REP_RESULT }; slap_callback cb = { NULL, slap_null_cb, NULL, NULL }; /* 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.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 ); slap_mods_free( mod, 1 ); } if ( ppb->send_ctrl ) { LDAPControl *ctrl = NULL; pp_info *pi = on->on_bi.bi_private; /* Do we really want to tell that the account is locked? */ if ( ppb->pErr == PP_accountLocked && !pi->use_lockout ) { ppb->pErr = PP_noError; } ctrl = create_passcontrol( warn, ngut, ppb->pErr ); ppb->oldctrls = add_passcontrol( op, rs, ctrl ); op->o_callback->sc_cleanup = ppolicy_ctrls_cleanup; } op->o_bd->bd_info = bi; return SLAP_CB_CONTINUE;}static intppolicy_bind( Operation *op, SlapReply *rs ){ slap_overinst *on = (slap_overinst *)op->o_bd->bd_info; /* Reset lockout status on all Bind requests */ if ( !BER_BVISEMPTY( &pwcons[op->o_conn->c_conn_idx].dn )) { ch_free( pwcons[op->o_conn->c_conn_idx].dn.bv_val ); BER_BVZERO( &pwcons[op->o_conn->c_conn_idx].dn ); } /* Root bypasses policy */ if ( !be_isroot_dn( op->o_bd, &op->o_req_ndn )) { 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; /* Setup a callback so we can munge the result */ cb->sc_response = ppolicy_bind_response; cb->sc_next = op->o_callback->sc_next; cb->sc_private = ppb; op->o_callback->sc_next = cb; /* Did we receive a password policy request control? */ if ( op->o_ctrlflag[ppolicy_cid] ) { ppb->send_ctrl = 1; } 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 ) { /* 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; } } return SLAP_CB_CONTINUE;}/* Reset the restricted info for the next session on this connection */static intppolicy_connection_destroy( BackendDB *bd, Connection *conn ){ if ( !BER_BVISEMPTY( &pwcons[conn->c_conn_idx].dn )) { ch_free( pwcons[conn->c_conn_idx].dn.bv_val ); BER_BVZERO( &pwcons[conn->c_conn_idx].dn ); } return SLAP_CB_CONTINUE;}/* Check if this connection is restricted */static intppolicy_restrict( Operation *op, SlapReply *rs ){ slap_overinst *on = (slap_overinst *)op->o_bd->bd_info; int send_ctrl = 0; /* Did we receive a password policy request control? */ if ( op->o_ctrlflag[ppolicy_cid] ) { send_ctrl = 1; } if ( op->o_conn && !BER_BVISEMPTY( &pwcons[op->o_conn->c_conn_idx].dn )) { LDAPControl **oldctrls; /* if the current authcDN doesn't match the one we recorded, * then an intervening Bind has succeeded and the restriction * no longer applies. (ITS#4516) */ if ( !dn_match( &op->o_conn->c_ndn, &pwcons[op->o_conn->c_conn_idx].dn )) { ch_free( pwcons[op->o_conn->c_conn_idx].dn.bv_val ); BER_BVZERO( &pwcons[op->o_conn->c_conn_idx].dn ); return SLAP_CB_CONTINUE; } Debug( LDAP_DEBUG_TRACE, "connection restricted to password changing only\n", 0, 0, 0); if ( send_ctrl ) { LDAPControl *ctrl = NULL; ctrl = create_passcontrol( -1, -1, PP_changeAfterReset ); oldctrls = add_passcontrol( op, rs, ctrl ); } op->o_bd->bd_info = (BackendInfo *)on->on_info; send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS, "Operations are restricted to bind/unbind/abandon/StartTLS/modify password" ); if ( send_ctrl ) { ctrls_cleanup( op, rs, oldctrls ); } return rs->sr_err; } return SLAP_CB_CONTINUE;}static intppolicy_add( Operation *op, SlapReply *rs ){ slap_overinst *on = (slap_overinst *)op->o_bd->bd_info; pp_info *pi = on->on_bi.bi_private; PassPolicy pp; Attribute *pa; const char *txt; if ( ppolicy_restrict( op, rs ) != SLAP_CB_CONTINUE ) return rs->sr_err; /* If this is a replica, assume the master checked everything */ if ( be_shadow_update( op )) return SLAP_CB_CONTINUE; /* Check for password in entry */ if ((pa = attr_find( op->oq_add.rs_e->e_attrs, slap_schema.si_ad_userPassword ))) { assert( pa->a_vals ); assert( !BER_BVISNULL( &pa->a_vals[ 0 ] ) ); if ( !BER_BVISNULL( &pa->a_vals[ 1 ] ) ) { send_ldap_error( op, rs, LDAP_CONSTRAINT_VIOLATION, "Password policy only allows one password value" ); return rs->sr_err; } /* * new entry contains a password - if we're not the root user * then we need to check that the password fits in with the * security policy for the new entry. */ ppolicy_get( op, op->ora_e, &pp ); if (pp.pwdCheckQuality > 0 && !be_isroot( op )) { struct berval *bv = &(pa->a_vals[0]); int rc, send_ctrl = 0; LDAPPasswordPolicyError pErr = PP_noError; /* 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 ); 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 ); oldctrls = add_passcontrol( op, rs, ctrl ); } send_ldap_error( op, rs, rc, "Password fails quality checking policy" ); if ( send_ctrl ) { ctrls_cleanup( op, rs, oldctrls ); } 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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -