rx-prototypes-20080924
[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 #include <stdarg.h>
25 #ifdef AFS_NT40_ENV
26 #include <winsock2.h>
27 #include <direct.h>
28 #include <io.h>
29 #include <WINNT/afsevent.h>
30 #else
31 #include <unistd.h>
32 #include <netinet/in.h>
33 #include <syslog.h>
34 #endif /* AFS_NT40_ENV */
35 #include <afs/cellconfig.h>
36 #include <rx/rx.h>
37 #include <rx/xdr.h>
38 #include <rx/rx_globals.h>
39 #include "bosint.h"
40 #include "bnode.h"
41 #include "bosprototypes.h"
42 #include <rx/rxkad.h>
43 #include <afs/keys.h>
44 #include <afs/ktime.h>
45 #include <afs/afsutil.h>
46 #include <afs/fileutil.h>
47 #include <afs/procmgmt.h>       /* signal(), kill(), wait(), etc. */
48 #if defined(AFS_SGI_ENV)
49 #include <afs/afs_args.h>
50 #endif
51
52 #define BOZO_LWP_STACKSIZE      16000
53 extern int BOZO_ExecuteRequest();
54 extern int RXSTATS_ExecuteRequest();
55 extern struct bnode_ops fsbnode_ops, dafsbnode_ops, ezbnode_ops, cronbnode_ops;
56
57 struct afsconf_dir *bozo_confdir = 0;   /* bozo configuration dir */
58 static PROCESS bozo_pid;
59 struct rx_securityClass *bozo_rxsc[3];
60 const char *bozo_fileName;
61 FILE *bozo_logFile;
62
63 int DoLogging = 0;
64 int DoSyslog = 0;
65 #ifndef AFS_NT40_ENV
66 int DoSyslogFacility = LOG_DAEMON;
67 #endif
68 static afs_int32 nextRestart;
69 static afs_int32 nextDay;
70
71 struct ktime bozo_nextRestartKT, bozo_nextDayKT;
72 int bozo_newKTs;
73 int rxBind = 0;
74 int rxkadDisableDotCheck = 0;
75
76 #define ADDRSPERSITE 16         /* Same global is in rx/rx_user.c */
77 afs_uint32 SHostAddrs[ADDRSPERSITE];
78
79 #ifdef BOS_RESTRICTED_MODE
80 int bozo_isrestricted = 0;
81 int bozo_restdisable = 0;
82
83 void
84 bozo_insecureme(int sig)
85 {
86     signal(SIGFPE, bozo_insecureme);
87     bozo_isrestricted = 0;
88     bozo_restdisable = 1;
89 }
90 #endif
91
92 struct bztemp {
93     FILE *file;
94 };
95
96 /* check whether caller is authorized to manage RX statistics */
97 int
98 bozo_rxstat_userok(struct rx_call *call)
99 {
100     return afsconf_SuperUser(bozo_confdir, call, NULL);
101 }
102
103 /* restart bozo process */
104 int
105 bozo_ReBozo()
106 {
107 #ifdef AFS_NT40_ENV
108     /* exit with restart code; SCM integrator process will restart bosserver */
109     int status = BOSEXIT_RESTART;
110
111     /* if noauth flag is set, pass "-noauth" to new bosserver */
112     if (afsconf_GetNoAuthFlag(bozo_confdir)) {
113         status |= BOSEXIT_NOAUTH_FLAG;
114     }
115     /* if logging is on, pass "-log" to new bosserver */
116     if (DoLogging) {
117         status |= BOSEXIT_LOGGING_FLAG;
118     }
119     exit(status);
120 #else
121     /* exec new bosserver process */
122     char *argv[4];
123     int i = 0;
124
125     argv[i] = (char *)AFSDIR_SERVER_BOSVR_FILEPATH;
126     i++;
127
128     /* if noauth flag is set, pass "-noauth" to new bosserver */
129     if (afsconf_GetNoAuthFlag(bozo_confdir)) {
130         argv[i] = "-noauth";
131         i++;
132     }
133     /* if logging is on, pass "-log" to new bosserver */
134     if (DoLogging) {
135         argv[i] = "-log";
136         i++;
137     }
138 #ifndef AFS_NT40_ENV
139     /* if syslog logging is on, pass "-syslog" to new bosserver */
140     if (DoSyslog) {
141         char *arg = (char *)malloc(40); /* enough for -syslog=# */
142         if (DoSyslogFacility != LOG_DAEMON) {
143             snprintf(arg, 40, "-syslog=%d", DoSyslogFacility);
144         } else {
145             strcpy(arg, "-syslog");
146         }
147         argv[i] = arg;
148         i++;
149     }
150 #endif
151
152     /* null-terminate argument list */
153     argv[i] = NULL;
154
155     /* close random fd's */
156     for (i = 3; i < 64; i++) {
157         close(i);
158     }
159
160     execv(argv[0], argv);       /* should not return */
161     _exit(1);
162 #endif /* AFS_NT40_ENV */
163 }
164
165 /* make sure a dir exists */
166 static int
167 MakeDir(const char *adir)
168 {
169     struct stat tstat;
170     register afs_int32 code;
171     if (stat(adir, &tstat) < 0 || (tstat.st_mode & S_IFMT) != S_IFDIR) {
172         int reqPerm;
173         unlink(adir);
174         reqPerm = GetRequiredDirPerm(adir);
175         if (reqPerm == -1)
176             reqPerm = 0777;
177 #ifdef AFS_NT40_ENV
178         /* underlying filesystem may not support directory protection */
179         code = mkdir(adir);
180 #else
181         code = mkdir(adir, reqPerm);
182 #endif
183         return code;
184     }
185     return 0;
186 }
187
188 /* create all the bozo dirs */
189 static int
190 CreateDirs()
191 {
192     if ((!strncmp
193          (AFSDIR_USR_DIRPATH, AFSDIR_CLIENT_ETC_DIRPATH,
194           strlen(AFSDIR_USR_DIRPATH)))
195         ||
196         (!strncmp
197          (AFSDIR_USR_DIRPATH, AFSDIR_SERVER_BIN_DIRPATH,
198           strlen(AFSDIR_USR_DIRPATH)))) {
199         MakeDir(AFSDIR_USR_DIRPATH);
200     }
201     if (!strncmp
202         (AFSDIR_SERVER_AFS_DIRPATH, AFSDIR_SERVER_BIN_DIRPATH,
203          strlen(AFSDIR_SERVER_AFS_DIRPATH))) {
204         MakeDir(AFSDIR_SERVER_AFS_DIRPATH);
205     }
206     MakeDir(AFSDIR_SERVER_BIN_DIRPATH);
207     MakeDir(AFSDIR_SERVER_ETC_DIRPATH);
208     MakeDir(AFSDIR_SERVER_LOCAL_DIRPATH);
209     MakeDir(AFSDIR_SERVER_DB_DIRPATH);
210     MakeDir(AFSDIR_SERVER_LOGS_DIRPATH);
211 #ifndef AFS_NT40_ENV
212     if (!strncmp
213         (AFSDIR_CLIENT_VICE_DIRPATH, AFSDIR_CLIENT_ETC_DIRPATH,
214          strlen(AFSDIR_CLIENT_VICE_DIRPATH))) {
215         MakeDir(AFSDIR_CLIENT_VICE_DIRPATH);
216     }
217     MakeDir(AFSDIR_CLIENT_ETC_DIRPATH);
218
219     symlink(AFSDIR_SERVER_THISCELL_FILEPATH, AFSDIR_CLIENT_THISCELL_FILEPATH);
220     symlink(AFSDIR_SERVER_CELLSERVDB_FILEPATH,
221             AFSDIR_CLIENT_CELLSERVDB_FILEPATH);
222 #endif /* AFS_NT40_ENV */
223     return 0;
224 }
225
226 /* strip the \\n from the end of the line, if it is present */
227 static int
228 StripLine(register char *abuffer)
229 {
230     register char *tp;
231
232     tp = abuffer + strlen(abuffer);     /* starts off pointing at the null  */
233     if (tp == abuffer)
234         return 0;               /* null string, no last character to check */
235     tp--;                       /* aim at last character */
236     if (*tp == '\n')
237         *tp = 0;
238     return 0;
239 }
240
241 /* write one bnode's worth of entry into the file */
242 static
243 bzwrite(register struct bnode *abnode, register struct bztemp *at)
244 {
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, char *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()
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 #ifndef AFS_NT40_ENV
729     int nofork = 0;
730     struct stat sb;
731 #endif
732 #ifdef  AFS_AIX32_ENV
733     struct sigaction nsa;
734
735     /* for some reason, this permits user-mode RX to run a lot faster.
736      * we do it here in the bosserver, so we don't have to do it 
737      * individually in each server.
738      */
739     tweak_config();
740
741     /*
742      * The following signal action for AIX is necessary so that in case of a 
743      * crash (i.e. core is generated) we can include the user's data section 
744      * in the core dump. Unfortunately, by default, only a partial core is
745      * generated which, in many cases, isn't too useful.
746      */
747     sigemptyset(&nsa.sa_mask);
748     nsa.sa_handler = SIG_DFL;
749     nsa.sa_flags = SA_FULLDUMP;
750     sigaction(SIGSEGV, &nsa, NULL);
751     sigaction(SIGABRT, &nsa, NULL);
752 #endif
753     osi_audit_init();
754 #ifdef BOS_RESTRICTED_MODE
755     signal(SIGFPE, bozo_insecureme);
756 #endif
757
758 #ifdef AFS_NT40_ENV
759     /* Initialize winsock */
760     if (afs_winsockInit() < 0) {
761         ReportErrorEventAlt(AFSEVT_SVR_WINSOCK_INIT_FAILED, 0, argv[0], 0);
762         fprintf(stderr, "%s: Couldn't initialize winsock.\n", argv[0]);
763         exit(2);
764     }
765 #endif
766
767     /* Initialize dirpaths */
768     if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
769 #ifdef AFS_NT40_ENV
770         ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
771 #endif
772         fprintf(stderr, "%s: Unable to obtain AFS server directory.\n",
773                 argv[0]);
774         exit(2);
775     }
776
777     /* some path inits */
778     bozo_fileName = AFSDIR_SERVER_BOZCONF_FILEPATH;
779
780     /* initialize the list of dirpaths that the bosserver has
781      * an interest in monitoring */
782     initBosEntryStats();
783
784 #if defined(AFS_SGI_ENV)
785     /* offer some protection if AFS isn't loaded */
786     if (syscall(AFS_SYSCALL, AFSOP_ENDLOG) < 0 && errno == ENOPKG) {
787         printf("bosserver: AFS doesn't appear to be configured in O.S..\n");
788         exit(1);
789     }
790 #endif
791
792     /* parse cmd line */
793     for (code = 1; code < argc; code++) {
794         if (strcmp(argv[code], "-noauth") == 0) {
795             /* set noauth flag */
796             noAuth = 1;
797         } else if (strcmp(argv[code], "-log") == 0) {
798             /* set extra logging flag */
799             DoLogging = 1;
800         }
801 #ifndef AFS_NT40_ENV
802         else if (strcmp(argv[code], "-syslog") == 0) {
803             /* set syslog logging flag */
804             DoSyslog = 1;
805         } else if (strncmp(argv[code], "-syslog=", 8) == 0) {
806             DoSyslog = 1;
807             DoSyslogFacility = atoi(argv[code] + 8);
808         } else if (strcmp(argv[code], "-nofork") == 0) {
809             nofork = 1;
810         }
811 #endif
812         else if (strcmp(argv[code], "-enable_peer_stats") == 0) {
813             rx_enablePeerRPCStats();
814         } else if (strcmp(argv[code], "-enable_process_stats") == 0) {
815             rx_enableProcessRPCStats();
816         }
817 #ifdef BOS_RESTRICTED_MODE
818         else if (strcmp(argv[code], "-restricted") == 0) {
819             bozo_isrestricted = 1;
820         }
821 #endif
822         else if (strcmp(argv[code], "-rxbind") == 0) {
823             rxBind = 1;
824         }
825         else if (strcmp(argv[code], "-allow-dotted-principals") == 0) {
826             rxkadDisableDotCheck = 1;
827         }
828         else if (!strcmp(argv[i], "-rxmaxmtu")) {
829             if ((i + 1) >= argc) {
830                 fprintf(stderr, "missing argument for -rxmaxmtu\n"); 
831                 exit(1); 
832             }
833             rxMaxMTU = atoi(argv[++i]);
834             if ((rxMaxMTU < RX_MIN_PACKET_SIZE) || 
835                 (rxMaxMTU > RX_MAX_PACKET_DATA_SIZE)) {
836                 printf("rxMaxMTU %d invalid; must be between %d-%d\n",
837                         rxMaxMTU, RX_MIN_PACKET_SIZE, 
838                         RX_MAX_PACKET_DATA_SIZE);
839                 exit(1);
840             }
841         }
842         else if (strcmp(argv[code], "-auditlog") == 0) {
843             int tempfd, flags;
844             FILE *auditout;
845             char oldName[MAXPATHLEN];
846             char *fileName = argv[++code];
847
848 #ifndef AFS_NT40_ENV
849             struct stat statbuf;
850             
851             if ((lstat(fileName, &statbuf) == 0) 
852                 && (S_ISFIFO(statbuf.st_mode))) {
853                 flags = O_WRONLY | O_NONBLOCK;
854             } else 
855 #endif
856             {
857                 strcpy(oldName, fileName);
858                 strcat(oldName, ".old");
859                 renamefile(fileName, oldName);
860                 flags = O_WRONLY | O_TRUNC | O_CREAT;
861             }
862             tempfd = open(fileName, flags, 0666);
863             if (tempfd > -1) {
864                 auditout = fdopen(tempfd, "a");
865                 if (auditout) {
866                     osi_audit_file(auditout);
867                 } else
868                     printf("Warning: auditlog %s not writable, ignored.\n", fileName);
869             } else
870                 printf("Warning: auditlog %s not writable, ignored.\n", fileName);
871         }
872         else {
873
874             /* hack to support help flag */
875
876 #ifndef AFS_NT40_ENV
877             printf("Usage: bosserver [-noauth] [-log] "
878                    "[-auditlog <log path>] "
879                    "[-rxmaxmtu <bytes>] [-rxbind] [-allow-dotted-principals]"
880                    "[-syslog[=FACILITY]] "
881                    "[-enable_peer_stats] [-enable_process_stats] "
882                    "[-nofork] " "[-help]\n");
883 #else
884             printf("Usage: bosserver [-noauth] [-log] "
885                    "[-auditlog <log path>] "
886                    "[-rxmaxmtu <bytes>] [-rxbind] [-allow-dotted-principals]"
887                    "[-enable_peer_stats] [-enable_process_stats] "
888                    "[-help]\n");
889 #endif
890             fflush(stdout);
891
892             exit(0);
893         }
894     }
895
896 #ifndef AFS_NT40_ENV
897     if (geteuid() != 0) {
898         printf("bosserver: must be run as root.\n");
899         exit(1);
900     }
901 #endif
902
903     code = bnode_Init();
904     if (code) {
905         printf("bosserver: could not init bnode package, code %d\n", code);
906         exit(1);
907     }
908
909     bnode_Register("fs", &fsbnode_ops, 3);
910     bnode_Register("dafs", &dafsbnode_ops, 4);
911     bnode_Register("simple", &ezbnode_ops, 1);
912     bnode_Register("cron", &cronbnode_ops, 2);
913
914     /* create useful dirs */
915     CreateDirs();
916
917     /* chdir to AFS log directory */
918     chdir(AFSDIR_SERVER_LOGS_DIRPATH);
919
920 #if 0
921     fputs(AFS_GOVERNMENT_MESSAGE, stdout);
922     fflush(stdout);
923 #endif
924
925     /* go into the background and remove our controlling tty, close open 
926        file desriptors
927      */
928
929 #ifndef AFS_NT40_ENV
930     if (!nofork)
931         daemon(1, 0);
932 #endif /* ! AFS_NT40_ENV */
933
934     if ((!DoSyslog)
935 #ifndef AFS_NT40_ENV
936         && ((lstat(AFSDIR_BOZLOG_FILE, &sb) == 0) && 
937         !(S_ISFIFO(sb.st_mode)))
938 #endif
939         ) {
940         strcpy(namebuf, AFSDIR_BOZLOG_FILE);
941         strcat(namebuf, ".old");
942         renamefile(AFSDIR_BOZLOG_FILE, namebuf);        /* try rename first */
943         bozo_logFile = fopen(AFSDIR_BOZLOG_FILE, "a");
944         if (!bozo_logFile) {
945             printf("bosserver: can't initialize log file (%s).\n",
946                    AFSDIR_SERVER_BOZLOG_FILEPATH);
947             exit(1);
948         }
949         /* keep log closed normally, so can be removed */
950         fclose(bozo_logFile);
951     } else {
952 #ifndef AFS_NT40_ENV
953         openlog("bosserver", LOG_PID, DoSyslogFacility);
954 #endif
955     }
956
957     /* Write current state of directory permissions to log file */
958     DirAccessOK();
959
960     for (i = 0; i < 10; i++) {
961         code = rx_Init(htons(AFSCONF_NANNYPORT));
962         if (code) {
963             bozo_Log("can't initialize rx: code=%d\n", code);
964             sleep(3);
965         } else
966             break;
967     }
968     if (i >= 10) {
969         bozo_Log("Bos giving up, can't initialize rx\n");
970         exit(code);
971     }
972
973     code = LWP_CreateProcess(BozoDaemon, BOZO_LWP_STACKSIZE, /* priority */ 1,
974                              (void *) /*parm */ 0, "bozo-the-clown",
975                              &bozo_pid);
976
977     /* try to read the key from the config file */
978     tdir = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH);
979     if (!tdir) {
980         /* try to create local cell config file */
981         struct afsconf_cell tcell;
982         strcpy(tcell.name, "localcell");
983         tcell.numServers = 1;
984         code = gethostname(tcell.hostName[0], MAXHOSTCHARS);
985         if (code) {
986             bozo_Log("failed to get hostname, code %d\n", errno);
987             exit(1);
988         }
989         if (tcell.hostName[0][0] == 0) {
990             bozo_Log("host name not set, can't start\n");
991             bozo_Log("try the 'hostname' command\n");
992             exit(1);
993         }
994         memset(tcell.hostAddr, 0, sizeof(tcell.hostAddr));      /* not computed */
995         code =
996             afsconf_SetCellInfo(bozo_confdir, AFSDIR_SERVER_ETC_DIRPATH,
997                                 &tcell);
998         if (code) {
999             bozo_Log
1000                 ("could not create cell database in '%s' (code %d), quitting\n",
1001                  AFSDIR_SERVER_ETC_DIRPATH, code);
1002             exit(1);
1003         }
1004         tdir = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH);
1005         if (!tdir) {
1006             bozo_Log
1007                 ("failed to open newly-created cell database, quitting\n");
1008             exit(1);
1009         }
1010     }
1011
1012     /* read init file, starting up programs */
1013     if (code = ReadBozoFile(0)) {
1014         bozo_Log
1015             ("bosserver: Something is wrong (%d) with the bos configuration file %s; aborting\n",
1016              code, AFSDIR_SERVER_BOZCONF_FILEPATH);
1017         exit(code);
1018     }
1019
1020     /* opened the cell databse */
1021     bozo_confdir = tdir;
1022
1023     /* allow super users to manage RX statistics */
1024     rx_SetRxStatUserOk(bozo_rxstat_userok);
1025
1026     /* have bcrypt key now */
1027
1028     afsconf_SetNoAuthFlag(tdir, noAuth);
1029
1030     bozo_rxsc[0] = rxnull_NewServerSecurityObject();
1031     bozo_rxsc[1] = (struct rx_securityClass *)0;
1032     bozo_rxsc[2] =
1033         rxkad_NewServerSecurityObject(0, tdir, afsconf_GetKey, NULL);
1034
1035     /* Disable jumbograms */
1036     rx_SetNoJumbo();
1037
1038     if (rxMaxMTU != -1) {
1039         rx_SetMaxMTU(rxMaxMTU);
1040     }
1041
1042     if (rxBind) {
1043         afs_int32 ccode;
1044 #ifndef AFS_NT40_ENV
1045         if (AFSDIR_SERVER_NETRESTRICT_FILEPATH || 
1046             AFSDIR_SERVER_NETINFO_FILEPATH) {
1047             char reason[1024];
1048             ccode = parseNetFiles(SHostAddrs, NULL, NULL,
1049                                            ADDRSPERSITE, reason,
1050                                            AFSDIR_SERVER_NETINFO_FILEPATH,
1051                                            AFSDIR_SERVER_NETRESTRICT_FILEPATH);
1052         } else 
1053 #endif  
1054         {
1055             ccode = rx_getAllAddr(SHostAddrs, ADDRSPERSITE);
1056         }
1057         if (ccode == 1) 
1058             host = SHostAddrs[0];
1059     }
1060
1061     tservice = rx_NewServiceHost(host,  /* port */ 0, /* service id */ 1,
1062                              /*service name */ "bozo",
1063                              /* security classes */
1064                              bozo_rxsc,
1065                              /* numb sec classes */ 3, BOZO_ExecuteRequest);
1066     rx_SetMinProcs(tservice, 2);
1067     rx_SetMaxProcs(tservice, 4);
1068     rx_SetStackSize(tservice, BOZO_LWP_STACKSIZE);      /* so gethostbyname works (in cell stuff) */
1069     if (rxkadDisableDotCheck) {
1070         rx_SetSecurityConfiguration(tservice, RXS_CONFIG_FLAGS,
1071                                     (void *)RXS_CONFIG_FLAGS_DISABLE_DOTCHECK);
1072     }
1073
1074     tservice =
1075         rx_NewServiceHost(host, 0, RX_STATS_SERVICE_ID, "rpcstats", bozo_rxsc,
1076                           3, RXSTATS_ExecuteRequest);
1077     rx_SetMinProcs(tservice, 2);
1078     rx_SetMaxProcs(tservice, 4);
1079     rx_StartServer(1);          /* donate this process */
1080     return 0;
1081 }
1082
1083 void
1084 bozo_Log(char *format, ...)
1085 {
1086     char tdate[27];
1087     time_t myTime;
1088     va_list ap;
1089
1090     va_start(ap, format);
1091
1092     if (DoSyslog) {
1093 #ifndef AFS_NT40_ENV
1094         vsyslog(LOG_INFO, format, ap);
1095 #endif
1096     } else {
1097         myTime = time(0);
1098         strcpy(tdate, ctime(&myTime));  /* copy out of static area asap */
1099         tdate[24] = ':';
1100
1101         /* log normally closed, so can be removed */
1102
1103         bozo_logFile = fopen(AFSDIR_SERVER_BOZLOG_FILEPATH, "a");
1104         if (bozo_logFile == NULL) {
1105             printf("bosserver: WARNING: problem with %s\n",
1106                    AFSDIR_SERVER_BOZLOG_FILEPATH);
1107             printf("%s ", tdate);
1108             vprintf(format, ap);
1109             fflush(stdout);
1110         } else {
1111             fprintf(bozo_logFile, "%s ", tdate);
1112             vfprintf(bozo_logFile, format, ap);
1113
1114             /* close so rm BosLog works */
1115             fclose(bozo_logFile);
1116         }
1117     }
1118 }