xref: /freebsd/crypto/krb5/src/plugins/kdb/db2/lockout.c (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1*7f2fe78bSCy Schubert /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2*7f2fe78bSCy Schubert /* plugins/kdb/db2/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 "kdb.h"
29*7f2fe78bSCy Schubert #include <stdio.h>
30*7f2fe78bSCy Schubert #include <errno.h>
31*7f2fe78bSCy Schubert #include <kadm5/server_internal.h>
32*7f2fe78bSCy Schubert #include "kdb5.h"
33*7f2fe78bSCy Schubert #include "kdb_db2.h"
34*7f2fe78bSCy Schubert 
35*7f2fe78bSCy Schubert /*
36*7f2fe78bSCy Schubert  * Helper routines for databases that wish to use the default
37*7f2fe78bSCy Schubert  * principal lockout functionality.
38*7f2fe78bSCy Schubert  */
39*7f2fe78bSCy Schubert 
40*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)41*7f2fe78bSCy Schubert lookup_lockout_policy(krb5_context context,
42*7f2fe78bSCy Schubert                       krb5_db_entry *entry,
43*7f2fe78bSCy Schubert                       krb5_kvno *pw_max_fail,
44*7f2fe78bSCy Schubert                       krb5_deltat *pw_failcnt_interval,
45*7f2fe78bSCy Schubert                       krb5_deltat *pw_lockout_duration)
46*7f2fe78bSCy Schubert {
47*7f2fe78bSCy Schubert     krb5_tl_data tl_data;
48*7f2fe78bSCy Schubert     krb5_error_code code;
49*7f2fe78bSCy Schubert     osa_princ_ent_rec adb;
50*7f2fe78bSCy Schubert     XDR xdrs;
51*7f2fe78bSCy Schubert 
52*7f2fe78bSCy Schubert     *pw_max_fail = 0;
53*7f2fe78bSCy Schubert     *pw_failcnt_interval = 0;
54*7f2fe78bSCy Schubert     *pw_lockout_duration = 0;
55*7f2fe78bSCy Schubert 
56*7f2fe78bSCy Schubert     tl_data.tl_data_type = KRB5_TL_KADM_DATA;
57*7f2fe78bSCy Schubert 
58*7f2fe78bSCy Schubert     code = krb5_dbe_lookup_tl_data(context, entry, &tl_data);
59*7f2fe78bSCy Schubert     if (code != 0 || tl_data.tl_data_length == 0)
60*7f2fe78bSCy Schubert         return code;
61*7f2fe78bSCy Schubert 
62*7f2fe78bSCy Schubert     memset(&adb, 0, sizeof(adb));
63*7f2fe78bSCy Schubert     xdrmem_create(&xdrs, (char *)tl_data.tl_data_contents,
64*7f2fe78bSCy Schubert                   tl_data.tl_data_length, XDR_DECODE);
65*7f2fe78bSCy Schubert     if (!xdr_osa_princ_ent_rec(&xdrs, &adb)) {
66*7f2fe78bSCy Schubert         xdr_destroy(&xdrs);
67*7f2fe78bSCy Schubert         return KADM5_XDR_FAILURE;
68*7f2fe78bSCy Schubert     }
69*7f2fe78bSCy Schubert 
70*7f2fe78bSCy Schubert     if (adb.policy != NULL) {
71*7f2fe78bSCy Schubert         osa_policy_ent_t policy = NULL;
72*7f2fe78bSCy Schubert 
73*7f2fe78bSCy Schubert         code = krb5_db2_get_policy(context, adb.policy, &policy);
74*7f2fe78bSCy Schubert         if (code == 0) {
75*7f2fe78bSCy Schubert             *pw_max_fail = policy->pw_max_fail;
76*7f2fe78bSCy Schubert             *pw_failcnt_interval = policy->pw_failcnt_interval;
77*7f2fe78bSCy Schubert             *pw_lockout_duration = policy->pw_lockout_duration;
78*7f2fe78bSCy Schubert             krb5_db_free_policy(context, policy);
79*7f2fe78bSCy Schubert         }
80*7f2fe78bSCy Schubert     }
81*7f2fe78bSCy Schubert 
82*7f2fe78bSCy Schubert     xdr_destroy(&xdrs);
83*7f2fe78bSCy Schubert 
84*7f2fe78bSCy Schubert     xdrmem_create(&xdrs, NULL, 0, XDR_FREE);
85*7f2fe78bSCy Schubert     xdr_osa_princ_ent_rec(&xdrs, &adb);
86*7f2fe78bSCy Schubert     xdr_destroy(&xdrs);
87*7f2fe78bSCy Schubert 
88*7f2fe78bSCy Schubert     return 0;
89*7f2fe78bSCy Schubert }
90*7f2fe78bSCy Schubert 
91*7f2fe78bSCy Schubert /* draft-behera-ldap-password-policy-10.txt 7.1 */
92*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)93*7f2fe78bSCy Schubert locked_check_p(krb5_context context,
94*7f2fe78bSCy Schubert                krb5_timestamp stamp,
95*7f2fe78bSCy Schubert                krb5_kvno max_fail,
96*7f2fe78bSCy Schubert                krb5_timestamp lockout_duration,
97*7f2fe78bSCy Schubert                krb5_db_entry *entry)
98*7f2fe78bSCy Schubert {
99*7f2fe78bSCy Schubert     krb5_timestamp unlock_time;
100*7f2fe78bSCy Schubert 
101*7f2fe78bSCy Schubert     /* If the entry was unlocked since the last failure, it's not locked. */
102*7f2fe78bSCy Schubert     if (krb5_dbe_lookup_last_admin_unlock(context, entry, &unlock_time) == 0 &&
103*7f2fe78bSCy Schubert         !ts_after(entry->last_failed, unlock_time))
104*7f2fe78bSCy Schubert         return FALSE;
105*7f2fe78bSCy Schubert 
106*7f2fe78bSCy Schubert     if (max_fail == 0 || entry->fail_auth_count < max_fail)
107*7f2fe78bSCy Schubert         return FALSE;
108*7f2fe78bSCy Schubert 
109*7f2fe78bSCy Schubert     if (lockout_duration == 0)
110*7f2fe78bSCy Schubert         return TRUE; /* principal permanently locked */
111*7f2fe78bSCy Schubert 
112*7f2fe78bSCy Schubert     return ts_after(ts_incr(entry->last_failed, lockout_duration), stamp);
113*7f2fe78bSCy Schubert }
114*7f2fe78bSCy Schubert 
115*7f2fe78bSCy Schubert krb5_error_code
krb5_db2_lockout_check_policy(krb5_context context,krb5_db_entry * entry,krb5_timestamp stamp)116*7f2fe78bSCy Schubert krb5_db2_lockout_check_policy(krb5_context context,
117*7f2fe78bSCy Schubert                               krb5_db_entry *entry,
118*7f2fe78bSCy Schubert                               krb5_timestamp stamp)
119*7f2fe78bSCy Schubert {
120*7f2fe78bSCy Schubert     krb5_error_code code;
121*7f2fe78bSCy Schubert     krb5_kvno max_fail = 0;
122*7f2fe78bSCy Schubert     krb5_deltat failcnt_interval = 0;
123*7f2fe78bSCy Schubert     krb5_deltat lockout_duration = 0;
124*7f2fe78bSCy Schubert     krb5_db2_context *db_ctx = context->dal_handle->db_context;
125*7f2fe78bSCy Schubert 
126*7f2fe78bSCy Schubert     if (db_ctx->disable_lockout)
127*7f2fe78bSCy Schubert         return 0;
128*7f2fe78bSCy Schubert 
129*7f2fe78bSCy Schubert     code = lookup_lockout_policy(context, entry, &max_fail,
130*7f2fe78bSCy Schubert                                  &failcnt_interval,
131*7f2fe78bSCy Schubert                                  &lockout_duration);
132*7f2fe78bSCy Schubert     if (code != 0)
133*7f2fe78bSCy Schubert         return code;
134*7f2fe78bSCy Schubert 
135*7f2fe78bSCy Schubert     if (locked_check_p(context, stamp, max_fail, lockout_duration, entry))
136*7f2fe78bSCy Schubert         return KRB5KDC_ERR_CLIENT_REVOKED;
137*7f2fe78bSCy Schubert 
138*7f2fe78bSCy Schubert     return 0;
139*7f2fe78bSCy Schubert }
140*7f2fe78bSCy Schubert 
141*7f2fe78bSCy Schubert krb5_error_code
krb5_db2_lockout_audit(krb5_context context,krb5_db_entry * entry,krb5_timestamp stamp,krb5_error_code status)142*7f2fe78bSCy Schubert krb5_db2_lockout_audit(krb5_context context,
143*7f2fe78bSCy Schubert                        krb5_db_entry *entry,
144*7f2fe78bSCy Schubert                        krb5_timestamp stamp,
145*7f2fe78bSCy Schubert                        krb5_error_code status)
146*7f2fe78bSCy Schubert {
147*7f2fe78bSCy Schubert     krb5_error_code code;
148*7f2fe78bSCy Schubert     krb5_kvno max_fail = 0;
149*7f2fe78bSCy Schubert     krb5_deltat failcnt_interval = 0;
150*7f2fe78bSCy Schubert     krb5_deltat lockout_duration = 0;
151*7f2fe78bSCy Schubert     krb5_db2_context *db_ctx = context->dal_handle->db_context;
152*7f2fe78bSCy Schubert     krb5_boolean need_update = FALSE;
153*7f2fe78bSCy Schubert     krb5_timestamp unlock_time;
154*7f2fe78bSCy Schubert 
155*7f2fe78bSCy Schubert     switch (status) {
156*7f2fe78bSCy Schubert     case 0:
157*7f2fe78bSCy Schubert     case KRB5KDC_ERR_PREAUTH_FAILED:
158*7f2fe78bSCy Schubert     case KRB5KRB_AP_ERR_BAD_INTEGRITY:
159*7f2fe78bSCy Schubert         break;
160*7f2fe78bSCy Schubert     default:
161*7f2fe78bSCy Schubert         return 0;
162*7f2fe78bSCy Schubert     }
163*7f2fe78bSCy Schubert 
164*7f2fe78bSCy Schubert     if (entry == NULL)
165*7f2fe78bSCy Schubert         return 0;
166*7f2fe78bSCy Schubert 
167*7f2fe78bSCy Schubert     if (!db_ctx->disable_lockout) {
168*7f2fe78bSCy Schubert         code = lookup_lockout_policy(context, entry, &max_fail,
169*7f2fe78bSCy Schubert                                      &failcnt_interval, &lockout_duration);
170*7f2fe78bSCy Schubert         if (code != 0)
171*7f2fe78bSCy Schubert             return code;
172*7f2fe78bSCy Schubert     }
173*7f2fe78bSCy Schubert 
174*7f2fe78bSCy Schubert     /*
175*7f2fe78bSCy Schubert      * Don't continue to modify the DB for an already locked account.
176*7f2fe78bSCy Schubert      * (In most cases, status will be KRB5KDC_ERR_CLIENT_REVOKED, and
177*7f2fe78bSCy Schubert      * this check is unneeded, but in rare cases, we can fail with an
178*7f2fe78bSCy Schubert      * integrity error or preauth failure before a policy check.)
179*7f2fe78bSCy Schubert      */
180*7f2fe78bSCy Schubert     if (locked_check_p(context, stamp, max_fail, lockout_duration, entry))
181*7f2fe78bSCy Schubert         return 0;
182*7f2fe78bSCy Schubert 
183*7f2fe78bSCy Schubert     /* Only mark the authentication as successful if the entry
184*7f2fe78bSCy Schubert      * required preauthentication, otherwise we have no idea. */
185*7f2fe78bSCy Schubert     if (status == 0 && (entry->attributes & KRB5_KDB_REQUIRES_PRE_AUTH)) {
186*7f2fe78bSCy Schubert         if (!db_ctx->disable_lockout && entry->fail_auth_count != 0) {
187*7f2fe78bSCy Schubert             entry->fail_auth_count = 0;
188*7f2fe78bSCy Schubert             need_update = TRUE;
189*7f2fe78bSCy Schubert         }
190*7f2fe78bSCy Schubert         if (!db_ctx->disable_last_success) {
191*7f2fe78bSCy Schubert             entry->last_success = stamp;
192*7f2fe78bSCy Schubert             need_update = TRUE;
193*7f2fe78bSCy Schubert         }
194*7f2fe78bSCy Schubert     } else if (!db_ctx->disable_lockout &&
195*7f2fe78bSCy Schubert                (status == KRB5KDC_ERR_PREAUTH_FAILED ||
196*7f2fe78bSCy Schubert                 status == KRB5KRB_AP_ERR_BAD_INTEGRITY)) {
197*7f2fe78bSCy Schubert         if (krb5_dbe_lookup_last_admin_unlock(context, entry,
198*7f2fe78bSCy Schubert                                               &unlock_time) == 0 &&
199*7f2fe78bSCy Schubert             !ts_after(entry->last_failed, unlock_time)) {
200*7f2fe78bSCy Schubert             /* Reset fail_auth_count after administrative unlock. */
201*7f2fe78bSCy Schubert             entry->fail_auth_count = 0;
202*7f2fe78bSCy Schubert         }
203*7f2fe78bSCy Schubert 
204*7f2fe78bSCy Schubert         if (failcnt_interval != 0 &&
205*7f2fe78bSCy Schubert             ts_after(stamp, ts_incr(entry->last_failed, failcnt_interval))) {
206*7f2fe78bSCy Schubert             /* Reset fail_auth_count after failcnt_interval. */
207*7f2fe78bSCy Schubert             entry->fail_auth_count = 0;
208*7f2fe78bSCy Schubert         }
209*7f2fe78bSCy Schubert 
210*7f2fe78bSCy Schubert         entry->last_failed = stamp;
211*7f2fe78bSCy Schubert         entry->fail_auth_count++;
212*7f2fe78bSCy Schubert         need_update = TRUE;
213*7f2fe78bSCy Schubert     }
214*7f2fe78bSCy Schubert 
215*7f2fe78bSCy Schubert     if (need_update) {
216*7f2fe78bSCy Schubert         code = krb5_db2_put_principal(context, entry, NULL);
217*7f2fe78bSCy Schubert         if (code != 0)
218*7f2fe78bSCy Schubert             return code;
219*7f2fe78bSCy Schubert     }
220*7f2fe78bSCy Schubert 
221*7f2fe78bSCy Schubert     return 0;
222*7f2fe78bSCy Schubert }
223