check return values
[heimdal.git] / kuser / kimpersonate.c
blob023f5587aad82616ec20c760e8a253e7e0e2a89d
1 /*
2 * Copyright (c) 2000 - 2007 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"
35 #include <parse_units.h>
37 static char *client_principal_str = NULL;
38 static krb5_principal client_principal;
39 static char *server_principal_str = NULL;
40 static krb5_principal server_principal;
42 static char *ccache_str = NULL;
44 static char *ticket_flags_str = NULL;
45 static TicketFlags ticket_flags;
46 static char *keytab_file = NULL;
47 static char *enc_type = "des-cbc-md5";
48 static int expiration_time = 3600;
49 static struct getarg_strings client_addresses;
50 static int version_flag = 0;
51 static int help_flag = 0;
52 static int use_krb5 = 1;
58 static void
59 encode_ticket (krb5_context context,
60 EncryptionKey *skey,
61 krb5_enctype etype,
62 int skvno,
63 krb5_creds *cred)
65 size_t len, size;
66 char *buf;
67 krb5_error_code ret;
68 krb5_crypto crypto;
69 EncryptedData enc_part;
70 EncTicketPart et;
71 Ticket ticket;
73 memset (&enc_part, 0, sizeof(enc_part));
74 memset (&ticket, 0, sizeof(ticket));
77 * Set up `enc_part'
80 et.flags = cred->flags.b;
81 et.key = cred->session;
82 et.crealm = cred->client->realm;
83 copy_PrincipalName(&cred->client->name, &et.cname);
85 krb5_data empty_string;
87 krb5_data_zero(&empty_string);
88 et.transited.tr_type = DOMAIN_X500_COMPRESS;
89 et.transited.contents = empty_string;
91 et.authtime = cred->times.authtime;
92 et.starttime = NULL;
93 et.endtime = cred->times.endtime;
94 et.renew_till = NULL;
95 et.caddr = &cred->addresses;
96 et.authorization_data = NULL; /* XXX allow random authorization_data */
99 * Encrypt `enc_part' of ticket with service key
102 ASN1_MALLOC_ENCODE(EncTicketPart, buf, len, &et, &size, ret);
103 if (ret)
104 krb5_err(context, 1, ret, "EncTicketPart");
106 ret = krb5_crypto_init(context, skey, etype, &crypto);
107 if (ret)
108 krb5_err(context, 1, ret, "krb5_crypto_init");
109 ret = krb5_encrypt_EncryptedData (context,
110 crypto,
111 KRB5_KU_TICKET,
112 buf,
113 len,
114 skvno,
115 &ticket.enc_part);
116 if (ret)
117 krb5_err(context, 1, ret, "krb5_encrypt_EncryptedData");
119 free(buf);
120 krb5_crypto_destroy(context, crypto);
123 * Encode ticket
126 ticket.tkt_vno = 5;
127 ticket.realm = cred->server->realm;
128 copy_PrincipalName(&cred->server->name, &ticket.sname);
130 ASN1_MALLOC_ENCODE(Ticket, buf, len, &ticket, &size, ret);
131 if(ret)
132 krb5_err (context, 1, ret, "encode_Ticket");
134 krb5_data_copy(&cred->ticket, buf, len);
135 free(buf);
142 static int
143 create_krb5_tickets (krb5_context context, krb5_keytab kt)
145 krb5_error_code ret;
146 krb5_keytab_entry entry;
147 krb5_creds cred;
148 krb5_enctype etype;
149 krb5_ccache ccache;
151 memset (&cred, 0, sizeof(cred));
153 ret = krb5_string_to_enctype (context, enc_type, &etype);
154 if (ret)
155 krb5_err (context, 1, ret, "krb5_string_to_enctype");
156 ret = krb5_kt_get_entry (context, kt, server_principal,
157 0, etype, &entry);
158 if (ret)
159 krb5_err (context, 1, ret, "krb5_kt_get_entry");
162 * setup cred
166 ret = krb5_copy_principal (context, client_principal, &cred.client);
167 if (ret)
168 krb5_err (context, 1, ret, "krb5_copy_principal");
169 ret = krb5_copy_principal (context, server_principal, &cred.server);
170 if (ret)
171 krb5_err (context, 1, ret, "krb5_copy_principal");
172 krb5_generate_random_keyblock(context, etype, &cred.session);
174 cred.times.authtime = time(NULL);
175 cred.times.starttime = time(NULL);
176 cred.times.endtime = time(NULL) + expiration_time;
177 cred.times.renew_till = 0;
178 krb5_data_zero(&cred.second_ticket);
180 ret = krb5_get_all_client_addrs (context, &cred.addresses);
181 if (ret)
182 krb5_err (context, 1, ret, "krb5_get_all_client_addrs");
183 cred.flags.b = ticket_flags;
187 * Encode encrypted part of ticket
190 encode_ticket (context, &entry.keyblock, etype, entry.vno, &cred);
193 * Write to cc
196 if (ccache_str) {
197 ret = krb5_cc_resolve(context, ccache_str, &ccache);
198 if (ret)
199 krb5_err (context, 1, ret, "krb5_cc_resolve");
200 } else {
201 ret = krb5_cc_default (context, &ccache);
202 if (ret)
203 krb5_err (context, 1, ret, "krb5_cc_default");
206 ret = krb5_cc_initialize (context, ccache, cred.client);
207 if (ret)
208 krb5_err (context, 1, ret, "krb5_cc_initialize");
210 ret = krb5_cc_store_cred (context, ccache, &cred);
211 if (ret)
212 krb5_err (context, 1, ret, "krb5_cc_store_cred");
214 krb5_free_cred_contents (context, &cred);
215 krb5_cc_close (context, ccache);
217 return 0;
224 static void
225 setup_env (krb5_context context, krb5_keytab *kt)
227 krb5_error_code ret;
229 if (keytab_file)
230 ret = krb5_kt_resolve (context, keytab_file, kt);
231 else
232 ret = krb5_kt_default (context, kt);
233 if (ret)
234 krb5_err (context, 1, ret, "resolving keytab");
236 if (client_principal_str == NULL)
237 krb5_errx (context, 1, "missing client principal");
238 ret = krb5_parse_name (context, client_principal_str, &client_principal);
239 if (ret)
240 krb5_err (context, 1, ret, "resolvning client name");
242 if (server_principal_str == NULL)
243 krb5_errx (context, 1, "missing server principal");
244 ret = krb5_parse_name (context, server_principal_str, &server_principal);
245 if (ret)
246 krb5_err (context, 1, ret, "resolvning client name");
248 if (ticket_flags_str) {
249 int ticket_flags_int;
251 ticket_flags_int = parse_flags(ticket_flags_str,
252 asn1_TicketFlags_units(), 0);
253 if (ticket_flags_int <= 0) {
254 krb5_warnx (context, "bad ticket flags: `%s'", ticket_flags_str);
255 print_flags_table (asn1_TicketFlags_units(), stderr);
256 exit (1);
258 if (ticket_flags_int)
259 ticket_flags = int2TicketFlags (ticket_flags_int);
267 struct getargs args[] = {
268 { "ccache", 0, arg_string, &ccache_str,
269 "name of kerberos 5 credential cache", "cache-name"},
270 { "server", 's', arg_string, &server_principal_str,
271 "name of server principal" },
272 { "client", 'c', arg_string, &client_principal_str,
273 "name of client principal" },
274 { "keytab", 'k', arg_string, &keytab_file,
275 "name of keytab file" },
276 { "krb5", '5', arg_flag, &use_krb5,
277 "create a kerberos 5 ticket"},
278 { "expire-time", 'e', arg_integer, &expiration_time,
279 "lifetime of ticket in seconds" },
280 { "client-addresses", 'a', arg_strings, &client_addresses,
281 "addresses of client" },
282 { "enc-type", 't', arg_string, &enc_type,
283 "encryption type" },
284 { "ticket-flags", 'f', arg_string, &ticket_flags_str,
285 "ticket flags for krb5 ticket" },
286 { "version", 0, arg_flag, &version_flag, "Print version",
287 NULL },
288 { "help", 0, arg_flag, &help_flag, NULL,
289 NULL }
292 static void
293 usage (int ret)
295 arg_printusage (args,
296 sizeof(args) / sizeof(args[0]),
297 NULL,
298 "");
299 exit (ret);
303 main (int argc, char **argv)
305 int optind = 0;
306 krb5_error_code ret;
307 krb5_context context;
308 krb5_keytab kt;
310 setprogname (argv[0]);
312 ret = krb5_init_context (&context);
313 if (ret)
314 errx(1, "krb5_init_context failed: %u", ret);
316 if (getarg (args, sizeof(args) / sizeof(args[0]), argc, argv,
317 &optind))
318 usage (1);
320 if (help_flag)
321 usage (0);
323 if (version_flag) {
324 print_version(NULL);
325 return 0;
328 setup_env (context, &kt);
330 if (use_krb5)
331 create_krb5_tickets (context, kt);
333 krb5_kt_close (context, kt);
334 return 0;