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>
29 #include <sys/types.h>
38 #include <rx/rx_globals.h>
39 #include <afs/cellconfig.h>
40 #include <afs/bubasics.h>
41 #include <afs/afsutil.h>
42 #include <afs/com_err.h>
47 #include "budb_errs.h"
49 #include "error_macros.h"
51 #include "afs/audit.h"
53 struct ubik_dbase *BU_dbase;
54 struct afsconf_dir *BU_conf; /* for getting cell info */
56 int argHandler(struct cmd_syndesc *, void *);
58 char lcell[MAXKTCREALMLEN];
62 /* server's global configuration information. This is exported to other
66 buServerConfT globalConf;
67 buServerConfP globalConfPtr = &globalConf;
68 char dbDir[AFSDIR_PATH_MAX], cellConfDir[AFSDIR_PATH_MAX];
69 /* debugging control */
74 #define ADDRSPERSITE 16 /* Same global is in rx/rx_user.c */
75 afs_uint32 SHostAddrs[ADDRSPERSITE];
77 #if defined(AFS_PTHREAD_ENV)
81 return pthread_getspecific(rx_thread_id_key);
85 /* check whether caller is authorized to manage RX statistics */
87 BU_rxstat_userok(call)
90 return afsconf_SuperUser(BU_conf, call, NULL);
94 convert_cell_to_ubik(cellinfo, myHost, serverList)
95 struct afsconf_cell *cellinfo;
97 afs_int32 *serverList;
104 gethostname(hostname, sizeof(hostname));
105 th = gethostbyname(hostname);
107 printf("prserver: couldn't get address of this host.\n");
110 memcpy(myHost, th->h_addr, sizeof(afs_int32));
112 for (i = 0; i < cellinfo->numServers; i++)
113 /* omit my host from serverList */
114 if (cellinfo->hostAddr[i].sin_addr.s_addr != *myHost)
115 *serverList++ = cellinfo->hostAddr[i].sin_addr.s_addr;
117 *serverList = 0; /* terminate list */
122 * The whole purpose of MyBeforeProc is to detect
123 * if the -help option was not within the command line.
124 * If it were, this routine would never have been called.
127 MyBeforeProc(register struct cmd_syndesc *as, void *arock)
133 /* initializeCommands
134 * initialize all the supported commands and their arguments
138 initializeArgHandler()
140 struct cmd_syndesc *cptr;
142 cmd_SetBeforeProc(MyBeforeProc, NULL);
144 cptr = cmd_CreateSyntax(NULL, argHandler, NULL, "Backup database server");
146 cmd_AddParm(cptr, "-database", CMD_SINGLE, CMD_OPTIONAL,
147 "database directory");
149 cmd_AddParm(cptr, "-cellservdb", CMD_SINGLE, CMD_OPTIONAL,
150 "cell configuration directory");
152 cmd_AddParm(cptr, "-resetdb", CMD_FLAG, CMD_OPTIONAL,
153 "truncate the database");
155 cmd_AddParm(cptr, "-noauth", CMD_FLAG, CMD_OPTIONAL,
156 "run without authentication");
158 cmd_AddParm(cptr, "-smallht", CMD_FLAG, CMD_OPTIONAL,
159 "use small hash tables");
161 cmd_AddParm(cptr, "-servers", CMD_LIST, CMD_OPTIONAL,
162 "list of ubik database servers");
164 cmd_AddParm(cptr, "-ubikbuffers", CMD_SINGLE, CMD_OPTIONAL,
165 "the number of ubik buffers");
167 cmd_AddParm(cptr, "-auditlog", CMD_SINGLE, CMD_OPTIONAL,
173 argHandler(struct cmd_syndesc *as, void *arock)
176 /* globalConfPtr provides the handle for the configuration information */
178 /* database directory */
179 if (as->parms[0].items != 0) {
180 globalConfPtr->databaseDirectory =
181 (char *)malloc(strlen(as->parms[0].items->data) + 1);
182 if (globalConfPtr->databaseDirectory == 0)
184 strcpy(globalConfPtr->databaseDirectory, as->parms[0].items->data);
187 /* -cellservdb, cell configuration directory */
188 if (as->parms[1].items != 0) {
189 globalConfPtr->cellConfigdir =
190 (char *)malloc(strlen(as->parms[1].items->data) + 1);
191 if (globalConfPtr->cellConfigdir == 0)
194 strcpy(globalConfPtr->cellConfigdir, as->parms[1].items->data);
196 globalConfPtr->debugFlags |= DF_RECHECKNOAUTH;
199 /* truncate the database */
200 if (as->parms[2].items != 0)
203 /* run without authentication */
204 if (as->parms[3].items != 0)
205 globalConfPtr->debugFlags |= DF_NOAUTH;
207 /* use small hash tables */
208 if (as->parms[4].items != 0)
209 globalConfPtr->debugFlags |= DF_SMALLHT;
211 /* user provided list of ubik database servers */
212 if (as->parms[5].items != 0)
213 parseServerList(as->parms[5].items);
215 /* user provided the number of ubik buffers */
216 if (as->parms[6].items != 0)
217 ubik_nBuffers = atoi(as->parms[6].items->data);
221 if (as->parms[7].items != 0) {
224 char oldName[MAXPATHLEN];
225 char *fileName = as->parms[7].items->data;
229 if ((lstat(fileName, &statbuf) == 0)
230 && (S_ISFIFO(statbuf.st_mode))) {
231 flags = O_WRONLY | O_NONBLOCK;
235 strcpy(oldName, fileName);
236 strcat(oldName, ".old");
237 renamefile(fileName, oldName);
238 flags = O_WRONLY | O_TRUNC | O_CREAT;
240 tempfd = open(fileName, flags, 0666);
242 auditout = fdopen(tempfd, "a");
244 osi_audit_file(auditout);
246 printf("Warning: auditlog %s not writable, ignored.\n", fileName);
248 printf("Warning: auditlog %s not writable, ignored.\n", fileName);
256 parseServerList(itemPtr)
257 struct cmd_item *itemPtr;
259 struct cmd_item *save;
262 afs_int32 nservers = 0;
267 /* compute number of servers in the list */
270 itemPtr = itemPtr->next;
273 LogDebug(3, "%d servers\n", nservers);
275 /* now can allocate the space for the server arguments */
276 serverArgs = (char **)malloc((nservers + 2) * sizeof(char *));
284 /* now go through and construct the list of servers */
287 *ptr++ = itemPtr->data;
288 itemPtr = itemPtr->next;
292 ubik_ParseServerList(nservers + 2, serverArgs, &globalConfPtr->myHost,
293 globalConfPtr->serverList);
297 /* free space for the server args */
298 free((char *)serverArgs);
305 * truncates just the database file.
315 (char *)malloc(strlen(globalConfPtr->databaseDirectory) +
316 strlen(globalConfPtr->databaseName) +
317 strlen(globalConfPtr->databaseExtension) + 1);
321 /* construct the database name */
322 strcpy(path, globalConfPtr->databaseDirectory);
323 strcat(path, globalConfPtr->databaseName);
324 strcat(path, globalConfPtr->databaseExtension);
326 fd = open(path, O_RDWR, 0755);
330 if (ftruncate(fd, 0) != 0) {
343 #include "AFS_component_version_number.c"
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 *sca[3];
361 extern int afsconf_ServerAuth();
362 extern int afsconf_CheckAuth();
364 extern int rx_stackSize;
365 extern int BUDB_ExecuteRequest();
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 = (char *)BU_conf;
490 ubik_SRXSecurityProc = afsconf_ServerAuth;
491 ubik_SRXSecurityRock = (char *)BU_conf;
493 ubik_CheckRXSecurityProc = afsconf_CheckAuth;
494 ubik_CheckRXSecurityRock = (char *)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 */
516 if (AFSDIR_SERVER_NETRESTRICT_FILEPATH ||
517 AFSDIR_SERVER_NETINFO_FILEPATH) {
519 ccode = parseNetFiles(SHostAddrs, NULL, NULL,
520 ADDRSPERSITE, reason,
521 AFSDIR_SERVER_NETINFO_FILEPATH,
522 AFSDIR_SERVER_NETRESTRICT_FILEPATH);
526 ccode = rx_getAllAddr(SHostAddrs, ADDRSPERSITE);
529 host = SHostAddrs[0];
530 rx_InitHost(host, htons(AFSCONF_BUDBPORT));
534 code = ubik_ServerInitByInfo (globalConfPtr->myHost,
535 htons(AFSCONF_BUDBPORT),
538 dbNamePtr, /* name prefix */
542 LogError(code, "Ubik init failed\n");
543 afs_com_err(whoami, code, "Ubik init failed");
547 sca[RX_SCINDEX_NULL] = rxnull_NewServerSecurityObject();
548 sca[RX_SCINDEX_VAB] = 0;
549 sca[RX_SCINDEX_KAD] =
550 rxkad_NewServerSecurityObject(rxkad_clear, BU_conf, afsconf_GetKey,
553 /* Disable jumbograms */
557 rx_NewServiceHost(host, 0, BUDB_SERVICE, "BackupDatabase", sca, 3,
558 BUDB_ExecuteRequest);
559 if (tservice == (struct rx_service *)0) {
560 LogError(0, "Could not create backup database rx service\n");
561 printf("Could not create backup database rx service\n");
564 rx_SetMinProcs(tservice, 1);
565 rx_SetMaxProcs(tservice, 3);
566 rx_SetStackSize(tservice, 10000);
568 /* allow super users to manage RX statistics */
569 rx_SetRxStatUserOk(BU_rxstat_userok);
571 /* misc. initialization */
573 /* database dump synchronization */
574 memset(dumpSyncPtr, 0, sizeof(*dumpSyncPtr));
575 Lock_Init(&dumpSyncPtr->ds_lock);
577 rx_StartServer(0); /* start handling requests */
584 currentTime = time(0);
585 LogError(0, "Ready to process requests at %s\n", ctime(¤tTime));
587 rx_ServerProc(); /* donate this LWP */
590 osi_audit(BUDB_FinishEvent, code, AUD_END);
597 /* do consistency checks on structure sizes */
598 if ((sizeof(struct htBlock) > BLOCKSIZE)
599 || (sizeof(struct vfBlock) > BLOCKSIZE)
600 || (sizeof(struct viBlock) > BLOCKSIZE)
601 || (sizeof(struct dBlock) > BLOCKSIZE)
602 || (sizeof(struct tBlock) > BLOCKSIZE)
604 fprintf(stderr, "Block layout error!\n");
610 LogDebug(level, a, b, c, d, e, f, g, h, i)
612 char *a, *b, *c, *d, *e, *f, *g, *h, *i;
615 if (debugging >= level) {
616 /* log normally closed so can remove it */
617 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
618 if (globalConfPtr->log != NULL) {
619 fprintf(globalConfPtr->log, a, b, c, d, e, f, g, h, i);
620 fflush(globalConfPtr->log);
621 fclose(globalConfPtr->log);
631 static char timestamp[20];
634 strftime(timestamp, 20, "%m/%d/%Y %T", lt);
639 Log(a, b, c, d, e, f, g, h, i)
640 char *a, *b, *c, *d, *e, *f, *g, *h, *i;
644 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
645 if (globalConfPtr->log != NULL) {
647 fprintf(globalConfPtr->log, "%s ", TimeStamp(now));
649 fprintf(globalConfPtr->log, a, b, c, d, e, f, g, h, i);
650 fflush(globalConfPtr->log);
651 fclose(globalConfPtr->log);
657 LogError(code, a, b, c, d, e, f, g, h, i)
659 char *a, *b, *c, *d, *e, *f, *g, *h, *i;
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 fprintf(globalConfPtr->log, a, b, c, d, e, f, g, h, i);
673 fflush(globalConfPtr->log);
674 fclose(globalConfPtr->log);
687 struct dump *dumpPtr;
689 struct dump hostDump;
690 extern buServerConfP globalConfPtr;
692 dump_ntoh(dumpPtr, &hostDump);
694 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
695 if (globalConfPtr->log != NULL) {
696 printDump(globalConfPtr->log, &hostDump);
697 fclose(globalConfPtr->log);