misc-build-cleanup-20010917
[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 <afsconfig.h>
11 #include <afs/param.h>
12
13 RCSID("$Header$");
14
15 #include <afs/stds.h>
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <fcntl.h>
19 #include <errno.h>
20 #include <string.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #ifdef AFS_NT40_ENV
24 #include <winsock2.h>
25 #include <direct.h>
26 #include <io.h>
27 #include <WINNT/afsevent.h>
28 #else
29 #include <unistd.h>
30 #include <netinet/in.h>
31 #include <syslog.h>
32 #endif /* AFS_NT40_ENV */
33 #include <afs/cellconfig.h>
34 #include <rx/rx.h>
35 #include <rx/xdr.h>
36 #include <rx/rx_globals.h>
37 #include "bosint.h"
38 #include "bnode.h"
39 #include <afs/auth.h>
40 #include <afs/keys.h>
41 #include <afs/ktime.h>
42 #include <afs/afsutil.h>
43 #include <afs/fileutil.h>
44 #include <afs/procmgmt.h>  /* signal(), kill(), wait(), etc. */
45 #if defined(AFS_SGI_ENV)
46 #include <afs/afs_args.h>
47 #endif
48
49
50 #define BOZO_LWP_STACKSIZE      16000
51 extern int BOZO_ExecuteRequest();
52 extern int RXSTATS_ExecuteRequest();
53 extern int afsconf_GetKey();
54 extern struct bnode_ops fsbnode_ops, ezbnode_ops, cronbnode_ops;
55
56 void bozo_Log();
57
58 struct afsconf_dir *bozo_confdir = 0;   /* bozo configuration dir */
59 static char *bozo_pid;
60 struct rx_securityClass *bozo_rxsc[3];
61 const char *bozo_fileName;
62 FILE *bozo_logFile;
63 extern int rx_stackSize;    /* for rx_SetStackSize macro */
64
65 int DoLogging = 0;
66 int DoSyslog = 0;
67 #ifndef AFS_NT40_ENV
68 int DoSyslogFacility = LOG_DAEMON;
69 #endif
70 static afs_int32 nextRestart;
71 static afs_int32 nextDay;
72
73 struct ktime bozo_nextRestartKT, bozo_nextDayKT;
74 int bozo_newKTs;
75 #ifdef BOS_RESTRICTED_MODE
76 int bozo_isrestricted=0;
77 int bozo_restdisable=0;
78
79 void bozo_insecureme(int sig) 
80 {
81      signal(SIGFPE, bozo_insecureme);
82      bozo_isrestricted=0;
83      bozo_restdisable=1;
84 }
85 #endif
86
87 struct bztemp {
88     FILE *file;
89 };
90
91 /* check whether caller is authorized to manage RX statistics */
92 int bozo_rxstat_userok(call)
93     struct rx_call *call;
94 {
95     return afsconf_SuperUser(bozo_confdir, call, (char *)0);
96 }
97
98 /* restart bozo process */
99 bozo_ReBozo() {
100 #ifdef AFS_NT40_ENV
101     /* exit with restart code; SCM integrator process will restart bosserver */
102     int status = BOSEXIT_RESTART;
103
104     /* if noauth flag is set, pass "-noauth" to new bosserver */
105     if (afsconf_GetNoAuthFlag(bozo_confdir)) {
106         status |= BOSEXIT_NOAUTH_FLAG;
107     }
108     /* if logging is on, pass "-log" to new bosserver */
109     if (DoLogging) {
110         status |= BOSEXIT_LOGGING_FLAG;
111     }
112     exit(status);
113 #else
114     /* exec new bosserver process */
115     char *argv[4];
116     int i = 0;
117
118     argv[i] = (char *)AFSDIR_SERVER_BOSVR_FILEPATH;
119     i++;
120
121     /* if noauth flag is set, pass "-noauth" to new bosserver */
122     if (afsconf_GetNoAuthFlag(bozo_confdir)) {
123         argv[i] = "-noauth";
124         i++;
125     }
126     /* if logging is on, pass "-log" to new bosserver */
127     if (DoLogging) {
128         argv[i] = "-log";
129         i++;
130     }
131
132 #ifndef AFS_NT40_ENV
133     /* if syslog logging is on, pass "-syslog" to new bosserver */
134     if (DoSyslog) {
135         char *arg=(char *)malloc(40); /* enough for -syslog=# */
136         if ( DoSyslogFacility != LOG_DAEMON ) {
137             snprintf(arg, 40, "-syslog=%d", DoSyslogFacility);
138         } else {
139             strcpy(arg, "-syslog");
140         }
141         argv[i] = arg;
142         i++;
143     }
144 #endif
145
146     /* null-terminate argument list */
147     argv[i] = NULL;
148
149     /* close random fd's */
150     for (i = 3; i < 64; i++) {
151         close(i);
152     }
153
154     execv(argv[0], argv);  /* should not return */
155     _exit(1);
156 #endif /* AFS_NT40_ENV */
157 }
158
159 /* make sure a dir exists */
160 static MakeDir(adir)
161 register char *adir; {
162     struct stat tstat;
163     register afs_int32 code;
164     if (stat(adir, &tstat) < 0 || (tstat.st_mode & S_IFMT) != S_IFDIR) {
165         int reqPerm;
166         unlink(adir);
167         reqPerm = GetRequiredDirPerm (adir);
168         if (reqPerm == -1) reqPerm = 0777;
169 #ifdef AFS_NT40_ENV
170         /* underlying filesystem may not support directory protection */
171         code = mkdir(adir);
172 #else
173         code = mkdir(adir, reqPerm);
174 #endif
175         return code;
176     }
177     return 0;
178 }
179
180 /* create all the bozo dirs */
181 static CreateDirs() {
182     
183     MakeDir(AFSDIR_USR_DIRPATH);
184     MakeDir(AFSDIR_SERVER_AFS_DIRPATH);
185     MakeDir(AFSDIR_SERVER_BIN_DIRPATH);
186     MakeDir(AFSDIR_SERVER_ETC_DIRPATH); 
187     MakeDir(AFSDIR_SERVER_LOCAL_DIRPATH);
188     MakeDir(AFSDIR_SERVER_DB_DIRPATH); 
189     MakeDir(AFSDIR_SERVER_LOGS_DIRPATH);
190 #ifndef AFS_NT40_ENV
191     MakeDir(AFSDIR_CLIENT_VICE_DIRPATH);
192     MakeDir(AFSDIR_CLIENT_ETC_DIRPATH);
193
194     symlink(AFSDIR_SERVER_THISCELL_FILEPATH, AFSDIR_CLIENT_THISCELL_FILEPATH); 
195     symlink(AFSDIR_SERVER_CELLSERVDB_FILEPATH, AFSDIR_CLIENT_CELLSERVDB_FILEPATH);
196 #endif /* AFS_NT40_ENV */
197     return 0;
198 }
199
200 /* strip the \\n from the end of the line, if it is present */
201 static StripLine(abuffer)
202 register char *abuffer; {
203     register char *tp;
204     
205     tp = abuffer + strlen(abuffer); /* starts off pointing at the null  */
206     if(tp == abuffer) return 0;     /* null string, no last character to check */
207     tp--;       /* aim at last character */
208     if (*tp == '\n') *tp = 0;
209     return 0;
210 }
211
212 /* write one bnode's worth of entry into the file */
213 static bzwrite(abnode, at)
214 register struct bnode *abnode;
215 register struct bztemp *at; {
216     register int i;
217     char tbuffer[BOZO_BSSIZE];
218     register afs_int32 code;
219
220     if (abnode->notifier)
221         fprintf(at->file, "bnode %s %s %d %s\n", 
222                 abnode->type->name, abnode->name, abnode->fileGoal, abnode->notifier);
223     else
224         fprintf(at->file, "bnode %s %s %d\n", abnode->type->name, abnode->name, abnode->fileGoal);
225     for(i=0;;i++) {
226         code = bnode_GetParm(abnode, i, tbuffer, BOZO_BSSIZE);
227         if (code) {
228             if (code != BZDOM) return code;
229             break;
230         }
231         fprintf(at->file, "parm %s\n", tbuffer);
232     }
233     fprintf(at->file, "end\n");
234     return 0;
235 }
236
237 #define MAXPARMS    20
238 ReadBozoFile(aname)
239 char *aname; {
240     register FILE *tfile;
241     char tbuffer[BOZO_BSSIZE];
242     register char *tp;
243     char *instp, *typep, *notifier, *notp;
244     register afs_int32 code;
245     afs_int32 ktmask, ktday, kthour, ktmin, ktsec;
246     afs_int32 i, goal;
247     struct bnode *tb;
248     char *parms[MAXPARMS];
249 #ifdef BOS_RESTRICTED_MODE
250     int rmode;
251 #endif
252
253     /* rename BozoInit to BosServer for the user */
254     if (!aname) {
255         /* if BozoInit exists and BosConfig doesn't, try a rename */
256         if (access(AFSDIR_SERVER_BOZINIT_FILEPATH, 0) == 0
257             && access(AFSDIR_SERVER_BOZCONF_FILEPATH, 0) != 0) {
258             code = renamefile(AFSDIR_SERVER_BOZINIT_FILEPATH, AFSDIR_SERVER_BOZCONF_FILEPATH);
259             if (code < 0)
260                 perror("bosconfig rename");
261         }
262 #ifdef BOS_NEW_CONFIG
263         if (access(AFSDIR_SERVER_BOZCONFNEW_FILEPATH, 0) == 0) {
264              code = renamefile(AFSDIR_SERVER_BOZCONFNEW_FILEPATH,
265                                AFSDIR_SERVER_BOZCONF_FILEPATH);
266              if (code < 0)
267                   perror("bosconfig rename");
268         }
269 #endif        
270     }
271
272     /* setup default times we want to do restarts */
273     bozo_nextRestartKT.mask = KTIME_HOUR | KTIME_MIN | KTIME_DAY;
274     bozo_nextRestartKT.hour = 4; /* 4 am */
275     bozo_nextRestartKT.min = 0;
276     bozo_nextRestartKT.day =    0;  /* Sunday */
277     bozo_nextDayKT.mask = KTIME_HOUR | KTIME_MIN;
278     bozo_nextDayKT.hour = 5;
279     bozo_nextDayKT.min = 0;
280
281     for(code=0;code<MAXPARMS;code++)
282         parms[code] = (char *) 0;
283     instp = typep = notifier = (char *) 0;
284     tfile = (FILE *) 0;
285     if (!aname) aname = (char *)bozo_fileName;
286     tfile = fopen(aname, "r");
287     if (!tfile) 
288         return 0;       /* -1 */
289     instp = (char *) malloc(BOZO_BSSIZE);
290     typep = (char *) malloc(BOZO_BSSIZE);
291     notifier = notp = (char *) malloc(BOZO_BSSIZE);
292     while (1) {
293         /* ok, read lines giving parms and such from the file */
294         tp = fgets(tbuffer, sizeof(tbuffer), tfile);
295         if (tp == (char *) 0) break;    /* all done */
296
297         if (strncmp(tbuffer, "restarttime", 11) == 0) {
298             code = sscanf(tbuffer, "restarttime %d %d %d %d %d",
299                           &ktmask, &ktday, &kthour, &ktmin, &ktsec);
300             if (code != 5) {
301                 code = -1;
302                 goto fail;
303             }
304             /* otherwise we've read in the proper ktime structure; now assign
305                it and continue processing */
306             bozo_nextRestartKT.mask = ktmask;
307             bozo_nextRestartKT.day = ktday;
308             bozo_nextRestartKT.hour = kthour;
309             bozo_nextRestartKT.min = ktmin;
310             bozo_nextRestartKT.sec = ktsec;
311             continue;
312         }
313
314         if (strncmp(tbuffer, "checkbintime", 12) == 0) {
315             code = sscanf(tbuffer, "checkbintime %d %d %d %d %d",
316                           &ktmask, &ktday, &kthour, &ktmin, &ktsec);
317             if (code != 5) {
318                 code = -1;
319                 goto fail;
320             }
321             /* otherwise we've read in the proper ktime structure; now assign
322                it and continue processing */
323             bozo_nextDayKT.mask = ktmask;       /* time to restart the system */
324             bozo_nextDayKT.day = ktday;
325             bozo_nextDayKT.hour = kthour;
326             bozo_nextDayKT.min = ktmin;
327             bozo_nextDayKT.sec = ktsec;
328             continue;
329         }
330
331 #ifdef BOS_RESTRICTED_MODE
332         if (strncmp(tbuffer, "restrictmode", 12) == 0) {
333             code = sscanf(tbuffer, "restrictmode %d",
334                           &rmode);
335             if (code != 1) {
336                 code = -1;
337                 goto fail;
338             }
339             if (rmode !=0 && rmode != 1) {
340                  code = -1;
341                  goto fail;
342             } 
343             bozo_isrestricted=rmode;
344             continue;
345         }
346 #endif
347         
348         if (strncmp("bnode", tbuffer, 5) != 0) {
349             code = -1;
350             goto fail;
351         }
352         notifier = notp;
353         code = sscanf(tbuffer, "bnode %s %s %d %s", typep, instp, &goal, notifier);
354         if (code < 3) {
355             code = -1;
356             goto fail;
357         } else if (code == 3)
358             notifier = (char *)0;
359         
360         for(i=0;i<MAXPARMS;i++) {
361             /* now read the parms, until we see an "end" line */
362             tp = fgets(tbuffer, sizeof(tbuffer), tfile);
363             if (!tp) {
364                 code = -1;
365                 goto fail;
366             }
367             StripLine(tbuffer);
368             if (!strncmp(tbuffer, "end", 3)) break;
369             if (strncmp(tbuffer, "parm ", 5)) {
370                 code = -1;
371                 goto fail;    /* no "parm " either */
372             }
373             if (!parms[i])  /* make sure there's space */
374                 parms[i] = (char *) malloc(BOZO_BSSIZE);
375             strcpy(parms[i], tbuffer+5);    /* remember the parameter for later */
376         }
377
378         /* ok, we have the type and parms, now create the object */
379         code = bnode_Create(typep, instp, &tb, parms[0], parms[1], parms[2],
380                           parms[3], parms[4], notifier,
381                           goal ? BSTAT_NORMAL : BSTAT_SHUTDOWN);
382         if (code) goto fail;
383
384         /* bnode created in 'temporarily shutdown' state;
385            check to see if we are supposed to run this guy,
386             and if so, start the process up */
387         if (goal) {
388             bnode_SetStat(tb, BSTAT_NORMAL);    /* set goal, taking effect immediately */
389         }
390         else {
391             bnode_SetStat(tb, BSTAT_SHUTDOWN);
392         }
393     }
394     /* all done */
395     code = 0;
396
397 fail:
398     if (instp) free(instp);
399     if (typep) free(typep);
400     for(i=0;i<MAXPARMS;i++) if (parms[i]) free(parms[i]);
401     if (tfile) fclose(tfile);
402     return code;
403 }
404
405 /* write a new bozo file */
406 WriteBozoFile(aname)
407 char *aname; {
408     register FILE *tfile;
409     char tbuffer[AFSDIR_PATH_MAX];
410     register afs_int32 code;
411     struct bztemp btemp;
412
413     if (!aname) aname = (char *)bozo_fileName;
414     strcpy(tbuffer, aname);
415     strcat(tbuffer, ".NBZ");
416     tfile = fopen(tbuffer, "w");
417     if (!tfile) return -1;
418     btemp.file = tfile;
419 #ifdef BOS_RESTRICTED_MODE
420     fprintf(tfile, "restrictmode %d\n", bozo_isrestricted);
421 #endif
422     fprintf(tfile, "restarttime %d %d %d %d %d\n", bozo_nextRestartKT.mask,
423             bozo_nextRestartKT.day, bozo_nextRestartKT.hour, bozo_nextRestartKT.min,
424             bozo_nextRestartKT.sec);
425     fprintf(tfile, "checkbintime %d %d %d %d %d\n", bozo_nextDayKT.mask,
426             bozo_nextDayKT.day, bozo_nextDayKT.hour, bozo_nextDayKT.min,
427             bozo_nextDayKT.sec);
428     code = bnode_ApplyInstance(bzwrite, &btemp);
429     if (code || (code = ferror(tfile))) {       /* something went wrong */
430         fclose(tfile);
431         unlink(tbuffer);
432         return code;
433     }
434     /* close the file, check for errors and snap new file into place */
435     if (fclose(tfile) == EOF) {
436         unlink(tbuffer);
437         return -1;
438     }
439     code = renamefile(tbuffer, aname);
440     if (code) {
441         unlink(tbuffer);
442         return -1;
443     }
444     return 0;
445 }
446
447 static bdrestart(abnode, arock)
448 register struct bnode *abnode;
449 char *arock; {
450     register afs_int32 code;
451     
452     if (abnode->fileGoal != BSTAT_NORMAL || abnode->goal != BSTAT_NORMAL)
453         return 0;       /* don't restart stopped bnodes */
454     bnode_Hold(abnode);
455     code = bnode_RestartP(abnode);
456     if (code) {
457         /* restart the dude */
458         bnode_SetStat(abnode, BSTAT_SHUTDOWN);
459         bnode_WaitStatus(abnode, BSTAT_SHUTDOWN);
460         bnode_SetStat(abnode, BSTAT_NORMAL);
461     }
462     bnode_Release(abnode);
463     return 0;   /* keep trying all bnodes */
464 }
465
466 #define BOZO_MINSKIP 3600           /* minimum to advance clock */
467 /* lwp to handle system restarts */
468 static BozoDaemon() {
469     register afs_int32 now;
470     
471     /* now initialize the values */
472     bozo_newKTs = 1;
473     while (1) {
474         IOMGR_Sleep(60);
475         now = FT_ApproxTime();
476
477 #ifdef BOS_RESTRICTED_MODE
478         if (bozo_restdisable) {
479              bozo_Log("Restricted mode disabled by signal\n");
480              bozo_restdisable=0;
481         }
482 #endif
483         if (bozo_newKTs) {      /* need to recompute restart times */
484             bozo_newKTs = 0;    /* done for a while */
485             nextRestart = ktime_next(&bozo_nextRestartKT, BOZO_MINSKIP);
486             nextDay = ktime_next(&bozo_nextDayKT, BOZO_MINSKIP);
487         }
488
489         /* see if we should do a restart */
490         if (now > nextRestart) {
491             BOZO_ReBozo(0);     /* doesn't come back */
492         }
493         
494         /* see if we should restart a server */
495         if (now > nextDay) {
496             nextDay = ktime_next(&bozo_nextDayKT, BOZO_MINSKIP);
497             
498             /* call the bnode restartp function, and restart all that require it */
499             bnode_ApplyInstance(bdrestart, 0);
500         }
501     }
502 }
503
504 #ifdef AFS_AIX32_ENV
505 static tweak_config()
506 {
507     FILE *f;
508     char c[80];
509     int s, sb_max, ipfragttl;
510
511     sb_max = 131072;
512     ipfragttl = 20;
513     f = popen("/usr/sbin/no -o sb_max", "r");
514     s = fscanf(f, "sb_max = %d", &sb_max);
515     fclose(f);
516     if (s < 1) 
517         return;
518     f = popen("/usr/sbin/no -o ipfragttl", "r");
519     s = fscanf(f, "ipfragttl = %d", &ipfragttl);
520     fclose(f);
521     if (s < 1) 
522         ipfragttl = 20;
523
524     if (sb_max < 131072) 
525         sb_max = 131072;
526     if (ipfragttl > 20)
527         ipfragttl = 20;
528     
529     sprintf(c, "/usr/sbin/no -o sb_max=%d -o ipfragttl=%d", sb_max, ipfragttl);
530     f = popen(c, "r");
531     fclose(f);
532 }
533 #endif
534
535 /*
536  * This routine causes the calling process to go into the background and
537  * to lose its controlling tty.
538  *
539  * It does not close or otherwise alter the standard file descriptors.
540  *
541  * It writes warning messages to the standard error output if certain
542  * fundamental errors occur.
543  *
544  * This routine requires
545  * 
546  * #include <sys/types.h>
547  * #include <sys/stat.h>
548  * #include <fcntl.h>
549  * #include <unistd.h>
550  * #include <stdlib.h>
551  *
552  * and has been tested on:
553  *
554  * AIX 4.2
555  * Digital Unix 4.0D
556  * HP-UX 11.0
557  * IRIX 6.5
558  * Linux 2.1.125
559  * Solaris 2.5
560  * Solaris 2.6
561  */
562
563 #ifndef AFS_NT40_ENV
564 static void
565 background(void)
566 {
567     /* 
568      * A process is a process group leader if its process ID
569      * (getpid()) and its process group ID (getpgrp()) are the same.
570      */
571
572     /*
573      * To create a new session (and thereby lose our controlling
574      * terminal) we cannot be a process group leader.
575      *
576      * To guarantee we are not a process group leader, we fork and
577      * let the parent process exit.
578      */
579
580     if (getpid() == getpgrp()) {
581         pid_t pid;
582         pid = fork();
583         switch(pid) {
584         case -1:
585             abort();    /* leave footprints */
586             break;
587         case 0:         /* child */
588             break;
589         default:        /* parent */
590             exit(0);
591             break;
592         }
593     }
594
595     /*
596      * By here, we are not a process group leader, so we can make a
597      * new session and become the session leader.
598      */
599
600     {
601         pid_t sid = setsid();
602
603         if (sid == -1) {
604             static char err[] = "bosserver: WARNING: setsid() failed\n";
605             write(STDERR_FILENO, err, sizeof err - 1);
606         }
607     }
608
609     /*
610      * Once we create a new session, the current process is a
611      * session leader without a controlling tty.
612      *
613      * On some systems, the first tty device the session leader
614      * opens automatically becomes the controlling tty for the
615      * session.
616      *
617      * So, to guarantee we do not acquire a controlling tty, we fork
618      * and let the parent process exit.  The child process is not a
619      * session leader, and so it will not acquire a controlling tty
620      * even if it should happen to open a tty device.
621      */
622
623     if (getpid() == getpgrp()) {
624         pid_t pid;
625         pid = fork();
626         switch(pid) {
627         case -1:
628             abort();    /* leave footprints */
629             break;
630         case 0:         /* child */
631             break;
632         default:        /* parent */
633             exit(0);
634             break;
635         }
636     }
637
638     /*
639      * check that we no longer have a controlling tty
640      */
641
642     {
643         int fd;
644
645         fd = open("/dev/tty", O_RDONLY);
646
647         if (fd >= 0) {
648             static char err[] = "bosserver: WARNING: /dev/tty still attached\n";
649             close(fd);
650             write(STDERR_FILENO, err, sizeof err - 1);
651         }
652     }
653 }
654 #endif /* ! AFS_NT40_ENV */
655
656 /* start a process and monitor it */
657
658 #include "AFS_component_version_number.c"
659
660
661 main (argc, argv,envp)
662 int argc;
663 char **argv;
664 char **envp;
665 {
666     struct rx_service *tservice;
667     register afs_int32 code;
668     struct afsconf_dir *tdir;
669     int noAuth = 0;
670     struct ktc_encryptionKey tkey;
671     int i;
672     char namebuf[AFSDIR_PATH_MAX];
673
674 #ifdef  AFS_AIX32_ENV
675     struct sigaction nsa;
676
677     /* for some reason, this permits user-mode RX to run a lot faster.
678      * we do it here in the bosserver, so we don't have to do it 
679      * individually in each server.
680      */
681     tweak_config();
682
683     /*
684      * The following signal action for AIX is necessary so that in case of a 
685      * crash (i.e. core is generated) we can include the user's data section 
686      * in the core dump. Unfortunately, by default, only a partial core is
687      * generated which, in many cases, isn't too useful.
688      */
689     sigemptyset(&nsa.sa_mask);
690     nsa.sa_handler = SIG_DFL;
691     nsa.sa_flags = SA_FULLDUMP;
692     sigaction(SIGSEGV, &nsa, NULL);
693     sigaction(SIGABRT, &nsa, NULL);
694 #endif
695 #ifdef BOS_RESTRICTED_MODE
696     signal(SIGFPE, bozo_insecureme);
697 #endif
698
699 #ifdef AFS_NT40_ENV
700     /* Initialize winsock */
701     if (afs_winsockInit() < 0) {
702         ReportErrorEventAlt(AFSEVT_SVR_WINSOCK_INIT_FAILED, 0, argv[0], 0);
703         fprintf(stderr, "%s: Couldn't initialize winsock.\n", argv[0]);
704         exit(2);
705     }
706 #endif
707
708     /* Initialize dirpaths */
709     if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
710 #ifdef AFS_NT40_ENV
711         ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
712 #endif
713         fprintf(stderr, "%s: Unable to obtain AFS server directory.\n", argv[0]);
714         exit(2);
715     }
716
717     /* some path inits */
718     bozo_fileName = AFSDIR_SERVER_BOZCONF_FILEPATH;
719
720     /* initialize the list of dirpaths that the bosserver has
721      * an interest in monitoring */
722     initBosEntryStats();
723
724 #if defined(AFS_SGI_ENV)
725     /* offer some protection if AFS isn't loaded */
726     if (syscall(AFS_SYSCALL, AFSOP_ENDLOG) < 0 && errno == ENOPKG) {
727         printf("bosserver: AFS doesn't appear to be configured in O.S..\n");
728         exit(1);
729     }
730 #endif
731
732     /* parse cmd line */
733     for(code=1;code<argc;code++) {
734         if (strcmp(argv[code], "-noauth")==0) {
735             /* set noauth flag */
736             noAuth = 1;
737         }
738         else if (strcmp(argv[code], "-log")==0) {
739             /* set extra logging flag */
740             DoLogging = 1;
741         }
742 #ifndef AFS_NT40_ENV
743         else if (strcmp(argv[code], "-syslog")==0) {
744             /* set syslog logging flag */
745             DoSyslog = 1;
746         } 
747         else if (strncmp(argv[code], "-syslog=",8)==0) {
748             DoSyslog = 1;
749             DoSyslogFacility = atoi(argv[code]+8);
750         }
751 #endif
752         else if (strcmp(argv[code], "-enable_peer_stats")==0) {
753             rx_enablePeerRPCStats();
754         }
755         else if (strcmp(argv[code], "-enable_process_stats")==0) {
756             rx_enableProcessRPCStats();
757         }
758 #ifdef BOS_RESTRICTED_MODE
759         else if (strcmp(argv[code], "-restricted")==0) {
760             bozo_isrestricted=1;
761         }
762 #endif
763         else {
764
765             /* hack to support help flag */
766
767 #ifndef AFS_NT40_ENV
768             printf("Usage: bosserver [-noauth] [-log] "
769                    "[-syslog[=FACILITY]] "
770                    "[-enable_peer_stats] [-enable_process_stats] "
771                    "[-help]\n");
772 #else
773             printf("Usage: bosserver [-noauth] [-log] "
774                    "[-enable_peer_stats] [-enable_process_stats] "
775                    "[-help]\n");
776 #endif
777             fflush(stdout);
778
779             exit(0);
780         }
781     }
782
783 #ifndef AFS_NT40_ENV
784     if (geteuid() != 0) {
785         printf("bosserver: must be run as root.\n");
786         exit(1);
787     }
788 #endif
789
790     code = bnode_Init();
791     if (code) {
792         printf("bosserver: could not init bnode package, code %d\n", code);
793         exit(1);
794     }
795
796     bnode_Register("fs", &fsbnode_ops, 3);
797     bnode_Register("simple", &ezbnode_ops, 1);
798     bnode_Register("cron", &cronbnode_ops, 2);
799
800     /* create useful dirs */
801     CreateDirs();
802
803     /* chdir to AFS log directory */
804     chdir(AFSDIR_SERVER_LOGS_DIRPATH);
805
806 #if 0
807     fputs(AFS_GOVERNMENT_MESSAGE, stdout);
808     fflush(stdout);
809 #endif
810
811     /* go into the background and remove our controlling tty */
812
813 #ifndef AFS_NT40_ENV
814     background();
815 #endif /* ! AFS_NT40_ENV */
816
817     if (!DoSyslog) {
818         strcpy(namebuf, AFSDIR_BOZLOG_FILE);
819         strcat(namebuf, ".old");
820         renamefile(AFSDIR_BOZLOG_FILE, namebuf);    /* try rename first */
821         bozo_logFile = fopen(AFSDIR_BOZLOG_FILE, "a");
822         if (!bozo_logFile) {
823             printf("bosserver: can't initialize log file (%s).\n",
824                    AFSDIR_SERVER_BOZLOG_FILEPATH);
825             exit(1);
826         }
827         /* keep log closed normally, so can be removed */
828         fclose(bozo_logFile);
829     } else {
830 #ifndef AFS_NT40_ENV
831         openlog("bosserver", LOG_PID, DoSyslogFacility);
832 #endif
833     }
834
835     /* Write current state of directory permissions to log file */
836     DirAccessOK ();
837
838     for (i=0;i<10;i++) {
839         code = rx_Init(htons(AFSCONF_NANNYPORT));
840         if (code) {
841             bozo_Log("can't initialize rx: code=%d\n",code);
842             sleep(3);
843         }
844         else break;
845     }
846     if (i>=10) {
847         bozo_Log("Bos giving up, can't initialize rx\n");
848         exit(code);
849     }
850
851     code = LWP_CreateProcess(BozoDaemon, BOZO_LWP_STACKSIZE, /* priority */ 1,
852                              /* parm */0, "bozo-the-clown", &bozo_pid);
853
854     /* try to read the key from the config file */
855     tdir = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH);
856     if (!tdir) {
857         /* try to create local cell config file */
858         struct afsconf_cell tcell;
859         strcpy(tcell.name, "localcell");
860         tcell.numServers = 1;
861         code = gethostname(tcell.hostName[0], MAXHOSTCHARS);
862         if (code) {
863             bozo_Log("failed to get hostname, code %d\n", errno);
864             exit(1);
865         }
866         if (tcell.hostName[0][0] == 0) {
867             bozo_Log("host name not set, can't start\n");
868             bozo_Log("try the 'hostname' command\n");
869             exit(1);
870         }
871         memset(tcell.hostAddr, 0, sizeof(tcell.hostAddr));      /* not computed */
872         code = afsconf_SetCellInfo(bozo_confdir, AFSDIR_SERVER_ETC_DIRPATH, &tcell);
873         if (code) {
874             bozo_Log("could not create cell database in '%s' (code %d), quitting\n", AFSDIR_SERVER_ETC_DIRPATH, code);
875             exit(1);
876         }
877         tdir = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH);
878         if (!tdir) {
879             bozo_Log("failed to open newly-created cell database, quitting\n");
880             exit(1);
881         }
882     }
883
884     /* read init file, starting up programs */
885     if (code=ReadBozoFile(0)) {
886         bozo_Log("bosserver: Something is wrong (%d) with the bos configuration file %s; aborting\n", code, AFSDIR_SERVER_BOZCONF_FILEPATH);
887         exit(code);
888     }
889
890     /* opened the cell databse */
891     bozo_confdir = tdir;
892     code = afsconf_GetKey(tdir, 999, &tkey);
893
894     /* allow super users to manage RX statistics */
895     rx_SetRxStatUserOk(bozo_rxstat_userok);
896
897     /* have bcrypt key now */
898
899     afsconf_SetNoAuthFlag(tdir, noAuth);
900
901     bozo_rxsc[0] = (struct rx_securityClass *) rxnull_NewServerSecurityObject();
902     bozo_rxsc[1] = (struct rx_securityClass *) 0;
903     bozo_rxsc[2] = (struct rx_securityClass *) rxkad_NewServerSecurityObject(
904                                          0, tdir, afsconf_GetKey, (char *) 0);
905
906     /* These two lines disallow jumbograms */
907     rx_maxReceiveSize = OLD_MAX_PACKET_SIZE;
908     rxi_nSendFrags = rxi_nRecvFrags = 1;
909
910     tservice = rx_NewService(/* port */ 0, /* service id */ 1, 
911                   /*service name */ "bozo", /* security classes */ bozo_rxsc,
912                   /* numb sec classes */ 3, BOZO_ExecuteRequest);
913     rx_SetMinProcs(tservice, 2);
914     rx_SetMaxProcs(tservice, 4);
915     rx_SetStackSize(tservice, BOZO_LWP_STACKSIZE); /* so gethostbyname works (in cell stuff) */
916
917     tservice = rx_NewService(0, RX_STATS_SERVICE_ID, "rpcstats", bozo_rxsc,
918                   3, RXSTATS_ExecuteRequest);
919     rx_SetMinProcs(tservice, 2);
920     rx_SetMaxProcs(tservice, 4);
921     rx_StartServer(1);      /* donate this process */
922 }
923
924 void
925 bozo_Log(a,b,c,d,e,f)
926 char *a, *b, *c, *d, *e, *f; {
927     char tdate[26];
928     time_t myTime;
929
930     if (DoSyslog) {
931 #ifndef AFS_NT40_ENV
932         syslog(LOG_INFO, a, b, c, d, e, f);
933 #endif
934     } else {
935         myTime = time(0);
936         strcpy(tdate, ctime(&myTime));  /* copy out of static area asap */
937         tdate[24] = ':';
938
939         /* log normally closed, so can be removed */
940
941         bozo_logFile=fopen(AFSDIR_SERVER_BOZLOG_FILEPATH, "a");
942         if(bozo_logFile == NULL) {
943             printf("bosserver: WARNING: problem with %s", AFSDIR_SERVER_BOZLOG_FILEPATH);
944             fflush(stdout);
945         }
946
947         if (bozo_logFile) {
948             fprintf(bozo_logFile, "%s ", tdate);
949             fprintf(bozo_logFile, a, b, c, d, e, f);
950             fflush(bozo_logFile);
951         } else {
952             printf("%s ", tdate);
953             printf(a, b, c, d, e, f);
954         }
955         
956         /* close so rm BosLog works */
957         fclose(bozo_logFile);
958     }
959 }