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