Blame src/plugins/kdb/lmdb/lockout.c

Packit Service 99d1c0
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
Packit Service 99d1c0
/* plugins/kdb/lmdb/lockout.c */
Packit Service 99d1c0
/*
Packit Service 99d1c0
 * Copyright (C) 2009, 2018 by the Massachusetts Institute of Technology.
Packit Service 99d1c0
 * All rights reserved.
Packit Service 99d1c0
 *
Packit Service 99d1c0
 * Export of this software from the United States of America may
Packit Service 99d1c0
 *   require a specific license from the United States Government.
Packit Service 99d1c0
 *   It is the responsibility of any person or organization contemplating
Packit Service 99d1c0
 *   export to obtain such a license before exporting.
Packit Service 99d1c0
 *
Packit Service 99d1c0
 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
Packit Service 99d1c0
 * distribute this software and its documentation for any purpose and
Packit Service 99d1c0
 * without fee is hereby granted, provided that the above copyright
Packit Service 99d1c0
 * notice appear in all copies and that both that copyright notice and
Packit Service 99d1c0
 * this permission notice appear in supporting documentation, and that
Packit Service 99d1c0
 * the name of M.I.T. not be used in advertising or publicity pertaining
Packit Service 99d1c0
 * to distribution of the software without specific, written prior
Packit Service 99d1c0
 * permission.  Furthermore if you modify this software you must label
Packit Service 99d1c0
 * your software as modified software and not distribute it in such a
Packit Service 99d1c0
 * fashion that it might be confused with the original M.I.T. software.
Packit Service 99d1c0
 * M.I.T. makes no representations about the suitability of
Packit Service 99d1c0
 * this software for any purpose.  It is provided "as is" without express
Packit Service 99d1c0
 * or implied warranty.
Packit Service 99d1c0
 */
Packit Service 99d1c0
Packit Service 99d1c0
#include "k5-int.h"
Packit Service 99d1c0
#include "kdb.h"
Packit Service 99d1c0
#include <kadm5/server_internal.h>
Packit Service 99d1c0
#include "kdb5.h"
Packit Service 99d1c0
#include "klmdb-int.h"
Packit Service 99d1c0
Packit Service 99d1c0
static krb5_error_code
Packit Service 99d1c0
lookup_lockout_policy(krb5_context context, krb5_db_entry *entry,
Packit Service 99d1c0
                      krb5_kvno *pw_max_fail, krb5_deltat *pw_failcnt_interval,
Packit Service 99d1c0
                      krb5_deltat *pw_lockout_duration)
Packit Service 99d1c0
{
Packit Service 99d1c0
    krb5_tl_data tl_data;
Packit Service 99d1c0
    krb5_error_code code;
Packit Service 99d1c0
    osa_princ_ent_rec adb;
Packit Service 99d1c0
    XDR xdrs;
Packit Service 99d1c0
Packit Service 99d1c0
    *pw_max_fail = 0;
Packit Service 99d1c0
    *pw_failcnt_interval = 0;
Packit Service 99d1c0
    *pw_lockout_duration = 0;
Packit Service 99d1c0
Packit Service 99d1c0
    tl_data.tl_data_type = KRB5_TL_KADM_DATA;
Packit Service 99d1c0
Packit Service 99d1c0
    code = krb5_dbe_lookup_tl_data(context, entry, &tl_data);
Packit Service 99d1c0
    if (code != 0 || tl_data.tl_data_length == 0)
Packit Service 99d1c0
        return code;
Packit Service 99d1c0
Packit Service 99d1c0
    memset(&adb, 0, sizeof(adb));
Packit Service 99d1c0
    xdrmem_create(&xdrs, (char *)tl_data.tl_data_contents,
Packit Service 99d1c0
                  tl_data.tl_data_length, XDR_DECODE);
Packit Service 99d1c0
    if (!xdr_osa_princ_ent_rec(&xdrs, &adb)) {
Packit Service 99d1c0
        xdr_destroy(&xdrs);
Packit Service 99d1c0
        return KADM5_XDR_FAILURE;
Packit Service 99d1c0
    }
Packit Service 99d1c0
Packit Service 99d1c0
    if (adb.policy != NULL) {
Packit Service 99d1c0
        osa_policy_ent_t policy = NULL;
Packit Service 99d1c0
Packit Service 99d1c0
        code = klmdb_get_policy(context, adb.policy, &policy);
Packit Service 99d1c0
        if (code == 0) {
Packit Service 99d1c0
            *pw_max_fail = policy->pw_max_fail;
Packit Service 99d1c0
            *pw_failcnt_interval = policy->pw_failcnt_interval;
Packit Service 99d1c0
            *pw_lockout_duration = policy->pw_lockout_duration;
Packit Service 99d1c0
            krb5_db_free_policy(context, policy);
Packit Service 99d1c0
        }
Packit Service 99d1c0
    }
Packit Service 99d1c0
Packit Service 99d1c0
    xdr_destroy(&xdrs);
Packit Service 99d1c0
Packit Service 99d1c0
    xdrmem_create(&xdrs, NULL, 0, XDR_FREE);
Packit Service 99d1c0
    xdr_osa_princ_ent_rec(&xdrs, &adb);
Packit Service 99d1c0
    xdr_destroy(&xdrs);
Packit Service 99d1c0
Packit Service 99d1c0
    return 0;
Packit Service 99d1c0
}
Packit Service 99d1c0
Packit Service 99d1c0
/* draft-behera-ldap-password-policy-10.txt 7.1 */
Packit Service 99d1c0
static krb5_boolean
Packit Service 99d1c0
locked_check_p(krb5_context context, krb5_timestamp stamp, krb5_kvno max_fail,
Packit Service 99d1c0
               krb5_timestamp lockout_duration, krb5_db_entry *entry)
Packit Service 99d1c0
{
Packit Service 99d1c0
    krb5_timestamp unlock_time;
Packit Service 99d1c0
Packit Service 99d1c0
    /* If the entry was unlocked since the last failure, it's not locked. */
Packit Service 99d1c0
    if (krb5_dbe_lookup_last_admin_unlock(context, entry, &unlock_time) == 0 &&
Packit Service 99d1c0
        !ts_after(entry->last_failed, unlock_time))
Packit Service 99d1c0
        return FALSE;
Packit Service 99d1c0
Packit Service 99d1c0
    if (max_fail == 0 || entry->fail_auth_count < max_fail)
Packit Service 99d1c0
        return FALSE;
Packit Service 99d1c0
Packit Service 99d1c0
    if (lockout_duration == 0)
Packit Service 99d1c0
        return TRUE; /* principal permanently locked */
Packit Service 99d1c0
Packit Service 99d1c0
    return ts_after(ts_incr(entry->last_failed, lockout_duration), stamp);
Packit Service 99d1c0
}
Packit Service 99d1c0
Packit Service 99d1c0
krb5_error_code
Packit Service 99d1c0
klmdb_lockout_check_policy(krb5_context context, krb5_db_entry *entry,
Packit Service 99d1c0
                           krb5_timestamp stamp)
Packit Service 99d1c0
{
Packit Service 99d1c0
    krb5_error_code code;
Packit Service 99d1c0
    krb5_kvno max_fail = 0;
Packit Service 99d1c0
    krb5_deltat failcnt_interval = 0;
Packit Service 99d1c0
    krb5_deltat lockout_duration = 0;
Packit Service 99d1c0
Packit Service 99d1c0
    code = lookup_lockout_policy(context, entry, &max_fail, &failcnt_interval,
Packit Service 99d1c0
                                 &lockout_duration);
Packit Service 99d1c0
    if (code != 0)
Packit Service 99d1c0
        return code;
Packit Service 99d1c0
Packit Service 99d1c0
    if (locked_check_p(context, stamp, max_fail, lockout_duration, entry))
Packit Service 99d1c0
        return KRB5KDC_ERR_CLIENT_REVOKED;
Packit Service 99d1c0
Packit Service 99d1c0
    return 0;
Packit Service 99d1c0
}
Packit Service 99d1c0
Packit Service 99d1c0
krb5_error_code
Packit Service 99d1c0
klmdb_lockout_audit(krb5_context context, krb5_db_entry *entry,
Packit Service 99d1c0
                    krb5_timestamp stamp, krb5_error_code status,
Packit Service 99d1c0
                    krb5_boolean disable_last_success,
Packit Service 99d1c0
                    krb5_boolean disable_lockout)
Packit Service 99d1c0
{
Packit Service 99d1c0
    krb5_error_code ret;
Packit Service 99d1c0
    krb5_kvno max_fail = 0;
Packit Service 99d1c0
    krb5_deltat failcnt_interval = 0, lockout_duration = 0;
Packit Service 99d1c0
    krb5_boolean zero_fail_count = FALSE;
Packit Service 99d1c0
    krb5_boolean set_last_success = FALSE, set_last_failure = FALSE;
Packit Service 99d1c0
    krb5_timestamp unlock_time;
Packit Service 99d1c0
Packit Service 99d1c0
    if (status != 0 && status != KRB5KDC_ERR_PREAUTH_FAILED &&
Packit Service 99d1c0
        status != KRB5KRB_AP_ERR_BAD_INTEGRITY)
Packit Service 99d1c0
        return 0;
Packit Service 99d1c0
Packit Service 99d1c0
    if (!disable_lockout) {
Packit Service 99d1c0
        ret = lookup_lockout_policy(context, entry, &max_fail,
Packit Service 99d1c0
                                    &failcnt_interval, &lockout_duration);
Packit Service 99d1c0
        if (ret)
Packit Service 99d1c0
            return ret;
Packit Service 99d1c0
    }
Packit Service 99d1c0
Packit Service 99d1c0
    /*
Packit Service 99d1c0
     * Don't continue to modify the DB for an already locked account.
Packit Service 99d1c0
     * (In most cases, status will be KRB5KDC_ERR_CLIENT_REVOKED, and
Packit Service 99d1c0
     * this check is unneeded, but in rare cases, we can fail with an
Packit Service 99d1c0
     * integrity error or preauth failure before a policy check.)
Packit Service 99d1c0
     */
Packit Service 99d1c0
    if (locked_check_p(context, stamp, max_fail, lockout_duration, entry))
Packit Service 99d1c0
        return 0;
Packit Service 99d1c0
Packit Service 99d1c0
    /* Only mark the authentication as successful if the entry
Packit Service 99d1c0
     * required preauthentication; otherwise we have no idea. */
Packit Service 99d1c0
    if (status == 0 && (entry->attributes & KRB5_KDB_REQUIRES_PRE_AUTH)) {
Packit Service 99d1c0
        if (!disable_lockout && entry->fail_auth_count != 0)
Packit Service 99d1c0
            zero_fail_count = TRUE;
Packit Service 99d1c0
        if (!disable_last_success)
Packit Service 99d1c0
            set_last_success = TRUE;
Packit Service 99d1c0
    } else if (status != 0 && !disable_lockout) {
Packit Service 99d1c0
        /* Reset the failure counter after an administrative unlock. */
Packit Service 99d1c0
        if (krb5_dbe_lookup_last_admin_unlock(context, entry,
Packit Service 99d1c0
                                              &unlock_time) == 0 &&
Packit Service 99d1c0
            !ts_after(entry->last_failed, unlock_time))
Packit Service 99d1c0
            zero_fail_count = TRUE;
Packit Service 99d1c0
Packit Service 99d1c0
        /* Reset the failure counter after failcnt_interval. */
Packit Service 99d1c0
        if (failcnt_interval != 0 &&
Packit Service 99d1c0
            ts_after(stamp, ts_incr(entry->last_failed, failcnt_interval)))
Packit Service 99d1c0
            zero_fail_count = TRUE;
Packit Service 99d1c0
Packit Service 99d1c0
        set_last_failure = TRUE;
Packit Service 99d1c0
    }
Packit Service 99d1c0
Packit Service 99d1c0
    return klmdb_update_lockout(context, entry, stamp, zero_fail_count,
Packit Service 99d1c0
                                set_last_success, set_last_failure);
Packit Service 99d1c0
}