xref: /freebsd/crypto/krb5/src/plugins/kdb/lmdb/lockout.c (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1*7f2fe78bSCy Schubert /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2*7f2fe78bSCy Schubert /* plugins/kdb/lmdb/lockout.c */
3*7f2fe78bSCy Schubert /*
4*7f2fe78bSCy Schubert  * Copyright (C) 2009, 2018 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 "kdb.h"
29*7f2fe78bSCy Schubert #include <kadm5/server_internal.h>
30*7f2fe78bSCy Schubert #include "kdb5.h"
31*7f2fe78bSCy Schubert #include "klmdb-int.h"
32*7f2fe78bSCy Schubert 
33*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)34*7f2fe78bSCy Schubert lookup_lockout_policy(krb5_context context, krb5_db_entry *entry,
35*7f2fe78bSCy Schubert                       krb5_kvno *pw_max_fail, krb5_deltat *pw_failcnt_interval,
36*7f2fe78bSCy Schubert                       krb5_deltat *pw_lockout_duration)
37*7f2fe78bSCy Schubert {
38*7f2fe78bSCy Schubert     krb5_tl_data tl_data;
39*7f2fe78bSCy Schubert     krb5_error_code code;
40*7f2fe78bSCy Schubert     osa_princ_ent_rec adb;
41*7f2fe78bSCy Schubert     XDR xdrs;
42*7f2fe78bSCy Schubert 
43*7f2fe78bSCy Schubert     *pw_max_fail = 0;
44*7f2fe78bSCy Schubert     *pw_failcnt_interval = 0;
45*7f2fe78bSCy Schubert     *pw_lockout_duration = 0;
46*7f2fe78bSCy Schubert 
47*7f2fe78bSCy Schubert     tl_data.tl_data_type = KRB5_TL_KADM_DATA;
48*7f2fe78bSCy Schubert 
49*7f2fe78bSCy Schubert     code = krb5_dbe_lookup_tl_data(context, entry, &tl_data);
50*7f2fe78bSCy Schubert     if (code != 0 || tl_data.tl_data_length == 0)
51*7f2fe78bSCy Schubert         return code;
52*7f2fe78bSCy Schubert 
53*7f2fe78bSCy Schubert     memset(&adb, 0, sizeof(adb));
54*7f2fe78bSCy Schubert     xdrmem_create(&xdrs, (char *)tl_data.tl_data_contents,
55*7f2fe78bSCy Schubert                   tl_data.tl_data_length, XDR_DECODE);
56*7f2fe78bSCy Schubert     if (!xdr_osa_princ_ent_rec(&xdrs, &adb)) {
57*7f2fe78bSCy Schubert         xdr_destroy(&xdrs);
58*7f2fe78bSCy Schubert         return KADM5_XDR_FAILURE;
59*7f2fe78bSCy Schubert     }
60*7f2fe78bSCy Schubert 
61*7f2fe78bSCy Schubert     if (adb.policy != NULL) {
62*7f2fe78bSCy Schubert         osa_policy_ent_t policy = NULL;
63*7f2fe78bSCy Schubert 
64*7f2fe78bSCy Schubert         code = klmdb_get_policy(context, adb.policy, &policy);
65*7f2fe78bSCy Schubert         if (code == 0) {
66*7f2fe78bSCy Schubert             *pw_max_fail = policy->pw_max_fail;
67*7f2fe78bSCy Schubert             *pw_failcnt_interval = policy->pw_failcnt_interval;
68*7f2fe78bSCy Schubert             *pw_lockout_duration = policy->pw_lockout_duration;
69*7f2fe78bSCy Schubert             krb5_db_free_policy(context, policy);
70*7f2fe78bSCy Schubert         }
71*7f2fe78bSCy Schubert     }
72*7f2fe78bSCy Schubert 
73*7f2fe78bSCy Schubert     xdr_destroy(&xdrs);
74*7f2fe78bSCy Schubert 
75*7f2fe78bSCy Schubert     xdrmem_create(&xdrs, NULL, 0, XDR_FREE);
76*7f2fe78bSCy Schubert     xdr_osa_princ_ent_rec(&xdrs, &adb);
77*7f2fe78bSCy Schubert     xdr_destroy(&xdrs);
78*7f2fe78bSCy Schubert 
79*7f2fe78bSCy Schubert     return 0;
80*7f2fe78bSCy Schubert }
81*7f2fe78bSCy Schubert 
82*7f2fe78bSCy Schubert /* draft-behera-ldap-password-policy-10.txt 7.1 */
83*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)84*7f2fe78bSCy Schubert locked_check_p(krb5_context context, krb5_timestamp stamp, krb5_kvno max_fail,
85*7f2fe78bSCy Schubert                krb5_timestamp lockout_duration, krb5_db_entry *entry)
86*7f2fe78bSCy Schubert {
87*7f2fe78bSCy Schubert     krb5_timestamp unlock_time;
88*7f2fe78bSCy Schubert 
89*7f2fe78bSCy Schubert     /* If the entry was unlocked since the last failure, it's not locked. */
90*7f2fe78bSCy Schubert     if (krb5_dbe_lookup_last_admin_unlock(context, entry, &unlock_time) == 0 &&
91*7f2fe78bSCy Schubert         !ts_after(entry->last_failed, unlock_time))
92*7f2fe78bSCy Schubert         return FALSE;
93*7f2fe78bSCy Schubert 
94*7f2fe78bSCy Schubert     if (max_fail == 0 || entry->fail_auth_count < max_fail)
95*7f2fe78bSCy Schubert         return FALSE;
96*7f2fe78bSCy Schubert 
97*7f2fe78bSCy Schubert     if (lockout_duration == 0)
98*7f2fe78bSCy Schubert         return TRUE; /* principal permanently locked */
99*7f2fe78bSCy Schubert 
100*7f2fe78bSCy Schubert     return ts_after(ts_incr(entry->last_failed, lockout_duration), stamp);
101*7f2fe78bSCy Schubert }
102*7f2fe78bSCy Schubert 
103*7f2fe78bSCy Schubert krb5_error_code
klmdb_lockout_check_policy(krb5_context context,krb5_db_entry * entry,krb5_timestamp stamp)104*7f2fe78bSCy Schubert klmdb_lockout_check_policy(krb5_context context, krb5_db_entry *entry,
105*7f2fe78bSCy Schubert                            krb5_timestamp stamp)
106*7f2fe78bSCy Schubert {
107*7f2fe78bSCy Schubert     krb5_error_code code;
108*7f2fe78bSCy Schubert     krb5_kvno max_fail = 0;
109*7f2fe78bSCy Schubert     krb5_deltat failcnt_interval = 0;
110*7f2fe78bSCy Schubert     krb5_deltat lockout_duration = 0;
111*7f2fe78bSCy Schubert 
112*7f2fe78bSCy Schubert     code = lookup_lockout_policy(context, entry, &max_fail, &failcnt_interval,
113*7f2fe78bSCy Schubert                                  &lockout_duration);
114*7f2fe78bSCy Schubert     if (code != 0)
115*7f2fe78bSCy Schubert         return code;
116*7f2fe78bSCy Schubert 
117*7f2fe78bSCy Schubert     if (locked_check_p(context, stamp, max_fail, lockout_duration, entry))
118*7f2fe78bSCy Schubert         return KRB5KDC_ERR_CLIENT_REVOKED;
119*7f2fe78bSCy Schubert 
120*7f2fe78bSCy Schubert     return 0;
121*7f2fe78bSCy Schubert }
122*7f2fe78bSCy Schubert 
123*7f2fe78bSCy Schubert krb5_error_code
klmdb_lockout_audit(krb5_context context,krb5_db_entry * entry,krb5_timestamp stamp,krb5_error_code status,krb5_boolean disable_last_success,krb5_boolean disable_lockout)124*7f2fe78bSCy Schubert klmdb_lockout_audit(krb5_context context, krb5_db_entry *entry,
125*7f2fe78bSCy Schubert                     krb5_timestamp stamp, krb5_error_code status,
126*7f2fe78bSCy Schubert                     krb5_boolean disable_last_success,
127*7f2fe78bSCy Schubert                     krb5_boolean disable_lockout)
128*7f2fe78bSCy Schubert {
129*7f2fe78bSCy Schubert     krb5_error_code ret;
130*7f2fe78bSCy Schubert     krb5_kvno max_fail = 0;
131*7f2fe78bSCy Schubert     krb5_deltat failcnt_interval = 0, lockout_duration = 0;
132*7f2fe78bSCy Schubert     krb5_boolean zero_fail_count = FALSE;
133*7f2fe78bSCy Schubert     krb5_boolean set_last_success = FALSE, set_last_failure = FALSE;
134*7f2fe78bSCy Schubert     krb5_timestamp unlock_time;
135*7f2fe78bSCy Schubert 
136*7f2fe78bSCy Schubert     if (status != 0 && status != KRB5KDC_ERR_PREAUTH_FAILED &&
137*7f2fe78bSCy Schubert         status != KRB5KRB_AP_ERR_BAD_INTEGRITY)
138*7f2fe78bSCy Schubert         return 0;
139*7f2fe78bSCy Schubert 
140*7f2fe78bSCy Schubert     if (!disable_lockout) {
141*7f2fe78bSCy Schubert         ret = lookup_lockout_policy(context, entry, &max_fail,
142*7f2fe78bSCy Schubert                                     &failcnt_interval, &lockout_duration);
143*7f2fe78bSCy Schubert         if (ret)
144*7f2fe78bSCy Schubert             return ret;
145*7f2fe78bSCy Schubert     }
146*7f2fe78bSCy Schubert 
147*7f2fe78bSCy Schubert     /*
148*7f2fe78bSCy Schubert      * Don't continue to modify the DB for an already locked account.
149*7f2fe78bSCy Schubert      * (In most cases, status will be KRB5KDC_ERR_CLIENT_REVOKED, and
150*7f2fe78bSCy Schubert      * this check is unneeded, but in rare cases, we can fail with an
151*7f2fe78bSCy Schubert      * integrity error or preauth failure before a policy check.)
152*7f2fe78bSCy Schubert      */
153*7f2fe78bSCy Schubert     if (locked_check_p(context, stamp, max_fail, lockout_duration, entry))
154*7f2fe78bSCy Schubert         return 0;
155*7f2fe78bSCy Schubert 
156*7f2fe78bSCy Schubert     /* Only mark the authentication as successful if the entry
157*7f2fe78bSCy Schubert      * required preauthentication; otherwise we have no idea. */
158*7f2fe78bSCy Schubert     if (status == 0 && (entry->attributes & KRB5_KDB_REQUIRES_PRE_AUTH)) {
159*7f2fe78bSCy Schubert         if (!disable_lockout && entry->fail_auth_count != 0)
160*7f2fe78bSCy Schubert             zero_fail_count = TRUE;
161*7f2fe78bSCy Schubert         if (!disable_last_success)
162*7f2fe78bSCy Schubert             set_last_success = TRUE;
163*7f2fe78bSCy Schubert     } else if (status != 0 && !disable_lockout) {
164*7f2fe78bSCy Schubert         /* Reset the failure counter after an administrative unlock. */
165*7f2fe78bSCy Schubert         if (krb5_dbe_lookup_last_admin_unlock(context, entry,
166*7f2fe78bSCy Schubert                                               &unlock_time) == 0 &&
167*7f2fe78bSCy Schubert             !ts_after(entry->last_failed, unlock_time))
168*7f2fe78bSCy Schubert             zero_fail_count = TRUE;
169*7f2fe78bSCy Schubert 
170*7f2fe78bSCy Schubert         /* Reset the failure counter after failcnt_interval. */
171*7f2fe78bSCy Schubert         if (failcnt_interval != 0 &&
172*7f2fe78bSCy Schubert             ts_after(stamp, ts_incr(entry->last_failed, failcnt_interval)))
173*7f2fe78bSCy Schubert             zero_fail_count = TRUE;
174*7f2fe78bSCy Schubert 
175*7f2fe78bSCy Schubert         set_last_failure = TRUE;
176*7f2fe78bSCy Schubert     }
177*7f2fe78bSCy Schubert 
178*7f2fe78bSCy Schubert     return klmdb_update_lockout(context, entry, stamp, zero_fail_count,
179*7f2fe78bSCy Schubert                                 set_last_success, set_last_failure);
180*7f2fe78bSCy Schubert }
181