home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / kerberosIV / klist / klist.c next >
Encoding:
C/C++ Source or Header  |  1990-06-25  |  7.0 KB  |  279 lines

  1. /*
  2.  * $Source: /usr/src/kerberosIV/klist/RCS/klist.c,v $
  3.  * $Author: kfall $ 
  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: /usr/src/kerberosIV/klist/RCS/klist.c,v 4.16 90/06/25 21:01:52 kfall Exp $";
  17. #endif    lint
  18.  
  19. #include <mit-copyright.h>
  20. #include <stdio.h>
  21. #include <strings.h>
  22. #include <sys/file.h>
  23. #include <des.h>
  24. #include <krb.h>
  25. #include <prot.h>
  26.  
  27. char   *tkt_string();
  28. char   *short_date();
  29. char   *whoami;            /* What was I invoked as?? */
  30. char   *getenv();
  31.  
  32. extern char *krb_err_txt[];
  33.  
  34. /* ARGSUSED */
  35. main(argc, argv)
  36.     int     argc;
  37.     char  **argv;
  38. {
  39.     int     long_form = 1;
  40.     int     tgt_test = 0;
  41.     int     do_srvtab = 0;
  42.     char   *tkt_file = NULL;
  43.     char   *cp;
  44.  
  45.     whoami = (cp = rindex(*argv, '/')) ? cp + 1 : *argv;
  46.  
  47.     while (*(++argv)) {
  48.     if (!strcmp(*argv, "-s")) {
  49.         long_form = 0;
  50.         continue;
  51.     }
  52.     if (!strcmp(*argv, "-t")) {
  53.         tgt_test = 1;
  54.         long_form = 0;
  55.         continue;
  56.     }
  57.     if (!strcmp(*argv, "-l")) {    /* now default */
  58.         continue;
  59.     }
  60.     if (!strcmp(*argv, "-file")) {
  61.         if (*(++argv)) {
  62.         tkt_file = *argv;
  63.         continue;
  64.         } else
  65.         usage();
  66.     }
  67.     if (!strcmp(*argv, "-srvtab")) {
  68.         if (tkt_file == NULL)    /* if no other file spec'ed,
  69.                        set file to default srvtab */
  70.             tkt_file = KEYFILE;
  71.         do_srvtab = 1;
  72.         continue;
  73.     }
  74.     usage();
  75.     }
  76.  
  77.     if (do_srvtab)
  78.     display_srvtab(tkt_file);
  79.     else
  80.     display_tktfile(tkt_file, tgt_test, long_form);
  81.     exit(0);
  82. }
  83.  
  84.  
  85. display_tktfile(file, tgt_test, long_form)
  86. char *file;
  87. int tgt_test, long_form;
  88. {
  89.     char    pname[ANAME_SZ];
  90.     char    pinst[INST_SZ];
  91.     char    prealm[REALM_SZ];
  92.     char    buf1[20], buf2[20];
  93.     int     k_errno;
  94.     CREDENTIALS c;
  95.     int     header = 1;
  96.  
  97.     if ((file == NULL) && ((file = getenv("KRBTKFILE")) == NULL))
  98.     file = TKT_FILE;
  99.  
  100.     if (long_form)
  101.     printf("Ticket file:    %s\n", file);
  102.  
  103.     /* 
  104.      * Since krb_get_tf_realm will return a ticket_file error, 
  105.      * we will call tf_init and tf_close first to filter out
  106.      * things like no ticket file.  Otherwise, the error that 
  107.      * the user would see would be 
  108.      * klist: can't find realm of ticket file: No ticket file (tf_util)
  109.      * instead of
  110.      * klist: No ticket file (tf_util)
  111.      */
  112.  
  113.     /* Open ticket file */
  114.     if (k_errno = tf_init(file, R_TKT_FIL)) {
  115.     if (!tgt_test)
  116.         fprintf(stderr, "%s: %s\n", whoami, krb_err_txt[k_errno]);
  117.     exit(1);
  118.     }
  119.     /* Close ticket file */
  120.     (void) tf_close();
  121.  
  122.     /* 
  123.      * We must find the realm of the ticket file here before calling
  124.      * tf_init because since the realm of the ticket file is not
  125.      * really stored in the principal section of the file, the
  126.      * routine we use must itself call tf_init and tf_close.
  127.      */
  128.     if ((k_errno = krb_get_tf_realm(file, prealm)) != KSUCCESS) {
  129.     if (!tgt_test)
  130.         fprintf(stderr, "%s: can't find realm of ticket file: %s\n",
  131.             whoami, krb_err_txt[k_errno]);
  132.     exit(1);
  133.     }
  134.  
  135.     /* Open ticket file */
  136.     if (k_errno = tf_init(file, R_TKT_FIL)) {
  137.     if (!tgt_test)
  138.         fprintf(stderr, "%s: %s\n", whoami, krb_err_txt[k_errno]);
  139.     exit(1);
  140.     }
  141.     /* Get principal name and instance */
  142.     if ((k_errno = tf_get_pname(pname)) ||
  143.     (k_errno = tf_get_pinst(pinst))) {
  144.         if (!tgt_test)
  145.             fprintf(stderr, "%s: %s\n", whoami, krb_err_txt[k_errno]);
  146.         exit(1);
  147.     }
  148.  
  149.     /* 
  150.      * You may think that this is the obvious place to get the
  151.      * realm of the ticket file, but it can't be done here as the
  152.      * routine to do this must open the ticket file.  This is why 
  153.      * it was done before tf_init.
  154.      */
  155.        
  156.     if (!tgt_test && long_form)
  157.     printf("Principal:\t%s%s%s%s%s\n\n", pname,
  158.            (pinst[0] ? "." : ""), pinst,
  159.            (prealm[0] ? "@" : ""), prealm);
  160.     while ((k_errno = tf_get_cred(&c)) == KSUCCESS) {
  161.     if (!tgt_test && long_form && header) {
  162.         printf("%-15s  %-15s  %s\n",
  163.            "  Issued", "  Expires", "  Principal");
  164.         header = 0;
  165.     }
  166.     if (tgt_test) {
  167.         c.issue_date += ((unsigned char) c.lifetime) * 5 * 60;
  168.         if (!strcmp(c.service, TICKET_GRANTING_TICKET) &&
  169.         !strcmp(c.instance, prealm)) {
  170.         if (time(0) < c.issue_date)
  171.             exit(0);        /* tgt hasn't expired */
  172.         else
  173.             exit(1);        /* has expired */
  174.         }
  175.         continue;            /* not a tgt */
  176.     }
  177.     if (long_form) {
  178.         (void) strcpy(buf1, short_date(&c.issue_date));
  179.         c.issue_date += ((unsigned char) c.lifetime) * 5 * 60;
  180.         (void) strcpy(buf2, short_date(&c.issue_date));
  181.         printf("%s  %s  ", buf1, buf2);
  182.     }
  183.     printf("%s%s%s%s%s\n",
  184.            c.service, (c.instance[0] ? "." : ""), c.instance,
  185.            (c.realm[0] ? "@" : ""), c.realm);
  186.     }
  187.     if (tgt_test)
  188.     exit(1);            /* no tgt found */
  189.     if (header && long_form && k_errno == EOF) {
  190.     printf("No tickets in file.\n");
  191.     }
  192. }
  193.  
  194. char   *
  195. short_date(dp)
  196.     long   *dp;
  197. {
  198.     register char *cp;
  199.     extern char *ctime();
  200.     cp = ctime(dp) + 4;
  201.     cp[15] = '\0';
  202.     return (cp);
  203. }
  204.  
  205. usage()
  206. {
  207.     fprintf(stderr,
  208.         "Usage: %s [ -s | -t ] [ -file filename ] [ -srvtab ]\n", whoami);
  209.     exit(1);
  210. }
  211.  
  212. display_srvtab(file)
  213. char *file;
  214. {
  215.     int stab;
  216.     char serv[SNAME_SZ];
  217.     char inst[INST_SZ];
  218.     char rlm[REALM_SZ];
  219.     unsigned char key[8];
  220.     unsigned char vno;
  221.     int count;
  222.  
  223.     printf("Server key file:   %s\n", file);
  224.     
  225.     if ((stab = open(file, O_RDONLY, 0400)) < 0) {
  226.     perror(file);
  227.     exit(1);
  228.     }
  229.     printf("%-15s %-15s %-10s %s\n","Service","Instance","Realm",
  230.        "Key Version");
  231.     printf("------------------------------------------------------\n");
  232.  
  233.     /* argh. getst doesn't return error codes, it silently fails */
  234.     while (((count = ok_getst(stab, serv, SNAME_SZ)) > 0)
  235.        && ((count = ok_getst(stab, inst, INST_SZ)) > 0)
  236.        && ((count = ok_getst(stab, rlm, REALM_SZ)) > 0)) {
  237.     if (((count = read(stab,(char *) &vno,1)) != 1) ||
  238.          ((count = read(stab,(char *) key,8)) != 8)) {
  239.         if (count < 0)
  240.         perror("reading from key file");
  241.         else
  242.         fprintf(stderr, "key file truncated\n");
  243.         exit(1);
  244.     }
  245.     printf("%-15s %-15s %-15s %d\n",serv,inst,rlm,vno);
  246.     }
  247.     if (count < 0)
  248.     perror(file);
  249.     (void) close(stab);
  250. }
  251.  
  252. /* adapted from getst() in librkb */
  253. /*
  254.  * ok_getst() takes a file descriptor, a string and a count.  It reads
  255.  * from the file until either it has read "count" characters, or until
  256.  * it reads a null byte.  When finished, what has been read exists in
  257.  * the given string "s".  If "count" characters were actually read, the
  258.  * last is changed to a null, so the returned string is always null-
  259.  * terminated.  ok_getst() returns the number of characters read, including
  260.  * the null terminator.
  261.  *
  262.  * If there is a read error, it returns -1 (like the read(2) system call)
  263.  */
  264.  
  265. ok_getst(fd, s, n)
  266.     int fd;
  267.     register char *s;
  268. {
  269.     register count = n;
  270.     int err;
  271.     while ((err = read(fd, s, 1)) > 0 && --count)
  272.         if (*s++ == '\0')
  273.             return (n - count);
  274.     if (err < 0)
  275.     return(-1);
  276.     *s = '\0';
  277.     return (n - count);
  278. }
  279.