windows-multi-fix-20061003
[openafs.git] / src / WINNT / afsd / ctokens.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 <afs/stds.h>
12
13 #include <windows.h>
14 #include <winsock2.h>
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <string.h>
18 #include <time.h>
19 #include <afs/auth.h>
20
21 main(argc, argv)
22         int argc;
23         char **argv;
24 {
25         int cellNum;
26         int rc;
27         time_t current_time;
28         time_t tokenExpireTime;
29         char *expireString;
30         char userName[100];
31         struct ktc_principal serviceName, clientName;
32         struct ktc_token token;
33         WSADATA WSAjunk;
34
35         WSAStartup(0x0101, &WSAjunk);
36         if (argc > 1) {
37            printf("%s [-help]\n", argv[0]);
38            return 0;
39         }
40
41         printf("\nTokens held by the Cache Manager:\n\n");
42         cellNum = 0;
43         current_time = time((void *) 0);
44
45         while (1) {
46                 rc = ktc_ListTokens(cellNum, &cellNum, &serviceName);
47                 if (rc == KTC_NOENT) {
48                         /* end of list */
49                         printf("   --End of list --\n");
50                         break;
51                 }
52                 else if (rc == KTC_NOCM) {
53                         printf("AFS device may not have started\n");
54                         break;
55                 }
56                 else if (rc) {
57                         printf("Unexpected error, code %d\n", rc);
58                         exit(1);
59                 }
60                 else {
61                         rc = ktc_GetToken(&serviceName, &token, sizeof(token),
62                                           &clientName);
63                         if (rc) {
64                                 printf("Unexpected error, service %s.%s.%s, code %d\n",
65                                         serviceName.name, serviceName.instance,
66                                         serviceName.cell, rc);
67                                 continue;
68                         }
69                         tokenExpireTime = token.endTime;
70                         strcpy(userName, clientName.name);
71                         if (clientName.instance[0] != 0) {
72                                 strcat(userName, ".");
73                                 strcat(userName, clientName.instance);
74                         }
75                         if (userName[0] == '\0')
76                                 printf("Tokens");
77                         else if (strncmp(userName, "AFS ID", 6) == 0)
78                                 printf("User's (%s) tokens", userName);
79                         else if (strncmp(userName, "Unix UID", 8) == 0)
80                                 printf("Tokens");
81                         else
82                                 printf("User %s's tokens", userName);
83                         printf(" for %s%s%s@%s ",
84                                 serviceName.name,
85                                 serviceName.instance[0] ? "." : "",
86                                 serviceName.instance,
87                                 serviceName.cell);
88                         if (tokenExpireTime <= current_time)
89                                 printf("[>> Expired <<]\n");
90                         else {
91                                 time_t t = tokenExpireTime;
92                                 expireString = ctime(&t);
93                                 expireString += 4;       /* Skip day of week */
94                                 expireString[12] = '\0'; /* Omit secs & year */
95                                 printf("[Expires %s]\n", expireString);
96                         }
97                 }
98         }
99         return(0);
100 }