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>
20 #include <WINNT/afsevent.h>
22 #include <netinet/in.h>
37 #include <sys/types.h>
46 #include <rx/rx_globals.h>
47 #include <afs/cellconfig.h>
49 #include <afs/bubasics.h>
50 #include <afs/afsutil.h>
51 #include <afs/com_err.h>
56 #include "budb_errs.h"
58 #include "error_macros.h"
60 #include "afs/audit.h"
62 struct ubik_dbase *BU_dbase;
63 struct afsconf_dir *BU_conf; /* for getting cell info */
65 char lcell[MAXKTCREALMLEN];
69 /* server's global configuration information. This is exported to other
73 buServerConfT globalConf;
74 buServerConfP globalConfPtr = &globalConf;
75 char dbDir[AFSDIR_PATH_MAX], cellConfDir[AFSDIR_PATH_MAX];
76 /* debugging control */
79 #if defined(AFS_PTHREAD_ENV)
83 return pthread_getspecific(rx_thread_id_key);
87 /* check whether caller is authorized to manage RX statistics */
89 BU_rxstat_userok(call)
92 return afsconf_SuperUser(BU_conf, call, NULL);
96 convert_cell_to_ubik(cellinfo, myHost, serverList)
97 struct afsconf_cell *cellinfo;
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.
130 register struct cmd_syndesc *as;
136 /* initializeCommands
137 * initialize all the supported commands and their arguments
140 initializeArgHandler()
142 struct cmd_syndesc *cptr;
146 cmd_SetBeforeProc(MyBeforeProc, NULL);
148 cptr = cmd_CreateSyntax(NULL, argHandler, NULL, "Backup database server");
150 cmd_AddParm(cptr, "-database", CMD_SINGLE, CMD_OPTIONAL,
151 "database directory");
153 cmd_AddParm(cptr, "-cellservdb", CMD_SINGLE, CMD_OPTIONAL,
154 "cell configuration directory");
156 cmd_AddParm(cptr, "-resetdb", CMD_FLAG, CMD_OPTIONAL,
157 "truncate the database");
159 cmd_AddParm(cptr, "-noauth", CMD_FLAG, CMD_OPTIONAL,
160 "run without authentication");
162 cmd_AddParm(cptr, "-smallht", CMD_FLAG, CMD_OPTIONAL,
163 "use small hash tables");
165 cmd_AddParm(cptr, "-servers", CMD_LIST, CMD_OPTIONAL,
166 "list of ubik database servers");
168 cmd_AddParm(cptr, "-ubikbuffers", CMD_SINGLE, CMD_OPTIONAL,
169 "the number of ubik buffers");
171 cmd_AddParm(cptr, "-auditlog", CMD_SINGLE, CMD_OPTIONAL,
177 argHandler(as, arock)
178 struct cmd_syndesc *as;
182 /* globalConfPtr provides the handle for the configuration information */
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)
190 strcpy(globalConfPtr->databaseDirectory, as->parms[0].items->data);
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)
200 strcpy(globalConfPtr->cellConfigdir, as->parms[1].items->data);
202 globalConfPtr->debugFlags |= DF_RECHECKNOAUTH;
205 /* truncate the database */
206 if (as->parms[2].items != 0)
209 /* run without authentication */
210 if (as->parms[3].items != 0)
211 globalConfPtr->debugFlags |= DF_NOAUTH;
213 /* use small hash tables */
214 if (as->parms[4].items != 0)
215 globalConfPtr->debugFlags |= DF_SMALLHT;
217 /* user provided list of ubik database servers */
218 if (as->parms[5].items != 0)
219 parseServerList(as->parms[5].items);
221 /* user provided the number of ubik buffers */
222 if (as->parms[6].items != 0)
223 ubik_nBuffers = atoi(as->parms[6].items->data);
227 if (as->parms[7].items != 0) {
230 char oldName[MAXPATHLEN];
231 char *fileName = as->parms[7].items->data;
235 if ((lstat(fileName, &statbuf) == 0)
236 && (S_ISFIFO(statbuf.st_mode))) {
237 flags = O_WRONLY | O_NONBLOCK;
241 strcpy(oldName, fileName);
242 strcat(oldName, ".old");
243 renamefile(fileName, oldName);
244 flags = O_WRONLY | O_TRUNC | O_CREAT;
246 tempfd = open(fileName, flags, 0666);
248 auditout = fdopen(tempfd, "a");
250 osi_audit_file(auditout);
252 printf("Warning: auditlog %s not writable, ignored.\n", fileName);
254 printf("Warning: auditlog %s not writable, ignored.\n", fileName);
262 parseServerList(itemPtr)
263 struct cmd_item *itemPtr;
265 struct cmd_item *save;
268 afs_int32 nservers = 0;
273 /* compute number of servers in the list */
276 itemPtr = itemPtr->next;
279 LogDebug(3, "%d servers\n", nservers);
281 /* now can allocate the space for the server arguments */
282 serverArgs = (char **)malloc((nservers + 2) * sizeof(char *));
290 /* now go through and construct the list of servers */
293 *ptr++ = itemPtr->data;
294 itemPtr = itemPtr->next;
298 ubik_ParseServerList(nservers + 2, serverArgs, &globalConfPtr->myHost,
299 globalConfPtr->serverList);
303 /* free space for the server args */
304 free((char *)serverArgs);
311 * truncates just the database file.
321 (char *)malloc(strlen(globalConfPtr->databaseDirectory) +
322 strlen(globalConfPtr->databaseName) +
323 strlen(globalConfPtr->databaseExtension) + 1);
327 /* construct the database name */
328 strcpy(path, globalConfPtr->databaseDirectory);
329 strcat(path, globalConfPtr->databaseName);
330 strcat(path, globalConfPtr->databaseExtension);
332 fd = open(path, O_RDWR, 0755);
336 if (ftruncate(fd, 0) != 0) {
349 #include "AFS_component_version_number.c"
355 char *whoami = argv[0];
357 struct afsconf_cell cellinfo;
361 char clones[MAXHOSTSPERCELL];
365 struct rx_service *tservice;
366 struct rx_securityClass *sca[3];
368 extern int afsconf_ServerAuth();
369 extern int afsconf_CheckAuth();
371 extern int rx_stackSize;
372 extern int BUDB_ExecuteRequest();
375 /* initialize winsock */
376 if (afs_winsockInit() < 0) {
377 ReportErrorEventAlt(AFSEVT_SVR_WINSOCK_INIT_FAILED, 0, argv[0], 0);
378 fprintf(stderr, "%s: Couldn't initialize winsock.\n", whoami);
385 * The following signal action for AIX is necessary so that in case of a
386 * crash (i.e. core is generated) we can include the user's data section
387 * in the core dump. Unfortunately, by default, only a partial core is
388 * generated which, in many cases, isn't too useful.
390 struct sigaction nsa;
392 sigemptyset(&nsa.sa_mask);
393 nsa.sa_handler = SIG_DFL;
394 nsa.sa_flags = SA_FULLDUMP;
395 sigaction(SIGSEGV, &nsa, NULL);
396 sigaction(SIGABRT, &nsa, NULL);
399 osi_audit(BUDB_StartEvent, 0, AUD_END);
401 initialize_BUDB_error_table();
402 initializeArgHandler();
404 /* Initialize dirpaths */
405 if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
407 ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
409 com_err(whoami, errno, "; Unable to obtain AFS server directory.");
413 memset(globalConfPtr, 0, sizeof(*globalConfPtr));
415 /* set default configuration values */
416 strcpy(dbDir, AFSDIR_SERVER_DB_DIRPATH);
418 globalConfPtr->databaseDirectory = dbDir;
419 globalConfPtr->databaseName = DEFAULT_DBPREFIX;
420 strcpy(cellConfDir, AFSDIR_SERVER_ETC_DIRPATH);
421 globalConfPtr->cellConfigdir = cellConfDir;
423 /* open the log file */
425 globalConfPtr->log = fopen(DEFAULT_LOGNAME,"a");
426 if ( globalConfPtr->log == NULL )
428 printf("Can't open log file %s - aborting\n", DEFAULT_LOGNAME);
435 #ifdef AFS_PTHREAD_ENV
436 SetLogThreadNumProgram( threadNum );
439 /* process the user supplied args */
441 code = cmd_Dispatch(argc, argv);
445 /* exit if there was a help option */
449 /* open the log file */
450 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
451 if (globalConfPtr->log == NULL) {
452 printf("Can't open log file %s - aborting\n",
453 AFSDIR_SERVER_BUDBLOG_FILEPATH);
457 /* keep log closed so can remove it */
459 fclose(globalConfPtr->log);
461 /* open the cell's configuration directory */
462 LogDebug(4, "opening %s\n", globalConfPtr->cellConfigdir);
464 BU_conf = afsconf_Open(globalConfPtr->cellConfigdir);
466 LogError(code, "Failed getting cell info\n");
467 com_err(whoami, code, "Failed getting cell info");
471 code = afsconf_GetLocalCell(BU_conf, lcell, sizeof(lcell));
473 LogError(0, "** Can't determine local cell name!\n");
477 if (globalConfPtr->myHost == 0) {
478 /* if user hasn't supplied a list of servers, extract server
479 * list from the cell's database
482 LogDebug(1, "Using server list from %s cell database.\n", lcell);
484 code = afsconf_GetExtendedCellInfo (BU_conf, lcell, 0, &cellinfo,
487 convert_cell_to_ubik(&cellinfo, &globalConfPtr->myHost,
488 globalConfPtr->serverList);
493 /* initialize ubik */
494 ubik_CRXSecurityProc = afsconf_ClientAuth;
495 ubik_CRXSecurityRock = (char *)BU_conf;
497 ubik_SRXSecurityProc = afsconf_ServerAuth;
498 ubik_SRXSecurityRock = (char *)BU_conf;
500 ubik_CheckRXSecurityProc = afsconf_CheckAuth;
501 ubik_CheckRXSecurityRock = (char *)BU_conf;
503 if (ubik_nBuffers == 0)
506 LogError(0, "Will allocate %d ubik buffers\n", ubik_nBuffers);
509 (char *)malloc(strlen(globalConfPtr->databaseDirectory) +
510 strlen(globalConfPtr->databaseName) + 1);
514 /* construct the database name */
515 strcpy(dbNamePtr, globalConfPtr->databaseDirectory);
516 strcat(dbNamePtr, globalConfPtr->databaseName); /* name prefix */
518 rx_SetRxDeadTime(60); /* 60 seconds inactive before timeout */
520 code = ubik_ServerInitByInfo (globalConfPtr->myHost,
521 htons(AFSCONF_BUDBPORT),
524 dbNamePtr, /* name prefix */
528 LogError(code, "Ubik init failed\n");
529 com_err(whoami, code, "Ubik init failed");
533 sca[RX_SCINDEX_NULL] = rxnull_NewServerSecurityObject();
534 sca[RX_SCINDEX_VAB] = 0;
535 sca[RX_SCINDEX_KAD] =
536 rxkad_NewServerSecurityObject(rxkad_clear, BU_conf, afsconf_GetKey,
539 /* Disable jumbograms */
543 rx_NewService(0, BUDB_SERVICE, "BackupDatabase", sca, 3,
544 BUDB_ExecuteRequest);
545 if (tservice == (struct rx_service *)0) {
546 LogError(0, "Could not create backup database rx service\n");
547 printf("Could not create backup database rx service\n");
550 rx_SetMinProcs(tservice, 1);
551 rx_SetMaxProcs(tservice, 3);
552 rx_SetStackSize(tservice, 10000);
554 /* allow super users to manage RX statistics */
555 rx_SetRxStatUserOk(BU_rxstat_userok);
557 /* misc. initialization */
559 /* database dump synchronization */
560 memset(dumpSyncPtr, 0, sizeof(*dumpSyncPtr));
561 Lock_Init(&dumpSyncPtr->ds_lock);
563 rx_StartServer(0); /* start handling requests */
570 currentTime = time(0);
571 LogError(0, "Ready to process requests at %s\n", ctime(¤tTime));
573 rx_ServerProc(); /* donate this LWP */
576 osi_audit(BUDB_FinishEvent, code, AUD_END);
583 /* do consistency checks on structure sizes */
584 if ((sizeof(struct htBlock) > BLOCKSIZE)
585 || (sizeof(struct vfBlock) > BLOCKSIZE)
586 || (sizeof(struct viBlock) > BLOCKSIZE)
587 || (sizeof(struct dBlock) > BLOCKSIZE)
588 || (sizeof(struct tBlock) > BLOCKSIZE)
590 fprintf(stderr, "Block layout error!\n");
596 LogDebug(level, a, b, c, d, e, f, g, h, i)
598 char *a, *b, *c, *d, *e, *f, *g, *h, *i;
601 if (debugging >= level) {
602 /* log normally closed so can remove it */
603 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
604 if (globalConfPtr->log != NULL) {
605 fprintf(globalConfPtr->log, a, b, c, d, e, f, g, h, i);
606 fflush(globalConfPtr->log);
607 fclose(globalConfPtr->log);
617 static char timestamp[20];
620 strftime(timestamp, 20, "%m/%d/%Y %T", lt);
625 Log(a, b, c, d, e, f, g, h, i)
626 char *a, *b, *c, *d, *e, *f, *g, *h, *i;
630 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
631 if (globalConfPtr->log != NULL) {
633 fprintf(globalConfPtr->log, "%s ", TimeStamp(now));
635 fprintf(globalConfPtr->log, a, b, c, d, e, f, g, h, i);
636 fflush(globalConfPtr->log);
637 fclose(globalConfPtr->log);
643 LogError(code, a, b, c, d, e, f, g, h, i)
645 char *a, *b, *c, *d, *e, *f, *g, *h, *i;
649 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
651 if (globalConfPtr->log != NULL) {
653 fprintf(globalConfPtr->log, "%s ", TimeStamp(now));
656 fprintf(globalConfPtr->log, "%s: %s\n", error_table_name(code),
657 error_message(code));
658 fprintf(globalConfPtr->log, a, b, c, d, e, f, g, h, i);
659 fflush(globalConfPtr->log);
660 fclose(globalConfPtr->log);
673 struct dump *dumpPtr;
675 struct dump hostDump;
676 extern buServerConfP globalConfPtr;
678 dump_ntoh(dumpPtr, &hostDump);
680 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
681 if (globalConfPtr->log != NULL) {
682 printDump(globalConfPtr->log, &hostDump);
683 fclose(globalConfPtr->log);