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");
238 /* setup default times we want to do restarts */
239 bozo_nextRestartKT.mask = KTIME_HOUR | KTIME_MIN | KTIME_DAY;
240 bozo_nextRestartKT.hour = 4; /* 4 am */
241 bozo_nextRestartKT.min = 0;
242 bozo_nextRestartKT.day = 0; /* Sunday */
243 bozo_nextDayKT.mask = KTIME_HOUR | KTIME_MIN;
244 bozo_nextDayKT.hour = 5;
245 bozo_nextDayKT.min = 0;
247 for(code=0;code<MAXPARMS;code++)
248 parms[code] = (char *) 0;
249 instp = typep = notifier = (char *) 0;
251 if (!aname) aname = (char *)bozo_fileName;
252 tfile = fopen(aname, "r");
255 instp = (char *) malloc(BOZO_BSSIZE);
256 typep = (char *) malloc(BOZO_BSSIZE);
257 notifier = notp = (char *) malloc(BOZO_BSSIZE);
259 /* ok, read lines giving parms and such from the file */
260 tp = fgets(tbuffer, sizeof(tbuffer), tfile);
261 if (tp == (char *) 0) break; /* all done */
263 if (strncmp(tbuffer, "restarttime", 11) == 0) {
264 code = sscanf(tbuffer, "restarttime %d %d %d %d %d",
265 &ktmask, &ktday, &kthour, &ktmin, &ktsec);
270 /* otherwise we've read in the proper ktime structure; now assign
271 it and continue processing */
272 bozo_nextRestartKT.mask = ktmask;
273 bozo_nextRestartKT.day = ktday;
274 bozo_nextRestartKT.hour = kthour;
275 bozo_nextRestartKT.min = ktmin;
276 bozo_nextRestartKT.sec = ktsec;
280 if (strncmp(tbuffer, "checkbintime", 12) == 0) {
281 code = sscanf(tbuffer, "checkbintime %d %d %d %d %d",
282 &ktmask, &ktday, &kthour, &ktmin, &ktsec);
287 /* otherwise we've read in the proper ktime structure; now assign
288 it and continue processing */
289 bozo_nextDayKT.mask = ktmask; /* time to restart the system */
290 bozo_nextDayKT.day = ktday;
291 bozo_nextDayKT.hour = kthour;
292 bozo_nextDayKT.min = ktmin;
293 bozo_nextDayKT.sec = ktsec;
297 #ifdef BOS_RESTRICTED_MODE
298 if (strncmp(tbuffer, "restrictmode", 12) == 0) {
299 code = sscanf(tbuffer, "restrictmode %d",
305 if (rmode !=0 && rmode != 1) {
309 bozo_isrestricted=rmode;
314 if (strncmp("bnode", tbuffer, 5) != 0) {
319 code = sscanf(tbuffer, "bnode %s %s %d %s", typep, instp, &goal, notifier);
323 } else if (code == 3)
324 notifier = (char *)0;
326 for(i=0;i<MAXPARMS;i++) {
327 /* now read the parms, until we see an "end" line */
328 tp = fgets(tbuffer, sizeof(tbuffer), tfile);
334 if (!strncmp(tbuffer, "end", 3)) break;
335 if (strncmp(tbuffer, "parm ", 5)) {
337 goto fail; /* no "parm " either */
339 if (!parms[i]) /* make sure there's space */
340 parms[i] = (char *) malloc(BOZO_BSSIZE);
341 strcpy(parms[i], tbuffer+5); /* remember the parameter for later */
344 /* ok, we have the type and parms, now create the object */
345 code = bnode_Create(typep, instp, &tb, parms[0], parms[1], parms[2],
346 parms[3], parms[4], notifier,
347 goal ? BSTAT_NORMAL : BSTAT_SHUTDOWN);
350 /* bnode created in 'temporarily shutdown' state;
351 check to see if we are supposed to run this guy,
352 and if so, start the process up */
354 bnode_SetStat(tb, BSTAT_NORMAL); /* set goal, taking effect immediately */
357 bnode_SetStat(tb, BSTAT_SHUTDOWN);
364 if (instp) free(instp);
365 if (typep) free(typep);
366 for(i=0;i<MAXPARMS;i++) if (parms[i]) free(parms[i]);
367 if (tfile) fclose(tfile);
371 /* write a new bozo file */
374 register FILE *tfile;
375 char tbuffer[AFSDIR_PATH_MAX];
376 register afs_int32 code;
379 if (!aname) aname = (char *)bozo_fileName;
380 strcpy(tbuffer, aname);
381 strcat(tbuffer, ".NBZ");
382 tfile = fopen(tbuffer, "w");
383 if (!tfile) return -1;
385 #ifdef BOS_RESTRICTED_MODE
386 fprintf(tfile, "restrictmode %d\n", bozo_isrestricted);
388 fprintf(tfile, "restarttime %d %d %d %d %d\n", bozo_nextRestartKT.mask,
389 bozo_nextRestartKT.day, bozo_nextRestartKT.hour, bozo_nextRestartKT.min,
390 bozo_nextRestartKT.sec);
391 fprintf(tfile, "checkbintime %d %d %d %d %d\n", bozo_nextDayKT.mask,
392 bozo_nextDayKT.day, bozo_nextDayKT.hour, bozo_nextDayKT.min,
394 code = bnode_ApplyInstance(bzwrite, &btemp);
395 if (code || (code = ferror(tfile))) { /* something went wrong */
400 /* close the file, check for errors and snap new file into place */
401 if (fclose(tfile) == EOF) {
405 code = renamefile(tbuffer, aname);
413 static bdrestart(abnode, arock)
414 register struct bnode *abnode;
416 register afs_int32 code;
418 if (abnode->fileGoal != BSTAT_NORMAL || abnode->goal != BSTAT_NORMAL)
419 return 0; /* don't restart stopped bnodes */
421 code = bnode_RestartP(abnode);
423 /* restart the dude */
424 bnode_SetStat(abnode, BSTAT_SHUTDOWN);
425 bnode_WaitStatus(abnode, BSTAT_SHUTDOWN);
426 bnode_SetStat(abnode, BSTAT_NORMAL);
428 bnode_Release(abnode);
429 return 0; /* keep trying all bnodes */
432 #define BOZO_MINSKIP 3600 /* minimum to advance clock */
433 /* lwp to handle system restarts */
434 static BozoDaemon() {
435 register afs_int32 now;
437 /* now initialize the values */
441 now = FT_ApproxTime();
443 #ifdef BOS_RESTRICTED_MODE
444 if (bozo_restdisable) {
445 bozo_Log("Restricted mode disabled by signal\n");
449 if (bozo_newKTs) { /* need to recompute restart times */
450 bozo_newKTs = 0; /* done for a while */
451 nextRestart = ktime_next(&bozo_nextRestartKT, BOZO_MINSKIP);
452 nextDay = ktime_next(&bozo_nextDayKT, BOZO_MINSKIP);
455 /* see if we should do a restart */
456 if (now > nextRestart) {
457 BOZO_ReBozo(0); /* doesn't come back */
460 /* see if we should restart a server */
462 nextDay = ktime_next(&bozo_nextDayKT, BOZO_MINSKIP);
464 /* call the bnode restartp function, and restart all that require it */
465 bnode_ApplyInstance(bdrestart, 0);
471 static tweak_config()
475 int s, sb_max, ipfragttl;
479 f = popen("/usr/sbin/no -o sb_max", "r");
480 s = fscanf(f, "sb_max = %d", &sb_max);
484 f = popen("/usr/sbin/no -o ipfragttl", "r");
485 s = fscanf(f, "ipfragttl = %d", &ipfragttl);
495 sprintf(c, "/usr/sbin/no -o sb_max=%d -o ipfragttl=%d", sb_max, ipfragttl);
502 * This routine causes the calling process to go into the background and
503 * to lose its controlling tty.
505 * It does not close or otherwise alter the standard file descriptors.
507 * It writes warning messages to the standard error output if certain
508 * fundamental errors occur.
510 * This routine requires
512 * #include <sys/types.h>
513 * #include <sys/stat.h>
515 * #include <unistd.h>
516 * #include <stdlib.h>
518 * and has been tested on:
534 * A process is a process group leader if its process ID
535 * (getpid()) and its process group ID (getpgrp()) are the same.
539 * To create a new session (and thereby lose our controlling
540 * terminal) we cannot be a process group leader.
542 * To guarantee we are not a process group leader, we fork and
543 * let the parent process exit.
546 if (getpid() == getpgrp()) {
551 abort(); /* leave footprints */
555 default: /* parent */
562 * By here, we are not a process group leader, so we can make a
563 * new session and become the session leader.
567 pid_t sid = setsid();
570 static char err[] = "bosserver: WARNING: setsid() failed\n";
571 write(STDERR_FILENO, err, sizeof err - 1);
576 * Once we create a new session, the current process is a
577 * session leader without a controlling tty.
579 * On some systems, the first tty device the session leader
580 * opens automatically becomes the controlling tty for the
583 * So, to guarantee we do not acquire a controlling tty, we fork
584 * and let the parent process exit. The child process is not a
585 * session leader, and so it will not acquire a controlling tty
586 * even if it should happen to open a tty device.
589 if (getpid() == getpgrp()) {
594 abort(); /* leave footprints */
598 default: /* parent */
605 * check that we no longer have a controlling tty
611 fd = open("/dev/tty", O_RDONLY);
614 static char err[] = "bosserver: WARNING: /dev/tty still attached\n";
616 write(STDERR_FILENO, err, sizeof err - 1);
620 #endif /* ! AFS_NT40_ENV */
622 /* start a process and monitor it */
624 #include "AFS_component_version_number.c"
627 main (argc, argv,envp)
632 struct rx_service *tservice;
633 register afs_int32 code;
634 struct afsconf_dir *tdir;
636 struct ktc_encryptionKey tkey;
639 char namebuf[AFSDIR_PATH_MAX];
642 struct sigaction nsa;
644 /* for some reason, this permits user-mode RX to run a lot faster.
645 * we do it here in the bosserver, so we don't have to do it
646 * individually in each server.
651 * The following signal action for AIX is necessary so that in case of a
652 * crash (i.e. core is generated) we can include the user's data section
653 * in the core dump. Unfortunately, by default, only a partial core is
654 * generated which, in many cases, isn't too useful.
656 sigemptyset(&nsa.sa_mask);
657 nsa.sa_handler = SIG_DFL;
658 nsa.sa_flags = SA_FULLDUMP;
659 sigaction(SIGSEGV, &nsa, NULL);
660 sigaction(SIGABRT, &nsa, NULL);
662 #ifdef BOS_RESTRICTED_MODE
663 signal(SIGFPE, bozo_insecureme);
667 /* Initialize winsock */
668 if (afs_winsockInit() < 0) {
669 ReportErrorEventAlt(AFSEVT_SVR_WINSOCK_INIT_FAILED, 0, argv[0], 0);
670 fprintf(stderr, "%s: Couldn't initialize winsock.\n", argv[0]);
675 /* Initialize dirpaths */
676 if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
678 ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
680 fprintf(stderr, "%s: Unable to obtain AFS server directory.\n", argv[0]);
684 /* some path inits */
685 bozo_fileName = AFSDIR_SERVER_BOZCONF_FILEPATH;
687 /* initialize the list of dirpaths that the bosserver has
688 * an interest in monitoring */
691 #if defined(AFS_SGI_ENV)
692 /* offer some protection if AFS isn't loaded */
693 if (syscall(AFS_SYSCALL, AFSOP_ENDLOG) < 0 && errno == ENOPKG) {
694 printf("bosserver: AFS doesn't appear to be configured in O.S..\n");
700 for(code=1;code<argc;code++) {
701 if (strcmp(argv[code], "-noauth")==0) {
702 /* set noauth flag */
705 else if (strcmp(argv[code], "-log")==0) {
706 /* set extra logging flag */
709 else if (strcmp(argv[code], "-enable_peer_stats")==0) {
710 rx_enablePeerRPCStats();
712 else if (strcmp(argv[code], "-enable_process_stats")==0) {
713 rx_enableProcessRPCStats();
715 #ifdef BOS_RESTRICTED_MODE
716 else if (strcmp(argv[code], "-restricted")==0) {
722 /* hack to support help flag */
724 printf("Usage: bosserver [-noauth] [-log] "
725 /* "[-enable_peer_stats] [-enable_process_stats] " */
734 if (geteuid() != 0) {
735 printf("bosserver: must be run as root.\n");
742 printf("bosserver: could not init bnode package, code %d\n", code);
746 bnode_Register("fs", &fsbnode_ops, 3);
747 bnode_Register("simple", &ezbnode_ops, 1);
748 bnode_Register("cron", &cronbnode_ops, 2);
750 /* create useful dirs */
753 /* chdir to AFS log directory */
754 chdir(AFSDIR_SERVER_LOGS_DIRPATH);
756 fputs(AFS_GOVERNMENT_MESSAGE, stdout);
759 /* go into the background and remove our controlling tty */
763 #endif /* ! AFS_NT40_ENV */
765 /* switch to logging information to the BosLog file */
766 strcpy(namebuf, AFSDIR_BOZLOG_FILE);
767 strcat(namebuf, ".old");
768 renamefile(AFSDIR_BOZLOG_FILE, namebuf); /* try rename first */
769 bozo_logFile = fopen(AFSDIR_BOZLOG_FILE, "a");
771 printf("bosserver: can't initialize log file (%s).\n",
772 AFSDIR_SERVER_BOZLOG_FILEPATH);
776 /* keep log closed normally, so can be removed */
778 fclose(bozo_logFile);
780 /* Write current state of directory permissions to log file */
784 code = rx_Init(htons(AFSCONF_NANNYPORT));
786 bozo_Log("can't initialize rx: code=%d\n",code);
794 code = LWP_CreateProcess(BozoDaemon, BOZO_LWP_STACKSIZE, /* priority */ 1,
795 /* parm */0, "bozo-the-clown", &bozo_pid);
797 /* try to read the key from the config file */
798 tdir = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH);
800 /* try to create local cell config file */
801 struct afsconf_cell tcell;
802 strcpy(tcell.name, "localcell");
803 tcell.numServers = 1;
804 code = gethostname(tcell.hostName[0], MAXHOSTCHARS);
806 bozo_Log("failed to get hostname, code %d\n", errno);
809 if (tcell.hostName[0][0] == 0) {
810 bozo_Log("host name not set, can't start\n");
811 bozo_Log("try the 'hostname' command\n");
814 bzero(tcell.hostAddr, sizeof(tcell.hostAddr)); /* not computed */
815 code = afsconf_SetCellInfo(bozo_confdir, AFSDIR_SERVER_ETC_DIRPATH, &tcell);
817 bozo_Log("could not create cell database in '%s' (code %d), quitting\n", AFSDIR_SERVER_ETC_DIRPATH, code);
820 tdir = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH);
822 bozo_Log("failed to open newly-created cell database, quitting\n");
827 /* read init file, starting up programs */
828 if (code=ReadBozoFile(0)) {
829 bozo_Log("bosserver: Something is wrong (%d) with the bos configuration file %s; aborting\n", code, AFSDIR_SERVER_BOZCONF_FILEPATH);
833 /* opened the cell databse */
835 code = afsconf_GetKey(tdir, 999, &tkey);
837 /* allow super users to manage RX statistics */
838 rx_SetRxStatUserOk(bozo_rxstat_userok);
840 /* have bcrypt key now */
842 afsconf_SetNoAuthFlag(tdir, noAuth);
844 bozo_rxsc[0] = (struct rx_securityClass *) rxnull_NewServerSecurityObject();
845 bozo_rxsc[1] = (struct rx_securityClass *) 0;
846 bozo_rxsc[2] = (struct rx_securityClass *) rxkad_NewServerSecurityObject(
847 0, tdir, afsconf_GetKey, (char *) 0);
849 /* These two lines disallow jumbograms */
850 rx_maxReceiveSize = OLD_MAX_PACKET_SIZE;
851 rxi_nSendFrags = rxi_nRecvFrags = 1;
853 tservice = rx_NewService(/* port */ 0, /* service id */ 1,
854 /*service name */ "bozo", /* security classes */ bozo_rxsc,
855 /* numb sec classes */ 3, BOZO_ExecuteRequest);
856 rx_SetMinProcs(tservice, 2);
857 rx_SetMaxProcs(tservice, 4);
858 rx_SetStackSize(tservice, BOZO_LWP_STACKSIZE); /* so gethostbyname works (in cell stuff) */
860 tservice = rx_NewService(0, RX_STATS_SERVICE_ID, "rpcstats", bozo_rxsc,
861 3, RXSTATS_ExecuteRequest);
862 rx_SetMinProcs(tservice, 2);
863 rx_SetMaxProcs(tservice, 4);
864 rx_StartServer(1); /* donate this process */
867 bozo_Log(a,b,c,d,e,f)
868 char *a, *b, *c, *d, *e, *f; {
873 strcpy(tdate, ctime(&myTime)); /* copy out of static area asap */
876 /* log normally closed, so can be removed */
878 bozo_logFile=fopen(AFSDIR_SERVER_BOZLOG_FILEPATH, "a");
879 if(bozo_logFile == NULL)
881 printf("bosserver: WARNING: problem with %s", AFSDIR_SERVER_BOZLOG_FILEPATH);
886 fprintf(bozo_logFile, "%s ", tdate);
887 fprintf(bozo_logFile, a, b, c, d, e, f);
888 fflush(bozo_logFile);
891 printf("%s ", tdate);
892 printf(a, b, c, d, e, f);
895 /* close so rm BosLog works */
897 fclose(bozo_logFile);