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