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 <afs/param.h>
24 #include <afsconfig.h>
32 #include <afs/comauth.h>
33 #include <sys/types.h>
34 #include <netinet/in.h>
35 #include <afs/cellconfig.h>
54 char cellName[MAXCELLCHARS];
58 SecretToken sToken, testSTok;
59 ClearToken cToken, testCTok;
60 struct AllTokenInfo origInfo[100]; /*All original token info*/
62 struct passwd *pw = &pwent;
63 struct passwd *lclpw = &pwent;
64 static char passwd[100] = { '\0' };
66 static char lclCellID[MAXCELLCHARS] = {'\0'}; /*Name of local cell*/
67 static char cellID[MAXCELLCHARS] = {'\0'}; /*Name of desired cell*/
68 int doLookup = TRUE; /*Assume pwd lookup needed*/
69 int foundUser = FALSE; /*Not yet, anyway*/
70 int foundPassword = FALSE; /*Not yet, anyway*/
71 int foundExplicitCell = FALSE; /*Not yet, anyway*/
72 int currArg = 0; /*Current (true) arg num*/
73 int usageError = FALSE; /*Did user screw up args?*/
74 int rc; /*Value of rpc return*/
75 int notPrimary; /*Log can't set primary ID*/
76 int useCellEntry; /*Do a cellular call?*/
77 int isPrimary; /*Were returned tokens primary?*/
78 int cellNum; /*Loop var for token gathering*/
81 * Get the tokens from the Cache Manager with both the cellular and non-cellular calls.
83 fprintf(stderr, "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;
88 fprintf(stderr, "Getting tokens from the Cache Manager (cellular call)\n");
92 printf("\t[%3d] ", cellNum);
93 rc = U_CellGetLocalTokens(useCellEntry, cellNum, &origInfo[cellNum].cTok, &origInfo[cellNum].sTok, origInfo[cellNum].cellName, &isPrimary);
95 /*Something didn't go well. Print out errno, unless we got an EDOM*/
97 printf("--End of list--\n");
99 printf("** Error in call, errno is %d\n", errno);
102 origInfo[cellNum].valid = 1;
103 printf("Vice ID %4d in cell '%s'%s\n",
104 origInfo[cellNum].cTok.ViceId, origInfo[cellNum].cellName,
105 (isPrimary ? " [Primary]" : ""));
108 } while (!(rc && (errno == EDOM)));
111 * Get our local cell's name and copy it into the desired cellID buffers (assume it'll
112 * be the target cell, too).
115 fprintf(stderr, "\nGetting local cell name\n");
117 rc = GetLocalCellName();
118 if (rc != CCONF_SUCCESS)
119 fprintf(stderr, "\tCan't get local cell name!\n");
120 strcpy(lclCellID, LclCellName);
121 strcpy(cellID, LclCellName);
122 fprintf(stderr, "\tUsing '%s' as the local cell name.\n", lclCellID);
125 * Parse our arguments. The current arg number is always 0.
128 while (currArg < argc) {
130 * Handle current argument.
133 fprintf(stderr, "Parsing arg %d.\n", currArg);
135 if (strcmp(argv[currArg], "-x") == 0) {
137 * Found -x flag. Remember not to do lookups if flag is the
138 * first true argument.
141 fprintf(stderr, "-x switch must appear before all other arguments.\n");
148 fprintf(stderr, "Found legal -x flag.\n");
151 else if (strcmp(argv[currArg], "-c") == 0) {
153 * Cell name explicitly mentioned; take it in if no other cell name has
154 * already been specified and if the name actually appears. If the
155 * given cell name differs from our own, we don't do a lookup.
157 if (foundExplicitCell) {
158 fprintf(stderr, "Only one -c switch allowed.\n");
162 if (currArg + 1 >= argc) {
163 fprintf(stderr, "Cell name must follow -c switch.\n");
167 foundExplicitCell = TRUE;
168 if (strcmp(argv[currArg], lclCellID) != 0) {
170 strcpy(cellID, argv[currArg+1]);
174 fprintf(stderr, "Found explicit cell name: '%s'\n", cellID);
177 else if (!foundUser) {
179 * If it's not a -x or a -c and we haven't found the user name yet, shove it into our
180 * local password entry buffer, remembering we have it.
183 lclpw->pw_name = argv[currArg];
186 fprintf(stderr, "Found user name: '%s'\n", lclpw->pw_name);
189 else if (!foundPassword) {
191 * Current argument is the desired password string. Remember it in our local
192 * buffer, and zero out the argument string - anyone can see it there with ps!
194 foundPassword = TRUE;
195 strcpy(passwd, argv[currArg]);
196 bzero(argv[currArg], strlen(passwd));
199 fprintf(stderr, "Found password: '%s' (%d chars), erased from arg list (now '%s').\n",
200 passwd, strlen(passwd), argv[currArg-1]);
205 * No more legal choices here. Remember to tell the user (constructively)
206 * that he screwed up.
215 * No arguments were provided; our only clue is the uid.
218 fprintf(stderr, "No arguments, using getpwuid(getuid()).\n");
220 pw = getpwuid(getuid());
222 fprintf (stderr, "\nCan't figure out your name in local cell '%s' from your user id.\n", lclCellID);
223 fprintf (stderr, "Try providing the user name.\n");
224 fprintf (stderr, "Usage: testlog [[-x] user [password] [-c cellname]]\n\n");
231 fprintf(stderr, "Found it, user name is '%s'.\n", lclpw->pw_name);
236 * Argument parsing is complete. If the user gave us bad arguments or didn't
237 * include a user, try to mend his evil ways.
239 if (usageError || !foundUser) {
240 fprintf (stderr, "Usage: testlog [[-x] user [password] [-c cellname]]\n\n");
245 * If we need to do a lookup on the user name (no -x flag, wants local cell, not already
246 * looked up), then do it.
249 pw = getpwnam(lclpw->pw_name);
251 fprintf(stderr, "'%s' is not a valid user in local cell '%s'.\n", lclpw->pw_name, lclCellID);
255 fprintf(stderr, "Lookup on user name '%s' succeeded.\n", pw->pw_name);
260 * Having all the info we need, we initialize our RPC connection.
262 if (U_InitRPC() != 0) {
263 fprintf (stderr, "%s: Problems with RPC (U_InitRPC failed).\n", argv[0]);
268 * Get the password if it was not provided.
270 if (passwd[0] == '\0') {
273 sprintf(buf, "Password for user '%s' in cell '%s': ",
274 lclpw->pw_name, cellID);
275 strcpy(passwd, getpass(buf));
279 * Get the corresponding set of tokens from an AuthServer.
281 fprintf(stderr, "Trying standard cellular authentication.\n");
285 if ((rc = U_CellAuthenticate(pw->pw_name, passwd, cellID, &cToken, &sToken))
287 fprintf(stderr, "\tInvalid login: code %d ('%s').\n", rc, U_Error(rc));
290 fprintf(stderr, "\tCell authentication successful.\n");
291 fprintf(stderr, "\tViceID field of clear token returned: %d\n",
297 * Give the non-primary tokens to the Cache Manager, along with the cell they're good for.
299 fprintf(stderr, "\nGiving tokens to Cache Manager (non-cellular call)\n");
300 rc = U_SetLocalTokens(0, &cToken, &sToken);
302 fprintf(stderr, "\tError: code %d ('%s')\n", rc, U_Error(rc));
303 fprintf(stderr, "Giving tokens to Cache Manager (cellular call, cell = '%s')\n",
306 if (rc = U_CellSetLocalTokens(0, &cToken, &sToken, cellID, notPrimary) !=
308 fprintf(stderr, "\tError: code %d ('%s')\n", rc, U_Error(rc));
311 * Restore all the original tokens.
313 fprintf(stderr, "\nRestoring all original tokens.\n");
314 for (cellNum = 0; cellNum < 100; cellNum++)
315 if (origInfo[cellNum].valid) {
316 fprintf(stderr, "\tuid %4d in cell '%s'\n",
317 origInfo[cellNum].cTok.ViceId, origInfo[cellNum].cellName);
318 if (rc = U_CellSetLocalTokens(0,
319 &origInfo[cellNum].cTok,
320 &origInfo[cellNum].sTok,
321 origInfo[cellNum].cellName,
322 notPrimary) != AUTH_SUCCESS)
323 fprintf(stderr, "\t** Error: code %d ('%s')\n", rc, U_Error(rc));