Remove the RCSID macro
[openafs.git] / src / vlserver / vlserver.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 #include <afsconfig.h>
11 #include <afs/param.h>
12
13
14 #include <afs/stds.h>
15 #include <sys/types.h>
16 #include <signal.h>
17 #include <sys/stat.h>
18 #include <string.h>
19 #ifdef HAVE_FCNTL_H
20 #include <fcntl.h>
21 #endif
22 #ifdef AFS_NT40_ENV
23 #include <winsock2.h>
24 #include <WINNT/afsevent.h>
25 #endif
26 #ifdef HAVE_SYS_FILE_H
27 #include <sys/file.h>
28 #endif
29 #include <time.h>
30 #ifdef HAVE_NETDB_H
31 #include <netdb.h>
32 #endif
33 #ifdef HAVE_NETINET_IN_H
34 #include <netinet/in.h>
35 #endif
36 #include <stdio.h>
37
38 #include <rx/xdr.h>
39 #include <rx/rx.h>
40 #include <rx/rx_globals.h>
41 #include <afs/cellconfig.h>
42 #include <afs/keys.h>
43 #include <afs/auth.h>
44 #include <lock.h>
45 #include <ubik.h>
46 #include <afs/afsutil.h>
47 #include "vlserver.h"
48
49
50 #define MAXLWP 16
51 const char *vl_dbaseName;
52 struct afsconf_dir *vldb_confdir = 0;   /* vldb configuration dir */
53 int lwps = 9;
54
55 struct vldstats dynamic_statistics;
56 struct ubik_dbase *VL_dbase;
57 afs_uint32 HostAddress[MAXSERVERID + 1];
58 extern int afsconf_CheckAuth();
59 extern int afsconf_ServerAuth();
60
61 static void *CheckSignal(void*);
62 int LogLevel = 0;
63 int smallMem = 0;
64 int rxJumbograms = 0;           /* default is to not send and receive jumbo grams */
65 int rxMaxMTU = -1;
66 afs_int32 rxBind = 0;
67 int rxkadDisableDotCheck = 0;
68 int debuglevel = 0;
69
70 #define ADDRSPERSITE 16         /* Same global is in rx/rx_user.c */
71 afs_uint32 SHostAddrs[ADDRSPERSITE];
72
73 static void
74 CheckSignal_Signal()
75 {
76 #if defined(AFS_PTHREAD_ENV)
77     CheckSignal(0);
78 #else
79     IOMGR_SoftSig(CheckSignal, 0);
80 #endif
81 }
82
83 static void *
84 CheckSignal(void *unused)
85 {
86     register int i, errorcode;
87     struct ubik_trans *trans;
88
89     if (errorcode =
90         Init_VLdbase(&trans, LOCKREAD, VLGETSTATS - VL_LOWEST_OPCODE))
91         return (void *)errorcode;
92     VLog(0, ("Dump name hash table out\n"));
93     for (i = 0; i < HASHSIZE; i++) {
94         HashNDump(trans, i);
95     }
96     VLog(0, ("Dump id hash table out\n"));
97     for (i = 0; i < HASHSIZE; i++) {
98         HashIdDump(trans, i);
99     }
100     return ((void *)ubik_EndTrans(trans));
101 }                               /*CheckSignal */
102
103
104 /* Initialize the stats for the opcodes */
105 void
106 initialize_dstats()
107 {
108     int i;
109
110     dynamic_statistics.start_time = (afs_uint32) time(0);
111     for (i = 0; i < MAX_NUMBER_OPCODES; i++) {
112         dynamic_statistics.requests[i] = 0;
113         dynamic_statistics.aborts[i] = 0;
114     }
115 }
116
117 /* check whether caller is authorized to manage RX statistics */
118 int
119 vldb_rxstat_userok(call)
120      struct rx_call *call;
121 {
122     return afsconf_SuperUser(vldb_confdir, call, NULL);
123 }
124
125 /* Main server module */
126
127 #include "AFS_component_version_number.c"
128
129 main(argc, argv)
130      int argc;
131      char **argv;
132 {
133     register afs_int32 code;
134     afs_int32 myHost;
135     struct rx_service *tservice;
136     struct rx_securityClass *sc[3];
137     extern int VL_ExecuteRequest();
138     extern int RXSTATS_ExecuteRequest();
139     struct afsconf_dir *tdir;
140     struct ktc_encryptionKey tkey;
141     struct afsconf_cell info;
142     struct hostent *th;
143     char hostname[VL_MAXNAMELEN];
144     int noAuth = 0, index, i;
145     extern int rx_extraPackets;
146     char commandLine[150];
147     char clones[MAXHOSTSPERCELL];
148     afs_uint32 host = ntohl(INADDR_ANY);
149
150 #ifdef  AFS_AIX32_ENV
151     /*
152      * The following signal action for AIX is necessary so that in case of a 
153      * crash (i.e. core is generated) we can include the user's data section 
154      * in the core dump. Unfortunately, by default, only a partial core is
155      * generated which, in many cases, isn't too useful.
156      */
157     struct sigaction nsa;
158
159     rx_extraPackets = 100;      /* should be a switch, I guess... */
160     sigemptyset(&nsa.sa_mask);
161     nsa.sa_handler = SIG_DFL;
162     nsa.sa_flags = SA_FULLDUMP;
163     sigaction(SIGABRT, &nsa, NULL);
164     sigaction(SIGSEGV, &nsa, NULL);
165 #endif
166     osi_audit_init();
167
168     /* Parse command line */
169     for (index = 1; index < argc; index++) {
170         if (strcmp(argv[index], "-noauth") == 0) {
171             noAuth = 1;
172         } else if (strcmp(argv[index], "-p") == 0) {
173             lwps = atoi(argv[++index]);
174             if (lwps > MAXLWP) {
175                 printf("Warning: '-p %d' is too big; using %d instead\n",
176                        lwps, MAXLWP);
177                 lwps = MAXLWP;
178             }
179         } else if (strcmp(argv[index], "-d") == 0) {
180             if ((index + 1) >= argc) {
181                 fprintf(stderr, "missing argument for -d\n"); 
182                 return -1; 
183             }
184             debuglevel = atoi(argv[++index]);
185             LogLevel = debuglevel;
186         } else if (strcmp(argv[index], "-nojumbo") == 0) {
187             rxJumbograms = 0;
188         } else if (strcmp(argv[index], "-jumbo") == 0) {
189             rxJumbograms = 1;
190         } else if (strcmp(argv[index], "-rxbind") == 0) {
191             rxBind = 1;
192         } else if (strcmp(argv[index], "-allow-dotted-principals") == 0) {
193             rxkadDisableDotCheck = 1;
194         } else if (!strcmp(argv[index], "-rxmaxmtu")) {
195             if ((index + 1) >= argc) {
196                 fprintf(stderr, "missing argument for -rxmaxmtu\n"); 
197                 return -1; 
198             }
199             rxMaxMTU = atoi(argv[++index]);
200             if ((rxMaxMTU < RX_MIN_PACKET_SIZE) || 
201                 (rxMaxMTU > RX_MAX_PACKET_DATA_SIZE)) {
202                 printf("rxMaxMTU %lu invalid; must be between %d-%d\n",
203                        rxMaxMTU, RX_MIN_PACKET_SIZE, 
204                        RX_MAX_PACKET_DATA_SIZE);
205                 return -1;
206             }
207
208         } else if (strcmp(argv[index], "-smallmem") == 0) {
209             smallMem = 1;
210
211         } else if (strcmp(argv[index], "-trace") == 0) {
212             extern char rxi_tracename[80];
213             strcpy(rxi_tracename, argv[++index]);
214
215        } else if (strcmp(argv[index], "-auditlog") == 0) {
216            char *fileName = argv[++index];
217
218            osi_audit_file(fileName);
219         } else if (strcmp(argv[index], "-enable_peer_stats") == 0) {
220             rx_enablePeerRPCStats();
221         } else if (strcmp(argv[index], "-enable_process_stats") == 0) {
222             rx_enableProcessRPCStats();
223 #ifndef AFS_NT40_ENV
224         } else if (strcmp(argv[index], "-syslog") == 0) {
225             /* set syslog logging flag */
226             serverLogSyslog = 1;
227         } else if (strncmp(argv[index], "-syslog=", 8) == 0) {
228             serverLogSyslog = 1;
229             serverLogSyslogFacility = atoi(argv[index] + 8);
230 #endif
231         } else {
232             /* support help flag */
233 #ifndef AFS_NT40_ENV
234             printf("Usage: vlserver [-p <number of processes>] [-nojumbo] "
235                    "[-rxmaxmtu <bytes>] [-rxbind] [-allow-dotted-principals] "
236                    "[-auditlog <log path>] [-jumbo] [-d <debug level>] "
237                    "[-syslog[=FACILITY]] "
238                    "[-enable_peer_stats] [-enable_process_stats] "
239                    "[-help]\n");
240 #else
241             printf("Usage: vlserver [-p <number of processes>] [-nojumbo] "
242                    "[-rxmaxmtu <bytes>] [-rxbind] [-allow-dotted-principals] "
243                    "[-auditlog <log path>] [-jumbo] [-d <debug level>] "
244                    "[-enable_peer_stats] [-enable_process_stats] "
245                    "[-help]\n");
246 #endif
247             fflush(stdout);
248             exit(0);
249         }
250     }
251
252     /* Initialize dirpaths */
253     if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
254 #ifdef AFS_NT40_ENV
255         ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
256 #endif
257         fprintf(stderr, "%s: Unable to obtain AFS server directory.\n",
258                 argv[0]);
259         exit(2);
260     }
261     vl_dbaseName = AFSDIR_SERVER_VLDB_FILEPATH;
262
263 #ifndef AFS_NT40_ENV
264     serverLogSyslogTag = "vlserver";
265 #endif
266     OpenLog(AFSDIR_SERVER_VLOG_FILEPATH);       /* set up logging */
267     SetupLogSignals();
268
269     tdir = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH);
270     if (!tdir) {
271         printf
272             ("vlserver: can't open configuration files in dir %s, giving up.\n",
273              AFSDIR_SERVER_ETC_DIRPATH);
274         exit(1);
275     }
276 #ifdef AFS_NT40_ENV
277     /* initialize winsock */
278     if (afs_winsockInit() < 0) {
279         ReportErrorEventAlt(AFSEVT_SVR_WINSOCK_INIT_FAILED, 0, argv[0], 0);
280         fprintf(stderr, "vlserver: couldn't initialize winsock. \n");
281         exit(1);
282     }
283 #endif
284     /* get this host */
285     gethostname(hostname, sizeof(hostname));
286     th = gethostbyname(hostname);
287     if (!th) {
288         printf("vlserver: couldn't get address of this host (%s).\n",
289                hostname);
290         exit(1);
291     }
292     memcpy(&myHost, th->h_addr, sizeof(afs_int32));
293
294 #if !defined(AFS_HPUX_ENV) && !defined(AFS_NT40_ENV)
295     signal(SIGXCPU, CheckSignal_Signal);
296 #endif
297     /* get list of servers */
298     code =
299         afsconf_GetExtendedCellInfo(tdir, NULL, AFSCONF_VLDBSERVICE, &info,
300                                     &clones);
301     if (code) {
302         printf("vlserver: Couldn't get cell server list for 'afsvldb'.\n");
303         exit(2);
304     }
305
306     vldb_confdir = tdir;        /* Preserve our configuration dir */
307     /* rxvab no longer supported */
308     memset(&tkey, 0, sizeof(tkey));
309
310     if (noAuth)
311         afsconf_SetNoAuthFlag(tdir, 1);
312
313     if (rxBind) {
314         afs_int32 ccode;
315 #ifndef AFS_NT40_ENV
316         if (AFSDIR_SERVER_NETRESTRICT_FILEPATH || 
317             AFSDIR_SERVER_NETINFO_FILEPATH) {
318             char reason[1024];
319             ccode = parseNetFiles(SHostAddrs, NULL, NULL,
320                                   ADDRSPERSITE, reason,
321                                   AFSDIR_SERVER_NETINFO_FILEPATH,
322                                   AFSDIR_SERVER_NETRESTRICT_FILEPATH);
323         } else 
324 #endif  
325         {
326             ccode = rx_getAllAddr(SHostAddrs, ADDRSPERSITE);
327         }
328         if (ccode == 1) {
329             host = SHostAddrs[0];
330             rx_InitHost(host, htons(AFSCONF_VLDBPORT));
331         }
332     }
333
334     ubik_nBuffers = 512;
335     ubik_CRXSecurityProc = afsconf_ClientAuth;
336     ubik_CRXSecurityRock = (char *)tdir;
337     ubik_SRXSecurityProc = afsconf_ServerAuth;
338     ubik_SRXSecurityRock = (char *)tdir;
339     ubik_CheckRXSecurityProc = afsconf_CheckAuth;
340     ubik_CheckRXSecurityRock = (char *)tdir;
341     code =
342         ubik_ServerInitByInfo(myHost, htons(AFSCONF_VLDBPORT), &info, clones,
343                               vl_dbaseName, &VL_dbase);
344     if (code) {
345         printf("vlserver: Ubik init failed with code %d\n", code);
346         exit(2);
347     }
348     if (!rxJumbograms) {
349         rx_SetNoJumbo();
350     }
351     if (rxMaxMTU != -1) {
352         rx_SetMaxMTU(rxMaxMTU);
353     }
354     rx_SetRxDeadTime(50);
355
356     memset(HostAddress, 0, sizeof(HostAddress));
357     initialize_dstats();
358
359     sc[0] = rxnull_NewServerSecurityObject();
360     sc[1] = (struct rx_securityClass *)0;
361     sc[2] = rxkad_NewServerSecurityObject(0, tdir, afsconf_GetKey, NULL);
362
363     tservice =
364         rx_NewServiceHost(host, 0, USER_SERVICE_ID, "Vldb server", sc, 3,
365                       VL_ExecuteRequest);
366     if (tservice == (struct rx_service *)0) {
367         printf("vlserver: Could not create VLDB_SERVICE rx service\n");
368         exit(3);
369     }
370     rx_SetMinProcs(tservice, 2);
371     if (lwps < 4)
372         lwps = 4;
373     rx_SetMaxProcs(tservice, lwps);
374
375     if (rxkadDisableDotCheck) {
376         rx_SetSecurityConfiguration(tservice, RXS_CONFIG_FLAGS,
377                                     (void *)RXS_CONFIG_FLAGS_DISABLE_DOTCHECK);
378     }
379
380     tservice =
381         rx_NewServiceHost(host, 0, RX_STATS_SERVICE_ID, "rpcstats", sc, 3,
382                       RXSTATS_ExecuteRequest);
383     if (tservice == (struct rx_service *)0) {
384         printf("vlserver: Could not create rpc stats rx service\n");
385         exit(3);
386     }
387     rx_SetMinProcs(tservice, 2);
388     rx_SetMaxProcs(tservice, 4);
389
390     for (commandLine[0] = '\0', i = 0; i < argc; i++) {
391         if (i > 0)
392             strcat(commandLine, " ");
393         strcat(commandLine, argv[i]);
394     }
395     ViceLog(0,
396             ("Starting AFS vlserver %d (%s)\n", VLDBVERSION_4, commandLine));
397     printf("%s\n", cml_version_number); /* Goes to the log */
398
399     /* allow super users to manage RX statistics */
400     rx_SetRxStatUserOk(vldb_rxstat_userok);
401
402     rx_StartServer(1);          /* Why waste this idle process?? */
403
404     return 0; /* not reachable */
405 }