Blame src/tests/gssapi/t_lifetime.c

Packit fd8b60
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
Packit fd8b60
/* tests/gssapi/t_lifetime.c - display cred and context lifetimes */
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
#include <stdio.h>
Packit fd8b60
#include <stdlib.h>
Packit fd8b60
#include <assert.h>
Packit fd8b60
#include "common.h"
Packit fd8b60
Packit fd8b60
/*
Packit fd8b60
 * Using the default credential, exercise the GSS functions which accept or
Packit fd8b60
 * produce lifetimes.  Display the following results, one per line, as ASCII
Packit fd8b60
 * integers or the string "indefinite":
Packit fd8b60
 *
Packit fd8b60
 *   initiator cred lifetime according to gss_acquire_cred()
Packit fd8b60
 *   initiator cred lifetime according to gss_inquire_cred()
Packit fd8b60
 *   acceptor cred lifetime according to gss_acquire_cred()
Packit fd8b60
 *   acceptor cred lifetime according to gss_inquire_cred()
Packit fd8b60
 *   initiator context lifetime according to gss_init_sec_context()
Packit fd8b60
 *   initiator context lifetime according to gss_inquire_context()
Packit fd8b60
 *   initiator context lifetime according to gss_context_time()
Packit fd8b60
 *   acceptor context lifetime according to gss_init_sec_context()
Packit fd8b60
 *   acceptor context lifetime according to gss_inquire_context()
Packit fd8b60
 *   acceptor context lifetime according to gss_context_time()
Packit fd8b60
 */
Packit fd8b60
Packit fd8b60
static void
Packit fd8b60
display_time(OM_uint32 tval)
Packit fd8b60
{
Packit fd8b60
    if (tval == GSS_C_INDEFINITE)
Packit fd8b60
        puts("indefinite");
Packit fd8b60
    else
Packit fd8b60
        printf("%u\n", (unsigned int)tval);
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_cred_id_t icred, acred;
Packit fd8b60
    gss_name_t tname;
Packit fd8b60
    gss_ctx_id_t ictx = GSS_C_NO_CONTEXT, actx = GSS_C_NO_CONTEXT;
Packit fd8b60
    gss_buffer_desc itok = GSS_C_EMPTY_BUFFER, atok = GSS_C_EMPTY_BUFFER;
Packit fd8b60
    OM_uint32 time_req = GSS_C_INDEFINITE, time_rec;
Packit fd8b60
Packit fd8b60
    if (argc < 2 || argc > 3) {
Packit fd8b60
        fprintf(stderr, "Usage: %s targetname [time_req]\n", argv[0]);
Packit fd8b60
        return 1;
Packit fd8b60
    }
Packit fd8b60
    tname = import_name(argv[1]);
Packit fd8b60
    if (argc >= 3)
Packit fd8b60
        time_req = atoll(argv[2]);
Packit fd8b60
Packit fd8b60
    /* Get initiator cred and display its lifetime according to
Packit fd8b60
     * gss_acquire_cred and gss_inquire_cred. */
Packit fd8b60
    major = gss_acquire_cred(&minor, GSS_C_NO_NAME, time_req, &mechset_krb5,
Packit fd8b60
                             GSS_C_INITIATE, &icred, NULL, &time_rec);
Packit fd8b60
    check_gsserr("gss_acquire_cred(initiate)", major, minor);
Packit fd8b60
    display_time(time_rec);
Packit fd8b60
    major = gss_inquire_cred(&minor, icred, NULL, &time_rec, NULL, NULL);
Packit fd8b60
    check_gsserr("gss_inquire_cred(initiate)", major, minor);
Packit fd8b60
    display_time(time_rec);
Packit fd8b60
Packit fd8b60
    /* Get acceptor cred and display its lifetime according to gss_acquire_cred
Packit fd8b60
     * and gss_inquire_cred. */
Packit fd8b60
    major = gss_acquire_cred(&minor, GSS_C_NO_NAME, time_req, &mechset_krb5,
Packit fd8b60
                             GSS_C_ACCEPT, &acred, NULL, &time_rec);
Packit fd8b60
    check_gsserr("gss_acquire_cred(accept)", major, minor);
Packit fd8b60
    display_time(time_rec);
Packit fd8b60
    major = gss_inquire_cred(&minor, acred, NULL, &time_rec, NULL, NULL);
Packit fd8b60
    check_gsserr("gss_inquire_cred(accept)", major, minor);
Packit fd8b60
    display_time(time_rec);
Packit fd8b60
Packit fd8b60
    /* Make an initiator context and display its lifetime according to
Packit fd8b60
     * gss_init_sec_context, gss_inquire_context, and gss_context_time. */
Packit fd8b60
    major = gss_init_sec_context(&minor, icred, &ictx, tname, &mech_krb5, 0,
Packit fd8b60
                                 time_req, GSS_C_NO_CHANNEL_BINDINGS, &atok,
Packit fd8b60
                                 NULL, &itok, NULL, &time_rec);
Packit fd8b60
    check_gsserr("gss_init_sec_context", major, minor);
Packit fd8b60
    assert(major == GSS_S_COMPLETE);
Packit fd8b60
    display_time(time_rec);
Packit fd8b60
    major = gss_inquire_context(&minor, ictx, NULL, NULL, &time_rec, NULL,
Packit fd8b60
                                NULL, NULL, NULL);
Packit fd8b60
    check_gsserr("gss_inquire_context(initiate)", major, minor);
Packit fd8b60
    display_time(time_rec);
Packit fd8b60
    major = gss_context_time(&minor, ictx, &time_rec);
Packit fd8b60
    check_gsserr("gss_context_time(initiate)", major, minor);
Packit fd8b60
    display_time(time_rec);
Packit fd8b60
Packit fd8b60
    major = gss_accept_sec_context(&minor, &actx, acred, &itok,
Packit fd8b60
                                   GSS_C_NO_CHANNEL_BINDINGS, NULL,
Packit fd8b60
                                   NULL, &atok, NULL, &time_rec, NULL);
Packit fd8b60
    check_gsserr("gss_accept_sec_context", major, minor);
Packit fd8b60
    assert(major == GSS_S_COMPLETE);
Packit fd8b60
    display_time(time_rec);
Packit fd8b60
    major = gss_inquire_context(&minor, actx, NULL, NULL, &time_rec, NULL,
Packit fd8b60
                                NULL, NULL, NULL);
Packit fd8b60
    check_gsserr("gss_inquire_context(accept)", major, minor);
Packit fd8b60
    display_time(time_rec);
Packit fd8b60
    major = gss_context_time(&minor, actx, &time_rec);
Packit fd8b60
    check_gsserr("gss_context_time(accept)", major, minor);
Packit fd8b60
    display_time(time_rec);
Packit fd8b60
Packit fd8b60
    (void)gss_release_buffer(&minor, &itok);
Packit fd8b60
    (void)gss_release_buffer(&minor, &atok);
Packit fd8b60
    (void)gss_release_name(&minor, &tname);
Packit fd8b60
    (void)gss_release_cred(&minor, &icred);
Packit fd8b60
    (void)gss_release_cred(&minor, &acred);
Packit fd8b60
    (void)gss_delete_sec_context(&minor, &ictx, NULL);
Packit fd8b60
    (void)gss_delete_sec_context(&minor, &actx, NULL);
Packit fd8b60
    return 0;
Packit fd8b60
}