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;
225 if ( tmg.tm_mday ) tmg.tm_hour += tmg.tm_mday > 0 ? 24 : -24;
226 if ( tmg.tm_hour ) t -= tmg.tm_hour * 3600;
227 tmg.tm_min -= tml.tm_min;
228 if ( tmg.tm_min ) t -= tmg.tm_min * 60;
229 tmg.tm_sec -= tml.tm_sec;
230 if ( tmg.tm_sec ) t -= tmg.tm_sec;
236 account_locked( Operation *op, Entry *e,
237 PassPolicy *pp, Modifications **mod )
245 if ( (la = attr_find( e->e_attrs, ad_pwdAccountLockedTime )) != NULL ) {
246 BerVarray vals = la->a_nvals;
249 * there is a lockout stamp - we now need to know if it's
252 if (vals[0].bv_val != NULL) {
257 if ((then = parse_time( vals[0].bv_val )) == (time_t)0)
260 now = slap_get_time();
262 if (now < then + pp->pwdLockoutDuration)
265 m = ch_calloc( sizeof(Modifications), 1 );
266 m->sml_op = LDAP_MOD_DELETE;
267 m->sml_type = ad_pwdAccountLockedTime->ad_cname;
268 m->sml_desc = ad_pwdAccountLockedTime;
277 #define PPOLICY_WARNING 0xa0L
278 #define PPOLICY_ERROR 0xa1L
280 #define PPOLICY_EXPIRE 0xa0L
281 #define PPOLICY_GRACE 0xa1L
284 create_passcontrol( int exptime, int grace, LDAPPasswordPolicyError err )
286 char berbuf[LBER_ELEMENT_SIZEOF], bb2[LBER_ELEMENT_SIZEOF];
287 BerElement *ber = (BerElement *)berbuf, *b2 = (BerElement *)bb2;
291 if ((c = ch_calloc( sizeof( LDAPControl ), 1 )) == NULL) return NULL;
292 c->ldctl_oid = LDAP_CONTROL_PASSWORDPOLICYRESPONSE;
293 c->ldctl_iscritical = 0;
294 c->ldctl_value.bv_val = NULL;
295 c->ldctl_value.bv_len = 0;
297 ber_init2( ber, NULL, LBER_USE_DER );
298 ber_printf(ber, "{" /*}*/ );
301 ber_init2( b2, NULL, LBER_USE_DER );
302 ber_printf( b2, "ti", PPOLICY_EXPIRE, exptime );
303 ber_flatten2( b2, &bv, 1 );
304 ber_printf( ber, "tO", PPOLICY_WARNING, &bv );
305 ch_free( bv.bv_val );
306 } else if (grace > 0) {
307 ber_init2( b2, NULL, LBER_USE_DER );
308 ber_printf( b2, "ti", PPOLICY_GRACE, grace );
309 ber_flatten2( b2, &bv, 1 );
310 ber_printf( ber, "tO", PPOLICY_WARNING, &bv );
311 ch_free( bv.bv_val );
314 if (err != PP_noError ) {
315 ber_printf( ber, "te", PPOLICY_ERROR, err );
317 ber_printf( ber, /*{*/ "N}" );
319 if (ber_flatten2( ber, &(c->ldctl_value), 1 ) == LBER_DEFAULT) {
321 (void)ber_free_buf(ber);
324 (void)ber_free_buf(ber);
329 ppolicy_get( Operation *op, Entry *e, PassPolicy *pp )
331 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
332 pp_info *pi = on->on_bi.bi_private;
337 AttributeDescription *oca = slap_schema.si_ad_objectClass;
339 AttributeDescription *ad;
341 void *opr = op->o_private;
343 memset( pp, 0, sizeof(PassPolicy) );
345 if ((a = attr_find( e->e_attrs, ad_pwdPolicySubentry )) == NULL) {
347 * entry has no password policy assigned - use default
349 vals = &pi->def_policy;
354 if (vals[0].bv_val == NULL) {
356 LDAP_LOG( OPERATION, ERR,
357 "ppolicy_get: NULL value for policySubEntry\n", 0, 0, 0 );
359 Debug( LDAP_DEBUG_ANY,
360 "ppolicy_get: NULL value for policySubEntry\n", 0, 0, 0 );
366 /* back-bdb stores lock info in o_private */
367 op->o_private = NULL;
368 op->o_bd->bd_info = (BackendInfo *)on->on_info;
369 rc = be_entry_get_rw( op, vals, NULL, NULL, 0, &pe );
370 op->o_bd->bd_info = (BackendInfo *)on;
372 if ( rc ) goto defaultpol;
374 #if 0 /* Only worry about userPassword for now */
375 if ((a = attr_find( pe->e_attrs, ad_pwdAttribute )))
376 slap_bv2ad( &a->a_vals[0], &pp->ad, &text );
378 pp->ad = slap_schema.si_ad_userPassword;
381 if ((a = attr_find( pe->e_attrs, ad_pwdMinAge )))
382 pp->pwdMinAge = atoi(a->a_vals[0].bv_val );
383 if ((a = attr_find( pe->e_attrs, ad_pwdMaxAge )))
384 pp->pwdMaxAge = atoi(a->a_vals[0].bv_val );
385 if ((a = attr_find( pe->e_attrs, ad_pwdInHistory )))
386 pp->pwdInHistory = atoi(a->a_vals[0].bv_val );
387 if ((a = attr_find( pe->e_attrs, ad_pwdCheckQuality )))
388 pp->pwdCheckQuality = atoi(a->a_vals[0].bv_val );
389 if ((a = attr_find( pe->e_attrs, ad_pwdMinLength )))
390 pp->pwdMinLength = atoi(a->a_vals[0].bv_val );
391 if ((a = attr_find( pe->e_attrs, ad_pwdMaxFailure )))
392 pp->pwdMaxFailure = atoi(a->a_vals[0].bv_val );
393 if ((a = attr_find( pe->e_attrs, ad_pwdGraceLoginLimit )))
394 pp->pwdGraceLoginLimit = atoi(a->a_vals[0].bv_val );
395 if ((a = attr_find( pe->e_attrs, ad_pwdExpireWarning )))
396 pp->pwdExpireWarning = atoi(a->a_vals[0].bv_val );
397 if ((a = attr_find( pe->e_attrs, ad_pwdFailureCountInterval )))
398 pp->pwdFailureCountInterval = atoi(a->a_vals[0].bv_val );
399 if ((a = attr_find( pe->e_attrs, ad_pwdLockoutDuration )))
400 pp->pwdLockoutDuration = atoi(a->a_vals[0].bv_val );
402 if ((a = attr_find( pe->e_attrs, ad_pwdCheckModule ))) {
403 strncpy(pp->pwdCheckModule, a->a_vals[0].bv_val,
404 sizeof(pp->pwdCheckModule));
405 pp->pwdCheckModule[sizeof(pp->pwdCheckModule)-1] = '\0';
408 if ((a = attr_find( pe->e_attrs, ad_pwdLockout )))
409 pp->pwdLockout = !strcmp( a->a_nvals[0].bv_val, "TRUE" );
410 if ((a = attr_find( pe->e_attrs, ad_pwdMustChange )))
411 pp->pwdMustChange = !strcmp( a->a_nvals[0].bv_val, "TRUE" );
412 if ((a = attr_find( pe->e_attrs, ad_pwdAllowUserChange )))
413 pp->pwdAllowUserChange = !strcmp( a->a_nvals[0].bv_val, "TRUE" );
414 if ((a = attr_find( pe->e_attrs, ad_pwdSafeModify )))
415 pp->pwdSafeModify = !strcmp( a->a_nvals[0].bv_val, "TRUE" );
417 op->o_bd->bd_info = (BackendInfo *)on->on_info;
418 be_entry_release_r( op, pe );
419 op->o_bd->bd_info = (BackendInfo *)on;
426 LDAP_LOG( OPERATION, DETAIL1,
427 "ppolicy_get: using default policy\n", 0, 0, 0 );
429 Debug( LDAP_DEBUG_ANY,
430 "ppolicy_get: using default policy\n", 0, 0, 0 );
437 password_scheme( struct berval *cred, struct berval *sch )
441 assert( cred != NULL );
448 if ((cred->bv_len == 0) || (cred->bv_val == NULL) ||
449 (cred->bv_val[0] != '{')) return LDAP_OTHER;
451 for(e = 1; cred->bv_val[e] && cred->bv_val[e] != '}'; e++);
452 if (cred->bv_val[e]) {
453 char *sc = ch_calloc( sizeof(char), e + 2);
454 sc[e + 1] = '\0'; /* terminate string */
455 strncpy( sc, cred->bv_val, e + 1);
456 e = lutil_passwd_scheme( sc );
459 sch->bv_val = cred->bv_val;
468 check_password_quality( struct berval *cred, PassPolicy *pp, LDAPPasswordPolicyError *err )
470 int rc = LDAP_SUCCESS, ok = LDAP_SUCCESS;
471 char *ptr = cred->bv_val;
475 assert( cred != NULL );
476 assert( pp != NULL );
478 if ((cred->bv_len == 0) || (pp->pwdMinLength > cred->bv_len)) {
479 rc = LDAP_CONSTRAINT_VIOLATION;
480 if ( err ) *err = PP_passwordTooShort;
485 * We need to know if the password is already hashed - if so
486 * what scheme is it. The reason being that the "hash" of
487 * {cleartext} still allows us to check the password.
489 rc = password_scheme( cred, &sch );
490 if (rc == LDAP_SUCCESS) {
491 if ((sch.bv_val) && (strncasecmp( sch.bv_val, "{cleartext}",
492 sch.bv_len ) == 0)) {
494 * We can check the cleartext "hash"
496 ptr = cred->bv_val + sch.bv_len;
498 /* everything else, we can't check */
499 if (pp->pwdCheckQuality == 2) {
500 rc = LDAP_CONSTRAINT_VIOLATION;
501 if (err) *err = PP_insufficientPasswordQuality;
505 * We can't check the syntax of the password, but it's not
506 * mandatory (according to the policy), so we return success.
515 if (pp->pwdCheckModule[0]) {
519 if ((mod = lt_dlopen( pp->pwdCheckModule )) == NULL) {
523 LDAP_LOG( SLAPD, CRIT,
524 "check_password_quality: lt_dlopen failed: (%s) %s.\n",
525 pp->pwdCheckModule, err, 0 );
527 Debug(LDAP_DEBUG_ANY,
528 "check_password_quality: lt_dlopen failed: (%s) %s.\n",
529 pp->pwdCheckModule, err, 0 );
531 ok = LDAP_OTHER; /* internal error */
533 int (*prog)( char *passwd, char **text, void *arg );
535 if ((prog = lt_dlsym( mod, "check_password" )) == NULL) {
539 LDAP_LOG( SLAPD, CRIT,
540 "check_password_quality: lt_dlsym failed: (%s) %s.\n",
541 pp->pwdCheckModule, err, 0 );
543 Debug(LDAP_DEBUG_ANY,
544 "check_password_quality: lt_dlsym failed: (%s) %s.\n",
545 pp->pwdCheckModule, err, 0 );
551 ldap_pvt_thread_mutex_lock( &chk_syntax_mutex );
552 ok = prog( cred->bv_val, &txt, NULL );
553 ldap_pvt_thread_mutex_unlock( &chk_syntax_mutex );
556 LDAP_LOG( SLAPD, CRIT,
557 "check_password_quality: module error (%s) %s[%d].\n",
558 pp->pwdCheckModule, txt, ok );
560 Debug(LDAP_DEBUG_ANY,
561 "check_password_quality: module error: (%s) %s.[%d]\n",
562 pp->pwdCheckModule, txt, ok );
574 if (ok != LDAP_SUCCESS) {
575 rc = LDAP_CONSTRAINT_VIOLATION;
576 if (err) *err = PP_insufficientPasswordQuality;
583 parse_pwdhistory( struct berval *bv, char **oid, time_t *oldtime, struct berval *oldpw )
586 struct berval nv, npw;
589 assert (bv && (bv->bv_len > 0) && (bv->bv_val) && oldtime && oldpw );
592 *oldtime = (time_t)-1;
593 oldpw->bv_val = NULL;
596 ber_dupbv( &nv, bv );
598 /* first get the time field */
599 for(i=0; (i < nv.bv_len) && (nv.bv_val[i] != '#'); i++);
600 if ( i == nv.bv_len) goto exit_failure; /* couldn't locate the '#' separator */
601 nv.bv_val[i++] = '\0'; /* terminate the string & move to next field */
603 *oldtime = parse_time( ptr );
604 if (*oldtime == (time_t)-1) goto exit_failure;
606 /* get the OID field */
607 for(ptr = &(nv.bv_val[i]);(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 */
610 if ( oid ) *oid = ber_strdup( ptr );
612 /* get the length field */
613 for(ptr = &(nv.bv_val[i]);(i < nv.bv_len) && (nv.bv_val[i] != '#'); i++);
614 if ( i == nv.bv_len) goto exit_failure; /* couldn't locate the '#' separator */
615 nv.bv_val[i++] = '\0'; /* terminate the string & move to next field */
616 oldpw->bv_len = strtol( ptr, NULL, 10 );
617 if (errno == ERANGE) goto exit_failure;
619 /* lastly, get the octets of the string */
620 for(j=i, ptr = &(nv.bv_val[i]);i < nv.bv_len; i++);
621 if (i-j != oldpw->bv_len) goto exit_failure; /* length is wrong */
624 npw.bv_len = oldpw->bv_len;
625 ber_dupbv( oldpw, &npw );
629 if (oid && *oid) { ber_memfree(*oid); *oid = NULL; }
631 ber_memfree( oldpw->bv_val); oldpw->bv_val = NULL;
634 ber_memfree(nv.bv_val);
639 add_to_pwd_history( pw_hist **l, time_t t,
640 struct berval *oldpw, struct berval *bv )
642 pw_hist *p, *p1, *p2;
646 p = ch_malloc( sizeof( pw_hist ));
648 ber_dupbv( &p->bv, bv );
653 /* degenerate case */
658 * advance p1 and p2 such that p1 is the node before the
659 * new one, and p2 is the node after it
661 for (p1 = NULL, p2 = *l; p2 && p2->t <= t; p1 = p2, p2=p2->next );
663 if (p1 == NULL) { *l = p; return; }
667 #ifndef MAX_PWD_HISTORY_SZ
668 #define MAX_PWD_HISTORY_SZ 1024
669 #endif /* MAX_PWD_HISTORY_SZ */
672 make_pwd_history_value( char *timebuf, struct berval *bv, Attribute *pa )
674 char str[ MAX_PWD_HISTORY_SZ ];
677 snprintf( str, MAX_PWD_HISTORY_SZ,
678 "%s#%s#%d#", timebuf,
679 pa->a_desc->ad_type->sat_syntax->ssyn_oid,
680 pa->a_nvals[0].bv_len );
681 str[MAX_PWD_HISTORY_SZ-1] = 0;
685 * We have to assume that the string is a string of octets,
686 * not readable characters. In reality, yes, it probably is
687 * a readable (ie, base64) string, but we can't count on that
688 * Hence, while the first 3 fields of the password history
689 * are definitely readable (a timestamp, an OID and an integer
690 * length), the remaining octets of the actual password
691 * are deemed to be binary data.
693 AC_MEMCPY( str + nlen, pa->a_nvals[0].bv_val, pa->a_nvals[0].bv_len );
694 nlen += pa->a_nvals[0].bv_len;
695 bv->bv_val = ch_malloc( nlen + 1 );
696 AC_MEMCPY( bv->bv_val, str, nlen );
697 bv->bv_val[nlen] = '\0';
702 free_pwd_history_list( pw_hist **l )
709 pw_hist *pp = p->next;
719 typedef struct ppbind {
723 LDAPPasswordPolicyError pErr;
728 ppolicy_bind_resp( Operation *op, SlapReply *rs )
730 ppbind *ppb = op->o_callback->sc_private;
731 slap_overinst *on = ppb->on;
732 Modifications *mod = ppb->mod, *m;
734 int ngut = -1, warn = -1, age, rc, i;
737 time_t now, then, pwtime = (time_t)-1;
739 char nowstr[ LDAP_LUTIL_GENTIME_BUFSIZE ];
740 BackendInfo *bi = op->o_bd->bd_info;
743 /* If we already know it's locked, just get on with it */
744 if ( ppb->pErr != PP_noError ) {
748 op->o_bd->bd_info = (BackendInfo *)on->on_info;
749 rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &e );
750 op->o_bd->bd_info = bi;
752 if ( rc != LDAP_SUCCESS ) {
753 return SLAP_CB_CONTINUE;
756 now = slap_get_time(); /* stored for later consideration */
757 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
759 lutil_gentime( nowstr, sizeof(nowstr), tm );
760 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
762 if ( rs->sr_err == LDAP_INVALID_CREDENTIALS ) {
765 m = ch_calloc( sizeof(Modifications), 1 );
766 m->sml_op = LDAP_MOD_ADD;
767 m->sml_type = ad_pwdFailureTime->ad_cname;
768 m->sml_desc = ad_pwdFailureTime;
769 m->sml_values = ch_calloc( sizeof(struct berval), 2 );
771 ber_str2bv( nowstr, 0, 1, &m->sml_values[0] );
776 * Count the pwdFailureTimes - if it's
777 * greater than the policy pwdMaxFailure,
778 * then lock the account.
780 if ((a = attr_find( e->e_attrs, ad_pwdFailureTime )) != NULL) {
781 for(i=0; a->a_nvals[i].bv_val; i++) {
784 * If the interval is 0, then failures
785 * stay on the record until explicitly
786 * reset by successful authentication.
788 if (ppb->pp.pwdFailureCountInterval == 0) {
791 parse_time(a->a_nvals[i].bv_val) +
792 ppb->pp.pwdFailureCountInterval) {
797 * We only count those failures
798 * which are not due to expire.
803 if ((ppb->pp.pwdMaxFailure > 0) &&
804 (fc >= ppb->pp.pwdMaxFailure - 1)) {
807 * We subtract 1 from the failure max
808 * because the new failure entry hasn't
809 * made it to the entry yet.
811 m = ch_calloc( sizeof(Modifications), 1 );
812 m->sml_op = LDAP_MOD_REPLACE;
813 m->sml_type = ad_pwdAccountLockedTime->ad_cname;
814 m->sml_desc = ad_pwdAccountLockedTime;
815 m->sml_values = ch_calloc( sizeof(struct berval), 2 );
816 ber_str2bv( nowstr, 0, 1, &m->sml_values[0] );
820 } else if ( rs->sr_err == LDAP_SUCCESS ) {
821 if ((a = attr_find( e->e_attrs, ad_pwdChangedTime )) != NULL)
822 pwtime = parse_time( a->a_nvals[0].bv_val );
824 /* delete all pwdFailureTimes */
825 if ( attr_find( e->e_attrs, ad_pwdFailureTime )) {
826 m = ch_calloc( sizeof(Modifications), 1 );
827 m->sml_op = LDAP_MOD_DELETE;
828 m->sml_type = ad_pwdFailureTime->ad_cname;
829 m->sml_desc = ad_pwdFailureTime;
835 * check to see if the password must be changed
837 if ( ppb->pp.pwdMustChange &&
838 (a = attr_find( e->e_attrs, ad_pwdReset )) &&
839 !strcmp( a->a_nvals[0].bv_val, "TRUE" ) ) {
841 * need to inject client controls here to give
842 * more information. For the moment, we ensure
843 * that we are disallowed from doing anything
844 * other than change password.
846 pwcons[op->o_conn->c_conn_idx].restrict = 1;
848 ppb->pErr = PP_changeAfterReset;
852 * the password does not need to be changed, so
853 * we now check whether the password has expired.
855 * We can skip this bit if passwords don't age in
858 if (ppb->pp.pwdMaxAge == 0) goto grace;
860 if (pwtime == (time_t)-1) {
862 * Hmm. No password changed time on the
863 * entry. This is odd - it should have
864 * been provided when the attribute was added.
866 * However, it's possible that it could be
867 * missing if the DIT was established via
871 LDAP_LOG( OPERATION, ERR,
872 "ppolicy_bind: Entry %s does not have valid pwdChangedTime attribute - assuming password expired\n",
873 e->e_name.bv_val, 0, 0);
875 Debug( LDAP_DEBUG_ANY,
876 "ppolicy_bind: Entry %s does not have valid pwdChangedTime attribute - assuming password expired\n",
877 e->e_name.bv_val, 0, 0);
883 * Check: was the last change time of
884 * the password older than the maximum age
885 * allowed. (Ignore case 2 from I-D, it's just silly.)
887 if (now - pwtime > ppb->pp.pwdMaxAge ) pwExpired = 1;
892 if (!pwExpired) goto check_expiring_password;
894 if ((a = attr_find( e->e_attrs, ad_pwdGraceUseTime )) == NULL)
895 ngut = ppb->pp.pwdGraceLoginLimit;
897 for(ngut=0; a->a_nvals[ngut].bv_val; ngut++);
898 ngut = ppb->pp.pwdGraceLoginLimit - ngut;
902 * ngut is the number of remaining grace logins
905 LDAP_LOG( OPERATION, ERR,
906 "ppolicy_bind: Entry %s has an expired password: %d grace logins\n",
907 e->e_name.bv_val, ngut, 0);
909 Debug( LDAP_DEBUG_ANY,
910 "ppolicy_bind: Entry %s has an expired password: %d grace logins\n",
911 e->e_name.bv_val, ngut, 0);
915 ppb->pErr = PP_passwordExpired;
916 rs->sr_err = LDAP_INVALID_CREDENTIALS;
921 * Add a grace user time to the entry
923 m = ch_calloc( sizeof(Modifications), 1 );
924 m->sml_op = LDAP_MOD_ADD;
925 m->sml_type = ad_pwdGraceUseTime->ad_cname;
926 m->sml_desc = ad_pwdGraceUseTime;
927 m->sml_values = ch_calloc( sizeof(struct berval), 2 );
928 ber_str2bv( nowstr, 0, 1, &m->sml_values[0] );
932 check_expiring_password:
934 * Now we need to check to see
935 * if it is about to expire, and if so, should the user
936 * be warned about it in the password policy control.
938 * If the password has expired, and we're in the grace period, then
939 * we don't need to do this bit. Similarly, if we don't have password
940 * aging, then there's no need to do this bit either.
942 if ((ppb->pp.pwdMaxAge < 1) || (pwExpired) || (ppb->pp.pwdExpireWarning < 1))
945 age = (int)(now - pwtime);
948 * We know that there is a password Change Time attribute - if
949 * there wasn't, then the pwdExpired value would be true, unless
950 * there is no password aging - and if there is no password aging,
951 * then this section isn't called anyway - you can't have an
952 * expiring password if there's no limit to expire.
954 if (ppb->pp.pwdMaxAge - age < ppb->pp.pwdExpireWarning ) {
956 * Set the warning value, add expiration warned timestamp to the entry.
958 if ((a = attr_find( e->e_attrs, ad_pwdExpirationWarned )) == NULL) {
959 m = ch_calloc( sizeof(Modifications), 1 );
960 m->sml_op = LDAP_MOD_ADD;
961 m->sml_type = ad_pwdExpirationWarned->ad_cname;
962 m->sml_desc = ad_pwdExpirationWarned;
963 m->sml_values = ch_calloc( sizeof(struct berval), 2 );
964 ber_str2bv( nowstr, 0, 1, &m->sml_values[0] );
969 warn = ppb->pp.pwdMaxAge - age; /* seconds left until expiry */
970 if (warn < 0) warn = 0; /* something weird here - why is pwExpired not set? */
973 LDAP_LOG( OPERATIONS, DETAIL1,
974 "ppolicy_bind: Setting warning for password expiry for %s = %d seconds\n",
975 op->o_req_dn.bv_val, warn, 0 );
977 Debug( LDAP_DEBUG_ANY,
978 "ppolicy_bind: Setting warning for password expiry for %s = %d seconds\n",
979 op->o_req_dn.bv_val, warn, 0 );
985 op->o_bd->bd_info = (BackendInfo *)on->on_info;
986 be_entry_release_r( op, e );
991 SlapReply r2 = { REP_RESULT };
992 slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
994 /* FIXME: Need to handle replication of some (but not all)
995 * of the operational attributes...
997 op2.o_tag = LDAP_REQ_MODIFY;
998 op2.o_callback = &cb;
999 op2.orm_modlist = mod;
1000 op2.o_dn = op->o_bd->be_rootdn;
1001 op2.o_ndn = op->o_bd->be_rootndn;
1002 op2.o_bd->bd_info = (BackendInfo *)on->on_info;
1003 rc = op->o_bd->be_modify( &op2, &r2 );
1004 slap_mods_free( mod );
1007 if ( ppb->send_ctrl ) {
1008 LDAPControl **ctrls = NULL;
1009 pp_info *pi = on->on_bi.bi_private;
1011 /* Do we really want to tell that the account is locked? */
1012 if ( ppb->pErr == PP_accountLocked && !pi->use_lockout ) {
1013 ppb->pErr = PP_noError;
1015 ctrls = ch_calloc( sizeof( LDAPControl *) , 2 );
1016 ctrls[0] = create_passcontrol( warn, ngut, ppb->pErr );
1018 rs->sr_ctrls = ctrls;
1020 op->o_bd->bd_info = bi;
1021 return SLAP_CB_CONTINUE;
1025 ppolicy_bind( Operation *op, SlapReply *rs )
1027 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1029 /* Root bypasses policy */
1030 if ( !be_isroot( op->o_bd, &op->o_req_ndn )) {
1036 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1037 rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &e );
1039 if ( rc != LDAP_SUCCESS ) {
1040 return SLAP_CB_CONTINUE;
1043 cb = op->o_tmpcalloc( sizeof(ppbind)+sizeof(slap_callback),
1044 1, op->o_tmpmemctx );
1045 ppb = (ppbind *)(cb+1);
1047 ppb->pErr = PP_noError;
1049 /* Setup a callback so we can munge the result */
1051 cb->sc_response = ppolicy_bind_resp;
1052 cb->sc_next = op->o_callback->sc_next;
1053 cb->sc_private = ppb;
1054 op->o_callback->sc_next = cb;
1056 /* Did we receive a password policy request control? */
1057 for ( i=0; op->o_ctrls && op->o_ctrls[i]; i++ ) {
1058 if ( !strcmp( op->o_ctrls[i]->ldctl_oid, LDAP_CONTROL_PASSWORDPOLICYREQUEST ) ) {
1064 op->o_bd->bd_info = (BackendInfo *)on;
1065 ppolicy_get( op, e, &ppb->pp );
1067 rc = account_locked( op, e, &ppb->pp, &ppb->mod );
1069 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1070 be_entry_release_r( op, e );
1073 /* This will be the Draft 8 response, Unwilling is bogus */
1074 ppb->pErr = PP_accountLocked;
1075 send_ldap_error( op, rs, LDAP_INVALID_CREDENTIALS, NULL );
1081 return SLAP_CB_CONTINUE;
1084 /* Reset the restrict flag for the next session on this connection */
1086 ppolicy_unbind( Operation *op, SlapReply *rs )
1088 pwcons[op->o_conn->c_conn_idx].restrict = 0;
1089 return SLAP_CB_CONTINUE;
1092 /* Check if this connection is restricted */
1098 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1099 int i, send_ctrl = 0;
1101 /* Did we receive a password policy request control? */
1102 for ( i=0; op->o_ctrls && op->o_ctrls[i]; i++ ) {
1103 if ( !strcmp( op->o_ctrls[i]->ldctl_oid,
1104 LDAP_CONTROL_PASSWORDPOLICYREQUEST ) ) {
1110 if ( op->o_conn && pwcons[op->o_conn->c_conn_idx].restrict ) {
1112 LDAP_LOG( OPERATION, ENTRY,
1113 "connection restricted to password changing only\n", 0, 0, 0 );
1115 Debug( LDAP_DEBUG_TRACE,
1116 "connection restricted to password changing only\n", 0, 0, 0);
1119 LDAPControl **ctrls = NULL;
1121 ctrls = ch_calloc( sizeof( LDAPControl *) , 2 );
1122 ctrls[0] = create_passcontrol( -1, -1, PP_changeAfterReset );
1124 rs->sr_ctrls = ctrls;
1126 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1127 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
1128 "Operations are restricted to bind/unbind/abandon/StartTLS/modify password" );
1132 return SLAP_CB_CONTINUE;
1140 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1145 if ( ppolicy_restrict( op, rs ) != SLAP_CB_CONTINUE )
1148 /* Check for password in entry */
1149 if ((pa = attr_find( op->oq_add.rs_e->e_attrs, slap_schema.si_ad_userPassword ))) {
1151 * new entry contains a password - if we're not the root user
1152 * then we need to check that the password fits in with the
1153 * security policy for the new entry.
1155 ppolicy_get( op, op->oq_add.rs_e, &pp );
1156 if (pp.pwdCheckQuality > 0 && !be_isroot( op->o_bd, &op->o_ndn )) {
1157 struct berval *bv = &(pa->a_vals[0]);
1158 int rc, i, send_ctrl = 0;
1159 LDAPPasswordPolicyError pErr = PP_noError;
1161 /* Did we receive a password policy request control? */
1162 for ( i=0; op->o_ctrls && op->o_ctrls[i]; i++ ) {
1163 if ( !strcmp( op->o_ctrls[i]->ldctl_oid,
1164 LDAP_CONTROL_PASSWORDPOLICYREQUEST ) ) {
1169 rc = check_password_quality( bv, &pp, &pErr );
1170 if (rc != LDAP_SUCCESS) {
1171 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1173 LDAPControl **ctrls = NULL;
1175 ctrls = ch_calloc( sizeof( LDAPControl *) , 2 );
1176 ctrls[0] = create_passcontrol( -1, -1, pErr );
1178 rs->sr_ctrls = ctrls;
1180 send_ldap_error( op, rs, rc, "Password fails quality checking policy" );
1184 /* If password aging is in effect, set the pwdChangedTime */
1185 if ( pp.pwdMaxAge || pp.pwdMinAge ) {
1186 struct berval timestamp;
1187 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
1189 time_t now = slap_get_time();
1191 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
1192 ltm = gmtime( &now );
1193 lutil_gentime( timebuf, sizeof(timebuf), ltm );
1194 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
1196 timestamp.bv_val = timebuf;
1197 timestamp.bv_len = strlen(timebuf);
1199 attr_merge_one( op->ora_e, ad_pwdChangedTime, ×tamp, NULL );
1202 return SLAP_CB_CONTINUE;
1206 ppolicy_modify( Operation *op, SlapReply *rs )
1208 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1209 int i, rc, mod_pw_only, pwmod, pwmop, deladd, hsize = 0;
1211 Modifications *mods = NULL, *modtail, *ml, *delmod, *addmod;
1212 Attribute *pa, *ha, *ra, at;
1213 int repl_user = be_isupdate( op->o_bd, &op->o_ndn );
1215 pw_hist *tl = NULL, *p;
1216 int zapReset, send_ctrl = 0;
1218 struct berval newpw = { 0, NULL }, oldpw = { 0, NULL }, *bv, cr[2];
1219 LDAPPasswordPolicyError pErr = PP_noError;
1221 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1222 rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &e );
1223 op->o_bd->bd_info = (BackendInfo *)on;
1225 if ( rc != LDAP_SUCCESS ) return SLAP_CB_CONTINUE;
1227 /* Did we receive a password policy request control? */
1228 for ( i=0; op->o_ctrls && op->o_ctrls[i]; i++ ) {
1229 if ( !strcmp( op->o_ctrls[i]->ldctl_oid,
1230 LDAP_CONTROL_PASSWORDPOLICYREQUEST ) ) {
1236 /* See if this is a pwdModify exop. If so, we can
1237 * access the plaintext passwords from that request.
1242 for ( sc = op->o_callback; sc; sc=sc->sc_next ) {
1243 if ( sc->sc_response == slap_replog_cb &&
1245 req_pwdexop_s *qpw = sc->sc_private;
1246 newpw = qpw->rs_new;
1247 oldpw = qpw->rs_old;
1253 ppolicy_get( op, e, &pp );
1255 for(ml = op->oq_modify.rs_modlist,
1256 pwmod = 0, mod_pw_only = 1,
1257 deladd = 0, delmod = NULL,
1260 ml != NULL; modtail = ml, ml = ml->sml_next ) {
1261 if ( ml->sml_desc == pp.ad ) {
1264 if ((deladd == 0) && (ml->sml_op == LDAP_MOD_DELETE) &&
1265 (ml->sml_values) && (ml->sml_values[0].bv_val != NULL)) {
1270 if ((deladd == 1) && ((ml->sml_op == LDAP_MOD_ADD) ||
1271 (ml->sml_op == LDAP_MOD_REPLACE)))
1274 if ((ml->sml_op == LDAP_MOD_ADD) ||
1275 (ml->sml_op == LDAP_MOD_REPLACE))
1277 } else if (! is_at_operational( ml->sml_desc->ad_type )) {
1279 /* modifying something other than password */
1283 * If there is a request to explicitly add a pwdReset
1284 * attribute, then we suppress the normal behaviour on
1285 * password change, which is to remove the pwdReset
1288 * This enables an administrator to assign a new password
1289 * and place a "must reset" flag on the entry, which will
1290 * stay until the user explicitly changes his/her password.
1292 if (ml->sml_desc == ad_pwdReset ) {
1293 if ((ml->sml_op == LDAP_MOD_ADD) ||
1294 (ml->sml_op == LDAP_MOD_REPLACE))
1299 if (pwcons[op->o_conn->c_conn_idx].restrict && !mod_pw_only) {
1301 LDAP_LOG( OPERATION, ENTRY,
1302 "connection restricted to password changing only\n", 0, 0, 0 );
1304 Debug( LDAP_DEBUG_TRACE,
1305 "connection restricted to password changing only\n", 0, 0, 0 );
1307 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1308 rs->sr_text = "Operations are restricted to bind/unbind/abandon/StartTLS/modify password";
1309 pErr = PP_changeAfterReset;
1310 goto return_results;
1314 * if we have a "safe password modify policy", then we need to check if we're doing
1315 * a delete (with the old password), followed by an add (with the new password).
1317 * If we don't have this, then we fail with an error. We also skip all the checks if
1318 * the root user is bound. Root can do anything, including avoid the policies.
1321 if (!pwmod) goto do_modify;
1324 * Did we get a valid add mod?
1328 rs->sr_err = LDAP_OTHER;
1329 rs->sr_text = "Internal Error";
1331 LDAP_LOG( OPERATION, ENTRY,
1332 "cannot locate modification supplying new password\n", 0, 0, 0 );
1334 Debug( LDAP_DEBUG_TRACE,
1335 "cannot locate modification supplying new password\n", 0, 0, 0 );
1337 goto return_results;
1341 * Build the password history list in ascending time order
1342 * We need this, even if the user is root, in order to maintain
1343 * the pwdHistory operational attributes properly.
1345 if (pp.pwdInHistory > 0 && (ha = attr_find( e->e_attrs, ad_pwdHistory ))) {
1346 struct berval oldpw;
1350 for(i=0; ha->a_nvals[i].bv_val; i++) {
1351 rc = parse_pwdhistory( &(ha->a_nvals[i]), NULL,
1354 if (rc != LDAP_SUCCESS) continue; /* invalid history entry */
1357 add_to_pwd_history( &tl, oldtime, &oldpw,
1358 &(ha->a_nvals[i]) );
1359 oldpw.bv_val = NULL;
1363 for(p=tl; p; p=p->next, hsize++); /* count history size */
1366 if (be_isroot( op->o_bd, &op->o_ndn)) goto do_modify;
1368 /* This is a pwdModify exop that provided the old pw.
1369 * We need to create a Delete mod for this old pw and
1370 * let the matching value get found later
1372 if (pp.pwdSafeModify && oldpw.bv_val ) {
1373 ml = (Modifications *) ch_malloc( sizeof( Modifications ) );
1374 ml->sml_op = LDAP_MOD_DELETE;
1375 ml->sml_desc = pp.ad;
1376 ml->sml_type = pp.ad->ad_cname;
1377 ml->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1378 ber_dupbv( &ml->sml_values[0], &oldpw );
1379 ml->sml_values[1].bv_len = 0;
1380 ml->sml_values[1].bv_val = NULL;
1381 ml->sml_nvalues = NULL;
1382 ml->sml_next = op->orm_modlist;
1383 op->orm_modlist = ml;
1388 if (pp.pwdSafeModify && deladd != 2) {
1390 LDAP_LOG( OPERATION, ENTRY,
1391 "change password must use DELETE followed by ADD/REPLACE\n",
1394 Debug( LDAP_DEBUG_TRACE,
1395 "change password must use DELETE followed by ADD/REPLACE\n",
1398 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1399 rs->sr_text = "Must supply old password to be changed as well as new one";
1400 pErr = PP_mustSupplyOldPassword;
1401 goto return_results;
1404 if (!pp.pwdAllowUserChange) {
1405 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1406 rs->sr_text = "User alteration of password is not allowed";
1407 pErr = PP_passwordModNotAllowed;
1408 goto return_results;
1411 if (pp.pwdMinAge > 0) {
1412 time_t pwtime = (time_t)-1, now;
1415 if ((pa = attr_find( e->e_attrs, ad_pwdChangedTime )) != NULL)
1416 pwtime = parse_time( pa->a_nvals[0].bv_val );
1417 now = slap_get_time();
1418 age = (int)(now - pwtime);
1419 if ((pwtime != (time_t)-1) && (age < pp.pwdMinAge)) {
1420 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1421 rs->sr_text = "Password is too young to change";
1422 pErr = PP_passwordTooYoung;
1423 goto return_results;
1427 if (delmod && (pa = attr_find( e->e_attrs, pp.ad )) != NULL) {
1429 * we have a password to check
1433 bv = oldpw.bv_val ? &oldpw : delmod->sml_values;
1434 rc = slap_passwd_check( op->o_conn, pa, bv, &txt );
1435 if (rc != LDAP_SUCCESS) {
1437 LDAP_LOG( OPERATION, ENTRY,
1438 "old password check failed: %s\n", txt, 0, 0 );
1440 Debug( LDAP_DEBUG_TRACE,
1441 "old password check failed: %s\n", txt, 0, 0 );
1444 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1445 rs->sr_text = "Must supply correct old password to change to new one";
1446 pErr = PP_mustSupplyOldPassword;
1447 goto return_results;
1453 * replace the delete value with the (possibly hashed)
1454 * value which is currently in the password.
1456 for(i=0; delmod->sml_values[i].bv_val; i++) {
1457 free(delmod->sml_values[i].bv_val);
1458 delmod->sml_values[i].bv_len = 0;
1460 free(delmod->sml_values);
1461 delmod->sml_values = ch_calloc( sizeof(struct berval), 2 );
1462 delmod->sml_values[1].bv_len = 0;
1463 delmod->sml_values[1].bv_val = NULL;
1464 ber_dupbv(&(delmod->sml_values[0]), &(pa->a_nvals[0]));
1468 bv = newpw.bv_val ? &newpw : addmod->sml_values;
1469 if (pp.pwdCheckQuality > 0) {
1471 rc = check_password_quality( bv, &pp, &pErr );
1472 if (rc != LDAP_SUCCESS) {
1474 rs->sr_text = "Password fails quality checking policy";
1475 goto return_results;
1480 * Last check - the password history.
1482 if (slap_passwd_check( op->o_conn, pa, bv, &txt ) == LDAP_SUCCESS) {
1484 * This is bad - it means that the user is attempting
1485 * to set the password to the same as the old one.
1487 rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
1488 rs->sr_text = "Password is not being changed from existing value";
1489 pErr = PP_passwordInHistory;
1490 goto return_results;
1493 if (pp.pwdInHistory < 1) goto do_modify;
1496 * Iterate through the password history, and fail on any
1501 cr[1].bv_val = NULL;
1502 for(p=tl; p; p=p->next) {
1504 rc = slap_passwd_check( op->o_conn, &at, bv, &txt );
1506 if (rc != LDAP_SUCCESS) continue;
1508 rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
1509 rs->sr_text = "Password is in history of old passwords";
1510 pErr = PP_passwordInHistory;
1511 goto return_results;
1515 if ((pwmod) && (!repl_user)) {
1516 struct berval timestamp;
1517 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
1519 time_t now = slap_get_time();
1523 * keep the necessary pwd.. operational attributes
1527 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
1528 ltm = gmtime( &now );
1529 lutil_gentime( timebuf, sizeof(timebuf), ltm );
1530 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
1532 timestamp.bv_val = timebuf;
1533 timestamp.bv_len = strlen(timebuf);
1534 mods = (Modifications *) ch_malloc( sizeof( Modifications ) );
1535 mods->sml_type.bv_val = NULL;
1536 mods->sml_desc = ad_pwdChangedTime;
1537 if (pwmop != LDAP_MOD_DELETE) {
1538 mods->sml_op = LDAP_MOD_REPLACE;
1539 mods->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1540 ber_dupbv( &mods->sml_values[0], ×tamp );
1541 mods->sml_values[1].bv_len = 0;
1542 mods->sml_values[1].bv_val = NULL;
1543 assert( mods->sml_values[0].bv_val );
1545 mods->sml_op = LDAP_MOD_DELETE;
1546 mods->sml_values = NULL;
1548 mods->sml_nvalues = NULL;
1549 mods->sml_next = NULL;
1550 modtail->sml_next = mods;
1553 if (attr_find(e->e_attrs, ad_pwdGraceUseTime )) {
1554 mods = (Modifications *) ch_malloc( sizeof( Modifications ) );
1555 mods->sml_op = LDAP_MOD_DELETE;
1556 mods->sml_type.bv_val = NULL;
1557 mods->sml_desc = ad_pwdGraceUseTime;
1558 mods->sml_values = NULL;
1559 mods->sml_nvalues = NULL;
1560 mods->sml_next = NULL;
1561 modtail->sml_next = mods;
1565 if (attr_find(e->e_attrs, ad_pwdExpirationWarned )) {
1566 mods = (Modifications *) ch_malloc( sizeof( Modifications ) );
1567 mods->sml_op = LDAP_MOD_DELETE;
1568 mods->sml_type.bv_val = NULL;
1569 mods->sml_desc = ad_pwdExpirationWarned;
1570 mods->sml_values = NULL;
1571 mods->sml_nvalues = NULL;
1572 mods->sml_next = NULL;
1573 modtail->sml_next = mods;
1577 /* Delete the pwdReset attribute, since it's being reset */
1578 if ((zapReset) && (attr_find(e->e_attrs, ad_pwdReset ))) {
1579 mods = (Modifications *) ch_malloc( sizeof( Modifications ) );
1580 mods->sml_op = LDAP_MOD_DELETE;
1581 mods->sml_type.bv_val = NULL;
1582 mods->sml_desc = ad_pwdReset;
1583 mods->sml_values = NULL;
1584 mods->sml_nvalues = NULL;
1585 mods->sml_next = NULL;
1586 modtail->sml_next = mods;
1590 if (pp.pwdInHistory > 0) {
1591 if (hsize >= pp.pwdInHistory) {
1593 * We use the >= operator, since we are going to add
1594 * the existing password attribute value into the
1595 * history - thus the cardinality of history values is
1596 * about to rise by one.
1598 * If this would push it over the limit of history
1599 * values (remembering - the password policy could have
1600 * changed since the password was last altered), we must
1601 * delete at least 1 value from the pwdHistory list.
1603 * In fact, we delete '(#pwdHistory attrs - max pwd
1604 * history length) + 1' values, starting with the oldest.
1605 * This is easily evaluated, since the linked list is
1606 * created in ascending time order.
1608 mods = (Modifications *) ch_malloc( sizeof( Modifications ) );
1609 mods->sml_op = LDAP_MOD_DELETE;
1610 mods->sml_type.bv_val = NULL;
1611 mods->sml_desc = ad_pwdHistory;
1612 mods->sml_nvalues = NULL;
1613 mods->sml_values = ch_calloc( sizeof( struct berval ),
1614 hsize - pp.pwdInHistory + 2 );
1615 mods->sml_values[ hsize - pp.pwdInHistory + 1 ].bv_val = NULL;
1616 mods->sml_values[ hsize - pp.pwdInHistory + 1 ].bv_len = 0;
1617 for(i=0,p=tl; i < (hsize - pp.pwdInHistory + 1); i++, p=p->next) {
1618 mods->sml_values[i].bv_val = NULL;
1619 mods->sml_values[i].bv_len = 0;
1620 ber_dupbv( &(mods->sml_values[i]), &p->bv );
1622 mods->sml_next = NULL;
1623 modtail->sml_next = mods;
1626 free_pwd_history_list( &tl );
1629 * Now add the existing password into the history list.
1630 * This will be executed even if the operation is to delete
1631 * the password entirely.
1633 * This isn't in the spec explicitly, but it seems to make
1634 * sense that the password history list is the list of all
1635 * previous passwords - even if they were deleted. Thus, if
1636 * someone tries to add a historical password at some future
1637 * point, it will fail.
1639 if ((pa = attr_find( e->e_attrs, pp.ad )) != NULL) {
1640 mods = (Modifications *) ch_malloc( sizeof( Modifications ) );
1641 mods->sml_op = LDAP_MOD_ADD;
1642 mods->sml_type.bv_val = NULL;
1643 mods->sml_desc = ad_pwdHistory;
1644 mods->sml_nvalues = NULL;
1645 mods->sml_values = ch_calloc( sizeof( struct berval ), 2 );
1646 mods->sml_values[ 1 ].bv_val = NULL;
1647 mods->sml_values[ 1 ].bv_len = 0;
1648 make_pwd_history_value( timebuf, &mods->sml_values[0], pa );
1649 mods->sml_next = NULL;
1650 modtail->sml_next = mods;
1654 LDAP_LOG ( OPERATION, ERR,
1655 "ppolicy_modify: password attr lookup failed\n", 0, 0, 0 );
1657 Debug( LDAP_DEBUG_TRACE,
1658 "ppolicy_modify: password attr lookup failed\n", 0, 0, 0 );
1664 * Controversial bit here. If the new password isn't hashed
1665 * (ie, is cleartext), we probably should hash it according
1666 * to the default hash. The reason for this is that we want
1667 * to use the policy if possible, but if we hash the password
1668 * before, then we're going to run into trouble when it
1669 * comes time to check the password.
1671 * Now, the right thing to do is to use the extended password
1672 * modify operation, but not all software can do this,
1673 * therefore it makes sense to hash the new password, now
1674 * we know it passes the policy requirements.
1676 * Of course, if the password is already hashed, then we
1680 if ((addmod) && !newpw.bv_val &&
1681 (password_scheme( &(addmod->sml_values[0]), NULL ) != LDAP_SUCCESS)) {
1682 struct berval hpw, bv;
1684 slap_passwd_hash( &(addmod->sml_values[0]), &hpw, &txt );
1685 if (hpw.bv_val == NULL) {
1687 * hashing didn't work. Emit an error.
1689 rs->sr_err = LDAP_OTHER;
1691 goto return_results;
1693 bv.bv_val = addmod->sml_values[0].bv_val;
1694 bv.bv_len = addmod->sml_values[0].bv_len;
1695 /* clear and discard the clear password */
1696 memset(bv.bv_val, 0, bv.bv_len);
1697 ber_memfree(bv.bv_val);
1698 addmod->sml_values[0].bv_val = hpw.bv_val;
1699 addmod->sml_values[0].bv_len = hpw.bv_len;
1702 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1703 be_entry_release_r( op, e );
1704 return SLAP_CB_CONTINUE;
1707 free_pwd_history_list( &tl );
1708 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1709 be_entry_release_r( op, e );
1711 LDAPControl **ctrls = NULL;
1713 ctrls = ch_calloc( sizeof( LDAPControl *) , 2 );
1714 ctrls[0] = create_passcontrol( -1, -1, pErr );
1716 rs->sr_ctrls = ctrls;
1718 send_ldap_result( op, rs );
1728 if ( ctrl->ldctl_value.bv_len ) {
1729 rs->sr_text = "passwordPolicyRequest control value not empty";
1730 return LDAP_PROTOCOL_ERROR;
1732 if ( ctrl->ldctl_iscritical ) {
1733 rs->sr_text = "passwordPolicyRequest control invalid criticality";
1734 return LDAP_PROTOCOL_ERROR;
1737 return LDAP_SUCCESS;
1745 slap_overinst *on = (slap_overinst *) be->bd_info;
1747 on->on_bi.bi_private = ch_calloc( sizeof(pp_info), 1 );
1749 if ( dtblsize && !pwcons )
1750 pwcons = ch_calloc(sizeof(pw_conn), dtblsize );
1760 slap_overinst *on = (slap_overinst *) be->bd_info;
1761 pp_info *pi = on->on_bi.bi_private;
1764 free( pi->def_policy.bv_val );
1779 slap_overinst *on = (slap_overinst *) be->bd_info;
1780 pp_info *pi = on->on_bi.bi_private;
1784 if ( strcasecmp( argv[0], "ppolicy_default" ) == 0 ) {
1786 fprintf( stderr, "%s: line %d: invalid arguments in \"ppolicy_default"
1787 " <policyDN>\n", fname, lineno );
1790 ber_str2bv( argv[1], 0, 0, &dn );
1791 if ( dnNormalize( 0, NULL, NULL, &dn, &pi->def_policy, NULL ) ) {
1792 fprintf( stderr, "%s: line %d: policyDN is invalid\n",
1797 } else if ( strcasecmp( argv[0], "ppolicy_use_lockout" ) == 0 ) {
1799 fprintf( stderr, "%s: line %d: ppolicy_use_lockout "
1800 "takes no arguments\n", fname, lineno );
1803 pi->use_lockout = 1;
1805 return SLAP_CONF_UNKNOWN;
1808 static char *extops[] = {
1809 LDAP_EXOP_MODIFY_PASSWD,
1813 static slap_overinst ppolicy;
1817 LDAPAttributeType *at;
1822 for (i=0; pwd_OpSchema[i].def; i++) {
1823 at = ldap_str2attributetype( pwd_OpSchema[i].def, &code, &err,
1824 LDAP_SCHEMA_ALLOW_ALL );
1826 fprintf( stderr, "AttributeType Load failed %s %s\n",
1827 ldap_scherr2str(code), err );
1830 code = at_add( at, &err );
1832 slap_str2ad( at->at_names[0], pwd_OpSchema[i].ad, &err );
1836 fprintf( stderr, "AttributeType Load failed %s %s\n",
1837 scherr2str(code), err );
1842 for (i=0; pwd_UsSchema[i].def; i++) {
1843 code = slap_str2ad( pwd_UsSchema[i].def, pwd_UsSchema[i].ad, &err );
1845 fprintf( stderr, "User Schema Load failed %d: %s\n", code, err );
1850 code = register_supported_control( LDAP_CONTROL_PASSWORDPOLICYREQUEST,
1851 SLAP_CTRL_ADD|SLAP_CTRL_BIND|SLAP_CTRL_MODIFY, extops,
1852 ppolicy_parseCtrl );
1853 if ( code != LDAP_SUCCESS ) {
1854 fprintf( stderr, "Failed to register control %d\n", code );
1858 ldap_pvt_thread_mutex_init( &chk_syntax_mutex );
1860 ppolicy.on_bi.bi_type = "ppolicy";
1861 ppolicy.on_bi.bi_db_init = ppolicy_db_init;
1862 ppolicy.on_bi.bi_db_config = ppolicy_config;
1863 ppolicy.on_bi.bi_db_close = ppolicy_close;
1865 ppolicy.on_bi.bi_op_add = ppolicy_add;
1866 ppolicy.on_bi.bi_op_bind = ppolicy_bind;
1867 ppolicy.on_bi.bi_op_unbind = ppolicy_unbind;
1868 ppolicy.on_bi.bi_op_compare = ppolicy_restrict;
1869 ppolicy.on_bi.bi_op_delete = ppolicy_restrict;
1870 ppolicy.on_bi.bi_op_modify = ppolicy_modify;
1871 ppolicy.on_bi.bi_op_search = ppolicy_restrict;
1873 return overlay_register( &ppolicy );
1876 #if SLAPD_OVER_PPOLICY == SLAPD_MOD_DYNAMIC
1877 int init_module(int argc, char *argv[]) {
1878 return ppolicy_init();
1882 #endif /* defined(SLAPD_OVER_PPOLICY) */