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