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