audit: remove static local realms
[openafs.git] / src / budb / server.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 #include <afs/cmd.h>
20 #include <lwp.h>
21 #include <ubik.h>
22 #include <rx/xdr.h>
23 #include <rx/rx.h>
24 #include <rx/rxkad.h>
25 #include <rx/rx_globals.h>
26 #include <afs/cellconfig.h>
27 #include <afs/bubasics.h>
28 #include <afs/afsutil.h>
29 #include <afs/com_err.h>
30 #include <afs/audit.h>
31
32 #include "budb_errs.h"
33 #include "database.h"
34 #include "error_macros.h"
35 #include "budb_internal.h"
36 #include "globals.h"
37
38 struct ubik_dbase *BU_dbase;
39 struct afsconf_dir *BU_conf;    /* for getting cell info */
40
41 int argHandler(struct cmd_syndesc *, void *);
42 int truncateDatabase(void);
43 int parseServerList(struct cmd_item *);
44
45 char lcell[MAXKTCREALMLEN];
46 afs_uint32 myHost = 0;
47 int helpOption;
48
49 /* server's global configuration information. This is exported to other
50  * files/routines
51  */
52
53 buServerConfT globalConf;
54 buServerConfP globalConfPtr = &globalConf;
55 char dbDir[AFSDIR_PATH_MAX], cellConfDir[AFSDIR_PATH_MAX];
56 /* debugging control */
57 int debugging = 0;
58
59 int rxBind = 0;
60 int lwps   = 3;
61
62 #define MINLWP  3
63 #define MAXLWP 16
64
65 #define ADDRSPERSITE 16         /* Same global is in rx/rx_user.c */
66 afs_uint32 SHostAddrs[ADDRSPERSITE];
67
68 /* check whether caller is authorized to manage RX statistics */
69 int
70 BU_rxstat_userok(struct rx_call *call)
71 {
72     return afsconf_SuperUser(BU_conf, call, NULL);
73 }
74
75 /**
76  * Return true if this name is a member of the local realm.
77  */
78 int
79 BU_IsLocalRealmMatch(void *rock, char *name, char *inst, char *cell)
80 {
81     struct afsconf_dir *dir = (struct afsconf_dir *)rock;
82     afs_int32 islocal = 0;      /* default to no */
83     int code;
84
85     code = afsconf_IsLocalRealmMatch(dir, &islocal, name, inst, cell);
86     if (code) {
87         LogError(code, "Failed local realm check; name=%s, inst=%s, cell=%s\n",
88                  name, inst, cell);
89     }
90     return islocal;
91 }
92
93 int
94 convert_cell_to_ubik(struct afsconf_cell *cellinfo, afs_uint32 *myHost,
95                      afs_uint32 *serverList)
96 {
97     int i;
98     char hostname[64];
99     struct hostent *th;
100
101     /* get this host */
102     gethostname(hostname, sizeof(hostname));
103     th = gethostbyname(hostname);
104     if (!th) {
105         printf("prserver: couldn't get address of this host.\n");
106         BUDB_EXIT(1);
107     }
108     memcpy(myHost, th->h_addr, sizeof(afs_uint32));
109
110     for (i = 0; i < cellinfo->numServers; i++)
111         /* omit my host from serverList */
112         if (cellinfo->hostAddr[i].sin_addr.s_addr != *myHost)
113             *serverList++ = cellinfo->hostAddr[i].sin_addr.s_addr;
114
115     *serverList = 0;            /* terminate list */
116     return 0;
117 }
118
119 /* MyBeforeProc
120  *      The whole purpose of MyBeforeProc is to detect
121  *      if the -help option was not within the command line.
122  *      If it were, this routine would never have been called.
123  */
124 static int
125 MyBeforeProc(struct cmd_syndesc *as, void *arock)
126 {
127     helpOption = 0;
128     return 0;
129 }
130
131 /* initializeCommands
132  *      initialize all the supported commands and their arguments
133  */
134
135 void
136 initializeArgHandler(void)
137 {
138     struct cmd_syndesc *cptr;
139
140     cmd_SetBeforeProc(MyBeforeProc, NULL);
141
142     cptr = cmd_CreateSyntax(NULL, argHandler, NULL, "Backup database server");
143
144     cmd_AddParm(cptr, "-database", CMD_SINGLE, CMD_OPTIONAL,
145                 "database directory");
146
147     cmd_AddParm(cptr, "-cellservdb", CMD_SINGLE, CMD_OPTIONAL,
148                 "cell configuration directory");
149
150     cmd_AddParm(cptr, "-resetdb", CMD_FLAG, CMD_OPTIONAL,
151                 "truncate the database");
152
153     cmd_AddParm(cptr, "-noauth", CMD_FLAG, CMD_OPTIONAL,
154                 "run without authentication");
155
156     cmd_AddParm(cptr, "-smallht", CMD_FLAG, CMD_OPTIONAL,
157                 "use small hash tables");
158
159     cmd_AddParm(cptr, "-servers", CMD_LIST, CMD_OPTIONAL,
160                 "list of ubik database servers");
161
162     cmd_AddParm(cptr, "-ubikbuffers", CMD_SINGLE, CMD_OPTIONAL,
163                 "the number of ubik buffers");
164
165     cmd_AddParm(cptr, "-auditlog", CMD_SINGLE, CMD_OPTIONAL,
166                 "audit log path");
167
168     cmd_AddParm(cptr, "-p", CMD_SINGLE, CMD_OPTIONAL,
169                 "number of processes");
170
171     cmd_AddParm(cptr, "-rxbind", CMD_FLAG, CMD_OPTIONAL,
172                 "bind the Rx socket (primary interface only)");
173
174     cmd_AddParm(cptr, "-audit-interface", CMD_SINGLE, CMD_OPTIONAL,
175                 "audit interface (file or sysvmq)");
176 }
177
178 int
179 argHandler(struct cmd_syndesc *as, void *arock)
180 {
181
182     /* globalConfPtr provides the handle for the configuration information */
183
184     /* database directory */
185     if (as->parms[0].items != 0) {
186         globalConfPtr->databaseDirectory =
187             (char *)malloc(strlen(as->parms[0].items->data) + 1);
188         if (globalConfPtr->databaseDirectory == 0)
189             BUDB_EXIT(-1);
190         strcpy(globalConfPtr->databaseDirectory, as->parms[0].items->data);
191     }
192
193     /* -cellservdb, cell configuration directory */
194     if (as->parms[1].items != 0) {
195         globalConfPtr->cellConfigdir =
196             (char *)malloc(strlen(as->parms[1].items->data) + 1);
197         if (globalConfPtr->cellConfigdir == 0)
198             BUDB_EXIT(-1);
199
200         strcpy(globalConfPtr->cellConfigdir, as->parms[1].items->data);
201
202         globalConfPtr->debugFlags |= DF_RECHECKNOAUTH;
203     }
204
205     /* truncate the database */
206     if (as->parms[2].items != 0)
207         truncateDatabase();
208
209     /* run without authentication */
210     if (as->parms[3].items != 0)
211         globalConfPtr->debugFlags |= DF_NOAUTH;
212
213     /* use small hash tables */
214     if (as->parms[4].items != 0)
215         globalConfPtr->debugFlags |= DF_SMALLHT;
216
217     /* user provided list of ubik database servers */
218     if (as->parms[5].items != 0)
219         parseServerList(as->parms[5].items);
220
221     /* user provided the number of ubik buffers    */
222     if (as->parms[6].items != 0)
223         ubik_nBuffers = atoi(as->parms[6].items->data);
224     else
225         ubik_nBuffers = 0;
226
227     /* param 7 (-auditlog) handled below */
228
229     /* user provided the number of threads    */
230     if (as->parms[8].items != 0) {
231         lwps = atoi(as->parms[8].items->data);
232         if (lwps > MAXLWP) {
233             printf ("Warning: '-p %d' is too big; using %d instead\n",
234                 lwps, MAXLWP);
235             lwps = MAXLWP;
236         }
237         if (lwps < MINLWP) {
238             printf ("Warning: '-p %d' is too small; using %d instead\n",
239                 lwps, MINLWP);
240             lwps = MINLWP;
241         }
242     }
243
244     /* user provided rxbind option    */
245     if (as->parms[9].items != 0) {
246         rxBind = 1;
247     }
248
249     /* -audit-interface */
250     if (as->parms[10].items != 0) {
251         char *interface = as->parms[10].items->data;
252
253         if (osi_audit_interface(interface)) {
254             printf("Invalid audit interface '%s'\n", interface);
255             BUDB_EXIT(-1);
256         }
257     }
258
259     /* -auditlog */
260     /* needs to be after -audit-interface, so we osi_audit_interface
261      * before we osi_audit_file */
262     if (as->parms[7].items != 0) {
263         char *fileName = as->parms[7].items->data;
264
265         osi_audit_file(fileName);
266     }
267
268     return 0;
269 }
270
271 /* --- */
272
273 int
274 parseServerList(struct cmd_item *itemPtr)
275 {
276     struct cmd_item *save;
277     char **serverArgs;
278     char **ptr;
279     afs_int32 nservers = 0;
280     afs_int32 code = 0;
281
282     save = itemPtr;
283
284     /* compute number of servers in the list */
285     while (itemPtr) {
286         nservers++;
287         itemPtr = itemPtr->next;
288     }
289
290     LogDebug(3, "%d servers\n", nservers);
291
292     /* now can allocate the space for the server arguments */
293     serverArgs = (char **)malloc((nservers + 2) * sizeof(char *));
294     if (serverArgs == 0)
295         ERROR(-1);
296
297     ptr = serverArgs;
298     *ptr++ = "";
299     *ptr++ = "-servers";
300
301     /* now go through and construct the list of servers */
302     itemPtr = save;
303     while (itemPtr) {
304         *ptr++ = itemPtr->data;
305         itemPtr = itemPtr->next;
306     }
307
308     code =
309         ubik_ParseServerList(nservers + 2, serverArgs, &globalConfPtr->myHost,
310                              globalConfPtr->serverList);
311     if (code)
312         ERROR(code);
313
314     /* free space for the server args */
315     free((char *)serverArgs);
316
317   error_exit:
318     return (code);
319 }
320
321 /* truncateDatabase
322  *      truncates just the database file.
323  */
324
325 int
326 truncateDatabase(void)
327 {
328     char *path;
329     afs_int32 code = 0;
330     int fd;
331
332     path = malloc(strlen(globalConfPtr->databaseDirectory) +
333                   strlen(globalConfPtr->databaseName) +
334                   strlen(globalConfPtr->databaseExtension) + 1);
335     if (path == NULL)
336         ERROR(-1);
337
338     /* construct the database name */
339     strcpy(path, globalConfPtr->databaseDirectory);
340     strcat(path, globalConfPtr->databaseName);
341     strcat(path, globalConfPtr->databaseExtension);
342
343     fd = open(path, O_RDWR, 0755);
344     if (!fd) {
345         code = errno;
346     } else {
347         if (ftruncate(fd, 0) != 0) {
348             code = errno;
349         } else
350             close(fd);
351     }
352
353     free(path);
354
355   error_exit:
356     return (code);
357 }
358
359
360 /* --- */
361
362 #include "AFS_component_version_number.c"
363
364 int
365 main(int argc, char **argv)
366 {
367     char *whoami = argv[0];
368     char *dbNamePtr = 0;
369     struct afsconf_cell cellinfo;
370     time_t currentTime;
371     afs_int32 code = 0;
372     afs_uint32 host = ntohl(INADDR_ANY);
373
374     char  clones[MAXHOSTSPERCELL];
375
376     struct rx_service *tservice;
377     struct rx_securityClass **securityClasses;
378     afs_int32 numClasses;
379
380     extern int rx_stackSize;
381
382 #ifdef AFS_NT40_ENV
383     /* initialize winsock */
384     if (afs_winsockInit() < 0) {
385         ReportErrorEventAlt(AFSEVT_SVR_WINSOCK_INIT_FAILED, 0, argv[0], 0);
386         fprintf(stderr, "%s: Couldn't initialize winsock.\n", whoami);
387         exit(1);
388     }
389 #endif
390
391 #ifdef  AFS_AIX32_ENV
392     /*
393      * The following signal action for AIX is necessary so that in case of a
394      * crash (i.e. core is generated) we can include the user's data section
395      * in the core dump. Unfortunately, by default, only a partial core is
396      * generated which, in many cases, isn't too useful.
397      */
398     struct sigaction nsa;
399
400     sigemptyset(&nsa.sa_mask);
401     nsa.sa_handler = SIG_DFL;
402     nsa.sa_flags = SA_FULLDUMP;
403     sigaction(SIGSEGV, &nsa, NULL);
404     sigaction(SIGABRT, &nsa, NULL);
405 #endif
406     osi_audit_init();
407     osi_audit(BUDB_StartEvent, 0, AUD_END);
408
409     initialize_BUDB_error_table();
410     initializeArgHandler();
411
412     /* Initialize dirpaths */
413     if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
414 #ifdef AFS_NT40_ENV
415         ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
416 #endif
417         afs_com_err(whoami, errno, "; Unable to obtain AFS server directory.");
418         exit(2);
419     }
420
421     memset(globalConfPtr, 0, sizeof(*globalConfPtr));
422
423     /* set default configuration values */
424     strcpy(dbDir, AFSDIR_SERVER_DB_DIRPATH);
425     strcat(dbDir, "/");
426     globalConfPtr->databaseDirectory = dbDir;
427     globalConfPtr->databaseName = DEFAULT_DBPREFIX;
428     strcpy(cellConfDir, AFSDIR_SERVER_ETC_DIRPATH);
429     globalConfPtr->cellConfigdir = cellConfDir;
430
431     /* open the log file */
432 /*
433     globalConfPtr->log = fopen(DEFAULT_LOGNAME,"a");
434     if ( globalConfPtr->log == NULL )
435     {
436         printf("Can't open log file %s - aborting\n", DEFAULT_LOGNAME);
437         BUDB_EXIT(-1);
438     }
439 */
440
441     srandom(1);
442
443 #ifdef AFS_PTHREAD_ENV
444     SetLogThreadNumProgram( rx_GetThreadNum );
445 #endif
446
447     /* process the user supplied args */
448     helpOption = 1;
449     code = cmd_Dispatch(argc, argv);
450     if (code)
451         ERROR(code);
452
453     /* exit if there was a help option */
454     if (helpOption)
455         BUDB_EXIT(0);
456
457     /* open the log file */
458     globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
459     if (globalConfPtr->log == NULL) {
460         printf("Can't open log file %s - aborting\n",
461                AFSDIR_SERVER_BUDBLOG_FILEPATH);
462         BUDB_EXIT(-1);
463     }
464
465     /* keep log closed so can remove it */
466
467     fclose(globalConfPtr->log);
468
469     /* open the cell's configuration directory */
470     LogDebug(4, "opening %s\n", globalConfPtr->cellConfigdir);
471
472     BU_conf = afsconf_Open(globalConfPtr->cellConfigdir);
473     if (BU_conf == 0) {
474         LogError(code, "Failed getting cell info\n");
475         afs_com_err(whoami, code, "Failed getting cell info");
476         ERROR(BUDB_NOCELLS);
477     }
478
479     code = afsconf_GetLocalCell(BU_conf, lcell, sizeof(lcell));
480     if (code) {
481         LogError(0, "** Can't determine local cell name!\n");
482         ERROR(code);
483     }
484
485     if (globalConfPtr->myHost == 0) {
486         /* if user hasn't supplied a list of servers, extract server
487          * list from the cell's database
488          */
489
490         LogDebug(1, "Using server list from %s cell database.\n", lcell);
491
492         code = afsconf_GetExtendedCellInfo (BU_conf, lcell, 0, &cellinfo,
493                                             clones);
494         code =
495             convert_cell_to_ubik(&cellinfo, &globalConfPtr->myHost,
496                                  globalConfPtr->serverList);
497         if (code)
498             ERROR(code);
499     }
500
501     /* initialize audit user check */
502     osi_audit_set_user_check(BU_conf, BU_IsLocalRealmMatch);
503
504     /* initialize ubik */
505     ubik_SetClientSecurityProcs(afsconf_ClientAuth, afsconf_UpToDate, BU_conf);
506     ubik_SetServerSecurityProcs(afsconf_BuildServerSecurityObjects,
507                                 afsconf_CheckAuth, BU_conf);
508
509     if (ubik_nBuffers == 0)
510         ubik_nBuffers = 400;
511
512     LogError(0, "Will allocate %d ubik buffers\n", ubik_nBuffers);
513
514     dbNamePtr =
515         (char *)malloc(strlen(globalConfPtr->databaseDirectory) +
516                        strlen(globalConfPtr->databaseName) + 1);
517     if (dbNamePtr == 0)
518         ERROR(-1);
519
520     /* construct the database name */
521     strcpy(dbNamePtr, globalConfPtr->databaseDirectory);
522     strcat(dbNamePtr, globalConfPtr->databaseName);     /* name prefix */
523
524     rx_SetRxDeadTime(60);       /* 60 seconds inactive before timeout */
525
526     if (rxBind) {
527         afs_int32 ccode;
528         if (AFSDIR_SERVER_NETRESTRICT_FILEPATH ||
529             AFSDIR_SERVER_NETINFO_FILEPATH) {
530             char reason[1024];
531             ccode = parseNetFiles(SHostAddrs, NULL, NULL,
532                                            ADDRSPERSITE, reason,
533                                            AFSDIR_SERVER_NETINFO_FILEPATH,
534                                            AFSDIR_SERVER_NETRESTRICT_FILEPATH);
535         } else
536         {
537             ccode = rx_getAllAddr(SHostAddrs, ADDRSPERSITE);
538         }
539         if (ccode == 1) {
540             host = SHostAddrs[0];
541             rx_InitHost(host, htons(AFSCONF_BUDBPORT));
542         }
543     }
544
545     code = ubik_ServerInitByInfo (globalConfPtr->myHost,
546                                   htons(AFSCONF_BUDBPORT),
547                                   &cellinfo,
548                                   clones,
549                                   dbNamePtr,           /* name prefix */
550                                   &BU_dbase);
551
552     if (code) {
553         LogError(code, "Ubik init failed\n");
554         afs_com_err(whoami, code, "Ubik init failed");
555         ERROR(code);
556     }
557
558     afsconf_BuildServerSecurityObjects(BU_conf, &securityClasses, &numClasses);
559
560     /* Disable jumbograms */
561     rx_SetNoJumbo();
562
563     tservice =
564         rx_NewServiceHost(host, 0, BUDB_SERVICE, "BackupDatabase",
565                           securityClasses, numClasses, BUDB_ExecuteRequest);
566
567     if (tservice == (struct rx_service *)0) {
568         LogError(0, "Could not create backup database rx service\n");
569         printf("Could not create backup database rx service\n");
570         BUDB_EXIT(3);
571     }
572     rx_SetMinProcs(tservice, 1);
573     rx_SetMaxProcs(tservice, lwps);
574     rx_SetStackSize(tservice, 10000);
575
576     /* allow super users to manage RX statistics */
577     rx_SetRxStatUserOk(BU_rxstat_userok);
578
579     /* misc. initialization */
580
581     /* database dump synchronization */
582     memset(dumpSyncPtr, 0, sizeof(*dumpSyncPtr));
583     Lock_Init(&dumpSyncPtr->ds_lock);
584
585     rx_StartServer(0);          /* start handling requests */
586
587     code = InitProcs();
588     if (code)
589         ERROR(code);
590
591
592     currentTime = time(0);
593     LogError(0, "Ready to process requests at %s\n", ctime(&currentTime));
594
595     rx_ServerProc(NULL);                /* donate this LWP */
596
597   error_exit:
598     osi_audit(BUDB_FinishEvent, code, AUD_END);
599     return (code);
600 }
601
602 void
603 consistencyCheckDb(void)
604 {
605     /* do consistency checks on structure sizes */
606     if ((sizeof(struct htBlock) > BLOCKSIZE)
607         || (sizeof(struct vfBlock) > BLOCKSIZE)
608         || (sizeof(struct viBlock) > BLOCKSIZE)
609         || (sizeof(struct dBlock) > BLOCKSIZE)
610         || (sizeof(struct tBlock) > BLOCKSIZE)
611         ) {
612         fprintf(stderr, "Block layout error!\n");
613         BUDB_EXIT(99);
614     }
615 }
616
617 void
618 LogDebug(int level, char *fmt, ... )
619 {
620     va_list ap;
621
622     va_start(ap, fmt);
623
624     if (debugging >= level) {
625         /* log normally closed so can remove it */
626         globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
627         if (globalConfPtr->log != NULL) {
628             vfprintf(globalConfPtr->log, fmt, ap);
629             fflush(globalConfPtr->log);
630             fclose(globalConfPtr->log);
631         }
632     }
633     va_end(ap);
634 }
635
636 static char *
637 TimeStamp(time_t t)
638 {
639     struct tm *lt;
640     static char timestamp[20];
641
642     lt = localtime(&t);
643     strftime(timestamp, 20, "%m/%d/%Y %H:%M:%S", lt);
644     return timestamp;
645 }
646
647 void
648 Log(char *fmt, ...)
649 {
650     va_list ap;
651     time_t now;
652
653     va_start(ap, fmt);
654     globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
655     if (globalConfPtr->log != NULL) {
656         now = time(0);
657         fprintf(globalConfPtr->log, "%s ", TimeStamp(now));
658
659         vfprintf(globalConfPtr->log, fmt, ap);
660         fflush(globalConfPtr->log);
661         fclose(globalConfPtr->log);
662     }
663     va_end(ap);
664 }
665
666 void
667 LogError(long code, char *fmt, ... )
668 {
669     va_list ap;
670     time_t now;
671
672     va_start(ap, fmt);
673     globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
674
675     if (globalConfPtr->log != NULL) {
676         now = time(0);
677         fprintf(globalConfPtr->log, "%s ", TimeStamp(now));
678
679         if (code)
680             fprintf(globalConfPtr->log, "%s: %s\n", afs_error_table_name(code),
681                     afs_error_message(code));
682         vfprintf(globalConfPtr->log, fmt, ap );
683         fflush(globalConfPtr->log);
684         fclose(globalConfPtr->log);
685     }
686 }
687
688
689 /*  ----------------
690  * debug
691  * ----------------
692  */
693
694 void
695 LogNetDump(struct dump *dumpPtr)
696 {
697     struct dump hostDump;
698     extern buServerConfP globalConfPtr;
699
700     dump_ntoh(dumpPtr, &hostDump);
701
702     globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
703     if (globalConfPtr->log != NULL) {
704         printDump(globalConfPtr->log, &hostDump);
705         fclose(globalConfPtr->log);
706     }
707 }
708