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"
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 NP_(" Issued","")
59 #define COL_EXPIRES NP_(" Expires", "")
60 #define COL_FLAGS NP_("Flags", "")
61 #define COL_NAME NP_(" Name", "")
62 #define COL_PRINCIPAL NP_(" Principal", "")
63 #define COL_PRINCIPAL_KVNO NP_(" Principal (kvno)", "")
64 #define COL_CACHENAME NP_(" Cache name", "")
67 print_cred(krb5_context context
, krb5_creds
*cred
, rtbl_t ct
, int do_flags
)
73 krb5_timeofday (context
, &sec
);
76 if(cred
->times
.starttime
)
77 rtbl_add_column_entry(ct
, COL_ISSUED
,
78 printable_time(cred
->times
.starttime
));
80 rtbl_add_column_entry(ct
, COL_ISSUED
,
81 printable_time(cred
->times
.authtime
));
83 if(cred
->times
.endtime
> sec
)
84 rtbl_add_column_entry(ct
, COL_EXPIRES
,
85 printable_time(cred
->times
.endtime
));
87 rtbl_add_column_entry(ct
, COL_EXPIRES
, N_(">>>Expired<<<", ""));
88 ret
= krb5_unparse_name (context
, cred
->server
, &str
);
90 krb5_err(context
, 1, ret
, "krb5_unparse_name");
91 rtbl_add_column_entry(ct
, COL_PRINCIPAL
, str
);
94 if(cred
->flags
.b
.forwardable
)
96 if(cred
->flags
.b
.forwarded
)
98 if(cred
->flags
.b
.proxiable
)
100 if(cred
->flags
.b
.proxy
)
102 if(cred
->flags
.b
.may_postdate
)
104 if(cred
->flags
.b
.postdated
)
106 if(cred
->flags
.b
.renewable
)
108 if(cred
->flags
.b
.initial
)
110 if(cred
->flags
.b
.invalid
)
112 if(cred
->flags
.b
.pre_authent
)
114 if(cred
->flags
.b
.hw_authent
)
117 rtbl_add_column_entry(ct
, COL_FLAGS
, s
);
123 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(N_("Server: %s\n", ""), str
);
138 ret
= krb5_unparse_name(context
, cred
->client
, &str
);
141 printf(N_("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(N_("Ticket etype: ", ""));
156 printf(N_("unknown-enctype(%d)", ""), t
.enc_part
.etype
);
159 printf(N_(", 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(N_("Session key: %s\n", "enctype"), str
);
171 printf(N_("Ticket length: %lu\n", ""),
172 (unsigned long)cred
->ticket
.length
);
174 printf(N_("Auth time: %s\n", ""),
175 printable_time_long(cred
->times
.authtime
));
176 if(cred
->times
.authtime
!= cred
->times
.starttime
)
177 printf(N_("Start time: %s\n", ""),
178 printable_time_long(cred
->times
.starttime
));
179 printf(N_("End time: %s", ""),
180 printable_time_long(cred
->times
.endtime
));
181 if(sec
> cred
->times
.endtime
)
182 printf(N_(" (expired)", ""));
184 if(cred
->flags
.b
.renewable
)
185 printf(N_("Renew till: %s\n", ""),
186 printable_time_long(cred
->times
.renew_till
));
189 unparse_flags(TicketFlags2int(cred
->flags
.b
),
190 asn1_TicketFlags_units(),
191 flags
, sizeof(flags
));
192 printf(N_("Ticket flags: %s\n", ""), flags
);
194 printf(N_("Addresses: ", ""));
195 if (cred
->addresses
.len
!= 0) {
196 for(j
= 0; j
< cred
->addresses
.len
; j
++){
200 ret
= krb5_print_address(&cred
->addresses
.val
[j
],
201 buf
, sizeof(buf
), &len
);
207 printf(N_("addressless", ""));
213 * Print all tickets in `ccache' on stdout, verbosily iff do_verbose.
217 print_tickets (krb5_context context
,
219 krb5_principal principal
,
226 krb5_cc_cursor cursor
;
232 ret
= krb5_unparse_name (context
, principal
, &str
);
234 krb5_err (context
, 1, ret
, "krb5_unparse_name");
236 printf ("%17s: %s:%s\n",
237 N_("Credentials cache", ""),
238 krb5_cc_get_type(context
, ccache
),
239 krb5_cc_get_name(context
, ccache
));
240 printf ("%17s: %s\n", N_("Principal", ""), str
);
242 ret
= krb5_cc_get_friendly_name(context
, ccache
, &name
);
244 if (strcmp(name
, str
) != 0)
245 printf ("%17s: %s\n", N_("Friendly name", ""), name
);
251 printf ("%17s: %d\n", 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", 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
,
293 if (!do_hidden
&& krb5_is_config_principal(context
, creds
.server
)) {
295 }else if(do_verbose
){
296 print_cred_verbose(context
, &creds
);
298 print_cred(context
, &creds
, ct
, do_flags
);
300 krb5_free_cred_contents (context
, &creds
);
302 if(ret
!= KRB5_CC_END
)
303 krb5_err(context
, 1, ret
, "krb5_cc_get_next");
304 ret
= krb5_cc_end_seq_get (context
, ccache
, &cursor
);
306 krb5_err (context
, 1, ret
, "krb5_cc_end_seq_get");
308 rtbl_format(ct
, stdout
);
314 * Check if there's a tgt for the realm of `principal' and ccache and
315 * if so return 0, else 1
319 check_for_tgt (krb5_context context
,
321 krb5_principal principal
,
327 krb5_const_realm client_realm
;
330 krb5_cc_clear_mcred(&pattern
);
332 client_realm
= krb5_principal_get_realm(context
, principal
);
334 ret
= krb5_make_principal (context
, &pattern
.server
,
335 client_realm
, KRB5_TGS_NAME
, client_realm
, NULL
);
337 krb5_err (context
, 1, ret
, "krb5_make_principal");
338 pattern
.client
= principal
;
340 ret
= krb5_cc_retrieve_cred (context
, ccache
, 0, &pattern
, &creds
);
341 krb5_free_principal (context
, pattern
.server
);
343 if (ret
== KRB5_CC_END
)
345 krb5_err (context
, 1, ret
, "krb5_cc_retrieve_cred");
348 expired
= time(NULL
) > creds
.times
.endtime
;
351 *expiration
= creds
.times
.endtime
;
353 krb5_free_cred_contents (context
, &creds
);
359 * Print a list of all AFS tokens
365 display_tokens(int do_verbose
)
368 unsigned char t
[4096];
369 struct ViceIoctl parms
;
371 parms
.in
= (void *)&i
;
372 parms
.in_size
= sizeof(i
);
373 parms
.out
= (void *)t
;
374 parms
.out_size
= sizeof(t
);
377 int32_t size_secret_tok
, size_public_tok
;
379 struct ClearToken ct
;
380 unsigned char *r
= t
;
382 char buf1
[20], buf2
[20];
384 if(k_pioctl(NULL
, VIOCGETTOK
, &parms
, 0) < 0) {
389 if(parms
.out_size
> sizeof(t
))
391 if(parms
.out_size
< sizeof(size_secret_tok
))
393 t
[min(parms
.out_size
,sizeof(t
)-1)] = 0;
394 memcpy(&size_secret_tok
, r
, sizeof(size_secret_tok
));
395 /* dont bother about the secret token */
396 r
+= size_secret_tok
+ sizeof(size_secret_tok
);
397 if (parms
.out_size
< (r
- t
) + sizeof(size_public_tok
))
399 memcpy(&size_public_tok
, r
, sizeof(size_public_tok
));
400 r
+= sizeof(size_public_tok
);
401 if (parms
.out_size
< (r
- t
) + size_public_tok
+ sizeof(int32_t))
403 memcpy(&ct
, r
, size_public_tok
);
404 r
+= size_public_tok
;
405 /* there is a int32_t with length of cellname, but we dont read it */
406 r
+= sizeof(int32_t);
409 gettimeofday (&tv
, NULL
);
410 strlcpy (buf1
, printable_time(ct
.BeginTimestamp
),
412 if (do_verbose
|| tv
.tv_sec
< ct
.EndTimestamp
)
413 strlcpy (buf2
, printable_time(ct
.EndTimestamp
),
416 strlcpy (buf2
, N_(">>> Expired <<<", ""), sizeof(buf2
));
418 printf("%s %s ", buf1
, buf2
);
420 if ((ct
.EndTimestamp
- ct
.BeginTimestamp
) & 1)
421 printf(N_("User's (AFS ID %d) tokens for %s", ""), ct
.ViceId
, cell
);
423 printf(N_("Tokens for %s", ""), cell
);
425 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
, N_("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_NAME
, 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_NAME
, "");
514 while (krb5_cc_cache_next (context
, cursor
, &id
) == 0) {
515 krb5_principal principal
= NULL
;
520 ret
= krb5_cc_get_principal(context
, id
, &principal
);
524 expired
= check_for_tgt (context
, id
, principal
, &t
);
526 ret
= krb5_cc_get_friendly_name(context
, id
, &name
);
529 rtbl_add_column_entry(ct
, COL_NAME
, name
);
530 rtbl_add_column_entry(ct
, COL_CACHENAME
,
531 krb5_cc_get_name(context
, id
));
533 str
= N_(">>> Expired <<<", "");
535 str
= printable_time(t
);
536 rtbl_add_column_entry(ct
, COL_EXPIRES
, str
);
539 krb5_cc_close(context
, id
);
541 krb5_free_principal(context
, principal
);
544 krb5_cc_cache_end_seq_get(context
, cursor
);
546 rtbl_format(ct
, stdout
);
556 static int version_flag
= 0;
557 static int help_flag
= 0;
558 static int do_verbose
= 0;
559 static int do_list_caches
= 0;
560 static int do_test
= 0;
562 static int do_tokens
= 0;
564 static int do_v5
= 1;
565 static char *cred_cache
;
566 static int do_flags
= 0;
567 static int do_hidden
= 0;
569 static struct getargs args
[] = {
570 { NULL
, 'f', arg_flag
, &do_flags
},
571 { "cache", 'c', arg_string
, &cred_cache
,
572 NP_("credentials cache to list", ""), "cache" },
573 { "test", 't', arg_flag
, &do_test
,
574 NP_("test for having tickets", ""), NULL
},
575 { NULL
, 's', arg_flag
, &do_test
},
577 { "tokens", 'T', arg_flag
, &do_tokens
,
578 NP_("display AFS tokens", ""), NULL
},
580 { "v5", '5', arg_flag
, &do_v5
,
581 NP_("display v5 cred cache", ""), NULL
},
582 { "list-caches", 'l', arg_flag
, &do_list_caches
,
583 NP_("verbose output", ""), NULL
},
584 { "verbose", 'v', arg_flag
, &do_verbose
,
585 NP_("verbose output", ""), NULL
},
586 { "hidden", 0, arg_flag
, &do_hidden
,
587 NP_("display hidden credentials", ""), NULL
},
588 { NULL
, 'a', arg_flag
, &do_verbose
},
589 { NULL
, 'n', arg_flag
, &do_verbose
},
590 { "version", 0, arg_flag
, &version_flag
,
591 NP_("print version", ""), NULL
},
592 { "help", 0, arg_flag
, &help_flag
,
599 arg_printusage_i18n (args
,
600 sizeof(args
)/sizeof(*args
),
609 main (int argc
, char **argv
)
614 setprogname (argv
[0]);
616 setlocale (LC_ALL
, "");
617 bindtextdomain ("heimdal_kuser", HEIMDAL_LOCALEDIR
);
618 textdomain("heimdal_kuser");
620 if(getarg(args
, sizeof(args
) / sizeof(args
[0]), argc
, argv
, &optidx
))
636 if (do_list_caches
) {
637 exit_status
= list_caches();
642 exit_status
= display_v5_ccache (cred_cache
, do_test
,
643 do_verbose
, do_flags
, do_hidden
);
647 if (do_tokens
&& k_hasafs ()) {
650 display_tokens (do_verbose
);