1 /* $NetBSD: kcpytkt.c,v 1.1.1.2 2014/04/24 12:45:28 pettai Exp $ */
4 #include "kuser_locl.h"
6 static char *etypestr
= 0;
7 static char *fromccachestr
= 0;
8 static char *flagstr
= 0;
9 static int quiet_flag
= 0;
10 static int version_flag
= 0;
11 static int help_flag
= 0;
13 struct getargs args
[] = {
14 { "cache", 'c', arg_string
, &fromccachestr
,
15 "Credentials cache", "cachename" },
16 { "enctype", 'e', arg_string
, &etypestr
,
17 "Encryption type", "enctype" },
18 { "flags", 'f', arg_string
, &flagstr
,
20 { "quiet", 'q', arg_flag
, &quiet_flag
, "Quiet" },
21 { "version", 0, arg_flag
, &version_flag
},
22 { "help", 0, arg_flag
, &help_flag
}
28 arg_printusage(args
, sizeof(args
)/sizeof(args
[0]),
29 "Usage: ", "dest_ccache service1 [service2 ...]");
33 static void do_kcpytkt (int argc
, char *argv
[], char *fromccachestr
, char *etypestr
, int flags
);
35 int main(int argc
, char *argv
[])
42 if (getarg(args
, sizeof(args
)/sizeof(args
[0]), argc
, argv
, &optidx
))
60 flags
= atoi(flagstr
);
62 do_kcpytkt(argc
, argv
, fromccachestr
, etypestr
, flags
);
67 static void do_kcpytkt (int count
, char *names
[],
68 char *fromccachestr
, char *etypestr
, int flags
)
74 krb5_ccache fromccache
;
75 krb5_ccache destccache
;
77 krb5_creds in_creds
, out_creds
;
81 ret
= krb5_init_context(&context
);
83 errx(1, "krb5_init_context failed: %d", ret
);
86 ret
= krb5_string_to_enctype(context
, etypestr
, &etype
);
88 krb5_err(context
, 1, ret
, "Can't convert enctype %s", etypestr
);
89 retflags
= KRB5_TC_MATCH_SRV_NAMEONLY
| KRB5_TC_MATCH_KEYTYPE
;
92 retflags
= KRB5_TC_MATCH_SRV_NAMEONLY
;
96 ret
= krb5_cc_resolve(context
, fromccachestr
, &fromccache
);
98 ret
= krb5_cc_default(context
, &fromccache
);
100 krb5_err(context
, 1, ret
, "Can't resolve credentials cache");
102 ret
= krb5_cc_get_principal(context
, fromccache
, &me
);
104 krb5_err(context
, 1, ret
, "Can't query client principal name");
106 ret
= krb5_cc_resolve(context
, names
[0], &destccache
);
108 krb5_err(context
, 1, ret
, "Can't resolve destination cache");
112 for (i
= 1; i
< count
; i
++) {
113 memset(&in_creds
, 0, sizeof(in_creds
));
115 in_creds
.client
= me
;
117 ret
= krb5_parse_name(context
, names
[i
], &in_creds
.server
);
120 krb5_warn(context
, ret
, "Parse error for %s", names
[i
]);
125 ret
= krb5_unparse_name(context
, in_creds
.server
, &princ
);
127 krb5_warn(context
, ret
, "Unparse error for %s", names
[i
]);
132 in_creds
.session
.keytype
= etype
;
134 ret
= krb5_cc_retrieve_cred(context
, fromccache
, retflags
,
135 &in_creds
, &out_creds
);
137 krb5_warn(context
, ret
, "Can't retrieve credentials for %s", princ
);
139 krb5_free_unparsed_name(context
, princ
);
145 ret
= krb5_cc_store_cred(context
, destccache
, &out_creds
);
147 krb5_free_principal(context
, in_creds
.server
);
150 krb5_warn(context
, ret
, "Can't store credentials for %s", princ
);
152 krb5_free_cred_contents(context
, &out_creds
);
153 krb5_free_unparsed_name(context
, princ
);
159 krb5_free_unparsed_name(context
, princ
);
160 krb5_free_cred_contents(context
, &out_creds
);
163 krb5_free_principal(context
, me
);
164 krb5_cc_close(context
, fromccache
);
165 krb5_cc_close(context
, destccache
);
166 krb5_free_context(context
);