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