/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/* tests/gcred.c - Test harness for referrals */2/*3* Copyright (C) 2012 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/*33* This program is intended to be run from a python script as:34*35* gcred [-f] [-t] nametype princname36*37* where nametype is one of "unknown", "principal", "srv-inst", and "srv-hst",38* and princname is the name of the service principal. gcred acquires39* credentials for the specified server principal. On success, gcred displays40* the server principal name of the obtained credentials to stdout and exits41* with status 0. On failure, gcred displays the error message for the failed42* operation to stderr and exits with status 1.43*44* The -f and -t flags set the KRB5_GC_FORWARDABLE and KRB5_GC_NO_TRANSIT_CHECK45* options respectively.46*/4748#include "k5-int.h"4950static krb5_context ctx;5152static void53check(krb5_error_code code)54{55const char *errmsg;5657if (code) {58errmsg = krb5_get_error_message(ctx, code);59fprintf(stderr, "%s\n", errmsg);60krb5_free_error_message(ctx, errmsg);61exit(1);62}63}6465int66main(int argc, char **argv)67{68krb5_principal client, server;69krb5_ccache ccache;70krb5_creds in_creds, *creds;71krb5_ticket *ticket;72krb5_flags options = 0;73char *name;74int c;7576check(krb5_init_context(&ctx));7778while ((c = getopt(argc, argv, "ft")) != -1) {79switch (c) {80case 'f':81options |= KRB5_GC_FORWARDABLE;82break;83case 't':84options |= KRB5_GC_NO_TRANSIT_CHECK;85break;86default:87abort();88}89}90argc -= optind;91argv += optind;92assert(argc == 2);93check(krb5_parse_name(ctx, argv[1], &server));94if (strcmp(argv[0], "unknown") == 0)95server->type = KRB5_NT_UNKNOWN;96else if (strcmp(argv[0], "principal") == 0)97server->type = KRB5_NT_PRINCIPAL;98else if (strcmp(argv[0], "srv-inst") == 0)99server->type = KRB5_NT_SRV_INST;100else if (strcmp(argv[0], "srv-hst") == 0)101server->type = KRB5_NT_SRV_HST;102else103abort();104105check(krb5_cc_default(ctx, &ccache));106check(krb5_cc_get_principal(ctx, ccache, &client));107memset(&in_creds, 0, sizeof(in_creds));108in_creds.client = client;109in_creds.server = server;110check(krb5_get_credentials(ctx, options, ccache, &in_creds, &creds));111check(krb5_decode_ticket(&creds->ticket, &ticket));112check(krb5_unparse_name(ctx, ticket->server, &name));113printf("%s\n", name);114115krb5_free_ticket(ctx, ticket);116krb5_free_unparsed_name(ctx, name);117krb5_free_creds(ctx, creds);118krb5_free_principal(ctx, client);119krb5_free_principal(ctx, server);120krb5_cc_close(ctx, ccache);121krb5_free_context(ctx);122return 0;123}124125126