auth: update the auth test programs
[openafs.git] / src / auth / test / testcellconf.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 testcellconfig.c:
12
13     Test of the routines used by the FileServer to manipulate the cell/server database and
14     determine the local cell name:
15         1) Reading in the local cell name from file.
16         2) Reading in the cell/server database from disk.
17         3) Reporting the set of servers associated with a given cell name.
18         4) Printing out the contents of the cell/server database.
19         5) Reclaiming the space used by an in-memory database.
20
21 Creation date:
22     17 August 1987
23
24 --------------------------------------------------------------------------------------------------------------*/
25 #include <afsconfig.h>
26 #include <afs/param.h>
27
28
29 #include <sys/types.h>
30 #include <stddef.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <unistd.h>
34 #include <afs/afsutil.h>
35 #ifdef AFS_NT40_ENV
36 #include <winsock2.h>
37 #else
38 #include <netinet/in.h>
39 #endif
40 #include <afs/cellconfig.h>
41 #include <afs/cmd.h>
42
43 int _afsconf_Touch(struct afsconf_dir *adir);
44
45 enum optionsList {
46     OPT_confdir,
47     OPT_cell,
48     OPT_reload
49 };
50
51 static int
52 PrintOneCell(struct afsconf_cell *ainfo, void *arock, struct afsconf_dir *adir)
53 {
54     int i;
55     long temp;
56
57     printf("Cell %s:\n", ainfo->name);
58     for (i = 0; i < ainfo->numServers; i++) {
59         memcpy(&temp, &ainfo->hostAddr[i].sin_addr, sizeof(long));
60         printf(" %d host %s at %lx port %x\n", i, ainfo->hostName[i], temp,
61                ainfo->hostAddr[i].sin_port);
62     }
63     return 0;
64 }
65
66 static void
67 PrintClones(char *clones, int numServers)
68 {
69     int i;
70
71     printf("Clones:\n");
72     for (i = 0; i < numServers; i++) {
73         printf(" %d clone %s\n", i, (clones[i] ? "yes" : "no"));
74     }
75 }
76
77 static int
78 TestCellConfig(struct cmd_syndesc *as, void *arock)
79 {
80     struct afsconf_dir *theDir;
81     char tbuffer[1024];
82     struct afsconf_cell theCell;
83     int code;
84     char *dirName = NULL;
85     char clones[MAXHOSTSPERCELL];
86     int reload = 0;
87     struct cmd_item *cells = NULL;
88
89     memset(clones, 0, sizeof(clones));
90
91     cmd_OptionAsString(as, OPT_confdir, &dirName);
92     reload = cmd_OptionPresent(as, OPT_reload);
93
94     if (!dirName)
95         dirName = strdup(AFSDIR_SERVER_ETC_DIRPATH);
96     theDir = afsconf_Open(dirName);
97     if (!theDir) {
98         printf("could not open configuration files in '%s'\n", dirName);
99         free(dirName);
100         return 1;
101     }
102
103     /* get the cell */
104     code = afsconf_GetLocalCell(theDir, tbuffer, sizeof(tbuffer));
105     if (code != 0) {
106         printf("get local cell failed, code %d\n", code);
107         afsconf_Close(theDir);
108         free(dirName);
109         return 1;
110     }
111     printf("Local cell is '%s'\n\n", tbuffer);
112
113     if (cmd_OptionAsList(as, OPT_cell, &cells) != 0) {
114         printf("About to print cell database contents:\n");
115         afsconf_CellApply(theDir, PrintOneCell, 0);
116         printf("Done.\n\n");
117         /* do this junk once */
118         printf("start of special test\n");
119         code = afsconf_GetCellInfo(theDir, NULL, "afsprot", &theCell);
120         if (code)
121             printf("failed to find afsprot service (%d)\n", code);
122         else {
123             printf("AFSPROT service:\n");
124             PrintOneCell(&theCell, NULL, theDir);
125         }
126         code = afsconf_GetExtendedCellInfo(theDir, NULL, "afsprot", &theCell, clones);
127         if (code) {
128             printf("failed to find extended cell info (%d)\n", code);
129         } else {
130             printf("AFSPROT service extended info:\n");
131             PrintOneCell(&theCell, NULL, theDir);
132             PrintClones(clones, theCell.numServers);
133         }
134         code = afsconf_GetCellInfo(theDir, 0, "bozotheclown", &theCell);
135         if (code == 0)
136             printf("unexpectedly found service 'bozotheclown'\n");
137         code = afsconf_GetCellInfo(theDir, NULL, "telnet", &theCell);
138         printf("Here's the telnet service:\n");
139         PrintOneCell(&theCell, NULL, theDir);
140         printf("done with special test\n");
141     } else {
142         /* Now print out specified cell info. */
143         for (; cells != NULL; cells = cells->next) {
144             char *cell = cells->data;
145             code = afsconf_GetCellInfo(theDir, cell, 0, &theCell);
146             if (code) {
147                 printf("Could not find info for cell '%s', code %d\n",
148                        cell, code);
149             } else
150                 PrintOneCell(&theCell, NULL, theDir);
151
152             /* And print extended cell info. */
153             memset(clones, 0, sizeof(clones));
154             code = afsconf_GetExtendedCellInfo(theDir, cell, "afsprot", &theCell, clones);
155             if (code) {
156                 printf("Could not find extended info for cell '%s', code %d\n",
157                        cell, code);
158             } else {
159                 PrintOneCell(&theCell, NULL, theDir);
160                 PrintClones(clones, theCell.numServers);
161             }
162         }
163     }
164
165     if (reload) {
166         printf("Forcing reload\n");
167         code = _afsconf_Touch(theDir);
168         if (code) {
169             printf("Unable to touch cellservdb file (%d)\n", code);
170         } else {
171             sleep(2);
172             code = afsconf_GetCellInfo(theDir, NULL, "afsprot", &theCell);
173             if (code)
174                 printf("failed to find afsprot service (%d)\n", code);
175             else
176                 PrintOneCell(&theCell, NULL, theDir);
177         }
178     }
179
180     /* all done */
181     afsconf_Close(theDir);
182     free(dirName);
183     return 0;
184 }
185
186
187 int
188 main(int argc, char *argv[])
189 {
190     afs_int32 code;
191     struct cmd_syndesc *ts;
192
193 #ifdef AFS_NT40_ENV
194     WSADATA WSAjunk;
195     /* Start up sockets */
196     WSAStartup(0x0101, &WSAjunk);
197 #endif /* AFS_NT40_ENV */
198
199     ts = cmd_CreateSyntax("initcmd", TestCellConfig, NULL, 0, "Test cell configuration");
200     cmd_AddParmAtOffset(ts, OPT_confdir, "-confdir", CMD_SINGLE, CMD_OPTIONAL,
201                 "Configuration directory pathname");
202     cmd_AddParmAtOffset(ts, OPT_cell, "-cell", CMD_LIST, CMD_OPTIONAL, "Cell to display");
203     cmd_AddParmAtOffset(ts, OPT_reload, "-reload", CMD_FLAG, CMD_OPTIONAL, "Perform reload test");
204
205     code = cmd_Dispatch(argc, argv);
206     return code;
207 }