Remove the RCSID macro
[openafs.git] / src / log / test / testlog.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 /*
11 /*
12         testlog -- Test communication with the Andrew Cache Manager.
13
14         Usage:
15                 testlog [[-x] user [password] [-c cellname]]
16
17                 where:
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.
21 */
22
23 #include <afsconfig.h>
24 #include <afs/param.h>
25
26
27 #include <itc.h>
28 #include <stdio.h>
29 #include <pwd.h>
30 #include <r/xdr.h>
31 #include <afs/comauth.h>
32 #include <sys/types.h>
33 #include <netinet/in.h>
34 #include <afs/cellconfig.h>
35 #include <errno.h>
36
37 extern int errno;
38
39 #define DB_CELLS    1
40 #define DB_ARGPARSE 0
41
42
43
44 main(argc, argv)
45      int argc;
46      char *argv[];
47
48 {                               /*Main routine */
49
50     struct AllTokenInfo {
51         SecretToken sTok;
52         ClearToken cTok;
53         char cellName[MAXCELLCHARS];
54         int valid;
55     };
56
57     SecretToken sToken, testSTok;
58     ClearToken cToken, testCTok;
59     struct AllTokenInfo origInfo[100];  /*All original token info */
60     struct passwd pwent;
61     struct passwd *pw = &pwent;
62     struct passwd *lclpw = &pwent;
63     static char passwd[100] = { '\0' };
64
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 */
78
79     /*
80      * Get the tokens from the Cache Manager with both the cellular and non-cellular calls.
81      */
82     fprintf(stderr,
83             "Getting tokens from the Cache Manager (non-cellular call)\n");
84     U_GetLocalTokens(&testCTok, &testSTok);
85
86     for (cellNum = 0; cellNum < 100; cellNum++)
87         origInfo[cellNum].valid = 0;
88     fprintf(stderr,
89             "Getting tokens from the Cache Manager (cellular call)\n");
90     useCellEntry = 1;
91     cellNum = 0;
92     do {
93         printf("\t[%3d] ", cellNum);
94         rc = U_CellGetLocalTokens(useCellEntry, cellNum,
95                                   &origInfo[cellNum].cTok,
96                                   &origInfo[cellNum].sTok,
97                                   origInfo[cellNum].cellName, &isPrimary);
98         if (rc) {
99             /*Something didn't go well.  Print out errno, unless we got an EDOM */
100             if (errno == EDOM)
101                 printf("--End of list--\n");
102             else
103                 printf("** Error in call, errno is %d\n", errno);
104         } else {
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]" : ""));
109         }
110         cellNum++;
111     } while (!(rc && (errno == EDOM)));
112
113     /*
114      * Get our local cell's name and copy it into the desired cellID buffers (assume it'll
115      * be the target cell, too).
116      */
117 #if DB_CELLS
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);
126
127     /*
128      * Parse our arguments.  The current arg number is always 0.
129      */
130     currArg = 1;
131     while (currArg < argc) {
132         /*
133          * Handle current argument.
134          */
135 #if DB_ARGPARSE
136         fprintf(stderr, "Parsing arg %d.\n", currArg);
137 #endif /* DB_ARGPARSE */
138         if (strcmp(argv[currArg], "-x") == 0) {
139             /*
140              * Found -x flag.  Remember not to do lookups if flag is the
141              * first true argument.
142              */
143             if (currArg != 1) {
144                 fprintf(stderr,
145                         "-x switch must appear before all other arguments.\n");
146                 usageError = TRUE;
147                 break;
148             }
149             doLookup = FALSE;
150             currArg++;
151 #if DB_ARGPARSE
152             fprintf(stderr, "Found legal -x flag.\n");
153 #endif /* DB_ARGPARSE */
154         } else if (strcmp(argv[currArg], "-c") == 0) {
155             /*
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.
159              */
160             if (foundExplicitCell) {
161                 fprintf(stderr, "Only one -c switch allowed.\n");
162                 usageError = TRUE;
163                 break;
164             }
165             if (currArg + 1 >= argc) {
166                 fprintf(stderr, "Cell name must follow -c switch.\n");
167                 usageError = TRUE;
168                 break;
169             }
170             foundExplicitCell = TRUE;
171             if (strcmp(argv[currArg], lclCellID) != 0) {
172                 doLookup = FALSE;
173                 strcpy(cellID, argv[currArg + 1]);
174             }
175             currArg += 2;
176 #if DB_ARGPARSE
177             fprintf(stderr, "Found explicit cell name: '%s'\n", cellID);
178 #endif /* DB_ARGPARSE */
179         } else if (!foundUser) {
180             /*
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.
183              */
184             foundUser = TRUE;
185             lclpw->pw_name = argv[currArg];
186             currArg++;
187 #if DB_ARGPARSE
188             fprintf(stderr, "Found user name: '%s'\n", lclpw->pw_name);
189 #endif /* DB_ARGPARSE */
190         } else if (!foundPassword) {
191             /*
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!
194              */
195             foundPassword = TRUE;
196             strcpy(passwd, argv[currArg]);
197             memset(argv[currArg], 0, strlen(passwd));
198             currArg++;
199 #if DB_ARGPARSE
200             fprintf(stderr,
201                     "Found password: '%s' (%d chars), erased from arg list (now '%s').\n",
202                     passwd, strlen(passwd), argv[currArg - 1]);
203 #endif /* DB_ARGPARSE */
204         } else {
205             /*
206              * No more legal choices here.  Remember to tell the user (constructively)
207              * that he screwed up.
208              */
209             usageError = TRUE;
210             break;
211         }
212     }                           /*end while */
213
214     if (argc < 2) {
215         /*
216          * No arguments were provided; our only clue is the uid.
217          */
218 #if DB_ARGPARSE
219         fprintf(stderr, "No arguments, using getpwuid(getuid()).\n");
220 #endif /* DB_ARGPARSE */
221         pw = getpwuid(getuid());
222         if (pw == NULL) {
223             fprintf(stderr,
224                     "\nCan't figure out your name in local cell '%s' from your user id.\n",
225                     lclCellID);
226             fprintf(stderr, "Try providing the user name.\n");
227             fprintf(stderr,
228                     "Usage: testlog [[-x] user [password] [-c cellname]]\n\n");
229             exit(1);
230         }
231         foundUser = TRUE;
232         doLookup = FALSE;
233         lclpw = pw;
234 #if DB_ARGPARSE
235         fprintf(stderr, "Found it, user name is '%s'.\n", lclpw->pw_name);
236 #endif /* DB_ARGPARSE */
237     }
238
239     /* 
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.
242      */
243     if (usageError || !foundUser) {
244         fprintf(stderr,
245                 "Usage: testlog [[-x] user [password] [-c cellname]]\n\n");
246         exit(1);
247     }
248
249     /*
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.
252      */
253     if (doLookup) {
254         pw = getpwnam(lclpw->pw_name);
255         if (pw == NULL) {
256             fprintf(stderr, "'%s' is not a valid user in local cell '%s'.\n",
257                     lclpw->pw_name, lclCellID);
258             exit(1);
259         }
260 #if DB_ARGPARSE
261         fprintf(stderr, "Lookup on user name '%s' succeeded.\n", pw->pw_name);
262 #endif /* DB_ARGPARSE */
263     }
264
265     /*
266      * Having all the info we need, we initialize our RPC connection.
267      */
268     if (U_InitRPC() != 0) {
269         fprintf(stderr, "%s: Problems with RPC (U_InitRPC failed).\n",
270                 argv[0]);
271         exit(1);
272     }
273
274     /*
275      * Get the password if it was not provided.
276      */
277     if (passwd[0] == '\0') {
278         char buf[128];
279
280         sprintf(buf, "Password for user '%s' in cell '%s': ", lclpw->pw_name,
281                 cellID);
282         strcpy(passwd, getpass(buf));
283     }
284
285     /*
286      * Get the corresponding set of tokens from an AuthServer.
287      */
288     fprintf(stderr, "Trying standard cellular authentication.\n");
289 #if DB_CELLS
290     cToken.ViceId = 123;
291 #endif /* DB_CELLS */
292     if ((rc =
293          U_CellAuthenticate(pw->pw_name, passwd, cellID, &cToken, &sToken))
294         != AUTH_SUCCESS)
295         fprintf(stderr, "\tInvalid login: code %d ('%s').\n", rc,
296                 U_Error(rc));
297 #if DB_CELLS
298     else {
299         fprintf(stderr, "\tCell authentication successful.\n");
300         fprintf(stderr, "\tViceID field of clear token returned: %d\n",
301                 cToken.ViceId);
302     }
303 #endif /* DB_CELLS */
304
305     /*
306      * Give the non-primary tokens to the Cache Manager, along with the cell they're good for.
307      */
308     fprintf(stderr, "\nGiving tokens to Cache Manager (non-cellular call)\n");
309     rc = U_SetLocalTokens(0, &cToken, &sToken);
310     if (rc)
311         fprintf(stderr, "\tError: code %d ('%s')\n", rc, U_Error(rc));
312     fprintf(stderr,
313             "Giving tokens to Cache Manager (cellular call, cell = '%s')\n",
314             cellID);
315     notPrimary = 0;
316     if (rc =
317         U_CellSetLocalTokens(0, &cToken, &sToken, cellID,
318                              notPrimary) != AUTH_SUCCESS)
319         fprintf(stderr, "\tError: code %d ('%s')\n", rc, U_Error(rc));
320
321     /*
322      * Restore all the original tokens.
323      */
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);
330             if (rc =
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,
336                         U_Error(rc));
337         }
338
339 }                               /*Main routine */