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>
19 #include <WINNT/afsevent.h>
27 #include <rx/rx_globals.h>
28 #include <afs/cellconfig.h>
29 #include <afs/bubasics.h>
30 #include <afs/afsutil.h>
31 #include <afs/com_err.h>
32 #include <afs/audit.h>
34 #include "budb_errs.h"
36 #include "error_macros.h"
37 #include "budb_internal.h"
40 struct ubik_dbase *BU_dbase;
41 struct afsconf_dir *BU_conf; /* for getting cell info */
43 int argHandler(struct cmd_syndesc *, void *);
44 int truncateDatabase(void);
45 int parseServerList(struct cmd_item *);
47 char lcell[MAXKTCREALMLEN];
48 afs_uint32 myHost = 0;
51 /* server's global configuration information. This is exported to other
55 buServerConfT globalConf;
56 buServerConfP globalConfPtr = &globalConf;
57 char dbDir[AFSDIR_PATH_MAX], cellConfDir[AFSDIR_PATH_MAX];
58 /* debugging control */
67 #define ADDRSPERSITE 16 /* Same global is in rx/rx_user.c */
68 afs_uint32 SHostAddrs[ADDRSPERSITE];
70 /* check whether caller is authorized to manage RX statistics */
72 BU_rxstat_userok(struct rx_call *call)
74 return afsconf_SuperUser(BU_conf, call, NULL);
78 convert_cell_to_ubik(struct afsconf_cell *cellinfo, afs_uint32 *myHost,
79 afs_uint32 *serverList)
86 gethostname(hostname, sizeof(hostname));
87 th = gethostbyname(hostname);
89 printf("prserver: couldn't get address of this host.\n");
92 memcpy(myHost, th->h_addr, sizeof(afs_uint32));
94 for (i = 0; i < cellinfo->numServers; i++)
95 /* omit my host from serverList */
96 if (cellinfo->hostAddr[i].sin_addr.s_addr != *myHost)
97 *serverList++ = cellinfo->hostAddr[i].sin_addr.s_addr;
99 *serverList = 0; /* terminate list */
104 * The whole purpose of MyBeforeProc is to detect
105 * if the -help option was not within the command line.
106 * If it were, this routine would never have been called.
109 MyBeforeProc(struct cmd_syndesc *as, void *arock)
115 /* initializeCommands
116 * initialize all the supported commands and their arguments
120 initializeArgHandler(void)
122 struct cmd_syndesc *cptr;
124 cmd_SetBeforeProc(MyBeforeProc, NULL);
126 cptr = cmd_CreateSyntax(NULL, argHandler, NULL, "Backup database server");
128 cmd_AddParm(cptr, "-database", CMD_SINGLE, CMD_OPTIONAL,
129 "database directory");
131 cmd_AddParm(cptr, "-cellservdb", CMD_SINGLE, CMD_OPTIONAL,
132 "cell configuration directory");
134 cmd_AddParm(cptr, "-resetdb", CMD_FLAG, CMD_OPTIONAL,
135 "truncate the database");
137 cmd_AddParm(cptr, "-noauth", CMD_FLAG, CMD_OPTIONAL,
138 "run without authentication");
140 cmd_AddParm(cptr, "-smallht", CMD_FLAG, CMD_OPTIONAL,
141 "use small hash tables");
143 cmd_AddParm(cptr, "-servers", CMD_LIST, CMD_OPTIONAL,
144 "list of ubik database servers");
146 cmd_AddParm(cptr, "-ubikbuffers", CMD_SINGLE, CMD_OPTIONAL,
147 "the number of ubik buffers");
149 cmd_AddParm(cptr, "-auditlog", CMD_SINGLE, CMD_OPTIONAL,
152 cmd_AddParm(cptr, "-p", CMD_SINGLE, CMD_OPTIONAL,
153 "number of processes");
155 cmd_AddParm(cptr, "-rxbind", CMD_FLAG, CMD_OPTIONAL,
156 "bind the Rx socket (primary interface only)");
158 cmd_AddParm(cptr, "-audit-interface", CMD_SINGLE, CMD_OPTIONAL,
159 "audit interface (file or sysvmq)");
163 argHandler(struct cmd_syndesc *as, void *arock)
166 /* globalConfPtr provides the handle for the configuration information */
168 /* database directory */
169 if (as->parms[0].items != 0) {
170 globalConfPtr->databaseDirectory =
171 (char *)malloc(strlen(as->parms[0].items->data) + 1);
172 if (globalConfPtr->databaseDirectory == 0)
174 strcpy(globalConfPtr->databaseDirectory, as->parms[0].items->data);
177 /* -cellservdb, cell configuration directory */
178 if (as->parms[1].items != 0) {
179 globalConfPtr->cellConfigdir =
180 (char *)malloc(strlen(as->parms[1].items->data) + 1);
181 if (globalConfPtr->cellConfigdir == 0)
184 strcpy(globalConfPtr->cellConfigdir, as->parms[1].items->data);
186 globalConfPtr->debugFlags |= DF_RECHECKNOAUTH;
189 /* truncate the database */
190 if (as->parms[2].items != 0)
193 /* run without authentication */
194 if (as->parms[3].items != 0)
195 globalConfPtr->debugFlags |= DF_NOAUTH;
197 /* use small hash tables */
198 if (as->parms[4].items != 0)
199 globalConfPtr->debugFlags |= DF_SMALLHT;
201 /* user provided list of ubik database servers */
202 if (as->parms[5].items != 0)
203 parseServerList(as->parms[5].items);
205 /* user provided the number of ubik buffers */
206 if (as->parms[6].items != 0)
207 ubik_nBuffers = atoi(as->parms[6].items->data);
211 /* param 7 (-auditlog) handled below */
213 /* user provided the number of threads */
214 if (as->parms[8].items != 0) {
215 lwps = atoi(as->parms[8].items->data);
217 printf ("Warning: '-p %d' is too big; using %d instead\n",
222 printf ("Warning: '-p %d' is too small; using %d instead\n",
228 /* user provided rxbind option */
229 if (as->parms[9].items != 0) {
233 /* -audit-interface */
234 if (as->parms[10].items != 0) {
235 char *interface = as->parms[10].items->data;
237 if (osi_audit_interface(interface)) {
238 printf("Invalid audit interface '%s'\n", interface);
244 /* needs to be after -audit-interface, so we osi_audit_interface
245 * before we osi_audit_file */
246 if (as->parms[7].items != 0) {
247 char *fileName = as->parms[7].items->data;
249 osi_audit_file(fileName);
258 parseServerList(struct cmd_item *itemPtr)
260 struct cmd_item *save;
263 afs_int32 nservers = 0;
268 /* compute number of servers in the list */
271 itemPtr = itemPtr->next;
274 LogDebug(3, "%d servers\n", nservers);
276 /* now can allocate the space for the server arguments */
277 serverArgs = (char **)malloc((nservers + 2) * sizeof(char *));
285 /* now go through and construct the list of servers */
288 *ptr++ = itemPtr->data;
289 itemPtr = itemPtr->next;
293 ubik_ParseServerList(nservers + 2, serverArgs, &globalConfPtr->myHost,
294 globalConfPtr->serverList);
298 /* free space for the server args */
299 free((char *)serverArgs);
306 * truncates just the database file.
310 truncateDatabase(void)
317 (char *)malloc(strlen(globalConfPtr->databaseDirectory) +
318 strlen(globalConfPtr->databaseName) +
319 strlen(globalConfPtr->databaseExtension) + 1);
323 /* construct the database name */
324 strcpy(path, globalConfPtr->databaseDirectory);
325 strcat(path, globalConfPtr->databaseName);
326 strcat(path, globalConfPtr->databaseExtension);
328 fd = open(path, O_RDWR, 0755);
332 if (ftruncate(fd, 0) != 0) {
345 #include "AFS_component_version_number.c"
348 main(int argc, char **argv)
350 char *whoami = argv[0];
352 struct afsconf_cell cellinfo;
355 afs_uint32 host = ntohl(INADDR_ANY);
357 char clones[MAXHOSTSPERCELL];
359 struct rx_service *tservice;
360 struct rx_securityClass **securityClasses;
361 afs_int32 numClasses;
363 extern int rx_stackSize;
366 /* initialize winsock */
367 if (afs_winsockInit() < 0) {
368 ReportErrorEventAlt(AFSEVT_SVR_WINSOCK_INIT_FAILED, 0, argv[0], 0);
369 fprintf(stderr, "%s: Couldn't initialize winsock.\n", whoami);
376 * The following signal action for AIX is necessary so that in case of a
377 * crash (i.e. core is generated) we can include the user's data section
378 * in the core dump. Unfortunately, by default, only a partial core is
379 * generated which, in many cases, isn't too useful.
381 struct sigaction nsa;
383 sigemptyset(&nsa.sa_mask);
384 nsa.sa_handler = SIG_DFL;
385 nsa.sa_flags = SA_FULLDUMP;
386 sigaction(SIGSEGV, &nsa, NULL);
387 sigaction(SIGABRT, &nsa, NULL);
390 osi_audit(BUDB_StartEvent, 0, AUD_END);
392 initialize_BUDB_error_table();
393 initializeArgHandler();
395 /* Initialize dirpaths */
396 if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
398 ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
400 afs_com_err(whoami, errno, "; Unable to obtain AFS server directory.");
404 memset(globalConfPtr, 0, sizeof(*globalConfPtr));
406 /* set default configuration values */
407 strcpy(dbDir, AFSDIR_SERVER_DB_DIRPATH);
409 globalConfPtr->databaseDirectory = dbDir;
410 globalConfPtr->databaseName = DEFAULT_DBPREFIX;
411 strcpy(cellConfDir, AFSDIR_SERVER_ETC_DIRPATH);
412 globalConfPtr->cellConfigdir = cellConfDir;
414 /* open the log file */
416 globalConfPtr->log = fopen(DEFAULT_LOGNAME,"a");
417 if ( globalConfPtr->log == NULL )
419 printf("Can't open log file %s - aborting\n", DEFAULT_LOGNAME);
426 #ifdef AFS_PTHREAD_ENV
427 SetLogThreadNumProgram( rx_GetThreadNum );
430 /* process the user supplied args */
432 code = cmd_Dispatch(argc, argv);
436 /* exit if there was a help option */
440 /* open the log file */
441 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
442 if (globalConfPtr->log == NULL) {
443 printf("Can't open log file %s - aborting\n",
444 AFSDIR_SERVER_BUDBLOG_FILEPATH);
448 /* keep log closed so can remove it */
450 fclose(globalConfPtr->log);
452 /* open the cell's configuration directory */
453 LogDebug(4, "opening %s\n", globalConfPtr->cellConfigdir);
455 BU_conf = afsconf_Open(globalConfPtr->cellConfigdir);
457 LogError(code, "Failed getting cell info\n");
458 afs_com_err(whoami, code, "Failed getting cell info");
462 code = afsconf_GetLocalCell(BU_conf, lcell, sizeof(lcell));
464 LogError(0, "** Can't determine local cell name!\n");
468 if (globalConfPtr->myHost == 0) {
469 /* if user hasn't supplied a list of servers, extract server
470 * list from the cell's database
473 LogDebug(1, "Using server list from %s cell database.\n", lcell);
475 code = afsconf_GetExtendedCellInfo (BU_conf, lcell, 0, &cellinfo,
478 convert_cell_to_ubik(&cellinfo, &globalConfPtr->myHost,
479 globalConfPtr->serverList);
484 /* initialize ubik */
485 ubik_SetClientSecurityProcs(afsconf_ClientAuth, afsconf_UpToDate, BU_conf);
486 ubik_SetServerSecurityProcs(afsconf_BuildServerSecurityObjects,
487 afsconf_CheckAuth, BU_conf);
489 if (ubik_nBuffers == 0)
492 LogError(0, "Will allocate %d ubik buffers\n", ubik_nBuffers);
495 (char *)malloc(strlen(globalConfPtr->databaseDirectory) +
496 strlen(globalConfPtr->databaseName) + 1);
500 /* construct the database name */
501 strcpy(dbNamePtr, globalConfPtr->databaseDirectory);
502 strcat(dbNamePtr, globalConfPtr->databaseName); /* name prefix */
504 rx_SetRxDeadTime(60); /* 60 seconds inactive before timeout */
508 if (AFSDIR_SERVER_NETRESTRICT_FILEPATH ||
509 AFSDIR_SERVER_NETINFO_FILEPATH) {
511 ccode = parseNetFiles(SHostAddrs, NULL, NULL,
512 ADDRSPERSITE, reason,
513 AFSDIR_SERVER_NETINFO_FILEPATH,
514 AFSDIR_SERVER_NETRESTRICT_FILEPATH);
517 ccode = rx_getAllAddr(SHostAddrs, ADDRSPERSITE);
520 host = SHostAddrs[0];
521 rx_InitHost(host, htons(AFSCONF_BUDBPORT));
525 code = ubik_ServerInitByInfo (globalConfPtr->myHost,
526 htons(AFSCONF_BUDBPORT),
529 dbNamePtr, /* name prefix */
533 LogError(code, "Ubik init failed\n");
534 afs_com_err(whoami, code, "Ubik init failed");
538 afsconf_BuildServerSecurityObjects(BU_conf, &securityClasses, &numClasses);
540 /* Disable jumbograms */
544 rx_NewServiceHost(host, 0, BUDB_SERVICE, "BackupDatabase",
545 securityClasses, numClasses, BUDB_ExecuteRequest);
547 if (tservice == (struct rx_service *)0) {
548 LogError(0, "Could not create backup database rx service\n");
549 printf("Could not create backup database rx service\n");
552 rx_SetMinProcs(tservice, 1);
553 rx_SetMaxProcs(tservice, lwps);
554 rx_SetStackSize(tservice, 10000);
556 /* allow super users to manage RX statistics */
557 rx_SetRxStatUserOk(BU_rxstat_userok);
559 /* misc. initialization */
561 /* database dump synchronization */
562 memset(dumpSyncPtr, 0, sizeof(*dumpSyncPtr));
563 Lock_Init(&dumpSyncPtr->ds_lock);
565 rx_StartServer(0); /* start handling requests */
572 currentTime = time(0);
573 LogError(0, "Ready to process requests at %s\n", ctime(¤tTime));
575 rx_ServerProc(NULL); /* donate this LWP */
578 osi_audit(BUDB_FinishEvent, code, AUD_END);
583 consistencyCheckDb(void)
585 /* do consistency checks on structure sizes */
586 if ((sizeof(struct htBlock) > BLOCKSIZE)
587 || (sizeof(struct vfBlock) > BLOCKSIZE)
588 || (sizeof(struct viBlock) > BLOCKSIZE)
589 || (sizeof(struct dBlock) > BLOCKSIZE)
590 || (sizeof(struct tBlock) > BLOCKSIZE)
592 fprintf(stderr, "Block layout error!\n");
598 LogDebug(int level, char *fmt, ... )
604 if (debugging >= level) {
605 /* log normally closed so can remove it */
606 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
607 if (globalConfPtr->log != NULL) {
608 vfprintf(globalConfPtr->log, fmt, ap);
609 fflush(globalConfPtr->log);
610 fclose(globalConfPtr->log);
620 static char timestamp[20];
623 strftime(timestamp, 20, "%m/%d/%Y %H:%M:%S", lt);
634 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
635 if (globalConfPtr->log != NULL) {
637 fprintf(globalConfPtr->log, "%s ", TimeStamp(now));
639 vfprintf(globalConfPtr->log, fmt, ap);
640 fflush(globalConfPtr->log);
641 fclose(globalConfPtr->log);
647 LogError(long code, char *fmt, ... )
653 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
655 if (globalConfPtr->log != NULL) {
657 fprintf(globalConfPtr->log, "%s ", TimeStamp(now));
660 fprintf(globalConfPtr->log, "%s: %s\n", afs_error_table_name(code),
661 afs_error_message(code));
662 vfprintf(globalConfPtr->log, fmt, ap );
663 fflush(globalConfPtr->log);
664 fclose(globalConfPtr->log);
675 LogNetDump(struct dump *dumpPtr)
677 struct dump hostDump;
678 extern buServerConfP globalConfPtr;
680 dump_ntoh(dumpPtr, &hostDump);
682 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
683 if (globalConfPtr->log != NULL) {
684 printDump(globalConfPtr->log, &hostDump);
685 fclose(globalConfPtr->log);