2 * Copyright 2000, International Business Machines Corporation and others.
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
10 #include <afsconfig.h>
11 #include <afs/param.h>
19 #include <WINNT/afsevent.h>
21 #include <netinet/in.h>
28 #include <sys/types.h>
37 #include <rx/rx_globals.h>
38 #include <afs/cellconfig.h>
39 #include <afs/bubasics.h>
40 #include <afs/afsutil.h>
41 #include <afs/com_err.h>
46 #include "budb_errs.h"
48 #include "error_macros.h"
49 #include "budb_internal.h"
51 #include "afs/audit.h"
53 struct ubik_dbase *BU_dbase;
54 struct afsconf_dir *BU_conf; /* for getting cell info */
56 int argHandler(struct cmd_syndesc *, void *);
57 int truncateDatabase(void);
58 int parseServerList(struct cmd_item *);
60 char lcell[MAXKTCREALMLEN];
61 afs_uint32 myHost = 0;
64 /* server's global configuration information. This is exported to other
68 buServerConfT globalConf;
69 buServerConfP globalConfPtr = &globalConf;
70 char dbDir[AFSDIR_PATH_MAX], cellConfDir[AFSDIR_PATH_MAX];
71 /* debugging control */
80 #define ADDRSPERSITE 16 /* Same global is in rx/rx_user.c */
81 afs_uint32 SHostAddrs[ADDRSPERSITE];
83 #if defined(AFS_PTHREAD_ENV)
87 return (intptr_t)pthread_getspecific(rx_thread_id_key);
91 /* check whether caller is authorized to manage RX statistics */
93 BU_rxstat_userok(struct rx_call *call)
95 return afsconf_SuperUser(BU_conf, call, NULL);
99 convert_cell_to_ubik(struct afsconf_cell *cellinfo, afs_uint32 *myHost,
100 afs_uint32 *serverList)
107 gethostname(hostname, sizeof(hostname));
108 th = gethostbyname(hostname);
110 printf("prserver: couldn't get address of this host.\n");
113 memcpy(myHost, th->h_addr, sizeof(afs_uint32));
115 for (i = 0; i < cellinfo->numServers; i++)
116 /* omit my host from serverList */
117 if (cellinfo->hostAddr[i].sin_addr.s_addr != *myHost)
118 *serverList++ = cellinfo->hostAddr[i].sin_addr.s_addr;
120 *serverList = 0; /* terminate list */
125 * The whole purpose of MyBeforeProc is to detect
126 * if the -help option was not within the command line.
127 * If it were, this routine would never have been called.
130 MyBeforeProc(struct cmd_syndesc *as, void *arock)
136 /* initializeCommands
137 * initialize all the supported commands and their arguments
141 initializeArgHandler(void)
143 struct cmd_syndesc *cptr;
145 cmd_SetBeforeProc(MyBeforeProc, NULL);
147 cptr = cmd_CreateSyntax(NULL, argHandler, NULL, "Backup database server");
149 cmd_AddParm(cptr, "-database", CMD_SINGLE, CMD_OPTIONAL,
150 "database directory");
152 cmd_AddParm(cptr, "-cellservdb", CMD_SINGLE, CMD_OPTIONAL,
153 "cell configuration directory");
155 cmd_AddParm(cptr, "-resetdb", CMD_FLAG, CMD_OPTIONAL,
156 "truncate the database");
158 cmd_AddParm(cptr, "-noauth", CMD_FLAG, CMD_OPTIONAL,
159 "run without authentication");
161 cmd_AddParm(cptr, "-smallht", CMD_FLAG, CMD_OPTIONAL,
162 "use small hash tables");
164 cmd_AddParm(cptr, "-servers", CMD_LIST, CMD_OPTIONAL,
165 "list of ubik database servers");
167 cmd_AddParm(cptr, "-ubikbuffers", CMD_SINGLE, CMD_OPTIONAL,
168 "the number of ubik buffers");
170 cmd_AddParm(cptr, "-auditlog", CMD_SINGLE, CMD_OPTIONAL,
173 cmd_AddParm(cptr, "-p", CMD_SINGLE, CMD_OPTIONAL,
174 "number of processes");
176 cmd_AddParm(cptr, "-rxbind", CMD_FLAG, CMD_OPTIONAL,
177 "bind the Rx socket (primary interface only)");
179 cmd_AddParm(cptr, "-audit-interface", CMD_SINGLE, CMD_OPTIONAL,
180 "audit interface (file or sysvmq)");
184 argHandler(struct cmd_syndesc *as, void *arock)
187 /* globalConfPtr provides the handle for the configuration information */
189 /* database directory */
190 if (as->parms[0].items != 0) {
191 globalConfPtr->databaseDirectory =
192 (char *)malloc(strlen(as->parms[0].items->data) + 1);
193 if (globalConfPtr->databaseDirectory == 0)
195 strcpy(globalConfPtr->databaseDirectory, as->parms[0].items->data);
198 /* -cellservdb, cell configuration directory */
199 if (as->parms[1].items != 0) {
200 globalConfPtr->cellConfigdir =
201 (char *)malloc(strlen(as->parms[1].items->data) + 1);
202 if (globalConfPtr->cellConfigdir == 0)
205 strcpy(globalConfPtr->cellConfigdir, as->parms[1].items->data);
207 globalConfPtr->debugFlags |= DF_RECHECKNOAUTH;
210 /* truncate the database */
211 if (as->parms[2].items != 0)
214 /* run without authentication */
215 if (as->parms[3].items != 0)
216 globalConfPtr->debugFlags |= DF_NOAUTH;
218 /* use small hash tables */
219 if (as->parms[4].items != 0)
220 globalConfPtr->debugFlags |= DF_SMALLHT;
222 /* user provided list of ubik database servers */
223 if (as->parms[5].items != 0)
224 parseServerList(as->parms[5].items);
226 /* user provided the number of ubik buffers */
227 if (as->parms[6].items != 0)
228 ubik_nBuffers = atoi(as->parms[6].items->data);
232 /* param 7 (-auditlog) handled below */
234 /* user provided the number of threads */
235 if (as->parms[8].items != 0) {
236 lwps = atoi(as->parms[8].items->data);
238 printf ("Warning: '-p %d' is too big; using %d instead\n",
243 printf ("Warning: '-p %d' is too small; using %d instead\n",
249 /* user provided rxbind option */
250 if (as->parms[9].items != 0) {
254 /* -audit-interface */
255 if (as->parms[10].items != 0) {
256 char *interface = as->parms[10].items->data;
258 if (osi_audit_interface(interface)) {
259 printf("Invalid audit interface '%s'\n", interface);
265 /* needs to be after -audit-interface, so we osi_audit_interface
266 * before we osi_audit_file */
267 if (as->parms[7].items != 0) {
268 char *fileName = as->parms[7].items->data;
270 osi_audit_file(fileName);
279 parseServerList(struct cmd_item *itemPtr)
281 struct cmd_item *save;
284 afs_int32 nservers = 0;
289 /* compute number of servers in the list */
292 itemPtr = itemPtr->next;
295 LogDebug(3, "%d servers\n", nservers);
297 /* now can allocate the space for the server arguments */
298 serverArgs = (char **)malloc((nservers + 2) * sizeof(char *));
306 /* now go through and construct the list of servers */
309 *ptr++ = itemPtr->data;
310 itemPtr = itemPtr->next;
314 ubik_ParseServerList(nservers + 2, serverArgs, &globalConfPtr->myHost,
315 globalConfPtr->serverList);
319 /* free space for the server args */
320 free((char *)serverArgs);
327 * truncates just the database file.
331 truncateDatabase(void)
338 (char *)malloc(strlen(globalConfPtr->databaseDirectory) +
339 strlen(globalConfPtr->databaseName) +
340 strlen(globalConfPtr->databaseExtension) + 1);
344 /* construct the database name */
345 strcpy(path, globalConfPtr->databaseDirectory);
346 strcat(path, globalConfPtr->databaseName);
347 strcat(path, globalConfPtr->databaseExtension);
349 fd = open(path, O_RDWR, 0755);
353 if (ftruncate(fd, 0) != 0) {
366 #include "AFS_component_version_number.c"
369 main(int argc, char **argv)
371 char *whoami = argv[0];
373 struct afsconf_cell cellinfo;
376 afs_uint32 host = ntohl(INADDR_ANY);
378 char clones[MAXHOSTSPERCELL];
380 struct rx_service *tservice;
381 struct rx_securityClass **securityClasses;
382 afs_int32 numClasses;
384 extern int rx_stackSize;
387 /* initialize winsock */
388 if (afs_winsockInit() < 0) {
389 ReportErrorEventAlt(AFSEVT_SVR_WINSOCK_INIT_FAILED, 0, argv[0], 0);
390 fprintf(stderr, "%s: Couldn't initialize winsock.\n", whoami);
397 * The following signal action for AIX is necessary so that in case of a
398 * crash (i.e. core is generated) we can include the user's data section
399 * in the core dump. Unfortunately, by default, only a partial core is
400 * generated which, in many cases, isn't too useful.
402 struct sigaction nsa;
404 sigemptyset(&nsa.sa_mask);
405 nsa.sa_handler = SIG_DFL;
406 nsa.sa_flags = SA_FULLDUMP;
407 sigaction(SIGSEGV, &nsa, NULL);
408 sigaction(SIGABRT, &nsa, NULL);
411 osi_audit(BUDB_StartEvent, 0, AUD_END);
413 initialize_BUDB_error_table();
414 initializeArgHandler();
416 /* Initialize dirpaths */
417 if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
419 ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
421 afs_com_err(whoami, errno, "; Unable to obtain AFS server directory.");
425 memset(globalConfPtr, 0, sizeof(*globalConfPtr));
427 /* set default configuration values */
428 strcpy(dbDir, AFSDIR_SERVER_DB_DIRPATH);
430 globalConfPtr->databaseDirectory = dbDir;
431 globalConfPtr->databaseName = DEFAULT_DBPREFIX;
432 strcpy(cellConfDir, AFSDIR_SERVER_ETC_DIRPATH);
433 globalConfPtr->cellConfigdir = cellConfDir;
435 /* open the log file */
437 globalConfPtr->log = fopen(DEFAULT_LOGNAME,"a");
438 if ( globalConfPtr->log == NULL )
440 printf("Can't open log file %s - aborting\n", DEFAULT_LOGNAME);
447 #ifdef AFS_PTHREAD_ENV
448 SetLogThreadNumProgram( threadNum );
451 /* process the user supplied args */
453 code = cmd_Dispatch(argc, argv);
457 /* exit if there was a help option */
461 /* open the log file */
462 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
463 if (globalConfPtr->log == NULL) {
464 printf("Can't open log file %s - aborting\n",
465 AFSDIR_SERVER_BUDBLOG_FILEPATH);
469 /* keep log closed so can remove it */
471 fclose(globalConfPtr->log);
473 /* open the cell's configuration directory */
474 LogDebug(4, "opening %s\n", globalConfPtr->cellConfigdir);
476 BU_conf = afsconf_Open(globalConfPtr->cellConfigdir);
478 LogError(code, "Failed getting cell info\n");
479 afs_com_err(whoami, code, "Failed getting cell info");
483 code = afsconf_GetLocalCell(BU_conf, lcell, sizeof(lcell));
485 LogError(0, "** Can't determine local cell name!\n");
489 if (globalConfPtr->myHost == 0) {
490 /* if user hasn't supplied a list of servers, extract server
491 * list from the cell's database
494 LogDebug(1, "Using server list from %s cell database.\n", lcell);
496 code = afsconf_GetExtendedCellInfo (BU_conf, lcell, 0, &cellinfo,
499 convert_cell_to_ubik(&cellinfo, &globalConfPtr->myHost,
500 globalConfPtr->serverList);
505 /* initialize ubik */
506 ubik_CRXSecurityProc = afsconf_ClientAuth;
507 ubik_CRXSecurityRock = BU_conf;
509 ubik_SRXSecurityProc = afsconf_ServerAuth;
510 ubik_SRXSecurityRock = BU_conf;
512 ubik_CheckRXSecurityProc = afsconf_CheckAuth;
513 ubik_CheckRXSecurityRock = BU_conf;
515 if (ubik_nBuffers == 0)
518 LogError(0, "Will allocate %d ubik buffers\n", ubik_nBuffers);
521 (char *)malloc(strlen(globalConfPtr->databaseDirectory) +
522 strlen(globalConfPtr->databaseName) + 1);
526 /* construct the database name */
527 strcpy(dbNamePtr, globalConfPtr->databaseDirectory);
528 strcat(dbNamePtr, globalConfPtr->databaseName); /* name prefix */
530 rx_SetRxDeadTime(60); /* 60 seconds inactive before timeout */
534 if (AFSDIR_SERVER_NETRESTRICT_FILEPATH ||
535 AFSDIR_SERVER_NETINFO_FILEPATH) {
537 ccode = parseNetFiles(SHostAddrs, NULL, NULL,
538 ADDRSPERSITE, reason,
539 AFSDIR_SERVER_NETINFO_FILEPATH,
540 AFSDIR_SERVER_NETRESTRICT_FILEPATH);
543 ccode = rx_getAllAddr(SHostAddrs, ADDRSPERSITE);
546 host = SHostAddrs[0];
547 rx_InitHost(host, htons(AFSCONF_BUDBPORT));
551 code = ubik_ServerInitByInfo (globalConfPtr->myHost,
552 htons(AFSCONF_BUDBPORT),
555 dbNamePtr, /* name prefix */
559 LogError(code, "Ubik init failed\n");
560 afs_com_err(whoami, code, "Ubik init failed");
564 afsconf_BuildServerSecurityObjects(BU_conf, 0,
565 &securityClasses, &numClasses);
567 /* Disable jumbograms */
571 rx_NewServiceHost(host, 0, BUDB_SERVICE, "BackupDatabase",
572 securityClasses, numClasses, BUDB_ExecuteRequest);
574 if (tservice == (struct rx_service *)0) {
575 LogError(0, "Could not create backup database rx service\n");
576 printf("Could not create backup database rx service\n");
579 rx_SetMinProcs(tservice, 1);
580 rx_SetMaxProcs(tservice, lwps);
581 rx_SetStackSize(tservice, 10000);
583 /* allow super users to manage RX statistics */
584 rx_SetRxStatUserOk(BU_rxstat_userok);
586 /* misc. initialization */
588 /* database dump synchronization */
589 memset(dumpSyncPtr, 0, sizeof(*dumpSyncPtr));
590 Lock_Init(&dumpSyncPtr->ds_lock);
592 rx_StartServer(0); /* start handling requests */
599 currentTime = time(0);
600 LogError(0, "Ready to process requests at %s\n", ctime(¤tTime));
602 rx_ServerProc(NULL); /* donate this LWP */
605 osi_audit(BUDB_FinishEvent, code, AUD_END);
610 consistencyCheckDb(void)
612 /* do consistency checks on structure sizes */
613 if ((sizeof(struct htBlock) > BLOCKSIZE)
614 || (sizeof(struct vfBlock) > BLOCKSIZE)
615 || (sizeof(struct viBlock) > BLOCKSIZE)
616 || (sizeof(struct dBlock) > BLOCKSIZE)
617 || (sizeof(struct tBlock) > BLOCKSIZE)
619 fprintf(stderr, "Block layout error!\n");
625 LogDebug(int level, char *fmt, ... )
631 if (debugging >= level) {
632 /* log normally closed so can remove it */
633 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
634 if (globalConfPtr->log != NULL) {
635 vfprintf(globalConfPtr->log, fmt, ap);
636 fflush(globalConfPtr->log);
637 fclose(globalConfPtr->log);
647 static char timestamp[20];
650 strftime(timestamp, 20, "%m/%d/%Y %H:%M:%S", lt);
661 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
662 if (globalConfPtr->log != NULL) {
664 fprintf(globalConfPtr->log, "%s ", TimeStamp(now));
666 vfprintf(globalConfPtr->log, fmt, ap);
667 fflush(globalConfPtr->log);
668 fclose(globalConfPtr->log);
674 LogError(long code, char *fmt, ... )
680 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
682 if (globalConfPtr->log != NULL) {
684 fprintf(globalConfPtr->log, "%s ", TimeStamp(now));
687 fprintf(globalConfPtr->log, "%s: %s\n", afs_error_table_name(code),
688 afs_error_message(code));
689 vfprintf(globalConfPtr->log, fmt, ap );
690 fflush(globalConfPtr->log);
691 fclose(globalConfPtr->log);
702 LogNetDump(struct dump *dumpPtr)
704 struct dump hostDump;
705 extern buServerConfP globalConfPtr;
707 dump_ntoh(dumpPtr, &hostDump);
709 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
710 if (globalConfPtr->log != NULL) {
711 printDump(globalConfPtr->log, &hostDump);
712 fclose(globalConfPtr->log);