2 * Copyright 2000, International Business Machines Corporation and others.
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
12 testlog -- Test communication with the Andrew Cache Manager.
15 testlog [[-x] user [password] [-c cellname]]
18 -x indicates that no password file lookups are to be done.
19 -c identifies cellname as the cell in which authentication is to take place.
20 This implies -x, unless the given cellname matches our local one.
23 #include <afsconfig.h>
24 #include <afs/param.h>
31 #include <afs/comauth.h>
32 #include <sys/types.h>
33 #include <netinet/in.h>
34 #include <afs/cellconfig.h>
53 char cellName[MAXCELLCHARS];
57 SecretToken sToken, testSTok;
58 ClearToken cToken, testCTok;
59 struct AllTokenInfo origInfo[100]; /*All original token info */
61 struct passwd *pw = &pwent;
62 struct passwd *lclpw = &pwent;
63 static char passwd[100] = { '\0' };
65 static char lclCellID[MAXCELLCHARS] = { '\0' }; /*Name of local cell */
66 static char cellID[MAXCELLCHARS] = { '\0' }; /*Name of desired cell */
67 int doLookup = TRUE; /*Assume pwd lookup needed */
68 int foundUser = FALSE; /*Not yet, anyway */
69 int foundPassword = FALSE; /*Not yet, anyway */
70 int foundExplicitCell = FALSE; /*Not yet, anyway */
71 int currArg = 0; /*Current (true) arg num */
72 int usageError = FALSE; /*Did user screw up args? */
73 int rc; /*Value of rpc return */
74 int notPrimary; /*Log can't set primary ID */
75 int useCellEntry; /*Do a cellular call? */
76 int isPrimary; /*Were returned tokens primary? */
77 int cellNum; /*Loop var for token gathering */
80 * Get the tokens from the Cache Manager with both the cellular and non-cellular calls.
83 "Getting tokens from the Cache Manager (non-cellular call)\n");
84 U_GetLocalTokens(&testCTok, &testSTok);
86 for (cellNum = 0; cellNum < 100; cellNum++)
87 origInfo[cellNum].valid = 0;
89 "Getting tokens from the Cache Manager (cellular call)\n");
93 printf("\t[%3d] ", cellNum);
94 rc = U_CellGetLocalTokens(useCellEntry, cellNum,
95 &origInfo[cellNum].cTok,
96 &origInfo[cellNum].sTok,
97 origInfo[cellNum].cellName, &isPrimary);
99 /*Something didn't go well. Print out errno, unless we got an EDOM */
101 printf("--End of list--\n");
103 printf("** Error in call, errno is %d\n", errno);
105 origInfo[cellNum].valid = 1;
106 printf("Vice ID %4d in cell '%s'%s\n",
107 origInfo[cellNum].cTok.ViceId, origInfo[cellNum].cellName,
108 (isPrimary ? " [Primary]" : ""));
111 } while (!(rc && (errno == EDOM)));
114 * Get our local cell's name and copy it into the desired cellID buffers (assume it'll
115 * be the target cell, too).
118 fprintf(stderr, "\nGetting local cell name\n");
119 #endif /* DB_CELLS */
120 rc = GetLocalCellName();
121 if (rc != CCONF_SUCCESS)
122 fprintf(stderr, "\tCan't get local cell name!\n");
123 strcpy(lclCellID, LclCellName);
124 strcpy(cellID, LclCellName);
125 fprintf(stderr, "\tUsing '%s' as the local cell name.\n", lclCellID);
128 * Parse our arguments. The current arg number is always 0.
131 while (currArg < argc) {
133 * Handle current argument.
136 fprintf(stderr, "Parsing arg %d.\n", currArg);
137 #endif /* DB_ARGPARSE */
138 if (strcmp(argv[currArg], "-x") == 0) {
140 * Found -x flag. Remember not to do lookups if flag is the
141 * first true argument.
145 "-x switch must appear before all other arguments.\n");
152 fprintf(stderr, "Found legal -x flag.\n");
153 #endif /* DB_ARGPARSE */
154 } else if (strcmp(argv[currArg], "-c") == 0) {
156 * Cell name explicitly mentioned; take it in if no other cell name has
157 * already been specified and if the name actually appears. If the
158 * given cell name differs from our own, we don't do a lookup.
160 if (foundExplicitCell) {
161 fprintf(stderr, "Only one -c switch allowed.\n");
165 if (currArg + 1 >= argc) {
166 fprintf(stderr, "Cell name must follow -c switch.\n");
170 foundExplicitCell = TRUE;
171 if (strcmp(argv[currArg], lclCellID) != 0) {
173 strcpy(cellID, argv[currArg + 1]);
177 fprintf(stderr, "Found explicit cell name: '%s'\n", cellID);
178 #endif /* DB_ARGPARSE */
179 } else if (!foundUser) {
181 * If it's not a -x or a -c and we haven't found the user name yet, shove it into our
182 * local password entry buffer, remembering we have it.
185 lclpw->pw_name = argv[currArg];
188 fprintf(stderr, "Found user name: '%s'\n", lclpw->pw_name);
189 #endif /* DB_ARGPARSE */
190 } else if (!foundPassword) {
192 * Current argument is the desired password string. Remember it in our local
193 * buffer, and zero out the argument string - anyone can see it there with ps!
195 foundPassword = TRUE;
196 strcpy(passwd, argv[currArg]);
197 memset(argv[currArg], 0, strlen(passwd));
201 "Found password: '%s' (%d chars), erased from arg list (now '%s').\n",
202 passwd, strlen(passwd), argv[currArg - 1]);
203 #endif /* DB_ARGPARSE */
206 * No more legal choices here. Remember to tell the user (constructively)
207 * that he screwed up.
216 * No arguments were provided; our only clue is the uid.
219 fprintf(stderr, "No arguments, using getpwuid(getuid()).\n");
220 #endif /* DB_ARGPARSE */
221 pw = getpwuid(getuid());
224 "\nCan't figure out your name in local cell '%s' from your user id.\n",
226 fprintf(stderr, "Try providing the user name.\n");
228 "Usage: testlog [[-x] user [password] [-c cellname]]\n\n");
235 fprintf(stderr, "Found it, user name is '%s'.\n", lclpw->pw_name);
236 #endif /* DB_ARGPARSE */
240 * Argument parsing is complete. If the user gave us bad arguments or didn't
241 * include a user, try to mend his evil ways.
243 if (usageError || !foundUser) {
245 "Usage: testlog [[-x] user [password] [-c cellname]]\n\n");
250 * If we need to do a lookup on the user name (no -x flag, wants local cell, not already
251 * looked up), then do it.
254 pw = getpwnam(lclpw->pw_name);
256 fprintf(stderr, "'%s' is not a valid user in local cell '%s'.\n",
257 lclpw->pw_name, lclCellID);
261 fprintf(stderr, "Lookup on user name '%s' succeeded.\n", pw->pw_name);
262 #endif /* DB_ARGPARSE */
266 * Having all the info we need, we initialize our RPC connection.
268 if (U_InitRPC() != 0) {
269 fprintf(stderr, "%s: Problems with RPC (U_InitRPC failed).\n",
275 * Get the password if it was not provided.
277 if (passwd[0] == '\0') {
280 sprintf(buf, "Password for user '%s' in cell '%s': ", lclpw->pw_name,
282 strcpy(passwd, getpass(buf));
286 * Get the corresponding set of tokens from an AuthServer.
288 fprintf(stderr, "Trying standard cellular authentication.\n");
291 #endif /* DB_CELLS */
293 U_CellAuthenticate(pw->pw_name, passwd, cellID, &cToken, &sToken))
295 fprintf(stderr, "\tInvalid login: code %d ('%s').\n", rc,
299 fprintf(stderr, "\tCell authentication successful.\n");
300 fprintf(stderr, "\tViceID field of clear token returned: %d\n",
303 #endif /* DB_CELLS */
306 * Give the non-primary tokens to the Cache Manager, along with the cell they're good for.
308 fprintf(stderr, "\nGiving tokens to Cache Manager (non-cellular call)\n");
309 rc = U_SetLocalTokens(0, &cToken, &sToken);
311 fprintf(stderr, "\tError: code %d ('%s')\n", rc, U_Error(rc));
313 "Giving tokens to Cache Manager (cellular call, cell = '%s')\n",
317 U_CellSetLocalTokens(0, &cToken, &sToken, cellID,
318 notPrimary) != AUTH_SUCCESS)
319 fprintf(stderr, "\tError: code %d ('%s')\n", rc, U_Error(rc));
322 * Restore all the original tokens.
324 fprintf(stderr, "\nRestoring all original tokens.\n");
325 for (cellNum = 0; cellNum < 100; cellNum++)
326 if (origInfo[cellNum].valid) {
327 fprintf(stderr, "\tuid %4d in cell '%s'\n",
328 origInfo[cellNum].cTok.ViceId,
329 origInfo[cellNum].cellName);
331 U_CellSetLocalTokens(0, &origInfo[cellNum].cTok,
332 &origInfo[cellNum].sTok,
333 origInfo[cellNum].cellName,
334 notPrimary) != AUTH_SUCCESS)
335 fprintf(stderr, "\t** Error: code %d ('%s')\n", rc,