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