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>
17 #include <WINNT/afsevent.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>
32 #include "budb_errs.h"
34 #include "error_macros.h"
35 #include "budb_internal.h"
38 struct ubik_dbase *BU_dbase;
39 struct afsconf_dir *BU_conf; /* for getting cell info */
41 int argHandler(struct cmd_syndesc *, void *);
42 int truncateDatabase(void);
43 int parseServerList(struct cmd_item *);
45 char lcell[MAXKTCREALMLEN];
46 afs_uint32 myHost = 0;
49 /* server's global configuration information. This is exported to other
53 buServerConfT globalConf;
54 buServerConfP globalConfPtr = &globalConf;
55 char dbDir[AFSDIR_PATH_MAX], cellConfDir[AFSDIR_PATH_MAX];
56 /* debugging control */
65 #define ADDRSPERSITE 16 /* Same global is in rx/rx_user.c */
66 afs_uint32 SHostAddrs[ADDRSPERSITE];
68 /* check whether caller is authorized to manage RX statistics */
70 BU_rxstat_userok(struct rx_call *call)
72 return afsconf_SuperUser(BU_conf, call, NULL);
76 convert_cell_to_ubik(struct afsconf_cell *cellinfo, afs_uint32 *myHost,
77 afs_uint32 *serverList)
84 gethostname(hostname, sizeof(hostname));
85 th = gethostbyname(hostname);
87 printf("prserver: couldn't get address of this host.\n");
90 memcpy(myHost, th->h_addr, sizeof(afs_uint32));
92 for (i = 0; i < cellinfo->numServers; i++)
93 /* omit my host from serverList */
94 if (cellinfo->hostAddr[i].sin_addr.s_addr != *myHost)
95 *serverList++ = cellinfo->hostAddr[i].sin_addr.s_addr;
97 *serverList = 0; /* terminate list */
102 * The whole purpose of MyBeforeProc is to detect
103 * if the -help option was not within the command line.
104 * If it were, this routine would never have been called.
107 MyBeforeProc(struct cmd_syndesc *as, void *arock)
113 /* initializeCommands
114 * initialize all the supported commands and their arguments
118 initializeArgHandler(void)
120 struct cmd_syndesc *cptr;
122 cmd_SetBeforeProc(MyBeforeProc, NULL);
124 cptr = cmd_CreateSyntax(NULL, argHandler, NULL, "Backup database server");
126 cmd_AddParm(cptr, "-database", CMD_SINGLE, CMD_OPTIONAL,
127 "database directory");
129 cmd_AddParm(cptr, "-cellservdb", CMD_SINGLE, CMD_OPTIONAL,
130 "cell configuration directory");
132 cmd_AddParm(cptr, "-resetdb", CMD_FLAG, CMD_OPTIONAL,
133 "truncate the database");
135 cmd_AddParm(cptr, "-noauth", CMD_FLAG, CMD_OPTIONAL,
136 "run without authentication");
138 cmd_AddParm(cptr, "-smallht", CMD_FLAG, CMD_OPTIONAL,
139 "use small hash tables");
141 cmd_AddParm(cptr, "-servers", CMD_LIST, CMD_OPTIONAL,
142 "list of ubik database servers");
144 cmd_AddParm(cptr, "-ubikbuffers", CMD_SINGLE, CMD_OPTIONAL,
145 "the number of ubik buffers");
147 cmd_AddParm(cptr, "-auditlog", CMD_SINGLE, CMD_OPTIONAL,
150 cmd_AddParm(cptr, "-p", CMD_SINGLE, CMD_OPTIONAL,
151 "number of processes");
153 cmd_AddParm(cptr, "-rxbind", CMD_FLAG, CMD_OPTIONAL,
154 "bind the Rx socket (primary interface only)");
156 cmd_AddParm(cptr, "-audit-interface", CMD_SINGLE, CMD_OPTIONAL,
157 "audit interface (file or sysvmq)");
161 argHandler(struct cmd_syndesc *as, void *arock)
164 /* globalConfPtr provides the handle for the configuration information */
166 /* database directory */
167 if (as->parms[0].items != 0) {
168 globalConfPtr->databaseDirectory =
169 (char *)malloc(strlen(as->parms[0].items->data) + 1);
170 if (globalConfPtr->databaseDirectory == 0)
172 strcpy(globalConfPtr->databaseDirectory, as->parms[0].items->data);
175 /* -cellservdb, cell configuration directory */
176 if (as->parms[1].items != 0) {
177 globalConfPtr->cellConfigdir =
178 (char *)malloc(strlen(as->parms[1].items->data) + 1);
179 if (globalConfPtr->cellConfigdir == 0)
182 strcpy(globalConfPtr->cellConfigdir, as->parms[1].items->data);
184 globalConfPtr->debugFlags |= DF_RECHECKNOAUTH;
187 /* truncate the database */
188 if (as->parms[2].items != 0)
191 /* run without authentication */
192 if (as->parms[3].items != 0)
193 globalConfPtr->debugFlags |= DF_NOAUTH;
195 /* use small hash tables */
196 if (as->parms[4].items != 0)
197 globalConfPtr->debugFlags |= DF_SMALLHT;
199 /* user provided list of ubik database servers */
200 if (as->parms[5].items != 0)
201 parseServerList(as->parms[5].items);
203 /* user provided the number of ubik buffers */
204 if (as->parms[6].items != 0)
205 ubik_nBuffers = atoi(as->parms[6].items->data);
209 /* param 7 (-auditlog) handled below */
211 /* user provided the number of threads */
212 if (as->parms[8].items != 0) {
213 lwps = atoi(as->parms[8].items->data);
215 printf ("Warning: '-p %d' is too big; using %d instead\n",
220 printf ("Warning: '-p %d' is too small; using %d instead\n",
226 /* user provided rxbind option */
227 if (as->parms[9].items != 0) {
231 /* -audit-interface */
232 if (as->parms[10].items != 0) {
233 char *interface = as->parms[10].items->data;
235 if (osi_audit_interface(interface)) {
236 printf("Invalid audit interface '%s'\n", interface);
242 /* needs to be after -audit-interface, so we osi_audit_interface
243 * before we osi_audit_file */
244 if (as->parms[7].items != 0) {
245 char *fileName = as->parms[7].items->data;
247 osi_audit_file(fileName);
256 parseServerList(struct cmd_item *itemPtr)
258 struct cmd_item *save;
261 afs_int32 nservers = 0;
266 /* compute number of servers in the list */
269 itemPtr = itemPtr->next;
272 LogDebug(3, "%d servers\n", nservers);
274 /* now can allocate the space for the server arguments */
275 serverArgs = (char **)malloc((nservers + 2) * sizeof(char *));
283 /* now go through and construct the list of servers */
286 *ptr++ = itemPtr->data;
287 itemPtr = itemPtr->next;
291 ubik_ParseServerList(nservers + 2, serverArgs, &globalConfPtr->myHost,
292 globalConfPtr->serverList);
296 /* free space for the server args */
297 free((char *)serverArgs);
304 * truncates just the database file.
308 truncateDatabase(void)
314 path = malloc(strlen(globalConfPtr->databaseDirectory) +
315 strlen(globalConfPtr->databaseName) +
316 strlen(globalConfPtr->databaseExtension) + 1);
320 /* construct the database name */
321 strcpy(path, globalConfPtr->databaseDirectory);
322 strcat(path, globalConfPtr->databaseName);
323 strcat(path, globalConfPtr->databaseExtension);
325 fd = open(path, O_RDWR, 0755);
329 if (ftruncate(fd, 0) != 0) {
344 #include "AFS_component_version_number.c"
347 main(int argc, char **argv)
349 char *whoami = argv[0];
351 struct afsconf_cell cellinfo;
354 afs_uint32 host = ntohl(INADDR_ANY);
356 char clones[MAXHOSTSPERCELL];
358 struct rx_service *tservice;
359 struct rx_securityClass **securityClasses;
360 afs_int32 numClasses;
362 extern int rx_stackSize;
365 /* initialize winsock */
366 if (afs_winsockInit() < 0) {
367 ReportErrorEventAlt(AFSEVT_SVR_WINSOCK_INIT_FAILED, 0, argv[0], 0);
368 fprintf(stderr, "%s: Couldn't initialize winsock.\n", whoami);
375 * The following signal action for AIX is necessary so that in case of a
376 * crash (i.e. core is generated) we can include the user's data section
377 * in the core dump. Unfortunately, by default, only a partial core is
378 * generated which, in many cases, isn't too useful.
380 struct sigaction nsa;
382 sigemptyset(&nsa.sa_mask);
383 nsa.sa_handler = SIG_DFL;
384 nsa.sa_flags = SA_FULLDUMP;
385 sigaction(SIGSEGV, &nsa, NULL);
386 sigaction(SIGABRT, &nsa, NULL);
389 osi_audit(BUDB_StartEvent, 0, AUD_END);
391 initialize_BUDB_error_table();
392 initializeArgHandler();
394 /* Initialize dirpaths */
395 if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
397 ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
399 afs_com_err(whoami, errno, "; Unable to obtain AFS server directory.");
403 memset(globalConfPtr, 0, sizeof(*globalConfPtr));
405 /* set default configuration values */
406 strcpy(dbDir, AFSDIR_SERVER_DB_DIRPATH);
408 globalConfPtr->databaseDirectory = dbDir;
409 globalConfPtr->databaseName = DEFAULT_DBPREFIX;
410 strcpy(cellConfDir, AFSDIR_SERVER_ETC_DIRPATH);
411 globalConfPtr->cellConfigdir = cellConfDir;
413 /* open the log file */
415 globalConfPtr->log = fopen(DEFAULT_LOGNAME,"a");
416 if ( globalConfPtr->log == NULL )
418 printf("Can't open log file %s - aborting\n", DEFAULT_LOGNAME);
425 #ifdef AFS_PTHREAD_ENV
426 SetLogThreadNumProgram( rx_GetThreadNum );
429 /* process the user supplied args */
431 code = cmd_Dispatch(argc, argv);
435 /* exit if there was a help option */
439 /* open the log file */
440 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
441 if (globalConfPtr->log == NULL) {
442 printf("Can't open log file %s - aborting\n",
443 AFSDIR_SERVER_BUDBLOG_FILEPATH);
447 /* keep log closed so can remove it */
449 fclose(globalConfPtr->log);
451 /* open the cell's configuration directory */
452 LogDebug(4, "opening %s\n", globalConfPtr->cellConfigdir);
454 BU_conf = afsconf_Open(globalConfPtr->cellConfigdir);
456 LogError(code, "Failed getting cell info\n");
457 afs_com_err(whoami, code, "Failed getting cell info");
461 code = afsconf_GetLocalCell(BU_conf, lcell, sizeof(lcell));
463 LogError(0, "** Can't determine local cell name!\n");
467 if (globalConfPtr->myHost == 0) {
468 /* if user hasn't supplied a list of servers, extract server
469 * list from the cell's database
472 LogDebug(1, "Using server list from %s cell database.\n", lcell);
474 code = afsconf_GetExtendedCellInfo (BU_conf, lcell, 0, &cellinfo,
477 convert_cell_to_ubik(&cellinfo, &globalConfPtr->myHost,
478 globalConfPtr->serverList);
483 /* initialize ubik */
484 ubik_SetClientSecurityProcs(afsconf_ClientAuth, afsconf_UpToDate, BU_conf);
485 ubik_SetServerSecurityProcs(afsconf_BuildServerSecurityObjects,
486 afsconf_CheckAuth, BU_conf);
488 if (ubik_nBuffers == 0)
491 LogError(0, "Will allocate %d ubik buffers\n", ubik_nBuffers);
494 (char *)malloc(strlen(globalConfPtr->databaseDirectory) +
495 strlen(globalConfPtr->databaseName) + 1);
499 /* construct the database name */
500 strcpy(dbNamePtr, globalConfPtr->databaseDirectory);
501 strcat(dbNamePtr, globalConfPtr->databaseName); /* name prefix */
503 rx_SetRxDeadTime(60); /* 60 seconds inactive before timeout */
507 if (AFSDIR_SERVER_NETRESTRICT_FILEPATH ||
508 AFSDIR_SERVER_NETINFO_FILEPATH) {
510 ccode = parseNetFiles(SHostAddrs, NULL, NULL,
511 ADDRSPERSITE, reason,
512 AFSDIR_SERVER_NETINFO_FILEPATH,
513 AFSDIR_SERVER_NETRESTRICT_FILEPATH);
516 ccode = rx_getAllAddr(SHostAddrs, ADDRSPERSITE);
519 host = SHostAddrs[0];
520 rx_InitHost(host, htons(AFSCONF_BUDBPORT));
524 code = ubik_ServerInitByInfo (globalConfPtr->myHost,
525 htons(AFSCONF_BUDBPORT),
528 dbNamePtr, /* name prefix */
532 LogError(code, "Ubik init failed\n");
533 afs_com_err(whoami, code, "Ubik init failed");
537 afsconf_BuildServerSecurityObjects(BU_conf, &securityClasses, &numClasses);
539 /* Disable jumbograms */
543 rx_NewServiceHost(host, 0, BUDB_SERVICE, "BackupDatabase",
544 securityClasses, numClasses, BUDB_ExecuteRequest);
546 if (tservice == (struct rx_service *)0) {
547 LogError(0, "Could not create backup database rx service\n");
548 printf("Could not create backup database rx service\n");
551 rx_SetMinProcs(tservice, 1);
552 rx_SetMaxProcs(tservice, lwps);
553 rx_SetStackSize(tservice, 10000);
555 /* allow super users to manage RX statistics */
556 rx_SetRxStatUserOk(BU_rxstat_userok);
558 /* misc. initialization */
560 /* database dump synchronization */
561 memset(dumpSyncPtr, 0, sizeof(*dumpSyncPtr));
562 Lock_Init(&dumpSyncPtr->ds_lock);
564 rx_StartServer(0); /* start handling requests */
571 currentTime = time(0);
572 LogError(0, "Ready to process requests at %s\n", ctime(¤tTime));
574 rx_ServerProc(NULL); /* donate this LWP */
577 osi_audit(BUDB_FinishEvent, code, AUD_END);
582 consistencyCheckDb(void)
584 /* do consistency checks on structure sizes */
585 if ((sizeof(struct htBlock) > BLOCKSIZE)
586 || (sizeof(struct vfBlock) > BLOCKSIZE)
587 || (sizeof(struct viBlock) > BLOCKSIZE)
588 || (sizeof(struct dBlock) > BLOCKSIZE)
589 || (sizeof(struct tBlock) > BLOCKSIZE)
591 fprintf(stderr, "Block layout error!\n");
597 LogDebug(int level, char *fmt, ... )
603 if (debugging >= level) {
604 /* log normally closed so can remove it */
605 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
606 if (globalConfPtr->log != NULL) {
607 vfprintf(globalConfPtr->log, fmt, ap);
608 fflush(globalConfPtr->log);
609 fclose(globalConfPtr->log);
619 static char timestamp[20];
622 strftime(timestamp, 20, "%m/%d/%Y %H:%M:%S", lt);
633 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
634 if (globalConfPtr->log != NULL) {
636 fprintf(globalConfPtr->log, "%s ", TimeStamp(now));
638 vfprintf(globalConfPtr->log, fmt, ap);
639 fflush(globalConfPtr->log);
640 fclose(globalConfPtr->log);
646 LogError(long code, char *fmt, ... )
652 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
654 if (globalConfPtr->log != NULL) {
656 fprintf(globalConfPtr->log, "%s ", TimeStamp(now));
659 fprintf(globalConfPtr->log, "%s: %s\n", afs_error_table_name(code),
660 afs_error_message(code));
661 vfprintf(globalConfPtr->log, fmt, ap );
662 fflush(globalConfPtr->log);
663 fclose(globalConfPtr->log);
674 LogNetDump(struct dump *dumpPtr)
676 struct dump hostDump;
677 extern buServerConfP globalConfPtr;
679 dump_ntoh(dumpPtr, &hostDump);
681 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
682 if (globalConfPtr->log != NULL) {
683 printDump(globalConfPtr->log, &hostDump);
684 fclose(globalConfPtr->log);