2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 2004-2006 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>
43 #ifndef MODULE_NAME_SZ
44 #define MODULE_NAME_SZ 256
47 /* Per-instance configuration information */
48 typedef struct pp_info {
49 struct berval def_policy; /* DN of default policy subentry */
50 int use_lockout; /* send AccountLocked result? */
51 int hash_passwords; /* transparently hash cleartext pwds */
54 /* Our per-connection info - note, it is not per-instance, it is
55 * used by all instances
57 typedef struct pw_conn {
58 struct berval dn; /* DN of restricted user */
61 static pw_conn *pwcons;
62 static int ppolicy_cid;
64 typedef struct pass_policy {
65 AttributeDescription *ad; /* attribute to which the policy applies */
66 int pwdMinAge; /* minimum time (seconds) until passwd can change */
67 int pwdMaxAge; /* time in seconds until pwd will expire after change */
68 int pwdInHistory; /* number of previous passwords kept */
69 int pwdCheckQuality; /* 0 = don't check quality, 1 = check if possible,
70 2 = check mandatory; fail if not possible */
71 int pwdMinLength; /* minimum number of chars in password */
72 int pwdExpireWarning; /* number of seconds that warning controls are
73 sent before a password expires */
74 int pwdGraceAuthNLimit; /* number of times you can log in with an
76 int pwdLockout; /* 0 = do not lockout passwords, 1 = lock them out */
77 int pwdLockoutDuration; /* time in seconds a password is locked out for */
78 int pwdMaxFailure; /* number of failed binds allowed before lockout */
79 int pwdFailureCountInterval; /* number of seconds before failure
81 int pwdMustChange; /* 0 = users can use admin set password
82 1 = users must change password after admin set */
83 int pwdAllowUserChange; /* 0 = users cannot change their passwords
84 1 = users can change them */
85 int pwdSafeModify; /* 0 = old password doesn't need to come
86 with password change request
87 1 = password change must supply existing pwd */
88 char pwdCheckModule[MODULE_NAME_SZ]; /* name of module to dynamically
89 load to check password */
92 typedef struct pw_hist {
93 time_t t; /* timestamp of history entry */
94 struct berval pw; /* old password hash */
95 struct berval bv; /* text of entire entry */
99 /* Operational attributes */
100 static AttributeDescription *ad_pwdChangedTime, *ad_pwdAccountLockedTime,
101 *ad_pwdFailureTime, *ad_pwdHistory, *ad_pwdGraceUseTime, *ad_pwdReset,
102 *ad_pwdPolicySubentry;
104 static struct schema_info {
106 AttributeDescription **ad;
108 { "( 1.3.6.1.4.1.42.2.27.8.1.16 "
109 "NAME ( 'pwdChangedTime' ) "
110 "DESC 'The time the password was last changed' "
111 "EQUALITY generalizedTimeMatch "
112 "ORDERING generalizedTimeOrderingMatch "
113 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
114 "SINGLE-VALUE NO-USER-MODIFICATION USAGE directoryOperation )",
115 &ad_pwdChangedTime },
116 { "( 1.3.6.1.4.1.42.2.27.8.1.17 "
117 "NAME ( 'pwdAccountLockedTime' ) "
118 "DESC 'The time an user account was locked' "
119 "EQUALITY generalizedTimeMatch "
120 "ORDERING generalizedTimeOrderingMatch "
121 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
124 /* Not until MANAGEDIT control is released */
125 "NO-USER-MODIFICATION "
127 "USAGE directoryOperation )",
128 &ad_pwdAccountLockedTime },
129 { "( 1.3.6.1.4.1.42.2.27.8.1.19 "
130 "NAME ( 'pwdFailureTime' ) "
131 "DESC 'The timestamps of the last consecutive authentication failures' "
132 "EQUALITY generalizedTimeMatch "
133 "ORDERING generalizedTimeOrderingMatch "
134 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
135 "NO-USER-MODIFICATION USAGE directoryOperation )",
136 &ad_pwdFailureTime },
137 { "( 1.3.6.1.4.1.42.2.27.8.1.20 "
138 "NAME ( 'pwdHistory' ) "
139 "DESC 'The history of users passwords' "
140 "EQUALITY octetStringMatch "
141 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.40 "
142 "NO-USER-MODIFICATION USAGE directoryOperation )",
144 { "( 1.3.6.1.4.1.42.2.27.8.1.21 "
145 "NAME ( 'pwdGraceUseTime' ) "
146 "DESC 'The timestamps of the grace login once the password has expired' "
147 "EQUALITY generalizedTimeMatch "
148 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
149 "NO-USER-MODIFICATION USAGE directoryOperation )",
150 &ad_pwdGraceUseTime },
151 { "( 1.3.6.1.4.1.42.2.27.8.1.22 "
152 "NAME ( 'pwdReset' ) "
153 "DESC 'The indication that the password has been reset' "
154 "EQUALITY booleanMatch "
155 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.7 "
156 "SINGLE-VALUE USAGE directoryOperation )",
158 { "( 1.3.6.1.4.1.42.2.27.8.1.23 "
159 "NAME ( 'pwdPolicySubentry' ) "
160 "DESC 'The pwdPolicy subentry in effect for this object' "
161 "EQUALITY distinguishedNameMatch "
162 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.12 "
165 /* Not until MANAGEDIT control is released */
166 "NO-USER-MODIFICATION "
168 "USAGE directoryOperation )",
169 &ad_pwdPolicySubentry },
173 /* User attributes */
174 static AttributeDescription *ad_pwdMinAge, *ad_pwdMaxAge, *ad_pwdInHistory,
175 *ad_pwdCheckQuality, *ad_pwdMinLength, *ad_pwdMaxFailure,
176 *ad_pwdGraceAuthNLimit, *ad_pwdExpireWarning, *ad_pwdLockoutDuration,
177 *ad_pwdFailureCountInterval, *ad_pwdCheckModule, *ad_pwdLockout,
178 *ad_pwdMustChange, *ad_pwdAllowUserChange, *ad_pwdSafeModify,
181 #define TAB(name) { #name, &ad_##name }
183 static struct schema_info pwd_UsSchema[] = {
188 TAB(pwdCheckQuality),
191 TAB(pwdGraceAuthNLimit),
192 TAB(pwdExpireWarning),
194 TAB(pwdLockoutDuration),
195 TAB(pwdFailureCountInterval),
198 TAB(pwdAllowUserChange),
203 static ldap_pvt_thread_mutex_t chk_syntax_mutex;
206 parse_time( char *atm )
209 struct lutil_timet tt;
210 time_t ret = (time_t)-1;
212 if ( lutil_parsetime( atm, &tm ) == 0) {
213 lutil_tm2time( &tm, &tt );
220 account_locked( Operation *op, Entry *e,
221 PassPolicy *pp, Modifications **mod )
227 if ( (la = attr_find( e->e_attrs, ad_pwdAccountLockedTime )) != NULL ) {
228 BerVarray vals = la->a_nvals;
231 * there is a lockout stamp - we now need to know if it's
234 if (vals[0].bv_val != NULL) {
238 if (!pp->pwdLockoutDuration)
241 if ((then = parse_time( vals[0].bv_val )) == (time_t)0)
244 now = slap_get_time();
246 if (now < then + pp->pwdLockoutDuration)
249 m = ch_calloc( sizeof(Modifications), 1 );
250 m->sml_op = LDAP_MOD_DELETE;
252 m->sml_type = ad_pwdAccountLockedTime->ad_cname;
253 m->sml_desc = ad_pwdAccountLockedTime;
262 /* IMPLICIT TAGS, all context-specific */
263 #define PPOLICY_WARNING 0xa0L /* constructed + 0 */
264 #define PPOLICY_ERROR 0x81L /* primitive + 1 */
266 #define PPOLICY_EXPIRE 0x80L /* primitive + 0 */
267 #define PPOLICY_GRACE 0x81L /* primitive + 1 */
270 create_passcontrol( int exptime, int grace, LDAPPasswordPolicyError err )
272 char berbuf[LBER_ELEMENT_SIZEOF], bb2[LBER_ELEMENT_SIZEOF];
273 BerElement *ber = (BerElement *)berbuf, *b2 = (BerElement *)bb2;
277 if ((c = ch_calloc( sizeof( LDAPControl ), 1 )) == NULL) return NULL;
278 c->ldctl_oid = LDAP_CONTROL_PASSWORDPOLICYRESPONSE;
279 c->ldctl_iscritical = 0;
280 c->ldctl_value.bv_val = NULL;
281 c->ldctl_value.bv_len = 0;
283 ber_init2( ber, NULL, LBER_USE_DER );
284 ber_printf(ber, "{" /*}*/ );
287 ber_init2( b2, NULL, LBER_USE_DER );
288 ber_printf( b2, "ti", PPOLICY_EXPIRE, exptime );
289 ber_flatten2( b2, &bv, 1 );
290 ber_printf( ber, "tO", PPOLICY_WARNING, &bv );
291 ch_free( bv.bv_val );
292 } else if (grace > 0) {
293 ber_init2( b2, NULL, LBER_USE_DER );
294 ber_printf( b2, "ti", PPOLICY_GRACE, grace );
295 ber_flatten2( b2, &bv, 1 );
296 ber_printf( ber, "tO", PPOLICY_WARNING, &bv );
297 ch_free( bv.bv_val );
300 if (err != PP_noError ) {
301 ber_printf( ber, "te", PPOLICY_ERROR, err );
303 ber_printf( ber, /*{*/ "N}" );
305 if (ber_flatten2( ber, &(c->ldctl_value), 1 ) == LBER_DEFAULT) {
307 (void)ber_free_buf(ber);
310 (void)ber_free_buf(ber);
315 ppolicy_get( Operation *op, Entry *e, PassPolicy *pp )
317 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
318 pp_info *pi = on->on_bi.bi_private;
327 memset( pp, 0, sizeof(PassPolicy) );
329 /* Users can change their own password by default */
330 pp->pwdAllowUserChange = 1;
332 if ((a = attr_find( e->e_attrs, ad_pwdPolicySubentry )) == NULL) {
334 * entry has no password policy assigned - use default
336 vals = &pi->def_policy;
341 if (vals[0].bv_val == NULL) {
342 Debug( LDAP_DEBUG_ANY,
343 "ppolicy_get: NULL value for policySubEntry\n", 0, 0, 0 );
348 op->o_bd->bd_info = (BackendInfo *)on->on_info;
349 rc = be_entry_get_rw( op, vals, NULL, NULL, 0, &pe );
350 op->o_bd->bd_info = (BackendInfo *)on;
352 if ( rc ) goto defaultpol;
354 #if 0 /* Only worry about userPassword for now */
355 if ((a = attr_find( pe->e_attrs, ad_pwdAttribute )))
356 slap_bv2ad( &a->a_vals[0], &pp->ad, &text );
358 pp->ad = slap_schema.si_ad_userPassword;
361 if ( ( a = attr_find( pe->e_attrs, ad_pwdMinAge ) )
362 && lutil_atoi( &pp->pwdMinAge, a->a_vals[0].bv_val ) != 0 )
364 if ( ( a = attr_find( pe->e_attrs, ad_pwdMaxAge ) )
365 && lutil_atoi( &pp->pwdMaxAge, a->a_vals[0].bv_val ) != 0 )
367 if ( ( a = attr_find( pe->e_attrs, ad_pwdInHistory ) )
368 && lutil_atoi( &pp->pwdInHistory, a->a_vals[0].bv_val ) != 0 )
370 if ( ( a = attr_find( pe->e_attrs, ad_pwdCheckQuality ) )
371 && lutil_atoi( &pp->pwdCheckQuality, a->a_vals[0].bv_val ) != 0 )
373 if ( ( a = attr_find( pe->e_attrs, ad_pwdMinLength ) )
374 && lutil_atoi( &pp->pwdMinLength, a->a_vals[0].bv_val ) != 0 )
376 if ( ( a = attr_find( pe->e_attrs, ad_pwdMaxFailure ) )
377 && lutil_atoi( &pp->pwdMaxFailure, a->a_vals[0].bv_val ) != 0 )
379 if ( ( a = attr_find( pe->e_attrs, ad_pwdGraceAuthNLimit ) )
380 && lutil_atoi( &pp->pwdGraceAuthNLimit, a->a_vals[0].bv_val ) != 0 )
382 if ( ( a = attr_find( pe->e_attrs, ad_pwdExpireWarning ) )
383 && lutil_atoi( &pp->pwdExpireWarning, a->a_vals[0].bv_val ) != 0 )
385 if ( ( a = attr_find( pe->e_attrs, ad_pwdFailureCountInterval ) )
386 && lutil_atoi( &pp->pwdFailureCountInterval, a->a_vals[0].bv_val ) != 0 )
388 if ( ( a = attr_find( pe->e_attrs, ad_pwdLockoutDuration ) )
389 && lutil_atoi( &pp->pwdLockoutDuration, a->a_vals[0].bv_val ) != 0 )
392 if ( ( a = attr_find( pe->e_attrs, ad_pwdCheckModule ) ) ) {
393 strncpy( pp->pwdCheckModule, a->a_vals[0].bv_val,
394 sizeof(pp->pwdCheckModule) );
395 pp->pwdCheckModule[sizeof(pp->pwdCheckModule)-1] = '\0';
398 if ((a = attr_find( pe->e_attrs, ad_pwdLockout )))
399 pp->pwdLockout = !strcmp( a->a_nvals[0].bv_val, "TRUE" );
400 if ((a = attr_find( pe->e_attrs, ad_pwdMustChange )))
401 pp->pwdMustChange = !strcmp( a->a_nvals[0].bv_val, "TRUE" );
402 if ((a = attr_find( pe->e_attrs, ad_pwdAllowUserChange )))
403 pp->pwdAllowUserChange = !strcmp( a->a_nvals[0].bv_val, "TRUE" );
404 if ((a = attr_find( pe->e_attrs, ad_pwdSafeModify )))
405 pp->pwdSafeModify = !strcmp( a->a_nvals[0].bv_val, "TRUE" );
407 op->o_bd->bd_info = (BackendInfo *)on->on_info;
408 be_entry_release_r( op, pe );
409 op->o_bd->bd_info = (BackendInfo *)on;
414 Debug( LDAP_DEBUG_ANY,
415 "ppolicy_get: using default policy\n", 0, 0, 0 );
420 password_scheme( struct berval *cred, struct berval *sch )
424 assert( cred != NULL );
431 if ((cred->bv_len == 0) || (cred->bv_val == NULL) ||
432 (cred->bv_val[0] != '{')) return LDAP_OTHER;
434 for(e = 1; cred->bv_val[e] && cred->bv_val[e] != '}'; e++);
435 if (cred->bv_val[e]) {
437 rc = lutil_passwd_scheme( cred->bv_val );
440 sch->bv_val = cred->bv_val;
450 check_password_quality( struct berval *cred, PassPolicy *pp, LDAPPasswordPolicyError *err, Entry *e )
452 int rc = LDAP_SUCCESS, ok = LDAP_SUCCESS;
453 char *ptr = cred->bv_val;
456 assert( cred != NULL );
457 assert( pp != NULL );
459 if ((cred->bv_len == 0) || (pp->pwdMinLength > cred->bv_len)) {
460 rc = LDAP_CONSTRAINT_VIOLATION;
461 if ( err ) *err = PP_passwordTooShort;
466 * We need to know if the password is already hashed - if so
467 * what scheme is it. The reason being that the "hash" of
468 * {cleartext} still allows us to check the password.
470 rc = password_scheme( cred, &sch );
471 if (rc == LDAP_SUCCESS) {
472 if ((sch.bv_val) && (strncasecmp( sch.bv_val, "{cleartext}",
473 sch.bv_len ) == 0)) {
475 * We can check the cleartext "hash"
477 ptr = cred->bv_val + sch.bv_len;
479 /* everything else, we can't check */
480 if (pp->pwdCheckQuality == 2) {
481 rc = LDAP_CONSTRAINT_VIOLATION;
482 if (err) *err = PP_insufficientPasswordQuality;
486 * We can't check the syntax of the password, but it's not
487 * mandatory (according to the policy), so we return success.
496 if (pp->pwdCheckModule[0]) {
501 if ((mod = lt_dlopen( pp->pwdCheckModule )) == NULL) {
504 Debug(LDAP_DEBUG_ANY,
505 "check_password_quality: lt_dlopen failed: (%s) %s.\n",
506 pp->pwdCheckModule, err, 0 );
507 ok = LDAP_OTHER; /* internal error */
509 int (*prog)( char *passwd, char **text, Entry *ent );
511 if ((prog = lt_dlsym( mod, "check_password" )) == NULL) {
514 Debug(LDAP_DEBUG_ANY,
515 "check_password_quality: lt_dlsym failed: (%s) %s.\n",
516 pp->pwdCheckModule, err, 0 );
521 ldap_pvt_thread_mutex_lock( &chk_syntax_mutex );
522 ok = prog( cred->bv_val, &txt, e );
523 ldap_pvt_thread_mutex_unlock( &chk_syntax_mutex );
525 Debug(LDAP_DEBUG_ANY,
526 "check_password_quality: module error: (%s) %s.[%d]\n",
527 pp->pwdCheckModule, txt, ok );
536 Debug(LDAP_DEBUG_ANY, "check_password_quality: external modules not "
537 "supported. pwdCheckModule ignored.\n", 0, 0, 0);
538 #endif /* SLAPD_MODULES */
542 if (ok != LDAP_SUCCESS) {
543 rc = LDAP_CONSTRAINT_VIOLATION;
544 if (err) *err = PP_insufficientPasswordQuality;
551 parse_pwdhistory( struct berval *bv, char **oid, time_t *oldtime, struct berval *oldpw )
554 struct berval nv, npw;
557 assert (bv && (bv->bv_len > 0) && (bv->bv_val) && oldtime && oldpw );
560 *oldtime = (time_t)-1;
561 oldpw->bv_val = NULL;
564 ber_dupbv( &nv, bv );
566 /* first get the time field */
567 for(i=0; (i < nv.bv_len) && (nv.bv_val[i] != '#'); i++);
568 if ( i == nv.bv_len) goto exit_failure; /* couldn't locate the '#' separator */
569 nv.bv_val[i++] = '\0'; /* terminate the string & move to next field */
571 *oldtime = parse_time( ptr );
572 if (*oldtime == (time_t)-1) goto exit_failure;
574 /* get the OID field */
575 for(ptr = &(nv.bv_val[i]);(i < nv.bv_len) && (nv.bv_val[i] != '#'); i++);
576 if ( i == nv.bv_len) goto exit_failure; /* couldn't locate the '#' separator */
577 nv.bv_val[i++] = '\0'; /* terminate the string & move to next field */
578 if ( oid ) *oid = ber_strdup( ptr );
580 /* get the length field */
581 for(ptr = &(nv.bv_val[i]);(i < nv.bv_len) && (nv.bv_val[i] != '#'); i++);
582 if ( i == nv.bv_len) goto exit_failure; /* couldn't locate the '#' separator */
583 nv.bv_val[i++] = '\0'; /* terminate the string & move to next field */
584 oldpw->bv_len = strtol( ptr, NULL, 10 );
585 if (errno == ERANGE) goto exit_failure;
587 /* lastly, get the octets of the string */
588 for(j=i, ptr = &(nv.bv_val[i]);i < nv.bv_len; i++);
589 if (i-j != oldpw->bv_len) goto exit_failure; /* length is wrong */
592 npw.bv_len = oldpw->bv_len;
593 ber_dupbv( oldpw, &npw );
597 if (oid && *oid) { ber_memfree(*oid); *oid = NULL; }
599 ber_memfree( oldpw->bv_val); oldpw->bv_val = NULL;
602 ber_memfree(nv.bv_val);
607 add_to_pwd_history( pw_hist **l, time_t t,
608 struct berval *oldpw, struct berval *bv )
610 pw_hist *p, *p1, *p2;
614 p = ch_malloc( sizeof( pw_hist ));
616 ber_dupbv( &p->bv, bv );
621 /* degenerate case */
626 * advance p1 and p2 such that p1 is the node before the
627 * new one, and p2 is the node after it
629 for (p1 = NULL, p2 = *l; p2 && p2->t <= t; p1 = p2, p2=p2->next );
631 if (p1 == NULL) { *l = p; return; }
635 #ifndef MAX_PWD_HISTORY_SZ
636 #define MAX_PWD_HISTORY_SZ 1024
637 #endif /* MAX_PWD_HISTORY_SZ */
640 make_pwd_history_value( char *timebuf, struct berval *bv, Attribute *pa )
642 char str[ MAX_PWD_HISTORY_SZ ];
645 snprintf( str, MAX_PWD_HISTORY_SZ,
646 "%s#%s#%lu#", timebuf,
647 pa->a_desc->ad_type->sat_syntax->ssyn_oid,
648 (unsigned long) pa->a_nvals[0].bv_len );
649 str[MAX_PWD_HISTORY_SZ-1] = 0;
653 * We have to assume that the string is a string of octets,
654 * not readable characters. In reality, yes, it probably is
655 * a readable (ie, base64) string, but we can't count on that
656 * Hence, while the first 3 fields of the password history
657 * are definitely readable (a timestamp, an OID and an integer
658 * length), the remaining octets of the actual password
659 * are deemed to be binary data.
661 AC_MEMCPY( str + nlen, pa->a_nvals[0].bv_val, pa->a_nvals[0].bv_len );
662 nlen += pa->a_nvals[0].bv_len;
663 bv->bv_val = ch_malloc( nlen + 1 );
664 AC_MEMCPY( bv->bv_val, str, nlen );
665 bv->bv_val[nlen] = '\0';
670 free_pwd_history_list( pw_hist **l )
677 pw_hist *pp = p->next;
687 typedef struct ppbind {
691 LDAPPasswordPolicyError pErr;
696 ppolicy_bind_resp( Operation *op, SlapReply *rs )
698 ppbind *ppb = op->o_callback->sc_private;
699 slap_overinst *on = ppb->on;
700 Modifications *mod = ppb->mod, *m;
702 int ngut = -1, warn = -1, age, rc;
704 time_t now, pwtime = (time_t)-1;
705 char nowstr[ LDAP_LUTIL_GENTIME_BUFSIZE ];
706 struct berval timestamp;
707 BackendInfo *bi = op->o_bd->bd_info;
710 /* If we already know it's locked, just get on with it */
711 if ( ppb->pErr != PP_noError ) {
715 op->o_bd->bd_info = (BackendInfo *)on->on_info;
716 rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &e );
717 op->o_bd->bd_info = bi;
719 if ( rc != LDAP_SUCCESS ) {
720 return SLAP_CB_CONTINUE;
723 now = slap_get_time(); /* stored for later consideration */
724 timestamp.bv_val = nowstr;
725 timestamp.bv_len = sizeof(nowstr);
726 slap_timestamp( &now, ×tamp );
728 if ( rs->sr_err == LDAP_INVALID_CREDENTIALS ) {
731 m = ch_calloc( sizeof(Modifications), 1 );
732 m->sml_op = LDAP_MOD_ADD;
734 m->sml_type = ad_pwdFailureTime->ad_cname;
735 m->sml_desc = ad_pwdFailureTime;
736 m->sml_values = ch_calloc( sizeof(struct berval), 2 );
737 m->sml_nvalues = ch_calloc( sizeof(struct berval), 2 );
739 ber_dupbv( &m->sml_values[0], ×tamp );
740 ber_dupbv( &m->sml_nvalues[0], ×tamp );
745 * Count the pwdFailureTimes - if it's
746 * greater than the policy pwdMaxFailure,
747 * then lock the account.
749 if ((a = attr_find( e->e_attrs, ad_pwdFailureTime )) != NULL) {
750 for(i=0; a->a_nvals[i].bv_val; i++) {
753 * If the interval is 0, then failures
754 * stay on the record until explicitly
755 * reset by successful authentication.
757 if (ppb->pp.pwdFailureCountInterval == 0) {
760 parse_time(a->a_nvals[i].bv_val) +
761 ppb->pp.pwdFailureCountInterval) {
766 * We only count those failures
767 * which are not due to expire.
772 if ((ppb->pp.pwdMaxFailure > 0) &&
773 (fc >= ppb->pp.pwdMaxFailure - 1)) {
776 * We subtract 1 from the failure max
777 * because the new failure entry hasn't
778 * made it to the entry yet.
780 m = ch_calloc( sizeof(Modifications), 1 );
781 m->sml_op = LDAP_MOD_REPLACE;
783 m->sml_type = ad_pwdAccountLockedTime->ad_cname;
784 m->sml_desc = ad_pwdAccountLockedTime;
785 m->sml_values = ch_calloc( sizeof(struct berval), 2 );
786 m->sml_nvalues = ch_calloc( sizeof(struct berval), 2 );
787 ber_dupbv( &m->sml_values[0], ×tamp );
788 ber_dupbv( &m->sml_nvalues[0], ×tamp );
792 } else if ( rs->sr_err == LDAP_SUCCESS ) {
793 if ((a = attr_find( e->e_attrs, ad_pwdChangedTime )) != NULL)
794 pwtime = parse_time( a->a_nvals[0].bv_val );
796 /* delete all pwdFailureTimes */
797 if ( attr_find( e->e_attrs, ad_pwdFailureTime )) {
798 m = ch_calloc( sizeof(Modifications), 1 );
799 m->sml_op = LDAP_MOD_DELETE;
801 m->sml_type = ad_pwdFailureTime->ad_cname;
802 m->sml_desc = ad_pwdFailureTime;
808 * check to see if the password must be changed
810 if ( ppb->pp.pwdMustChange &&
811 (a = attr_find( e->e_attrs, ad_pwdReset )) &&
812 !strcmp( a->a_nvals[0].bv_val, "TRUE" ) ) {
814 * need to inject client controls here to give
815 * more information. For the moment, we ensure
816 * that we are disallowed from doing anything
817 * other than change password.
819 ber_dupbv( &pwcons[op->o_conn->c_conn_idx].dn,
820 &op->o_conn->c_ndn );
822 ppb->pErr = PP_changeAfterReset;
826 * the password does not need to be changed, so
827 * we now check whether the password has expired.
829 * We can skip this bit if passwords don't age in
832 if (ppb->pp.pwdMaxAge == 0) goto grace;
834 if (pwtime == (time_t)-1) {
836 * Hmm. No password changed time on the
837 * entry. This is odd - it should have
838 * been provided when the attribute was added.
840 * However, it's possible that it could be
841 * missing if the DIT was established via
844 Debug( LDAP_DEBUG_ANY,
845 "ppolicy_bind: Entry %s does not have valid pwdChangedTime attribute - assuming password expired\n",
846 e->e_name.bv_val, 0, 0);
851 * Check: was the last change time of
852 * the password older than the maximum age
853 * allowed. (Ignore case 2 from I-D, it's just silly.)
855 if (now - pwtime > ppb->pp.pwdMaxAge ) pwExpired = 1;
860 if (!pwExpired) goto check_expiring_password;
862 if ((a = attr_find( e->e_attrs, ad_pwdGraceUseTime )) == NULL)
863 ngut = ppb->pp.pwdGraceAuthNLimit;
865 for(ngut=0; a->a_nvals[ngut].bv_val; ngut++);
866 ngut = ppb->pp.pwdGraceAuthNLimit - ngut;
870 * ngut is the number of remaining grace logins
872 Debug( LDAP_DEBUG_ANY,
873 "ppolicy_bind: Entry %s has an expired password: %d grace logins\n",
874 e->e_name.bv_val, ngut, 0);
877 ppb->pErr = PP_passwordExpired;
878 rs->sr_err = LDAP_INVALID_CREDENTIALS;
883 * Add a grace user time to the entry
885 m = ch_calloc( sizeof(Modifications), 1 );
886 m->sml_op = LDAP_MOD_ADD;
888 m->sml_type = ad_pwdGraceUseTime->ad_cname;
889 m->sml_desc = ad_pwdGraceUseTime;
890 m->sml_values = ch_calloc( sizeof(struct berval), 2 );
891 m->sml_nvalues = ch_calloc( sizeof(struct berval), 2 );
892 ber_dupbv( &m->sml_values[0], ×tamp );
893 ber_dupbv( &m->sml_nvalues[0], ×tamp );
897 check_expiring_password:
899 * Now we need to check to see
900 * if it is about to expire, and if so, should the user
901 * be warned about it in the password policy control.
903 * If the password has expired, and we're in the grace period, then
904 * we don't need to do this bit. Similarly, if we don't have password
905 * aging, then there's no need to do this bit either.
907 if ((ppb->pp.pwdMaxAge < 1) || (pwExpired) || (ppb->pp.pwdExpireWarning < 1))
910 age = (int)(now - pwtime);
913 * We know that there is a password Change Time attribute - if
914 * there wasn't, then the pwdExpired value would be true, unless
915 * there is no password aging - and if there is no password aging,
916 * then this section isn't called anyway - you can't have an
917 * expiring password if there's no limit to expire.
919 if (ppb->pp.pwdMaxAge - age < ppb->pp.pwdExpireWarning ) {
921 * Set the warning value.
923 warn = ppb->pp.pwdMaxAge - age; /* seconds left until expiry */
924 if (warn < 0) warn = 0; /* something weird here - why is pwExpired not set? */
926 Debug( LDAP_DEBUG_ANY,
927 "ppolicy_bind: Setting warning for password expiry for %s = %d seconds\n",
928 op->o_req_dn.bv_val, warn, 0 );
933 op->o_bd->bd_info = (BackendInfo *)on->on_info;
934 be_entry_release_r( op, e );
939 SlapReply r2 = { REP_RESULT };
940 slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
942 /* FIXME: Need to handle replication of some (but not all)
943 * of the operational attributes...
945 op2.o_tag = LDAP_REQ_MODIFY;
946 op2.o_callback = &cb;
947 op2.orm_modlist = mod;
948 op2.o_dn = op->o_bd->be_rootdn;
949 op2.o_ndn = op->o_bd->be_rootndn;
950 op2.o_bd->bd_info = (BackendInfo *)on->on_info;
951 rc = op->o_bd->be_modify( &op2, &r2 );
952 slap_mods_free( mod, 1 );
955 if ( ppb->send_ctrl ) {
956 LDAPControl **ctrls = NULL;
957 pp_info *pi = on->on_bi.bi_private;
959 /* Do we really want to tell that the account is locked? */
960 if ( ppb->pErr == PP_accountLocked && !pi->use_lockout ) {
961 ppb->pErr = PP_noError;
963 ctrls = ch_calloc( sizeof( LDAPControl *) , 2 );
964 ctrls[0] = create_passcontrol( warn, ngut, ppb->pErr );
966 rs->sr_ctrls = ctrls;
968 op->o_bd->bd_info = bi;
969 return SLAP_CB_CONTINUE;
973 ppolicy_bind( Operation *op, SlapReply *rs )
975 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
977 /* Reset lockout status on all Bind requests */
978 if ( !BER_BVISEMPTY( &pwcons[op->o_conn->c_conn_idx].dn )) {
979 ch_free( pwcons[op->o_conn->c_conn_idx].dn.bv_val );
980 BER_BVZERO( &pwcons[op->o_conn->c_conn_idx].dn );
983 /* Root bypasses policy */
984 if ( !be_isroot_dn( op->o_bd, &op->o_req_ndn )) {
990 op->o_bd->bd_info = (BackendInfo *)on->on_info;
991 rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &e );
993 if ( rc != LDAP_SUCCESS ) {
994 return SLAP_CB_CONTINUE;
997 cb = op->o_tmpcalloc( sizeof(ppbind)+sizeof(slap_callback),
998 1, op->o_tmpmemctx );
999 ppb = (ppbind *)(cb+1);
1001 ppb->pErr = PP_noError;
1003 /* Setup a callback so we can munge the result */
1005 cb->sc_response = ppolicy_bind_resp;
1006 cb->sc_next = op->o_callback->sc_next;
1007 cb->sc_private = ppb;
1008 op->o_callback->sc_next = cb;
1010 /* Did we receive a password policy request control? */
1011 if ( op->o_ctrlflag[ppolicy_cid] ) {
1015 op->o_bd->bd_info = (BackendInfo *)on;
1016 ppolicy_get( op, e, &ppb->pp );
1018 rc = account_locked( op, e, &ppb->pp, &ppb->mod );
1020 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1021 be_entry_release_r( op, e );
1024 /* This will be the Draft 8 response, Unwilling is bogus */
1025 ppb->pErr = PP_accountLocked;
1026 send_ldap_error( op, rs, LDAP_INVALID_CREDENTIALS, NULL );
1032 return SLAP_CB_CONTINUE;
1035 /* Reset the restricted info for the next session on this connection */
1037 ppolicy_connection_destroy( BackendDB *bd, Connection *conn )
1039 if ( !BER_BVISEMPTY( &pwcons[conn->c_conn_idx].dn )) {
1040 ch_free( pwcons[conn->c_conn_idx].dn.bv_val );
1041 BER_BVZERO( &pwcons[conn->c_conn_idx].dn );
1043 return SLAP_CB_CONTINUE;
1046 /* Check if this connection is restricted */
1052 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1055 /* Did we receive a password policy request control? */
1056 if ( op->o_ctrlflag[ppolicy_cid] ) {
1060 if ( op->o_conn && !BER_BVISEMPTY( &pwcons[op->o_conn->c_conn_idx].dn )) {
1061 /* if the current authcDN doesn't match the one we recorded,
1062 * then an intervening Bind has succeeded and the restriction
1063 * no longer applies. (ITS#4516)
1065 if ( !dn_match( &op->o_conn->c_ndn,
1066 &pwcons[op->o_conn->c_conn_idx].dn )) {
1067 ch_free( pwcons[op->o_conn->c_conn_idx].dn.bv_val );
1068 BER_BVZERO( &pwcons[op->o_conn->c_conn_idx].dn );
1069 return SLAP_CB_CONTINUE;
1072 Debug( LDAP_DEBUG_TRACE,
1073 "connection restricted to password changing only\n", 0, 0, 0);
1075 LDAPControl **ctrls = NULL;
1077 ctrls = ch_calloc( sizeof( LDAPControl *) , 2 );
1078 ctrls[0] = create_passcontrol( -1, -1, PP_changeAfterReset );
1080 rs->sr_ctrls = ctrls;
1082 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1083 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
1084 "Operations are restricted to bind/unbind/abandon/StartTLS/modify password" );
1088 return SLAP_CB_CONTINUE;
1096 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1097 pp_info *pi = on->on_bi.bi_private;
1102 if ( ppolicy_restrict( op, rs ) != SLAP_CB_CONTINUE )
1105 /* If this is a replica, assume the master checked everything */
1106 if ( be_shadow_update( op ))
1107 return SLAP_CB_CONTINUE;
1109 /* Check for password in entry */
1110 if ((pa = attr_find( op->oq_add.rs_e->e_attrs,
1111 slap_schema.si_ad_userPassword )))
1114 * new entry contains a password - if we're not the root user
1115 * then we need to check that the password fits in with the
1116 * security policy for the new entry.
1118 ppolicy_get( op, op->ora_e, &pp );
1119 if (pp.pwdCheckQuality > 0 && !be_isroot( op )) {
1120 struct berval *bv = &(pa->a_vals[0]);
1121 int rc, send_ctrl = 0;
1122 LDAPPasswordPolicyError pErr = PP_noError;
1124 /* Did we receive a password policy request control? */
1125 if ( op->o_ctrlflag[ppolicy_cid] ) {
1128 rc = check_password_quality( bv, &pp, &pErr, op->ora_e );
1129 if (rc != LDAP_SUCCESS) {
1130 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1132 LDAPControl **ctrls = NULL;
1134 ctrls = ch_calloc( sizeof( LDAPControl *) , 2 );
1135 ctrls[0] = create_passcontrol( -1, -1, pErr );
1137 rs->sr_ctrls = ctrls;
1139 send_ldap_error( op, rs, rc, "Password fails quality checking policy" );
1144 * A controversial bit. We hash cleartext
1145 * passwords provided via add and modify operations
1146 * You're not really supposed to do this, since
1147 * the X.500 model says "store attributes" as they
1148 * get provided. By default, this is what we do
1150 * But if the hash_passwords flag is set, we hash
1151 * any cleartext password attribute values via the
1152 * default password hashing scheme.
1154 if ((pi->hash_passwords) &&
1155 (password_scheme( &(pa->a_vals[0]), NULL ) != LDAP_SUCCESS)) {
1158 slap_passwd_hash( &(pa->a_vals[0]), &hpw, &txt );
1159 if (hpw.bv_val == NULL) {
1161 * hashing didn't work. Emit an error.
1163 rs->sr_err = LDAP_OTHER;
1165 send_ldap_error( op, rs, LDAP_OTHER, "Password hashing failed" );
1169 memset( pa->a_vals[0].bv_val, 0, pa->a_vals[0].bv_len);
1170 ber_memfree( pa->a_vals[0].bv_val );
1171 pa->a_vals[0].bv_val = hpw.bv_val;
1172 pa->a_vals[0].bv_len = hpw.bv_len;
1175 /* If password aging is in effect, set the pwdChangedTime */
1176 if ( pp.pwdMaxAge || pp.pwdMinAge ) {
1177 struct berval timestamp;
1178 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
1179 time_t now = slap_get_time();
1181 timestamp.bv_val = timebuf;
1182 timestamp.bv_len = sizeof(timebuf);
1183 slap_timestamp( &now, ×tamp );
1185 attr_merge_one( op->ora_e, ad_pwdChangedTime, ×tamp, ×tamp );
1188 return SLAP_CB_CONTINUE;
1192 ppolicy_mod_cb( Operation *op, SlapReply *rs )
1194 slap_callback *sc = op->o_callback;
1195 op->o_callback = sc->sc_next;
1196 if ( rs->sr_err == LDAP_SUCCESS ) {
1197 ch_free( pwcons[op->o_conn->c_conn_idx].dn.bv_val );
1198 BER_BVZERO( &pwcons[op->o_conn->c_conn_idx].dn );
1200 op->o_tmpfree( sc, op->o_tmpmemctx );
1201 return SLAP_CB_CONTINUE;
1205 ppolicy_modify( Operation *op, SlapReply *rs )
1207 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1208 pp_info *pi = on->on_bi.bi_private;
1209 int i, rc, mod_pw_only, pwmod, pwmop = -1, deladd,
1212 Modifications *mods = NULL, *modtail = NULL,
1213 *ml, *delmod, *addmod;
1214 Attribute *pa, *ha, at;
1216 pw_hist *tl = NULL, *p;
1217 int zapReset, send_ctrl = 0;
1219 struct berval newpw = BER_BVNULL, oldpw = BER_BVNULL,
1221 LDAPPasswordPolicyError pErr = PP_noError;
1223 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1224 rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &e );
1225 op->o_bd->bd_info = (BackendInfo *)on;
1227 if ( rc != LDAP_SUCCESS ) return SLAP_CB_CONTINUE;
1229 /* If this is a replica, we may need to tweak some of the
1230 * master's modifications. Otherwise, just pass it through.
1232 if ( be_shadow_update( op )) {
1233 Modifications **prev;
1234 int got_del_grace = 0, got_del_lock = 0, got_pw = 0, got_del_fail = 0;
1235 Attribute *a_grace, *a_lock, *a_fail;
1237 a_grace = attr_find( e->e_attrs, ad_pwdGraceUseTime );
1238 a_lock = attr_find( e->e_attrs, ad_pwdAccountLockedTime );
1239 a_fail = attr_find( e->e_attrs, ad_pwdFailureTime );
1241 for( prev = &op->oq_modify.rs_modlist, ml = *prev; ml; ml = *prev ) {
1243 if ( ml->sml_desc == slap_schema.si_ad_userPassword )
1246 /* If we're deleting an attr that didn't exist,
1247 * drop this delete op
1249 if ( ml->sml_op == LDAP_MOD_DELETE ) {
1252 if ( ml->sml_desc == ad_pwdGraceUseTime ) {
1257 if ( ml->sml_desc == ad_pwdAccountLockedTime ) {
1262 if ( ml->sml_desc == ad_pwdFailureTime ) {
1268 *prev = ml->sml_next;
1269 ml->sml_next = NULL;
1270 slap_mods_free( ml, 1 );
1274 prev = &ml->sml_next;
1277 /* If we're resetting the password, make sure grace, accountlock,
1278 * and failure also get removed.
1281 if ( a_grace && !got_del_grace ) {
1282 ml = (Modifications *) ch_malloc( sizeof( Modifications ) );
1283 ml->sml_op = LDAP_MOD_DELETE;
1284 ml->sml_flags = SLAP_MOD_INTERNAL;
1285 ml->sml_type.bv_val = NULL;
1286 ml->sml_desc = ad_pwdGraceUseTime;
1287 ml->sml_values = NULL;
1288 ml->sml_nvalues = NULL;
1289 ml->sml_next = NULL;
1291 prev = &ml->sml_next;
1293 if ( a_lock && !got_del_lock ) {
1294 ml = (Modifications *) ch_malloc( sizeof( Modifications ) );
1295 ml->sml_op = LDAP_MOD_DELETE;
1296 ml->sml_flags = SLAP_MOD_INTERNAL;
1297 ml->sml_type.bv_val = NULL;
1298 ml->sml_desc = ad_pwdAccountLockedTime;
1299 ml->sml_values = NULL;
1300 ml->sml_nvalues = NULL;
1301 ml->sml_next = NULL;
1304 if ( a_fail && !got_del_fail ) {
1305 ml = (Modifications *) ch_malloc( sizeof( Modifications ) );
1306 ml->sml_op = LDAP_MOD_DELETE;
1307 ml->sml_flags = SLAP_MOD_INTERNAL;
1308 ml->sml_type.bv_val = NULL;
1309 ml->sml_desc = ad_pwdFailureTime;
1310 ml->sml_values = NULL;
1311 ml->sml_nvalues = NULL;
1312 ml->sml_next = NULL;
1316 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1317 be_entry_release_r( op, e );
1318 return SLAP_CB_CONTINUE;
1321 /* Did we receive a password policy request control? */
1322 if ( op->o_ctrlflag[ppolicy_cid] ) {
1326 /* See if this is a pwdModify exop. If so, we can
1327 * access the plaintext passwords from that request.
1332 for ( sc = op->o_callback; sc; sc=sc->sc_next ) {
1333 if ( sc->sc_response == slap_replog_cb &&
1335 req_pwdexop_s *qpw = sc->sc_private;
1336 newpw = qpw->rs_new;
1337 oldpw = qpw->rs_old;
1343 ppolicy_get( op, e, &pp );
1345 for ( ml = op->oq_modify.rs_modlist,
1346 pwmod = 0, mod_pw_only = 1,
1347 deladd = 0, delmod = NULL,
1350 ml != NULL; modtail = ml, ml = ml->sml_next )
1352 if ( ml->sml_desc == pp.ad ) {
1355 if ((deladd == 0) && (ml->sml_op == LDAP_MOD_DELETE) &&
1356 (ml->sml_values) && (ml->sml_values[0].bv_val != NULL)) {
1361 if ((deladd == 1) && ((ml->sml_op == LDAP_MOD_ADD) ||
1362 (ml->sml_op == LDAP_MOD_REPLACE)))
1365 if ((ml->sml_op == LDAP_MOD_ADD) ||
1366 (ml->sml_op == LDAP_MOD_REPLACE))
1368 } else if (! is_at_operational( ml->sml_desc->ad_type )) {
1370 /* modifying something other than password */
1374 * If there is a request to explicitly add a pwdReset
1375 * attribute, then we suppress the normal behaviour on
1376 * password change, which is to remove the pwdReset
1379 * This enables an administrator to assign a new password
1380 * and place a "must reset" flag on the entry, which will
1381 * stay until the user explicitly changes his/her password.
1383 if (ml->sml_desc == ad_pwdReset ) {
1384 if ((ml->sml_op == LDAP_MOD_ADD) ||
1385 (ml->sml_op == LDAP_MOD_REPLACE))
1390 if (!BER_BVISEMPTY( &pwcons[op->o_conn->c_conn_idx].dn ) && !mod_pw_only ) {
1391 if ( dn_match( &op->o_conn->c_ndn,
1392 &pwcons[op->o_conn->c_conn_idx].dn )) {
1393 Debug( LDAP_DEBUG_TRACE,
1394 "connection restricted to password changing only\n", 0, 0, 0 );
1395 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
1396 rs->sr_text = "Operations are restricted to bind/unbind/abandon/StartTLS/modify password";
1397 pErr = PP_changeAfterReset;
1398 goto return_results;
1400 ch_free( pwcons[op->o_conn->c_conn_idx].dn.bv_val );
1401 BER_BVZERO( &pwcons[op->o_conn->c_conn_idx].dn );
1406 * if we have a "safe password modify policy", then we need to check if we're doing
1407 * a delete (with the old password), followed by an add (with the new password).
1409 * If we don't have this, then we fail with an error. We also skip all the checks if
1410 * the root user is bound. Root can do anything, including avoid the policies.
1413 if (!pwmod) goto do_modify;
1416 * Did we get a valid add mod?
1420 rs->sr_err = LDAP_OTHER;
1421 rs->sr_text = "Internal Error";
1422 Debug( LDAP_DEBUG_TRACE,
1423 "cannot locate modification supplying new password\n", 0, 0, 0 );
1424 goto return_results;
1428 * Build the password history list in ascending time order
1429 * We need this, even if the user is root, in order to maintain
1430 * the pwdHistory operational attributes properly.
1432 if (pp.pwdInHistory > 0 && (ha = attr_find( e->e_attrs, ad_pwdHistory ))) {
1433 struct berval oldpw;
1436 for(i=0; ha->a_nvals[i].bv_val; i++) {
1437 rc = parse_pwdhistory( &(ha->a_nvals[i]), NULL,
1440 if (rc != LDAP_SUCCESS) continue; /* invalid history entry */
1443 add_to_pwd_history( &tl, oldtime, &oldpw,
1444 &(ha->a_nvals[i]) );
1445 oldpw.bv_val = NULL;
1449 for(p=tl; p; p=p->next, hsize++); /* count history size */
1452 if (be_isroot( op )) goto do_modify;
1454 /* This is a pwdModify exop that provided the old pw.
1455 * We need to create a Delete mod for this old pw and
1456 * let the matching value get found later
1458 if (pp.pwdSafeModify && oldpw.bv_val ) {
1459 ml = (Modifications *) ch_malloc( sizeof( Modifications ) );
1460 ml->sml_op = LDAP_MOD_DELETE;
1461 ml->sml_flags = SLAP_MOD_INTERNAL;
1462 ml->sml_desc = pp.ad;
1463 ml->sml_type = pp.ad->ad_cname;
1464 ml->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1465 ber_dupbv( &ml->sml_values[0], &oldpw );
1466 ml->sml_values[1].bv_len = 0;
1467 ml->sml_values[1].bv_val = NULL;
1468 ml->sml_nvalues = NULL;
1469 ml->sml_next = op->orm_modlist;
1470 op->orm_modlist = ml;
1475 if (pp.pwdSafeModify && deladd != 2) {
1476 Debug( LDAP_DEBUG_TRACE,
1477 "change password must use DELETE followed by ADD/REPLACE\n",
1479 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
1480 rs->sr_text = "Must supply old password to be changed as well as new one";
1481 pErr = PP_mustSupplyOldPassword;
1482 goto return_results;
1485 if (!pp.pwdAllowUserChange) {
1486 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
1487 rs->sr_text = "User alteration of password is not allowed";
1488 pErr = PP_passwordModNotAllowed;
1489 goto return_results;
1492 if (pp.pwdMinAge > 0) {
1493 time_t pwtime = (time_t)-1, now;
1496 if ((pa = attr_find( e->e_attrs, ad_pwdChangedTime )) != NULL)
1497 pwtime = parse_time( pa->a_nvals[0].bv_val );
1498 now = slap_get_time();
1499 age = (int)(now - pwtime);
1500 if ((pwtime != (time_t)-1) && (age < pp.pwdMinAge)) {
1501 rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
1502 rs->sr_text = "Password is too young to change";
1503 pErr = PP_passwordTooYoung;
1504 goto return_results;
1508 /* pa is used in password history check below, be sure it's set */
1509 if ((pa = attr_find( e->e_attrs, pp.ad )) != NULL && delmod) {
1511 * we have a password to check
1515 bv = oldpw.bv_val ? &oldpw : delmod->sml_values;
1516 /* FIXME: no access checking? */
1517 rc = slap_passwd_check( op, NULL, pa, bv, &txt );
1518 if (rc != LDAP_SUCCESS) {
1519 Debug( LDAP_DEBUG_TRACE,
1520 "old password check failed: %s\n", txt, 0, 0 );
1522 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1523 rs->sr_text = "Must supply correct old password to change to new one";
1524 pErr = PP_mustSupplyOldPassword;
1525 goto return_results;
1531 * replace the delete value with the (possibly hashed)
1532 * value which is currently in the password.
1534 for(i=0; delmod->sml_values[i].bv_val; i++) {
1535 free(delmod->sml_values[i].bv_val);
1536 delmod->sml_values[i].bv_len = 0;
1538 free(delmod->sml_values);
1539 delmod->sml_values = ch_calloc( sizeof(struct berval), 2 );
1540 delmod->sml_values[1].bv_len = 0;
1541 delmod->sml_values[1].bv_val = NULL;
1542 ber_dupbv(&(delmod->sml_values[0]), &(pa->a_nvals[0]));
1546 bv = newpw.bv_val ? &newpw : addmod->sml_values;
1547 if (pp.pwdCheckQuality > 0) {
1549 rc = check_password_quality( bv, &pp, &pErr, e );
1550 if (rc != LDAP_SUCCESS) {
1552 rs->sr_text = "Password fails quality checking policy";
1553 goto return_results;
1559 * Last check - the password history.
1561 /* FIXME: no access checking? */
1562 if (slap_passwd_check( op, NULL, pa, bv, &txt ) == LDAP_SUCCESS) {
1564 * This is bad - it means that the user is attempting
1565 * to set the password to the same as the old one.
1567 rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
1568 rs->sr_text = "Password is not being changed from existing value";
1569 pErr = PP_passwordInHistory;
1570 goto return_results;
1573 if (pp.pwdInHistory < 1) goto do_modify;
1576 * Iterate through the password history, and fail on any
1581 cr[1].bv_val = NULL;
1582 for(p=tl; p; p=p->next) {
1584 /* FIXME: no access checking? */
1585 rc = slap_passwd_check( op, NULL, &at, bv, &txt );
1587 if (rc != LDAP_SUCCESS) continue;
1589 rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
1590 rs->sr_text = "Password is in history of old passwords";
1591 pErr = PP_passwordInHistory;
1592 goto return_results;
1598 struct berval timestamp;
1599 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
1600 time_t now = slap_get_time();
1602 /* If the conn is restricted, set a callback to clear it
1603 * if the pwmod succeeds
1605 if (!BER_BVISEMPTY( &pwcons[op->o_conn->c_conn_idx].dn )) {
1606 slap_callback *sc = op->o_tmpcalloc( 1, sizeof( slap_callback ),
1608 sc->sc_next = op->o_callback;
1609 /* Must use sc_response to insure we reset on success, before
1610 * the client sees the response. Must use sc_cleanup to insure
1611 * that it gets cleaned up if sc_response is not called.
1613 sc->sc_response = ppolicy_mod_cb;
1614 sc->sc_cleanup = ppolicy_mod_cb;
1615 op->o_callback = sc;
1619 * keep the necessary pwd.. operational attributes
1623 timestamp.bv_val = timebuf;
1624 timestamp.bv_len = sizeof(timebuf);
1625 slap_timestamp( &now, ×tamp );
1627 mods = (Modifications *) ch_malloc( sizeof( Modifications ) );
1628 mods->sml_type.bv_val = NULL;
1629 mods->sml_desc = ad_pwdChangedTime;
1630 if (pwmop != LDAP_MOD_DELETE) {
1631 mods->sml_op = LDAP_MOD_REPLACE;
1632 mods->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1633 mods->sml_nvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1634 ber_dupbv( &mods->sml_values[0], ×tamp );
1635 ber_dupbv( &mods->sml_nvalues[0], ×tamp );
1636 mods->sml_values[1].bv_len = 0;
1637 mods->sml_values[1].bv_val = NULL;
1638 assert( mods->sml_values[0].bv_val != NULL );
1640 mods->sml_op = LDAP_MOD_DELETE;
1641 mods->sml_values = NULL;
1643 mods->sml_flags = SLAP_MOD_INTERNAL;
1644 mods->sml_nvalues = NULL;
1645 mods->sml_next = NULL;
1646 modtail->sml_next = mods;
1649 if (attr_find(e->e_attrs, ad_pwdGraceUseTime )) {
1650 mods = (Modifications *) ch_malloc( sizeof( Modifications ) );
1651 mods->sml_op = LDAP_MOD_DELETE;
1652 mods->sml_flags = SLAP_MOD_INTERNAL;
1653 mods->sml_type.bv_val = NULL;
1654 mods->sml_desc = ad_pwdGraceUseTime;
1655 mods->sml_values = NULL;
1656 mods->sml_nvalues = NULL;
1657 mods->sml_next = NULL;
1658 modtail->sml_next = mods;
1662 if (attr_find(e->e_attrs, ad_pwdAccountLockedTime )) {
1663 mods = (Modifications *) ch_malloc( sizeof( Modifications ) );
1664 mods->sml_op = LDAP_MOD_DELETE;
1665 mods->sml_flags = SLAP_MOD_INTERNAL;
1666 mods->sml_type.bv_val = NULL;
1667 mods->sml_desc = ad_pwdAccountLockedTime;
1668 mods->sml_values = NULL;
1669 mods->sml_nvalues = NULL;
1670 mods->sml_next = NULL;
1671 modtail->sml_next = mods;
1675 if (attr_find(e->e_attrs, ad_pwdFailureTime )) {
1676 mods = (Modifications *) ch_malloc( sizeof( Modifications ) );
1677 mods->sml_op = LDAP_MOD_DELETE;
1678 mods->sml_flags = SLAP_MOD_INTERNAL;
1679 mods->sml_type.bv_val = NULL;
1680 mods->sml_desc = ad_pwdFailureTime;
1681 mods->sml_values = NULL;
1682 mods->sml_nvalues = NULL;
1683 mods->sml_next = NULL;
1684 modtail->sml_next = mods;
1688 /* Delete the pwdReset attribute, since it's being reset */
1689 if ((zapReset) && (attr_find(e->e_attrs, ad_pwdReset ))) {
1690 mods = (Modifications *) ch_malloc( sizeof( Modifications ) );
1691 mods->sml_op = LDAP_MOD_DELETE;
1692 mods->sml_flags = SLAP_MOD_INTERNAL;
1693 mods->sml_type.bv_val = NULL;
1694 mods->sml_desc = ad_pwdReset;
1695 mods->sml_values = NULL;
1696 mods->sml_nvalues = NULL;
1697 mods->sml_next = NULL;
1698 modtail->sml_next = mods;
1702 if (pp.pwdInHistory > 0) {
1703 if (hsize >= pp.pwdInHistory) {
1705 * We use the >= operator, since we are going to add
1706 * the existing password attribute value into the
1707 * history - thus the cardinality of history values is
1708 * about to rise by one.
1710 * If this would push it over the limit of history
1711 * values (remembering - the password policy could have
1712 * changed since the password was last altered), we must
1713 * delete at least 1 value from the pwdHistory list.
1715 * In fact, we delete '(#pwdHistory attrs - max pwd
1716 * history length) + 1' values, starting with the oldest.
1717 * This is easily evaluated, since the linked list is
1718 * created in ascending time order.
1720 mods = (Modifications *) ch_malloc( sizeof( Modifications ) );
1721 mods->sml_op = LDAP_MOD_DELETE;
1722 mods->sml_flags = SLAP_MOD_INTERNAL;
1723 mods->sml_type.bv_val = NULL;
1724 mods->sml_desc = ad_pwdHistory;
1725 mods->sml_nvalues = NULL;
1726 mods->sml_values = ch_calloc( sizeof( struct berval ),
1727 hsize - pp.pwdInHistory + 2 );
1728 mods->sml_values[ hsize - pp.pwdInHistory + 1 ].bv_val = NULL;
1729 mods->sml_values[ hsize - pp.pwdInHistory + 1 ].bv_len = 0;
1730 for(i=0,p=tl; i < (hsize - pp.pwdInHistory + 1); i++, p=p->next) {
1731 mods->sml_values[i].bv_val = NULL;
1732 mods->sml_values[i].bv_len = 0;
1733 ber_dupbv( &(mods->sml_values[i]), &p->bv );
1735 mods->sml_next = NULL;
1736 modtail->sml_next = mods;
1739 free_pwd_history_list( &tl );
1742 * Now add the existing password into the history list.
1743 * This will be executed even if the operation is to delete
1744 * the password entirely.
1746 * This isn't in the spec explicitly, but it seems to make
1747 * sense that the password history list is the list of all
1748 * previous passwords - even if they were deleted. Thus, if
1749 * someone tries to add a historical password at some future
1750 * point, it will fail.
1752 if ((pa = attr_find( e->e_attrs, pp.ad )) != NULL) {
1753 mods = (Modifications *) ch_malloc( sizeof( Modifications ) );
1754 mods->sml_op = LDAP_MOD_ADD;
1755 mods->sml_flags = SLAP_MOD_INTERNAL;
1756 mods->sml_type.bv_val = NULL;
1757 mods->sml_desc = ad_pwdHistory;
1758 mods->sml_nvalues = NULL;
1759 mods->sml_values = ch_calloc( sizeof( struct berval ), 2 );
1760 mods->sml_values[ 1 ].bv_val = NULL;
1761 mods->sml_values[ 1 ].bv_len = 0;
1762 make_pwd_history_value( timebuf, &mods->sml_values[0], pa );
1763 mods->sml_next = NULL;
1764 modtail->sml_next = mods;
1767 Debug( LDAP_DEBUG_TRACE,
1768 "ppolicy_modify: password attr lookup failed\n", 0, 0, 0 );
1773 * Controversial bit here. If the new password isn't hashed
1774 * (ie, is cleartext), we probably should hash it according
1775 * to the default hash. The reason for this is that we want
1776 * to use the policy if possible, but if we hash the password
1777 * before, then we're going to run into trouble when it
1778 * comes time to check the password.
1780 * Now, the right thing to do is to use the extended password
1781 * modify operation, but not all software can do this,
1782 * therefore it makes sense to hash the new password, now
1783 * we know it passes the policy requirements.
1785 * Of course, if the password is already hashed, then we
1789 if ((pi->hash_passwords) && (addmod) && !newpw.bv_val &&
1790 (password_scheme( &(addmod->sml_values[0]), NULL ) != LDAP_SUCCESS)) {
1791 struct berval hpw, bv;
1793 slap_passwd_hash( &(addmod->sml_values[0]), &hpw, &txt );
1794 if (hpw.bv_val == NULL) {
1796 * hashing didn't work. Emit an error.
1798 rs->sr_err = LDAP_OTHER;
1800 goto return_results;
1802 bv.bv_val = addmod->sml_values[0].bv_val;
1803 bv.bv_len = addmod->sml_values[0].bv_len;
1804 /* clear and discard the clear password */
1805 memset(bv.bv_val, 0, bv.bv_len);
1806 ber_memfree(bv.bv_val);
1807 addmod->sml_values[0].bv_val = hpw.bv_val;
1808 addmod->sml_values[0].bv_len = hpw.bv_len;
1811 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1812 be_entry_release_r( op, e );
1813 return SLAP_CB_CONTINUE;
1816 free_pwd_history_list( &tl );
1817 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1818 be_entry_release_r( op, e );
1820 LDAPControl **ctrls = NULL;
1822 ctrls = ch_calloc( sizeof( LDAPControl *) , 2 );
1823 ctrls[0] = create_passcontrol( -1, -1, pErr );
1825 rs->sr_ctrls = ctrls;
1827 send_ldap_result( op, rs );
1837 if ( ctrl->ldctl_value.bv_len ) {
1838 rs->sr_text = "passwordPolicyRequest control value not empty";
1839 return LDAP_PROTOCOL_ERROR;
1841 if ( ctrl->ldctl_iscritical ) {
1842 rs->sr_text = "passwordPolicyRequest control invalid criticality";
1843 return LDAP_PROTOCOL_ERROR;
1845 op->o_ctrlflag[ppolicy_cid] = SLAP_CONTROL_NONCRITICAL;
1847 return LDAP_SUCCESS;
1857 AttributeDescription *ad = NULL;
1861 code = slap_bv2ad( val, &ad, &err );
1863 ber_dupbv_x( out, &ad->ad_type->sat_cname, ctx );
1877 AttributeDescription *ad = NULL;
1881 code = slap_bv2ad( val, &ad, &err );
1883 ber_str2bv_x( ad->ad_type->sat_oid, 0, 1, out, ctx );
1893 slap_overinst *on = (slap_overinst *) be->bd_info;
1895 /* Has User Schema been initialized yet? */
1896 if ( !pwd_UsSchema[0].ad[0] ) {
1900 for (i=0; pwd_UsSchema[i].def; i++) {
1901 code = slap_str2ad( pwd_UsSchema[i].def, pwd_UsSchema[i].ad, &err );
1903 fprintf( stderr, "User Schema Load failed %d: %s\n", code, err );
1911 syn = ch_malloc( sizeof( Syntax ));
1912 *syn = *ad_pwdAttribute->ad_type->sat_syntax;
1913 syn->ssyn_pretty = attrPretty;
1914 ad_pwdAttribute->ad_type->sat_syntax = syn;
1916 mr = ch_malloc( sizeof( MatchingRule ));
1917 *mr = *ad_pwdAttribute->ad_type->sat_equality;
1918 mr->smr_normalize = attrNormalize;
1919 ad_pwdAttribute->ad_type->sat_equality = mr;
1923 on->on_bi.bi_private = ch_calloc( sizeof(pp_info), 1 );
1925 if ( dtblsize && !pwcons )
1926 pwcons = ch_calloc(sizeof(pw_conn), dtblsize );
1936 return overlay_register_control( be, LDAP_CONTROL_PASSWORDPOLICYREQUEST );
1944 slap_overinst *on = (slap_overinst *) be->bd_info;
1945 pp_info *pi = on->on_bi.bi_private;
1948 free( pi->def_policy.bv_val );
1963 slap_overinst *on = (slap_overinst *) be->bd_info;
1964 pp_info *pi = on->on_bi.bi_private;
1968 if ( strcasecmp( argv[0], "ppolicy_default" ) == 0 ) {
1970 fprintf( stderr, "%s: line %d: invalid arguments in \"ppolicy_default"
1971 " <policyDN>\n", fname, lineno );
1974 ber_str2bv( argv[1], 0, 0, &dn );
1975 if ( dnNormalize( 0, NULL, NULL, &dn, &pi->def_policy, NULL ) ) {
1976 fprintf( stderr, "%s: line %d: policyDN is invalid\n",
1982 } else if ( strcasecmp( argv[0], "ppolicy_use_lockout" ) == 0 ) {
1984 fprintf( stderr, "%s: line %d: ppolicy_use_lockout "
1985 "takes no arguments\n", fname, lineno );
1988 pi->use_lockout = 1;
1990 } else if ( strcasecmp( argv[0], "ppolicy_hash_cleartext" ) == 0 ) {
1992 fprintf( stderr, "%s: line %d: ppolicy_hash_cleartext "
1993 "takes no arguments\n", fname, lineno );
1996 pi->hash_passwords = 1;
1999 return SLAP_CONF_UNKNOWN;
2002 static char *extops[] = {
2003 LDAP_EXOP_MODIFY_PASSWD,
2007 static slap_overinst ppolicy;
2009 int ppolicy_initialize()
2011 LDAPAttributeType *at;
2015 for (i=0; pwd_OpSchema[i].def; i++) {
2016 at = ldap_str2attributetype( pwd_OpSchema[i].def, &code, &err,
2017 LDAP_SCHEMA_ALLOW_ALL );
2019 fprintf( stderr, "AttributeType Load failed %s %s\n",
2020 ldap_scherr2str(code), err );
2023 code = at_add( at, 0, NULL, &err );
2025 slap_str2ad( at->at_names[0], pwd_OpSchema[i].ad, &err );
2029 fprintf( stderr, "AttributeType Load failed %s %s\n",
2030 scherr2str(code), err );
2033 /* Allow Manager to set these as needed */
2034 if ( is_at_no_user_mod( (*pwd_OpSchema[i].ad)->ad_type )) {
2035 (*pwd_OpSchema[i].ad)->ad_type->sat_flags |=
2040 code = register_supported_control( LDAP_CONTROL_PASSWORDPOLICYREQUEST,
2041 SLAP_CTRL_ADD|SLAP_CTRL_BIND|SLAP_CTRL_MODIFY|SLAP_CTRL_HIDE, extops,
2042 ppolicy_parseCtrl, &ppolicy_cid );
2043 if ( code != LDAP_SUCCESS ) {
2044 fprintf( stderr, "Failed to register control %d\n", code );
2048 ldap_pvt_thread_mutex_init( &chk_syntax_mutex );
2050 ppolicy.on_bi.bi_type = "ppolicy";
2051 ppolicy.on_bi.bi_db_init = ppolicy_db_init;
2052 ppolicy.on_bi.bi_db_open = ppolicy_db_open;
2053 ppolicy.on_bi.bi_db_config = ppolicy_config;
2054 ppolicy.on_bi.bi_db_close = ppolicy_close;
2056 ppolicy.on_bi.bi_op_add = ppolicy_add;
2057 ppolicy.on_bi.bi_op_bind = ppolicy_bind;
2058 ppolicy.on_bi.bi_op_compare = ppolicy_restrict;
2059 ppolicy.on_bi.bi_op_delete = ppolicy_restrict;
2060 ppolicy.on_bi.bi_op_modify = ppolicy_modify;
2061 ppolicy.on_bi.bi_op_search = ppolicy_restrict;
2062 ppolicy.on_bi.bi_connection_destroy = ppolicy_connection_destroy;
2064 return overlay_register( &ppolicy );
2067 #if SLAPD_OVER_PPOLICY == SLAPD_MOD_DYNAMIC
2068 int init_module(int argc, char *argv[]) {
2069 return ppolicy_initialize();
2073 #endif /* defined(SLAPD_OVER_PPOLICY) */