2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 2004 Howard Chu, Symas Corporation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
18 /* This file implements "Password Policy for LDAP Directories",
19 * based on draft behera-ldap-password-policy-07
22 #ifdef SLAPD_OVER_PPOLICY
24 #if SLAPD_OVER_PPOLICY == SLAPD_MOD_DYNAMIC
34 #include <ac/string.h>
36 #ifndef MODULE_NAME_SZ
37 #define MODULE_NAME_SZ 256
40 /* Per-instance configuration information */
41 typedef struct pp_info {
42 struct berval def_policy; /* DN of default policy subentry */
43 int use_lockout; /* send AccountLocked result? */
46 /* Our per-connection info - note, it is not per-instance, it is
47 * used by all instances
49 typedef struct pw_conn {
50 int restrict; /* TRUE if connection is restricted */
53 static pw_conn *pwcons;
55 typedef struct pass_policy {
56 AttributeDescription *ad; /* attribute to which the policy applies */
57 int pwdMinAge; /* minimum time (seconds) until passwd can change */
58 int pwdMaxAge; /* time in seconds until pwd will expire after change */
59 int pwdInHistory; /* number of previous passwords kept */
60 int pwdCheckQuality; /* 0 = don't check quality, 1 = check if possible,
61 2 = check mandatory; fail if not possible */
62 int pwdMinLength; /* minimum number of chars in password */
63 int pwdExpireWarning; /* number of seconds that warning controls are
64 sent before a password expires */
65 int pwdGraceLoginLimit; /* number of times you can log in with an
67 int pwdLockout; /* 0 = do not lockout passwords, 1 = lock them out */
68 int pwdLockoutDuration; /* time in seconds a password is locked out for */
69 int pwdMaxFailure; /* number of failed binds allowed before lockout */
70 int pwdFailureCountInterval; /* number of seconds before failure
72 int pwdMustChange; /* 0 = users can use admin set password
73 1 = users must change password after admin set */
74 int pwdAllowUserChange; /* 0 = users cannot change their passwords
75 1 = users can change them */
76 int pwdSafeModify; /* 0 = old password doesn't need to come
77 with password change request
78 1 = password change must supply existing pwd */
79 char pwdCheckModule[MODULE_NAME_SZ]; /* name of module to dynamically
80 load to check password */
83 typedef struct pw_hist {
84 time_t t; /* timestamp of history entry */
85 struct berval pw; /* old password hash */
86 struct berval bv; /* text of entire entry */
90 /* Operational attributes */
91 static AttributeDescription *ad_pwdChangedTime, *ad_pwdAccountLockedTime,
92 *ad_pwdExpirationWarned, *ad_pwdFailureTime, *ad_pwdHistory,
93 *ad_pwdGraceUseTime, *ad_pwdReset, *ad_pwdPolicySubentry;
95 static struct schema_info {
97 AttributeDescription **ad;
99 { "( 1.3.6.1.4.1.42.2.27.8.1.16 "
100 "NAME ( 'pwdChangedTime' ) "
101 "DESC 'The time the password was last changed' "
102 "EQUALITY generalizedTimeMatch "
103 "ORDERING generalizedTimeOrderingMatch "
104 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
105 "SINGLE-VALUE USAGE directoryOperation )",
106 &ad_pwdChangedTime },
107 { "( 1.3.6.1.4.1.42.2.27.8.1.17 "
108 "NAME ( 'pwdAccountLockedTime' ) "
109 "DESC 'The time an user account was locked' "
110 "EQUALITY generalizedTimeMatch "
111 "ORDERING generalizedTimeOrderingMatch "
112 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
113 "SINGLE-VALUE USAGE directoryOperation )",
114 &ad_pwdAccountLockedTime },
115 { "( 1.3.6.1.4.1.42.2.27.8.1.18 "
116 "NAME ( 'pwdExpirationWarned' ) "
117 "DESC 'The time the user was first warned about the coming expiration of the password' "
118 "EQUALITY generalizedTimeMatch "
119 "ORDERING generalizedTimeOrderingMatch "
120 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
121 "SINGLE-VALUE USAGE directoryOperation )",
122 &ad_pwdExpirationWarned },
123 { "( 1.3.6.1.4.1.42.2.27.8.1.19 "
124 "NAME ( 'pwdFailureTime' ) "
125 "DESC 'The timestamps of the last consecutive authentication failures' "
126 "EQUALITY generalizedTimeMatch "
127 "ORDERING generalizedTimeOrderingMatch "
128 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
129 "USAGE directoryOperation )",
130 &ad_pwdFailureTime },
131 { "( 1.3.6.1.4.1.42.2.27.8.1.20 "
132 "NAME ( 'pwdHistory' ) "
133 "DESC 'The history of users passwords' "
134 "EQUALITY octetStringMatch "
135 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.40 "
136 "USAGE directoryOperation )",
138 { "( 1.3.6.1.4.1.42.2.27.8.1.21 "
139 "NAME ( 'pwdGraceUseTime' ) "
140 "DESC 'The timestamps of the grace login once the password has expired' "
141 "EQUALITY generalizedTimeMatch "
142 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
143 "USAGE directoryOperation )",
144 &ad_pwdGraceUseTime },
145 { "( 1.3.6.1.4.1.42.2.27.8.1.22 "
146 "NAME ( 'pwdReset' ) "
147 "DESC 'The indication that the password has been reset' "
148 "EQUALITY booleanMatch "
149 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.7 "
150 "SINGLE-VALUE USAGE directoryOperation )",
152 { "( 1.3.6.1.4.1.42.2.27.8.1.23 "
153 "NAME ( 'pwdPolicySubentry' ) "
154 "DESC 'The pwdPolicy subentry in effect for this object' "
155 "EQUALITY distinguishedNameMatch "
156 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.12 "
157 "SINGLE-VALUE USAGE directoryOperation )",
158 &ad_pwdPolicySubentry },
162 /* User attributes */
163 static AttributeDescription *ad_pwdMinAge, *ad_pwdMaxAge, *ad_pwdInHistory,
164 *ad_pwdCheckQuality, *ad_pwdMinLength, *ad_pwdMaxFailure,
165 *ad_pwdGraceLoginLimit, *ad_pwdExpireWarning, *ad_pwdLockoutDuration,
166 *ad_pwdFailureCountInterval, *ad_pwdCheckModule, *ad_pwdLockout,
167 *ad_pwdMustChange, *ad_pwdAllowUserChange, *ad_pwdSafeModify,
170 #define TAB(name) { #name, &ad_##name }
172 static struct schema_info pwd_UsSchema[] = {
177 TAB(pwdCheckQuality),
180 TAB(pwdGraceLoginLimit),
181 TAB(pwdExpireWarning),
183 TAB(pwdLockoutDuration),
184 TAB(pwdFailureCountInterval),
187 TAB(pwdAllowUserChange),
192 static ldap_pvt_thread_mutex_t chk_syntax_mutex;
195 parse_time( char *atm )
200 if (!atm) return (time_t)-1;
203 * special case - if the lowest allowable GeneralizedTime is here, return
204 * this is as zero time. Note: this might also be the case if the value stored
205 * is equivalent to the start of the epoch (ie, Jan 1, 1970 at midnight.
207 if (strcmp(atm, "00000101000000Z") == 0) return (time_t)0;
209 * else parse the time and return it's time_t value. This will be -1 if the
210 * text isn't a valid time string.
212 strptime( atm, "%Y%m%d%H%M%SZ", &tml );
215 if ( t == (time_t)-1 ) return t;
217 /* mktime() assumes localtime. Compute the offset to GMT.
219 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
221 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
223 tmg.tm_mday -= tml.tm_mday;
224 tmg.tm_hour -= tml.tm_hour;
226 /* The difference should only be +/- 1 day, but may
227 * fall outside this range at the beginning/end of a month
229 if ( tmg.tm_mday > 1 ) tmg.tm_mday = -1;
230 else if ( tmg.tm_mday < -1 ) tmg.tm_mday = 1;
232 tmg.tm_hour += tmg.tm_mday > 0 ? 24 : -24;
234 if ( tmg.tm_hour ) t -= tmg.tm_hour * 3600;
235 tmg.tm_min -= tml.tm_min;
236 if ( tmg.tm_min ) t -= tmg.tm_min * 60;
237 tmg.tm_sec -= tml.tm_sec;
238 if ( tmg.tm_sec ) t -= tmg.tm_sec;
244 account_locked( Operation *op, Entry *e,
245 PassPolicy *pp, Modifications **mod )
253 if ( (la = attr_find( e->e_attrs, ad_pwdAccountLockedTime )) != NULL ) {
254 BerVarray vals = la->a_nvals;
257 * there is a lockout stamp - we now need to know if it's
260 if (vals[0].bv_val != NULL) {
265 if ((then = parse_time( vals[0].bv_val )) == (time_t)0)
268 now = slap_get_time();
270 if (now < then + pp->pwdLockoutDuration)
273 m = ch_calloc( sizeof(Modifications), 1 );
274 m->sml_op = LDAP_MOD_DELETE;
275 m->sml_type = ad_pwdAccountLockedTime->ad_cname;
276 m->sml_desc = ad_pwdAccountLockedTime;
285 #define PPOLICY_WARNING 0xa0L
286 #define PPOLICY_ERROR 0xa1L
288 #define PPOLICY_EXPIRE 0xa0L
289 #define PPOLICY_GRACE 0xa1L
292 create_passcontrol( int exptime, int grace, LDAPPasswordPolicyError err )
294 char berbuf[LBER_ELEMENT_SIZEOF], bb2[LBER_ELEMENT_SIZEOF];
295 BerElement *ber = (BerElement *)berbuf, *b2 = (BerElement *)bb2;
299 if ((c = ch_calloc( sizeof( LDAPControl ), 1 )) == NULL) return NULL;
300 c->ldctl_oid = LDAP_CONTROL_PASSWORDPOLICYRESPONSE;
301 c->ldctl_iscritical = 0;
302 c->ldctl_value.bv_val = NULL;
303 c->ldctl_value.bv_len = 0;
305 ber_init2( ber, NULL, LBER_USE_DER );
306 ber_printf(ber, "{" /*}*/ );
309 ber_init2( b2, NULL, LBER_USE_DER );
310 ber_printf( b2, "ti", PPOLICY_EXPIRE, exptime );
311 ber_flatten2( b2, &bv, 1 );
312 ber_printf( ber, "tO", PPOLICY_WARNING, &bv );
313 ch_free( bv.bv_val );
314 } else if (grace > 0) {
315 ber_init2( b2, NULL, LBER_USE_DER );
316 ber_printf( b2, "ti", PPOLICY_GRACE, grace );
317 ber_flatten2( b2, &bv, 1 );
318 ber_printf( ber, "tO", PPOLICY_WARNING, &bv );
319 ch_free( bv.bv_val );
322 if (err != PP_noError ) {
323 ber_printf( ber, "te", PPOLICY_ERROR, err );
325 ber_printf( ber, /*{*/ "N}" );
327 if (ber_flatten2( ber, &(c->ldctl_value), 1 ) == LBER_DEFAULT) {
329 (void)ber_free_buf(ber);
332 (void)ber_free_buf(ber);
337 ppolicy_get( Operation *op, Entry *e, PassPolicy *pp )
339 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
340 pp_info *pi = on->on_bi.bi_private;
345 AttributeDescription *oca = slap_schema.si_ad_objectClass;
347 AttributeDescription *ad;
349 void *opr = op->o_private;
351 memset( pp, 0, sizeof(PassPolicy) );
353 if ((a = attr_find( e->e_attrs, ad_pwdPolicySubentry )) == NULL) {
355 * entry has no password policy assigned - use default
357 vals = &pi->def_policy;
362 if (vals[0].bv_val == NULL) {
364 LDAP_LOG( OPERATION, ERR,
365 "ppolicy_get: NULL value for policySubEntry\n", 0, 0, 0 );
367 Debug( LDAP_DEBUG_ANY,
368 "ppolicy_get: NULL value for policySubEntry\n", 0, 0, 0 );
374 /* back-bdb stores lock info in o_private */
375 op->o_private = NULL;
376 op->o_bd->bd_info = (BackendInfo *)on->on_info;
377 rc = be_entry_get_rw( op, vals, NULL, NULL, 0, &pe );
378 op->o_bd->bd_info = (BackendInfo *)on;
380 if ( rc ) goto defaultpol;
382 #if 0 /* Only worry about userPassword for now */
383 if ((a = attr_find( pe->e_attrs, ad_pwdAttribute )))
384 slap_bv2ad( &a->a_vals[0], &pp->ad, &text );
386 pp->ad = slap_schema.si_ad_userPassword;
389 if ((a = attr_find( pe->e_attrs, ad_pwdMinAge )))
390 pp->pwdMinAge = atoi(a->a_vals[0].bv_val );
391 if ((a = attr_find( pe->e_attrs, ad_pwdMaxAge )))
392 pp->pwdMaxAge = atoi(a->a_vals[0].bv_val );
393 if ((a = attr_find( pe->e_attrs, ad_pwdInHistory )))
394 pp->pwdInHistory = atoi(a->a_vals[0].bv_val );
395 if ((a = attr_find( pe->e_attrs, ad_pwdCheckQuality )))
396 pp->pwdCheckQuality = atoi(a->a_vals[0].bv_val );
397 if ((a = attr_find( pe->e_attrs, ad_pwdMinLength )))
398 pp->pwdMinLength = atoi(a->a_vals[0].bv_val );
399 if ((a = attr_find( pe->e_attrs, ad_pwdMaxFailure )))
400 pp->pwdMaxFailure = atoi(a->a_vals[0].bv_val );
401 if ((a = attr_find( pe->e_attrs, ad_pwdGraceLoginLimit )))
402 pp->pwdGraceLoginLimit = atoi(a->a_vals[0].bv_val );
403 if ((a = attr_find( pe->e_attrs, ad_pwdExpireWarning )))
404 pp->pwdExpireWarning = atoi(a->a_vals[0].bv_val );
405 if ((a = attr_find( pe->e_attrs, ad_pwdFailureCountInterval )))
406 pp->pwdFailureCountInterval = atoi(a->a_vals[0].bv_val );
407 if ((a = attr_find( pe->e_attrs, ad_pwdLockoutDuration )))
408 pp->pwdLockoutDuration = atoi(a->a_vals[0].bv_val );
410 if ((a = attr_find( pe->e_attrs, ad_pwdCheckModule ))) {
411 strncpy(pp->pwdCheckModule, a->a_vals[0].bv_val,
412 sizeof(pp->pwdCheckModule));
413 pp->pwdCheckModule[sizeof(pp->pwdCheckModule)-1] = '\0';
416 if ((a = attr_find( pe->e_attrs, ad_pwdLockout )))
417 pp->pwdLockout = !strcmp( a->a_nvals[0].bv_val, "TRUE" );
418 if ((a = attr_find( pe->e_attrs, ad_pwdMustChange )))
419 pp->pwdMustChange = !strcmp( a->a_nvals[0].bv_val, "TRUE" );
420 if ((a = attr_find( pe->e_attrs, ad_pwdAllowUserChange )))
421 pp->pwdAllowUserChange = !strcmp( a->a_nvals[0].bv_val, "TRUE" );
422 if ((a = attr_find( pe->e_attrs, ad_pwdSafeModify )))
423 pp->pwdSafeModify = !strcmp( a->a_nvals[0].bv_val, "TRUE" );
425 op->o_bd->bd_info = (BackendInfo *)on->on_info;
426 be_entry_release_r( op, pe );
427 op->o_bd->bd_info = (BackendInfo *)on;
434 LDAP_LOG( OPERATION, DETAIL1,
435 "ppolicy_get: using default policy\n", 0, 0, 0 );
437 Debug( LDAP_DEBUG_ANY,
438 "ppolicy_get: using default policy\n", 0, 0, 0 );
445 password_scheme( struct berval *cred, struct berval *sch )
449 assert( cred != NULL );
456 if ((cred->bv_len == 0) || (cred->bv_val == NULL) ||
457 (cred->bv_val[0] != '{')) return LDAP_OTHER;
459 for(e = 1; cred->bv_val[e] && cred->bv_val[e] != '}'; e++);
460 if (cred->bv_val[e]) {
461 char *sc = ch_calloc( sizeof(char), e + 2);
462 sc[e + 1] = '\0'; /* terminate string */
463 strncpy( sc, cred->bv_val, e + 1);
464 e = lutil_passwd_scheme( sc );
467 sch->bv_val = cred->bv_val;
476 check_password_quality( struct berval *cred, PassPolicy *pp, LDAPPasswordPolicyError *err )
478 int rc = LDAP_SUCCESS, ok = LDAP_SUCCESS;
479 char *ptr = cred->bv_val;
483 assert( cred != NULL );
484 assert( pp != NULL );
486 if ((cred->bv_len == 0) || (pp->pwdMinLength > cred->bv_len)) {
487 rc = LDAP_CONSTRAINT_VIOLATION;
488 if ( err ) *err = PP_passwordTooShort;
493 * We need to know if the password is already hashed - if so
494 * what scheme is it. The reason being that the "hash" of
495 * {cleartext} still allows us to check the password.
497 rc = password_scheme( cred, &sch );
498 if (rc == LDAP_SUCCESS) {
499 if ((sch.bv_val) && (strncasecmp( sch.bv_val, "{cleartext}",
500 sch.bv_len ) == 0)) {
502 * We can check the cleartext "hash"
504 ptr = cred->bv_val + sch.bv_len;
506 /* everything else, we can't check */
507 if (pp->pwdCheckQuality == 2) {
508 rc = LDAP_CONSTRAINT_VIOLATION;
509 if (err) *err = PP_insufficientPasswordQuality;
513 * We can't check the syntax of the password, but it's not
514 * mandatory (according to the policy), so we return success.
523 if (pp->pwdCheckModule[0]) {
527 if ((mod = lt_dlopen( pp->pwdCheckModule )) == NULL) {
531 LDAP_LOG( SLAPD, CRIT,
532 "check_password_quality: lt_dlopen failed: (%s) %s.\n",
533 pp->pwdCheckModule, err, 0 );
535 Debug(LDAP_DEBUG_ANY,
536 "check_password_quality: lt_dlopen failed: (%s) %s.\n",
537 pp->pwdCheckModule, err, 0 );
539 ok = LDAP_OTHER; /* internal error */
541 int (*prog)( char *passwd, char **text, void *arg );
543 if ((prog = lt_dlsym( mod, "check_password" )) == NULL) {
547 LDAP_LOG( SLAPD, CRIT,
548 "check_password_quality: lt_dlsym failed: (%s) %s.\n",
549 pp->pwdCheckModule, err, 0 );
551 Debug(LDAP_DEBUG_ANY,
552 "check_password_quality: lt_dlsym failed: (%s) %s.\n",
553 pp->pwdCheckModule, err, 0 );
559 ldap_pvt_thread_mutex_lock( &chk_syntax_mutex );
560 ok = prog( cred->bv_val, &txt, NULL );
561 ldap_pvt_thread_mutex_unlock( &chk_syntax_mutex );
564 LDAP_LOG( SLAPD, CRIT,
565 "check_password_quality: module error (%s) %s[%d].\n",
566 pp->pwdCheckModule, txt, ok );
568 Debug(LDAP_DEBUG_ANY,
569 "check_password_quality: module error: (%s) %s.[%d]\n",
570 pp->pwdCheckModule, txt, ok );
582 if (ok != LDAP_SUCCESS) {
583 rc = LDAP_CONSTRAINT_VIOLATION;
584 if (err) *err = PP_insufficientPasswordQuality;
591 parse_pwdhistory( struct berval *bv, char **oid, time_t *oldtime, struct berval *oldpw )
594 struct berval nv, npw;
597 assert (bv && (bv->bv_len > 0) && (bv->bv_val) && oldtime && oldpw );
600 *oldtime = (time_t)-1;
601 oldpw->bv_val = NULL;
604 ber_dupbv( &nv, bv );
606 /* first get the time field */
607 for(i=0; (i < nv.bv_len) && (nv.bv_val[i] != '#'); i++);
608 if ( i == nv.bv_len) goto exit_failure; /* couldn't locate the '#' separator */
609 nv.bv_val[i++] = '\0'; /* terminate the string & move to next field */
611 *oldtime = parse_time( ptr );
612 if (*oldtime == (time_t)-1) goto exit_failure;
614 /* get the OID field */
615 for(ptr = &(nv.bv_val[i]);(i < nv.bv_len) && (nv.bv_val[i] != '#'); i++);
616 if ( i == nv.bv_len) goto exit_failure; /* couldn't locate the '#' separator */
617 nv.bv_val[i++] = '\0'; /* terminate the string & move to next field */
618 if ( oid ) *oid = ber_strdup( ptr );
620 /* get the length field */
621 for(ptr = &(nv.bv_val[i]);(i < nv.bv_len) && (nv.bv_val[i] != '#'); i++);
622 if ( i == nv.bv_len) goto exit_failure; /* couldn't locate the '#' separator */
623 nv.bv_val[i++] = '\0'; /* terminate the string & move to next field */
624 oldpw->bv_len = strtol( ptr, NULL, 10 );
625 if (errno == ERANGE) goto exit_failure;
627 /* lastly, get the octets of the string */
628 for(j=i, ptr = &(nv.bv_val[i]);i < nv.bv_len; i++);
629 if (i-j != oldpw->bv_len) goto exit_failure; /* length is wrong */
632 npw.bv_len = oldpw->bv_len;
633 ber_dupbv( oldpw, &npw );
637 if (oid && *oid) { ber_memfree(*oid); *oid = NULL; }
639 ber_memfree( oldpw->bv_val); oldpw->bv_val = NULL;
642 ber_memfree(nv.bv_val);
647 add_to_pwd_history( pw_hist **l, time_t t,
648 struct berval *oldpw, struct berval *bv )
650 pw_hist *p, *p1, *p2;
654 p = ch_malloc( sizeof( pw_hist ));
656 ber_dupbv( &p->bv, bv );
661 /* degenerate case */
666 * advance p1 and p2 such that p1 is the node before the
667 * new one, and p2 is the node after it
669 for (p1 = NULL, p2 = *l; p2 && p2->t <= t; p1 = p2, p2=p2->next );
671 if (p1 == NULL) { *l = p; return; }
675 #ifndef MAX_PWD_HISTORY_SZ
676 #define MAX_PWD_HISTORY_SZ 1024
677 #endif /* MAX_PWD_HISTORY_SZ */
680 make_pwd_history_value( char *timebuf, struct berval *bv, Attribute *pa )
682 char str[ MAX_PWD_HISTORY_SZ ];
685 snprintf( str, MAX_PWD_HISTORY_SZ,
686 "%s#%s#%d#", timebuf,
687 pa->a_desc->ad_type->sat_syntax->ssyn_oid,
688 pa->a_nvals[0].bv_len );
689 str[MAX_PWD_HISTORY_SZ-1] = 0;
693 * We have to assume that the string is a string of octets,
694 * not readable characters. In reality, yes, it probably is
695 * a readable (ie, base64) string, but we can't count on that
696 * Hence, while the first 3 fields of the password history
697 * are definitely readable (a timestamp, an OID and an integer
698 * length), the remaining octets of the actual password
699 * are deemed to be binary data.
701 AC_MEMCPY( str + nlen, pa->a_nvals[0].bv_val, pa->a_nvals[0].bv_len );
702 nlen += pa->a_nvals[0].bv_len;
703 bv->bv_val = ch_malloc( nlen + 1 );
704 AC_MEMCPY( bv->bv_val, str, nlen );
705 bv->bv_val[nlen] = '\0';
710 free_pwd_history_list( pw_hist **l )
717 pw_hist *pp = p->next;
727 typedef struct ppbind {
731 LDAPPasswordPolicyError pErr;
736 ppolicy_bind_resp( Operation *op, SlapReply *rs )
738 ppbind *ppb = op->o_callback->sc_private;
739 slap_overinst *on = ppb->on;
740 Modifications *mod = ppb->mod, *m;
742 int ngut = -1, warn = -1, age, rc, i;
745 time_t now, then, pwtime = (time_t)-1;
747 char nowstr[ LDAP_LUTIL_GENTIME_BUFSIZE ];
748 BackendInfo *bi = op->o_bd->bd_info;
751 /* If we already know it's locked, just get on with it */
752 if ( ppb->pErr != PP_noError ) {
756 op->o_bd->bd_info = (BackendInfo *)on->on_info;
757 rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &e );
758 op->o_bd->bd_info = bi;
760 if ( rc != LDAP_SUCCESS ) {
761 return SLAP_CB_CONTINUE;
764 now = slap_get_time(); /* stored for later consideration */
765 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
767 lutil_gentime( nowstr, sizeof(nowstr), tm );
768 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
770 if ( rs->sr_err == LDAP_INVALID_CREDENTIALS ) {
773 m = ch_calloc( sizeof(Modifications), 1 );
774 m->sml_op = LDAP_MOD_ADD;
775 m->sml_type = ad_pwdFailureTime->ad_cname;
776 m->sml_desc = ad_pwdFailureTime;
777 m->sml_values = ch_calloc( sizeof(struct berval), 2 );
779 ber_str2bv( nowstr, 0, 1, &m->sml_values[0] );
784 * Count the pwdFailureTimes - if it's
785 * greater than the policy pwdMaxFailure,
786 * then lock the account.
788 if ((a = attr_find( e->e_attrs, ad_pwdFailureTime )) != NULL) {
789 for(i=0; a->a_nvals[i].bv_val; i++) {
792 * If the interval is 0, then failures
793 * stay on the record until explicitly
794 * reset by successful authentication.
796 if (ppb->pp.pwdFailureCountInterval == 0) {
799 parse_time(a->a_nvals[i].bv_val) +
800 ppb->pp.pwdFailureCountInterval) {
805 * We only count those failures
806 * which are not due to expire.
811 if ((ppb->pp.pwdMaxFailure > 0) &&
812 (fc >= ppb->pp.pwdMaxFailure - 1)) {
815 * We subtract 1 from the failure max
816 * because the new failure entry hasn't
817 * made it to the entry yet.
819 m = ch_calloc( sizeof(Modifications), 1 );
820 m->sml_op = LDAP_MOD_REPLACE;
821 m->sml_type = ad_pwdAccountLockedTime->ad_cname;
822 m->sml_desc = ad_pwdAccountLockedTime;
823 m->sml_values = ch_calloc( sizeof(struct berval), 2 );
824 ber_str2bv( nowstr, 0, 1, &m->sml_values[0] );
828 } else if ( rs->sr_err == LDAP_SUCCESS ) {
829 if ((a = attr_find( e->e_attrs, ad_pwdChangedTime )) != NULL)
830 pwtime = parse_time( a->a_nvals[0].bv_val );
832 /* delete all pwdFailureTimes */
833 if ( attr_find( e->e_attrs, ad_pwdFailureTime )) {
834 m = ch_calloc( sizeof(Modifications), 1 );
835 m->sml_op = LDAP_MOD_DELETE;
836 m->sml_type = ad_pwdFailureTime->ad_cname;
837 m->sml_desc = ad_pwdFailureTime;
843 * check to see if the password must be changed
845 if ( ppb->pp.pwdMustChange &&
846 (a = attr_find( e->e_attrs, ad_pwdReset )) &&
847 !strcmp( a->a_nvals[0].bv_val, "TRUE" ) ) {
849 * need to inject client controls here to give
850 * more information. For the moment, we ensure
851 * that we are disallowed from doing anything
852 * other than change password.
854 pwcons[op->o_conn->c_conn_idx].restrict = 1;
856 ppb->pErr = PP_changeAfterReset;
860 * the password does not need to be changed, so
861 * we now check whether the password has expired.
863 * We can skip this bit if passwords don't age in
866 if (ppb->pp.pwdMaxAge == 0) goto grace;
868 if (pwtime == (time_t)-1) {
870 * Hmm. No password changed time on the
871 * entry. This is odd - it should have
872 * been provided when the attribute was added.
874 * However, it's possible that it could be
875 * missing if the DIT was established via
879 LDAP_LOG( OPERATION, ERR,
880 "ppolicy_bind: Entry %s does not have valid pwdChangedTime attribute - assuming password expired\n",
881 e->e_name.bv_val, 0, 0);
883 Debug( LDAP_DEBUG_ANY,
884 "ppolicy_bind: Entry %s does not have valid pwdChangedTime attribute - assuming password expired\n",
885 e->e_name.bv_val, 0, 0);
891 * Check: was the last change time of
892 * the password older than the maximum age
893 * allowed. (Ignore case 2 from I-D, it's just silly.)
895 if (now - pwtime > ppb->pp.pwdMaxAge ) pwExpired = 1;
900 if (!pwExpired) goto check_expiring_password;
902 if ((a = attr_find( e->e_attrs, ad_pwdGraceUseTime )) == NULL)
903 ngut = ppb->pp.pwdGraceLoginLimit;
905 for(ngut=0; a->a_nvals[ngut].bv_val; ngut++);
906 ngut = ppb->pp.pwdGraceLoginLimit - ngut;
910 * ngut is the number of remaining grace logins
913 LDAP_LOG( OPERATION, ERR,
914 "ppolicy_bind: Entry %s has an expired password: %d grace logins\n",
915 e->e_name.bv_val, ngut, 0);
917 Debug( LDAP_DEBUG_ANY,
918 "ppolicy_bind: Entry %s has an expired password: %d grace logins\n",
919 e->e_name.bv_val, ngut, 0);
923 ppb->pErr = PP_passwordExpired;
924 rs->sr_err = LDAP_INVALID_CREDENTIALS;
929 * Add a grace user time to the entry
931 m = ch_calloc( sizeof(Modifications), 1 );
932 m->sml_op = LDAP_MOD_ADD;
933 m->sml_type = ad_pwdGraceUseTime->ad_cname;
934 m->sml_desc = ad_pwdGraceUseTime;
935 m->sml_values = ch_calloc( sizeof(struct berval), 2 );
936 ber_str2bv( nowstr, 0, 1, &m->sml_values[0] );
940 check_expiring_password:
942 * Now we need to check to see
943 * if it is about to expire, and if so, should the user
944 * be warned about it in the password policy control.
946 * If the password has expired, and we're in the grace period, then
947 * we don't need to do this bit. Similarly, if we don't have password
948 * aging, then there's no need to do this bit either.
950 if ((ppb->pp.pwdMaxAge < 1) || (pwExpired) || (ppb->pp.pwdExpireWarning < 1))
953 age = (int)(now - pwtime);
956 * We know that there is a password Change Time attribute - if
957 * there wasn't, then the pwdExpired value would be true, unless
958 * there is no password aging - and if there is no password aging,
959 * then this section isn't called anyway - you can't have an
960 * expiring password if there's no limit to expire.
962 if (ppb->pp.pwdMaxAge - age < ppb->pp.pwdExpireWarning ) {
964 * Set the warning value, add expiration warned timestamp to the entry.
966 if ((a = attr_find( e->e_attrs, ad_pwdExpirationWarned )) == NULL) {
967 m = ch_calloc( sizeof(Modifications), 1 );
968 m->sml_op = LDAP_MOD_ADD;
969 m->sml_type = ad_pwdExpirationWarned->ad_cname;
970 m->sml_desc = ad_pwdExpirationWarned;
971 m->sml_values = ch_calloc( sizeof(struct berval), 2 );
972 ber_str2bv( nowstr, 0, 1, &m->sml_values[0] );
977 warn = ppb->pp.pwdMaxAge - age; /* seconds left until expiry */
978 if (warn < 0) warn = 0; /* something weird here - why is pwExpired not set? */
981 LDAP_LOG( OPERATIONS, DETAIL1,
982 "ppolicy_bind: Setting warning for password expiry for %s = %d seconds\n",
983 op->o_req_dn.bv_val, warn, 0 );
985 Debug( LDAP_DEBUG_ANY,
986 "ppolicy_bind: Setting warning for password expiry for %s = %d seconds\n",
987 op->o_req_dn.bv_val, warn, 0 );
993 op->o_bd->bd_info = (BackendInfo *)on->on_info;
994 be_entry_release_r( op, e );
999 SlapReply r2 = { REP_RESULT };
1000 slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
1002 /* FIXME: Need to handle replication of some (but not all)
1003 * of the operational attributes...
1005 op2.o_tag = LDAP_REQ_MODIFY;
1006 op2.o_callback = &cb;
1007 op2.orm_modlist = mod;
1008 op2.o_dn = op->o_bd->be_rootdn;
1009 op2.o_ndn = op->o_bd->be_rootndn;
1010 op2.o_bd->bd_info = (BackendInfo *)on->on_info;
1011 rc = op->o_bd->be_modify( &op2, &r2 );
1012 slap_mods_free( mod );
1015 if ( ppb->send_ctrl ) {
1016 LDAPControl **ctrls = NULL;
1017 pp_info *pi = on->on_bi.bi_private;
1019 /* Do we really want to tell that the account is locked? */
1020 if ( ppb->pErr == PP_accountLocked && !pi->use_lockout ) {
1021 ppb->pErr = PP_noError;
1023 ctrls = ch_calloc( sizeof( LDAPControl *) , 2 );
1024 ctrls[0] = create_passcontrol( warn, ngut, ppb->pErr );
1026 rs->sr_ctrls = ctrls;
1028 op->o_bd->bd_info = bi;
1029 return SLAP_CB_CONTINUE;
1033 ppolicy_bind( Operation *op, SlapReply *rs )
1035 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1037 /* Root bypasses policy */
1038 if ( !be_isroot( op->o_bd, &op->o_req_ndn )) {
1044 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1045 rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &e );
1047 if ( rc != LDAP_SUCCESS ) {
1048 return SLAP_CB_CONTINUE;
1051 cb = op->o_tmpcalloc( sizeof(ppbind)+sizeof(slap_callback),
1052 1, op->o_tmpmemctx );
1053 ppb = (ppbind *)(cb+1);
1055 ppb->pErr = PP_noError;
1057 /* Setup a callback so we can munge the result */
1059 cb->sc_response = ppolicy_bind_resp;
1060 cb->sc_next = op->o_callback->sc_next;
1061 cb->sc_private = ppb;
1062 op->o_callback->sc_next = cb;
1064 /* Did we receive a password policy request control? */
1065 for ( i=0; op->o_ctrls && op->o_ctrls[i]; i++ ) {
1066 if ( !strcmp( op->o_ctrls[i]->ldctl_oid, LDAP_CONTROL_PASSWORDPOLICYREQUEST ) ) {
1072 op->o_bd->bd_info = (BackendInfo *)on;
1073 ppolicy_get( op, e, &ppb->pp );
1075 rc = account_locked( op, e, &ppb->pp, &ppb->mod );
1077 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1078 be_entry_release_r( op, e );
1081 /* This will be the Draft 8 response, Unwilling is bogus */
1082 ppb->pErr = PP_accountLocked;
1083 send_ldap_error( op, rs, LDAP_INVALID_CREDENTIALS, NULL );
1089 return SLAP_CB_CONTINUE;
1092 /* Reset the restrict flag for the next session on this connection */
1094 ppolicy_unbind( Operation *op, SlapReply *rs )
1096 pwcons[op->o_conn->c_conn_idx].restrict = 0;
1097 return SLAP_CB_CONTINUE;
1100 /* Check if this connection is restricted */
1106 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1107 int i, send_ctrl = 0;
1109 /* Did we receive a password policy request control? */
1110 for ( i=0; op->o_ctrls && op->o_ctrls[i]; i++ ) {
1111 if ( !strcmp( op->o_ctrls[i]->ldctl_oid,
1112 LDAP_CONTROL_PASSWORDPOLICYREQUEST ) ) {
1118 if ( op->o_conn && pwcons[op->o_conn->c_conn_idx].restrict ) {
1120 LDAP_LOG( OPERATION, ENTRY,
1121 "connection restricted to password changing only\n", 0, 0, 0 );
1123 Debug( LDAP_DEBUG_TRACE,
1124 "connection restricted to password changing only\n", 0, 0, 0);
1127 LDAPControl **ctrls = NULL;
1129 ctrls = ch_calloc( sizeof( LDAPControl *) , 2 );
1130 ctrls[0] = create_passcontrol( -1, -1, PP_changeAfterReset );
1132 rs->sr_ctrls = ctrls;
1134 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1135 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
1136 "Operations are restricted to bind/unbind/abandon/StartTLS/modify password" );
1140 return SLAP_CB_CONTINUE;
1148 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1153 if ( ppolicy_restrict( op, rs ) != SLAP_CB_CONTINUE )
1156 /* Check for password in entry */
1157 if ((pa = attr_find( op->oq_add.rs_e->e_attrs, slap_schema.si_ad_userPassword ))) {
1159 * new entry contains a password - if we're not the root user
1160 * then we need to check that the password fits in with the
1161 * security policy for the new entry.
1163 ppolicy_get( op, op->oq_add.rs_e, &pp );
1164 if (pp.pwdCheckQuality > 0 && !be_isroot( op->o_bd, &op->o_ndn )) {
1165 struct berval *bv = &(pa->a_vals[0]);
1166 int rc, i, send_ctrl = 0;
1167 LDAPPasswordPolicyError pErr = PP_noError;
1169 /* Did we receive a password policy request control? */
1170 for ( i=0; op->o_ctrls && op->o_ctrls[i]; i++ ) {
1171 if ( !strcmp( op->o_ctrls[i]->ldctl_oid,
1172 LDAP_CONTROL_PASSWORDPOLICYREQUEST ) ) {
1177 rc = check_password_quality( bv, &pp, &pErr );
1178 if (rc != LDAP_SUCCESS) {
1179 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1181 LDAPControl **ctrls = NULL;
1183 ctrls = ch_calloc( sizeof( LDAPControl *) , 2 );
1184 ctrls[0] = create_passcontrol( -1, -1, pErr );
1186 rs->sr_ctrls = ctrls;
1188 send_ldap_error( op, rs, rc, "Password fails quality checking policy" );
1192 /* If password aging is in effect, set the pwdChangedTime */
1193 if ( pp.pwdMaxAge || pp.pwdMinAge ) {
1194 struct berval timestamp;
1195 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
1197 time_t now = slap_get_time();
1199 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
1200 ltm = gmtime( &now );
1201 lutil_gentime( timebuf, sizeof(timebuf), ltm );
1202 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
1204 timestamp.bv_val = timebuf;
1205 timestamp.bv_len = strlen(timebuf);
1207 attr_merge_one( op->ora_e, ad_pwdChangedTime, ×tamp, NULL );
1210 return SLAP_CB_CONTINUE;
1214 ppolicy_modify( Operation *op, SlapReply *rs )
1216 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1217 int i, rc, mod_pw_only, pwmod, pwmop, deladd, hsize = 0;
1219 Modifications *mods = NULL, *modtail, *ml, *delmod, *addmod;
1220 Attribute *pa, *ha, *ra, at;
1221 int repl_user = be_isupdate( op->o_bd, &op->o_ndn );
1223 pw_hist *tl = NULL, *p;
1224 int zapReset, send_ctrl = 0;
1226 struct berval newpw = { 0, NULL }, oldpw = { 0, NULL }, *bv, cr[2];
1227 LDAPPasswordPolicyError pErr = PP_noError;
1229 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1230 rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &e );
1231 op->o_bd->bd_info = (BackendInfo *)on;
1233 if ( rc != LDAP_SUCCESS ) return SLAP_CB_CONTINUE;
1235 /* Did we receive a password policy request control? */
1236 for ( i=0; op->o_ctrls && op->o_ctrls[i]; i++ ) {
1237 if ( !strcmp( op->o_ctrls[i]->ldctl_oid,
1238 LDAP_CONTROL_PASSWORDPOLICYREQUEST ) ) {
1244 /* See if this is a pwdModify exop. If so, we can
1245 * access the plaintext passwords from that request.
1250 for ( sc = op->o_callback; sc; sc=sc->sc_next ) {
1251 if ( sc->sc_response == slap_replog_cb &&
1253 req_pwdexop_s *qpw = sc->sc_private;
1254 newpw = qpw->rs_new;
1255 oldpw = qpw->rs_old;
1261 ppolicy_get( op, e, &pp );
1263 for(ml = op->oq_modify.rs_modlist,
1264 pwmod = 0, mod_pw_only = 1,
1265 deladd = 0, delmod = NULL,
1268 ml != NULL; modtail = ml, ml = ml->sml_next ) {
1269 if ( ml->sml_desc == pp.ad ) {
1272 if ((deladd == 0) && (ml->sml_op == LDAP_MOD_DELETE) &&
1273 (ml->sml_values) && (ml->sml_values[0].bv_val != NULL)) {
1278 if ((deladd == 1) && ((ml->sml_op == LDAP_MOD_ADD) ||
1279 (ml->sml_op == LDAP_MOD_REPLACE)))
1282 if ((ml->sml_op == LDAP_MOD_ADD) ||
1283 (ml->sml_op == LDAP_MOD_REPLACE))
1285 } else if (! is_at_operational( ml->sml_desc->ad_type )) {
1287 /* modifying something other than password */
1291 * If there is a request to explicitly add a pwdReset
1292 * attribute, then we suppress the normal behaviour on
1293 * password change, which is to remove the pwdReset
1296 * This enables an administrator to assign a new password
1297 * and place a "must reset" flag on the entry, which will
1298 * stay until the user explicitly changes his/her password.
1300 if (ml->sml_desc == ad_pwdReset ) {
1301 if ((ml->sml_op == LDAP_MOD_ADD) ||
1302 (ml->sml_op == LDAP_MOD_REPLACE))
1307 if (pwcons[op->o_conn->c_conn_idx].restrict && !mod_pw_only) {
1309 LDAP_LOG( OPERATION, ENTRY,
1310 "connection restricted to password changing only\n", 0, 0, 0 );
1312 Debug( LDAP_DEBUG_TRACE,
1313 "connection restricted to password changing only\n", 0, 0, 0 );
1315 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1316 rs->sr_text = "Operations are restricted to bind/unbind/abandon/StartTLS/modify password";
1317 pErr = PP_changeAfterReset;
1318 goto return_results;
1322 * if we have a "safe password modify policy", then we need to check if we're doing
1323 * a delete (with the old password), followed by an add (with the new password).
1325 * If we don't have this, then we fail with an error. We also skip all the checks if
1326 * the root user is bound. Root can do anything, including avoid the policies.
1329 if (!pwmod) goto do_modify;
1332 * Did we get a valid add mod?
1336 rs->sr_err = LDAP_OTHER;
1337 rs->sr_text = "Internal Error";
1339 LDAP_LOG( OPERATION, ENTRY,
1340 "cannot locate modification supplying new password\n", 0, 0, 0 );
1342 Debug( LDAP_DEBUG_TRACE,
1343 "cannot locate modification supplying new password\n", 0, 0, 0 );
1345 goto return_results;
1349 * Build the password history list in ascending time order
1350 * We need this, even if the user is root, in order to maintain
1351 * the pwdHistory operational attributes properly.
1353 if (pp.pwdInHistory > 0 && (ha = attr_find( e->e_attrs, ad_pwdHistory ))) {
1354 struct berval oldpw;
1358 for(i=0; ha->a_nvals[i].bv_val; i++) {
1359 rc = parse_pwdhistory( &(ha->a_nvals[i]), NULL,
1362 if (rc != LDAP_SUCCESS) continue; /* invalid history entry */
1365 add_to_pwd_history( &tl, oldtime, &oldpw,
1366 &(ha->a_nvals[i]) );
1367 oldpw.bv_val = NULL;
1371 for(p=tl; p; p=p->next, hsize++); /* count history size */
1374 if (be_isroot( op->o_bd, &op->o_ndn)) goto do_modify;
1376 /* This is a pwdModify exop that provided the old pw.
1377 * We need to create a Delete mod for this old pw and
1378 * let the matching value get found later
1380 if (pp.pwdSafeModify && oldpw.bv_val ) {
1381 ml = (Modifications *) ch_malloc( sizeof( Modifications ) );
1382 ml->sml_op = LDAP_MOD_DELETE;
1383 ml->sml_desc = pp.ad;
1384 ml->sml_type = pp.ad->ad_cname;
1385 ml->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1386 ber_dupbv( &ml->sml_values[0], &oldpw );
1387 ml->sml_values[1].bv_len = 0;
1388 ml->sml_values[1].bv_val = NULL;
1389 ml->sml_nvalues = NULL;
1390 ml->sml_next = op->orm_modlist;
1391 op->orm_modlist = ml;
1396 if (pp.pwdSafeModify && deladd != 2) {
1398 LDAP_LOG( OPERATION, ENTRY,
1399 "change password must use DELETE followed by ADD/REPLACE\n",
1402 Debug( LDAP_DEBUG_TRACE,
1403 "change password must use DELETE followed by ADD/REPLACE\n",
1406 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1407 rs->sr_text = "Must supply old password to be changed as well as new one";
1408 pErr = PP_mustSupplyOldPassword;
1409 goto return_results;
1412 if (!pp.pwdAllowUserChange) {
1413 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1414 rs->sr_text = "User alteration of password is not allowed";
1415 pErr = PP_passwordModNotAllowed;
1416 goto return_results;
1419 if (pp.pwdMinAge > 0) {
1420 time_t pwtime = (time_t)-1, now;
1423 if ((pa = attr_find( e->e_attrs, ad_pwdChangedTime )) != NULL)
1424 pwtime = parse_time( pa->a_nvals[0].bv_val );
1425 now = slap_get_time();
1426 age = (int)(now - pwtime);
1427 if ((pwtime != (time_t)-1) && (age < pp.pwdMinAge)) {
1428 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1429 rs->sr_text = "Password is too young to change";
1430 pErr = PP_passwordTooYoung;
1431 goto return_results;
1435 if (delmod && (pa = attr_find( e->e_attrs, pp.ad )) != NULL) {
1437 * we have a password to check
1441 bv = oldpw.bv_val ? &oldpw : delmod->sml_values;
1442 rc = slap_passwd_check( op->o_conn, pa, bv, &txt );
1443 if (rc != LDAP_SUCCESS) {
1445 LDAP_LOG( OPERATION, ENTRY,
1446 "old password check failed: %s\n", txt, 0, 0 );
1448 Debug( LDAP_DEBUG_TRACE,
1449 "old password check failed: %s\n", txt, 0, 0 );
1452 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1453 rs->sr_text = "Must supply correct old password to change to new one";
1454 pErr = PP_mustSupplyOldPassword;
1455 goto return_results;
1461 * replace the delete value with the (possibly hashed)
1462 * value which is currently in the password.
1464 for(i=0; delmod->sml_values[i].bv_val; i++) {
1465 free(delmod->sml_values[i].bv_val);
1466 delmod->sml_values[i].bv_len = 0;
1468 free(delmod->sml_values);
1469 delmod->sml_values = ch_calloc( sizeof(struct berval), 2 );
1470 delmod->sml_values[1].bv_len = 0;
1471 delmod->sml_values[1].bv_val = NULL;
1472 ber_dupbv(&(delmod->sml_values[0]), &(pa->a_nvals[0]));
1476 bv = newpw.bv_val ? &newpw : addmod->sml_values;
1477 if (pp.pwdCheckQuality > 0) {
1479 rc = check_password_quality( bv, &pp, &pErr );
1480 if (rc != LDAP_SUCCESS) {
1482 rs->sr_text = "Password fails quality checking policy";
1483 goto return_results;
1488 * Last check - the password history.
1490 if (slap_passwd_check( op->o_conn, pa, bv, &txt ) == LDAP_SUCCESS) {
1492 * This is bad - it means that the user is attempting
1493 * to set the password to the same as the old one.
1495 rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
1496 rs->sr_text = "Password is not being changed from existing value";
1497 pErr = PP_passwordInHistory;
1498 goto return_results;
1501 if (pp.pwdInHistory < 1) goto do_modify;
1504 * Iterate through the password history, and fail on any
1509 cr[1].bv_val = NULL;
1510 for(p=tl; p; p=p->next) {
1512 rc = slap_passwd_check( op->o_conn, &at, bv, &txt );
1514 if (rc != LDAP_SUCCESS) continue;
1516 rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
1517 rs->sr_text = "Password is in history of old passwords";
1518 pErr = PP_passwordInHistory;
1519 goto return_results;
1523 if ((pwmod) && (!repl_user)) {
1524 struct berval timestamp;
1525 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
1527 time_t now = slap_get_time();
1531 * keep the necessary pwd.. operational attributes
1535 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
1536 ltm = gmtime( &now );
1537 lutil_gentime( timebuf, sizeof(timebuf), ltm );
1538 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
1540 timestamp.bv_val = timebuf;
1541 timestamp.bv_len = strlen(timebuf);
1542 mods = (Modifications *) ch_malloc( sizeof( Modifications ) );
1543 mods->sml_type.bv_val = NULL;
1544 mods->sml_desc = ad_pwdChangedTime;
1545 if (pwmop != LDAP_MOD_DELETE) {
1546 mods->sml_op = LDAP_MOD_REPLACE;
1547 mods->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1548 ber_dupbv( &mods->sml_values[0], ×tamp );
1549 mods->sml_values[1].bv_len = 0;
1550 mods->sml_values[1].bv_val = NULL;
1551 assert( mods->sml_values[0].bv_val );
1553 mods->sml_op = LDAP_MOD_DELETE;
1554 mods->sml_values = NULL;
1556 mods->sml_nvalues = NULL;
1557 mods->sml_next = NULL;
1558 modtail->sml_next = mods;
1561 if (attr_find(e->e_attrs, ad_pwdGraceUseTime )) {
1562 mods = (Modifications *) ch_malloc( sizeof( Modifications ) );
1563 mods->sml_op = LDAP_MOD_DELETE;
1564 mods->sml_type.bv_val = NULL;
1565 mods->sml_desc = ad_pwdGraceUseTime;
1566 mods->sml_values = NULL;
1567 mods->sml_nvalues = NULL;
1568 mods->sml_next = NULL;
1569 modtail->sml_next = mods;
1573 if (attr_find(e->e_attrs, ad_pwdExpirationWarned )) {
1574 mods = (Modifications *) ch_malloc( sizeof( Modifications ) );
1575 mods->sml_op = LDAP_MOD_DELETE;
1576 mods->sml_type.bv_val = NULL;
1577 mods->sml_desc = ad_pwdExpirationWarned;
1578 mods->sml_values = NULL;
1579 mods->sml_nvalues = NULL;
1580 mods->sml_next = NULL;
1581 modtail->sml_next = mods;
1585 /* Delete the pwdReset attribute, since it's being reset */
1586 if ((zapReset) && (attr_find(e->e_attrs, ad_pwdReset ))) {
1587 mods = (Modifications *) ch_malloc( sizeof( Modifications ) );
1588 mods->sml_op = LDAP_MOD_DELETE;
1589 mods->sml_type.bv_val = NULL;
1590 mods->sml_desc = ad_pwdReset;
1591 mods->sml_values = NULL;
1592 mods->sml_nvalues = NULL;
1593 mods->sml_next = NULL;
1594 modtail->sml_next = mods;
1598 if (pp.pwdInHistory > 0) {
1599 if (hsize >= pp.pwdInHistory) {
1601 * We use the >= operator, since we are going to add
1602 * the existing password attribute value into the
1603 * history - thus the cardinality of history values is
1604 * about to rise by one.
1606 * If this would push it over the limit of history
1607 * values (remembering - the password policy could have
1608 * changed since the password was last altered), we must
1609 * delete at least 1 value from the pwdHistory list.
1611 * In fact, we delete '(#pwdHistory attrs - max pwd
1612 * history length) + 1' values, starting with the oldest.
1613 * This is easily evaluated, since the linked list is
1614 * created in ascending time order.
1616 mods = (Modifications *) ch_malloc( sizeof( Modifications ) );
1617 mods->sml_op = LDAP_MOD_DELETE;
1618 mods->sml_type.bv_val = NULL;
1619 mods->sml_desc = ad_pwdHistory;
1620 mods->sml_nvalues = NULL;
1621 mods->sml_values = ch_calloc( sizeof( struct berval ),
1622 hsize - pp.pwdInHistory + 2 );
1623 mods->sml_values[ hsize - pp.pwdInHistory + 1 ].bv_val = NULL;
1624 mods->sml_values[ hsize - pp.pwdInHistory + 1 ].bv_len = 0;
1625 for(i=0,p=tl; i < (hsize - pp.pwdInHistory + 1); i++, p=p->next) {
1626 mods->sml_values[i].bv_val = NULL;
1627 mods->sml_values[i].bv_len = 0;
1628 ber_dupbv( &(mods->sml_values[i]), &p->bv );
1630 mods->sml_next = NULL;
1631 modtail->sml_next = mods;
1634 free_pwd_history_list( &tl );
1637 * Now add the existing password into the history list.
1638 * This will be executed even if the operation is to delete
1639 * the password entirely.
1641 * This isn't in the spec explicitly, but it seems to make
1642 * sense that the password history list is the list of all
1643 * previous passwords - even if they were deleted. Thus, if
1644 * someone tries to add a historical password at some future
1645 * point, it will fail.
1647 if ((pa = attr_find( e->e_attrs, pp.ad )) != NULL) {
1648 mods = (Modifications *) ch_malloc( sizeof( Modifications ) );
1649 mods->sml_op = LDAP_MOD_ADD;
1650 mods->sml_type.bv_val = NULL;
1651 mods->sml_desc = ad_pwdHistory;
1652 mods->sml_nvalues = NULL;
1653 mods->sml_values = ch_calloc( sizeof( struct berval ), 2 );
1654 mods->sml_values[ 1 ].bv_val = NULL;
1655 mods->sml_values[ 1 ].bv_len = 0;
1656 make_pwd_history_value( timebuf, &mods->sml_values[0], pa );
1657 mods->sml_next = NULL;
1658 modtail->sml_next = mods;
1662 LDAP_LOG ( OPERATION, ERR,
1663 "ppolicy_modify: password attr lookup failed\n", 0, 0, 0 );
1665 Debug( LDAP_DEBUG_TRACE,
1666 "ppolicy_modify: password attr lookup failed\n", 0, 0, 0 );
1672 * Controversial bit here. If the new password isn't hashed
1673 * (ie, is cleartext), we probably should hash it according
1674 * to the default hash. The reason for this is that we want
1675 * to use the policy if possible, but if we hash the password
1676 * before, then we're going to run into trouble when it
1677 * comes time to check the password.
1679 * Now, the right thing to do is to use the extended password
1680 * modify operation, but not all software can do this,
1681 * therefore it makes sense to hash the new password, now
1682 * we know it passes the policy requirements.
1684 * Of course, if the password is already hashed, then we
1688 if ((addmod) && !newpw.bv_val &&
1689 (password_scheme( &(addmod->sml_values[0]), NULL ) != LDAP_SUCCESS)) {
1690 struct berval hpw, bv;
1692 slap_passwd_hash( &(addmod->sml_values[0]), &hpw, &txt );
1693 if (hpw.bv_val == NULL) {
1695 * hashing didn't work. Emit an error.
1697 rs->sr_err = LDAP_OTHER;
1699 goto return_results;
1701 bv.bv_val = addmod->sml_values[0].bv_val;
1702 bv.bv_len = addmod->sml_values[0].bv_len;
1703 /* clear and discard the clear password */
1704 memset(bv.bv_val, 0, bv.bv_len);
1705 ber_memfree(bv.bv_val);
1706 addmod->sml_values[0].bv_val = hpw.bv_val;
1707 addmod->sml_values[0].bv_len = hpw.bv_len;
1710 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1711 be_entry_release_r( op, e );
1712 return SLAP_CB_CONTINUE;
1715 free_pwd_history_list( &tl );
1716 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1717 be_entry_release_r( op, e );
1719 LDAPControl **ctrls = NULL;
1721 ctrls = ch_calloc( sizeof( LDAPControl *) , 2 );
1722 ctrls[0] = create_passcontrol( -1, -1, pErr );
1724 rs->sr_ctrls = ctrls;
1726 send_ldap_result( op, rs );
1736 if ( ctrl->ldctl_value.bv_len ) {
1737 rs->sr_text = "passwordPolicyRequest control value not empty";
1738 return LDAP_PROTOCOL_ERROR;
1740 if ( ctrl->ldctl_iscritical ) {
1741 rs->sr_text = "passwordPolicyRequest control invalid criticality";
1742 return LDAP_PROTOCOL_ERROR;
1745 return LDAP_SUCCESS;
1753 slap_overinst *on = (slap_overinst *) be->bd_info;
1755 on->on_bi.bi_private = ch_calloc( sizeof(pp_info), 1 );
1757 if ( dtblsize && !pwcons )
1758 pwcons = ch_calloc(sizeof(pw_conn), dtblsize );
1768 slap_overinst *on = (slap_overinst *) be->bd_info;
1769 pp_info *pi = on->on_bi.bi_private;
1772 free( pi->def_policy.bv_val );
1787 slap_overinst *on = (slap_overinst *) be->bd_info;
1788 pp_info *pi = on->on_bi.bi_private;
1792 if ( strcasecmp( argv[0], "ppolicy_default" ) == 0 ) {
1794 fprintf( stderr, "%s: line %d: invalid arguments in \"ppolicy_default"
1795 " <policyDN>\n", fname, lineno );
1798 ber_str2bv( argv[1], 0, 0, &dn );
1799 if ( dnNormalize( 0, NULL, NULL, &dn, &pi->def_policy, NULL ) ) {
1800 fprintf( stderr, "%s: line %d: policyDN is invalid\n",
1805 } else if ( strcasecmp( argv[0], "ppolicy_use_lockout" ) == 0 ) {
1807 fprintf( stderr, "%s: line %d: ppolicy_use_lockout "
1808 "takes no arguments\n", fname, lineno );
1811 pi->use_lockout = 1;
1813 return SLAP_CONF_UNKNOWN;
1816 static char *extops[] = {
1817 LDAP_EXOP_MODIFY_PASSWD,
1821 static slap_overinst ppolicy;
1825 LDAPAttributeType *at;
1830 for (i=0; pwd_OpSchema[i].def; i++) {
1831 at = ldap_str2attributetype( pwd_OpSchema[i].def, &code, &err,
1832 LDAP_SCHEMA_ALLOW_ALL );
1834 fprintf( stderr, "AttributeType Load failed %s %s\n",
1835 ldap_scherr2str(code), err );
1838 code = at_add( at, &err );
1840 slap_str2ad( at->at_names[0], pwd_OpSchema[i].ad, &err );
1844 fprintf( stderr, "AttributeType Load failed %s %s\n",
1845 scherr2str(code), err );
1850 for (i=0; pwd_UsSchema[i].def; i++) {
1851 code = slap_str2ad( pwd_UsSchema[i].def, pwd_UsSchema[i].ad, &err );
1853 fprintf( stderr, "User Schema Load failed %d: %s\n", code, err );
1858 code = register_supported_control( LDAP_CONTROL_PASSWORDPOLICYREQUEST,
1859 SLAP_CTRL_ADD|SLAP_CTRL_BIND|SLAP_CTRL_MODIFY, extops,
1860 ppolicy_parseCtrl );
1861 if ( code != LDAP_SUCCESS ) {
1862 fprintf( stderr, "Failed to register control %d\n", code );
1866 ldap_pvt_thread_mutex_init( &chk_syntax_mutex );
1868 ppolicy.on_bi.bi_type = "ppolicy";
1869 ppolicy.on_bi.bi_db_init = ppolicy_db_init;
1870 ppolicy.on_bi.bi_db_config = ppolicy_config;
1871 ppolicy.on_bi.bi_db_close = ppolicy_close;
1873 ppolicy.on_bi.bi_op_add = ppolicy_add;
1874 ppolicy.on_bi.bi_op_bind = ppolicy_bind;
1875 ppolicy.on_bi.bi_op_unbind = ppolicy_unbind;
1876 ppolicy.on_bi.bi_op_compare = ppolicy_restrict;
1877 ppolicy.on_bi.bi_op_delete = ppolicy_restrict;
1878 ppolicy.on_bi.bi_op_modify = ppolicy_modify;
1879 ppolicy.on_bi.bi_op_search = ppolicy_restrict;
1881 return overlay_register( &ppolicy );
1884 #if SLAPD_OVER_PPOLICY == SLAPD_MOD_DYNAMIC
1885 int init_module(int argc, char *argv[]) {
1886 return ppolicy_init();
1890 #endif /* defined(SLAPD_OVER_PPOLICY) */