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