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>
35 #include <sys/types.h>
44 #include <rx/rx_globals.h>
45 #include <afs/cellconfig.h>
47 #include <afs/bubasics.h>
48 #include <afs/afsutil.h>
49 #include <afs/com_err.h>
54 #include "budb_errs.h"
56 #include "error_macros.h"
58 #include "afs/audit.h"
61 extern afs_int32 ubik_lastYesTime;
62 extern afs_int32 ubik_nBuffers;
64 struct ubik_dbase *BU_dbase;
65 struct afsconf_dir *BU_conf; /* for getting cell info */
67 char lcell[MAXKTCREALMLEN];
71 /* server's global configuration information. This is exported to other
75 buServerConfT globalConf;
76 buServerConfP globalConfPtr = &globalConf;
77 char dbDir[AFSDIR_PATH_MAX], cellConfDir[AFSDIR_PATH_MAX];
78 /* debugging control */
81 /* check whether caller is authorized to manage RX statistics */
82 int BU_rxstat_userok(call)
85 return afsconf_SuperUser(BU_conf, call, NULL);
89 convert_cell_to_ubik (cellinfo, myHost, serverList)
90 struct afsconf_cell *cellinfo;
92 afs_int32 *serverList;
99 gethostname(hostname,sizeof(hostname));
100 th = gethostbyname(hostname);
103 printf("prserver: couldn't get address of this host.\n");
106 memcpy(myHost, th->h_addr, sizeof(afs_int32));
108 for (i=0; i<cellinfo->numServers; i++)
109 /* omit my host from serverList */
110 if (cellinfo->hostAddr[i].sin_addr.s_addr != *myHost)
111 *serverList++ = cellinfo->hostAddr[i].sin_addr.s_addr;
113 *serverList = 0; /* terminate list */
118 * The whole purpose of MyBeforeProc is to detect
119 * if the -help option was not within the command line.
120 * If it were, this routine would never have been called.
122 static int MyBeforeProc(as)
123 register struct cmd_syndesc *as;
129 /* initializeCommands
130 * initialize all the supported commands and their arguments
133 initializeArgHandler()
135 struct cmd_syndesc *cptr;
139 cmd_SetBeforeProc(MyBeforeProc, NULL);
141 cptr = cmd_CreateSyntax(NULL, argHandler, NULL,
142 "Backup database server");
144 cmd_AddParm(cptr, "-database", CMD_SINGLE, CMD_OPTIONAL,
145 "database directory");
147 cmd_AddParm(cptr, "-cellservdb", CMD_SINGLE, CMD_OPTIONAL,
148 "cell configuration directory");
150 cmd_AddParm(cptr, "-resetdb", CMD_FLAG, CMD_OPTIONAL,
151 "truncate the database");
153 cmd_AddParm(cptr, "-noauth", CMD_FLAG, CMD_OPTIONAL,
154 "run without authentication");
156 cmd_AddParm(cptr, "-smallht", CMD_FLAG, CMD_OPTIONAL,
157 "use small hash tables");
159 cmd_AddParm(cptr, "-servers", CMD_LIST, CMD_OPTIONAL,
160 "list of ubik database servers");
164 argHandler(as, arock)
165 struct cmd_syndesc *as;
169 /* globalConfPtr provides the handle for the configuration information */
171 /* database directory */
172 if ( as->parms[0].items != 0 )
174 globalConfPtr->databaseDirectory =
175 (char *) malloc(strlen(as->parms[0].items->data)+1);
176 if ( globalConfPtr->databaseDirectory == 0 )
178 strcpy(globalConfPtr->databaseDirectory, as->parms[0].items->data);
181 /* -cellservdb, cell configuration directory */
182 if ( as->parms[1].items != 0 )
184 globalConfPtr->cellConfigdir =
185 (char *) malloc(strlen(as->parms[1].items->data)+1);
186 if ( globalConfPtr->cellConfigdir == 0 )
189 strcpy(globalConfPtr->cellConfigdir, as->parms[1].items->data);
191 globalConfPtr->debugFlags |= DF_RECHECKNOAUTH;
194 /* truncate the database */
195 if ( as->parms[2].items != 0 )
198 /* run without authentication */
199 if ( as->parms[3].items != 0 )
200 globalConfPtr->debugFlags |= DF_NOAUTH;
202 /* use small hash tables */
203 if ( as->parms[4].items != 0 )
204 globalConfPtr->debugFlags |= DF_SMALLHT;
206 /* user provided list of ubik database servers */
207 if ( as->parms[5].items != 0 )
208 parseServerList(as->parms[5].items);
215 parseServerList(itemPtr)
216 struct cmd_item *itemPtr;
218 struct cmd_item *save;
221 afs_int32 nservers = 0;
226 /* compute number of servers in the list */
230 itemPtr = itemPtr->next;
233 LogDebug(3, "%d servers\n", nservers);
235 /* now can allocate the space for the server arguments */
236 serverArgs = (char **) malloc( (nservers+2) * sizeof(char *) );
237 if ( serverArgs == 0 )
244 /* now go through and construct the list of servers */
248 *ptr++ = itemPtr->data;
249 itemPtr = itemPtr->next;
252 code = ubik_ParseServerList(nservers+2, serverArgs,
253 &globalConfPtr->myHost,
254 globalConfPtr->serverList);
258 /* free space for the server args */
259 free( (char *) serverArgs);
266 * truncates just the database file.
275 path = (char *) malloc(strlen(globalConfPtr->databaseDirectory) +
276 strlen(globalConfPtr->databaseName) +
277 strlen(globalConfPtr->databaseExtension) + 1);
281 /* construct the database name */
282 strcpy(path, globalConfPtr->databaseDirectory);
283 strcat(path, globalConfPtr->databaseName);
284 strcat(path, globalConfPtr->databaseExtension);
286 fd = open(path, O_RDWR, 0755);
290 if (ftruncate(fd, 0) != 0 ) {
303 #include "AFS_component_version_number.c"
309 char *whoami = argv[0];
311 struct afsconf_cell cellinfo;
315 struct rx_service *tservice;
316 struct rx_securityClass *sca[3];
318 extern int afsconf_ServerAuth();
319 extern int afsconf_CheckAuth();
321 extern int rx_stackSize;
322 extern int BUDB_ExecuteRequest();
325 /* initialize winsock */
326 if (afs_winsockInit()<0) {
327 ReportErrorEventAlt(AFSEVT_SVR_WINSOCK_INIT_FAILED, 0, argv[0],0);
328 fprintf(stderr, "%s: Couldn't initialize winsock.\n", whoami);
335 * The following signal action for AIX is necessary so that in case of a
336 * crash (i.e. core is generated) we can include the user's data section
337 * in the core dump. Unfortunately, by default, only a partial core is
338 * generated which, in many cases, isn't too useful.
340 struct sigaction nsa;
342 sigemptyset(&nsa.sa_mask);
343 nsa.sa_handler = SIG_DFL;
344 nsa.sa_flags = SA_FULLDUMP;
345 sigaction(SIGSEGV, &nsa, NULL);
346 sigaction(SIGABRT, &nsa, NULL);
348 osi_audit(BUDB_StartEvent, 0, AUD_END);
350 initialize_BUDB_error_table();
351 initializeArgHandler();
353 /* Initialize dirpaths */
354 if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
356 ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
358 com_err(whoami,errno,"; Unable to obtain AFS server directory.");
362 memset(globalConfPtr, 0, sizeof(*globalConfPtr));
364 /* set default configuration values */
365 strcpy(dbDir, AFSDIR_SERVER_DB_DIRPATH);
367 globalConfPtr->databaseDirectory = dbDir;
368 globalConfPtr->databaseName = DEFAULT_DBPREFIX;
369 strcpy(cellConfDir, AFSDIR_SERVER_ETC_DIRPATH);
370 globalConfPtr->cellConfigdir = cellConfDir;
372 /* open the log file */
374 globalConfPtr->log = fopen(DEFAULT_LOGNAME,"a");
375 if ( globalConfPtr->log == NULL )
377 printf("Can't open log file %s - aborting\n", DEFAULT_LOGNAME);
384 /* process the user supplied args */
386 code = cmd_Dispatch(argc, argv);
390 /* exit if there was a help option */
394 /* open the log file */
395 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
396 if ( globalConfPtr->log == NULL )
398 printf("Can't open log file %s - aborting\n",
399 AFSDIR_SERVER_BUDBLOG_FILEPATH);
403 /* keep log closed so can remove it */
405 fclose(globalConfPtr->log);
407 /* open the cell's configuration directory */
408 LogDebug(4, "opening %s\n", globalConfPtr->cellConfigdir);
410 BU_conf = afsconf_Open(globalConfPtr->cellConfigdir);
413 LogError(code, "Failed getting cell info\n");
414 com_err(whoami, code, "Failed getting cell info");
418 code = afsconf_GetLocalCell(BU_conf, lcell, sizeof(lcell));
421 LogError(0, "** Can't determine local cell name!\n");
425 if ( globalConfPtr->myHost == 0 )
427 /* if user hasn't supplied a list of servers, extract server
428 * list from the cell's database
431 LogDebug(1, "Using server list from %s cell database.\n", lcell);
433 code = afsconf_GetCellInfo (BU_conf, lcell, 0, &cellinfo);
434 code = convert_cell_to_ubik (&cellinfo,
435 &globalConfPtr->myHost,
436 globalConfPtr->serverList);
441 /* initialize ubik */
442 ubik_CRXSecurityProc = afsconf_ClientAuth;
443 ubik_CRXSecurityRock = (char *)BU_conf;
445 ubik_SRXSecurityProc = afsconf_ServerAuth;
446 ubik_SRXSecurityRock = (char *)BU_conf;
448 ubik_CheckRXSecurityProc = afsconf_CheckAuth;
449 ubik_CheckRXSecurityRock = (char *)BU_conf;
453 dbNamePtr = (char *) malloc(strlen(globalConfPtr->databaseDirectory) +
454 strlen(globalConfPtr->databaseName) + 1);
455 if ( dbNamePtr == 0 )
458 /* construct the database name */
459 strcpy(dbNamePtr, globalConfPtr->databaseDirectory);
460 strcat(dbNamePtr, globalConfPtr->databaseName); /* name prefix */
462 rx_SetRxDeadTime(60); /* 60 seconds inactive before timeout */
464 code = ubik_ServerInit(globalConfPtr->myHost,
465 htons(AFSCONF_BUDBPORT),
466 globalConfPtr->serverList,
467 dbNamePtr, /* name prefix */
471 LogError(code, "Ubik init failed\n");
472 com_err(whoami, code, "Ubik init failed");
476 sca[RX_SCINDEX_NULL] = rxnull_NewServerSecurityObject();
477 sca[RX_SCINDEX_VAB] = 0;
478 sca[RX_SCINDEX_KAD] = rxkad_NewServerSecurityObject(rxkad_clear,
483 /* Disable jumbograms */
486 tservice = rx_NewService(0, BUDB_SERVICE, "BackupDatabase",
487 sca, 3, BUDB_ExecuteRequest);
488 if (tservice == (struct rx_service *)0)
490 LogError(0, "Could not create backup database rx service\n");
491 printf("Could not create backup database rx service\n");
494 rx_SetMinProcs(tservice, 1);
495 rx_SetMaxProcs(tservice, 3);
496 rx_SetStackSize(tservice, 10000);
498 /* allow super users to manage RX statistics */
499 rx_SetRxStatUserOk(BU_rxstat_userok);
501 /* misc. initialization */
503 /* database dump synchronization */
504 memset(dumpSyncPtr, 0, sizeof(*dumpSyncPtr));
505 Lock_Init(&dumpSyncPtr->ds_lock);
507 rx_StartServer(0); /* start handling requests */
514 currentTime = time(0);
515 LogError(0, "Ready to process requests at %s\n", ctime(¤tTime));
517 rx_ServerProc(); /* donate this LWP */
520 osi_audit(BUDB_FinishEvent, code, AUD_END);
527 /* do consistency checks on structure sizes */
528 if ( (sizeof(struct htBlock) > BLOCKSIZE)
529 || (sizeof(struct vfBlock) > BLOCKSIZE)
530 || (sizeof(struct viBlock) > BLOCKSIZE)
531 || (sizeof(struct dBlock) > BLOCKSIZE)
532 || (sizeof(struct tBlock) > BLOCKSIZE)
535 fprintf (stderr, "Block layout error!\n");
541 LogDebug(level, a,b,c,d,e,f,g,h,i)
543 char *a, *b, *c, *d, *e, *f, *g, *h, *i;
546 if ( debugging >= level)
548 /* log normally closed so can remove it */
549 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
550 if ( globalConfPtr->log != NULL )
552 fprintf(globalConfPtr->log, a, b, c, d, e, f, g, h, i);
553 fflush(globalConfPtr->log);
554 fclose(globalConfPtr->log);
559 static char *TimeStamp(time_t t)
562 static char timestamp[20];
565 strftime (timestamp, 20, "%m/%d/%Y %T", lt);
570 Log(a,b,c,d,e,f,g,h,i)
571 char *a, *b, *c, *d, *e, *f, *g, *h, *i;
575 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
576 if ( globalConfPtr->log != NULL )
579 fprintf(globalConfPtr->log, "%s ", TimeStamp(now));
581 fprintf(globalConfPtr->log, a, b, c, d, e, f, g, h, i);
582 fflush(globalConfPtr->log);
583 fclose(globalConfPtr->log);
588 LogError(code, a,b,c,d,e,f,g,h,i)
590 char *a, *b, *c, *d, *e, *f, *g, *h, *i;
594 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
596 if ( globalConfPtr->log != NULL )
599 fprintf(globalConfPtr->log, "%s ", TimeStamp(now));
602 fprintf(globalConfPtr->log, "%s: %s\n", error_table_name(code),
603 error_message(code));
604 fprintf(globalConfPtr->log, a, b, c, d, e, f, g, h, i);
605 fflush(globalConfPtr->log);
606 fclose(globalConfPtr->log);
618 struct dump *dumpPtr;
620 struct dump hostDump;
621 extern buServerConfP globalConfPtr;
623 dump_ntoh(dumpPtr, &hostDump);
625 globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
626 if ( globalConfPtr->log != NULL )
628 printDump(globalConfPtr->log, &hostDump);
629 fclose(globalConfPtr->log);