1 /* $NetBSD: kvno.c,v 1.1.1.2 2014/04/24 12:45:28 pettai Exp $ */
4 * Copyright (C) 1998 by the FundsXpress, INC.
8 * Export of this software from the United States of America may require
9 * a specific license from the United States Government. It is the
10 * responsibility of any person or organization contemplating export to
11 * obtain such a license before exporting.
13 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
14 * distribute this software and its documentation for any purpose and
15 * without fee is hereby granted, provided that the above copyright
16 * notice appear in all copies and that both that copyright notice and
17 * this permission notice appear in supporting documentation, and that
18 * the name of FundsXpress. not be used in advertising or publicity pertaining
19 * to distribution of the software without specific, written prior
20 * permission. FundsXpress makes no representations about the suitability of
21 * this software for any purpose. It is provided "as is" without express
22 * or implied warranty.
24 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
26 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29 #include "kuser_locl.h"
31 static char *etype_str
= NULL
;
32 static char *ccache_name
= NULL
;
33 static char *keytab_name
= NULL
;
34 static char *sname
= NULL
;
36 static int version_flag
= 0;
37 static int help_flag
= 0;
38 static int quiet_flag
= 0;
40 static void do_v5_kvno (int argc
, char *argv
[],
41 char *ccache_name
, char *etype_str
, char *keytab_name
,
44 struct getargs args
[] = {
45 { "enctype", 'e', arg_string
, &etype_str
,
46 NP_("Encryption type to use", ""), "enctype" },
47 { "cache", 'c', arg_string
, &ccache_name
,
48 NP_("Credentials cache", ""), "cachename" },
49 { "keytab", 'k', arg_string
, &keytab_name
,
50 NP_("Keytab to use", ""), "keytabname" },
51 { "server", 'S', arg_string
, &sname
,
52 NP_("Server to get ticket for", ""), "principal" },
53 { "quiet", 'q', arg_flag
, &quiet_flag
,
55 { "version", 0, arg_flag
, &version_flag
},
56 { "help", 0, arg_flag
, &help_flag
}
62 arg_printusage_i18n (args
, sizeof(args
)/sizeof(*args
),
63 N_("Usage: ", ""), NULL
,
64 "principal1 [principal2 ...]",
69 int main(int argc
, char *argv
[])
73 setprogname (argv
[0]);
75 setlocale(LC_ALL
, "");
76 bindtextdomain ("heimdal_kuser", HEIMDAL_LOCALEDIR
);
77 textdomain("heimdal_kuser");
79 if (getarg(args
, sizeof(args
)/sizeof(args
[0]), argc
, argv
, &optidx
))
93 do_v5_kvno(argc
, argv
, ccache_name
, etype_str
, keytab_name
, sname
);
98 static void do_v5_kvno (int count
, char *names
[],
99 char * ccache_name
, char *etype_str
, char *keytab_name
,
103 krb5_context context
= 0;
108 krb5_creds in_creds
, *out_creds
= NULL
;
112 krb5_keytab keytab
= NULL
;
114 ret
= krb5_init_context(&context
);
116 errx(1, "krb5_init_context failed: %d", ret
);
119 ret
= krb5_string_to_enctype(context
, etype_str
, &etype
);
121 krb5_err(context
, 1, ret
, "Failed to convert encryption type %s", etype_str
);
127 ret
= krb5_cc_resolve(context
, ccache_name
, &ccache
);
129 ret
= krb5_cc_default(context
, &ccache
);
131 krb5_err(context
, 1, ret
, "Failed to open credentials cache %s",
132 (ccache_name
) ? ccache_name
: "(Default)");
135 ret
= krb5_kt_resolve(context
, keytab_name
, &keytab
);
137 krb5_err(context
, 1, ret
, "Can't resolve keytab %s", keytab_name
);
140 ret
= krb5_cc_get_principal(context
, ccache
, &me
);
142 krb5_err(context
, 1, ret
, "krb5_cc_get_principal");
146 for (i
= 0; i
< count
; i
++) {
147 memset(&in_creds
, 0, sizeof(in_creds
));
148 memset(&ticket
, 0, sizeof(ticket
));
150 in_creds
.client
= me
;
153 ret
= krb5_sname_to_principal(context
, names
[i
],
154 sname
, KRB5_NT_SRV_HST
,
157 ret
= krb5_parse_name(context
, names
[i
], &in_creds
.server
);
161 krb5_warn(context
, ret
, "Couldn't parse principal name %s", names
[i
]);
166 ret
= krb5_unparse_name(context
, in_creds
.server
, &princ
);
168 krb5_warn(context
, ret
, "Couldn't format parsed principal name for '%s'",
174 in_creds
.session
.keytype
= etype
;
176 ret
= krb5_get_credentials(context
, 0, ccache
, &in_creds
, &out_creds
);
179 krb5_warn(context
, ret
, "Couldn't get credentials for %s", princ
);
184 ret
= decode_Ticket(out_creds
->ticket
.data
, out_creds
->ticket
.length
,
187 krb5_err(context
, 1, ret
, "Can't decode ticket for %s", princ
);
194 krb5_keytab_entry kte
;
197 EncTicketPart decr_part
;
199 ret
= krb5_kt_get_entry(context
, keytab
, in_creds
.server
,
200 (ticket
.enc_part
.kvno
!= NULL
)?
201 *ticket
.enc_part
.kvno
: 0,
202 ticket
.enc_part
.etype
,
205 krb5_warn(context
, ret
, "Can't decrypt ticket for %s", princ
);
207 printf("%s: kvno = %d, keytab entry invalid", princ
,
208 (ticket
.enc_part
.kvno
!= NULL
)?
209 *ticket
.enc_part
.kvno
: 0);
214 ret
= krb5_crypto_init(context
, &kte
.keyblock
, 0, &crypto
);
216 krb5_warn(context
, ret
, "krb5_crypto_init");
218 krb5_kt_free_entry(context
, &kte
);
222 ret
= krb5_decrypt_EncryptedData (context
, crypto
, KRB5_KU_TICKET
,
223 &ticket
.enc_part
, &dec_data
);
224 krb5_crypto_destroy(context
, crypto
);
225 krb5_kt_free_entry(context
, &kte
);
228 krb5_warn(context
, ret
, "krb5_decrypt_EncryptedData");
233 ret
= decode_EncTicketPart(dec_data
.data
, dec_data
.length
,
235 krb5_data_free(&dec_data
);
237 krb5_warn(context
, ret
, "decode_EncTicketPart");
243 printf("%s: kvno = %d, keytab entry valid\n", princ
,
244 (ticket
.enc_part
.kvno
!= NULL
)?
245 *ticket
.enc_part
.kvno
: 0);
247 free_EncTicketPart(&decr_part
);
250 printf("%s: kvno = %d\n", princ
,
251 (ticket
.enc_part
.kvno
!= NULL
)? *ticket
.enc_part
.kvno
: 0);
256 krb5_free_creds(context
, out_creds
);
261 krb5_free_unparsed_name(context
, princ
);
265 krb5_free_principal(context
, in_creds
.server
);
267 free_Ticket(&ticket
);
271 krb5_kt_close(context
, keytab
);
272 krb5_free_principal(context
, me
);
273 krb5_cc_close(context
, ccache
);
274 krb5_free_context(context
);