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