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