tokens: Use the new tokens interface
[openafs.git] / src / log / tokens.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  *
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <afsconfig.h>
11 #include <afs/param.h>
12
13
14 #include <stdio.h>
15 #ifdef  AFS_AIX32_ENV
16 #include <signal.h>
17 #endif
18 #include <sys/file.h>
19 #include <rx/xdr.h>
20 #include <errno.h>
21 #include <sys/types.h>
22 #include <afs/auth.h>
23 #include <time.h>               /*time(), ctime() */
24 #include <pwd.h>
25 #include <afs/ktc.h>
26
27 #include <string.h>
28
29 #define VIRTUE
30 #define VICE
31
32 #ifdef CMUWP_ENV
33 #include <afs/afsutil.h>        /*getv*(), getc*() routine family */
34 #endif /* CMUWP_ENV */
35
36 #undef VIRTUE
37 #undef VICE
38
39 #include "AFS_component_version_number.c"
40
41 int
42 main(int argc, char **argv)
43 {                               /*Main program */
44     int cellNum;                /*Cell entry number */
45     int rc;                     /*Return value from U_CellGetLocalTokens */
46     time_t current_time;        /*Current time of day */
47     time_t tokenExpireTime;     /*When token expires */
48     char *expireString;         /*Char string of expiration time */
49     char UserName[MAXKTCNAMELEN * 2 + 2]; /*Printable user name */
50     char *cellName;
51     struct ktc_principal clientName;    /* service name for ticket */
52     struct ktc_token token;     /* the token we're printing */
53     struct ktc_setTokenData *tokenSet;
54
55 #ifdef  AFS_AIX32_ENV
56     /*
57      * The following signal action for AIX is necessary so that in case of a
58      * crash (i.e. core is generated) we can include the user's data section
59      * in the core dump. Unfortunately, by default, only a partial core is
60      * generated which, in many cases, isn't too useful.
61      */
62     struct sigaction nsa;
63
64     sigemptyset(&nsa.sa_mask);
65     nsa.sa_handler = SIG_DFL;
66     nsa.sa_flags = SA_FULLDUMP;
67     sigaction(SIGSEGV, &nsa, NULL);
68 #endif
69
70     /* has no args ... support for help flag */
71
72     if (argc > 1) {
73         /* syntax from AFS Com Ref Man p9-39 */
74
75         printf("Usage: tokens [-help]\n");
76         fflush(stdout);
77         exit(0);
78     }
79
80     printf("\nTokens held by the Cache Manager:\n\n");
81     cellNum = 0;
82     current_time = time(0);
83     while (1) {
84         rc = ktc_ListTokensEx(cellNum, &cellNum, &cellName);
85         if (rc) {
86             /* only error is now end of list */
87             printf("   --End of list--\n");
88             break;
89         } else {
90             /* get the ticket info itself */
91             rc = ktc_GetTokenEx(cellName, &tokenSet);
92             if (rc) {
93                 printf
94                     ("tokens: failed to get token info for cell %s (code %d)\n",
95                      cellName, rc);
96                 continue;
97             }
98             rc = token_extractRxkad(tokenSet, &token, NULL, &clientName);
99             if (rc == 0) {
100                 tokenExpireTime = token.endTime;
101                 strcpy(UserName, clientName.name);
102                 if (clientName.instance[0] != 0) {
103                     strcat(UserName, ".");
104                     strcat(UserName, clientName.instance);
105                 }
106                 if (UserName[0] == 0)
107                     printf("rxkad Tokens");
108                 else if (strncmp(UserName, "AFS ID", 6) == 0) {
109                     printf("User's (%s) rxkad tokens", UserName);
110                 } else if (strncmp(UserName, "Unix UID", 8) == 0) {
111                     printf("RxkadTokens");
112                 } else
113                     printf("User %s's rxkad tokens", UserName);
114                 printf(" for %s ", cellName);
115                 if (tokenExpireTime <= current_time)
116                     printf("[>> Expired <<]\n");
117                 else {
118                     expireString = ctime(&tokenExpireTime);
119                     expireString += 4;  /*Move past the day of week */
120                     expireString[12] = '\0';
121                     printf("[Expires %s]\n", expireString);
122                 }
123             }
124             token_FreeSet(&tokenSet);
125         }
126     }
127     exit(0);
128 }                               /*Main program */