1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
3 * plugins/kdb/ldap/lockout.c
5 * Copyright (C) 2009 by the Massachusetts Institute of Technology.
8 * Export of this software from the United States of America may
9 * require a specific license from the United States Government.
10 * It is the responsibility of any person or organization contemplating
11 * export to obtain such a license before exporting.
13 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
14 * distribute this software and its documentation for any purpose and
15 * without fee is hereby granted, provided that the above copyright
16 * notice appear in all copies and that both that copyright notice and
17 * this permission notice appear in supporting documentation, and that
18 * the name of M.I.T. not be used in advertising or publicity pertaining
19 * to distribution of the software without specific, written prior
20 * permission. Furthermore if you modify this software you must label
21 * your software as modified software and not distribute it in such a
22 * fashion that it might be confused with the original M.I.T. software.
23 * M.I.T. makes no representations about the suitability of
24 * this software for any purpose. It is provided "as is" without express
25 * or implied warranty.
35 #include <kadm5/admin.h>
39 #include "princ_xdr.h"
40 #include "ldap_principal.h"
41 #include "ldap_pwd_policy.h"
42 #include "ldap_tkt_policy.h"
44 static krb5_error_code
45 lookup_lockout_policy(krb5_context context,
47 krb5_kvno *pw_max_fail,
48 krb5_deltat *pw_failcnt_interval,
49 krb5_deltat *pw_lockout_duration)
53 osa_princ_ent_rec adb;
57 *pw_failcnt_interval = 0;
58 *pw_lockout_duration = 0;
60 tl_data.tl_data_type = KRB5_TL_KADM_DATA;
62 code = krb5_dbe_lookup_tl_data(context, entry, &tl_data);
63 if (code != 0 || tl_data.tl_data_length == 0)
66 memset(&adb, 0, sizeof(adb));
68 code = krb5_lookup_tl_kadm_data(&tl_data, &adb);
72 if (adb.policy != NULL) {
73 osa_policy_ent_t policy = NULL;
75 code = krb5_ldap_get_password_policy(context, adb.policy, &policy);
77 *pw_max_fail = policy->pw_max_fail;
78 *pw_failcnt_interval = policy->pw_failcnt_interval;
79 *pw_lockout_duration = policy->pw_lockout_duration;
81 krb5_ldap_free_password_policy(context, policy);
84 xdrmem_create(&xdrs, NULL, 0, XDR_FREE);
85 ldap_xdr_osa_princ_ent_rec(&xdrs, &adb);
91 /* draft-behera-ldap-password-policy-10.txt 7.1 */
93 locked_check_p(krb5_context context,
96 krb5_timestamp lockout_duration,
99 krb5_timestamp unlock_time;
101 /* If the entry was unlocked since the last failure, it's not locked. */
102 if (krb5_dbe_lookup_last_admin_unlock(context, entry, &unlock_time) == 0 &&
103 entry->last_failed <= unlock_time)
106 if (max_fail == 0 || entry->fail_auth_count < max_fail)
109 if (lockout_duration == 0)
110 return TRUE; /* principal permanently locked */
112 return (stamp < entry->last_failed + lockout_duration);
116 krb5_ldap_lockout_check_policy(krb5_context context,
117 krb5_db_entry *entry,
118 krb5_timestamp stamp)
120 krb5_error_code code;
121 kdb5_dal_handle *dal_handle;
122 krb5_ldap_context *ldap_context;
123 krb5_kvno max_fail = 0;
124 krb5_deltat failcnt_interval = 0;
125 krb5_deltat lockout_duration = 0;
128 if (ldap_context->disable_lockout)
131 code = lookup_lockout_policy(context, entry, &max_fail,
134 if (code != 0 || failcnt_interval == 0)
137 if (locked_check_p(context, stamp, max_fail, lockout_duration, entry))
138 return KRB5KDC_ERR_CLIENT_REVOKED;
144 krb5_ldap_lockout_audit(krb5_context context,
145 krb5_db_entry *entry,
146 krb5_timestamp stamp,
147 krb5_error_code status)
149 krb5_error_code code;
150 kdb5_dal_handle *dal_handle;
151 krb5_ldap_context *ldap_context;
152 krb5_kvno max_fail = 0;
153 krb5_deltat failcnt_interval = 0;
154 krb5_deltat lockout_duration = 0;
155 krb5_timestamp unlock_time;
161 case KRB5KDC_ERR_PREAUTH_FAILED:
162 case KRB5KRB_AP_ERR_BAD_INTEGRITY:
171 if (!ldap_context->disable_lockout) {
172 code = lookup_lockout_policy(context, entry, &max_fail,
180 * Don't continue to modify the DB for an already locked account.
181 * (In most cases, status will be KRB5KDC_ERR_CLIENT_REVOKED, and
182 * this check is unneeded, but in rare cases, we can fail with an
183 * integrity error or preauth failure before a policy check.)
185 if (locked_check_p(context, stamp, max_fail, lockout_duration, entry))
190 /* Only mark the authentication as successful if the entry
191 * required preauthentication, otherwise we have no idea. */
192 if (status == 0 && (entry->attributes & KRB5_KDB_REQUIRES_PRE_AUTH)) {
193 if (!ldap_context->disable_lockout && entry->fail_auth_count != 0) {
194 entry->fail_auth_count = 0;
195 entry->mask |= KADM5_FAIL_AUTH_COUNT;
197 if (!ldap_context->disable_last_success) {
198 entry->last_success = stamp;
199 entry->mask |= KADM5_LAST_SUCCESS;
201 } else if (!ldap_context->disable_lockout &&
202 (status == KRB5KDC_ERR_PREAUTH_FAILED ||
203 status == KRB5KRB_AP_ERR_BAD_INTEGRITY)) {
204 if (krb5_dbe_lookup_last_admin_unlock(context, entry,
205 &unlock_time) == 0 &&
206 entry->last_failed <= unlock_time) {
207 /* Reset fail_auth_count after administrative unlock. */
208 entry->fail_auth_count = 0;
209 entry->mask |= KADM5_FAIL_AUTH_COUNT;
212 if (failcnt_interval != 0 &&
213 stamp > entry->last_failed + failcnt_interval) {
214 /* Reset fail_auth_count after failcnt_interval */
215 entry->fail_auth_count = 0;
216 entry->mask |= KADM5_FAIL_AUTH_COUNT;
219 entry->last_failed = stamp;
220 entry->mask |= KADM5_LAST_FAILED | KADM5_FAIL_AUTH_COUNT_INCREMENT;
224 code = krb5_ldap_put_principal(context, entry, NULL);