dfcd981759d17e13382736db88cd8b7b65f747e8
[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
13
14 #include <afs/stds.h>
15 #include <sys/types.h>
16 #include <signal.h>
17 #include <sys/stat.h>
18 #include <string.h>
19 #ifdef HAVE_FCNTL_H
20 #include <fcntl.h>
21 #endif
22 #ifdef AFS_NT40_ENV
23 #include <winsock2.h>
24 #include <WINNT/afsevent.h>
25 #endif
26 #ifdef HAVE_SYS_FILE_H
27 #include <sys/file.h>
28 #endif
29 #include <time.h>
30 #ifdef HAVE_NETDB_H
31 #include <netdb.h>
32 #endif
33 #ifdef HAVE_NETINET_IN_H
34 #include <netinet/in.h>
35 #endif
36 #include <stdio.h>
37 #ifdef HAVE_STDINT_H
38 # include <stdint.h>
39 #endif
40
41 #include <rx/xdr.h>
42 #include <rx/rx.h>
43 #include <rx/rx_globals.h>
44 #include <rx/rxstat.h>
45 #include <afs/cellconfig.h>
46 #include <afs/keys.h>
47 #include <afs/auth.h>
48 #include <afs/audit.h>
49 #include <afs/com_err.h>
50 #include <lock.h>
51 #include <ubik.h>
52 #include <afs/afsutil.h>
53 #include "vlserver.h"
54 #include "vlserver_internal.h"
55
56 #define MAXLWP 16
57 const char *vl_dbaseName;
58 struct afsconf_dir *vldb_confdir = 0;   /* vldb configuration dir */
59 int lwps = 9;
60
61 struct vldstats dynamic_statistics;
62 struct ubik_dbase *VL_dbase;
63 afs_uint32 rd_HostAddress[MAXSERVERID + 1];
64 afs_uint32 wr_HostAddress[MAXSERVERID + 1];
65
66 static void *CheckSignal(void*);
67 int LogLevel = 0;
68 int smallMem = 0;
69 int rxJumbograms = 0;           /* default is to not send and receive jumbo grams */
70 int rxMaxMTU = -1;
71 afs_int32 rxBind = 0;
72 int rxkadDisableDotCheck = 0;
73 int debuglevel = 0;
74
75 #define ADDRSPERSITE 16         /* Same global is in rx/rx_user.c */
76 afs_uint32 SHostAddrs[ADDRSPERSITE];
77
78 static void
79 CheckSignal_Signal(int unused)
80 {
81 #if defined(AFS_PTHREAD_ENV)
82     CheckSignal(0);
83 #else
84     IOMGR_SoftSig(CheckSignal, 0);
85 #endif
86 }
87
88 static void *
89 CheckSignal(void *unused)
90 {
91     int i, errorcode;
92     struct vl_ctx ctx;
93
94     if ((errorcode =
95         Init_VLdbase(&ctx, LOCKREAD, VLGETSTATS - VL_LOWEST_OPCODE)))
96         return (void *)(intptr_t)errorcode;
97     VLog(0, ("Dump name hash table out\n"));
98     for (i = 0; i < HASHSIZE; i++) {
99         HashNDump(&ctx, i);
100     }
101     VLog(0, ("Dump id hash table out\n"));
102     for (i = 0; i < HASHSIZE; i++) {
103         HashIdDump(&ctx, i);
104     }
105     return ((void *)(intptr_t)ubik_EndTrans(ctx.trans));
106 }                               /*CheckSignal */
107
108
109 /* Initialize the stats for the opcodes */
110 void
111 initialize_dstats(void)
112 {
113     int i;
114
115     dynamic_statistics.start_time = (afs_uint32) time(0);
116     for (i = 0; i < MAX_NUMBER_OPCODES; i++) {
117         dynamic_statistics.requests[i] = 0;
118         dynamic_statistics.aborts[i] = 0;
119     }
120 }
121
122 /* check whether caller is authorized to manage RX statistics */
123 int
124 vldb_rxstat_userok(struct rx_call *call)
125 {
126     return afsconf_SuperUser(vldb_confdir, call, NULL);
127 }
128
129 /* Main server module */
130
131 #include "AFS_component_version_number.c"
132
133 int
134 main(int argc, char **argv)
135 {
136     afs_int32 code;
137     afs_uint32 myHost;
138     struct rx_service *tservice;
139     struct rx_securityClass **securityClasses;
140     afs_int32 numClasses;
141     struct afsconf_dir *tdir;
142     struct ktc_encryptionKey tkey;
143     struct afsconf_cell info;
144     struct hostent *th;
145     char hostname[VL_MAXNAMELEN];
146     int noAuth = 0, index;
147     char clones[MAXHOSTSPERCELL];
148     char *auditFileName = NULL;
149     afs_uint32 host = ntohl(INADDR_ANY);
150
151 #ifdef  AFS_AIX32_ENV
152     /*
153      * The following signal action for AIX is necessary so that in case of a
154      * crash (i.e. core is generated) we can include the user's data section
155      * in the core dump. Unfortunately, by default, only a partial core is
156      * generated which, in many cases, isn't too useful.
157      */
158     struct sigaction nsa;
159
160     rx_extraPackets = 100;      /* should be a switch, I guess... */
161     sigemptyset(&nsa.sa_mask);
162     nsa.sa_handler = SIG_DFL;
163     nsa.sa_flags = SA_FULLDUMP;
164     sigaction(SIGABRT, &nsa, NULL);
165     sigaction(SIGSEGV, &nsa, NULL);
166 #endif
167     osi_audit_init();
168
169     /* Parse command line */
170     for (index = 1; index < argc; index++) {
171         if (strcmp(argv[index], "-noauth") == 0) {
172             noAuth = 1;
173         } else if (strcmp(argv[index], "-p") == 0) {
174             lwps = atoi(argv[++index]);
175             if (lwps > MAXLWP) {
176                 printf("Warning: '-p %d' is too big; using %d instead\n",
177                        lwps, MAXLWP);
178                 lwps = MAXLWP;
179             }
180         } else if (strcmp(argv[index], "-d") == 0) {
181             if ((index + 1) >= argc) {
182                 fprintf(stderr, "missing argument for -d\n");
183                 return -1;
184             }
185             debuglevel = atoi(argv[++index]);
186             LogLevel = debuglevel;
187         } else if (strcmp(argv[index], "-nojumbo") == 0) {
188             rxJumbograms = 0;
189         } else if (strcmp(argv[index], "-jumbo") == 0) {
190             rxJumbograms = 1;
191         } else if (strcmp(argv[index], "-rxbind") == 0) {
192             rxBind = 1;
193         } else if (strcmp(argv[index], "-allow-dotted-principals") == 0) {
194             rxkadDisableDotCheck = 1;
195         } else if (!strcmp(argv[index], "-rxmaxmtu")) {
196             if ((index + 1) >= argc) {
197                 fprintf(stderr, "missing argument for -rxmaxmtu\n");
198                 return -1;
199             }
200             rxMaxMTU = atoi(argv[++index]);
201             if ((rxMaxMTU < RX_MIN_PACKET_SIZE) ||
202                 (rxMaxMTU > RX_MAX_PACKET_DATA_SIZE)) {
203                 printf("rxMaxMTU %d invalid; must be between %d-%" AFS_SIZET_FMT "\n",
204                        rxMaxMTU, RX_MIN_PACKET_SIZE,
205                        RX_MAX_PACKET_DATA_SIZE);
206                 return -1;
207             }
208
209         } else if (strcmp(argv[index], "-smallmem") == 0) {
210             smallMem = 1;
211
212         } else if (strcmp(argv[index], "-trace") == 0) {
213             extern char rxi_tracename[80];
214             strcpy(rxi_tracename, argv[++index]);
215
216         } else if (strcmp(argv[index], "-auditlog") == 0) {
217             auditFileName = argv[++index];
218
219         } else if (strcmp(argv[index], "-audit-interface") == 0) {
220             char *interface = argv[++index];
221
222             if (osi_audit_interface(interface)) {
223                 printf("Invalid audit interface '%s'\n", interface);
224                 return -1;
225             }
226
227         } else if (strcmp(argv[index], "-enable_peer_stats") == 0) {
228             rx_enablePeerRPCStats();
229         } else if (strcmp(argv[index], "-enable_process_stats") == 0) {
230             rx_enableProcessRPCStats();
231 #ifndef AFS_NT40_ENV
232         } else if (strcmp(argv[index], "-syslog") == 0) {
233             /* set syslog logging flag */
234             serverLogSyslog = 1;
235         } else if (strncmp(argv[index], "-syslog=", 8) == 0) {
236             serverLogSyslog = 1;
237             serverLogSyslogFacility = atoi(argv[index] + 8);
238 #endif
239         } else {
240             /* support help flag */
241 #ifndef AFS_NT40_ENV
242             printf("Usage: vlserver [-p <number of processes>] [-nojumbo] "
243                    "[-rxmaxmtu <bytes>] [-rxbind] [-allow-dotted-principals] "
244                    "[-auditlog <log path>] [-jumbo] [-d <debug level>] "
245                    "[-syslog[=FACILITY]] "
246                    "[-enable_peer_stats] [-enable_process_stats] "
247                    "[-help]\n");
248 #else
249             printf("Usage: vlserver [-p <number of processes>] [-nojumbo] "
250                    "[-rxmaxmtu <bytes>] [-rxbind] [-allow-dotted-principals] "
251                    "[-auditlog <log path>] [-jumbo] [-d <debug level>] "
252                    "[-enable_peer_stats] [-enable_process_stats] "
253                    "[-help]\n");
254 #endif
255             fflush(stdout);
256             exit(0);
257         }
258     }
259
260     if (auditFileName) {
261         osi_audit_file(auditFileName);
262     }
263
264     /* Initialize dirpaths */
265     if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
266 #ifdef AFS_NT40_ENV
267         ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
268 #endif
269         fprintf(stderr, "%s: Unable to obtain AFS server directory.\n",
270                 argv[0]);
271         exit(2);
272     }
273     vl_dbaseName = AFSDIR_SERVER_VLDB_FILEPATH;
274
275 #ifndef AFS_NT40_ENV
276     serverLogSyslogTag = "vlserver";
277 #endif
278     OpenLog(AFSDIR_SERVER_VLOG_FILEPATH);       /* set up logging */
279     SetupLogSignals();
280
281     tdir = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH);
282     if (!tdir) {
283         printf
284             ("vlserver: can't open configuration files in dir %s, giving up.\n",
285              AFSDIR_SERVER_ETC_DIRPATH);
286         exit(1);
287     }
288 #ifdef AFS_NT40_ENV
289     /* initialize winsock */
290     if (afs_winsockInit() < 0) {
291         ReportErrorEventAlt(AFSEVT_SVR_WINSOCK_INIT_FAILED, 0, argv[0], 0);
292         fprintf(stderr, "vlserver: couldn't initialize winsock. \n");
293         exit(1);
294     }
295 #endif
296     /* get this host */
297     gethostname(hostname, sizeof(hostname));
298     th = gethostbyname(hostname);
299     if (!th) {
300         printf("vlserver: couldn't get address of this host (%s).\n",
301                hostname);
302         exit(1);
303     }
304     memcpy(&myHost, th->h_addr, sizeof(afs_uint32));
305
306 #if !defined(AFS_HPUX_ENV) && !defined(AFS_NT40_ENV)
307     signal(SIGXCPU, CheckSignal_Signal);
308 #endif
309     /* get list of servers */
310     code =
311         afsconf_GetExtendedCellInfo(tdir, NULL, AFSCONF_VLDBSERVICE, &info,
312                                     clones);
313     if (code) {
314         printf("vlserver: Couldn't get cell server list for 'afsvldb'.\n");
315         exit(2);
316     }
317
318     vldb_confdir = tdir;        /* Preserve our configuration dir */
319     /* rxvab no longer supported */
320     memset(&tkey, 0, sizeof(tkey));
321
322     if (noAuth)
323         afsconf_SetNoAuthFlag(tdir, 1);
324
325     if (rxBind) {
326         afs_int32 ccode;
327 #ifndef AFS_NT40_ENV
328         if (AFSDIR_SERVER_NETRESTRICT_FILEPATH ||
329             AFSDIR_SERVER_NETINFO_FILEPATH) {
330             char reason[1024];
331             ccode = parseNetFiles(SHostAddrs, NULL, NULL,
332                                   ADDRSPERSITE, reason,
333                                   AFSDIR_SERVER_NETINFO_FILEPATH,
334                                   AFSDIR_SERVER_NETRESTRICT_FILEPATH);
335         } else
336 #endif
337         {
338             ccode = rx_getAllAddr(SHostAddrs, ADDRSPERSITE);
339         }
340         if (ccode == 1) {
341             host = SHostAddrs[0];
342             rx_InitHost(host, htons(AFSCONF_VLDBPORT));
343         }
344     }
345
346     ubik_nBuffers = 512;
347     ubik_CRXSecurityProc = afsconf_ClientAuth;
348     ubik_CRXSecurityRock = (char *)tdir;
349     ubik_SRXSecurityProc = afsconf_ServerAuth;
350     ubik_SRXSecurityRock = (char *)tdir;
351     ubik_CheckRXSecurityProc = afsconf_CheckAuth;
352     ubik_CheckRXSecurityRock = (char *)tdir;
353     ubik_SyncWriterCacheProc = vlsynccache;
354     code =
355         ubik_ServerInitByInfo(myHost, htons(AFSCONF_VLDBPORT), &info, clones,
356                               vl_dbaseName, &VL_dbase);
357     if (code) {
358         printf("vlserver: Ubik init failed: %s\n", afs_error_message(code));
359         exit(2);
360     }
361     if (!rxJumbograms) {
362         rx_SetNoJumbo();
363     }
364     if (rxMaxMTU != -1) {
365         rx_SetMaxMTU(rxMaxMTU);
366     }
367     rx_SetRxDeadTime(50);
368
369     memset(rd_HostAddress, 0, sizeof(rd_HostAddress));
370     memset(wr_HostAddress, 0, sizeof(wr_HostAddress));
371     initialize_dstats();
372
373     afsconf_BuildServerSecurityObjects(tdir, 0, &securityClasses, &numClasses);
374
375     tservice =
376         rx_NewServiceHost(host, 0, USER_SERVICE_ID, "Vldb server",
377                           securityClasses, numClasses,
378                           VL_ExecuteRequest);
379     if (tservice == (struct rx_service *)0) {
380         printf("vlserver: Could not create VLDB_SERVICE rx service\n");
381         exit(3);
382     }
383     rx_SetMinProcs(tservice, 2);
384     if (lwps < 4)
385         lwps = 4;
386     rx_SetMaxProcs(tservice, lwps);
387
388     if (rxkadDisableDotCheck) {
389         rx_SetSecurityConfiguration(tservice, RXS_CONFIG_FLAGS,
390                                     (void *)RXS_CONFIG_FLAGS_DISABLE_DOTCHECK);
391     }
392
393     tservice =
394         rx_NewServiceHost(host, 0, RX_STATS_SERVICE_ID, "rpcstats",
395                           securityClasses, numClasses,
396                           RXSTATS_ExecuteRequest);
397     if (tservice == (struct rx_service *)0) {
398         printf("vlserver: Could not create rpc stats rx service\n");
399         exit(3);
400     }
401     rx_SetMinProcs(tservice, 2);
402     rx_SetMaxProcs(tservice, 4);
403
404     LogCommandLine(argc, argv, "vlserver", VldbVersion, "Starting AFS", FSLog);
405     printf("%s\n", cml_version_number); /* Goes to the log */
406
407     /* allow super users to manage RX statistics */
408     rx_SetRxStatUserOk(vldb_rxstat_userok);
409
410     rx_StartServer(1);          /* Why waste this idle process?? */
411
412     return 0; /* not reachable */
413 }