1 /* 2 * $Source: /mit/kerberos/src/kuser/RCS/klist.c,v $ 3 * $Author: jtkohl $ 4 * 5 * Copyright 1987, 1988 by the Massachusetts Institute of Technology. 6 * 7 * For copying and distribution information, please see the file 8 * <mit-copyright.h>. 9 * 10 * Lists your current Kerberos tickets. 11 * Written by Bill Sommerfeld, MIT Project Athena. 12 */ 13 14 #ifndef lint 15 static char rcsid_klist_c[] = 16 "$Header: klist.c,v 4.11 89/01/23 09:34:58 jtkohl Exp $"; 17 #endif lint 18 19 #include <kerberos/mit-copyright.h> 20 #include <stdio.h> 21 #include <strings.h> 22 #include <sys/file.h> 23 #include <kerberos/krb.h> 24 #include <kerberos/prot.h> 25 26 char *tkt_string(); 27 char *short_date(); 28 char *whoami; /* What was I invoked as?? */ 29 char *getenv(); 30 31 extern char *krb_err_txt[]; 32 33 /* ARGSUSED */ 34 main(argc, argv) 35 int argc; 36 char **argv; 37 { 38 int long_form = 1; 39 int tgt_test = 0; 40 int do_srvtab = 0; 41 char *tkt_file = NULL; 42 char *cp; 43 44 whoami = (cp = rindex(*argv, '/')) ? cp + 1 : *argv; 45 46 while (*(++argv)) { 47 if (!strcmp(*argv, "-s")) { 48 long_form = 0; 49 continue; 50 } 51 if (!strcmp(*argv, "-t")) { 52 tgt_test = 1; 53 long_form = 0; 54 continue; 55 } 56 if (!strcmp(*argv, "-l")) { /* now default */ 57 continue; 58 } 59 if (!strcmp(*argv, "-file")) { 60 if (*(++argv)) { 61 tkt_file = *argv; 62 continue; 63 } else 64 usage(); 65 } 66 if (!strcmp(*argv, "-srvtab")) { 67 if (tkt_file == NULL) /* if no other file spec'ed, 68 set file to default srvtab */ 69 tkt_file = KEYFILE; 70 do_srvtab = 1; 71 continue; 72 } 73 usage(); 74 } 75 76 if (do_srvtab) 77 display_srvtab(tkt_file); 78 else 79 display_tktfile(tkt_file, tgt_test, long_form); 80 exit(0); 81 } 82 83 84 display_tktfile(file, tgt_test, long_form) 85 char *file; 86 int tgt_test, long_form; 87 { 88 char pname[ANAME_SZ]; 89 char pinst[INST_SZ]; 90 char prealm[REALM_SZ]; 91 char buf1[20], buf2[20]; 92 int k_errno; 93 CREDENTIALS c; 94 int header = 1; 95 96 if ((file == NULL) && ((file = getenv("KRBTKFILE")) == NULL)) 97 file = TKT_FILE; 98 99 if (long_form) 100 printf("Ticket file: %s\n", file); 101 102 /* Open ticket file */ 103 if (k_errno = tf_init(file, R_TKT_FIL)) { 104 if (!tgt_test) 105 fprintf(stderr, "%s: %s\n", whoami, krb_err_txt[k_errno]); 106 exit(1); 107 } 108 /* Get principal name and instance */ 109 if ((k_errno = tf_get_pname(pname)) || 110 (k_errno = tf_get_pinst(pinst))) { 111 if (!tgt_test) 112 fprintf(stderr, "%s: %s\n", whoami, krb_err_txt[k_errno]); 113 exit(1); 114 } 115 if ((k_errno = krb_get_lrealm(prealm, 1)) != KSUCCESS) { 116 fprintf(stderr, "%s: can't find local realm: %s\n", 117 whoami, krb_err_txt[k_errno]); 118 exit(1); 119 } 120 if (!tgt_test && long_form) 121 printf("Principal:\t%s%s%s%s%s\n\n", pname, 122 (pinst[0] ? "." : ""), pinst, 123 (prealm[0] ? "@" : ""), prealm); 124 while ((k_errno = tf_get_cred(&c)) == KSUCCESS) { 125 if (!tgt_test && long_form && header) { 126 printf("%-15s %-15s %s\n", 127 " Issued", " Expires", " Principal"); 128 header = 0; 129 } 130 if (tgt_test) { 131 c.issue_date += c.lifetime * 5 * 60; 132 if (!strcmp(c.service, TICKET_GRANTING_TICKET) && 133 !strcmp(c.instance, prealm)) { 134 if (time(0) < c.issue_date) 135 exit(0); /* tgt hasn't expired */ 136 else 137 exit(1); /* has expired */ 138 } 139 continue; /* not a tgt */ 140 } 141 if (long_form) { 142 (void) strcpy(buf1, short_date(&c.issue_date)); 143 c.issue_date += c.lifetime * 5 * 60; 144 (void) strcpy(buf2, short_date(&c.issue_date)); 145 printf("%s %s ", buf1, buf2); 146 } 147 printf("%s%s%s%s%s\n", 148 c.service, (c.instance[0] ? "." : ""), c.instance, 149 (c.realm[0] ? "@" : ""), c.realm); 150 } 151 if (tgt_test) 152 exit(1); /* no tgt found */ 153 if (header && long_form && k_errno == EOF) { 154 printf("No tickets in file.\n"); 155 } 156 } 157 158 char * 159 short_date(dp) 160 long *dp; 161 { 162 register char *cp; 163 extern char *ctime(); 164 cp = ctime(dp) + 4; 165 cp[15] = '\0'; 166 return (cp); 167 } 168 169 usage() 170 { 171 fprintf(stderr, 172 "Usage: %s [ -s | -t ] [ -file filename ] [ -srvtab ]\n", whoami); 173 exit(1); 174 } 175 176 display_srvtab(file) 177 char *file; 178 { 179 int stab; 180 char serv[SNAME_SZ]; 181 char inst[INST_SZ]; 182 char rlm[REALM_SZ]; 183 unsigned char key[8]; 184 unsigned char vno; 185 int count; 186 187 printf("Server key file: %s\n", file); 188 189 if ((stab = open(file, O_RDONLY, 0400)) < 0) { 190 perror(file); 191 exit(1); 192 } 193 printf("%-15s %-15s %-10s %s\n","Service","Instance","Realm", 194 "Key Version"); 195 printf("-----------------------------------------------------------\n"); 196 197 /* argh. getst doesn't return error codes, it silently fails */ 198 while (((count = ok_getst(stab, serv, SNAME_SZ)) > 0) 199 && ((count = ok_getst(stab, inst, INST_SZ)) > 0) 200 && ((count = ok_getst(stab, rlm, REALM_SZ)) > 0)) { 201 if (((count = read(stab,(char *) &vno,1)) != 1) || 202 ((count = read(stab,(char *) key,8)) != 8)) { 203 if (count < 0) 204 perror("reading from key file"); 205 else 206 fprintf(stderr, "key file truncated\n"); 207 exit(1); 208 } 209 printf("%-15s %-15s %-15s %d\n",serv,inst,rlm,vno); 210 } 211 if (count < 0) 212 perror(file); 213 (void) close(stab); 214 } 215 216 /* adapted from getst() in librkb */ 217 /* 218 * ok_getst() takes a file descriptor, a string and a count. It reads 219 * from the file until either it has read "count" characters, or until 220 * it reads a null byte. When finished, what has been read exists in 221 * the given string "s". If "count" characters were actually read, the 222 * last is changed to a null, so the returned string is always null- 223 * terminated. ok_getst() returns the number of characters read, including 224 * the null terminator. 225 * 226 * If there is a read error, it returns -1 (like the read(2) system call) 227 */ 228 229 ok_getst(fd, s, n) 230 int fd; 231 register char *s; 232 { 233 register count = n; 234 int err; 235 while ((err = read(fd, s, 1)) > 0 && --count) 236 if (*s++ == '\0') 237 return (n - count); 238 if (err < 0) 239 return(-1); 240 *s = '\0'; 241 return (n - count); 242 } 243