Path: blob/main/crypto/krb5/src/tests/gssapi/t_inq_cred.c
34914 views
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/* tests/gssapi/t_inq_cred.c - Test program for gss_inquire_cred behavior */2/*3* Copyright 2012 by the Massachusetts Institute of Technology.4* All Rights Reserved.5*6* Export of this software from the United States of America may7* require a specific license from the United States Government.8* It is the responsibility of any person or organization contemplating9* export to obtain such a license before exporting.10*11* WITHIN THAT CONSTRAINT, permission to use, copy, modify, and12* distribute this software and its documentation for any purpose and13* without fee is hereby granted, provided that the above copyright14* notice appear in all copies and that both that copyright notice and15* this permission notice appear in supporting documentation, and that16* the name of M.I.T. not be used in advertising or publicity pertaining17* to distribution of the software without specific, written prior18* permission. Furthermore if you modify this software you must label19* your software as modified software and not distribute it in such a20* fashion that it might be confused with the original M.I.T. software.21* M.I.T. makes no representations about the suitability of22* this software for any purpose. It is provided "as is" without express23* or implied warranty.24*/2526/*27* Test program for gss_inquire_cred, intended to be run from a Python test28* script. Acquires credentials, inquires them, and prints the resulting name29* and lifetime.30*31* Usage: ./t_inq_cred [-k|-s] [-a|-b|-i] [initiatorname]32*33* By default no mechanism is specified when acquiring credentials; -k34* indicates the krb5 mech and -s indicates SPNEGO. By default or with -i,35* initiator credentials are acquired; -a indicates acceptor credentials and -b36* indicates credentials of both types. The credential is acquired with no37* name by default; a krb5 principal name or host-based name (prefixed with38* "gss:") may be supplied as an argument.39*/4041#include <stdio.h>42#include <stdlib.h>43#include <string.h>4445#include "common.h"4647static void48usage(void)49{50fprintf(stderr,51"Usage: t_inq_cred [-k|-s] [-a|-b|-i] [princ|gss:service@host]\n");52exit(1);53}5455int56main(int argc, char *argv[])57{58OM_uint32 minor, major, lifetime;59gss_cred_usage_t cred_usage = GSS_C_INITIATE;60gss_OID_set mechs = GSS_C_NO_OID_SET;61gss_cred_id_t cred = GSS_C_NO_CREDENTIAL;62gss_name_t name = GSS_C_NO_NAME;63gss_buffer_desc buf;64const char *name_arg = NULL;65char opt;6667while (argc > 1 && argv[1][0] == '-') {68opt = argv[1][1];69argc--, argv++;70if (opt == 'a')71cred_usage = GSS_C_ACCEPT;72else if (opt == 'b')73cred_usage = GSS_C_BOTH;74else if (opt == 'i')75cred_usage = GSS_C_INITIATE;76else if (opt == 'k')77mechs = &mechset_krb5;78else if (opt == 's')79mechs = &mechset_spnego;80else81usage();82}83if (argc > 2)84usage();85if (argc > 1)86name_arg = argv[1];8788/* Import the name, if given. */89if (name_arg != NULL)90name = import_name(name_arg);9192/* Acquire a credential. */93major = gss_acquire_cred(&minor, name, GSS_C_INDEFINITE, mechs, cred_usage,94&cred, NULL, NULL);95check_gsserr("gss_acquire_cred", major, minor);9697/* Inquire about the credential. */98(void)gss_release_name(&minor, &name);99major = gss_inquire_cred(&minor, cred, &name, &lifetime, NULL, NULL);100check_gsserr("gss_inquire_cred", major, minor);101102/* Get a display form of the name. */103buf.value = NULL;104buf.length = 0;105major = gss_display_name(&minor, name, &buf, NULL);106check_gsserr("gss_display_name", major, minor);107108printf("name: %.*s\n", (int)buf.length, (char *)buf.value);109printf("lifetime: %d\n", (int)lifetime);110111(void)gss_release_cred(&minor, &cred);112(void)gss_release_name(&minor, &name);113(void)gss_release_buffer(&minor, &buf);114return 0;115}116117118