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>
48 #include <afs/bubasics.h>
49 #include <afs/afsutil.h>
50 #include <afs/com_err.h>
55 #include "budb_errs.h"
57 #include "error_macros.h"
59 #include "afs/audit.h"
61 struct ubik_dbase *BU_dbase;
62 struct afsconf_dir *BU_conf; /* for getting cell info */
64 char lcell[MAXKTCREALMLEN];
68 /* server's global configuration information. This is exported to other
72 buServerConfT globalConf;
73 buServerConfP globalConfPtr = &globalConf;
74 char dbDir[AFSDIR_PATH_MAX], cellConfDir[AFSDIR_PATH_MAX];
75 /* debugging control */
80 #define ADDRSPERSITE 16 /* Same global is in rx/rx_user.c */
81 afs_uint32 SHostAddrs[ADDRSPERSITE];
83 #if defined(AFS_PTHREAD_ENV)
87 return pthread_getspecific(rx_thread_id_key);
91 /* check whether caller is authorized to manage RX statistics */
93 BU_rxstat_userok(call)
96 return afsconf_SuperUser(BU_conf, call, NULL);
100 convert_cell_to_ubik(cellinfo, myHost, serverList)
101 struct afsconf_cell *cellinfo;
103 afs_int32 *serverList;
110 gethostname(hostname, sizeof(hostname));
111 th = gethostbyname(hostname);
113 printf("prserver: couldn't get address of this host.\n");
116 memcpy(myHost, th->h_addr, sizeof(afs_int32));
118 for (i = 0; i < cellinfo->numServers; i++)
119 /* omit my host from serverList */
120 if (cellinfo->hostAddr[i].sin_addr.s_addr != *myHost)
121 *serverList++ = cellinfo->hostAddr[i].sin_addr.s_addr;
123 *serverList = 0; /* terminate list */
128 * The whole purpose of MyBeforeProc is to detect
129 * if the -help option was not within the command line.
130 * If it were, this routine would never have been called.
134 register struct cmd_syndesc *as;
140 /* initializeCommands
141 * initialize all the supported commands and their arguments
144 initializeArgHandler()
146 struct cmd_syndesc *cptr;
150 cmd_SetBeforeProc(MyBeforeProc, NULL);
152 cptr = cmd_CreateSyntax(NULL, argHandler, NULL, "Backup database server");
154 cmd_AddParm(cptr, "-database", CMD_SINGLE, CMD_OPTIONAL,
155 "database directory");
157 cmd_AddParm(cptr, "-cellservdb", CMD_SINGLE, CMD_OPTIONAL,
158 "cell configuration directory");
160 cmd_AddParm(cptr, "-resetdb", CMD_FLAG, CMD_OPTIONAL,
161 "truncate the database");
163 cmd_AddParm(cptr, "-noauth", CMD_FLAG, CMD_OPTIONAL,
164 "run without authentication");
166 cmd_AddParm(cptr, "-smallht", CMD_FLAG, CMD_OPTIONAL,
167 "use small hash tables");
169 cmd_AddParm(cptr, "-servers", CMD_LIST, CMD_OPTIONAL,
170 "list of ubik database servers");
172 cmd_AddParm(cptr, "-ubikbuffers", CMD_SINGLE, CMD_OPTIONAL,
173 "the number of ubik buffers");
175 cmd_AddParm(cptr, "-auditlog", CMD_SINGLE, CMD_OPTIONAL,
181 argHandler(as, arock)
182 struct cmd_syndesc *as;
186 /* globalConfPtr provides the handle for the configuration information */
188 /* database directory */
189 if (as->parms[0].items != 0) {
190 globalConfPtr->databaseDirectory =
191 (char *)malloc(strlen(as->parms[0].items->data) + 1);
192 if (globalConfPtr->databaseDirectory == 0)
194 strcpy(globalConfPtr->databaseDirectory, as->parms[0].items->data);
197 /* -cellservdb, cell configuration directory */
198 if (as->parms[1].items != 0) {
199 globalConfPtr->cellConfigdir =
200 (char *)malloc(strlen(as->parms[1].items->data) + 1);
201 if (globalConfPtr->cellConfigdir == 0)
204 strcpy(globalConfPtr->cellConfigdir, as->parms[1].items->data);
206 globalConfPtr->debugFlags |= DF_RECHECKNOAUTH;
209 /* truncate the database */
210 if (as->parms[2].items != 0)
213 /* run without authentication */
214 if (as->parms[3].items != 0)
215 globalConfPtr->debugFlags |= DF_NOAUTH;
217 /* use small hash tables */
218 if (as->parms[4].items != 0)
219 globalConfPtr->debugFlags |= DF_SMALLHT;
221 /* user provided list of ubik database servers */
222 if (as->parms[5].items != 0)
223 parseServerList(as->parms[5].items);
225 /* user provided the number of ubik buffers */
226 if (as->parms[6].items != 0)
227 ubik_nBuffers = atoi(as->parms[6].items->data);
231 if (as->parms[7].items != 0) {
234 char oldName[MAXPATHLEN];
235 char *fileName = as->parms[7].items->data;
239 if ((lstat(fileName, &statbuf) == 0)
240 && (S_ISFIFO(statbuf.st_mode))) {
241 flags = O_WRONLY | O_NONBLOCK;
245 strcpy(oldName, fileName);
246 strcat(oldName, ".old");
247 renamefile(fileName, oldName);
248 flags = O_WRONLY | O_TRUNC | O_CREAT;
250 tempfd = open(fileName, flags, 0666);
252 auditout = fdopen(tempfd, "a");
254 osi_audit_file(auditout);
256 printf("Warning: auditlog %s not writable, ignored.\n", fileName);
258 printf("Warning: auditlog %s not writable, ignored.\n", fileName);
266 parseServerList(itemPtr)
267 struct cmd_item *itemPtr;
269 struct cmd_item *save;
272 afs_int32 nservers = 0;
277 /* compute number of servers in the list */
280 itemPtr = itemPtr->next;
283 LogDebug(3, "%d servers\n", nservers);
285 /* now can allocate the space for the server arguments */
286 serverArgs = (char **)malloc((nservers + 2) * sizeof(char *));
294 /* now go through and construct the list of servers */
297 *ptr++ = itemPtr->data;
298 itemPtr = itemPtr->next;
302 ubik_ParseServerList(nservers + 2, serverArgs, &globalConfPtr->myHost,
303 globalConfPtr->serverList);
307 /* free space for the server args */
308 free((char *)serverArgs);
315 * truncates just the database file.
325 (char *)malloc(strlen(globalConfPtr->databaseDirectory) +
326 strlen(globalConfPtr->databaseName) +
327 strlen(globalConfPtr->databaseExtension) + 1);
331 /* construct the database name */
332 strcpy(path, globalConfPtr->databaseDirectory);
333 strcat(path, globalConfPtr->databaseName);
334 strcat(path, globalConfPtr->databaseExtension);
336 fd = open(path, O_RDWR, 0755);
340 if (ftruncate(fd, 0) != 0) {
353 #include "AFS_component_version_number.c"
359 char *whoami = argv[0];
361 struct afsconf_cell cellinfo;
364 afs_uint32 host = ntohl(INADDR_ANY);
366 char clones[MAXHOSTSPERCELL];
368 struct rx_service *tservice;
369 struct rx_securityClass *sca[3];
371 extern int afsconf_ServerAuth();
372 extern int afsconf_CheckAuth();
374 extern int rx_stackSize;
375 extern int BUDB_ExecuteRequest();
378 /* initialize winsock */
379 if (afs_winsockInit() < 0) {
380 ReportErrorEventAlt(AFSEVT_SVR_WINSOCK_INIT_FAILED, 0, argv[0], 0);
381 fprintf(stderr, "%s: Couldn't initialize winsock.\n", whoami);
388 * The following signal action for AIX is necessary so that in case of a
389 * crash (i.e. core is generated) we can include the user's data section
390 * in the core dump. Unfortunately, by default, only a partial core is
391 * generated which, in many cases, isn't too useful.
393 struct sigaction nsa;
395 sigemptyset(&nsa.sa_mask);
396 nsa.sa_handler = SIG_DFL;
397 nsa.sa_flags = SA_FULLDUMP;
398 sigaction(SIGSEGV, &nsa, NULL);
399 sigaction(SIGABRT, &nsa, NULL);
402 osi_audit(BUDB_StartEvent, 0, AUD_END);
404 initialize_BUDB_error_table();
405 initializeArgHandler();
407 /* Initialize dirpaths */
408 if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
410 ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
412 afs_com_err(whoami, errno, "; Unable to obtain AFS server directory.");
416 memset(globalConfPtr, 0, sizeof(*globalConfPtr));
418 /* set default configuration values */
419 strcpy(dbDir, AFSDIR_SERVER_DB_DIRPATH);
421 globalConfPtr->databaseDirectory = dbDir;
422 globalConfPtr->databaseName = DEFAULT_DBPREFIX;
423 strcpy(cellConfDir, AFSDIR_SERVER_ETC_DIRPATH);
424 globalConfPtr->cellConfigdir = cellConfDir;
426 /* open the log file */
428 globalConfPtr->log = fopen(DEFAULT_LOGNAME,"a");
429 if ( globalConfPtr->log == NULL )
431 printf("Can't open log file %s - aborting\n", DEFAULT_LOGNAME);
438 #ifdef AFS_PTHREAD_ENV
439 SetLogThreadNumProgram( threadNum );
442 /* process the user supplied args */
444 code = cmd_Dispatch(argc, argv);
448 /* exit if there was a help option */
452 /* open the log file */
453 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
454 if (globalConfPtr->log == NULL) {
455 printf("Can't open log file %s - aborting\n",
456 AFSDIR_SERVER_BUDBLOG_FILEPATH);
460 /* keep log closed so can remove it */
462 fclose(globalConfPtr->log);
464 /* open the cell's configuration directory */
465 LogDebug(4, "opening %s\n", globalConfPtr->cellConfigdir);
467 BU_conf = afsconf_Open(globalConfPtr->cellConfigdir);
469 LogError(code, "Failed getting cell info\n");
470 afs_com_err(whoami, code, "Failed getting cell info");
474 code = afsconf_GetLocalCell(BU_conf, lcell, sizeof(lcell));
476 LogError(0, "** Can't determine local cell name!\n");
480 if (globalConfPtr->myHost == 0) {
481 /* if user hasn't supplied a list of servers, extract server
482 * list from the cell's database
485 LogDebug(1, "Using server list from %s cell database.\n", lcell);
487 code = afsconf_GetExtendedCellInfo (BU_conf, lcell, 0, &cellinfo,
490 convert_cell_to_ubik(&cellinfo, &globalConfPtr->myHost,
491 globalConfPtr->serverList);
496 /* initialize ubik */
497 ubik_CRXSecurityProc = afsconf_ClientAuth;
498 ubik_CRXSecurityRock = (char *)BU_conf;
500 ubik_SRXSecurityProc = afsconf_ServerAuth;
501 ubik_SRXSecurityRock = (char *)BU_conf;
503 ubik_CheckRXSecurityProc = afsconf_CheckAuth;
504 ubik_CheckRXSecurityRock = (char *)BU_conf;
506 if (ubik_nBuffers == 0)
509 LogError(0, "Will allocate %d ubik buffers\n", ubik_nBuffers);
512 (char *)malloc(strlen(globalConfPtr->databaseDirectory) +
513 strlen(globalConfPtr->databaseName) + 1);
517 /* construct the database name */
518 strcpy(dbNamePtr, globalConfPtr->databaseDirectory);
519 strcat(dbNamePtr, globalConfPtr->databaseName); /* name prefix */
521 rx_SetRxDeadTime(60); /* 60 seconds inactive before timeout */
526 if (AFSDIR_SERVER_NETRESTRICT_FILEPATH ||
527 AFSDIR_SERVER_NETINFO_FILEPATH) {
529 ccode = parseNetFiles(SHostAddrs, NULL, NULL,
530 ADDRSPERSITE, reason,
531 AFSDIR_SERVER_NETINFO_FILEPATH,
532 AFSDIR_SERVER_NETRESTRICT_FILEPATH);
536 ccode = rx_getAllAddr(SHostAddrs, ADDRSPERSITE);
539 host = SHostAddrs[0];
540 rx_InitHost(host, htons(AFSCONF_BUDBPORT));
544 code = ubik_ServerInitByInfo (globalConfPtr->myHost,
545 htons(AFSCONF_BUDBPORT),
548 dbNamePtr, /* name prefix */
552 LogError(code, "Ubik init failed\n");
553 afs_com_err(whoami, code, "Ubik init failed");
557 sca[RX_SCINDEX_NULL] = rxnull_NewServerSecurityObject();
558 sca[RX_SCINDEX_VAB] = 0;
559 sca[RX_SCINDEX_KAD] =
560 rxkad_NewServerSecurityObject(rxkad_clear, BU_conf, afsconf_GetKey,
563 /* Disable jumbograms */
567 rx_NewServiceHost(host, 0, BUDB_SERVICE, "BackupDatabase", sca, 3,
568 BUDB_ExecuteRequest);
569 if (tservice == (struct rx_service *)0) {
570 LogError(0, "Could not create backup database rx service\n");
571 printf("Could not create backup database rx service\n");
574 rx_SetMinProcs(tservice, 1);
575 rx_SetMaxProcs(tservice, 3);
576 rx_SetStackSize(tservice, 10000);
578 /* allow super users to manage RX statistics */
579 rx_SetRxStatUserOk(BU_rxstat_userok);
581 /* misc. initialization */
583 /* database dump synchronization */
584 memset(dumpSyncPtr, 0, sizeof(*dumpSyncPtr));
585 Lock_Init(&dumpSyncPtr->ds_lock);
587 rx_StartServer(0); /* start handling requests */
594 currentTime = time(0);
595 LogError(0, "Ready to process requests at %s\n", ctime(¤tTime));
597 rx_ServerProc(); /* donate this LWP */
600 osi_audit(BUDB_FinishEvent, code, AUD_END);
607 /* do consistency checks on structure sizes */
608 if ((sizeof(struct htBlock) > BLOCKSIZE)
609 || (sizeof(struct vfBlock) > BLOCKSIZE)
610 || (sizeof(struct viBlock) > BLOCKSIZE)
611 || (sizeof(struct dBlock) > BLOCKSIZE)
612 || (sizeof(struct tBlock) > BLOCKSIZE)
614 fprintf(stderr, "Block layout error!\n");
620 LogDebug(level, a, b, c, d, e, f, g, h, i)
622 char *a, *b, *c, *d, *e, *f, *g, *h, *i;
625 if (debugging >= level) {
626 /* log normally closed so can remove it */
627 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
628 if (globalConfPtr->log != NULL) {
629 fprintf(globalConfPtr->log, a, b, c, d, e, f, g, h, i);
630 fflush(globalConfPtr->log);
631 fclose(globalConfPtr->log);
641 static char timestamp[20];
644 strftime(timestamp, 20, "%m/%d/%Y %T", lt);
649 Log(a, b, c, d, e, f, g, h, i)
650 char *a, *b, *c, *d, *e, *f, *g, *h, *i;
654 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
655 if (globalConfPtr->log != NULL) {
657 fprintf(globalConfPtr->log, "%s ", TimeStamp(now));
659 fprintf(globalConfPtr->log, a, b, c, d, e, f, g, h, i);
660 fflush(globalConfPtr->log);
661 fclose(globalConfPtr->log);
667 LogError(code, a, b, c, d, e, f, g, h, i)
669 char *a, *b, *c, *d, *e, *f, *g, *h, *i;
673 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
675 if (globalConfPtr->log != NULL) {
677 fprintf(globalConfPtr->log, "%s ", TimeStamp(now));
680 fprintf(globalConfPtr->log, "%s: %s\n", afs_error_table_name(code),
681 afs_error_message(code));
682 fprintf(globalConfPtr->log, a, b, c, d, e, f, g, h, i);
683 fflush(globalConfPtr->log);
684 fclose(globalConfPtr->log);
697 struct dump *dumpPtr;
699 struct dump hostDump;
700 extern buServerConfP globalConfPtr;
702 dump_ntoh(dumpPtr, &hostDump);
704 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
705 if (globalConfPtr->log != NULL) {
706 printDump(globalConfPtr->log, &hostDump);
707 fclose(globalConfPtr->log);