2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 2004-2005 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 int restricted; /* TRUE if connection is restricted */
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 #define PPOLICY_WARNING 0xa0L
263 #define PPOLICY_ERROR 0xa1L
265 #define PPOLICY_EXPIRE 0xa0L
266 #define PPOLICY_GRACE 0xa1L
269 create_passcontrol( int exptime, int grace, LDAPPasswordPolicyError err )
271 char berbuf[LBER_ELEMENT_SIZEOF], bb2[LBER_ELEMENT_SIZEOF];
272 BerElement *ber = (BerElement *)berbuf, *b2 = (BerElement *)bb2;
276 if ((c = ch_calloc( sizeof( LDAPControl ), 1 )) == NULL) return NULL;
277 c->ldctl_oid = LDAP_CONTROL_PASSWORDPOLICYRESPONSE;
278 c->ldctl_iscritical = 0;
279 c->ldctl_value.bv_val = NULL;
280 c->ldctl_value.bv_len = 0;
282 ber_init2( ber, NULL, LBER_USE_DER );
283 ber_printf(ber, "{" /*}*/ );
286 ber_init2( b2, NULL, LBER_USE_DER );
287 ber_printf( b2, "ti", PPOLICY_EXPIRE, exptime );
288 ber_flatten2( b2, &bv, 1 );
289 ber_printf( ber, "tO", PPOLICY_WARNING, &bv );
290 ch_free( bv.bv_val );
291 } else if (grace > 0) {
292 ber_init2( b2, NULL, LBER_USE_DER );
293 ber_printf( b2, "ti", PPOLICY_GRACE, grace );
294 ber_flatten2( b2, &bv, 1 );
295 ber_printf( ber, "tO", PPOLICY_WARNING, &bv );
296 ch_free( bv.bv_val );
299 if (err != PP_noError ) {
300 ber_printf( ber, "te", PPOLICY_ERROR, err );
302 ber_printf( ber, /*{*/ "N}" );
304 if (ber_flatten2( ber, &(c->ldctl_value), 1 ) == LBER_DEFAULT) {
306 (void)ber_free_buf(ber);
309 (void)ber_free_buf(ber);
314 ppolicy_get( Operation *op, Entry *e, PassPolicy *pp )
316 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
317 pp_info *pi = on->on_bi.bi_private;
326 memset( pp, 0, sizeof(PassPolicy) );
328 /* Users can change their own password by default */
329 pp->pwdAllowUserChange = 1;
331 if ((a = attr_find( e->e_attrs, ad_pwdPolicySubentry )) == NULL) {
333 * entry has no password policy assigned - use default
335 vals = &pi->def_policy;
340 if (vals[0].bv_val == NULL) {
341 Debug( LDAP_DEBUG_ANY,
342 "ppolicy_get: NULL value for policySubEntry\n", 0, 0, 0 );
347 op->o_bd->bd_info = (BackendInfo *)on->on_info;
348 rc = be_entry_get_rw( op, vals, NULL, NULL, 0, &pe );
349 op->o_bd->bd_info = (BackendInfo *)on;
351 if ( rc ) goto defaultpol;
353 #if 0 /* Only worry about userPassword for now */
354 if ((a = attr_find( pe->e_attrs, ad_pwdAttribute )))
355 slap_bv2ad( &a->a_vals[0], &pp->ad, &text );
357 pp->ad = slap_schema.si_ad_userPassword;
360 if ((a = attr_find( pe->e_attrs, ad_pwdMinAge )))
361 pp->pwdMinAge = atoi(a->a_vals[0].bv_val );
362 if ((a = attr_find( pe->e_attrs, ad_pwdMaxAge )))
363 pp->pwdMaxAge = atoi(a->a_vals[0].bv_val );
364 if ((a = attr_find( pe->e_attrs, ad_pwdInHistory )))
365 pp->pwdInHistory = atoi(a->a_vals[0].bv_val );
366 if ((a = attr_find( pe->e_attrs, ad_pwdCheckQuality )))
367 pp->pwdCheckQuality = atoi(a->a_vals[0].bv_val );
368 if ((a = attr_find( pe->e_attrs, ad_pwdMinLength )))
369 pp->pwdMinLength = atoi(a->a_vals[0].bv_val );
370 if ((a = attr_find( pe->e_attrs, ad_pwdMaxFailure )))
371 pp->pwdMaxFailure = atoi(a->a_vals[0].bv_val );
372 if ((a = attr_find( pe->e_attrs, ad_pwdGraceAuthNLimit )))
373 pp->pwdGraceAuthNLimit = atoi(a->a_vals[0].bv_val );
374 if ((a = attr_find( pe->e_attrs, ad_pwdExpireWarning )))
375 pp->pwdExpireWarning = atoi(a->a_vals[0].bv_val );
376 if ((a = attr_find( pe->e_attrs, ad_pwdFailureCountInterval )))
377 pp->pwdFailureCountInterval = atoi(a->a_vals[0].bv_val );
378 if ((a = attr_find( pe->e_attrs, ad_pwdLockoutDuration )))
379 pp->pwdLockoutDuration = atoi(a->a_vals[0].bv_val );
381 if ((a = attr_find( pe->e_attrs, ad_pwdCheckModule ))) {
382 strncpy(pp->pwdCheckModule, a->a_vals[0].bv_val,
383 sizeof(pp->pwdCheckModule));
384 pp->pwdCheckModule[sizeof(pp->pwdCheckModule)-1] = '\0';
387 if ((a = attr_find( pe->e_attrs, ad_pwdLockout )))
388 pp->pwdLockout = !strcmp( a->a_nvals[0].bv_val, "TRUE" );
389 if ((a = attr_find( pe->e_attrs, ad_pwdMustChange )))
390 pp->pwdMustChange = !strcmp( a->a_nvals[0].bv_val, "TRUE" );
391 if ((a = attr_find( pe->e_attrs, ad_pwdAllowUserChange )))
392 pp->pwdAllowUserChange = !strcmp( a->a_nvals[0].bv_val, "TRUE" );
393 if ((a = attr_find( pe->e_attrs, ad_pwdSafeModify )))
394 pp->pwdSafeModify = !strcmp( a->a_nvals[0].bv_val, "TRUE" );
396 op->o_bd->bd_info = (BackendInfo *)on->on_info;
397 be_entry_release_r( op, pe );
398 op->o_bd->bd_info = (BackendInfo *)on;
403 Debug( LDAP_DEBUG_ANY,
404 "ppolicy_get: using default policy\n", 0, 0, 0 );
409 password_scheme( struct berval *cred, struct berval *sch )
413 assert( cred != NULL );
420 if ((cred->bv_len == 0) || (cred->bv_val == NULL) ||
421 (cred->bv_val[0] != '{')) return LDAP_OTHER;
423 for(e = 1; cred->bv_val[e] && cred->bv_val[e] != '}'; e++);
424 if (cred->bv_val[e]) {
426 rc = lutil_passwd_scheme( cred->bv_val );
428 sch->bv_val = cred->bv_val;
437 check_password_quality( struct berval *cred, PassPolicy *pp, LDAPPasswordPolicyError *err, Entry *e )
439 int rc = LDAP_SUCCESS, ok = LDAP_SUCCESS;
440 char *ptr = cred->bv_val;
443 assert( cred != NULL );
444 assert( pp != NULL );
446 if ((cred->bv_len == 0) || (pp->pwdMinLength > cred->bv_len)) {
447 rc = LDAP_CONSTRAINT_VIOLATION;
448 if ( err ) *err = PP_passwordTooShort;
453 * We need to know if the password is already hashed - if so
454 * what scheme is it. The reason being that the "hash" of
455 * {cleartext} still allows us to check the password.
457 rc = password_scheme( cred, &sch );
458 if (rc == LDAP_SUCCESS) {
459 if ((sch.bv_val) && (strncasecmp( sch.bv_val, "{cleartext}",
460 sch.bv_len ) == 0)) {
462 * We can check the cleartext "hash"
464 ptr = cred->bv_val + sch.bv_len;
466 /* everything else, we can't check */
467 if (pp->pwdCheckQuality == 2) {
468 rc = LDAP_CONSTRAINT_VIOLATION;
469 if (err) *err = PP_insufficientPasswordQuality;
473 * We can't check the syntax of the password, but it's not
474 * mandatory (according to the policy), so we return success.
483 if (pp->pwdCheckModule[0]) {
488 if ((mod = lt_dlopen( pp->pwdCheckModule )) == NULL) {
491 Debug(LDAP_DEBUG_ANY,
492 "check_password_quality: lt_dlopen failed: (%s) %s.\n",
493 pp->pwdCheckModule, err, 0 );
494 ok = LDAP_OTHER; /* internal error */
496 int (*prog)( char *passwd, char **text, Entry *ent );
498 if ((prog = lt_dlsym( mod, "check_password" )) == NULL) {
501 Debug(LDAP_DEBUG_ANY,
502 "check_password_quality: lt_dlsym failed: (%s) %s.\n",
503 pp->pwdCheckModule, err, 0 );
508 ldap_pvt_thread_mutex_lock( &chk_syntax_mutex );
509 ok = prog( cred->bv_val, &txt, e );
510 ldap_pvt_thread_mutex_unlock( &chk_syntax_mutex );
512 Debug(LDAP_DEBUG_ANY,
513 "check_password_quality: module error: (%s) %s.[%d]\n",
514 pp->pwdCheckModule, txt, ok );
523 Debug(LDAP_DEBUG_ANY, "check_password_quality: external modules not "
524 "supported. pwdCheckModule ignored.\n", 0, 0, 0);
525 #endif /* SLAPD_MODULES */
529 if (ok != LDAP_SUCCESS) {
530 rc = LDAP_CONSTRAINT_VIOLATION;
531 if (err) *err = PP_insufficientPasswordQuality;
538 parse_pwdhistory( struct berval *bv, char **oid, time_t *oldtime, struct berval *oldpw )
541 struct berval nv, npw;
544 assert (bv && (bv->bv_len > 0) && (bv->bv_val) && oldtime && oldpw );
547 *oldtime = (time_t)-1;
548 oldpw->bv_val = NULL;
551 ber_dupbv( &nv, bv );
553 /* first get the time field */
554 for(i=0; (i < nv.bv_len) && (nv.bv_val[i] != '#'); i++);
555 if ( i == nv.bv_len) goto exit_failure; /* couldn't locate the '#' separator */
556 nv.bv_val[i++] = '\0'; /* terminate the string & move to next field */
558 *oldtime = parse_time( ptr );
559 if (*oldtime == (time_t)-1) goto exit_failure;
561 /* get the OID field */
562 for(ptr = &(nv.bv_val[i]);(i < nv.bv_len) && (nv.bv_val[i] != '#'); i++);
563 if ( i == nv.bv_len) goto exit_failure; /* couldn't locate the '#' separator */
564 nv.bv_val[i++] = '\0'; /* terminate the string & move to next field */
565 if ( oid ) *oid = ber_strdup( ptr );
567 /* get the length field */
568 for(ptr = &(nv.bv_val[i]);(i < nv.bv_len) && (nv.bv_val[i] != '#'); i++);
569 if ( i == nv.bv_len) goto exit_failure; /* couldn't locate the '#' separator */
570 nv.bv_val[i++] = '\0'; /* terminate the string & move to next field */
571 oldpw->bv_len = strtol( ptr, NULL, 10 );
572 if (errno == ERANGE) goto exit_failure;
574 /* lastly, get the octets of the string */
575 for(j=i, ptr = &(nv.bv_val[i]);i < nv.bv_len; i++);
576 if (i-j != oldpw->bv_len) goto exit_failure; /* length is wrong */
579 npw.bv_len = oldpw->bv_len;
580 ber_dupbv( oldpw, &npw );
584 if (oid && *oid) { ber_memfree(*oid); *oid = NULL; }
586 ber_memfree( oldpw->bv_val); oldpw->bv_val = NULL;
589 ber_memfree(nv.bv_val);
594 add_to_pwd_history( pw_hist **l, time_t t,
595 struct berval *oldpw, struct berval *bv )
597 pw_hist *p, *p1, *p2;
601 p = ch_malloc( sizeof( pw_hist ));
603 ber_dupbv( &p->bv, bv );
608 /* degenerate case */
613 * advance p1 and p2 such that p1 is the node before the
614 * new one, and p2 is the node after it
616 for (p1 = NULL, p2 = *l; p2 && p2->t <= t; p1 = p2, p2=p2->next );
618 if (p1 == NULL) { *l = p; return; }
622 #ifndef MAX_PWD_HISTORY_SZ
623 #define MAX_PWD_HISTORY_SZ 1024
624 #endif /* MAX_PWD_HISTORY_SZ */
627 make_pwd_history_value( char *timebuf, struct berval *bv, Attribute *pa )
629 char str[ MAX_PWD_HISTORY_SZ ];
632 snprintf( str, MAX_PWD_HISTORY_SZ,
633 "%s#%s#%lu#", timebuf,
634 pa->a_desc->ad_type->sat_syntax->ssyn_oid,
635 (unsigned long) pa->a_nvals[0].bv_len );
636 str[MAX_PWD_HISTORY_SZ-1] = 0;
640 * We have to assume that the string is a string of octets,
641 * not readable characters. In reality, yes, it probably is
642 * a readable (ie, base64) string, but we can't count on that
643 * Hence, while the first 3 fields of the password history
644 * are definitely readable (a timestamp, an OID and an integer
645 * length), the remaining octets of the actual password
646 * are deemed to be binary data.
648 AC_MEMCPY( str + nlen, pa->a_nvals[0].bv_val, pa->a_nvals[0].bv_len );
649 nlen += pa->a_nvals[0].bv_len;
650 bv->bv_val = ch_malloc( nlen + 1 );
651 AC_MEMCPY( bv->bv_val, str, nlen );
652 bv->bv_val[nlen] = '\0';
657 free_pwd_history_list( pw_hist **l )
664 pw_hist *pp = p->next;
674 typedef struct ppbind {
678 LDAPPasswordPolicyError pErr;
683 ppolicy_bind_resp( Operation *op, SlapReply *rs )
685 ppbind *ppb = op->o_callback->sc_private;
686 slap_overinst *on = ppb->on;
687 Modifications *mod = ppb->mod, *m;
689 int ngut = -1, warn = -1, age, rc;
691 time_t now, pwtime = (time_t)-1;
692 char nowstr[ LDAP_LUTIL_GENTIME_BUFSIZE ];
693 struct berval timestamp;
694 BackendInfo *bi = op->o_bd->bd_info;
697 /* If we already know it's locked, just get on with it */
698 if ( ppb->pErr != PP_noError ) {
702 op->o_bd->bd_info = (BackendInfo *)on->on_info;
703 rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &e );
704 op->o_bd->bd_info = bi;
706 if ( rc != LDAP_SUCCESS ) {
707 return SLAP_CB_CONTINUE;
710 now = slap_get_time(); /* stored for later consideration */
711 timestamp.bv_val = nowstr;
712 timestamp.bv_len = sizeof(nowstr);
713 slap_timestamp( &now, ×tamp );
715 if ( rs->sr_err == LDAP_INVALID_CREDENTIALS ) {
718 m = ch_calloc( sizeof(Modifications), 1 );
719 m->sml_op = LDAP_MOD_ADD;
721 m->sml_type = ad_pwdFailureTime->ad_cname;
722 m->sml_desc = ad_pwdFailureTime;
723 m->sml_values = ch_calloc( sizeof(struct berval), 2 );
725 ber_dupbv( &m->sml_values[0], ×tamp );
730 * Count the pwdFailureTimes - if it's
731 * greater than the policy pwdMaxFailure,
732 * then lock the account.
734 if ((a = attr_find( e->e_attrs, ad_pwdFailureTime )) != NULL) {
735 for(i=0; a->a_nvals[i].bv_val; i++) {
738 * If the interval is 0, then failures
739 * stay on the record until explicitly
740 * reset by successful authentication.
742 if (ppb->pp.pwdFailureCountInterval == 0) {
745 parse_time(a->a_nvals[i].bv_val) +
746 ppb->pp.pwdFailureCountInterval) {
751 * We only count those failures
752 * which are not due to expire.
757 if ((ppb->pp.pwdMaxFailure > 0) &&
758 (fc >= ppb->pp.pwdMaxFailure - 1)) {
761 * We subtract 1 from the failure max
762 * because the new failure entry hasn't
763 * made it to the entry yet.
765 m = ch_calloc( sizeof(Modifications), 1 );
766 m->sml_op = LDAP_MOD_REPLACE;
768 m->sml_type = ad_pwdAccountLockedTime->ad_cname;
769 m->sml_desc = ad_pwdAccountLockedTime;
770 m->sml_values = ch_calloc( sizeof(struct berval), 2 );
771 ber_dupbv( &m->sml_values[0], ×tamp );
775 } else if ( rs->sr_err == LDAP_SUCCESS ) {
776 if ((a = attr_find( e->e_attrs, ad_pwdChangedTime )) != NULL)
777 pwtime = parse_time( a->a_nvals[0].bv_val );
779 /* delete all pwdFailureTimes */
780 if ( attr_find( e->e_attrs, ad_pwdFailureTime )) {
781 m = ch_calloc( sizeof(Modifications), 1 );
782 m->sml_op = LDAP_MOD_DELETE;
784 m->sml_type = ad_pwdFailureTime->ad_cname;
785 m->sml_desc = ad_pwdFailureTime;
791 * check to see if the password must be changed
793 if ( ppb->pp.pwdMustChange &&
794 (a = attr_find( e->e_attrs, ad_pwdReset )) &&
795 !strcmp( a->a_nvals[0].bv_val, "TRUE" ) ) {
797 * need to inject client controls here to give
798 * more information. For the moment, we ensure
799 * that we are disallowed from doing anything
800 * other than change password.
802 pwcons[op->o_conn->c_conn_idx].restricted = 1;
804 ppb->pErr = PP_changeAfterReset;
808 * the password does not need to be changed, so
809 * we now check whether the password has expired.
811 * We can skip this bit if passwords don't age in
814 if (ppb->pp.pwdMaxAge == 0) goto grace;
816 if (pwtime == (time_t)-1) {
818 * Hmm. No password changed time on the
819 * entry. This is odd - it should have
820 * been provided when the attribute was added.
822 * However, it's possible that it could be
823 * missing if the DIT was established via
826 Debug( LDAP_DEBUG_ANY,
827 "ppolicy_bind: Entry %s does not have valid pwdChangedTime attribute - assuming password expired\n",
828 e->e_name.bv_val, 0, 0);
833 * Check: was the last change time of
834 * the password older than the maximum age
835 * allowed. (Ignore case 2 from I-D, it's just silly.)
837 if (now - pwtime > ppb->pp.pwdMaxAge ) pwExpired = 1;
842 if (!pwExpired) goto check_expiring_password;
844 if ((a = attr_find( e->e_attrs, ad_pwdGraceUseTime )) == NULL)
845 ngut = ppb->pp.pwdGraceAuthNLimit;
847 for(ngut=0; a->a_nvals[ngut].bv_val; ngut++);
848 ngut = ppb->pp.pwdGraceAuthNLimit - ngut;
852 * ngut is the number of remaining grace logins
854 Debug( LDAP_DEBUG_ANY,
855 "ppolicy_bind: Entry %s has an expired password: %d grace logins\n",
856 e->e_name.bv_val, ngut, 0);
859 ppb->pErr = PP_passwordExpired;
860 rs->sr_err = LDAP_INVALID_CREDENTIALS;
865 * Add a grace user time to the entry
867 m = ch_calloc( sizeof(Modifications), 1 );
868 m->sml_op = LDAP_MOD_ADD;
870 m->sml_type = ad_pwdGraceUseTime->ad_cname;
871 m->sml_desc = ad_pwdGraceUseTime;
872 m->sml_values = ch_calloc( sizeof(struct berval), 2 );
873 ber_dupbv( &m->sml_values[0], ×tamp );
877 check_expiring_password:
879 * Now we need to check to see
880 * if it is about to expire, and if so, should the user
881 * be warned about it in the password policy control.
883 * If the password has expired, and we're in the grace period, then
884 * we don't need to do this bit. Similarly, if we don't have password
885 * aging, then there's no need to do this bit either.
887 if ((ppb->pp.pwdMaxAge < 1) || (pwExpired) || (ppb->pp.pwdExpireWarning < 1))
890 age = (int)(now - pwtime);
893 * We know that there is a password Change Time attribute - if
894 * there wasn't, then the pwdExpired value would be true, unless
895 * there is no password aging - and if there is no password aging,
896 * then this section isn't called anyway - you can't have an
897 * expiring password if there's no limit to expire.
899 if (ppb->pp.pwdMaxAge - age < ppb->pp.pwdExpireWarning ) {
901 * Set the warning value.
903 warn = ppb->pp.pwdMaxAge - age; /* seconds left until expiry */
904 if (warn < 0) warn = 0; /* something weird here - why is pwExpired not set? */
906 Debug( LDAP_DEBUG_ANY,
907 "ppolicy_bind: Setting warning for password expiry for %s = %d seconds\n",
908 op->o_req_dn.bv_val, warn, 0 );
913 op->o_bd->bd_info = (BackendInfo *)on->on_info;
914 be_entry_release_r( op, e );
919 SlapReply r2 = { REP_RESULT };
920 slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
922 /* FIXME: Need to handle replication of some (but not all)
923 * of the operational attributes...
925 op2.o_tag = LDAP_REQ_MODIFY;
926 op2.o_callback = &cb;
927 op2.orm_modlist = mod;
928 op2.o_dn = op->o_bd->be_rootdn;
929 op2.o_ndn = op->o_bd->be_rootndn;
930 op2.o_bd->bd_info = (BackendInfo *)on->on_info;
931 rc = op->o_bd->be_modify( &op2, &r2 );
932 slap_mods_free( mod, 1 );
935 if ( ppb->send_ctrl ) {
936 LDAPControl **ctrls = NULL;
937 pp_info *pi = on->on_bi.bi_private;
939 /* Do we really want to tell that the account is locked? */
940 if ( ppb->pErr == PP_accountLocked && !pi->use_lockout ) {
941 ppb->pErr = PP_noError;
943 ctrls = ch_calloc( sizeof( LDAPControl *) , 2 );
944 ctrls[0] = create_passcontrol( warn, ngut, ppb->pErr );
946 rs->sr_ctrls = ctrls;
948 op->o_bd->bd_info = bi;
949 return SLAP_CB_CONTINUE;
953 ppolicy_bind( Operation *op, SlapReply *rs )
955 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
957 /* Reset lockout status on all Bind requests */
958 pwcons[op->o_conn->c_conn_idx].restricted = 0;
960 /* Root bypasses policy */
961 if ( !be_isroot_dn( op->o_bd, &op->o_req_ndn )) {
967 op->o_bd->bd_info = (BackendInfo *)on->on_info;
968 rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &e );
970 if ( rc != LDAP_SUCCESS ) {
971 return SLAP_CB_CONTINUE;
974 cb = op->o_tmpcalloc( sizeof(ppbind)+sizeof(slap_callback),
975 1, op->o_tmpmemctx );
976 ppb = (ppbind *)(cb+1);
978 ppb->pErr = PP_noError;
980 /* Setup a callback so we can munge the result */
982 cb->sc_response = ppolicy_bind_resp;
983 cb->sc_next = op->o_callback->sc_next;
984 cb->sc_private = ppb;
985 op->o_callback->sc_next = cb;
987 /* Did we receive a password policy request control? */
988 if ( op->o_ctrlflag[ppolicy_cid] ) {
992 op->o_bd->bd_info = (BackendInfo *)on;
993 ppolicy_get( op, e, &ppb->pp );
995 rc = account_locked( op, e, &ppb->pp, &ppb->mod );
997 op->o_bd->bd_info = (BackendInfo *)on->on_info;
998 be_entry_release_r( op, e );
1001 /* This will be the Draft 8 response, Unwilling is bogus */
1002 ppb->pErr = PP_accountLocked;
1003 send_ldap_error( op, rs, LDAP_INVALID_CREDENTIALS, NULL );
1009 return SLAP_CB_CONTINUE;
1012 /* Reset the restricted flag for the next session on this connection */
1014 ppolicy_connection_destroy( BackendDB *bd, Connection *conn )
1016 pwcons[conn->c_conn_idx].restricted = 0;
1017 return SLAP_CB_CONTINUE;
1020 /* Check if this connection is restricted */
1026 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1029 /* Did we receive a password policy request control? */
1030 if ( op->o_ctrlflag[ppolicy_cid] ) {
1034 if ( op->o_conn && pwcons[op->o_conn->c_conn_idx].restricted ) {
1035 Debug( LDAP_DEBUG_TRACE,
1036 "connection restricted to password changing only\n", 0, 0, 0);
1038 LDAPControl **ctrls = NULL;
1040 ctrls = ch_calloc( sizeof( LDAPControl *) , 2 );
1041 ctrls[0] = create_passcontrol( -1, -1, PP_changeAfterReset );
1043 rs->sr_ctrls = ctrls;
1045 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1046 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
1047 "Operations are restricted to bind/unbind/abandon/StartTLS/modify password" );
1051 return SLAP_CB_CONTINUE;
1059 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1060 pp_info *pi = on->on_bi.bi_private;
1065 if ( ppolicy_restrict( op, rs ) != SLAP_CB_CONTINUE )
1068 /* If this is a replica, assume the master checked everything */
1069 if ( be_shadow_update( op ))
1070 return SLAP_CB_CONTINUE;
1072 /* Check for password in entry */
1073 if ((pa = attr_find( op->oq_add.rs_e->e_attrs,
1074 slap_schema.si_ad_userPassword )))
1077 * new entry contains a password - if we're not the root user
1078 * then we need to check that the password fits in with the
1079 * security policy for the new entry.
1081 ppolicy_get( op, op->ora_e, &pp );
1082 if (pp.pwdCheckQuality > 0 && !be_isroot( op )) {
1083 struct berval *bv = &(pa->a_vals[0]);
1084 int rc, send_ctrl = 0;
1085 LDAPPasswordPolicyError pErr = PP_noError;
1087 /* Did we receive a password policy request control? */
1088 if ( op->o_ctrlflag[ppolicy_cid] ) {
1091 rc = check_password_quality( bv, &pp, &pErr, op->ora_e );
1092 if (rc != LDAP_SUCCESS) {
1093 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1095 LDAPControl **ctrls = NULL;
1097 ctrls = ch_calloc( sizeof( LDAPControl *) , 2 );
1098 ctrls[0] = create_passcontrol( -1, -1, pErr );
1100 rs->sr_ctrls = ctrls;
1102 send_ldap_error( op, rs, rc, "Password fails quality checking policy" );
1107 * A controversial bit. We hash cleartext
1108 * passwords provided via add and modify operations
1109 * You're not really supposed to do this, since
1110 * the X.500 model says "store attributes" as they
1111 * get provided. By default, this is what we do
1113 * But if the hash_passwords flag is set, we hash
1114 * any cleartext password attribute values via the
1115 * default password hashing scheme.
1117 if ((pi->hash_passwords) &&
1118 (password_scheme( &(pa->a_vals[0]), NULL ) != LDAP_SUCCESS)) {
1121 slap_passwd_hash( &(pa->a_vals[0]), &hpw, &txt );
1122 if (hpw.bv_val == NULL) {
1124 * hashing didn't work. Emit an error.
1126 rs->sr_err = LDAP_OTHER;
1128 send_ldap_error( op, rs, LDAP_OTHER, "Password hashing failed" );
1132 memset( pa->a_vals[0].bv_val, 0, pa->a_vals[0].bv_len);
1133 ber_memfree( pa->a_vals[0].bv_val );
1134 pa->a_vals[0].bv_val = hpw.bv_val;
1135 pa->a_vals[0].bv_len = hpw.bv_len;
1138 /* If password aging is in effect, set the pwdChangedTime */
1139 if ( pp.pwdMaxAge || pp.pwdMinAge ) {
1140 struct berval timestamp;
1141 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
1142 time_t now = slap_get_time();
1144 timestamp.bv_val = timebuf;
1145 timestamp.bv_len = sizeof(timebuf);
1146 slap_timestamp( &now, ×tamp );
1148 attr_merge_one( op->ora_e, ad_pwdChangedTime, ×tamp, NULL );
1151 return SLAP_CB_CONTINUE;
1155 ppolicy_modify( Operation *op, SlapReply *rs )
1157 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1158 pp_info *pi = on->on_bi.bi_private;
1159 int i, rc, mod_pw_only, pwmod, pwmop = -1, deladd,
1162 Modifications *mods = NULL, *modtail = NULL,
1163 *ml, *delmod, *addmod;
1164 Attribute *pa, *ha, at;
1166 pw_hist *tl = NULL, *p;
1167 int zapReset, send_ctrl = 0;
1169 struct berval newpw = BER_BVNULL, oldpw = BER_BVNULL,
1171 LDAPPasswordPolicyError pErr = PP_noError;
1173 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1174 rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &e );
1175 op->o_bd->bd_info = (BackendInfo *)on;
1177 if ( rc != LDAP_SUCCESS ) return SLAP_CB_CONTINUE;
1179 /* If this is a replica, we may need to tweak some of the
1180 * master's modifications. Otherwise, just pass it through.
1182 if ( be_shadow_update( op )) {
1183 Modifications **prev;
1184 int got_del_grace = 0, got_del_lock = 0, got_pw = 0, got_del_fail = 0;
1185 Attribute *a_grace, *a_lock, *a_fail;
1187 a_grace = attr_find( e->e_attrs, ad_pwdGraceUseTime );
1188 a_lock = attr_find( e->e_attrs, ad_pwdAccountLockedTime );
1189 a_fail = attr_find( e->e_attrs, ad_pwdFailureTime );
1191 for( prev = &op->oq_modify.rs_modlist, ml = *prev; ml; ml = *prev ) {
1193 if ( ml->sml_desc == slap_schema.si_ad_userPassword )
1196 /* If we're deleting an attr that didn't exist,
1197 * drop this delete op
1199 if ( ml->sml_op == LDAP_MOD_DELETE ) {
1202 if ( ml->sml_desc == ad_pwdGraceUseTime ) {
1207 if ( ml->sml_desc == ad_pwdAccountLockedTime ) {
1212 if ( ml->sml_desc == ad_pwdFailureTime ) {
1218 *prev = ml->sml_next;
1219 ml->sml_next = NULL;
1220 slap_mods_free( ml, 1 );
1224 prev = &ml->sml_next;
1227 /* If we're resetting the password, make sure grace, accountlock,
1228 * and failure also get removed.
1231 if ( a_grace && !got_del_grace ) {
1232 ml = (Modifications *) ch_malloc( sizeof( Modifications ) );
1233 ml->sml_op = LDAP_MOD_DELETE;
1234 ml->sml_flags = SLAP_MOD_INTERNAL;
1235 ml->sml_type.bv_val = NULL;
1236 ml->sml_desc = ad_pwdGraceUseTime;
1237 ml->sml_values = NULL;
1238 ml->sml_nvalues = NULL;
1239 ml->sml_next = NULL;
1241 prev = &ml->sml_next;
1243 if ( a_lock && !got_del_lock ) {
1244 ml = (Modifications *) ch_malloc( sizeof( Modifications ) );
1245 ml->sml_op = LDAP_MOD_DELETE;
1246 ml->sml_flags = SLAP_MOD_INTERNAL;
1247 ml->sml_type.bv_val = NULL;
1248 ml->sml_desc = ad_pwdAccountLockedTime;
1249 ml->sml_values = NULL;
1250 ml->sml_nvalues = NULL;
1251 ml->sml_next = NULL;
1254 if ( a_fail && !got_del_fail ) {
1255 ml = (Modifications *) ch_malloc( sizeof( Modifications ) );
1256 ml->sml_op = LDAP_MOD_DELETE;
1257 ml->sml_flags = SLAP_MOD_INTERNAL;
1258 ml->sml_type.bv_val = NULL;
1259 ml->sml_desc = ad_pwdFailureTime;
1260 ml->sml_values = NULL;
1261 ml->sml_nvalues = NULL;
1262 ml->sml_next = NULL;
1266 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1267 be_entry_release_r( op, e );
1268 return SLAP_CB_CONTINUE;
1271 /* Did we receive a password policy request control? */
1272 if ( op->o_ctrlflag[ppolicy_cid] ) {
1276 /* See if this is a pwdModify exop. If so, we can
1277 * access the plaintext passwords from that request.
1282 for ( sc = op->o_callback; sc; sc=sc->sc_next ) {
1283 if ( sc->sc_response == slap_replog_cb &&
1285 req_pwdexop_s *qpw = sc->sc_private;
1286 newpw = qpw->rs_new;
1287 oldpw = qpw->rs_old;
1293 ppolicy_get( op, e, &pp );
1295 for ( ml = op->oq_modify.rs_modlist,
1296 pwmod = 0, mod_pw_only = 1,
1297 deladd = 0, delmod = NULL,
1300 ml != NULL; modtail = ml, ml = ml->sml_next )
1302 if ( ml->sml_desc == pp.ad ) {
1305 if ((deladd == 0) && (ml->sml_op == LDAP_MOD_DELETE) &&
1306 (ml->sml_values) && (ml->sml_values[0].bv_val != NULL)) {
1311 if ((deladd == 1) && ((ml->sml_op == LDAP_MOD_ADD) ||
1312 (ml->sml_op == LDAP_MOD_REPLACE)))
1315 if ((ml->sml_op == LDAP_MOD_ADD) ||
1316 (ml->sml_op == LDAP_MOD_REPLACE))
1318 } else if (! is_at_operational( ml->sml_desc->ad_type )) {
1320 /* modifying something other than password */
1324 * If there is a request to explicitly add a pwdReset
1325 * attribute, then we suppress the normal behaviour on
1326 * password change, which is to remove the pwdReset
1329 * This enables an administrator to assign a new password
1330 * and place a "must reset" flag on the entry, which will
1331 * stay until the user explicitly changes his/her password.
1333 if (ml->sml_desc == ad_pwdReset ) {
1334 if ((ml->sml_op == LDAP_MOD_ADD) ||
1335 (ml->sml_op == LDAP_MOD_REPLACE))
1340 if (pwcons[op->o_conn->c_conn_idx].restricted && !mod_pw_only) {
1341 Debug( LDAP_DEBUG_TRACE,
1342 "connection restricted to password changing only\n", 0, 0, 0 );
1343 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
1344 rs->sr_text = "Operations are restricted to bind/unbind/abandon/StartTLS/modify password";
1345 pErr = PP_changeAfterReset;
1346 goto return_results;
1350 * if we have a "safe password modify policy", then we need to check if we're doing
1351 * a delete (with the old password), followed by an add (with the new password).
1353 * If we don't have this, then we fail with an error. We also skip all the checks if
1354 * the root user is bound. Root can do anything, including avoid the policies.
1357 if (!pwmod) goto do_modify;
1360 * Did we get a valid add mod?
1364 rs->sr_err = LDAP_OTHER;
1365 rs->sr_text = "Internal Error";
1366 Debug( LDAP_DEBUG_TRACE,
1367 "cannot locate modification supplying new password\n", 0, 0, 0 );
1368 goto return_results;
1372 * Build the password history list in ascending time order
1373 * We need this, even if the user is root, in order to maintain
1374 * the pwdHistory operational attributes properly.
1376 if (pp.pwdInHistory > 0 && (ha = attr_find( e->e_attrs, ad_pwdHistory ))) {
1377 struct berval oldpw;
1380 for(i=0; ha->a_nvals[i].bv_val; i++) {
1381 rc = parse_pwdhistory( &(ha->a_nvals[i]), NULL,
1384 if (rc != LDAP_SUCCESS) continue; /* invalid history entry */
1387 add_to_pwd_history( &tl, oldtime, &oldpw,
1388 &(ha->a_nvals[i]) );
1389 oldpw.bv_val = NULL;
1393 for(p=tl; p; p=p->next, hsize++); /* count history size */
1396 if (be_isroot( op )) goto do_modify;
1398 /* This is a pwdModify exop that provided the old pw.
1399 * We need to create a Delete mod for this old pw and
1400 * let the matching value get found later
1402 if (pp.pwdSafeModify && oldpw.bv_val ) {
1403 ml = (Modifications *) ch_malloc( sizeof( Modifications ) );
1404 ml->sml_op = LDAP_MOD_DELETE;
1405 ml->sml_flags = SLAP_MOD_INTERNAL;
1406 ml->sml_desc = pp.ad;
1407 ml->sml_type = pp.ad->ad_cname;
1408 ml->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1409 ber_dupbv( &ml->sml_values[0], &oldpw );
1410 ml->sml_values[1].bv_len = 0;
1411 ml->sml_values[1].bv_val = NULL;
1412 ml->sml_nvalues = NULL;
1413 ml->sml_next = op->orm_modlist;
1414 op->orm_modlist = ml;
1419 if (pp.pwdSafeModify && deladd != 2) {
1420 Debug( LDAP_DEBUG_TRACE,
1421 "change password must use DELETE followed by ADD/REPLACE\n",
1423 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
1424 rs->sr_text = "Must supply old password to be changed as well as new one";
1425 pErr = PP_mustSupplyOldPassword;
1426 goto return_results;
1429 if (!pp.pwdAllowUserChange) {
1430 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
1431 rs->sr_text = "User alteration of password is not allowed";
1432 pErr = PP_passwordModNotAllowed;
1433 goto return_results;
1436 if (pp.pwdMinAge > 0) {
1437 time_t pwtime = (time_t)-1, now;
1440 if ((pa = attr_find( e->e_attrs, ad_pwdChangedTime )) != NULL)
1441 pwtime = parse_time( pa->a_nvals[0].bv_val );
1442 now = slap_get_time();
1443 age = (int)(now - pwtime);
1444 if ((pwtime != (time_t)-1) && (age < pp.pwdMinAge)) {
1445 rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
1446 rs->sr_text = "Password is too young to change";
1447 pErr = PP_passwordTooYoung;
1448 goto return_results;
1452 /* pa is used in password history check below, be sure it's set */
1453 if ((pa = attr_find( e->e_attrs, pp.ad )) != NULL && delmod) {
1455 * we have a password to check
1459 bv = oldpw.bv_val ? &oldpw : delmod->sml_values;
1460 /* FIXME: no access checking? */
1461 rc = slap_passwd_check( op, NULL, pa, bv, &txt );
1462 if (rc != LDAP_SUCCESS) {
1463 Debug( LDAP_DEBUG_TRACE,
1464 "old password check failed: %s\n", txt, 0, 0 );
1466 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1467 rs->sr_text = "Must supply correct old password to change to new one";
1468 pErr = PP_mustSupplyOldPassword;
1469 goto return_results;
1475 * replace the delete value with the (possibly hashed)
1476 * value which is currently in the password.
1478 for(i=0; delmod->sml_values[i].bv_val; i++) {
1479 free(delmod->sml_values[i].bv_val);
1480 delmod->sml_values[i].bv_len = 0;
1482 free(delmod->sml_values);
1483 delmod->sml_values = ch_calloc( sizeof(struct berval), 2 );
1484 delmod->sml_values[1].bv_len = 0;
1485 delmod->sml_values[1].bv_val = NULL;
1486 ber_dupbv(&(delmod->sml_values[0]), &(pa->a_nvals[0]));
1490 bv = newpw.bv_val ? &newpw : addmod->sml_values;
1491 if (pp.pwdCheckQuality > 0) {
1493 rc = check_password_quality( bv, &pp, &pErr, e );
1494 if (rc != LDAP_SUCCESS) {
1496 rs->sr_text = "Password fails quality checking policy";
1497 goto return_results;
1503 * Last check - the password history.
1505 /* FIXME: no access checking? */
1506 if (slap_passwd_check( op, NULL, pa, bv, &txt ) == LDAP_SUCCESS) {
1508 * This is bad - it means that the user is attempting
1509 * to set the password to the same as the old one.
1511 rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
1512 rs->sr_text = "Password is not being changed from existing value";
1513 pErr = PP_passwordInHistory;
1514 goto return_results;
1517 if (pp.pwdInHistory < 1) goto do_modify;
1520 * Iterate through the password history, and fail on any
1525 cr[1].bv_val = NULL;
1526 for(p=tl; p; p=p->next) {
1528 /* FIXME: no access checking? */
1529 rc = slap_passwd_check( op, NULL, &at, bv, &txt );
1531 if (rc != LDAP_SUCCESS) continue;
1533 rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
1534 rs->sr_text = "Password is in history of old passwords";
1535 pErr = PP_passwordInHistory;
1536 goto return_results;
1542 struct berval timestamp;
1543 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
1544 time_t now = slap_get_time();
1547 * keep the necessary pwd.. operational attributes
1551 timestamp.bv_val = timebuf;
1552 timestamp.bv_len = sizeof(timebuf);
1553 slap_timestamp( &now, ×tamp );
1555 mods = (Modifications *) ch_malloc( sizeof( Modifications ) );
1556 mods->sml_type.bv_val = NULL;
1557 mods->sml_desc = ad_pwdChangedTime;
1558 if (pwmop != LDAP_MOD_DELETE) {
1559 mods->sml_op = LDAP_MOD_REPLACE;
1560 mods->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1561 ber_dupbv( &mods->sml_values[0], ×tamp );
1562 mods->sml_values[1].bv_len = 0;
1563 mods->sml_values[1].bv_val = NULL;
1564 assert( mods->sml_values[0].bv_val != NULL );
1566 mods->sml_op = LDAP_MOD_DELETE;
1567 mods->sml_values = NULL;
1569 mods->sml_flags = SLAP_MOD_INTERNAL;
1570 mods->sml_nvalues = NULL;
1571 mods->sml_next = NULL;
1572 modtail->sml_next = mods;
1575 if (attr_find(e->e_attrs, ad_pwdGraceUseTime )) {
1576 mods = (Modifications *) ch_malloc( sizeof( Modifications ) );
1577 mods->sml_op = LDAP_MOD_DELETE;
1578 mods->sml_flags = SLAP_MOD_INTERNAL;
1579 mods->sml_type.bv_val = NULL;
1580 mods->sml_desc = ad_pwdGraceUseTime;
1581 mods->sml_values = NULL;
1582 mods->sml_nvalues = NULL;
1583 mods->sml_next = NULL;
1584 modtail->sml_next = mods;
1588 if (attr_find(e->e_attrs, ad_pwdAccountLockedTime )) {
1589 mods = (Modifications *) ch_malloc( sizeof( Modifications ) );
1590 mods->sml_op = LDAP_MOD_DELETE;
1591 mods->sml_flags = SLAP_MOD_INTERNAL;
1592 mods->sml_type.bv_val = NULL;
1593 mods->sml_desc = ad_pwdAccountLockedTime;
1594 mods->sml_values = NULL;
1595 mods->sml_nvalues = NULL;
1596 mods->sml_next = NULL;
1597 modtail->sml_next = mods;
1601 if (attr_find(e->e_attrs, ad_pwdFailureTime )) {
1602 mods = (Modifications *) ch_malloc( sizeof( Modifications ) );
1603 mods->sml_op = LDAP_MOD_DELETE;
1604 mods->sml_flags = SLAP_MOD_INTERNAL;
1605 mods->sml_type.bv_val = NULL;
1606 mods->sml_desc = ad_pwdFailureTime;
1607 mods->sml_values = NULL;
1608 mods->sml_nvalues = NULL;
1609 mods->sml_next = NULL;
1610 modtail->sml_next = mods;
1614 /* Delete the pwdReset attribute, since it's being reset */
1615 if ((zapReset) && (attr_find(e->e_attrs, ad_pwdReset ))) {
1616 mods = (Modifications *) ch_malloc( sizeof( Modifications ) );
1617 mods->sml_op = LDAP_MOD_DELETE;
1618 mods->sml_flags = SLAP_MOD_INTERNAL;
1619 mods->sml_type.bv_val = NULL;
1620 mods->sml_desc = ad_pwdReset;
1621 mods->sml_values = NULL;
1622 mods->sml_nvalues = NULL;
1623 mods->sml_next = NULL;
1624 modtail->sml_next = mods;
1628 if (pp.pwdInHistory > 0) {
1629 if (hsize >= pp.pwdInHistory) {
1631 * We use the >= operator, since we are going to add
1632 * the existing password attribute value into the
1633 * history - thus the cardinality of history values is
1634 * about to rise by one.
1636 * If this would push it over the limit of history
1637 * values (remembering - the password policy could have
1638 * changed since the password was last altered), we must
1639 * delete at least 1 value from the pwdHistory list.
1641 * In fact, we delete '(#pwdHistory attrs - max pwd
1642 * history length) + 1' values, starting with the oldest.
1643 * This is easily evaluated, since the linked list is
1644 * created in ascending time order.
1646 mods = (Modifications *) ch_malloc( sizeof( Modifications ) );
1647 mods->sml_op = LDAP_MOD_DELETE;
1648 mods->sml_flags = SLAP_MOD_INTERNAL;
1649 mods->sml_type.bv_val = NULL;
1650 mods->sml_desc = ad_pwdHistory;
1651 mods->sml_nvalues = NULL;
1652 mods->sml_values = ch_calloc( sizeof( struct berval ),
1653 hsize - pp.pwdInHistory + 2 );
1654 mods->sml_values[ hsize - pp.pwdInHistory + 1 ].bv_val = NULL;
1655 mods->sml_values[ hsize - pp.pwdInHistory + 1 ].bv_len = 0;
1656 for(i=0,p=tl; i < (hsize - pp.pwdInHistory + 1); i++, p=p->next) {
1657 mods->sml_values[i].bv_val = NULL;
1658 mods->sml_values[i].bv_len = 0;
1659 ber_dupbv( &(mods->sml_values[i]), &p->bv );
1661 mods->sml_next = NULL;
1662 modtail->sml_next = mods;
1665 free_pwd_history_list( &tl );
1668 * Now add the existing password into the history list.
1669 * This will be executed even if the operation is to delete
1670 * the password entirely.
1672 * This isn't in the spec explicitly, but it seems to make
1673 * sense that the password history list is the list of all
1674 * previous passwords - even if they were deleted. Thus, if
1675 * someone tries to add a historical password at some future
1676 * point, it will fail.
1678 if ((pa = attr_find( e->e_attrs, pp.ad )) != NULL) {
1679 mods = (Modifications *) ch_malloc( sizeof( Modifications ) );
1680 mods->sml_op = LDAP_MOD_ADD;
1681 mods->sml_flags = SLAP_MOD_INTERNAL;
1682 mods->sml_type.bv_val = NULL;
1683 mods->sml_desc = ad_pwdHistory;
1684 mods->sml_nvalues = NULL;
1685 mods->sml_values = ch_calloc( sizeof( struct berval ), 2 );
1686 mods->sml_values[ 1 ].bv_val = NULL;
1687 mods->sml_values[ 1 ].bv_len = 0;
1688 make_pwd_history_value( timebuf, &mods->sml_values[0], pa );
1689 mods->sml_next = NULL;
1690 modtail->sml_next = mods;
1693 Debug( LDAP_DEBUG_TRACE,
1694 "ppolicy_modify: password attr lookup failed\n", 0, 0, 0 );
1699 * Controversial bit here. If the new password isn't hashed
1700 * (ie, is cleartext), we probably should hash it according
1701 * to the default hash. The reason for this is that we want
1702 * to use the policy if possible, but if we hash the password
1703 * before, then we're going to run into trouble when it
1704 * comes time to check the password.
1706 * Now, the right thing to do is to use the extended password
1707 * modify operation, but not all software can do this,
1708 * therefore it makes sense to hash the new password, now
1709 * we know it passes the policy requirements.
1711 * Of course, if the password is already hashed, then we
1715 if ((pi->hash_passwords) && (addmod) && !newpw.bv_val &&
1716 (password_scheme( &(addmod->sml_values[0]), NULL ) != LDAP_SUCCESS)) {
1717 struct berval hpw, bv;
1719 slap_passwd_hash( &(addmod->sml_values[0]), &hpw, &txt );
1720 if (hpw.bv_val == NULL) {
1722 * hashing didn't work. Emit an error.
1724 rs->sr_err = LDAP_OTHER;
1726 goto return_results;
1728 bv.bv_val = addmod->sml_values[0].bv_val;
1729 bv.bv_len = addmod->sml_values[0].bv_len;
1730 /* clear and discard the clear password */
1731 memset(bv.bv_val, 0, bv.bv_len);
1732 ber_memfree(bv.bv_val);
1733 addmod->sml_values[0].bv_val = hpw.bv_val;
1734 addmod->sml_values[0].bv_len = hpw.bv_len;
1737 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1738 be_entry_release_r( op, e );
1739 return SLAP_CB_CONTINUE;
1742 free_pwd_history_list( &tl );
1743 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1744 be_entry_release_r( op, e );
1746 LDAPControl **ctrls = NULL;
1748 ctrls = ch_calloc( sizeof( LDAPControl *) , 2 );
1749 ctrls[0] = create_passcontrol( -1, -1, pErr );
1751 rs->sr_ctrls = ctrls;
1753 send_ldap_result( op, rs );
1763 if ( ctrl->ldctl_value.bv_len ) {
1764 rs->sr_text = "passwordPolicyRequest control value not empty";
1765 return LDAP_PROTOCOL_ERROR;
1767 if ( ctrl->ldctl_iscritical ) {
1768 rs->sr_text = "passwordPolicyRequest control invalid criticality";
1769 return LDAP_PROTOCOL_ERROR;
1771 op->o_ctrlflag[ppolicy_cid] = SLAP_CONTROL_NONCRITICAL;
1773 return LDAP_SUCCESS;
1783 AttributeDescription *ad = NULL;
1787 code = slap_bv2ad( val, &ad, &err );
1789 ber_dupbv_x( out, &ad->ad_type->sat_cname, ctx );
1803 AttributeDescription *ad = NULL;
1807 code = slap_bv2ad( val, &ad, &err );
1809 ber_str2bv_x( ad->ad_type->sat_oid, 0, 1, out, ctx );
1819 slap_overinst *on = (slap_overinst *) be->bd_info;
1821 /* Has User Schema been initialized yet? */
1822 if ( !pwd_UsSchema[0].ad[0] ) {
1826 for (i=0; pwd_UsSchema[i].def; i++) {
1827 code = slap_str2ad( pwd_UsSchema[i].def, pwd_UsSchema[i].ad, &err );
1829 fprintf( stderr, "User Schema Load failed %d: %s\n", code, err );
1837 syn = ch_malloc( sizeof( Syntax ));
1838 *syn = *ad_pwdAttribute->ad_type->sat_syntax;
1839 syn->ssyn_pretty = attrPretty;
1840 ad_pwdAttribute->ad_type->sat_syntax = syn;
1842 mr = ch_malloc( sizeof( MatchingRule ));
1843 *mr = *ad_pwdAttribute->ad_type->sat_equality;
1844 mr->smr_normalize = attrNormalize;
1845 ad_pwdAttribute->ad_type->sat_equality = mr;
1849 on->on_bi.bi_private = ch_calloc( sizeof(pp_info), 1 );
1851 if ( dtblsize && !pwcons )
1852 pwcons = ch_calloc(sizeof(pw_conn), dtblsize );
1862 return overlay_register_control( be, LDAP_CONTROL_PASSWORDPOLICYREQUEST );
1870 slap_overinst *on = (slap_overinst *) be->bd_info;
1871 pp_info *pi = on->on_bi.bi_private;
1874 free( pi->def_policy.bv_val );
1889 slap_overinst *on = (slap_overinst *) be->bd_info;
1890 pp_info *pi = on->on_bi.bi_private;
1894 if ( strcasecmp( argv[0], "ppolicy_default" ) == 0 ) {
1896 fprintf( stderr, "%s: line %d: invalid arguments in \"ppolicy_default"
1897 " <policyDN>\n", fname, lineno );
1900 ber_str2bv( argv[1], 0, 0, &dn );
1901 if ( dnNormalize( 0, NULL, NULL, &dn, &pi->def_policy, NULL ) ) {
1902 fprintf( stderr, "%s: line %d: policyDN is invalid\n",
1908 } else if ( strcasecmp( argv[0], "ppolicy_use_lockout" ) == 0 ) {
1910 fprintf( stderr, "%s: line %d: ppolicy_use_lockout "
1911 "takes no arguments\n", fname, lineno );
1914 pi->use_lockout = 1;
1916 } else if ( strcasecmp( argv[0], "ppolicy_hash_cleartext" ) == 0 ) {
1918 fprintf( stderr, "%s: line %d: ppolicy_hash_cleartext "
1919 "takes no arguments\n", fname, lineno );
1922 pi->hash_passwords = 1;
1925 return SLAP_CONF_UNKNOWN;
1928 static char *extops[] = {
1929 LDAP_EXOP_MODIFY_PASSWD,
1933 static slap_overinst ppolicy;
1937 LDAPAttributeType *at;
1941 for (i=0; pwd_OpSchema[i].def; i++) {
1942 at = ldap_str2attributetype( pwd_OpSchema[i].def, &code, &err,
1943 LDAP_SCHEMA_ALLOW_ALL );
1945 fprintf( stderr, "AttributeType Load failed %s %s\n",
1946 ldap_scherr2str(code), err );
1949 code = at_add( at, 0, NULL, &err );
1951 slap_str2ad( at->at_names[0], pwd_OpSchema[i].ad, &err );
1955 fprintf( stderr, "AttributeType Load failed %s %s\n",
1956 scherr2str(code), err );
1959 /* Allow Manager to set these as needed */
1960 if ( is_at_no_user_mod( (*pwd_OpSchema[i].ad)->ad_type )) {
1961 (*pwd_OpSchema[i].ad)->ad_type->sat_flags |=
1966 code = register_supported_control( LDAP_CONTROL_PASSWORDPOLICYREQUEST,
1967 SLAP_CTRL_ADD|SLAP_CTRL_BIND|SLAP_CTRL_MODIFY|SLAP_CTRL_HIDE, extops,
1968 ppolicy_parseCtrl, &ppolicy_cid );
1969 if ( code != LDAP_SUCCESS ) {
1970 fprintf( stderr, "Failed to register control %d\n", code );
1974 ldap_pvt_thread_mutex_init( &chk_syntax_mutex );
1976 ppolicy.on_bi.bi_type = "ppolicy";
1977 ppolicy.on_bi.bi_db_init = ppolicy_db_init;
1978 ppolicy.on_bi.bi_db_open = ppolicy_db_open;
1979 ppolicy.on_bi.bi_db_config = ppolicy_config;
1980 ppolicy.on_bi.bi_db_close = ppolicy_close;
1982 ppolicy.on_bi.bi_op_add = ppolicy_add;
1983 ppolicy.on_bi.bi_op_bind = ppolicy_bind;
1984 ppolicy.on_bi.bi_op_compare = ppolicy_restrict;
1985 ppolicy.on_bi.bi_op_delete = ppolicy_restrict;
1986 ppolicy.on_bi.bi_op_modify = ppolicy_modify;
1987 ppolicy.on_bi.bi_op_search = ppolicy_restrict;
1988 ppolicy.on_bi.bi_connection_destroy = ppolicy_connection_destroy;
1990 return overlay_register( &ppolicy );
1993 #if SLAPD_OVER_PPOLICY == SLAPD_MOD_DYNAMIC
1994 int init_module(int argc, char *argv[]) {
1995 return ppolicy_init();
1999 #endif /* defined(SLAPD_OVER_PPOLICY) */