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);
398 osi_audit(BUDB_StartEvent, 0, AUD_END);
400 initialize_BUDB_error_table();
401 initializeArgHandler();
403 /* Initialize dirpaths */
404 if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
406 ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
408 com_err(whoami, errno, "; Unable to obtain AFS server directory.");
412 memset(globalConfPtr, 0, sizeof(*globalConfPtr));
414 /* set default configuration values */
415 strcpy(dbDir, AFSDIR_SERVER_DB_DIRPATH);
417 globalConfPtr->databaseDirectory = dbDir;
418 globalConfPtr->databaseName = DEFAULT_DBPREFIX;
419 strcpy(cellConfDir, AFSDIR_SERVER_ETC_DIRPATH);
420 globalConfPtr->cellConfigdir = cellConfDir;
422 /* open the log file */
424 globalConfPtr->log = fopen(DEFAULT_LOGNAME,"a");
425 if ( globalConfPtr->log == NULL )
427 printf("Can't open log file %s - aborting\n", DEFAULT_LOGNAME);
434 #ifdef AFS_PTHREAD_ENV
435 SetLogThreadNumProgram( threadNum );
438 /* process the user supplied args */
440 code = cmd_Dispatch(argc, argv);
444 /* exit if there was a help option */
448 /* open the log file */
449 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
450 if (globalConfPtr->log == NULL) {
451 printf("Can't open log file %s - aborting\n",
452 AFSDIR_SERVER_BUDBLOG_FILEPATH);
456 /* keep log closed so can remove it */
458 fclose(globalConfPtr->log);
460 /* open the cell's configuration directory */
461 LogDebug(4, "opening %s\n", globalConfPtr->cellConfigdir);
463 BU_conf = afsconf_Open(globalConfPtr->cellConfigdir);
465 LogError(code, "Failed getting cell info\n");
466 com_err(whoami, code, "Failed getting cell info");
470 code = afsconf_GetLocalCell(BU_conf, lcell, sizeof(lcell));
472 LogError(0, "** Can't determine local cell name!\n");
476 if (globalConfPtr->myHost == 0) {
477 /* if user hasn't supplied a list of servers, extract server
478 * list from the cell's database
481 LogDebug(1, "Using server list from %s cell database.\n", lcell);
483 code = afsconf_GetExtendedCellInfo (BU_conf, lcell, 0, &cellinfo,
486 convert_cell_to_ubik(&cellinfo, &globalConfPtr->myHost,
487 globalConfPtr->serverList);
492 /* initialize ubik */
493 ubik_CRXSecurityProc = afsconf_ClientAuth;
494 ubik_CRXSecurityRock = (char *)BU_conf;
496 ubik_SRXSecurityProc = afsconf_ServerAuth;
497 ubik_SRXSecurityRock = (char *)BU_conf;
499 ubik_CheckRXSecurityProc = afsconf_CheckAuth;
500 ubik_CheckRXSecurityRock = (char *)BU_conf;
502 if (ubik_nBuffers == 0)
505 LogError(0, "Will allocate %d ubik buffers\n", ubik_nBuffers);
508 (char *)malloc(strlen(globalConfPtr->databaseDirectory) +
509 strlen(globalConfPtr->databaseName) + 1);
513 /* construct the database name */
514 strcpy(dbNamePtr, globalConfPtr->databaseDirectory);
515 strcat(dbNamePtr, globalConfPtr->databaseName); /* name prefix */
517 rx_SetRxDeadTime(60); /* 60 seconds inactive before timeout */
519 code = ubik_ServerInitByInfo (globalConfPtr->myHost,
520 htons(AFSCONF_BUDBPORT),
523 dbNamePtr, /* name prefix */
527 LogError(code, "Ubik init failed\n");
528 com_err(whoami, code, "Ubik init failed");
532 sca[RX_SCINDEX_NULL] = rxnull_NewServerSecurityObject();
533 sca[RX_SCINDEX_VAB] = 0;
534 sca[RX_SCINDEX_KAD] =
535 rxkad_NewServerSecurityObject(rxkad_clear, BU_conf, afsconf_GetKey,
538 /* Disable jumbograms */
542 rx_NewService(0, BUDB_SERVICE, "BackupDatabase", sca, 3,
543 BUDB_ExecuteRequest);
544 if (tservice == (struct rx_service *)0) {
545 LogError(0, "Could not create backup database rx service\n");
546 printf("Could not create backup database rx service\n");
549 rx_SetMinProcs(tservice, 1);
550 rx_SetMaxProcs(tservice, 3);
551 rx_SetStackSize(tservice, 10000);
553 /* allow super users to manage RX statistics */
554 rx_SetRxStatUserOk(BU_rxstat_userok);
556 /* misc. initialization */
558 /* database dump synchronization */
559 memset(dumpSyncPtr, 0, sizeof(*dumpSyncPtr));
560 Lock_Init(&dumpSyncPtr->ds_lock);
562 rx_StartServer(0); /* start handling requests */
569 currentTime = time(0);
570 LogError(0, "Ready to process requests at %s\n", ctime(¤tTime));
572 rx_ServerProc(); /* donate this LWP */
575 osi_audit(BUDB_FinishEvent, code, AUD_END);
582 /* do consistency checks on structure sizes */
583 if ((sizeof(struct htBlock) > BLOCKSIZE)
584 || (sizeof(struct vfBlock) > BLOCKSIZE)
585 || (sizeof(struct viBlock) > BLOCKSIZE)
586 || (sizeof(struct dBlock) > BLOCKSIZE)
587 || (sizeof(struct tBlock) > BLOCKSIZE)
589 fprintf(stderr, "Block layout error!\n");
595 LogDebug(level, a, b, c, d, e, f, g, h, i)
597 char *a, *b, *c, *d, *e, *f, *g, *h, *i;
600 if (debugging >= level) {
601 /* log normally closed so can remove it */
602 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
603 if (globalConfPtr->log != NULL) {
604 fprintf(globalConfPtr->log, a, b, c, d, e, f, g, h, i);
605 fflush(globalConfPtr->log);
606 fclose(globalConfPtr->log);
616 static char timestamp[20];
619 strftime(timestamp, 20, "%m/%d/%Y %T", lt);
624 Log(a, b, c, d, e, f, g, h, i)
625 char *a, *b, *c, *d, *e, *f, *g, *h, *i;
629 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
630 if (globalConfPtr->log != NULL) {
632 fprintf(globalConfPtr->log, "%s ", TimeStamp(now));
634 fprintf(globalConfPtr->log, a, b, c, d, e, f, g, h, i);
635 fflush(globalConfPtr->log);
636 fclose(globalConfPtr->log);
642 LogError(code, a, b, c, d, e, f, g, h, i)
644 char *a, *b, *c, *d, *e, *f, *g, *h, *i;
648 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
650 if (globalConfPtr->log != NULL) {
652 fprintf(globalConfPtr->log, "%s ", TimeStamp(now));
655 fprintf(globalConfPtr->log, "%s: %s\n", error_table_name(code),
656 error_message(code));
657 fprintf(globalConfPtr->log, a, b, c, d, e, f, g, h, i);
658 fflush(globalConfPtr->log);
659 fclose(globalConfPtr->log);
672 struct dump *dumpPtr;
674 struct dump hostDump;
675 extern buServerConfP globalConfPtr;
677 dump_ntoh(dumpPtr, &hostDump);
679 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
680 if (globalConfPtr->log != NULL) {
681 printDump(globalConfPtr->log, &hostDump);
682 fclose(globalConfPtr->log);