summaryrefslogtreecommitdiffstats
path: root/src/clients/klist/klist.c
blob: e59b24ce9f3a3a7a232c75af5ef9dc75004b17b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
 * clients/klist/klist.c
 *
 * Copyright 1990 by the Massachusetts Institute of Technology.
 * All Rights Reserved.
 *
 * Export of this software from the United States of America may
 *   require a specific license from the United States Government.
 *   It is the responsibility of any person or organization contemplating
 *   export to obtain such a license before exporting.
 * 
 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
 * distribute this software and its documentation for any purpose and
 * without fee is hereby granted, provided that the above copyright
 * notice appear in all copies and that both that copyright notice and
 * this permission notice appear in supporting documentation, and that
 * the name of M.I.T. not be used in advertising or publicity pertaining
 * to distribution of the software without specific, written prior
 * permission.  M.I.T. makes no representations about the suitability of
 * this software for any purpose.  It is provided "as is" without express
 * or implied warranty.
 * 
 *
 * List out the contents of your credential cache.
 */

#include <stdio.h>
#include "krb5.h"
#include "com_err.h"
#include <time.h>

extern int optind;
extern char *optarg;
int show_etype = 0;
int show_flags = 0;
char *progname;
char *defname;
time_t now;

void
show_credential 
	PROTOTYPE((krb5_context,
		   krb5_creds *));

void
main(argc, argv)
    int argc;
    char **argv;
{
    int c;
    int errflg = 0;
    int code;
    krb5_ccache cache = NULL;
    krb5_cc_cursor cur;
    krb5_creds creds;
    char *cache_name;
    krb5_principal princ;
    krb5_flags flags;
    krb5_context kcontext;

    krb5_init_context(&kcontext);
    krb5_init_ets(kcontext);

    time(&now);
    
    if (strrchr(argv[0], '/'))
	progname = strrchr(argv[0], '/')+1;
    else
	progname = argv[0];

    while ((c = getopt(argc, argv, "efc:")) != EOF) {
	switch (c) {
	case 'e':
	     show_etype = 1;
	     break;
	case 'f':
	     show_flags = 1;
	     break;
	case 'c':
	    if (cache == NULL) {
		cache_name = optarg;
		
		code = krb5_cc_resolve (kcontext, cache_name, &cache);
		if (code != 0) {
		    com_err(progname, code, "while resolving %s", cache_name);
		    errflg++;
		}
	    } else {
		fprintf(stderr, "Only one -c option allowed\n");
		errflg++;
	    }
	    break;
	case '?':
	default:
	    errflg++;
	    break;
	}
    }

    if (optind != argc)
	errflg++;
    
    if (errflg) {
	fprintf(stderr, "Usage: %s [ -c cache ]\n", progname);
	exit(2);
    }
    if (cache == NULL) {
	if (code = krb5_cc_default(kcontext, &cache)) {
	    com_err(progname, code, "while getting default ccache");
	    exit(1);
	}
    }

    flags = 0;				/* turns off OPENCLOSE mode */
    if (code = krb5_cc_set_flags(kcontext, cache, flags)) {
	if (code == ENOENT) {
	    com_err(progname, code, "(ticket cache %s)",
		    krb5_cc_get_name(kcontext, cache));
	} else
	    com_err(progname, code,
		    "while setting cache flags (ticket cache %s)",
		    krb5_cc_get_name(kcontext, cache));
	exit(1);
    }
    if (code = krb5_cc_get_principal(kcontext, cache, &princ)) {
	com_err(progname, code, "while retrieving principal name");
	exit(1);
    }
    if (code = krb5_unparse_name(kcontext, princ, &defname)) {
	com_err(progname, code, "while unparsing principal name");
	exit(1);
    }
    printf("Ticket cache: %s\nDefault principal: %s\n\n",
           krb5_cc_get_name(kcontext, cache), defname);
    if (code = krb5_cc_start_seq_get(kcontext, cache, &cur)) {
	com_err(progname, code, "while starting to retrieve tickets");
	exit(1);
    }
    fputs("  Valid starting       Expires          Service principal\n",
	  stdout);
    while (!(code = krb5_cc_next_cred(kcontext, cache, &cur, &creds))) {
	show_credential(kcontext, &creds);
	krb5_free_cred_contents(kcontext, &creds);
    }
    if (code == KRB5_CC_END) {
	if (code = krb5_cc_end_seq_get(kcontext, cache, &cur)) {
	    com_err(progname, code, "while finishing ticket retrieval");
	    exit(1);
	}
	flags = KRB5_TC_OPENCLOSE;	/* turns on OPENCLOSE mode */
	if (code = krb5_cc_set_flags(kcontext, cache, flags)) {
	    com_err(progname, code, "while closing ccache");
	    exit(1);
	}
	exit(0);
    } else {
	com_err(progname, code, "while retrieving a ticket");
	exit(1);
    }	
}

char *
flags_string(cred)
    register krb5_creds *cred;
{
    static char buf[32];
    int i = 0;
	
    if (cred->ticket_flags & TKT_FLG_FORWARDABLE)
        buf[i++] = 'F';
    if (cred->ticket_flags & TKT_FLG_FORWARDED)
        buf[i++] = 'f';
    if (cred->ticket_flags & TKT_FLG_PROXIABLE)
        buf[i++] = 'P';
    if (cred->ticket_flags & TKT_FLG_PROXY)
        buf[i++] = 'p';
    if (cred->ticket_flags & TKT_FLG_MAY_POSTDATE)
        buf[i++] = 'D';
    if (cred->ticket_flags & TKT_FLG_POSTDATED)
        buf[i++] = 'd';
    if (cred->ticket_flags & TKT_FLG_INVALID)
        buf[i++] = 'i';
    if (cred->ticket_flags & TKT_FLG_RENEWABLE)
        buf[i++] = 'R';
    if (cred->ticket_flags & TKT_FLG_INITIAL)
        buf[i++] = 'I';
    if (cred->ticket_flags & TKT_FLG_HW_AUTH)
        buf[i++] = 'H';
    if (cred->ticket_flags & TKT_FLG_PRE_AUTH)
        buf[i++] = 'A';
    buf[i] = '\0';
    return(buf);
}

static  char *Month_names[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
				"Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };

void 
printtime(tv)
    time_t tv;
{
    struct tm *stime;

    stime = localtime((time_t *)&tv);
    printf("%2d-%s-%2d %02d:%02d:%02d",
           stime->tm_mday,
           Month_names[stime->tm_mon],
           stime->tm_year,
           stime->tm_hour,
           stime->tm_min,
           stime->tm_sec);
}

/* Make sure this list matches the ETYPE order in encryption.h */
#define ETYPE_MAX 6
char * etype_string[ETYPE_MAX] = {
    "ETYPE_NULL", 
    "ETYPE_DES_CBC_CRC", 
    "ETYPE_DES_CBC_MD4", 
    "ETYPE_DES_CBC_MD5", 
    "ETYPE_RAW_DES_CBC", 
    NULL };

void
show_credential(kcontext, cred)
    krb5_context kcontext;
    register krb5_creds *cred;
{
    krb5_error_code retval;
    char *name, *sname, *flags;
    int	first = 1;

    retval = krb5_unparse_name(kcontext, cred->client, &name);
    if (retval) {
	com_err(progname, retval, "while unparsing client name");
	return;
    }
    retval = krb5_unparse_name(kcontext, cred->server, &sname);
    if (retval) {
	com_err(progname, retval, "while unparsing server name");
	free(name);
	return;
    }
    if (!cred->times.starttime)
	cred->times.starttime = cred->times.authtime;

    printtime(cred->times.starttime);
    putchar(' '); putchar(' ');
    printtime(cred->times.endtime);
    putchar(' '); putchar(' ');

    printf("%s\n", sname);

    if (strcmp(name, defname)) {
	    printf("\tfor client %s", name);
	    first = 0;
    }
    
    if (cred->times.renew_till) {
	if (first)
		fputs("\t",stdout);
	else
		fputs(", ",stdout);
	fputs("renew until ", stdout);
        printtime(cred->times.renew_till);
    }

    if (show_etype) {
	krb5_enctype etype = cred->keyblock.etype;

	if (!first) 
	    putchar('\n');

	printf("\tEncryption type: ");
	if (etype != ETYPE_UNKNOWN) {
	    if ((etype < ETYPE_MAX) && etype_string[etype]) {
		printf("%s", etype_string[etype]);
	    } else {
		printf("UNRECOGNIZED");
	    }
	} else {
	    printf("ETYPE_UNKNOWN");
	}
    }

    if (show_flags) {
	flags = flags_string(cred);
	if (flags && *flags) {
	    if (first)
		fputs("\t",stdout);
	    else
		fputs(", ",stdout);
	    printf("Flags: %s", flags);
	    first = 0;
        }
    }

    putchar('\n');
    free(name);
    free(sname);
}