xref: /freebsd/crypto/heimdal/kuser/kimpersonate.c (revision 6a068746777241722b2b32c5d0bc443a2a64d80b)
1c19800e8SDoug Rabson /*
2*ae771770SStanislav Sedov  * Copyright (c) 2000 - 2007 Kungliga Tekniska Högskolan
3c19800e8SDoug Rabson  * (Royal Institute of Technology, Stockholm, Sweden).
4c19800e8SDoug Rabson  * All rights reserved.
5c19800e8SDoug Rabson  *
6c19800e8SDoug Rabson  * Redistribution and use in source and binary forms, with or without
7c19800e8SDoug Rabson  * modification, are permitted provided that the following conditions
8c19800e8SDoug Rabson  * are met:
9c19800e8SDoug Rabson  *
10c19800e8SDoug Rabson  * 1. Redistributions of source code must retain the above copyright
11c19800e8SDoug Rabson  *    notice, this list of conditions and the following disclaimer.
12c19800e8SDoug Rabson  *
13c19800e8SDoug Rabson  * 2. Redistributions in binary form must reproduce the above copyright
14c19800e8SDoug Rabson  *    notice, this list of conditions and the following disclaimer in the
15c19800e8SDoug Rabson  *    documentation and/or other materials provided with the distribution.
16c19800e8SDoug Rabson  *
17c19800e8SDoug Rabson  * 3. Neither the name of the Institute nor the names of its contributors
18c19800e8SDoug Rabson  *    may be used to endorse or promote products derived from this software
19c19800e8SDoug Rabson  *    without specific prior written permission.
20c19800e8SDoug Rabson  *
21c19800e8SDoug Rabson  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22c19800e8SDoug Rabson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23c19800e8SDoug Rabson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24c19800e8SDoug Rabson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25c19800e8SDoug Rabson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26c19800e8SDoug Rabson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27c19800e8SDoug Rabson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28c19800e8SDoug Rabson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29c19800e8SDoug Rabson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30c19800e8SDoug Rabson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31c19800e8SDoug Rabson  * SUCH DAMAGE.
32c19800e8SDoug Rabson  */
33c19800e8SDoug Rabson 
34c19800e8SDoug Rabson #include "kuser_locl.h"
35c19800e8SDoug Rabson #include <parse_units.h>
36c19800e8SDoug Rabson 
37c19800e8SDoug Rabson static char *client_principal_str = NULL;
38c19800e8SDoug Rabson static krb5_principal client_principal;
39c19800e8SDoug Rabson static char *server_principal_str = NULL;
40c19800e8SDoug Rabson static krb5_principal server_principal;
41c19800e8SDoug Rabson 
42c19800e8SDoug Rabson static char *ccache_str = NULL;
43c19800e8SDoug Rabson 
44c19800e8SDoug Rabson static char *ticket_flags_str = NULL;
45c19800e8SDoug Rabson static TicketFlags ticket_flags;
46c19800e8SDoug Rabson static char *keytab_file = NULL;
47*ae771770SStanislav Sedov static char *enctype_string = NULL;
48c19800e8SDoug Rabson static int   expiration_time = 3600;
49c19800e8SDoug Rabson static struct getarg_strings client_addresses;
50c19800e8SDoug Rabson static int   version_flag = 0;
51c19800e8SDoug Rabson static int   help_flag = 0;
52c19800e8SDoug Rabson static int   use_krb5 = 1;
53c19800e8SDoug Rabson 
54*ae771770SStanislav Sedov static const char *enc_type = "des-cbc-md5";
55*ae771770SStanislav Sedov 
56c19800e8SDoug Rabson /*
57c19800e8SDoug Rabson  *
58c19800e8SDoug Rabson  */
59c19800e8SDoug Rabson 
60c19800e8SDoug Rabson static void
encode_ticket(krb5_context context,EncryptionKey * skey,krb5_enctype etype,int skvno,krb5_creds * cred)61c19800e8SDoug Rabson encode_ticket (krb5_context context,
62c19800e8SDoug Rabson 	       EncryptionKey *skey,
63c19800e8SDoug Rabson 	       krb5_enctype etype,
64c19800e8SDoug Rabson 	       int skvno,
65c19800e8SDoug Rabson 	       krb5_creds *cred)
66c19800e8SDoug Rabson {
67c19800e8SDoug Rabson     size_t len, size;
68c19800e8SDoug Rabson     char *buf;
69c19800e8SDoug Rabson     krb5_error_code ret;
70c19800e8SDoug Rabson     krb5_crypto crypto;
71c19800e8SDoug Rabson     EncryptedData enc_part;
72c19800e8SDoug Rabson     EncTicketPart et;
73c19800e8SDoug Rabson     Ticket ticket;
74c19800e8SDoug Rabson 
75c19800e8SDoug Rabson     memset (&enc_part, 0, sizeof(enc_part));
76c19800e8SDoug Rabson     memset (&ticket, 0, sizeof(ticket));
77c19800e8SDoug Rabson 
78c19800e8SDoug Rabson     /*
79c19800e8SDoug Rabson      * Set up `enc_part'
80c19800e8SDoug Rabson      */
81c19800e8SDoug Rabson 
82c19800e8SDoug Rabson     et.flags = cred->flags.b;
83c19800e8SDoug Rabson     et.key = cred->session;
84*ae771770SStanislav Sedov     et.crealm = cred->client->realm;
85c19800e8SDoug Rabson     copy_PrincipalName(&cred->client->name, &et.cname);
86c19800e8SDoug Rabson     {
87c19800e8SDoug Rabson 	krb5_data empty_string;
88c19800e8SDoug Rabson 
89c19800e8SDoug Rabson 	krb5_data_zero(&empty_string);
90c19800e8SDoug Rabson 	et.transited.tr_type = DOMAIN_X500_COMPRESS;
91c19800e8SDoug Rabson 	et.transited.contents = empty_string;
92c19800e8SDoug Rabson     }
93c19800e8SDoug Rabson     et.authtime = cred->times.authtime;
94c19800e8SDoug Rabson     et.starttime = NULL;
95c19800e8SDoug Rabson     et.endtime = cred->times.endtime;
96c19800e8SDoug Rabson     et.renew_till = NULL;
97c19800e8SDoug Rabson     et.caddr = &cred->addresses;
98c19800e8SDoug Rabson     et.authorization_data = NULL; /* XXX allow random authorization_data */
99c19800e8SDoug Rabson 
100c19800e8SDoug Rabson     /*
101c19800e8SDoug Rabson      * Encrypt `enc_part' of ticket with service key
102c19800e8SDoug Rabson      */
103c19800e8SDoug Rabson 
104c19800e8SDoug Rabson     ASN1_MALLOC_ENCODE(EncTicketPart, buf, len, &et, &size, ret);
105c19800e8SDoug Rabson     if (ret)
106c19800e8SDoug Rabson 	krb5_err(context, 1, ret, "EncTicketPart");
107c19800e8SDoug Rabson 
108*ae771770SStanislav Sedov     ret = krb5_crypto_init(context, skey, etype, &crypto);
109*ae771770SStanislav Sedov     if (ret)
110*ae771770SStanislav Sedov 	krb5_err(context, 1, ret, "krb5_crypto_init");
111*ae771770SStanislav Sedov     ret = krb5_encrypt_EncryptedData (context,
112c19800e8SDoug Rabson 				      crypto,
113c19800e8SDoug Rabson 				      KRB5_KU_TICKET,
114c19800e8SDoug Rabson 				      buf,
115c19800e8SDoug Rabson 				      len,
116c19800e8SDoug Rabson 				      skvno,
117c19800e8SDoug Rabson 				      &ticket.enc_part);
118*ae771770SStanislav Sedov     if (ret)
119*ae771770SStanislav Sedov 	krb5_err(context, 1, ret, "krb5_encrypt_EncryptedData");
120*ae771770SStanislav Sedov 
121c19800e8SDoug Rabson     free(buf);
122c19800e8SDoug Rabson     krb5_crypto_destroy(context, crypto);
123c19800e8SDoug Rabson 
124c19800e8SDoug Rabson     /*
125c19800e8SDoug Rabson      * Encode ticket
126c19800e8SDoug Rabson      */
127c19800e8SDoug Rabson 
128c19800e8SDoug Rabson     ticket.tkt_vno = 5;
129*ae771770SStanislav Sedov     ticket.realm = cred->server->realm;
130c19800e8SDoug Rabson     copy_PrincipalName(&cred->server->name, &ticket.sname);
131c19800e8SDoug Rabson 
132c19800e8SDoug Rabson     ASN1_MALLOC_ENCODE(Ticket, buf, len, &ticket, &size, ret);
133c19800e8SDoug Rabson     if(ret)
134c19800e8SDoug Rabson 	krb5_err (context, 1, ret, "encode_Ticket");
135c19800e8SDoug Rabson 
136c19800e8SDoug Rabson     krb5_data_copy(&cred->ticket, buf, len);
137*ae771770SStanislav Sedov     free(buf);
138c19800e8SDoug Rabson }
139c19800e8SDoug Rabson 
140c19800e8SDoug Rabson /*
141c19800e8SDoug Rabson  *
142c19800e8SDoug Rabson  */
143c19800e8SDoug Rabson 
144c19800e8SDoug Rabson static int
create_krb5_tickets(krb5_context context,krb5_keytab kt)145c19800e8SDoug Rabson create_krb5_tickets (krb5_context context, krb5_keytab kt)
146c19800e8SDoug Rabson {
147c19800e8SDoug Rabson     krb5_error_code ret;
148c19800e8SDoug Rabson     krb5_keytab_entry entry;
149c19800e8SDoug Rabson     krb5_creds cred;
150c19800e8SDoug Rabson     krb5_enctype etype;
151c19800e8SDoug Rabson     krb5_ccache ccache;
152c19800e8SDoug Rabson 
153c19800e8SDoug Rabson     memset (&cred, 0, sizeof(cred));
154c19800e8SDoug Rabson 
155c19800e8SDoug Rabson     ret = krb5_string_to_enctype (context, enc_type, &etype);
156c19800e8SDoug Rabson     if (ret)
157c19800e8SDoug Rabson 	krb5_err (context, 1, ret, "krb5_string_to_enctype");
158c19800e8SDoug Rabson     ret = krb5_kt_get_entry (context, kt, server_principal,
159c19800e8SDoug Rabson 			     0, etype, &entry);
160c19800e8SDoug Rabson     if (ret)
161c19800e8SDoug Rabson 	krb5_err (context, 1, ret, "krb5_kt_get_entry");
162c19800e8SDoug Rabson 
163c19800e8SDoug Rabson     /*
164c19800e8SDoug Rabson      * setup cred
165c19800e8SDoug Rabson      */
166c19800e8SDoug Rabson 
167c19800e8SDoug Rabson 
168c19800e8SDoug Rabson     ret = krb5_copy_principal (context, client_principal, &cred.client);
169c19800e8SDoug Rabson     if (ret)
170c19800e8SDoug Rabson 	krb5_err (context, 1, ret, "krb5_copy_principal");
171c19800e8SDoug Rabson     ret = krb5_copy_principal (context, server_principal, &cred.server);
172c19800e8SDoug Rabson     if (ret)
173c19800e8SDoug Rabson 	krb5_err (context, 1, ret, "krb5_copy_principal");
174c19800e8SDoug Rabson     krb5_generate_random_keyblock(context, etype, &cred.session);
175c19800e8SDoug Rabson 
176c19800e8SDoug Rabson     cred.times.authtime = time(NULL);
177c19800e8SDoug Rabson     cred.times.starttime = time(NULL);
178c19800e8SDoug Rabson     cred.times.endtime = time(NULL) + expiration_time;
179c19800e8SDoug Rabson     cred.times.renew_till = 0;
180c19800e8SDoug Rabson     krb5_data_zero(&cred.second_ticket);
181c19800e8SDoug Rabson 
182c19800e8SDoug Rabson     ret = krb5_get_all_client_addrs (context, &cred.addresses);
183c19800e8SDoug Rabson     if (ret)
184c19800e8SDoug Rabson 	krb5_err (context, 1, ret, "krb5_get_all_client_addrs");
185c19800e8SDoug Rabson     cred.flags.b = ticket_flags;
186c19800e8SDoug Rabson 
187c19800e8SDoug Rabson 
188c19800e8SDoug Rabson     /*
189c19800e8SDoug Rabson      * Encode encrypted part of ticket
190c19800e8SDoug Rabson      */
191c19800e8SDoug Rabson 
192c19800e8SDoug Rabson     encode_ticket (context, &entry.keyblock, etype, entry.vno, &cred);
193c19800e8SDoug Rabson 
194c19800e8SDoug Rabson     /*
195c19800e8SDoug Rabson      * Write to cc
196c19800e8SDoug Rabson      */
197c19800e8SDoug Rabson 
198c19800e8SDoug Rabson     if (ccache_str) {
199c19800e8SDoug Rabson 	ret = krb5_cc_resolve(context, ccache_str, &ccache);
200c19800e8SDoug Rabson 	if (ret)
201c19800e8SDoug Rabson 	    krb5_err (context, 1, ret, "krb5_cc_resolve");
202c19800e8SDoug Rabson     } else {
203c19800e8SDoug Rabson 	ret = krb5_cc_default (context, &ccache);
204c19800e8SDoug Rabson 	if (ret)
205c19800e8SDoug Rabson 	    krb5_err (context, 1, ret, "krb5_cc_default");
206c19800e8SDoug Rabson     }
207c19800e8SDoug Rabson 
208c19800e8SDoug Rabson     ret = krb5_cc_initialize (context, ccache, cred.client);
209c19800e8SDoug Rabson     if (ret)
210c19800e8SDoug Rabson 	krb5_err (context, 1, ret, "krb5_cc_initialize");
211c19800e8SDoug Rabson 
212c19800e8SDoug Rabson     ret = krb5_cc_store_cred (context, ccache, &cred);
213c19800e8SDoug Rabson     if (ret)
214c19800e8SDoug Rabson 	krb5_err (context, 1, ret, "krb5_cc_store_cred");
215c19800e8SDoug Rabson 
216c19800e8SDoug Rabson     krb5_free_cred_contents (context, &cred);
217c19800e8SDoug Rabson     krb5_cc_close (context, ccache);
218c19800e8SDoug Rabson 
219c19800e8SDoug Rabson     return 0;
220c19800e8SDoug Rabson }
221c19800e8SDoug Rabson 
222c19800e8SDoug Rabson /*
223c19800e8SDoug Rabson  *
224c19800e8SDoug Rabson  */
225c19800e8SDoug Rabson 
226c19800e8SDoug Rabson static void
setup_env(krb5_context context,krb5_keytab * kt)227c19800e8SDoug Rabson setup_env (krb5_context context, krb5_keytab *kt)
228c19800e8SDoug Rabson {
229c19800e8SDoug Rabson     krb5_error_code ret;
230c19800e8SDoug Rabson 
231c19800e8SDoug Rabson     if (keytab_file)
232c19800e8SDoug Rabson 	ret = krb5_kt_resolve (context, keytab_file, kt);
233c19800e8SDoug Rabson     else
234c19800e8SDoug Rabson 	ret = krb5_kt_default (context, kt);
235c19800e8SDoug Rabson     if (ret)
236c19800e8SDoug Rabson 	krb5_err (context, 1, ret, "resolving keytab");
237c19800e8SDoug Rabson 
238c19800e8SDoug Rabson     if (client_principal_str == NULL)
239c19800e8SDoug Rabson 	krb5_errx (context, 1, "missing client principal");
240c19800e8SDoug Rabson     ret = krb5_parse_name (context, client_principal_str, &client_principal);
241c19800e8SDoug Rabson     if (ret)
242c19800e8SDoug Rabson 	krb5_err (context, 1, ret, "resolvning client name");
243c19800e8SDoug Rabson 
244c19800e8SDoug Rabson     if (server_principal_str == NULL)
245c19800e8SDoug Rabson 	krb5_errx (context, 1, "missing server principal");
246c19800e8SDoug Rabson     ret = krb5_parse_name (context, server_principal_str, &server_principal);
247c19800e8SDoug Rabson     if (ret)
248c19800e8SDoug Rabson 	krb5_err (context, 1, ret, "resolvning client name");
249c19800e8SDoug Rabson 
250c19800e8SDoug Rabson     if (ticket_flags_str) {
251c19800e8SDoug Rabson 	int ticket_flags_int;
252c19800e8SDoug Rabson 
253c19800e8SDoug Rabson 	ticket_flags_int = parse_flags(ticket_flags_str,
254c19800e8SDoug Rabson 				       asn1_TicketFlags_units(), 0);
255c19800e8SDoug Rabson 	if (ticket_flags_int <= 0) {
256c19800e8SDoug Rabson 	    krb5_warnx (context, "bad ticket flags: `%s'", ticket_flags_str);
257c19800e8SDoug Rabson 	    print_flags_table (asn1_TicketFlags_units(), stderr);
258c19800e8SDoug Rabson 	    exit (1);
259c19800e8SDoug Rabson 	}
260c19800e8SDoug Rabson 	if (ticket_flags_int)
261c19800e8SDoug Rabson 	    ticket_flags = int2TicketFlags (ticket_flags_int);
262c19800e8SDoug Rabson     }
263c19800e8SDoug Rabson }
264c19800e8SDoug Rabson 
265c19800e8SDoug Rabson /*
266c19800e8SDoug Rabson  *
267c19800e8SDoug Rabson  */
268c19800e8SDoug Rabson 
269c19800e8SDoug Rabson struct getargs args[] = {
270c19800e8SDoug Rabson     { "ccache", 0, arg_string, &ccache_str,
271c19800e8SDoug Rabson       "name of kerberos 5 credential cache", "cache-name"},
272c19800e8SDoug Rabson     { "server", 's', arg_string, &server_principal_str,
273*ae771770SStanislav Sedov       "name of server principal", NULL },
274c19800e8SDoug Rabson     { "client", 'c', arg_string, &client_principal_str,
275*ae771770SStanislav Sedov       "name of client principal", NULL },
276c19800e8SDoug Rabson     { "keytab", 'k', arg_string, &keytab_file,
277*ae771770SStanislav Sedov       "name of keytab file", NULL },
278c19800e8SDoug Rabson     { "krb5", '5', arg_flag,	 &use_krb5,
279*ae771770SStanislav Sedov       "create a kerberos 5 ticket", NULL },
280c19800e8SDoug Rabson     { "expire-time", 'e', arg_integer, &expiration_time,
281*ae771770SStanislav Sedov       "lifetime of ticket in seconds", NULL },
282c19800e8SDoug Rabson     { "client-addresses", 'a', arg_strings, &client_addresses,
283*ae771770SStanislav Sedov       "addresses of client", NULL },
284*ae771770SStanislav Sedov     { "enc-type", 't', arg_string, 	&enctype_string,
285*ae771770SStanislav Sedov       "encryption type", NULL },
286c19800e8SDoug Rabson     { "ticket-flags", 'f', arg_string,   &ticket_flags_str,
287*ae771770SStanislav Sedov       "ticket flags for krb5 ticket", NULL },
288c19800e8SDoug Rabson     { "version", 0,  arg_flag,		&version_flag,	"Print version",
289c19800e8SDoug Rabson       NULL },
290c19800e8SDoug Rabson     { "help",	 0,  arg_flag,		&help_flag,	NULL,
291c19800e8SDoug Rabson       NULL }
292c19800e8SDoug Rabson };
293c19800e8SDoug Rabson 
294c19800e8SDoug Rabson static void
usage(int ret)295c19800e8SDoug Rabson usage (int ret)
296c19800e8SDoug Rabson {
297c19800e8SDoug Rabson     arg_printusage (args,
298c19800e8SDoug Rabson 		    sizeof(args) / sizeof(args[0]),
299c19800e8SDoug Rabson 		    NULL,
300c19800e8SDoug Rabson 		    "");
301c19800e8SDoug Rabson     exit (ret);
302c19800e8SDoug Rabson }
303c19800e8SDoug Rabson 
304c19800e8SDoug Rabson int
main(int argc,char ** argv)305c19800e8SDoug Rabson main (int argc, char **argv)
306c19800e8SDoug Rabson {
307*ae771770SStanislav Sedov     int optidx = 0;
308c19800e8SDoug Rabson     krb5_error_code ret;
309c19800e8SDoug Rabson     krb5_context context;
310c19800e8SDoug Rabson     krb5_keytab kt;
311c19800e8SDoug Rabson 
312c19800e8SDoug Rabson     setprogname (argv[0]);
313c19800e8SDoug Rabson 
314c19800e8SDoug Rabson     ret = krb5_init_context (&context);
315c19800e8SDoug Rabson     if (ret)
316c19800e8SDoug Rabson 	errx(1, "krb5_init_context failed: %u", ret);
317c19800e8SDoug Rabson 
318*ae771770SStanislav Sedov     if (getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx))
319c19800e8SDoug Rabson 	usage(1);
320c19800e8SDoug Rabson 
321c19800e8SDoug Rabson     if (help_flag)
322c19800e8SDoug Rabson 	usage(0);
323c19800e8SDoug Rabson 
324c19800e8SDoug Rabson     if (version_flag) {
325c19800e8SDoug Rabson 	print_version(NULL);
326c19800e8SDoug Rabson 	return 0;
327c19800e8SDoug Rabson     }
328c19800e8SDoug Rabson 
329*ae771770SStanislav Sedov     if (enctype_string)
330*ae771770SStanislav Sedov 	enc_type = enctype_string;
331*ae771770SStanislav Sedov 
332c19800e8SDoug Rabson     setup_env(context, &kt);
333c19800e8SDoug Rabson 
334c19800e8SDoug Rabson     if (use_krb5)
335c19800e8SDoug Rabson 	create_krb5_tickets(context, kt);
336c19800e8SDoug Rabson 
337c19800e8SDoug Rabson     krb5_kt_close(context, kt);
338*ae771770SStanislav Sedov 
339c19800e8SDoug Rabson     return 0;
340c19800e8SDoug Rabson }
341