2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 2004-2015 The OpenLDAP Foundation.
5 * Portions Copyright 2004-2005 Howard Chu, Symas Corporation.
6 * Portions Copyright 2004 Hewlett-Packard Company.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
13 * A copy of this license is available in the file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
18 * This work was developed by Howard Chu for inclusion in
19 * OpenLDAP Software, based on prior work by Neil Dunbar (HP).
20 * This work was sponsored by the Hewlett-Packard Company.
25 /* This file implements "Password Policy for LDAP Directories",
26 * based on draft behera-ldap-password-policy-09
29 #ifdef SLAPD_OVER_PPOLICY
35 #define LIBLTDL_DLL_IMPORT /* Win32: don't re-export libltdl's symbols */
40 #include <ac/string.h>
44 #ifndef MODULE_NAME_SZ
45 #define MODULE_NAME_SZ 256
48 #ifndef PPOLICY_DEFAULT_MAXRECORDED_FAILURE
49 #define PPOLICY_DEFAULT_MAXRECORDED_FAILURE 5
52 /* Per-instance configuration information */
53 typedef struct pp_info {
54 struct berval def_policy; /* DN of default policy subentry */
55 int use_lockout; /* send AccountLocked result? */
56 int hash_passwords; /* transparently hash cleartext pwds */
57 int forward_updates; /* use frontend for policy state updates */
60 /* Our per-connection info - note, it is not per-instance, it is
61 * used by all instances
63 typedef struct pw_conn {
64 struct berval dn; /* DN of restricted user */
67 static pw_conn *pwcons;
68 static int ppolicy_cid;
71 typedef struct pass_policy {
72 AttributeDescription *ad; /* attribute to which the policy applies */
73 int pwdMinAge; /* minimum time (seconds) until passwd can change */
74 int pwdMaxAge; /* time in seconds until pwd will expire after change */
75 int pwdInHistory; /* number of previous passwords kept */
76 int pwdCheckQuality; /* 0 = don't check quality, 1 = check if possible,
77 2 = check mandatory; fail if not possible */
78 int pwdMinLength; /* minimum number of chars in password */
79 int pwdExpireWarning; /* number of seconds that warning controls are
80 sent before a password expires */
81 int pwdGraceAuthNLimit; /* number of times you can log in with an
83 int pwdLockout; /* 0 = do not lockout passwords, 1 = lock them out */
84 int pwdLockoutDuration; /* time in seconds a password is locked out for */
85 int pwdMaxFailure; /* number of failed binds allowed before lockout */
86 int pwdMaxRecordedFailure; /* number of failed binds to store */
87 int pwdFailureCountInterval; /* number of seconds before failure
89 int pwdMustChange; /* 0 = users can use admin set password
90 1 = users must change password after admin set */
91 int pwdAllowUserChange; /* 0 = users cannot change their passwords
92 1 = users can change them */
93 int pwdSafeModify; /* 0 = old password doesn't need to come
94 with password change request
95 1 = password change must supply existing pwd */
96 char pwdCheckModule[MODULE_NAME_SZ]; /* name of module to dynamically
97 load to check password */
100 typedef struct pw_hist {
101 time_t t; /* timestamp of history entry */
102 struct berval pw; /* old password hash */
103 struct berval bv; /* text of entire entry */
104 struct pw_hist *next;
107 /* Operational attributes */
108 static AttributeDescription *ad_pwdChangedTime, *ad_pwdAccountLockedTime,
109 *ad_pwdFailureTime, *ad_pwdHistory, *ad_pwdGraceUseTime, *ad_pwdReset,
110 *ad_pwdPolicySubentry;
112 static struct schema_info {
114 AttributeDescription **ad;
116 { "( 1.3.6.1.4.1.42.2.27.8.1.16 "
117 "NAME ( 'pwdChangedTime' ) "
118 "DESC 'The time the password was last changed' "
119 "EQUALITY generalizedTimeMatch "
120 "ORDERING generalizedTimeOrderingMatch "
121 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
122 "SINGLE-VALUE NO-USER-MODIFICATION USAGE directoryOperation )",
123 &ad_pwdChangedTime },
124 { "( 1.3.6.1.4.1.42.2.27.8.1.17 "
125 "NAME ( 'pwdAccountLockedTime' ) "
126 "DESC 'The time an user account was locked' "
127 "EQUALITY generalizedTimeMatch "
128 "ORDERING generalizedTimeOrderingMatch "
129 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
132 /* Not until Relax control is released */
133 "NO-USER-MODIFICATION "
135 "USAGE directoryOperation )",
136 &ad_pwdAccountLockedTime },
137 { "( 1.3.6.1.4.1.42.2.27.8.1.19 "
138 "NAME ( 'pwdFailureTime' ) "
139 "DESC 'The timestamps of the last consecutive authentication failures' "
140 "EQUALITY generalizedTimeMatch "
141 "ORDERING generalizedTimeOrderingMatch "
142 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
143 "NO-USER-MODIFICATION USAGE directoryOperation )",
144 &ad_pwdFailureTime },
145 { "( 1.3.6.1.4.1.42.2.27.8.1.20 "
146 "NAME ( 'pwdHistory' ) "
147 "DESC 'The history of users passwords' "
148 "EQUALITY octetStringMatch "
149 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.40 "
150 "NO-USER-MODIFICATION USAGE directoryOperation )",
152 { "( 1.3.6.1.4.1.42.2.27.8.1.21 "
153 "NAME ( 'pwdGraceUseTime' ) "
154 "DESC 'The timestamps of the grace login once the password has expired' "
155 "EQUALITY generalizedTimeMatch "
156 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
157 "NO-USER-MODIFICATION USAGE directoryOperation )",
158 &ad_pwdGraceUseTime },
159 { "( 1.3.6.1.4.1.42.2.27.8.1.22 "
160 "NAME ( 'pwdReset' ) "
161 "DESC 'The indication that the password has been reset' "
162 "EQUALITY booleanMatch "
163 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.7 "
164 "SINGLE-VALUE USAGE directoryOperation )",
166 { "( 1.3.6.1.4.1.42.2.27.8.1.23 "
167 "NAME ( 'pwdPolicySubentry' ) "
168 "DESC 'The pwdPolicy subentry in effect for this object' "
169 "EQUALITY distinguishedNameMatch "
170 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.12 "
173 /* Not until Relax control is released */
174 "NO-USER-MODIFICATION "
176 "USAGE directoryOperation )",
177 &ad_pwdPolicySubentry },
181 /* User attributes */
182 static AttributeDescription *ad_pwdMinAge, *ad_pwdMaxAge, *ad_pwdInHistory,
183 *ad_pwdCheckQuality, *ad_pwdMinLength, *ad_pwdMaxFailure,
184 *ad_pwdGraceAuthNLimit, *ad_pwdExpireWarning, *ad_pwdLockoutDuration,
185 *ad_pwdFailureCountInterval, *ad_pwdCheckModule, *ad_pwdLockout,
186 *ad_pwdMustChange, *ad_pwdAllowUserChange, *ad_pwdSafeModify,
187 *ad_pwdAttribute, *ad_pwdMaxRecordedFailure;
189 #define TAB(name) { #name, &ad_##name }
191 static struct schema_info pwd_UsSchema[] = {
196 TAB(pwdCheckQuality),
199 TAB(pwdGraceAuthNLimit),
200 TAB(pwdExpireWarning),
202 TAB(pwdLockoutDuration),
203 TAB(pwdFailureCountInterval),
206 TAB(pwdAllowUserChange),
211 static ldap_pvt_thread_mutex_t chk_syntax_mutex;
215 PPOLICY_HASH_CLEARTEXT,
219 static ConfigDriver ppolicy_cf_default;
221 static ConfigTable ppolicycfg[] = {
222 { "ppolicy_default", "policyDN", 2, 2, 0,
223 ARG_DN|ARG_QUOTE|ARG_MAGIC|PPOLICY_DEFAULT, ppolicy_cf_default,
224 "( OLcfgOvAt:12.1 NAME 'olcPPolicyDefault' "
225 "DESC 'DN of a pwdPolicy object for uncustomized objects' "
226 "SYNTAX OMsDN SINGLE-VALUE )", NULL, NULL },
227 { "ppolicy_hash_cleartext", "on|off", 1, 2, 0,
228 ARG_ON_OFF|ARG_OFFSET|PPOLICY_HASH_CLEARTEXT,
229 (void *)offsetof(pp_info,hash_passwords),
230 "( OLcfgOvAt:12.2 NAME 'olcPPolicyHashCleartext' "
231 "DESC 'Hash passwords on add or modify' "
232 "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
233 { "ppolicy_forward_updates", "on|off", 1, 2, 0,
234 ARG_ON_OFF|ARG_OFFSET,
235 (void *)offsetof(pp_info,forward_updates),
236 "( OLcfgOvAt:12.4 NAME 'olcPPolicyForwardUpdates' "
237 "DESC 'Allow policy state updates to be forwarded via updateref' "
238 "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
239 { "ppolicy_use_lockout", "on|off", 1, 2, 0,
240 ARG_ON_OFF|ARG_OFFSET|PPOLICY_USE_LOCKOUT,
241 (void *)offsetof(pp_info,use_lockout),
242 "( OLcfgOvAt:12.3 NAME 'olcPPolicyUseLockout' "
243 "DESC 'Warn clients with AccountLocked' "
244 "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
245 { NULL, NULL, 0, 0, 0, ARG_IGNORED }
248 static ConfigOCs ppolicyocs[] = {
249 { "( OLcfgOvOc:12.1 "
250 "NAME 'olcPPolicyConfig' "
251 "DESC 'Password Policy configuration' "
252 "SUP olcOverlayConfig "
253 "MAY ( olcPPolicyDefault $ olcPPolicyHashCleartext $ "
254 "olcPPolicyUseLockout $ olcPPolicyForwardUpdates ) )",
255 Cft_Overlay, ppolicycfg },
260 ppolicy_cf_default( ConfigArgs *c )
262 slap_overinst *on = (slap_overinst *)c->bi;
263 pp_info *pi = (pp_info *)on->on_bi.bi_private;
264 int rc = ARG_BAD_CONF;
266 assert ( c->type == PPOLICY_DEFAULT );
267 Debug(LDAP_DEBUG_TRACE, "==> ppolicy_cf_default\n", 0, 0, 0);
270 case SLAP_CONFIG_EMIT:
271 Debug(LDAP_DEBUG_TRACE, "==> ppolicy_cf_default emit\n", 0, 0, 0);
273 if ( !BER_BVISEMPTY( &pi->def_policy )) {
274 rc = value_add_one( &c->rvalue_vals,
277 rc = value_add_one( &c->rvalue_nvals,
281 case LDAP_MOD_DELETE:
282 Debug(LDAP_DEBUG_TRACE, "==> ppolicy_cf_default delete\n", 0, 0, 0);
283 if ( pi->def_policy.bv_val ) {
284 ber_memfree ( pi->def_policy.bv_val );
285 pi->def_policy.bv_val = NULL;
287 pi->def_policy.bv_len = 0;
290 case SLAP_CONFIG_ADD:
291 /* fallthrough to LDAP_MOD_ADD */
293 Debug(LDAP_DEBUG_TRACE, "==> ppolicy_cf_default add\n", 0, 0, 0);
294 if ( pi->def_policy.bv_val ) {
295 ber_memfree ( pi->def_policy.bv_val );
297 pi->def_policy = c->value_ndn;
298 ber_memfree( c->value_dn.bv_val );
299 BER_BVZERO( &c->value_dn );
300 BER_BVZERO( &c->value_ndn );
311 parse_time( char *atm )
314 struct lutil_timet tt;
315 time_t ret = (time_t)-1;
317 if ( lutil_parsetime( atm, &tm ) == 0) {
318 lutil_tm2time( &tm, &tt );
325 account_locked( Operation *op, Entry *e,
326 PassPolicy *pp, Modifications **mod )
332 if ( !pp->pwdLockout )
335 if ( (la = attr_find( e->e_attrs, ad_pwdAccountLockedTime )) != NULL ) {
336 BerVarray vals = la->a_nvals;
339 * there is a lockout stamp - we now need to know if it's
342 if (vals[0].bv_val != NULL) {
346 if ((then = parse_time( vals[0].bv_val )) == (time_t)0)
349 now = slap_get_time();
351 /* Still in the future? not yet in effect */
355 if (!pp->pwdLockoutDuration)
358 if (now < then + pp->pwdLockoutDuration)
361 m = ch_calloc( sizeof(Modifications), 1 );
362 m->sml_op = LDAP_MOD_DELETE;
364 m->sml_type = ad_pwdAccountLockedTime->ad_cname;
365 m->sml_desc = ad_pwdAccountLockedTime;
374 /* IMPLICIT TAGS, all context-specific */
375 #define PPOLICY_WARNING 0xa0L /* constructed + 0 */
376 #define PPOLICY_ERROR 0x81L /* primitive + 1 */
378 #define PPOLICY_EXPIRE 0x80L /* primitive + 0 */
379 #define PPOLICY_GRACE 0x81L /* primitive + 1 */
381 static const char ppolicy_ctrl_oid[] = LDAP_CONTROL_PASSWORDPOLICYRESPONSE;
384 create_passcontrol( Operation *op, int exptime, int grace, LDAPPasswordPolicyError err )
386 BerElementBuffer berbuf, bb2;
387 BerElement *ber = (BerElement *) &berbuf, *b2 = (BerElement *) &bb2;
388 LDAPControl c = { 0 }, *cp;
392 BER_BVZERO( &c.ldctl_value );
394 ber_init2( ber, NULL, LBER_USE_DER );
395 ber_printf( ber, "{" /*}*/ );
397 if ( exptime >= 0 ) {
398 ber_init2( b2, NULL, LBER_USE_DER );
399 ber_printf( b2, "ti", PPOLICY_EXPIRE, exptime );
400 rc = ber_flatten2( b2, &bv, 1 );
401 (void)ber_free_buf(b2);
406 ber_printf( ber, "tO", PPOLICY_WARNING, &bv );
407 ch_free( bv.bv_val );
408 } else if ( grace > 0 ) {
409 ber_init2( b2, NULL, LBER_USE_DER );
410 ber_printf( b2, "ti", PPOLICY_GRACE, grace );
411 rc = ber_flatten2( b2, &bv, 1 );
412 (void)ber_free_buf(b2);
417 ber_printf( ber, "tO", PPOLICY_WARNING, &bv );
418 ch_free( bv.bv_val );
421 if (err != PP_noError ) {
422 ber_printf( ber, "te", PPOLICY_ERROR, err );
424 ber_printf( ber, /*{*/ "N}" );
426 if (ber_flatten2( ber, &c.ldctl_value, 0 ) == -1) {
429 cp = op->o_tmpalloc( sizeof( LDAPControl ) + c.ldctl_value.bv_len, op->o_tmpmemctx );
430 cp->ldctl_oid = (char *)ppolicy_ctrl_oid;
431 cp->ldctl_iscritical = 0;
432 cp->ldctl_value.bv_val = (char *)&cp[1];
433 cp->ldctl_value.bv_len = c.ldctl_value.bv_len;
434 AC_MEMCPY( cp->ldctl_value.bv_val, c.ldctl_value.bv_val, c.ldctl_value.bv_len );
436 (void)ber_free_buf(ber);
441 static LDAPControl **
442 add_passcontrol( Operation *op, SlapReply *rs, LDAPControl *ctrl )
444 LDAPControl **ctrls, **oldctrls = rs->sr_ctrls;
449 for ( ; oldctrls[n]; n++ )
454 ctrls = op->o_tmpcalloc( sizeof( LDAPControl * ), n, op->o_tmpmemctx );
458 for ( ; oldctrls[n]; n++ ) {
459 ctrls[n] = oldctrls[n];
465 rs->sr_ctrls = ctrls;
471 ppolicy_get( Operation *op, Entry *e, PassPolicy *pp )
473 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
474 pp_info *pi = on->on_bi.bi_private;
483 memset( pp, 0, sizeof(PassPolicy) );
485 pp->ad = slap_schema.si_ad_userPassword;
487 /* Users can change their own password by default */
488 pp->pwdAllowUserChange = 1;
490 if ((a = attr_find( e->e_attrs, ad_pwdPolicySubentry )) == NULL) {
492 * entry has no password policy assigned - use default
494 vals = &pi->def_policy;
499 if (vals[0].bv_val == NULL) {
500 Debug( LDAP_DEBUG_ANY,
501 "ppolicy_get: NULL value for policySubEntry\n", 0, 0, 0 );
506 op->o_bd->bd_info = (BackendInfo *)on->on_info;
507 rc = be_entry_get_rw( op, vals, NULL, NULL, 0, &pe );
508 op->o_bd->bd_info = (BackendInfo *)on;
510 if ( rc ) goto defaultpol;
512 #if 0 /* Only worry about userPassword for now */
513 if ((a = attr_find( pe->e_attrs, ad_pwdAttribute )))
514 slap_bv2ad( &a->a_vals[0], &pp->ad, &text );
517 if ( ( a = attr_find( pe->e_attrs, ad_pwdMinAge ) )
518 && lutil_atoi( &pp->pwdMinAge, a->a_vals[0].bv_val ) != 0 )
520 if ( ( a = attr_find( pe->e_attrs, ad_pwdMaxAge ) )
521 && lutil_atoi( &pp->pwdMaxAge, a->a_vals[0].bv_val ) != 0 )
523 if ( ( a = attr_find( pe->e_attrs, ad_pwdInHistory ) )
524 && lutil_atoi( &pp->pwdInHistory, a->a_vals[0].bv_val ) != 0 )
526 if ( ( a = attr_find( pe->e_attrs, ad_pwdCheckQuality ) )
527 && lutil_atoi( &pp->pwdCheckQuality, a->a_vals[0].bv_val ) != 0 )
529 if ( ( a = attr_find( pe->e_attrs, ad_pwdMinLength ) )
530 && lutil_atoi( &pp->pwdMinLength, a->a_vals[0].bv_val ) != 0 )
532 if ( ( a = attr_find( pe->e_attrs, ad_pwdMaxFailure ) )
533 && lutil_atoi( &pp->pwdMaxFailure, a->a_vals[0].bv_val ) != 0 )
535 if ( ( a = attr_find( pe->e_attrs, ad_pwdMaxRecordedFailure ) )
536 && lutil_atoi( &pp->pwdMaxRecordedFailure, a->a_vals[0].bv_val ) != 0 )
538 if ( ( a = attr_find( pe->e_attrs, ad_pwdGraceAuthNLimit ) )
539 && lutil_atoi( &pp->pwdGraceAuthNLimit, a->a_vals[0].bv_val ) != 0 )
541 if ( ( a = attr_find( pe->e_attrs, ad_pwdExpireWarning ) )
542 && lutil_atoi( &pp->pwdExpireWarning, a->a_vals[0].bv_val ) != 0 )
544 if ( ( a = attr_find( pe->e_attrs, ad_pwdFailureCountInterval ) )
545 && lutil_atoi( &pp->pwdFailureCountInterval, a->a_vals[0].bv_val ) != 0 )
547 if ( ( a = attr_find( pe->e_attrs, ad_pwdLockoutDuration ) )
548 && lutil_atoi( &pp->pwdLockoutDuration, a->a_vals[0].bv_val ) != 0 )
551 if ( ( a = attr_find( pe->e_attrs, ad_pwdCheckModule ) ) ) {
552 strncpy( pp->pwdCheckModule, a->a_vals[0].bv_val,
553 sizeof(pp->pwdCheckModule) );
554 pp->pwdCheckModule[sizeof(pp->pwdCheckModule)-1] = '\0';
557 if ((a = attr_find( pe->e_attrs, ad_pwdLockout )))
558 pp->pwdLockout = bvmatch( &a->a_nvals[0], &slap_true_bv );
559 if ((a = attr_find( pe->e_attrs, ad_pwdMustChange )))
560 pp->pwdMustChange = bvmatch( &a->a_nvals[0], &slap_true_bv );
561 if ((a = attr_find( pe->e_attrs, ad_pwdAllowUserChange )))
562 pp->pwdAllowUserChange = bvmatch( &a->a_nvals[0], &slap_true_bv );
563 if ((a = attr_find( pe->e_attrs, ad_pwdSafeModify )))
564 pp->pwdSafeModify = bvmatch( &a->a_nvals[0], &slap_true_bv );
566 if ( pp->pwdMaxRecordedFailure < pp->pwdMaxFailure )
567 pp->pwdMaxRecordedFailure = pp->pwdMaxFailure;
568 if ( !pp->pwdMaxRecordedFailure )
569 pp->pwdMaxRecordedFailure = PPOLICY_DEFAULT_MAXRECORDED_FAILURE;
571 op->o_bd->bd_info = (BackendInfo *)on->on_info;
572 be_entry_release_r( op, pe );
573 op->o_bd->bd_info = (BackendInfo *)on;
578 Debug( LDAP_DEBUG_TRACE,
579 "ppolicy_get: using default policy\n", 0, 0, 0 );
584 password_scheme( struct berval *cred, struct berval *sch )
588 assert( cred != NULL );
595 if ((cred->bv_len == 0) || (cred->bv_val == NULL) ||
596 (cred->bv_val[0] != '{')) return LDAP_OTHER;
598 for(e = 1; cred->bv_val[e] && cred->bv_val[e] != '}'; e++);
599 if (cred->bv_val[e]) {
601 rc = lutil_passwd_scheme( cred->bv_val );
604 sch->bv_val = cred->bv_val;
614 check_password_quality( struct berval *cred, PassPolicy *pp, LDAPPasswordPolicyError *err, Entry *e, char **txt )
616 int rc = LDAP_SUCCESS, ok = LDAP_SUCCESS;
620 assert( cred != NULL );
621 assert( pp != NULL );
622 assert( txt != NULL );
628 if ((cred->bv_len == 0) || (pp->pwdMinLength > cred->bv_len)) {
629 rc = LDAP_CONSTRAINT_VIOLATION;
630 if ( err ) *err = PP_passwordTooShort;
635 * We need to know if the password is already hashed - if so
636 * what scheme is it. The reason being that the "hash" of
637 * {cleartext} still allows us to check the password.
639 rc = password_scheme( cred, &sch );
640 if (rc == LDAP_SUCCESS) {
641 if ((sch.bv_val) && (strncasecmp( sch.bv_val, "{cleartext}",
642 sch.bv_len ) == 0)) {
644 * We can check the cleartext "hash"
646 ptr = cred->bv_val + sch.bv_len;
648 /* everything else, we can't check */
649 if (pp->pwdCheckQuality == 2) {
650 rc = LDAP_CONSTRAINT_VIOLATION;
651 if (err) *err = PP_insufficientPasswordQuality;
655 * We can't check the syntax of the password, but it's not
656 * mandatory (according to the policy), so we return success.
665 if (pp->pwdCheckModule[0]) {
670 if ((mod = lt_dlopen( pp->pwdCheckModule )) == NULL) {
673 Debug(LDAP_DEBUG_ANY,
674 "check_password_quality: lt_dlopen failed: (%s) %s.\n",
675 pp->pwdCheckModule, err, 0 );
676 ok = LDAP_OTHER; /* internal error */
678 /* FIXME: the error message ought to be passed thru a
679 * struct berval, with preallocated buffer and size
680 * passed in. Module can still allocate a buffer for
681 * it if the provided one is too small.
683 int (*prog)( char *passwd, char **text, Entry *ent );
685 if ((prog = lt_dlsym( mod, "check_password" )) == NULL) {
688 Debug(LDAP_DEBUG_ANY,
689 "check_password_quality: lt_dlsym failed: (%s) %s.\n",
690 pp->pwdCheckModule, err, 0 );
693 ldap_pvt_thread_mutex_lock( &chk_syntax_mutex );
694 ok = prog( ptr, txt, e );
695 ldap_pvt_thread_mutex_unlock( &chk_syntax_mutex );
696 if (ok != LDAP_SUCCESS) {
697 Debug(LDAP_DEBUG_ANY,
698 "check_password_quality: module error: (%s) %s.[%d]\n",
699 pp->pwdCheckModule, *txt ? *txt : "", ok );
706 Debug(LDAP_DEBUG_ANY, "check_password_quality: external modules not "
707 "supported. pwdCheckModule ignored.\n", 0, 0, 0);
708 #endif /* SLAPD_MODULES */
712 if (ok != LDAP_SUCCESS) {
713 rc = LDAP_CONSTRAINT_VIOLATION;
714 if (err) *err = PP_insufficientPasswordQuality;
721 parse_pwdhistory( struct berval *bv, char **oid, time_t *oldtime, struct berval *oldpw )
724 struct berval nv, npw;
727 assert (bv && (bv->bv_len > 0) && (bv->bv_val) && oldtime && oldpw );
732 *oldtime = (time_t)-1;
735 ber_dupbv( &nv, bv );
737 /* first get the time field */
738 for ( i = 0; (i < nv.bv_len) && (nv.bv_val[i] != '#'); i++ )
740 if ( i == nv.bv_len ) {
741 goto exit_failure; /* couldn't locate the '#' separator */
743 nv.bv_val[i++] = '\0'; /* terminate the string & move to next field */
745 *oldtime = parse_time( ptr );
746 if (*oldtime == (time_t)-1) {
750 /* get the OID field */
751 for (ptr = &(nv.bv_val[i]); (i < nv.bv_len) && (nv.bv_val[i] != '#'); i++ )
753 if ( i == nv.bv_len ) {
754 goto exit_failure; /* couldn't locate the '#' separator */
756 nv.bv_val[i++] = '\0'; /* terminate the string & move to next field */
758 *oid = ber_strdup( ptr );
761 /* get the length field */
762 for ( ptr = &(nv.bv_val[i]); (i < nv.bv_len) && (nv.bv_val[i] != '#'); i++ )
764 if ( i == nv.bv_len ) {
765 goto exit_failure; /* couldn't locate the '#' separator */
767 nv.bv_val[i++] = '\0'; /* terminate the string & move to next field */
768 oldpw->bv_len = strtol( ptr, NULL, 10 );
769 if (errno == ERANGE) {
773 /* lastly, get the octets of the string */
774 for ( j = i, ptr = &(nv.bv_val[i]); i < nv.bv_len; i++ )
776 if ( i - j != oldpw->bv_len) {
777 goto exit_failure; /* length is wrong */
781 npw.bv_len = oldpw->bv_len;
782 ber_dupbv( oldpw, &npw );
783 ber_memfree( nv.bv_val );
792 if ( oldpw->bv_val ) {
793 ber_memfree( oldpw->bv_val);
796 ber_memfree( nv.bv_val );
802 add_to_pwd_history( pw_hist **l, time_t t,
803 struct berval *oldpw, struct berval *bv )
805 pw_hist *p, *p1, *p2;
809 p = ch_malloc( sizeof( pw_hist ));
811 ber_dupbv( &p->bv, bv );
816 /* degenerate case */
821 * advance p1 and p2 such that p1 is the node before the
822 * new one, and p2 is the node after it
824 for (p1 = NULL, p2 = *l; p2 && p2->t <= t; p1 = p2, p2=p2->next );
826 if (p1 == NULL) { *l = p; return; }
830 #ifndef MAX_PWD_HISTORY_SZ
831 #define MAX_PWD_HISTORY_SZ 1024
832 #endif /* MAX_PWD_HISTORY_SZ */
835 make_pwd_history_value( char *timebuf, struct berval *bv, Attribute *pa )
837 char str[ MAX_PWD_HISTORY_SZ ];
840 snprintf( str, MAX_PWD_HISTORY_SZ,
841 "%s#%s#%lu#", timebuf,
842 pa->a_desc->ad_type->sat_syntax->ssyn_oid,
843 (unsigned long) pa->a_nvals[0].bv_len );
844 str[MAX_PWD_HISTORY_SZ-1] = 0;
848 * We have to assume that the string is a string of octets,
849 * not readable characters. In reality, yes, it probably is
850 * a readable (ie, base64) string, but we can't count on that
851 * Hence, while the first 3 fields of the password history
852 * are definitely readable (a timestamp, an OID and an integer
853 * length), the remaining octets of the actual password
854 * are deemed to be binary data.
856 AC_MEMCPY( str + nlen, pa->a_nvals[0].bv_val, pa->a_nvals[0].bv_len );
857 nlen += pa->a_nvals[0].bv_len;
858 bv->bv_val = ch_malloc( nlen + 1 );
859 AC_MEMCPY( bv->bv_val, str, nlen );
860 bv->bv_val[nlen] = '\0';
865 free_pwd_history_list( pw_hist **l )
872 pw_hist *pp = p->next;
882 typedef struct ppbind {
886 LDAPControl **oldctrls;
888 LDAPPasswordPolicyError pErr;
893 ctrls_cleanup( Operation *op, SlapReply *rs, LDAPControl **oldctrls )
897 assert( rs->sr_ctrls != NULL );
898 assert( rs->sr_ctrls[0] != NULL );
900 for ( n = 0; rs->sr_ctrls[n]; n++ ) {
901 if ( rs->sr_ctrls[n]->ldctl_oid == ppolicy_ctrl_oid ) {
902 op->o_tmpfree( rs->sr_ctrls[n], op->o_tmpmemctx );
903 rs->sr_ctrls[n] = (LDAPControl *)(-1);
908 if ( rs->sr_ctrls[n] == NULL ) {
912 op->o_tmpfree( rs->sr_ctrls, op->o_tmpmemctx );
914 rs->sr_ctrls = oldctrls;
918 ppolicy_ctrls_cleanup( Operation *op, SlapReply *rs )
920 ppbind *ppb = op->o_callback->sc_private;
921 if ( ppb->send_ctrl ) {
922 ctrls_cleanup( op, rs, ppb->oldctrls );
924 return SLAP_CB_CONTINUE;
928 ppolicy_bind_response( Operation *op, SlapReply *rs )
930 ppbind *ppb = op->o_callback->sc_private;
931 slap_overinst *on = ppb->on;
932 Modifications *mod = ppb->mod, *m;
934 int ngut = -1, warn = -1, age, rc;
936 time_t now, pwtime = (time_t)-1;
937 struct lutil_tm now_tm;
938 struct lutil_timet now_usec;
939 char nowstr[ LDAP_LUTIL_GENTIME_BUFSIZE ];
940 char nowstr_usec[ LDAP_LUTIL_GENTIME_BUFSIZE+8 ];
941 struct berval timestamp, timestamp_usec;
942 BackendInfo *bi = op->o_bd->bd_info;
945 /* If we already know it's locked, just get on with it */
946 if ( ppb->pErr != PP_noError ) {
950 op->o_bd->bd_info = (BackendInfo *)on->on_info;
951 rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &e );
952 op->o_bd->bd_info = bi;
954 if ( rc != LDAP_SUCCESS ) {
955 return SLAP_CB_CONTINUE;
958 ldap_pvt_gettime(&now_tm); /* stored for later consideration */
959 lutil_tm2time(&now_tm, &now_usec);
960 now = now_usec.tt_sec;
961 timestamp.bv_val = nowstr;
962 timestamp.bv_len = sizeof(nowstr);
963 slap_timestamp( &now, ×tamp );
965 /* Separate timestamp for pwdFailureTime with microsecond granularity */
966 strcpy(nowstr_usec, nowstr);
967 timestamp_usec.bv_val = nowstr_usec;
968 timestamp_usec.bv_len = timestamp.bv_len;
969 snprintf( timestamp_usec.bv_val + timestamp_usec.bv_len-1, sizeof(".123456Z"), ".%06dZ", now_usec.tt_usec );
970 timestamp_usec.bv_len += STRLENOF(".123456");
972 if ( rs->sr_err == LDAP_INVALID_CREDENTIALS ) {
975 m = ch_calloc( sizeof(Modifications), 1 );
976 m->sml_op = LDAP_MOD_ADD;
978 m->sml_type = ad_pwdFailureTime->ad_cname;
979 m->sml_desc = ad_pwdFailureTime;
981 m->sml_values = ch_calloc( sizeof(struct berval), 2 );
982 m->sml_nvalues = ch_calloc( sizeof(struct berval), 2 );
984 ber_dupbv( &m->sml_values[0], ×tamp_usec );
985 ber_dupbv( &m->sml_nvalues[0], ×tamp_usec );
990 * Count the pwdFailureTimes - if it's
991 * greater than the policy pwdMaxFailure,
992 * then lock the account.
994 if ((a = attr_find( e->e_attrs, ad_pwdFailureTime )) != NULL) {
995 for(i=0; a->a_nvals[i].bv_val; i++) {
998 * If the interval is 0, then failures
999 * stay on the record until explicitly
1000 * reset by successful authentication.
1002 if (ppb->pp.pwdFailureCountInterval == 0) {
1005 parse_time(a->a_nvals[i].bv_val) +
1006 ppb->pp.pwdFailureCountInterval) {
1011 * We only count those failures
1012 * which are not due to expire.
1015 /* Do we have too many timestamps? If so, delete some values.
1016 * We don't bother to sort the values here. OpenLDAP keeps the
1017 * values in order by default. Fundamentally, relying on the
1018 * information here is wrong anyway; monitoring systems should
1019 * be tracking Bind failures in syslog, not here.
1021 if (a->a_numvals >= ppb->pp.pwdMaxRecordedFailure) {
1022 int j = ppb->pp.pwdMaxRecordedFailure-1;
1023 /* If more than 2x, cheaper to perform a Replace */
1024 if (a->a_numvals >= 2 * ppb->pp.pwdMaxRecordedFailure) {
1025 struct berval v, nv;
1027 /* Change the mod we constructed above */
1028 m->sml_op = LDAP_MOD_REPLACE;
1029 m->sml_numvals = ppb->pp.pwdMaxRecordedFailure;
1030 v = m->sml_values[0];
1031 nv = m->sml_nvalues[0];
1032 ch_free(m->sml_values);
1033 ch_free(m->sml_nvalues);
1034 m->sml_values = ch_calloc( sizeof(struct berval), 2 );
1035 m->sml_nvalues = ch_calloc( sizeof(struct berval), 2 );
1036 for (i=0; i<j; i++) {
1037 ber_dupbv(&m->sml_values[i], &a->a_vals[a->a_numvals-j+i]);
1038 ber_dupbv(&m->sml_nvalues[i], &a->a_nvals[a->a_numvals-j+i]);
1040 m->sml_values[i] = v;
1041 m->sml_nvalues[i] = nv;
1043 /* else just delete some */
1044 m = ch_calloc( sizeof(Modifications), 1 );
1045 m->sml_op = LDAP_MOD_DELETE;
1046 m->sml_type = ad_pwdFailureTime->ad_cname;
1047 m->sml_desc = ad_pwdFailureTime;
1048 m->sml_numvals = a->a_numvals - j;
1049 m->sml_values = ch_calloc( sizeof(struct berval), m->sml_numvals );
1050 m->sml_nvalues = ch_calloc( sizeof(struct berval), m->sml_numvals );
1051 for (i=0; i<m->sml_numvals; i++) {
1052 ber_dupbv(&m->sml_values[i], &a->a_vals[i]);
1053 ber_dupbv(&m->sml_nvalues[i], &a->a_nvals[i]);
1061 if ((ppb->pp.pwdMaxFailure > 0) &&
1062 (fc >= ppb->pp.pwdMaxFailure - 1)) {
1065 * We subtract 1 from the failure max
1066 * because the new failure entry hasn't
1067 * made it to the entry yet.
1069 m = ch_calloc( sizeof(Modifications), 1 );
1070 m->sml_op = LDAP_MOD_REPLACE;
1072 m->sml_type = ad_pwdAccountLockedTime->ad_cname;
1073 m->sml_desc = ad_pwdAccountLockedTime;
1075 m->sml_values = ch_calloc( sizeof(struct berval), 2 );
1076 m->sml_nvalues = ch_calloc( sizeof(struct berval), 2 );
1077 ber_dupbv( &m->sml_values[0], ×tamp );
1078 ber_dupbv( &m->sml_nvalues[0], ×tamp );
1082 } else if ( rs->sr_err == LDAP_SUCCESS ) {
1083 if ((a = attr_find( e->e_attrs, ad_pwdChangedTime )) != NULL)
1084 pwtime = parse_time( a->a_nvals[0].bv_val );
1086 /* delete all pwdFailureTimes */
1087 if ( attr_find( e->e_attrs, ad_pwdFailureTime )) {
1088 m = ch_calloc( sizeof(Modifications), 1 );
1089 m->sml_op = LDAP_MOD_DELETE;
1091 m->sml_type = ad_pwdFailureTime->ad_cname;
1092 m->sml_desc = ad_pwdFailureTime;
1098 * check to see if the password must be changed
1100 if ( ppb->pp.pwdMustChange &&
1101 (a = attr_find( e->e_attrs, ad_pwdReset )) &&
1102 bvmatch( &a->a_nvals[0], &slap_true_bv ) )
1105 * need to inject client controls here to give
1106 * more information. For the moment, we ensure
1107 * that we are disallowed from doing anything
1108 * other than change password.
1110 if ( ppb->set_restrict ) {
1111 ber_dupbv( &pwcons[op->o_conn->c_conn_idx].dn,
1112 &op->o_conn->c_ndn );
1115 ppb->pErr = PP_changeAfterReset;
1119 * the password does not need to be changed, so
1120 * we now check whether the password has expired.
1122 * We can skip this bit if passwords don't age in
1123 * the policy. Also, if there was no pwdChangedTime
1124 * attribute in the entry, the password never expires.
1126 if (ppb->pp.pwdMaxAge == 0) goto grace;
1128 if (pwtime != (time_t)-1) {
1130 * Check: was the last change time of
1131 * the password older than the maximum age
1132 * allowed. (Ignore case 2 from I-D, it's just silly.)
1134 if (now - pwtime > ppb->pp.pwdMaxAge ) pwExpired = 1;
1139 if (!pwExpired) goto check_expiring_password;
1141 if ((a = attr_find( e->e_attrs, ad_pwdGraceUseTime )) == NULL)
1142 ngut = ppb->pp.pwdGraceAuthNLimit;
1144 for(ngut=0; a->a_nvals[ngut].bv_val; ngut++);
1145 ngut = ppb->pp.pwdGraceAuthNLimit - ngut;
1149 * ngut is the number of remaining grace logins
1151 Debug( LDAP_DEBUG_ANY,
1152 "ppolicy_bind: Entry %s has an expired password: %d grace logins\n",
1153 e->e_name.bv_val, ngut, 0);
1156 ppb->pErr = PP_passwordExpired;
1157 rs->sr_err = LDAP_INVALID_CREDENTIALS;
1162 * Add a grace user time to the entry
1164 m = ch_calloc( sizeof(Modifications), 1 );
1165 m->sml_op = LDAP_MOD_ADD;
1167 m->sml_type = ad_pwdGraceUseTime->ad_cname;
1168 m->sml_desc = ad_pwdGraceUseTime;
1170 m->sml_values = ch_calloc( sizeof(struct berval), 2 );
1171 m->sml_nvalues = ch_calloc( sizeof(struct berval), 2 );
1172 ber_dupbv( &m->sml_values[0], ×tamp );
1173 ber_dupbv( &m->sml_nvalues[0], ×tamp );
1177 check_expiring_password:
1179 * Now we need to check to see
1180 * if it is about to expire, and if so, should the user
1181 * be warned about it in the password policy control.
1183 * If the password has expired, and we're in the grace period, then
1184 * we don't need to do this bit. Similarly, if we don't have password
1185 * aging, then there's no need to do this bit either.
1187 if ((ppb->pp.pwdMaxAge < 1) || (pwExpired) || (ppb->pp.pwdExpireWarning < 1))
1190 age = (int)(now - pwtime);
1193 * We know that there is a password Change Time attribute - if
1194 * there wasn't, then the pwdExpired value would be true, unless
1195 * there is no password aging - and if there is no password aging,
1196 * then this section isn't called anyway - you can't have an
1197 * expiring password if there's no limit to expire.
1199 if (ppb->pp.pwdMaxAge - age < ppb->pp.pwdExpireWarning ) {
1201 * Set the warning value.
1203 warn = ppb->pp.pwdMaxAge - age; /* seconds left until expiry */
1204 if (warn < 0) warn = 0; /* something weird here - why is pwExpired not set? */
1206 Debug( LDAP_DEBUG_ANY,
1207 "ppolicy_bind: Setting warning for password expiry for %s = %d seconds\n",
1208 op->o_req_dn.bv_val, warn, 0 );
1213 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1214 be_entry_release_r( op, e );
1218 Operation op2 = *op;
1219 SlapReply r2 = { REP_RESULT };
1220 slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
1221 pp_info *pi = on->on_bi.bi_private;
1222 LDAPControl c, *ca[2];
1224 op2.o_tag = LDAP_REQ_MODIFY;
1225 op2.o_callback = &cb;
1226 op2.orm_modlist = mod;
1227 op2.orm_no_opattrs = 0;
1228 op2.o_dn = op->o_bd->be_rootdn;
1229 op2.o_ndn = op->o_bd->be_rootndn;
1231 /* If this server is a shadow and forward_updates is true,
1232 * use the frontend to perform this modify. That will trigger
1233 * the update referral, which can then be forwarded by the
1234 * chain overlay. Obviously the updateref and chain overlay
1235 * must be configured appropriately for this to be useful.
1237 if ( SLAP_SHADOW( op->o_bd ) && pi->forward_updates ) {
1238 op2.o_bd = frontendDB;
1240 /* Must use Relax control since these are no-user-mod */
1241 op2.o_relax = SLAP_CONTROL_CRITICAL;
1245 BER_BVZERO( &c.ldctl_value );
1246 c.ldctl_iscritical = 1;
1247 c.ldctl_oid = LDAP_CONTROL_RELAX;
1249 /* If not forwarding, don't update opattrs and don't replicate */
1250 if ( SLAP_SINGLE_SHADOW( op->o_bd )) {
1251 op2.orm_no_opattrs = 1;
1252 op2.o_dont_replicate = 1;
1254 op2.o_bd->bd_info = (BackendInfo *)on->on_info;
1256 rc = op2.o_bd->be_modify( &op2, &r2 );
1257 slap_mods_free( mod, 1 );
1260 if ( ppb->send_ctrl ) {
1261 LDAPControl *ctrl = NULL;
1262 pp_info *pi = on->on_bi.bi_private;
1264 /* Do we really want to tell that the account is locked? */
1265 if ( ppb->pErr == PP_accountLocked && !pi->use_lockout ) {
1266 ppb->pErr = PP_noError;
1268 ctrl = create_passcontrol( op, warn, ngut, ppb->pErr );
1269 ppb->oldctrls = add_passcontrol( op, rs, ctrl );
1270 op->o_callback->sc_cleanup = ppolicy_ctrls_cleanup;
1272 op->o_bd->bd_info = bi;
1273 return SLAP_CB_CONTINUE;
1277 ppolicy_bind( Operation *op, SlapReply *rs )
1279 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1281 /* Reset lockout status on all Bind requests */
1282 if ( !BER_BVISEMPTY( &pwcons[op->o_conn->c_conn_idx].dn )) {
1283 ch_free( pwcons[op->o_conn->c_conn_idx].dn.bv_val );
1284 BER_BVZERO( &pwcons[op->o_conn->c_conn_idx].dn );
1287 /* Root bypasses policy */
1288 if ( !be_isroot_dn( op->o_bd, &op->o_req_ndn )) {
1294 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1295 rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &e );
1297 if ( rc != LDAP_SUCCESS ) {
1298 return SLAP_CB_CONTINUE;
1301 cb = op->o_tmpcalloc( sizeof(ppbind)+sizeof(slap_callback),
1302 1, op->o_tmpmemctx );
1303 ppb = (ppbind *)(cb+1);
1305 ppb->pErr = PP_noError;
1306 ppb->set_restrict = 1;
1308 /* Setup a callback so we can munge the result */
1310 cb->sc_response = ppolicy_bind_response;
1311 cb->sc_next = op->o_callback->sc_next;
1312 cb->sc_private = ppb;
1313 op->o_callback->sc_next = cb;
1315 /* Did we receive a password policy request control? */
1316 if ( op->o_ctrlflag[ppolicy_cid] ) {
1320 op->o_bd->bd_info = (BackendInfo *)on;
1321 ppolicy_get( op, e, &ppb->pp );
1323 rc = account_locked( op, e, &ppb->pp, &ppb->mod );
1325 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1326 be_entry_release_r( op, e );
1329 ppb->pErr = PP_accountLocked;
1330 send_ldap_error( op, rs, LDAP_INVALID_CREDENTIALS, NULL );
1336 return SLAP_CB_CONTINUE;
1339 /* Reset the restricted info for the next session on this connection */
1341 ppolicy_connection_destroy( BackendDB *bd, Connection *conn )
1343 if ( pwcons && !BER_BVISEMPTY( &pwcons[conn->c_conn_idx].dn )) {
1344 ch_free( pwcons[conn->c_conn_idx].dn.bv_val );
1345 BER_BVZERO( &pwcons[conn->c_conn_idx].dn );
1347 return SLAP_CB_CONTINUE;
1350 /* Check if this connection is restricted */
1356 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1359 /* Did we receive a password policy request control? */
1360 if ( op->o_ctrlflag[ppolicy_cid] ) {
1364 if ( op->o_conn && !BER_BVISEMPTY( &pwcons[op->o_conn->c_conn_idx].dn )) {
1365 LDAPControl **oldctrls;
1366 /* if the current authcDN doesn't match the one we recorded,
1367 * then an intervening Bind has succeeded and the restriction
1368 * no longer applies. (ITS#4516)
1370 if ( !dn_match( &op->o_conn->c_ndn,
1371 &pwcons[op->o_conn->c_conn_idx].dn )) {
1372 ch_free( pwcons[op->o_conn->c_conn_idx].dn.bv_val );
1373 BER_BVZERO( &pwcons[op->o_conn->c_conn_idx].dn );
1374 return SLAP_CB_CONTINUE;
1377 Debug( LDAP_DEBUG_TRACE,
1378 "connection restricted to password changing only\n", 0, 0, 0);
1380 LDAPControl *ctrl = NULL;
1381 ctrl = create_passcontrol( op, -1, -1, PP_changeAfterReset );
1382 oldctrls = add_passcontrol( op, rs, ctrl );
1384 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1385 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
1386 "Operations are restricted to bind/unbind/abandon/StartTLS/modify password" );
1388 ctrls_cleanup( op, rs, oldctrls );
1393 return SLAP_CB_CONTINUE;
1397 ppolicy_compare_response(
1401 /* map compare responses to bind responses */
1402 if ( rs->sr_err == LDAP_COMPARE_TRUE )
1403 rs->sr_err = LDAP_SUCCESS;
1404 else if ( rs->sr_err == LDAP_COMPARE_FALSE )
1405 rs->sr_err = LDAP_INVALID_CREDENTIALS;
1407 ppolicy_bind_response( op, rs );
1409 /* map back to compare */
1410 if ( rs->sr_err == LDAP_SUCCESS )
1411 rs->sr_err = LDAP_COMPARE_TRUE;
1412 else if ( rs->sr_err == LDAP_INVALID_CREDENTIALS )
1413 rs->sr_err = LDAP_COMPARE_FALSE;
1415 return SLAP_CB_CONTINUE;
1423 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1425 if ( ppolicy_restrict( op, rs ) != SLAP_CB_CONTINUE )
1428 /* Did we receive a password policy request control?
1429 * Are we testing the userPassword?
1431 if ( op->o_ctrlflag[ppolicy_cid] &&
1432 op->orc_ava->aa_desc == slap_schema.si_ad_userPassword ) {
1438 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1439 rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &e );
1441 if ( rc != LDAP_SUCCESS ) {
1442 return SLAP_CB_CONTINUE;
1445 cb = op->o_tmpcalloc( sizeof(ppbind)+sizeof(slap_callback),
1446 1, op->o_tmpmemctx );
1447 ppb = (ppbind *)(cb+1);
1449 ppb->pErr = PP_noError;
1451 /* failures here don't lockout the connection */
1452 ppb->set_restrict = 0;
1454 /* Setup a callback so we can munge the result */
1456 cb->sc_response = ppolicy_compare_response;
1457 cb->sc_next = op->o_callback->sc_next;
1458 cb->sc_private = ppb;
1459 op->o_callback->sc_next = cb;
1461 op->o_bd->bd_info = (BackendInfo *)on;
1462 ppolicy_get( op, e, &ppb->pp );
1464 rc = account_locked( op, e, &ppb->pp, &ppb->mod );
1466 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1467 be_entry_release_r( op, e );
1470 ppb->pErr = PP_accountLocked;
1471 send_ldap_error( op, rs, LDAP_COMPARE_FALSE, NULL );
1475 return SLAP_CB_CONTINUE;
1483 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1484 pp_info *pi = on->on_bi.bi_private;
1489 if ( ppolicy_restrict( op, rs ) != SLAP_CB_CONTINUE )
1492 /* If this is a replica, assume the master checked everything */
1493 if ( be_shadow_update( op ))
1494 return SLAP_CB_CONTINUE;
1496 /* Check for password in entry */
1497 if ((pa = attr_find( op->oq_add.rs_e->e_attrs,
1498 slap_schema.si_ad_userPassword )))
1500 assert( pa->a_vals != NULL );
1501 assert( !BER_BVISNULL( &pa->a_vals[ 0 ] ) );
1503 if ( !BER_BVISNULL( &pa->a_vals[ 1 ] ) ) {
1504 send_ldap_error( op, rs, LDAP_CONSTRAINT_VIOLATION, "Password policy only allows one password value" );
1509 * new entry contains a password - if we're not the root user
1510 * then we need to check that the password fits in with the
1511 * security policy for the new entry.
1513 ppolicy_get( op, op->ora_e, &pp );
1514 if (pp.pwdCheckQuality > 0 && !be_isroot( op )) {
1515 struct berval *bv = &(pa->a_vals[0]);
1516 int rc, send_ctrl = 0;
1517 LDAPPasswordPolicyError pErr = PP_noError;
1520 /* Did we receive a password policy request control? */
1521 if ( op->o_ctrlflag[ppolicy_cid] ) {
1524 rc = check_password_quality( bv, &pp, &pErr, op->ora_e, &txt );
1525 if (rc != LDAP_SUCCESS) {
1526 LDAPControl **oldctrls = NULL;
1527 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1529 LDAPControl *ctrl = NULL;
1530 ctrl = create_passcontrol( op, -1, -1, pErr );
1531 oldctrls = add_passcontrol( op, rs, ctrl );
1533 send_ldap_error( op, rs, rc, txt ? txt : "Password fails quality checking policy" );
1538 ctrls_cleanup( op, rs, oldctrls );
1544 * A controversial bit. We hash cleartext
1545 * passwords provided via add and modify operations
1546 * You're not really supposed to do this, since
1547 * the X.500 model says "store attributes" as they
1548 * get provided. By default, this is what we do
1550 * But if the hash_passwords flag is set, we hash
1551 * any cleartext password attribute values via the
1552 * default password hashing scheme.
1554 if ((pi->hash_passwords) &&
1555 (password_scheme( &(pa->a_vals[0]), NULL ) != LDAP_SUCCESS)) {
1558 slap_passwd_hash( &(pa->a_vals[0]), &hpw, &txt );
1559 if (hpw.bv_val == NULL) {
1561 * hashing didn't work. Emit an error.
1563 rs->sr_err = LDAP_OTHER;
1565 send_ldap_error( op, rs, LDAP_OTHER, "Password hashing failed" );
1569 memset( pa->a_vals[0].bv_val, 0, pa->a_vals[0].bv_len);
1570 ber_memfree( pa->a_vals[0].bv_val );
1571 pa->a_vals[0].bv_val = hpw.bv_val;
1572 pa->a_vals[0].bv_len = hpw.bv_len;
1575 /* If password aging is in effect, set the pwdChangedTime */
1576 if ( pp.pwdMaxAge || pp.pwdMinAge ) {
1577 struct berval timestamp;
1578 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
1579 time_t now = slap_get_time();
1581 timestamp.bv_val = timebuf;
1582 timestamp.bv_len = sizeof(timebuf);
1583 slap_timestamp( &now, ×tamp );
1585 attr_merge_one( op->ora_e, ad_pwdChangedTime, ×tamp, ×tamp );
1588 return SLAP_CB_CONTINUE;
1592 ppolicy_mod_cb( Operation *op, SlapReply *rs )
1594 slap_callback *sc = op->o_callback;
1595 op->o_callback = sc->sc_next;
1596 if ( rs->sr_err == LDAP_SUCCESS ) {
1597 ch_free( pwcons[op->o_conn->c_conn_idx].dn.bv_val );
1598 BER_BVZERO( &pwcons[op->o_conn->c_conn_idx].dn );
1600 op->o_tmpfree( sc, op->o_tmpmemctx );
1601 return SLAP_CB_CONTINUE;
1605 ppolicy_modify( Operation *op, SlapReply *rs )
1607 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1608 pp_info *pi = on->on_bi.bi_private;
1609 int i, rc, mod_pw_only, pwmod, pwmop = -1, deladd,
1612 Modifications *mods = NULL, *modtail = NULL,
1613 *ml, *delmod, *addmod;
1614 Attribute *pa, *ha, at;
1616 pw_hist *tl = NULL, *p;
1617 int zapReset, send_ctrl = 0, free_txt = 0;
1619 struct berval newpw = BER_BVNULL, oldpw = BER_BVNULL,
1621 LDAPPasswordPolicyError pErr = PP_noError;
1622 LDAPControl *ctrl = NULL;
1623 LDAPControl **oldctrls = NULL;
1625 int got_del_grace = 0, got_del_lock = 0, got_pw = 0, got_del_fail = 0;
1626 int got_changed = 0, got_history = 0;
1628 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1629 rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &e );
1630 op->o_bd->bd_info = (BackendInfo *)on;
1632 if ( rc != LDAP_SUCCESS ) return SLAP_CB_CONTINUE;
1634 /* If this is a replica, we may need to tweak some of the
1635 * master's modifications. Otherwise, just pass it through.
1637 if ( be_shadow_update( op )) {
1638 Modifications **prev;
1639 Attribute *a_grace, *a_lock, *a_fail;
1641 a_grace = attr_find( e->e_attrs, ad_pwdGraceUseTime );
1642 a_lock = attr_find( e->e_attrs, ad_pwdAccountLockedTime );
1643 a_fail = attr_find( e->e_attrs, ad_pwdFailureTime );
1645 for( prev = &op->orm_modlist, ml = *prev; ml; ml = *prev ) {
1647 if ( ml->sml_desc == slap_schema.si_ad_userPassword )
1650 /* If we're deleting an attr that didn't exist,
1651 * drop this delete op
1653 if ( ml->sml_op == LDAP_MOD_DELETE ) {
1656 if ( ml->sml_desc == ad_pwdGraceUseTime ) {
1657 if ( !a_grace || got_del_grace ) {
1663 if ( ml->sml_desc == ad_pwdAccountLockedTime ) {
1664 if ( !a_lock || got_del_lock ) {
1670 if ( ml->sml_desc == ad_pwdFailureTime ) {
1671 if ( !a_fail || got_del_fail ) {
1678 *prev = ml->sml_next;
1679 ml->sml_next = NULL;
1680 slap_mods_free( ml, 1 );
1684 prev = &ml->sml_next;
1687 /* If we're resetting the password, make sure grace, accountlock,
1688 * and failure also get removed.
1691 if ( a_grace && !got_del_grace ) {
1692 ml = (Modifications *) ch_malloc( sizeof( Modifications ) );
1693 ml->sml_op = LDAP_MOD_DELETE;
1694 ml->sml_flags = SLAP_MOD_INTERNAL;
1695 ml->sml_type.bv_val = NULL;
1696 ml->sml_desc = ad_pwdGraceUseTime;
1697 ml->sml_numvals = 0;
1698 ml->sml_values = NULL;
1699 ml->sml_nvalues = NULL;
1700 ml->sml_next = NULL;
1702 prev = &ml->sml_next;
1704 if ( a_lock && !got_del_lock ) {
1705 ml = (Modifications *) ch_malloc( sizeof( Modifications ) );
1706 ml->sml_op = LDAP_MOD_DELETE;
1707 ml->sml_flags = SLAP_MOD_INTERNAL;
1708 ml->sml_type.bv_val = NULL;
1709 ml->sml_desc = ad_pwdAccountLockedTime;
1710 ml->sml_numvals = 0;
1711 ml->sml_values = NULL;
1712 ml->sml_nvalues = NULL;
1713 ml->sml_next = NULL;
1716 if ( a_fail && !got_del_fail ) {
1717 ml = (Modifications *) ch_malloc( sizeof( Modifications ) );
1718 ml->sml_op = LDAP_MOD_DELETE;
1719 ml->sml_flags = SLAP_MOD_INTERNAL;
1720 ml->sml_type.bv_val = NULL;
1721 ml->sml_desc = ad_pwdFailureTime;
1722 ml->sml_numvals = 0;
1723 ml->sml_values = NULL;
1724 ml->sml_nvalues = NULL;
1725 ml->sml_next = NULL;
1729 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1730 be_entry_release_r( op, e );
1731 return SLAP_CB_CONTINUE;
1734 /* Did we receive a password policy request control? */
1735 if ( op->o_ctrlflag[ppolicy_cid] ) {
1739 /* See if this is a pwdModify exop. If so, we can
1740 * access the plaintext passwords from that request.
1745 for ( sc = op->o_callback; sc; sc=sc->sc_next ) {
1746 if ( sc->sc_response == slap_null_cb &&
1748 req_pwdexop_s *qpw = sc->sc_private;
1749 newpw = qpw->rs_new;
1750 oldpw = qpw->rs_old;
1757 ppolicy_get( op, e, &pp );
1759 for ( ml = op->orm_modlist,
1760 pwmod = 0, mod_pw_only = 1,
1761 deladd = 0, delmod = NULL,
1764 ml != NULL; modtail = ml, ml = ml->sml_next )
1766 if ( ml->sml_desc == pp.ad ) {
1769 if ((deladd == 0) && (ml->sml_op == LDAP_MOD_DELETE) &&
1770 (ml->sml_values) && !BER_BVISNULL( &ml->sml_values[0] ))
1776 if ((ml->sml_op == LDAP_MOD_ADD) ||
1777 (ml->sml_op == LDAP_MOD_REPLACE))
1779 if ( ml->sml_values && !BER_BVISNULL( &ml->sml_values[0] )) {
1783 /* FIXME: there's no easy way to ensure
1784 * that add does not cause multiple
1785 * userPassword values; one way (that
1786 * would be consistent with the single
1787 * password constraint) would be to turn
1788 * add into replace); another would be
1791 * Let's check at least that a single value
1794 if ( addmod || !BER_BVISNULL( &ml->sml_values[ 1 ] ) ) {
1795 rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
1796 rs->sr_text = "Password policy only allows one password value";
1797 goto return_results;
1802 /* replace can have no values, add cannot */
1803 assert( ml->sml_op == LDAP_MOD_REPLACE );
1807 } else if ( !(ml->sml_flags & SLAP_MOD_INTERNAL) && !is_at_operational( ml->sml_desc->ad_type ) ) {
1809 /* modifying something other than password */
1813 * If there is a request to explicitly add a pwdReset
1814 * attribute, then we suppress the normal behaviour on
1815 * password change, which is to remove the pwdReset
1818 * This enables an administrator to assign a new password
1819 * and place a "must reset" flag on the entry, which will
1820 * stay until the user explicitly changes his/her password.
1822 if (ml->sml_desc == ad_pwdReset ) {
1823 if ((ml->sml_op == LDAP_MOD_ADD) ||
1824 (ml->sml_op == LDAP_MOD_REPLACE))
1827 if ( ml->sml_op == LDAP_MOD_DELETE ) {
1828 if ( ml->sml_desc == ad_pwdGraceUseTime ) {
1830 } else if ( ml->sml_desc == ad_pwdAccountLockedTime ) {
1832 } else if ( ml->sml_desc == ad_pwdFailureTime ) {
1836 if ( ml->sml_desc == ad_pwdChangedTime ) {
1838 } else if (ml->sml_desc == ad_pwdHistory ) {
1843 if (!BER_BVISEMPTY( &pwcons[op->o_conn->c_conn_idx].dn ) && !mod_pw_only ) {
1844 if ( dn_match( &op->o_conn->c_ndn,
1845 &pwcons[op->o_conn->c_conn_idx].dn )) {
1846 Debug( LDAP_DEBUG_TRACE,
1847 "connection restricted to password changing only\n", 0, 0, 0 );
1848 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
1849 rs->sr_text = "Operations are restricted to bind/unbind/abandon/StartTLS/modify password";
1850 pErr = PP_changeAfterReset;
1851 goto return_results;
1853 ch_free( pwcons[op->o_conn->c_conn_idx].dn.bv_val );
1854 BER_BVZERO( &pwcons[op->o_conn->c_conn_idx].dn );
1859 * if we have a "safe password modify policy", then we need to check if we're doing
1860 * a delete (with the old password), followed by an add (with the new password).
1862 * If we got just a delete with nothing else, just let it go. We also skip all the checks if
1863 * the root user is bound. Root can do anything, including avoid the policies.
1866 if (!pwmod) goto do_modify;
1869 * Build the password history list in ascending time order
1870 * We need this, even if the user is root, in order to maintain
1871 * the pwdHistory operational attributes properly.
1873 if (addmod && pp.pwdInHistory > 0 && (ha = attr_find( e->e_attrs, ad_pwdHistory ))) {
1874 struct berval oldpw;
1877 for(i=0; ha->a_nvals[i].bv_val; i++) {
1878 rc = parse_pwdhistory( &(ha->a_nvals[i]), NULL,
1881 if (rc != LDAP_SUCCESS) continue; /* invalid history entry */
1884 add_to_pwd_history( &tl, oldtime, &oldpw,
1885 &(ha->a_nvals[i]) );
1886 oldpw.bv_val = NULL;
1890 for(p=tl; p; p=p->next, hsize++); /* count history size */
1893 if (be_isroot( op )) goto do_modify;
1895 /* NOTE: according to draft-behera-ldap-password-policy
1896 * pwdAllowUserChange == FALSE must only prevent pwd changes
1897 * by the user the pwd belongs to (ITS#7021) */
1898 if (!pp.pwdAllowUserChange && dn_match(&op->o_req_ndn, &op->o_ndn)) {
1899 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
1900 rs->sr_text = "User alteration of password is not allowed";
1901 pErr = PP_passwordModNotAllowed;
1902 goto return_results;
1905 /* Just deleting? */
1907 /* skip everything else */
1912 /* This is a pwdModify exop that provided the old pw.
1913 * We need to create a Delete mod for this old pw and
1914 * let the matching value get found later
1916 if (pp.pwdSafeModify && oldpw.bv_val ) {
1917 ml = (Modifications *)ch_calloc( sizeof( Modifications ), 1 );
1918 ml->sml_op = LDAP_MOD_DELETE;
1919 ml->sml_flags = SLAP_MOD_INTERNAL;
1920 ml->sml_desc = pp.ad;
1921 ml->sml_type = pp.ad->ad_cname;
1922 ml->sml_numvals = 1;
1923 ml->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1924 ber_dupbv( &ml->sml_values[0], &oldpw );
1925 BER_BVZERO( &ml->sml_values[1] );
1926 ml->sml_next = op->orm_modlist;
1927 op->orm_modlist = ml;
1932 if (pp.pwdSafeModify && deladd != 2) {
1933 Debug( LDAP_DEBUG_TRACE,
1934 "change password must use DELETE followed by ADD/REPLACE\n",
1936 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
1937 rs->sr_text = "Must supply old password to be changed as well as new one";
1938 pErr = PP_mustSupplyOldPassword;
1939 goto return_results;
1942 /* Check age, but only if pwdReset is not TRUE */
1943 pa = attr_find( e->e_attrs, ad_pwdReset );
1944 if ((!pa || !bvmatch( &pa->a_nvals[0], &slap_true_bv )) &&
1946 time_t pwtime = (time_t)-1, now;
1949 if ((pa = attr_find( e->e_attrs, ad_pwdChangedTime )) != NULL)
1950 pwtime = parse_time( pa->a_nvals[0].bv_val );
1951 now = slap_get_time();
1952 age = (int)(now - pwtime);
1953 if ((pwtime != (time_t)-1) && (age < pp.pwdMinAge)) {
1954 rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
1955 rs->sr_text = "Password is too young to change";
1956 pErr = PP_passwordTooYoung;
1957 goto return_results;
1961 /* pa is used in password history check below, be sure it's set */
1962 if ((pa = attr_find( e->e_attrs, pp.ad )) != NULL && delmod) {
1964 * we have a password to check
1966 bv = oldpw.bv_val ? &oldpw : delmod->sml_values;
1967 /* FIXME: no access checking? */
1968 rc = slap_passwd_check( op, NULL, pa, bv, &txt );
1969 if (rc != LDAP_SUCCESS) {
1970 Debug( LDAP_DEBUG_TRACE,
1971 "old password check failed: %s\n", txt, 0, 0 );
1973 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1974 rs->sr_text = "Must supply correct old password to change to new one";
1975 pErr = PP_mustSupplyOldPassword;
1976 goto return_results;
1982 * replace the delete value with the (possibly hashed)
1983 * value which is currently in the password.
1985 for ( i = 0; !BER_BVISNULL( &delmod->sml_values[i] ); i++ ) {
1986 free( delmod->sml_values[i].bv_val );
1987 BER_BVZERO( &delmod->sml_values[i] );
1989 free( delmod->sml_values );
1990 delmod->sml_values = ch_calloc( sizeof(struct berval), 2 );
1991 BER_BVZERO( &delmod->sml_values[1] );
1992 ber_dupbv( &(delmod->sml_values[0]), &(pa->a_nvals[0]) );
1996 bv = newpw.bv_val ? &newpw : &addmod->sml_values[0];
1997 if (pp.pwdCheckQuality > 0) {
1999 rc = check_password_quality( bv, &pp, &pErr, e, (char **)&txt );
2000 if (rc != LDAP_SUCCESS) {
2006 rs->sr_text = "Password fails quality checking policy";
2008 goto return_results;
2012 /* If pwdInHistory is zero, passwords may be reused */
2013 if (pa && pp.pwdInHistory > 0) {
2015 * Last check - the password history.
2017 /* FIXME: no access checking? */
2018 if (slap_passwd_check( op, NULL, pa, bv, &txt ) == LDAP_SUCCESS) {
2020 * This is bad - it means that the user is attempting
2021 * to set the password to the same as the old one.
2023 rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
2024 rs->sr_text = "Password is not being changed from existing value";
2025 pErr = PP_passwordInHistory;
2026 goto return_results;
2030 * Iterate through the password history, and fail on any
2035 cr[1].bv_val = NULL;
2036 for(p=tl; p; p=p->next) {
2038 /* FIXME: no access checking? */
2039 rc = slap_passwd_check( op, NULL, &at, bv, &txt );
2041 if (rc != LDAP_SUCCESS) continue;
2043 rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
2044 rs->sr_text = "Password is in history of old passwords";
2045 pErr = PP_passwordInHistory;
2046 goto return_results;
2052 struct berval timestamp;
2053 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
2054 time_t now = slap_get_time();
2056 /* If the conn is restricted, set a callback to clear it
2057 * if the pwmod succeeds
2059 if (!BER_BVISEMPTY( &pwcons[op->o_conn->c_conn_idx].dn )) {
2060 slap_callback *sc = op->o_tmpcalloc( 1, sizeof( slap_callback ),
2062 sc->sc_next = op->o_callback;
2063 /* Must use sc_response to insure we reset on success, before
2064 * the client sees the response. Must use sc_cleanup to insure
2065 * that it gets cleaned up if sc_response is not called.
2067 sc->sc_response = ppolicy_mod_cb;
2068 sc->sc_cleanup = ppolicy_mod_cb;
2069 op->o_callback = sc;
2073 * keep the necessary pwd.. operational attributes
2078 timestamp.bv_val = timebuf;
2079 timestamp.bv_len = sizeof(timebuf);
2080 slap_timestamp( &now, ×tamp );
2083 if (pwmop != LDAP_MOD_DELETE) {
2084 mods = (Modifications *) ch_calloc( sizeof( Modifications ), 1 );
2085 mods->sml_op = LDAP_MOD_REPLACE;
2086 mods->sml_numvals = 1;
2087 mods->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
2088 ber_dupbv( &mods->sml_values[0], ×tamp );
2089 BER_BVZERO( &mods->sml_values[1] );
2090 assert( !BER_BVISNULL( &mods->sml_values[0] ) );
2091 } else if (attr_find(e->e_attrs, ad_pwdChangedTime )) {
2092 mods = (Modifications *) ch_calloc( sizeof( Modifications ), 1 );
2093 mods->sml_op = LDAP_MOD_DELETE;
2096 mods->sml_desc = ad_pwdChangedTime;
2097 mods->sml_flags = SLAP_MOD_INTERNAL;
2098 mods->sml_next = NULL;
2099 modtail->sml_next = mods;
2104 if (!got_del_grace && attr_find(e->e_attrs, ad_pwdGraceUseTime )) {
2105 mods = (Modifications *) ch_calloc( sizeof( Modifications ), 1 );
2106 mods->sml_op = LDAP_MOD_DELETE;
2107 mods->sml_desc = ad_pwdGraceUseTime;
2108 mods->sml_flags = SLAP_MOD_INTERNAL;
2109 mods->sml_next = NULL;
2110 modtail->sml_next = mods;
2114 if (!got_del_lock && attr_find(e->e_attrs, ad_pwdAccountLockedTime )) {
2115 mods = (Modifications *) ch_calloc( sizeof( Modifications ), 1 );
2116 mods->sml_op = LDAP_MOD_DELETE;
2117 mods->sml_desc = ad_pwdAccountLockedTime;
2118 mods->sml_flags = SLAP_MOD_INTERNAL;
2119 mods->sml_next = NULL;
2120 modtail->sml_next = mods;
2124 if (!got_del_fail && attr_find(e->e_attrs, ad_pwdFailureTime )) {
2125 mods = (Modifications *) ch_calloc( sizeof( Modifications ), 1 );
2126 mods->sml_op = LDAP_MOD_DELETE;
2127 mods->sml_desc = ad_pwdFailureTime;
2128 mods->sml_flags = SLAP_MOD_INTERNAL;
2129 mods->sml_next = NULL;
2130 modtail->sml_next = mods;
2134 /* Delete the pwdReset attribute, since it's being reset */
2135 if ((zapReset) && (attr_find(e->e_attrs, ad_pwdReset ))) {
2136 mods = (Modifications *) ch_calloc( sizeof( Modifications ), 1 );
2137 mods->sml_op = LDAP_MOD_DELETE;
2138 mods->sml_desc = ad_pwdReset;
2139 mods->sml_flags = SLAP_MOD_INTERNAL;
2140 mods->sml_next = NULL;
2141 modtail->sml_next = mods;
2145 if (!got_history && pp.pwdInHistory > 0) {
2146 if (hsize >= pp.pwdInHistory) {
2148 * We use the >= operator, since we are going to add
2149 * the existing password attribute value into the
2150 * history - thus the cardinality of history values is
2151 * about to rise by one.
2153 * If this would push it over the limit of history
2154 * values (remembering - the password policy could have
2155 * changed since the password was last altered), we must
2156 * delete at least 1 value from the pwdHistory list.
2158 * In fact, we delete '(#pwdHistory attrs - max pwd
2159 * history length) + 1' values, starting with the oldest.
2160 * This is easily evaluated, since the linked list is
2161 * created in ascending time order.
2163 mods = (Modifications *) ch_calloc( sizeof( Modifications ), 1 );
2164 mods->sml_op = LDAP_MOD_DELETE;
2165 mods->sml_flags = SLAP_MOD_INTERNAL;
2166 mods->sml_desc = ad_pwdHistory;
2167 mods->sml_numvals = hsize - pp.pwdInHistory + 1;
2168 mods->sml_values = ch_calloc( sizeof( struct berval ),
2169 hsize - pp.pwdInHistory + 2 );
2170 BER_BVZERO( &mods->sml_values[ hsize - pp.pwdInHistory + 1 ] );
2171 for(i=0,p=tl; i < (hsize - pp.pwdInHistory + 1); i++, p=p->next) {
2172 BER_BVZERO( &mods->sml_values[i] );
2173 ber_dupbv( &(mods->sml_values[i]), &p->bv );
2175 mods->sml_next = NULL;
2176 modtail->sml_next = mods;
2179 free_pwd_history_list( &tl );
2182 * Now add the existing password into the history list.
2183 * This will be executed even if the operation is to delete
2184 * the password entirely.
2186 * This isn't in the spec explicitly, but it seems to make
2187 * sense that the password history list is the list of all
2188 * previous passwords - even if they were deleted. Thus, if
2189 * someone tries to add a historical password at some future
2190 * point, it will fail.
2192 if ((pa = attr_find( e->e_attrs, pp.ad )) != NULL) {
2193 mods = (Modifications *) ch_malloc( sizeof( Modifications ) );
2194 mods->sml_op = LDAP_MOD_ADD;
2195 mods->sml_flags = SLAP_MOD_INTERNAL;
2196 mods->sml_type.bv_val = NULL;
2197 mods->sml_desc = ad_pwdHistory;
2198 mods->sml_nvalues = NULL;
2199 mods->sml_numvals = 1;
2200 mods->sml_values = ch_calloc( sizeof( struct berval ), 2 );
2201 mods->sml_values[ 1 ].bv_val = NULL;
2202 mods->sml_values[ 1 ].bv_len = 0;
2203 make_pwd_history_value( timebuf, &mods->sml_values[0], pa );
2204 mods->sml_next = NULL;
2205 modtail->sml_next = mods;
2209 Debug( LDAP_DEBUG_TRACE,
2210 "ppolicy_modify: password attr lookup failed\n", 0, 0, 0 );
2215 * Controversial bit here. If the new password isn't hashed
2216 * (ie, is cleartext), we probably should hash it according
2217 * to the default hash. The reason for this is that we want
2218 * to use the policy if possible, but if we hash the password
2219 * before, then we're going to run into trouble when it
2220 * comes time to check the password.
2222 * Now, the right thing to do is to use the extended password
2223 * modify operation, but not all software can do this,
2224 * therefore it makes sense to hash the new password, now
2225 * we know it passes the policy requirements.
2227 * Of course, if the password is already hashed, then we
2231 if ((pi->hash_passwords) && (addmod) && !newpw.bv_val &&
2232 (password_scheme( &(addmod->sml_values[0]), NULL ) != LDAP_SUCCESS))
2234 struct berval hpw, bv;
2236 slap_passwd_hash( &(addmod->sml_values[0]), &hpw, &txt );
2237 if (hpw.bv_val == NULL) {
2239 * hashing didn't work. Emit an error.
2241 rs->sr_err = LDAP_OTHER;
2243 goto return_results;
2245 bv = addmod->sml_values[0];
2246 /* clear and discard the clear password */
2247 memset(bv.bv_val, 0, bv.bv_len);
2248 ber_memfree(bv.bv_val);
2249 addmod->sml_values[0] = hpw;
2252 op->o_bd->bd_info = (BackendInfo *)on->on_info;
2253 be_entry_release_r( op, e );
2254 return SLAP_CB_CONTINUE;
2257 free_pwd_history_list( &tl );
2258 op->o_bd->bd_info = (BackendInfo *)on->on_info;
2259 be_entry_release_r( op, e );
2261 ctrl = create_passcontrol( op, -1, -1, pErr );
2262 oldctrls = add_passcontrol( op, rs, ctrl );
2264 send_ldap_result( op, rs );
2266 free( (char *)txt );
2271 if ( rs->sr_flags & REP_CTRLS_MUSTBEFREED ) {
2272 op->o_tmpfree( oldctrls, op->o_tmpmemctx );
2275 rs->sr_flags |= REP_CTRLS_MUSTBEFREED;
2278 ctrls_cleanup( op, rs, oldctrls );
2290 if ( !BER_BVISNULL( &ctrl->ldctl_value ) ) {
2291 rs->sr_text = "passwordPolicyRequest control value not absent";
2292 return LDAP_PROTOCOL_ERROR;
2294 op->o_ctrlflag[ppolicy_cid] = ctrl->ldctl_iscritical
2295 ? SLAP_CONTROL_CRITICAL
2296 : SLAP_CONTROL_NONCRITICAL;
2298 return LDAP_SUCCESS;
2308 AttributeDescription *ad = NULL;
2312 code = slap_bv2ad( val, &ad, &err );
2314 ber_dupbv_x( out, &ad->ad_type->sat_cname, ctx );
2328 AttributeDescription *ad = NULL;
2332 code = slap_bv2ad( val, &ad, &err );
2334 ber_str2bv_x( ad->ad_type->sat_oid, 0, 1, out, ctx );
2345 slap_overinst *on = (slap_overinst *) be->bd_info;
2347 if ( SLAP_ISGLOBALOVERLAY( be ) ) {
2348 /* do not allow slapo-ppolicy to be global by now (ITS#5858) */
2350 snprintf( cr->msg, sizeof(cr->msg),
2351 "slapo-ppolicy cannot be global" );
2352 Debug( LDAP_DEBUG_ANY, "%s\n", cr->msg, 0, 0 );
2357 /* Has User Schema been initialized yet? */
2358 if ( !pwd_UsSchema[0].ad[0] ) {
2362 for (i=0; pwd_UsSchema[i].def; i++) {
2363 code = slap_str2ad( pwd_UsSchema[i].def, pwd_UsSchema[i].ad, &err );
2366 snprintf( cr->msg, sizeof(cr->msg),
2367 "User Schema load failed for attribute \"%s\". Error code %d: %s",
2368 pwd_UsSchema[i].def, code, err );
2369 Debug( LDAP_DEBUG_ANY, "%s\n", cr->msg, 0, 0 );
2378 syn = ch_malloc( sizeof( Syntax ));
2379 *syn = *ad_pwdAttribute->ad_type->sat_syntax;
2380 syn->ssyn_pretty = attrPretty;
2381 ad_pwdAttribute->ad_type->sat_syntax = syn;
2383 mr = ch_malloc( sizeof( MatchingRule ));
2384 *mr = *ad_pwdAttribute->ad_type->sat_equality;
2385 mr->smr_normalize = attrNormalize;
2386 ad_pwdAttribute->ad_type->sat_equality = mr;
2390 on->on_bi.bi_private = ch_calloc( sizeof(pp_info), 1 );
2392 if ( dtblsize && !pwcons ) {
2393 /* accommodate for c_conn_idx == -1 */
2394 pwcons = ch_calloc( sizeof(pw_conn), dtblsize + 1 );
2409 return overlay_register_control( be, LDAP_CONTROL_PASSWORDPOLICYREQUEST );
2418 #ifdef SLAP_CONFIG_DELETE
2419 overlay_unregister_control( be, LDAP_CONTROL_PASSWORDPOLICYREQUEST );
2420 #endif /* SLAP_CONFIG_DELETE */
2431 slap_overinst *on = (slap_overinst *) be->bd_info;
2432 pp_info *pi = on->on_bi.bi_private;
2434 on->on_bi.bi_private = NULL;
2435 free( pi->def_policy.bv_val );
2439 if ( ov_count <=0 && pwcons ) {
2440 pw_conn *pwc = pwcons;
2448 static char *extops[] = {
2449 LDAP_EXOP_MODIFY_PASSWD,
2453 static slap_overinst ppolicy;
2455 int ppolicy_initialize()
2459 for (i=0; pwd_OpSchema[i].def; i++) {
2460 code = register_at( pwd_OpSchema[i].def, pwd_OpSchema[i].ad, 0 );
2462 Debug( LDAP_DEBUG_ANY,
2463 "ppolicy_initialize: register_at failed\n", 0, 0, 0 );
2466 /* Allow Manager to set these as needed */
2467 if ( is_at_no_user_mod( (*pwd_OpSchema[i].ad)->ad_type )) {
2468 (*pwd_OpSchema[i].ad)->ad_type->sat_flags |=
2473 code = register_supported_control( LDAP_CONTROL_PASSWORDPOLICYREQUEST,
2474 SLAP_CTRL_ADD|SLAP_CTRL_BIND|SLAP_CTRL_MODIFY|SLAP_CTRL_HIDE, extops,
2475 ppolicy_parseCtrl, &ppolicy_cid );
2476 if ( code != LDAP_SUCCESS ) {
2477 Debug( LDAP_DEBUG_ANY, "Failed to register control %d\n", code, 0, 0 );
2481 ldap_pvt_thread_mutex_init( &chk_syntax_mutex );
2483 ppolicy.on_bi.bi_type = "ppolicy";
2484 ppolicy.on_bi.bi_db_init = ppolicy_db_init;
2485 ppolicy.on_bi.bi_db_open = ppolicy_db_open;
2486 ppolicy.on_bi.bi_db_close = ppolicy_db_close;
2487 ppolicy.on_bi.bi_db_destroy = ppolicy_db_destroy;
2489 ppolicy.on_bi.bi_op_add = ppolicy_add;
2490 ppolicy.on_bi.bi_op_bind = ppolicy_bind;
2491 ppolicy.on_bi.bi_op_compare = ppolicy_compare;
2492 ppolicy.on_bi.bi_op_delete = ppolicy_restrict;
2493 ppolicy.on_bi.bi_op_modify = ppolicy_modify;
2494 ppolicy.on_bi.bi_op_search = ppolicy_restrict;
2495 ppolicy.on_bi.bi_connection_destroy = ppolicy_connection_destroy;
2497 ppolicy.on_bi.bi_cf_ocs = ppolicyocs;
2498 code = config_register_schema( ppolicycfg, ppolicyocs );
2499 if ( code ) return code;
2501 return overlay_register( &ppolicy );
2504 #if SLAPD_OVER_PPOLICY == SLAPD_MOD_DYNAMIC
2505 int init_module(int argc, char *argv[]) {
2506 return ppolicy_initialize();
2510 #endif /* defined(SLAPD_OVER_PPOLICY) */