implement-bos-restricted-mode-20010129
[openafs.git] / src / bozo / bosserver.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
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
8  */
9
10 #include <afs/param.h>
11 #include <afs/stds.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <fcntl.h>
15 #include <errno.h>
16 #include <string.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #ifdef AFS_NT40_ENV
20 #include <winsock2.h>
21 #include <direct.h>
22 #include <io.h>
23 #include <WINNT/afsevent.h>
24 #else
25 #include <unistd.h>
26 #include <netinet/in.h>
27 #endif /* AFS_NT40_ENV */
28 #include <afs/cellconfig.h>
29 #include <rx/rx.h>
30 #include <rx/xdr.h>
31 #include <rx/rx_globals.h>
32 #include "bosint.h"
33 #include "bnode.h"
34 #include <afs/auth.h>
35 #include <afs/keys.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>
42 #endif
43
44
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;
54 FILE *bozo_logFile;
55 extern int rx_stackSize;    /* for rx_SetStackSize macro */
56
57 int DoLogging = 0;
58 static afs_int32 nextRestart;
59 static afs_int32 nextDay;
60
61 struct ktime bozo_nextRestartKT, bozo_nextDayKT;
62 int bozo_newKTs;
63 #ifdef BOS_RESTRICTED_MODE
64 int bozo_isrestricted=0;
65 int bozo_restdisable=0;
66
67 void bozo_insecureme(int sig) 
68 {
69      signal(SIGFPE, bozo_insecureme);
70      bozo_isrestricted=0;
71      bozo_restdisable=1;
72 }
73 #endif
74
75 struct bztemp {
76     FILE *file;
77 };
78
79 /* check whether caller is authorized to manage RX statistics */
80 int bozo_rxstat_userok(call)
81     struct rx_call *call;
82 {
83     return afsconf_SuperUser(bozo_confdir, call, (char *)0);
84 }
85
86 /* restart bozo process */
87 bozo_ReBozo() {
88 #ifdef AFS_NT40_ENV
89     /* exit with restart code; SCM integrator process will restart bosserver */
90     int status = BOSEXIT_RESTART;
91
92     /* if noauth flag is set, pass "-noauth" to new bosserver */
93     if (afsconf_GetNoAuthFlag(bozo_confdir)) {
94         status |= BOSEXIT_NOAUTH_FLAG;
95     }
96     /* if logging is on, pass "-log" to new bosserver */
97     if (DoLogging) {
98         status |= BOSEXIT_LOGGING_FLAG;
99     }
100     exit(status);
101 #else
102     /* exec new bosserver process */
103     char *argv[4];
104     int i = 0;
105
106     argv[i] = (char *)AFSDIR_SERVER_BOSVR_FILEPATH;
107     i++;
108
109     /* if noauth flag is set, pass "-noauth" to new bosserver */
110     if (afsconf_GetNoAuthFlag(bozo_confdir)) {
111         argv[i] = "-noauth";
112         i++;
113     }
114     /* if logging is on, pass "-log" to new bosserver */
115     if (DoLogging) {
116         argv[i] = "-log";
117         i++;
118     }
119
120     /* null-terminate argument list */
121     argv[i] = NULL;
122
123     /* close random fd's */
124     for (i = 3; i < 64; i++) {
125         close(i);
126     }
127
128     execv(argv[0], argv);  /* should not return */
129     _exit(1);
130 #endif /* AFS_NT40_ENV */
131 }
132
133 /* make sure a dir exists */
134 static MakeDir(adir)
135 register char *adir; {
136     struct stat tstat;
137     register afs_int32 code;
138     if (stat(adir, &tstat) < 0 || (tstat.st_mode & S_IFMT) != S_IFDIR) {
139         int reqPerm;
140         unlink(adir);
141         reqPerm = GetRequiredDirPerm (adir);
142         if (reqPerm == -1) reqPerm = 0777;
143 #ifdef AFS_NT40_ENV
144         /* underlying filesystem may not support directory protection */
145         code = mkdir(adir);
146 #else
147         code = mkdir(adir, reqPerm);
148 #endif
149         return code;
150     }
151     return 0;
152 }
153
154 /* create all the bozo dirs */
155 static CreateDirs() {
156     
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);
164 #ifndef AFS_NT40_ENV
165     MakeDir(AFSDIR_CLIENT_VICE_DIRPATH);
166     MakeDir(AFSDIR_CLIENT_ETC_DIRPATH);
167
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 */
171     return 0;
172 }
173
174 /* strip the \\n from the end of the line, if it is present */
175 static StripLine(abuffer)
176 register char *abuffer; {
177     register char *tp;
178     
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;
183     return 0;
184 }
185
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; {
190     register int i;
191     char tbuffer[BOZO_BSSIZE];
192     register afs_int32 code;
193
194     if (abnode->notifier)
195         fprintf(at->file, "bnode %s %s %d %s\n", 
196                 abnode->type->name, abnode->name, abnode->fileGoal, abnode->notifier);
197     else
198         fprintf(at->file, "bnode %s %s %d\n", abnode->type->name, abnode->name, abnode->fileGoal);
199     for(i=0;;i++) {
200         code = bnode_GetParm(abnode, i, tbuffer, BOZO_BSSIZE);
201         if (code) {
202             if (code != BZDOM) return code;
203             break;
204         }
205         fprintf(at->file, "parm %s\n", tbuffer);
206     }
207     fprintf(at->file, "end\n");
208     return 0;
209 }
210
211 #define MAXPARMS    20
212 ReadBozoFile(aname)
213 char *aname; {
214     register FILE *tfile;
215     char tbuffer[BOZO_BSSIZE];
216     register char *tp;
217     char *instp, *typep, *notifier, *notp;
218     register afs_int32 code;
219     afs_int32 ktmask, ktday, kthour, ktmin, ktsec;
220     afs_int32 i, goal;
221     struct bnode *tb;
222     char *parms[MAXPARMS];
223 #ifdef BOS_RESTRICTED_MODE
224     int rmode;
225 #endif
226
227     /* rename BozoInit to BosServer for the user */
228     if (!aname) {
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);
233             if (code < 0)
234                 perror("bosconfig rename");
235         }
236     }
237
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;
246
247     for(code=0;code<MAXPARMS;code++)
248         parms[code] = (char *) 0;
249     instp = typep = notifier = (char *) 0;
250     tfile = (FILE *) 0;
251     if (!aname) aname = (char *)bozo_fileName;
252     tfile = fopen(aname, "r");
253     if (!tfile) 
254         return 0;       /* -1 */
255     instp = (char *) malloc(BOZO_BSSIZE);
256     typep = (char *) malloc(BOZO_BSSIZE);
257     notifier = notp = (char *) malloc(BOZO_BSSIZE);
258     while (1) {
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 */
262
263         if (strncmp(tbuffer, "restarttime", 11) == 0) {
264             code = sscanf(tbuffer, "restarttime %d %d %d %d %d",
265                           &ktmask, &ktday, &kthour, &ktmin, &ktsec);
266             if (code != 5) {
267                 code = -1;
268                 goto fail;
269             }
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;
277             continue;
278         }
279
280         if (strncmp(tbuffer, "checkbintime", 12) == 0) {
281             code = sscanf(tbuffer, "checkbintime %d %d %d %d %d",
282                           &ktmask, &ktday, &kthour, &ktmin, &ktsec);
283             if (code != 5) {
284                 code = -1;
285                 goto fail;
286             }
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;
294             continue;
295         }
296
297 #ifdef BOS_RESTRICTED_MODE
298         if (strncmp(tbuffer, "restrictmode", 12) == 0) {
299             code = sscanf(tbuffer, "restrictmode %d",
300                           &rmode);
301             if (code != 1) {
302                 code = -1;
303                 goto fail;
304             }
305             if (rmode !=0 && rmode != 1) {
306                  code = -1;
307                  goto fail;
308             } 
309             bozo_isrestricted=rmode;
310             continue;
311         }
312 #endif
313         
314         if (strncmp("bnode", tbuffer, 5) != 0) {
315             code = -1;
316             goto fail;
317         }
318         notifier = notp;
319         code = sscanf(tbuffer, "bnode %s %s %d %s", typep, instp, &goal, notifier);
320         if (code < 3) {
321             code = -1;
322             goto fail;
323         } else if (code == 3)
324             notifier = (char *)0;
325         
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);
329             if (!tp) {
330                 code = -1;
331                 goto fail;
332             }
333             StripLine(tbuffer);
334             if (!strncmp(tbuffer, "end", 3)) break;
335             if (strncmp(tbuffer, "parm ", 5)) {
336                 code = -1;
337                 goto fail;    /* no "parm " either */
338             }
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 */
342         }
343
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);
348         if (code) goto fail;
349
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 */
353         if (goal) {
354             bnode_SetStat(tb, BSTAT_NORMAL);    /* set goal, taking effect immediately */
355         }
356         else {
357             bnode_SetStat(tb, BSTAT_SHUTDOWN);
358         }
359     }
360     /* all done */
361     code = 0;
362
363 fail:
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);
368     return code;
369 }
370
371 /* write a new bozo file */
372 WriteBozoFile(aname)
373 char *aname; {
374     register FILE *tfile;
375     char tbuffer[AFSDIR_PATH_MAX];
376     register afs_int32 code;
377     struct bztemp btemp;
378
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;
384     btemp.file = tfile;
385 #ifdef BOS_RESTRICTED_MODE
386     fprintf(tfile, "restrictmode %d\n", bozo_isrestricted);
387 #endif
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,
393             bozo_nextDayKT.sec);
394     code = bnode_ApplyInstance(bzwrite, &btemp);
395     if (code || (code = ferror(tfile))) {       /* something went wrong */
396         fclose(tfile);
397         unlink(tbuffer);
398         return code;
399     }
400     /* close the file, check for errors and snap new file into place */
401     if (fclose(tfile) == EOF) {
402         unlink(tbuffer);
403         return -1;
404     }
405     code = renamefile(tbuffer, aname);
406     if (code) {
407         unlink(tbuffer);
408         return -1;
409     }
410     return 0;
411 }
412
413 static bdrestart(abnode, arock)
414 register struct bnode *abnode;
415 char *arock; {
416     register afs_int32 code;
417     
418     if (abnode->fileGoal != BSTAT_NORMAL || abnode->goal != BSTAT_NORMAL)
419         return 0;       /* don't restart stopped bnodes */
420     bnode_Hold(abnode);
421     code = bnode_RestartP(abnode);
422     if (code) {
423         /* restart the dude */
424         bnode_SetStat(abnode, BSTAT_SHUTDOWN);
425         bnode_WaitStatus(abnode, BSTAT_SHUTDOWN);
426         bnode_SetStat(abnode, BSTAT_NORMAL);
427     }
428     bnode_Release(abnode);
429     return 0;   /* keep trying all bnodes */
430 }
431
432 #define BOZO_MINSKIP 3600           /* minimum to advance clock */
433 /* lwp to handle system restarts */
434 static BozoDaemon() {
435     register afs_int32 now;
436     
437     /* now initialize the values */
438     bozo_newKTs = 1;
439     while (1) {
440         IOMGR_Sleep(60);
441         now = FT_ApproxTime();
442
443 #ifdef BOS_RESTRICTED_MODE
444         if (bozo_restdisable) {
445              bozo_Log("Restricted mode disabled by signal\n");
446              bozo_restdisable=0;
447         }
448 #endif
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);
453         }
454
455         /* see if we should do a restart */
456         if (now > nextRestart) {
457             BOZO_ReBozo(0);     /* doesn't come back */
458         }
459         
460         /* see if we should restart a server */
461         if (now > nextDay) {
462             nextDay = ktime_next(&bozo_nextDayKT, BOZO_MINSKIP);
463             
464             /* call the bnode restartp function, and restart all that require it */
465             bnode_ApplyInstance(bdrestart, 0);
466         }
467     }
468 }
469
470 #ifdef AFS_AIX32_ENV
471 static tweak_config()
472 {
473     FILE *f;
474     char c[80];
475     int s, sb_max, ipfragttl;
476
477     sb_max = 131072;
478     ipfragttl = 20;
479     f = popen("/usr/sbin/no -o sb_max", "r");
480     s = fscanf(f, "sb_max = %d", &sb_max);
481     fclose(f);
482     if (s < 1) 
483         return;
484     f = popen("/usr/sbin/no -o ipfragttl", "r");
485     s = fscanf(f, "ipfragttl = %d", &ipfragttl);
486     fclose(f);
487     if (s < 1) 
488         ipfragttl = 20;
489
490     if (sb_max < 131072) 
491         sb_max = 131072;
492     if (ipfragttl > 20)
493         ipfragttl = 20;
494     
495     sprintf(c, "/usr/sbin/no -o sb_max=%d -o ipfragttl=%d", sb_max, ipfragttl);
496     f = popen(c, "r");
497     fclose(f);
498 }
499 #endif
500
501 /*
502  * This routine causes the calling process to go into the background and
503  * to lose its controlling tty.
504  *
505  * It does not close or otherwise alter the standard file descriptors.
506  *
507  * It writes warning messages to the standard error output if certain
508  * fundamental errors occur.
509  *
510  * This routine requires
511  * 
512  * #include <sys/types.h>
513  * #include <sys/stat.h>
514  * #include <fcntl.h>
515  * #include <unistd.h>
516  * #include <stdlib.h>
517  *
518  * and has been tested on:
519  *
520  * AIX 4.2
521  * Digital Unix 4.0D
522  * HP-UX 11.0
523  * IRIX 6.5
524  * Linux 2.1.125
525  * Solaris 2.5
526  * Solaris 2.6
527  */
528
529 #ifndef AFS_NT40_ENV
530 static void
531 background(void)
532 {
533     /* 
534      * A process is a process group leader if its process ID
535      * (getpid()) and its process group ID (getpgrp()) are the same.
536      */
537
538     /*
539      * To create a new session (and thereby lose our controlling
540      * terminal) we cannot be a process group leader.
541      *
542      * To guarantee we are not a process group leader, we fork and
543      * let the parent process exit.
544      */
545
546     if (getpid() == getpgrp()) {
547         pid_t pid;
548         pid = fork();
549         switch(pid) {
550         case -1:
551             abort();    /* leave footprints */
552             break;
553         case 0:         /* child */
554             break;
555         default:        /* parent */
556             exit(0);
557             break;
558         }
559     }
560
561     /*
562      * By here, we are not a process group leader, so we can make a
563      * new session and become the session leader.
564      */
565
566     {
567         pid_t sid = setsid();
568
569         if (sid == -1) {
570             static char err[] = "bosserver: WARNING: setsid() failed\n";
571             write(STDERR_FILENO, err, sizeof err - 1);
572         }
573     }
574
575     /*
576      * Once we create a new session, the current process is a
577      * session leader without a controlling tty.
578      *
579      * On some systems, the first tty device the session leader
580      * opens automatically becomes the controlling tty for the
581      * session.
582      *
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.
587      */
588
589     if (getpid() == getpgrp()) {
590         pid_t pid;
591         pid = fork();
592         switch(pid) {
593         case -1:
594             abort();    /* leave footprints */
595             break;
596         case 0:         /* child */
597             break;
598         default:        /* parent */
599             exit(0);
600             break;
601         }
602     }
603
604     /*
605      * check that we no longer have a controlling tty
606      */
607
608     {
609         int fd;
610
611         fd = open("/dev/tty", O_RDONLY);
612
613         if (fd >= 0) {
614             static char err[] = "bosserver: WARNING: /dev/tty still attached\n";
615             close(fd);
616             write(STDERR_FILENO, err, sizeof err - 1);
617         }
618     }
619 }
620 #endif /* ! AFS_NT40_ENV */
621
622 /* start a process and monitor it */
623
624 #include "AFS_component_version_number.c"
625
626
627 main (argc, argv,envp)
628 int argc;
629 char **argv;
630 char **envp;
631 {
632     struct rx_service *tservice;
633     register afs_int32 code;
634     struct afsconf_dir *tdir;
635     int noAuth = 0;
636     struct ktc_encryptionKey tkey;
637     int i;
638     pid_t       newSessionID;
639     char namebuf[AFSDIR_PATH_MAX];
640
641 #ifdef  AFS_AIX32_ENV
642     struct sigaction nsa;
643
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.
647      */
648     tweak_config();
649
650     /*
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.
655      */
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);
661 #endif
662 #ifdef BOS_RESTRICTED_MODE
663     signal(SIGFPE, bozo_insecureme);
664 #endif
665
666 #ifdef AFS_NT40_ENV
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]);
671         exit(2);
672     }
673 #endif
674
675     /* Initialize dirpaths */
676     if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
677 #ifdef AFS_NT40_ENV
678         ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
679 #endif
680         fprintf(stderr, "%s: Unable to obtain AFS server directory.\n", argv[0]);
681         exit(2);
682     }
683
684     /* some path inits */
685     bozo_fileName = AFSDIR_SERVER_BOZCONF_FILEPATH;
686
687     /* initialize the list of dirpaths that the bosserver has
688      * an interest in monitoring */
689     initBosEntryStats();
690
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");
695         exit(1);
696     }
697 #endif
698
699     /* parse cmd line */
700     for(code=1;code<argc;code++) {
701         if (strcmp(argv[code], "-noauth")==0) {
702             /* set noauth flag */
703             noAuth = 1;
704         }
705         else if (strcmp(argv[code], "-log")==0) {
706             /* set extra logging flag */
707             DoLogging = 1;
708         }
709         else if (strcmp(argv[code], "-enable_peer_stats")==0) {
710             rx_enablePeerRPCStats();
711         }
712         else if (strcmp(argv[code], "-enable_process_stats")==0) {
713             rx_enableProcessRPCStats();
714         }
715 #ifdef BOS_RESTRICTED_MODE
716         else if (strcmp(argv[code], "-restricted")==0) {
717             bozo_isrestricted=1;
718         }
719 #endif
720         else {
721
722             /* hack to support help flag */
723
724             printf("Usage: bosserver [-noauth] [-log] "
725                    /* "[-enable_peer_stats] [-enable_process_stats] " */
726                    "[-help]\n");
727             fflush(stdout);
728
729             exit(0);
730         }
731     }
732
733 #ifndef AFS_NT40_ENV
734     if (geteuid() != 0) {
735         printf("bosserver: must be run as root.\n");
736         exit(1);
737     }
738 #endif
739
740     code = bnode_Init();
741     if (code) {
742         printf("bosserver: could not init bnode package, code %d\n", code);
743         exit(1);
744     }
745
746     bnode_Register("fs", &fsbnode_ops, 3);
747     bnode_Register("simple", &ezbnode_ops, 1);
748     bnode_Register("cron", &cronbnode_ops, 2);
749
750     /* create useful dirs */
751     CreateDirs();
752
753     /* chdir to AFS log directory */
754     chdir(AFSDIR_SERVER_LOGS_DIRPATH);
755
756     fputs(AFS_GOVERNMENT_MESSAGE, stdout);
757     fflush(stdout);
758
759     /* go into the background and remove our controlling tty */
760
761 #ifndef AFS_NT40_ENV
762     background();
763 #endif /* ! AFS_NT40_ENV */
764
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");
770     if (!bozo_logFile) {
771         printf("bosserver: can't initialize log file (%s).\n",
772                AFSDIR_SERVER_BOZLOG_FILEPATH);
773         exit(1);
774     }
775
776     /* keep log closed normally, so can be removed */
777
778     fclose(bozo_logFile);
779
780     /* Write current state of directory permissions to log file */
781     DirAccessOK ();
782
783     for (i=0;i<10;i++) {
784         code = rx_Init(htons(AFSCONF_NANNYPORT));
785         if (code) {
786             bozo_Log("can't initialize rx: code=%d\n",code);
787             sleep(3);
788         }
789         else break;
790     }
791     if (i>=10)
792         exit(code);
793
794     code = LWP_CreateProcess(BozoDaemon, BOZO_LWP_STACKSIZE, /* priority */ 1,
795                              /* parm */0, "bozo-the-clown", &bozo_pid);
796
797     /* try to read the key from the config file */
798     tdir = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH);
799     if (!tdir) {
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);
805         if (code) {
806             bozo_Log("failed to get hostname, code %d\n", errno);
807             exit(1);
808         }
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");
812             exit(1);
813         }
814         bzero(tcell.hostAddr, sizeof(tcell.hostAddr));  /* not computed */
815         code = afsconf_SetCellInfo(bozo_confdir, AFSDIR_SERVER_ETC_DIRPATH, &tcell);
816         if (code) {
817             bozo_Log("could not create cell database in '%s' (code %d), quitting\n", AFSDIR_SERVER_ETC_DIRPATH, code);
818             exit(1);
819         }
820         tdir = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH);
821         if (!tdir) {
822             bozo_Log("failed to open newly-created cell database, quitting\n");
823             exit(1);
824         }
825     }
826
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);
830         exit(code);
831     }
832
833     /* opened the cell databse */
834     bozo_confdir = tdir;
835     code = afsconf_GetKey(tdir, 999, &tkey);
836
837     /* allow super users to manage RX statistics */
838     rx_SetRxStatUserOk(bozo_rxstat_userok);
839
840     /* have bcrypt key now */
841
842     afsconf_SetNoAuthFlag(tdir, noAuth);
843
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);
848
849     /* These two lines disallow jumbograms */
850     rx_maxReceiveSize = OLD_MAX_PACKET_SIZE;
851     rxi_nSendFrags = rxi_nRecvFrags = 1;
852
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) */
859
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 */
865 }
866
867 bozo_Log(a,b,c,d,e,f)
868 char *a, *b, *c, *d, *e, *f; {
869     char tdate[26];
870     time_t myTime;
871
872     myTime = time(0);
873     strcpy(tdate, ctime(&myTime));      /* copy out of static area asap */
874     tdate[24] = ':';
875
876     /* log normally closed, so can be removed */
877
878     bozo_logFile=fopen(AFSDIR_SERVER_BOZLOG_FILEPATH, "a");
879     if(bozo_logFile == NULL)
880     {
881         printf("bosserver: WARNING: problem with %s", AFSDIR_SERVER_BOZLOG_FILEPATH);
882         fflush(stdout);
883     }
884
885     if (bozo_logFile) {
886         fprintf(bozo_logFile, "%s ", tdate);
887         fprintf(bozo_logFile, a, b, c, d, e, f);
888         fflush(bozo_logFile);
889     }
890     else {
891         printf("%s ", tdate);
892         printf(a, b, c, d, e, f);
893     }
894
895     /* close so rm BosLog works */
896
897     fclose(bozo_logFile);
898 }