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