Log binding ip address and port during startup
[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 smallMem = 0;
56 int restrictedQueryLevel = RESTRICTED_QUERY_ANYUSER;
57 int rxJumbograms = 0;           /* default is to not send and receive jumbo grams */
58 int rxMaxMTU = -1;
59 afs_int32 rxBind = 0;
60 int rxkadDisableDotCheck = 0;
61
62 #define ADDRSPERSITE 16         /* Same global is in rx/rx_user.c */
63 afs_uint32 SHostAddrs[ADDRSPERSITE];
64
65 static void
66 CheckSignal_Signal(int unused)
67 {
68 #if defined(AFS_PTHREAD_ENV)
69     CheckSignal(0);
70 #else
71     IOMGR_SoftSig(CheckSignal, 0);
72 #endif
73 }
74
75 static void *
76 CheckSignal(void *unused)
77 {
78     int i, errorcode;
79     struct vl_ctx ctx;
80
81     if ((errorcode =
82         Init_VLdbase(&ctx, LOCKREAD, VLGETSTATS - VL_LOWEST_OPCODE)))
83         return (void *)(intptr_t)errorcode;
84     VLog(0, ("Dump name hash table out\n"));
85     for (i = 0; i < HASHSIZE; i++) {
86         HashNDump(&ctx, i);
87     }
88     VLog(0, ("Dump id hash table out\n"));
89     for (i = 0; i < HASHSIZE; i++) {
90         HashIdDump(&ctx, i);
91     }
92     return ((void *)(intptr_t)ubik_EndTrans(ctx.trans));
93 }                               /*CheckSignal */
94
95
96 /* Initialize the stats for the opcodes */
97 void
98 initialize_dstats(void)
99 {
100     int i;
101
102     dynamic_statistics.start_time = (afs_uint32) time(0);
103     for (i = 0; i < MAX_NUMBER_OPCODES; i++) {
104         dynamic_statistics.requests[i] = 0;
105         dynamic_statistics.aborts[i] = 0;
106     }
107 }
108
109 /* check whether caller is authorized to manage RX statistics */
110 int
111 vldb_rxstat_userok(struct rx_call *call)
112 {
113     return afsconf_SuperUser(vldb_confdir, call, NULL);
114 }
115
116 /**
117  * Return true if this name is a member of the local realm.
118  */
119 int
120 vldb_IsLocalRealmMatch(void *rock, char *name, char *inst, char *cell)
121 {
122     struct afsconf_dir *dir = (struct afsconf_dir *)rock;
123     afs_int32 islocal = 0;      /* default to no */
124     int code;
125
126     code = afsconf_IsLocalRealmMatch(dir, &islocal, name, inst, cell);
127     if (code) {
128         VLog(0,
129                 ("Failed local realm check; code=%d, name=%s, inst=%s, cell=%s\n",
130                  code, name, inst, cell));
131     }
132     return islocal;
133 }
134
135 /* Main server module */
136
137 #include "AFS_component_version_number.c"
138
139 enum optionsList {
140     OPT_noauth,
141     OPT_smallmem,
142     OPT_auditlog,
143     OPT_auditiface,
144     OPT_config,
145     OPT_debug,
146     OPT_database,
147     OPT_logfile,
148     OPT_threads,
149 #ifdef HAVE_SYSLOG
150     OPT_syslog,
151 #endif
152     OPT_peer,
153     OPT_process,
154     OPT_nojumbo,
155     OPT_jumbo,
156     OPT_rxbind,
157     OPT_rxmaxmtu,
158     OPT_trace,
159     OPT_dotted,
160     OPT_restricted_query,
161     OPT_transarc_logs
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
184     char *vl_dbaseName;
185     char *configDir;
186
187     char *auditFileName = NULL;
188     char *interface = NULL;
189     char *optstring = NULL;
190
191     char *restricted_query_parameter = NULL;
192
193 #ifdef  AFS_AIX32_ENV
194     /*
195      * The following signal action for AIX is necessary so that in case of a
196      * crash (i.e. core is generated) we can include the user's data section
197      * in the core dump. Unfortunately, by default, only a partial core is
198      * generated which, in many cases, isn't too useful.
199      */
200     struct sigaction nsa;
201
202     rx_extraPackets = 100;      /* should be a switch, I guess... */
203     sigemptyset(&nsa.sa_mask);
204     nsa.sa_handler = SIG_DFL;
205     nsa.sa_flags = SA_FULLDUMP;
206     sigaction(SIGABRT, &nsa, NULL);
207     sigaction(SIGSEGV, &nsa, NULL);
208 #endif
209     osi_audit_init();
210
211     memset(&logopts, 0, sizeof(logopts));
212
213     /* Initialize dirpaths */
214     if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
215 #ifdef AFS_NT40_ENV
216         ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
217 #endif
218         fprintf(stderr, "%s: Unable to obtain AFS server directory.\n",
219                 argv[0]);
220         exit(2);
221     }
222
223     vl_dbaseName = strdup(AFSDIR_SERVER_VLDB_FILEPATH);
224     configDir = strdup(AFSDIR_SERVER_ETC_DIRPATH);
225
226     cmd_DisableAbbreviations();
227     cmd_DisablePositionalCommands();
228     opts = cmd_CreateSyntax(NULL, NULL, NULL, 0, NULL);
229
230     /* vlserver specific options */
231     cmd_AddParmAtOffset(opts, OPT_noauth, "-noauth", CMD_FLAG,
232                         CMD_OPTIONAL, "disable authentication");
233     cmd_AddParmAtOffset(opts, OPT_smallmem, "-smallmem", CMD_FLAG,
234                         CMD_OPTIONAL, "optimise for small memory systems");
235
236     /* general server options */
237     cmd_AddParmAtOffset(opts, OPT_auditlog, "-auditlog", CMD_SINGLE,
238                         CMD_OPTIONAL, "location of audit log");
239     cmd_AddParmAtOffset(opts, OPT_auditiface, "-audit-interface", CMD_SINGLE,
240                         CMD_OPTIONAL, "interface to use for audit logging");
241     cmd_AddParmAtOffset(opts, OPT_config, "-config", CMD_SINGLE,
242                         CMD_OPTIONAL, "configuration location");
243     cmd_AddParmAtOffset(opts, OPT_debug, "-d", CMD_SINGLE,
244                         CMD_OPTIONAL, "debug level");
245     cmd_AddParmAtOffset(opts, OPT_database, "-database", CMD_SINGLE,
246                         CMD_OPTIONAL, "database file");
247     cmd_AddParmAlias(opts, OPT_database, "-db");
248     cmd_AddParmAtOffset(opts, OPT_logfile, "-logfile", CMD_SINGLE,
249                         CMD_OPTIONAL, "location of logfile");
250     cmd_AddParmAtOffset(opts, OPT_threads, "-p", CMD_SINGLE, CMD_OPTIONAL,
251                         "number of threads");
252 #ifdef HAVE_SYSLOG
253     cmd_AddParmAtOffset(opts, OPT_syslog, "-syslog", CMD_SINGLE_OR_FLAG,
254                         CMD_OPTIONAL, "log to syslog");
255 #endif
256     cmd_AddParmAtOffset(opts, OPT_transarc_logs, "-transarc-logs", CMD_FLAG,
257                         CMD_OPTIONAL, "enable Transarc style logging");
258
259     /* rx options */
260     cmd_AddParmAtOffset(opts, OPT_peer, "-enable_peer_stats", CMD_FLAG,
261                         CMD_OPTIONAL, "enable RX transport statistics");
262     cmd_AddParmAtOffset(opts, OPT_process, "-enable_process_stats", CMD_FLAG,
263                         CMD_OPTIONAL, "enable RX RPC statistics");
264     cmd_AddParmAtOffset(opts, OPT_nojumbo, "-nojumbo", CMD_FLAG,
265                         CMD_OPTIONAL, "disable jumbograms");
266     cmd_AddParmAtOffset(opts, OPT_jumbo, "-jumbo", CMD_FLAG,
267                         CMD_OPTIONAL, "enable jumbograms");
268     cmd_AddParmAtOffset(opts, OPT_rxbind, "-rxbind", CMD_FLAG,
269                         CMD_OPTIONAL, "bind only to the primary interface");
270     cmd_AddParmAtOffset(opts, OPT_rxmaxmtu, "-rxmaxmtu", CMD_SINGLE,
271                         CMD_OPTIONAL, "maximum MTU for RX");
272     cmd_AddParmAtOffset(opts, OPT_trace, "-trace", CMD_SINGLE,
273                         CMD_OPTIONAL, "rx trace file");
274     cmd_AddParmAtOffset(opts, OPT_restricted_query, "-restricted_query",
275                         CMD_SINGLE, CMD_OPTIONAL, "anyuser | admin");
276
277
278     /* rxkad options */
279     cmd_AddParmAtOffset(opts, OPT_dotted, "-allow-dotted-principals",
280                         CMD_FLAG, CMD_OPTIONAL,
281                         "permit Kerberos 5 principals with dots");
282
283     code = cmd_Parse(argc, argv, &opts);
284     if (code == CMD_HELP) {
285         exit(0);
286     }
287     if (code)
288         return -1;
289
290     cmd_OptionAsString(opts, OPT_config, &configDir);
291
292     cmd_OpenConfigFile(AFSDIR_SERVER_CONFIG_FILE_FILEPATH);
293     cmd_SetCommandName("vlserver");
294
295     /* vlserver options */
296     cmd_OptionAsFlag(opts, OPT_noauth, &noAuth);
297     cmd_OptionAsFlag(opts, OPT_smallmem, &smallMem);
298     if (cmd_OptionAsString(opts, OPT_trace, &optstring) == 0) {
299         extern char rxi_tracename[80];
300         strcpy(rxi_tracename, optstring);
301         free(optstring);
302         optstring = NULL;
303     }
304
305     /* general server options */
306
307     cmd_OptionAsString(opts, OPT_auditlog, &auditFileName);
308
309     if (cmd_OptionAsString(opts, OPT_auditiface, &interface) == 0) {
310         if (osi_audit_interface(interface)) {
311             printf("Invalid audit interface '%s'\n", interface);
312             return -1;
313         }
314         free(interface);
315     }
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     if (auditFileName) {
391         osi_audit_file(auditFileName);
392     }
393
394     OpenLog(&logopts);
395 #ifdef AFS_PTHREAD_ENV
396     opr_softsig_Init();
397     SetupLogSoftSignals();
398 #else
399     SetupLogSignals();
400 #endif
401
402     tdir = afsconf_Open(configDir);
403     if (!tdir) {
404         VLog(0,
405             ("vlserver: can't open configuration files in dir %s, giving up.\n",
406              configDir));
407         exit(1);
408     }
409
410     /* initialize audit user check */
411     osi_audit_set_user_check(tdir, vldb_IsLocalRealmMatch);
412
413 #ifdef AFS_NT40_ENV
414     /* initialize winsock */
415     if (afs_winsockInit() < 0) {
416         ReportErrorEventAlt(AFSEVT_SVR_WINSOCK_INIT_FAILED, 0, argv[0], 0);
417         VLog(0, ("vlserver: couldn't initialize winsock. \n"));
418         exit(1);
419     }
420 #endif
421     /* get this host */
422     gethostname(hostname, sizeof(hostname));
423     th = gethostbyname(hostname);
424     if (!th) {
425         VLog(0, ("vlserver: couldn't get address of this host (%s).\n",
426                hostname));
427         exit(1);
428     }
429     memcpy(&myHost, th->h_addr, sizeof(afs_uint32));
430
431 #if !defined(AFS_HPUX_ENV) && !defined(AFS_NT40_ENV)
432     signal(SIGXCPU, CheckSignal_Signal);
433 #endif
434     /* get list of servers */
435     code =
436         afsconf_GetExtendedCellInfo(tdir, NULL, AFSCONF_VLDBSERVICE, &info,
437                                     clones);
438     if (code) {
439         printf("vlserver: Couldn't get cell server list for 'afsvldb'.\n");
440         exit(2);
441     }
442
443     vldb_confdir = tdir;        /* Preserve our configuration dir */
444     /* rxvab no longer supported */
445     memset(&tkey, 0, sizeof(tkey));
446
447     if (noAuth)
448         afsconf_SetNoAuthFlag(tdir, 1);
449
450     if (rxBind) {
451         afs_int32 ccode;
452 #ifndef AFS_NT40_ENV
453         if (AFSDIR_SERVER_NETRESTRICT_FILEPATH ||
454             AFSDIR_SERVER_NETINFO_FILEPATH) {
455             char reason[1024];
456             ccode = afsconf_ParseNetFiles(SHostAddrs, NULL, NULL,
457                                           ADDRSPERSITE, reason,
458                                           AFSDIR_SERVER_NETINFO_FILEPATH,
459                                           AFSDIR_SERVER_NETRESTRICT_FILEPATH);
460         } else
461 #endif
462         {
463             ccode = rx_getAllAddr(SHostAddrs, ADDRSPERSITE);
464         }
465         if (ccode == 1) {
466             host = SHostAddrs[0];
467         }
468     }
469
470     if (!rxJumbograms) {
471         rx_SetNoJumbo();
472     }
473     if (rxMaxMTU != -1) {
474         if (rx_SetMaxMTU(rxMaxMTU) != 0) {
475             VLog(0, ("rxMaxMTU %d invalid\n", rxMaxMTU));
476             return -1;
477         }
478     }
479
480     VLog(0, ("vlserver binding rx to %s:%d\n",
481          afs_inet_ntoa_r(host, hoststr), AFSCONF_VLDBPORT));
482     code = rx_InitHost(host, htons(AFSCONF_VLDBPORT));
483     if (code < 0) {
484         VLog(0, ("vlserver: Rx init failed: %d\n", code));
485         exit(1);
486     }
487     rx_SetRxDeadTime(50);
488
489     ubik_nBuffers = 512;
490     ubik_SetClientSecurityProcs(afsconf_ClientAuth, afsconf_UpToDate, tdir);
491     ubik_SetServerSecurityProcs(afsconf_BuildServerSecurityObjects,
492                                 afsconf_CheckAuth, tdir);
493
494     ubik_SyncWriterCacheProc = vlsynccache;
495     code =
496         ubik_ServerInitByInfo(myHost, htons(AFSCONF_VLDBPORT), &info, clones,
497                               vl_dbaseName, &VL_dbase);
498     if (code) {
499         VLog(0, ("vlserver: Ubik init failed: %s\n", afs_error_message(code)));
500         exit(2);
501     }
502
503     memset(rd_HostAddress, 0, sizeof(rd_HostAddress));
504     memset(wr_HostAddress, 0, sizeof(wr_HostAddress));
505     initialize_dstats();
506
507     afsconf_BuildServerSecurityObjects(tdir, &securityClasses, &numClasses);
508
509     tservice =
510         rx_NewServiceHost(host, 0, USER_SERVICE_ID, "Vldb server",
511                           securityClasses, numClasses,
512                           VL_ExecuteRequest);
513     if (tservice == (struct rx_service *)0) {
514         VLog(0, ("vlserver: Could not create VLDB_SERVICE rx service\n"));
515         exit(3);
516     }
517     rx_SetMinProcs(tservice, 2);
518     if (lwps < 4)
519         lwps = 4;
520     rx_SetMaxProcs(tservice, lwps);
521
522     if (rxkadDisableDotCheck) {
523         rx_SetSecurityConfiguration(tservice, RXS_CONFIG_FLAGS,
524                                     (void *)RXS_CONFIG_FLAGS_DISABLE_DOTCHECK);
525     }
526
527     tservice =
528         rx_NewServiceHost(host, 0, RX_STATS_SERVICE_ID, "rpcstats",
529                           securityClasses, numClasses,
530                           RXSTATS_ExecuteRequest);
531     if (tservice == (struct rx_service *)0) {
532         VLog(0, ("vlserver: Could not create rpc stats rx service\n"));
533         exit(3);
534     }
535     rx_SetMinProcs(tservice, 2);
536     rx_SetMaxProcs(tservice, 4);
537
538     LogCommandLine(argc, argv, "vlserver", VldbVersion, "Starting AFS", FSLog);
539     if (afsconf_GetLatestKey(tdir, NULL, NULL) == 0) {
540         LogDesWarning();
541     }
542     VLog(0, ("%s\n", cml_version_number));
543
544     /* allow super users to manage RX statistics */
545     rx_SetRxStatUserOk(vldb_rxstat_userok);
546
547     rx_StartServer(1);          /* Why waste this idle process?? */
548
549     return 0; /* not reachable */
550 }