No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / dist / heimdal / kuser / kimpersonate.c
blob05ad24ba0b278a0d1b5a2000c63deabe2d60a389
1 /*
2 * Copyright (c) 2000 - 2007 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
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 __RCSID("$Heimdal: kimpersonate.c 22117 2007-12-03 21:24:16Z lha $"
36 "$NetBSD$");
37 #include <parse_units.h>
39 static char *client_principal_str = NULL;
40 static krb5_principal client_principal;
41 static char *server_principal_str = NULL;
42 static krb5_principal server_principal;
44 static char *ccache_str = NULL;
46 static char *ticket_flags_str = NULL;
47 static TicketFlags ticket_flags;
48 static char *keytab_file = NULL;
49 static char *enc_type = "des-cbc-md5";
50 static int expiration_time = 3600;
51 static struct getarg_strings client_addresses;
52 static int version_flag = 0;
53 static int help_flag = 0;
54 static int use_krb5 = 1;
60 static void
61 encode_ticket (krb5_context context,
62 EncryptionKey *skey,
63 krb5_enctype etype,
64 int skvno,
65 krb5_creds *cred)
67 size_t len, size;
68 char *buf;
69 krb5_error_code ret;
70 krb5_crypto crypto;
71 EncryptedData enc_part;
72 EncTicketPart et;
73 Ticket ticket;
75 memset (&enc_part, 0, sizeof(enc_part));
76 memset (&ticket, 0, sizeof(ticket));
79 * Set up `enc_part'
82 et.flags = cred->flags.b;
83 et.key = cred->session;
84 et.crealm = *krb5_princ_realm (context, cred->client);
85 copy_PrincipalName(&cred->client->name, &et.cname);
87 krb5_data empty_string;
89 krb5_data_zero(&empty_string);
90 et.transited.tr_type = DOMAIN_X500_COMPRESS;
91 et.transited.contents = empty_string;
93 et.authtime = cred->times.authtime;
94 et.starttime = NULL;
95 et.endtime = cred->times.endtime;
96 et.renew_till = NULL;
97 et.caddr = &cred->addresses;
98 et.authorization_data = NULL; /* XXX allow random authorization_data */
101 * Encrypt `enc_part' of ticket with service key
104 ASN1_MALLOC_ENCODE(EncTicketPart, buf, len, &et, &size, ret);
105 if (ret)
106 krb5_err(context, 1, ret, "EncTicketPart");
108 krb5_crypto_init(context, skey, etype, &crypto);
109 krb5_encrypt_EncryptedData (context,
110 crypto,
111 KRB5_KU_TICKET,
112 buf,
113 len,
114 skvno,
115 &ticket.enc_part);
116 free(buf);
117 krb5_crypto_destroy(context, crypto);
120 * Encode ticket
123 ticket.tkt_vno = 5;
124 ticket.realm = *krb5_princ_realm (context, cred->server);
125 copy_PrincipalName(&cred->server->name, &ticket.sname);
127 ASN1_MALLOC_ENCODE(Ticket, buf, len, &ticket, &size, ret);
128 if(ret)
129 krb5_err (context, 1, ret, "encode_Ticket");
131 krb5_data_copy(&cred->ticket, buf, len);
138 static int
139 create_krb5_tickets (krb5_context context, krb5_keytab kt)
141 krb5_error_code ret;
142 krb5_keytab_entry entry;
143 krb5_creds cred;
144 krb5_enctype etype;
145 krb5_ccache ccache;
147 memset (&cred, 0, sizeof(cred));
149 ret = krb5_string_to_enctype (context, enc_type, &etype);
150 if (ret)
151 krb5_err (context, 1, ret, "krb5_string_to_enctype");
152 ret = krb5_kt_get_entry (context, kt, server_principal,
153 0, etype, &entry);
154 if (ret)
155 krb5_err (context, 1, ret, "krb5_kt_get_entry");
158 * setup cred
162 ret = krb5_copy_principal (context, client_principal, &cred.client);
163 if (ret)
164 krb5_err (context, 1, ret, "krb5_copy_principal");
165 ret = krb5_copy_principal (context, server_principal, &cred.server);
166 if (ret)
167 krb5_err (context, 1, ret, "krb5_copy_principal");
168 krb5_generate_random_keyblock(context, etype, &cred.session);
170 cred.times.authtime = time(NULL);
171 cred.times.starttime = time(NULL);
172 cred.times.endtime = time(NULL) + expiration_time;
173 cred.times.renew_till = 0;
174 krb5_data_zero(&cred.second_ticket);
176 ret = krb5_get_all_client_addrs (context, &cred.addresses);
177 if (ret)
178 krb5_err (context, 1, ret, "krb5_get_all_client_addrs");
179 cred.flags.b = ticket_flags;
183 * Encode encrypted part of ticket
186 encode_ticket (context, &entry.keyblock, etype, entry.vno, &cred);
189 * Write to cc
192 if (ccache_str) {
193 ret = krb5_cc_resolve(context, ccache_str, &ccache);
194 if (ret)
195 krb5_err (context, 1, ret, "krb5_cc_resolve");
196 } else {
197 ret = krb5_cc_default (context, &ccache);
198 if (ret)
199 krb5_err (context, 1, ret, "krb5_cc_default");
202 ret = krb5_cc_initialize (context, ccache, cred.client);
203 if (ret)
204 krb5_err (context, 1, ret, "krb5_cc_initialize");
206 ret = krb5_cc_store_cred (context, ccache, &cred);
207 if (ret)
208 krb5_err (context, 1, ret, "krb5_cc_store_cred");
210 krb5_free_cred_contents (context, &cred);
211 krb5_cc_close (context, ccache);
213 return 0;
220 static void
221 setup_env (krb5_context context, krb5_keytab *kt)
223 krb5_error_code ret;
225 if (keytab_file)
226 ret = krb5_kt_resolve (context, keytab_file, kt);
227 else
228 ret = krb5_kt_default (context, kt);
229 if (ret)
230 krb5_err (context, 1, ret, "resolving keytab");
232 if (client_principal_str == NULL)
233 krb5_errx (context, 1, "missing client principal");
234 ret = krb5_parse_name (context, client_principal_str, &client_principal);
235 if (ret)
236 krb5_err (context, 1, ret, "resolvning client name");
238 if (server_principal_str == NULL)
239 krb5_errx (context, 1, "missing server principal");
240 ret = krb5_parse_name (context, server_principal_str, &server_principal);
241 if (ret)
242 krb5_err (context, 1, ret, "resolvning client name");
244 if (ticket_flags_str) {
245 int ticket_flags_int;
247 ticket_flags_int = parse_flags(ticket_flags_str,
248 asn1_TicketFlags_units(), 0);
249 if (ticket_flags_int <= 0) {
250 krb5_warnx (context, "bad ticket flags: `%s'", ticket_flags_str);
251 print_flags_table (asn1_TicketFlags_units(), stderr);
252 exit (1);
254 if (ticket_flags_int)
255 ticket_flags = int2TicketFlags (ticket_flags_int);
263 struct getargs args[] = {
264 { "ccache", 0, arg_string, &ccache_str,
265 "name of kerberos 5 credential cache", "cache-name"},
266 { "server", 's', arg_string, &server_principal_str,
267 "name of server principal" },
268 { "client", 'c', arg_string, &client_principal_str,
269 "name of client principal" },
270 { "keytab", 'k', arg_string, &keytab_file,
271 "name of keytab file" },
272 { "krb5", '5', arg_flag, &use_krb5,
273 "create a kerberos 5 ticket"},
274 { "expire-time", 'e', arg_integer, &expiration_time,
275 "lifetime of ticket in seconds" },
276 { "client-addresses", 'a', arg_strings, &client_addresses,
277 "addresses of client" },
278 { "enc-type", 't', arg_string, &enc_type,
279 "encryption type" },
280 { "ticket-flags", 'f', arg_string, &ticket_flags_str,
281 "ticket flags for krb5 ticket" },
282 { "version", 0, arg_flag, &version_flag, "Print version",
283 NULL },
284 { "help", 0, arg_flag, &help_flag, NULL,
285 NULL }
288 static void
289 usage (int ret)
291 arg_printusage (args,
292 sizeof(args) / sizeof(args[0]),
293 NULL,
294 "");
295 exit (ret);
299 main (int argc, char **argv)
301 int optind = 0;
302 krb5_error_code ret;
303 krb5_context context;
304 krb5_keytab kt;
306 setprogname (argv[0]);
308 ret = krb5_init_context (&context);
309 if (ret)
310 errx(1, "krb5_init_context failed: %u", ret);
312 if (getarg (args, sizeof(args) / sizeof(args[0]), argc, argv,
313 &optind))
314 usage (1);
316 if (help_flag)
317 usage (0);
319 if (version_flag) {
320 print_version(NULL);
321 return 0;
324 setup_env (context, &kt);
326 if (use_krb5)
327 create_krb5_tickets (context, kt);
329 krb5_kt_close (context, kt);
330 return 0;