1c19800e8SDoug Rabson /*
2*ae771770SStanislav Sedov * Copyright (c) 2004 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
34*ae771770SStanislav Sedov #include "kuser_locl.h"
35c19800e8SDoug Rabson #include <config.h>
36c19800e8SDoug Rabson #include <parse_units.h>
37c19800e8SDoug Rabson #include <parse_time.h>
38*ae771770SStanislav Sedov #include "kcc-commands.h"
39c19800e8SDoug Rabson
40c19800e8SDoug Rabson static int32_t
bitswap32(int32_t b)41c19800e8SDoug Rabson bitswap32(int32_t b)
42c19800e8SDoug Rabson {
43c19800e8SDoug Rabson int32_t r = 0;
44c19800e8SDoug Rabson int i;
45c19800e8SDoug Rabson for (i = 0; i < 32; i++) {
46c19800e8SDoug Rabson r = r << 1 | (b & 1);
47c19800e8SDoug Rabson b = b >> 1;
48c19800e8SDoug Rabson }
49c19800e8SDoug Rabson return r;
50c19800e8SDoug Rabson }
51c19800e8SDoug Rabson
52c19800e8SDoug Rabson static void
parse_ticket_flags(krb5_context context,const char * string,krb5_ticket_flags * ret_flags)53c19800e8SDoug Rabson parse_ticket_flags(krb5_context context,
54c19800e8SDoug Rabson const char *string, krb5_ticket_flags *ret_flags)
55c19800e8SDoug Rabson {
56c19800e8SDoug Rabson TicketFlags ff;
57c19800e8SDoug Rabson int flags = parse_flags(string, asn1_TicketFlags_units(), 0);
58c19800e8SDoug Rabson if (flags == -1) /* XXX */
59c19800e8SDoug Rabson krb5_errx(context, 1, "bad flags specified: \"%s\"", string);
60c19800e8SDoug Rabson
61c19800e8SDoug Rabson memset(&ff, 0, sizeof(ff));
62c19800e8SDoug Rabson ff.proxy = 1;
63*ae771770SStanislav Sedov if ((size_t)parse_flags("proxy", asn1_TicketFlags_units(), 0) == TicketFlags2int(ff))
64c19800e8SDoug Rabson ret_flags->i = flags;
65c19800e8SDoug Rabson else
66c19800e8SDoug Rabson ret_flags->i = bitswap32(flags);
67c19800e8SDoug Rabson }
68c19800e8SDoug Rabson
69*ae771770SStanislav Sedov struct ctx {
70*ae771770SStanislav Sedov krb5_flags whichfields;
71*ae771770SStanislav Sedov krb5_creds mcreds;
72*ae771770SStanislav Sedov };
73*ae771770SStanislav Sedov
74*ae771770SStanislav Sedov static krb5_boolean
matchfunc(krb5_context context,void * ptr,const krb5_creds * creds)75*ae771770SStanislav Sedov matchfunc(krb5_context context, void *ptr, const krb5_creds *creds)
76*ae771770SStanislav Sedov {
77*ae771770SStanislav Sedov struct ctx *ctx = ptr;
78*ae771770SStanislav Sedov if (krb5_compare_creds(context, ctx->whichfields, &ctx->mcreds, creds))
79*ae771770SStanislav Sedov return TRUE;
80*ae771770SStanislav Sedov return FALSE;
81*ae771770SStanislav Sedov }
82*ae771770SStanislav Sedov
83c19800e8SDoug Rabson int
copy_cred_cache(struct copy_cred_cache_options * opt,int argc,char ** argv)84*ae771770SStanislav Sedov copy_cred_cache(struct copy_cred_cache_options *opt, int argc, char **argv)
85c19800e8SDoug Rabson {
86c19800e8SDoug Rabson krb5_error_code ret;
87c19800e8SDoug Rabson const char *from_name, *to_name;
88c19800e8SDoug Rabson krb5_ccache from_ccache, to_ccache;
89c19800e8SDoug Rabson unsigned int matched;
90*ae771770SStanislav Sedov struct ctx ctx;
91c19800e8SDoug Rabson
92*ae771770SStanislav Sedov memset(&ctx, 0, sizeof(ctx));
93c19800e8SDoug Rabson
94*ae771770SStanislav Sedov if (opt->service_string) {
95*ae771770SStanislav Sedov ret = krb5_parse_name(kcc_context, opt->service_string, &ctx.mcreds.server);
96c19800e8SDoug Rabson if (ret)
97*ae771770SStanislav Sedov krb5_err(kcc_context, 1, ret, "%s", opt->service_string);
98c19800e8SDoug Rabson }
99*ae771770SStanislav Sedov if (opt->enctype_string) {
100c19800e8SDoug Rabson krb5_enctype enctype;
101*ae771770SStanislav Sedov ret = krb5_string_to_enctype(kcc_context, opt->enctype_string, &enctype);
102c19800e8SDoug Rabson if (ret)
103*ae771770SStanislav Sedov krb5_err(kcc_context, 1, ret, "%s", opt->enctype_string);
104*ae771770SStanislav Sedov ctx.whichfields |= KRB5_TC_MATCH_KEYTYPE;
105*ae771770SStanislav Sedov ctx.mcreds.session.keytype = enctype;
106c19800e8SDoug Rabson }
107*ae771770SStanislav Sedov if (opt->flags_string) {
108*ae771770SStanislav Sedov parse_ticket_flags(kcc_context, opt->flags_string, &ctx.mcreds.flags);
109*ae771770SStanislav Sedov ctx.whichfields |= KRB5_TC_MATCH_FLAGS;
110c19800e8SDoug Rabson }
111*ae771770SStanislav Sedov if (opt->valid_for_string) {
112*ae771770SStanislav Sedov time_t t = parse_time(opt->valid_for_string, "s");
113c19800e8SDoug Rabson if(t < 0)
114*ae771770SStanislav Sedov errx(1, "unknown time \"%s\"", opt->valid_for_string);
115*ae771770SStanislav Sedov ctx.mcreds.times.endtime = time(NULL) + t;
116*ae771770SStanislav Sedov ctx.whichfields |= KRB5_TC_MATCH_TIMES;
117c19800e8SDoug Rabson }
118*ae771770SStanislav Sedov if (opt->fcache_version_integer)
119*ae771770SStanislav Sedov krb5_set_fcache_version(kcc_context, opt->fcache_version_integer);
120c19800e8SDoug Rabson
121c19800e8SDoug Rabson if (argc == 1) {
122*ae771770SStanislav Sedov from_name = krb5_cc_default_name(kcc_context);
123c19800e8SDoug Rabson to_name = argv[0];
124c19800e8SDoug Rabson } else {
125c19800e8SDoug Rabson from_name = argv[0];
126c19800e8SDoug Rabson to_name = argv[1];
127c19800e8SDoug Rabson }
128c19800e8SDoug Rabson
129*ae771770SStanislav Sedov ret = krb5_cc_resolve(kcc_context, from_name, &from_ccache);
130c19800e8SDoug Rabson if (ret)
131*ae771770SStanislav Sedov krb5_err(kcc_context, 1, ret, "%s", from_name);
132c19800e8SDoug Rabson
133*ae771770SStanislav Sedov if (opt->krbtgt_only_flag) {
134c19800e8SDoug Rabson krb5_principal client;
135*ae771770SStanislav Sedov ret = krb5_cc_get_principal(kcc_context, from_ccache, &client);
136c19800e8SDoug Rabson if (ret)
137*ae771770SStanislav Sedov krb5_err(kcc_context, 1, ret, "getting default principal");
138*ae771770SStanislav Sedov ret = krb5_make_principal(kcc_context, &ctx.mcreds.server,
139*ae771770SStanislav Sedov krb5_principal_get_realm(kcc_context, client),
140c19800e8SDoug Rabson KRB5_TGS_NAME,
141*ae771770SStanislav Sedov krb5_principal_get_realm(kcc_context, client),
142c19800e8SDoug Rabson NULL);
143c19800e8SDoug Rabson if (ret)
144*ae771770SStanislav Sedov krb5_err(kcc_context, 1, ret, "constructing krbtgt principal");
145*ae771770SStanislav Sedov krb5_free_principal(kcc_context, client);
146c19800e8SDoug Rabson }
147*ae771770SStanislav Sedov ret = krb5_cc_resolve(kcc_context, to_name, &to_ccache);
148c19800e8SDoug Rabson if (ret)
149*ae771770SStanislav Sedov krb5_err(kcc_context, 1, ret, "%s", to_name);
150c19800e8SDoug Rabson
151*ae771770SStanislav Sedov ret = krb5_cc_copy_match_f(kcc_context, from_ccache, to_ccache,
152*ae771770SStanislav Sedov matchfunc, &ctx, &matched);
153c19800e8SDoug Rabson if (ret)
154*ae771770SStanislav Sedov krb5_err(kcc_context, 1, ret, "copying cred cache");
155c19800e8SDoug Rabson
156*ae771770SStanislav Sedov krb5_cc_close(kcc_context, from_ccache);
157c19800e8SDoug Rabson if(matched == 0)
158*ae771770SStanislav Sedov krb5_cc_destroy(kcc_context, to_ccache);
159c19800e8SDoug Rabson else
160*ae771770SStanislav Sedov krb5_cc_close(kcc_context, to_ccache);
161*ae771770SStanislav Sedov
162c19800e8SDoug Rabson return matched == 0;
163c19800e8SDoug Rabson }
164