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("$Heimdal: klist.c 20516 2007-04-22 10:40:41Z lha $"
41 printable_time(time_t t
)
44 strlcpy(s
, ctime(&t
)+ 4, sizeof(s
));
50 printable_time_long(time_t t
)
53 strlcpy(s
, ctime(&t
)+ 4, sizeof(s
));
58 #define COL_ISSUED " Issued"
59 #define COL_EXPIRES " Expires"
60 #define COL_FLAGS "Flags"
61 #define COL_PRINCIPAL " Principal"
62 #define COL_PRINCIPAL_KVNO " Principal (kvno)"
63 #define COL_CACHENAME " Cache name"
66 print_cred(krb5_context context
, krb5_creds
*cred
, rtbl_t ct
, int do_flags
)
72 krb5_timeofday (context
, &sec
);
75 if(cred
->times
.starttime
)
76 rtbl_add_column_entry(ct
, COL_ISSUED
,
77 printable_time(cred
->times
.starttime
));
79 rtbl_add_column_entry(ct
, COL_ISSUED
,
80 printable_time(cred
->times
.authtime
));
82 if(cred
->times
.endtime
> sec
)
83 rtbl_add_column_entry(ct
, COL_EXPIRES
,
84 printable_time(cred
->times
.endtime
));
86 rtbl_add_column_entry(ct
, COL_EXPIRES
, ">>>Expired<<<");
87 ret
= krb5_unparse_name (context
, cred
->server
, &str
);
89 krb5_err(context
, 1, ret
, "krb5_unparse_name");
90 rtbl_add_column_entry(ct
, COL_PRINCIPAL
, str
);
93 if(cred
->flags
.b
.forwardable
)
95 if(cred
->flags
.b
.forwarded
)
97 if(cred
->flags
.b
.proxiable
)
99 if(cred
->flags
.b
.proxy
)
101 if(cred
->flags
.b
.may_postdate
)
103 if(cred
->flags
.b
.postdated
)
105 if(cred
->flags
.b
.renewable
)
107 if(cred
->flags
.b
.initial
)
109 if(cred
->flags
.b
.invalid
)
111 if(cred
->flags
.b
.pre_authent
)
113 if(cred
->flags
.b
.hw_authent
)
116 rtbl_add_column_entry(ct
, COL_FLAGS
, s
);
122 print_cred_verbose(krb5_context context
, krb5_creds
*cred
)
130 krb5_timeofday (context
, &sec
);
132 ret
= krb5_unparse_name(context
, cred
->server
, &str
);
135 printf("Server: %s\n", str
);
138 ret
= krb5_unparse_name(context
, cred
->client
, &str
);
141 printf("Client: %s\n", str
);
149 decode_Ticket(cred
->ticket
.data
, cred
->ticket
.length
, &t
, &len
);
150 ret
= krb5_enctype_to_string(context
, t
.enc_part
.etype
, &s
);
151 printf("Ticket etype: ");
156 printf("unknown(%d)", t
.enc_part
.etype
);
159 printf(", kvno %d", *t
.enc_part
.kvno
);
161 if(cred
->session
.keytype
!= t
.enc_part
.etype
) {
162 ret
= krb5_enctype_to_string(context
, cred
->session
.keytype
, &str
);
164 krb5_warn(context
, ret
, "session keytype");
166 printf("Session key: %s\n", str
);
171 printf("Ticket length: %lu\n", (unsigned long)cred
->ticket
.length
);
173 printf("Auth time: %s\n", printable_time_long(cred
->times
.authtime
));
174 if(cred
->times
.authtime
!= cred
->times
.starttime
)
175 printf("Start time: %s\n", printable_time_long(cred
->times
.starttime
));
176 printf("End time: %s", printable_time_long(cred
->times
.endtime
));
177 if(sec
> cred
->times
.endtime
)
178 printf(" (expired)");
180 if(cred
->flags
.b
.renewable
)
181 printf("Renew till: %s\n",
182 printable_time_long(cred
->times
.renew_till
));
183 printf("Ticket flags: ");
184 #define PRINT_FLAG2(f, s) if(cred->flags.b.f) { if(!first_flag) printf(", "); printf("%s", #s); first_flag = 0; }
185 #define PRINT_FLAG(f) PRINT_FLAG2(f, f)
187 PRINT_FLAG(forwardable
);
188 PRINT_FLAG(forwarded
);
189 PRINT_FLAG(proxiable
);
191 PRINT_FLAG2(may_postdate
, may
-postdate
);
192 PRINT_FLAG(postdated
);
194 PRINT_FLAG(renewable
);
196 PRINT_FLAG2(pre_authent
, pre
-authenticated
);
197 PRINT_FLAG2(hw_authent
, hw
-authenticated
);
198 PRINT_FLAG2(transited_policy_checked
, transited
-policy
-checked
);
199 PRINT_FLAG2(ok_as_delegate
, ok
-as
-delegate
);
200 PRINT_FLAG(anonymous
);
202 printf("Addresses: ");
203 if (cred
->addresses
.len
!= 0) {
204 for(j
= 0; j
< cred
->addresses
.len
; j
++){
208 ret
= krb5_print_address(&cred
->addresses
.val
[j
],
209 buf
, sizeof(buf
), &len
);
215 printf("addressless");
221 * Print all tickets in `ccache' on stdout, verbosily iff do_verbose.
225 print_tickets (krb5_context context
,
227 krb5_principal principal
,
234 krb5_cc_cursor cursor
;
240 ret
= krb5_unparse_name (context
, principal
, &str
);
242 krb5_err (context
, 1, ret
, "krb5_unparse_name");
244 printf ("%17s: %s:%s\n",
246 krb5_cc_get_type(context
, ccache
),
247 krb5_cc_get_name(context
, ccache
));
248 printf ("%17s: %s\n", "Principal", str
);
252 printf ("%17s: %d\n", "Cache version",
253 krb5_cc_get_version(context
, ccache
));
255 krb5_get_kdc_sec_offset(context
, &sec
, &usec
);
257 if (do_verbose
&& sec
!= 0) {
269 unparse_time (val
, buf
, sizeof(buf
));
271 printf ("%17s: %s%s\n", "KDC time offset",
272 sig
== -1 ? "-" : "", buf
);
277 ret
= krb5_cc_start_seq_get (context
, ccache
, &cursor
);
279 krb5_err(context
, 1, ret
, "krb5_cc_start_seq_get");
283 rtbl_add_column(ct
, COL_ISSUED
, 0);
284 rtbl_add_column(ct
, COL_EXPIRES
, 0);
286 rtbl_add_column(ct
, COL_FLAGS
, 0);
287 rtbl_add_column(ct
, COL_PRINCIPAL
, 0);
288 rtbl_set_separator(ct
, " ");
290 while ((ret
= krb5_cc_next_cred (context
,
295 str
= krb5_principal_get_comp_string(context
, creds
.server
, 0);
296 if (!do_hidden
&& str
&& str
[0] == '@') {
298 }else if(do_verbose
){
299 print_cred_verbose(context
, &creds
);
301 print_cred(context
, &creds
, ct
, do_flags
);
303 krb5_free_cred_contents (context
, &creds
);
305 if(ret
!= KRB5_CC_END
)
306 krb5_err(context
, 1, ret
, "krb5_cc_get_next");
307 ret
= krb5_cc_end_seq_get (context
, ccache
, &cursor
);
309 krb5_err (context
, 1, ret
, "krb5_cc_end_seq_get");
311 rtbl_format(ct
, stdout
);
317 * Check if there's a tgt for the realm of `principal' and ccache and
318 * if so return 0, else 1
322 check_for_tgt (krb5_context context
,
324 krb5_principal principal
,
330 krb5_realm
*client_realm
;
333 krb5_cc_clear_mcred(&pattern
);
335 client_realm
= krb5_princ_realm (context
, principal
);
337 ret
= krb5_make_principal (context
, &pattern
.server
,
338 *client_realm
, KRB5_TGS_NAME
, *client_realm
,
341 krb5_err (context
, 1, ret
, "krb5_make_principal");
342 pattern
.client
= principal
;
344 ret
= krb5_cc_retrieve_cred (context
, ccache
, 0, &pattern
, &creds
);
345 krb5_free_principal (context
, pattern
.server
);
347 if (ret
== KRB5_CC_END
)
349 krb5_err (context
, 1, ret
, "krb5_cc_retrieve_cred");
352 expired
= time(NULL
) > creds
.times
.endtime
;
355 *expiration
= creds
.times
.endtime
;
357 krb5_free_cred_contents (context
, &creds
);
363 * Print a list of all AFS tokens
367 display_tokens(int do_verbose
)
370 unsigned char t
[4096];
371 struct ViceIoctl parms
;
373 parms
.in
= (void *)&i
;
374 parms
.in_size
= sizeof(i
);
375 parms
.out
= (void *)t
;
376 parms
.out_size
= sizeof(t
);
379 int32_t size_secret_tok
, size_public_tok
;
381 struct ClearToken ct
;
382 unsigned char *r
= t
;
384 char buf1
[20], buf2
[20];
386 if(k_pioctl(NULL
, VIOCGETTOK
, &parms
, 0) < 0) {
391 if(parms
.out_size
> sizeof(t
))
393 if(parms
.out_size
< sizeof(size_secret_tok
))
395 t
[min(parms
.out_size
,sizeof(t
)-1)] = 0;
396 memcpy(&size_secret_tok
, r
, sizeof(size_secret_tok
));
397 /* dont bother about the secret token */
398 r
+= size_secret_tok
+ sizeof(size_secret_tok
);
399 if (parms
.out_size
< (r
- t
) + sizeof(size_public_tok
))
401 memcpy(&size_public_tok
, r
, sizeof(size_public_tok
));
402 r
+= sizeof(size_public_tok
);
403 if (parms
.out_size
< (r
- t
) + size_public_tok
+ sizeof(int32_t))
405 memcpy(&ct
, r
, size_public_tok
);
406 r
+= size_public_tok
;
407 /* there is a int32_t with length of cellname, but we dont read it */
408 r
+= sizeof(int32_t);
411 gettimeofday (&tv
, NULL
);
412 strlcpy (buf1
, printable_time(ct
.BeginTimestamp
),
414 if (do_verbose
|| tv
.tv_sec
< ct
.EndTimestamp
)
415 strlcpy (buf2
, printable_time(ct
.EndTimestamp
),
418 strlcpy (buf2
, ">>> Expired <<<", sizeof(buf2
));
420 printf("%s %s ", buf1
, buf2
);
422 if ((ct
.EndTimestamp
- ct
.BeginTimestamp
) & 1)
423 printf("User's (AFS ID %d) tokens for %s", ct
.ViceId
, cell
);
425 printf("Tokens for %s", cell
);
427 printf(" (%d)", ct
.AuthHandle
);
433 * display the ccache in `cred_cache'
437 display_v5_ccache (const char *cred_cache
, int do_test
, int do_verbose
,
438 int do_flags
, int do_hidden
)
441 krb5_context context
;
443 krb5_principal principal
;
446 ret
= krb5_init_context (&context
);
448 errx (1, "krb5_init_context failed: %d", ret
);
451 ret
= krb5_cc_resolve(context
, cred_cache
, &ccache
);
453 krb5_err (context
, 1, ret
, "%s", cred_cache
);
455 ret
= krb5_cc_default (context
, &ccache
);
457 krb5_err (context
, 1, ret
, "krb5_cc_resolve");
460 ret
= krb5_cc_get_principal (context
, ccache
, &principal
);
464 krb5_warnx(context
, "No ticket file: %s",
465 krb5_cc_get_name(context
, ccache
));
468 krb5_err (context
, 1, ret
, "krb5_cc_get_principal");
471 exit_status
= check_for_tgt (context
, ccache
, principal
, NULL
);
473 print_tickets (context
, ccache
, principal
, do_verbose
,
474 do_flags
, do_hidden
);
476 ret
= krb5_cc_close (context
, ccache
);
478 krb5_err (context
, 1, ret
, "krb5_cc_close");
480 krb5_free_principal (context
, principal
);
481 krb5_free_context (context
);
492 krb5_cc_cache_cursor cursor
;
493 krb5_context context
;
498 ret
= krb5_init_context (&context
);
500 errx (1, "krb5_init_context failed: %d", ret
);
502 ret
= krb5_cc_cache_get_first (context
, NULL
, &cursor
);
503 if (ret
== KRB5_CC_NOSUPP
)
506 krb5_err (context
, 1, ret
, "krb5_cc_cache_get_first");
509 rtbl_add_column(ct
, COL_PRINCIPAL
, 0);
510 rtbl_add_column(ct
, COL_CACHENAME
, 0);
511 rtbl_add_column(ct
, COL_EXPIRES
, 0);
512 rtbl_set_prefix(ct
, " ");
513 rtbl_set_column_prefix(ct
, COL_PRINCIPAL
, "");
515 while ((ret
= krb5_cc_cache_next (context
, cursor
, &id
)) == 0) {
516 krb5_principal principal
;
519 ret
= krb5_cc_get_principal(context
, id
, &principal
);
522 int expired
= check_for_tgt (context
, id
, principal
, &t
);
524 ret
= krb5_unparse_name(context
, principal
, &name
);
526 rtbl_add_column_entry(ct
, COL_PRINCIPAL
, name
);
527 rtbl_add_column_entry(ct
, COL_CACHENAME
,
528 krb5_cc_get_name(context
, id
));
529 rtbl_add_column_entry(ct
, COL_EXPIRES
,
530 expired
? ">>> Expired <<<" :
533 krb5_free_principal(context
, principal
);
536 krb5_cc_close(context
, id
);
539 krb5_cc_cache_end_seq_get(context
, cursor
);
541 rtbl_format(ct
, stdout
);
551 static int version_flag
= 0;
552 static int help_flag
= 0;
553 static int do_verbose
= 0;
554 static int do_list_caches
= 0;
555 static int do_test
= 0;
556 static int do_tokens
= 0;
557 static int do_v5
= 1;
558 static char *cred_cache
;
559 static int do_flags
= 0;
560 static int do_hidden
= 0;
562 static struct getargs args
[] = {
563 { NULL
, 'f', arg_flag
, &do_flags
},
564 { "cache", 'c', arg_string
, &cred_cache
,
565 "credentials cache to list", "cache" },
566 { "test", 't', arg_flag
, &do_test
,
567 "test for having tickets", NULL
},
568 { NULL
, 's', arg_flag
, &do_test
},
569 { "tokens", 'T', arg_flag
, &do_tokens
,
570 "display AFS tokens", NULL
},
571 { "v5", '5', arg_flag
, &do_v5
,
572 "display v5 cred cache", NULL
},
573 { "list-caches", 'l', arg_flag
, &do_list_caches
,
574 "verbose output", NULL
},
575 { "verbose", 'v', arg_flag
, &do_verbose
,
576 "verbose output", NULL
},
577 { "hidden", 0, arg_flag
, &do_hidden
,
578 "display hidden credentials", NULL
},
579 { NULL
, 'a', arg_flag
, &do_verbose
},
580 { NULL
, 'n', arg_flag
, &do_verbose
},
581 { "version", 0, arg_flag
, &version_flag
,
582 "print version", NULL
},
583 { "help", 0, arg_flag
, &help_flag
,
590 arg_printusage (args
,
591 sizeof(args
)/sizeof(*args
),
598 main (int argc
, char **argv
)
603 setprogname (argv
[0]);
605 if(getarg(args
, sizeof(args
) / sizeof(args
[0]), argc
, argv
, &optidx
))
622 if (do_list_caches
) {
623 exit_status
= list_caches();
628 exit_status
= display_v5_ccache (cred_cache
, do_test
,
629 do_verbose
, do_flags
, do_hidden
);
632 if (do_tokens
&& k_hasafs ()) {
635 display_tokens (do_verbose
);