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];
60 afs_uint32 myHost = 0;
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 (intptr_t)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_uint32 *myHost,
99 afs_uint32 *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_uint32));
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(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 **securityClasses;
381 afs_int32 numClasses;
383 extern int rx_stackSize;
386 /* initialize winsock */
387 if (afs_winsockInit() < 0) {
388 ReportErrorEventAlt(AFSEVT_SVR_WINSOCK_INIT_FAILED, 0, argv[0], 0);
389 fprintf(stderr, "%s: Couldn't initialize winsock.\n", whoami);
396 * The following signal action for AIX is necessary so that in case of a
397 * crash (i.e. core is generated) we can include the user's data section
398 * in the core dump. Unfortunately, by default, only a partial core is
399 * generated which, in many cases, isn't too useful.
401 struct sigaction nsa;
403 sigemptyset(&nsa.sa_mask);
404 nsa.sa_handler = SIG_DFL;
405 nsa.sa_flags = SA_FULLDUMP;
406 sigaction(SIGSEGV, &nsa, NULL);
407 sigaction(SIGABRT, &nsa, NULL);
410 osi_audit(BUDB_StartEvent, 0, AUD_END);
412 initialize_BUDB_error_table();
413 initializeArgHandler();
415 /* Initialize dirpaths */
416 if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
418 ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
420 afs_com_err(whoami, errno, "; Unable to obtain AFS server directory.");
424 memset(globalConfPtr, 0, sizeof(*globalConfPtr));
426 /* set default configuration values */
427 strcpy(dbDir, AFSDIR_SERVER_DB_DIRPATH);
429 globalConfPtr->databaseDirectory = dbDir;
430 globalConfPtr->databaseName = DEFAULT_DBPREFIX;
431 strcpy(cellConfDir, AFSDIR_SERVER_ETC_DIRPATH);
432 globalConfPtr->cellConfigdir = cellConfDir;
434 /* open the log file */
436 globalConfPtr->log = fopen(DEFAULT_LOGNAME,"a");
437 if ( globalConfPtr->log == NULL )
439 printf("Can't open log file %s - aborting\n", DEFAULT_LOGNAME);
446 #ifdef AFS_PTHREAD_ENV
447 SetLogThreadNumProgram( threadNum );
450 /* process the user supplied args */
452 code = cmd_Dispatch(argc, argv);
456 /* exit if there was a help option */
460 /* open the log file */
461 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
462 if (globalConfPtr->log == NULL) {
463 printf("Can't open log file %s - aborting\n",
464 AFSDIR_SERVER_BUDBLOG_FILEPATH);
468 /* keep log closed so can remove it */
470 fclose(globalConfPtr->log);
472 /* open the cell's configuration directory */
473 LogDebug(4, "opening %s\n", globalConfPtr->cellConfigdir);
475 BU_conf = afsconf_Open(globalConfPtr->cellConfigdir);
477 LogError(code, "Failed getting cell info\n");
478 afs_com_err(whoami, code, "Failed getting cell info");
482 code = afsconf_GetLocalCell(BU_conf, lcell, sizeof(lcell));
484 LogError(0, "** Can't determine local cell name!\n");
488 if (globalConfPtr->myHost == 0) {
489 /* if user hasn't supplied a list of servers, extract server
490 * list from the cell's database
493 LogDebug(1, "Using server list from %s cell database.\n", lcell);
495 code = afsconf_GetExtendedCellInfo (BU_conf, lcell, 0, &cellinfo,
498 convert_cell_to_ubik(&cellinfo, &globalConfPtr->myHost,
499 globalConfPtr->serverList);
504 /* initialize ubik */
505 ubik_CRXSecurityProc = afsconf_ClientAuth;
506 ubik_CRXSecurityRock = BU_conf;
508 ubik_SRXSecurityProc = afsconf_ServerAuth;
509 ubik_SRXSecurityRock = BU_conf;
511 ubik_CheckRXSecurityProc = afsconf_CheckAuth;
512 ubik_CheckRXSecurityRock = BU_conf;
514 if (ubik_nBuffers == 0)
517 LogError(0, "Will allocate %d ubik buffers\n", ubik_nBuffers);
520 (char *)malloc(strlen(globalConfPtr->databaseDirectory) +
521 strlen(globalConfPtr->databaseName) + 1);
525 /* construct the database name */
526 strcpy(dbNamePtr, globalConfPtr->databaseDirectory);
527 strcat(dbNamePtr, globalConfPtr->databaseName); /* name prefix */
529 rx_SetRxDeadTime(60); /* 60 seconds inactive before timeout */
533 if (AFSDIR_SERVER_NETRESTRICT_FILEPATH ||
534 AFSDIR_SERVER_NETINFO_FILEPATH) {
536 ccode = parseNetFiles(SHostAddrs, NULL, NULL,
537 ADDRSPERSITE, reason,
538 AFSDIR_SERVER_NETINFO_FILEPATH,
539 AFSDIR_SERVER_NETRESTRICT_FILEPATH);
542 ccode = rx_getAllAddr(SHostAddrs, ADDRSPERSITE);
545 host = SHostAddrs[0];
546 rx_InitHost(host, htons(AFSCONF_BUDBPORT));
550 code = ubik_ServerInitByInfo (globalConfPtr->myHost,
551 htons(AFSCONF_BUDBPORT),
554 dbNamePtr, /* name prefix */
558 LogError(code, "Ubik init failed\n");
559 afs_com_err(whoami, code, "Ubik init failed");
563 afsconf_BuildServerSecurityObjects(BU_conf, 0,
564 &securityClasses, &numClasses);
566 /* Disable jumbograms */
570 rx_NewServiceHost(host, 0, BUDB_SERVICE, "BackupDatabase",
571 securityClasses, numClasses, BUDB_ExecuteRequest);
573 if (tservice == (struct rx_service *)0) {
574 LogError(0, "Could not create backup database rx service\n");
575 printf("Could not create backup database rx service\n");
578 rx_SetMinProcs(tservice, 1);
579 rx_SetMaxProcs(tservice, lwps);
580 rx_SetStackSize(tservice, 10000);
582 /* allow super users to manage RX statistics */
583 rx_SetRxStatUserOk(BU_rxstat_userok);
585 /* misc. initialization */
587 /* database dump synchronization */
588 memset(dumpSyncPtr, 0, sizeof(*dumpSyncPtr));
589 Lock_Init(&dumpSyncPtr->ds_lock);
591 rx_StartServer(0); /* start handling requests */
598 currentTime = time(0);
599 LogError(0, "Ready to process requests at %s\n", ctime(¤tTime));
601 rx_ServerProc(NULL); /* donate this LWP */
604 osi_audit(BUDB_FinishEvent, code, AUD_END);
609 consistencyCheckDb(void)
611 /* do consistency checks on structure sizes */
612 if ((sizeof(struct htBlock) > BLOCKSIZE)
613 || (sizeof(struct vfBlock) > BLOCKSIZE)
614 || (sizeof(struct viBlock) > BLOCKSIZE)
615 || (sizeof(struct dBlock) > BLOCKSIZE)
616 || (sizeof(struct tBlock) > BLOCKSIZE)
618 fprintf(stderr, "Block layout error!\n");
624 LogDebug(int level, char *fmt, ... )
630 if (debugging >= level) {
631 /* log normally closed so can remove it */
632 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
633 if (globalConfPtr->log != NULL) {
634 vfprintf(globalConfPtr->log, fmt, ap);
635 fflush(globalConfPtr->log);
636 fclose(globalConfPtr->log);
646 static char timestamp[20];
649 strftime(timestamp, 20, "%m/%d/%Y %H:%M:%S", lt);
660 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
661 if (globalConfPtr->log != NULL) {
663 fprintf(globalConfPtr->log, "%s ", TimeStamp(now));
665 vfprintf(globalConfPtr->log, fmt, ap);
666 fflush(globalConfPtr->log);
667 fclose(globalConfPtr->log);
673 LogError(long code, char *fmt, ... )
679 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
681 if (globalConfPtr->log != NULL) {
683 fprintf(globalConfPtr->log, "%s ", TimeStamp(now));
686 fprintf(globalConfPtr->log, "%s: %s\n", afs_error_table_name(code),
687 afs_error_message(code));
688 vfprintf(globalConfPtr->log, fmt, ap );
689 fflush(globalConfPtr->log);
690 fclose(globalConfPtr->log);
701 LogNetDump(struct dump *dumpPtr)
703 struct dump hostDump;
704 extern buServerConfP globalConfPtr;
706 dump_ntoh(dumpPtr, &hostDump);
708 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
709 if (globalConfPtr->log != NULL) {
710 printDump(globalConfPtr->log, &hostDump);
711 fclose(globalConfPtr->log);