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