2 * Copyright (c) 1997-2004 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
34 #include "kuser_locl.h"
37 RCSID("$Id: klist.c 20516 2007-04-22 10:40:41Z lha $");
40 printable_time(time_t t)
43 strlcpy(s, ctime(&t)+ 4, sizeof(s));
49 printable_time_long(time_t t)
52 strlcpy(s, ctime(&t)+ 4, sizeof(s));
57 #define COL_ISSUED " Issued"
58 #define COL_EXPIRES " Expires"
59 #define COL_FLAGS "Flags"
60 #define COL_PRINCIPAL " Principal"
61 #define COL_PRINCIPAL_KVNO " Principal (kvno)"
62 #define COL_CACHENAME " Cache name"
65 print_cred(krb5_context context, krb5_creds *cred, rtbl_t ct, int do_flags)
71 krb5_timeofday (context, &sec);
74 if(cred->times.starttime)
75 rtbl_add_column_entry(ct, COL_ISSUED,
76 printable_time(cred->times.starttime));
78 rtbl_add_column_entry(ct, COL_ISSUED,
79 printable_time(cred->times.authtime));
81 if(cred->times.endtime > sec)
82 rtbl_add_column_entry(ct, COL_EXPIRES,
83 printable_time(cred->times.endtime));
85 rtbl_add_column_entry(ct, COL_EXPIRES, ">>>Expired<<<");
86 ret = krb5_unparse_name (context, cred->server, &str);
88 krb5_err(context, 1, ret, "krb5_unparse_name");
89 rtbl_add_column_entry(ct, COL_PRINCIPAL, str);
92 if(cred->flags.b.forwardable)
94 if(cred->flags.b.forwarded)
96 if(cred->flags.b.proxiable)
98 if(cred->flags.b.proxy)
100 if(cred->flags.b.may_postdate)
102 if(cred->flags.b.postdated)
104 if(cred->flags.b.renewable)
106 if(cred->flags.b.initial)
108 if(cred->flags.b.invalid)
110 if(cred->flags.b.pre_authent)
112 if(cred->flags.b.hw_authent)
115 rtbl_add_column_entry(ct, COL_FLAGS, s);
121 print_cred_verbose(krb5_context context, krb5_creds *cred)
129 krb5_timeofday (context, &sec);
131 ret = krb5_unparse_name(context, cred->server, &str);
134 printf("Server: %s\n", str);
137 ret = krb5_unparse_name(context, cred->client, &str);
140 printf("Client: %s\n", str);
148 decode_Ticket(cred->ticket.data, cred->ticket.length, &t, &len);
149 ret = krb5_enctype_to_string(context, t.enc_part.etype, &s);
150 printf("Ticket etype: ");
155 printf("unknown(%d)", t.enc_part.etype);
158 printf(", kvno %d", *t.enc_part.kvno);
160 if(cred->session.keytype != t.enc_part.etype) {
161 ret = krb5_enctype_to_string(context, cred->session.keytype, &str);
163 krb5_warn(context, ret, "session keytype");
165 printf("Session key: %s\n", str);
170 printf("Ticket length: %lu\n", (unsigned long)cred->ticket.length);
172 printf("Auth time: %s\n", printable_time_long(cred->times.authtime));
173 if(cred->times.authtime != cred->times.starttime)
174 printf("Start time: %s\n", printable_time_long(cred->times.starttime));
175 printf("End time: %s", printable_time_long(cred->times.endtime));
176 if(sec > cred->times.endtime)
177 printf(" (expired)");
179 if(cred->flags.b.renewable)
180 printf("Renew till: %s\n",
181 printable_time_long(cred->times.renew_till));
182 printf("Ticket flags: ");
183 #define PRINT_FLAG2(f, s) if(cred->flags.b.f) { if(!first_flag) printf(", "); printf("%s", #s); first_flag = 0; }
184 #define PRINT_FLAG(f) PRINT_FLAG2(f, f)
186 PRINT_FLAG(forwardable);
187 PRINT_FLAG(forwarded);
188 PRINT_FLAG(proxiable);
190 PRINT_FLAG2(may_postdate, may-postdate);
191 PRINT_FLAG(postdated);
193 PRINT_FLAG(renewable);
195 PRINT_FLAG2(pre_authent, pre-authenticated);
196 PRINT_FLAG2(hw_authent, hw-authenticated);
197 PRINT_FLAG2(transited_policy_checked, transited-policy-checked);
198 PRINT_FLAG2(ok_as_delegate, ok-as-delegate);
199 PRINT_FLAG(anonymous);
201 printf("Addresses: ");
202 if (cred->addresses.len != 0) {
203 for(j = 0; j < cred->addresses.len; j++){
207 ret = krb5_print_address(&cred->addresses.val[j],
208 buf, sizeof(buf), &len);
214 printf("addressless");
220 * Print all tickets in `ccache' on stdout, verbosily iff do_verbose.
224 print_tickets (krb5_context context,
226 krb5_principal principal,
233 krb5_cc_cursor cursor;
239 ret = krb5_unparse_name (context, principal, &str);
241 krb5_err (context, 1, ret, "krb5_unparse_name");
243 printf ("%17s: %s:%s\n",
245 krb5_cc_get_type(context, ccache),
246 krb5_cc_get_name(context, ccache));
247 printf ("%17s: %s\n", "Principal", str);
251 printf ("%17s: %d\n", "Cache version",
252 krb5_cc_get_version(context, ccache));
254 krb5_get_kdc_sec_offset(context, &sec, &usec);
256 if (do_verbose && sec != 0) {
268 unparse_time (val, buf, sizeof(buf));
270 printf ("%17s: %s%s\n", "KDC time offset",
271 sig == -1 ? "-" : "", buf);
276 ret = krb5_cc_start_seq_get (context, ccache, &cursor);
278 krb5_err(context, 1, ret, "krb5_cc_start_seq_get");
282 rtbl_add_column(ct, COL_ISSUED, 0);
283 rtbl_add_column(ct, COL_EXPIRES, 0);
285 rtbl_add_column(ct, COL_FLAGS, 0);
286 rtbl_add_column(ct, COL_PRINCIPAL, 0);
287 rtbl_set_separator(ct, " ");
289 while ((ret = krb5_cc_next_cred (context,
294 str = krb5_principal_get_comp_string(context, creds.server, 0);
295 if (!do_hidden && str && str[0] == '@') {
297 }else if(do_verbose){
298 print_cred_verbose(context, &creds);
300 print_cred(context, &creds, ct, do_flags);
302 krb5_free_cred_contents (context, &creds);
304 if(ret != KRB5_CC_END)
305 krb5_err(context, 1, ret, "krb5_cc_get_next");
306 ret = krb5_cc_end_seq_get (context, ccache, &cursor);
308 krb5_err (context, 1, ret, "krb5_cc_end_seq_get");
310 rtbl_format(ct, stdout);
316 * Check if there's a tgt for the realm of `principal' and ccache and
317 * if so return 0, else 1
321 check_for_tgt (krb5_context context,
323 krb5_principal principal,
329 krb5_realm *client_realm;
332 krb5_cc_clear_mcred(&pattern);
334 client_realm = krb5_princ_realm (context, principal);
336 ret = krb5_make_principal (context, &pattern.server,
337 *client_realm, KRB5_TGS_NAME, *client_realm,
340 krb5_err (context, 1, ret, "krb5_make_principal");
341 pattern.client = principal;
343 ret = krb5_cc_retrieve_cred (context, ccache, 0, &pattern, &creds);
344 krb5_free_principal (context, pattern.server);
346 if (ret == KRB5_CC_END)
348 krb5_err (context, 1, ret, "krb5_cc_retrieve_cred");
351 expired = time(NULL) > creds.times.endtime;
354 *expiration = creds.times.endtime;
356 krb5_free_cred_contents (context, &creds);
362 * Print a list of all AFS tokens
366 display_tokens(int do_verbose)
369 unsigned char t[4096];
370 struct ViceIoctl parms;
372 parms.in = (void *)&i;
373 parms.in_size = sizeof(i);
374 parms.out = (void *)t;
375 parms.out_size = sizeof(t);
378 int32_t size_secret_tok, size_public_tok;
380 struct ClearToken ct;
381 unsigned char *r = t;
383 char buf1[20], buf2[20];
385 if(k_pioctl(NULL, VIOCGETTOK, &parms, 0) < 0) {
390 if(parms.out_size > sizeof(t))
392 if(parms.out_size < sizeof(size_secret_tok))
394 t[min(parms.out_size,sizeof(t)-1)] = 0;
395 memcpy(&size_secret_tok, r, sizeof(size_secret_tok));
396 /* dont bother about the secret token */
397 r += size_secret_tok + sizeof(size_secret_tok);
398 if (parms.out_size < (r - t) + sizeof(size_public_tok))
400 memcpy(&size_public_tok, r, sizeof(size_public_tok));
401 r += sizeof(size_public_tok);
402 if (parms.out_size < (r - t) + size_public_tok + sizeof(int32_t))
404 memcpy(&ct, r, size_public_tok);
405 r += size_public_tok;
406 /* there is a int32_t with length of cellname, but we dont read it */
407 r += sizeof(int32_t);
410 gettimeofday (&tv, NULL);
411 strlcpy (buf1, printable_time(ct.BeginTimestamp),
413 if (do_verbose || tv.tv_sec < ct.EndTimestamp)
414 strlcpy (buf2, printable_time(ct.EndTimestamp),
417 strlcpy (buf2, ">>> Expired <<<", sizeof(buf2));
419 printf("%s %s ", buf1, buf2);
421 if ((ct.EndTimestamp - ct.BeginTimestamp) & 1)
422 printf("User's (AFS ID %d) tokens for %s", ct.ViceId, cell);
424 printf("Tokens for %s", cell);
426 printf(" (%d)", ct.AuthHandle);
432 * display the ccache in `cred_cache'
436 display_v5_ccache (const char *cred_cache, int do_test, int do_verbose,
437 int do_flags, int do_hidden)
440 krb5_context context;
442 krb5_principal principal;
445 ret = krb5_init_context (&context);
447 errx (1, "krb5_init_context failed: %d", ret);
450 ret = krb5_cc_resolve(context, cred_cache, &ccache);
452 krb5_err (context, 1, ret, "%s", cred_cache);
454 ret = krb5_cc_default (context, &ccache);
456 krb5_err (context, 1, ret, "krb5_cc_resolve");
459 ret = krb5_cc_get_principal (context, ccache, &principal);
463 krb5_warnx(context, "No ticket file: %s",
464 krb5_cc_get_name(context, ccache));
467 krb5_err (context, 1, ret, "krb5_cc_get_principal");
470 exit_status = check_for_tgt (context, ccache, principal, NULL);
472 print_tickets (context, ccache, principal, do_verbose,
473 do_flags, do_hidden);
475 ret = krb5_cc_close (context, ccache);
477 krb5_err (context, 1, ret, "krb5_cc_close");
479 krb5_free_principal (context, principal);
480 krb5_free_context (context);
491 krb5_cc_cache_cursor cursor;
492 krb5_context context;
497 ret = krb5_init_context (&context);
499 errx (1, "krb5_init_context failed: %d", ret);
501 ret = krb5_cc_cache_get_first (context, NULL, &cursor);
502 if (ret == KRB5_CC_NOSUPP)
505 krb5_err (context, 1, ret, "krb5_cc_cache_get_first");
508 rtbl_add_column(ct, COL_PRINCIPAL, 0);
509 rtbl_add_column(ct, COL_CACHENAME, 0);
510 rtbl_add_column(ct, COL_EXPIRES, 0);
511 rtbl_set_prefix(ct, " ");
512 rtbl_set_column_prefix(ct, COL_PRINCIPAL, "");
514 while ((ret = krb5_cc_cache_next (context, cursor, &id)) == 0) {
515 krb5_principal principal;
518 ret = krb5_cc_get_principal(context, id, &principal);
521 int expired = check_for_tgt (context, id, principal, &t);
523 ret = krb5_unparse_name(context, principal, &name);
525 rtbl_add_column_entry(ct, COL_PRINCIPAL, name);
526 rtbl_add_column_entry(ct, COL_CACHENAME,
527 krb5_cc_get_name(context, id));
528 rtbl_add_column_entry(ct, COL_EXPIRES,
529 expired ? ">>> Expired <<<" :
532 krb5_free_principal(context, principal);
535 krb5_cc_close(context, id);
538 krb5_cc_cache_end_seq_get(context, cursor);
540 rtbl_format(ct, stdout);
550 static int version_flag = 0;
551 static int help_flag = 0;
552 static int do_verbose = 0;
553 static int do_list_caches = 0;
554 static int do_test = 0;
555 static int do_tokens = 0;
556 static int do_v5 = 1;
557 static char *cred_cache;
558 static int do_flags = 0;
559 static int do_hidden = 0;
561 static struct getargs args[] = {
562 { NULL, 'f', arg_flag, &do_flags },
563 { "cache", 'c', arg_string, &cred_cache,
564 "credentials cache to list", "cache" },
565 { "test", 't', arg_flag, &do_test,
566 "test for having tickets", NULL },
567 { NULL, 's', arg_flag, &do_test },
568 { "tokens", 'T', arg_flag, &do_tokens,
569 "display AFS tokens", NULL },
570 { "v5", '5', arg_flag, &do_v5,
571 "display v5 cred cache", NULL},
572 { "list-caches", 'l', arg_flag, &do_list_caches,
573 "verbose output", NULL },
574 { "verbose", 'v', arg_flag, &do_verbose,
575 "verbose output", NULL },
576 { "hidden", 0, arg_flag, &do_hidden,
577 "display hidden credentials", NULL },
578 { NULL, 'a', arg_flag, &do_verbose },
579 { NULL, 'n', arg_flag, &do_verbose },
580 { "version", 0, arg_flag, &version_flag,
581 "print version", NULL },
582 { "help", 0, arg_flag, &help_flag,
589 arg_printusage (args,
590 sizeof(args)/sizeof(*args),
597 main (int argc, char **argv)
602 setprogname (argv[0]);
604 if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx))
621 if (do_list_caches) {
622 exit_status = list_caches();
627 exit_status = display_v5_ccache (cred_cache, do_test,
628 do_verbose, do_flags, do_hidden);
631 if (do_tokens && k_hasafs ()) {
634 display_tokens (do_verbose);