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