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>
26 #include <sys/types.h>
35 #include <rx/rx_globals.h>
36 #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"
49 #include "afs/audit.h"
52 extern afs_int32 ubik_lastYesTime;
53 extern afs_int32 ubik_nBuffers;
55 struct ubik_dbase *BU_dbase;
56 struct afsconf_dir *BU_conf; /* for getting cell info */
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 */
72 /* check whether caller is authorized to manage RX statistics */
73 int BU_rxstat_userok(call)
76 return afsconf_SuperUser(BU_conf, call, (char *)0);
80 convert_cell_to_ubik (cellinfo, myHost, serverList)
81 struct afsconf_cell *cellinfo;
83 afs_int32 *serverList;
90 gethostname(hostname,sizeof(hostname));
91 th = gethostbyname(hostname);
94 printf("prserver: couldn't get address of this host.\n");
97 memcpy(myHost, th->h_addr, sizeof(afs_int32));
99 for (i=0; i<cellinfo->numServers; i++)
100 /* omit my host from serverList */
101 if (cellinfo->hostAddr[i].sin_addr.s_addr != *myHost)
102 *serverList++ = cellinfo->hostAddr[i].sin_addr.s_addr;
104 *serverList = 0; /* terminate list */
109 * The whole purpose of MyBeforeProc is to detect
110 * if the -help option was not within the command line.
111 * If it were, this routine would never have been called.
113 static int MyBeforeProc(as)
114 register struct cmd_syndesc *as;
120 /* initializeCommands
121 * initialize all the supported commands and their arguments
124 initializeArgHandler()
126 struct cmd_syndesc *cptr;
130 cmd_SetBeforeProc(MyBeforeProc, (char *)0);
132 cptr = cmd_CreateSyntax((char *) 0, argHandler, (char *) 0,
133 "Backup database server");
135 cmd_AddParm(cptr, "-database", CMD_SINGLE, CMD_OPTIONAL,
136 "database directory");
138 cmd_AddParm(cptr, "-cellservdb", CMD_SINGLE, CMD_OPTIONAL,
139 "cell configuration directory");
141 cmd_AddParm(cptr, "-resetdb", CMD_FLAG, CMD_OPTIONAL,
142 "truncate the database");
144 cmd_AddParm(cptr, "-noauth", CMD_FLAG, CMD_OPTIONAL,
145 "run without authentication");
147 cmd_AddParm(cptr, "-smallht", CMD_FLAG, CMD_OPTIONAL,
148 "use small hash tables");
150 cmd_AddParm(cptr, "-servers", CMD_LIST, CMD_OPTIONAL,
151 "list of ubik database servers");
155 argHandler(as, arock)
156 struct cmd_syndesc *as;
160 /* globalConfPtr provides the handle for the configuration information */
162 /* database directory */
163 if ( as->parms[0].items != 0 )
165 globalConfPtr->databaseDirectory =
166 (char *) malloc(strlen(as->parms[0].items->data)+1);
167 if ( globalConfPtr->databaseDirectory == 0 )
169 strcpy(globalConfPtr->databaseDirectory, as->parms[0].items->data);
172 /* -cellservdb, cell configuration directory */
173 if ( as->parms[1].items != 0 )
175 globalConfPtr->cellConfigdir =
176 (char *) malloc(strlen(as->parms[1].items->data)+1);
177 if ( globalConfPtr->cellConfigdir == 0 )
180 strcpy(globalConfPtr->cellConfigdir, as->parms[1].items->data);
182 globalConfPtr->debugFlags |= DF_RECHECKNOAUTH;
185 /* truncate the database */
186 if ( as->parms[2].items != 0 )
189 /* run without authentication */
190 if ( as->parms[3].items != 0 )
191 globalConfPtr->debugFlags |= DF_NOAUTH;
193 /* use small hash tables */
194 if ( as->parms[4].items != 0 )
195 globalConfPtr->debugFlags |= DF_SMALLHT;
197 /* user provided list of ubik database servers */
198 if ( as->parms[5].items != 0 )
199 parseServerList(as->parms[5].items);
206 parseServerList(itemPtr)
207 struct cmd_item *itemPtr;
209 struct cmd_item *save;
212 afs_int32 nservers = 0;
217 /* compute number of servers in the list */
221 itemPtr = itemPtr->next;
224 LogDebug(3, "%d servers\n", nservers);
226 /* now can allocate the space for the server arguments */
227 serverArgs = (char **) malloc( (nservers+2) * sizeof(char *) );
228 if ( serverArgs == 0 )
235 /* now go through and construct the list of servers */
239 *ptr++ = itemPtr->data;
240 itemPtr = itemPtr->next;
243 code = ubik_ParseServerList(nservers+2, serverArgs,
244 &globalConfPtr->myHost,
245 globalConfPtr->serverList);
249 /* free space for the server args */
250 free( (char *) serverArgs);
257 * truncates just the database file.
266 path = (char *) malloc(strlen(globalConfPtr->databaseDirectory) +
267 strlen(globalConfPtr->databaseName) +
268 strlen(globalConfPtr->databaseExtension) + 1);
272 /* construct the database name */
273 strcpy(path, globalConfPtr->databaseDirectory);
274 strcat(path, globalConfPtr->databaseName);
275 strcat(path, globalConfPtr->databaseExtension);
277 fd = open(path, O_RDWR, 0755);
281 if (ftruncate(fd, 0) != 0 ) {
294 #include "AFS_component_version_number.c"
300 char *whoami = argv[0];
302 struct afsconf_cell cellinfo;
306 struct rx_service *tservice;
307 struct rx_securityClass *sca[3];
309 extern int afsconf_ServerAuth();
310 extern int afsconf_CheckAuth();
312 extern int rx_stackSize;
313 extern struct rx_securityClass *rxnull_NewServerSecurityObject();
314 extern int BUDB_ExecuteRequest();
317 /* initialize winsock */
318 if (afs_winsockInit()<0) {
319 ReportErrorEventAlt(AFSEVT_SVR_WINSOCK_INIT_FAILED, 0, argv[0],0);
320 fprintf(stderr, "%s: Couldn't initialize winsock.\n", whoami);
327 * The following signal action for AIX is necessary so that in case of a
328 * crash (i.e. core is generated) we can include the user's data section
329 * in the core dump. Unfortunately, by default, only a partial core is
330 * generated which, in many cases, isn't too useful.
332 struct sigaction nsa;
334 sigemptyset(&nsa.sa_mask);
335 nsa.sa_handler = SIG_DFL;
336 nsa.sa_flags = SA_FULLDUMP;
337 sigaction(SIGSEGV, &nsa, NULL);
338 sigaction(SIGABRT, &nsa, NULL);
340 osi_audit(BUDB_StartEvent, 0, AUD_END);
342 initialize_budb_error_table();
343 initializeArgHandler();
345 /* Initialize dirpaths */
346 if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
348 ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
350 com_err(whoami,errno,"; Unable to obtain AFS server directory.");
354 memset(globalConfPtr, 0, sizeof(*globalConfPtr));
356 /* set default configuration values */
357 strcpy(dbDir, AFSDIR_SERVER_DB_DIRPATH);
359 globalConfPtr->databaseDirectory = dbDir;
360 globalConfPtr->databaseName = DEFAULT_DBPREFIX;
361 strcpy(cellConfDir, AFSDIR_SERVER_ETC_DIRPATH);
362 globalConfPtr->cellConfigdir = cellConfDir;
364 /* open the log file */
366 globalConfPtr->log = fopen(DEFAULT_LOGNAME,"a");
367 if ( globalConfPtr->log == NULL )
369 printf("Can't open log file %s - aborting\n", DEFAULT_LOGNAME);
376 /* process the user supplied args */
378 code = cmd_Dispatch(argc, argv);
382 /* exit if there was a help option */
386 /* open the log file */
387 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
388 if ( globalConfPtr->log == NULL )
390 printf("Can't open log file %s - aborting\n",
391 AFSDIR_SERVER_BUDBLOG_FILEPATH);
395 /* keep log closed so can remove it */
397 fclose(globalConfPtr->log);
399 /* open the cell's configuration directory */
400 LogDebug(4, "opening %s\n", globalConfPtr->cellConfigdir);
402 BU_conf = afsconf_Open(globalConfPtr->cellConfigdir);
405 LogError(code, "Failed getting cell info\n");
406 com_err(whoami, code, "Failed getting cell info");
410 code = afsconf_GetLocalCell(BU_conf, lcell, sizeof(lcell));
413 LogError(0, "** Can't determine local cell name!\n");
417 if ( globalConfPtr->myHost == 0 )
419 /* if user hasn't supplied a list of servers, extract server
420 * list from the cell's database
423 LogDebug(1, "Using server list from %s cell database.\n", lcell);
425 code = afsconf_GetCellInfo (BU_conf, lcell, 0, &cellinfo);
426 code = convert_cell_to_ubik (&cellinfo,
427 &globalConfPtr->myHost,
428 globalConfPtr->serverList);
433 /* initialize ubik */
434 ubik_CRXSecurityProc = afsconf_ClientAuth;
435 ubik_CRXSecurityRock = (char *)BU_conf;
437 ubik_SRXSecurityProc = afsconf_ServerAuth;
438 ubik_SRXSecurityRock = (char *)BU_conf;
440 ubik_CheckRXSecurityProc = afsconf_CheckAuth;
441 ubik_CheckRXSecurityRock = (char *)BU_conf;
445 dbNamePtr = (char *) malloc(strlen(globalConfPtr->databaseDirectory) +
446 strlen(globalConfPtr->databaseName) + 1);
447 if ( dbNamePtr == 0 )
450 /* construct the database name */
451 strcpy(dbNamePtr, globalConfPtr->databaseDirectory);
452 strcat(dbNamePtr, globalConfPtr->databaseName); /* name prefix */
454 rx_SetRxDeadTime(60); /* 60 seconds inactive before timeout */
456 code = ubik_ServerInit(globalConfPtr->myHost,
457 htons(AFSCONF_BUDBPORT),
458 globalConfPtr->serverList,
459 dbNamePtr, /* name prefix */
463 LogError(code, "Ubik init failed\n");
464 com_err(whoami, code, "Ubik init failed");
468 sca[RX_SCINDEX_NULL] = rxnull_NewServerSecurityObject();
469 sca[RX_SCINDEX_VAB] = 0;
470 sca[RX_SCINDEX_KAD] = rxkad_NewServerSecurityObject(rxkad_clear,
475 /* These two lines disallow jumbograms */
476 rx_maxReceiveSize = OLD_MAX_PACKET_SIZE;
477 rxi_nSendFrags = rxi_nRecvFrags = 1;
479 tservice = rx_NewService(0, BUDB_SERVICE, "BackupDatabase",
480 sca, 3, BUDB_ExecuteRequest);
481 if (tservice == (struct rx_service *)0)
483 LogError(0, "Could not create backup database rx service\n");
484 printf("Could not create backup database rx service\n");
487 rx_SetMinProcs(tservice, 1);
488 rx_SetMaxProcs(tservice, 3);
489 rx_SetStackSize(tservice, 10000);
491 /* allow super users to manage RX statistics */
492 rx_SetRxStatUserOk(BU_rxstat_userok);
494 /* misc. initialization */
496 /* database dump synchronization */
497 memset(dumpSyncPtr, 0, sizeof(*dumpSyncPtr));
498 Lock_Init(&dumpSyncPtr->ds_lock);
500 rx_StartServer(0); /* start handling requests */
507 currentTime = time(0);
508 LogError(0, "Ready to process requests at %s\n", ctime(¤tTime));
510 rx_ServerProc(); /* donate this LWP */
513 osi_audit(BUDB_FinishEvent, code, AUD_END);
520 /* do consistency checks on structure sizes */
521 if ( (sizeof(struct htBlock) > BLOCKSIZE)
522 || (sizeof(struct vfBlock) > BLOCKSIZE)
523 || (sizeof(struct viBlock) > BLOCKSIZE)
524 || (sizeof(struct dBlock) > BLOCKSIZE)
525 || (sizeof(struct tBlock) > BLOCKSIZE)
528 fprintf (stderr, "Block layout error!\n");
534 LogDebug(level, a,b,c,d,e,f,g,h,i)
536 char *a, *b, *c, *d, *e, *f, *g, *h, *i;
539 if ( debugging >= level)
541 /* log normally closed so can remove it */
542 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
543 if ( globalConfPtr->log != NULL )
545 fprintf(globalConfPtr->log, a, b, c, d, e, f, g, h, i);
546 fflush(globalConfPtr->log);
547 fclose(globalConfPtr->log);
552 static char *TimeStamp(time_t t)
555 static char timestamp[20];
558 strftime (timestamp, 20, "%m/%d/%Y %T", lt);
563 Log(a,b,c,d,e,f,g,h,i)
564 char *a, *b, *c, *d, *e, *f, *g, *h, *i;
568 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
569 if ( globalConfPtr->log != NULL )
572 fprintf(globalConfPtr->log, "%s ", TimeStamp(now));
574 fprintf(globalConfPtr->log, a, b, c, d, e, f, g, h, i);
575 fflush(globalConfPtr->log);
576 fclose(globalConfPtr->log);
581 LogError(code, a,b,c,d,e,f,g,h,i)
583 char *a, *b, *c, *d, *e, *f, *g, *h, *i;
587 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
589 if ( globalConfPtr->log != NULL )
592 fprintf(globalConfPtr->log, "%s ", TimeStamp(now));
595 fprintf(globalConfPtr->log, "%s: %s\n", error_table_name(code),
596 error_message(code));
597 fprintf(globalConfPtr->log, a, b, c, d, e, f, g, h, i);
598 fflush(globalConfPtr->log);
599 fclose(globalConfPtr->log);
611 struct dump *dumpPtr;
613 struct dump hostDump;
614 extern buServerConfP globalConfPtr;
616 dump_ntoh(dumpPtr, &hostDump);
618 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
619 if ( globalConfPtr->log != NULL )
621 printDump(globalConfPtr->log, &hostDump);
622 fclose(globalConfPtr->log);