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>
17 #include <WINNT/afsevent.h>
25 #include <rx/rx_globals.h>
26 #include <afs/cellconfig.h>
27 #include <afs/bubasics.h>
28 #include <afs/afsutil.h>
29 #include <afs/com_err.h>
30 #include <afs/audit.h>
32 #include "budb_errs.h"
34 #include "error_macros.h"
35 #include "budb_internal.h"
38 struct ubik_dbase *BU_dbase;
39 struct afsconf_dir *BU_conf; /* for getting cell info */
41 int argHandler(struct cmd_syndesc *, void *);
42 int truncateDatabase(void);
43 int parseServerList(struct cmd_item *);
45 char lcell[MAXKTCREALMLEN];
46 afs_uint32 myHost = 0;
49 /* server's global configuration information. This is exported to other
53 buServerConfT globalConf;
54 buServerConfP globalConfPtr = &globalConf;
55 char dbDir[AFSDIR_PATH_MAX], cellConfDir[AFSDIR_PATH_MAX];
56 /* debugging control */
65 #define ADDRSPERSITE 16 /* Same global is in rx/rx_user.c */
66 afs_uint32 SHostAddrs[ADDRSPERSITE];
68 /* check whether caller is authorized to manage RX statistics */
70 BU_rxstat_userok(struct rx_call *call)
72 return afsconf_SuperUser(BU_conf, call, NULL);
76 * Return true if this name is a member of the local realm.
79 BU_IsLocalRealmMatch(void *rock, char *name, char *inst, char *cell)
81 struct afsconf_dir *dir = (struct afsconf_dir *)rock;
82 afs_int32 islocal = 0; /* default to no */
85 code = afsconf_IsLocalRealmMatch(dir, &islocal, name, inst, cell);
87 LogError(code, "Failed local realm check; name=%s, inst=%s, cell=%s\n",
94 convert_cell_to_ubik(struct afsconf_cell *cellinfo, afs_uint32 *myHost,
95 afs_uint32 *serverList)
102 gethostname(hostname, sizeof(hostname));
103 th = gethostbyname(hostname);
105 printf("prserver: couldn't get address of this host.\n");
108 memcpy(myHost, th->h_addr, sizeof(afs_uint32));
110 for (i = 0; i < cellinfo->numServers; i++)
111 /* omit my host from serverList */
112 if (cellinfo->hostAddr[i].sin_addr.s_addr != *myHost)
113 *serverList++ = cellinfo->hostAddr[i].sin_addr.s_addr;
115 *serverList = 0; /* terminate list */
120 * The whole purpose of MyBeforeProc is to detect
121 * if the -help option was not within the command line.
122 * If it were, this routine would never have been called.
125 MyBeforeProc(struct cmd_syndesc *as, void *arock)
131 /* initializeCommands
132 * initialize all the supported commands and their arguments
136 initializeArgHandler(void)
138 struct cmd_syndesc *cptr;
140 cmd_SetBeforeProc(MyBeforeProc, NULL);
142 cptr = cmd_CreateSyntax(NULL, argHandler, NULL, "Backup database server");
144 cmd_AddParm(cptr, "-database", CMD_SINGLE, CMD_OPTIONAL,
145 "database directory");
147 cmd_AddParm(cptr, "-cellservdb", CMD_SINGLE, CMD_OPTIONAL,
148 "cell configuration directory");
150 cmd_AddParm(cptr, "-resetdb", CMD_FLAG, CMD_OPTIONAL,
151 "truncate the database");
153 cmd_AddParm(cptr, "-noauth", CMD_FLAG, CMD_OPTIONAL,
154 "run without authentication");
156 cmd_AddParm(cptr, "-smallht", CMD_FLAG, CMD_OPTIONAL,
157 "use small hash tables");
159 cmd_AddParm(cptr, "-servers", CMD_LIST, CMD_OPTIONAL,
160 "list of ubik database servers");
162 cmd_AddParm(cptr, "-ubikbuffers", CMD_SINGLE, CMD_OPTIONAL,
163 "the number of ubik buffers");
165 cmd_AddParm(cptr, "-auditlog", CMD_SINGLE, CMD_OPTIONAL,
168 cmd_AddParm(cptr, "-p", CMD_SINGLE, CMD_OPTIONAL,
169 "number of processes");
171 cmd_AddParm(cptr, "-rxbind", CMD_FLAG, CMD_OPTIONAL,
172 "bind the Rx socket (primary interface only)");
174 cmd_AddParm(cptr, "-audit-interface", CMD_SINGLE, CMD_OPTIONAL,
175 "audit interface (file or sysvmq)");
179 argHandler(struct cmd_syndesc *as, void *arock)
182 /* globalConfPtr provides the handle for the configuration information */
184 /* database directory */
185 if (as->parms[0].items != 0) {
186 globalConfPtr->databaseDirectory = strdup(as->parms[0].items->data);
187 if (globalConfPtr->databaseDirectory == 0)
191 /* -cellservdb, cell configuration directory */
192 if (as->parms[1].items != 0) {
193 globalConfPtr->cellConfigdir = strdup(as->parms[1].items->data);
194 if (globalConfPtr->cellConfigdir == 0)
197 globalConfPtr->debugFlags |= DF_RECHECKNOAUTH;
200 /* truncate the database */
201 if (as->parms[2].items != 0)
204 /* run without authentication */
205 if (as->parms[3].items != 0)
206 globalConfPtr->debugFlags |= DF_NOAUTH;
208 /* use small hash tables */
209 if (as->parms[4].items != 0)
210 globalConfPtr->debugFlags |= DF_SMALLHT;
212 /* user provided list of ubik database servers */
213 if (as->parms[5].items != 0)
214 parseServerList(as->parms[5].items);
216 /* user provided the number of ubik buffers */
217 if (as->parms[6].items != 0)
218 ubik_nBuffers = atoi(as->parms[6].items->data);
222 /* param 7 (-auditlog) handled below */
224 /* user provided the number of threads */
225 if (as->parms[8].items != 0) {
226 lwps = atoi(as->parms[8].items->data);
228 printf ("Warning: '-p %d' is too big; using %d instead\n",
233 printf ("Warning: '-p %d' is too small; using %d instead\n",
239 /* user provided rxbind option */
240 if (as->parms[9].items != 0) {
244 /* -audit-interface */
245 if (as->parms[10].items != 0) {
246 char *interface = as->parms[10].items->data;
248 if (osi_audit_interface(interface)) {
249 printf("Invalid audit interface '%s'\n", interface);
255 /* needs to be after -audit-interface, so we osi_audit_interface
256 * before we osi_audit_file */
257 if (as->parms[7].items != 0) {
258 char *fileName = as->parms[7].items->data;
260 osi_audit_file(fileName);
269 parseServerList(struct cmd_item *itemPtr)
271 struct cmd_item *save;
274 afs_int32 nservers = 0;
279 /* compute number of servers in the list */
282 itemPtr = itemPtr->next;
285 LogDebug(3, "%d servers\n", nservers);
287 /* now can allocate the space for the server arguments */
288 serverArgs = (char **)malloc((nservers + 2) * sizeof(char *));
296 /* now go through and construct the list of servers */
299 *ptr++ = itemPtr->data;
300 itemPtr = itemPtr->next;
304 ubik_ParseServerList(nservers + 2, serverArgs, &globalConfPtr->myHost,
305 globalConfPtr->serverList);
309 /* free space for the server args */
310 free((char *)serverArgs);
317 * truncates just the database file.
321 truncateDatabase(void)
327 asprintf(&path, "%s%s%s",
328 globalConfPtr->databaseDirectory,
329 globalConfPtr->databaseName,
330 globalConfPtr->databaseExtension);
334 fd = open(path, O_RDWR, 0755);
338 if (ftruncate(fd, 0) != 0) {
353 #include "AFS_component_version_number.c"
356 main(int argc, char **argv)
358 char *whoami = argv[0];
360 struct afsconf_cell cellinfo;
363 afs_uint32 host = ntohl(INADDR_ANY);
365 char clones[MAXHOSTSPERCELL];
367 struct rx_service *tservice;
368 struct rx_securityClass **securityClasses;
369 afs_int32 numClasses;
371 extern int rx_stackSize;
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( rx_GetThreadNum );
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 audit user check */
493 osi_audit_set_user_check(BU_conf, BU_IsLocalRealmMatch);
495 /* initialize ubik */
496 ubik_SetClientSecurityProcs(afsconf_ClientAuth, afsconf_UpToDate, BU_conf);
497 ubik_SetServerSecurityProcs(afsconf_BuildServerSecurityObjects,
498 afsconf_CheckAuth, BU_conf);
500 if (ubik_nBuffers == 0)
503 LogError(0, "Will allocate %d ubik buffers\n", ubik_nBuffers);
505 asprintf(&dbNamePtr, "%s%s", globalConfPtr->databaseDirectory,
506 globalConfPtr->databaseName);
510 rx_SetRxDeadTime(60); /* 60 seconds inactive before timeout */
514 if (AFSDIR_SERVER_NETRESTRICT_FILEPATH ||
515 AFSDIR_SERVER_NETINFO_FILEPATH) {
517 ccode = parseNetFiles(SHostAddrs, NULL, NULL,
518 ADDRSPERSITE, reason,
519 AFSDIR_SERVER_NETINFO_FILEPATH,
520 AFSDIR_SERVER_NETRESTRICT_FILEPATH);
523 ccode = rx_getAllAddr(SHostAddrs, ADDRSPERSITE);
526 host = SHostAddrs[0];
527 rx_InitHost(host, htons(AFSCONF_BUDBPORT));
531 /* Disable jumbograms */
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 afsconf_BuildServerSecurityObjects(BU_conf, &securityClasses, &numClasses);
550 rx_NewServiceHost(host, 0, BUDB_SERVICE, "BackupDatabase",
551 securityClasses, numClasses, BUDB_ExecuteRequest);
553 if (tservice == (struct rx_service *)0) {
554 LogError(0, "Could not create backup database rx service\n");
555 printf("Could not create backup database rx service\n");
558 rx_SetMinProcs(tservice, 1);
559 rx_SetMaxProcs(tservice, lwps);
560 rx_SetStackSize(tservice, 10000);
562 /* allow super users to manage RX statistics */
563 rx_SetRxStatUserOk(BU_rxstat_userok);
565 /* misc. initialization */
567 /* database dump synchronization */
568 memset(dumpSyncPtr, 0, sizeof(*dumpSyncPtr));
569 Lock_Init(&dumpSyncPtr->ds_lock);
571 rx_StartServer(0); /* start handling requests */
578 currentTime = time(0);
579 LogError(0, "Ready to process requests at %s\n", ctime(¤tTime));
581 rx_ServerProc(NULL); /* donate this LWP */
584 osi_audit(BUDB_FinishEvent, code, AUD_END);
589 consistencyCheckDb(void)
591 /* do consistency checks on structure sizes */
592 if ((sizeof(struct htBlock) > BLOCKSIZE)
593 || (sizeof(struct vfBlock) > BLOCKSIZE)
594 || (sizeof(struct viBlock) > BLOCKSIZE)
595 || (sizeof(struct dBlock) > BLOCKSIZE)
596 || (sizeof(struct tBlock) > BLOCKSIZE)
598 fprintf(stderr, "Block layout error!\n");
604 LogDebug(int level, char *fmt, ... )
610 if (debugging >= level) {
611 /* log normally closed so can remove it */
612 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
613 if (globalConfPtr->log != NULL) {
614 vfprintf(globalConfPtr->log, fmt, ap);
615 fflush(globalConfPtr->log);
616 fclose(globalConfPtr->log);
626 static char timestamp[20];
629 strftime(timestamp, 20, "%m/%d/%Y %H:%M:%S", lt);
640 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
641 if (globalConfPtr->log != NULL) {
643 fprintf(globalConfPtr->log, "%s ", TimeStamp(now));
645 vfprintf(globalConfPtr->log, fmt, ap);
646 fflush(globalConfPtr->log);
647 fclose(globalConfPtr->log);
653 LogError(long code, char *fmt, ... )
659 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
661 if (globalConfPtr->log != NULL) {
663 fprintf(globalConfPtr->log, "%s ", TimeStamp(now));
666 fprintf(globalConfPtr->log, "%s: %s\n", afs_error_table_name(code),
667 afs_error_message(code));
668 vfprintf(globalConfPtr->log, fmt, ap );
669 fflush(globalConfPtr->log);
670 fclose(globalConfPtr->log);
681 LogNetDump(struct dump *dumpPtr)
683 struct dump hostDump;
684 extern buServerConfP globalConfPtr;
686 dump_ntoh(dumpPtr, &hostDump);
688 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
689 if (globalConfPtr->log != NULL) {
690 printDump(globalConfPtr->log, &hostDump);
691 fclose(globalConfPtr->log);