reindent-20030715
[openafs.git] / src / auth / test / ktctest.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 /* Test of the ktc_*Token() routines */
11
12 #include <stdlib.h>
13 #include <stddef.h>
14 #include <stdio.h>
15 #include <errno.h>
16
17 #include <afsconfig.h>
18 #include <afs/param.h>
19
20 RCSID
21     ("$Header$");
22
23 #include <afs/stds.h>
24 #include <afs/afsutil.h>
25 #include <afs/auth.h>
26
27 extern int ktc_SetToken(struct ktc_principal *aserver,
28                         struct ktc_token *atoken,
29                         struct ktc_principal *aclient, int flags);
30
31 extern int ktc_GetToken(struct ktc_principal *aserver,
32                         struct ktc_token *atoken, int atokenLen,
33                         struct ktc_principal *aclient);
34
35 extern int ktc_ListTokens(int aprevIndex, int *aindex,
36                           struct ktc_principal *aserver);
37
38 extern int ktc_ForgetAllTokens(void);
39
40
41 static int SamePrincipal(struct ktc_principal *p1, struct ktc_principal *p2);
42 static int SameToken(struct ktc_token *t1, struct ktc_token *t2);
43
44
45 #define MAXCELLS  20
46
47 int
48 main(void)
49 {
50     struct ktc_principal oldServer[MAXCELLS], newServer[MAXCELLS];
51     struct ktc_principal oldClient[MAXCELLS], newClient[MAXCELLS];
52     struct ktc_token oldToken[MAXCELLS], newToken[MAXCELLS];
53     int cellCount, cellIndex;
54     int i, code;
55
56 #ifdef AFS_NT40_ENV
57     /* Initialize winsock; required by NT pioctl() */
58     if (afs_winsockInit()) {
59         printf("\nUnable to initialize winsock (required by NT pioctl()).\n");
60         exit(1);
61     }
62 #endif
63
64     /* Get original tokens */
65
66     printf("\nFetching original tokens.\n");
67
68     cellIndex = 0;
69
70     for (i = 0; i < MAXCELLS; i++) {
71         /* fetch server principal */
72         code = ktc_ListTokens(cellIndex, &cellIndex, &oldServer[i]);
73
74         if (code) {
75             if (code == KTC_NOENT) {
76                 /* no more tokens */
77                 break;
78             } else {
79                 /* some error occured */
80                 perror("ktc_ListTokens failed fetching original tokens");
81                 exit(1);
82             }
83         }
84
85         /* fetch token and client identity w.r.t. server */
86         code =
87             ktc_GetToken(&oldServer[i], &oldToken[i],
88                          sizeof(struct ktc_token), &oldClient[i]);
89
90         if (code) {
91             /* some unexpected error occured */
92             perror("ktc_GetToken failed fetching original tokens");
93             exit(1);
94         }
95     }
96
97     cellCount = i;
98
99     if (cellCount == 0) {
100         printf("Obtain one or more tokens prior to executing test.\n");
101         exit(0);
102     } else if (cellCount == MAXCELLS) {
103         printf("Only first %d tokens utilized by test; rest will be lost.\n",
104                MAXCELLS);
105     }
106
107     for (i = 0; i < cellCount; i++) {
108         printf("Token[%d]: server = %s@%s, client = %s@%s\n", i,
109                oldServer[i].name, oldServer[i].cell, oldClient[i].name,
110                oldClient[i].cell);
111     }
112
113
114     /* Forget original tokens */
115
116     printf("\nClearing original tokens and verifying disposal.\n");
117
118     code = ktc_ForgetAllTokens();
119
120     if (code) {
121         perror("ktc_ForgetAllTokens failed on original tokens");
122         exit(1);
123     }
124
125     for (i = 0; i < cellCount; i++) {
126         struct ktc_principal dummyPrincipal;
127         struct ktc_token dummyToken;
128
129         code =
130             ktc_GetToken(&oldServer[i], &dummyToken, sizeof(struct ktc_token),
131                          &dummyPrincipal);
132
133         if (code != KTC_NOENT) {
134             printf("ktc_ForgetAllTokens did not eliminate all tokens.\n");
135             exit(1);
136         }
137
138         cellIndex = 0;
139
140         code = ktc_ListTokens(cellIndex, &cellIndex, &dummyPrincipal);
141
142         if (code != KTC_NOENT) {
143             printf("ktc_ForgetAllTokens did not eliminate all tokens.\n");
144             exit(1);
145         }
146     }
147
148
149     /* Reinstall tokens */
150
151     printf("\nReinstalling original tokens.\n");
152
153     for (i = 0; i < cellCount; i++) {
154         code = ktc_SetToken(&oldServer[i], &oldToken[i], &oldClient[i], 0);
155
156         if (code) {
157             perror("ktc_SetToken failed reinstalling tokens");
158             exit(1);
159         }
160     }
161
162
163     /* Get reinstalled tokens */
164
165     printf("\nFetching reinstalled tokens.\n");
166
167     cellIndex = 0;
168
169     for (i = 0; i < MAXCELLS; i++) {
170         /* fetch server principal */
171         code = ktc_ListTokens(cellIndex, &cellIndex, &newServer[i]);
172
173         if (code) {
174             if (code == KTC_NOENT) {
175                 /* no more tokens */
176                 break;
177             } else {
178                 /* some error occured */
179                 perror("ktc_ListTokens failed fetching reinstalled tokens");
180                 exit(1);
181             }
182         }
183
184         /* fetch token and client identity w.r.t. server */
185         code =
186             ktc_GetToken(&newServer[i], &newToken[i],
187                          sizeof(struct ktc_token), &newClient[i]);
188
189         if (code) {
190             /* some unexpected error occured */
191             perror("ktc_GetToken failed fetching reinstalled tokens");
192             exit(1);
193         }
194     }
195
196
197     /* Verify content of reinstalled tokens */
198
199     printf("\nVerifying reinstalled tokens against original tokens.\n");
200
201     if (i != cellCount) {
202         printf("Reinstalled token count does not match original count.\n");
203         exit(1);
204     }
205
206     for (i = 0; i < cellCount; i++) {
207         int k, found;
208         found = 0;
209
210         for (k = 0; k < cellCount; k++) {
211             if (SamePrincipal(&oldServer[i], &newServer[k])
212                 && SamePrincipal(&oldClient[i], &newClient[k])
213                 && SameToken(&oldToken[i], &newToken[k])) {
214                 /* found a matching token */
215                 found = 1;
216                 break;
217             }
218         }
219
220         if (!found) {
221             printf("Reinstalled token does not match any original token.\n");
222             exit(1);
223         }
224     }
225
226     /* Test passes */
227
228     printf("\nTest completed without error.\n");
229     return 0;
230 }
231
232
233 static int
234 SamePrincipal(struct ktc_principal *p1, struct ktc_principal *p2)
235 {
236     if (strcmp(p1->name, p2->name) || strcmp(p1->instance, p2->instance)
237         || strcmp(p1->cell, p2->cell)) {
238         /* principals do not match */
239         return 0;
240     } else {
241         /* same principal */
242         return 1;
243     }
244 }
245
246
247 static int
248 SameToken(struct ktc_token *t1, struct ktc_token *t2)
249 {
250     if ((t1->startTime != t2->startTime) || (t1->endTime != t2->endTime)
251         || memcmp(&t1->sessionKey, &t2->sessionKey, sizeof(t1->sessionKey))
252         || (t1->kvno != t2->kvno) || (t1->ticketLen != t2->ticketLen)
253         || memcmp(t1->ticket, t2->ticket, t1->ticketLen)) {
254         /* tokens do not match */
255         return 0;
256     } else {
257         /* same token */
258         return 1;
259     }
260 }