bos: convert struct bnode to use opr
[openafs.git] / src / bozo / bosoprocs.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  *
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <afsconfig.h>
11 #include <afs/param.h>
12 #include <afs/stds.h>
13
14 #include <roken.h>
15
16 #ifdef AFS_NT40_ENV
17 #include <sys/utime.h>
18 #endif /* AFS_NT40_ENV */
19
20 #include <rx/xdr.h>
21 #include <rx/rx.h>
22 #include <rx/rxkad.h>
23 #include <afs/cellconfig.h>
24 #include <afs/keys.h>
25 #include <afs/afsutil.h>
26 #include <afs/fileutil.h>
27 #include <afs/ktime.h>
28 #include <afs/audit.h>
29 #include <afs/kautils.h>
30
31 #include "bnode.h"
32 #include "bnode_internal.h"
33 #include "bosint.h"
34 #include "bosprototypes.h"
35
36 extern struct ktime bozo_nextRestartKT, bozo_nextDayKT;
37
38 extern struct afsconf_dir *bozo_confdir;
39 extern int bozo_newKTs;
40 extern int DoLogging;
41 extern int bozo_isrestricted;
42
43 afs_int32
44 SBOZO_GetRestartTime(struct rx_call *acall, afs_int32 atype, struct bozo_netKTime *aktime)
45 {
46     afs_int32 code;
47
48     code = 0;                   /* assume success */
49     switch (atype) {
50     case 1:
51         memcpy(aktime, &bozo_nextRestartKT, sizeof(struct ktime));
52         break;
53
54     case 2:
55         memcpy(aktime, &bozo_nextDayKT, sizeof(struct ktime));
56         break;
57
58     default:
59         code = BZDOM;
60         break;
61     }
62
63     return code;
64 }
65
66 afs_int32
67 SBOZO_SetRestartTime(struct rx_call *acall, afs_int32 atype, struct bozo_netKTime *aktime)
68 {
69     afs_int32 code;
70     char caller[MAXKTCNAMELEN];
71
72     /* check for proper permissions */
73     if (!afsconf_SuperUser(bozo_confdir, acall, caller)) {
74         code = BZACCESS;
75         goto fail;
76     }
77     if (DoLogging)
78         bozo_Log("%s is executing SetRestartTime\n", caller);
79
80     code = 0;                   /* assume success */
81     switch (atype) {
82     case 1:
83         memcpy(&bozo_nextRestartKT, aktime, sizeof(struct ktime));
84         break;
85
86     case 2:
87         memcpy(&bozo_nextDayKT, aktime, sizeof(struct ktime));
88         break;
89
90     default:
91         code = BZDOM;
92         break;
93     }
94
95     if (code == 0) {
96         /* try to update the bozo init file */
97         code = WriteBozoFile(0);
98         bozo_newKTs = 1;
99     }
100
101   fail:
102     osi_auditU(acall, BOS_SetRestartEvent, code, AUD_END);
103     return code;
104 }
105
106 afs_int32
107 SBOZO_Exec(struct rx_call *acall, char *acmd)
108 {
109
110     char caller[MAXKTCNAMELEN];
111     int code;
112
113     if (!afsconf_SuperUser(bozo_confdir, acall, caller)) {
114         code = BZACCESS;
115         goto fail;
116     }
117     if (bozo_isrestricted) {
118         code = BZACCESS;
119         goto fail;
120     }
121     if (DoLogging)
122         bozo_Log("%s is executing the shell command '%s'\n", caller, acmd);
123
124     /* should copy output to acall, but don't yet cause its hard */
125     /*  hard... NOT!  Nnow _at least_ return the exit status */
126     code = system(acmd);
127     osi_auditU(acall, BOS_ExecEvent, code, AUD_STR, acmd, AUD_END);
128
129   fail:
130     return code;
131 }
132
133 afs_int32
134 SBOZO_GetDates(struct rx_call *acall, char *aname, afs_int32 *atime,
135                afs_int32 *abakTime, afs_int32 *aoldTime)
136 {
137     struct stat tstat;
138     char *strp;
139     char tbuffer[AFSDIR_PATH_MAX];
140
141     *atime = *abakTime = *aoldTime = 0;
142
143     /* construct local path from canonical (wire-format) path */
144     if (ConstructLocalBinPath(aname, &strp)) {
145         return 0;
146     }
147     strcpy(tbuffer, strp);
148     free(strp);
149
150     strp = tbuffer + strlen(tbuffer);
151
152     if (!stat(tbuffer, &tstat)) {
153         *atime = tstat.st_mtime;
154     }
155
156     strcpy(strp, ".BAK");
157     if (!stat(tbuffer, &tstat)) {
158         *abakTime = tstat.st_mtime;
159     }
160
161     strcpy(strp, ".OLD");
162     if (!stat(tbuffer, &tstat)) {
163         *aoldTime = tstat.st_mtime;
164     }
165     return 0;
166 }
167
168 afs_int32
169 SBOZO_UnInstall(struct rx_call *acall, char *aname)
170 {
171     char *filepath;
172     char fpOld[AFSDIR_PATH_MAX], fpBak[AFSDIR_PATH_MAX];
173     afs_int32 code;
174     char caller[MAXKTCNAMELEN];
175     struct stat tstat;
176
177     if (!afsconf_SuperUser(bozo_confdir, acall, caller)) {
178         code = BZACCESS;
179         osi_auditU(acall, BOS_UnInstallEvent, code, AUD_STR, aname, AUD_END);
180         return code;
181     }
182     if (bozo_isrestricted) {
183         code = BZACCESS;
184         osi_auditU(acall, BOS_UnInstallEvent, code, AUD_STR, aname, AUD_END);
185         return code;
186     }
187
188     /* construct local path from canonical (wire-format) path */
189     if (ConstructLocalBinPath(aname, &filepath)) {
190         return BZNOENT;
191     }
192
193     if (DoLogging)
194         bozo_Log("%s is executing UnInstall '%s'\n", caller, filepath);
195
196     strcpy(fpBak, filepath);
197     strcat(fpBak, ".BAK");
198     strcpy(fpOld, filepath);
199     strcat(fpOld, ".OLD");
200
201     code = rk_rename(fpBak, filepath);
202     if (code) {
203         /* can't find .BAK, try .OLD */
204         code = rk_rename(fpOld, filepath);
205         if (code && errno == ENOENT)    /* If doesn't exist don't fail */
206             code = 0;
207     } else {
208         /* now rename .OLD to .BAK */
209         if (stat(fpOld, &tstat) == 0)
210             code = rk_rename(fpOld, fpBak);
211     }
212     if (code)
213         code = errno;
214
215     osi_auditU(acall, BOS_UnInstallEvent, code, AUD_STR, filepath, AUD_END);
216     free(filepath);
217
218     return code;
219 }
220
221 #define BOZO_OLDTIME        (7*24*3600) /* 7 days old */
222 static void
223 SaveOldFiles(char *aname)
224 {
225     afs_int32 code;
226     char bbuffer[AFSDIR_PATH_MAX], obuffer[AFSDIR_PATH_MAX];
227     struct stat tstat;
228     afs_int32 now;
229     afs_int32 oldTime, bakTime;
230
231     strcpy(bbuffer, aname);
232     strcat(bbuffer, ".BAK");
233     strcpy(obuffer, aname);
234     strcat(obuffer, ".OLD");
235     now = FT_ApproxTime();
236
237     code = stat(aname, &tstat);
238     if (code < 0)
239         return;                 /* can't stat file */
240
241     code = stat(obuffer, &tstat);       /* discover old file's time */
242     if (code)
243         oldTime = 0;
244     else
245         oldTime = tstat.st_mtime;
246
247     code = stat(bbuffer, &tstat);       /* discover back file's time */
248     if (code)
249         bakTime = 0;
250     else
251         bakTime = tstat.st_mtime;
252
253     if (bakTime && (oldTime == 0 || bakTime < now - BOZO_OLDTIME)) {
254         /* no .OLD file, or .BAK is at least a week old */
255         code = rk_rename(bbuffer, obuffer);
256     }
257
258     /* finally rename to .BAK extension */
259     rk_rename(aname, bbuffer);
260 }
261
262 afs_int32
263 SBOZO_Install(struct rx_call *acall, char *aname, afs_int32 asize, afs_int32 mode, afs_int32 amtime)
264 {
265     afs_int32 code;
266     int fd;
267     afs_int32 len;
268     afs_int32 total;
269 #ifdef AFS_NT40_ENV
270     struct _utimbuf utbuf;
271 #else
272     struct timeval tvb[2];
273 #endif
274     char filepath[AFSDIR_PATH_MAX], tbuffer[AFSDIR_PATH_MAX], *fpp;
275     char caller[MAXKTCNAMELEN];
276
277     if (!afsconf_SuperUser(bozo_confdir, acall, caller))
278         return BZACCESS;
279     if (bozo_isrestricted)
280         return BZACCESS;
281
282     /* construct local path from canonical (wire-format) path */
283     if (ConstructLocalBinPath(aname, &fpp)) {
284         return BZNOENT;
285     }
286     strcpy(filepath, fpp);
287     free(fpp);
288
289     if (DoLogging)
290         bozo_Log("%s is executing Install '%s'\n", caller, filepath);
291
292     /* open file */
293     fpp = filepath + strlen(filepath);
294     strcpy(fpp, ".NEW");        /* append ".NEW" to end of filepath */
295     fd = open(filepath, O_CREAT | O_RDWR | O_TRUNC, 0777);
296     if (fd < 0)
297         return errno;
298     total = 0;
299     while (1) {
300         len = rx_Read(acall, tbuffer, sizeof(tbuffer));
301         if (len < 0) {
302             close(fd);
303             unlink(filepath);
304             return 102;
305         }
306         if (len == 0)
307             break;              /* no more input */
308         code = write(fd, tbuffer, len);
309         if (code != len) {
310             close(fd);
311             unlink(filepath);
312             return 100;
313         }
314         total += len;           /* track total written for safety check at end */
315     }
316     close(fd);
317     if (asize != total) {
318         unlink(filepath);
319         return 101;             /* wrong size */
320     }
321
322     /* save old files */
323     *fpp = '\0';                /* remove ".NEW" from end of filepath */
324     SaveOldFiles(filepath);     /* don't care if it works, still install */
325
326     /* all done, rename to final name */
327     strcpy(tbuffer, filepath);
328     strcat(tbuffer, ".NEW");
329     code = (rk_rename(tbuffer, filepath) ? errno : 0);
330
331     /* label file with same time for our sanity */
332 #ifdef AFS_NT40_ENV
333     utbuf.actime = utbuf.modtime = amtime;
334     _utime(filepath, &utbuf);
335 #else
336     tvb[0].tv_sec = tvb[1].tv_sec = amtime;
337     tvb[0].tv_usec = tvb[1].tv_usec = 0;
338     utimes(filepath, tvb);
339 #endif /* AFS_NT40_ENV */
340
341     if (mode)
342         chmod(filepath, mode);
343
344     if (code < 0) {
345         osi_auditU(acall, BOS_InstallEvent, code, AUD_STR, filepath, AUD_END);
346         return errno;
347     } else
348         return 0;
349 }
350
351 afs_int32
352 SBOZO_SetCellName(struct rx_call *acall, char *aname)
353 {
354     struct afsconf_cell tcell;
355     afs_int32 code;
356     char caller[MAXKTCNAMELEN];
357     char clones[MAXHOSTSPERCELL];
358
359     if (!afsconf_SuperUser(bozo_confdir, acall, caller)) {
360         code = BZACCESS;
361         goto fail;
362     }
363     if (DoLogging)
364         bozo_Log("%s is executing SetCellName '%s'\n", caller, aname);
365
366     code =
367         afsconf_GetExtendedCellInfo(bozo_confdir, NULL, NULL, &tcell,
368                                     clones);
369     if (code)
370         goto fail;
371
372     /* Check that tcell has enough space for the new cellname. */
373     if (strlen(aname) > sizeof tcell.name - 1) {
374         bozo_Log
375             ("ERROR: SetCellName: cell name '%s' exceeds %ld bytes (cell name not changed)\n",
376              aname, (long)(sizeof tcell.name - 1));
377         code = BZDOM;
378         goto fail;
379     }
380
381     strcpy(tcell.name, aname);
382     code =
383         afsconf_SetExtendedCellInfo(bozo_confdir, AFSDIR_SERVER_ETC_DIRPATH,
384                                     &tcell, clones);
385
386   fail:
387     osi_auditU(acall, BOS_SetCellEvent, code, AUD_STR, aname, AUD_END);
388     return code;
389 }
390
391 afs_int32
392 SBOZO_GetCellName(struct rx_call *acall, char **aname)
393 {
394     afs_int32 code;
395     char tname[MAXCELLCHARS];
396
397     code = afsconf_GetLocalCell(bozo_confdir, tname, sizeof(tname));
398     if (code) {
399         /* must set output parameters even if aborting */
400         *aname = malloc(1);
401         **aname = 0;
402     } else {
403         *aname = strdup(tname);
404     }
405
406     return code;
407 }
408
409 afs_int32
410 SBOZO_GetCellHost(struct rx_call *acall, afs_uint32 awhich, char **aname)
411 {
412     afs_int32 code;
413     struct afsconf_cell tcell;
414     char *tp;
415     char clones[MAXHOSTSPERCELL];
416
417     code =
418         afsconf_GetExtendedCellInfo(bozo_confdir, NULL, NULL, &tcell,
419                                     clones);
420     if (code)
421         goto fail;
422
423     if (awhich >= tcell.numServers) {
424         code = BZDOM;
425         goto fail;
426     }
427
428     tp = tcell.hostName[awhich];
429     if (clones[awhich]) {
430         asprintf(aname, "[%s]", tp);
431     } else
432         *aname = strdup(tp);
433     goto done;
434
435   fail:
436     *aname = malloc(1); /* return fake string */
437     **aname = 0;
438
439   done:
440     return code;
441 }
442
443 afs_int32
444 SBOZO_DeleteCellHost(struct rx_call *acall, char *aname)
445 {
446     afs_int32 code;
447     struct afsconf_cell tcell;
448     afs_int32 which;
449     int i;
450     char caller[MAXKTCNAMELEN];
451     char clones[MAXHOSTSPERCELL];
452
453     if (!afsconf_SuperUser(bozo_confdir, acall, caller)) {
454         code = BZACCESS;
455         goto fail;
456     }
457     if (DoLogging)
458         bozo_Log("%s is executing DeleteCellHost '%s'\n", caller, aname);
459
460     code =
461         afsconf_GetExtendedCellInfo(bozo_confdir, NULL, NULL, &tcell,
462                                     clones);
463     if (code)
464         goto fail;
465
466     which = -1;
467     for (i = 0; i < tcell.numServers; i++) {
468         if (strcmp(tcell.hostName[i], aname) == 0) {
469             which = i;
470             break;
471         }
472     }
473
474     if (which < 0) {
475         code = BZNOENT;
476         goto fail;
477     }
478
479     memset(&tcell.hostAddr[which], 0, sizeof(struct sockaddr_in));
480     memset(tcell.hostName[which], 0, MAXHOSTCHARS);
481     code =
482         afsconf_SetExtendedCellInfo(bozo_confdir, AFSDIR_SERVER_ETC_DIRPATH,
483                                     &tcell, clones);
484
485   fail:
486     osi_auditU(acall, BOS_DeleteHostEvent, code, AUD_STR, aname, AUD_END);
487     return code;
488 }
489
490 afs_int32
491 SBOZO_AddCellHost(struct rx_call *acall, char *aname)
492 {
493     afs_int32 code;
494     struct afsconf_cell tcell;
495     afs_int32 which;
496     int i;
497     char caller[MAXKTCNAMELEN];
498     char clones[MAXHOSTSPERCELL];
499     char *n;
500     char isClone = 0;
501
502     if (!afsconf_SuperUser(bozo_confdir, acall, caller)) {
503         code = BZACCESS;
504         goto fail;
505     }
506     if (DoLogging)
507         bozo_Log("%s is executing AddCellHost '%s'\n", caller, aname);
508
509     code =
510         afsconf_GetExtendedCellInfo(bozo_confdir, NULL, NULL, &tcell,
511                                     clones);
512     if (code)
513         goto fail;
514
515     n = aname;
516     if (*n == '[') {
517         *(n + strlen(n) - 1) = 0;
518         ++n;
519         isClone = 1;
520     }
521
522     which = -1;
523     for (i = 0; i < tcell.numServers; i++) {
524         if (strcmp(tcell.hostName[i], n) == 0) {
525             which = i;
526             break;
527         }
528     }
529     if (which < 0) {
530         which = tcell.numServers;
531         tcell.numServers++;
532
533         /*
534          * Check that tcell has enough space for an additional host.
535          *
536          * We assume that tcell.hostAddr[] and tcell.hostName[] have the
537          * same number of entries.
538          */
539         if (tcell.numServers >
540             sizeof tcell.hostAddr / sizeof tcell.hostAddr[0]) {
541             bozo_Log
542                 ("ERROR: AddCellHost: attempt to add more than %ld database servers (database server '%s' not added)\n",
543                  (long)(sizeof tcell.hostAddr / sizeof tcell.hostAddr[0]),
544                  aname);
545             code = BZDOM;
546             goto fail;
547         }
548
549         /* Check that tcell has enough space for the new hostname. */
550         if (strlen(aname) > sizeof tcell.hostName[0] - 1) {
551             bozo_Log
552                 ("ERROR: AddCellHost: host name '%s' exceeds %ld bytes (not added)\n",
553                  aname, (long)(sizeof tcell.hostName[0] - 1));
554             code = BZDOM;
555             goto fail;
556         }
557     }
558
559     memset(&tcell.hostAddr[which], 0, sizeof(struct sockaddr_in));
560     strcpy(tcell.hostName[which], n);
561     clones[which] = isClone;
562     code =
563         afsconf_SetExtendedCellInfo(bozo_confdir, AFSDIR_SERVER_ETC_DIRPATH,
564                                     &tcell, clones);
565
566   fail:
567     osi_auditU(acall, BOS_AddHostEvent, code, AUD_STR, aname, AUD_END);
568     return code;
569 }
570
571 afs_int32
572 SBOZO_ListKeys(struct rx_call *acall, afs_int32 an, afs_int32 *akvno,
573                struct bozo_key *akey, struct bozo_keyInfo *akeyinfo)
574 {
575     struct afsconf_keys tkeys;
576     afs_int32 code;
577     struct stat tstat;
578     int noauth = 0;
579     char caller[MAXKTCNAMELEN];
580     rxkad_level enc_level = rxkad_clear;
581
582     if (!afsconf_SuperUser(bozo_confdir, acall, caller)) {
583         code = BZACCESS;
584         goto fail;
585     }
586     if (DoLogging)
587         bozo_Log("%s is executing ListKeys\n", caller);
588
589     code = afsconf_GetKeys(bozo_confdir, &tkeys);
590     if (code)
591         goto fail;
592
593     if (tkeys.nkeys <= an) {
594         code = BZDOM;
595         goto fail;
596     }
597     *akvno = tkeys.key[an].kvno;
598     memset(akeyinfo, 0, sizeof(struct bozo_keyInfo));
599
600     noauth = afsconf_GetNoAuthFlag(bozo_confdir);
601     rxkad_GetServerInfo(rx_ConnectionOf(acall), &enc_level, 0, 0, 0, 0, 0);
602     /*
603      * only return actual keys in noauth or if this is an encrypted connection
604      */
605
606     if ((noauth) || (enc_level == rxkad_crypt)) {
607         memcpy(akey, tkeys.key[an].key, 8);
608     } else
609         memset(akey, 0, 8);
610
611     code = stat(AFSDIR_SERVER_KEY_FILEPATH, &tstat);
612     if (code == 0) {
613         akeyinfo->mod_sec = tstat.st_mtime;
614     }
615     ka_KeyCheckSum(tkeys.key[an].key, &akeyinfo->keyCheckSum);
616     /* only errors is bad key parity */
617
618   fail:
619     if (noauth)
620         osi_auditU(acall, BOS_UnAuthListKeysEvent, code, AUD_END);
621     osi_auditU(acall, BOS_ListKeysEvent, code, AUD_END);
622     return code;
623 }
624
625 afs_int32
626 SBOZO_AddKey(struct rx_call *acall, afs_int32 an, struct bozo_key *akey)
627 {
628     afs_int32 code;
629     char caller[MAXKTCNAMELEN];
630     rxkad_level enc_level = rxkad_clear;
631     int noauth;
632
633     if (!afsconf_SuperUser(bozo_confdir, acall, caller)) {
634         code = BZACCESS;
635         goto fail;
636     }
637     noauth = afsconf_GetNoAuthFlag(bozo_confdir);
638     rxkad_GetServerInfo(rx_ConnectionOf(acall), &enc_level, 0, 0, 0, 0, 0);
639     if ((!noauth) && (enc_level != rxkad_crypt)) {
640         code = BZENCREQ;
641         goto fail;
642     }
643     if (DoLogging)
644         bozo_Log("%s is executing AddKey\n", caller);
645
646     code = afsconf_AddKey(bozo_confdir, an, akey->data, 0);
647     if (code == AFSCONF_KEYINUSE)
648         code = BZKEYINUSE;      /* Unique code for afs rpc calls */
649   fail:
650     osi_auditU(acall, BOS_AddKeyEvent, code, AUD_END);
651     return code;
652 }
653
654 afs_int32
655 SBOZO_SetNoAuthFlag(struct rx_call *acall, afs_int32 aflag)
656 {
657     afs_int32 code = 0;
658     char caller[MAXKTCNAMELEN];
659
660     if (!afsconf_SuperUser(bozo_confdir, acall, caller)) {
661         code = BZACCESS;
662         goto fail;
663     }
664     if (DoLogging)
665         bozo_Log("%s is executing Set No Authentication\n", caller);
666
667     afsconf_SetNoAuthFlag(bozo_confdir, aflag);
668
669   fail:
670     osi_auditU(acall, BOS_SetNoAuthEvent, code, AUD_LONG, aflag, AUD_END);
671     return code;
672 }
673
674 afs_int32
675 SBOZO_DeleteKey(struct rx_call *acall, afs_int32 an)
676 {
677     afs_int32 code;
678     char caller[MAXKTCNAMELEN];
679
680     if (!afsconf_SuperUser(bozo_confdir, acall, caller)) {
681         code = BZACCESS;
682         goto fail;
683     }
684     if (DoLogging)
685         bozo_Log("%s is executing DeleteKey\n", caller);
686
687     code = afsconf_DeleteKey(bozo_confdir, an);
688
689   fail:
690     osi_auditU(acall, BOS_DeleteKeyEvent, code, AUD_END);
691     return code;
692 }
693
694
695 afs_int32
696 SBOZO_ListSUsers(struct rx_call *acall, afs_int32 an, char **aname)
697 {
698     afs_int32 code;
699     char *tp;
700
701     tp = *aname = malloc(256);
702     *tp = 0;                    /* in case getnthuser doesn't null-terminate the string */
703     code = afsconf_GetNthUser(bozo_confdir, an, tp, 256);
704
705   /* fail: */
706     osi_auditU(acall, BOS_ListSUserEvent, code, AUD_END);
707     return code;
708 }
709
710 afs_int32
711 SBOZO_AddSUser(struct rx_call *acall, char *aname)
712 {
713     afs_int32 code;
714     char caller[MAXKTCNAMELEN];
715
716     if (!afsconf_SuperUser(bozo_confdir, acall, caller)) {
717         code = BZACCESS;
718         goto fail;
719     }
720     if (DoLogging)
721         bozo_Log("%s is executing Add SuperUser '%s'\n", caller, aname);
722
723     code = afsconf_AddUser(bozo_confdir, aname);
724
725   fail:
726     osi_auditU(acall, BOS_AddSUserEvent, code, AUD_END);
727     return code;
728 }
729
730 afs_int32
731 SBOZO_DeleteSUser(struct rx_call *acall, char *aname)
732 {
733     afs_int32 code;
734     char caller[MAXKTCNAMELEN];
735
736     if (!afsconf_SuperUser(bozo_confdir, acall, caller)) {
737         code = BZACCESS;
738         goto fail;
739     }
740
741     if (DoLogging)
742         bozo_Log("%s is executing Delete SuperUser '%s'\n", caller, aname);
743
744     code = afsconf_DeleteUser(bozo_confdir, aname);
745
746   fail:
747     osi_auditU(acall, BOS_DeleteSUserEvent, code, AUD_END);
748     return code;
749 }
750
751 afs_int32
752 SBOZO_CreateBnode(struct rx_call *acall, char *atype, char *ainstance,
753                   char *ap1, char *ap2, char *ap3, char *ap4, char *ap5,
754                   char *notifier)
755 {
756     struct bnode *tb;
757     afs_int32 code;
758     char caller[MAXKTCNAMELEN];
759
760     if (!afsconf_SuperUser(bozo_confdir, acall, caller)) {
761         code = BZACCESS;
762         goto fail;
763     }
764     if (bozo_isrestricted) {
765         const char *salvpath = AFSDIR_CANONICAL_SERVER_SALVAGER_FILEPATH;
766         /* for DAFS, 'bos salvage' will pass "salvageserver -client" instead */
767         const char *salsrvpath = AFSDIR_CANONICAL_SERVER_SALSRV_FILEPATH " -client ";
768
769         /* still allow 'bos salvage' to work */
770         if (strcmp(atype, "cron") || strcmp(ainstance, "salvage-tmp")
771             || strcmp(ap2, "now")
772             || (strncmp(ap1, salvpath, strlen(salvpath))
773                 && strncmp(ap1, salsrvpath, strlen(salsrvpath)))) {
774
775             code = BZACCESS;
776             goto fail;
777         }
778     }
779
780     code =
781         bnode_Create(atype, ainstance, &tb, ap1, ap2, ap3, ap4, ap5, notifier,
782                      BSTAT_NORMAL, 1);
783     if (!code)
784         bnode_SetStat(tb, BSTAT_NORMAL);
785
786   fail:
787     osi_auditU(acall, BOS_CreateBnodeEvent, code, AUD_END);
788     return code;
789 }
790
791 afs_int32
792 SBOZO_WaitAll(struct rx_call *acall)
793 {
794     afs_int32 code;
795     char caller[MAXKTCNAMELEN];
796
797     if (!afsconf_SuperUser(bozo_confdir, acall, caller)) {
798         code = BZACCESS;
799         goto fail;
800     }
801
802     if (DoLogging)
803         bozo_Log("%s is executing Wait for All\n", caller);
804
805     code = bnode_WaitAll();
806
807   fail:
808     osi_auditU(acall, BOS_WaitAllEvent, code, AUD_END);
809     return code;
810 }
811
812 afs_int32
813 SBOZO_DeleteBnode(struct rx_call *acall, char *ainstance)
814 {
815     afs_int32 code;
816     char caller[MAXKTCNAMELEN];
817
818     if (!afsconf_SuperUser(bozo_confdir, acall, caller)) {
819         code = BZACCESS;
820         goto fail;
821     }
822     if (bozo_isrestricted) {
823         code = BZACCESS;
824         goto fail;
825     }
826     if (DoLogging)
827         bozo_Log("%s is executing DeleteBnode '%s'\n", caller, ainstance);
828
829     code = bnode_DeleteName(ainstance);
830
831   fail:
832     osi_auditU(acall, BOS_DeleteBnodeEvent, code, AUD_STR, ainstance,
833                AUD_END);
834     return code;
835 }
836
837 static int
838 swproc(struct bnode *abnode, void *arock)
839 {
840     if (abnode->goal == BSTAT_NORMAL)
841         return 0;               /* this one's not shutting down */
842     /* otherwise, we are shutting down */
843     bnode_Hold(abnode);
844     bnode_WaitStatus(abnode, BSTAT_SHUTDOWN);
845     bnode_Release(abnode);
846     return 0;                   /* don't stop apply function early, no matter what */
847 }
848
849 static int
850 stproc(struct bnode *abnode, void *arock)
851 {
852     if (abnode->fileGoal == BSTAT_SHUTDOWN)
853         return 0;               /* don't do these guys */
854
855     bnode_Hold(abnode);
856     bnode_ResetErrorCount(abnode);
857     bnode_SetStat(abnode, BSTAT_NORMAL);
858     bnode_Release(abnode);
859     return 0;
860 }
861
862 static int
863 sdproc(struct bnode *abnode, void *arock)
864 {
865     bnode_Hold(abnode);
866     bnode_SetStat(abnode, BSTAT_SHUTDOWN);
867     bnode_Release(abnode);
868     return 0;
869 }
870
871 /* shutdown and leave down */
872 afs_int32
873 SBOZO_ShutdownAll(struct rx_call *acall)
874 {
875     /* iterate over all bnodes, setting the status to temporarily disabled */
876     afs_int32 code;
877     char caller[MAXKTCNAMELEN];
878
879     /* check for authorization */
880     if (!afsconf_SuperUser(bozo_confdir, acall, caller)) {
881         code = BZACCESS;
882         goto fail;
883     }
884     if (DoLogging)
885         bozo_Log("%s is executing ShutdownAll\n", caller);
886
887     code = bnode_ApplyInstance(sdproc, NULL);
888
889   fail:
890     osi_auditU(acall, BOS_ShutdownAllEvent, code, AUD_END);
891     return code;
892 }
893
894 /* shutdown and restart */
895 afs_int32
896 SBOZO_RestartAll(struct rx_call *acall)
897 {
898     afs_int32 code;
899     char caller[MAXKTCNAMELEN];
900
901     if (!afsconf_SuperUser(bozo_confdir, acall, caller)) {
902         code = BZACCESS;
903         goto fail;
904     }
905     if (DoLogging)
906         bozo_Log("%s is executing RestartAll\n", caller);
907
908     /* start shutdown of all processes */
909     code = bnode_ApplyInstance(sdproc, NULL);
910     if (code)
911         goto fail;
912
913     /* wait for all done */
914     code = bnode_ApplyInstance(swproc, NULL);
915     if (code)
916         goto fail;
917
918     /* start them up again */
919     code = bnode_ApplyInstance(stproc, NULL);
920
921   fail:
922     osi_auditU(acall, BOS_RestartAllEvent, code, AUD_END);
923     return code;
924 }
925
926 afs_int32
927 SBOZO_ReBozo(struct rx_call *acall)
928 {
929     afs_int32 code;
930     char caller[MAXKTCNAMELEN];
931
932     /* acall is null if called internally to restart bosserver */
933     if (acall && !afsconf_SuperUser(bozo_confdir, acall, caller)) {
934         code = BZACCESS;
935         goto fail;
936     }
937     if (DoLogging)
938         bozo_Log("%s is executing ReBozo\n", caller);
939
940     /* start shutdown of all processes */
941     code = bnode_ApplyInstance(sdproc, NULL);
942     if (code)
943         goto fail;
944
945     /* wait for all done */
946     code = bnode_ApplyInstance(swproc, NULL);
947     if (code)
948         goto fail;
949
950     if (acall)
951         osi_auditU(acall, BOS_RebozoEvent, code, AUD_END);
952     else
953         osi_audit(BOS_RebozoIntEvent, code, AUD_END);
954
955     if (acall)
956         rx_EndCall(acall, 0);   /* try to get it done */
957     rx_Finalize();
958     bozo_ReBozo();              /* this reexecs us, and doesn't return, of course */
959
960   fail:
961     /* Differentiate between external and internal ReBozo; prevents AFS_Aud_NoCall event */
962     if (acall)
963         osi_auditU(acall, BOS_RebozoEvent, code, AUD_END);
964     else
965         osi_audit(BOS_RebozoIntEvent, code, AUD_END);
966     return code;                /* should only get here in unusual circumstances */
967 }
968
969 /* make sure all are running */
970 afs_int32
971 SBOZO_StartupAll(struct rx_call *acall)
972 {
973     afs_int32 code;
974     char caller[MAXKTCNAMELEN];
975
976     if (!afsconf_SuperUser(bozo_confdir, acall, caller)) {
977         code = BZACCESS;
978         goto fail;
979     }
980     if (DoLogging)
981         bozo_Log("%s is executing StartupAll\n", caller);
982     code = bnode_ApplyInstance(stproc, NULL);
983
984   fail:
985     osi_auditU(acall, BOS_StartupAllEvent, code, AUD_END);
986     return code;
987 }
988
989 afs_int32
990 SBOZO_Restart(struct rx_call *acall, char *ainstance)
991 {
992     struct bnode *tb;
993     afs_int32 code;
994     char caller[MAXKTCNAMELEN];
995
996     if (!afsconf_SuperUser(bozo_confdir, acall, caller)) {
997         code = BZACCESS;
998         goto fail;
999     }
1000     if (DoLogging)
1001         bozo_Log("%s is executing Restart '%s'\n", caller, ainstance);
1002
1003     tb = bnode_FindInstance(ainstance);
1004     if (!tb) {
1005         code = BZNOENT;
1006         goto fail;
1007     }
1008
1009     /* setup return code */
1010     code = 0;
1011
1012     bnode_Hold(tb);
1013     bnode_SetStat(tb, BSTAT_SHUTDOWN);
1014     code = bnode_WaitStatus(tb, BSTAT_SHUTDOWN);        /* this can fail */
1015     bnode_ResetErrorCount(tb);
1016     bnode_SetStat(tb, BSTAT_NORMAL);
1017     bnode_Release(tb);
1018
1019   fail:
1020     osi_auditU(acall, BOS_RestartEvent, code, AUD_STR, ainstance, AUD_END);
1021     return code;
1022 }
1023
1024 /* set temp status */
1025 afs_int32
1026 SBOZO_SetTStatus(struct rx_call *acall, char *ainstance, afs_int32 astatus)
1027 {
1028     struct bnode *tb;
1029     afs_int32 code;
1030     char caller[MAXKTCNAMELEN];
1031
1032     if (!afsconf_SuperUser(bozo_confdir, acall, caller)) {
1033         code = BZACCESS;
1034         goto fail;
1035     }
1036     if (DoLogging)
1037         bozo_Log("%s is executing SetTempStatus '%s'\n", caller, ainstance);
1038
1039     tb = bnode_FindInstance(ainstance);
1040     if (!tb) {
1041         code = BZNOENT;
1042         goto fail;
1043     }
1044     bnode_Hold(tb);
1045     bnode_ResetErrorCount(tb);
1046     code = bnode_SetStat(tb, astatus);
1047     bnode_Release(tb);
1048
1049   fail:
1050     osi_auditU(acall, BOS_SetTempStatusEvent, code, AUD_STR, ainstance,
1051                AUD_END);
1052     return code;
1053 }
1054
1055 afs_int32
1056 SBOZO_SetStatus(struct rx_call *acall, char *ainstance, afs_int32 astatus)
1057 {
1058     struct bnode *tb;
1059     afs_int32 code;
1060     char caller[MAXKTCNAMELEN];
1061
1062     if (!afsconf_SuperUser(bozo_confdir, acall, caller)) {
1063         code = BZACCESS;
1064         goto fail;
1065     }
1066     if (DoLogging)
1067         bozo_Log("%s is executing SetStatus '%s' (status = %d)\n", caller,
1068                  ainstance, astatus);
1069
1070     tb = bnode_FindInstance(ainstance);
1071     if (!tb) {
1072         code = BZNOENT;
1073         goto fail;
1074     }
1075     bnode_Hold(tb);
1076     bnode_SetFileGoal(tb, astatus);
1077     code = bnode_SetStat(tb, astatus);
1078     bnode_Release(tb);
1079
1080   fail:
1081     osi_auditU(acall, BOS_SetStatusEvent, code, AUD_STR, ainstance, AUD_END);
1082     return code;
1083 }
1084
1085 afs_int32
1086 SBOZO_GetStatus(struct rx_call *acall, char *ainstance, afs_int32 *astat,
1087                 char **astatDescr)
1088 {
1089     struct bnode *tb;
1090     afs_int32 code;
1091
1092     tb = bnode_FindInstance(ainstance);
1093     if (!tb) {
1094         code = BZNOENT;
1095         goto fail;
1096     }
1097
1098     bnode_Hold(tb);
1099     code = bnode_GetStat(tb, astat);
1100     if (code) {
1101         bnode_Release(tb);
1102         goto fail;
1103     }
1104
1105     *astatDescr = malloc(BOZO_BSSIZE);
1106     code = bnode_GetString(tb, *astatDescr, BOZO_BSSIZE);
1107     bnode_Release(tb);
1108     if (code)
1109         (*astatDescr)[0] = 0;   /* null string means no further info */
1110     return 0;
1111
1112   fail:
1113     *astatDescr = malloc(1);
1114     **astatDescr = 0;
1115     return code;
1116 }
1117
1118 struct eidata {
1119     char *iname;
1120     int counter;
1121 };
1122
1123 static int
1124 eifunc(struct bnode *abnode, void *param)
1125 {
1126     struct eidata *arock = (struct eidata *)param;
1127
1128     if (arock->counter-- == 0) {
1129         /* done */
1130         strcpy(arock->iname, abnode->name);
1131         return 1;
1132     } else {
1133         /* not there yet */
1134         return 0;
1135     }
1136 }
1137
1138 static int
1139 ZapFile(const char *adir, const char *aname)
1140 {
1141     char tbuffer[256];
1142     if (snprintf(tbuffer, 256, "%s/%s", adir, aname)<256)
1143         return unlink(tbuffer);
1144     else
1145         return -1;
1146 }
1147
1148 afs_int32
1149 SBOZO_Prune(struct rx_call *acall, afs_int32 aflags)
1150 {
1151     afs_int32 code;
1152     DIR *dirp;
1153     struct dirent *tde;
1154     int i;
1155     char caller[MAXKTCNAMELEN];
1156
1157     if (!afsconf_SuperUser(bozo_confdir, acall, caller)) {
1158         code = BZACCESS;
1159         goto fail;
1160     }
1161     if (bozo_isrestricted) {
1162         code = BZACCESS;
1163         goto fail;
1164     }
1165     if (DoLogging)
1166         bozo_Log("%s is executing Prune (flags=%d)\n", caller, aflags);
1167
1168     /* first scan AFS binary directory */
1169     dirp = opendir(AFSDIR_SERVER_BIN_DIRPATH);
1170     if (dirp) {
1171         for (tde = readdir(dirp); tde; tde = readdir(dirp)) {
1172             i = strlen(tde->d_name);
1173             if (aflags & BOZO_PRUNEOLD) {
1174                 if (i >= 4 && strncmp(tde->d_name + i - 4, ".OLD", 4) == 0)
1175                     ZapFile(AFSDIR_SERVER_BIN_DIRPATH, tde->d_name);
1176             }
1177             if (aflags & BOZO_PRUNEBAK) {
1178                 if (i >= 4 && strncmp(tde->d_name + i - 4, ".BAK", 4) == 0)
1179                     ZapFile(AFSDIR_SERVER_BIN_DIRPATH, tde->d_name);
1180             }
1181         }
1182         closedir(dirp);
1183     }
1184
1185     /* then scan AFS log directory */
1186     dirp = opendir(AFSDIR_SERVER_LOGS_DIRPATH);
1187     if (dirp) {
1188         for (tde = readdir(dirp); tde; tde = readdir(dirp)) {
1189             if (aflags & BOZO_PRUNECORE) {
1190                 if (strncmp(tde->d_name, AFSDIR_CORE_FILE, 4) == 0)
1191                     ZapFile(AFSDIR_SERVER_LOGS_DIRPATH, tde->d_name);
1192             }
1193         }
1194         closedir(dirp);
1195     }
1196     code = 0;
1197
1198   fail:
1199     osi_auditU(acall, BOS_PruneLogs, code, AUD_END);
1200     return code;
1201 }
1202
1203 afs_int32
1204 SBOZO_EnumerateInstance(struct rx_call *acall, afs_int32 anum,
1205                         char **ainstance)
1206 {
1207     struct eidata tdata;
1208
1209     *ainstance = malloc(BOZO_BSSIZE);
1210     **ainstance = 0;
1211     tdata.counter = anum;
1212     tdata.iname = *ainstance;
1213     bnode_ApplyInstance(eifunc, &tdata);
1214     if (tdata.counter >= 0)
1215         return BZDOM;           /* anum > # of actual instances */
1216     else
1217         return 0;
1218 }
1219
1220 struct bozo_bosEntryStats bozo_bosEntryStats[] = {
1221     {NULL, 1, 1, 0755, 02},     /* AFSDIR_SERVER_AFS_DIRPATH    */
1222     {NULL, 1, 1, 0755, 02},     /* AFSDIR_SERVER_ETC_DIRPATH    */
1223     {NULL, 1, 1, 0755, 02},     /* AFSDIR_SERVER_BIN_DIRPATH    */
1224     {NULL, 1, 1, 0755, 02},     /* AFSDIR_SERVER_LOGS_DIRPATH   */
1225     {NULL, 1, 0, 0700, 07},     /* AFSDIR_SERVER_BACKUP_DIRPATH */
1226     {NULL, 1, 1, 0700, 07},     /* AFSDIR_SERVER_DB_DIRPATH     */
1227     {NULL, 1, 1, 0700, 07},     /* AFSDIR_SERVER_LOCAL_DIRPATH  */
1228     {NULL, 0, 1, 0600, 07},     /* AFSDIR_SERVER_KEY_FILEPATH   */
1229     {NULL, 0, 1, 0600, 03}
1230 };                              /* AFSDIR_SERVER_ULIST_FILEPATH */
1231 int bozo_nbosEntryStats =
1232     sizeof(bozo_bosEntryStats) / sizeof(bozo_bosEntryStats[0]);
1233
1234 /* This function performs initialization of the bozo_bosEntrystats[]
1235  * array. This array contains the list of dirs that the bosserver
1236  * is interested in along with their recommended permissions
1237  * NOTE: This initialization is a bit ugly. This was caused because
1238  * the path names require procedural as opposed to static initialization.
1239  * The other fields in the struct are however, statically initialized.
1240  */
1241 int
1242 initBosEntryStats(void)
1243 {
1244     bozo_bosEntryStats[0].path = AFSDIR_SERVER_AFS_DIRPATH;
1245     bozo_bosEntryStats[1].path = AFSDIR_SERVER_ETC_DIRPATH;
1246     bozo_bosEntryStats[2].path = AFSDIR_SERVER_BIN_DIRPATH;
1247     bozo_bosEntryStats[3].path = AFSDIR_SERVER_LOGS_DIRPATH;
1248     bozo_bosEntryStats[4].path = AFSDIR_SERVER_BACKUP_DIRPATH;
1249     bozo_bosEntryStats[5].path = AFSDIR_SERVER_DB_DIRPATH;
1250     bozo_bosEntryStats[6].path = AFSDIR_SERVER_LOCAL_DIRPATH;
1251     bozo_bosEntryStats[7].path = AFSDIR_SERVER_KEY_FILEPATH;
1252     bozo_bosEntryStats[8].path = AFSDIR_SERVER_ULIST_FILEPATH;
1253
1254     return 0;
1255 }
1256
1257 /* StatEachEntry - If it's not there, it is okay.  If anything else goes wrong
1258  * complain.  Otherwise check permissions: shouldn't allow write or (usually)
1259  * read. */
1260
1261 static int
1262 StatEachEntry(IN struct bozo_bosEntryStats *stats)
1263 {
1264     struct stat info;
1265     if (stat(stats->path, &info)) {
1266         if (errno == ENOENT)
1267             return 1;           /* no such entry: just ignore it */
1268         return 0;               /* something else went wrong */
1269     } else {
1270         int rights;
1271         if (((info.st_mode & S_IFDIR) != 0) != stats->dir)
1272             return 0;           /* not expected type */
1273         if (stats->rootOwner && (info.st_uid != 0))
1274             return 0;           /* not owned by root */
1275         rights = (info.st_mode & 0000777);
1276         if ((rights & stats->reqPerm) != stats->reqPerm)
1277             return 0;           /* required permissions not present */
1278         if ((rights & stats->proPerm) != 0)
1279             return 0;           /* prohibited permissions present */
1280     }
1281     return 1;
1282 }
1283
1284 /* DirAccessOK - checks the mode bits on the AFS dir and decendents and
1285  * returns 0 if some are not OK and 1 otherwise.  For efficiency, it doesn't do
1286  * this check more often than every 5 seconds. */
1287
1288 int
1289 DirAccessOK(void)
1290 {
1291 #ifdef AFS_NT40_ENV
1292     /* underlying filesystem may not support directory protection */
1293     return 1;
1294 #else
1295     static afs_uint32 lastTime = 0;
1296     afs_uint32 now = FT_ApproxTime();
1297     static int lastResult = -1;
1298     int result;
1299     int i;
1300
1301     if ((now - lastTime) < 5)
1302         return lastResult;
1303     lastTime = now;
1304
1305     result = 1;
1306     for (i = 0; i < bozo_nbosEntryStats; i++) {
1307         struct bozo_bosEntryStats *e = &bozo_bosEntryStats[i];
1308         if (!StatEachEntry(e)) {
1309             bozo_Log("unhappy with %s which is a %s that should "
1310                      "have at least rights %o, at most rights %o %s\n",
1311                      e->path, e->dir ? "dir" : "file", e->reqPerm,
1312                      (~e->proPerm & 0777),
1313                      e->rootOwner ? ", owned by root" : "");
1314             result = 0;
1315             break;
1316         }
1317     }
1318
1319     if (result != lastResult) { /* log changes */
1320         bozo_Log("Server directory access is %sokay\n",
1321                  (result ? "" : "not "));
1322     }
1323     lastResult = result;
1324     return lastResult;
1325 #endif /* AFS_NT40_ENV */
1326 }
1327
1328 int
1329 GetRequiredDirPerm(const char *path)
1330 {
1331     int i;
1332     for (i = 0; i < bozo_nbosEntryStats; i++)
1333         if (strcmp(path, bozo_bosEntryStats[i].path) == 0)
1334             return bozo_bosEntryStats[i].reqPerm;
1335     return -1;
1336 }
1337
1338 afs_int32
1339 SBOZO_GetInstanceInfo(IN struct rx_call *acall,
1340                       IN char *ainstance,
1341                       OUT char **atype,
1342                       OUT struct bozo_status *astatus)
1343 {
1344     struct bnode *tb;
1345
1346     tb = bnode_FindInstance(ainstance);
1347     *atype = malloc(BOZO_BSSIZE);
1348     **atype = 0;
1349     if (!tb)
1350         return BZNOENT;
1351     if (tb->type)
1352         strcpy(*atype, tb->type->name);
1353     else
1354         (*atype)[0] = 0;        /* null string */
1355     memset(astatus, 0, sizeof(struct bozo_status));     /* good defaults */
1356     astatus->goal = tb->goal;
1357     astatus->fileGoal = tb->fileGoal;
1358     astatus->procStartTime = tb->procStartTime;
1359     astatus->procStarts = tb->procStarts;
1360     astatus->lastAnyExit = tb->lastAnyExit;
1361     astatus->lastErrorExit = tb->lastErrorExit;
1362     astatus->errorCode = tb->errorCode;
1363     astatus->errorSignal = tb->errorSignal;
1364     if (tb->flags & BNODE_ERRORSTOP)
1365         astatus->flags |= BOZO_ERRORSTOP;
1366     if (bnode_HasCore(tb))
1367         astatus->flags |= BOZO_HASCORE;
1368     if (!DirAccessOK())
1369         astatus->flags |= BOZO_BADDIRACCESS;
1370     return 0;
1371 }
1372
1373 afs_int32
1374 SBOZO_GetInstanceParm(struct rx_call *acall,
1375                       char *ainstance,
1376                       afs_int32 anum,
1377                       char **aparm)
1378 {
1379     struct bnode *tb;
1380     char *tp;
1381     afs_int32 code;
1382
1383     tp = malloc(BOZO_BSSIZE);
1384     *aparm = tp;
1385     *tp = 0;                    /* null-terminate string in error case */
1386     tb = bnode_FindInstance(ainstance);
1387     if (!tb)
1388         return BZNOENT;
1389     bnode_Hold(tb);
1390     if (anum == 999) {
1391         if (tb->notifier) {
1392             memcpy(tp, tb->notifier, strlen(tb->notifier) + 1);
1393             code = 0;
1394         } else
1395             code = BZNOENT;     /* XXXXX */
1396     } else
1397         code = bnode_GetParm(tb, anum, tp, BOZO_BSSIZE);
1398     bnode_Release(tb);
1399
1400     /* Not Currently Audited */
1401     return code;
1402 }
1403
1404 afs_int32
1405 SBOZO_GetLog(struct rx_call *acall, char *aname)
1406 {
1407     afs_int32 code;
1408     FILE *tfile;
1409     int tc;
1410     char *logpath;
1411     char buffer;
1412     char caller[MAXKTCNAMELEN];
1413
1414     /* Check access since 'aname' could specify a file outside of the
1415      * AFS log directory (which is bosserver's working directory).
1416      */
1417     if (!afsconf_SuperUser(bozo_confdir, acall, caller)) {
1418         code = BZACCESS;
1419         goto fail;
1420     }
1421     if (bozo_isrestricted && strchr(aname, '/')
1422         && strcmp(aname, AFSDIR_CANONICAL_SERVER_SLVGLOG_FILEPATH)) {
1423         code = BZACCESS;
1424         goto fail;
1425     }
1426
1427     /* construct local path from canonical (wire-format) path */
1428     if (ConstructLocalLogPath(aname, &logpath)) {
1429         return BZNOENT;
1430     }
1431     if (DoLogging)
1432         bozo_Log("%s is executing GetLog '%s'\n", caller, logpath);
1433     tfile = fopen(logpath, "r");
1434     free(logpath);
1435
1436     if (!tfile) {
1437         return BZNOENT;
1438     }
1439
1440     while (1) {
1441         tc = getc(tfile);
1442         if (tc == 0)
1443             continue;           /* our termination condition on other end */
1444         if (tc == EOF)
1445             break;
1446         buffer = tc;
1447         if (rx_Write(acall, &buffer, 1) != 1) {
1448             fclose(tfile);
1449             code = BZNET;
1450             goto fail;
1451         }
1452     }
1453
1454     /* all done, cleanup and return */
1455     fclose(tfile);
1456
1457     /* write out the end delimeter */
1458     buffer = 0;
1459     if (rx_Write(acall, &buffer, 1) != 1)
1460         return BZNET;
1461     code = 0;
1462
1463   fail:
1464     osi_auditU(acall, BOS_GetLogsEvent, code, AUD_END);
1465     return code;
1466 }
1467
1468 afs_int32
1469 SBOZO_GetInstanceStrings(struct rx_call *acall, char *abnodeName,
1470                          char **as1, char **as2, char **as3, char **as4)
1471 {
1472     struct bnode *tb;
1473
1474     *as2 = malloc(1);
1475     **as2 = 0;
1476     *as3 = malloc(1);
1477     **as3 = 0;
1478     *as4 = malloc(1);
1479     **as4 = 0;
1480     tb = bnode_FindInstance(abnodeName);
1481     if (!tb)
1482         goto fail;
1483
1484     /* now, return the appropriate error string, if any */
1485     if (tb->lastErrorName) {
1486         *as1 = strdup(tb->lastErrorName);
1487     } else {
1488         *as1 = malloc(1);
1489         **as1 = 0;
1490     }
1491     return 0;
1492
1493   fail:
1494     *as1 = malloc(1);
1495     **as1 = 0;
1496     return BZNOENT;
1497 }
1498
1499 afs_int32
1500 SBOZO_GetRestrictedMode(struct rx_call *acall, afs_int32 *arestmode)
1501 {
1502     *arestmode = bozo_isrestricted;
1503     return 0;
1504 }
1505
1506 afs_int32
1507 SBOZO_SetRestrictedMode(struct rx_call *acall, afs_int32 arestmode)
1508 {
1509     afs_int32 code;
1510     char caller[MAXKTCNAMELEN];
1511
1512     if (!afsconf_SuperUser(bozo_confdir, acall, caller)) {
1513         return BZACCESS;
1514     }
1515     if (bozo_isrestricted) {
1516         return BZACCESS;
1517     }
1518     if (arestmode != 0 && arestmode != 1) {
1519         return BZDOM;
1520     }
1521     bozo_isrestricted = arestmode;
1522     code = WriteBozoFile(0);
1523
1524     return code;
1525 }
1526
1527 void *
1528 bozo_ShutdownAndExit(void *param)
1529 {
1530     int asignal = (intptr_t)param;
1531     int code;
1532
1533     bozo_Log
1534         ("Shutdown of BOS server and processes in response to signal %d\n",
1535          asignal);
1536
1537     /* start shutdown of all processes */
1538     if ((code = bnode_ApplyInstance(sdproc, NULL)) == 0) {
1539         /* wait for shutdown to complete */
1540         code = bnode_ApplyInstance(swproc, NULL);
1541     }
1542
1543     if (code) {
1544         bozo_Log("Shutdown incomplete (code = %d); manual cleanup required\n",
1545                  code);
1546     }
1547
1548     rx_Finalize();
1549     exit(code);
1550 }