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>
18 #include <WINNT/afsevent.h>
20 #include <netinet/in.h>
27 #include <sys/types.h>
36 #include <rx/rx_globals.h>
37 #include <afs/cellconfig.h>
38 #include <afs/bubasics.h>
39 #include <afs/afsutil.h>
40 #include <afs/com_err.h>
45 #include "budb_errs.h"
47 #include "error_macros.h"
48 #include "budb_prototypes.h"
50 #include "afs/audit.h"
52 struct ubik_dbase *BU_dbase;
53 struct afsconf_dir *BU_conf; /* for getting cell info */
55 int argHandler(struct cmd_syndesc *, void *);
56 int truncateDatabase(void);
57 int parseServerList(struct cmd_item *);
59 char lcell[MAXKTCREALMLEN];
63 /* server's global configuration information. This is exported to other
67 buServerConfT globalConf;
68 buServerConfP globalConfPtr = &globalConf;
69 char dbDir[AFSDIR_PATH_MAX], cellConfDir[AFSDIR_PATH_MAX];
70 /* debugging control */
79 #define ADDRSPERSITE 16 /* Same global is in rx/rx_user.c */
80 afs_uint32 SHostAddrs[ADDRSPERSITE];
82 #if defined(AFS_PTHREAD_ENV)
86 return pthread_getspecific(rx_thread_id_key);
90 /* check whether caller is authorized to manage RX statistics */
92 BU_rxstat_userok(struct rx_call *call)
94 return afsconf_SuperUser(BU_conf, call, NULL);
98 convert_cell_to_ubik(struct afsconf_cell *cellinfo, afs_int32 *myHost,
99 afs_int32 *serverList)
106 gethostname(hostname, sizeof(hostname));
107 th = gethostbyname(hostname);
109 printf("prserver: couldn't get address of this host.\n");
112 memcpy(myHost, th->h_addr, sizeof(afs_int32));
114 for (i = 0; i < cellinfo->numServers; i++)
115 /* omit my host from serverList */
116 if (cellinfo->hostAddr[i].sin_addr.s_addr != *myHost)
117 *serverList++ = cellinfo->hostAddr[i].sin_addr.s_addr;
119 *serverList = 0; /* terminate list */
124 * The whole purpose of MyBeforeProc is to detect
125 * if the -help option was not within the command line.
126 * If it were, this routine would never have been called.
129 MyBeforeProc(register struct cmd_syndesc *as, void *arock)
135 /* initializeCommands
136 * initialize all the supported commands and their arguments
140 initializeArgHandler(void)
142 struct cmd_syndesc *cptr;
144 cmd_SetBeforeProc(MyBeforeProc, NULL);
146 cptr = cmd_CreateSyntax(NULL, argHandler, NULL, "Backup database server");
148 cmd_AddParm(cptr, "-database", CMD_SINGLE, CMD_OPTIONAL,
149 "database directory");
151 cmd_AddParm(cptr, "-cellservdb", CMD_SINGLE, CMD_OPTIONAL,
152 "cell configuration directory");
154 cmd_AddParm(cptr, "-resetdb", CMD_FLAG, CMD_OPTIONAL,
155 "truncate the database");
157 cmd_AddParm(cptr, "-noauth", CMD_FLAG, CMD_OPTIONAL,
158 "run without authentication");
160 cmd_AddParm(cptr, "-smallht", CMD_FLAG, CMD_OPTIONAL,
161 "use small hash tables");
163 cmd_AddParm(cptr, "-servers", CMD_LIST, CMD_OPTIONAL,
164 "list of ubik database servers");
166 cmd_AddParm(cptr, "-ubikbuffers", CMD_SINGLE, CMD_OPTIONAL,
167 "the number of ubik buffers");
169 cmd_AddParm(cptr, "-auditlog", CMD_SINGLE, CMD_OPTIONAL,
172 cmd_AddParm(cptr, "-p", CMD_SINGLE, CMD_OPTIONAL,
173 "number of processes");
175 cmd_AddParm(cptr, "-rxbind", CMD_FLAG, CMD_OPTIONAL,
176 "bind the Rx socket (primary interface only)");
181 argHandler(struct cmd_syndesc *as, void *arock)
184 /* globalConfPtr provides the handle for the configuration information */
186 /* database directory */
187 if (as->parms[0].items != 0) {
188 globalConfPtr->databaseDirectory =
189 (char *)malloc(strlen(as->parms[0].items->data) + 1);
190 if (globalConfPtr->databaseDirectory == 0)
192 strcpy(globalConfPtr->databaseDirectory, as->parms[0].items->data);
195 /* -cellservdb, cell configuration directory */
196 if (as->parms[1].items != 0) {
197 globalConfPtr->cellConfigdir =
198 (char *)malloc(strlen(as->parms[1].items->data) + 1);
199 if (globalConfPtr->cellConfigdir == 0)
202 strcpy(globalConfPtr->cellConfigdir, as->parms[1].items->data);
204 globalConfPtr->debugFlags |= DF_RECHECKNOAUTH;
207 /* truncate the database */
208 if (as->parms[2].items != 0)
211 /* run without authentication */
212 if (as->parms[3].items != 0)
213 globalConfPtr->debugFlags |= DF_NOAUTH;
215 /* use small hash tables */
216 if (as->parms[4].items != 0)
217 globalConfPtr->debugFlags |= DF_SMALLHT;
219 /* user provided list of ubik database servers */
220 if (as->parms[5].items != 0)
221 parseServerList(as->parms[5].items);
223 /* user provided the number of ubik buffers */
224 if (as->parms[6].items != 0)
225 ubik_nBuffers = atoi(as->parms[6].items->data);
229 if (as->parms[7].items != 0) {
230 char *fileName = as->parms[7].items->data;
232 osi_audit_file(fileName);
235 /* user provided the number of threads */
236 if (as->parms[8].items != 0) {
237 lwps = atoi(as->parms[8].items->data);
239 printf ("Warning: '-p %d' is too big; using %d instead\n",
244 printf ("Warning: '-p %d' is too small; using %d instead\n",
250 /* user provided rxbind option */
251 if (as->parms[9].items != 0) {
261 parseServerList(struct cmd_item *itemPtr)
263 struct cmd_item *save;
266 afs_int32 nservers = 0;
271 /* compute number of servers in the list */
274 itemPtr = itemPtr->next;
277 LogDebug(3, "%d servers\n", nservers);
279 /* now can allocate the space for the server arguments */
280 serverArgs = (char **)malloc((nservers + 2) * sizeof(char *));
288 /* now go through and construct the list of servers */
291 *ptr++ = itemPtr->data;
292 itemPtr = itemPtr->next;
296 ubik_ParseServerList(nservers + 2, serverArgs, &globalConfPtr->myHost,
297 globalConfPtr->serverList);
301 /* free space for the server args */
302 free((char *)serverArgs);
309 * truncates just the database file.
313 truncateDatabase(void)
320 (char *)malloc(strlen(globalConfPtr->databaseDirectory) +
321 strlen(globalConfPtr->databaseName) +
322 strlen(globalConfPtr->databaseExtension) + 1);
326 /* construct the database name */
327 strcpy(path, globalConfPtr->databaseDirectory);
328 strcat(path, globalConfPtr->databaseName);
329 strcat(path, globalConfPtr->databaseExtension);
331 fd = open(path, O_RDWR, 0755);
335 if (ftruncate(fd, 0) != 0) {
348 #include "AFS_component_version_number.c"
351 main(int argc, char **argv)
353 char *whoami = argv[0];
355 struct afsconf_cell cellinfo;
358 afs_uint32 host = ntohl(INADDR_ANY);
360 char clones[MAXHOSTSPERCELL];
362 struct rx_service *tservice;
363 struct rx_securityClass *sca[3];
365 extern int rx_stackSize;
368 /* initialize winsock */
369 if (afs_winsockInit() < 0) {
370 ReportErrorEventAlt(AFSEVT_SVR_WINSOCK_INIT_FAILED, 0, argv[0], 0);
371 fprintf(stderr, "%s: Couldn't initialize winsock.\n", whoami);
378 * The following signal action for AIX is necessary so that in case of a
379 * crash (i.e. core is generated) we can include the user's data section
380 * in the core dump. Unfortunately, by default, only a partial core is
381 * generated which, in many cases, isn't too useful.
383 struct sigaction nsa;
385 sigemptyset(&nsa.sa_mask);
386 nsa.sa_handler = SIG_DFL;
387 nsa.sa_flags = SA_FULLDUMP;
388 sigaction(SIGSEGV, &nsa, NULL);
389 sigaction(SIGABRT, &nsa, NULL);
392 osi_audit(BUDB_StartEvent, 0, AUD_END);
394 initialize_BUDB_error_table();
395 initializeArgHandler();
397 /* Initialize dirpaths */
398 if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
400 ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
402 afs_com_err(whoami, errno, "; Unable to obtain AFS server directory.");
406 memset(globalConfPtr, 0, sizeof(*globalConfPtr));
408 /* set default configuration values */
409 strcpy(dbDir, AFSDIR_SERVER_DB_DIRPATH);
411 globalConfPtr->databaseDirectory = dbDir;
412 globalConfPtr->databaseName = DEFAULT_DBPREFIX;
413 strcpy(cellConfDir, AFSDIR_SERVER_ETC_DIRPATH);
414 globalConfPtr->cellConfigdir = cellConfDir;
416 /* open the log file */
418 globalConfPtr->log = fopen(DEFAULT_LOGNAME,"a");
419 if ( globalConfPtr->log == NULL )
421 printf("Can't open log file %s - aborting\n", DEFAULT_LOGNAME);
428 #ifdef AFS_PTHREAD_ENV
429 SetLogThreadNumProgram( threadNum );
432 /* process the user supplied args */
434 code = cmd_Dispatch(argc, argv);
438 /* exit if there was a help option */
442 /* open the log file */
443 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
444 if (globalConfPtr->log == NULL) {
445 printf("Can't open log file %s - aborting\n",
446 AFSDIR_SERVER_BUDBLOG_FILEPATH);
450 /* keep log closed so can remove it */
452 fclose(globalConfPtr->log);
454 /* open the cell's configuration directory */
455 LogDebug(4, "opening %s\n", globalConfPtr->cellConfigdir);
457 BU_conf = afsconf_Open(globalConfPtr->cellConfigdir);
459 LogError(code, "Failed getting cell info\n");
460 afs_com_err(whoami, code, "Failed getting cell info");
464 code = afsconf_GetLocalCell(BU_conf, lcell, sizeof(lcell));
466 LogError(0, "** Can't determine local cell name!\n");
470 if (globalConfPtr->myHost == 0) {
471 /* if user hasn't supplied a list of servers, extract server
472 * list from the cell's database
475 LogDebug(1, "Using server list from %s cell database.\n", lcell);
477 code = afsconf_GetExtendedCellInfo (BU_conf, lcell, 0, &cellinfo,
480 convert_cell_to_ubik(&cellinfo, &globalConfPtr->myHost,
481 globalConfPtr->serverList);
486 /* initialize ubik */
487 ubik_CRXSecurityProc = afsconf_ClientAuth;
488 ubik_CRXSecurityRock = BU_conf;
490 ubik_SRXSecurityProc = afsconf_ServerAuth;
491 ubik_SRXSecurityRock = BU_conf;
493 ubik_CheckRXSecurityProc = afsconf_CheckAuth;
494 ubik_CheckRXSecurityRock = BU_conf;
496 if (ubik_nBuffers == 0)
499 LogError(0, "Will allocate %d ubik buffers\n", ubik_nBuffers);
502 (char *)malloc(strlen(globalConfPtr->databaseDirectory) +
503 strlen(globalConfPtr->databaseName) + 1);
507 /* construct the database name */
508 strcpy(dbNamePtr, globalConfPtr->databaseDirectory);
509 strcat(dbNamePtr, globalConfPtr->databaseName); /* name prefix */
511 rx_SetRxDeadTime(60); /* 60 seconds inactive before timeout */
515 if (AFSDIR_SERVER_NETRESTRICT_FILEPATH ||
516 AFSDIR_SERVER_NETINFO_FILEPATH) {
518 ccode = parseNetFiles(SHostAddrs, NULL, NULL,
519 ADDRSPERSITE, reason,
520 AFSDIR_SERVER_NETINFO_FILEPATH,
521 AFSDIR_SERVER_NETRESTRICT_FILEPATH);
524 ccode = rx_getAllAddr(SHostAddrs, ADDRSPERSITE);
527 host = SHostAddrs[0];
528 rx_InitHost(host, htons(AFSCONF_BUDBPORT));
532 code = ubik_ServerInitByInfo (globalConfPtr->myHost,
533 htons(AFSCONF_BUDBPORT),
536 dbNamePtr, /* name prefix */
540 LogError(code, "Ubik init failed\n");
541 afs_com_err(whoami, code, "Ubik init failed");
545 sca[RX_SCINDEX_NULL] = rxnull_NewServerSecurityObject();
546 sca[RX_SCINDEX_VAB] = 0;
547 sca[RX_SCINDEX_KAD] =
548 rxkad_NewServerSecurityObject(rxkad_clear, BU_conf, afsconf_GetKey,
551 /* Disable jumbograms */
555 rx_NewServiceHost(host, 0, BUDB_SERVICE, "BackupDatabase", sca, 3,
556 BUDB_ExecuteRequest);
557 if (tservice == (struct rx_service *)0) {
558 LogError(0, "Could not create backup database rx service\n");
559 printf("Could not create backup database rx service\n");
562 rx_SetMinProcs(tservice, 1);
563 rx_SetMaxProcs(tservice, lwps);
564 rx_SetStackSize(tservice, 10000);
566 /* allow super users to manage RX statistics */
567 rx_SetRxStatUserOk(BU_rxstat_userok);
569 /* misc. initialization */
571 /* database dump synchronization */
572 memset(dumpSyncPtr, 0, sizeof(*dumpSyncPtr));
573 Lock_Init(&dumpSyncPtr->ds_lock);
575 rx_StartServer(0); /* start handling requests */
582 currentTime = time(0);
583 LogError(0, "Ready to process requests at %s\n", ctime(¤tTime));
585 rx_ServerProc(NULL); /* donate this LWP */
588 osi_audit(BUDB_FinishEvent, code, AUD_END);
593 consistencyCheckDb(void)
595 /* do consistency checks on structure sizes */
596 if ((sizeof(struct htBlock) > BLOCKSIZE)
597 || (sizeof(struct vfBlock) > BLOCKSIZE)
598 || (sizeof(struct viBlock) > BLOCKSIZE)
599 || (sizeof(struct dBlock) > BLOCKSIZE)
600 || (sizeof(struct tBlock) > BLOCKSIZE)
602 fprintf(stderr, "Block layout error!\n");
608 LogDebug(int level, char *fmt, ... )
614 if (debugging >= level) {
615 /* log normally closed so can remove it */
616 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
617 if (globalConfPtr->log != NULL) {
618 vfprintf(globalConfPtr->log, fmt, ap);
619 fflush(globalConfPtr->log);
620 fclose(globalConfPtr->log);
630 static char timestamp[20];
633 strftime(timestamp, 20, "%m/%d/%Y %H:%M:%S", lt);
644 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
645 if (globalConfPtr->log != NULL) {
647 fprintf(globalConfPtr->log, "%s ", TimeStamp(now));
649 vfprintf(globalConfPtr->log, fmt, ap);
650 fflush(globalConfPtr->log);
651 fclose(globalConfPtr->log);
657 LogError(long code, char *fmt, ... )
663 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
665 if (globalConfPtr->log != NULL) {
667 fprintf(globalConfPtr->log, "%s ", TimeStamp(now));
670 fprintf(globalConfPtr->log, "%s: %s\n", afs_error_table_name(code),
671 afs_error_message(code));
672 vfprintf(globalConfPtr->log, fmt, ap );
673 fflush(globalConfPtr->log);
674 fclose(globalConfPtr->log);
685 LogNetDump(struct dump *dumpPtr)
687 struct dump hostDump;
688 extern buServerConfP globalConfPtr;
690 dump_ntoh(dumpPtr, &hostDump);
692 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
693 if (globalConfPtr->log != NULL) {
694 printDump(globalConfPtr->log, &hostDump);
695 fclose(globalConfPtr->log);