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