1/*
2 * Copyright (c) 2008 - 2011 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Portions Copyright (c) 2011 Apple Inc. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * 3. Neither the name of the Institute nor the names of its contributors
20 *    may be used to endorse or promote products derived from this software
21 *    without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36#include "krb5_locl.h"
37#include <err.h>
38#include <getarg.h>
39
40static char *ccache_string = NULL;
41static int verbose_flag = 0;
42static int version_flag = 0;
43static int help_flag	= 0;
44
45static struct getargs args[] = {
46    { "cache",			'c', arg_string, &ccache_string,
47	"credentials cache to use", "cache" },
48    {"verbose",	0,	arg_flag,	&verbose_flag,
49	"verbose debugging", NULL },
50    {"version",	0,	arg_flag,	&version_flag,
51	"print version", NULL },
52    {"help",	0,	arg_flag,	&help_flag,
53	NULL, NULL }
54};
55
56static void
57usage (int ret)
58{
59    arg_printusage (args,
60		    sizeof(args)/sizeof(*args),
61		    NULL,
62		    "principal ...");
63    exit (ret);
64}
65
66int
67main(int argc, char **argv)
68{
69    krb5_context context;
70    krb5_error_code ret;
71    krb5_ccache id;
72    int i, optidx = 0;
73
74    setprogname (argv[0]);
75
76    if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx))
77	usage(1);
78
79    if (help_flag)
80	usage (0);
81
82    if(version_flag){
83	print_version(NULL);
84	exit(0);
85    }
86
87    argc -= optidx;
88    argv += optidx;
89
90    if (argc < 1)
91	usage(1);
92
93    ret = krb5_init_context(&context);
94    if (ret)
95	errx(1, "krb5_init_context");
96
97    if (verbose_flag) {
98	krb5_log_facility *log;
99	krb5_initlog(context, "libkrb5", &log);
100	krb5_addlog_dest(context, log, "0-/STDERR");
101	krb5_set_debug_dest(context, log);
102    }
103
104    if (ccache_string) {
105	ret = krb5_cc_resolve(context, ccache_string, &id);
106    } else {
107	ret = krb5_cc_default(context, &id);
108    }
109    if (ret)
110	krb5_err(context, 1, ret, "krb5_cc_%s",
111		 ccache_string ? "resolve" : "default");
112
113    for (i = 0; i < argc; i++) {
114	krb5_tkt_creds_context ctx = NULL;
115	krb5_creds incred, *outcred = NULL;
116	krb5_data indata, outdata;
117
118	memset(&incred, 0, sizeof(incred));
119	krb5_data_zero(&indata);
120	krb5_data_zero(&outdata);
121
122	/*
123	 *
124	 */
125
126	printf("%s\n", argv[i]);
127
128	ret = krb5_cc_get_principal(context, id, &incred.client);
129	if (ret)
130	    krb5_err(context, 1, ret, "krb5_cc_get_principal");
131
132	ret = krb5_parse_name(context, argv[i], &incred.server);
133	if (ret)
134	    krb5_err(context, 1, ret, "krb5_parse_name");
135
136	ret = krb5_tkt_creds_init(context, id, &incred, 0, &ctx);
137	if (ret)
138	    krb5_err(context, 1, ret, "krb5_tkt_creds_init");
139
140	krb5_free_principal(context, incred.server);
141	krb5_free_principal(context, incred.client);
142
143	/*
144	 *
145	 */
146
147	while (1) {
148	    krb5_realm realm = NULL;
149	    unsigned int flags = 0;
150
151	    ret = krb5_tkt_creds_step(context, ctx, &indata, &outdata,
152				      &realm, &flags);
153	    krb5_data_free(&indata);
154	    if (ret)
155		krb5_err(context, 1, ret, "krb5_tkt_step_step");
156
157	    if ((flags & KRB5_TKT_STATE_CONTINUE) == 0) {
158		heim_assert(outdata.length == 0, "data to send to KDC");
159		break;
160	    }
161	    heim_assert(outdata.length != 0, "no data to send to KDC!");
162
163	    ret = krb5_sendto_context(context, NULL,
164				      &outdata, realm, &indata);
165	    if (ret)
166		krb5_err(context, 1, ret, "krb5_sendto_context");
167	    krb5_data_free(&outdata);
168	}
169
170	ret = krb5_tkt_creds_get_creds(context, ctx, &outcred);
171	if (ret)
172	    krb5_err(context, 1, ret, "krb5_tkt_creds_get_creds");
173
174	krb5_tkt_creds_free(context, ctx);
175    }
176
177    krb5_free_context(context);
178
179    return 0;
180}
181