Path: blob/main/crypto/krb5/src/plugins/localauth/test/main.c
34890 views
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/* plugins/localauth/test/main.c - test modules for localauth interface */2/*3* Copyright (C) 2013 by the Massachusetts Institute of Technology.4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9*10* * Redistributions of source code must retain the above copyright11* notice, this list of conditions and the following disclaimer.12*13* * Redistributions in binary form must reproduce the above copyright14* notice, this list of conditions and the following disclaimer in15* the documentation and/or other materials provided with the16* distribution.17*18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS19* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT20* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS21* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE22* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,23* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES24* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR25* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)26* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,27* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)28* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED29* OF THE POSSIBILITY OF SUCH DAMAGE.30*/3132/* This file implements two testing localauth modules, each implementing33* clearly recognizable behavior for the localauth test script. */3435#include "k5-int.h"36#include <krb5/localauth_plugin.h>3738struct krb5_localauth_moddata_st {39int a;40int b;41};4243static krb5_error_code44init_test(krb5_context context, krb5_localauth_moddata *data_out)45{46krb5_localauth_moddata d;4748*data_out = NULL;49d = malloc(sizeof(*d));50if (d == NULL)51return ENOMEM;52d->a = 3;53d->b = 4;54*data_out = d;55return 0;56}5758static void59fini_test(krb5_context context, krb5_localauth_moddata data)60{61assert(data->a == 3);62assert(data->b == 4);63free(data);64}6566static krb5_error_code67an2ln_test(krb5_context context, krb5_localauth_moddata data, const char *type,68const char *residual, krb5_const_principal aname, char **lname_out)69{70krb5_error_code ret;71char *lname = NULL;7273*lname_out = NULL;74if (data != NULL) {75assert(data->a == 3);76assert(data->b == 4);77}78if (type == NULL) {79/* Map any three-component test/___/___ principal to its realm name. */80if (aname->length == 3 && data_eq_string(aname->data[0], "test")) {81lname = k5memdup0(aname->realm.data, aname->realm.length, &ret);82if (lname == NULL)83return ret;84}85} else if (strcmp(type, "TYPEA") == 0) {86/* Map any two-component principal to its second component. */87if (aname->length == 2) {88lname = k5memdup0(aname->data[1].data, aname->data[1].length,89&ret);90if (lname == NULL)91return ret;92}93} else {94assert(strcmp(type, "TYPEB") == 0);95/* Map to the residual string. */96lname = strdup(residual == NULL ? "(null)" : residual);97if (lname == NULL)98return ENOMEM;99}100if (lname == NULL)101return KRB5_LNAME_NOTRANS;102*lname_out = lname;103return 0;104}105106static krb5_error_code107userok_test(krb5_context context, krb5_localauth_moddata data,108krb5_const_principal aname, const char *lname)109{110if (data != NULL) {111assert(data->a == 3);112assert(data->b == 4);113}114115/* Return success if the number of components in the principal is equal to116* the length of the local name. */117if ((size_t)aname->length == strlen(lname))118return 0;119120/* Pass control down if the first component is "pass". */121if (aname->length >= 1 && data_eq_string(aname->data[0], "pass"))122return KRB5_PLUGIN_NO_HANDLE;123124/* Otherwise reject. */125return EPERM;126}127128static void129freestr(krb5_context context, krb5_localauth_moddata data, char *str)130{131free(str);132}133134krb5_error_code135localauth_test1_initvt(krb5_context context, int maj_ver, int min_ver,136krb5_plugin_vtable vtable);137krb5_error_code138localauth_test2_initvt(krb5_context context, int maj_ver, int min_ver,139krb5_plugin_vtable vtable);140141krb5_error_code142localauth_test1_initvt(krb5_context context, int maj_ver, int min_ver,143krb5_plugin_vtable vtable)144{145krb5_localauth_vtable vt = (krb5_localauth_vtable)vtable;146147vt->init = init_test;148vt->fini = fini_test;149vt->name = "test1";150vt->an2ln = an2ln_test;151vt->userok = userok_test;152vt->free_string = freestr;153return 0;154}155156krb5_error_code157localauth_test2_initvt(krb5_context context, int maj_ver, int min_ver,158krb5_plugin_vtable vtable)159{160krb5_localauth_vtable vt = (krb5_localauth_vtable)vtable;161static const char *types[] = { "TYPEA", "TYPEB", NULL };162163vt->name = "test2";164vt->an2ln_types = types;165vt->an2ln = an2ln_test;166vt->free_string = freestr;167return 0;168}169170171