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