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 char lcell[MAXKTCREALMLEN];
60 /* server's global configuration information. This is exported to other
64 buServerConfT globalConf;
65 buServerConfP globalConfPtr = &globalConf;
66 char dbDir[AFSDIR_PATH_MAX], cellConfDir[AFSDIR_PATH_MAX];
67 /* debugging control */
72 #define ADDRSPERSITE 16 /* Same global is in rx/rx_user.c */
73 afs_uint32 SHostAddrs[ADDRSPERSITE];
75 #if defined(AFS_PTHREAD_ENV)
79 return pthread_getspecific(rx_thread_id_key);
83 /* check whether caller is authorized to manage RX statistics */
85 BU_rxstat_userok(call)
88 return afsconf_SuperUser(BU_conf, call, NULL);
92 convert_cell_to_ubik(cellinfo, myHost, serverList)
93 struct afsconf_cell *cellinfo;
95 afs_int32 *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_int32));
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.
126 register struct cmd_syndesc *as;
132 /* initializeCommands
133 * initialize all the supported commands and their arguments
136 initializeArgHandler()
138 struct cmd_syndesc *cptr;
142 cmd_SetBeforeProc(MyBeforeProc, NULL);
144 cptr = cmd_CreateSyntax(NULL, argHandler, NULL, "Backup database server");
146 cmd_AddParm(cptr, "-database", CMD_SINGLE, CMD_OPTIONAL,
147 "database directory");
149 cmd_AddParm(cptr, "-cellservdb", CMD_SINGLE, CMD_OPTIONAL,
150 "cell configuration directory");
152 cmd_AddParm(cptr, "-resetdb", CMD_FLAG, CMD_OPTIONAL,
153 "truncate the database");
155 cmd_AddParm(cptr, "-noauth", CMD_FLAG, CMD_OPTIONAL,
156 "run without authentication");
158 cmd_AddParm(cptr, "-smallht", CMD_FLAG, CMD_OPTIONAL,
159 "use small hash tables");
161 cmd_AddParm(cptr, "-servers", CMD_LIST, CMD_OPTIONAL,
162 "list of ubik database servers");
164 cmd_AddParm(cptr, "-ubikbuffers", CMD_SINGLE, CMD_OPTIONAL,
165 "the number of ubik buffers");
167 cmd_AddParm(cptr, "-auditlog", CMD_SINGLE, CMD_OPTIONAL,
173 argHandler(as, arock)
174 struct cmd_syndesc *as;
178 /* globalConfPtr provides the handle for the configuration information */
180 /* database directory */
181 if (as->parms[0].items != 0) {
182 globalConfPtr->databaseDirectory =
183 (char *)malloc(strlen(as->parms[0].items->data) + 1);
184 if (globalConfPtr->databaseDirectory == 0)
186 strcpy(globalConfPtr->databaseDirectory, as->parms[0].items->data);
189 /* -cellservdb, cell configuration directory */
190 if (as->parms[1].items != 0) {
191 globalConfPtr->cellConfigdir =
192 (char *)malloc(strlen(as->parms[1].items->data) + 1);
193 if (globalConfPtr->cellConfigdir == 0)
196 strcpy(globalConfPtr->cellConfigdir, as->parms[1].items->data);
198 globalConfPtr->debugFlags |= DF_RECHECKNOAUTH;
201 /* truncate the database */
202 if (as->parms[2].items != 0)
205 /* run without authentication */
206 if (as->parms[3].items != 0)
207 globalConfPtr->debugFlags |= DF_NOAUTH;
209 /* use small hash tables */
210 if (as->parms[4].items != 0)
211 globalConfPtr->debugFlags |= DF_SMALLHT;
213 /* user provided list of ubik database servers */
214 if (as->parms[5].items != 0)
215 parseServerList(as->parms[5].items);
217 /* user provided the number of ubik buffers */
218 if (as->parms[6].items != 0)
219 ubik_nBuffers = atoi(as->parms[6].items->data);
223 if (as->parms[7].items != 0) {
226 char oldName[MAXPATHLEN];
227 char *fileName = as->parms[7].items->data;
231 if ((lstat(fileName, &statbuf) == 0)
232 && (S_ISFIFO(statbuf.st_mode))) {
233 flags = O_WRONLY | O_NONBLOCK;
237 strcpy(oldName, fileName);
238 strcat(oldName, ".old");
239 renamefile(fileName, oldName);
240 flags = O_WRONLY | O_TRUNC | O_CREAT;
242 tempfd = open(fileName, flags, 0666);
244 auditout = fdopen(tempfd, "a");
246 osi_audit_file(auditout);
248 printf("Warning: auditlog %s not writable, ignored.\n", fileName);
250 printf("Warning: auditlog %s not writable, ignored.\n", fileName);
258 parseServerList(itemPtr)
259 struct cmd_item *itemPtr;
261 struct cmd_item *save;
264 afs_int32 nservers = 0;
269 /* compute number of servers in the list */
272 itemPtr = itemPtr->next;
275 LogDebug(3, "%d servers\n", nservers);
277 /* now can allocate the space for the server arguments */
278 serverArgs = (char **)malloc((nservers + 2) * sizeof(char *));
286 /* now go through and construct the list of servers */
289 *ptr++ = itemPtr->data;
290 itemPtr = itemPtr->next;
294 ubik_ParseServerList(nservers + 2, serverArgs, &globalConfPtr->myHost,
295 globalConfPtr->serverList);
299 /* free space for the server args */
300 free((char *)serverArgs);
307 * truncates just the database file.
317 (char *)malloc(strlen(globalConfPtr->databaseDirectory) +
318 strlen(globalConfPtr->databaseName) +
319 strlen(globalConfPtr->databaseExtension) + 1);
323 /* construct the database name */
324 strcpy(path, globalConfPtr->databaseDirectory);
325 strcat(path, globalConfPtr->databaseName);
326 strcat(path, globalConfPtr->databaseExtension);
328 fd = open(path, O_RDWR, 0755);
332 if (ftruncate(fd, 0) != 0) {
345 #include "AFS_component_version_number.c"
351 char *whoami = argv[0];
353 struct afsconf_cell cellinfo;
356 afs_uint32 host = ntohl(INADDR_ANY);
358 char clones[MAXHOSTSPERCELL];
360 struct rx_service *tservice;
361 struct rx_securityClass *sca[3];
363 extern int afsconf_ServerAuth();
364 extern int afsconf_CheckAuth();
366 extern int rx_stackSize;
367 extern int BUDB_ExecuteRequest();
370 /* initialize winsock */
371 if (afs_winsockInit() < 0) {
372 ReportErrorEventAlt(AFSEVT_SVR_WINSOCK_INIT_FAILED, 0, argv[0], 0);
373 fprintf(stderr, "%s: Couldn't initialize winsock.\n", whoami);
380 * The following signal action for AIX is necessary so that in case of a
381 * crash (i.e. core is generated) we can include the user's data section
382 * in the core dump. Unfortunately, by default, only a partial core is
383 * generated which, in many cases, isn't too useful.
385 struct sigaction nsa;
387 sigemptyset(&nsa.sa_mask);
388 nsa.sa_handler = SIG_DFL;
389 nsa.sa_flags = SA_FULLDUMP;
390 sigaction(SIGSEGV, &nsa, NULL);
391 sigaction(SIGABRT, &nsa, NULL);
394 osi_audit(BUDB_StartEvent, 0, AUD_END);
396 initialize_BUDB_error_table();
397 initializeArgHandler();
399 /* Initialize dirpaths */
400 if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
402 ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
404 afs_com_err(whoami, errno, "; Unable to obtain AFS server directory.");
408 memset(globalConfPtr, 0, sizeof(*globalConfPtr));
410 /* set default configuration values */
411 strcpy(dbDir, AFSDIR_SERVER_DB_DIRPATH);
413 globalConfPtr->databaseDirectory = dbDir;
414 globalConfPtr->databaseName = DEFAULT_DBPREFIX;
415 strcpy(cellConfDir, AFSDIR_SERVER_ETC_DIRPATH);
416 globalConfPtr->cellConfigdir = cellConfDir;
418 /* open the log file */
420 globalConfPtr->log = fopen(DEFAULT_LOGNAME,"a");
421 if ( globalConfPtr->log == NULL )
423 printf("Can't open log file %s - aborting\n", DEFAULT_LOGNAME);
430 #ifdef AFS_PTHREAD_ENV
431 SetLogThreadNumProgram( threadNum );
434 /* process the user supplied args */
436 code = cmd_Dispatch(argc, argv);
440 /* exit if there was a help option */
444 /* open the log file */
445 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
446 if (globalConfPtr->log == NULL) {
447 printf("Can't open log file %s - aborting\n",
448 AFSDIR_SERVER_BUDBLOG_FILEPATH);
452 /* keep log closed so can remove it */
454 fclose(globalConfPtr->log);
456 /* open the cell's configuration directory */
457 LogDebug(4, "opening %s\n", globalConfPtr->cellConfigdir);
459 BU_conf = afsconf_Open(globalConfPtr->cellConfigdir);
461 LogError(code, "Failed getting cell info\n");
462 afs_com_err(whoami, code, "Failed getting cell info");
466 code = afsconf_GetLocalCell(BU_conf, lcell, sizeof(lcell));
468 LogError(0, "** Can't determine local cell name!\n");
472 if (globalConfPtr->myHost == 0) {
473 /* if user hasn't supplied a list of servers, extract server
474 * list from the cell's database
477 LogDebug(1, "Using server list from %s cell database.\n", lcell);
479 code = afsconf_GetExtendedCellInfo (BU_conf, lcell, 0, &cellinfo,
482 convert_cell_to_ubik(&cellinfo, &globalConfPtr->myHost,
483 globalConfPtr->serverList);
488 /* initialize ubik */
489 ubik_CRXSecurityProc = afsconf_ClientAuth;
490 ubik_CRXSecurityRock = (char *)BU_conf;
492 ubik_SRXSecurityProc = afsconf_ServerAuth;
493 ubik_SRXSecurityRock = (char *)BU_conf;
495 ubik_CheckRXSecurityProc = afsconf_CheckAuth;
496 ubik_CheckRXSecurityRock = (char *)BU_conf;
498 if (ubik_nBuffers == 0)
501 LogError(0, "Will allocate %d ubik buffers\n", ubik_nBuffers);
504 (char *)malloc(strlen(globalConfPtr->databaseDirectory) +
505 strlen(globalConfPtr->databaseName) + 1);
509 /* construct the database name */
510 strcpy(dbNamePtr, globalConfPtr->databaseDirectory);
511 strcat(dbNamePtr, globalConfPtr->databaseName); /* name prefix */
513 rx_SetRxDeadTime(60); /* 60 seconds inactive before timeout */
518 if (AFSDIR_SERVER_NETRESTRICT_FILEPATH ||
519 AFSDIR_SERVER_NETINFO_FILEPATH) {
521 ccode = parseNetFiles(SHostAddrs, NULL, NULL,
522 ADDRSPERSITE, reason,
523 AFSDIR_SERVER_NETINFO_FILEPATH,
524 AFSDIR_SERVER_NETRESTRICT_FILEPATH);
528 ccode = rx_getAllAddr(SHostAddrs, ADDRSPERSITE);
531 host = SHostAddrs[0];
532 rx_InitHost(host, htons(AFSCONF_BUDBPORT));
536 code = ubik_ServerInitByInfo (globalConfPtr->myHost,
537 htons(AFSCONF_BUDBPORT),
540 dbNamePtr, /* name prefix */
544 LogError(code, "Ubik init failed\n");
545 afs_com_err(whoami, code, "Ubik init failed");
549 sca[RX_SCINDEX_NULL] = rxnull_NewServerSecurityObject();
550 sca[RX_SCINDEX_VAB] = 0;
551 sca[RX_SCINDEX_KAD] =
552 rxkad_NewServerSecurityObject(rxkad_clear, BU_conf, afsconf_GetKey,
555 /* Disable jumbograms */
559 rx_NewServiceHost(host, 0, BUDB_SERVICE, "BackupDatabase", sca, 3,
560 BUDB_ExecuteRequest);
561 if (tservice == (struct rx_service *)0) {
562 LogError(0, "Could not create backup database rx service\n");
563 printf("Could not create backup database rx service\n");
566 rx_SetMinProcs(tservice, 1);
567 rx_SetMaxProcs(tservice, 3);
568 rx_SetStackSize(tservice, 10000);
570 /* allow super users to manage RX statistics */
571 rx_SetRxStatUserOk(BU_rxstat_userok);
573 /* misc. initialization */
575 /* database dump synchronization */
576 memset(dumpSyncPtr, 0, sizeof(*dumpSyncPtr));
577 Lock_Init(&dumpSyncPtr->ds_lock);
579 rx_StartServer(0); /* start handling requests */
586 currentTime = time(0);
587 LogError(0, "Ready to process requests at %s\n", ctime(¤tTime));
589 rx_ServerProc(); /* donate this LWP */
592 osi_audit(BUDB_FinishEvent, code, AUD_END);
599 /* do consistency checks on structure sizes */
600 if ((sizeof(struct htBlock) > BLOCKSIZE)
601 || (sizeof(struct vfBlock) > BLOCKSIZE)
602 || (sizeof(struct viBlock) > BLOCKSIZE)
603 || (sizeof(struct dBlock) > BLOCKSIZE)
604 || (sizeof(struct tBlock) > BLOCKSIZE)
606 fprintf(stderr, "Block layout error!\n");
612 LogDebug(level, a, b, c, d, e, f, g, h, i)
614 char *a, *b, *c, *d, *e, *f, *g, *h, *i;
617 if (debugging >= level) {
618 /* log normally closed so can remove it */
619 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
620 if (globalConfPtr->log != NULL) {
621 fprintf(globalConfPtr->log, a, b, c, d, e, f, g, h, i);
622 fflush(globalConfPtr->log);
623 fclose(globalConfPtr->log);
633 static char timestamp[20];
636 strftime(timestamp, 20, "%m/%d/%Y %T", lt);
641 Log(a, b, c, d, e, f, g, h, i)
642 char *a, *b, *c, *d, *e, *f, *g, *h, *i;
646 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
647 if (globalConfPtr->log != NULL) {
649 fprintf(globalConfPtr->log, "%s ", TimeStamp(now));
651 fprintf(globalConfPtr->log, a, b, c, d, e, f, g, h, i);
652 fflush(globalConfPtr->log);
653 fclose(globalConfPtr->log);
659 LogError(code, a, b, c, d, e, f, g, h, i)
661 char *a, *b, *c, *d, *e, *f, *g, *h, *i;
665 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
667 if (globalConfPtr->log != NULL) {
669 fprintf(globalConfPtr->log, "%s ", TimeStamp(now));
672 fprintf(globalConfPtr->log, "%s: %s\n", afs_error_table_name(code),
673 afs_error_message(code));
674 fprintf(globalConfPtr->log, a, b, c, d, e, f, g, h, i);
675 fflush(globalConfPtr->log);
676 fclose(globalConfPtr->log);
689 struct dump *dumpPtr;
691 struct dump hostDump;
692 extern buServerConfP globalConfPtr;
694 dump_ntoh(dumpPtr, &hostDump);
696 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
697 if (globalConfPtr->log != NULL) {
698 printDump(globalConfPtr->log, &hostDump);
699 fclose(globalConfPtr->log);