Path: blob/main/crypto/krb5/src/plugins/kdb/lmdb/lockout.c
34914 views
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/* plugins/kdb/lmdb/lockout.c */2/*3* Copyright (C) 2009, 2018 by the Massachusetts Institute of Technology.4* All rights reserved.5*6* Export of this software from the United States of America may7* require a specific license from the United States Government.8* It is the responsibility of any person or organization contemplating9* export to obtain such a license before exporting.10*11* WITHIN THAT CONSTRAINT, permission to use, copy, modify, and12* distribute this software and its documentation for any purpose and13* without fee is hereby granted, provided that the above copyright14* notice appear in all copies and that both that copyright notice and15* this permission notice appear in supporting documentation, and that16* the name of M.I.T. not be used in advertising or publicity pertaining17* to distribution of the software without specific, written prior18* permission. Furthermore if you modify this software you must label19* your software as modified software and not distribute it in such a20* fashion that it might be confused with the original M.I.T. software.21* M.I.T. makes no representations about the suitability of22* this software for any purpose. It is provided "as is" without express23* or implied warranty.24*/2526#include "k5-int.h"27#include "kdb.h"28#include <kadm5/server_internal.h>29#include "kdb5.h"30#include "klmdb-int.h"3132static krb5_error_code33lookup_lockout_policy(krb5_context context, krb5_db_entry *entry,34krb5_kvno *pw_max_fail, krb5_deltat *pw_failcnt_interval,35krb5_deltat *pw_lockout_duration)36{37krb5_tl_data tl_data;38krb5_error_code code;39osa_princ_ent_rec adb;40XDR xdrs;4142*pw_max_fail = 0;43*pw_failcnt_interval = 0;44*pw_lockout_duration = 0;4546tl_data.tl_data_type = KRB5_TL_KADM_DATA;4748code = krb5_dbe_lookup_tl_data(context, entry, &tl_data);49if (code != 0 || tl_data.tl_data_length == 0)50return code;5152memset(&adb, 0, sizeof(adb));53xdrmem_create(&xdrs, (char *)tl_data.tl_data_contents,54tl_data.tl_data_length, XDR_DECODE);55if (!xdr_osa_princ_ent_rec(&xdrs, &adb)) {56xdr_destroy(&xdrs);57return KADM5_XDR_FAILURE;58}5960if (adb.policy != NULL) {61osa_policy_ent_t policy = NULL;6263code = klmdb_get_policy(context, adb.policy, &policy);64if (code == 0) {65*pw_max_fail = policy->pw_max_fail;66*pw_failcnt_interval = policy->pw_failcnt_interval;67*pw_lockout_duration = policy->pw_lockout_duration;68krb5_db_free_policy(context, policy);69}70}7172xdr_destroy(&xdrs);7374xdrmem_create(&xdrs, NULL, 0, XDR_FREE);75xdr_osa_princ_ent_rec(&xdrs, &adb);76xdr_destroy(&xdrs);7778return 0;79}8081/* draft-behera-ldap-password-policy-10.txt 7.1 */82static krb5_boolean83locked_check_p(krb5_context context, krb5_timestamp stamp, krb5_kvno max_fail,84krb5_timestamp lockout_duration, krb5_db_entry *entry)85{86krb5_timestamp unlock_time;8788/* If the entry was unlocked since the last failure, it's not locked. */89if (krb5_dbe_lookup_last_admin_unlock(context, entry, &unlock_time) == 0 &&90!ts_after(entry->last_failed, unlock_time))91return FALSE;9293if (max_fail == 0 || entry->fail_auth_count < max_fail)94return FALSE;9596if (lockout_duration == 0)97return TRUE; /* principal permanently locked */9899return ts_after(ts_incr(entry->last_failed, lockout_duration), stamp);100}101102krb5_error_code103klmdb_lockout_check_policy(krb5_context context, krb5_db_entry *entry,104krb5_timestamp stamp)105{106krb5_error_code code;107krb5_kvno max_fail = 0;108krb5_deltat failcnt_interval = 0;109krb5_deltat lockout_duration = 0;110111code = lookup_lockout_policy(context, entry, &max_fail, &failcnt_interval,112&lockout_duration);113if (code != 0)114return code;115116if (locked_check_p(context, stamp, max_fail, lockout_duration, entry))117return KRB5KDC_ERR_CLIENT_REVOKED;118119return 0;120}121122krb5_error_code123klmdb_lockout_audit(krb5_context context, krb5_db_entry *entry,124krb5_timestamp stamp, krb5_error_code status,125krb5_boolean disable_last_success,126krb5_boolean disable_lockout)127{128krb5_error_code ret;129krb5_kvno max_fail = 0;130krb5_deltat failcnt_interval = 0, lockout_duration = 0;131krb5_boolean zero_fail_count = FALSE;132krb5_boolean set_last_success = FALSE, set_last_failure = FALSE;133krb5_timestamp unlock_time;134135if (status != 0 && status != KRB5KDC_ERR_PREAUTH_FAILED &&136status != KRB5KRB_AP_ERR_BAD_INTEGRITY)137return 0;138139if (!disable_lockout) {140ret = lookup_lockout_policy(context, entry, &max_fail,141&failcnt_interval, &lockout_duration);142if (ret)143return ret;144}145146/*147* Don't continue to modify the DB for an already locked account.148* (In most cases, status will be KRB5KDC_ERR_CLIENT_REVOKED, and149* this check is unneeded, but in rare cases, we can fail with an150* integrity error or preauth failure before a policy check.)151*/152if (locked_check_p(context, stamp, max_fail, lockout_duration, entry))153return 0;154155/* Only mark the authentication as successful if the entry156* required preauthentication; otherwise we have no idea. */157if (status == 0 && (entry->attributes & KRB5_KDB_REQUIRES_PRE_AUTH)) {158if (!disable_lockout && entry->fail_auth_count != 0)159zero_fail_count = TRUE;160if (!disable_last_success)161set_last_success = TRUE;162} else if (status != 0 && !disable_lockout) {163/* Reset the failure counter after an administrative unlock. */164if (krb5_dbe_lookup_last_admin_unlock(context, entry,165&unlock_time) == 0 &&166!ts_after(entry->last_failed, unlock_time))167zero_fail_count = TRUE;168169/* Reset the failure counter after failcnt_interval. */170if (failcnt_interval != 0 &&171ts_after(stamp, ts_incr(entry->last_failed, failcnt_interval)))172zero_fail_count = TRUE;173174set_last_failure = TRUE;175}176177return klmdb_update_lockout(context, entry, stamp, zero_fail_count,178set_last_success, set_last_failure);179}180181182