Blame src/tests/gssapi/t_credstore.c

Packit fd8b60
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
Packit fd8b60
/*
Packit fd8b60
 * Copyright 2011 Red Hat, Inc.
Packit fd8b60
 *
Packit fd8b60
 * Permission is hereby granted, free of charge, to any person
Packit fd8b60
 * obtaining a copy of this software and associated documentation files
Packit fd8b60
 * (the "Software"), to deal in the Software without restriction,
Packit fd8b60
 * including without limitation the rights to use, copy, modify, merge,
Packit fd8b60
 * publish, distribute, sublicense, and/or sell copies of the Software,
Packit fd8b60
 * and to permit persons to whom the Software is furnished to do so,
Packit fd8b60
 * subject to the following conditions:
Packit fd8b60
 *
Packit fd8b60
 * The above copyright notice and this permission notice shall be
Packit fd8b60
 * included in all copies or substantial portions of the Software.
Packit fd8b60
 *
Packit fd8b60
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
Packit fd8b60
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Packit fd8b60
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Packit fd8b60
 * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
Packit fd8b60
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
Packit fd8b60
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
Packit fd8b60
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
Packit fd8b60
 * SOFTWARE.
Packit fd8b60
 */
Packit fd8b60
Packit fd8b60
#include <stdio.h>
Packit fd8b60
#include <stdlib.h>
Packit fd8b60
#include <string.h>
Packit fd8b60
Packit fd8b60
#include "common.h"
Packit fd8b60
Packit fd8b60
static void
Packit fd8b60
usage(void)
Packit fd8b60
{
Packit fd8b60
    fprintf(stderr,
Packit fd8b60
            "Usage: t_credstore [-sabi] principal [{key value} ...]\n");
Packit fd8b60
    exit(1);
Packit fd8b60
}
Packit fd8b60
Packit fd8b60
int
Packit fd8b60
main(int argc, char *argv[])
Packit fd8b60
{
Packit fd8b60
    OM_uint32 minor, major;
Packit fd8b60
    gss_key_value_set_desc store;
Packit fd8b60
    gss_name_t name;
Packit fd8b60
    gss_cred_usage_t cred_usage = GSS_C_BOTH;
Packit fd8b60
    gss_OID_set mechs = GSS_C_NO_OID_SET;
Packit fd8b60
    gss_cred_id_t cred = GSS_C_NO_CREDENTIAL;
Packit fd8b60
    gss_ctx_id_t ictx = GSS_C_NO_CONTEXT, actx = GSS_C_NO_CONTEXT;
Packit fd8b60
    gss_buffer_desc itok, atok;
Packit fd8b60
    krb5_boolean store_creds = FALSE, replay = FALSE;
Packit fd8b60
    char opt;
Packit fd8b60
Packit fd8b60
    /* Parse options. */
Packit fd8b60
    for (argv++; *argv != NULL && **argv == '-'; argv++) {
Packit fd8b60
        opt = (*argv)[1];
Packit fd8b60
        if (opt == 's')
Packit fd8b60
            store_creds = TRUE;
Packit fd8b60
        else if (opt == 'r')
Packit fd8b60
            replay = TRUE;
Packit fd8b60
        else if (opt == 'a')
Packit fd8b60
            cred_usage = GSS_C_ACCEPT;
Packit fd8b60
        else if (opt == 'b')
Packit fd8b60
            cred_usage = GSS_C_BOTH;
Packit fd8b60
        else if (opt == 'i')
Packit fd8b60
            cred_usage = GSS_C_INITIATE;
Packit fd8b60
        else
Packit fd8b60
            usage();
Packit fd8b60
    }
Packit fd8b60
Packit fd8b60
    /* Get the principal name. */
Packit fd8b60
    if (*argv == NULL)
Packit fd8b60
        usage();
Packit fd8b60
    name = import_name(*argv++);
Packit fd8b60
Packit fd8b60
    /* Put any remaining arguments into the store. */
Packit fd8b60
    store.elements = calloc(argc, sizeof(struct gss_key_value_element_struct));
Packit fd8b60
    if (!store.elements)
Packit fd8b60
        errout("OOM");
Packit fd8b60
    store.count = 0;
Packit fd8b60
    while (*argv != NULL) {
Packit fd8b60
        if (*(argv + 1) == NULL)
Packit fd8b60
            usage();
Packit fd8b60
        store.elements[store.count].key = *argv;
Packit fd8b60
        store.elements[store.count].value = *(argv + 1);
Packit fd8b60
        store.count++;
Packit fd8b60
        argv += 2;
Packit fd8b60
    }
Packit fd8b60
Packit fd8b60
    if (store_creds) {
Packit fd8b60
        /* Acquire default creds and try to store them in the cred store. */
Packit fd8b60
        major = gss_acquire_cred(&minor, GSS_C_NO_NAME, 0, GSS_C_NO_OID_SET,
Packit fd8b60
                                 GSS_C_INITIATE, &cred, NULL, NULL);
Packit fd8b60
        check_gsserr("gss_acquire_cred", major, minor);
Packit fd8b60
Packit fd8b60
        major = gss_store_cred_into(&minor, cred, GSS_C_INITIATE,
Packit fd8b60
                                    GSS_C_NO_OID, 1, 0, &store, NULL, NULL);
Packit fd8b60
        check_gsserr("gss_store_cred_into", major, minor);
Packit fd8b60
Packit fd8b60
        gss_release_cred(&minor, &cred);
Packit fd8b60
    }
Packit fd8b60
Packit fd8b60
    /* Try to acquire creds from store. */
Packit fd8b60
    major = gss_acquire_cred_from(&minor, name, 0, mechs, cred_usage,
Packit fd8b60
                                  &store, &cred, NULL, NULL);
Packit fd8b60
    check_gsserr("gss_acquire_cred_from", major, minor);
Packit fd8b60
Packit fd8b60
    if (replay) {
Packit fd8b60
        /* Induce a replay using cred as the acceptor cred, to test the replay
Packit fd8b60
         * cache indicated by the store. */
Packit fd8b60
        major = gss_init_sec_context(&minor, GSS_C_NO_CREDENTIAL, &ictx, name,
Packit fd8b60
                                     &mech_krb5, 0, GSS_C_INDEFINITE,
Packit fd8b60
                                     GSS_C_NO_CHANNEL_BINDINGS,
Packit fd8b60
                                     GSS_C_NO_BUFFER, NULL, &itok, NULL, NULL);
Packit fd8b60
        check_gsserr("gss_init_sec_context", major, minor);
Packit fd8b60
        (void)gss_delete_sec_context(&minor, &ictx, NULL);
Packit fd8b60
Packit fd8b60
        major = gss_accept_sec_context(&minor, &actx, cred, &itok,
Packit fd8b60
                                       GSS_C_NO_CHANNEL_BINDINGS, NULL, NULL,
Packit fd8b60
                                       &atok, NULL, NULL, NULL);
Packit fd8b60
        check_gsserr("gss_accept_sec_context(1)", major, minor);
Packit fd8b60
        (void)gss_release_buffer(&minor, &atok);
Packit fd8b60
        (void)gss_delete_sec_context(&minor, &actx, NULL);
Packit fd8b60
Packit fd8b60
        major = gss_accept_sec_context(&minor, &actx, cred, &itok,
Packit fd8b60
                                       GSS_C_NO_CHANNEL_BINDINGS, NULL, NULL,
Packit fd8b60
                                       &atok, NULL, NULL, NULL);
Packit fd8b60
        check_gsserr("gss_accept_sec_context(2)", major, minor);
Packit fd8b60
        (void)gss_release_buffer(&minor, &itok);
Packit fd8b60
        (void)gss_release_buffer(&minor, &atok);
Packit fd8b60
        (void)gss_delete_sec_context(&minor, &actx, NULL);
Packit fd8b60
    }
Packit fd8b60
Packit fd8b60
    gss_release_name(&minor, &name);
Packit fd8b60
    gss_release_cred(&minor, &cred);
Packit fd8b60
    free(store.elements);
Packit fd8b60
    return 0;
Packit fd8b60
}