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>
18 #include <WINNT/afsevent.h>
20 #include <netinet/in.h>
27 #include <sys/types.h>
36 #include <rx/rx_globals.h>
37 #include <afs/cellconfig.h>
38 #include <afs/bubasics.h>
39 #include <afs/afsutil.h>
40 #include <afs/com_err.h>
45 #include "budb_errs.h"
47 #include "error_macros.h"
48 #include "budb_internal.h"
50 #include "afs/audit.h"
52 struct ubik_dbase *BU_dbase;
53 struct afsconf_dir *BU_conf; /* for getting cell info */
55 int argHandler(struct cmd_syndesc *, void *);
56 int truncateDatabase(void);
57 int parseServerList(struct cmd_item *);
59 char lcell[MAXKTCREALMLEN];
63 /* server's global configuration information. This is exported to other
67 buServerConfT globalConf;
68 buServerConfP globalConfPtr = &globalConf;
69 char dbDir[AFSDIR_PATH_MAX], cellConfDir[AFSDIR_PATH_MAX];
70 /* debugging control */
79 #define ADDRSPERSITE 16 /* Same global is in rx/rx_user.c */
80 afs_uint32 SHostAddrs[ADDRSPERSITE];
82 #if defined(AFS_PTHREAD_ENV)
86 return pthread_getspecific(rx_thread_id_key);
90 /* check whether caller is authorized to manage RX statistics */
92 BU_rxstat_userok(struct rx_call *call)
94 return afsconf_SuperUser(BU_conf, call, NULL);
98 convert_cell_to_ubik(struct afsconf_cell *cellinfo, afs_int32 *myHost,
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.
129 MyBeforeProc(register struct cmd_syndesc *as, void *arock)
135 /* initializeCommands
136 * initialize all the supported commands and their arguments
140 initializeArgHandler(void)
142 struct cmd_syndesc *cptr;
144 cmd_SetBeforeProc(MyBeforeProc, NULL);
146 cptr = cmd_CreateSyntax(NULL, argHandler, NULL, "Backup database server");
148 cmd_AddParm(cptr, "-database", CMD_SINGLE, CMD_OPTIONAL,
149 "database directory");
151 cmd_AddParm(cptr, "-cellservdb", CMD_SINGLE, CMD_OPTIONAL,
152 "cell configuration directory");
154 cmd_AddParm(cptr, "-resetdb", CMD_FLAG, CMD_OPTIONAL,
155 "truncate the database");
157 cmd_AddParm(cptr, "-noauth", CMD_FLAG, CMD_OPTIONAL,
158 "run without authentication");
160 cmd_AddParm(cptr, "-smallht", CMD_FLAG, CMD_OPTIONAL,
161 "use small hash tables");
163 cmd_AddParm(cptr, "-servers", CMD_LIST, CMD_OPTIONAL,
164 "list of ubik database servers");
166 cmd_AddParm(cptr, "-ubikbuffers", CMD_SINGLE, CMD_OPTIONAL,
167 "the number of ubik buffers");
169 cmd_AddParm(cptr, "-auditlog", CMD_SINGLE, CMD_OPTIONAL,
172 cmd_AddParm(cptr, "-p", CMD_SINGLE, CMD_OPTIONAL,
173 "number of processes");
175 cmd_AddParm(cptr, "-rxbind", CMD_FLAG, CMD_OPTIONAL,
176 "bind the Rx socket (primary interface only)");
178 cmd_AddParm(cptr, "-audit-interface", CMD_SINGLE, CMD_OPTIONAL,
179 "audit interface (file or sysvmq)");
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 /* param 7 (-auditlog) handled below */
233 /* user provided the number of threads */
234 if (as->parms[8].items != 0) {
235 lwps = atoi(as->parms[8].items->data);
237 printf ("Warning: '-p %d' is too big; using %d instead\n",
242 printf ("Warning: '-p %d' is too small; using %d instead\n",
248 /* user provided rxbind option */
249 if (as->parms[9].items != 0) {
253 /* -audit-interface */
254 if (as->parms[10].items != 0) {
255 char *interface = as->parms[10].items->data;
257 if (osi_audit_interface(interface)) {
258 printf("Invalid audit interface '%s'\n", interface);
264 /* needs to be after -audit-interface, so we osi_audit_interface
265 * before we osi_audit_file */
266 if (as->parms[7].items != 0) {
267 char *fileName = as->parms[7].items->data;
269 osi_audit_file(fileName);
278 parseServerList(struct cmd_item *itemPtr)
280 struct cmd_item *save;
283 afs_int32 nservers = 0;
288 /* compute number of servers in the list */
291 itemPtr = itemPtr->next;
294 LogDebug(3, "%d servers\n", nservers);
296 /* now can allocate the space for the server arguments */
297 serverArgs = (char **)malloc((nservers + 2) * sizeof(char *));
305 /* now go through and construct the list of servers */
308 *ptr++ = itemPtr->data;
309 itemPtr = itemPtr->next;
313 ubik_ParseServerList(nservers + 2, serverArgs, &globalConfPtr->myHost,
314 globalConfPtr->serverList);
318 /* free space for the server args */
319 free((char *)serverArgs);
326 * truncates just the database file.
330 truncateDatabase(void)
337 (char *)malloc(strlen(globalConfPtr->databaseDirectory) +
338 strlen(globalConfPtr->databaseName) +
339 strlen(globalConfPtr->databaseExtension) + 1);
343 /* construct the database name */
344 strcpy(path, globalConfPtr->databaseDirectory);
345 strcat(path, globalConfPtr->databaseName);
346 strcat(path, globalConfPtr->databaseExtension);
348 fd = open(path, O_RDWR, 0755);
352 if (ftruncate(fd, 0) != 0) {
365 #include "AFS_component_version_number.c"
368 main(int argc, char **argv)
370 char *whoami = argv[0];
372 struct afsconf_cell cellinfo;
375 afs_uint32 host = ntohl(INADDR_ANY);
377 char clones[MAXHOSTSPERCELL];
379 struct rx_service *tservice;
380 struct rx_securityClass *sca[3];
382 extern int rx_stackSize;
385 /* initialize winsock */
386 if (afs_winsockInit() < 0) {
387 ReportErrorEventAlt(AFSEVT_SVR_WINSOCK_INIT_FAILED, 0, argv[0], 0);
388 fprintf(stderr, "%s: Couldn't initialize winsock.\n", whoami);
395 * The following signal action for AIX is necessary so that in case of a
396 * crash (i.e. core is generated) we can include the user's data section
397 * in the core dump. Unfortunately, by default, only a partial core is
398 * generated which, in many cases, isn't too useful.
400 struct sigaction nsa;
402 sigemptyset(&nsa.sa_mask);
403 nsa.sa_handler = SIG_DFL;
404 nsa.sa_flags = SA_FULLDUMP;
405 sigaction(SIGSEGV, &nsa, NULL);
406 sigaction(SIGABRT, &nsa, NULL);
409 osi_audit(BUDB_StartEvent, 0, AUD_END);
411 initialize_BUDB_error_table();
412 initializeArgHandler();
414 /* Initialize dirpaths */
415 if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
417 ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
419 afs_com_err(whoami, errno, "; Unable to obtain AFS server directory.");
423 memset(globalConfPtr, 0, sizeof(*globalConfPtr));
425 /* set default configuration values */
426 strcpy(dbDir, AFSDIR_SERVER_DB_DIRPATH);
428 globalConfPtr->databaseDirectory = dbDir;
429 globalConfPtr->databaseName = DEFAULT_DBPREFIX;
430 strcpy(cellConfDir, AFSDIR_SERVER_ETC_DIRPATH);
431 globalConfPtr->cellConfigdir = cellConfDir;
433 /* open the log file */
435 globalConfPtr->log = fopen(DEFAULT_LOGNAME,"a");
436 if ( globalConfPtr->log == NULL )
438 printf("Can't open log file %s - aborting\n", DEFAULT_LOGNAME);
445 #ifdef AFS_PTHREAD_ENV
446 SetLogThreadNumProgram( threadNum );
449 /* process the user supplied args */
451 code = cmd_Dispatch(argc, argv);
455 /* exit if there was a help option */
459 /* open the log file */
460 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
461 if (globalConfPtr->log == NULL) {
462 printf("Can't open log file %s - aborting\n",
463 AFSDIR_SERVER_BUDBLOG_FILEPATH);
467 /* keep log closed so can remove it */
469 fclose(globalConfPtr->log);
471 /* open the cell's configuration directory */
472 LogDebug(4, "opening %s\n", globalConfPtr->cellConfigdir);
474 BU_conf = afsconf_Open(globalConfPtr->cellConfigdir);
476 LogError(code, "Failed getting cell info\n");
477 afs_com_err(whoami, code, "Failed getting cell info");
481 code = afsconf_GetLocalCell(BU_conf, lcell, sizeof(lcell));
483 LogError(0, "** Can't determine local cell name!\n");
487 if (globalConfPtr->myHost == 0) {
488 /* if user hasn't supplied a list of servers, extract server
489 * list from the cell's database
492 LogDebug(1, "Using server list from %s cell database.\n", lcell);
494 code = afsconf_GetExtendedCellInfo (BU_conf, lcell, 0, &cellinfo,
497 convert_cell_to_ubik(&cellinfo, &globalConfPtr->myHost,
498 globalConfPtr->serverList);
503 /* initialize ubik */
504 ubik_CRXSecurityProc = afsconf_ClientAuth;
505 ubik_CRXSecurityRock = BU_conf;
507 ubik_SRXSecurityProc = afsconf_ServerAuth;
508 ubik_SRXSecurityRock = BU_conf;
510 ubik_CheckRXSecurityProc = afsconf_CheckAuth;
511 ubik_CheckRXSecurityRock = BU_conf;
513 if (ubik_nBuffers == 0)
516 LogError(0, "Will allocate %d ubik buffers\n", ubik_nBuffers);
519 (char *)malloc(strlen(globalConfPtr->databaseDirectory) +
520 strlen(globalConfPtr->databaseName) + 1);
524 /* construct the database name */
525 strcpy(dbNamePtr, globalConfPtr->databaseDirectory);
526 strcat(dbNamePtr, globalConfPtr->databaseName); /* name prefix */
528 rx_SetRxDeadTime(60); /* 60 seconds inactive before timeout */
532 if (AFSDIR_SERVER_NETRESTRICT_FILEPATH ||
533 AFSDIR_SERVER_NETINFO_FILEPATH) {
535 ccode = parseNetFiles(SHostAddrs, NULL, NULL,
536 ADDRSPERSITE, reason,
537 AFSDIR_SERVER_NETINFO_FILEPATH,
538 AFSDIR_SERVER_NETRESTRICT_FILEPATH);
541 ccode = rx_getAllAddr(SHostAddrs, ADDRSPERSITE);
544 host = SHostAddrs[0];
545 rx_InitHost(host, htons(AFSCONF_BUDBPORT));
549 code = ubik_ServerInitByInfo (globalConfPtr->myHost,
550 htons(AFSCONF_BUDBPORT),
553 dbNamePtr, /* name prefix */
557 LogError(code, "Ubik init failed\n");
558 afs_com_err(whoami, code, "Ubik init failed");
562 sca[RX_SCINDEX_NULL] = rxnull_NewServerSecurityObject();
563 sca[RX_SCINDEX_VAB] = 0;
564 sca[RX_SCINDEX_KAD] =
565 rxkad_NewServerSecurityObject(rxkad_clear, BU_conf, afsconf_GetKey,
568 /* Disable jumbograms */
572 rx_NewServiceHost(host, 0, BUDB_SERVICE, "BackupDatabase", sca, 3,
573 BUDB_ExecuteRequest);
574 if (tservice == (struct rx_service *)0) {
575 LogError(0, "Could not create backup database rx service\n");
576 printf("Could not create backup database rx service\n");
579 rx_SetMinProcs(tservice, 1);
580 rx_SetMaxProcs(tservice, lwps);
581 rx_SetStackSize(tservice, 10000);
583 /* allow super users to manage RX statistics */
584 rx_SetRxStatUserOk(BU_rxstat_userok);
586 /* misc. initialization */
588 /* database dump synchronization */
589 memset(dumpSyncPtr, 0, sizeof(*dumpSyncPtr));
590 Lock_Init(&dumpSyncPtr->ds_lock);
592 rx_StartServer(0); /* start handling requests */
599 currentTime = time(0);
600 LogError(0, "Ready to process requests at %s\n", ctime(¤tTime));
602 rx_ServerProc(NULL); /* donate this LWP */
605 osi_audit(BUDB_FinishEvent, code, AUD_END);
610 consistencyCheckDb(void)
612 /* do consistency checks on structure sizes */
613 if ((sizeof(struct htBlock) > BLOCKSIZE)
614 || (sizeof(struct vfBlock) > BLOCKSIZE)
615 || (sizeof(struct viBlock) > BLOCKSIZE)
616 || (sizeof(struct dBlock) > BLOCKSIZE)
617 || (sizeof(struct tBlock) > BLOCKSIZE)
619 fprintf(stderr, "Block layout error!\n");
625 LogDebug(int level, char *fmt, ... )
631 if (debugging >= level) {
632 /* log normally closed so can remove it */
633 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
634 if (globalConfPtr->log != NULL) {
635 vfprintf(globalConfPtr->log, fmt, ap);
636 fflush(globalConfPtr->log);
637 fclose(globalConfPtr->log);
647 static char timestamp[20];
650 strftime(timestamp, 20, "%m/%d/%Y %H:%M:%S", lt);
661 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
662 if (globalConfPtr->log != NULL) {
664 fprintf(globalConfPtr->log, "%s ", TimeStamp(now));
666 vfprintf(globalConfPtr->log, fmt, ap);
667 fflush(globalConfPtr->log);
668 fclose(globalConfPtr->log);
674 LogError(long code, char *fmt, ... )
680 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
682 if (globalConfPtr->log != NULL) {
684 fprintf(globalConfPtr->log, "%s ", TimeStamp(now));
687 fprintf(globalConfPtr->log, "%s: %s\n", afs_error_table_name(code),
688 afs_error_message(code));
689 vfprintf(globalConfPtr->log, fmt, ap );
690 fflush(globalConfPtr->log);
691 fclose(globalConfPtr->log);
702 LogNetDump(struct dump *dumpPtr)
704 struct dump hostDump;
705 extern buServerConfP globalConfPtr;
707 dump_ntoh(dumpPtr, &hostDump);
709 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
710 if (globalConfPtr->log != NULL) {
711 printDump(globalConfPtr->log, &hostDump);
712 fclose(globalConfPtr->log);