audit: remove static local realms
[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         ViceLog(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     /* vlserver options */
271     cmd_OptionAsFlag(opts, OPT_noauth, &noAuth);
272     cmd_OptionAsFlag(opts, OPT_smallmem, &smallMem);
273     if (cmd_OptionAsString(opts, OPT_trace, &optstring) == 0) {
274         extern char rxi_tracename[80];
275         strcpy(rxi_tracename, optstring);
276         free(optstring);
277         optstring = NULL;
278     }
279
280     /* general server options */
281
282     cmd_OptionAsString(opts, OPT_auditlog, &auditFileName);
283
284     if (cmd_OptionAsString(opts, OPT_auditiface, &interface) == 0) {
285         if (osi_audit_interface(interface)) {
286             printf("Invalid audit interface '%s'\n", interface);
287             return -1;
288         }
289         free(interface);
290     }
291
292     cmd_OptionAsString(opts, OPT_config, &configDir);
293     cmd_OptionAsInt(opts, OPT_debug, &LogLevel);
294     cmd_OptionAsString(opts, OPT_database, &vl_dbaseName);
295     cmd_OptionAsString(opts, OPT_logfile, &logFile);
296
297     if (cmd_OptionAsInt(opts, OPT_threads, &lwps) == 0) {
298         if (lwps > MAXLWP) {
299              printf("Warning: '-p %d' is too big; using %d instead\n",
300                     lwps, MAXLWP);
301              lwps = MAXLWP;
302         }
303     }
304 #ifndef AFS_NT40_ENV
305     if (cmd_OptionPresent(opts, OPT_syslog)) {
306         serverLogSyslog = 1;
307         cmd_OptionAsInt(opts, OPT_syslog, &serverLogSyslogFacility);
308     }
309 #endif
310
311     /* rx options */
312     if (cmd_OptionPresent(opts, OPT_peer))
313         rx_enablePeerRPCStats();
314     if (cmd_OptionPresent(opts, OPT_process))
315         rx_enableProcessRPCStats();
316     if (cmd_OptionPresent(opts, OPT_nojumbo))
317         rxJumbograms = 0;
318     if (cmd_OptionPresent(opts, OPT_jumbo))
319         rxJumbograms = 1;
320
321     cmd_OptionAsFlag(opts, OPT_rxbind, &rxBind);
322
323     cmd_OptionAsInt(opts, OPT_rxmaxmtu, &rxMaxMTU);
324
325     /* rxkad options */
326     cmd_OptionAsFlag(opts, OPT_dotted, &rxkadDisableDotCheck);
327
328     if (auditFileName) {
329         osi_audit_file(auditFileName);
330     }
331
332 #ifndef AFS_NT40_ENV
333     serverLogSyslogTag = "vlserver";
334 #endif
335     OpenLog(logFile);   /* set up logging */
336     SetupLogSignals();
337
338     tdir = afsconf_Open(configDir);
339     if (!tdir) {
340         printf
341             ("vlserver: can't open configuration files in dir %s, giving up.\n",
342              configDir);
343         exit(1);
344     }
345
346     /* initialize audit user check */
347     osi_audit_set_user_check(configDir, vldb_IsLocalRealmMatch);
348
349 #ifdef AFS_NT40_ENV
350     /* initialize winsock */
351     if (afs_winsockInit() < 0) {
352         ReportErrorEventAlt(AFSEVT_SVR_WINSOCK_INIT_FAILED, 0, argv[0], 0);
353         fprintf(stderr, "vlserver: couldn't initialize winsock. \n");
354         exit(1);
355     }
356 #endif
357     /* get this host */
358     gethostname(hostname, sizeof(hostname));
359     th = gethostbyname(hostname);
360     if (!th) {
361         printf("vlserver: couldn't get address of this host (%s).\n",
362                hostname);
363         exit(1);
364     }
365     memcpy(&myHost, th->h_addr, sizeof(afs_uint32));
366
367 #if !defined(AFS_HPUX_ENV) && !defined(AFS_NT40_ENV)
368     signal(SIGXCPU, CheckSignal_Signal);
369 #endif
370     /* get list of servers */
371     code =
372         afsconf_GetExtendedCellInfo(tdir, NULL, AFSCONF_VLDBSERVICE, &info,
373                                     clones);
374     if (code) {
375         printf("vlserver: Couldn't get cell server list for 'afsvldb'.\n");
376         exit(2);
377     }
378
379     vldb_confdir = tdir;        /* Preserve our configuration dir */
380     /* rxvab no longer supported */
381     memset(&tkey, 0, sizeof(tkey));
382
383     if (noAuth)
384         afsconf_SetNoAuthFlag(tdir, 1);
385
386     if (rxBind) {
387         afs_int32 ccode;
388 #ifndef AFS_NT40_ENV
389         if (AFSDIR_SERVER_NETRESTRICT_FILEPATH ||
390             AFSDIR_SERVER_NETINFO_FILEPATH) {
391             char reason[1024];
392             ccode = parseNetFiles(SHostAddrs, NULL, NULL,
393                                   ADDRSPERSITE, reason,
394                                   AFSDIR_SERVER_NETINFO_FILEPATH,
395                                   AFSDIR_SERVER_NETRESTRICT_FILEPATH);
396         } else
397 #endif
398         {
399             ccode = rx_getAllAddr(SHostAddrs, ADDRSPERSITE);
400         }
401         if (ccode == 1) {
402             host = SHostAddrs[0];
403             rx_InitHost(host, htons(AFSCONF_VLDBPORT));
404         }
405     }
406
407     ubik_nBuffers = 512;
408     ubik_SetClientSecurityProcs(afsconf_ClientAuth, afsconf_UpToDate, tdir);
409     ubik_SetServerSecurityProcs(afsconf_BuildServerSecurityObjects,
410                                 afsconf_CheckAuth, tdir);
411
412     ubik_SyncWriterCacheProc = vlsynccache;
413     code =
414         ubik_ServerInitByInfo(myHost, htons(AFSCONF_VLDBPORT), &info, clones,
415                               vl_dbaseName, &VL_dbase);
416     if (code) {
417         printf("vlserver: Ubik init failed: %s\n", afs_error_message(code));
418         exit(2);
419     }
420     if (!rxJumbograms) {
421         rx_SetNoJumbo();
422     }
423     if (rxMaxMTU != -1) {
424         if (rx_SetMaxMTU(rxMaxMTU) != 0) {
425             printf("rxMaxMTU %d invalid\n", rxMaxMTU);
426             return -1;
427         }
428     }
429     rx_SetRxDeadTime(50);
430
431     memset(rd_HostAddress, 0, sizeof(rd_HostAddress));
432     memset(wr_HostAddress, 0, sizeof(wr_HostAddress));
433     initialize_dstats();
434
435     afsconf_BuildServerSecurityObjects(tdir, &securityClasses, &numClasses);
436
437     tservice =
438         rx_NewServiceHost(host, 0, USER_SERVICE_ID, "Vldb server",
439                           securityClasses, numClasses,
440                           VL_ExecuteRequest);
441     if (tservice == (struct rx_service *)0) {
442         printf("vlserver: Could not create VLDB_SERVICE rx service\n");
443         exit(3);
444     }
445     rx_SetMinProcs(tservice, 2);
446     if (lwps < 4)
447         lwps = 4;
448     rx_SetMaxProcs(tservice, lwps);
449
450     if (rxkadDisableDotCheck) {
451         rx_SetSecurityConfiguration(tservice, RXS_CONFIG_FLAGS,
452                                     (void *)RXS_CONFIG_FLAGS_DISABLE_DOTCHECK);
453     }
454
455     tservice =
456         rx_NewServiceHost(host, 0, RX_STATS_SERVICE_ID, "rpcstats",
457                           securityClasses, numClasses,
458                           RXSTATS_ExecuteRequest);
459     if (tservice == (struct rx_service *)0) {
460         printf("vlserver: Could not create rpc stats rx service\n");
461         exit(3);
462     }
463     rx_SetMinProcs(tservice, 2);
464     rx_SetMaxProcs(tservice, 4);
465
466     LogCommandLine(argc, argv, "vlserver", VldbVersion, "Starting AFS", FSLog);
467     printf("%s\n", cml_version_number); /* Goes to the log */
468
469     /* allow super users to manage RX statistics */
470     rx_SetRxStatUserOk(vldb_rxstat_userok);
471
472     rx_StartServer(1);          /* Why waste this idle process?? */
473
474     return 0; /* not reachable */
475 }