xref: /illumos-gate/usr/src/cmd/krb5/klist/klist.c (revision 55fea89d)
17c478bd9Sstevel@tonic-gate /*
2a49a392fSps  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate 
67c478bd9Sstevel@tonic-gate /*
77c478bd9Sstevel@tonic-gate  * clients/klist/klist.c
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * Copyright 1990 by the Massachusetts Institute of Technology.
107c478bd9Sstevel@tonic-gate  * All Rights Reserved.
117c478bd9Sstevel@tonic-gate  *
127c478bd9Sstevel@tonic-gate  * Export of this software from the United States of America may
137c478bd9Sstevel@tonic-gate  *   require a specific license from the United States Government.
147c478bd9Sstevel@tonic-gate  *   It is the responsibility of any person or organization contemplating
157c478bd9Sstevel@tonic-gate  *   export to obtain such a license before exporting.
16*55fea89dSDan Cross  *
177c478bd9Sstevel@tonic-gate  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
187c478bd9Sstevel@tonic-gate  * distribute this software and its documentation for any purpose and
197c478bd9Sstevel@tonic-gate  * without fee is hereby granted, provided that the above copyright
207c478bd9Sstevel@tonic-gate  * notice appear in all copies and that both that copyright notice and
217c478bd9Sstevel@tonic-gate  * this permission notice appear in supporting documentation, and that
227c478bd9Sstevel@tonic-gate  * the name of M.I.T. not be used in advertising or publicity pertaining
237c478bd9Sstevel@tonic-gate  * to distribution of the software without specific, written prior
247c478bd9Sstevel@tonic-gate  * permission.  Furthermore if you modify this software you must label
257c478bd9Sstevel@tonic-gate  * your software as modified software and not distribute it in such a
267c478bd9Sstevel@tonic-gate  * fashion that it might be confused with the original M.I.T. software.
277c478bd9Sstevel@tonic-gate  * M.I.T. makes no representations about the suitability of
287c478bd9Sstevel@tonic-gate  * this software for any purpose.  It is provided "as is" without express
297c478bd9Sstevel@tonic-gate  * or implied warranty.
30*55fea89dSDan Cross  *
317c478bd9Sstevel@tonic-gate  *
327c478bd9Sstevel@tonic-gate  * List out the contents of your credential cache or keytab.
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include <k5-int.h>
367c478bd9Sstevel@tonic-gate #include "com_err.h"
377c478bd9Sstevel@tonic-gate #include <krb5.h>
387c478bd9Sstevel@tonic-gate #ifdef KRB5_KRB4_COMPAT
397c478bd9Sstevel@tonic-gate #include <kerberosIV/krb.h>
407c478bd9Sstevel@tonic-gate #endif /* KRB5_KRB4_COMPAT */
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #include <stdlib.h>
437c478bd9Sstevel@tonic-gate #include <string.h>
447c478bd9Sstevel@tonic-gate #include <stdio.h>
457c478bd9Sstevel@tonic-gate #include <time.h>
467c478bd9Sstevel@tonic-gate #include <libintl.h>
477c478bd9Sstevel@tonic-gate #include <locale.h>
487c478bd9Sstevel@tonic-gate #include <netinet/in.h>
4956a424ccSmp #if defined(HAVE_ARPA_INET_H)
507c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
5156a424ccSmp #endif
527c478bd9Sstevel@tonic-gate #include <inet/ip.h>
537c478bd9Sstevel@tonic-gate #include <inet/ip6.h>
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate #ifndef _WIN32
56*55fea89dSDan Cross #define GET_PROGNAME(x) (strrchr((x), '/') ? strrchr((x), '/')+1 : (x))
577c478bd9Sstevel@tonic-gate #else
587c478bd9Sstevel@tonic-gate #define GET_PROGNAME(x) max(max(strrchr((x), '/'), strrchr((x), '\\')) + 1,(x))
597c478bd9Sstevel@tonic-gate #endif /* _WIN32 */
607c478bd9Sstevel@tonic-gate 
6156a424ccSmp #ifndef _WIN32
627c478bd9Sstevel@tonic-gate #include <sys/socket.h>
637c478bd9Sstevel@tonic-gate #include <netdb.h>
6456a424ccSmp #endif
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate extern int optind;
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate int show_flags = 0, show_time = 0, status_only = 0, show_keys = 0;
697c478bd9Sstevel@tonic-gate int show_etype = 0, show_addresses = 0, no_resolve = 0;
707c478bd9Sstevel@tonic-gate char *defname;
717c478bd9Sstevel@tonic-gate char *progname;
727c478bd9Sstevel@tonic-gate krb5_int32 now;
737c478bd9Sstevel@tonic-gate size_t timestamp_width;
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate krb5_context kcontext;
767c478bd9Sstevel@tonic-gate 
77505d05c7Sgtb char * etype_string (krb5_enctype );
7856a424ccSmp void show_credential (krb5_creds *);
79*55fea89dSDan Cross 
80505d05c7Sgtb void do_ccache (char *);
81505d05c7Sgtb void do_keytab (char *);
82505d05c7Sgtb void printtime (time_t);
83505d05c7Sgtb void one_addr (krb5_address *);
8456a424ccSmp void fillit (FILE *, unsigned int, int);
857c478bd9Sstevel@tonic-gate void show_addr(krb5_address *a);
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate #ifdef KRB5_KRB4_COMPAT
88505d05c7Sgtb void do_v4_ccache (char *);
897c478bd9Sstevel@tonic-gate #endif /* KRB5_KRB4_COMPAT */
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate #define DEFAULT 0
927c478bd9Sstevel@tonic-gate #define CCACHE 1
937c478bd9Sstevel@tonic-gate #define KEYTAB 2
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate /*
967c478bd9Sstevel@tonic-gate  * The reason we start out with got_k4 and got_k5 as zero (false) is
977c478bd9Sstevel@tonic-gate  * so that we can easily add dynamic loading support for determining
987c478bd9Sstevel@tonic-gate  * whether Kerberos 4 and Keberos 5 libraries are available
997c478bd9Sstevel@tonic-gate  */
1007c478bd9Sstevel@tonic-gate 
101*55fea89dSDan Cross static int got_k5 = 0;
1027c478bd9Sstevel@tonic-gate static int got_k4 = 0;
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate static int default_k5 = 1;
1057c478bd9Sstevel@tonic-gate #ifdef KRB5_KRB4_COMPAT
1067c478bd9Sstevel@tonic-gate static int default_k4 = 1;
1077c478bd9Sstevel@tonic-gate #else  /* KRB5_KRB4_COMPAT */
1087c478bd9Sstevel@tonic-gate static int default_k4 = 0;
1097c478bd9Sstevel@tonic-gate #endif /* KRB5_KRB4_COMPAT */
1107c478bd9Sstevel@tonic-gate 
usage()11156a424ccSmp static void usage()
1127c478bd9Sstevel@tonic-gate {
1137c478bd9Sstevel@tonic-gate #define KRB_AVAIL_STRING(x) ((x)?gettext("available"):gettext("not available"))
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate     fprintf(stderr, gettext("Usage: %s [-5] [-4] [-e]"
1167c478bd9Sstevel@tonic-gate                     " [[-c] [-f] [-s] [-a [-n]]] "
117*55fea89dSDan Cross 	            "[-k [-t] [-K]] [name]\n"), progname);
1187c478bd9Sstevel@tonic-gate     fprintf(stderr, "\t-5 Kerberos 5 (%s)\n", KRB_AVAIL_STRING(got_k5));
1197c478bd9Sstevel@tonic-gate     fprintf(stderr, "\t-4 Kerberos 4 (%s)\n", KRB_AVAIL_STRING(got_k4));
1207c478bd9Sstevel@tonic-gate     fprintf(stderr, gettext("\t   (Default is %s%s%s%s)\n"),
1217c478bd9Sstevel@tonic-gate 	    default_k5?"Kerberos 5":"",
1227c478bd9Sstevel@tonic-gate 	    (default_k5 && default_k4)?gettext(" and "):"",
1237c478bd9Sstevel@tonic-gate 	    default_k4?"Kerberos 4":"",
1247c478bd9Sstevel@tonic-gate 	    (!default_k5 && !default_k4)?gettext("neither"):"");
1257c478bd9Sstevel@tonic-gate     fprintf(stderr, gettext("\t-c specifies credentials cache\n"));
1267c478bd9Sstevel@tonic-gate     fprintf(stderr, gettext("\t-k specifies keytab\n"));
1277c478bd9Sstevel@tonic-gate     fprintf(stderr, gettext("\t   (Default is credentials cache)\n"));
1287c478bd9Sstevel@tonic-gate     fprintf(stderr, gettext("\t-e shows the encryption type\n"));
1297c478bd9Sstevel@tonic-gate     fprintf(stderr, gettext("\toptions for credential caches:\n"));
1307c478bd9Sstevel@tonic-gate     fprintf(stderr, gettext("\t\t-f shows credentials flags\n"));
1317c478bd9Sstevel@tonic-gate     fprintf(stderr, gettext("\t\t-s sets exit status based on valid tgt existence\n"));
1327c478bd9Sstevel@tonic-gate     fprintf(stderr, gettext("\t\t-a displays the address list\n"));
1337c478bd9Sstevel@tonic-gate     fprintf(stderr, gettext("\t\t-n do not reverse-resolve\n"));
1347c478bd9Sstevel@tonic-gate     fprintf(stderr, gettext("\toptions for keytabs:\n"));
1357c478bd9Sstevel@tonic-gate     fprintf(stderr, gettext("\t\t-t shows keytab entry timestamps\n"));
1367c478bd9Sstevel@tonic-gate     fprintf(stderr, gettext("\t\t-K shows keytab entry DES keys\n"));
1377c478bd9Sstevel@tonic-gate     exit(1);
1387c478bd9Sstevel@tonic-gate }
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate int
main(argc,argv)14256a424ccSmp main(argc, argv)
14356a424ccSmp     int argc;
14456a424ccSmp     char **argv;
1457c478bd9Sstevel@tonic-gate {
1467c478bd9Sstevel@tonic-gate     int c;
1477c478bd9Sstevel@tonic-gate     char *name;
1487c478bd9Sstevel@tonic-gate     int mode;
1497c478bd9Sstevel@tonic-gate     int use_k5 = 0, use_k4 = 0;
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate     got_k5 = 1;
1527c478bd9Sstevel@tonic-gate #ifdef KRB5_KRB4_COMPAT
1537c478bd9Sstevel@tonic-gate     got_k4 = 1;
1547c478bd9Sstevel@tonic-gate #endif /* KRB5_KRB4_COMPAT */
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate     (void) setlocale(LC_ALL, "");
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
1597c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
1607c478bd9Sstevel@tonic-gate #endif /* !TEXT_DOMAIN */
1617c478bd9Sstevel@tonic-gate 
162*55fea89dSDan Cross     (void) textdomain(TEXT_DOMAIN);
1637c478bd9Sstevel@tonic-gate 
164*55fea89dSDan Cross     progname = GET_PROGNAME(argv[0]);
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate     name = NULL;
1677c478bd9Sstevel@tonic-gate     mode = DEFAULT;
1687c478bd9Sstevel@tonic-gate     while ((c = getopt(argc, argv, "fetKsnack45")) != -1) {
1697c478bd9Sstevel@tonic-gate 	switch (c) {
1707c478bd9Sstevel@tonic-gate 	case 'f':
1717c478bd9Sstevel@tonic-gate 	    show_flags = 1;
1727c478bd9Sstevel@tonic-gate 	    break;
1737c478bd9Sstevel@tonic-gate 	case 'e':
1747c478bd9Sstevel@tonic-gate 	    show_etype = 1;
1757c478bd9Sstevel@tonic-gate 	    break;
1767c478bd9Sstevel@tonic-gate 	case 't':
1777c478bd9Sstevel@tonic-gate 	    show_time = 1;
1787c478bd9Sstevel@tonic-gate 	    break;
1797c478bd9Sstevel@tonic-gate 	case 'K':
1807c478bd9Sstevel@tonic-gate 	    show_keys = 1;
1817c478bd9Sstevel@tonic-gate 	    break;
1827c478bd9Sstevel@tonic-gate 	case 's':
1837c478bd9Sstevel@tonic-gate 	    status_only = 1;
1847c478bd9Sstevel@tonic-gate 	    break;
1857c478bd9Sstevel@tonic-gate 	case 'n':
1867c478bd9Sstevel@tonic-gate 	    no_resolve = 1;
1877c478bd9Sstevel@tonic-gate 	    break;
1887c478bd9Sstevel@tonic-gate 	case 'a':
1897c478bd9Sstevel@tonic-gate 	    show_addresses = 1;
1907c478bd9Sstevel@tonic-gate 	    break;
1917c478bd9Sstevel@tonic-gate 	case 'c':
1927c478bd9Sstevel@tonic-gate 	    if (mode != DEFAULT) usage();
1937c478bd9Sstevel@tonic-gate 	    mode = CCACHE;
1947c478bd9Sstevel@tonic-gate 	    break;
1957c478bd9Sstevel@tonic-gate 	case 'k':
1967c478bd9Sstevel@tonic-gate 	    if (mode != DEFAULT) usage();
1977c478bd9Sstevel@tonic-gate 	    mode = KEYTAB;
1987c478bd9Sstevel@tonic-gate 	    break;
1997c478bd9Sstevel@tonic-gate 	case '4':
2007c478bd9Sstevel@tonic-gate 	    if (!got_k4)
2017c478bd9Sstevel@tonic-gate 	    {
2027c478bd9Sstevel@tonic-gate #ifdef KRB5_KRB4_COMPAT
2037c478bd9Sstevel@tonic-gate 		fprintf(stderr, "Kerberos 4 support could not be loaded\n");
2047c478bd9Sstevel@tonic-gate #else  /* KRB5_KRB4_COMPAT */
2057c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("This was not built with Kerberos 4 support\n"));
2067c478bd9Sstevel@tonic-gate #endif /* KRB5_KRB4_COMPAT */
2077c478bd9Sstevel@tonic-gate 		exit(3);
2087c478bd9Sstevel@tonic-gate 	    }
2097c478bd9Sstevel@tonic-gate 	    use_k4 = 1;
2107c478bd9Sstevel@tonic-gate 	    break;
2117c478bd9Sstevel@tonic-gate 	case '5':
2127c478bd9Sstevel@tonic-gate 	    if (!got_k5)
2137c478bd9Sstevel@tonic-gate 	    {
2147c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("Kerberos 5 support could not be loaded\n"));
2157c478bd9Sstevel@tonic-gate 		exit(3);
2167c478bd9Sstevel@tonic-gate 	    }
2177c478bd9Sstevel@tonic-gate 	    use_k5 = 1;
2187c478bd9Sstevel@tonic-gate 	    break;
2197c478bd9Sstevel@tonic-gate 	default:
2207c478bd9Sstevel@tonic-gate 	    usage();
2217c478bd9Sstevel@tonic-gate 	    break;
2227c478bd9Sstevel@tonic-gate 	}
2237c478bd9Sstevel@tonic-gate     }
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate     if (no_resolve && !show_addresses) {
2267c478bd9Sstevel@tonic-gate 	usage();
2277c478bd9Sstevel@tonic-gate     }
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate     if (mode == DEFAULT || mode == CCACHE) {
2307c478bd9Sstevel@tonic-gate 	if (show_time || show_keys)
2317c478bd9Sstevel@tonic-gate 	    usage();
2327c478bd9Sstevel@tonic-gate     } else {
2337c478bd9Sstevel@tonic-gate 	if (show_flags || status_only || show_addresses)
2347c478bd9Sstevel@tonic-gate 	    usage();
2357c478bd9Sstevel@tonic-gate     }
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate     if (argc - optind > 1) {
2387c478bd9Sstevel@tonic-gate 		fprintf(stderr,
2397c478bd9Sstevel@tonic-gate 			gettext("Extra arguments (starting with \"%s\").\n"),
2407c478bd9Sstevel@tonic-gate 		argv[optind+1]);
2417c478bd9Sstevel@tonic-gate 	usage();
2427c478bd9Sstevel@tonic-gate     }
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate     name = (optind == argc-1) ? argv[optind] : 0;
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate     if (!use_k5 && !use_k4)
2477c478bd9Sstevel@tonic-gate     {
2487c478bd9Sstevel@tonic-gate 	use_k5 = default_k5;
2497c478bd9Sstevel@tonic-gate 	use_k4 = default_k4;
2507c478bd9Sstevel@tonic-gate     }
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate     if (!use_k5)
2537c478bd9Sstevel@tonic-gate 	got_k5 = 0;
2547c478bd9Sstevel@tonic-gate     if (!use_k4)
2557c478bd9Sstevel@tonic-gate 	got_k4 = 0;
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate     now = time(0);
2587c478bd9Sstevel@tonic-gate     {
2597c478bd9Sstevel@tonic-gate 	char tmp[BUFSIZ];
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	if (!krb5_timestamp_to_sfstring(now, tmp, 20, (char *) NULL) ||
262*55fea89dSDan Cross 	    !krb5_timestamp_to_sfstring(now, tmp, sizeof(tmp),
2637c478bd9Sstevel@tonic-gate 					(char *) NULL))
26456a424ccSmp 	    timestamp_width = (int) strlen(tmp);
2657c478bd9Sstevel@tonic-gate 	else
2667c478bd9Sstevel@tonic-gate 	    timestamp_width = 15;
2677c478bd9Sstevel@tonic-gate     }
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate     if (got_k5)
2707c478bd9Sstevel@tonic-gate     {
2717c478bd9Sstevel@tonic-gate 	krb5_error_code retval;
2727c478bd9Sstevel@tonic-gate 	retval = krb5_init_context(&kcontext);
2737c478bd9Sstevel@tonic-gate 	if (retval) {
2747c478bd9Sstevel@tonic-gate 	    com_err(progname, retval, gettext("while initializing krb5"));
2757c478bd9Sstevel@tonic-gate 	    exit(1);
2767c478bd9Sstevel@tonic-gate 	}
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	if (mode == DEFAULT || mode == CCACHE)
2797c478bd9Sstevel@tonic-gate 	    do_ccache(name);
2807c478bd9Sstevel@tonic-gate 	else
2817c478bd9Sstevel@tonic-gate 	    do_keytab(name);
2827c478bd9Sstevel@tonic-gate     } else {
2837c478bd9Sstevel@tonic-gate #ifdef KRB5_KRB4_COMPAT
2847c478bd9Sstevel@tonic-gate 	if (mode == DEFAULT || mode == CCACHE)
2857c478bd9Sstevel@tonic-gate 	    do_v4_ccache(name);
2867c478bd9Sstevel@tonic-gate 	else {
2877c478bd9Sstevel@tonic-gate 	    /* We may want to add v4 srvtab support */
288*55fea89dSDan Cross 	    fprintf(stderr,
289*55fea89dSDan Cross 		    "%s: srvtab option not supported for Kerberos 4\n",
2907c478bd9Sstevel@tonic-gate 		    progname);
2917c478bd9Sstevel@tonic-gate 	    exit(1);
2927c478bd9Sstevel@tonic-gate 	}
2937c478bd9Sstevel@tonic-gate #endif /* KRB4_KRB5_COMPAT */
2947c478bd9Sstevel@tonic-gate     }
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate     return 0;
297*55fea89dSDan Cross }
2987c478bd9Sstevel@tonic-gate 
do_keytab(name)2997c478bd9Sstevel@tonic-gate void do_keytab(name)
3007c478bd9Sstevel@tonic-gate    char *name;
3017c478bd9Sstevel@tonic-gate {
3027c478bd9Sstevel@tonic-gate      krb5_keytab kt;
3037c478bd9Sstevel@tonic-gate      krb5_keytab_entry entry;
3047c478bd9Sstevel@tonic-gate      krb5_kt_cursor cursor;
3057c478bd9Sstevel@tonic-gate      char buf[BUFSIZ]; /* hopefully large enough for any type */
3067c478bd9Sstevel@tonic-gate      char *pname;
3077c478bd9Sstevel@tonic-gate      int code;
308*55fea89dSDan Cross 
3097c478bd9Sstevel@tonic-gate      if (name == NULL) {
3107c478bd9Sstevel@tonic-gate 	  if ((code = krb5_kt_default(kcontext, &kt))) {
3117c478bd9Sstevel@tonic-gate 			com_err(progname, code,
3127c478bd9Sstevel@tonic-gate 				gettext("while getting default keytab"));
3137c478bd9Sstevel@tonic-gate 	       exit(1);
3147c478bd9Sstevel@tonic-gate 	  }
3157c478bd9Sstevel@tonic-gate      } else {
3167c478bd9Sstevel@tonic-gate 	  if ((code = krb5_kt_resolve(kcontext, name, &kt))) {
3177c478bd9Sstevel@tonic-gate 			com_err(progname, code,
3187c478bd9Sstevel@tonic-gate 				gettext("while resolving keytab %s"),
3197c478bd9Sstevel@tonic-gate 		       name);
3207c478bd9Sstevel@tonic-gate 	       exit(1);
3217c478bd9Sstevel@tonic-gate 	  }
3227c478bd9Sstevel@tonic-gate      }
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate      if ((code = krb5_kt_get_name(kcontext, kt, buf, BUFSIZ))) {
32556a424ccSmp 	  com_err(progname, code,
3267c478bd9Sstevel@tonic-gate 			gettext("while getting keytab name"));
3277c478bd9Sstevel@tonic-gate 	  exit(1);
3287c478bd9Sstevel@tonic-gate      }
3297c478bd9Sstevel@tonic-gate 
33056a424ccSmp      printf(gettext("Keytab name: %s\n"), buf);
331*55fea89dSDan Cross 
3327c478bd9Sstevel@tonic-gate      if ((code = krb5_kt_start_seq_get(kcontext, kt, &cursor))) {
33356a424ccSmp 	  com_err(progname, code,
3347c478bd9Sstevel@tonic-gate 			gettext("while starting keytab scan"));
3357c478bd9Sstevel@tonic-gate 	  exit(1);
3367c478bd9Sstevel@tonic-gate      }
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate      if (show_time) {
33956a424ccSmp 	  printf(gettext("KVNO Timestamp"));
34056a424ccSmp 	  fillit(stdout, timestamp_width -
34156a424ccSmp 	    sizeof (gettext("Timestamp")) + 2, (int)' ');
34256a424ccSmp 	  printf(gettext("Principal\n"));
34356a424ccSmp 	  printf("---- ");
3447c478bd9Sstevel@tonic-gate 	  fillit(stdout, timestamp_width, (int) '-');
3457c478bd9Sstevel@tonic-gate 	  printf(" ");
34656a424ccSmp 	  fillit(stdout, 78 - timestamp_width -
3477c478bd9Sstevel@tonic-gate 		    sizeof (gettext("KVNO")), (int)'-');
3487c478bd9Sstevel@tonic-gate 	  printf("\n");
3497c478bd9Sstevel@tonic-gate      } else {
35056a424ccSmp 	  printf(gettext("KVNO Principal\n"));
35156a424ccSmp 	  printf("---- ------------------------------"
3527c478bd9Sstevel@tonic-gate 			    "--------------------------------------"
3537c478bd9Sstevel@tonic-gate 			    "------\n");
3547c478bd9Sstevel@tonic-gate      }
355*55fea89dSDan Cross 
35656a424ccSmp      while ((code = krb5_kt_next_entry(kcontext, kt, &entry, &cursor)) == 0) {
35756a424ccSmp 	  if ((code = krb5_unparse_name(kcontext, entry.principal, &pname))) {
35856a424ccSmp 	       com_err(progname, code,
3597c478bd9Sstevel@tonic-gate 				gettext("while unparsing principal name"));
3607c478bd9Sstevel@tonic-gate 	       exit(1);
3617c478bd9Sstevel@tonic-gate 	  }
3627c478bd9Sstevel@tonic-gate 	  printf("%4d ", entry.vno);
3637c478bd9Sstevel@tonic-gate 	  if (show_time) {
3647c478bd9Sstevel@tonic-gate 	       printtime(entry.timestamp);
3657c478bd9Sstevel@tonic-gate 	       printf(" ");
3667c478bd9Sstevel@tonic-gate 	  }
3677c478bd9Sstevel@tonic-gate 	  printf("%s", pname);
3687c478bd9Sstevel@tonic-gate 	  if (show_etype)
3697c478bd9Sstevel@tonic-gate 	      printf(" (%s) " , etype_string(entry.key.enctype));
3707c478bd9Sstevel@tonic-gate 	  if (show_keys) {
3717c478bd9Sstevel@tonic-gate 	       printf(" (0x");
3727c478bd9Sstevel@tonic-gate 	       {
3737c478bd9Sstevel@tonic-gate 		    int i;
3747c478bd9Sstevel@tonic-gate 		    for (i = 0; i < entry.key.length; i++)
3757c478bd9Sstevel@tonic-gate 			 printf("%02x", entry.key.contents[i]);
3767c478bd9Sstevel@tonic-gate 	       }
3777c478bd9Sstevel@tonic-gate 	       printf(")");
3787c478bd9Sstevel@tonic-gate 	  }
3797c478bd9Sstevel@tonic-gate 	  printf("\n");
3807c478bd9Sstevel@tonic-gate 	  krb5_free_unparsed_name(kcontext, pname);
3817c478bd9Sstevel@tonic-gate      }
3827c478bd9Sstevel@tonic-gate      if (code && code != KRB5_KT_END) {
3837c478bd9Sstevel@tonic-gate 		com_err(progname, code,
3847c478bd9Sstevel@tonic-gate 			gettext("while scanning keytab"));
3857c478bd9Sstevel@tonic-gate 	  exit(1);
3867c478bd9Sstevel@tonic-gate      }
3877c478bd9Sstevel@tonic-gate      if ((code = krb5_kt_end_seq_get(kcontext, kt, &cursor))) {
3887c478bd9Sstevel@tonic-gate 		com_err(progname, code,
3897c478bd9Sstevel@tonic-gate 			gettext("while ending keytab scan"));
3907c478bd9Sstevel@tonic-gate 	  exit(1);
3917c478bd9Sstevel@tonic-gate      }
3927c478bd9Sstevel@tonic-gate      exit(0);
3937c478bd9Sstevel@tonic-gate }
do_ccache(name)3947c478bd9Sstevel@tonic-gate void do_ccache(name)
3957c478bd9Sstevel@tonic-gate    char *name;
3967c478bd9Sstevel@tonic-gate {
3977c478bd9Sstevel@tonic-gate     krb5_ccache cache = NULL;
3987c478bd9Sstevel@tonic-gate     krb5_cc_cursor cur;
3997c478bd9Sstevel@tonic-gate     krb5_creds creds;
4007c478bd9Sstevel@tonic-gate     krb5_principal princ;
4017c478bd9Sstevel@tonic-gate     krb5_flags flags;
4027c478bd9Sstevel@tonic-gate     krb5_error_code code;
4037c478bd9Sstevel@tonic-gate     int	exit_status = 0;
404*55fea89dSDan Cross 
4057c478bd9Sstevel@tonic-gate     if (status_only)
4067c478bd9Sstevel@tonic-gate 	/* exit_status is set back to 0 if a valid tgt is found */
4077c478bd9Sstevel@tonic-gate 	exit_status = 1;
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate     if (name == NULL) {
4107c478bd9Sstevel@tonic-gate 	if ((code = krb5_cc_default(kcontext, &cache))) {
4117c478bd9Sstevel@tonic-gate 	    if (!status_only)
4127c478bd9Sstevel@tonic-gate 				com_err(progname, code,
4137c478bd9Sstevel@tonic-gate 					gettext("while getting default "
4147c478bd9Sstevel@tonic-gate 						"ccache"));
4157c478bd9Sstevel@tonic-gate 	    exit(1);
4167c478bd9Sstevel@tonic-gate 	    }
4177c478bd9Sstevel@tonic-gate     } else {
4187c478bd9Sstevel@tonic-gate 	if ((code = krb5_cc_resolve(kcontext, name, &cache))) {
4197c478bd9Sstevel@tonic-gate 	    if (!status_only)
4207c478bd9Sstevel@tonic-gate 				com_err(progname, code,
4217c478bd9Sstevel@tonic-gate 					gettext("while resolving ccache %s"),
4227c478bd9Sstevel@tonic-gate 			name);
4237c478bd9Sstevel@tonic-gate 	    exit(1);
4247c478bd9Sstevel@tonic-gate 	}
4257c478bd9Sstevel@tonic-gate     }
426*55fea89dSDan Cross 
4277c478bd9Sstevel@tonic-gate     flags = 0;				/* turns off OPENCLOSE mode */
4287c478bd9Sstevel@tonic-gate     if ((code = krb5_cc_set_flags(kcontext, cache, flags))) {
4297c478bd9Sstevel@tonic-gate 	if (code == KRB5_FCC_NOFILE) {
4307c478bd9Sstevel@tonic-gate 	    if (!status_only) {
4317c478bd9Sstevel@tonic-gate 		com_err(progname, code, gettext("(ticket cache %s:%s)"),
4327c478bd9Sstevel@tonic-gate 			krb5_cc_get_type(kcontext, cache),
4337c478bd9Sstevel@tonic-gate 			krb5_cc_get_name(kcontext, cache));
4347c478bd9Sstevel@tonic-gate #ifdef KRB5_KRB4_COMPAT
4357c478bd9Sstevel@tonic-gate 		if (name == NULL)
4367c478bd9Sstevel@tonic-gate 		    do_v4_ccache(0);
4377c478bd9Sstevel@tonic-gate #endif /* KRB5_KRB4_COMPAT */
4387c478bd9Sstevel@tonic-gate 	    }
4397c478bd9Sstevel@tonic-gate 	} else {
4407c478bd9Sstevel@tonic-gate 	    if (!status_only)
4417c478bd9Sstevel@tonic-gate 		com_err(progname, code,
4427c478bd9Sstevel@tonic-gate 			gettext("while setting cache "
4437c478bd9Sstevel@tonic-gate 				"flags(ticket cache %s:%s)"),
4447c478bd9Sstevel@tonic-gate 			krb5_cc_get_type(kcontext, cache),
44556a424ccSmp 			krb5_cc_get_name(kcontext, cache));
4467c478bd9Sstevel@tonic-gate 	}
4477c478bd9Sstevel@tonic-gate 	exit(1);
4487c478bd9Sstevel@tonic-gate     }
4497c478bd9Sstevel@tonic-gate     if ((code = krb5_cc_get_principal(kcontext, cache, &princ))) {
4507c478bd9Sstevel@tonic-gate 	if (!status_only)
4517c478bd9Sstevel@tonic-gate 			com_err(progname, code,
4527c478bd9Sstevel@tonic-gate 				gettext("while retrieving principal name"));
4537c478bd9Sstevel@tonic-gate 	exit(1);
4547c478bd9Sstevel@tonic-gate     }
4557c478bd9Sstevel@tonic-gate     if ((code = krb5_unparse_name(kcontext, princ, &defname))) {
4567c478bd9Sstevel@tonic-gate 	if (!status_only)
4577c478bd9Sstevel@tonic-gate 			com_err(progname, code,
4587c478bd9Sstevel@tonic-gate 				gettext("while unparsing principal name"));
4597c478bd9Sstevel@tonic-gate 	exit(1);
4607c478bd9Sstevel@tonic-gate     }
4617c478bd9Sstevel@tonic-gate     if (!status_only) {
4627c478bd9Sstevel@tonic-gate 		printf(gettext("Ticket cache: %s:%s\nDefault principal: "
4637c478bd9Sstevel@tonic-gate 			    "%s\n\n"),
4647c478bd9Sstevel@tonic-gate 	       krb5_cc_get_type(kcontext, cache),
46556a424ccSmp 	       krb5_cc_get_name(kcontext, cache), defname);
4667c478bd9Sstevel@tonic-gate 		fputs(gettext("Valid starting"), stdout);
4677c478bd9Sstevel@tonic-gate 		fillit(stdout, timestamp_width -
4687c478bd9Sstevel@tonic-gate 		    sizeof (gettext("Valid starting")) + 3, (int)' ');
4697c478bd9Sstevel@tonic-gate 		fputs(gettext("Expires"), stdout);
4707c478bd9Sstevel@tonic-gate 		fillit(stdout, timestamp_width -
4717c478bd9Sstevel@tonic-gate 		    sizeof (gettext("Expires")) + 3, (int)' ');
4727c478bd9Sstevel@tonic-gate 		fputs(gettext("Service principal\n"), stdout);
4737c478bd9Sstevel@tonic-gate     }
4747c478bd9Sstevel@tonic-gate     if ((code = krb5_cc_start_seq_get(kcontext, cache, &cur))) {
4757c478bd9Sstevel@tonic-gate 	if (!status_only)
4767c478bd9Sstevel@tonic-gate 			com_err(progname, code,
4777c478bd9Sstevel@tonic-gate 				gettext("while starting to retrieve tickets"));
4787c478bd9Sstevel@tonic-gate 	exit(1);
4797c478bd9Sstevel@tonic-gate     }
4807c478bd9Sstevel@tonic-gate     while (!(code = krb5_cc_next_cred(kcontext, cache, &cur, &creds))) {
4817c478bd9Sstevel@tonic-gate 	if (status_only) {
4827c478bd9Sstevel@tonic-gate 	    if (exit_status && creds.server->length == 2 &&
4837c478bd9Sstevel@tonic-gate 			    strcmp(creds.server->realm.data,
4847c478bd9Sstevel@tonic-gate 				princ->realm.data) == 0 &&
4857c478bd9Sstevel@tonic-gate 			    strcmp((char *)creds.server->data[0].data,
4867c478bd9Sstevel@tonic-gate 				"krbtgt") == 0 &&
4877c478bd9Sstevel@tonic-gate 		strcmp((char *)creds.server->data[1].data,
488*55fea89dSDan Cross 		       princ->realm.data) == 0 &&
4897c478bd9Sstevel@tonic-gate 		creds.times.endtime > now)
4907c478bd9Sstevel@tonic-gate 		exit_status = 0;
4917c478bd9Sstevel@tonic-gate 	} else {
49256a424ccSmp 	    show_credential(&creds);
4937c478bd9Sstevel@tonic-gate 	}
4947c478bd9Sstevel@tonic-gate 	krb5_free_cred_contents(kcontext, &creds);
4957c478bd9Sstevel@tonic-gate     }
4967c478bd9Sstevel@tonic-gate     if (code == KRB5_CC_END) {
4977c478bd9Sstevel@tonic-gate 	if ((code = krb5_cc_end_seq_get(kcontext, cache, &cur))) {
4987c478bd9Sstevel@tonic-gate 	    if (!status_only)
4997c478bd9Sstevel@tonic-gate 				com_err(progname, code,
5007c478bd9Sstevel@tonic-gate 					gettext("while finishing ticket "
5017c478bd9Sstevel@tonic-gate 						"retrieval"));
5027c478bd9Sstevel@tonic-gate 	    exit(1);
5037c478bd9Sstevel@tonic-gate 	}
5047c478bd9Sstevel@tonic-gate 	flags = KRB5_TC_OPENCLOSE;	/* turns on OPENCLOSE mode */
5057c478bd9Sstevel@tonic-gate 	if ((code = krb5_cc_set_flags(kcontext, cache, flags))) {
5067c478bd9Sstevel@tonic-gate 	    if (!status_only)
5077c478bd9Sstevel@tonic-gate 				com_err(progname, code,
5087c478bd9Sstevel@tonic-gate 					gettext("while closing ccache"));
5097c478bd9Sstevel@tonic-gate 	    exit(1);
5107c478bd9Sstevel@tonic-gate 	}
5117c478bd9Sstevel@tonic-gate #ifdef KRB5_KRB4_COMPAT
5127c478bd9Sstevel@tonic-gate 	if (name == NULL && !status_only)
5137c478bd9Sstevel@tonic-gate 	    do_v4_ccache(0);
5147c478bd9Sstevel@tonic-gate #endif /* KRB5_KRB4_COMPAT */
5157c478bd9Sstevel@tonic-gate 	exit(exit_status);
5167c478bd9Sstevel@tonic-gate     } else {
5177c478bd9Sstevel@tonic-gate 	if (!status_only)
5187c478bd9Sstevel@tonic-gate 			com_err(progname, code,
5197c478bd9Sstevel@tonic-gate 				gettext("while retrieving a ticket"));
5207c478bd9Sstevel@tonic-gate 	exit(1);
521*55fea89dSDan Cross     }
5227c478bd9Sstevel@tonic-gate }
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate char *
etype_string(enctype)5257c478bd9Sstevel@tonic-gate etype_string(enctype)
5267c478bd9Sstevel@tonic-gate     krb5_enctype enctype;
5277c478bd9Sstevel@tonic-gate {
528a49a392fSps     static char buf[256];
5297c478bd9Sstevel@tonic-gate     krb5_error_code retval;
530*55fea89dSDan Cross 
5317c478bd9Sstevel@tonic-gate     if ((retval = krb5_enctype_to_string(enctype, buf, sizeof(buf)))) {
5327c478bd9Sstevel@tonic-gate 	/* XXX if there's an error != EINVAL, I should probably report it */
533a49a392fSps 	snprintf(buf, sizeof(buf), gettext("unsupported encryption type %d"), enctype);
5347c478bd9Sstevel@tonic-gate     }
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate     return buf;
5377c478bd9Sstevel@tonic-gate }
5387c478bd9Sstevel@tonic-gate 
53956a424ccSmp static char *
flags_string(cred)5407c478bd9Sstevel@tonic-gate flags_string(cred)
5417c478bd9Sstevel@tonic-gate     register krb5_creds *cred;
5427c478bd9Sstevel@tonic-gate {
5437c478bd9Sstevel@tonic-gate     static char buf[32];
5447c478bd9Sstevel@tonic-gate     int i = 0;
545*55fea89dSDan Cross 
5467c478bd9Sstevel@tonic-gate     if (cred->ticket_flags & TKT_FLG_FORWARDABLE)
5477c478bd9Sstevel@tonic-gate 	buf[i++] = 'F';
5487c478bd9Sstevel@tonic-gate     if (cred->ticket_flags & TKT_FLG_FORWARDED)
5497c478bd9Sstevel@tonic-gate 	buf[i++] = 'f';
5507c478bd9Sstevel@tonic-gate     if (cred->ticket_flags & TKT_FLG_PROXIABLE)
5517c478bd9Sstevel@tonic-gate 	buf[i++] = 'P';
5527c478bd9Sstevel@tonic-gate     if (cred->ticket_flags & TKT_FLG_PROXY)
5537c478bd9Sstevel@tonic-gate 	buf[i++] = 'p';
5547c478bd9Sstevel@tonic-gate     if (cred->ticket_flags & TKT_FLG_MAY_POSTDATE)
5557c478bd9Sstevel@tonic-gate 	buf[i++] = 'D';
5567c478bd9Sstevel@tonic-gate     if (cred->ticket_flags & TKT_FLG_POSTDATED)
5577c478bd9Sstevel@tonic-gate 	buf[i++] = 'd';
5587c478bd9Sstevel@tonic-gate     if (cred->ticket_flags & TKT_FLG_INVALID)
5597c478bd9Sstevel@tonic-gate 	buf[i++] = 'i';
5607c478bd9Sstevel@tonic-gate     if (cred->ticket_flags & TKT_FLG_RENEWABLE)
5617c478bd9Sstevel@tonic-gate 	buf[i++] = 'R';
5627c478bd9Sstevel@tonic-gate     if (cred->ticket_flags & TKT_FLG_INITIAL)
5637c478bd9Sstevel@tonic-gate 	buf[i++] = 'I';
5647c478bd9Sstevel@tonic-gate     if (cred->ticket_flags & TKT_FLG_HW_AUTH)
5657c478bd9Sstevel@tonic-gate 	buf[i++] = 'H';
5667c478bd9Sstevel@tonic-gate     if (cred->ticket_flags & TKT_FLG_PRE_AUTH)
5677c478bd9Sstevel@tonic-gate 	buf[i++] = 'A';
56856a424ccSmp     if (cred->ticket_flags & TKT_FLG_TRANSIT_POLICY_CHECKED)
56956a424ccSmp 	buf[i++] = 'T';
57056a424ccSmp     if (cred->ticket_flags & TKT_FLG_OK_AS_DELEGATE)
57156a424ccSmp 	buf[i++] = 'O';		/* D/d are taken.  Use short strings?  */
57256a424ccSmp     if (cred->ticket_flags & TKT_FLG_ANONYMOUS)
57356a424ccSmp 	buf[i++] = 'a';
5747c478bd9Sstevel@tonic-gate     buf[i] = '\0';
5757c478bd9Sstevel@tonic-gate     return(buf);
5767c478bd9Sstevel@tonic-gate }
5777c478bd9Sstevel@tonic-gate 
578*55fea89dSDan Cross void
printtime(tv)5797c478bd9Sstevel@tonic-gate printtime(tv)
5807c478bd9Sstevel@tonic-gate     time_t tv;
5817c478bd9Sstevel@tonic-gate {
5827c478bd9Sstevel@tonic-gate     char timestring[BUFSIZ];
5837c478bd9Sstevel@tonic-gate     char fill;
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate     fill = ' ';
5867c478bd9Sstevel@tonic-gate 	if (!krb5_timestamp_to_sfstring((krb5_timestamp) tv, timestring,
5877c478bd9Sstevel@tonic-gate 					timestamp_width+1, &fill)) {
5887c478bd9Sstevel@tonic-gate 	printf(timestring);
5897c478bd9Sstevel@tonic-gate     }
5907c478bd9Sstevel@tonic-gate }
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate void
show_credential(cred)59356a424ccSmp show_credential(cred)
5947c478bd9Sstevel@tonic-gate     register krb5_creds * cred;
5957c478bd9Sstevel@tonic-gate {
5967c478bd9Sstevel@tonic-gate     krb5_error_code retval;
5977c478bd9Sstevel@tonic-gate     krb5_ticket *tkt;
5987c478bd9Sstevel@tonic-gate     char *name, *sname, *flags;
5997c478bd9Sstevel@tonic-gate     int	extra_field = 0;
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate     retval = krb5_unparse_name(kcontext, cred->client, &name);
6027c478bd9Sstevel@tonic-gate     if (retval) {
6037c478bd9Sstevel@tonic-gate 		com_err(progname, retval,
6047c478bd9Sstevel@tonic-gate 			gettext("while unparsing client name"));
6057c478bd9Sstevel@tonic-gate 	return;
6067c478bd9Sstevel@tonic-gate     }
6077c478bd9Sstevel@tonic-gate     retval = krb5_unparse_name(kcontext, cred->server, &sname);
6087c478bd9Sstevel@tonic-gate     if (retval) {
6097c478bd9Sstevel@tonic-gate 		com_err(progname, retval,
6107c478bd9Sstevel@tonic-gate 			gettext("while unparsing server name"));
6117c478bd9Sstevel@tonic-gate 		krb5_free_unparsed_name(kcontext, name);
6127c478bd9Sstevel@tonic-gate 	return;
6137c478bd9Sstevel@tonic-gate     }
6147c478bd9Sstevel@tonic-gate     if (!cred->times.starttime)
6157c478bd9Sstevel@tonic-gate 	cred->times.starttime = cred->times.authtime;
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate     printtime(cred->times.starttime);
6187c478bd9Sstevel@tonic-gate     putchar(' '); putchar(' ');
6197c478bd9Sstevel@tonic-gate     printtime(cred->times.endtime);
6207c478bd9Sstevel@tonic-gate     putchar(' '); putchar(' ');
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate     printf("%s\n", sname);
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate     if (strcmp(name, defname)) {
6257c478bd9Sstevel@tonic-gate 		printf(gettext("\tfor client %s"), name);
6267c478bd9Sstevel@tonic-gate 	    extra_field++;
6277c478bd9Sstevel@tonic-gate     }
628*55fea89dSDan Cross 
6297c478bd9Sstevel@tonic-gate     if (cred->times.renew_till) {
6307c478bd9Sstevel@tonic-gate 	if (!extra_field)
6317c478bd9Sstevel@tonic-gate 		fputs("\t",stdout);
6327c478bd9Sstevel@tonic-gate 	else
6337c478bd9Sstevel@tonic-gate 		fputs(", ",stdout);
634796b8631SToomas Soome 	fputs(gettext("renew until "), stdout);
6357c478bd9Sstevel@tonic-gate 	printtime(cred->times.renew_till);
6367c478bd9Sstevel@tonic-gate 	extra_field += 2;
6377c478bd9Sstevel@tonic-gate     }
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate     if (extra_field > 3) {
6407c478bd9Sstevel@tonic-gate 	fputs("\n", stdout);
6417c478bd9Sstevel@tonic-gate 	extra_field = 0;
6427c478bd9Sstevel@tonic-gate     }
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate     if (show_flags) {
6457c478bd9Sstevel@tonic-gate 	flags = flags_string(cred);
6467c478bd9Sstevel@tonic-gate 	if (flags && *flags) {
6477c478bd9Sstevel@tonic-gate 	    if (!extra_field)
6487c478bd9Sstevel@tonic-gate 		fputs("\t",stdout);
6497c478bd9Sstevel@tonic-gate 	    else
6507c478bd9Sstevel@tonic-gate 		fputs(", ",stdout);
6517c478bd9Sstevel@tonic-gate 			printf(gettext("Flags: %s"), flags);
6527c478bd9Sstevel@tonic-gate 	    extra_field++;
6537c478bd9Sstevel@tonic-gate 	}
6547c478bd9Sstevel@tonic-gate     }
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate     if (extra_field > 2) {
6577c478bd9Sstevel@tonic-gate 	fputs("\n", stdout);
6587c478bd9Sstevel@tonic-gate 	extra_field = 0;
6597c478bd9Sstevel@tonic-gate     }
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate     if (show_etype) {
6627c478bd9Sstevel@tonic-gate 	retval = decode_krb5_ticket(&cred->ticket, &tkt);
66356a424ccSmp 	if (retval)
66456a424ccSmp 	    goto err_tkt;
66556a424ccSmp 
66656a424ccSmp 	if (!extra_field)
66756a424ccSmp 	    fputs("\t",stdout);
66856a424ccSmp 	else
66956a424ccSmp 	    fputs(", ",stdout);
67056a424ccSmp 	printf(gettext("Etype(skey, tkt): %s, "),
67156a424ccSmp 	       etype_string(cred->keyblock.enctype));
67256a424ccSmp 	printf("%s ",
67356a424ccSmp 	       etype_string(tkt->enc_part.enctype));
67456a424ccSmp 	extra_field++;
67556a424ccSmp 
67656a424ccSmp     err_tkt:
67756a424ccSmp 	if (tkt != NULL)
6787c478bd9Sstevel@tonic-gate 	    krb5_free_ticket(kcontext, tkt);
6797c478bd9Sstevel@tonic-gate     }
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate     /* if any additional info was printed, extra_field is non-zero */
6827c478bd9Sstevel@tonic-gate     if (extra_field)
6837c478bd9Sstevel@tonic-gate 	putchar('\n');
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate     if (show_addresses) {
6877c478bd9Sstevel@tonic-gate 	if (!cred->addresses || !cred->addresses[0]) {
6887c478bd9Sstevel@tonic-gate 	    printf(gettext("\tAddresses: (none)\n"));
6897c478bd9Sstevel@tonic-gate 	} else {
6907c478bd9Sstevel@tonic-gate 	    int i;
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate 	    printf(gettext("\tAddresses: "));
6937c478bd9Sstevel@tonic-gate 	    one_addr(cred->addresses[0]);
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate 	    for (i=1; cred->addresses[i]; i++) {
6967c478bd9Sstevel@tonic-gate 		printf(", ");
6977c478bd9Sstevel@tonic-gate 		one_addr(cred->addresses[i]);
6987c478bd9Sstevel@tonic-gate 	    }
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 	    printf("\n");
7017c478bd9Sstevel@tonic-gate 	}
7027c478bd9Sstevel@tonic-gate     }
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate     krb5_free_unparsed_name(kcontext, name);
7057c478bd9Sstevel@tonic-gate     krb5_free_unparsed_name(kcontext, sname);
7067c478bd9Sstevel@tonic-gate }
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate #include "port-sockets.h"
7097c478bd9Sstevel@tonic-gate #include "socket-utils.h" /* for ss2sin etc */
7107c478bd9Sstevel@tonic-gate #include <fake-addrinfo.h>
7117c478bd9Sstevel@tonic-gate 
one_addr(a)7127c478bd9Sstevel@tonic-gate void one_addr(a)
7137c478bd9Sstevel@tonic-gate     krb5_address *a;
7147c478bd9Sstevel@tonic-gate {
7157c478bd9Sstevel@tonic-gate     struct sockaddr_storage ss;
7167c478bd9Sstevel@tonic-gate     int err;
7177c478bd9Sstevel@tonic-gate     char namebuf[NI_MAXHOST];
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate     memset (&ss, 0, sizeof (ss));
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate     switch (a->addrtype) {
7227c478bd9Sstevel@tonic-gate     case ADDRTYPE_INET:
7237c478bd9Sstevel@tonic-gate 	if (a->length != IPV4_ADDR_LEN) {
7247c478bd9Sstevel@tonic-gate 	broken:
7257c478bd9Sstevel@tonic-gate 	    printf ("broken address (type %d length %d)",
7267c478bd9Sstevel@tonic-gate 		    a->addrtype, a->length);
7277c478bd9Sstevel@tonic-gate 	    return;
7287c478bd9Sstevel@tonic-gate 	}
7297c478bd9Sstevel@tonic-gate 	{
7307c478bd9Sstevel@tonic-gate 	    struct sockaddr_in *sinp = ss2sin (&ss);
7317c478bd9Sstevel@tonic-gate 	    sinp->sin_family = AF_INET;
7327c478bd9Sstevel@tonic-gate #ifdef HAVE_SA_LEN
7337c478bd9Sstevel@tonic-gate 	    sinp->sin_len = sizeof (struct sockaddr_in);
7347c478bd9Sstevel@tonic-gate #endif
7357c478bd9Sstevel@tonic-gate 	    memcpy (&sinp->sin_addr, a->contents, IPV4_ADDR_LEN);
7367c478bd9Sstevel@tonic-gate 	}
7377c478bd9Sstevel@tonic-gate 	break;
7387c478bd9Sstevel@tonic-gate #ifdef KRB5_USE_INET6
7397c478bd9Sstevel@tonic-gate     case ADDRTYPE_INET6:
7407c478bd9Sstevel@tonic-gate 	if (a->length != IPV6_ADDR_LEN)
7417c478bd9Sstevel@tonic-gate 	    goto broken;
7427c478bd9Sstevel@tonic-gate 	{
7437c478bd9Sstevel@tonic-gate 	    struct sockaddr_in6 *sin6p = ss2sin6 (&ss);
7447c478bd9Sstevel@tonic-gate 	    sin6p->sin6_family = AF_INET6;
7457c478bd9Sstevel@tonic-gate #ifdef HAVE_SA_LEN
7467c478bd9Sstevel@tonic-gate 	    sin6p->sin6_len = sizeof (struct sockaddr_in6);
7477c478bd9Sstevel@tonic-gate #endif
7487c478bd9Sstevel@tonic-gate 	    memcpy (&sin6p->sin6_addr, a->contents, IPV6_ADDR_LEN);
7497c478bd9Sstevel@tonic-gate 	}
7507c478bd9Sstevel@tonic-gate 	break;
7517c478bd9Sstevel@tonic-gate #endif
7527c478bd9Sstevel@tonic-gate     default:
7537c478bd9Sstevel@tonic-gate 	printf(gettext("unknown addr type %d"), a->addrtype);
7547c478bd9Sstevel@tonic-gate 	return;
7557c478bd9Sstevel@tonic-gate     }
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate     namebuf[0] = 0;
7587c478bd9Sstevel@tonic-gate     err = getnameinfo (ss2sa (&ss), socklen (ss2sa (&ss)),
7597c478bd9Sstevel@tonic-gate 		       namebuf, sizeof (namebuf), 0, 0,
7607c478bd9Sstevel@tonic-gate 		       no_resolve ? NI_NUMERICHOST : 0U);
7617c478bd9Sstevel@tonic-gate     if (err) {
7627c478bd9Sstevel@tonic-gate 	printf (gettext("unprintable address (type %d, error %d %s)"), a->addrtype, err,
7637c478bd9Sstevel@tonic-gate 		gai_strerror (err));
7647c478bd9Sstevel@tonic-gate 	return;
7657c478bd9Sstevel@tonic-gate     }
7667c478bd9Sstevel@tonic-gate     printf ("%s", namebuf);
7677c478bd9Sstevel@tonic-gate }
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate void
fillit(f,num,c)7707c478bd9Sstevel@tonic-gate fillit(f, num, c)
7717c478bd9Sstevel@tonic-gate     FILE		*f;
77256a424ccSmp     unsigned int	num;
7737c478bd9Sstevel@tonic-gate     int			c;
7747c478bd9Sstevel@tonic-gate {
7757c478bd9Sstevel@tonic-gate     int i;
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate     for (i=0; i<num; i++)
7787c478bd9Sstevel@tonic-gate 	fputc(c, f);
7797c478bd9Sstevel@tonic-gate }
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate #ifdef KRB5_KRB4_COMPAT
7827c478bd9Sstevel@tonic-gate void
do_v4_ccache(name)7837c478bd9Sstevel@tonic-gate do_v4_ccache(name)
7847c478bd9Sstevel@tonic-gate     char * name;
7857c478bd9Sstevel@tonic-gate {
7867c478bd9Sstevel@tonic-gate     char    pname[ANAME_SZ];
7877c478bd9Sstevel@tonic-gate     char    pinst[INST_SZ];
7887c478bd9Sstevel@tonic-gate     char    prealm[REALM_SZ];
7897c478bd9Sstevel@tonic-gate     char    *file;
7907c478bd9Sstevel@tonic-gate     int     k_errno;
7917c478bd9Sstevel@tonic-gate     CREDENTIALS c;
7927c478bd9Sstevel@tonic-gate     int     header = 1;
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate     if (!got_k4)
7957c478bd9Sstevel@tonic-gate 	return;
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate     file = name?name:tkt_string();
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate     if (status_only) {
800*55fea89dSDan Cross 	fprintf(stderr,
8017c478bd9Sstevel@tonic-gate 		"%s: exit status option not supported for Kerberos 4\n",
8027c478bd9Sstevel@tonic-gate 		progname);
8037c478bd9Sstevel@tonic-gate 	exit(1);
8047c478bd9Sstevel@tonic-gate     }
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate     if (got_k5)
8077c478bd9Sstevel@tonic-gate 	printf("\n\n");
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate     printf("Kerberos 4 ticket cache: %s\n", file);
8107c478bd9Sstevel@tonic-gate 
811*55fea89dSDan Cross     /*
812*55fea89dSDan Cross      * Since krb_get_tf_realm will return a ticket_file error,
8137c478bd9Sstevel@tonic-gate      * we will call tf_init and tf_close first to filter out
814*55fea89dSDan Cross      * things like no ticket file.  Otherwise, the error that
815*55fea89dSDan Cross      * the user would see would be
8167c478bd9Sstevel@tonic-gate      * klist: can't find realm of ticket file: No ticket file (tf_util)
8177c478bd9Sstevel@tonic-gate      * instead of
8187c478bd9Sstevel@tonic-gate      * klist: No ticket file (tf_util)
8197c478bd9Sstevel@tonic-gate      */
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate     /* Open ticket file */
82256a424ccSmp     k_errno = tf_init(file, R_TKT_FIL);
82356a424ccSmp     if (k_errno) {
8247c478bd9Sstevel@tonic-gate 	fprintf(stderr, "%s: %s\n", progname, krb_get_err_text (k_errno));
8257c478bd9Sstevel@tonic-gate 	exit(1);
8267c478bd9Sstevel@tonic-gate     }
8277c478bd9Sstevel@tonic-gate     /* Close ticket file */
8287c478bd9Sstevel@tonic-gate     (void) tf_close();
8297c478bd9Sstevel@tonic-gate 
830*55fea89dSDan Cross     /*
8317c478bd9Sstevel@tonic-gate      * We must find the realm of the ticket file here before calling
8327c478bd9Sstevel@tonic-gate      * tf_init because since the realm of the ticket file is not
8337c478bd9Sstevel@tonic-gate      * really stored in the principal section of the file, the
8347c478bd9Sstevel@tonic-gate      * routine we use must itself call tf_init and tf_close.
8357c478bd9Sstevel@tonic-gate      */
8367c478bd9Sstevel@tonic-gate     if ((k_errno = krb_get_tf_realm(file, prealm)) != KSUCCESS) {
8377c478bd9Sstevel@tonic-gate 	fprintf(stderr, "%s: can't find realm of ticket file: %s\n",
8387c478bd9Sstevel@tonic-gate 		progname, krb_get_err_text (k_errno));
8397c478bd9Sstevel@tonic-gate 	exit(1);
8407c478bd9Sstevel@tonic-gate     }
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate     /* Open ticket file */
84356a424ccSmp     if ((k_errno = tf_init(file, R_TKT_FIL))) {
8447c478bd9Sstevel@tonic-gate 	fprintf(stderr, "%s: %s\n", progname, krb_get_err_text (k_errno));
8457c478bd9Sstevel@tonic-gate 	exit(1);
8467c478bd9Sstevel@tonic-gate     }
8477c478bd9Sstevel@tonic-gate     /* Get principal name and instance */
8487c478bd9Sstevel@tonic-gate     if ((k_errno = tf_get_pname(pname)) ||
8497c478bd9Sstevel@tonic-gate 	(k_errno = tf_get_pinst(pinst))) {
8507c478bd9Sstevel@tonic-gate 	fprintf(stderr, "%s: %s\n", progname, krb_get_err_text (k_errno));
8517c478bd9Sstevel@tonic-gate 	exit(1);
8527c478bd9Sstevel@tonic-gate     }
8537c478bd9Sstevel@tonic-gate 
854*55fea89dSDan Cross     /*
8557c478bd9Sstevel@tonic-gate      * You may think that this is the obvious place to get the
8567c478bd9Sstevel@tonic-gate      * realm of the ticket file, but it can't be done here as the
857*55fea89dSDan Cross      * routine to do this must open the ticket file.  This is why
8587c478bd9Sstevel@tonic-gate      * it was done before tf_init.
8597c478bd9Sstevel@tonic-gate      */
860*55fea89dSDan Cross 
8617c478bd9Sstevel@tonic-gate     printf("Principal: %s%s%s%s%s\n\n", pname,
8627c478bd9Sstevel@tonic-gate 	   (pinst[0] ? "." : ""), pinst,
8637c478bd9Sstevel@tonic-gate 	   (prealm[0] ? "@" : ""), prealm);
8647c478bd9Sstevel@tonic-gate     while ((k_errno = tf_get_cred(&c)) == KSUCCESS) {
8657c478bd9Sstevel@tonic-gate 	if (header) {
8667c478bd9Sstevel@tonic-gate 	    printf("%-18s  %-18s  %s\n",
8677c478bd9Sstevel@tonic-gate 		   "  Issued", "  Expires", "  Principal");
8687c478bd9Sstevel@tonic-gate 	    header = 0;
8697c478bd9Sstevel@tonic-gate 	}
8707c478bd9Sstevel@tonic-gate 	printtime(c.issue_date);
8717c478bd9Sstevel@tonic-gate 	fputs("  ", stdout);
87256a424ccSmp 	printtime(krb_life_to_time(c.issue_date, c.lifetime));
8737c478bd9Sstevel@tonic-gate 	printf("  %s%s%s%s%s\n",
8747c478bd9Sstevel@tonic-gate 	       c.service, (c.instance[0] ? "." : ""), c.instance,
8757c478bd9Sstevel@tonic-gate 	       (c.realm[0] ? "@" : ""), c.realm);
8767c478bd9Sstevel@tonic-gate     }
8777c478bd9Sstevel@tonic-gate     if (header && k_errno == EOF) {
8787c478bd9Sstevel@tonic-gate 	printf("No tickets in file.\n");
8797c478bd9Sstevel@tonic-gate     }
8807c478bd9Sstevel@tonic-gate }
8817c478bd9Sstevel@tonic-gate #endif /* KRB4_KRB5_COMPAT */
882