Blame src/plugins/kadm5_auth/test/main.c

Packit fd8b60
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
Packit fd8b60
/* plugins/kadm5_auth/test/main.c - test modules for kadm5_auth interface */
Packit fd8b60
/*
Packit fd8b60
 * Copyright (C) 2017 by the Massachusetts Institute of Technology.
Packit fd8b60
 * All rights reserved.
Packit fd8b60
 *
Packit fd8b60
 * Redistribution and use in source and binary forms, with or without
Packit fd8b60
 * modification, are permitted provided that the following conditions
Packit fd8b60
 * are met:
Packit fd8b60
 *
Packit fd8b60
 * * Redistributions of source code must retain the above copyright
Packit fd8b60
 *   notice, this list of conditions and the following disclaimer.
Packit fd8b60
 *
Packit fd8b60
 * * Redistributions in binary form must reproduce the above copyright
Packit fd8b60
 *   notice, this list of conditions and the following disclaimer in
Packit fd8b60
 *   the documentation and/or other materials provided with the
Packit fd8b60
 *   distribution.
Packit fd8b60
 *
Packit fd8b60
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit fd8b60
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit fd8b60
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
Packit fd8b60
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
Packit fd8b60
 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
Packit fd8b60
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
Packit fd8b60
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
Packit fd8b60
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
Packit fd8b60
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
Packit fd8b60
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
Packit fd8b60
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
Packit fd8b60
 * OF THE POSSIBILITY OF SUCH DAMAGE.
Packit fd8b60
 */
Packit fd8b60
Packit fd8b60
/*
Packit fd8b60
 * This file implements two testing kadm5_auth modules, the welcomer and the
Packit fd8b60
 * bouncer.  The welcomer implements permissive behavior, while the bouncer
Packit fd8b60
 * implements restrictive behavior.
Packit fd8b60
 *
Packit fd8b60
 * Module data objects and restrictions are adequately tested by the acl
Packit fd8b60
 * module, so we do not test them here.  Focus instead on the ability to
Packit fd8b60
 * examine principal and policy objects and to perform DB operations.
Packit fd8b60
 */
Packit fd8b60
Packit fd8b60
#include "k5-int.h"
Packit fd8b60
#include <kadm5/admin.h>
Packit fd8b60
#include <krb5/kadm5_auth_plugin.h>
Packit fd8b60
Packit fd8b60
krb5_error_code
Packit fd8b60
kadm5_auth_welcomer_initvt(krb5_context context, int maj_ver, int min_ver,
Packit fd8b60
                           krb5_plugin_vtable vtable);
Packit fd8b60
krb5_error_code
Packit fd8b60
kadm5_auth_bouncer_initvt(krb5_context context, int maj_ver, int min_ver,
Packit fd8b60
                          krb5_plugin_vtable vtable);
Packit fd8b60
Packit fd8b60
/* The welcomer authorizes all getprinc operations, since kadmin uses them as a
Packit fd8b60
 * precursor to modprinc. */
Packit fd8b60
static krb5_error_code
Packit fd8b60
welcomer_getprinc(krb5_context context, kadm5_auth_moddata data,
Packit fd8b60
                  krb5_const_principal client, krb5_const_principal target)
Packit fd8b60
{
Packit fd8b60
    return 0;
Packit fd8b60
}
Packit fd8b60
Packit fd8b60
/* The welcomer authorizes addprinc operations which set a policy "VIP". */
Packit fd8b60
static krb5_error_code
Packit fd8b60
welcomer_addprinc(krb5_context context, kadm5_auth_moddata data,
Packit fd8b60
                  krb5_const_principal client, krb5_const_principal target,
Packit fd8b60
                  const struct _kadm5_principal_ent_t *ent, long mask,
Packit fd8b60
                  struct kadm5_auth_restrictions **rs_out)
Packit fd8b60
{
Packit fd8b60
    if ((mask & KADM5_POLICY) && strcmp(ent->policy, "VIP") == 0)
Packit fd8b60
        return 0;
Packit fd8b60
    return KRB5_PLUGIN_NO_HANDLE;
Packit fd8b60
}
Packit fd8b60
Packit fd8b60
/* The bouncer denies addprinc operations which include a maximum lifetime. */
Packit fd8b60
static krb5_error_code
Packit fd8b60
bouncer_addprinc(krb5_context context, kadm5_auth_moddata data,
Packit fd8b60
                 krb5_const_principal client, krb5_const_principal target,
Packit fd8b60
                 const struct _kadm5_principal_ent_t *ent, long mask,
Packit fd8b60
                 struct kadm5_auth_restrictions **rs_out)
Packit fd8b60
{
Packit fd8b60
    return (mask & KADM5_MAX_LIFE) ? EPERM : KRB5_PLUGIN_NO_HANDLE;
Packit fd8b60
}
Packit fd8b60
Packit fd8b60
/* The welcomer authorizes modprinc operations which only set maxrenewlife. */
Packit fd8b60
static krb5_error_code
Packit fd8b60
welcomer_modprinc(krb5_context context, kadm5_auth_moddata data,
Packit fd8b60
                  krb5_const_principal client, krb5_const_principal target,
Packit fd8b60
                  const struct _kadm5_principal_ent_t *ent, long mask,
Packit fd8b60
                  struct kadm5_auth_restrictions **rs_out)
Packit fd8b60
{
Packit fd8b60
    return (mask == KADM5_MAX_RLIFE) ? 0 : KRB5_PLUGIN_NO_HANDLE;
Packit fd8b60
}
Packit fd8b60
Packit fd8b60
/* The bouncer denies modprinc operations if the target principal has an even
Packit fd8b60
 * number of components. */
Packit fd8b60
static krb5_error_code
Packit fd8b60
bouncer_modprinc(krb5_context context, kadm5_auth_moddata data,
Packit fd8b60
                 krb5_const_principal client, krb5_const_principal target,
Packit fd8b60
                 const struct _kadm5_principal_ent_t *ent, long mask,
Packit fd8b60
                 struct kadm5_auth_restrictions **rs_out)
Packit fd8b60
{
Packit fd8b60
    return (target->length % 2 == 0) ? EPERM : KRB5_PLUGIN_NO_HANDLE;
Packit fd8b60
}
Packit fd8b60
Packit fd8b60
/* The welcomer authorizes setstr operations for the attribute "note". */
Packit fd8b60
static krb5_error_code
Packit fd8b60
welcomer_setstr(krb5_context context, kadm5_auth_moddata data,
Packit fd8b60
                krb5_const_principal client, krb5_const_principal target,
Packit fd8b60
                const char *key, const char *value)
Packit fd8b60
{
Packit fd8b60
    return (strcmp(key, "note") == 0) ? 0 : KRB5_PLUGIN_NO_HANDLE;
Packit fd8b60
}
Packit fd8b60
Packit fd8b60
/* The bouncer denies setstr operations if the value is more than 10 bytes. */
Packit fd8b60
static krb5_error_code
Packit fd8b60
bouncer_setstr(krb5_context context, kadm5_auth_moddata data,
Packit fd8b60
               krb5_const_principal client, krb5_const_principal target,
Packit fd8b60
               const char *key, const char *value)
Packit fd8b60
{
Packit fd8b60
    return (strlen(value) > 10) ? EPERM : KRB5_PLUGIN_NO_HANDLE;
Packit fd8b60
}
Packit fd8b60
Packit fd8b60
/* The welcomer authorizes delprinc operations if the target principal starts
Packit fd8b60
 * with "d". */
Packit fd8b60
static krb5_error_code
Packit fd8b60
welcomer_delprinc(krb5_context context, kadm5_auth_moddata data,
Packit fd8b60
                  krb5_const_principal client, krb5_const_principal target)
Packit fd8b60
{
Packit fd8b60
    if (target->length > 0 && target->data[0].length > 0 &&
Packit fd8b60
        *target->data[0].data == 'd')
Packit fd8b60
        return 0;
Packit fd8b60
    return KRB5_PLUGIN_NO_HANDLE;
Packit fd8b60
}
Packit fd8b60
Packit fd8b60
/* The bouncer denies delprinc operations if the target principal has the
Packit fd8b60
 * "nodelete" string attribute. */
Packit fd8b60
static krb5_error_code
Packit fd8b60
bouncer_delprinc(krb5_context context, kadm5_auth_moddata data,
Packit fd8b60
                 krb5_const_principal client, krb5_const_principal target)
Packit fd8b60
{
Packit fd8b60
    krb5_error_code ret;
Packit fd8b60
    krb5_db_entry *ent;
Packit fd8b60
    char *val = NULL;
Packit fd8b60
Packit fd8b60
    if (krb5_db_get_principal(context, target, 0, &ent) != 0)
Packit fd8b60
        return EPERM;
Packit fd8b60
    ret = krb5_dbe_get_string(context, ent, "nodelete", &val;;
Packit fd8b60
    krb5_db_free_principal(context, ent);
Packit fd8b60
    ret = (ret != 0 || val != NULL) ? EPERM : KRB5_PLUGIN_NO_HANDLE;
Packit fd8b60
    krb5_dbe_free_string(context, val);
Packit fd8b60
    return ret;
Packit fd8b60
}
Packit fd8b60
Packit fd8b60
/* The welcomer authorizes rename operations if the first components of the
Packit fd8b60
 * principals have the same length. */
Packit fd8b60
static krb5_error_code
Packit fd8b60
welcomer_renprinc(krb5_context context, kadm5_auth_moddata data,
Packit fd8b60
                  krb5_const_principal client, krb5_const_principal src,
Packit fd8b60
                  krb5_const_principal dest)
Packit fd8b60
{
Packit fd8b60
    if (src->length > 0 && dest->length > 0 &&
Packit fd8b60
        src->data[0].length == dest->data[0].length)
Packit fd8b60
        return 0;
Packit fd8b60
    return KRB5_PLUGIN_NO_HANDLE;
Packit fd8b60
}
Packit fd8b60
Packit fd8b60
/* The bouncer denies rename operations if the source principal starts with
Packit fd8b60
 * "a". */
Packit fd8b60
static krb5_error_code
Packit fd8b60
bouncer_renprinc(krb5_context context, kadm5_auth_moddata data,
Packit fd8b60
                 krb5_const_principal client, krb5_const_principal src,
Packit fd8b60
                 krb5_const_principal dest)
Packit fd8b60
{
Packit fd8b60
    if (src->length > 0 && src->data[0].length > 0 &&
Packit fd8b60
        *src->data[0].data == 'a')
Packit fd8b60
        return EPERM;
Packit fd8b60
    return KRB5_PLUGIN_NO_HANDLE;
Packit fd8b60
}
Packit fd8b60
Packit fd8b60
/* The welcomer authorizes addpol operations which set a minlength of 3. */
Packit fd8b60
static krb5_error_code
Packit fd8b60
welcomer_addpol(krb5_context context, kadm5_auth_moddata data,
Packit fd8b60
                krb5_const_principal client, const char *policy,
Packit fd8b60
                const struct _kadm5_policy_ent_t *ent, long mask)
Packit fd8b60
{
Packit fd8b60
    if ((mask & KADM5_PW_MIN_LENGTH) && ent->pw_min_length == 3)
Packit fd8b60
        return 0;
Packit fd8b60
    return KRB5_PLUGIN_NO_HANDLE;
Packit fd8b60
}
Packit fd8b60
Packit fd8b60
/* The bouncer denies addpol operations if the name is 3 bytes or less. */
Packit fd8b60
static krb5_error_code
Packit fd8b60
bouncer_addpol(krb5_context context, kadm5_auth_moddata data,
Packit fd8b60
               krb5_const_principal client, const char *policy,
Packit fd8b60
               const struct _kadm5_policy_ent_t *ent, long mask)
Packit fd8b60
{
Packit fd8b60
    return (strlen(policy) <= 3) ? EPERM : KRB5_PLUGIN_NO_HANDLE;
Packit fd8b60
}
Packit fd8b60
Packit fd8b60
/* The welcomer authorizes modpol operations which only change min_life. */
Packit fd8b60
static krb5_error_code
Packit fd8b60
welcomer_modpol(krb5_context context, kadm5_auth_moddata data,
Packit fd8b60
                krb5_const_principal client, const char *policy,
Packit fd8b60
                const struct _kadm5_policy_ent_t *ent, long mask)
Packit fd8b60
{
Packit fd8b60
    return (mask == KADM5_PW_MIN_LIFE) ? 0 : KRB5_PLUGIN_NO_HANDLE;
Packit fd8b60
}
Packit fd8b60
Packit fd8b60
/* The bouncer denies modpol operations which set pw_min_life above 10. */
Packit fd8b60
static krb5_error_code
Packit fd8b60
bouncer_modpol(krb5_context context, kadm5_auth_moddata data,
Packit fd8b60
               krb5_const_principal client, const char *policy,
Packit fd8b60
               const struct _kadm5_policy_ent_t *ent, long mask)
Packit fd8b60
{
Packit fd8b60
    if ((mask & KADM5_PW_MIN_LIFE) && ent->pw_min_life > 10)
Packit fd8b60
        return EPERM;
Packit fd8b60
    return KRB5_PLUGIN_NO_HANDLE;
Packit fd8b60
}
Packit fd8b60
Packit fd8b60
/* The welcomer authorizes getpol operations if the policy and client principal
Packit fd8b60
 * policy have the same length. */
Packit fd8b60
static krb5_error_code
Packit fd8b60
welcomer_getpol(krb5_context context, kadm5_auth_moddata data,
Packit fd8b60
                krb5_const_principal client, const char *policy,
Packit fd8b60
                const char *client_policy)
Packit fd8b60
{
Packit fd8b60
    if (client_policy != NULL && strlen(policy) == strlen(client_policy))
Packit fd8b60
        return 0;
Packit fd8b60
    return KRB5_PLUGIN_NO_HANDLE;
Packit fd8b60
}
Packit fd8b60
Packit fd8b60
/* The bouncer denies getpol operations if the policy name begins with 'x'. */
Packit fd8b60
static krb5_error_code
Packit fd8b60
bouncer_getpol(krb5_context context, kadm5_auth_moddata data,
Packit fd8b60
               krb5_const_principal client, const char *policy,
Packit fd8b60
               const char *client_policy)
Packit fd8b60
{
Packit fd8b60
    return (*policy == 'x') ? EPERM : KRB5_PLUGIN_NO_HANDLE;
Packit fd8b60
}
Packit fd8b60
Packit fd8b60
/* The welcomer counts end calls by incrementing the "ends" string attribute on
Packit fd8b60
 * the "opcount" principal, if it exists. */
Packit fd8b60
static void
Packit fd8b60
welcomer_end(krb5_context context, kadm5_auth_moddata data)
Packit fd8b60
{
Packit fd8b60
    krb5_principal princ = NULL;
Packit fd8b60
    krb5_db_entry *ent = NULL;
Packit fd8b60
    char *val = NULL, buf[10];
Packit fd8b60
Packit fd8b60
    if (krb5_parse_name(context, "opcount", &princ) != 0)
Packit fd8b60
        goto cleanup;
Packit fd8b60
    if (krb5_db_get_principal(context, princ, 0, &ent) != 0)
Packit fd8b60
        goto cleanup;
Packit fd8b60
    if (krb5_dbe_get_string(context, ent, "ends", &val) != 0 || val == NULL)
Packit fd8b60
        goto cleanup;
Packit fd8b60
    snprintf(buf, sizeof(buf), "%d", atoi(val) + 1);
Packit fd8b60
    if (krb5_dbe_set_string(context, ent, "ends", buf) != 0)
Packit fd8b60
        goto cleanup;
Packit fd8b60
    ent->mask = KADM5_TL_DATA;
Packit fd8b60
    krb5_db_put_principal(context, ent);
Packit fd8b60
Packit fd8b60
cleanup:
Packit fd8b60
    krb5_dbe_free_string(context, val);
Packit fd8b60
    krb5_db_free_principal(context, ent);
Packit fd8b60
    krb5_free_principal(context, princ);
Packit fd8b60
}
Packit fd8b60
Packit fd8b60
krb5_error_code
Packit fd8b60
kadm5_auth_welcomer_initvt(krb5_context context, int maj_ver, int min_ver,
Packit fd8b60
                           krb5_plugin_vtable vtable)
Packit fd8b60
{
Packit fd8b60
    kadm5_auth_vtable vt = (kadm5_auth_vtable)vtable;
Packit fd8b60
Packit fd8b60
    vt->name = "welcomer";
Packit fd8b60
    vt->addprinc = welcomer_addprinc;
Packit fd8b60
    vt->modprinc = welcomer_modprinc;
Packit fd8b60
    vt->setstr = welcomer_setstr;
Packit fd8b60
    vt->delprinc = welcomer_delprinc;
Packit fd8b60
    vt->renprinc = welcomer_renprinc;
Packit fd8b60
    vt->getprinc = welcomer_getprinc;
Packit fd8b60
    vt->addpol = welcomer_addpol;
Packit fd8b60
    vt->modpol = welcomer_modpol;
Packit fd8b60
    vt->getpol = welcomer_getpol;
Packit fd8b60
    vt->end = welcomer_end;
Packit fd8b60
    return 0;
Packit fd8b60
}
Packit fd8b60
Packit fd8b60
krb5_error_code
Packit fd8b60
kadm5_auth_bouncer_initvt(krb5_context context, int maj_ver, int min_ver,
Packit fd8b60
                          krb5_plugin_vtable vtable)
Packit fd8b60
{
Packit fd8b60
    kadm5_auth_vtable vt = (kadm5_auth_vtable)vtable;
Packit fd8b60
Packit fd8b60
    vt->name = "bouncer";
Packit fd8b60
    vt->addprinc = bouncer_addprinc;
Packit fd8b60
    vt->modprinc = bouncer_modprinc;
Packit fd8b60
    vt->setstr = bouncer_setstr;
Packit fd8b60
    vt->delprinc = bouncer_delprinc;
Packit fd8b60
    vt->renprinc = bouncer_renprinc;
Packit fd8b60
    vt->addpol = bouncer_addpol;
Packit fd8b60
    vt->modpol = bouncer_modpol;
Packit fd8b60
    vt->getpol = bouncer_getpol;
Packit fd8b60
    return 0;
Packit fd8b60
}