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>
17 #include <WINNT/afsevent.h>
25 #include <rx/rx_globals.h>
26 #include <afs/cellconfig.h>
27 #include <afs/bubasics.h>
28 #include <afs/afsutil.h>
29 #include <afs/com_err.h>
30 #include <afs/audit.h>
32 #include "budb_errs.h"
34 #include "error_macros.h"
35 #include "budb_internal.h"
38 struct ubik_dbase *BU_dbase;
39 struct afsconf_dir *BU_conf; /* for getting cell info */
41 int argHandler(struct cmd_syndesc *, void *);
42 int truncateDatabase(void);
43 int parseServerList(struct cmd_item *);
45 char lcell[MAXKTCREALMLEN];
46 afs_uint32 myHost = 0;
48 static struct logOptions logopts;
50 /* server's global configuration information. This is exported to other
54 buServerConfT globalConf;
55 buServerConfP globalConfPtr = &globalConf;
56 char dbDir[AFSDIR_PATH_MAX], cellConfDir[AFSDIR_PATH_MAX];
57 /* debugging control */
66 #define ADDRSPERSITE 16 /* Same global is in rx/rx_user.c */
67 afs_uint32 SHostAddrs[ADDRSPERSITE];
69 /* check whether caller is authorized to manage RX statistics */
71 BU_rxstat_userok(struct rx_call *call)
73 return afsconf_SuperUser(BU_conf, call, NULL);
77 * Return true if this name is a member of the local realm.
80 BU_IsLocalRealmMatch(void *rock, char *name, char *inst, char *cell)
82 struct afsconf_dir *dir = (struct afsconf_dir *)rock;
83 afs_int32 islocal = 0; /* default to no */
86 code = afsconf_IsLocalRealmMatch(dir, &islocal, name, inst, cell);
88 LogError(code, "Failed local realm check; name=%s, inst=%s, cell=%s\n",
95 convert_cell_to_ubik(struct afsconf_cell *cellinfo, afs_uint32 *myHost,
96 afs_uint32 *serverList)
103 gethostname(hostname, sizeof(hostname));
104 th = gethostbyname(hostname);
106 printf("prserver: couldn't get address of this host.\n");
109 memcpy(myHost, th->h_addr, sizeof(afs_uint32));
111 for (i = 0; i < cellinfo->numServers; i++)
112 /* omit my host from serverList */
113 if (cellinfo->hostAddr[i].sin_addr.s_addr != *myHost)
114 *serverList++ = cellinfo->hostAddr[i].sin_addr.s_addr;
116 *serverList = 0; /* terminate list */
121 * The whole purpose of MyBeforeProc is to detect
122 * if the -help option was not within the command line.
123 * If it were, this routine would never have been called.
126 MyBeforeProc(struct cmd_syndesc *as, void *arock)
132 /* initializeCommands
133 * initialize all the supported commands and their arguments
137 initializeArgHandler(void)
139 struct cmd_syndesc *cptr;
141 cmd_SetBeforeProc(MyBeforeProc, NULL);
143 cptr = cmd_CreateSyntax(NULL, argHandler, NULL, 0, "Backup database server");
145 cmd_AddParm(cptr, "-database", CMD_SINGLE, CMD_OPTIONAL,
146 "database directory");
148 cmd_AddParm(cptr, "-cellservdb", CMD_SINGLE, CMD_OPTIONAL,
149 "cell configuration directory");
151 cmd_AddParm(cptr, "-resetdb", CMD_FLAG, CMD_OPTIONAL,
152 "truncate the database");
154 cmd_AddParm(cptr, "-noauth", CMD_FLAG, CMD_OPTIONAL,
155 "run without authentication");
157 cmd_AddParm(cptr, "-smallht", CMD_FLAG, CMD_OPTIONAL,
158 "use small hash tables");
160 cmd_AddParm(cptr, "-servers", CMD_LIST, CMD_OPTIONAL,
161 "list of ubik database servers");
163 cmd_AddParm(cptr, "-ubikbuffers", CMD_SINGLE, CMD_OPTIONAL,
164 "the number of ubik buffers");
166 cmd_AddParm(cptr, "-auditlog", CMD_SINGLE, CMD_OPTIONAL,
169 cmd_AddParm(cptr, "-p", CMD_SINGLE, CMD_OPTIONAL,
170 "number of processes");
172 cmd_AddParm(cptr, "-rxbind", CMD_FLAG, CMD_OPTIONAL,
173 "bind the Rx socket (primary interface only)");
175 cmd_AddParm(cptr, "-audit-interface", CMD_SINGLE, CMD_OPTIONAL,
176 "audit interface (file or sysvmq)");
178 cmd_AddParm(cptr, "-transarc-logs", CMD_FLAG, CMD_OPTIONAL,
179 "enable Transarc style logging");
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 = strdup(as->parms[0].items->data);
191 if (globalConfPtr->databaseDirectory == 0)
195 /* -cellservdb, cell configuration directory */
196 if (as->parms[1].items != 0) {
197 globalConfPtr->cellConfigdir = strdup(as->parms[1].items->data);
198 if (globalConfPtr->cellConfigdir == 0)
201 globalConfPtr->debugFlags |= DF_RECHECKNOAUTH;
204 /* truncate the database */
205 if (as->parms[2].items != 0)
208 /* run without authentication */
209 if (as->parms[3].items != 0)
210 globalConfPtr->debugFlags |= DF_NOAUTH;
212 /* use small hash tables */
213 if (as->parms[4].items != 0)
214 globalConfPtr->debugFlags |= DF_SMALLHT;
216 /* user provided list of ubik database servers */
217 if (as->parms[5].items != 0)
218 parseServerList(as->parms[5].items);
220 /* user provided the number of ubik buffers */
221 if (as->parms[6].items != 0)
222 ubik_nBuffers = atoi(as->parms[6].items->data);
226 /* param 7 (-auditlog) handled below */
228 /* user provided the number of threads */
229 if (as->parms[8].items != 0) {
230 lwps = atoi(as->parms[8].items->data);
232 printf ("Warning: '-p %d' is too big; using %d instead\n",
237 printf ("Warning: '-p %d' is too small; using %d instead\n",
243 /* user provided rxbind option */
244 if (as->parms[9].items != 0) {
248 /* -audit-interface */
249 if (as->parms[10].items != 0) {
250 char *interface = as->parms[10].items->data;
252 if (osi_audit_interface(interface)) {
253 printf("Invalid audit interface '%s'\n", interface);
258 if (as->parms[11].items != 0) {
259 logopts.lopt_rotateOnOpen = 1;
260 logopts.lopt_rotateStyle = logRotate_old;
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 = 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 */
326 * truncates just the database file.
330 truncateDatabase(void)
336 r = asprintf(&path, "%s%s%s",
337 globalConfPtr->databaseDirectory,
338 globalConfPtr->databaseName,
339 globalConfPtr->databaseExtension);
340 if (r < 0 || path == NULL)
343 fd = open(path, O_RDWR, 0755);
347 if (ftruncate(fd, 0) != 0) {
362 #include "AFS_component_version_number.c"
365 main(int argc, char **argv)
367 char *whoami = argv[0];
369 struct afsconf_cell cellinfo_s;
370 struct afsconf_cell *cellinfo = NULL;
373 afs_uint32 host = ntohl(INADDR_ANY);
376 char clones[MAXHOSTSPERCELL];
378 struct rx_service *tservice;
379 struct rx_securityClass **securityClasses;
380 afs_int32 numClasses;
382 extern int rx_stackSize;
385 /* initialize winsock */
386 if (afs_winsockInit() < 0) {
387 ReportErrorEventAlt(AFSEVT_SVR_WINSOCK_INIT_FAILED, 0, argv[0], 0);
388 fprintf(stderr, "%s: Couldn't initialize winsock.\n", whoami);
395 * The following signal action for AIX is necessary so that in case of a
396 * crash (i.e. core is generated) we can include the user's data section
397 * in the core dump. Unfortunately, by default, only a partial core is
398 * generated which, in many cases, isn't too useful.
400 struct sigaction nsa;
402 sigemptyset(&nsa.sa_mask);
403 nsa.sa_handler = SIG_DFL;
404 nsa.sa_flags = SA_FULLDUMP;
405 sigaction(SIGSEGV, &nsa, NULL);
406 sigaction(SIGABRT, &nsa, NULL);
409 memset(&cellinfo_s, 0, sizeof(cellinfo_s));
410 memset(clones, 0, sizeof(clones));
412 memset(&logopts, 0, sizeof(logopts));
413 logopts.lopt_dest = logDest_file;
414 logopts.lopt_filename = AFSDIR_SERVER_BUDBLOG_FILEPATH;
417 osi_audit(BUDB_StartEvent, 0, AUD_END);
419 initialize_BUDB_error_table();
420 initializeArgHandler();
422 /* Initialize dirpaths */
423 if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
425 ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
427 afs_com_err(whoami, errno, "; Unable to obtain AFS server directory.");
431 memset(globalConfPtr, 0, sizeof(*globalConfPtr));
433 /* set default configuration values */
434 strcpy(dbDir, AFSDIR_SERVER_DB_DIRPATH);
436 globalConfPtr->databaseDirectory = dbDir;
437 globalConfPtr->databaseName = DEFAULT_DBPREFIX;
438 strcpy(cellConfDir, AFSDIR_SERVER_ETC_DIRPATH);
439 globalConfPtr->cellConfigdir = cellConfDir;
443 #ifdef AFS_PTHREAD_ENV
444 SetLogThreadNumProgram( rx_GetThreadNum );
447 /* process the user supplied args */
449 code = cmd_Dispatch(argc, argv);
453 /* exit if there was a help option */
457 /* open the log file */
460 /* open the cell's configuration directory */
461 LogDebug(4, "opening %s\n", globalConfPtr->cellConfigdir);
463 BU_conf = afsconf_Open(globalConfPtr->cellConfigdir);
465 LogError(code, "Failed getting cell info\n");
466 afs_com_err(whoami, code, "Failed getting cell info");
470 code = afsconf_GetLocalCell(BU_conf, lcell, sizeof(lcell));
472 LogError(0, "** Can't determine local cell name!\n");
476 if (globalConfPtr->myHost == 0) {
477 /* if user hasn't supplied a list of servers, extract server
478 * list from the cell's database
481 cellinfo = &cellinfo_s;
483 LogDebug(1, "Using server list from %s cell database.\n", lcell);
485 code = afsconf_GetExtendedCellInfo (BU_conf, lcell, 0, cellinfo,
488 LogError(0, "Can't read cell information\n");
493 convert_cell_to_ubik(cellinfo, &globalConfPtr->myHost,
494 globalConfPtr->serverList);
499 /* initialize audit user check */
500 osi_audit_set_user_check(BU_conf, BU_IsLocalRealmMatch);
502 /* initialize ubik */
503 ubik_SetClientSecurityProcs(afsconf_ClientAuth, afsconf_UpToDate, BU_conf);
504 ubik_SetServerSecurityProcs(afsconf_BuildServerSecurityObjects,
505 afsconf_CheckAuth, BU_conf);
507 if (ubik_nBuffers == 0)
510 LogError(0, "Will allocate %d ubik buffers\n", ubik_nBuffers);
512 r = asprintf(&dbNamePtr, "%s%s", globalConfPtr->databaseDirectory,
513 globalConfPtr->databaseName);
514 if (r < 0 || dbNamePtr == 0)
517 rx_SetRxDeadTime(60); /* 60 seconds inactive before timeout */
521 if (AFSDIR_SERVER_NETRESTRICT_FILEPATH ||
522 AFSDIR_SERVER_NETINFO_FILEPATH) {
524 ccode = afsconf_ParseNetFiles(SHostAddrs, NULL, NULL,
525 ADDRSPERSITE, reason,
526 AFSDIR_SERVER_NETINFO_FILEPATH,
527 AFSDIR_SERVER_NETRESTRICT_FILEPATH);
530 ccode = rx_getAllAddr(SHostAddrs, ADDRSPERSITE);
533 host = SHostAddrs[0];
534 rx_InitHost(host, htons(AFSCONF_BUDBPORT));
538 /* Disable jumbograms */
542 code = ubik_ServerInitByInfo(globalConfPtr->myHost,
543 htons(AFSCONF_BUDBPORT),
546 dbNamePtr, /* name prefix */
549 code = ubik_ServerInit(globalConfPtr->myHost, htons(AFSCONF_BUDBPORT),
550 globalConfPtr->serverList,
551 dbNamePtr, /* name prefix */
556 LogError(code, "Ubik init failed\n");
557 afs_com_err(whoami, code, "Ubik init failed");
561 afsconf_BuildServerSecurityObjects(BU_conf, &securityClasses, &numClasses);
564 rx_NewServiceHost(host, 0, BUDB_SERVICE, "BackupDatabase",
565 securityClasses, numClasses, BUDB_ExecuteRequest);
567 if (tservice == (struct rx_service *)0) {
568 LogError(0, "Could not create backup database rx service\n");
569 printf("Could not create backup database rx service\n");
572 rx_SetMinProcs(tservice, 1);
573 rx_SetMaxProcs(tservice, lwps);
574 rx_SetStackSize(tservice, 10000);
576 /* allow super users to manage RX statistics */
577 rx_SetRxStatUserOk(BU_rxstat_userok);
579 /* misc. initialization */
581 /* database dump synchronization */
582 memset(dumpSyncPtr, 0, sizeof(*dumpSyncPtr));
583 Lock_Init(&dumpSyncPtr->ds_lock);
585 rx_StartServer(0); /* start handling requests */
592 currentTime = time(0);
593 LogError(0, "Ready to process requests at %s\n", ctime(¤tTime));
595 rx_ServerProc(NULL); /* donate this LWP */
598 osi_audit(BUDB_FinishEvent, code, AUD_END);
603 consistencyCheckDb(void)
605 /* do consistency checks on structure sizes */
606 if ((sizeof(struct htBlock) > BLOCKSIZE)
607 || (sizeof(struct vfBlock) > BLOCKSIZE)
608 || (sizeof(struct viBlock) > BLOCKSIZE)
609 || (sizeof(struct dBlock) > BLOCKSIZE)
610 || (sizeof(struct tBlock) > BLOCKSIZE)
612 fprintf(stderr, "Block layout error!\n");
618 LogDebug(int level, char *fmt, ... )
620 if (debugging >= level) {
639 LogError(long code, char *fmt, ... )
646 len = vsnprintf(buffer, sizeof(buffer), fmt, ap);
652 /* Be consistent with (unintentional?) historic behavior. */
654 FSLog("%s: %s\n", afs_error_table_name(code), afs_error_message(code));
655 WriteLogBuffer(buffer, len);