afsconfig-and-rcsid-all-around-20010705
[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 <afs/param.h>
11 #include <afsconfig.h>
12
13 RCSID("$Header$");
14
15 #include <stdio.h>
16 #ifdef  AFS_AIX32_ENV
17 #include <signal.h>
18 #endif
19 #include <sys/file.h>
20 #include <rx/xdr.h>
21 #include <errno.h>
22 #include <sys/types.h>
23 #include <afs/auth.h>
24 #include <time.h>               /*time(), ctime()*/
25 #include <pwd.h>
26
27 #define VIRTUE
28 #define VICE
29
30 #ifdef CMUWP_ENV
31 #include <afs/afsutil.h>                /*getv*(), getc*() routine family*/
32 #endif /* CMUWP_ENV */
33
34 #undef VIRTUE
35 #undef VICE
36
37 #include "AFS_component_version_number.c"
38
39 main(argc, argv)
40     int argc;
41     char **argv;
42
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[16];       /*Printable user name*/
50     struct ktc_principal serviceName, clientName;       /* service name for ticket */
51     struct ktc_token token;             /* the token we're printing */
52
53 #ifdef  AFS_AIX32_ENV
54     /*
55      * The following signal action for AIX is necessary so that in case of a 
56      * crash (i.e. core is generated) we can include the user's data section 
57      * in the core dump. Unfortunately, by default, only a partial core is
58      * generated which, in many cases, isn't too useful.
59      */
60     struct sigaction nsa;
61     
62     sigemptyset(&nsa.sa_mask);
63     nsa.sa_handler = SIG_DFL;
64     nsa.sa_flags = SA_FULLDUMP;
65     sigaction(SIGSEGV, &nsa, NULL);
66 #endif
67
68         /* has no args ... support for help flag */
69
70         if(argc>1)
71         {
72                 /* syntax from AFS Com Ref Man p9-39 */
73
74                 printf("Usage: tokens [-help]\n");
75                 fflush(stdout);
76                 exit(0);
77         }
78
79     printf("\nTokens held by the Cache Manager:\n\n");
80     cellNum = 0;
81     current_time = time(0);
82     while (1) {
83         rc = ktc_ListTokens(cellNum, &cellNum, &serviceName);
84         if (rc) {
85             /* only error is now end of list */
86             printf("   --End of list--\n");
87             break;
88         }
89         else {
90             /* get the ticket info itself */
91             rc = ktc_GetToken(&serviceName, &token, sizeof(token), &clientName);
92             if (rc) {
93                 printf("tokens: failed to get token info for service %s.%s.%s (code %d)\n",
94                        serviceName.name, serviceName.instance, serviceName.cell, rc);
95                 continue;
96             }
97             tokenExpireTime = token.endTime;
98             strcpy(UserName, clientName.name);
99             if (clientName.instance[0] != 0) {
100                 strcat(UserName, ".");
101                 strcat(UserName, clientName.instance);
102             }
103             if (UserName[0] == 0)
104                 printf("Tokens");
105             else if (strncmp(UserName, "AFS ID", 6) == 0) {
106                 printf("User's (%s) tokens", UserName);
107             }
108             else if (strncmp(UserName, "Unix UID", 8) == 0) {
109                 printf("Tokens");
110             }
111             else
112                 printf("User %s's tokens", UserName);
113             printf(" for %s%s%s@%s ",
114                    serviceName.name,
115                    serviceName.instance[0] ? "." : "",
116                    serviceName.instance,
117                    serviceName.cell);
118             if (tokenExpireTime <= current_time)
119                 printf("[>> Expired <<]\n");
120             else {
121                 expireString = ctime(&tokenExpireTime);
122                 expireString += 4; /*Move past the day of week*/
123                 expireString[12] = '\0';
124                 printf("[Expires %s]\n",
125                        expireString);
126             }
127         }
128     }
129     exit (0);
130 } /*Main program*/