1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* tests/gcred.c - Test harness for referrals */
3 /*
4 * Copyright (C) 2012 by the Massachusetts Institute of Technology.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30 * OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * This program is intended to be run from a python script as:
35 *
36 * gcred [-f] [-t] nametype princname
37 *
38 * where nametype is one of "unknown", "principal", "srv-inst", and "srv-hst",
39 * and princname is the name of the service principal. gcred acquires
40 * credentials for the specified server principal. On success, gcred displays
41 * the server principal name of the obtained credentials to stdout and exits
42 * with status 0. On failure, gcred displays the error message for the failed
43 * operation to stderr and exits with status 1.
44 *
45 * The -f and -t flags set the KRB5_GC_FORWARDABLE and KRB5_GC_NO_TRANSIT_CHECK
46 * options respectively.
47 */
48
49 #include "k5-int.h"
50
51 static krb5_context ctx;
52
53 static void
check(krb5_error_code code)54 check(krb5_error_code code)
55 {
56 const char *errmsg;
57
58 if (code) {
59 errmsg = krb5_get_error_message(ctx, code);
60 fprintf(stderr, "%s\n", errmsg);
61 krb5_free_error_message(ctx, errmsg);
62 exit(1);
63 }
64 }
65
66 int
main(int argc,char ** argv)67 main(int argc, char **argv)
68 {
69 krb5_principal client, server;
70 krb5_ccache ccache;
71 krb5_creds in_creds, *creds;
72 krb5_ticket *ticket;
73 krb5_flags options = 0;
74 char *name;
75 int c;
76
77 check(krb5_init_context(&ctx));
78
79 while ((c = getopt(argc, argv, "ft")) != -1) {
80 switch (c) {
81 case 'f':
82 options |= KRB5_GC_FORWARDABLE;
83 break;
84 case 't':
85 options |= KRB5_GC_NO_TRANSIT_CHECK;
86 break;
87 default:
88 abort();
89 }
90 }
91 argc -= optind;
92 argv += optind;
93 assert(argc == 2);
94 check(krb5_parse_name(ctx, argv[1], &server));
95 if (strcmp(argv[0], "unknown") == 0)
96 server->type = KRB5_NT_UNKNOWN;
97 else if (strcmp(argv[0], "principal") == 0)
98 server->type = KRB5_NT_PRINCIPAL;
99 else if (strcmp(argv[0], "srv-inst") == 0)
100 server->type = KRB5_NT_SRV_INST;
101 else if (strcmp(argv[0], "srv-hst") == 0)
102 server->type = KRB5_NT_SRV_HST;
103 else
104 abort();
105
106 check(krb5_cc_default(ctx, &ccache));
107 check(krb5_cc_get_principal(ctx, ccache, &client));
108 memset(&in_creds, 0, sizeof(in_creds));
109 in_creds.client = client;
110 in_creds.server = server;
111 check(krb5_get_credentials(ctx, options, ccache, &in_creds, &creds));
112 check(krb5_decode_ticket(&creds->ticket, &ticket));
113 check(krb5_unparse_name(ctx, ticket->server, &name));
114 printf("%s\n", name);
115
116 krb5_free_ticket(ctx, ticket);
117 krb5_free_unparsed_name(ctx, name);
118 krb5_free_creds(ctx, creds);
119 krb5_free_principal(ctx, client);
120 krb5_free_principal(ctx, server);
121 krb5_cc_close(ctx, ccache);
122 krb5_free_context(ctx);
123 return 0;
124 }
125