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