f64f97e71d457713b5489e1e74ac0d639d05ad8b
[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 #ifdef AFS_PTHREAD_ENV
16 # include <opr/softsig.h>
17 # include <afs/procmgmt_softsig.h> /* must come after softsig.h */
18 #endif
19
20 #ifdef AFS_NT40_ENV
21 #include <WINNT/afsevent.h>
22 #endif
23
24 #ifdef HAVE_SYS_FILE_H
25 #include <sys/file.h>
26 #endif
27
28 #include <rx/xdr.h>
29 #include <rx/rx.h>
30 #include <rx/rx_globals.h>
31 #include <rx/rxstat.h>
32 #include <afs/cmd.h>
33 #include <afs/cellconfig.h>
34 #include <afs/keys.h>
35 #include <afs/auth.h>
36 #include <afs/audit.h>
37 #include <afs/com_err.h>
38 #include <lock.h>
39 #include <ubik.h>
40 #include <afs/afsutil.h>
41
42 #include "vlserver.h"
43 #include "vlserver_internal.h"
44
45 #define MAXLWP 64
46 struct afsconf_dir *vldb_confdir = 0;   /* vldb configuration dir */
47 int lwps = 9;
48
49 struct vldstats dynamic_statistics;
50 struct ubik_dbase *VL_dbase;
51 afs_uint32 rd_HostAddress[MAXSERVERID + 1];
52 afs_uint32 wr_HostAddress[MAXSERVERID + 1];
53
54 static void *CheckSignal(void*);
55 int LogLevel = 0;
56 int smallMem = 0;
57 int restrictedQueryLevel = RESTRICTED_QUERY_ANYUSER;
58 int rxJumbograms = 0;           /* default is to not send and receive jumbo grams */
59 int rxMaxMTU = -1;
60 afs_int32 rxBind = 0;
61 int rxkadDisableDotCheck = 0;
62
63 #define ADDRSPERSITE 16         /* Same global is in rx/rx_user.c */
64 afs_uint32 SHostAddrs[ADDRSPERSITE];
65
66 static void
67 CheckSignal_Signal(int unused)
68 {
69 #if defined(AFS_PTHREAD_ENV)
70     CheckSignal(0);
71 #else
72     IOMGR_SoftSig(CheckSignal, 0);
73 #endif
74 }
75
76 static void *
77 CheckSignal(void *unused)
78 {
79     int i, errorcode;
80     struct vl_ctx ctx;
81
82     if ((errorcode =
83         Init_VLdbase(&ctx, LOCKREAD, VLGETSTATS - VL_LOWEST_OPCODE)))
84         return (void *)(intptr_t)errorcode;
85     VLog(0, ("Dump name hash table out\n"));
86     for (i = 0; i < HASHSIZE; i++) {
87         HashNDump(&ctx, i);
88     }
89     VLog(0, ("Dump id hash table out\n"));
90     for (i = 0; i < HASHSIZE; i++) {
91         HashIdDump(&ctx, i);
92     }
93     return ((void *)(intptr_t)ubik_EndTrans(ctx.trans));
94 }                               /*CheckSignal */
95
96
97 /* Initialize the stats for the opcodes */
98 void
99 initialize_dstats(void)
100 {
101     int i;
102
103     dynamic_statistics.start_time = (afs_uint32) time(0);
104     for (i = 0; i < MAX_NUMBER_OPCODES; i++) {
105         dynamic_statistics.requests[i] = 0;
106         dynamic_statistics.aborts[i] = 0;
107     }
108 }
109
110 /* check whether caller is authorized to manage RX statistics */
111 int
112 vldb_rxstat_userok(struct rx_call *call)
113 {
114     return afsconf_SuperUser(vldb_confdir, call, NULL);
115 }
116
117 /**
118  * Return true if this name is a member of the local realm.
119  */
120 int
121 vldb_IsLocalRealmMatch(void *rock, char *name, char *inst, char *cell)
122 {
123     struct afsconf_dir *dir = (struct afsconf_dir *)rock;
124     afs_int32 islocal = 0;      /* default to no */
125     int code;
126
127     code = afsconf_IsLocalRealmMatch(dir, &islocal, name, inst, cell);
128     if (code) {
129         VLog(0,
130                 ("Failed local realm check; code=%d, name=%s, inst=%s, cell=%s\n",
131                  code, name, inst, cell));
132     }
133     return islocal;
134 }
135
136 /* Main server module */
137
138 #include "AFS_component_version_number.c"
139
140 enum optionsList {
141     OPT_noauth,
142     OPT_smallmem,
143     OPT_auditlog,
144     OPT_auditiface,
145     OPT_config,
146     OPT_debug,
147     OPT_database,
148     OPT_logfile,
149     OPT_threads,
150     OPT_syslog,
151     OPT_peer,
152     OPT_process,
153     OPT_nojumbo,
154     OPT_jumbo,
155     OPT_rxbind,
156     OPT_rxmaxmtu,
157     OPT_trace,
158     OPT_dotted,
159     OPT_restricted_query
160 };
161
162 int
163 main(int argc, char **argv)
164 {
165     afs_int32 code;
166     afs_uint32 myHost;
167     struct rx_service *tservice;
168     struct rx_securityClass **securityClasses;
169     afs_int32 numClasses;
170     struct afsconf_dir *tdir;
171     struct ktc_encryptionKey tkey;
172     struct afsconf_cell info;
173     struct hostent *th;
174     char hostname[VL_MAXNAMELEN];
175     int noAuth = 0;
176     char clones[MAXHOSTSPERCELL];
177     afs_uint32 host = ntohl(INADDR_ANY);
178     struct cmd_syndesc *opts;
179
180     char *vl_dbaseName;
181     char *configDir;
182     char *logFile;
183
184     char *auditFileName = NULL;
185     char *interface = NULL;
186     char *optstring = NULL;
187
188     char *restricted_query_parameter = NULL;
189
190 #ifdef  AFS_AIX32_ENV
191     /*
192      * The following signal action for AIX is necessary so that in case of a
193      * crash (i.e. core is generated) we can include the user's data section
194      * in the core dump. Unfortunately, by default, only a partial core is
195      * generated which, in many cases, isn't too useful.
196      */
197     struct sigaction nsa;
198
199     rx_extraPackets = 100;      /* should be a switch, I guess... */
200     sigemptyset(&nsa.sa_mask);
201     nsa.sa_handler = SIG_DFL;
202     nsa.sa_flags = SA_FULLDUMP;
203     sigaction(SIGABRT, &nsa, NULL);
204     sigaction(SIGSEGV, &nsa, NULL);
205 #endif
206     osi_audit_init();
207
208     /* Initialize dirpaths */
209     if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
210 #ifdef AFS_NT40_ENV
211         ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
212 #endif
213         fprintf(stderr, "%s: Unable to obtain AFS server directory.\n",
214                 argv[0]);
215         exit(2);
216     }
217
218     vl_dbaseName = strdup(AFSDIR_SERVER_VLDB_FILEPATH);
219     configDir = strdup(AFSDIR_SERVER_ETC_DIRPATH);
220     logFile = strdup(AFSDIR_SERVER_VLOG_FILEPATH);
221
222     cmd_DisableAbbreviations();
223     cmd_DisablePositionalCommands();
224     opts = cmd_CreateSyntax(NULL, NULL, NULL, 0, NULL);
225
226     /* vlserver specific options */
227     cmd_AddParmAtOffset(opts, OPT_noauth, "-noauth", CMD_FLAG,
228                         CMD_OPTIONAL, "disable authentication");
229     cmd_AddParmAtOffset(opts, OPT_smallmem, "-smallmem", CMD_FLAG,
230                         CMD_OPTIONAL, "optimise for small memory systems");
231
232     /* general server options */
233     cmd_AddParmAtOffset(opts, OPT_auditlog, "-auditlog", CMD_SINGLE,
234                         CMD_OPTIONAL, "location of audit log");
235     cmd_AddParmAtOffset(opts, OPT_auditiface, "-audit-interface", CMD_SINGLE,
236                         CMD_OPTIONAL, "interface to use for audit logging");
237     cmd_AddParmAtOffset(opts, OPT_config, "-config", CMD_SINGLE,
238                         CMD_OPTIONAL, "configuration location");
239     cmd_AddParmAtOffset(opts, OPT_debug, "-d", CMD_SINGLE,
240                         CMD_OPTIONAL, "debug level");
241     cmd_AddParmAtOffset(opts, OPT_database, "-database", CMD_SINGLE,
242                         CMD_OPTIONAL, "database file");
243     cmd_AddParmAlias(opts, OPT_database, "-db");
244     cmd_AddParmAtOffset(opts, OPT_logfile, "-logfile", CMD_SINGLE,
245                         CMD_OPTIONAL, "location of logfile");
246     cmd_AddParmAtOffset(opts, OPT_threads, "-p", CMD_SINGLE, CMD_OPTIONAL,
247                         "number of threads");
248 #if !defined(AFS_NT40_ENV)
249     cmd_AddParmAtOffset(opts, OPT_syslog, "-syslog", CMD_SINGLE_OR_FLAG,
250                         CMD_OPTIONAL, "log to syslog");
251 #endif
252
253     /* rx options */
254     cmd_AddParmAtOffset(opts, OPT_peer, "-enable_peer_stats", CMD_FLAG,
255                         CMD_OPTIONAL, "enable RX transport statistics");
256     cmd_AddParmAtOffset(opts, OPT_process, "-enable_process_stats", CMD_FLAG,
257                         CMD_OPTIONAL, "enable RX RPC statistics");
258     cmd_AddParmAtOffset(opts, OPT_nojumbo, "-nojumbo", CMD_FLAG,
259                         CMD_OPTIONAL, "disable jumbograms");
260     cmd_AddParmAtOffset(opts, OPT_jumbo, "-jumbo", CMD_FLAG,
261                         CMD_OPTIONAL, "enable jumbograms");
262     cmd_AddParmAtOffset(opts, OPT_rxbind, "-rxbind", CMD_FLAG,
263                         CMD_OPTIONAL, "bind only to the primary interface");
264     cmd_AddParmAtOffset(opts, OPT_rxmaxmtu, "-rxmaxmtu", CMD_SINGLE,
265                         CMD_OPTIONAL, "maximum MTU for RX");
266     cmd_AddParmAtOffset(opts, OPT_trace, "-trace", CMD_SINGLE,
267                         CMD_OPTIONAL, "rx trace file");
268     cmd_AddParmAtOffset(opts, OPT_restricted_query, "-restricted_query",
269                         CMD_SINGLE, CMD_OPTIONAL, "anyuser | admin");
270
271
272     /* rxkad options */
273     cmd_AddParmAtOffset(opts, OPT_dotted, "-allow-dotted-principals",
274                         CMD_FLAG, CMD_OPTIONAL,
275                         "permit Kerberos 5 principals with dots");
276
277     code = cmd_Parse(argc, argv, &opts);
278     if (code == CMD_HELP) {
279         exit(0);
280     }
281     if (code)
282         return -1;
283
284     cmd_OptionAsString(opts, OPT_config, &configDir);
285
286     cmd_OpenConfigFile(AFSDIR_SERVER_CONFIG_FILE_FILEPATH);
287     cmd_SetCommandName("vlserver");
288
289     /* vlserver options */
290     cmd_OptionAsFlag(opts, OPT_noauth, &noAuth);
291     cmd_OptionAsFlag(opts, OPT_smallmem, &smallMem);
292     if (cmd_OptionAsString(opts, OPT_trace, &optstring) == 0) {
293         extern char rxi_tracename[80];
294         strcpy(rxi_tracename, optstring);
295         free(optstring);
296         optstring = NULL;
297     }
298
299     /* general server options */
300
301     cmd_OptionAsString(opts, OPT_auditlog, &auditFileName);
302
303     if (cmd_OptionAsString(opts, OPT_auditiface, &interface) == 0) {
304         if (osi_audit_interface(interface)) {
305             printf("Invalid audit interface '%s'\n", interface);
306             return -1;
307         }
308         free(interface);
309     }
310
311     cmd_OptionAsInt(opts, OPT_debug, &LogLevel);
312     cmd_OptionAsString(opts, OPT_database, &vl_dbaseName);
313     cmd_OptionAsString(opts, OPT_logfile, &logFile);
314
315     if (cmd_OptionAsInt(opts, OPT_threads, &lwps) == 0) {
316         if (lwps > MAXLWP) {
317              printf("Warning: '-p %d' is too big; using %d instead\n",
318                     lwps, MAXLWP);
319              lwps = MAXLWP;
320         }
321     }
322 #ifndef AFS_NT40_ENV
323     if (cmd_OptionPresent(opts, OPT_syslog)) {
324         serverLogSyslog = 1;
325         cmd_OptionAsInt(opts, OPT_syslog, &serverLogSyslogFacility);
326     }
327 #endif
328
329     /* rx options */
330     if (cmd_OptionPresent(opts, OPT_peer))
331         rx_enablePeerRPCStats();
332     if (cmd_OptionPresent(opts, OPT_process))
333         rx_enableProcessRPCStats();
334     if (cmd_OptionPresent(opts, OPT_nojumbo))
335         rxJumbograms = 0;
336     if (cmd_OptionPresent(opts, OPT_jumbo))
337         rxJumbograms = 1;
338
339     cmd_OptionAsFlag(opts, OPT_rxbind, &rxBind);
340
341     cmd_OptionAsInt(opts, OPT_rxmaxmtu, &rxMaxMTU);
342
343     /* rxkad options */
344     cmd_OptionAsFlag(opts, OPT_dotted, &rxkadDisableDotCheck);
345
346     /* restricted query */
347     if (cmd_OptionAsString(opts, OPT_restricted_query,
348                            &restricted_query_parameter) == 0) {
349         if (strcmp(restricted_query_parameter, "anyuser") == 0)
350             restrictedQueryLevel = RESTRICTED_QUERY_ANYUSER;
351         else if (strcmp(restricted_query_parameter, "admin") == 0)
352             restrictedQueryLevel = RESTRICTED_QUERY_ADMIN;
353         else {
354             printf("invalid argument for -restricted_query: %s\n",
355                    restricted_query_parameter);
356             return -1;
357         }
358         free(restricted_query_parameter);
359     }
360
361     if (auditFileName) {
362         osi_audit_file(auditFileName);
363     }
364
365 #ifndef AFS_NT40_ENV
366     serverLogSyslogTag = "vlserver";
367 #endif
368     OpenLog(logFile);   /* set up logging */
369 #ifdef AFS_PTHREAD_ENV
370     opr_softsig_Init();
371     SetupLogSoftSignals();
372 #else
373     SetupLogSignals();
374 #endif
375
376     tdir = afsconf_Open(configDir);
377     if (!tdir) {
378         VLog(0,
379             ("vlserver: can't open configuration files in dir %s, giving up.\n",
380              configDir));
381         exit(1);
382     }
383
384     /* initialize audit user check */
385     osi_audit_set_user_check(tdir, vldb_IsLocalRealmMatch);
386
387 #ifdef AFS_NT40_ENV
388     /* initialize winsock */
389     if (afs_winsockInit() < 0) {
390         ReportErrorEventAlt(AFSEVT_SVR_WINSOCK_INIT_FAILED, 0, argv[0], 0);
391         VLog(0, ("vlserver: couldn't initialize winsock. \n"));
392         exit(1);
393     }
394 #endif
395     /* get this host */
396     gethostname(hostname, sizeof(hostname));
397     th = gethostbyname(hostname);
398     if (!th) {
399         VLog(0, ("vlserver: couldn't get address of this host (%s).\n",
400                hostname));
401         exit(1);
402     }
403     memcpy(&myHost, th->h_addr, sizeof(afs_uint32));
404
405 #if !defined(AFS_HPUX_ENV) && !defined(AFS_NT40_ENV)
406     signal(SIGXCPU, CheckSignal_Signal);
407 #endif
408     /* get list of servers */
409     code =
410         afsconf_GetExtendedCellInfo(tdir, NULL, AFSCONF_VLDBSERVICE, &info,
411                                     clones);
412     if (code) {
413         printf("vlserver: Couldn't get cell server list for 'afsvldb'.\n");
414         exit(2);
415     }
416
417     vldb_confdir = tdir;        /* Preserve our configuration dir */
418     /* rxvab no longer supported */
419     memset(&tkey, 0, sizeof(tkey));
420
421     if (noAuth)
422         afsconf_SetNoAuthFlag(tdir, 1);
423
424     if (rxBind) {
425         afs_int32 ccode;
426 #ifndef AFS_NT40_ENV
427         if (AFSDIR_SERVER_NETRESTRICT_FILEPATH ||
428             AFSDIR_SERVER_NETINFO_FILEPATH) {
429             char reason[1024];
430             ccode = afsconf_ParseNetFiles(SHostAddrs, NULL, NULL,
431                                           ADDRSPERSITE, reason,
432                                           AFSDIR_SERVER_NETINFO_FILEPATH,
433                                           AFSDIR_SERVER_NETRESTRICT_FILEPATH);
434         } else
435 #endif
436         {
437             ccode = rx_getAllAddr(SHostAddrs, ADDRSPERSITE);
438         }
439         if (ccode == 1) {
440             host = SHostAddrs[0];
441             rx_InitHost(host, htons(AFSCONF_VLDBPORT));
442         }
443     }
444
445     if (!rxJumbograms) {
446         rx_SetNoJumbo();
447     }
448     if (rxMaxMTU != -1) {
449         if (rx_SetMaxMTU(rxMaxMTU) != 0) {
450             VLog(0, ("rxMaxMTU %d invalid\n", rxMaxMTU));
451             return -1;
452         }
453     }
454
455     ubik_nBuffers = 512;
456     ubik_SetClientSecurityProcs(afsconf_ClientAuth, afsconf_UpToDate, tdir);
457     ubik_SetServerSecurityProcs(afsconf_BuildServerSecurityObjects,
458                                 afsconf_CheckAuth, tdir);
459
460     ubik_SyncWriterCacheProc = vlsynccache;
461     code =
462         ubik_ServerInitByInfo(myHost, htons(AFSCONF_VLDBPORT), &info, clones,
463                               vl_dbaseName, &VL_dbase);
464     if (code) {
465         VLog(0, ("vlserver: Ubik init failed: %s\n", afs_error_message(code)));
466         exit(2);
467     }
468     rx_SetRxDeadTime(50);
469
470     memset(rd_HostAddress, 0, sizeof(rd_HostAddress));
471     memset(wr_HostAddress, 0, sizeof(wr_HostAddress));
472     initialize_dstats();
473
474     afsconf_BuildServerSecurityObjects(tdir, &securityClasses, &numClasses);
475
476     tservice =
477         rx_NewServiceHost(host, 0, USER_SERVICE_ID, "Vldb server",
478                           securityClasses, numClasses,
479                           VL_ExecuteRequest);
480     if (tservice == (struct rx_service *)0) {
481         VLog(0, ("vlserver: Could not create VLDB_SERVICE rx service\n"));
482         exit(3);
483     }
484     rx_SetMinProcs(tservice, 2);
485     if (lwps < 4)
486         lwps = 4;
487     rx_SetMaxProcs(tservice, lwps);
488
489     if (rxkadDisableDotCheck) {
490         rx_SetSecurityConfiguration(tservice, RXS_CONFIG_FLAGS,
491                                     (void *)RXS_CONFIG_FLAGS_DISABLE_DOTCHECK);
492     }
493
494     tservice =
495         rx_NewServiceHost(host, 0, RX_STATS_SERVICE_ID, "rpcstats",
496                           securityClasses, numClasses,
497                           RXSTATS_ExecuteRequest);
498     if (tservice == (struct rx_service *)0) {
499         VLog(0, ("vlserver: Could not create rpc stats rx service\n"));
500         exit(3);
501     }
502     rx_SetMinProcs(tservice, 2);
503     rx_SetMaxProcs(tservice, 4);
504
505     LogCommandLine(argc, argv, "vlserver", VldbVersion, "Starting AFS", FSLog);
506     if (afsconf_GetLatestKey(tdir, NULL, NULL) == 0) {
507         LogDesWarning();
508     }
509     VLog(0, ("%s\n", cml_version_number));
510
511     /* allow super users to manage RX statistics */
512     rx_SetRxStatUserOk(vldb_rxstat_userok);
513
514     rx_StartServer(1);          /* Why waste this idle process?? */
515
516     return 0; /* not reachable */
517 }