2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 2004-2005 The OpenLDAP Foundation.
5 * Portions Copyright 2004-2005 Howard Chu, Symas Corporation.
6 * Portions Copyright 2004 Hewlett-Packard Company.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
13 * A copy of this license is available in the file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
18 * This work was developed by Howard Chu for inclusion in
19 * OpenLDAP Software, based on prior work by Neil Dunbar (HP).
20 * This work was sponsored by the Hewlett-Packard Company.
25 /* This file implements "Password Policy for LDAP Directories",
26 * based on draft behera-ldap-password-policy-09
29 #ifdef SLAPD_OVER_PPOLICY
35 #define LIBLTDL_DLL_IMPORT /* Win32: don't re-export libltdl's symbols */
40 #include <ac/string.h>
43 #ifndef MODULE_NAME_SZ
44 #define MODULE_NAME_SZ 256
47 /* Per-instance configuration information */
48 typedef struct pp_info {
49 struct berval def_policy; /* DN of default policy subentry */
50 int use_lockout; /* send AccountLocked result? */
51 int hash_passwords; /* transparently hash cleartext pwds */
54 /* Our per-connection info - note, it is not per-instance, it is
55 * used by all instances
57 typedef struct pw_conn {
58 int restricted; /* TRUE if connection is restricted */
61 static pw_conn *pwcons;
62 static int ppolicy_cid;
64 typedef struct pass_policy {
65 AttributeDescription *ad; /* attribute to which the policy applies */
66 int pwdMinAge; /* minimum time (seconds) until passwd can change */
67 int pwdMaxAge; /* time in seconds until pwd will expire after change */
68 int pwdInHistory; /* number of previous passwords kept */
69 int pwdCheckQuality; /* 0 = don't check quality, 1 = check if possible,
70 2 = check mandatory; fail if not possible */
71 int pwdMinLength; /* minimum number of chars in password */
72 int pwdExpireWarning; /* number of seconds that warning controls are
73 sent before a password expires */
74 int pwdGraceAuthNLimit; /* number of times you can log in with an
76 int pwdLockout; /* 0 = do not lockout passwords, 1 = lock them out */
77 int pwdLockoutDuration; /* time in seconds a password is locked out for */
78 int pwdMaxFailure; /* number of failed binds allowed before lockout */
79 int pwdFailureCountInterval; /* number of seconds before failure
81 int pwdMustChange; /* 0 = users can use admin set password
82 1 = users must change password after admin set */
83 int pwdAllowUserChange; /* 0 = users cannot change their passwords
84 1 = users can change them */
85 int pwdSafeModify; /* 0 = old password doesn't need to come
86 with password change request
87 1 = password change must supply existing pwd */
88 char pwdCheckModule[MODULE_NAME_SZ]; /* name of module to dynamically
89 load to check password */
92 typedef struct pw_hist {
93 time_t t; /* timestamp of history entry */
94 struct berval pw; /* old password hash */
95 struct berval bv; /* text of entire entry */
99 /* Operational attributes */
100 static AttributeDescription *ad_pwdChangedTime, *ad_pwdAccountLockedTime,
101 *ad_pwdFailureTime, *ad_pwdHistory, *ad_pwdGraceUseTime, *ad_pwdReset,
102 *ad_pwdPolicySubentry;
104 static struct schema_info {
106 AttributeDescription **ad;
108 { "( 1.3.6.1.4.1.42.2.27.8.1.16 "
109 "NAME ( 'pwdChangedTime' ) "
110 "DESC 'The time the password was last changed' "
111 "EQUALITY generalizedTimeMatch "
112 "ORDERING generalizedTimeOrderingMatch "
113 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
114 "SINGLE-VALUE NO-USER-MODIFICATION USAGE directoryOperation )",
115 &ad_pwdChangedTime },
116 { "( 1.3.6.1.4.1.42.2.27.8.1.17 "
117 "NAME ( 'pwdAccountLockedTime' ) "
118 "DESC 'The time an user account was locked' "
119 "EQUALITY generalizedTimeMatch "
120 "ORDERING generalizedTimeOrderingMatch "
121 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
122 "SINGLE-VALUE NO-USER-MODIFICATION USAGE directoryOperation )",
123 &ad_pwdAccountLockedTime },
124 { "( 1.3.6.1.4.1.42.2.27.8.1.19 "
125 "NAME ( 'pwdFailureTime' ) "
126 "DESC 'The timestamps of the last consecutive authentication failures' "
127 "EQUALITY generalizedTimeMatch "
128 "ORDERING generalizedTimeOrderingMatch "
129 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
130 "NO-USER-MODIFICATION USAGE directoryOperation )",
131 &ad_pwdFailureTime },
132 { "( 1.3.6.1.4.1.42.2.27.8.1.20 "
133 "NAME ( 'pwdHistory' ) "
134 "DESC 'The history of users passwords' "
135 "EQUALITY octetStringMatch "
136 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.40 "
137 "NO-USER-MODIFICATION USAGE directoryOperation )",
139 { "( 1.3.6.1.4.1.42.2.27.8.1.21 "
140 "NAME ( 'pwdGraceUseTime' ) "
141 "DESC 'The timestamps of the grace login once the password has expired' "
142 "EQUALITY generalizedTimeMatch "
143 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
144 "NO-USER-MODIFICATION USAGE directoryOperation )",
145 &ad_pwdGraceUseTime },
146 { "( 1.3.6.1.4.1.42.2.27.8.1.22 "
147 "NAME ( 'pwdReset' ) "
148 "DESC 'The indication that the password has been reset' "
149 "EQUALITY booleanMatch "
150 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.7 "
151 "SINGLE-VALUE USAGE directoryOperation )",
153 { "( 1.3.6.1.4.1.42.2.27.8.1.23 "
154 "NAME ( 'pwdPolicySubentry' ) "
155 "DESC 'The pwdPolicy subentry in effect for this object' "
156 "EQUALITY distinguishedNameMatch "
157 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.12 "
158 "SINGLE-VALUE NO-USER-MODIFICATION USAGE directoryOperation )",
159 &ad_pwdPolicySubentry },
163 /* User attributes */
164 static AttributeDescription *ad_pwdMinAge, *ad_pwdMaxAge, *ad_pwdInHistory,
165 *ad_pwdCheckQuality, *ad_pwdMinLength, *ad_pwdMaxFailure,
166 *ad_pwdGraceAuthNLimit, *ad_pwdExpireWarning, *ad_pwdLockoutDuration,
167 *ad_pwdFailureCountInterval, *ad_pwdCheckModule, *ad_pwdLockout,
168 *ad_pwdMustChange, *ad_pwdAllowUserChange, *ad_pwdSafeModify,
171 #define TAB(name) { #name, &ad_##name }
173 static struct schema_info pwd_UsSchema[] = {
178 TAB(pwdCheckQuality),
181 TAB(pwdGraceAuthNLimit),
182 TAB(pwdExpireWarning),
184 TAB(pwdLockoutDuration),
185 TAB(pwdFailureCountInterval),
188 TAB(pwdAllowUserChange),
193 static ldap_pvt_thread_mutex_t chk_syntax_mutex;
196 parse_time( char *atm )
199 struct lutil_timet tt;
200 time_t ret = (time_t)-1;
202 if ( lutil_parsetime( atm, &tm ) == 0) {
203 lutil_tm2time( &tm, &tt );
210 account_locked( Operation *op, Entry *e,
211 PassPolicy *pp, Modifications **mod )
217 if ( (la = attr_find( e->e_attrs, ad_pwdAccountLockedTime )) != NULL ) {
218 BerVarray vals = la->a_nvals;
221 * there is a lockout stamp - we now need to know if it's
224 if (vals[0].bv_val != NULL) {
228 if (!pp->pwdLockoutDuration)
231 if ((then = parse_time( vals[0].bv_val )) == (time_t)0)
234 now = slap_get_time();
236 if (now < then + pp->pwdLockoutDuration)
239 m = ch_calloc( sizeof(Modifications), 1 );
240 m->sml_op = LDAP_MOD_DELETE;
242 m->sml_type = ad_pwdAccountLockedTime->ad_cname;
243 m->sml_desc = ad_pwdAccountLockedTime;
252 #define PPOLICY_WARNING 0xa0L
253 #define PPOLICY_ERROR 0xa1L
255 #define PPOLICY_EXPIRE 0xa0L
256 #define PPOLICY_GRACE 0xa1L
259 create_passcontrol( int exptime, int grace, LDAPPasswordPolicyError err )
261 char berbuf[LBER_ELEMENT_SIZEOF], bb2[LBER_ELEMENT_SIZEOF];
262 BerElement *ber = (BerElement *)berbuf, *b2 = (BerElement *)bb2;
266 if ((c = ch_calloc( sizeof( LDAPControl ), 1 )) == NULL) return NULL;
267 c->ldctl_oid = LDAP_CONTROL_PASSWORDPOLICYRESPONSE;
268 c->ldctl_iscritical = 0;
269 c->ldctl_value.bv_val = NULL;
270 c->ldctl_value.bv_len = 0;
272 ber_init2( ber, NULL, LBER_USE_DER );
273 ber_printf(ber, "{" /*}*/ );
276 ber_init2( b2, NULL, LBER_USE_DER );
277 ber_printf( b2, "ti", PPOLICY_EXPIRE, exptime );
278 ber_flatten2( b2, &bv, 1 );
279 ber_printf( ber, "tO", PPOLICY_WARNING, &bv );
280 ch_free( bv.bv_val );
281 } else if (grace > 0) {
282 ber_init2( b2, NULL, LBER_USE_DER );
283 ber_printf( b2, "ti", PPOLICY_GRACE, grace );
284 ber_flatten2( b2, &bv, 1 );
285 ber_printf( ber, "tO", PPOLICY_WARNING, &bv );
286 ch_free( bv.bv_val );
289 if (err != PP_noError ) {
290 ber_printf( ber, "te", PPOLICY_ERROR, err );
292 ber_printf( ber, /*{*/ "N}" );
294 if (ber_flatten2( ber, &(c->ldctl_value), 1 ) == LBER_DEFAULT) {
296 (void)ber_free_buf(ber);
299 (void)ber_free_buf(ber);
304 ppolicy_get( Operation *op, Entry *e, PassPolicy *pp )
306 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
307 pp_info *pi = on->on_bi.bi_private;
316 memset( pp, 0, sizeof(PassPolicy) );
318 /* Users can change their own password by default */
319 pp->pwdAllowUserChange = 1;
321 if ((a = attr_find( e->e_attrs, ad_pwdPolicySubentry )) == NULL) {
323 * entry has no password policy assigned - use default
325 vals = &pi->def_policy;
330 if (vals[0].bv_val == NULL) {
331 Debug( LDAP_DEBUG_ANY,
332 "ppolicy_get: NULL value for policySubEntry\n", 0, 0, 0 );
337 op->o_bd->bd_info = (BackendInfo *)on->on_info;
338 rc = be_entry_get_rw( op, vals, NULL, NULL, 0, &pe );
339 op->o_bd->bd_info = (BackendInfo *)on;
341 if ( rc ) goto defaultpol;
343 #if 0 /* Only worry about userPassword for now */
344 if ((a = attr_find( pe->e_attrs, ad_pwdAttribute )))
345 slap_bv2ad( &a->a_vals[0], &pp->ad, &text );
347 pp->ad = slap_schema.si_ad_userPassword;
350 if ((a = attr_find( pe->e_attrs, ad_pwdMinAge )))
351 pp->pwdMinAge = atoi(a->a_vals[0].bv_val );
352 if ((a = attr_find( pe->e_attrs, ad_pwdMaxAge )))
353 pp->pwdMaxAge = atoi(a->a_vals[0].bv_val );
354 if ((a = attr_find( pe->e_attrs, ad_pwdInHistory )))
355 pp->pwdInHistory = atoi(a->a_vals[0].bv_val );
356 if ((a = attr_find( pe->e_attrs, ad_pwdCheckQuality )))
357 pp->pwdCheckQuality = atoi(a->a_vals[0].bv_val );
358 if ((a = attr_find( pe->e_attrs, ad_pwdMinLength )))
359 pp->pwdMinLength = atoi(a->a_vals[0].bv_val );
360 if ((a = attr_find( pe->e_attrs, ad_pwdMaxFailure )))
361 pp->pwdMaxFailure = atoi(a->a_vals[0].bv_val );
362 if ((a = attr_find( pe->e_attrs, ad_pwdGraceAuthNLimit )))
363 pp->pwdGraceAuthNLimit = atoi(a->a_vals[0].bv_val );
364 if ((a = attr_find( pe->e_attrs, ad_pwdExpireWarning )))
365 pp->pwdExpireWarning = atoi(a->a_vals[0].bv_val );
366 if ((a = attr_find( pe->e_attrs, ad_pwdFailureCountInterval )))
367 pp->pwdFailureCountInterval = atoi(a->a_vals[0].bv_val );
368 if ((a = attr_find( pe->e_attrs, ad_pwdLockoutDuration )))
369 pp->pwdLockoutDuration = atoi(a->a_vals[0].bv_val );
371 if ((a = attr_find( pe->e_attrs, ad_pwdCheckModule ))) {
372 strncpy(pp->pwdCheckModule, a->a_vals[0].bv_val,
373 sizeof(pp->pwdCheckModule));
374 pp->pwdCheckModule[sizeof(pp->pwdCheckModule)-1] = '\0';
377 if ((a = attr_find( pe->e_attrs, ad_pwdLockout )))
378 pp->pwdLockout = !strcmp( a->a_nvals[0].bv_val, "TRUE" );
379 if ((a = attr_find( pe->e_attrs, ad_pwdMustChange )))
380 pp->pwdMustChange = !strcmp( a->a_nvals[0].bv_val, "TRUE" );
381 if ((a = attr_find( pe->e_attrs, ad_pwdAllowUserChange )))
382 pp->pwdAllowUserChange = !strcmp( a->a_nvals[0].bv_val, "TRUE" );
383 if ((a = attr_find( pe->e_attrs, ad_pwdSafeModify )))
384 pp->pwdSafeModify = !strcmp( a->a_nvals[0].bv_val, "TRUE" );
386 op->o_bd->bd_info = (BackendInfo *)on->on_info;
387 be_entry_release_r( op, pe );
388 op->o_bd->bd_info = (BackendInfo *)on;
393 Debug( LDAP_DEBUG_ANY,
394 "ppolicy_get: using default policy\n", 0, 0, 0 );
399 password_scheme( struct berval *cred, struct berval *sch )
403 assert( cred != NULL );
410 if ((cred->bv_len == 0) || (cred->bv_val == NULL) ||
411 (cred->bv_val[0] != '{')) return LDAP_OTHER;
413 for(e = 1; cred->bv_val[e] && cred->bv_val[e] != '}'; e++);
414 if (cred->bv_val[e]) {
416 rc = lutil_passwd_scheme( cred->bv_val );
418 sch->bv_val = cred->bv_val;
427 check_password_quality( struct berval *cred, PassPolicy *pp, LDAPPasswordPolicyError *err, Entry *e )
429 int rc = LDAP_SUCCESS, ok = LDAP_SUCCESS;
430 char *ptr = cred->bv_val;
433 assert( cred != NULL );
434 assert( pp != NULL );
436 if ((cred->bv_len == 0) || (pp->pwdMinLength > cred->bv_len)) {
437 rc = LDAP_CONSTRAINT_VIOLATION;
438 if ( err ) *err = PP_passwordTooShort;
443 * We need to know if the password is already hashed - if so
444 * what scheme is it. The reason being that the "hash" of
445 * {cleartext} still allows us to check the password.
447 rc = password_scheme( cred, &sch );
448 if (rc == LDAP_SUCCESS) {
449 if ((sch.bv_val) && (strncasecmp( sch.bv_val, "{cleartext}",
450 sch.bv_len ) == 0)) {
452 * We can check the cleartext "hash"
454 ptr = cred->bv_val + sch.bv_len;
456 /* everything else, we can't check */
457 if (pp->pwdCheckQuality == 2) {
458 rc = LDAP_CONSTRAINT_VIOLATION;
459 if (err) *err = PP_insufficientPasswordQuality;
463 * We can't check the syntax of the password, but it's not
464 * mandatory (according to the policy), so we return success.
473 if (pp->pwdCheckModule[0]) {
478 if ((mod = lt_dlopen( pp->pwdCheckModule )) == NULL) {
481 Debug(LDAP_DEBUG_ANY,
482 "check_password_quality: lt_dlopen failed: (%s) %s.\n",
483 pp->pwdCheckModule, err, 0 );
484 ok = LDAP_OTHER; /* internal error */
486 int (*prog)( char *passwd, char **text, Entry *ent );
488 if ((prog = lt_dlsym( mod, "check_password" )) == NULL) {
491 Debug(LDAP_DEBUG_ANY,
492 "check_password_quality: lt_dlsym failed: (%s) %s.\n",
493 pp->pwdCheckModule, err, 0 );
498 ldap_pvt_thread_mutex_lock( &chk_syntax_mutex );
499 ok = prog( cred->bv_val, &txt, e );
500 ldap_pvt_thread_mutex_unlock( &chk_syntax_mutex );
502 Debug(LDAP_DEBUG_ANY,
503 "check_password_quality: module error: (%s) %s.[%d]\n",
504 pp->pwdCheckModule, txt, ok );
513 Debug(LDAP_DEBUG_ANY, "check_password_quality: external modules not "
514 "supported. pwdCheckModule ignored.\n", 0, 0, 0);
515 #endif /* SLAPD_MODULES */
519 if (ok != LDAP_SUCCESS) {
520 rc = LDAP_CONSTRAINT_VIOLATION;
521 if (err) *err = PP_insufficientPasswordQuality;
528 parse_pwdhistory( struct berval *bv, char **oid, time_t *oldtime, struct berval *oldpw )
531 struct berval nv, npw;
534 assert (bv && (bv->bv_len > 0) && (bv->bv_val) && oldtime && oldpw );
537 *oldtime = (time_t)-1;
538 oldpw->bv_val = NULL;
541 ber_dupbv( &nv, bv );
543 /* first get the time field */
544 for(i=0; (i < nv.bv_len) && (nv.bv_val[i] != '#'); i++);
545 if ( i == nv.bv_len) goto exit_failure; /* couldn't locate the '#' separator */
546 nv.bv_val[i++] = '\0'; /* terminate the string & move to next field */
548 *oldtime = parse_time( ptr );
549 if (*oldtime == (time_t)-1) goto exit_failure;
551 /* get the OID field */
552 for(ptr = &(nv.bv_val[i]);(i < nv.bv_len) && (nv.bv_val[i] != '#'); i++);
553 if ( i == nv.bv_len) goto exit_failure; /* couldn't locate the '#' separator */
554 nv.bv_val[i++] = '\0'; /* terminate the string & move to next field */
555 if ( oid ) *oid = ber_strdup( ptr );
557 /* get the length field */
558 for(ptr = &(nv.bv_val[i]);(i < nv.bv_len) && (nv.bv_val[i] != '#'); i++);
559 if ( i == nv.bv_len) goto exit_failure; /* couldn't locate the '#' separator */
560 nv.bv_val[i++] = '\0'; /* terminate the string & move to next field */
561 oldpw->bv_len = strtol( ptr, NULL, 10 );
562 if (errno == ERANGE) goto exit_failure;
564 /* lastly, get the octets of the string */
565 for(j=i, ptr = &(nv.bv_val[i]);i < nv.bv_len; i++);
566 if (i-j != oldpw->bv_len) goto exit_failure; /* length is wrong */
569 npw.bv_len = oldpw->bv_len;
570 ber_dupbv( oldpw, &npw );
574 if (oid && *oid) { ber_memfree(*oid); *oid = NULL; }
576 ber_memfree( oldpw->bv_val); oldpw->bv_val = NULL;
579 ber_memfree(nv.bv_val);
584 add_to_pwd_history( pw_hist **l, time_t t,
585 struct berval *oldpw, struct berval *bv )
587 pw_hist *p, *p1, *p2;
591 p = ch_malloc( sizeof( pw_hist ));
593 ber_dupbv( &p->bv, bv );
598 /* degenerate case */
603 * advance p1 and p2 such that p1 is the node before the
604 * new one, and p2 is the node after it
606 for (p1 = NULL, p2 = *l; p2 && p2->t <= t; p1 = p2, p2=p2->next );
608 if (p1 == NULL) { *l = p; return; }
612 #ifndef MAX_PWD_HISTORY_SZ
613 #define MAX_PWD_HISTORY_SZ 1024
614 #endif /* MAX_PWD_HISTORY_SZ */
617 make_pwd_history_value( char *timebuf, struct berval *bv, Attribute *pa )
619 char str[ MAX_PWD_HISTORY_SZ ];
622 snprintf( str, MAX_PWD_HISTORY_SZ,
623 "%s#%s#%lu#", timebuf,
624 pa->a_desc->ad_type->sat_syntax->ssyn_oid,
625 (unsigned long) pa->a_nvals[0].bv_len );
626 str[MAX_PWD_HISTORY_SZ-1] = 0;
630 * We have to assume that the string is a string of octets,
631 * not readable characters. In reality, yes, it probably is
632 * a readable (ie, base64) string, but we can't count on that
633 * Hence, while the first 3 fields of the password history
634 * are definitely readable (a timestamp, an OID and an integer
635 * length), the remaining octets of the actual password
636 * are deemed to be binary data.
638 AC_MEMCPY( str + nlen, pa->a_nvals[0].bv_val, pa->a_nvals[0].bv_len );
639 nlen += pa->a_nvals[0].bv_len;
640 bv->bv_val = ch_malloc( nlen + 1 );
641 AC_MEMCPY( bv->bv_val, str, nlen );
642 bv->bv_val[nlen] = '\0';
647 free_pwd_history_list( pw_hist **l )
654 pw_hist *pp = p->next;
664 typedef struct ppbind {
668 LDAPPasswordPolicyError pErr;
673 ppolicy_bind_resp( Operation *op, SlapReply *rs )
675 ppbind *ppb = op->o_callback->sc_private;
676 slap_overinst *on = ppb->on;
677 Modifications *mod = ppb->mod, *m;
679 int ngut = -1, warn = -1, age, rc;
681 time_t now, pwtime = (time_t)-1;
682 char nowstr[ LDAP_LUTIL_GENTIME_BUFSIZE ];
683 struct berval timestamp;
684 BackendInfo *bi = op->o_bd->bd_info;
687 /* If we already know it's locked, just get on with it */
688 if ( ppb->pErr != PP_noError ) {
692 op->o_bd->bd_info = (BackendInfo *)on->on_info;
693 rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &e );
694 op->o_bd->bd_info = bi;
696 if ( rc != LDAP_SUCCESS ) {
697 return SLAP_CB_CONTINUE;
700 now = slap_get_time(); /* stored for later consideration */
701 timestamp.bv_val = nowstr;
702 timestamp.bv_len = sizeof(nowstr);
703 slap_timestamp( &now, ×tamp );
705 if ( rs->sr_err == LDAP_INVALID_CREDENTIALS ) {
708 m = ch_calloc( sizeof(Modifications), 1 );
709 m->sml_op = LDAP_MOD_ADD;
711 m->sml_type = ad_pwdFailureTime->ad_cname;
712 m->sml_desc = ad_pwdFailureTime;
713 m->sml_values = ch_calloc( sizeof(struct berval), 2 );
715 ber_dupbv( &m->sml_values[0], ×tamp );
720 * Count the pwdFailureTimes - if it's
721 * greater than the policy pwdMaxFailure,
722 * then lock the account.
724 if ((a = attr_find( e->e_attrs, ad_pwdFailureTime )) != NULL) {
725 for(i=0; a->a_nvals[i].bv_val; i++) {
728 * If the interval is 0, then failures
729 * stay on the record until explicitly
730 * reset by successful authentication.
732 if (ppb->pp.pwdFailureCountInterval == 0) {
735 parse_time(a->a_nvals[i].bv_val) +
736 ppb->pp.pwdFailureCountInterval) {
741 * We only count those failures
742 * which are not due to expire.
747 if ((ppb->pp.pwdMaxFailure > 0) &&
748 (fc >= ppb->pp.pwdMaxFailure - 1)) {
751 * We subtract 1 from the failure max
752 * because the new failure entry hasn't
753 * made it to the entry yet.
755 m = ch_calloc( sizeof(Modifications), 1 );
756 m->sml_op = LDAP_MOD_REPLACE;
758 m->sml_type = ad_pwdAccountLockedTime->ad_cname;
759 m->sml_desc = ad_pwdAccountLockedTime;
760 m->sml_values = ch_calloc( sizeof(struct berval), 2 );
761 ber_dupbv( &m->sml_values[0], ×tamp );
765 } else if ( rs->sr_err == LDAP_SUCCESS ) {
766 if ((a = attr_find( e->e_attrs, ad_pwdChangedTime )) != NULL)
767 pwtime = parse_time( a->a_nvals[0].bv_val );
769 /* delete all pwdFailureTimes */
770 if ( attr_find( e->e_attrs, ad_pwdFailureTime )) {
771 m = ch_calloc( sizeof(Modifications), 1 );
772 m->sml_op = LDAP_MOD_DELETE;
774 m->sml_type = ad_pwdFailureTime->ad_cname;
775 m->sml_desc = ad_pwdFailureTime;
781 * check to see if the password must be changed
783 if ( ppb->pp.pwdMustChange &&
784 (a = attr_find( e->e_attrs, ad_pwdReset )) &&
785 !strcmp( a->a_nvals[0].bv_val, "TRUE" ) ) {
787 * need to inject client controls here to give
788 * more information. For the moment, we ensure
789 * that we are disallowed from doing anything
790 * other than change password.
792 pwcons[op->o_conn->c_conn_idx].restricted = 1;
794 ppb->pErr = PP_changeAfterReset;
798 * the password does not need to be changed, so
799 * we now check whether the password has expired.
801 * We can skip this bit if passwords don't age in
804 if (ppb->pp.pwdMaxAge == 0) goto grace;
806 if (pwtime == (time_t)-1) {
808 * Hmm. No password changed time on the
809 * entry. This is odd - it should have
810 * been provided when the attribute was added.
812 * However, it's possible that it could be
813 * missing if the DIT was established via
816 Debug( LDAP_DEBUG_ANY,
817 "ppolicy_bind: Entry %s does not have valid pwdChangedTime attribute - assuming password expired\n",
818 e->e_name.bv_val, 0, 0);
823 * Check: was the last change time of
824 * the password older than the maximum age
825 * allowed. (Ignore case 2 from I-D, it's just silly.)
827 if (now - pwtime > ppb->pp.pwdMaxAge ) pwExpired = 1;
832 if (!pwExpired) goto check_expiring_password;
834 if ((a = attr_find( e->e_attrs, ad_pwdGraceUseTime )) == NULL)
835 ngut = ppb->pp.pwdGraceAuthNLimit;
837 for(ngut=0; a->a_nvals[ngut].bv_val; ngut++);
838 ngut = ppb->pp.pwdGraceAuthNLimit - ngut;
842 * ngut is the number of remaining grace logins
844 Debug( LDAP_DEBUG_ANY,
845 "ppolicy_bind: Entry %s has an expired password: %d grace logins\n",
846 e->e_name.bv_val, ngut, 0);
849 ppb->pErr = PP_passwordExpired;
850 rs->sr_err = LDAP_INVALID_CREDENTIALS;
855 * Add a grace user time to the entry
857 m = ch_calloc( sizeof(Modifications), 1 );
858 m->sml_op = LDAP_MOD_ADD;
860 m->sml_type = ad_pwdGraceUseTime->ad_cname;
861 m->sml_desc = ad_pwdGraceUseTime;
862 m->sml_values = ch_calloc( sizeof(struct berval), 2 );
863 ber_dupbv( &m->sml_values[0], ×tamp );
867 check_expiring_password:
869 * Now we need to check to see
870 * if it is about to expire, and if so, should the user
871 * be warned about it in the password policy control.
873 * If the password has expired, and we're in the grace period, then
874 * we don't need to do this bit. Similarly, if we don't have password
875 * aging, then there's no need to do this bit either.
877 if ((ppb->pp.pwdMaxAge < 1) || (pwExpired) || (ppb->pp.pwdExpireWarning < 1))
880 age = (int)(now - pwtime);
883 * We know that there is a password Change Time attribute - if
884 * there wasn't, then the pwdExpired value would be true, unless
885 * there is no password aging - and if there is no password aging,
886 * then this section isn't called anyway - you can't have an
887 * expiring password if there's no limit to expire.
889 if (ppb->pp.pwdMaxAge - age < ppb->pp.pwdExpireWarning ) {
891 * Set the warning value.
893 warn = ppb->pp.pwdMaxAge - age; /* seconds left until expiry */
894 if (warn < 0) warn = 0; /* something weird here - why is pwExpired not set? */
896 Debug( LDAP_DEBUG_ANY,
897 "ppolicy_bind: Setting warning for password expiry for %s = %d seconds\n",
898 op->o_req_dn.bv_val, warn, 0 );
903 op->o_bd->bd_info = (BackendInfo *)on->on_info;
904 be_entry_release_r( op, e );
909 SlapReply r2 = { REP_RESULT };
910 slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
912 /* FIXME: Need to handle replication of some (but not all)
913 * of the operational attributes...
915 op2.o_tag = LDAP_REQ_MODIFY;
916 op2.o_callback = &cb;
917 op2.orm_modlist = mod;
918 op2.o_dn = op->o_bd->be_rootdn;
919 op2.o_ndn = op->o_bd->be_rootndn;
920 op2.o_bd->bd_info = (BackendInfo *)on->on_info;
921 rc = op->o_bd->be_modify( &op2, &r2 );
922 slap_mods_free( mod, 1 );
925 if ( ppb->send_ctrl ) {
926 LDAPControl **ctrls = NULL;
927 pp_info *pi = on->on_bi.bi_private;
929 /* Do we really want to tell that the account is locked? */
930 if ( ppb->pErr == PP_accountLocked && !pi->use_lockout ) {
931 ppb->pErr = PP_noError;
933 ctrls = ch_calloc( sizeof( LDAPControl *) , 2 );
934 ctrls[0] = create_passcontrol( warn, ngut, ppb->pErr );
936 rs->sr_ctrls = ctrls;
938 op->o_bd->bd_info = bi;
939 return SLAP_CB_CONTINUE;
943 ppolicy_bind( Operation *op, SlapReply *rs )
945 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
947 /* Reset lockout status on all Bind requests */
948 pwcons[op->o_conn->c_conn_idx].restricted = 0;
950 /* Root bypasses policy */
951 if ( !be_isroot_dn( op->o_bd, &op->o_req_ndn )) {
957 op->o_bd->bd_info = (BackendInfo *)on->on_info;
958 rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &e );
960 if ( rc != LDAP_SUCCESS ) {
961 return SLAP_CB_CONTINUE;
964 cb = op->o_tmpcalloc( sizeof(ppbind)+sizeof(slap_callback),
965 1, op->o_tmpmemctx );
966 ppb = (ppbind *)(cb+1);
968 ppb->pErr = PP_noError;
970 /* Setup a callback so we can munge the result */
972 cb->sc_response = ppolicy_bind_resp;
973 cb->sc_next = op->o_callback->sc_next;
974 cb->sc_private = ppb;
975 op->o_callback->sc_next = cb;
977 /* Did we receive a password policy request control? */
978 if ( op->o_ctrlflag[ppolicy_cid] ) {
982 op->o_bd->bd_info = (BackendInfo *)on;
983 ppolicy_get( op, e, &ppb->pp );
985 rc = account_locked( op, e, &ppb->pp, &ppb->mod );
987 op->o_bd->bd_info = (BackendInfo *)on->on_info;
988 be_entry_release_r( op, e );
991 /* This will be the Draft 8 response, Unwilling is bogus */
992 ppb->pErr = PP_accountLocked;
993 send_ldap_error( op, rs, LDAP_INVALID_CREDENTIALS, NULL );
999 return SLAP_CB_CONTINUE;
1002 /* Reset the restricted flag for the next session on this connection */
1004 ppolicy_connection_destroy( BackendDB *bd, Connection *conn )
1006 pwcons[conn->c_conn_idx].restricted = 0;
1007 return SLAP_CB_CONTINUE;
1010 /* Check if this connection is restricted */
1016 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1019 /* Did we receive a password policy request control? */
1020 if ( op->o_ctrlflag[ppolicy_cid] ) {
1024 if ( op->o_conn && pwcons[op->o_conn->c_conn_idx].restricted ) {
1025 Debug( LDAP_DEBUG_TRACE,
1026 "connection restricted to password changing only\n", 0, 0, 0);
1028 LDAPControl **ctrls = NULL;
1030 ctrls = ch_calloc( sizeof( LDAPControl *) , 2 );
1031 ctrls[0] = create_passcontrol( -1, -1, PP_changeAfterReset );
1033 rs->sr_ctrls = ctrls;
1035 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1036 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
1037 "Operations are restricted to bind/unbind/abandon/StartTLS/modify password" );
1041 return SLAP_CB_CONTINUE;
1049 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1050 pp_info *pi = on->on_bi.bi_private;
1055 if ( ppolicy_restrict( op, rs ) != SLAP_CB_CONTINUE )
1058 /* Check for password in entry */
1059 if ((pa = attr_find( op->oq_add.rs_e->e_attrs,
1060 slap_schema.si_ad_userPassword )))
1063 * new entry contains a password - if we're not the root user
1064 * then we need to check that the password fits in with the
1065 * security policy for the new entry.
1067 ppolicy_get( op, op->ora_e, &pp );
1068 if (pp.pwdCheckQuality > 0 && !be_isroot( op )) {
1069 struct berval *bv = &(pa->a_vals[0]);
1070 int rc, send_ctrl = 0;
1071 LDAPPasswordPolicyError pErr = PP_noError;
1073 /* Did we receive a password policy request control? */
1074 if ( op->o_ctrlflag[ppolicy_cid] ) {
1077 rc = check_password_quality( bv, &pp, &pErr, op->ora_e );
1078 if (rc != LDAP_SUCCESS) {
1079 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1081 LDAPControl **ctrls = NULL;
1083 ctrls = ch_calloc( sizeof( LDAPControl *) , 2 );
1084 ctrls[0] = create_passcontrol( -1, -1, pErr );
1086 rs->sr_ctrls = ctrls;
1088 send_ldap_error( op, rs, rc, "Password fails quality checking policy" );
1092 * A controversial bit. We hash cleartext
1093 * passwords provided via add and modify operations
1094 * You're not really supposed to do this, since
1095 * the X.500 model says "store attributes" as they
1096 * get provided. By default, this is what we do
1098 * But if the hash_passwords flag is set, we hash
1099 * any cleartext password attribute values via the
1100 * default password hashing scheme.
1102 if ((pi->hash_passwords) &&
1103 (password_scheme( &(pa->a_vals[0]), NULL ) != LDAP_SUCCESS)) {
1106 slap_passwd_hash( &(pa->a_vals[0]), &hpw, &txt );
1107 if (hpw.bv_val == NULL) {
1109 * hashing didn't work. Emit an error.
1111 rs->sr_err = LDAP_OTHER;
1113 send_ldap_error( op, rs, LDAP_OTHER, "Password hashing failed" );
1117 memset( pa->a_vals[0].bv_val, 0, pa->a_vals[0].bv_len);
1118 ber_memfree( pa->a_vals[0].bv_val );
1119 pa->a_vals[0].bv_val = hpw.bv_val;
1120 pa->a_vals[0].bv_len = hpw.bv_len;
1123 /* If password aging is in effect, set the pwdChangedTime */
1124 if (( pp.pwdMaxAge || pp.pwdMinAge ) && !be_shadow_update( op )) {
1125 struct berval timestamp;
1126 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
1127 time_t now = slap_get_time();
1129 timestamp.bv_val = timebuf;
1130 timestamp.bv_len = sizeof(timebuf);
1131 slap_timestamp( &now, ×tamp );
1133 attr_merge_one( op->ora_e, ad_pwdChangedTime, ×tamp, NULL );
1136 return SLAP_CB_CONTINUE;
1140 ppolicy_modify( Operation *op, SlapReply *rs )
1142 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1143 pp_info *pi = on->on_bi.bi_private;
1144 int i, rc, mod_pw_only, pwmod, pwmop, deladd,
1147 Modifications *mods = NULL, *modtail, *ml, *delmod, *addmod;
1148 Attribute *pa, *ha, at;
1150 pw_hist *tl = NULL, *p;
1151 int zapReset, send_ctrl = 0;
1153 struct berval newpw = BER_BVNULL, oldpw = BER_BVNULL,
1155 LDAPPasswordPolicyError pErr = PP_noError;
1157 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1158 rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &e );
1159 op->o_bd->bd_info = (BackendInfo *)on;
1161 if ( rc != LDAP_SUCCESS ) return SLAP_CB_CONTINUE;
1163 /* Did we receive a password policy request control? */
1164 if ( op->o_ctrlflag[ppolicy_cid] ) {
1168 /* See if this is a pwdModify exop. If so, we can
1169 * access the plaintext passwords from that request.
1174 for ( sc = op->o_callback; sc; sc=sc->sc_next ) {
1175 if ( sc->sc_response == slap_replog_cb &&
1177 req_pwdexop_s *qpw = sc->sc_private;
1178 newpw = qpw->rs_new;
1179 oldpw = qpw->rs_old;
1185 ppolicy_get( op, e, &pp );
1187 for(ml = op->oq_modify.rs_modlist,
1188 pwmod = 0, mod_pw_only = 1,
1189 deladd = 0, delmod = NULL,
1192 ml != NULL; modtail = ml, ml = ml->sml_next ) {
1193 if ( ml->sml_desc == pp.ad ) {
1196 if ((deladd == 0) && (ml->sml_op == LDAP_MOD_DELETE) &&
1197 (ml->sml_values) && (ml->sml_values[0].bv_val != NULL)) {
1202 if ((deladd == 1) && ((ml->sml_op == LDAP_MOD_ADD) ||
1203 (ml->sml_op == LDAP_MOD_REPLACE)))
1206 if ((ml->sml_op == LDAP_MOD_ADD) ||
1207 (ml->sml_op == LDAP_MOD_REPLACE))
1209 } else if (! is_at_operational( ml->sml_desc->ad_type )) {
1211 /* modifying something other than password */
1215 * If there is a request to explicitly add a pwdReset
1216 * attribute, then we suppress the normal behaviour on
1217 * password change, which is to remove the pwdReset
1220 * This enables an administrator to assign a new password
1221 * and place a "must reset" flag on the entry, which will
1222 * stay until the user explicitly changes his/her password.
1224 if (ml->sml_desc == ad_pwdReset ) {
1225 if ((ml->sml_op == LDAP_MOD_ADD) ||
1226 (ml->sml_op == LDAP_MOD_REPLACE))
1231 if (pwcons[op->o_conn->c_conn_idx].restricted && !mod_pw_only) {
1232 Debug( LDAP_DEBUG_TRACE,
1233 "connection restricted to password changing only\n", 0, 0, 0 );
1234 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
1235 rs->sr_text = "Operations are restricted to bind/unbind/abandon/StartTLS/modify password";
1236 pErr = PP_changeAfterReset;
1237 goto return_results;
1241 * if we have a "safe password modify policy", then we need to check if we're doing
1242 * a delete (with the old password), followed by an add (with the new password).
1244 * If we don't have this, then we fail with an error. We also skip all the checks if
1245 * the root user is bound. Root can do anything, including avoid the policies.
1248 if (!pwmod) goto do_modify;
1251 * Did we get a valid add mod?
1255 rs->sr_err = LDAP_OTHER;
1256 rs->sr_text = "Internal Error";
1257 Debug( LDAP_DEBUG_TRACE,
1258 "cannot locate modification supplying new password\n", 0, 0, 0 );
1259 goto return_results;
1263 * Build the password history list in ascending time order
1264 * We need this, even if the user is root, in order to maintain
1265 * the pwdHistory operational attributes properly.
1267 if (pp.pwdInHistory > 0 && (ha = attr_find( e->e_attrs, ad_pwdHistory ))) {
1268 struct berval oldpw;
1271 for(i=0; ha->a_nvals[i].bv_val; i++) {
1272 rc = parse_pwdhistory( &(ha->a_nvals[i]), NULL,
1275 if (rc != LDAP_SUCCESS) continue; /* invalid history entry */
1278 add_to_pwd_history( &tl, oldtime, &oldpw,
1279 &(ha->a_nvals[i]) );
1280 oldpw.bv_val = NULL;
1284 for(p=tl; p; p=p->next, hsize++); /* count history size */
1287 if (be_isroot( op )) goto do_modify;
1289 /* This is a pwdModify exop that provided the old pw.
1290 * We need to create a Delete mod for this old pw and
1291 * let the matching value get found later
1293 if (pp.pwdSafeModify && oldpw.bv_val ) {
1294 ml = (Modifications *) ch_malloc( sizeof( Modifications ) );
1295 ml->sml_op = LDAP_MOD_DELETE;
1296 ml->sml_flags = SLAP_MOD_INTERNAL;
1297 ml->sml_desc = pp.ad;
1298 ml->sml_type = pp.ad->ad_cname;
1299 ml->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1300 ber_dupbv( &ml->sml_values[0], &oldpw );
1301 ml->sml_values[1].bv_len = 0;
1302 ml->sml_values[1].bv_val = NULL;
1303 ml->sml_nvalues = NULL;
1304 ml->sml_next = op->orm_modlist;
1305 op->orm_modlist = ml;
1310 if (pp.pwdSafeModify && deladd != 2) {
1311 Debug( LDAP_DEBUG_TRACE,
1312 "change password must use DELETE followed by ADD/REPLACE\n",
1314 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
1315 rs->sr_text = "Must supply old password to be changed as well as new one";
1316 pErr = PP_mustSupplyOldPassword;
1317 goto return_results;
1320 if (!pp.pwdAllowUserChange) {
1321 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
1322 rs->sr_text = "User alteration of password is not allowed";
1323 pErr = PP_passwordModNotAllowed;
1324 goto return_results;
1327 if (pp.pwdMinAge > 0) {
1328 time_t pwtime = (time_t)-1, now;
1331 if ((pa = attr_find( e->e_attrs, ad_pwdChangedTime )) != NULL)
1332 pwtime = parse_time( pa->a_nvals[0].bv_val );
1333 now = slap_get_time();
1334 age = (int)(now - pwtime);
1335 if ((pwtime != (time_t)-1) && (age < pp.pwdMinAge)) {
1336 rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
1337 rs->sr_text = "Password is too young to change";
1338 pErr = PP_passwordTooYoung;
1339 goto return_results;
1343 /* pa is used in password history check below, be sure it's set */
1344 if ((pa = attr_find( e->e_attrs, pp.ad )) != NULL && delmod) {
1346 * we have a password to check
1350 bv = oldpw.bv_val ? &oldpw : delmod->sml_values;
1351 /* FIXME: no access checking? */
1352 rc = slap_passwd_check( op, NULL, pa, bv, &txt );
1353 if (rc != LDAP_SUCCESS) {
1354 Debug( LDAP_DEBUG_TRACE,
1355 "old password check failed: %s\n", txt, 0, 0 );
1357 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1358 rs->sr_text = "Must supply correct old password to change to new one";
1359 pErr = PP_mustSupplyOldPassword;
1360 goto return_results;
1366 * replace the delete value with the (possibly hashed)
1367 * value which is currently in the password.
1369 for(i=0; delmod->sml_values[i].bv_val; i++) {
1370 free(delmod->sml_values[i].bv_val);
1371 delmod->sml_values[i].bv_len = 0;
1373 free(delmod->sml_values);
1374 delmod->sml_values = ch_calloc( sizeof(struct berval), 2 );
1375 delmod->sml_values[1].bv_len = 0;
1376 delmod->sml_values[1].bv_val = NULL;
1377 ber_dupbv(&(delmod->sml_values[0]), &(pa->a_nvals[0]));
1381 bv = newpw.bv_val ? &newpw : addmod->sml_values;
1382 if (pp.pwdCheckQuality > 0) {
1384 rc = check_password_quality( bv, &pp, &pErr, e );
1385 if (rc != LDAP_SUCCESS) {
1387 rs->sr_text = "Password fails quality checking policy";
1388 goto return_results;
1394 * Last check - the password history.
1396 /* FIXME: no access checking? */
1397 if (slap_passwd_check( op, NULL, pa, bv, &txt ) == LDAP_SUCCESS) {
1399 * This is bad - it means that the user is attempting
1400 * to set the password to the same as the old one.
1402 rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
1403 rs->sr_text = "Password is not being changed from existing value";
1404 pErr = PP_passwordInHistory;
1405 goto return_results;
1408 if (pp.pwdInHistory < 1) goto do_modify;
1411 * Iterate through the password history, and fail on any
1416 cr[1].bv_val = NULL;
1417 for(p=tl; p; p=p->next) {
1419 /* FIXME: no access checking? */
1420 rc = slap_passwd_check( op, NULL, &at, bv, &txt );
1422 if (rc != LDAP_SUCCESS) continue;
1424 rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
1425 rs->sr_text = "Password is in history of old passwords";
1426 pErr = PP_passwordInHistory;
1427 goto return_results;
1432 if ((pwmod) && (!be_shadow_update( op ))) {
1433 struct berval timestamp;
1434 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
1435 time_t now = slap_get_time();
1438 * keep the necessary pwd.. operational attributes
1442 timestamp.bv_val = timebuf;
1443 timestamp.bv_len = sizeof(timebuf);
1444 slap_timestamp( &now, ×tamp );
1446 mods = (Modifications *) ch_malloc( sizeof( Modifications ) );
1447 mods->sml_type.bv_val = NULL;
1448 mods->sml_desc = ad_pwdChangedTime;
1449 if (pwmop != LDAP_MOD_DELETE) {
1450 mods->sml_op = LDAP_MOD_REPLACE;
1451 mods->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1452 ber_dupbv( &mods->sml_values[0], ×tamp );
1453 mods->sml_values[1].bv_len = 0;
1454 mods->sml_values[1].bv_val = NULL;
1455 assert( mods->sml_values[0].bv_val != NULL );
1457 mods->sml_op = LDAP_MOD_DELETE;
1458 mods->sml_values = NULL;
1460 mods->sml_flags = SLAP_MOD_INTERNAL;
1461 mods->sml_nvalues = NULL;
1462 mods->sml_next = NULL;
1463 modtail->sml_next = mods;
1466 if (attr_find(e->e_attrs, ad_pwdGraceUseTime )) {
1467 mods = (Modifications *) ch_malloc( sizeof( Modifications ) );
1468 mods->sml_op = LDAP_MOD_DELETE;
1469 mods->sml_flags = SLAP_MOD_INTERNAL;
1470 mods->sml_type.bv_val = NULL;
1471 mods->sml_desc = ad_pwdGraceUseTime;
1472 mods->sml_values = NULL;
1473 mods->sml_nvalues = NULL;
1474 mods->sml_next = NULL;
1475 modtail->sml_next = mods;
1479 /* Delete the pwdReset attribute, since it's being reset */
1480 if ((zapReset) && (attr_find(e->e_attrs, ad_pwdReset ))) {
1481 mods = (Modifications *) ch_malloc( sizeof( Modifications ) );
1482 mods->sml_op = LDAP_MOD_DELETE;
1483 mods->sml_flags = SLAP_MOD_INTERNAL;
1484 mods->sml_type.bv_val = NULL;
1485 mods->sml_desc = ad_pwdReset;
1486 mods->sml_values = NULL;
1487 mods->sml_nvalues = NULL;
1488 mods->sml_next = NULL;
1489 modtail->sml_next = mods;
1493 if (pp.pwdInHistory > 0) {
1494 if (hsize >= pp.pwdInHistory) {
1496 * We use the >= operator, since we are going to add
1497 * the existing password attribute value into the
1498 * history - thus the cardinality of history values is
1499 * about to rise by one.
1501 * If this would push it over the limit of history
1502 * values (remembering - the password policy could have
1503 * changed since the password was last altered), we must
1504 * delete at least 1 value from the pwdHistory list.
1506 * In fact, we delete '(#pwdHistory attrs - max pwd
1507 * history length) + 1' values, starting with the oldest.
1508 * This is easily evaluated, since the linked list is
1509 * created in ascending time order.
1511 mods = (Modifications *) ch_malloc( sizeof( Modifications ) );
1512 mods->sml_op = LDAP_MOD_DELETE;
1513 mods->sml_flags = SLAP_MOD_INTERNAL;
1514 mods->sml_type.bv_val = NULL;
1515 mods->sml_desc = ad_pwdHistory;
1516 mods->sml_nvalues = NULL;
1517 mods->sml_values = ch_calloc( sizeof( struct berval ),
1518 hsize - pp.pwdInHistory + 2 );
1519 mods->sml_values[ hsize - pp.pwdInHistory + 1 ].bv_val = NULL;
1520 mods->sml_values[ hsize - pp.pwdInHistory + 1 ].bv_len = 0;
1521 for(i=0,p=tl; i < (hsize - pp.pwdInHistory + 1); i++, p=p->next) {
1522 mods->sml_values[i].bv_val = NULL;
1523 mods->sml_values[i].bv_len = 0;
1524 ber_dupbv( &(mods->sml_values[i]), &p->bv );
1526 mods->sml_next = NULL;
1527 modtail->sml_next = mods;
1530 free_pwd_history_list( &tl );
1533 * Now add the existing password into the history list.
1534 * This will be executed even if the operation is to delete
1535 * the password entirely.
1537 * This isn't in the spec explicitly, but it seems to make
1538 * sense that the password history list is the list of all
1539 * previous passwords - even if they were deleted. Thus, if
1540 * someone tries to add a historical password at some future
1541 * point, it will fail.
1543 if ((pa = attr_find( e->e_attrs, pp.ad )) != NULL) {
1544 mods = (Modifications *) ch_malloc( sizeof( Modifications ) );
1545 mods->sml_op = LDAP_MOD_ADD;
1546 mods->sml_flags = SLAP_MOD_INTERNAL;
1547 mods->sml_type.bv_val = NULL;
1548 mods->sml_desc = ad_pwdHistory;
1549 mods->sml_nvalues = NULL;
1550 mods->sml_values = ch_calloc( sizeof( struct berval ), 2 );
1551 mods->sml_values[ 1 ].bv_val = NULL;
1552 mods->sml_values[ 1 ].bv_len = 0;
1553 make_pwd_history_value( timebuf, &mods->sml_values[0], pa );
1554 mods->sml_next = NULL;
1555 modtail->sml_next = mods;
1558 Debug( LDAP_DEBUG_TRACE,
1559 "ppolicy_modify: password attr lookup failed\n", 0, 0, 0 );
1564 * Controversial bit here. If the new password isn't hashed
1565 * (ie, is cleartext), we probably should hash it according
1566 * to the default hash. The reason for this is that we want
1567 * to use the policy if possible, but if we hash the password
1568 * before, then we're going to run into trouble when it
1569 * comes time to check the password.
1571 * Now, the right thing to do is to use the extended password
1572 * modify operation, but not all software can do this,
1573 * therefore it makes sense to hash the new password, now
1574 * we know it passes the policy requirements.
1576 * Of course, if the password is already hashed, then we
1580 if ((pi->hash_passwords) && (addmod) && !newpw.bv_val &&
1581 (password_scheme( &(addmod->sml_values[0]), NULL ) != LDAP_SUCCESS)) {
1582 struct berval hpw, bv;
1584 slap_passwd_hash( &(addmod->sml_values[0]), &hpw, &txt );
1585 if (hpw.bv_val == NULL) {
1587 * hashing didn't work. Emit an error.
1589 rs->sr_err = LDAP_OTHER;
1591 goto return_results;
1593 bv.bv_val = addmod->sml_values[0].bv_val;
1594 bv.bv_len = addmod->sml_values[0].bv_len;
1595 /* clear and discard the clear password */
1596 memset(bv.bv_val, 0, bv.bv_len);
1597 ber_memfree(bv.bv_val);
1598 addmod->sml_values[0].bv_val = hpw.bv_val;
1599 addmod->sml_values[0].bv_len = hpw.bv_len;
1602 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1603 be_entry_release_r( op, e );
1604 return SLAP_CB_CONTINUE;
1607 free_pwd_history_list( &tl );
1608 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1609 be_entry_release_r( op, e );
1611 LDAPControl **ctrls = NULL;
1613 ctrls = ch_calloc( sizeof( LDAPControl *) , 2 );
1614 ctrls[0] = create_passcontrol( -1, -1, pErr );
1616 rs->sr_ctrls = ctrls;
1618 send_ldap_result( op, rs );
1628 if ( ctrl->ldctl_value.bv_len ) {
1629 rs->sr_text = "passwordPolicyRequest control value not empty";
1630 return LDAP_PROTOCOL_ERROR;
1632 if ( ctrl->ldctl_iscritical ) {
1633 rs->sr_text = "passwordPolicyRequest control invalid criticality";
1634 return LDAP_PROTOCOL_ERROR;
1636 op->o_ctrlflag[ppolicy_cid] = SLAP_CONTROL_NONCRITICAL;
1638 return LDAP_SUCCESS;
1646 slap_overinst *on = (slap_overinst *) be->bd_info;
1648 /* Has User Schema been initialized yet? */
1649 if ( !pwd_UsSchema[0].ad[0] ) {
1653 for (i=0; pwd_UsSchema[i].def; i++) {
1654 code = slap_str2ad( pwd_UsSchema[i].def, pwd_UsSchema[i].ad, &err );
1656 fprintf( stderr, "User Schema Load failed %d: %s\n", code, err );
1662 on->on_bi.bi_private = ch_calloc( sizeof(pp_info), 1 );
1664 if ( dtblsize && !pwcons )
1665 pwcons = ch_calloc(sizeof(pw_conn), dtblsize );
1675 return overlay_register_control( be, LDAP_CONTROL_PASSWORDPOLICYREQUEST );
1683 slap_overinst *on = (slap_overinst *) be->bd_info;
1684 pp_info *pi = on->on_bi.bi_private;
1687 free( pi->def_policy.bv_val );
1702 slap_overinst *on = (slap_overinst *) be->bd_info;
1703 pp_info *pi = on->on_bi.bi_private;
1707 if ( strcasecmp( argv[0], "ppolicy_default" ) == 0 ) {
1709 fprintf( stderr, "%s: line %d: invalid arguments in \"ppolicy_default"
1710 " <policyDN>\n", fname, lineno );
1713 ber_str2bv( argv[1], 0, 0, &dn );
1714 if ( dnNormalize( 0, NULL, NULL, &dn, &pi->def_policy, NULL ) ) {
1715 fprintf( stderr, "%s: line %d: policyDN is invalid\n",
1721 } else if ( strcasecmp( argv[0], "ppolicy_use_lockout" ) == 0 ) {
1723 fprintf( stderr, "%s: line %d: ppolicy_use_lockout "
1724 "takes no arguments\n", fname, lineno );
1727 pi->use_lockout = 1;
1729 } else if ( strcasecmp( argv[0], "ppolicy_hash_cleartext" ) == 0 ) {
1731 fprintf( stderr, "%s: line %d: ppolicy_hash_cleartext "
1732 "takes no arguments\n", fname, lineno );
1735 pi->hash_passwords = 1;
1737 return SLAP_CONF_UNKNOWN;
1740 static char *extops[] = {
1741 LDAP_EXOP_MODIFY_PASSWD,
1745 static slap_overinst ppolicy;
1749 LDAPAttributeType *at;
1753 for (i=0; pwd_OpSchema[i].def; i++) {
1754 at = ldap_str2attributetype( pwd_OpSchema[i].def, &code, &err,
1755 LDAP_SCHEMA_ALLOW_ALL );
1757 fprintf( stderr, "AttributeType Load failed %s %s\n",
1758 ldap_scherr2str(code), err );
1761 code = at_add( at, 0, NULL, &err );
1763 slap_str2ad( at->at_names[0], pwd_OpSchema[i].ad, &err );
1767 fprintf( stderr, "AttributeType Load failed %s %s\n",
1768 scherr2str(code), err );
1771 /* Allow Manager to set these as needed */
1772 if ( is_at_no_user_mod( (*pwd_OpSchema[i].ad)->ad_type )) {
1773 (*pwd_OpSchema[i].ad)->ad_type->sat_flags |=
1778 code = register_supported_control( LDAP_CONTROL_PASSWORDPOLICYREQUEST,
1779 SLAP_CTRL_ADD|SLAP_CTRL_BIND|SLAP_CTRL_MODIFY|SLAP_CTRL_HIDE, extops,
1780 ppolicy_parseCtrl, &ppolicy_cid );
1781 if ( code != LDAP_SUCCESS ) {
1782 fprintf( stderr, "Failed to register control %d\n", code );
1786 ldap_pvt_thread_mutex_init( &chk_syntax_mutex );
1788 ppolicy.on_bi.bi_type = "ppolicy";
1789 ppolicy.on_bi.bi_db_init = ppolicy_db_init;
1790 ppolicy.on_bi.bi_db_open = ppolicy_db_open;
1791 ppolicy.on_bi.bi_db_config = ppolicy_config;
1792 ppolicy.on_bi.bi_db_close = ppolicy_close;
1794 ppolicy.on_bi.bi_op_add = ppolicy_add;
1795 ppolicy.on_bi.bi_op_bind = ppolicy_bind;
1796 ppolicy.on_bi.bi_op_compare = ppolicy_restrict;
1797 ppolicy.on_bi.bi_op_delete = ppolicy_restrict;
1798 ppolicy.on_bi.bi_op_modify = ppolicy_modify;
1799 ppolicy.on_bi.bi_op_search = ppolicy_restrict;
1800 ppolicy.on_bi.bi_connection_destroy = ppolicy_connection_destroy;
1802 return overlay_register( &ppolicy );
1805 #if SLAPD_OVER_PPOLICY == SLAPD_MOD_DYNAMIC
1806 int init_module(int argc, char *argv[]) {
1807 return ppolicy_init();
1811 #endif /* defined(SLAPD_OVER_PPOLICY) */