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>
20 #include <WINNT/afsevent.h>
22 #include <netinet/in.h>
29 #include <sys/types.h>
38 #include <rx/rx_globals.h>
39 #include <afs/cellconfig.h>
40 #include <afs/bubasics.h>
41 #include <afs/afsutil.h>
42 #include <afs/com_err.h>
47 #include "budb_errs.h"
49 #include "error_macros.h"
51 #include "afs/audit.h"
53 struct ubik_dbase *BU_dbase;
54 struct afsconf_dir *BU_conf; /* for getting cell info */
56 int argHandler(struct cmd_syndesc *, void *);
58 char lcell[MAXKTCREALMLEN];
62 /* server's global configuration information. This is exported to other
66 buServerConfT globalConf;
67 buServerConfP globalConfPtr = &globalConf;
68 char dbDir[AFSDIR_PATH_MAX], cellConfDir[AFSDIR_PATH_MAX];
69 /* debugging control */
78 #define ADDRSPERSITE 16 /* Same global is in rx/rx_user.c */
79 afs_uint32 SHostAddrs[ADDRSPERSITE];
81 #if defined(AFS_PTHREAD_ENV)
85 return pthread_getspecific(rx_thread_id_key);
89 /* check whether caller is authorized to manage RX statistics */
91 BU_rxstat_userok(call)
94 return afsconf_SuperUser(BU_conf, call, NULL);
98 convert_cell_to_ubik(cellinfo, myHost, serverList)
99 struct afsconf_cell *cellinfo;
101 afs_int32 *serverList;
108 gethostname(hostname, sizeof(hostname));
109 th = gethostbyname(hostname);
111 printf("prserver: couldn't get address of this host.\n");
114 memcpy(myHost, th->h_addr, sizeof(afs_int32));
116 for (i = 0; i < cellinfo->numServers; i++)
117 /* omit my host from serverList */
118 if (cellinfo->hostAddr[i].sin_addr.s_addr != *myHost)
119 *serverList++ = cellinfo->hostAddr[i].sin_addr.s_addr;
121 *serverList = 0; /* terminate list */
126 * The whole purpose of MyBeforeProc is to detect
127 * if the -help option was not within the command line.
128 * If it were, this routine would never have been called.
131 MyBeforeProc(register struct cmd_syndesc *as, void *arock)
137 /* initializeCommands
138 * initialize all the supported commands and their arguments
142 initializeArgHandler()
144 struct cmd_syndesc *cptr;
146 cmd_SetBeforeProc(MyBeforeProc, NULL);
148 cptr = cmd_CreateSyntax(NULL, argHandler, NULL, "Backup database server");
150 cmd_AddParm(cptr, "-database", CMD_SINGLE, CMD_OPTIONAL,
151 "database directory");
153 cmd_AddParm(cptr, "-cellservdb", CMD_SINGLE, CMD_OPTIONAL,
154 "cell configuration directory");
156 cmd_AddParm(cptr, "-resetdb", CMD_FLAG, CMD_OPTIONAL,
157 "truncate the database");
159 cmd_AddParm(cptr, "-noauth", CMD_FLAG, CMD_OPTIONAL,
160 "run without authentication");
162 cmd_AddParm(cptr, "-smallht", CMD_FLAG, CMD_OPTIONAL,
163 "use small hash tables");
165 cmd_AddParm(cptr, "-servers", CMD_LIST, CMD_OPTIONAL,
166 "list of ubik database servers");
168 cmd_AddParm(cptr, "-ubikbuffers", CMD_SINGLE, CMD_OPTIONAL,
169 "the number of ubik buffers");
171 cmd_AddParm(cptr, "-auditlog", CMD_SINGLE, CMD_OPTIONAL,
174 cmd_AddParm(cptr, "-p", CMD_SINGLE, CMD_OPTIONAL,
175 "number of processes");
177 cmd_AddParm(cptr, "-rxbind", CMD_FLAG, CMD_OPTIONAL,
178 "bind the Rx socket (primary interface only)");
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 if (as->parms[7].items != 0) {
234 char oldName[MAXPATHLEN];
235 char *fileName = as->parms[7].items->data;
239 if ((lstat(fileName, &statbuf) == 0)
240 && (S_ISFIFO(statbuf.st_mode))) {
241 flags = O_WRONLY | O_NONBLOCK;
245 strcpy(oldName, fileName);
246 strcat(oldName, ".old");
247 renamefile(fileName, oldName);
248 flags = O_WRONLY | O_TRUNC | O_CREAT;
250 tempfd = open(fileName, flags, 0666);
252 auditout = fdopen(tempfd, "a");
254 osi_audit_file(auditout);
256 printf("Warning: auditlog %s not writable, ignored.\n", fileName);
258 printf("Warning: auditlog %s not writable, ignored.\n", fileName);
261 /* user provided the number of threads */
262 if (as->parms[8].items != 0) {
263 lwps = atoi(as->parms[8].items->data);
265 printf ("Warning: '-p %d' is too big; using %d instead\n",
270 printf ("Warning: '-p %d' is too small; using %d instead\n",
276 /* user provided rxbind option */
277 if (as->parms[9].items != 0) {
286 parseServerList(itemPtr)
287 struct cmd_item *itemPtr;
289 struct cmd_item *save;
292 afs_int32 nservers = 0;
297 /* compute number of servers in the list */
300 itemPtr = itemPtr->next;
303 LogDebug(3, "%d servers\n", nservers);
305 /* now can allocate the space for the server arguments */
306 serverArgs = (char **)malloc((nservers + 2) * sizeof(char *));
314 /* now go through and construct the list of servers */
317 *ptr++ = itemPtr->data;
318 itemPtr = itemPtr->next;
322 ubik_ParseServerList(nservers + 2, serverArgs, &globalConfPtr->myHost,
323 globalConfPtr->serverList);
327 /* free space for the server args */
328 free((char *)serverArgs);
335 * truncates just the database file.
345 (char *)malloc(strlen(globalConfPtr->databaseDirectory) +
346 strlen(globalConfPtr->databaseName) +
347 strlen(globalConfPtr->databaseExtension) + 1);
351 /* construct the database name */
352 strcpy(path, globalConfPtr->databaseDirectory);
353 strcat(path, globalConfPtr->databaseName);
354 strcat(path, globalConfPtr->databaseExtension);
356 fd = open(path, O_RDWR, 0755);
360 if (ftruncate(fd, 0) != 0) {
373 #include "AFS_component_version_number.c"
379 char *whoami = argv[0];
381 struct afsconf_cell cellinfo;
384 afs_uint32 host = ntohl(INADDR_ANY);
386 char clones[MAXHOSTSPERCELL];
388 struct rx_service *tservice;
389 struct rx_securityClass *sca[3];
391 extern int afsconf_ServerAuth();
392 extern int afsconf_CheckAuth();
394 extern int rx_stackSize;
395 extern int BUDB_ExecuteRequest();
398 /* initialize winsock */
399 if (afs_winsockInit() < 0) {
400 ReportErrorEventAlt(AFSEVT_SVR_WINSOCK_INIT_FAILED, 0, argv[0], 0);
401 fprintf(stderr, "%s: Couldn't initialize winsock.\n", whoami);
408 * The following signal action for AIX is necessary so that in case of a
409 * crash (i.e. core is generated) we can include the user's data section
410 * in the core dump. Unfortunately, by default, only a partial core is
411 * generated which, in many cases, isn't too useful.
413 struct sigaction nsa;
415 sigemptyset(&nsa.sa_mask);
416 nsa.sa_handler = SIG_DFL;
417 nsa.sa_flags = SA_FULLDUMP;
418 sigaction(SIGSEGV, &nsa, NULL);
419 sigaction(SIGABRT, &nsa, NULL);
422 osi_audit(BUDB_StartEvent, 0, AUD_END);
424 initialize_BUDB_error_table();
425 initializeArgHandler();
427 /* Initialize dirpaths */
428 if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
430 ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
432 afs_com_err(whoami, errno, "; Unable to obtain AFS server directory.");
436 memset(globalConfPtr, 0, sizeof(*globalConfPtr));
438 /* set default configuration values */
439 strcpy(dbDir, AFSDIR_SERVER_DB_DIRPATH);
441 globalConfPtr->databaseDirectory = dbDir;
442 globalConfPtr->databaseName = DEFAULT_DBPREFIX;
443 strcpy(cellConfDir, AFSDIR_SERVER_ETC_DIRPATH);
444 globalConfPtr->cellConfigdir = cellConfDir;
446 /* open the log file */
448 globalConfPtr->log = fopen(DEFAULT_LOGNAME,"a");
449 if ( globalConfPtr->log == NULL )
451 printf("Can't open log file %s - aborting\n", DEFAULT_LOGNAME);
458 #ifdef AFS_PTHREAD_ENV
459 SetLogThreadNumProgram( threadNum );
462 /* process the user supplied args */
464 code = cmd_Dispatch(argc, argv);
468 /* exit if there was a help option */
472 /* open the log file */
473 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
474 if (globalConfPtr->log == NULL) {
475 printf("Can't open log file %s - aborting\n",
476 AFSDIR_SERVER_BUDBLOG_FILEPATH);
480 /* keep log closed so can remove it */
482 fclose(globalConfPtr->log);
484 /* open the cell's configuration directory */
485 LogDebug(4, "opening %s\n", globalConfPtr->cellConfigdir);
487 BU_conf = afsconf_Open(globalConfPtr->cellConfigdir);
489 LogError(code, "Failed getting cell info\n");
490 afs_com_err(whoami, code, "Failed getting cell info");
494 code = afsconf_GetLocalCell(BU_conf, lcell, sizeof(lcell));
496 LogError(0, "** Can't determine local cell name!\n");
500 if (globalConfPtr->myHost == 0) {
501 /* if user hasn't supplied a list of servers, extract server
502 * list from the cell's database
505 LogDebug(1, "Using server list from %s cell database.\n", lcell);
507 code = afsconf_GetExtendedCellInfo (BU_conf, lcell, 0, &cellinfo,
510 convert_cell_to_ubik(&cellinfo, &globalConfPtr->myHost,
511 globalConfPtr->serverList);
516 /* initialize ubik */
517 ubik_CRXSecurityProc = afsconf_ClientAuth;
518 ubik_CRXSecurityRock = (char *)BU_conf;
520 ubik_SRXSecurityProc = afsconf_ServerAuth;
521 ubik_SRXSecurityRock = (char *)BU_conf;
523 ubik_CheckRXSecurityProc = afsconf_CheckAuth;
524 ubik_CheckRXSecurityRock = (char *)BU_conf;
526 if (ubik_nBuffers == 0)
529 LogError(0, "Will allocate %d ubik buffers\n", ubik_nBuffers);
532 (char *)malloc(strlen(globalConfPtr->databaseDirectory) +
533 strlen(globalConfPtr->databaseName) + 1);
537 /* construct the database name */
538 strcpy(dbNamePtr, globalConfPtr->databaseDirectory);
539 strcat(dbNamePtr, globalConfPtr->databaseName); /* name prefix */
541 rx_SetRxDeadTime(60); /* 60 seconds inactive before timeout */
545 if (AFSDIR_SERVER_NETRESTRICT_FILEPATH ||
546 AFSDIR_SERVER_NETINFO_FILEPATH) {
548 ccode = parseNetFiles(SHostAddrs, NULL, NULL,
549 ADDRSPERSITE, reason,
550 AFSDIR_SERVER_NETINFO_FILEPATH,
551 AFSDIR_SERVER_NETRESTRICT_FILEPATH);
554 ccode = rx_getAllAddr(SHostAddrs, ADDRSPERSITE);
557 host = SHostAddrs[0];
558 rx_InitHost(host, htons(AFSCONF_BUDBPORT));
562 code = ubik_ServerInitByInfo (globalConfPtr->myHost,
563 htons(AFSCONF_BUDBPORT),
566 dbNamePtr, /* name prefix */
570 LogError(code, "Ubik init failed\n");
571 afs_com_err(whoami, code, "Ubik init failed");
575 sca[RX_SCINDEX_NULL] = rxnull_NewServerSecurityObject();
576 sca[RX_SCINDEX_VAB] = 0;
577 sca[RX_SCINDEX_KAD] =
578 rxkad_NewServerSecurityObject(rxkad_clear, BU_conf, afsconf_GetKey,
581 /* Disable jumbograms */
585 rx_NewServiceHost(host, 0, BUDB_SERVICE, "BackupDatabase", sca, 3,
586 BUDB_ExecuteRequest);
587 if (tservice == (struct rx_service *)0) {
588 LogError(0, "Could not create backup database rx service\n");
589 printf("Could not create backup database rx service\n");
592 rx_SetMinProcs(tservice, 1);
593 rx_SetMaxProcs(tservice, lwps);
594 rx_SetStackSize(tservice, 10000);
596 /* allow super users to manage RX statistics */
597 rx_SetRxStatUserOk(BU_rxstat_userok);
599 /* misc. initialization */
601 /* database dump synchronization */
602 memset(dumpSyncPtr, 0, sizeof(*dumpSyncPtr));
603 Lock_Init(&dumpSyncPtr->ds_lock);
605 rx_StartServer(0); /* start handling requests */
612 currentTime = time(0);
613 LogError(0, "Ready to process requests at %s\n", ctime(¤tTime));
615 rx_ServerProc(NULL); /* donate this LWP */
618 osi_audit(BUDB_FinishEvent, code, AUD_END);
625 /* do consistency checks on structure sizes */
626 if ((sizeof(struct htBlock) > BLOCKSIZE)
627 || (sizeof(struct vfBlock) > BLOCKSIZE)
628 || (sizeof(struct viBlock) > BLOCKSIZE)
629 || (sizeof(struct dBlock) > BLOCKSIZE)
630 || (sizeof(struct tBlock) > BLOCKSIZE)
632 fprintf(stderr, "Block layout error!\n");
638 LogDebug(level, a, b, c, d, e, f, g, h, i)
640 char *a, *b, *c, *d, *e, *f, *g, *h, *i;
643 if (debugging >= level) {
644 /* log normally closed so can remove it */
645 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
646 if (globalConfPtr->log != NULL) {
647 fprintf(globalConfPtr->log, a, b, c, d, e, f, g, h, i);
648 fflush(globalConfPtr->log);
649 fclose(globalConfPtr->log);
659 static char timestamp[20];
662 strftime(timestamp, 20, "%m/%d/%Y %H:%M:%S", lt);
667 Log(a, b, c, d, e, f, g, h, i)
668 char *a, *b, *c, *d, *e, *f, *g, *h, *i;
672 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
673 if (globalConfPtr->log != NULL) {
675 fprintf(globalConfPtr->log, "%s ", TimeStamp(now));
677 fprintf(globalConfPtr->log, a, b, c, d, e, f, g, h, i);
678 fflush(globalConfPtr->log);
679 fclose(globalConfPtr->log);
685 LogError(code, a, b, c, d, e, f, g, h, i)
687 char *a, *b, *c, *d, *e, *f, *g, *h, *i;
691 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
693 if (globalConfPtr->log != NULL) {
695 fprintf(globalConfPtr->log, "%s ", TimeStamp(now));
698 fprintf(globalConfPtr->log, "%s: %s\n", afs_error_table_name(code),
699 afs_error_message(code));
700 fprintf(globalConfPtr->log, a, b, c, d, e, f, g, h, i);
701 fflush(globalConfPtr->log);
702 fclose(globalConfPtr->log);
715 struct dump *dumpPtr;
717 struct dump hostDump;
718 extern buServerConfP globalConfPtr;
720 dump_ntoh(dumpPtr, &hostDump);
722 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
723 if (globalConfPtr->log != NULL) {
724 printDump(globalConfPtr->log, &hostDump);
725 fclose(globalConfPtr->log);