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