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>
12 #include <sys/types.h>
23 #include <WINNT/afsevent.h>
26 #include <netinet/in.h>
27 #endif /* AFS_NT40_ENV */
28 #include <afs/cellconfig.h>
31 #include <rx/rx_globals.h>
36 #include <afs/ktime.h>
37 #include <afs/afsutil.h>
38 #include <afs/fileutil.h>
39 #include <afs/procmgmt.h> /* signal(), kill(), wait(), etc. */
40 #if defined(AFS_SGI_ENV)
41 #include <afs/afs_args.h>
45 #define BOZO_LWP_STACKSIZE 16000
46 extern int BOZO_ExecuteRequest();
47 extern int RXSTATS_ExecuteRequest();
48 extern int afsconf_GetKey();
49 extern struct bnode_ops fsbnode_ops, ezbnode_ops, cronbnode_ops;
50 struct afsconf_dir *bozo_confdir = 0; /* bozo configuration dir */
51 static char *bozo_pid;
52 struct rx_securityClass *bozo_rxsc[3];
53 const char *bozo_fileName;
55 extern int rx_stackSize; /* for rx_SetStackSize macro */
58 static afs_int32 nextRestart;
59 static afs_int32 nextDay;
61 struct ktime bozo_nextRestartKT, bozo_nextDayKT;
63 #ifdef BOS_RESTRICTED_MODE
64 int bozo_isrestricted=0;
65 int bozo_restdisable=0;
67 void bozo_insecureme(int sig)
69 signal(SIGFPE, bozo_insecureme);
79 /* check whether caller is authorized to manage RX statistics */
80 int bozo_rxstat_userok(call)
83 return afsconf_SuperUser(bozo_confdir, call, (char *)0);
86 /* restart bozo process */
89 /* exit with restart code; SCM integrator process will restart bosserver */
90 int status = BOSEXIT_RESTART;
92 /* if noauth flag is set, pass "-noauth" to new bosserver */
93 if (afsconf_GetNoAuthFlag(bozo_confdir)) {
94 status |= BOSEXIT_NOAUTH_FLAG;
96 /* if logging is on, pass "-log" to new bosserver */
98 status |= BOSEXIT_LOGGING_FLAG;
102 /* exec new bosserver process */
106 argv[i] = (char *)AFSDIR_SERVER_BOSVR_FILEPATH;
109 /* if noauth flag is set, pass "-noauth" to new bosserver */
110 if (afsconf_GetNoAuthFlag(bozo_confdir)) {
114 /* if logging is on, pass "-log" to new bosserver */
120 /* null-terminate argument list */
123 /* close random fd's */
124 for (i = 3; i < 64; i++) {
128 execv(argv[0], argv); /* should not return */
130 #endif /* AFS_NT40_ENV */
133 /* make sure a dir exists */
135 register char *adir; {
137 register afs_int32 code;
138 if (stat(adir, &tstat) < 0 || (tstat.st_mode & S_IFMT) != S_IFDIR) {
141 reqPerm = GetRequiredDirPerm (adir);
142 if (reqPerm == -1) reqPerm = 0777;
144 /* underlying filesystem may not support directory protection */
147 code = mkdir(adir, reqPerm);
154 /* create all the bozo dirs */
155 static CreateDirs() {
157 MakeDir(AFSDIR_USR_DIRPATH);
158 MakeDir(AFSDIR_SERVER_AFS_DIRPATH);
159 MakeDir(AFSDIR_SERVER_BIN_DIRPATH);
160 MakeDir(AFSDIR_SERVER_ETC_DIRPATH);
161 MakeDir(AFSDIR_SERVER_LOCAL_DIRPATH);
162 MakeDir(AFSDIR_SERVER_DB_DIRPATH);
163 MakeDir(AFSDIR_SERVER_LOGS_DIRPATH);
165 MakeDir(AFSDIR_CLIENT_VICE_DIRPATH);
166 MakeDir(AFSDIR_CLIENT_ETC_DIRPATH);
168 symlink(AFSDIR_SERVER_THISCELL_FILEPATH, AFSDIR_CLIENT_THISCELL_FILEPATH);
169 symlink(AFSDIR_SERVER_CELLSERVDB_FILEPATH, AFSDIR_CLIENT_CELLSERVDB_FILEPATH);
170 #endif /* AFS_NT40_ENV */
174 /* strip the \\n from the end of the line, if it is present */
175 static StripLine(abuffer)
176 register char *abuffer; {
179 tp = abuffer + strlen(abuffer); /* starts off pointing at the null */
180 if(tp == abuffer) return 0; /* null string, no last character to check */
181 tp--; /* aim at last character */
182 if (*tp == '\n') *tp = 0;
186 /* write one bnode's worth of entry into the file */
187 static bzwrite(abnode, at)
188 register struct bnode *abnode;
189 register struct bztemp *at; {
191 char tbuffer[BOZO_BSSIZE];
192 register afs_int32 code;
194 if (abnode->notifier)
195 fprintf(at->file, "bnode %s %s %d %s\n",
196 abnode->type->name, abnode->name, abnode->fileGoal, abnode->notifier);
198 fprintf(at->file, "bnode %s %s %d\n", abnode->type->name, abnode->name, abnode->fileGoal);
200 code = bnode_GetParm(abnode, i, tbuffer, BOZO_BSSIZE);
202 if (code != BZDOM) return code;
205 fprintf(at->file, "parm %s\n", tbuffer);
207 fprintf(at->file, "end\n");
214 register FILE *tfile;
215 char tbuffer[BOZO_BSSIZE];
217 char *instp, *typep, *notifier, *notp;
218 register afs_int32 code;
219 afs_int32 ktmask, ktday, kthour, ktmin, ktsec;
222 char *parms[MAXPARMS];
223 #ifdef BOS_RESTRICTED_MODE
227 /* rename BozoInit to BosServer for the user */
229 /* if BozoInit exists and BosConfig doesn't, try a rename */
230 if (access(AFSDIR_SERVER_BOZINIT_FILEPATH, 0) == 0
231 && access(AFSDIR_SERVER_BOZCONF_FILEPATH, 0) != 0) {
232 code = renamefile(AFSDIR_SERVER_BOZINIT_FILEPATH, AFSDIR_SERVER_BOZCONF_FILEPATH);
234 perror("bosconfig rename");
236 #ifdef BOS_NEW_CONFIG
237 if (access(AFSDIR_SERVER_BOZCONFNEW_FILEPATH, 0) == 0) {
238 code = renamefile(AFSDIR_SERVER_BOZCONFNEW_FILEPATH,
239 AFSDIR_SERVER_BOZCONF_FILEPATH);
241 perror("bosconfig rename");
246 /* setup default times we want to do restarts */
247 bozo_nextRestartKT.mask = KTIME_HOUR | KTIME_MIN | KTIME_DAY;
248 bozo_nextRestartKT.hour = 4; /* 4 am */
249 bozo_nextRestartKT.min = 0;
250 bozo_nextRestartKT.day = 0; /* Sunday */
251 bozo_nextDayKT.mask = KTIME_HOUR | KTIME_MIN;
252 bozo_nextDayKT.hour = 5;
253 bozo_nextDayKT.min = 0;
255 for(code=0;code<MAXPARMS;code++)
256 parms[code] = (char *) 0;
257 instp = typep = notifier = (char *) 0;
259 if (!aname) aname = (char *)bozo_fileName;
260 tfile = fopen(aname, "r");
263 instp = (char *) malloc(BOZO_BSSIZE);
264 typep = (char *) malloc(BOZO_BSSIZE);
265 notifier = notp = (char *) malloc(BOZO_BSSIZE);
267 /* ok, read lines giving parms and such from the file */
268 tp = fgets(tbuffer, sizeof(tbuffer), tfile);
269 if (tp == (char *) 0) break; /* all done */
271 if (strncmp(tbuffer, "restarttime", 11) == 0) {
272 code = sscanf(tbuffer, "restarttime %d %d %d %d %d",
273 &ktmask, &ktday, &kthour, &ktmin, &ktsec);
278 /* otherwise we've read in the proper ktime structure; now assign
279 it and continue processing */
280 bozo_nextRestartKT.mask = ktmask;
281 bozo_nextRestartKT.day = ktday;
282 bozo_nextRestartKT.hour = kthour;
283 bozo_nextRestartKT.min = ktmin;
284 bozo_nextRestartKT.sec = ktsec;
288 if (strncmp(tbuffer, "checkbintime", 12) == 0) {
289 code = sscanf(tbuffer, "checkbintime %d %d %d %d %d",
290 &ktmask, &ktday, &kthour, &ktmin, &ktsec);
295 /* otherwise we've read in the proper ktime structure; now assign
296 it and continue processing */
297 bozo_nextDayKT.mask = ktmask; /* time to restart the system */
298 bozo_nextDayKT.day = ktday;
299 bozo_nextDayKT.hour = kthour;
300 bozo_nextDayKT.min = ktmin;
301 bozo_nextDayKT.sec = ktsec;
305 #ifdef BOS_RESTRICTED_MODE
306 if (strncmp(tbuffer, "restrictmode", 12) == 0) {
307 code = sscanf(tbuffer, "restrictmode %d",
313 if (rmode !=0 && rmode != 1) {
317 bozo_isrestricted=rmode;
322 if (strncmp("bnode", tbuffer, 5) != 0) {
327 code = sscanf(tbuffer, "bnode %s %s %d %s", typep, instp, &goal, notifier);
331 } else if (code == 3)
332 notifier = (char *)0;
334 for(i=0;i<MAXPARMS;i++) {
335 /* now read the parms, until we see an "end" line */
336 tp = fgets(tbuffer, sizeof(tbuffer), tfile);
342 if (!strncmp(tbuffer, "end", 3)) break;
343 if (strncmp(tbuffer, "parm ", 5)) {
345 goto fail; /* no "parm " either */
347 if (!parms[i]) /* make sure there's space */
348 parms[i] = (char *) malloc(BOZO_BSSIZE);
349 strcpy(parms[i], tbuffer+5); /* remember the parameter for later */
352 /* ok, we have the type and parms, now create the object */
353 code = bnode_Create(typep, instp, &tb, parms[0], parms[1], parms[2],
354 parms[3], parms[4], notifier,
355 goal ? BSTAT_NORMAL : BSTAT_SHUTDOWN);
358 /* bnode created in 'temporarily shutdown' state;
359 check to see if we are supposed to run this guy,
360 and if so, start the process up */
362 bnode_SetStat(tb, BSTAT_NORMAL); /* set goal, taking effect immediately */
365 bnode_SetStat(tb, BSTAT_SHUTDOWN);
372 if (instp) free(instp);
373 if (typep) free(typep);
374 for(i=0;i<MAXPARMS;i++) if (parms[i]) free(parms[i]);
375 if (tfile) fclose(tfile);
379 /* write a new bozo file */
382 register FILE *tfile;
383 char tbuffer[AFSDIR_PATH_MAX];
384 register afs_int32 code;
387 if (!aname) aname = (char *)bozo_fileName;
388 strcpy(tbuffer, aname);
389 strcat(tbuffer, ".NBZ");
390 tfile = fopen(tbuffer, "w");
391 if (!tfile) return -1;
393 #ifdef BOS_RESTRICTED_MODE
394 fprintf(tfile, "restrictmode %d\n", bozo_isrestricted);
396 fprintf(tfile, "restarttime %d %d %d %d %d\n", bozo_nextRestartKT.mask,
397 bozo_nextRestartKT.day, bozo_nextRestartKT.hour, bozo_nextRestartKT.min,
398 bozo_nextRestartKT.sec);
399 fprintf(tfile, "checkbintime %d %d %d %d %d\n", bozo_nextDayKT.mask,
400 bozo_nextDayKT.day, bozo_nextDayKT.hour, bozo_nextDayKT.min,
402 code = bnode_ApplyInstance(bzwrite, &btemp);
403 if (code || (code = ferror(tfile))) { /* something went wrong */
408 /* close the file, check for errors and snap new file into place */
409 if (fclose(tfile) == EOF) {
413 code = renamefile(tbuffer, aname);
421 static bdrestart(abnode, arock)
422 register struct bnode *abnode;
424 register afs_int32 code;
426 if (abnode->fileGoal != BSTAT_NORMAL || abnode->goal != BSTAT_NORMAL)
427 return 0; /* don't restart stopped bnodes */
429 code = bnode_RestartP(abnode);
431 /* restart the dude */
432 bnode_SetStat(abnode, BSTAT_SHUTDOWN);
433 bnode_WaitStatus(abnode, BSTAT_SHUTDOWN);
434 bnode_SetStat(abnode, BSTAT_NORMAL);
436 bnode_Release(abnode);
437 return 0; /* keep trying all bnodes */
440 #define BOZO_MINSKIP 3600 /* minimum to advance clock */
441 /* lwp to handle system restarts */
442 static BozoDaemon() {
443 register afs_int32 now;
445 /* now initialize the values */
449 now = FT_ApproxTime();
451 #ifdef BOS_RESTRICTED_MODE
452 if (bozo_restdisable) {
453 bozo_Log("Restricted mode disabled by signal\n");
457 if (bozo_newKTs) { /* need to recompute restart times */
458 bozo_newKTs = 0; /* done for a while */
459 nextRestart = ktime_next(&bozo_nextRestartKT, BOZO_MINSKIP);
460 nextDay = ktime_next(&bozo_nextDayKT, BOZO_MINSKIP);
463 /* see if we should do a restart */
464 if (now > nextRestart) {
465 BOZO_ReBozo(0); /* doesn't come back */
468 /* see if we should restart a server */
470 nextDay = ktime_next(&bozo_nextDayKT, BOZO_MINSKIP);
472 /* call the bnode restartp function, and restart all that require it */
473 bnode_ApplyInstance(bdrestart, 0);
479 static tweak_config()
483 int s, sb_max, ipfragttl;
487 f = popen("/usr/sbin/no -o sb_max", "r");
488 s = fscanf(f, "sb_max = %d", &sb_max);
492 f = popen("/usr/sbin/no -o ipfragttl", "r");
493 s = fscanf(f, "ipfragttl = %d", &ipfragttl);
503 sprintf(c, "/usr/sbin/no -o sb_max=%d -o ipfragttl=%d", sb_max, ipfragttl);
510 * This routine causes the calling process to go into the background and
511 * to lose its controlling tty.
513 * It does not close or otherwise alter the standard file descriptors.
515 * It writes warning messages to the standard error output if certain
516 * fundamental errors occur.
518 * This routine requires
520 * #include <sys/types.h>
521 * #include <sys/stat.h>
523 * #include <unistd.h>
524 * #include <stdlib.h>
526 * and has been tested on:
542 * A process is a process group leader if its process ID
543 * (getpid()) and its process group ID (getpgrp()) are the same.
547 * To create a new session (and thereby lose our controlling
548 * terminal) we cannot be a process group leader.
550 * To guarantee we are not a process group leader, we fork and
551 * let the parent process exit.
554 if (getpid() == getpgrp()) {
559 abort(); /* leave footprints */
563 default: /* parent */
570 * By here, we are not a process group leader, so we can make a
571 * new session and become the session leader.
575 pid_t sid = setsid();
578 static char err[] = "bosserver: WARNING: setsid() failed\n";
579 write(STDERR_FILENO, err, sizeof err - 1);
584 * Once we create a new session, the current process is a
585 * session leader without a controlling tty.
587 * On some systems, the first tty device the session leader
588 * opens automatically becomes the controlling tty for the
591 * So, to guarantee we do not acquire a controlling tty, we fork
592 * and let the parent process exit. The child process is not a
593 * session leader, and so it will not acquire a controlling tty
594 * even if it should happen to open a tty device.
597 if (getpid() == getpgrp()) {
602 abort(); /* leave footprints */
606 default: /* parent */
613 * check that we no longer have a controlling tty
619 fd = open("/dev/tty", O_RDONLY);
622 static char err[] = "bosserver: WARNING: /dev/tty still attached\n";
624 write(STDERR_FILENO, err, sizeof err - 1);
628 #endif /* ! AFS_NT40_ENV */
630 /* start a process and monitor it */
632 #include "AFS_component_version_number.c"
635 main (argc, argv,envp)
640 struct rx_service *tservice;
641 register afs_int32 code;
642 struct afsconf_dir *tdir;
644 struct ktc_encryptionKey tkey;
647 char namebuf[AFSDIR_PATH_MAX];
650 struct sigaction nsa;
652 /* for some reason, this permits user-mode RX to run a lot faster.
653 * we do it here in the bosserver, so we don't have to do it
654 * individually in each server.
659 * The following signal action for AIX is necessary so that in case of a
660 * crash (i.e. core is generated) we can include the user's data section
661 * in the core dump. Unfortunately, by default, only a partial core is
662 * generated which, in many cases, isn't too useful.
664 sigemptyset(&nsa.sa_mask);
665 nsa.sa_handler = SIG_DFL;
666 nsa.sa_flags = SA_FULLDUMP;
667 sigaction(SIGSEGV, &nsa, NULL);
668 sigaction(SIGABRT, &nsa, NULL);
670 #ifdef BOS_RESTRICTED_MODE
671 signal(SIGFPE, bozo_insecureme);
675 /* Initialize winsock */
676 if (afs_winsockInit() < 0) {
677 ReportErrorEventAlt(AFSEVT_SVR_WINSOCK_INIT_FAILED, 0, argv[0], 0);
678 fprintf(stderr, "%s: Couldn't initialize winsock.\n", argv[0]);
683 /* Initialize dirpaths */
684 if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
686 ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
688 fprintf(stderr, "%s: Unable to obtain AFS server directory.\n", argv[0]);
692 /* some path inits */
693 bozo_fileName = AFSDIR_SERVER_BOZCONF_FILEPATH;
695 /* initialize the list of dirpaths that the bosserver has
696 * an interest in monitoring */
699 #if defined(AFS_SGI_ENV)
700 /* offer some protection if AFS isn't loaded */
701 if (syscall(AFS_SYSCALL, AFSOP_ENDLOG) < 0 && errno == ENOPKG) {
702 printf("bosserver: AFS doesn't appear to be configured in O.S..\n");
708 for(code=1;code<argc;code++) {
709 if (strcmp(argv[code], "-noauth")==0) {
710 /* set noauth flag */
713 else if (strcmp(argv[code], "-log")==0) {
714 /* set extra logging flag */
717 else if (strcmp(argv[code], "-enable_peer_stats")==0) {
718 rx_enablePeerRPCStats();
720 else if (strcmp(argv[code], "-enable_process_stats")==0) {
721 rx_enableProcessRPCStats();
723 #ifdef BOS_RESTRICTED_MODE
724 else if (strcmp(argv[code], "-restricted")==0) {
730 /* hack to support help flag */
732 printf("Usage: bosserver [-noauth] [-log] "
733 /* "[-enable_peer_stats] [-enable_process_stats] " */
742 if (geteuid() != 0) {
743 printf("bosserver: must be run as root.\n");
750 printf("bosserver: could not init bnode package, code %d\n", code);
754 bnode_Register("fs", &fsbnode_ops, 3);
755 bnode_Register("simple", &ezbnode_ops, 1);
756 bnode_Register("cron", &cronbnode_ops, 2);
758 /* create useful dirs */
761 /* chdir to AFS log directory */
762 chdir(AFSDIR_SERVER_LOGS_DIRPATH);
765 fputs(AFS_GOVERNMENT_MESSAGE, stdout);
769 /* go into the background and remove our controlling tty */
773 #endif /* ! AFS_NT40_ENV */
775 /* switch to logging information to the BosLog file */
776 strcpy(namebuf, AFSDIR_BOZLOG_FILE);
777 strcat(namebuf, ".old");
778 renamefile(AFSDIR_BOZLOG_FILE, namebuf); /* try rename first */
779 bozo_logFile = fopen(AFSDIR_BOZLOG_FILE, "a");
781 printf("bosserver: can't initialize log file (%s).\n",
782 AFSDIR_SERVER_BOZLOG_FILEPATH);
786 /* keep log closed normally, so can be removed */
788 fclose(bozo_logFile);
790 /* Write current state of directory permissions to log file */
794 code = rx_Init(htons(AFSCONF_NANNYPORT));
796 bozo_Log("can't initialize rx: code=%d\n",code);
802 bozo_Log("Bos giving up, can't initialize rx\n");
806 code = LWP_CreateProcess(BozoDaemon, BOZO_LWP_STACKSIZE, /* priority */ 1,
807 /* parm */0, "bozo-the-clown", &bozo_pid);
809 /* try to read the key from the config file */
810 tdir = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH);
812 /* try to create local cell config file */
813 struct afsconf_cell tcell;
814 strcpy(tcell.name, "localcell");
815 tcell.numServers = 1;
816 code = gethostname(tcell.hostName[0], MAXHOSTCHARS);
818 bozo_Log("failed to get hostname, code %d\n", errno);
821 if (tcell.hostName[0][0] == 0) {
822 bozo_Log("host name not set, can't start\n");
823 bozo_Log("try the 'hostname' command\n");
826 bzero(tcell.hostAddr, sizeof(tcell.hostAddr)); /* not computed */
827 code = afsconf_SetCellInfo(bozo_confdir, AFSDIR_SERVER_ETC_DIRPATH, &tcell);
829 bozo_Log("could not create cell database in '%s' (code %d), quitting\n", AFSDIR_SERVER_ETC_DIRPATH, code);
832 tdir = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH);
834 bozo_Log("failed to open newly-created cell database, quitting\n");
839 /* read init file, starting up programs */
840 if (code=ReadBozoFile(0)) {
841 bozo_Log("bosserver: Something is wrong (%d) with the bos configuration file %s; aborting\n", code, AFSDIR_SERVER_BOZCONF_FILEPATH);
845 /* opened the cell databse */
847 code = afsconf_GetKey(tdir, 999, &tkey);
849 /* allow super users to manage RX statistics */
850 rx_SetRxStatUserOk(bozo_rxstat_userok);
852 /* have bcrypt key now */
854 afsconf_SetNoAuthFlag(tdir, noAuth);
856 bozo_rxsc[0] = (struct rx_securityClass *) rxnull_NewServerSecurityObject();
857 bozo_rxsc[1] = (struct rx_securityClass *) 0;
858 bozo_rxsc[2] = (struct rx_securityClass *) rxkad_NewServerSecurityObject(
859 0, tdir, afsconf_GetKey, (char *) 0);
861 /* These two lines disallow jumbograms */
862 rx_maxReceiveSize = OLD_MAX_PACKET_SIZE;
863 rxi_nSendFrags = rxi_nRecvFrags = 1;
865 tservice = rx_NewService(/* port */ 0, /* service id */ 1,
866 /*service name */ "bozo", /* security classes */ bozo_rxsc,
867 /* numb sec classes */ 3, BOZO_ExecuteRequest);
868 rx_SetMinProcs(tservice, 2);
869 rx_SetMaxProcs(tservice, 4);
870 rx_SetStackSize(tservice, BOZO_LWP_STACKSIZE); /* so gethostbyname works (in cell stuff) */
872 tservice = rx_NewService(0, RX_STATS_SERVICE_ID, "rpcstats", bozo_rxsc,
873 3, RXSTATS_ExecuteRequest);
874 rx_SetMinProcs(tservice, 2);
875 rx_SetMaxProcs(tservice, 4);
876 rx_StartServer(1); /* donate this process */
879 bozo_Log(a,b,c,d,e,f)
880 char *a, *b, *c, *d, *e, *f; {
885 strcpy(tdate, ctime(&myTime)); /* copy out of static area asap */
888 /* log normally closed, so can be removed */
890 bozo_logFile=fopen(AFSDIR_SERVER_BOZLOG_FILEPATH, "a");
891 if(bozo_logFile == NULL)
893 printf("bosserver: WARNING: problem with %s", AFSDIR_SERVER_BOZLOG_FILEPATH);
898 fprintf(bozo_logFile, "%s ", tdate);
899 fprintf(bozo_logFile, a, b, c, d, e, f);
900 fflush(bozo_logFile);
903 printf("%s ", tdate);
904 printf(a, b, c, d, e, f);
907 /* close so rm BosLog works */
909 fclose(bozo_logFile);