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 <afs/param.h>
14 #include <WINNT/afsevent.h>
16 #include <netinet/in.h>
22 #include <sys/types.h>
31 #include <rx/rx_globals.h>
32 #include <afs/cellconfig.h>
34 #include <afs/bubasics.h>
35 #include <afs/afsutil.h>
36 #include <afs/com_err.h>
41 #include "budb_errs.h"
43 #include "error_macros.h"
45 #include "afs/audit.h"
48 extern afs_int32 ubik_lastYesTime;
49 extern afs_int32 ubik_nBuffers;
51 struct ubik_dbase *BU_dbase;
52 struct afsconf_dir *BU_conf; /* for getting cell info */
54 char lcell[MAXKTCREALMLEN];
58 /* server's global configuration information. This is exported to other
62 buServerConfT globalConf;
63 buServerConfP globalConfPtr = &globalConf;
64 char dbDir[AFSDIR_PATH_MAX], cellConfDir[AFSDIR_PATH_MAX];
65 /* debugging control */
68 /* check whether caller is authorized to manage RX statistics */
69 int BU_rxstat_userok(call)
72 return afsconf_SuperUser(BU_conf, call, (char *)0);
76 convert_cell_to_ubik (cellinfo, myHost, serverList)
77 struct afsconf_cell *cellinfo;
79 afs_int32 *serverList;
86 gethostname(hostname,sizeof(hostname));
87 th = gethostbyname(hostname);
90 printf("prserver: couldn't get address of this host.\n");
93 bcopy(th->h_addr,myHost,sizeof(afs_int32));
95 for (i=0; i<cellinfo->numServers; i++)
96 /* omit my host from serverList */
97 if (cellinfo->hostAddr[i].sin_addr.s_addr != *myHost)
98 *serverList++ = cellinfo->hostAddr[i].sin_addr.s_addr;
100 *serverList = 0; /* terminate list */
105 * The whole purpose of MyBeforeProc is to detect
106 * if the -help option was not within the command line.
107 * If it were, this routine would never have been called.
109 static int MyBeforeProc(as)
110 register struct cmd_syndesc *as;
116 /* initializeCommands
117 * initialize all the supported commands and their arguments
120 initializeArgHandler()
122 struct cmd_syndesc *cptr;
126 cmd_SetBeforeProc(MyBeforeProc, (char *)0);
128 cptr = cmd_CreateSyntax((char *) 0, argHandler, (char *) 0,
129 "Backup database server");
131 cmd_AddParm(cptr, "-database", CMD_SINGLE, CMD_OPTIONAL,
132 "database directory");
134 cmd_AddParm(cptr, "-cellservdb", CMD_SINGLE, CMD_OPTIONAL,
135 "cell configuration directory");
137 cmd_AddParm(cptr, "-resetdb", CMD_FLAG, CMD_OPTIONAL,
138 "truncate the database");
140 cmd_AddParm(cptr, "-noauth", CMD_FLAG, CMD_OPTIONAL,
141 "run without authentication");
143 cmd_AddParm(cptr, "-smallht", CMD_FLAG, CMD_OPTIONAL,
144 "use small hash tables");
146 cmd_AddParm(cptr, "-servers", CMD_LIST, CMD_OPTIONAL,
147 "list of ubik database servers");
151 argHandler(as, arock)
152 struct cmd_syndesc *as;
156 /* globalConfPtr provides the handle for the configuration information */
158 /* database directory */
159 if ( as->parms[0].items != 0 )
161 globalConfPtr->databaseDirectory =
162 (char *) malloc(strlen(as->parms[0].items->data)+1);
163 if ( globalConfPtr->databaseDirectory == 0 )
165 strcpy(globalConfPtr->databaseDirectory, as->parms[0].items->data);
168 /* -cellservdb, cell configuration directory */
169 if ( as->parms[1].items != 0 )
171 globalConfPtr->cellConfigdir =
172 (char *) malloc(strlen(as->parms[1].items->data)+1);
173 if ( globalConfPtr->cellConfigdir == 0 )
176 strcpy(globalConfPtr->cellConfigdir, as->parms[1].items->data);
178 globalConfPtr->debugFlags |= DF_RECHECKNOAUTH;
181 /* truncate the database */
182 if ( as->parms[2].items != 0 )
185 /* run without authentication */
186 if ( as->parms[3].items != 0 )
187 globalConfPtr->debugFlags |= DF_NOAUTH;
189 /* use small hash tables */
190 if ( as->parms[4].items != 0 )
191 globalConfPtr->debugFlags |= DF_SMALLHT;
193 /* user provided list of ubik database servers */
194 if ( as->parms[5].items != 0 )
195 parseServerList(as->parms[5].items);
202 parseServerList(itemPtr)
203 struct cmd_item *itemPtr;
205 struct cmd_item *save;
208 afs_int32 nservers = 0;
213 /* compute number of servers in the list */
217 itemPtr = itemPtr->next;
220 LogDebug(3, "%d servers\n", nservers);
222 /* now can allocate the space for the server arguments */
223 serverArgs = (char **) malloc( (nservers+2) * sizeof(char *) );
224 if ( serverArgs == 0 )
231 /* now go through and construct the list of servers */
235 *ptr++ = itemPtr->data;
236 itemPtr = itemPtr->next;
239 code = ubik_ParseServerList(nservers+2, serverArgs,
240 &globalConfPtr->myHost,
241 globalConfPtr->serverList);
245 /* free space for the server args */
246 free( (char *) serverArgs);
253 * truncates just the database file.
262 path = (char *) malloc(strlen(globalConfPtr->databaseDirectory) +
263 strlen(globalConfPtr->databaseName) +
264 strlen(globalConfPtr->databaseExtension) + 1);
268 /* construct the database name */
269 strcpy(path, globalConfPtr->databaseDirectory);
270 strcat(path, globalConfPtr->databaseName);
271 strcat(path, globalConfPtr->databaseExtension);
273 fd = open(path, O_RDWR, 0755);
277 if (ftruncate(fd, 0) != 0 ) {
290 #include "AFS_component_version_number.c"
296 char *whoami = argv[0];
298 struct afsconf_cell cellinfo;
302 struct rx_service *tservice;
303 struct rx_securityClass *sca[3];
305 extern int afsconf_ClientAuth();
306 extern int afsconf_ServerAuth();
307 extern int afsconf_CheckAuth();
309 extern int rx_stackSize;
310 extern struct rx_securityClass *rxnull_NewServerSecurityObject();
311 extern int BUDB_ExecuteRequest();
312 extern afsconf_GetKey();
315 /* initialize winsock */
316 if (afs_winsockInit()<0) {
317 ReportErrorEventAlt(AFSEVT_SVR_WINSOCK_INIT_FAILED, 0, argv[0],0);
318 fprintf(stderr, "%s: Couldn't initialize winsock.\n", whoami);
325 * The following signal action for AIX is necessary so that in case of a
326 * crash (i.e. core is generated) we can include the user's data section
327 * in the core dump. Unfortunately, by default, only a partial core is
328 * generated which, in many cases, isn't too useful.
330 struct sigaction nsa;
332 sigemptyset(&nsa.sa_mask);
333 nsa.sa_handler = SIG_DFL;
334 nsa.sa_flags = SA_FULLDUMP;
335 sigaction(SIGSEGV, &nsa, NULL);
336 sigaction(SIGABRT, &nsa, NULL);
338 osi_audit(BUDB_StartEvent, 0, AUD_END);
340 initialize_budb_error_table();
341 initializeArgHandler();
343 /* Initialize dirpaths */
344 if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
346 ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
348 com_err(whoami,errno,"; Unable to obtain AFS server directory.");
352 bzero(globalConfPtr, sizeof(*globalConfPtr));
354 /* set default configuration values */
355 strcpy(dbDir, AFSDIR_SERVER_DB_DIRPATH);
357 globalConfPtr->databaseDirectory = dbDir;
358 globalConfPtr->databaseName = DEFAULT_DBPREFIX;
359 strcpy(cellConfDir, AFSDIR_SERVER_ETC_DIRPATH);
360 globalConfPtr->cellConfigdir = cellConfDir;
362 /* open the log file */
364 globalConfPtr->log = fopen(DEFAULT_LOGNAME,"a");
365 if ( globalConfPtr->log == NULL )
367 printf("Can't open log file %s - aborting\n", DEFAULT_LOGNAME);
374 /* process the user supplied args */
376 code = cmd_Dispatch(argc, argv);
380 /* exit if there was a help option */
384 /* open the log file */
385 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
386 if ( globalConfPtr->log == NULL )
388 printf("Can't open log file %s - aborting\n",
389 AFSDIR_SERVER_BUDBLOG_FILEPATH);
393 /* keep log closed so can remove it */
395 fclose(globalConfPtr->log);
397 /* open the cell's configuration directory */
398 LogDebug(4, "opening %s\n", globalConfPtr->cellConfigdir);
400 BU_conf = afsconf_Open(globalConfPtr->cellConfigdir);
403 LogError(code, "Failed getting cell info\n");
404 com_err(whoami, code, "Failed getting cell info");
408 code = afsconf_GetLocalCell(BU_conf, lcell, sizeof(lcell));
411 LogError(0, "** Can't determine local cell name!\n");
415 if ( globalConfPtr->myHost == 0 )
417 /* if user hasn't supplied a list of servers, extract server
418 * list from the cell's database
421 LogDebug(1, "Using server list from %s cell database.\n", lcell);
423 code = afsconf_GetCellInfo (BU_conf, lcell, 0, &cellinfo);
424 code = convert_cell_to_ubik (&cellinfo,
425 &globalConfPtr->myHost,
426 globalConfPtr->serverList);
431 /* initialize ubik */
432 ubik_CRXSecurityProc = afsconf_ClientAuth;
433 ubik_CRXSecurityRock = (char *)BU_conf;
435 ubik_SRXSecurityProc = afsconf_ServerAuth;
436 ubik_SRXSecurityRock = (char *)BU_conf;
438 ubik_CheckRXSecurityProc = afsconf_CheckAuth;
439 ubik_CheckRXSecurityRock = (char *)BU_conf;
443 dbNamePtr = (char *) malloc(strlen(globalConfPtr->databaseDirectory) +
444 strlen(globalConfPtr->databaseName) + 1);
445 if ( dbNamePtr == 0 )
448 /* construct the database name */
449 strcpy(dbNamePtr, globalConfPtr->databaseDirectory);
450 strcat(dbNamePtr, globalConfPtr->databaseName); /* name prefix */
452 rx_SetRxDeadTime(60); /* 60 seconds inactive before timeout */
454 code = ubik_ServerInit(globalConfPtr->myHost,
455 htons(AFSCONF_BUDBPORT),
456 globalConfPtr->serverList,
457 dbNamePtr, /* name prefix */
461 LogError(code, "Ubik init failed\n");
462 com_err(whoami, code, "Ubik init failed");
466 sca[RX_SCINDEX_NULL] = rxnull_NewServerSecurityObject();
467 sca[RX_SCINDEX_VAB] = 0;
468 sca[RX_SCINDEX_KAD] = rxkad_NewServerSecurityObject(rxkad_clear,
473 /* These two lines disallow jumbograms */
474 rx_maxReceiveSize = OLD_MAX_PACKET_SIZE;
475 rxi_nSendFrags = rxi_nRecvFrags = 1;
477 tservice = rx_NewService(0, BUDB_SERVICE, "BackupDatabase",
478 sca, 3, BUDB_ExecuteRequest);
479 if (tservice == (struct rx_service *)0)
481 LogError(0, "Could not create backup database rx service\n");
482 printf("Could not create backup database rx service\n");
485 rx_SetMinProcs(tservice, 1);
486 rx_SetMaxProcs(tservice, 3);
487 rx_SetStackSize(tservice, 10000);
489 /* allow super users to manage RX statistics */
490 rx_SetRxStatUserOk(BU_rxstat_userok);
492 /* misc. initialization */
494 /* database dump synchronization */
495 bzero(dumpSyncPtr, sizeof(*dumpSyncPtr));
496 Lock_Init(&dumpSyncPtr->ds_lock);
498 rx_StartServer(0); /* start handling requests */
505 currentTime = time(0);
506 LogError(0, "Ready to process requests at %s\n", ctime(¤tTime));
508 rx_ServerProc(); /* donate this LWP */
511 osi_audit(BUDB_FinishEvent, code, AUD_END);
518 /* do consistency checks on structure sizes */
519 if ( (sizeof(struct htBlock) > BLOCKSIZE)
520 || (sizeof(struct vfBlock) > BLOCKSIZE)
521 || (sizeof(struct viBlock) > BLOCKSIZE)
522 || (sizeof(struct dBlock) > BLOCKSIZE)
523 || (sizeof(struct tBlock) > BLOCKSIZE)
526 fprintf (stderr, "Block layout error!\n");
532 LogDebug(level, a,b,c,d,e,f,g,h,i)
534 char *a, *b, *c, *d, *e, *f, *g, *h, *i;
537 if ( debugging >= level)
539 /* log normally closed so can remove it */
540 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
541 if ( globalConfPtr->log != NULL )
543 fprintf(globalConfPtr->log, a, b, c, d, e, f, g, h, i);
544 fflush(globalConfPtr->log);
545 fclose(globalConfPtr->log);
550 static char *TimeStamp(time_t t)
553 static char timestamp[20];
556 strftime (timestamp, 20, "%m/%d/%Y %T", lt);
561 Log(a,b,c,d,e,f,g,h,i)
562 char *a, *b, *c, *d, *e, *f, *g, *h, *i;
566 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
567 if ( globalConfPtr->log != NULL )
570 fprintf(globalConfPtr->log, "%s ", TimeStamp(now));
572 fprintf(globalConfPtr->log, a, b, c, d, e, f, g, h, i);
573 fflush(globalConfPtr->log);
574 fclose(globalConfPtr->log);
579 LogError(code, a,b,c,d,e,f,g,h,i)
581 char *a, *b, *c, *d, *e, *f, *g, *h, *i;
585 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
587 if ( globalConfPtr->log != NULL )
590 fprintf(globalConfPtr->log, "%s ", TimeStamp(now));
593 fprintf(globalConfPtr->log, "%s: %s\n", error_table_name(code),
594 error_message(code));
595 fprintf(globalConfPtr->log, a, b, c, d, e, f, g, h, i);
596 fflush(globalConfPtr->log);
597 fclose(globalConfPtr->log);
609 struct dump *dumpPtr;
611 struct dump hostDump;
612 extern buServerConfP globalConfPtr;
614 dump_ntoh(dumpPtr, &hostDump);
616 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
617 if ( globalConfPtr->log != NULL )
619 printDump(globalConfPtr->log, &hostDump);
620 fclose(globalConfPtr->log);