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 =
187 (char *)malloc(strlen(as->parms[0].items->data) + 1);
188 if (globalConfPtr->databaseDirectory == 0)
190 strcpy(globalConfPtr->databaseDirectory, as->parms[0].items->data);
193 /* -cellservdb, cell configuration directory */
194 if (as->parms[1].items != 0) {
195 globalConfPtr->cellConfigdir =
196 (char *)malloc(strlen(as->parms[1].items->data) + 1);
197 if (globalConfPtr->cellConfigdir == 0)
200 strcpy(globalConfPtr->cellConfigdir, as->parms[1].items->data);
202 globalConfPtr->debugFlags |= DF_RECHECKNOAUTH;
205 /* truncate the database */
206 if (as->parms[2].items != 0)
209 /* run without authentication */
210 if (as->parms[3].items != 0)
211 globalConfPtr->debugFlags |= DF_NOAUTH;
213 /* use small hash tables */
214 if (as->parms[4].items != 0)
215 globalConfPtr->debugFlags |= DF_SMALLHT;
217 /* user provided list of ubik database servers */
218 if (as->parms[5].items != 0)
219 parseServerList(as->parms[5].items);
221 /* user provided the number of ubik buffers */
222 if (as->parms[6].items != 0)
223 ubik_nBuffers = atoi(as->parms[6].items->data);
227 /* param 7 (-auditlog) handled below */
229 /* user provided the number of threads */
230 if (as->parms[8].items != 0) {
231 lwps = atoi(as->parms[8].items->data);
233 printf ("Warning: '-p %d' is too big; using %d instead\n",
238 printf ("Warning: '-p %d' is too small; using %d instead\n",
244 /* user provided rxbind option */
245 if (as->parms[9].items != 0) {
249 /* -audit-interface */
250 if (as->parms[10].items != 0) {
251 char *interface = as->parms[10].items->data;
253 if (osi_audit_interface(interface)) {
254 printf("Invalid audit interface '%s'\n", interface);
260 /* needs to be after -audit-interface, so we osi_audit_interface
261 * before we osi_audit_file */
262 if (as->parms[7].items != 0) {
263 char *fileName = as->parms[7].items->data;
265 osi_audit_file(fileName);
274 parseServerList(struct cmd_item *itemPtr)
276 struct cmd_item *save;
279 afs_int32 nservers = 0;
284 /* compute number of servers in the list */
287 itemPtr = itemPtr->next;
290 LogDebug(3, "%d servers\n", nservers);
292 /* now can allocate the space for the server arguments */
293 serverArgs = (char **)malloc((nservers + 2) * sizeof(char *));
301 /* now go through and construct the list of servers */
304 *ptr++ = itemPtr->data;
305 itemPtr = itemPtr->next;
309 ubik_ParseServerList(nservers + 2, serverArgs, &globalConfPtr->myHost,
310 globalConfPtr->serverList);
314 /* free space for the server args */
315 free((char *)serverArgs);
322 * truncates just the database file.
326 truncateDatabase(void)
332 path = malloc(strlen(globalConfPtr->databaseDirectory) +
333 strlen(globalConfPtr->databaseName) +
334 strlen(globalConfPtr->databaseExtension) + 1);
338 /* construct the database name */
339 strcpy(path, globalConfPtr->databaseDirectory);
340 strcat(path, globalConfPtr->databaseName);
341 strcat(path, globalConfPtr->databaseExtension);
343 fd = open(path, O_RDWR, 0755);
347 if (ftruncate(fd, 0) != 0) {
362 #include "AFS_component_version_number.c"
365 main(int argc, char **argv)
367 char *whoami = argv[0];
369 struct afsconf_cell cellinfo;
372 afs_uint32 host = ntohl(INADDR_ANY);
374 char clones[MAXHOSTSPERCELL];
376 struct rx_service *tservice;
377 struct rx_securityClass **securityClasses;
378 afs_int32 numClasses;
380 extern int rx_stackSize;
383 /* initialize winsock */
384 if (afs_winsockInit() < 0) {
385 ReportErrorEventAlt(AFSEVT_SVR_WINSOCK_INIT_FAILED, 0, argv[0], 0);
386 fprintf(stderr, "%s: Couldn't initialize winsock.\n", whoami);
393 * The following signal action for AIX is necessary so that in case of a
394 * crash (i.e. core is generated) we can include the user's data section
395 * in the core dump. Unfortunately, by default, only a partial core is
396 * generated which, in many cases, isn't too useful.
398 struct sigaction nsa;
400 sigemptyset(&nsa.sa_mask);
401 nsa.sa_handler = SIG_DFL;
402 nsa.sa_flags = SA_FULLDUMP;
403 sigaction(SIGSEGV, &nsa, NULL);
404 sigaction(SIGABRT, &nsa, NULL);
407 osi_audit(BUDB_StartEvent, 0, AUD_END);
409 initialize_BUDB_error_table();
410 initializeArgHandler();
412 /* Initialize dirpaths */
413 if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
415 ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
417 afs_com_err(whoami, errno, "; Unable to obtain AFS server directory.");
421 memset(globalConfPtr, 0, sizeof(*globalConfPtr));
423 /* set default configuration values */
424 strcpy(dbDir, AFSDIR_SERVER_DB_DIRPATH);
426 globalConfPtr->databaseDirectory = dbDir;
427 globalConfPtr->databaseName = DEFAULT_DBPREFIX;
428 strcpy(cellConfDir, AFSDIR_SERVER_ETC_DIRPATH);
429 globalConfPtr->cellConfigdir = cellConfDir;
431 /* open the log file */
433 globalConfPtr->log = fopen(DEFAULT_LOGNAME,"a");
434 if ( globalConfPtr->log == NULL )
436 printf("Can't open log file %s - aborting\n", DEFAULT_LOGNAME);
443 #ifdef AFS_PTHREAD_ENV
444 SetLogThreadNumProgram( rx_GetThreadNum );
447 /* process the user supplied args */
449 code = cmd_Dispatch(argc, argv);
453 /* exit if there was a help option */
457 /* open the log file */
458 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
459 if (globalConfPtr->log == NULL) {
460 printf("Can't open log file %s - aborting\n",
461 AFSDIR_SERVER_BUDBLOG_FILEPATH);
465 /* keep log closed so can remove it */
467 fclose(globalConfPtr->log);
469 /* open the cell's configuration directory */
470 LogDebug(4, "opening %s\n", globalConfPtr->cellConfigdir);
472 BU_conf = afsconf_Open(globalConfPtr->cellConfigdir);
474 LogError(code, "Failed getting cell info\n");
475 afs_com_err(whoami, code, "Failed getting cell info");
479 code = afsconf_GetLocalCell(BU_conf, lcell, sizeof(lcell));
481 LogError(0, "** Can't determine local cell name!\n");
485 if (globalConfPtr->myHost == 0) {
486 /* if user hasn't supplied a list of servers, extract server
487 * list from the cell's database
490 LogDebug(1, "Using server list from %s cell database.\n", lcell);
492 code = afsconf_GetExtendedCellInfo (BU_conf, lcell, 0, &cellinfo,
495 convert_cell_to_ubik(&cellinfo, &globalConfPtr->myHost,
496 globalConfPtr->serverList);
501 /* initialize audit user check */
502 osi_audit_set_user_check(BU_conf, BU_IsLocalRealmMatch);
504 /* initialize ubik */
505 ubik_SetClientSecurityProcs(afsconf_ClientAuth, afsconf_UpToDate, BU_conf);
506 ubik_SetServerSecurityProcs(afsconf_BuildServerSecurityObjects,
507 afsconf_CheckAuth, BU_conf);
509 if (ubik_nBuffers == 0)
512 LogError(0, "Will allocate %d ubik buffers\n", ubik_nBuffers);
515 (char *)malloc(strlen(globalConfPtr->databaseDirectory) +
516 strlen(globalConfPtr->databaseName) + 1);
520 /* construct the database name */
521 strcpy(dbNamePtr, globalConfPtr->databaseDirectory);
522 strcat(dbNamePtr, globalConfPtr->databaseName); /* name prefix */
524 rx_SetRxDeadTime(60); /* 60 seconds inactive before timeout */
528 if (AFSDIR_SERVER_NETRESTRICT_FILEPATH ||
529 AFSDIR_SERVER_NETINFO_FILEPATH) {
531 ccode = parseNetFiles(SHostAddrs, NULL, NULL,
532 ADDRSPERSITE, reason,
533 AFSDIR_SERVER_NETINFO_FILEPATH,
534 AFSDIR_SERVER_NETRESTRICT_FILEPATH);
537 ccode = rx_getAllAddr(SHostAddrs, ADDRSPERSITE);
540 host = SHostAddrs[0];
541 rx_InitHost(host, htons(AFSCONF_BUDBPORT));
545 code = ubik_ServerInitByInfo (globalConfPtr->myHost,
546 htons(AFSCONF_BUDBPORT),
549 dbNamePtr, /* name prefix */
553 LogError(code, "Ubik init failed\n");
554 afs_com_err(whoami, code, "Ubik init failed");
558 afsconf_BuildServerSecurityObjects(BU_conf, &securityClasses, &numClasses);
560 /* Disable jumbograms */
564 rx_NewServiceHost(host, 0, BUDB_SERVICE, "BackupDatabase",
565 securityClasses, numClasses, BUDB_ExecuteRequest);
567 if (tservice == (struct rx_service *)0) {
568 LogError(0, "Could not create backup database rx service\n");
569 printf("Could not create backup database rx service\n");
572 rx_SetMinProcs(tservice, 1);
573 rx_SetMaxProcs(tservice, lwps);
574 rx_SetStackSize(tservice, 10000);
576 /* allow super users to manage RX statistics */
577 rx_SetRxStatUserOk(BU_rxstat_userok);
579 /* misc. initialization */
581 /* database dump synchronization */
582 memset(dumpSyncPtr, 0, sizeof(*dumpSyncPtr));
583 Lock_Init(&dumpSyncPtr->ds_lock);
585 rx_StartServer(0); /* start handling requests */
592 currentTime = time(0);
593 LogError(0, "Ready to process requests at %s\n", ctime(¤tTime));
595 rx_ServerProc(NULL); /* donate this LWP */
598 osi_audit(BUDB_FinishEvent, code, AUD_END);
603 consistencyCheckDb(void)
605 /* do consistency checks on structure sizes */
606 if ((sizeof(struct htBlock) > BLOCKSIZE)
607 || (sizeof(struct vfBlock) > BLOCKSIZE)
608 || (sizeof(struct viBlock) > BLOCKSIZE)
609 || (sizeof(struct dBlock) > BLOCKSIZE)
610 || (sizeof(struct tBlock) > BLOCKSIZE)
612 fprintf(stderr, "Block layout error!\n");
618 LogDebug(int level, char *fmt, ... )
624 if (debugging >= level) {
625 /* log normally closed so can remove it */
626 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
627 if (globalConfPtr->log != NULL) {
628 vfprintf(globalConfPtr->log, fmt, ap);
629 fflush(globalConfPtr->log);
630 fclose(globalConfPtr->log);
640 static char timestamp[20];
643 strftime(timestamp, 20, "%m/%d/%Y %H:%M:%S", lt);
654 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
655 if (globalConfPtr->log != NULL) {
657 fprintf(globalConfPtr->log, "%s ", TimeStamp(now));
659 vfprintf(globalConfPtr->log, fmt, ap);
660 fflush(globalConfPtr->log);
661 fclose(globalConfPtr->log);
667 LogError(long code, char *fmt, ... )
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 vfprintf(globalConfPtr->log, fmt, ap );
683 fflush(globalConfPtr->log);
684 fclose(globalConfPtr->log);
695 LogNetDump(struct dump *dumpPtr)
697 struct dump hostDump;
698 extern buServerConfP globalConfPtr;
700 dump_ntoh(dumpPtr, &hostDump);
702 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
703 if (globalConfPtr->log != NULL) {
704 printDump(globalConfPtr->log, &hostDump);
705 fclose(globalConfPtr->log);