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/authcon.h>
28 #include <afs/bubasics.h>
29 #include <afs/afsutil.h>
30 #include <afs/com_err.h>
31 #include <afs/audit.h>
33 #include "budb_errs.h"
35 #include "error_macros.h"
36 #include "budb_internal.h"
39 struct ubik_dbase *BU_dbase;
40 struct afsconf_dir *BU_conf; /* for getting cell info */
42 int argHandler(struct cmd_syndesc *, void *);
43 int truncateDatabase(void);
44 int parseServerList(struct cmd_item *);
46 char lcell[MAXKTCREALMLEN];
47 afs_uint32 myHost = 0;
49 static struct logOptions logopts;
51 /* server's global configuration information. This is exported to other
55 buServerConfT globalConf;
56 buServerConfP globalConfPtr = &globalConf;
57 char dbDir[AFSDIR_PATH_MAX], cellConfDir[AFSDIR_PATH_MAX];
58 /* debugging control */
67 #define ADDRSPERSITE 16 /* Same global is in rx/rx_user.c */
68 afs_uint32 SHostAddrs[ADDRSPERSITE];
70 /* check whether caller is authorized to manage RX statistics */
72 BU_rxstat_userok(struct rx_call *call)
74 return afsconf_SuperUser(BU_conf, call, NULL);
78 * Return true if this name is a member of the local realm.
81 BU_IsLocalRealmMatch(void *rock, char *name, char *inst, char *cell)
83 struct afsconf_dir *dir = (struct afsconf_dir *)rock;
84 afs_int32 islocal = 0; /* default to no */
87 code = afsconf_IsLocalRealmMatch(dir, &islocal, name, inst, cell);
89 LogError(code, "Failed local realm check; name=%s, inst=%s, cell=%s\n",
96 convert_cell_to_ubik(struct afsconf_cell *cellinfo, afs_uint32 *myHost,
97 afs_uint32 *serverList)
104 gethostname(hostname, sizeof(hostname));
105 th = gethostbyname(hostname);
107 printf("prserver: couldn't get address of this host.\n");
110 memcpy(myHost, th->h_addr, sizeof(afs_uint32));
112 for (i = 0; i < cellinfo->numServers; i++)
113 /* omit my host from serverList */
114 if (cellinfo->hostAddr[i].sin_addr.s_addr != *myHost)
115 *serverList++ = cellinfo->hostAddr[i].sin_addr.s_addr;
117 *serverList = 0; /* terminate list */
122 * The whole purpose of MyBeforeProc is to detect
123 * if the -help option was not within the command line.
124 * If it were, this routine would never have been called.
127 MyBeforeProc(struct cmd_syndesc *as, void *arock)
133 /* initializeCommands
134 * initialize all the supported commands and their arguments
138 initializeArgHandler(void)
140 struct cmd_syndesc *cptr;
142 cmd_SetBeforeProc(MyBeforeProc, NULL);
144 cptr = cmd_CreateSyntax(NULL, argHandler, NULL, 0, "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_LIST, CMD_OPTIONAL,
168 "[interface:]path[:options]");
170 cmd_AddParm(cptr, "-p", CMD_SINGLE, CMD_OPTIONAL,
171 "number of processes");
173 cmd_AddParm(cptr, "-rxbind", CMD_FLAG, CMD_OPTIONAL,
174 "bind the Rx socket (primary interface only)");
176 cmd_AddParm(cptr, "-audit-interface", CMD_SINGLE, CMD_OPTIONAL,
177 "default interface");
179 cmd_AddParm(cptr, "-transarc-logs", CMD_FLAG, CMD_OPTIONAL,
180 "enable Transarc style logging");
184 argHandler(struct cmd_syndesc *as, void *arock)
187 char *auditIface = NULL;
189 /* globalConfPtr provides the handle for the configuration information */
191 /* database directory */
192 if (as->parms[0].items != 0) {
193 globalConfPtr->databaseDirectory = strdup(as->parms[0].items->data);
194 if (globalConfPtr->databaseDirectory == 0)
198 /* -cellservdb, cell configuration directory */
199 if (as->parms[1].items != 0) {
200 globalConfPtr->cellConfigdir = strdup(as->parms[1].items->data);
201 if (globalConfPtr->cellConfigdir == 0)
204 globalConfPtr->debugFlags |= DF_RECHECKNOAUTH;
207 /* truncate the database */
208 if (as->parms[2].items != 0)
211 /* run without authentication */
212 if (as->parms[3].items != 0)
213 globalConfPtr->debugFlags |= DF_NOAUTH;
215 /* use small hash tables */
216 if (as->parms[4].items != 0)
217 globalConfPtr->debugFlags |= DF_SMALLHT;
219 /* user provided list of ubik database servers */
220 if (as->parms[5].items != 0)
221 parseServerList(as->parms[5].items);
223 /* user provided the number of ubik buffers */
224 if (as->parms[6].items != 0)
225 ubik_nBuffers = atoi(as->parms[6].items->data);
229 /* param 7 (-auditlog) handled below */
231 /* user provided the number of threads */
232 if (as->parms[8].items != 0) {
233 lwps = atoi(as->parms[8].items->data);
235 printf ("Warning: '-p %d' is too big; using %d instead\n",
240 printf ("Warning: '-p %d' is too small; using %d instead\n",
246 /* user provided rxbind option */
247 if (as->parms[9].items != 0) {
251 /* -audit-interface and -auditlog */
252 if (as->parms[10].items != 0)
253 auditIface = as->parms[10].items->data;
255 if (as->parms[7].items != 0) {
257 code = osi_audit_cmd_Options(auditIface, as->parms[7].items);
263 if (as->parms[11].items != 0) {
264 logopts.lopt_rotateOnOpen = 1;
265 logopts.lopt_rotateStyle = logRotate_old;
274 parseServerList(struct cmd_item *itemPtr)
276 struct cmd_item *save;
279 afs_int32 nservers = 0;
284 /* compute number of servers in the list */
287 itemPtr = itemPtr->next;
290 LogDebug(3, "%d servers\n", nservers);
292 /* now can allocate the space for the server arguments */
293 serverArgs = malloc((nservers + 2) * sizeof(char *));
301 /* now go through and construct the list of servers */
304 *ptr++ = itemPtr->data;
305 itemPtr = itemPtr->next;
309 ubik_ParseServerList(nservers + 2, serverArgs, &globalConfPtr->myHost,
310 globalConfPtr->serverList);
314 /* free space for the server args */
322 * truncates just the database file.
326 truncateDatabase(void)
332 r = asprintf(&path, "%s%s%s",
333 globalConfPtr->databaseDirectory,
334 globalConfPtr->databaseName,
335 globalConfPtr->databaseExtension);
336 if (r < 0 || path == NULL)
339 fd = open(path, O_RDWR, 0755);
343 if (ftruncate(fd, 0) != 0) {
358 #include "AFS_component_version_number.c"
361 main(int argc, char **argv)
363 char *whoami = argv[0];
365 struct afsconf_cell cellinfo_s;
366 struct afsconf_cell *cellinfo = NULL;
370 afs_uint32 host = ntohl(INADDR_ANY);
373 char clones[MAXHOSTSPERCELL];
375 struct rx_service *tservice;
376 struct rx_securityClass **securityClasses;
377 afs_int32 numClasses;
378 struct afsconf_bsso_info bsso;
380 extern int rx_stackSize;
383 /* initialize winsock */
384 if (afs_winsockInit() < 0) {
385 ReportErrorEventAlt(AFSEVT_SVR_WINSOCK_INIT_FAILED, 0, argv[0], 0);
386 fprintf(stderr, "%s: Couldn't initialize winsock.\n", whoami);
393 * The following signal action for AIX is necessary so that in case of a
394 * crash (i.e. core is generated) we can include the user's data section
395 * in the core dump. Unfortunately, by default, only a partial core is
396 * generated which, in many cases, isn't too useful.
398 struct sigaction nsa;
400 sigemptyset(&nsa.sa_mask);
401 nsa.sa_handler = SIG_DFL;
402 nsa.sa_flags = SA_FULLDUMP;
403 sigaction(SIGSEGV, &nsa, NULL);
404 sigaction(SIGABRT, &nsa, NULL);
407 memset(&cellinfo_s, 0, sizeof(cellinfo_s));
408 memset(clones, 0, sizeof(clones));
409 memset(&bsso, 0, sizeof(bsso));
411 memset(&logopts, 0, sizeof(logopts));
412 logopts.lopt_dest = logDest_file;
413 logopts.lopt_filename = AFSDIR_SERVER_BUDBLOG_FILEPATH;
417 initialize_BUDB_error_table();
418 initializeArgHandler();
420 /* Initialize dirpaths */
421 if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
423 ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
425 afs_com_err(whoami, errno, "; Unable to obtain AFS server directory.");
429 memset(globalConfPtr, 0, sizeof(*globalConfPtr));
431 /* set default configuration values */
432 strcpy(dbDir, AFSDIR_SERVER_DB_DIRPATH);
434 globalConfPtr->databaseDirectory = dbDir;
435 globalConfPtr->databaseName = DEFAULT_DBPREFIX;
436 strcpy(cellConfDir, AFSDIR_SERVER_ETC_DIRPATH);
437 globalConfPtr->cellConfigdir = cellConfDir;
441 #ifdef AFS_PTHREAD_ENV
442 SetLogThreadNumProgram( rx_GetThreadNum );
445 /* process the user supplied args */
447 code = cmd_Dispatch(argc, argv);
451 /* exit if there was a help option */
457 osi_audit(BUDB_StartEvent, 0, AUD_END);
459 /* open the log file */
462 /* open the cell's configuration directory */
463 LogDebug(4, "opening %s\n", globalConfPtr->cellConfigdir);
465 BU_conf = afsconf_Open(globalConfPtr->cellConfigdir);
467 LogError(code, "Failed getting cell info\n");
468 afs_com_err(whoami, code, "Failed getting cell info");
472 code = afsconf_GetLocalCell(BU_conf, lcell, sizeof(lcell));
474 LogError(0, "** Can't determine local cell name!\n");
478 if (globalConfPtr->myHost == 0) {
479 /* if user hasn't supplied a list of servers, extract server
480 * list from the cell's database
483 cellinfo = &cellinfo_s;
485 LogDebug(1, "Using server list from %s cell database.\n", lcell);
487 code = afsconf_GetExtendedCellInfo (BU_conf, lcell, 0, cellinfo,
490 LogError(0, "Can't read cell information\n");
495 convert_cell_to_ubik(cellinfo, &globalConfPtr->myHost,
496 globalConfPtr->serverList);
501 /* initialize audit user check */
502 osi_audit_set_user_check(BU_conf, BU_IsLocalRealmMatch);
504 /* initialize ubik */
505 ubik_SetClientSecurityProcs(afsconf_ClientAuth, afsconf_UpToDate, BU_conf);
506 ubik_SetServerSecurityProcs(afsconf_BuildServerSecurityObjects,
507 afsconf_CheckAuth, BU_conf);
509 if (ubik_nBuffers == 0)
512 LogError(0, "Will allocate %d ubik buffers\n", ubik_nBuffers);
514 r = asprintf(&dbNamePtr, "%s%s", globalConfPtr->databaseDirectory,
515 globalConfPtr->databaseName);
516 if (r < 0 || dbNamePtr == 0)
519 rx_SetRxDeadTime(60); /* 60 seconds inactive before timeout */
523 if (AFSDIR_SERVER_NETRESTRICT_FILEPATH ||
524 AFSDIR_SERVER_NETINFO_FILEPATH) {
526 ccode = afsconf_ParseNetFiles(SHostAddrs, NULL, NULL,
527 ADDRSPERSITE, reason,
528 AFSDIR_SERVER_NETINFO_FILEPATH,
529 AFSDIR_SERVER_NETRESTRICT_FILEPATH);
532 ccode = rx_getAllAddr(SHostAddrs, ADDRSPERSITE);
535 host = SHostAddrs[0];
539 Log("buserver binding rx to %s:%d\n",
540 afs_inet_ntoa_r(host, hoststr), AFSCONF_BUDBPORT);
541 code = rx_InitHost(host, htons(AFSCONF_BUDBPORT));
543 LogError(code, "rx init failed\n");
544 afs_com_err(whoami, code, "rx init failed");
548 /* Disable jumbograms */
552 code = ubik_ServerInitByInfo(globalConfPtr->myHost,
553 htons(AFSCONF_BUDBPORT),
556 dbNamePtr, /* name prefix */
559 code = ubik_ServerInit(globalConfPtr->myHost, htons(AFSCONF_BUDBPORT),
560 globalConfPtr->serverList,
561 dbNamePtr, /* name prefix */
566 LogError(code, "Ubik init failed\n");
567 afs_com_err(whoami, code, "Ubik init failed");
573 afsconf_BuildServerSecurityObjects_int(&bsso, &securityClasses,
577 rx_NewServiceHost(host, 0, BUDB_SERVICE, "BackupDatabase",
578 securityClasses, numClasses, BUDB_ExecuteRequest);
580 if (tservice == (struct rx_service *)0) {
581 LogError(0, "Could not create backup database rx service\n");
582 printf("Could not create backup database rx service\n");
585 rx_SetMinProcs(tservice, 1);
586 rx_SetMaxProcs(tservice, lwps);
587 rx_SetStackSize(tservice, 10000);
589 /* allow super users to manage RX statistics */
590 rx_SetRxStatUserOk(BU_rxstat_userok);
592 /* misc. initialization */
594 /* database dump synchronization */
595 memset(dumpSyncPtr, 0, sizeof(*dumpSyncPtr));
596 Lock_Init(&dumpSyncPtr->ds_lock);
598 rx_StartServer(0); /* start handling requests */
605 currentTime = time(0);
606 LogError(0, "Ready to process requests at %s\n", ctime(¤tTime));
608 rx_ServerProc(NULL); /* donate this LWP */
611 osi_audit(BUDB_FinishEvent, code, AUD_END);
616 consistencyCheckDb(void)
618 /* do consistency checks on structure sizes */
619 if ((sizeof(struct htBlock) > BLOCKSIZE)
620 || (sizeof(struct vfBlock) > BLOCKSIZE)
621 || (sizeof(struct viBlock) > BLOCKSIZE)
622 || (sizeof(struct dBlock) > BLOCKSIZE)
623 || (sizeof(struct tBlock) > BLOCKSIZE)
625 fprintf(stderr, "Block layout error!\n");
631 LogDebug(int level, char *fmt, ... )
633 if (debugging >= level) {
652 LogError(long code, char *fmt, ... )
659 len = vsnprintf(buffer, sizeof(buffer), fmt, ap);
665 /* Be consistent with (unintentional?) historic behavior. */
667 FSLog("%s: %s\n", afs_error_table_name(code), afs_error_message(code));
668 WriteLogBuffer(buffer, len);