1*7f2fe78bSCy Schubert /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2*7f2fe78bSCy Schubert /* plugins/kdb/ldap/libkdb_ldap/lockout.c */
3*7f2fe78bSCy Schubert /*
4*7f2fe78bSCy Schubert * Copyright (C) 2009 by the Massachusetts Institute of Technology.
5*7f2fe78bSCy Schubert * All rights reserved.
6*7f2fe78bSCy Schubert *
7*7f2fe78bSCy Schubert * Export of this software from the United States of America may
8*7f2fe78bSCy Schubert * require a specific license from the United States Government.
9*7f2fe78bSCy Schubert * It is the responsibility of any person or organization contemplating
10*7f2fe78bSCy Schubert * export to obtain such a license before exporting.
11*7f2fe78bSCy Schubert *
12*7f2fe78bSCy Schubert * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13*7f2fe78bSCy Schubert * distribute this software and its documentation for any purpose and
14*7f2fe78bSCy Schubert * without fee is hereby granted, provided that the above copyright
15*7f2fe78bSCy Schubert * notice appear in all copies and that both that copyright notice and
16*7f2fe78bSCy Schubert * this permission notice appear in supporting documentation, and that
17*7f2fe78bSCy Schubert * the name of M.I.T. not be used in advertising or publicity pertaining
18*7f2fe78bSCy Schubert * to distribution of the software without specific, written prior
19*7f2fe78bSCy Schubert * permission. Furthermore if you modify this software you must label
20*7f2fe78bSCy Schubert * your software as modified software and not distribute it in such a
21*7f2fe78bSCy Schubert * fashion that it might be confused with the original M.I.T. software.
22*7f2fe78bSCy Schubert * M.I.T. makes no representations about the suitability of
23*7f2fe78bSCy Schubert * this software for any purpose. It is provided "as is" without express
24*7f2fe78bSCy Schubert * or implied warranty.
25*7f2fe78bSCy Schubert */
26*7f2fe78bSCy Schubert
27*7f2fe78bSCy Schubert #include <k5-int.h>
28*7f2fe78bSCy Schubert #include <kadm5/admin.h>
29*7f2fe78bSCy Schubert #include <kdb.h>
30*7f2fe78bSCy Schubert
31*7f2fe78bSCy Schubert #include "kdb_ldap.h"
32*7f2fe78bSCy Schubert #include "princ_xdr.h"
33*7f2fe78bSCy Schubert #include "ldap_principal.h"
34*7f2fe78bSCy Schubert #include "ldap_pwd_policy.h"
35*7f2fe78bSCy Schubert #include "ldap_tkt_policy.h"
36*7f2fe78bSCy Schubert
37*7f2fe78bSCy Schubert static krb5_error_code
lookup_lockout_policy(krb5_context context,krb5_db_entry * entry,krb5_kvno * pw_max_fail,krb5_deltat * pw_failcnt_interval,krb5_deltat * pw_lockout_duration)38*7f2fe78bSCy Schubert lookup_lockout_policy(krb5_context context,
39*7f2fe78bSCy Schubert krb5_db_entry *entry,
40*7f2fe78bSCy Schubert krb5_kvno *pw_max_fail,
41*7f2fe78bSCy Schubert krb5_deltat *pw_failcnt_interval,
42*7f2fe78bSCy Schubert krb5_deltat *pw_lockout_duration)
43*7f2fe78bSCy Schubert {
44*7f2fe78bSCy Schubert krb5_tl_data tl_data;
45*7f2fe78bSCy Schubert krb5_error_code code;
46*7f2fe78bSCy Schubert osa_princ_ent_rec adb;
47*7f2fe78bSCy Schubert
48*7f2fe78bSCy Schubert *pw_max_fail = 0;
49*7f2fe78bSCy Schubert *pw_failcnt_interval = 0;
50*7f2fe78bSCy Schubert *pw_lockout_duration = 0;
51*7f2fe78bSCy Schubert
52*7f2fe78bSCy Schubert tl_data.tl_data_type = KRB5_TL_KADM_DATA;
53*7f2fe78bSCy Schubert
54*7f2fe78bSCy Schubert code = krb5_dbe_lookup_tl_data(context, entry, &tl_data);
55*7f2fe78bSCy Schubert if (code != 0 || tl_data.tl_data_length == 0)
56*7f2fe78bSCy Schubert return code;
57*7f2fe78bSCy Schubert
58*7f2fe78bSCy Schubert memset(&adb, 0, sizeof(adb));
59*7f2fe78bSCy Schubert
60*7f2fe78bSCy Schubert code = krb5_lookup_tl_kadm_data(&tl_data, &adb);
61*7f2fe78bSCy Schubert if (code != 0)
62*7f2fe78bSCy Schubert return code;
63*7f2fe78bSCy Schubert
64*7f2fe78bSCy Schubert if (adb.policy != NULL) {
65*7f2fe78bSCy Schubert osa_policy_ent_t policy = NULL;
66*7f2fe78bSCy Schubert
67*7f2fe78bSCy Schubert code = krb5_ldap_get_password_policy(context, adb.policy, &policy);
68*7f2fe78bSCy Schubert if (code == 0) {
69*7f2fe78bSCy Schubert *pw_max_fail = policy->pw_max_fail;
70*7f2fe78bSCy Schubert *pw_failcnt_interval = policy->pw_failcnt_interval;
71*7f2fe78bSCy Schubert *pw_lockout_duration = policy->pw_lockout_duration;
72*7f2fe78bSCy Schubert }
73*7f2fe78bSCy Schubert krb5_db_free_policy(context, policy);
74*7f2fe78bSCy Schubert }
75*7f2fe78bSCy Schubert
76*7f2fe78bSCy Schubert ldap_osa_free_princ_ent(&adb);
77*7f2fe78bSCy Schubert
78*7f2fe78bSCy Schubert return 0;
79*7f2fe78bSCy Schubert }
80*7f2fe78bSCy Schubert
81*7f2fe78bSCy Schubert /* draft-behera-ldap-password-policy-10.txt 7.1 */
82*7f2fe78bSCy Schubert static krb5_boolean
locked_check_p(krb5_context context,krb5_timestamp stamp,krb5_kvno max_fail,krb5_timestamp lockout_duration,krb5_db_entry * entry)83*7f2fe78bSCy Schubert locked_check_p(krb5_context context,
84*7f2fe78bSCy Schubert krb5_timestamp stamp,
85*7f2fe78bSCy Schubert krb5_kvno max_fail,
86*7f2fe78bSCy Schubert krb5_timestamp lockout_duration,
87*7f2fe78bSCy Schubert krb5_db_entry *entry)
88*7f2fe78bSCy Schubert {
89*7f2fe78bSCy Schubert krb5_timestamp unlock_time;
90*7f2fe78bSCy Schubert
91*7f2fe78bSCy Schubert /* If the entry was unlocked since the last failure, it's not locked. */
92*7f2fe78bSCy Schubert if (krb5_dbe_lookup_last_admin_unlock(context, entry, &unlock_time) == 0 &&
93*7f2fe78bSCy Schubert !ts_after(entry->last_failed, unlock_time))
94*7f2fe78bSCy Schubert return FALSE;
95*7f2fe78bSCy Schubert
96*7f2fe78bSCy Schubert if (max_fail == 0 || entry->fail_auth_count < max_fail)
97*7f2fe78bSCy Schubert return FALSE;
98*7f2fe78bSCy Schubert
99*7f2fe78bSCy Schubert if (lockout_duration == 0)
100*7f2fe78bSCy Schubert return TRUE; /* principal permanently locked */
101*7f2fe78bSCy Schubert
102*7f2fe78bSCy Schubert return ts_after(ts_incr(entry->last_failed, lockout_duration), stamp);
103*7f2fe78bSCy Schubert }
104*7f2fe78bSCy Schubert
105*7f2fe78bSCy Schubert krb5_error_code
krb5_ldap_lockout_check_policy(krb5_context context,krb5_db_entry * entry,krb5_timestamp stamp)106*7f2fe78bSCy Schubert krb5_ldap_lockout_check_policy(krb5_context context,
107*7f2fe78bSCy Schubert krb5_db_entry *entry,
108*7f2fe78bSCy Schubert krb5_timestamp stamp)
109*7f2fe78bSCy Schubert {
110*7f2fe78bSCy Schubert krb5_error_code code;
111*7f2fe78bSCy Schubert kdb5_dal_handle *dal_handle;
112*7f2fe78bSCy Schubert krb5_ldap_context *ldap_context;
113*7f2fe78bSCy Schubert krb5_kvno max_fail = 0;
114*7f2fe78bSCy Schubert krb5_deltat failcnt_interval = 0;
115*7f2fe78bSCy Schubert krb5_deltat lockout_duration = 0;
116*7f2fe78bSCy Schubert
117*7f2fe78bSCy Schubert SETUP_CONTEXT();
118*7f2fe78bSCy Schubert if (ldap_context->disable_lockout)
119*7f2fe78bSCy Schubert return 0;
120*7f2fe78bSCy Schubert
121*7f2fe78bSCy Schubert code = lookup_lockout_policy(context, entry, &max_fail,
122*7f2fe78bSCy Schubert &failcnt_interval,
123*7f2fe78bSCy Schubert &lockout_duration);
124*7f2fe78bSCy Schubert if (code != 0)
125*7f2fe78bSCy Schubert return code;
126*7f2fe78bSCy Schubert
127*7f2fe78bSCy Schubert if (locked_check_p(context, stamp, max_fail, lockout_duration, entry))
128*7f2fe78bSCy Schubert return KRB5KDC_ERR_CLIENT_REVOKED;
129*7f2fe78bSCy Schubert
130*7f2fe78bSCy Schubert return 0;
131*7f2fe78bSCy Schubert }
132*7f2fe78bSCy Schubert
133*7f2fe78bSCy Schubert krb5_error_code
krb5_ldap_lockout_audit(krb5_context context,krb5_db_entry * entry,krb5_timestamp stamp,krb5_error_code status)134*7f2fe78bSCy Schubert krb5_ldap_lockout_audit(krb5_context context,
135*7f2fe78bSCy Schubert krb5_db_entry *entry,
136*7f2fe78bSCy Schubert krb5_timestamp stamp,
137*7f2fe78bSCy Schubert krb5_error_code status)
138*7f2fe78bSCy Schubert {
139*7f2fe78bSCy Schubert krb5_error_code code;
140*7f2fe78bSCy Schubert kdb5_dal_handle *dal_handle;
141*7f2fe78bSCy Schubert krb5_ldap_context *ldap_context;
142*7f2fe78bSCy Schubert krb5_kvno max_fail = 0;
143*7f2fe78bSCy Schubert krb5_deltat failcnt_interval = 0;
144*7f2fe78bSCy Schubert krb5_deltat lockout_duration = 0;
145*7f2fe78bSCy Schubert krb5_timestamp unlock_time;
146*7f2fe78bSCy Schubert
147*7f2fe78bSCy Schubert SETUP_CONTEXT();
148*7f2fe78bSCy Schubert
149*7f2fe78bSCy Schubert switch (status) {
150*7f2fe78bSCy Schubert case 0:
151*7f2fe78bSCy Schubert case KRB5KDC_ERR_PREAUTH_FAILED:
152*7f2fe78bSCy Schubert case KRB5KRB_AP_ERR_BAD_INTEGRITY:
153*7f2fe78bSCy Schubert break;
154*7f2fe78bSCy Schubert default:
155*7f2fe78bSCy Schubert return 0;
156*7f2fe78bSCy Schubert }
157*7f2fe78bSCy Schubert
158*7f2fe78bSCy Schubert if (entry == NULL)
159*7f2fe78bSCy Schubert return 0;
160*7f2fe78bSCy Schubert
161*7f2fe78bSCy Schubert if (!ldap_context->disable_lockout) {
162*7f2fe78bSCy Schubert code = lookup_lockout_policy(context, entry, &max_fail,
163*7f2fe78bSCy Schubert &failcnt_interval,
164*7f2fe78bSCy Schubert &lockout_duration);
165*7f2fe78bSCy Schubert if (code != 0)
166*7f2fe78bSCy Schubert return code;
167*7f2fe78bSCy Schubert }
168*7f2fe78bSCy Schubert
169*7f2fe78bSCy Schubert /*
170*7f2fe78bSCy Schubert * Don't continue to modify the DB for an already locked account.
171*7f2fe78bSCy Schubert * (In most cases, status will be KRB5KDC_ERR_CLIENT_REVOKED, and
172*7f2fe78bSCy Schubert * this check is unneeded, but in rare cases, we can fail with an
173*7f2fe78bSCy Schubert * integrity error or preauth failure before a policy check.)
174*7f2fe78bSCy Schubert */
175*7f2fe78bSCy Schubert if (locked_check_p(context, stamp, max_fail, lockout_duration, entry))
176*7f2fe78bSCy Schubert return 0;
177*7f2fe78bSCy Schubert
178*7f2fe78bSCy Schubert entry->mask = 0;
179*7f2fe78bSCy Schubert
180*7f2fe78bSCy Schubert /* Only mark the authentication as successful if the entry
181*7f2fe78bSCy Schubert * required preauthentication, otherwise we have no idea. */
182*7f2fe78bSCy Schubert if (status == 0 && (entry->attributes & KRB5_KDB_REQUIRES_PRE_AUTH)) {
183*7f2fe78bSCy Schubert if (!ldap_context->disable_lockout && entry->fail_auth_count != 0) {
184*7f2fe78bSCy Schubert entry->fail_auth_count = 0;
185*7f2fe78bSCy Schubert entry->mask |= KADM5_FAIL_AUTH_COUNT;
186*7f2fe78bSCy Schubert }
187*7f2fe78bSCy Schubert if (!ldap_context->disable_last_success) {
188*7f2fe78bSCy Schubert entry->last_success = stamp;
189*7f2fe78bSCy Schubert entry->mask |= KADM5_LAST_SUCCESS;
190*7f2fe78bSCy Schubert }
191*7f2fe78bSCy Schubert } else if (!ldap_context->disable_lockout &&
192*7f2fe78bSCy Schubert (status == KRB5KDC_ERR_PREAUTH_FAILED ||
193*7f2fe78bSCy Schubert status == KRB5KRB_AP_ERR_BAD_INTEGRITY)) {
194*7f2fe78bSCy Schubert if (krb5_dbe_lookup_last_admin_unlock(context, entry,
195*7f2fe78bSCy Schubert &unlock_time) == 0 &&
196*7f2fe78bSCy Schubert !ts_after(entry->last_failed, unlock_time)) {
197*7f2fe78bSCy Schubert /* Reset fail_auth_count after administrative unlock. */
198*7f2fe78bSCy Schubert entry->fail_auth_count = 0;
199*7f2fe78bSCy Schubert entry->mask |= KADM5_FAIL_AUTH_COUNT;
200*7f2fe78bSCy Schubert }
201*7f2fe78bSCy Schubert
202*7f2fe78bSCy Schubert if (failcnt_interval != 0 &&
203*7f2fe78bSCy Schubert ts_after(stamp, ts_incr(entry->last_failed, failcnt_interval))) {
204*7f2fe78bSCy Schubert /* Reset fail_auth_count after failcnt_interval */
205*7f2fe78bSCy Schubert entry->fail_auth_count = 0;
206*7f2fe78bSCy Schubert entry->mask |= KADM5_FAIL_AUTH_COUNT;
207*7f2fe78bSCy Schubert }
208*7f2fe78bSCy Schubert
209*7f2fe78bSCy Schubert entry->last_failed = stamp;
210*7f2fe78bSCy Schubert entry->mask |= KADM5_LAST_FAILED | KADM5_FAIL_AUTH_COUNT_INCREMENT;
211*7f2fe78bSCy Schubert }
212*7f2fe78bSCy Schubert
213*7f2fe78bSCy Schubert if (entry->mask) {
214*7f2fe78bSCy Schubert code = krb5_ldap_put_principal(context, entry, NULL);
215*7f2fe78bSCy Schubert if (code != 0)
216*7f2fe78bSCy Schubert return code;
217*7f2fe78bSCy Schubert }
218*7f2fe78bSCy Schubert
219*7f2fe78bSCy Schubert return 0;
220*7f2fe78bSCy Schubert }
221