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