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