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