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>
27 #include <rx/rx_globals.h>
28 #include <afs/cellconfig.h>
29 #include <afs/bubasics.h>
30 #include <afs/afsutil.h>
31 #include <afs/com_err.h>
32 #include <afs/audit.h>
34 #include "budb_errs.h"
36 #include "error_macros.h"
37 #include "budb_internal.h"
40 struct ubik_dbase *BU_dbase;
41 struct afsconf_dir *BU_conf; /* for getting cell info */
43 int argHandler(struct cmd_syndesc *, void *);
44 int truncateDatabase(void);
45 int parseServerList(struct cmd_item *);
47 char lcell[MAXKTCREALMLEN];
48 afs_uint32 myHost = 0;
51 /* server's global configuration information. This is exported to other
55 buServerConfT globalConf;
56 buServerConfP globalConfPtr = &globalConf;
57 char dbDir[AFSDIR_PATH_MAX], cellConfDir[AFSDIR_PATH_MAX];
58 /* debugging control */
67 #define ADDRSPERSITE 16 /* Same global is in rx/rx_user.c */
68 afs_uint32 SHostAddrs[ADDRSPERSITE];
70 /* check whether caller is authorized to manage RX statistics */
72 BU_rxstat_userok(struct rx_call *call)
74 return afsconf_SuperUser(BU_conf, call, NULL);
78 convert_cell_to_ubik(struct afsconf_cell *cellinfo, afs_uint32 *myHost,
79 afs_uint32 *serverList)
86 gethostname(hostname, sizeof(hostname));
87 th = gethostbyname(hostname);
89 printf("prserver: couldn't get address of this host.\n");
92 memcpy(myHost, th->h_addr, sizeof(afs_uint32));
94 for (i = 0; i < cellinfo->numServers; i++)
95 /* omit my host from serverList */
96 if (cellinfo->hostAddr[i].sin_addr.s_addr != *myHost)
97 *serverList++ = cellinfo->hostAddr[i].sin_addr.s_addr;
99 *serverList = 0; /* terminate list */
104 * The whole purpose of MyBeforeProc is to detect
105 * if the -help option was not within the command line.
106 * If it were, this routine would never have been called.
109 MyBeforeProc(struct cmd_syndesc *as, void *arock)
115 /* initializeCommands
116 * initialize all the supported commands and their arguments
120 initializeArgHandler(void)
122 struct cmd_syndesc *cptr;
124 cmd_SetBeforeProc(MyBeforeProc, NULL);
126 cptr = cmd_CreateSyntax(NULL, argHandler, NULL, "Backup database server");
128 cmd_AddParm(cptr, "-database", CMD_SINGLE, CMD_OPTIONAL,
129 "database directory");
131 cmd_AddParm(cptr, "-cellservdb", CMD_SINGLE, CMD_OPTIONAL,
132 "cell configuration directory");
134 cmd_AddParm(cptr, "-resetdb", CMD_FLAG, CMD_OPTIONAL,
135 "truncate the database");
137 cmd_AddParm(cptr, "-noauth", CMD_FLAG, CMD_OPTIONAL,
138 "run without authentication");
140 cmd_AddParm(cptr, "-smallht", CMD_FLAG, CMD_OPTIONAL,
141 "use small hash tables");
143 cmd_AddParm(cptr, "-servers", CMD_LIST, CMD_OPTIONAL,
144 "list of ubik database servers");
146 cmd_AddParm(cptr, "-ubikbuffers", CMD_SINGLE, CMD_OPTIONAL,
147 "the number of ubik buffers");
149 cmd_AddParm(cptr, "-auditlog", CMD_SINGLE, CMD_OPTIONAL,
152 cmd_AddParm(cptr, "-p", CMD_SINGLE, CMD_OPTIONAL,
153 "number of processes");
155 cmd_AddParm(cptr, "-rxbind", CMD_FLAG, CMD_OPTIONAL,
156 "bind the Rx socket (primary interface only)");
158 cmd_AddParm(cptr, "-audit-interface", CMD_SINGLE, CMD_OPTIONAL,
159 "audit interface (file or sysvmq)");
163 argHandler(struct cmd_syndesc *as, void *arock)
166 /* globalConfPtr provides the handle for the configuration information */
168 /* database directory */
169 if (as->parms[0].items != 0) {
170 globalConfPtr->databaseDirectory =
171 (char *)malloc(strlen(as->parms[0].items->data) + 1);
172 if (globalConfPtr->databaseDirectory == 0)
174 strcpy(globalConfPtr->databaseDirectory, as->parms[0].items->data);
177 /* -cellservdb, cell configuration directory */
178 if (as->parms[1].items != 0) {
179 globalConfPtr->cellConfigdir =
180 (char *)malloc(strlen(as->parms[1].items->data) + 1);
181 if (globalConfPtr->cellConfigdir == 0)
184 strcpy(globalConfPtr->cellConfigdir, as->parms[1].items->data);
186 globalConfPtr->debugFlags |= DF_RECHECKNOAUTH;
189 /* truncate the database */
190 if (as->parms[2].items != 0)
193 /* run without authentication */
194 if (as->parms[3].items != 0)
195 globalConfPtr->debugFlags |= DF_NOAUTH;
197 /* use small hash tables */
198 if (as->parms[4].items != 0)
199 globalConfPtr->debugFlags |= DF_SMALLHT;
201 /* user provided list of ubik database servers */
202 if (as->parms[5].items != 0)
203 parseServerList(as->parms[5].items);
205 /* user provided the number of ubik buffers */
206 if (as->parms[6].items != 0)
207 ubik_nBuffers = atoi(as->parms[6].items->data);
211 /* param 7 (-auditlog) handled below */
213 /* user provided the number of threads */
214 if (as->parms[8].items != 0) {
215 lwps = atoi(as->parms[8].items->data);
217 printf ("Warning: '-p %d' is too big; using %d instead\n",
222 printf ("Warning: '-p %d' is too small; using %d instead\n",
228 /* user provided rxbind option */
229 if (as->parms[9].items != 0) {
233 /* -audit-interface */
234 if (as->parms[10].items != 0) {
235 char *interface = as->parms[10].items->data;
237 if (osi_audit_interface(interface)) {
238 printf("Invalid audit interface '%s'\n", interface);
244 /* needs to be after -audit-interface, so we osi_audit_interface
245 * before we osi_audit_file */
246 if (as->parms[7].items != 0) {
247 char *fileName = as->parms[7].items->data;
249 osi_audit_file(fileName);
258 parseServerList(struct cmd_item *itemPtr)
260 struct cmd_item *save;
263 afs_int32 nservers = 0;
268 /* compute number of servers in the list */
271 itemPtr = itemPtr->next;
274 LogDebug(3, "%d servers\n", nservers);
276 /* now can allocate the space for the server arguments */
277 serverArgs = (char **)malloc((nservers + 2) * sizeof(char *));
285 /* now go through and construct the list of servers */
288 *ptr++ = itemPtr->data;
289 itemPtr = itemPtr->next;
293 ubik_ParseServerList(nservers + 2, serverArgs, &globalConfPtr->myHost,
294 globalConfPtr->serverList);
298 /* free space for the server args */
299 free((char *)serverArgs);
306 * truncates just the database file.
310 truncateDatabase(void)
316 path = malloc(strlen(globalConfPtr->databaseDirectory) +
317 strlen(globalConfPtr->databaseName) +
318 strlen(globalConfPtr->databaseExtension) + 1);
322 /* construct the database name */
323 strcpy(path, globalConfPtr->databaseDirectory);
324 strcat(path, globalConfPtr->databaseName);
325 strcat(path, globalConfPtr->databaseExtension);
327 fd = open(path, O_RDWR, 0755);
331 if (ftruncate(fd, 0) != 0) {
346 #include "AFS_component_version_number.c"
349 main(int argc, char **argv)
351 char *whoami = argv[0];
353 struct afsconf_cell cellinfo;
356 afs_uint32 host = ntohl(INADDR_ANY);
358 char clones[MAXHOSTSPERCELL];
360 struct rx_service *tservice;
361 struct rx_securityClass **securityClasses;
362 afs_int32 numClasses;
364 extern int rx_stackSize;
367 /* initialize winsock */
368 if (afs_winsockInit() < 0) {
369 ReportErrorEventAlt(AFSEVT_SVR_WINSOCK_INIT_FAILED, 0, argv[0], 0);
370 fprintf(stderr, "%s: Couldn't initialize winsock.\n", whoami);
377 * The following signal action for AIX is necessary so that in case of a
378 * crash (i.e. core is generated) we can include the user's data section
379 * in the core dump. Unfortunately, by default, only a partial core is
380 * generated which, in many cases, isn't too useful.
382 struct sigaction nsa;
384 sigemptyset(&nsa.sa_mask);
385 nsa.sa_handler = SIG_DFL;
386 nsa.sa_flags = SA_FULLDUMP;
387 sigaction(SIGSEGV, &nsa, NULL);
388 sigaction(SIGABRT, &nsa, NULL);
391 osi_audit(BUDB_StartEvent, 0, AUD_END);
393 initialize_BUDB_error_table();
394 initializeArgHandler();
396 /* Initialize dirpaths */
397 if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
399 ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
401 afs_com_err(whoami, errno, "; Unable to obtain AFS server directory.");
405 memset(globalConfPtr, 0, sizeof(*globalConfPtr));
407 /* set default configuration values */
408 strcpy(dbDir, AFSDIR_SERVER_DB_DIRPATH);
410 globalConfPtr->databaseDirectory = dbDir;
411 globalConfPtr->databaseName = DEFAULT_DBPREFIX;
412 strcpy(cellConfDir, AFSDIR_SERVER_ETC_DIRPATH);
413 globalConfPtr->cellConfigdir = cellConfDir;
415 /* open the log file */
417 globalConfPtr->log = fopen(DEFAULT_LOGNAME,"a");
418 if ( globalConfPtr->log == NULL )
420 printf("Can't open log file %s - aborting\n", DEFAULT_LOGNAME);
427 #ifdef AFS_PTHREAD_ENV
428 SetLogThreadNumProgram( rx_GetThreadNum );
431 /* process the user supplied args */
433 code = cmd_Dispatch(argc, argv);
437 /* exit if there was a help option */
441 /* open the log file */
442 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
443 if (globalConfPtr->log == NULL) {
444 printf("Can't open log file %s - aborting\n",
445 AFSDIR_SERVER_BUDBLOG_FILEPATH);
449 /* keep log closed so can remove it */
451 fclose(globalConfPtr->log);
453 /* open the cell's configuration directory */
454 LogDebug(4, "opening %s\n", globalConfPtr->cellConfigdir);
456 BU_conf = afsconf_Open(globalConfPtr->cellConfigdir);
458 LogError(code, "Failed getting cell info\n");
459 afs_com_err(whoami, code, "Failed getting cell info");
463 code = afsconf_GetLocalCell(BU_conf, lcell, sizeof(lcell));
465 LogError(0, "** Can't determine local cell name!\n");
469 if (globalConfPtr->myHost == 0) {
470 /* if user hasn't supplied a list of servers, extract server
471 * list from the cell's database
474 LogDebug(1, "Using server list from %s cell database.\n", lcell);
476 code = afsconf_GetExtendedCellInfo (BU_conf, lcell, 0, &cellinfo,
479 convert_cell_to_ubik(&cellinfo, &globalConfPtr->myHost,
480 globalConfPtr->serverList);
485 /* initialize ubik */
486 ubik_SetClientSecurityProcs(afsconf_ClientAuth, afsconf_UpToDate, BU_conf);
487 ubik_SetServerSecurityProcs(afsconf_BuildServerSecurityObjects,
488 afsconf_CheckAuth, BU_conf);
490 if (ubik_nBuffers == 0)
493 LogError(0, "Will allocate %d ubik buffers\n", ubik_nBuffers);
496 (char *)malloc(strlen(globalConfPtr->databaseDirectory) +
497 strlen(globalConfPtr->databaseName) + 1);
501 /* construct the database name */
502 strcpy(dbNamePtr, globalConfPtr->databaseDirectory);
503 strcat(dbNamePtr, globalConfPtr->databaseName); /* name prefix */
505 rx_SetRxDeadTime(60); /* 60 seconds inactive before timeout */
509 if (AFSDIR_SERVER_NETRESTRICT_FILEPATH ||
510 AFSDIR_SERVER_NETINFO_FILEPATH) {
512 ccode = parseNetFiles(SHostAddrs, NULL, NULL,
513 ADDRSPERSITE, reason,
514 AFSDIR_SERVER_NETINFO_FILEPATH,
515 AFSDIR_SERVER_NETRESTRICT_FILEPATH);
518 ccode = rx_getAllAddr(SHostAddrs, ADDRSPERSITE);
521 host = SHostAddrs[0];
522 rx_InitHost(host, htons(AFSCONF_BUDBPORT));
526 code = ubik_ServerInitByInfo (globalConfPtr->myHost,
527 htons(AFSCONF_BUDBPORT),
530 dbNamePtr, /* name prefix */
534 LogError(code, "Ubik init failed\n");
535 afs_com_err(whoami, code, "Ubik init failed");
539 afsconf_BuildServerSecurityObjects(BU_conf, &securityClasses, &numClasses);
541 /* Disable jumbograms */
545 rx_NewServiceHost(host, 0, BUDB_SERVICE, "BackupDatabase",
546 securityClasses, numClasses, BUDB_ExecuteRequest);
548 if (tservice == (struct rx_service *)0) {
549 LogError(0, "Could not create backup database rx service\n");
550 printf("Could not create backup database rx service\n");
553 rx_SetMinProcs(tservice, 1);
554 rx_SetMaxProcs(tservice, lwps);
555 rx_SetStackSize(tservice, 10000);
557 /* allow super users to manage RX statistics */
558 rx_SetRxStatUserOk(BU_rxstat_userok);
560 /* misc. initialization */
562 /* database dump synchronization */
563 memset(dumpSyncPtr, 0, sizeof(*dumpSyncPtr));
564 Lock_Init(&dumpSyncPtr->ds_lock);
566 rx_StartServer(0); /* start handling requests */
573 currentTime = time(0);
574 LogError(0, "Ready to process requests at %s\n", ctime(¤tTime));
576 rx_ServerProc(NULL); /* donate this LWP */
579 osi_audit(BUDB_FinishEvent, code, AUD_END);
584 consistencyCheckDb(void)
586 /* do consistency checks on structure sizes */
587 if ((sizeof(struct htBlock) > BLOCKSIZE)
588 || (sizeof(struct vfBlock) > BLOCKSIZE)
589 || (sizeof(struct viBlock) > BLOCKSIZE)
590 || (sizeof(struct dBlock) > BLOCKSIZE)
591 || (sizeof(struct tBlock) > BLOCKSIZE)
593 fprintf(stderr, "Block layout error!\n");
599 LogDebug(int level, char *fmt, ... )
605 if (debugging >= level) {
606 /* log normally closed so can remove it */
607 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
608 if (globalConfPtr->log != NULL) {
609 vfprintf(globalConfPtr->log, fmt, ap);
610 fflush(globalConfPtr->log);
611 fclose(globalConfPtr->log);
621 static char timestamp[20];
624 strftime(timestamp, 20, "%m/%d/%Y %H:%M:%S", lt);
635 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
636 if (globalConfPtr->log != NULL) {
638 fprintf(globalConfPtr->log, "%s ", TimeStamp(now));
640 vfprintf(globalConfPtr->log, fmt, ap);
641 fflush(globalConfPtr->log);
642 fclose(globalConfPtr->log);
648 LogError(long code, char *fmt, ... )
654 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
656 if (globalConfPtr->log != NULL) {
658 fprintf(globalConfPtr->log, "%s ", TimeStamp(now));
661 fprintf(globalConfPtr->log, "%s: %s\n", afs_error_table_name(code),
662 afs_error_message(code));
663 vfprintf(globalConfPtr->log, fmt, ap );
664 fflush(globalConfPtr->log);
665 fclose(globalConfPtr->log);
676 LogNetDump(struct dump *dumpPtr)
678 struct dump hostDump;
679 extern buServerConfP globalConfPtr;
681 dump_ntoh(dumpPtr, &hostDump);
683 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
684 if (globalConfPtr->log != NULL) {
685 printDump(globalConfPtr->log, &hostDump);
686 fclose(globalConfPtr->log);