make bind principal a common function
[heimdal.git] / kuser / kgetcred.c
blob9670b5796f780839fa7255d1abe15c13d8121180
1 /*
2 * Copyright (c) 1997 - 2008 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #include "kuser_locl.h"
36 static char *cache_str;
37 static char *out_cache_str;
38 static char *delegation_cred_str;
39 static char *etype_str;
40 static int transit_flag = 1;
41 static int forwardable_flag;
42 static int canonicalize_flag;
43 static char *impersonate_str;
44 static char *nametype_str;
45 static int version_flag;
46 static int help_flag;
48 struct getargs args[] = {
49 { "cache", 'c', arg_string, &cache_str,
50 NP_("credential cache to use", ""), "cache"},
51 { "out-cache", 0, arg_string, &out_cache_str,
52 NP_("credential cache to store credential in", ""), "cache"},
53 { "delegation-credential-cache",0,arg_string, &delegation_cred_str,
54 NP_("where to find the ticket use for delegation", ""), "cache"},
55 { "canonicalize", 0, arg_flag, &canonicalize_flag,
56 NP_("canonicalize the principal", ""), NULL },
57 { "forwardable", 0, arg_flag, &forwardable_flag,
58 NP_("forwardable ticket requested", ""), NULL},
59 { "transit-check", 0, arg_negative_flag, &transit_flag, NULL, NULL },
60 { "enctype", 'e', arg_string, &etype_str,
61 NP_("encryption type to use", ""), "enctype"},
62 { "impersonate", 0, arg_string, &impersonate_str,
63 NP_("client to impersonate", ""), "principal"},
64 { "name-type", 0, arg_string, &nametype_str, NULL, NULL },
65 { "version", 0, arg_flag, &version_flag, NULL, NULL },
66 { "help", 0, arg_flag, &help_flag, NULL, NULL }
69 static void
70 usage (int ret)
72 arg_printusage (args,
73 sizeof(args)/sizeof(*args),
74 NULL,
75 "service");
76 exit (ret);
79 int
80 main(int argc, char **argv)
82 krb5_error_code ret;
83 krb5_context context;
84 krb5_ccache cache;
85 krb5_creds *out;
86 int optidx = 0;
87 krb5_get_creds_opt opt;
88 krb5_principal server = NULL;
89 krb5_principal impersonate = NULL;
91 setprogname (argv[0]);
93 ret = krb5_init_context (&context);
94 if (ret)
95 errx(1, "krb5_init_context failed: %d", ret);
97 if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx))
98 usage(1);
100 if (help_flag)
101 usage (0);
103 if(version_flag) {
104 print_version(NULL);
105 exit(0);
108 argc -= optidx;
109 argv += optidx;
111 if(cache_str) {
112 ret = krb5_cc_resolve(context, cache_str, &cache);
113 if (ret)
114 krb5_err (context, 1, ret, "%s", cache_str);
115 } else {
116 ret = krb5_cc_default (context, &cache);
117 if (ret)
118 krb5_err (context, 1, ret, "krb5_cc_resolve");
121 ret = krb5_get_creds_opt_alloc(context, &opt);
122 if (ret)
123 krb5_err (context, 1, ret, "krb5_get_creds_opt_alloc");
125 if (etype_str) {
126 krb5_enctype enctype;
128 ret = krb5_string_to_enctype(context, etype_str, &enctype);
129 if (ret)
130 krb5_errx (context, 1, N_("unrecognized enctype: %s", ""),
131 etype_str);
132 krb5_get_creds_opt_set_enctype(context, opt, enctype);
135 if (impersonate_str) {
136 ret = krb5_parse_name(context, impersonate_str, &impersonate);
137 if (ret)
138 krb5_err (context, 1, ret, "krb5_parse_name %s", impersonate_str);
139 krb5_get_creds_opt_set_impersonate(context, opt, impersonate);
140 krb5_get_creds_opt_add_options(context, opt, KRB5_GC_NO_STORE);
143 if (out_cache_str)
144 krb5_get_creds_opt_add_options(context, opt, KRB5_GC_NO_STORE);
146 if (forwardable_flag)
147 krb5_get_creds_opt_add_options(context, opt, KRB5_GC_FORWARDABLE);
148 if (!transit_flag)
149 krb5_get_creds_opt_add_options(context, opt, KRB5_GC_NO_TRANSIT_CHECK);
150 if (canonicalize_flag)
151 krb5_get_creds_opt_add_options(context, opt, KRB5_GC_CANONICALIZE);
153 if (delegation_cred_str) {
154 krb5_ccache id;
155 krb5_creds c, mc;
156 Ticket ticket;
158 krb5_cc_clear_mcred(&mc);
159 ret = krb5_cc_get_principal(context, cache, &mc.server);
160 if (ret)
161 krb5_err (context, 1, ret, "krb5_cc_get_principal");
163 ret = krb5_cc_resolve(context, delegation_cred_str, &id);
164 if(ret)
165 krb5_err (context, 1, ret, "krb5_cc_resolve");
167 ret = krb5_cc_retrieve_cred(context, id, 0, &mc, &c);
168 if(ret)
169 krb5_err (context, 1, ret, "krb5_cc_retrieve_cred");
171 ret = decode_Ticket(c.ticket.data, c.ticket.length, &ticket, NULL);
172 if (ret) {
173 krb5_clear_error_message(context);
174 krb5_err (context, 1, ret, "decode_Ticket");
176 krb5_free_cred_contents(context, &c);
178 ret = krb5_get_creds_opt_set_ticket(context, opt, &ticket);
179 if(ret)
180 krb5_err (context, 1, ret, "krb5_get_creds_opt_set_ticket");
181 free_Ticket(&ticket);
183 krb5_cc_close (context, id);
184 krb5_free_principal(context, mc.server);
186 krb5_get_creds_opt_add_options(context, opt,
187 KRB5_GC_CONSTRAINED_DELEGATION);
190 if (nametype_str) {
191 int32_t nametype;
192 char *sname = NULL;
193 char *hname = NULL;
195 ret = krb5_parse_nametype(context, nametype_str, &nametype);
196 if (ret)
197 krb5_err(context, 1, ret, "krb5_parse_nametype");
199 if (nametype == KRB5_NT_SRV_HST && argc == 2) {
200 sname = argv[0];
201 hname = argv[1];
202 ret = krb5_sname_to_principal(context, hname, sname,
203 KRB5_NT_SRV_HST, &server);
204 if (ret)
205 krb5_err(context, 1, ret, "krb5_sname_to_principal %s/%s",
206 (sname && *sname) ? sname : "<default>",
207 (hname && *hname) ? hname : "<default>");
208 } else {
209 if (argc != 1)
210 usage(1);
211 ret = krb5_parse_name(context, argv[0], &server);
212 if (ret)
213 krb5_err (context, 1, ret, "krb5_parse_name %s", argv[0]);
214 server->name.name_type = (NAME_TYPE)nametype;
216 } else if (argc == 1) {
217 ret = krb5_parse_name(context, argv[0], &server);
218 if (ret)
219 krb5_err (context, 1, ret, "krb5_parse_name %s", argv[0]);
220 } else {
221 usage(1);
224 ret = krb5_get_creds(context, opt, cache, server, &out);
225 if (ret)
226 krb5_err (context, 1, ret, "krb5_get_creds");
228 if (out_cache_str) {
229 krb5_ccache id;
231 ret = krb5_cc_resolve(context, out_cache_str, &id);
232 if(ret)
233 krb5_err (context, 1, ret, "krb5_cc_resolve");
235 ret = krb5_cc_initialize(context, id, out->client);
236 if(ret)
237 krb5_err (context, 1, ret, "krb5_cc_initialize");
239 ret = krb5_cc_store_cred(context, id, out);
240 if(ret)
241 krb5_err (context, 1, ret, "krb5_cc_store_cred");
242 krb5_cc_close (context, id);
245 krb5_free_creds(context, out);
246 krb5_free_principal(context, server);
247 krb5_get_creds_opt_free(context, opt);
248 krb5_cc_close (context, cache);
249 krb5_free_context (context);
251 return 0;