69c2bc40f1ea8dd6fd6f6239b47543ce0e89e00e
[openafs.git] / src / bozo / bos.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 <afs/stds.h>
12 #include <stdlib.h>
13 #include <stddef.h>
14 #include <sys/types.h>
15 #ifdef AFS_NT40_ENV
16 #include <winsock2.h>
17 #include <io.h>
18 #include <fcntl.h>
19 #else
20 #include <sys/file.h>
21 #include <netinet/in.h>
22 #include <netdb.h>
23 #include <sys/socket.h>
24 #include <strings.h>
25 #endif /* AFS_NT40_ENV */
26 #include <string.h>
27 #include <afs/procmgmt.h>  /* signal(), kill(), wait(), etc. */
28 #include <time.h>
29 #include "bnode.h"
30 #include <errno.h>
31 #include <afs/afsutil.h>
32 #include <afs/cellconfig.h>
33 #include <rx/rx.h>
34 #include <sys/stat.h>
35 #include <rx/xdr.h>
36 #include <afs/auth.h>
37 #include <rx/rxkad.h>
38 #include <afs/cellconfig.h>
39 #include <stdio.h>
40 #include <afs/cmd.h>
41 #include <afs/com_err.h>
42 #include <ubik.h>
43 #include <afs/ktime.h>
44
45
46
47 extern char *volutil_PartitionName();
48 extern struct rx_call *rx_NewCall();
49 extern char *volutil_PartitionName();
50 extern struct hostent *hostutil_GetHostByName();
51
52 static IStatServer();
53 static DoStat();
54
55 #include "bosint.h"
56 #include "../permit_xprt.h"
57
58 /* dummy routine for the audit work.  It should do nothing since audits */
59 /* occur at the server level and bos is not a server. */
60 osi_audit() {return 0;}
61
62 /* keep those lines small */
63 static char *em(acode)
64 afs_int32 acode; {
65     if (acode == -1)
66         return "communications failure (-1)";
67     else if (acode == -3)
68         return "communications timeout (-3)";
69     else return (char *) error_message(acode);
70 }
71
72 /* get partition id from a name */
73 static afs_int32 GetPartitionID(aname)
74 char *aname; {
75     register char tc;
76     char ascii[3];
77
78     tc = *aname;
79     if (tc == 0) return -1;     /* unknown */
80     /* numbers go straight through */
81     if (tc >= '0' && tc <= '9') {
82         return atoi(aname);
83     }
84     /* otherwise check for vicepa or /vicepa, or just plain "a" */
85     ascii[2] = 0;
86     if (strlen(aname) <= 2) {
87         strcpy(ascii, aname);
88     }
89     else if (!strncmp(aname, "/vicep", 6)) {
90         strncpy(ascii, aname+6, 2);
91     }
92     else if (!strncmp(aname, "vicep", 5)) {
93         strncpy(ascii, aname+5, 2);
94     }
95     else return -1;     /* bad partition name */
96     /* now partitions are named /vicepa ... /vicepz, /vicepaa, /vicepab,
97      * .../vicepzz, and are numbered from 0.  Do the appropriate conversion */
98     if (ascii[1] == 0) {
99         /* one char name, 0..25 */
100         if (ascii[0] <  'a' || ascii[0] > 'z')  return -1;  /* wrongo */
101         return ascii[0] - 'a';
102     }
103     else {
104         /* two char name, 26 .. <whatever> */
105         if (ascii[0] <  'a' || ascii[0] > 'z')  return -1;  /* wrongo */
106         if (ascii[1] <  'a' || ascii[1] > 'z')  return -1;  /* just as bad */
107         return (ascii[0] - 'a') * 26 + (ascii[1] - 'a') + 26;
108     }
109 }
110
111 /* make ctime easier to use */
112 static char *DateOf(atime)
113 afs_int32 atime; {
114     static char tbuffer[30];
115     register char *tp;
116     tp=ctime((time_t *)&atime);
117     if (tp) {
118         strcpy(tbuffer, tp);
119         tbuffer[24] = 0;    /* get rid of new line */
120     }
121     else
122         strcpy(tbuffer, "BAD TIME");
123     return tbuffer;
124 }
125
126 /* global stuff from main for communicating with GetConn */
127 static struct rx_securityClass *sc[3];
128 static int scIndex;
129
130 /* use the syntax descr to get a connection, authenticated appropriately.
131  * aencrypt is set if we want to encrypt the data on the wire.
132  */
133 static struct rx_connection *GetConn(as, aencrypt)
134 int aencrypt;
135 struct cmd_syndesc *as; {
136     struct hostent *th;
137     char *hostname;
138     register afs_int32 code;
139     register struct rx_connection *tconn;
140     afs_int32 addr;
141     register struct afsconf_dir *tdir;
142     int encryptLevel;
143     struct ktc_principal sname;
144     struct ktc_token ttoken;
145     int localauth;
146     const char *confdir;
147
148     hostname = as->parms[0].items->data;
149     th = hostutil_GetHostByName(hostname);
150     if (!th) {
151         printf("bos: can't find address for host '%s'\n", hostname);
152         exit(1);
153     }
154     bcopy(th->h_addr, &addr, sizeof(afs_int32));
155
156     /* get tokens for making authenticated connections */
157     localauth = (as->parms[14].items != 0);
158     confdir = (localauth ? AFSDIR_SERVER_ETC_DIRPATH : AFSDIR_CLIENT_ETC_DIRPATH);
159     tdir = afsconf_Open (confdir);
160     if (tdir) {
161         struct afsconf_cell info;
162         char *tname;
163
164         if (as->parms[12].items) tname = as->parms[12].items->data;
165         else tname = (char *) 0;
166         /* next call expands cell name abbrevs for us and handles looking up
167          * local cell */
168         code = afsconf_GetCellInfo(tdir, tname, (char *) 0, &info);
169         if (code) {
170             com_err ("bos", code, "(can't find cell '%s' in cell database)",
171                    (tname? tname : "<default>"));
172             exit(1);
173         }
174         else strcpy(sname.cell, info.name);
175     }
176     else {
177         printf("bos: can't open cell database (%s)\n", confdir);
178         exit(1);
179     }
180     sname.instance[0] = 0;
181     strcpy(sname.name, "afs");
182     sc[0] = (struct rx_securityClass *) rxnull_NewClientSecurityObject();
183     sc[1] = 0;
184     sc[2] = 0;
185     scIndex = 0;
186
187     if (!as->parms[13].items) { /* not -noauth */
188         if (as->parms[14].items) { /* -localauth */
189             code = afsconf_GetLatestKey (tdir, 0,0);
190             if (code) com_err ("bos", code, "(getting key from local KeyFile)");
191             else {
192                 if (aencrypt)
193                     code = afsconf_ClientAuthSecure (tdir, &sc[2], &scIndex);
194                 else
195                     code = afsconf_ClientAuth (tdir, &sc[2], &scIndex);
196                 if (code) com_err ("bos", code, "(calling ClientAuth)");
197                 else if (scIndex != 2)  /* this shouldn't happen */
198                     sc[scIndex] = sc[2];
199             }
200         } else { /* not -localauth, check for tickets */
201             code = ktc_GetToken(&sname, &ttoken, sizeof(ttoken), (char *)0);
202             if (code == 0) {
203                 /* have tickets, will travel */
204                 if (ttoken.kvno >= 0 && ttoken.kvno <= 255) ;
205                 else {
206                     fprintf (stderr,
207                              "bos: funny kvno (%d) in ticket, proceeding\n",
208                              ttoken.kvno);
209                 }
210                 /* kerberos tix */
211                 if (aencrypt) encryptLevel = rxkad_crypt;
212                 else encryptLevel = rxkad_clear;
213                 sc[2] = (struct rx_securityClass *)
214                     rxkad_NewClientSecurityObject
215                         (encryptLevel, &ttoken.sessionKey,
216                          ttoken.kvno, ttoken.ticketLen, ttoken.ticket);
217                 scIndex = 2;
218             } else com_err ("bos", code, "(getting tickets)");
219         }
220         if ((scIndex == 0) || (sc[scIndex] == 0)) {
221             fprintf (stderr, "bos: running unauthenticated\n");
222             scIndex = 0;
223         }
224     }
225     tconn = rx_NewConnection (addr, htons(AFSCONF_NANNYPORT), 1,
226                               sc[scIndex], scIndex);
227     if (!tconn) {
228         fprintf (stderr, "bos: could not create rx connection\n");
229         exit(1);
230     }
231     rxs_Release (sc[scIndex]);
232
233     return tconn;
234 }
235
236 static SetAuth(as)
237 struct cmd_syndesc *as; {
238     register afs_int32 code;
239     register struct rx_connection *tconn;
240     afs_int32 flag;
241     register char *tp;
242     
243     tconn = GetConn(as, 0);
244     tp = as->parms[1].items->data;
245     if (strcmp(tp, "on")==0) flag = 0;  /* auth req.: noauthflag is false */
246     else if (strcmp(tp, "off") == 0) flag = 1;
247     else {
248         printf("bos: illegal authentication specifier '%s', must be 'off' or 'on'.\n", tp);
249         return 1;
250     }
251     code = BOZO_SetNoAuthFlag(tconn, flag);
252     if (code) com_err ("bos", code, "(failed to set authentication flag)");
253     return 0;
254 }
255
256 /* take a name (e.g. foo/bar, and a dir e.g. /usr/afs/bin, and construct
257  * /usr/afs/bin/bar */
258 static ComputeDestDir(aname, adir, aresult, alen)
259 char *aname, *adir, *aresult;
260 afs_int32 alen; {
261     register char *tp;
262
263     strcpy(aresult, adir);
264     tp = rindex(aname, '/');
265     if (!tp) {
266         /* no '/' in name */
267         strcat(aresult, "/");
268         strcat(aresult, aname);
269     }
270     else {
271         /* tp points at the / character */
272         strcat(aresult, tp);
273     }
274     return 0;
275 }
276
277 /* copy data from fd afd to rx call acall */
278 static CopyBytes(afd, acall)
279 int afd;
280 register struct rx_call *acall; {
281     register afs_int32 code;
282     register afs_int32 len;
283     char tbuffer[256];
284
285     while(1) {
286         len = read(afd, tbuffer, sizeof(tbuffer));
287         if (len < 0) return errno;
288         if (len == 0) return 0;    /* all done */
289         code = rx_Write(acall, tbuffer, len);
290         if (code != len) return -1;
291     }
292 }
293
294 static Prune(as)
295 register struct cmd_syndesc *as; {
296     register afs_int32 code;
297     register struct rx_connection *tconn;
298     register afs_int32 flags;
299     
300     tconn = GetConn(as, 0);
301     flags = 0;
302     if (as->parms[1].items) flags |= BOZO_PRUNEBAK;
303     if (as->parms[2].items) flags |= BOZO_PRUNEOLD;
304     if (as->parms[3].items) flags |= BOZO_PRUNECORE;
305     if (as->parms[4].items) flags |= 0xff;
306     code = BOZO_Prune(tconn, flags);
307     if (code) com_err ("bos", code, "(failed to prune server files)");
308     return code;
309 }
310
311 static Exec(as)
312 register struct cmd_syndesc *as; {
313     register struct rx_connection *tconn;
314     register afs_int32 code;
315
316     tconn = GetConn(as, 0);
317     code = BOZO_Exec(tconn, as->parms[1].items->data);
318     if (code) printf("bos: failed to execute command (%s)\n",
319                      em(code));
320     return code;
321 }
322
323 static GetDate(as)
324 register struct cmd_syndesc *as; {
325     register afs_int32 code;
326     char tbuffer[256];
327     char destDir[256];
328     afs_int32 time, bakTime, oldTime;
329     register struct rx_connection *tconn;
330     register struct cmd_item *ti;
331
332     tconn = GetConn(as, 0);
333     if (!as->parms[1].items) {
334         printf("bos: no files to check\n");
335         return 1;
336     }
337
338     /* compute dest dir or file; default MUST be canonical form of dir path */
339     if (as->parms[2].items) strcpy(destDir, as->parms[2].items->data);
340     else strcpy(destDir, AFSDIR_CANONICAL_SERVER_BIN_DIRPATH);
341
342     for(ti=as->parms[1].items; ti; ti=ti->next) {
343         /* check date for each file */
344         ComputeDestDir(ti->data, destDir, tbuffer, sizeof(tbuffer));
345         code = BOZO_GetDates(tconn, tbuffer, &time, &bakTime, &oldTime);
346         if (code) {
347             printf("bos: failed to check date on %s (%s)\n", ti->data,
348                    em(code));
349             return 1;
350         }
351         else {
352             printf("File %s ", tbuffer);
353             if (time == 0) printf("does not exist, ");
354             else printf("dated %s, ", DateOf(time));
355             if (bakTime == 0) printf("no .BAK file, ");
356             else printf(".BAK file dated %s, ", DateOf(bakTime));
357             if (oldTime == 0) printf("no .OLD file.");
358             else printf(".OLD file dated %s.", DateOf(oldTime));
359             printf("\n");
360         }
361     }
362 }
363
364 static UnInstall(as)
365 register struct cmd_syndesc *as; {
366     register afs_int32 code;
367     char tbuffer[256];
368     char destDir[256];
369     register struct cmd_item *ti;
370     register struct rx_connection *tconn;
371
372     tconn = GetConn(as, 0);
373     if (!as->parms[1].items) {
374         printf("bos: no files to uninstall\n");
375         return 1;
376     }
377
378     /* compute dest dir or file; default MUST be canonical form of dir path */
379     if (as->parms[2].items) strcpy(destDir, as->parms[2].items->data);
380     else strcpy(destDir, AFSDIR_CANONICAL_SERVER_BIN_DIRPATH);
381
382     for(ti=as->parms[1].items; ti; ti=ti->next) {
383         /* uninstall each file */
384         ComputeDestDir(ti->data, destDir, tbuffer, sizeof(tbuffer));
385         code = BOZO_UnInstall(tconn, tbuffer);
386         if (code) {
387             printf("bos: failed to uninstall %s (%s)\n", ti->data,
388                    em(code));
389             return 1;
390         }
391         else printf("bos: uninstalled file %s\n", ti->data);
392     }
393     return 0;
394 }
395
396 static afs_int32 GetServerGoal(aconn, aname)
397 char *aname;
398 struct rx_connection *aconn; {
399     char buffer[500];
400     char *tp;
401     register afs_int32 code;
402     struct bozo_status istatus;
403
404     tp = buffer;
405     code = BOZO_GetInstanceInfo(aconn, aname, &tp, &istatus);
406     if (code) {
407         printf("bos: failed to get instance info for '%s' (%s)\n",
408                aname, em(code));
409         /* if we can't get the answer, assume its running */
410         return BSTAT_NORMAL; 
411     }
412     if (istatus.goal == 0)
413         return BSTAT_SHUTDOWN;
414     else
415         return BSTAT_NORMAL;
416 }
417
418 static Install(as)
419 struct cmd_syndesc *as; {
420     struct rx_connection *tconn;
421     register afs_int32 code;
422     register struct cmd_item *ti;
423     struct stat tstat;
424     char tbuffer[256];
425     int fd;
426     struct rx_call *tcall;
427     char destDir[256];
428
429     tconn = GetConn(as, 0);
430     if (!as->parms[1].items) {
431         printf("bos: no files to install\n");
432         return 1;
433     }
434
435     /* compute dest dir or file; default MUST be canonical form of dir path */
436     if (as->parms[2].items) strcpy(destDir, as->parms[2].items->data);
437     else strcpy(destDir, AFSDIR_CANONICAL_SERVER_BIN_DIRPATH);
438
439     for(ti=as->parms[1].items; ti; ti=ti->next) {
440         /* install each file */
441         fd = open(ti->data, O_RDONLY);
442         if (fd < 0) {
443             /* better to quit on error than continue for install command */
444             printf("bos: can't find file '%s', quitting\n", ti->data);
445             return 1;
446         }
447         code = fstat(fd, &tstat);
448         if (code) {
449             printf("bos: failed to stat file %s, errno is %d\n", ti->data,
450                    errno);
451             return 1;
452         }
453         /* compute destination dir */
454         ComputeDestDir(ti->data, destDir, tbuffer, sizeof(tbuffer));
455         tcall = rx_NewCall(tconn);
456         code = StartBOZO_Install(tcall, tbuffer, tstat.st_size, (afs_int32)tstat.st_mode,
457                                  tstat.st_mtime);
458         if (code == 0) {
459             code = CopyBytes(fd, tcall);
460         }
461         code = rx_EndCall(tcall, code);
462         if (code) {
463             printf("bos: failed to install %s (%s)\n", ti->data, em(code));
464             return 1;
465         }
466         else printf("bos: installed file %s\n", ti->data);
467     }
468     return 0;
469 }
470
471 static Shutdown(as)
472 struct cmd_syndesc *as; {
473     register struct rx_connection *tconn;
474     register afs_int32 code;
475     register struct cmd_item *ti;
476     
477     tconn = GetConn(as, 0);
478     if (as->parms[1].items == 0) {
479         code = BOZO_ShutdownAll(tconn);
480         if (code) printf("bos: failed to shutdown servers (%s)\n", em(code));
481     }
482     else {
483         for(ti = as->parms[1].items; ti; ti=ti->next) {
484             code = BOZO_SetTStatus(tconn, ti->data, BSTAT_SHUTDOWN);
485             if (code)
486                 printf("bos: failed to shutdown instance %s (%s)\n",
487                        ti->data, em(code));
488         }
489     }
490     if (as->parms[8].items) {
491         code = BOZO_WaitAll(tconn);
492         if (code)
493             printf("bos: can't wait for processes to shutdown (%s)\n",
494                    em(code));
495     }
496     return 0;
497 }
498
499 static GetRestartCmd(as, arock)
500 struct cmd_syndesc *as;
501 char *arock; {
502     register afs_int32 code;
503     struct ktime generalTime, newBinaryTime;
504     char messageBuffer[256];
505     struct rx_connection *tconn;
506     char *hostp;
507
508     hostp = as->parms[0].items->data;   /* host name for messages */
509     tconn = GetConn(as, 0);
510
511     code = BOZO_GetRestartTime(tconn, 1, &generalTime);
512     if (code) {
513         printf("bos: failed to retrieve restart information (%s)\n", em(code));
514         return code;
515     }
516     code = BOZO_GetRestartTime(tconn, 2, &newBinaryTime);
517     if (code) {
518         printf("bos: failed to retrieve restart information (%s)\n", em(code));
519         return code;
520     }
521
522     code = ktime_DisplayString(&generalTime, messageBuffer);
523     if (code) {
524         printf("bos: failed to decode restart time (%s)\n", em(code));
525         return code;
526     }
527     printf("Server %s restarts %s\n", hostp, messageBuffer);
528
529     code = ktime_DisplayString(&newBinaryTime, messageBuffer);
530     if (code) {
531         printf("bos: failed to decode restart time (%s)\n", em(code));
532         return code;
533     }
534     printf("Server %s restarts for new binaries %s\n", hostp, messageBuffer);
535
536     /* all done now */
537     return 0;
538 }
539
540 static SetRestartCmd(as, arock)
541 struct cmd_syndesc *as;
542 char *arock; {
543     afs_int32 count;
544     register afs_int32 code;
545     struct ktime restartTime;
546     afs_int32 type;
547     struct rx_connection *tconn;
548
549     count = 0;
550     tconn = GetConn(as, 0);
551     if (as->parms[2].items) {count++; type = 1;}
552     if (as->parms[3].items) {count++; type = 2;}
553     if (count > 1) {
554         printf("bos: can't specify more than one restart time at a time\n");
555         return -1;
556     }
557     if (count == 0) type = 1;   /* by default set general restart time */
558
559     if (code = ktime_ParsePeriodic(as->parms[1].items->data, &restartTime)) {
560         printf("bos: failed to parse '%s' as periodic restart time(%s)\n",
561                as->parms[1].items->data, em(code));
562         return code;
563     }
564
565     code = BOZO_SetRestartTime(tconn, type, &restartTime);
566     if (code) {
567         printf("bos: failed to set restart time at server (%s)\n", em(code));
568         return code;
569     }
570     return 0;
571 }
572
573 static Startup(as)
574 struct cmd_syndesc *as; {
575     register struct rx_connection *tconn;
576     register afs_int32 code;
577     register struct cmd_item *ti;
578     
579     tconn = GetConn(as, 0);
580     if (as->parms[1].items == 0) {
581         code = BOZO_StartupAll(tconn);
582         if (code)
583             printf("bos: failed to startup servers (%s)\n", em(code));
584     }
585     else {
586         for(ti = as->parms[1].items; ti; ti=ti->next) {
587             code = BOZO_SetTStatus(tconn, ti->data, BSTAT_NORMAL);
588             if (code)
589                 printf("bos: failed to start instance %s (%s)\n",
590                        ti->data, em(code));
591         }
592     }
593     return 0;
594 }
595
596 static Restart(as)
597 struct cmd_syndesc *as; {
598     register struct rx_connection *tconn;
599     register afs_int32 code;
600     register struct cmd_item *ti;
601     
602     tconn = GetConn(as, 0);
603     if (as->parms[2].items) {
604         /* this is really a rebozo command */
605         if (as->parms[1].items) {
606             /* specified specific things to restart, can't do this at the same
607              * time */
608             printf("bos: can't specify both '-bos' and specific servers to restart.\n");
609             return 1;
610         }
611         /* otherwise do a rebozo */
612         code = BOZO_ReBozo(tconn);
613         if (code)
614             printf("bos: failed to restart bosserver (%s)\n", em(code));
615         return code;
616     }
617     if (as->parms[1].items == 0) {
618         if (as->parms[3].items) {               /* '-all' */
619             code = BOZO_RestartAll(tconn);
620             if (code) printf("bos: failed to restart servers (%s)\n", em(code));
621         } else
622             printf("bos: To restart all processes please specify '-all'\n");
623     }
624     else {
625         if (as->parms[3].items) {
626             printf("bos: Can't use '-all' along with individual instances\n");
627         } else {
628             for(ti = as->parms[1].items; ti; ti=ti->next) {
629                 code = BOZO_Restart(tconn, ti->data);
630                 if (code)
631                     printf("bos: failed to restart instance %s (%s)\n", ti->data,
632                            em(code));
633             }
634         }
635     }
636     return 0;
637 }
638
639 static SetCellName(as)
640 struct cmd_syndesc *as; {
641     register struct rx_connection *tconn;
642     register afs_int32 code;
643     
644     tconn = GetConn(as, 0);
645     code = BOZO_SetCellName(tconn, as->parms[1].items->data);
646     if (code) printf("bos: failed to set cell (%s)\n", em(code));
647     return 0;
648 }
649
650 static AddHost(as)
651 register struct cmd_syndesc *as; {
652     register struct rx_connection *tconn;
653     register afs_int32 code;
654     register struct cmd_item *ti;
655     
656     tconn = GetConn(as, 0);
657     for(ti = as->parms[1].items; ti; ti=ti->next) {
658         code = BOZO_AddCellHost(tconn, ti->data);
659         if (code)
660             printf("bos: failed to add host %s (%s)\n", ti->data, em(code));
661     }
662     return 0;
663 }
664
665 static RemoveHost(as)
666 register struct cmd_syndesc *as; {
667     register struct rx_connection *tconn;
668     register afs_int32 code;
669     register struct cmd_item *ti;
670     
671     tconn = GetConn(as, 0);
672     for(ti=as->parms[1].items; ti; ti=ti->next) {
673         code = BOZO_DeleteCellHost(tconn, ti->data);
674         if (code)
675             printf("bos: failed to delete host %s (%s)\n", ti->data, em(code));
676     }
677     return 0;
678 }
679
680 static ListHosts(as)
681 register struct cmd_syndesc *as; {
682     register struct rx_connection *tconn;
683     register afs_int32 code;
684     char tbuffer[256];
685     char *tp;
686     register afs_int32 i;
687
688     tp = tbuffer;
689     tconn = GetConn(as, 0);
690     code = BOZO_GetCellName(tconn, &tp);
691     if (code) {
692         printf("bos: failed to get cell name (%s)\n", em(code));
693         exit(1);
694     }
695     printf("Cell name is %s\n", tbuffer);
696     for(i=0;;i++) {
697         code = BOZO_GetCellHost(tconn, i, &tp);
698         if (code == BZDOM) break;
699         if (code != 0) {
700             printf("bos: failed to get cell host %d (%s)\n", i, em(code));
701             exit(1);
702         }
703         printf("    Host %d is %s\n", i+1, tbuffer);
704     }
705     return 0;
706 }
707
708 static AddKey(as)
709 register struct cmd_syndesc *as; {
710     register struct rx_connection *tconn;
711     register afs_int32 code;
712     struct ktc_encryptionKey tkey;
713     afs_int32 temp;
714     char *tcell;
715     char cellBuffer[256];
716     char buf[BUFSIZ], ver[BUFSIZ];
717     
718     tconn = GetConn(as, 1);
719     bzero(&tkey, sizeof(struct ktc_encryptionKey));
720
721         if(as->parms[1].items)
722                 strcpy(buf,as->parms[1].items->data);
723         else
724         {
725                 /* prompt for key */
726                 code=des_read_pw_string(buf,sizeof(buf),"input key: ",0);
727                 if(code || strlen(buf)==0)
728                 {
729                         fprintf(stderr,"Bad key: \n");
730                         exit(1);
731                 }
732                 code=des_read_pw_string(ver,sizeof(ver),"Retype input key: ",0);
733                 if(code || strlen(ver)==0)
734                 {
735                         fprintf(stderr,"Bad key: \n");
736                         exit(1);
737                 }
738                 if (strcmp (ver, buf) != 0) {
739                     fprintf (stderr, "\nInput key mismatch\n");
740                     exit(1);
741                 }
742
743         }
744
745     temp = atoi(as->parms[2].items->data);
746     if (temp == 999) {
747         /* bcrypt key */
748 /*
749         strcpy((char *)&tkey, as->parms[1].items->data);
750 */
751         strcpy((char *)&tkey, buf);
752     }
753     else {    /* kerberos key */
754         if (as->parms[12].items) {
755             strcpy(cellBuffer, as->parms[12].items->data);
756
757             /* string to key needs upper-case cell names */
758
759             /* I don't believe this is true.  The string to key function
760              * actually expands the cell name, then LOWER-CASES it.  Perhaps it
761              * didn't use to??? */
762             ucstring(cellBuffer, cellBuffer, strlen(cellBuffer));
763             tcell = cellBuffer;
764         }
765         else tcell = (char *) 0;    /* no cell specified, use current */
766 /*
767         ka_StringToKey(as->parms[1].items->data, tcell, &tkey);
768 */
769         ka_StringToKey(buf,tcell,&tkey);
770     }
771     tconn = GetConn(as, 1);
772     code = BOZO_AddKey(tconn, temp, &tkey);
773     if (code) {
774         printf("bos: failed to set key %d (%s)\n", temp, em(code));
775         exit(1);
776     }
777     return 0;
778 }
779
780 static RemoveKey(as)
781 register struct cmd_syndesc *as; {
782     register struct rx_connection *tconn;
783     register afs_int32 code;
784     afs_int32 temp;
785     register struct cmd_item *ti;
786     
787     tconn = GetConn(as, 0);
788     for(ti=as->parms[1].items; ti; ti=ti->next) {
789         temp = atoi(ti->data);
790         code = BOZO_DeleteKey(tconn, temp);
791         if (code) {
792             printf("bos: failed to delete key %d (%s)\n", temp, em(code));
793             exit(1);
794         }
795     }
796     return 0;
797 }
798
799 static ListKeys(as)
800   IN register struct cmd_syndesc *as;
801 {
802     register struct rx_connection *tconn;
803     register afs_int32 code;
804     struct ktc_encryptionKey tkey;
805     afs_int32 kvno;
806     struct bozo_keyInfo keyInfo;
807     int everWorked;
808     register afs_int32 i;
809     
810     tconn = GetConn(as, 1);
811     everWorked = 0;
812     for(i=0;;i++) {
813         code = BOZO_ListKeys(tconn, i, &kvno, &tkey, &keyInfo);
814         if (code) break;
815         everWorked = 1;
816         /* first check if key is returned */
817         if ((!ka_KeyIsZero (&tkey, sizeof(tkey))) && (as->parms[1].items)) {
818             printf ("key %d is '", kvno);
819             ka_PrintBytes (&tkey, sizeof(tkey));
820             printf ("'\n");
821         } else {
822             if (keyInfo.keyCheckSum == 0) /* shouldn't happen */
823                 printf ("key version is %d\n", kvno);
824             else
825                 printf ("key %d has cksum %u\n", kvno, keyInfo.keyCheckSum);
826         }
827     }
828     if (everWorked) {
829         printf("Keys last changed on %s.\n", DateOf(keyInfo.mod_sec));
830     }
831     if (code != BZDOM)
832         printf("bos: %s error encountered while listing keys\n", em(code));
833     else
834         printf("All done.\n");
835     return 0;
836 }
837
838 static AddSUser(as)
839 register struct cmd_syndesc *as; {
840     register struct rx_connection *tconn;
841     register afs_int32 code;
842     int failed;
843     register struct cmd_item *ti;
844
845     failed = 0;
846     tconn = GetConn(as, 0);
847     for(ti=as->parms[1].items; ti; ti=ti->next) {
848         code = BOZO_AddSUser(tconn, ti->data);
849         if (code) {
850             printf("bos: failed to add user '%s' (%s)\n", ti->data, em(code));
851             failed = 1;
852         }
853     }
854     return failed;
855 }
856
857 static RemoveSUser(as)
858 register struct cmd_syndesc *as; {
859     register struct rx_connection *tconn;
860     register struct cmd_item *ti;
861     register afs_int32 code;
862     int failed;
863
864     failed = 0;
865     tconn = GetConn(as, 0);
866     for(ti=as->parms[1].items; ti; ti=ti->next) {
867         code = BOZO_DeleteSUser(tconn, ti->data);
868         if (code) {
869             printf("bos: failed to delete user '%s', ", ti->data);
870             if (code == ENOENT)
871                 printf("(no such user)\n");
872             else
873                 printf("(%s)\n", em(code));
874             failed = 1;
875         }
876     }
877     return failed;
878 }
879
880 #define NPERLINE    10      /* dudes to print per line */
881 static ListSUsers(as)
882 register struct cmd_syndesc *as; {
883     register struct rx_connection *tconn;
884     register int i;
885     register afs_int32 code;
886     char tbuffer[256];
887     char *tp;
888     int lastNL, printGreeting;
889     
890     tconn = GetConn(as, 0);
891     lastNL = 0;
892     printGreeting = 1;
893     for(i=0;;i++) {
894         tp = tbuffer;
895         code = BOZO_ListSUsers(tconn, i, &tp);
896         if (code) break;
897         if (printGreeting) {
898             printGreeting = 0;  /* delay until after first call succeeds */
899             printf("SUsers are: ");
900         }
901         printf("%s ", tbuffer);
902         if ((i % NPERLINE) == NPERLINE-1) {
903             printf("\n");
904             lastNL = 1;
905         }
906         else lastNL = 0;
907     }
908     if (code != 1) {
909         /* a real error code, instead of scanned past end */
910         printf("bos: failed to retrieve super-user list (%s)\n", em(code));
911         return code;
912     }
913     if (lastNL == 0) printf("\n");
914     return 0;
915 }
916
917 static StatServer(as)
918 register struct cmd_syndesc *as; {
919     register struct rx_connection *tconn;
920     register afs_int32 code;
921     register int i;
922     char ibuffer[BOZO_BSSIZE];
923     char *tp;
924     int int32p;
925     
926     /* int32p==1 is obsolete, smaller, printout */
927     int32p = (as->parms[2].items != 0? 2 : 0);
928
929     /* no parms does something pretty different */
930     if (as->parms[1].items) return IStatServer(as, int32p);
931
932     tconn = GetConn(as, 0);
933     for(i=0;;i++) {
934         /* for each instance */
935         tp = ibuffer;
936         code = BOZO_EnumerateInstance(tconn, i, &tp);
937         if (code == BZDOM) break;
938         if (code) {
939             printf("bos: failed to contact host's bosserver (%s).\n",
940                    em(code));
941             break;
942         }
943         DoStat(ibuffer, tconn, int32p, (i==0)); /* print status line */
944     }
945     return 0;
946 }
947
948 static CreateServer(as)
949 register struct cmd_syndesc *as; {
950     register struct rx_connection *tconn;
951     register afs_int32 code;
952     char *parms[6];
953     register struct cmd_item *ti;
954     register int i;
955     char *type, *name, *notifier=NONOTIFIER;
956
957     tconn = GetConn(as, 0);
958     for(i=0;i<6;i++) parms[i] = "";
959     for(i=0,ti=as->parms[3].items; (ti && i<6); ti=ti->next,i++) {
960         parms[i] = ti->data;
961     }
962     name = as->parms[1].items->data;
963     type = as->parms[2].items->data;
964     if (ti = as->parms[4].items) {
965         notifier = ti->data;
966     }
967     code = BOZO_CreateBnode(tconn, type, name, parms[0], parms[1], parms[2],
968                             parms[3], parms[4], notifier);
969     if (code) {
970         printf("bos: failed to create new server instance %s of type '%s' (%s)\n", 
971                name, type, em(code));
972     }
973     return code;
974 }
975
976 static DeleteServer(as)
977 register struct cmd_syndesc *as; {
978     register struct rx_connection *tconn;
979     register afs_int32 code;
980     register struct cmd_item *ti;
981
982     code = 0;
983     tconn = GetConn(as, 0);
984     for(ti=as->parms[1].items; ti; ti=ti->next) {
985         code = BOZO_DeleteBnode(tconn, ti->data);
986         if (code) {
987             if (code == BZBUSY)
988                 printf("bos: can't delete running instance '%s'\n", ti->data);
989             else
990                 printf("bos: failed to delete instance '%s' (%s)\n",
991                        ti->data, em(code));
992         }
993     }
994     return code;
995 }
996
997 static StartServer(as)
998 register struct cmd_syndesc *as; {
999     register struct rx_connection *tconn;
1000     register afs_int32 code;
1001     register struct cmd_item *ti;
1002
1003     code = 0;
1004     tconn = GetConn(as, 0);
1005     for(ti=as->parms[1].items; ti; ti=ti->next) {
1006         code = BOZO_SetStatus(tconn, ti->data, BSTAT_NORMAL);
1007         if (code)
1008             printf("bos: failed to start instance '%s' (%s)\n",
1009                    ti->data, em(code));
1010     }
1011     return code;
1012 }
1013
1014 static StopServer(as)
1015 register struct cmd_syndesc *as; {
1016     register struct rx_connection *tconn;
1017     register afs_int32 code;
1018     register struct cmd_item *ti;
1019
1020     code = 0;
1021     tconn = GetConn(as, 0);
1022     for(ti=as->parms[1].items; ti; ti=ti->next) {
1023         code = BOZO_SetStatus(tconn, ti->data, BSTAT_SHUTDOWN);
1024         if (code)
1025             printf("bos: failed to change stop instance '%s' (%s)\n",
1026                    ti->data, em(code));
1027     }
1028     if (as->parms[8].items) {
1029         code = BOZO_WaitAll(tconn);
1030         if (code)
1031             printf("bos: can't wait for processes to shutdown (%s)\n",
1032                    em(code));
1033     }
1034     return code;
1035 }
1036
1037 static DoSalvage(aconn, aparm1, aparm2, aoutName, showlog,parallel,atmpDir,orphans)
1038 struct rx_connection *aconn;
1039 char *aoutName;
1040 char *aparm1;
1041 char *aparm2; 
1042 afs_int32 showlog;
1043 char *parallel;
1044 char *atmpDir;
1045 char *orphans;
1046 {
1047     register afs_int32 code;
1048     char *parms[6];
1049     char buffer;
1050     char tbuffer[BOZO_BSSIZE];
1051     struct bozo_status istatus;
1052     struct rx_call *tcall;
1053     char *tp;
1054     FILE *outFile;
1055     int closeIt;
1056     char partName[20];  /* canonical name for partition */
1057     afs_int32 partNumber;
1058     char *notifier = NONOTIFIER;
1059
1060     /* if a partition was specified, canonicalize the name, since
1061        the salvager has a stupid partition ID parser */
1062     if (aparm1) {
1063         partNumber = volutil_GetPartitionID(aparm1);
1064         if (partNumber < 0) {
1065             printf("bos: could not parse partition ID '%s'\n", aparm1);
1066             return EINVAL;
1067         }
1068         tp = volutil_PartitionName(partNumber);
1069         if (!tp) {
1070             printf("bos: internal error parsing partition ID '%s'\n", aparm1);
1071             return EINVAL;
1072         }
1073         strcpy(partName, tp);
1074     }
1075     else partName[0] = 0;
1076
1077     /* open the file name */
1078     if (aoutName) {
1079         outFile = fopen(aoutName, "w");
1080         if (!outFile) {
1081             printf("bos: can't open specified SalvageLog file '%s'\n",
1082                    aoutName);
1083             return ENOENT;
1084         }
1085         closeIt = 1;    /* close this file later */
1086     }
1087     else {
1088         outFile = stdout;
1089         closeIt = 0;    /* don't close this file later */
1090     }
1091
1092     for(code=2;code<6;code++) parms[code] = "";
1093     if (!aparm2) aparm2 = "";
1094     /* MUST pass canonical (wire-format) salvager path to bosserver */
1095     strncpy(tbuffer, AFSDIR_CANONICAL_SERVER_SALVAGER_FILEPATH, BOZO_BSSIZE);
1096     if (*aparm2 != 0) {
1097         if ( (strlen(tbuffer) + 1 + strlen(partName) + 1 + strlen(aparm2) + 1) > BOZO_BSSIZE ) {
1098            printf("bos: command line too big\n");
1099            return(E2BIG);
1100         }
1101         strcat(tbuffer, " ");
1102         strcat(tbuffer, partName);
1103         strcat(tbuffer, " ");
1104         strcat(tbuffer, aparm2);
1105     } else {
1106         if ( (strlen(tbuffer) + 4 + strlen(partName) + 1) > BOZO_BSSIZE ) {
1107            printf("bos: command line too big\n");
1108            return(E2BIG);
1109         }
1110         strcat (tbuffer, " -f ");
1111         strcat (tbuffer, partName);
1112     }
1113
1114     /* add the parallel option if given */
1115     if (parallel != (char *)0) {
1116         if ( (strlen(tbuffer) + 11 + strlen(parallel) + 1) > BOZO_BSSIZE ) {
1117            printf("bos: command line too big\n");
1118            return(E2BIG);
1119         }
1120         strcat(tbuffer, " -parallel ");
1121         strcat(tbuffer, parallel);
1122     }
1123
1124     /* add the tmpdir option if given */
1125     if (atmpDir != (char *)0) {
1126         if ( (strlen(tbuffer) + 9 + strlen(atmpDir) + 1) > BOZO_BSSIZE ) {
1127            printf("bos: command line too big\n");
1128            return(E2BIG);
1129         }
1130         strcat(tbuffer, " -tmpdir ");
1131         strcat(tbuffer, atmpDir);
1132     }
1133
1134     /* add the orphans option if given */
1135     if (orphans != (char *)0) {
1136         if ( (strlen(tbuffer) + 10 + strlen(orphans) + 1) > BOZO_BSSIZE ) {
1137            printf("bos: command line too big\n");
1138            return(E2BIG);
1139         }
1140         strcat(tbuffer, " -orphans ");
1141         strcat(tbuffer, orphans);
1142     }
1143
1144     parms[0] = tbuffer;
1145     parms[1] = "now";       /* when to do it */
1146     code = BOZO_CreateBnode(aconn, "cron", "salvage-tmp", parms[0], parms[1],
1147                             parms[2], parms[3], parms[4], notifier);
1148     if (code) goto done;
1149     /* now wait for bnode to disappear */
1150     while (1) {
1151         IOMGR_Sleep(5);
1152         tp = tbuffer;
1153         code = BOZO_GetInstanceInfo(aconn, "salvage-tmp", &tp, &istatus);
1154         if (code) break;
1155         printf("bos: waiting for salvage to complete.\n");
1156     }
1157     if (code != BZNOENT) {
1158         printf("bos: salvage failed (%s)\n", em(code));
1159         goto done;
1160     }
1161
1162     /* now print the log file to the output file */
1163     printf("bos: salvage completed\n");
1164     if (aoutName || showlog) {
1165         fprintf(outFile, "SalvageLog:\n");
1166         tcall = rx_NewCall(aconn);
1167         /* MUST pass canonical (wire-format) salvager log path to bosserver */
1168         code = StartBOZO_GetLog(tcall, AFSDIR_CANONICAL_SERVER_SLVGLOG_FILEPATH);
1169         if (code) {
1170             rx_EndCall(tcall, code);
1171             goto done;
1172         }
1173         /* copy data */
1174         while(1) {
1175             code = rx_Read(tcall, &buffer, 1);
1176             if (code != 1) break;
1177             putc(buffer, outFile);
1178             if (buffer == 0) break;     /* the end delimeter */
1179         }
1180         code = rx_EndCall(tcall, 0);
1181         /* fall through into cleanup code */
1182     }
1183
1184   done:
1185     if (closeIt && outFile) fclose(outFile);
1186     return code;
1187 }
1188
1189 static GetLogCmd(as)
1190 register struct cmd_syndesc *as; {
1191     struct rx_connection *tconn;
1192     register struct rx_call *tcall;
1193     register afs_int32 code;
1194     char buffer;
1195     int error;
1196
1197     printf("Fetching log file '%s'...\n", as->parms[1].items->data);
1198     tconn = GetConn(as, 0);
1199     tcall = rx_NewCall(tconn);
1200     code = StartBOZO_GetLog(tcall, as->parms[1].items->data);
1201     if (code) {
1202         rx_EndCall(tcall, code);
1203         goto done;
1204     }
1205     /* copy data */
1206     error = 0;
1207     while(1) {
1208         code = rx_Read(tcall, &buffer, 1);
1209         if (code != 1) {
1210             error = EIO;
1211             break;
1212         }
1213         if (buffer == 0) break; /* the end delimeter */
1214         putchar(buffer);
1215     }
1216     code = rx_EndCall(tcall, error);
1217     /* fall through into cleanup code */
1218
1219   done:
1220     if (code)
1221         com_err("bos", code, "(while reading log)");
1222     return code;
1223 }
1224
1225 static SalvageCmd(as)
1226 struct cmd_syndesc *as; {
1227     register struct rx_connection *tconn;
1228     register afs_int32 code, rc;
1229     char *outName;
1230     char tname[65];
1231     afs_int32 newID;
1232     extern struct ubik_client *cstruct;
1233     afs_int32 curGoal, showlog = 0;
1234     char *parallel;
1235     char *tmpDir;
1236     char *orphans;
1237     
1238     /* parm 0 is machine name, 1 is partition, 2 is volume, 3 is -all flag */
1239     tconn = GetConn(as, 0);
1240
1241     /* we can do a volume, a partition or the whole thing, but not mixtures
1242      * thereof */
1243     if (!as->parms[1].items && as->parms[2].items) {
1244         printf("bos: must specify partition to salvage individual volume.\n");
1245         return -1;
1246     }
1247     if (as->parms[5].items && as->parms[3].items) {
1248         printf("bos: can not specify both -file and -showlog.\n");
1249         return -1;
1250     }
1251     if (as->parms[4].items && (as->parms[1].items || as->parms[2].items)) {
1252         printf("bos: can not specify -all with other flags.\n");
1253         return -1;
1254     }
1255
1256     /* get the output file name, if any */
1257     if (as->parms[3].items)
1258         outName = as->parms[3].items->data;
1259     else
1260         outName = (char *) 0;
1261
1262     if (as->parms[5].items)
1263         showlog = 1;
1264
1265     /* parallel option */
1266     parallel = (char *) 0;
1267     if (as->parms[6].items)
1268         parallel = as->parms[6].items->data;
1269
1270     /* get the tmpdir filename if any */
1271     tmpDir = (char *)0;
1272     if (as->parms[7].items)
1273         tmpDir = as->parms[7].items->data;
1274
1275     /* -orphans option */
1276     orphans = (char *)0;
1277     if (as->parms[8].items)
1278         orphans = as->parms[8].items->data;
1279
1280     if (as->parms[4].items) {
1281         /* salvage whole enchilada */
1282         curGoal = GetServerGoal(tconn, "fs");
1283         if (curGoal == BSTAT_NORMAL) {
1284             printf("bos: shutting down fs.\n");
1285             code = BOZO_SetTStatus(tconn, "fs", BSTAT_SHUTDOWN);
1286             if (code) {
1287                 printf("bos: failed to stop 'fs' (%s)\n", em(code));
1288                 return code;
1289             }
1290             code = BOZO_WaitAll(tconn); /* wait for shutdown to complete */
1291             if (code)
1292                 printf("bos: failed to wait for file server shutdown, continuing.\n");
1293         }
1294         /* now do the salvage operation */
1295         printf("Starting salvage.\n");
1296         rc = DoSalvage(tconn, (char *) 0, (char *) 0, outName, showlog,parallel,tmpDir,orphans);
1297         if (rc) printf("bos: failed to start 'salvager' (%s)\n", em(rc));
1298         if (curGoal == BSTAT_NORMAL) {
1299             printf("bos: restarting fs.\n");
1300             code = BOZO_SetTStatus(tconn, "fs", BSTAT_NORMAL);
1301             if (code) {
1302                 printf("bos: failed to restart 'fs' (%s)\n", em(code));
1303                 return code;
1304             }
1305         }
1306         if (rc) return rc;
1307     }
1308     else if (!as->parms[2].items) {
1309         if (!as->parms[1].items) {
1310             printf("bos: must specify -all switch to salvage all partitions.\n");
1311             return -1;
1312         }
1313         if (volutil_GetPartitionID(as->parms[1].items->data) < 0) {
1314             /* can't parse volume ID, so complain before shutting down
1315              * file server.
1316              */
1317             printf("bos: can't interpret %s as partition ID.\n",
1318                    as->parms[1].items->data);
1319             return -1;
1320         }
1321         curGoal = GetServerGoal(tconn, "fs");
1322         /* salvage a whole partition (specified by parms[1]) */
1323         if (curGoal == BSTAT_NORMAL) {
1324             printf("bos: shutting down fs.\n");
1325             code = BOZO_SetTStatus(tconn, "fs", BSTAT_SHUTDOWN);
1326             if (code) {
1327                 printf("bos: can't stop 'fs' (%s)\n", em(code));
1328                 return code;
1329             }
1330             code = BOZO_WaitAll(tconn); /* wait for shutdown to complete */
1331             if (code)
1332                 printf("bos: failed to wait for file server shutdown, continuing.\n");
1333         }
1334         /* now do the salvage operation */
1335         printf("Starting salvage.\n");
1336         rc = DoSalvage(tconn, as->parms[1].items->data, (char *) 0,
1337                        outName, showlog,parallel,tmpDir,orphans);
1338         if (rc) printf("bos: failed to start 'salvager' (%s)\n", em(rc));
1339         if (curGoal == BSTAT_NORMAL) {
1340             printf("bos: restarting fs.\n");
1341             code = BOZO_SetTStatus(tconn, "fs", BSTAT_NORMAL);
1342             if (code) {
1343                 printf("bos: failed to restart 'fs' (%s)\n", em(code));
1344                 return code;
1345             }
1346         }
1347         if (rc) return rc;
1348     }
1349     else {
1350         /* salvage individual volume (don't shutdown fs first), just use
1351          * single-shot cron bnode.  Must leave server running when using this
1352          * option, since salvager will ask file server for the volume */
1353         char *tmpname;
1354         afs_int32 err;
1355         const char *confdir;
1356         int localauth;
1357
1358         if (as->parms[12].items) tmpname = as->parms[12].items->data;
1359         else tmpname = (char *) 0;
1360
1361         localauth = (as->parms[14].items != 0);
1362         confdir = (localauth ? AFSDIR_SERVER_ETC_DIRPATH : AFSDIR_CLIENT_ETC_DIRPATH);
1363         code = vsu_ClientInit(/* noauth */ 1, confdir, tmpname,
1364                               /* server auth */ 0, &cstruct, (int (*)()) 0);
1365         if (code == 0) {
1366             newID = vsu_GetVolumeID(as->parms[2].items->data, cstruct, &err);
1367             if (newID == 0) {
1368                 printf("bos: can't interpret %s as volume name or ID\n", as->parms[2].items->data);
1369                 return -1;
1370             }
1371             sprintf(tname, "%u", newID);
1372         }
1373         else {
1374             printf("bos: can't initialize volume system client (code %d), trying anyway.\n",
1375                    as->parms[2].items->data, code);
1376             strncpy(tname, as->parms[2].items->data, sizeof(tname));
1377         }
1378         if (volutil_GetPartitionID(as->parms[1].items->data) < 0) {
1379             /* can't parse volume ID, so complain before shutting down
1380              * file server.
1381              */
1382             printf("bos: can't interpret %s as partition ID.\n",
1383                    as->parms[1].items->data);
1384             return -1;
1385         }
1386         printf("Starting salvage.\n");
1387         rc = DoSalvage(tconn, as->parms[1].items->data, tname, outName,
1388                        showlog,parallel,tmpDir,orphans);
1389         if (rc) {
1390            printf("bos: failed to start 'salvager' (%s)\n", em(rc));
1391            return rc;
1392         }
1393     }
1394     return 0;
1395 }
1396
1397 static IStatServer(as, int32p)
1398 int int32p;
1399 register struct cmd_syndesc *as; {
1400     register struct rx_connection *tconn;
1401     register struct cmd_item *ti;
1402     int firstTime = 1;
1403
1404     tconn = GetConn(as, 0);
1405     for (ti=as->parms[1].items; ti; ti=ti->next) {
1406         DoStat(ti->data, tconn, int32p, firstTime);
1407         firstTime = 0;
1408     }
1409     return 0;
1410 }
1411
1412 static DoStat (aname, aconn, aint32p, firstTime)
1413   IN char *aname;
1414   IN register struct rx_connection *aconn;
1415   IN int aint32p;
1416   IN int firstTime;                     /* true iff first instance in cmd */
1417 {
1418     afs_int32 temp;
1419     char buffer[500];
1420     register afs_int32 code;
1421     register afs_int32 i;
1422     struct bozo_status istatus;
1423     char *tp;
1424     char *is1, *is2, *is3, *is4;        /* instance strings */
1425
1426     tp = buffer;
1427     code = BOZO_GetInstanceInfo(aconn, aname, &tp, &istatus);
1428     if (code) {
1429         printf("bos: failed to get instance info for '%s' (%s)\n",
1430                aname, em(code));
1431         return -1;
1432     }
1433     if (firstTime && aint32p && (istatus.flags & BOZO_BADDIRACCESS))
1434         printf ("Bosserver reports inappropriate access on server directories\n");
1435     printf("Instance %s, ", aname);
1436     if (aint32p) printf("(type is %s) ", buffer);
1437     if (istatus.fileGoal == istatus.goal) {
1438         if (!istatus.goal) printf("disabled, ");
1439     }
1440     else {
1441         if (istatus.fileGoal) printf("temporarily disabled, ");
1442         else printf("temporarily enabled, ");
1443     }
1444
1445     if (istatus.flags & BOZO_ERRORSTOP)
1446         printf("stopped for too many errors, ");
1447     if (istatus.flags & BOZO_HASCORE)
1448         printf("has core file, ");
1449
1450     tp = buffer;
1451     code = BOZO_GetStatus(aconn, aname, &temp, &tp);
1452     if (code)
1453         printf("bos: failed to get status for instance '%s' (%s)\n",
1454                aname, em(code));
1455     else {
1456         printf("currently ", aname);
1457         if (temp == BSTAT_NORMAL) printf("running normally.\n");
1458         else if (temp == BSTAT_SHUTDOWN) printf("shutdown.\n");
1459         else if (temp == BSTAT_STARTINGUP) printf("starting up.\n");
1460         else if (temp == BSTAT_SHUTTINGDOWN) printf("shutting down.\n");
1461         if (buffer[0] != 0) {
1462             printf("    Auxiliary status is: %s.\n", buffer);
1463         }
1464     }
1465
1466     /* are we done yet? */
1467     if (!aint32p) return 0;
1468
1469     if (istatus.procStartTime)
1470         printf("    Process last started at %s (%d proc starts)\n",
1471                DateOf(istatus.procStartTime), istatus.procStarts);
1472     if (istatus.lastAnyExit) {
1473         printf("    Last exit at %s\n", DateOf(istatus.lastAnyExit));
1474     }
1475     if (istatus.lastErrorExit) {
1476         is1 = is2 = is3 = is4 = (char *) 0;
1477         printf("    Last error exit at %s, ", DateOf(istatus.lastErrorExit));
1478         code = BOZO_GetInstanceStrings(aconn, aname, &is1, &is2, &is3, &is4);
1479         /* don't complain about failing call, since could simply mean
1480          * interface mismatch.
1481          */
1482         if (code == 0) {
1483             if (*is1 != 0) {
1484                 /* non-null instance string */
1485                 printf("by %s, ", is1);
1486             }
1487             free(is1);
1488             free(is2);
1489             free(is3);
1490             free(is4);
1491         }
1492         if (istatus.errorSignal) {
1493             if (istatus.errorSignal == SIGTERM)
1494                 printf("due to shutdown request\n");
1495             else
1496                 printf("due to signal %d\n", istatus.errorSignal);
1497         }
1498         else
1499             printf("by exiting with code %d\n", istatus.errorCode);
1500     }
1501     
1502     if (aint32p > 1) {
1503         /* try to display all the parms */
1504         for(i=0;;i++) {
1505             tp = buffer;
1506             code = BOZO_GetInstanceParm(aconn, aname, i, &tp);
1507             if (code) break;
1508             printf("    Command %d is '%s'\n", i+1, buffer);
1509         }
1510         tp = buffer;
1511         code = BOZO_GetInstanceParm(aconn, aname, 999, &tp);
1512         if (!code) {
1513             /* Any type of failure is treated as not having a notifier program */
1514             printf("    Notifier  is '%s'\n", buffer);
1515         }
1516         printf("\n");
1517     }
1518     return 0;
1519 }
1520
1521 static void add_std_args (ts)
1522   register struct cmd_syndesc *ts;
1523 {
1524     cmd_Seek(ts, 12);
1525     /* 12 */ cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cell name");
1526     /* 13 */cmd_AddParm(ts, "-noauth", CMD_FLAG, CMD_OPTIONAL,
1527                         "don't authenticate");
1528     /* 14 */ cmd_AddParm (ts, "-localauth", CMD_FLAG, CMD_OPTIONAL,
1529                           "create tickets from KeyFile");
1530 }
1531   
1532 #include "AFS_component_version_number.c"
1533
1534 main(argc, argv)
1535   int argc;
1536   char **argv;
1537 {
1538     register afs_int32 code;
1539     register struct cmd_syndesc *ts;
1540     extern int afsconf_SawCell;
1541
1542 #ifdef  AFS_AIX32_ENV
1543     /*
1544      * The following signal action for AIX is necessary so that in case of a 
1545      * crash (i.e. core is generated) we can include the user's data section 
1546      * in the core dump. Unfortunately, by default, only a partial core is
1547      * generated which, in many cases, isn't too useful.
1548      */
1549     struct sigaction nsa;
1550     
1551     sigemptyset(&nsa.sa_mask);
1552     nsa.sa_handler = SIG_DFL;
1553     nsa.sa_flags = SA_FULLDUMP;
1554     sigaction(SIGSEGV, &nsa, NULL);
1555     sigaction(SIGABRT, &nsa, NULL);
1556 #endif
1557
1558     /* start up rx */
1559     code = rx_Init(0);
1560     if (code) {
1561         printf("bos: could not initialize rx (%s)\n", em(code));
1562         exit(1);
1563     }
1564
1565     /* To resolve a AFSCELL environment "Note: ..." problem: Because bos calls ka_Init (why?) before any
1566      * checkup on the existance of a "-cell" option on the particular bos command we don't print the
1567      * message if a cell is passed in. Luckily the rest of the programs don't call these adhoc routines
1568      * and things work fine. Note that if the "-cell" isn't passed then we're still ok since later on
1569      * the proper routine (afsconf_GetCellInfo) is going to be called to do the right thing.
1570      */
1571     afsconf_SawCell = 1;        /* Means don't print warning if AFSCELL is set */
1572     ka_Init(0);
1573     afsconf_SawCell = 0;        /* Reset it */
1574     /* don't check error code, since fails sometimes when we're setting up a
1575      * system */
1576     initialize_cmd_error_table();
1577     initialize_bz_error_table();
1578
1579     ts = cmd_CreateSyntax("start", StartServer, 0, "start running a server");
1580     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
1581     cmd_AddParm(ts, "-instance", CMD_LIST, 0, "server process name");
1582     add_std_args (ts);
1583
1584     ts = cmd_CreateSyntax("stop", StopServer, 0, "halt a server instance");
1585     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
1586     cmd_AddParm(ts, "-instance", CMD_LIST, 0, "server process name");
1587     cmd_Seek(ts, 8);
1588     cmd_AddParm(ts, "-wait", CMD_FLAG, CMD_OPTIONAL,
1589                 "wait for process to stop");
1590     add_std_args (ts);
1591     
1592     ts = cmd_CreateSyntax("status", StatServer, 0,
1593                           "show server instance status");
1594     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
1595     cmd_AddParm(ts, "-instance", CMD_LIST, CMD_OPTIONAL,
1596                 "server process name");
1597     cmd_AddParm(ts, "-long", CMD_FLAG, CMD_OPTIONAL, "long status");
1598     add_std_args (ts);
1599
1600     ts = cmd_CreateSyntax("shutdown", Shutdown, 0, "shutdown all processes");
1601     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
1602     cmd_AddParm(ts, "-instance", CMD_LIST, CMD_OPTIONAL, "instances");
1603     cmd_Seek(ts, 8);
1604     cmd_AddParm(ts, "-wait", CMD_FLAG, CMD_OPTIONAL,
1605                 "wait for process to stop");
1606     add_std_args (ts);
1607
1608     ts = cmd_CreateSyntax("startup", Startup, 0, "start all processes");
1609     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
1610     cmd_AddParm(ts, "-instance", CMD_LIST, CMD_OPTIONAL, "instances");
1611     add_std_args (ts);
1612
1613     ts = cmd_CreateSyntax("restart", Restart, 0, "restart all processes");
1614     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
1615     cmd_AddParm(ts, "-instance", CMD_LIST, CMD_OPTIONAL, "instances");
1616     cmd_AddParm(ts, "-bosserver", CMD_FLAG, CMD_OPTIONAL, "restart bosserver");
1617     cmd_AddParm(ts, "-all", CMD_FLAG, CMD_OPTIONAL, "restart all processes");
1618     add_std_args (ts);
1619
1620 #ifndef OPBOS
1621
1622     ts = cmd_CreateSyntax("create", CreateServer, 0,
1623                           "create a new server instance");
1624     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
1625     cmd_AddParm(ts, "-instance", CMD_SINGLE, 0, "server process name");
1626     cmd_AddParm(ts, "-type", CMD_SINGLE, 0, "server type");
1627     cmd_AddParm(ts, "-cmd", CMD_LIST, 0, "command lines");
1628     cmd_AddParm(ts, "-notifier", CMD_SINGLE, CMD_OPTIONAL, "Notifier program");
1629     add_std_args (ts);
1630
1631     ts = cmd_CreateSyntax("delete", DeleteServer, 0,
1632                           "delete a server instance");
1633     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
1634     cmd_AddParm(ts, "-instance", CMD_LIST, 0, "server process name");
1635     add_std_args (ts);
1636     
1637     ts = cmd_CreateSyntax("adduser", AddSUser, 0,
1638                           "add users to super-user list");
1639     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
1640     cmd_AddParm(ts, "-user", CMD_LIST, 0, "user names");
1641     add_std_args (ts);
1642
1643     ts = cmd_CreateSyntax("removeuser", RemoveSUser, 0,
1644                           "remove users from super-user list");
1645     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
1646     cmd_AddParm(ts, "-user", CMD_LIST, 0, "user names");
1647     add_std_args (ts);
1648
1649     ts = cmd_CreateSyntax("listusers", ListSUsers, 0, "list super-users");
1650     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
1651     add_std_args (ts);
1652
1653     ts = cmd_CreateSyntax("addkey", AddKey, 0,
1654                            "add keys to key dbase (kvno 999 is bcrypt)");
1655     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
1656     cmd_AddParm(ts, "-key", CMD_SINGLE, CMD_OPTIONAL, "key");
1657     cmd_AddParm(ts, "-kvno", CMD_SINGLE, 0, "key version number");
1658     add_std_args (ts);
1659
1660     ts = cmd_CreateSyntax("removekey", RemoveKey, 0,
1661                           "remove keys from key dbase");
1662     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
1663     cmd_AddParm(ts, "-kvno", CMD_LIST, 0, "key version number");
1664     add_std_args (ts);
1665
1666     ts = cmd_CreateSyntax("listkeys", ListKeys, 0, "list keys");
1667     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
1668     cmd_AddParm(ts, "-showkey", CMD_FLAG, CMD_OPTIONAL, "show the actual key rather than the checksum");
1669     add_std_args (ts);
1670
1671     ts = cmd_CreateSyntax("listhosts", ListHosts, 0, "get cell host list");
1672     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
1673     add_std_args (ts);
1674     cmd_CreateAlias(ts, "getcell");
1675
1676     ts = cmd_CreateSyntax("setcellname", SetCellName, 0, "set cell name");
1677     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
1678     cmd_AddParm(ts, "-name", CMD_SINGLE, 0, "cell name");
1679     add_std_args (ts);
1680
1681     ts = cmd_CreateSyntax("addhost", AddHost, 0, "add host to cell dbase");
1682     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
1683     cmd_AddParm(ts, "-host", CMD_LIST, 0, "host name");
1684     add_std_args (ts);
1685
1686     ts = cmd_CreateSyntax("removehost", RemoveHost, 0,
1687                           "remove host from cell dbase");
1688     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
1689     cmd_AddParm(ts, "-host", CMD_LIST, 0, "host name");
1690     add_std_args (ts);
1691
1692     ts = cmd_CreateSyntax("setauth", SetAuth, 0,
1693                           "set authentication required flag");
1694     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
1695     cmd_AddParm(ts, "-authrequired", CMD_SINGLE,
1696                 0, "on or off: authentication required for admin requests");
1697     add_std_args (ts);
1698     
1699     ts = cmd_CreateSyntax("install", Install, 0, "install program");
1700     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
1701     cmd_AddParm(ts, "-file", CMD_LIST, 0, "files to install");
1702     cmd_AddParm(ts, "-dir", CMD_SINGLE, CMD_OPTIONAL, "destination dir");
1703     add_std_args (ts);
1704
1705     ts = cmd_CreateSyntax("uninstall", UnInstall, 0, "uninstall program");
1706     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
1707     cmd_AddParm(ts, "-file", CMD_LIST, 0, "files to uninstall");
1708     cmd_AddParm(ts, "-dir", CMD_SINGLE, CMD_OPTIONAL, "destination dir");
1709     add_std_args (ts);
1710
1711     ts = cmd_CreateSyntax("getlog", GetLogCmd, 0, "examine log file");
1712     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
1713     cmd_AddParm(ts, "-file", CMD_SINGLE, 0, "log file to examine");
1714     add_std_args (ts);
1715
1716     ts = cmd_CreateSyntax("getdate", GetDate, 0, "get dates for programs");
1717     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
1718     cmd_AddParm(ts, "-file", CMD_LIST, 0, "files to check");
1719     cmd_AddParm(ts, "-dir", CMD_SINGLE, CMD_OPTIONAL, "destination dir");
1720     add_std_args (ts);
1721
1722     ts = cmd_CreateSyntax("exec", Exec, 0, "execute shell command on server");
1723     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
1724     cmd_AddParm(ts, "-cmd", CMD_SINGLE, 0, "command to execute");
1725     add_std_args (ts);
1726
1727     ts = cmd_CreateSyntax("prune", Prune, 0, "prune server files");
1728     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
1729     cmd_AddParm(ts, "-bak", CMD_FLAG, CMD_OPTIONAL, "delete .BAK files");
1730     cmd_AddParm(ts, "-old", CMD_FLAG, CMD_OPTIONAL, "delete .OLD files");
1731     cmd_AddParm(ts, "-core", CMD_FLAG, CMD_OPTIONAL, "delete core files");
1732     cmd_AddParm(ts, "-all", CMD_FLAG, CMD_OPTIONAL, "delete all junk files");
1733     add_std_args (ts);
1734
1735     ts = cmd_CreateSyntax("setrestart", SetRestartCmd, 0, "set restart times");
1736     cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_REQUIRED, "machine name");
1737     cmd_AddParm(ts, "-time", CMD_SINGLE,
1738                 CMD_REQUIRED, "time to restart server");
1739     cmd_AddParm(ts, "-general", CMD_FLAG,
1740                 CMD_OPTIONAL, "set general restart time");
1741     cmd_AddParm(ts, "-newbinary", CMD_FLAG,
1742                 CMD_OPTIONAL, "set new binary restart time");
1743     add_std_args (ts);
1744
1745     ts = cmd_CreateSyntax("getrestart", GetRestartCmd, 0, "get restart times");
1746     cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_REQUIRED, "machine name");
1747     add_std_args (ts);
1748
1749     ts = cmd_CreateSyntax("salvage", SalvageCmd, 0,
1750                           "salvage partition or volumes");
1751     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
1752     cmd_AddParm(ts, "-partition", CMD_SINGLE,
1753                 CMD_OPTIONAL, "salvage partition");
1754     cmd_AddParm(ts, "-volume", CMD_SINGLE,
1755                 CMD_OPTIONAL, "salvage volume number or volume name");
1756     cmd_AddParm(ts, "-file", CMD_SINGLE,
1757                 CMD_OPTIONAL, "salvage log output file");
1758     cmd_AddParm(ts, "-all", CMD_FLAG, CMD_OPTIONAL, "salvage whole server");
1759     cmd_AddParm(ts, "-showlog", CMD_FLAG, CMD_OPTIONAL, "display salvage log");
1760     cmd_AddParm(ts, "-parallel",CMD_SINGLE, CMD_OPTIONAL, 
1761                 "# of max parallel partition salvaging");
1762     cmd_AddParm(ts, "-tmpdir",CMD_SINGLE, CMD_OPTIONAL, 
1763                 "directory to place tmp files");
1764     cmd_AddParm(ts, "-orphans", CMD_SINGLE, CMD_OPTIONAL, 
1765                 "ignore | remove | attach");
1766     add_std_args (ts);
1767
1768 #endif
1769
1770     code = cmd_Dispatch(argc, argv);
1771     rx_Finalize();
1772     exit(code);
1773 }