2 * Copyright (c) 1997-2008 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"
36 #include "parse_units.h"
39 printable_time(time_t t
)
42 strlcpy(s
, ctime(&t
)+ 4, sizeof(s
));
48 printable_time_long(time_t t
)
51 strlcpy(s
, ctime(&t
)+ 4, sizeof(s
));
56 #define COL_ISSUED NP_(" Issued","")
57 #define COL_EXPIRES NP_(" Expires", "")
58 #define COL_FLAGS NP_("Flags", "")
59 #define COL_NAME NP_(" Name", "")
60 #define COL_PRINCIPAL NP_(" Principal", "")
61 #define COL_PRINCIPAL_KVNO NP_(" Principal (kvno)", "")
62 #define COL_CACHENAME NP_(" 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
, N_(">>>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
)
128 krb5_timeofday (context
, &sec
);
130 ret
= krb5_unparse_name(context
, cred
->server
, &str
);
133 printf(N_("Server: %s\n", ""), str
);
136 ret
= krb5_unparse_name(context
, cred
->client
, &str
);
139 printf(N_("Client: %s\n", ""), str
);
147 decode_Ticket(cred
->ticket
.data
, cred
->ticket
.length
, &t
, &len
);
148 ret
= krb5_enctype_to_string(context
, t
.enc_part
.etype
, &s
);
149 printf(N_("Ticket etype: ", ""));
154 printf(N_("unknown-enctype(%d)", ""), t
.enc_part
.etype
);
157 printf(N_(", kvno %d", ""), *t
.enc_part
.kvno
);
159 if(cred
->session
.keytype
!= t
.enc_part
.etype
) {
160 ret
= krb5_enctype_to_string(context
, cred
->session
.keytype
, &str
);
162 krb5_warn(context
, ret
, "session keytype");
164 printf(N_("Session key: %s\n", "enctype"), str
);
169 printf(N_("Ticket length: %lu\n", ""),
170 (unsigned long)cred
->ticket
.length
);
172 printf(N_("Auth time: %s\n", ""),
173 printable_time_long(cred
->times
.authtime
));
174 if(cred
->times
.authtime
!= cred
->times
.starttime
)
175 printf(N_("Start time: %s\n", ""),
176 printable_time_long(cred
->times
.starttime
));
177 printf(N_("End time: %s", ""),
178 printable_time_long(cred
->times
.endtime
));
179 if(sec
> cred
->times
.endtime
)
180 printf(N_(" (expired)", ""));
182 if(cred
->flags
.b
.renewable
)
183 printf(N_("Renew till: %s\n", ""),
184 printable_time_long(cred
->times
.renew_till
));
187 unparse_flags(TicketFlags2int(cred
->flags
.b
),
188 asn1_TicketFlags_units(),
189 flags
, sizeof(flags
));
190 printf(N_("Ticket flags: %s\n", ""), flags
);
192 printf(N_("Addresses: ", ""));
193 if (cred
->addresses
.len
!= 0) {
194 for(j
= 0; j
< cred
->addresses
.len
; j
++){
198 ret
= krb5_print_address(&cred
->addresses
.val
[j
],
199 buf
, sizeof(buf
), &len
);
205 printf(N_("addressless", ""));
211 * Print all tickets in `ccache' on stdout, verbosily iff do_verbose.
215 print_tickets (krb5_context context
,
217 krb5_principal principal
,
224 krb5_cc_cursor cursor
;
230 ret
= krb5_unparse_name (context
, principal
, &str
);
232 krb5_err (context
, 1, ret
, "krb5_unparse_name");
234 printf ("%17s: %s:%s\n",
235 N_("Credentials cache", ""),
236 krb5_cc_get_type(context
, ccache
),
237 krb5_cc_get_name(context
, ccache
));
238 printf ("%17s: %s\n", N_("Principal", ""), str
);
240 ret
= krb5_cc_get_friendly_name(context
, ccache
, &name
);
242 if (strcmp(name
, str
) != 0)
243 printf ("%17s: %s\n", N_("Friendly name", ""), name
);
249 printf ("%17s: %d\n", N_("Cache version", ""),
250 krb5_cc_get_version(context
, ccache
));
252 krb5_get_kdc_sec_offset(context
, &sec
, &usec
);
254 if (do_verbose
&& sec
!= 0) {
266 unparse_time (val
, buf
, sizeof(buf
));
268 printf ("%17s: %s%s\n", N_("KDC time offset", ""),
269 sig
== -1 ? "-" : "", buf
);
274 ret
= krb5_cc_start_seq_get (context
, ccache
, &cursor
);
276 krb5_err(context
, 1, ret
, "krb5_cc_start_seq_get");
280 rtbl_add_column(ct
, COL_ISSUED
, 0);
281 rtbl_add_column(ct
, COL_EXPIRES
, 0);
283 rtbl_add_column(ct
, COL_FLAGS
, 0);
284 rtbl_add_column(ct
, COL_PRINCIPAL
, 0);
285 rtbl_set_separator(ct
, " ");
287 while ((ret
= krb5_cc_next_cred (context
,
291 if (!do_hidden
&& krb5_is_config_principal(context
, creds
.server
)) {
293 }else if(do_verbose
){
294 print_cred_verbose(context
, &creds
);
296 print_cred(context
, &creds
, ct
, do_flags
);
298 krb5_free_cred_contents (context
, &creds
);
300 if(ret
!= KRB5_CC_END
)
301 krb5_err(context
, 1, ret
, "krb5_cc_get_next");
302 ret
= krb5_cc_end_seq_get (context
, ccache
, &cursor
);
304 krb5_err (context
, 1, ret
, "krb5_cc_end_seq_get");
306 rtbl_format(ct
, stdout
);
312 * Check if there's a tgt for the realm of `principal' and ccache and
313 * if so return 0, else 1
317 check_for_tgt (krb5_context context
,
319 krb5_principal principal
,
325 krb5_const_realm client_realm
;
328 krb5_cc_clear_mcred(&pattern
);
330 client_realm
= krb5_principal_get_realm(context
, principal
);
332 ret
= krb5_make_principal (context
, &pattern
.server
,
333 client_realm
, KRB5_TGS_NAME
, client_realm
, NULL
);
335 krb5_err (context
, 1, ret
, "krb5_make_principal");
336 pattern
.client
= principal
;
338 ret
= krb5_cc_retrieve_cred (context
, ccache
, 0, &pattern
, &creds
);
339 krb5_free_principal (context
, pattern
.server
);
341 if (ret
== KRB5_CC_END
)
343 krb5_err (context
, 1, ret
, "krb5_cc_retrieve_cred");
346 expired
= time(NULL
) > creds
.times
.endtime
;
349 *expiration
= creds
.times
.endtime
;
351 krb5_free_cred_contents (context
, &creds
);
357 * Print a list of all AFS tokens
363 display_tokens(int do_verbose
)
366 unsigned char t
[4096];
367 struct ViceIoctl parms
;
369 parms
.in
= (void *)&i
;
370 parms
.in_size
= sizeof(i
);
371 parms
.out
= (void *)t
;
372 parms
.out_size
= sizeof(t
);
375 int32_t size_secret_tok
, size_public_tok
;
377 struct ClearToken ct
;
378 unsigned char *r
= t
;
380 char buf1
[20], buf2
[20];
382 if(k_pioctl(NULL
, VIOCGETTOK
, &parms
, 0) < 0) {
387 if(parms
.out_size
> sizeof(t
))
389 if(parms
.out_size
< sizeof(size_secret_tok
))
391 t
[min(parms
.out_size
,sizeof(t
)-1)] = 0;
392 memcpy(&size_secret_tok
, r
, sizeof(size_secret_tok
));
393 /* dont bother about the secret token */
394 r
+= size_secret_tok
+ sizeof(size_secret_tok
);
395 if (parms
.out_size
< (r
- t
) + sizeof(size_public_tok
))
397 memcpy(&size_public_tok
, r
, sizeof(size_public_tok
));
398 r
+= sizeof(size_public_tok
);
399 if (parms
.out_size
< (r
- t
) + size_public_tok
+ sizeof(int32_t))
401 memcpy(&ct
, r
, size_public_tok
);
402 r
+= size_public_tok
;
403 /* there is a int32_t with length of cellname, but we dont read it */
404 r
+= sizeof(int32_t);
407 gettimeofday (&tv
, NULL
);
408 strlcpy (buf1
, printable_time(ct
.BeginTimestamp
),
410 if (do_verbose
|| tv
.tv_sec
< ct
.EndTimestamp
)
411 strlcpy (buf2
, printable_time(ct
.EndTimestamp
),
414 strlcpy (buf2
, N_(">>> Expired <<<", ""), sizeof(buf2
));
416 printf("%s %s ", buf1
, buf2
);
418 if ((ct
.EndTimestamp
- ct
.BeginTimestamp
) & 1)
419 printf(N_("User's (AFS ID %d) tokens for %s", ""), ct
.ViceId
, cell
);
421 printf(N_("Tokens for %s", ""), cell
);
423 printf(" (%d)", ct
.AuthHandle
);
430 * display the ccache in `cred_cache'
434 display_v5_ccache (const char *cred_cache
, int do_test
, int do_verbose
,
435 int do_flags
, int do_hidden
)
438 krb5_context context
;
440 krb5_principal principal
;
443 ret
= krb5_init_context (&context
);
445 errx (1, "krb5_init_context failed: %d", ret
);
448 ret
= krb5_cc_resolve(context
, cred_cache
, &ccache
);
450 krb5_err (context
, 1, ret
, "%s", cred_cache
);
452 ret
= krb5_cc_default (context
, &ccache
);
454 krb5_err (context
, 1, ret
, "krb5_cc_resolve");
457 ret
= krb5_cc_get_principal (context
, ccache
, &principal
);
461 krb5_warnx(context
, N_("No ticket file: %s", ""),
462 krb5_cc_get_name(context
, ccache
));
465 krb5_err (context
, 1, ret
, "krb5_cc_get_principal");
468 exit_status
= check_for_tgt (context
, ccache
, principal
, NULL
);
470 print_tickets (context
, ccache
, principal
, do_verbose
,
471 do_flags
, do_hidden
);
473 ret
= krb5_cc_close (context
, ccache
);
475 krb5_err (context
, 1, ret
, "krb5_cc_close");
477 krb5_free_principal (context
, principal
);
478 krb5_free_context (context
);
489 krb5_cc_cache_cursor cursor
;
490 krb5_context context
;
495 ret
= krb5_init_context (&context
);
497 errx (1, "krb5_init_context failed: %d", ret
);
499 ret
= krb5_cc_cache_get_first (context
, NULL
, &cursor
);
500 if (ret
== KRB5_CC_NOSUPP
)
503 krb5_err (context
, 1, ret
, "krb5_cc_cache_get_first");
506 rtbl_add_column(ct
, COL_NAME
, 0);
507 rtbl_add_column(ct
, COL_CACHENAME
, 0);
508 rtbl_add_column(ct
, COL_EXPIRES
, 0);
509 rtbl_set_prefix(ct
, " ");
510 rtbl_set_column_prefix(ct
, COL_NAME
, "");
512 while (krb5_cc_cache_next (context
, cursor
, &id
) == 0) {
513 krb5_principal principal
= NULL
;
518 ret
= krb5_cc_get_principal(context
, id
, &principal
);
522 expired
= check_for_tgt (context
, id
, principal
, &t
);
524 ret
= krb5_cc_get_friendly_name(context
, id
, &name
);
527 rtbl_add_column_entry(ct
, COL_NAME
, name
);
528 rtbl_add_column_entry(ct
, COL_CACHENAME
,
529 krb5_cc_get_name(context
, id
));
531 str
= N_(">>> Expired <<<", "");
533 str
= printable_time(t
);
534 rtbl_add_column_entry(ct
, COL_EXPIRES
, str
);
537 krb5_cc_close(context
, id
);
539 krb5_free_principal(context
, principal
);
542 krb5_cc_cache_end_seq_get(context
, cursor
);
544 rtbl_format(ct
, stdout
);
554 static int version_flag
= 0;
555 static int help_flag
= 0;
556 static int do_verbose
= 0;
557 static int do_list_caches
= 0;
558 static int do_test
= 0;
560 static int do_tokens
= 0;
562 static int do_v5
= 1;
563 static char *cred_cache
;
564 static int do_flags
= 0;
565 static int do_hidden
= 0;
567 static struct getargs args
[] = {
568 { NULL
, 'f', arg_flag
, &do_flags
},
569 { "cache", 'c', arg_string
, &cred_cache
,
570 NP_("credentials cache to list", ""), "cache" },
571 { "test", 't', arg_flag
, &do_test
,
572 NP_("test for having tickets", ""), NULL
},
573 { NULL
, 's', arg_flag
, &do_test
},
575 { "tokens", 'T', arg_flag
, &do_tokens
,
576 NP_("display AFS tokens", ""), NULL
},
578 { "v5", '5', arg_flag
, &do_v5
,
579 NP_("display v5 cred cache", ""), NULL
},
580 { "list-caches", 'l', arg_flag
, &do_list_caches
,
581 NP_("verbose output", ""), NULL
},
582 { "verbose", 'v', arg_flag
, &do_verbose
,
583 NP_("verbose output", ""), NULL
},
584 { "hidden", 0, arg_flag
, &do_hidden
,
585 NP_("display hidden credentials", ""), NULL
},
586 { NULL
, 'a', arg_flag
, &do_verbose
},
587 { NULL
, 'n', arg_flag
, &do_verbose
},
588 { "version", 0, arg_flag
, &version_flag
,
589 NP_("print version", ""), NULL
},
590 { "help", 0, arg_flag
, &help_flag
,
597 arg_printusage_i18n (args
,
598 sizeof(args
)/sizeof(*args
),
607 main (int argc
, char **argv
)
612 setprogname (argv
[0]);
614 setlocale (LC_ALL
, "");
615 bindtextdomain ("heimdal_kuser", HEIMDAL_LOCALEDIR
);
616 textdomain("heimdal_kuser");
618 if(getarg(args
, sizeof(args
) / sizeof(args
[0]), argc
, argv
, &optidx
))
634 if (do_list_caches
) {
635 exit_status
= list_caches();
640 exit_status
= display_v5_ccache (cred_cache
, do_test
,
641 do_verbose
, do_flags
, do_hidden
);
645 if (do_tokens
&& k_hasafs ()) {
648 display_tokens (do_verbose
);