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