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 */
78 #define ADDRSPERSITE 16 /* Same global is in rx/rx_user.c */
79 afs_uint32 SHostAddrs[ADDRSPERSITE];
81 #if defined(AFS_PTHREAD_ENV)
85 return pthread_getspecific(rx_thread_id_key);
89 /* check whether caller is authorized to manage RX statistics */
91 BU_rxstat_userok(call)
94 return afsconf_SuperUser(BU_conf, call, NULL);
98 convert_cell_to_ubik(cellinfo, myHost, serverList)
99 struct afsconf_cell *cellinfo;
101 afs_int32 *serverList;
108 gethostname(hostname, sizeof(hostname));
109 th = gethostbyname(hostname);
111 printf("prserver: couldn't get address of this host.\n");
114 memcpy(myHost, th->h_addr, sizeof(afs_int32));
116 for (i = 0; i < cellinfo->numServers; i++)
117 /* omit my host from serverList */
118 if (cellinfo->hostAddr[i].sin_addr.s_addr != *myHost)
119 *serverList++ = cellinfo->hostAddr[i].sin_addr.s_addr;
121 *serverList = 0; /* terminate list */
126 * The whole purpose of MyBeforeProc is to detect
127 * if the -help option was not within the command line.
128 * If it were, this routine would never have been called.
131 MyBeforeProc(register struct cmd_syndesc *as, void *arock)
137 /* initializeCommands
138 * initialize all the supported commands and their arguments
142 initializeArgHandler()
144 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,
174 cmd_AddParm(cptr, "-p", CMD_SINGLE, CMD_OPTIONAL,
175 "number of processes");
177 cmd_AddParm(cptr, "-rxbind", CMD_FLAG, CMD_OPTIONAL,
178 "bind the Rx socket (primary interface only)");
183 argHandler(struct cmd_syndesc *as, void *arock)
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) {
232 char *fileName = as->parms[7].items->data;
234 osi_audit_file(fileName);
237 /* user provided the number of threads */
238 if (as->parms[8].items != 0) {
239 lwps = atoi(as->parms[8].items->data);
241 printf ("Warning: '-p %d' is too big; using %d instead\n",
246 printf ("Warning: '-p %d' is too small; using %d instead\n",
252 /* user provided rxbind option */
253 if (as->parms[9].items != 0) {
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;
360 afs_uint32 host = ntohl(INADDR_ANY);
362 char clones[MAXHOSTSPERCELL];
364 struct rx_service *tservice;
365 struct rx_securityClass *sca[3];
367 extern int afsconf_ServerAuth();
368 extern int afsconf_CheckAuth();
370 extern int rx_stackSize;
371 extern int BUDB_ExecuteRequest();
374 /* initialize winsock */
375 if (afs_winsockInit() < 0) {
376 ReportErrorEventAlt(AFSEVT_SVR_WINSOCK_INIT_FAILED, 0, argv[0], 0);
377 fprintf(stderr, "%s: Couldn't initialize winsock.\n", whoami);
384 * The following signal action for AIX is necessary so that in case of a
385 * crash (i.e. core is generated) we can include the user's data section
386 * in the core dump. Unfortunately, by default, only a partial core is
387 * generated which, in many cases, isn't too useful.
389 struct sigaction nsa;
391 sigemptyset(&nsa.sa_mask);
392 nsa.sa_handler = SIG_DFL;
393 nsa.sa_flags = SA_FULLDUMP;
394 sigaction(SIGSEGV, &nsa, NULL);
395 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 afs_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 afs_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 */
521 if (AFSDIR_SERVER_NETRESTRICT_FILEPATH ||
522 AFSDIR_SERVER_NETINFO_FILEPATH) {
524 ccode = parseNetFiles(SHostAddrs, NULL, NULL,
525 ADDRSPERSITE, reason,
526 AFSDIR_SERVER_NETINFO_FILEPATH,
527 AFSDIR_SERVER_NETRESTRICT_FILEPATH);
530 ccode = rx_getAllAddr(SHostAddrs, ADDRSPERSITE);
533 host = SHostAddrs[0];
534 rx_InitHost(host, htons(AFSCONF_BUDBPORT));
538 code = ubik_ServerInitByInfo (globalConfPtr->myHost,
539 htons(AFSCONF_BUDBPORT),
542 dbNamePtr, /* name prefix */
546 LogError(code, "Ubik init failed\n");
547 afs_com_err(whoami, code, "Ubik init failed");
551 sca[RX_SCINDEX_NULL] = rxnull_NewServerSecurityObject();
552 sca[RX_SCINDEX_VAB] = 0;
553 sca[RX_SCINDEX_KAD] =
554 rxkad_NewServerSecurityObject(rxkad_clear, BU_conf, afsconf_GetKey,
557 /* Disable jumbograms */
561 rx_NewServiceHost(host, 0, BUDB_SERVICE, "BackupDatabase", sca, 3,
562 BUDB_ExecuteRequest);
563 if (tservice == (struct rx_service *)0) {
564 LogError(0, "Could not create backup database rx service\n");
565 printf("Could not create backup database rx service\n");
568 rx_SetMinProcs(tservice, 1);
569 rx_SetMaxProcs(tservice, lwps);
570 rx_SetStackSize(tservice, 10000);
572 /* allow super users to manage RX statistics */
573 rx_SetRxStatUserOk(BU_rxstat_userok);
575 /* misc. initialization */
577 /* database dump synchronization */
578 memset(dumpSyncPtr, 0, sizeof(*dumpSyncPtr));
579 Lock_Init(&dumpSyncPtr->ds_lock);
581 rx_StartServer(0); /* start handling requests */
588 currentTime = time(0);
589 LogError(0, "Ready to process requests at %s\n", ctime(¤tTime));
591 rx_ServerProc(NULL); /* donate this LWP */
594 osi_audit(BUDB_FinishEvent, code, AUD_END);
601 /* do consistency checks on structure sizes */
602 if ((sizeof(struct htBlock) > BLOCKSIZE)
603 || (sizeof(struct vfBlock) > BLOCKSIZE)
604 || (sizeof(struct viBlock) > BLOCKSIZE)
605 || (sizeof(struct dBlock) > BLOCKSIZE)
606 || (sizeof(struct tBlock) > BLOCKSIZE)
608 fprintf(stderr, "Block layout error!\n");
614 LogDebug(level, a, b, c, d, e, f, g, h, i)
616 char *a, *b, *c, *d, *e, *f, *g, *h, *i;
619 if (debugging >= level) {
620 /* log normally closed so can remove it */
621 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
622 if (globalConfPtr->log != NULL) {
623 fprintf(globalConfPtr->log, a, b, c, d, e, f, g, h, i);
624 fflush(globalConfPtr->log);
625 fclose(globalConfPtr->log);
635 static char timestamp[20];
638 strftime(timestamp, 20, "%m/%d/%Y %H:%M:%S", lt);
643 Log(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");
649 if (globalConfPtr->log != NULL) {
651 fprintf(globalConfPtr->log, "%s ", TimeStamp(now));
653 fprintf(globalConfPtr->log, a, b, c, d, e, f, g, h, i);
654 fflush(globalConfPtr->log);
655 fclose(globalConfPtr->log);
661 LogError(code, a, b, c, d, e, f, g, h, i)
663 char *a, *b, *c, *d, *e, *f, *g, *h, *i;
667 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
669 if (globalConfPtr->log != NULL) {
671 fprintf(globalConfPtr->log, "%s ", TimeStamp(now));
674 fprintf(globalConfPtr->log, "%s: %s\n", afs_error_table_name(code),
675 afs_error_message(code));
676 fprintf(globalConfPtr->log, a, b, c, d, e, f, g, h, i);
677 fflush(globalConfPtr->log);
678 fclose(globalConfPtr->log);
691 struct dump *dumpPtr;
693 struct dump hostDump;
694 extern buServerConfP globalConfPtr;
696 dump_ntoh(dumpPtr, &hostDump);
698 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
699 if (globalConfPtr->log != NULL) {
700 printDump(globalConfPtr->log, &hostDump);
701 fclose(globalConfPtr->log);