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