2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 2004-2005 The OpenLDAP Foundation.
5 * Portions Copyright 2004 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-08
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 USAGE directoryOperation NO-USER-MODIFICATION )",
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 "
122 "SINGLE-VALUE USAGE directoryOperation )",
123 &ad_pwdAccountLockedTime },
124 { "( 1.3.6.1.4.1.42.2.27.8.1.19 "
125 "NAME ( 'pwdFailureTime' ) "
126 "DESC 'The timestamps of the last consecutive authentication failures' "
127 "EQUALITY generalizedTimeMatch "
128 "ORDERING generalizedTimeOrderingMatch "
129 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
130 "USAGE directoryOperation )",
131 &ad_pwdFailureTime },
132 { "( 1.3.6.1.4.1.42.2.27.8.1.20 "
133 "NAME ( 'pwdHistory' ) "
134 "DESC 'The history of users passwords' "
135 "EQUALITY octetStringMatch "
136 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.40 "
137 "USAGE directoryOperation NO-USER-MODIFICATION )",
139 { "( 1.3.6.1.4.1.42.2.27.8.1.21 "
140 "NAME ( 'pwdGraceUseTime' ) "
141 "DESC 'The timestamps of the grace login once the password has expired' "
142 "EQUALITY generalizedTimeMatch "
143 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
144 "USAGE directoryOperation NO-USER-MODIFICATION )",
145 &ad_pwdGraceUseTime },
146 { "( 1.3.6.1.4.1.42.2.27.8.1.22 "
147 "NAME ( 'pwdReset' ) "
148 "DESC 'The indication that the password has been reset' "
149 "EQUALITY booleanMatch "
150 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.7 "
151 "SINGLE-VALUE USAGE directoryOperation )",
153 { "( 1.3.6.1.4.1.42.2.27.8.1.23 "
154 "NAME ( 'pwdPolicySubentry' ) "
155 "DESC 'The pwdPolicy subentry in effect for this object' "
156 "EQUALITY distinguishedNameMatch "
157 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.12 "
158 "SINGLE-VALUE USAGE directoryOperation )",
159 &ad_pwdPolicySubentry },
163 /* User attributes */
164 static AttributeDescription *ad_pwdMinAge, *ad_pwdMaxAge, *ad_pwdInHistory,
165 *ad_pwdCheckQuality, *ad_pwdMinLength, *ad_pwdMaxFailure,
166 *ad_pwdGraceAuthNLimit, *ad_pwdExpireWarning, *ad_pwdLockoutDuration,
167 *ad_pwdFailureCountInterval, *ad_pwdCheckModule, *ad_pwdLockout,
168 *ad_pwdMustChange, *ad_pwdAllowUserChange, *ad_pwdSafeModify,
171 #define TAB(name) { #name, &ad_##name }
173 static struct schema_info pwd_UsSchema[] = {
178 TAB(pwdCheckQuality),
181 TAB(pwdGraceAuthNLimit),
182 TAB(pwdExpireWarning),
184 TAB(pwdLockoutDuration),
185 TAB(pwdFailureCountInterval),
188 TAB(pwdAllowUserChange),
193 static ldap_pvt_thread_mutex_t chk_syntax_mutex;
196 parse_time( char *atm )
199 struct lutil_timet tt;
200 time_t ret = (time_t)-1;
202 if ( lutil_parsetime( atm, &tm ) == 0) {
203 lutil_tm2time( &tm, &tt );
210 account_locked( Operation *op, Entry *e,
211 PassPolicy *pp, Modifications **mod )
219 if ( (la = attr_find( e->e_attrs, ad_pwdAccountLockedTime )) != NULL ) {
220 BerVarray vals = la->a_nvals;
223 * there is a lockout stamp - we now need to know if it's
226 if (vals[0].bv_val != NULL) {
231 if ((then = parse_time( vals[0].bv_val )) == (time_t)0)
234 now = slap_get_time();
236 if (now < then + pp->pwdLockoutDuration)
239 m = ch_calloc( sizeof(Modifications), 1 );
240 m->sml_op = LDAP_MOD_DELETE;
242 m->sml_type = ad_pwdAccountLockedTime->ad_cname;
243 m->sml_desc = ad_pwdAccountLockedTime;
252 #define PPOLICY_WARNING 0xa0L
253 #define PPOLICY_ERROR 0xa1L
255 #define PPOLICY_EXPIRE 0xa0L
256 #define PPOLICY_GRACE 0xa1L
259 create_passcontrol( int exptime, int grace, LDAPPasswordPolicyError err )
261 char berbuf[LBER_ELEMENT_SIZEOF], bb2[LBER_ELEMENT_SIZEOF];
262 BerElement *ber = (BerElement *)berbuf, *b2 = (BerElement *)bb2;
266 if ((c = ch_calloc( sizeof( LDAPControl ), 1 )) == NULL) return NULL;
267 c->ldctl_oid = LDAP_CONTROL_PASSWORDPOLICYRESPONSE;
268 c->ldctl_iscritical = 0;
269 c->ldctl_value.bv_val = NULL;
270 c->ldctl_value.bv_len = 0;
272 ber_init2( ber, NULL, LBER_USE_DER );
273 ber_printf(ber, "{" /*}*/ );
276 ber_init2( b2, NULL, LBER_USE_DER );
277 ber_printf( b2, "ti", PPOLICY_EXPIRE, exptime );
278 ber_flatten2( b2, &bv, 1 );
279 ber_printf( ber, "tO", PPOLICY_WARNING, &bv );
280 ch_free( bv.bv_val );
281 } else if (grace > 0) {
282 ber_init2( b2, NULL, LBER_USE_DER );
283 ber_printf( b2, "ti", PPOLICY_GRACE, grace );
284 ber_flatten2( b2, &bv, 1 );
285 ber_printf( ber, "tO", PPOLICY_WARNING, &bv );
286 ch_free( bv.bv_val );
289 if (err != PP_noError ) {
290 ber_printf( ber, "te", PPOLICY_ERROR, err );
292 ber_printf( ber, /*{*/ "N}" );
294 if (ber_flatten2( ber, &(c->ldctl_value), 1 ) == LBER_DEFAULT) {
296 (void)ber_free_buf(ber);
299 (void)ber_free_buf(ber);
304 ppolicy_get( Operation *op, Entry *e, PassPolicy *pp )
306 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
307 pp_info *pi = on->on_bi.bi_private;
312 AttributeDescription *oca = slap_schema.si_ad_objectClass;
314 AttributeDescription *ad;
317 memset( pp, 0, sizeof(PassPolicy) );
319 /* Users can change their own password by default */
320 pp->pwdAllowUserChange = 1;
322 if ((a = attr_find( e->e_attrs, ad_pwdPolicySubentry )) == NULL) {
324 * entry has no password policy assigned - use default
326 vals = &pi->def_policy;
331 if (vals[0].bv_val == NULL) {
332 Debug( LDAP_DEBUG_ANY,
333 "ppolicy_get: NULL value for policySubEntry\n", 0, 0, 0 );
338 op->o_bd->bd_info = (BackendInfo *)on->on_info;
339 rc = be_entry_get_rw( op, vals, NULL, NULL, 0, &pe );
340 op->o_bd->bd_info = (BackendInfo *)on;
342 if ( rc ) goto defaultpol;
344 #if 0 /* Only worry about userPassword for now */
345 if ((a = attr_find( pe->e_attrs, ad_pwdAttribute )))
346 slap_bv2ad( &a->a_vals[0], &pp->ad, &text );
348 pp->ad = slap_schema.si_ad_userPassword;
351 if ((a = attr_find( pe->e_attrs, ad_pwdMinAge )))
352 pp->pwdMinAge = atoi(a->a_vals[0].bv_val );
353 if ((a = attr_find( pe->e_attrs, ad_pwdMaxAge )))
354 pp->pwdMaxAge = atoi(a->a_vals[0].bv_val );
355 if ((a = attr_find( pe->e_attrs, ad_pwdInHistory )))
356 pp->pwdInHistory = atoi(a->a_vals[0].bv_val );
357 if ((a = attr_find( pe->e_attrs, ad_pwdCheckQuality )))
358 pp->pwdCheckQuality = atoi(a->a_vals[0].bv_val );
359 if ((a = attr_find( pe->e_attrs, ad_pwdMinLength )))
360 pp->pwdMinLength = atoi(a->a_vals[0].bv_val );
361 if ((a = attr_find( pe->e_attrs, ad_pwdMaxFailure )))
362 pp->pwdMaxFailure = atoi(a->a_vals[0].bv_val );
363 if ((a = attr_find( pe->e_attrs, ad_pwdGraceAuthNLimit )))
364 pp->pwdGraceAuthNLimit = atoi(a->a_vals[0].bv_val );
365 if ((a = attr_find( pe->e_attrs, ad_pwdExpireWarning )))
366 pp->pwdExpireWarning = atoi(a->a_vals[0].bv_val );
367 if ((a = attr_find( pe->e_attrs, ad_pwdFailureCountInterval )))
368 pp->pwdFailureCountInterval = atoi(a->a_vals[0].bv_val );
369 if ((a = attr_find( pe->e_attrs, ad_pwdLockoutDuration )))
370 pp->pwdLockoutDuration = atoi(a->a_vals[0].bv_val );
372 if ((a = attr_find( pe->e_attrs, ad_pwdCheckModule ))) {
373 strncpy(pp->pwdCheckModule, a->a_vals[0].bv_val,
374 sizeof(pp->pwdCheckModule));
375 pp->pwdCheckModule[sizeof(pp->pwdCheckModule)-1] = '\0';
378 if ((a = attr_find( pe->e_attrs, ad_pwdLockout )))
379 pp->pwdLockout = !strcmp( a->a_nvals[0].bv_val, "TRUE" );
380 if ((a = attr_find( pe->e_attrs, ad_pwdMustChange )))
381 pp->pwdMustChange = !strcmp( a->a_nvals[0].bv_val, "TRUE" );
382 if ((a = attr_find( pe->e_attrs, ad_pwdAllowUserChange )))
383 pp->pwdAllowUserChange = !strcmp( a->a_nvals[0].bv_val, "TRUE" );
384 if ((a = attr_find( pe->e_attrs, ad_pwdSafeModify )))
385 pp->pwdSafeModify = !strcmp( a->a_nvals[0].bv_val, "TRUE" );
387 op->o_bd->bd_info = (BackendInfo *)on->on_info;
388 be_entry_release_r( op, pe );
389 op->o_bd->bd_info = (BackendInfo *)on;
394 Debug( LDAP_DEBUG_ANY,
395 "ppolicy_get: using default policy\n", 0, 0, 0 );
400 password_scheme( struct berval *cred, struct berval *sch )
404 assert( cred != NULL );
411 if ((cred->bv_len == 0) || (cred->bv_val == NULL) ||
412 (cred->bv_val[0] != '{')) return LDAP_OTHER;
414 for(e = 1; cred->bv_val[e] && cred->bv_val[e] != '}'; e++);
415 if (cred->bv_val[e]) {
417 char *sc = ch_calloc( sizeof(char), e + 2);
418 sc[e + 1] = '\0'; /* terminate string */
419 strncpy( sc, cred->bv_val, e + 1);
420 rc = lutil_passwd_scheme( sc );
423 sch->bv_val = cred->bv_val;
432 check_password_quality( struct berval *cred, PassPolicy *pp, LDAPPasswordPolicyError *err, Entry *e )
434 int rc = LDAP_SUCCESS, ok = LDAP_SUCCESS;
435 char *ptr = cred->bv_val;
439 assert( cred != NULL );
440 assert( pp != NULL );
442 if ((cred->bv_len == 0) || (pp->pwdMinLength > cred->bv_len)) {
443 rc = LDAP_CONSTRAINT_VIOLATION;
444 if ( err ) *err = PP_passwordTooShort;
449 * We need to know if the password is already hashed - if so
450 * what scheme is it. The reason being that the "hash" of
451 * {cleartext} still allows us to check the password.
453 rc = password_scheme( cred, &sch );
454 if (rc == LDAP_SUCCESS) {
455 if ((sch.bv_val) && (strncasecmp( sch.bv_val, "{cleartext}",
456 sch.bv_len ) == 0)) {
458 * We can check the cleartext "hash"
460 ptr = cred->bv_val + sch.bv_len;
462 /* everything else, we can't check */
463 if (pp->pwdCheckQuality == 2) {
464 rc = LDAP_CONSTRAINT_VIOLATION;
465 if (err) *err = PP_insufficientPasswordQuality;
469 * We can't check the syntax of the password, but it's not
470 * mandatory (according to the policy), so we return success.
479 if (pp->pwdCheckModule[0]) {
484 if ((mod = lt_dlopen( pp->pwdCheckModule )) == NULL) {
487 Debug(LDAP_DEBUG_ANY,
488 "check_password_quality: lt_dlopen failed: (%s) %s.\n",
489 pp->pwdCheckModule, err, 0 );
490 ok = LDAP_OTHER; /* internal error */
492 int (*prog)( char *passwd, char **text, Entry *ent );
494 if ((prog = lt_dlsym( mod, "check_password" )) == NULL) {
497 Debug(LDAP_DEBUG_ANY,
498 "check_password_quality: lt_dlsym failed: (%s) %s.\n",
499 pp->pwdCheckModule, err, 0 );
504 ldap_pvt_thread_mutex_lock( &chk_syntax_mutex );
505 ok = prog( cred->bv_val, &txt, e );
506 ldap_pvt_thread_mutex_unlock( &chk_syntax_mutex );
508 Debug(LDAP_DEBUG_ANY,
509 "check_password_quality: module error: (%s) %s.[%d]\n",
510 pp->pwdCheckModule, txt, ok );
519 Debug(LDAP_DEBUG_ANY, "check_password_quality: external modules not "
520 "supported. pwdCheckModule ignored.\n", 0, 0, 0);
521 #endif /* SLAPD_MODULES */
525 if (ok != LDAP_SUCCESS) {
526 rc = LDAP_CONSTRAINT_VIOLATION;
527 if (err) *err = PP_insufficientPasswordQuality;
534 parse_pwdhistory( struct berval *bv, char **oid, time_t *oldtime, struct berval *oldpw )
537 struct berval nv, npw;
540 assert (bv && (bv->bv_len > 0) && (bv->bv_val) && oldtime && oldpw );
543 *oldtime = (time_t)-1;
544 oldpw->bv_val = NULL;
547 ber_dupbv( &nv, bv );
549 /* first get the time field */
550 for(i=0; (i < nv.bv_len) && (nv.bv_val[i] != '#'); i++);
551 if ( i == nv.bv_len) goto exit_failure; /* couldn't locate the '#' separator */
552 nv.bv_val[i++] = '\0'; /* terminate the string & move to next field */
554 *oldtime = parse_time( ptr );
555 if (*oldtime == (time_t)-1) goto exit_failure;
557 /* get the OID field */
558 for(ptr = &(nv.bv_val[i]);(i < nv.bv_len) && (nv.bv_val[i] != '#'); i++);
559 if ( i == nv.bv_len) goto exit_failure; /* couldn't locate the '#' separator */
560 nv.bv_val[i++] = '\0'; /* terminate the string & move to next field */
561 if ( oid ) *oid = ber_strdup( ptr );
563 /* get the length field */
564 for(ptr = &(nv.bv_val[i]);(i < nv.bv_len) && (nv.bv_val[i] != '#'); i++);
565 if ( i == nv.bv_len) goto exit_failure; /* couldn't locate the '#' separator */
566 nv.bv_val[i++] = '\0'; /* terminate the string & move to next field */
567 oldpw->bv_len = strtol( ptr, NULL, 10 );
568 if (errno == ERANGE) goto exit_failure;
570 /* lastly, get the octets of the string */
571 for(j=i, ptr = &(nv.bv_val[i]);i < nv.bv_len; i++);
572 if (i-j != oldpw->bv_len) goto exit_failure; /* length is wrong */
575 npw.bv_len = oldpw->bv_len;
576 ber_dupbv( oldpw, &npw );
580 if (oid && *oid) { ber_memfree(*oid); *oid = NULL; }
582 ber_memfree( oldpw->bv_val); oldpw->bv_val = NULL;
585 ber_memfree(nv.bv_val);
590 add_to_pwd_history( pw_hist **l, time_t t,
591 struct berval *oldpw, struct berval *bv )
593 pw_hist *p, *p1, *p2;
597 p = ch_malloc( sizeof( pw_hist ));
599 ber_dupbv( &p->bv, bv );
604 /* degenerate case */
609 * advance p1 and p2 such that p1 is the node before the
610 * new one, and p2 is the node after it
612 for (p1 = NULL, p2 = *l; p2 && p2->t <= t; p1 = p2, p2=p2->next );
614 if (p1 == NULL) { *l = p; return; }
618 #ifndef MAX_PWD_HISTORY_SZ
619 #define MAX_PWD_HISTORY_SZ 1024
620 #endif /* MAX_PWD_HISTORY_SZ */
623 make_pwd_history_value( char *timebuf, struct berval *bv, Attribute *pa )
625 char str[ MAX_PWD_HISTORY_SZ ];
628 snprintf( str, MAX_PWD_HISTORY_SZ,
629 "%s#%s#%d#", timebuf,
630 pa->a_desc->ad_type->sat_syntax->ssyn_oid,
631 pa->a_nvals[0].bv_len );
632 str[MAX_PWD_HISTORY_SZ-1] = 0;
636 * We have to assume that the string is a string of octets,
637 * not readable characters. In reality, yes, it probably is
638 * a readable (ie, base64) string, but we can't count on that
639 * Hence, while the first 3 fields of the password history
640 * are definitely readable (a timestamp, an OID and an integer
641 * length), the remaining octets of the actual password
642 * are deemed to be binary data.
644 AC_MEMCPY( str + nlen, pa->a_nvals[0].bv_val, pa->a_nvals[0].bv_len );
645 nlen += pa->a_nvals[0].bv_len;
646 bv->bv_val = ch_malloc( nlen + 1 );
647 AC_MEMCPY( bv->bv_val, str, nlen );
648 bv->bv_val[nlen] = '\0';
653 free_pwd_history_list( pw_hist **l )
660 pw_hist *pp = p->next;
670 typedef struct ppbind {
674 LDAPPasswordPolicyError pErr;
679 ppolicy_bind_resp( Operation *op, SlapReply *rs )
681 ppbind *ppb = op->o_callback->sc_private;
682 slap_overinst *on = ppb->on;
683 Modifications *mod = ppb->mod, *m;
685 int ngut = -1, warn = -1, age, rc, i;
687 time_t now, then, pwtime = (time_t)-1;
689 char nowstr[ LDAP_LUTIL_GENTIME_BUFSIZE ];
690 struct berval timestamp;
691 BackendInfo *bi = op->o_bd->bd_info;
694 /* If we already know it's locked, just get on with it */
695 if ( ppb->pErr != PP_noError ) {
699 op->o_bd->bd_info = (BackendInfo *)on->on_info;
700 rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &e );
701 op->o_bd->bd_info = bi;
703 if ( rc != LDAP_SUCCESS ) {
704 return SLAP_CB_CONTINUE;
707 now = slap_get_time(); /* stored for later consideration */
708 timestamp.bv_val = nowstr;
709 timestamp.bv_len = sizeof(nowstr);
710 slap_timestamp( &now, ×tamp );
712 if ( rs->sr_err == LDAP_INVALID_CREDENTIALS ) {
715 m = ch_calloc( sizeof(Modifications), 1 );
716 m->sml_op = LDAP_MOD_ADD;
718 m->sml_type = ad_pwdFailureTime->ad_cname;
719 m->sml_desc = ad_pwdFailureTime;
720 m->sml_values = ch_calloc( sizeof(struct berval), 2 );
722 ber_dupbv( &m->sml_values[0], ×tamp );
727 * Count the pwdFailureTimes - if it's
728 * greater than the policy pwdMaxFailure,
729 * then lock the account.
731 if ((a = attr_find( e->e_attrs, ad_pwdFailureTime )) != NULL) {
732 for(i=0; a->a_nvals[i].bv_val; i++) {
735 * If the interval is 0, then failures
736 * stay on the record until explicitly
737 * reset by successful authentication.
739 if (ppb->pp.pwdFailureCountInterval == 0) {
742 parse_time(a->a_nvals[i].bv_val) +
743 ppb->pp.pwdFailureCountInterval) {
748 * We only count those failures
749 * which are not due to expire.
754 if ((ppb->pp.pwdMaxFailure > 0) &&
755 (fc >= ppb->pp.pwdMaxFailure - 1)) {
758 * We subtract 1 from the failure max
759 * because the new failure entry hasn't
760 * made it to the entry yet.
762 m = ch_calloc( sizeof(Modifications), 1 );
763 m->sml_op = LDAP_MOD_REPLACE;
765 m->sml_type = ad_pwdAccountLockedTime->ad_cname;
766 m->sml_desc = ad_pwdAccountLockedTime;
767 m->sml_values = ch_calloc( sizeof(struct berval), 2 );
768 ber_dupbv( &m->sml_values[0], ×tamp );
772 } else if ( rs->sr_err == LDAP_SUCCESS ) {
773 if ((a = attr_find( e->e_attrs, ad_pwdChangedTime )) != NULL)
774 pwtime = parse_time( a->a_nvals[0].bv_val );
776 /* delete all pwdFailureTimes */
777 if ( attr_find( e->e_attrs, ad_pwdFailureTime )) {
778 m = ch_calloc( sizeof(Modifications), 1 );
779 m->sml_op = LDAP_MOD_DELETE;
781 m->sml_type = ad_pwdFailureTime->ad_cname;
782 m->sml_desc = ad_pwdFailureTime;
788 * check to see if the password must be changed
790 if ( ppb->pp.pwdMustChange &&
791 (a = attr_find( e->e_attrs, ad_pwdReset )) &&
792 !strcmp( a->a_nvals[0].bv_val, "TRUE" ) ) {
794 * need to inject client controls here to give
795 * more information. For the moment, we ensure
796 * that we are disallowed from doing anything
797 * other than change password.
799 pwcons[op->o_conn->c_conn_idx].restricted = 1;
801 ppb->pErr = PP_changeAfterReset;
805 * the password does not need to be changed, so
806 * we now check whether the password has expired.
808 * We can skip this bit if passwords don't age in
811 if (ppb->pp.pwdMaxAge == 0) goto grace;
813 if (pwtime == (time_t)-1) {
815 * Hmm. No password changed time on the
816 * entry. This is odd - it should have
817 * been provided when the attribute was added.
819 * However, it's possible that it could be
820 * missing if the DIT was established via
823 Debug( LDAP_DEBUG_ANY,
824 "ppolicy_bind: Entry %s does not have valid pwdChangedTime attribute - assuming password expired\n",
825 e->e_name.bv_val, 0, 0);
830 * Check: was the last change time of
831 * the password older than the maximum age
832 * allowed. (Ignore case 2 from I-D, it's just silly.)
834 if (now - pwtime > ppb->pp.pwdMaxAge ) pwExpired = 1;
839 if (!pwExpired) goto check_expiring_password;
841 if ((a = attr_find( e->e_attrs, ad_pwdGraceUseTime )) == NULL)
842 ngut = ppb->pp.pwdGraceAuthNLimit;
844 for(ngut=0; a->a_nvals[ngut].bv_val; ngut++);
845 ngut = ppb->pp.pwdGraceAuthNLimit - ngut;
849 * ngut is the number of remaining grace logins
851 Debug( LDAP_DEBUG_ANY,
852 "ppolicy_bind: Entry %s has an expired password: %d grace logins\n",
853 e->e_name.bv_val, ngut, 0);
856 ppb->pErr = PP_passwordExpired;
857 rs->sr_err = LDAP_INVALID_CREDENTIALS;
862 * Add a grace user time to the entry
864 m = ch_calloc( sizeof(Modifications), 1 );
865 m->sml_op = LDAP_MOD_ADD;
867 m->sml_type = ad_pwdGraceUseTime->ad_cname;
868 m->sml_desc = ad_pwdGraceUseTime;
869 m->sml_values = ch_calloc( sizeof(struct berval), 2 );
870 ber_dupbv( &m->sml_values[0], ×tamp );
874 check_expiring_password:
876 * Now we need to check to see
877 * if it is about to expire, and if so, should the user
878 * be warned about it in the password policy control.
880 * If the password has expired, and we're in the grace period, then
881 * we don't need to do this bit. Similarly, if we don't have password
882 * aging, then there's no need to do this bit either.
884 if ((ppb->pp.pwdMaxAge < 1) || (pwExpired) || (ppb->pp.pwdExpireWarning < 1))
887 age = (int)(now - pwtime);
890 * We know that there is a password Change Time attribute - if
891 * there wasn't, then the pwdExpired value would be true, unless
892 * there is no password aging - and if there is no password aging,
893 * then this section isn't called anyway - you can't have an
894 * expiring password if there's no limit to expire.
896 if (ppb->pp.pwdMaxAge - age < ppb->pp.pwdExpireWarning ) {
898 * Set the warning value.
900 warn = ppb->pp.pwdMaxAge - age; /* seconds left until expiry */
901 if (warn < 0) warn = 0; /* something weird here - why is pwExpired not set? */
903 Debug( LDAP_DEBUG_ANY,
904 "ppolicy_bind: Setting warning for password expiry for %s = %d seconds\n",
905 op->o_req_dn.bv_val, warn, 0 );
910 op->o_bd->bd_info = (BackendInfo *)on->on_info;
911 be_entry_release_r( op, e );
916 SlapReply r2 = { REP_RESULT };
917 slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
919 /* FIXME: Need to handle replication of some (but not all)
920 * of the operational attributes...
922 op2.o_tag = LDAP_REQ_MODIFY;
923 op2.o_callback = &cb;
924 op2.orm_modlist = mod;
925 op2.o_dn = op->o_bd->be_rootdn;
926 op2.o_ndn = op->o_bd->be_rootndn;
927 op2.o_bd->bd_info = (BackendInfo *)on->on_info;
928 rc = op->o_bd->be_modify( &op2, &r2 );
929 slap_mods_free( mod );
932 if ( ppb->send_ctrl ) {
933 LDAPControl **ctrls = NULL;
934 pp_info *pi = on->on_bi.bi_private;
936 /* Do we really want to tell that the account is locked? */
937 if ( ppb->pErr == PP_accountLocked && !pi->use_lockout ) {
938 ppb->pErr = PP_noError;
940 ctrls = ch_calloc( sizeof( LDAPControl *) , 2 );
941 ctrls[0] = create_passcontrol( warn, ngut, ppb->pErr );
943 rs->sr_ctrls = ctrls;
945 op->o_bd->bd_info = bi;
946 return SLAP_CB_CONTINUE;
950 ppolicy_bind( Operation *op, SlapReply *rs )
952 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
954 /* Root bypasses policy */
955 if ( !be_isroot_dn( op->o_bd, &op->o_req_ndn )) {
961 op->o_bd->bd_info = (BackendInfo *)on->on_info;
962 rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &e );
964 if ( rc != LDAP_SUCCESS ) {
965 return SLAP_CB_CONTINUE;
968 cb = op->o_tmpcalloc( sizeof(ppbind)+sizeof(slap_callback),
969 1, op->o_tmpmemctx );
970 ppb = (ppbind *)(cb+1);
972 ppb->pErr = PP_noError;
974 /* Setup a callback so we can munge the result */
976 cb->sc_response = ppolicy_bind_resp;
977 cb->sc_next = op->o_callback->sc_next;
978 cb->sc_private = ppb;
979 op->o_callback->sc_next = cb;
981 /* Did we receive a password policy request control? */
982 if ( op->o_ctrlflag[ppolicy_cid] ) {
986 op->o_bd->bd_info = (BackendInfo *)on;
987 ppolicy_get( op, e, &ppb->pp );
989 rc = account_locked( op, e, &ppb->pp, &ppb->mod );
991 op->o_bd->bd_info = (BackendInfo *)on->on_info;
992 be_entry_release_r( op, e );
995 /* This will be the Draft 8 response, Unwilling is bogus */
996 ppb->pErr = PP_accountLocked;
997 send_ldap_error( op, rs, LDAP_INVALID_CREDENTIALS, NULL );
1003 return SLAP_CB_CONTINUE;
1006 /* Reset the restricted flag for the next session on this connection */
1008 ppolicy_unbind( Operation *op, SlapReply *rs )
1010 pwcons[op->o_conn->c_conn_idx].restricted = 0;
1011 return SLAP_CB_CONTINUE;
1014 /* Check if this connection is restricted */
1020 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1021 int i, send_ctrl = 0;
1023 /* Did we receive a password policy request control? */
1024 if ( op->o_ctrlflag[ppolicy_cid] ) {
1028 if ( op->o_conn && pwcons[op->o_conn->c_conn_idx].restricted ) {
1029 Debug( LDAP_DEBUG_TRACE,
1030 "connection restricted to password changing only\n", 0, 0, 0);
1032 LDAPControl **ctrls = NULL;
1034 ctrls = ch_calloc( sizeof( LDAPControl *) , 2 );
1035 ctrls[0] = create_passcontrol( -1, -1, PP_changeAfterReset );
1037 rs->sr_ctrls = ctrls;
1039 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1040 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
1041 "Operations are restricted to bind/unbind/abandon/StartTLS/modify password" );
1045 return SLAP_CB_CONTINUE;
1053 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1054 pp_info *pi = on->on_bi.bi_private;
1060 if ( ppolicy_restrict( op, rs ) != SLAP_CB_CONTINUE )
1063 /* Check for password in entry */
1064 if ((pa = attr_find( op->oq_add.rs_e->e_attrs,
1065 slap_schema.si_ad_userPassword )))
1068 * new entry contains a password - if we're not the root user
1069 * then we need to check that the password fits in with the
1070 * security policy for the new entry.
1072 ppolicy_get( op, op->ora_e, &pp );
1073 if (pp.pwdCheckQuality > 0 && !be_isroot( op )) {
1074 struct berval *bv = &(pa->a_vals[0]);
1075 int rc, i, send_ctrl = 0;
1076 LDAPPasswordPolicyError pErr = PP_noError;
1078 /* Did we receive a password policy request control? */
1079 if ( op->o_ctrlflag[ppolicy_cid] ) {
1082 rc = check_password_quality( bv, &pp, &pErr, op->ora_e );
1083 if (rc != LDAP_SUCCESS) {
1084 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1086 LDAPControl **ctrls = NULL;
1088 ctrls = ch_calloc( sizeof( LDAPControl *) , 2 );
1089 ctrls[0] = create_passcontrol( -1, -1, pErr );
1091 rs->sr_ctrls = ctrls;
1093 send_ldap_error( op, rs, rc, "Password fails quality checking policy" );
1097 * A controversial bit. We hash cleartext
1098 * passwords provided via add and modify operations
1099 * You're not really supposed to do this, since
1100 * the X.500 model says "store attributes" as they
1101 * get provided. By default, this is what we do
1103 * But if the hash_passwords flag is set, we hash
1104 * any cleartext password attribute values via the
1105 * default password hashing scheme.
1107 if ((pi->hash_passwords) &&
1108 (password_scheme( &(pa->a_vals[0]), NULL ) != LDAP_SUCCESS)) {
1111 slap_passwd_hash( &(pa->a_vals[0]), &hpw, &txt );
1112 if (hpw.bv_val == NULL) {
1114 * hashing didn't work. Emit an error.
1116 rs->sr_err = LDAP_OTHER;
1118 send_ldap_error( op, rs, LDAP_OTHER, "Password hashing failed" );
1122 memset( pa->a_vals[0].bv_val, 0, pa->a_vals[0].bv_len);
1123 ber_memfree( pa->a_vals[0].bv_val );
1124 pa->a_vals[0].bv_val = hpw.bv_val;
1125 pa->a_vals[0].bv_len = hpw.bv_len;
1128 /* If password aging is in effect, set the pwdChangedTime */
1129 if (( pp.pwdMaxAge || pp.pwdMinAge ) && !be_shadow_update( op )) {
1130 struct berval timestamp;
1131 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
1132 time_t now = slap_get_time();
1134 timestamp.bv_val = timebuf;
1135 timestamp.bv_len = sizeof(timebuf);
1136 slap_timestamp( &now, ×tamp );
1138 attr_merge_one( op->ora_e, ad_pwdChangedTime, ×tamp, NULL );
1141 return SLAP_CB_CONTINUE;
1145 ppolicy_modify( Operation *op, SlapReply *rs )
1147 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1148 pp_info *pi = on->on_bi.bi_private;
1149 int i, rc, mod_pw_only, pwmod, pwmop, deladd,
1152 Modifications *mods = NULL, *modtail, *ml, *delmod, *addmod;
1153 Attribute *pa, *ha, *ra, at;
1155 pw_hist *tl = NULL, *p;
1156 int zapReset, send_ctrl = 0;
1158 struct berval newpw = BER_BVNULL, oldpw = BER_BVNULL,
1160 LDAPPasswordPolicyError pErr = PP_noError;
1162 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1163 rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &e );
1164 op->o_bd->bd_info = (BackendInfo *)on;
1166 if ( rc != LDAP_SUCCESS ) return SLAP_CB_CONTINUE;
1168 /* Did we receive a password policy request control? */
1169 if ( op->o_ctrlflag[ppolicy_cid] ) {
1173 /* See if this is a pwdModify exop. If so, we can
1174 * access the plaintext passwords from that request.
1179 for ( sc = op->o_callback; sc; sc=sc->sc_next ) {
1180 if ( sc->sc_response == slap_replog_cb &&
1182 req_pwdexop_s *qpw = sc->sc_private;
1183 newpw = qpw->rs_new;
1184 oldpw = qpw->rs_old;
1190 ppolicy_get( op, e, &pp );
1192 for(ml = op->oq_modify.rs_modlist,
1193 pwmod = 0, mod_pw_only = 1,
1194 deladd = 0, delmod = NULL,
1197 ml != NULL; modtail = ml, ml = ml->sml_next ) {
1198 if ( ml->sml_desc == pp.ad ) {
1201 if ((deladd == 0) && (ml->sml_op == LDAP_MOD_DELETE) &&
1202 (ml->sml_values) && (ml->sml_values[0].bv_val != NULL)) {
1207 if ((deladd == 1) && ((ml->sml_op == LDAP_MOD_ADD) ||
1208 (ml->sml_op == LDAP_MOD_REPLACE)))
1211 if ((ml->sml_op == LDAP_MOD_ADD) ||
1212 (ml->sml_op == LDAP_MOD_REPLACE))
1214 } else if (! is_at_operational( ml->sml_desc->ad_type )) {
1216 /* modifying something other than password */
1220 * If there is a request to explicitly add a pwdReset
1221 * attribute, then we suppress the normal behaviour on
1222 * password change, which is to remove the pwdReset
1225 * This enables an administrator to assign a new password
1226 * and place a "must reset" flag on the entry, which will
1227 * stay until the user explicitly changes his/her password.
1229 if (ml->sml_desc == ad_pwdReset ) {
1230 if ((ml->sml_op == LDAP_MOD_ADD) ||
1231 (ml->sml_op == LDAP_MOD_REPLACE))
1236 if (pwcons[op->o_conn->c_conn_idx].restricted && !mod_pw_only) {
1237 Debug( LDAP_DEBUG_TRACE,
1238 "connection restricted to password changing only\n", 0, 0, 0 );
1239 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
1240 rs->sr_text = "Operations are restricted to bind/unbind/abandon/StartTLS/modify password";
1241 pErr = PP_changeAfterReset;
1242 goto return_results;
1246 * if we have a "safe password modify policy", then we need to check if we're doing
1247 * a delete (with the old password), followed by an add (with the new password).
1249 * If we don't have this, then we fail with an error. We also skip all the checks if
1250 * the root user is bound. Root can do anything, including avoid the policies.
1253 if (!pwmod) goto do_modify;
1256 * Did we get a valid add mod?
1260 rs->sr_err = LDAP_OTHER;
1261 rs->sr_text = "Internal Error";
1262 Debug( LDAP_DEBUG_TRACE,
1263 "cannot locate modification supplying new password\n", 0, 0, 0 );
1264 goto return_results;
1268 * Build the password history list in ascending time order
1269 * We need this, even if the user is root, in order to maintain
1270 * the pwdHistory operational attributes properly.
1272 if (pp.pwdInHistory > 0 && (ha = attr_find( e->e_attrs, ad_pwdHistory ))) {
1273 struct berval oldpw;
1277 for(i=0; ha->a_nvals[i].bv_val; i++) {
1278 rc = parse_pwdhistory( &(ha->a_nvals[i]), NULL,
1281 if (rc != LDAP_SUCCESS) continue; /* invalid history entry */
1284 add_to_pwd_history( &tl, oldtime, &oldpw,
1285 &(ha->a_nvals[i]) );
1286 oldpw.bv_val = NULL;
1290 for(p=tl; p; p=p->next, hsize++); /* count history size */
1293 if (be_isroot( op )) goto do_modify;
1295 /* This is a pwdModify exop that provided the old pw.
1296 * We need to create a Delete mod for this old pw and
1297 * let the matching value get found later
1299 if (pp.pwdSafeModify && oldpw.bv_val ) {
1300 ml = (Modifications *) ch_malloc( sizeof( Modifications ) );
1301 ml->sml_op = LDAP_MOD_DELETE;
1302 ml->sml_flags = SLAP_MOD_INTERNAL;
1303 ml->sml_desc = pp.ad;
1304 ml->sml_type = pp.ad->ad_cname;
1305 ml->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1306 ber_dupbv( &ml->sml_values[0], &oldpw );
1307 ml->sml_values[1].bv_len = 0;
1308 ml->sml_values[1].bv_val = NULL;
1309 ml->sml_nvalues = NULL;
1310 ml->sml_next = op->orm_modlist;
1311 op->orm_modlist = ml;
1316 if (pp.pwdSafeModify && deladd != 2) {
1317 Debug( LDAP_DEBUG_TRACE,
1318 "change password must use DELETE followed by ADD/REPLACE\n",
1320 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
1321 rs->sr_text = "Must supply old password to be changed as well as new one";
1322 pErr = PP_mustSupplyOldPassword;
1323 goto return_results;
1326 if (!pp.pwdAllowUserChange) {
1327 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
1328 rs->sr_text = "User alteration of password is not allowed";
1329 pErr = PP_passwordModNotAllowed;
1330 goto return_results;
1333 if (pp.pwdMinAge > 0) {
1334 time_t pwtime = (time_t)-1, now;
1337 if ((pa = attr_find( e->e_attrs, ad_pwdChangedTime )) != NULL)
1338 pwtime = parse_time( pa->a_nvals[0].bv_val );
1339 now = slap_get_time();
1340 age = (int)(now - pwtime);
1341 if ((pwtime != (time_t)-1) && (age < pp.pwdMinAge)) {
1342 rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
1343 rs->sr_text = "Password is too young to change";
1344 pErr = PP_passwordTooYoung;
1345 goto return_results;
1349 /* pa is used in password history check below, be sure it's set */
1350 if ((pa = attr_find( e->e_attrs, pp.ad )) != NULL && delmod) {
1352 * we have a password to check
1356 bv = oldpw.bv_val ? &oldpw : delmod->sml_values;
1357 /* FIXME: no access checking? */
1358 rc = slap_passwd_check( op, NULL, pa, bv, &txt );
1359 if (rc != LDAP_SUCCESS) {
1360 Debug( LDAP_DEBUG_TRACE,
1361 "old password check failed: %s\n", txt, 0, 0 );
1363 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1364 rs->sr_text = "Must supply correct old password to change to new one";
1365 pErr = PP_mustSupplyOldPassword;
1366 goto return_results;
1372 * replace the delete value with the (possibly hashed)
1373 * value which is currently in the password.
1375 for(i=0; delmod->sml_values[i].bv_val; i++) {
1376 free(delmod->sml_values[i].bv_val);
1377 delmod->sml_values[i].bv_len = 0;
1379 free(delmod->sml_values);
1380 delmod->sml_values = ch_calloc( sizeof(struct berval), 2 );
1381 delmod->sml_values[1].bv_len = 0;
1382 delmod->sml_values[1].bv_val = NULL;
1383 ber_dupbv(&(delmod->sml_values[0]), &(pa->a_nvals[0]));
1387 bv = newpw.bv_val ? &newpw : addmod->sml_values;
1388 if (pp.pwdCheckQuality > 0) {
1390 rc = check_password_quality( bv, &pp, &pErr, e );
1391 if (rc != LDAP_SUCCESS) {
1393 rs->sr_text = "Password fails quality checking policy";
1394 goto return_results;
1400 * Last check - the password history.
1402 /* FIXME: no access checking? */
1403 if (slap_passwd_check( op, NULL, pa, bv, &txt ) == LDAP_SUCCESS) {
1405 * This is bad - it means that the user is attempting
1406 * to set the password to the same as the old one.
1408 rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
1409 rs->sr_text = "Password is not being changed from existing value";
1410 pErr = PP_passwordInHistory;
1411 goto return_results;
1414 if (pp.pwdInHistory < 1) goto do_modify;
1417 * Iterate through the password history, and fail on any
1422 cr[1].bv_val = NULL;
1423 for(p=tl; p; p=p->next) {
1425 /* FIXME: no access checking? */
1426 rc = slap_passwd_check( op, NULL, &at, bv, &txt );
1428 if (rc != LDAP_SUCCESS) continue;
1430 rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
1431 rs->sr_text = "Password is in history of old passwords";
1432 pErr = PP_passwordInHistory;
1433 goto return_results;
1438 if ((pwmod) && (!be_shadow_update( op ))) {
1439 struct berval timestamp;
1440 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
1441 time_t now = slap_get_time();
1445 * keep the necessary pwd.. operational attributes
1449 timestamp.bv_val = timebuf;
1450 timestamp.bv_len = sizeof(timebuf);
1451 slap_timestamp( &now, ×tamp );
1453 mods = (Modifications *) ch_malloc( sizeof( Modifications ) );
1454 mods->sml_type.bv_val = NULL;
1455 mods->sml_desc = ad_pwdChangedTime;
1456 if (pwmop != LDAP_MOD_DELETE) {
1457 mods->sml_op = LDAP_MOD_REPLACE;
1458 mods->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1459 ber_dupbv( &mods->sml_values[0], ×tamp );
1460 mods->sml_values[1].bv_len = 0;
1461 mods->sml_values[1].bv_val = NULL;
1462 assert( mods->sml_values[0].bv_val );
1464 mods->sml_op = LDAP_MOD_DELETE;
1465 mods->sml_values = NULL;
1467 mods->sml_flags = SLAP_MOD_INTERNAL;
1468 mods->sml_nvalues = NULL;
1469 mods->sml_next = NULL;
1470 modtail->sml_next = mods;
1473 if (attr_find(e->e_attrs, ad_pwdGraceUseTime )) {
1474 mods = (Modifications *) ch_malloc( sizeof( Modifications ) );
1475 mods->sml_op = LDAP_MOD_DELETE;
1476 mods->sml_flags = SLAP_MOD_INTERNAL;
1477 mods->sml_type.bv_val = NULL;
1478 mods->sml_desc = ad_pwdGraceUseTime;
1479 mods->sml_values = NULL;
1480 mods->sml_nvalues = NULL;
1481 mods->sml_next = NULL;
1482 modtail->sml_next = mods;
1486 /* Delete the pwdReset attribute, since it's being reset */
1487 if ((zapReset) && (attr_find(e->e_attrs, ad_pwdReset ))) {
1488 mods = (Modifications *) ch_malloc( sizeof( Modifications ) );
1489 mods->sml_op = LDAP_MOD_DELETE;
1490 mods->sml_flags = SLAP_MOD_INTERNAL;
1491 mods->sml_type.bv_val = NULL;
1492 mods->sml_desc = ad_pwdReset;
1493 mods->sml_values = NULL;
1494 mods->sml_nvalues = NULL;
1495 mods->sml_next = NULL;
1496 modtail->sml_next = mods;
1500 if (pp.pwdInHistory > 0) {
1501 if (hsize >= pp.pwdInHistory) {
1503 * We use the >= operator, since we are going to add
1504 * the existing password attribute value into the
1505 * history - thus the cardinality of history values is
1506 * about to rise by one.
1508 * If this would push it over the limit of history
1509 * values (remembering - the password policy could have
1510 * changed since the password was last altered), we must
1511 * delete at least 1 value from the pwdHistory list.
1513 * In fact, we delete '(#pwdHistory attrs - max pwd
1514 * history length) + 1' values, starting with the oldest.
1515 * This is easily evaluated, since the linked list is
1516 * created in ascending time order.
1518 mods = (Modifications *) ch_malloc( sizeof( Modifications ) );
1519 mods->sml_op = LDAP_MOD_DELETE;
1520 mods->sml_flags = SLAP_MOD_INTERNAL;
1521 mods->sml_type.bv_val = NULL;
1522 mods->sml_desc = ad_pwdHistory;
1523 mods->sml_nvalues = NULL;
1524 mods->sml_values = ch_calloc( sizeof( struct berval ),
1525 hsize - pp.pwdInHistory + 2 );
1526 mods->sml_values[ hsize - pp.pwdInHistory + 1 ].bv_val = NULL;
1527 mods->sml_values[ hsize - pp.pwdInHistory + 1 ].bv_len = 0;
1528 for(i=0,p=tl; i < (hsize - pp.pwdInHistory + 1); i++, p=p->next) {
1529 mods->sml_values[i].bv_val = NULL;
1530 mods->sml_values[i].bv_len = 0;
1531 ber_dupbv( &(mods->sml_values[i]), &p->bv );
1533 mods->sml_next = NULL;
1534 modtail->sml_next = mods;
1537 free_pwd_history_list( &tl );
1540 * Now add the existing password into the history list.
1541 * This will be executed even if the operation is to delete
1542 * the password entirely.
1544 * This isn't in the spec explicitly, but it seems to make
1545 * sense that the password history list is the list of all
1546 * previous passwords - even if they were deleted. Thus, if
1547 * someone tries to add a historical password at some future
1548 * point, it will fail.
1550 if ((pa = attr_find( e->e_attrs, pp.ad )) != NULL) {
1551 mods = (Modifications *) ch_malloc( sizeof( Modifications ) );
1552 mods->sml_op = LDAP_MOD_ADD;
1553 mods->sml_flags = SLAP_MOD_INTERNAL;
1554 mods->sml_type.bv_val = NULL;
1555 mods->sml_desc = ad_pwdHistory;
1556 mods->sml_nvalues = NULL;
1557 mods->sml_values = ch_calloc( sizeof( struct berval ), 2 );
1558 mods->sml_values[ 1 ].bv_val = NULL;
1559 mods->sml_values[ 1 ].bv_len = 0;
1560 make_pwd_history_value( timebuf, &mods->sml_values[0], pa );
1561 mods->sml_next = NULL;
1562 modtail->sml_next = mods;
1565 Debug( LDAP_DEBUG_TRACE,
1566 "ppolicy_modify: password attr lookup failed\n", 0, 0, 0 );
1571 * Controversial bit here. If the new password isn't hashed
1572 * (ie, is cleartext), we probably should hash it according
1573 * to the default hash. The reason for this is that we want
1574 * to use the policy if possible, but if we hash the password
1575 * before, then we're going to run into trouble when it
1576 * comes time to check the password.
1578 * Now, the right thing to do is to use the extended password
1579 * modify operation, but not all software can do this,
1580 * therefore it makes sense to hash the new password, now
1581 * we know it passes the policy requirements.
1583 * Of course, if the password is already hashed, then we
1587 if ((pi->hash_passwords) && (addmod) && !newpw.bv_val &&
1588 (password_scheme( &(addmod->sml_values[0]), NULL ) != LDAP_SUCCESS)) {
1589 struct berval hpw, bv;
1591 slap_passwd_hash( &(addmod->sml_values[0]), &hpw, &txt );
1592 if (hpw.bv_val == NULL) {
1594 * hashing didn't work. Emit an error.
1596 rs->sr_err = LDAP_OTHER;
1598 goto return_results;
1600 bv.bv_val = addmod->sml_values[0].bv_val;
1601 bv.bv_len = addmod->sml_values[0].bv_len;
1602 /* clear and discard the clear password */
1603 memset(bv.bv_val, 0, bv.bv_len);
1604 ber_memfree(bv.bv_val);
1605 addmod->sml_values[0].bv_val = hpw.bv_val;
1606 addmod->sml_values[0].bv_len = hpw.bv_len;
1609 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1610 be_entry_release_r( op, e );
1611 return SLAP_CB_CONTINUE;
1614 free_pwd_history_list( &tl );
1615 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1616 be_entry_release_r( op, e );
1618 LDAPControl **ctrls = NULL;
1620 ctrls = ch_calloc( sizeof( LDAPControl *) , 2 );
1621 ctrls[0] = create_passcontrol( -1, -1, pErr );
1623 rs->sr_ctrls = ctrls;
1625 send_ldap_result( op, rs );
1635 if ( ctrl->ldctl_value.bv_len ) {
1636 rs->sr_text = "passwordPolicyRequest control value not empty";
1637 return LDAP_PROTOCOL_ERROR;
1639 if ( ctrl->ldctl_iscritical ) {
1640 rs->sr_text = "passwordPolicyRequest control invalid criticality";
1641 return LDAP_PROTOCOL_ERROR;
1643 op->o_ctrlflag[ppolicy_cid] = SLAP_CONTROL_NONCRITICAL;
1645 return LDAP_SUCCESS;
1653 slap_overinst *on = (slap_overinst *) be->bd_info;
1655 /* Has User Schema been initialized yet? */
1656 if ( !pwd_UsSchema[0].ad[0] ) {
1660 for (i=0; pwd_UsSchema[i].def; i++) {
1661 code = slap_str2ad( pwd_UsSchema[i].def, pwd_UsSchema[i].ad, &err );
1663 fprintf( stderr, "User Schema Load failed %d: %s\n", code, err );
1669 on->on_bi.bi_private = ch_calloc( sizeof(pp_info), 1 );
1671 if ( dtblsize && !pwcons )
1672 pwcons = ch_calloc(sizeof(pw_conn), dtblsize );
1682 return overlay_register_control( be, LDAP_CONTROL_PASSWORDPOLICYREQUEST );
1690 slap_overinst *on = (slap_overinst *) be->bd_info;
1691 pp_info *pi = on->on_bi.bi_private;
1694 free( pi->def_policy.bv_val );
1709 slap_overinst *on = (slap_overinst *) be->bd_info;
1710 pp_info *pi = on->on_bi.bi_private;
1714 if ( strcasecmp( argv[0], "ppolicy_default" ) == 0 ) {
1716 fprintf( stderr, "%s: line %d: invalid arguments in \"ppolicy_default"
1717 " <policyDN>\n", fname, lineno );
1720 ber_str2bv( argv[1], 0, 0, &dn );
1721 if ( dnNormalize( 0, NULL, NULL, &dn, &pi->def_policy, NULL ) ) {
1722 fprintf( stderr, "%s: line %d: policyDN is invalid\n",
1728 } else if ( strcasecmp( argv[0], "ppolicy_use_lockout" ) == 0 ) {
1730 fprintf( stderr, "%s: line %d: ppolicy_use_lockout "
1731 "takes no arguments\n", fname, lineno );
1734 pi->use_lockout = 1;
1736 } else if ( strcasecmp( argv[0], "ppolicy_hash_cleartext" ) == 0 ) {
1738 fprintf( stderr, "%s: line %d: ppolicy_hash_cleartext "
1739 "takes no arguments\n", fname, lineno );
1742 pi->hash_passwords = 1;
1744 return SLAP_CONF_UNKNOWN;
1747 static char *extops[] = {
1748 LDAP_EXOP_MODIFY_PASSWD,
1752 static slap_overinst ppolicy;
1756 LDAPAttributeType *at;
1760 for (i=0; pwd_OpSchema[i].def; i++) {
1761 at = ldap_str2attributetype( pwd_OpSchema[i].def, &code, &err,
1762 LDAP_SCHEMA_ALLOW_ALL );
1764 fprintf( stderr, "AttributeType Load failed %s %s\n",
1765 ldap_scherr2str(code), err );
1768 code = at_add( at, 0, NULL, &err );
1770 slap_str2ad( at->at_names[0], pwd_OpSchema[i].ad, &err );
1774 fprintf( stderr, "AttributeType Load failed %s %s\n",
1775 scherr2str(code), err );
1780 code = register_supported_control( LDAP_CONTROL_PASSWORDPOLICYREQUEST,
1781 SLAP_CTRL_ADD|SLAP_CTRL_BIND|SLAP_CTRL_MODIFY|SLAP_CTRL_HIDE, extops,
1782 ppolicy_parseCtrl, &ppolicy_cid );
1783 if ( code != LDAP_SUCCESS ) {
1784 fprintf( stderr, "Failed to register control %d\n", code );
1788 ldap_pvt_thread_mutex_init( &chk_syntax_mutex );
1790 ppolicy.on_bi.bi_type = "ppolicy";
1791 ppolicy.on_bi.bi_db_init = ppolicy_db_init;
1792 ppolicy.on_bi.bi_db_open = ppolicy_db_open;
1793 ppolicy.on_bi.bi_db_config = ppolicy_config;
1794 ppolicy.on_bi.bi_db_close = ppolicy_close;
1796 ppolicy.on_bi.bi_op_add = ppolicy_add;
1797 ppolicy.on_bi.bi_op_bind = ppolicy_bind;
1798 ppolicy.on_bi.bi_op_unbind = ppolicy_unbind;
1799 ppolicy.on_bi.bi_op_compare = ppolicy_restrict;
1800 ppolicy.on_bi.bi_op_delete = ppolicy_restrict;
1801 ppolicy.on_bi.bi_op_modify = ppolicy_modify;
1802 ppolicy.on_bi.bi_op_search = ppolicy_restrict;
1804 return overlay_register( &ppolicy );
1807 #if SLAPD_OVER_PPOLICY == SLAPD_MOD_DYNAMIC
1808 int init_module(int argc, char *argv[]) {
1809 return ppolicy_init();
1813 #endif /* defined(SLAPD_OVER_PPOLICY) */