136656Skfall /*
236656Skfall * $Source: /mit/kerberos/src/kuser/RCS/klist.c,v $
336656Skfall * $Author: jtkohl $
436656Skfall *
536656Skfall * Copyright 1987, 1988 by the Massachusetts Institute of Technology.
636656Skfall *
736656Skfall * For copying and distribution information, please see the file
836656Skfall * <mit-copyright.h>.
936656Skfall *
1036656Skfall * Lists your current Kerberos tickets.
1136656Skfall * Written by Bill Sommerfeld, MIT Project Athena.
1236656Skfall */
1336656Skfall
1436656Skfall #ifndef lint
1536656Skfall static char rcsid_klist_c[] =
1636656Skfall "$Header: klist.c,v 4.11 89/01/23 09:34:58 jtkohl Exp $";
1736656Skfall #endif lint
1836656Skfall
1936665Skfall #include <kerberos/mit-copyright.h>
2036656Skfall #include <stdio.h>
2136656Skfall #include <strings.h>
2236656Skfall #include <sys/file.h>
2336665Skfall #include <kerberos/krb.h>
2436665Skfall #include <kerberos/prot.h>
2536656Skfall
2636656Skfall char *tkt_string();
2736656Skfall char *short_date();
2836656Skfall char *whoami; /* What was I invoked as?? */
2936656Skfall char *getenv();
3036656Skfall
3136656Skfall extern char *krb_err_txt[];
3236656Skfall
3336656Skfall /* ARGSUSED */
main(argc,argv)3436656Skfall main(argc, argv)
3536656Skfall int argc;
3636656Skfall char **argv;
3736656Skfall {
3836656Skfall int long_form = 1;
3936656Skfall int tgt_test = 0;
4036656Skfall int do_srvtab = 0;
4136656Skfall char *tkt_file = NULL;
4236656Skfall char *cp;
4336656Skfall
4436656Skfall whoami = (cp = rindex(*argv, '/')) ? cp + 1 : *argv;
4536656Skfall
4636656Skfall while (*(++argv)) {
4736656Skfall if (!strcmp(*argv, "-s")) {
4836656Skfall long_form = 0;
4936656Skfall continue;
5036656Skfall }
5136656Skfall if (!strcmp(*argv, "-t")) {
5236656Skfall tgt_test = 1;
5336656Skfall long_form = 0;
5436656Skfall continue;
5536656Skfall }
5636656Skfall if (!strcmp(*argv, "-l")) { /* now default */
5736656Skfall continue;
5836656Skfall }
5936656Skfall if (!strcmp(*argv, "-file")) {
6036656Skfall if (*(++argv)) {
6136656Skfall tkt_file = *argv;
6236656Skfall continue;
6336656Skfall } else
6436656Skfall usage();
6536656Skfall }
6636656Skfall if (!strcmp(*argv, "-srvtab")) {
6736656Skfall if (tkt_file == NULL) /* if no other file spec'ed,
6836656Skfall set file to default srvtab */
6936656Skfall tkt_file = KEYFILE;
7036656Skfall do_srvtab = 1;
7136656Skfall continue;
7236656Skfall }
7336656Skfall usage();
7436656Skfall }
7536656Skfall
7636656Skfall if (do_srvtab)
7736656Skfall display_srvtab(tkt_file);
7836656Skfall else
7936656Skfall display_tktfile(tkt_file, tgt_test, long_form);
8036656Skfall exit(0);
8136656Skfall }
8236656Skfall
8336656Skfall
display_tktfile(file,tgt_test,long_form)8436656Skfall display_tktfile(file, tgt_test, long_form)
8536656Skfall char *file;
8636656Skfall int tgt_test, long_form;
8736656Skfall {
8836656Skfall char pname[ANAME_SZ];
8936656Skfall char pinst[INST_SZ];
9036656Skfall char prealm[REALM_SZ];
9136656Skfall char buf1[20], buf2[20];
9236656Skfall int k_errno;
9336656Skfall CREDENTIALS c;
9436656Skfall int header = 1;
9536656Skfall
9636656Skfall if ((file == NULL) && ((file = getenv("KRBTKFILE")) == NULL))
9736656Skfall file = TKT_FILE;
9836656Skfall
9936656Skfall if (long_form)
10036656Skfall printf("Ticket file: %s\n", file);
10136656Skfall
10236656Skfall /* Open ticket file */
10336656Skfall if (k_errno = tf_init(file, R_TKT_FIL)) {
10436656Skfall if (!tgt_test)
10536656Skfall fprintf(stderr, "%s: %s\n", whoami, krb_err_txt[k_errno]);
10636656Skfall exit(1);
10736656Skfall }
10836656Skfall /* Get principal name and instance */
10936656Skfall if ((k_errno = tf_get_pname(pname)) ||
11036656Skfall (k_errno = tf_get_pinst(pinst))) {
11136656Skfall if (!tgt_test)
11236656Skfall fprintf(stderr, "%s: %s\n", whoami, krb_err_txt[k_errno]);
11336656Skfall exit(1);
11436656Skfall }
11536656Skfall if ((k_errno = krb_get_lrealm(prealm, 1)) != KSUCCESS) {
11636656Skfall fprintf(stderr, "%s: can't find local realm: %s\n",
11736656Skfall whoami, krb_err_txt[k_errno]);
11836656Skfall exit(1);
11936656Skfall }
12036656Skfall if (!tgt_test && long_form)
12136656Skfall printf("Principal:\t%s%s%s%s%s\n\n", pname,
12236656Skfall (pinst[0] ? "." : ""), pinst,
12336656Skfall (prealm[0] ? "@" : ""), prealm);
12436656Skfall while ((k_errno = tf_get_cred(&c)) == KSUCCESS) {
12536656Skfall if (!tgt_test && long_form && header) {
12636656Skfall printf("%-15s %-15s %s\n",
12736656Skfall " Issued", " Expires", " Principal");
12836656Skfall header = 0;
12936656Skfall }
13036656Skfall if (tgt_test) {
131*38041Skfall c.issue_date += ((unsigned char) c.lifetime) * 5 * 60;
13236656Skfall if (!strcmp(c.service, TICKET_GRANTING_TICKET) &&
13336656Skfall !strcmp(c.instance, prealm)) {
13436656Skfall if (time(0) < c.issue_date)
13536656Skfall exit(0); /* tgt hasn't expired */
13636656Skfall else
13736656Skfall exit(1); /* has expired */
13836656Skfall }
13936656Skfall continue; /* not a tgt */
14036656Skfall }
14136656Skfall if (long_form) {
14236656Skfall (void) strcpy(buf1, short_date(&c.issue_date));
14336656Skfall c.issue_date += c.lifetime * 5 * 60;
14436656Skfall (void) strcpy(buf2, short_date(&c.issue_date));
14536656Skfall printf("%s %s ", buf1, buf2);
14636656Skfall }
14736656Skfall printf("%s%s%s%s%s\n",
14836656Skfall c.service, (c.instance[0] ? "." : ""), c.instance,
14936656Skfall (c.realm[0] ? "@" : ""), c.realm);
15036656Skfall }
15136656Skfall if (tgt_test)
15236656Skfall exit(1); /* no tgt found */
15336656Skfall if (header && long_form && k_errno == EOF) {
15436656Skfall printf("No tickets in file.\n");
15536656Skfall }
15636656Skfall }
15736656Skfall
15836656Skfall char *
short_date(dp)15936656Skfall short_date(dp)
16036656Skfall long *dp;
16136656Skfall {
16236656Skfall register char *cp;
16336656Skfall extern char *ctime();
16436656Skfall cp = ctime(dp) + 4;
16536656Skfall cp[15] = '\0';
16636656Skfall return (cp);
16736656Skfall }
16836656Skfall
usage()16936656Skfall usage()
17036656Skfall {
17136656Skfall fprintf(stderr,
17236656Skfall "Usage: %s [ -s | -t ] [ -file filename ] [ -srvtab ]\n", whoami);
17336656Skfall exit(1);
17436656Skfall }
17536656Skfall
display_srvtab(file)17636656Skfall display_srvtab(file)
17736656Skfall char *file;
17836656Skfall {
17936656Skfall int stab;
18036656Skfall char serv[SNAME_SZ];
18136656Skfall char inst[INST_SZ];
18236656Skfall char rlm[REALM_SZ];
18336656Skfall unsigned char key[8];
18436656Skfall unsigned char vno;
18536656Skfall int count;
18636656Skfall
18736656Skfall printf("Server key file: %s\n", file);
18836656Skfall
18936656Skfall if ((stab = open(file, O_RDONLY, 0400)) < 0) {
19036656Skfall perror(file);
19136656Skfall exit(1);
19236656Skfall }
19336665Skfall printf("%-15s %-15s %-10s %s\n","Service","Instance","Realm",
19436656Skfall "Key Version");
19536665Skfall printf("-----------------------------------------------------------\n");
19636656Skfall
19736656Skfall /* argh. getst doesn't return error codes, it silently fails */
19836656Skfall while (((count = ok_getst(stab, serv, SNAME_SZ)) > 0)
19936656Skfall && ((count = ok_getst(stab, inst, INST_SZ)) > 0)
20036656Skfall && ((count = ok_getst(stab, rlm, REALM_SZ)) > 0)) {
20136656Skfall if (((count = read(stab,(char *) &vno,1)) != 1) ||
20236656Skfall ((count = read(stab,(char *) key,8)) != 8)) {
20336656Skfall if (count < 0)
20436656Skfall perror("reading from key file");
20536656Skfall else
20636656Skfall fprintf(stderr, "key file truncated\n");
20736656Skfall exit(1);
20836656Skfall }
20936656Skfall printf("%-15s %-15s %-15s %d\n",serv,inst,rlm,vno);
21036656Skfall }
21136656Skfall if (count < 0)
21236656Skfall perror(file);
21336656Skfall (void) close(stab);
21436656Skfall }
21536656Skfall
21636656Skfall /* adapted from getst() in librkb */
21736656Skfall /*
21836656Skfall * ok_getst() takes a file descriptor, a string and a count. It reads
21936656Skfall * from the file until either it has read "count" characters, or until
22036656Skfall * it reads a null byte. When finished, what has been read exists in
22136656Skfall * the given string "s". If "count" characters were actually read, the
22236656Skfall * last is changed to a null, so the returned string is always null-
22336656Skfall * terminated. ok_getst() returns the number of characters read, including
22436656Skfall * the null terminator.
22536656Skfall *
22636656Skfall * If there is a read error, it returns -1 (like the read(2) system call)
22736656Skfall */
22836656Skfall
ok_getst(fd,s,n)22936656Skfall ok_getst(fd, s, n)
23036656Skfall int fd;
23136656Skfall register char *s;
23236656Skfall {
23336656Skfall register count = n;
23436656Skfall int err;
23536656Skfall while ((err = read(fd, s, 1)) > 0 && --count)
23636656Skfall if (*s++ == '\0')
23736656Skfall return (n - count);
23836656Skfall if (err < 0)
23936656Skfall return(-1);
24036656Skfall *s = '\0';
24136656Skfall return (n - count);
24236656Skfall }
243