Makes fs getfid error handling consistent with other fs commands
[openafs.git] / src / venus / fs.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  *
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <afsconfig.h>
11 #include <afs/param.h>
12
13
14 #include <afs/afs_consts.h>
15 #include <afs/afs_args.h>
16 #include <rx/xdr.h>
17 #include <sys/ioctl.h>
18 #include <sys/socket.h>
19 #include <netdb.h>
20 #include <errno.h>
21 #include <stdio.h>
22 #include <ctype.h>
23 #include <netinet/in.h>
24 #include <sys/stat.h>
25 #include <afs/stds.h>
26 #include <afs/vice.h>
27 #include <afs/venus.h>
28 #include <afs/com_err.h>
29 #include <afs/afs_consts.h>
30 #ifdef  AFS_AIX32_ENV
31 #include <signal.h>
32 #endif
33
34 #include <string.h>
35
36 #undef VIRTUE
37 #undef VICE
38 #include "afs/prs_fs.h"
39 #include <afs/afsint.h>
40 #include <afs/cellconfig.h>
41 #include <ubik.h>
42 #include <rx/rxkad.h>
43 #include <rx/rx_globals.h>
44 #include <afs/vldbint.h>
45 #include <afs/volser.h>
46 #include <afs/vlserver.h>
47 #include <afs/cmd.h>
48 #include <afs/afsutil.h>
49 #include <afs/com_err.h>
50 #include <stdlib.h>
51 #include <assert.h>
52 #include <afs/ptclient.h>
53 #include <afs/ptuser.h>
54 #include <afs/afsutil.h>
55 #include <afs/sys_prototypes.h>
56
57 #define MAXNAME 100
58 #define MAXINSIZE 1300          /* pioctl complains if data is larger than this */
59 #define VMSGSIZE 128            /* size of msg buf in volume hdr */
60
61 static char space[AFS_PIOCTL_MAXSIZE];
62 static char tspace[1024];
63 static struct ubik_client *uclient;
64
65 static int GetClientAddrsCmd(struct cmd_syndesc *, void *);
66 static int SetClientAddrsCmd(struct cmd_syndesc *, void *);
67 static int FlushMountCmd(struct cmd_syndesc *, void *);
68 static int RxStatProcCmd(struct cmd_syndesc *, void *);
69 static int RxStatPeerCmd(struct cmd_syndesc *, void *);
70 static int GetFidCmd(struct cmd_syndesc *, void *);
71 static int UuidCmd(struct cmd_syndesc *, void *);
72
73 static char pn[] = "fs";
74 static int rxInitDone = 0;
75
76 struct AclEntry;
77 struct Acl;
78 static void ZapList(struct AclEntry *);
79 static int PruneList(struct AclEntry **, int);
80 static int CleanAcl(struct Acl *, char *);
81 static int SetVolCmd(struct cmd_syndesc *as, void *arock);
82 static int GetCellName(char *, struct afsconf_cell *);
83 static int VLDBInit(int, struct afsconf_cell *);
84 static void Die(int, char *);
85
86 /*
87  * Character to use between name and rights in printed representation for
88  * DFS ACL's.
89  */
90 #define DFS_SEPARATOR   ' '
91
92 typedef char sec_rgy_name_t[1025];      /* A DCE definition */
93
94 struct Acl {
95     int dfs;                    /* Originally true if a dfs acl; now also the type
96                                  * of the acl (1, 2, or 3, corresponding to object,
97                                  * initial dir, or initial object). */
98     sec_rgy_name_t cell;        /* DFS cell name */
99     int nplus;
100     int nminus;
101     struct AclEntry *pluslist;
102     struct AclEntry *minuslist;
103 };
104
105 struct AclEntry {
106     struct AclEntry *next;
107     char name[MAXNAME];
108     afs_int32 rights;
109 };
110
111 struct vcxstat2 {
112     afs_int32 callerAccess;
113     afs_int32 cbExpires;
114     afs_int32 anyAccess;
115     char mvstat;
116 };
117
118 static void
119 ZapAcl(struct Acl *acl)
120 {
121     if (!acl)
122         return;
123     ZapList(acl->pluslist);
124     ZapList(acl->minuslist);
125     free(acl);
126 }
127
128 static int
129 foldcmp(char *a, char *b)
130 {
131     char t, u;
132     while (1) {
133         t = *a++;
134         u = *b++;
135         if (t >= 'A' && t <= 'Z')
136             t += 0x20;
137         if (u >= 'A' && u <= 'Z')
138             u += 0x20;
139         if (t != u)
140             return 1;
141         if (t == 0)
142             return 0;
143     }
144 }
145
146 /*
147  * Mods for the AFS/DFS protocol translator.
148  *
149  * DFS rights. It's ugly to put these definitions here, but they
150  * *cannot* change, because they're part of the wire protocol.
151  * In any event, the protocol translator will guarantee these
152  * assignments for AFS cache managers.
153  */
154 #define DFS_READ          0x01
155 #define DFS_WRITE         0x02
156 #define DFS_EXECUTE       0x04
157 #define DFS_CONTROL       0x08
158 #define DFS_INSERT        0x10
159 #define DFS_DELETE        0x20
160
161 /* the application definable ones (backwards from AFS) */
162 #define DFS_USR0 0x80000000     /* "A" bit */
163 #define DFS_USR1 0x40000000     /* "B" bit */
164 #define DFS_USR2 0x20000000     /* "C" bit */
165 #define DFS_USR3 0x10000000     /* "D" bit */
166 #define DFS_USR4 0x08000000     /* "E" bit */
167 #define DFS_USR5 0x04000000     /* "F" bit */
168 #define DFS_USR6 0x02000000     /* "G" bit */
169 #define DFS_USR7 0x01000000     /* "H" bit */
170 #define DFS_USRALL      (DFS_USR0 | DFS_USR1 | DFS_USR2 | DFS_USR3 |\
171                          DFS_USR4 | DFS_USR5 | DFS_USR6 | DFS_USR7)
172
173 /*
174  * Offset of -id switch in command structure for various commands.
175  * The -if switch is the next switch always.
176  */
177 static int parm_setacl_id, parm_copyacl_id, parm_listacl_id;
178
179 /*
180  * Determine whether either the -id or -if switches are present, and
181  * return 0, 1 or 2, as appropriate. Abort if both switches are present.
182  */
183 /*    int id;   Offset of -id switch; -if is next switch */
184 static int
185 getidf(struct cmd_syndesc *as, int id)
186 {
187     int idf = 0;
188
189     if (as->parms[id].items) {
190         idf |= 1;
191     }
192     if (as->parms[id + 1].items) {
193         idf |= 2;
194     }
195     if (idf == 3) {
196         fprintf(stderr,
197                 "%s: you may specify either -id or -if, but not both switches\n",
198                 pn);
199         exit(1);
200     }
201     return idf;
202 }
203
204 static int
205 PRights(afs_int32 arights, int dfs)
206 {
207     if (!dfs) {
208         if (arights & PRSFS_READ)
209             printf("r");
210         if (arights & PRSFS_LOOKUP)
211             printf("l");
212         if (arights & PRSFS_INSERT)
213             printf("i");
214         if (arights & PRSFS_DELETE)
215             printf("d");
216         if (arights & PRSFS_WRITE)
217             printf("w");
218         if (arights & PRSFS_LOCK)
219             printf("k");
220         if (arights & PRSFS_ADMINISTER)
221             printf("a");
222         if (arights & PRSFS_USR0)
223             printf("A");
224         if (arights & PRSFS_USR1)
225             printf("B");
226         if (arights & PRSFS_USR2)
227             printf("C");
228         if (arights & PRSFS_USR3)
229             printf("D");
230         if (arights & PRSFS_USR4)
231             printf("E");
232         if (arights & PRSFS_USR5)
233             printf("F");
234         if (arights & PRSFS_USR6)
235             printf("G");
236         if (arights & PRSFS_USR7)
237             printf("H");
238     } else {
239         if (arights & DFS_READ)
240             printf("r");
241         else
242             printf("-");
243         if (arights & DFS_WRITE)
244             printf("w");
245         else
246             printf("-");
247         if (arights & DFS_EXECUTE)
248             printf("x");
249         else
250             printf("-");
251         if (arights & DFS_CONTROL)
252             printf("c");
253         else
254             printf("-");
255         if (arights & DFS_INSERT)
256             printf("i");
257         else
258             printf("-");
259         if (arights & DFS_DELETE)
260             printf("d");
261         else
262             printf("-");
263         if (arights & (DFS_USRALL))
264             printf("+");
265         if (arights & DFS_USR0)
266             printf("A");
267         if (arights & DFS_USR1)
268             printf("B");
269         if (arights & DFS_USR2)
270             printf("C");
271         if (arights & DFS_USR3)
272             printf("D");
273         if (arights & DFS_USR4)
274             printf("E");
275         if (arights & DFS_USR5)
276             printf("F");
277         if (arights & DFS_USR6)
278             printf("G");
279         if (arights & DFS_USR7)
280             printf("H");
281     }
282     return 0;
283 }
284
285 /* this function returns TRUE (1) if the file is in AFS, otherwise false (0) */
286 static int
287 InAFS(char *apath)
288 {
289     struct ViceIoctl blob;
290     afs_int32 code;
291
292     blob.in_size = 0;
293     blob.out_size = AFS_PIOCTL_MAXSIZE;
294     blob.out = space;
295
296     code = pioctl(apath, VIOC_FILE_CELL_NAME, &blob, 1);
297     if (code) {
298         if ((errno == EINVAL) || (errno == ENOENT))
299             return 0;
300     }
301     return 1;
302 }
303
304 /* return a static pointer to a buffer */
305 static char *
306 Parent(char *apath)
307 {
308     char *tp;
309     strlcpy(tspace, apath, sizeof(tspace));
310     tp = strrchr(tspace, '/');
311     if (tp == (char *)tspace)
312         tp++;
313     else if (tp == (char *)NULL) {
314         tp      = (char *)tspace;
315         *(tp++) = '.';
316     }
317     *tp = '\0';
318     return tspace;
319 }
320
321 enum rtype { add, destroy, deny };
322
323 static afs_int32
324 Convert(char *arights, int dfs, enum rtype *rtypep)
325 {
326     int i, len;
327     afs_int32 mode;
328     char tc;
329
330     *rtypep = add;              /* add rights, by default */
331
332     if (dfs) {
333         if (!strcmp(arights, "null")) {
334             *rtypep = deny;
335             return 0;
336         }
337         if (!strcmp(arights, "read"))
338             return DFS_READ | DFS_EXECUTE;
339         if (!strcmp(arights, "write"))
340             return DFS_READ | DFS_EXECUTE | DFS_INSERT | DFS_DELETE |
341                 DFS_WRITE;
342         if (!strcmp(arights, "all"))
343             return DFS_READ | DFS_EXECUTE | DFS_INSERT | DFS_DELETE |
344                 DFS_WRITE | DFS_CONTROL;
345     } else {
346         if (!strcmp(arights, "read"))
347             return PRSFS_READ | PRSFS_LOOKUP;
348         if (!strcmp(arights, "write"))
349             return PRSFS_READ | PRSFS_LOOKUP | PRSFS_INSERT | PRSFS_DELETE |
350                 PRSFS_WRITE | PRSFS_LOCK;
351         if (!strcmp(arights, "mail"))
352             return PRSFS_INSERT | PRSFS_LOCK | PRSFS_LOOKUP;
353         if (!strcmp(arights, "all"))
354             return PRSFS_READ | PRSFS_LOOKUP | PRSFS_INSERT | PRSFS_DELETE |
355                 PRSFS_WRITE | PRSFS_LOCK | PRSFS_ADMINISTER;
356     }
357     if (!strcmp(arights, "none")) {
358         *rtypep = destroy;      /* Remove entire entry */
359         return 0;
360     }
361     len = strlen(arights);
362     mode = 0;
363     for (i = 0; i < len; i++) {
364         tc = *arights++;
365         if (dfs) {
366             if (tc == '-')
367                 continue;
368             else if (tc == 'r')
369                 mode |= DFS_READ;
370             else if (tc == 'w')
371                 mode |= DFS_WRITE;
372             else if (tc == 'x')
373                 mode |= DFS_EXECUTE;
374             else if (tc == 'c')
375                 mode |= DFS_CONTROL;
376             else if (tc == 'i')
377                 mode |= DFS_INSERT;
378             else if (tc == 'd')
379                 mode |= DFS_DELETE;
380             else if (tc == 'A')
381                 mode |= DFS_USR0;
382             else if (tc == 'B')
383                 mode |= DFS_USR1;
384             else if (tc == 'C')
385                 mode |= DFS_USR2;
386             else if (tc == 'D')
387                 mode |= DFS_USR3;
388             else if (tc == 'E')
389                 mode |= DFS_USR4;
390             else if (tc == 'F')
391                 mode |= DFS_USR5;
392             else if (tc == 'G')
393                 mode |= DFS_USR6;
394             else if (tc == 'H')
395                 mode |= DFS_USR7;
396             else {
397                 fprintf(stderr, "%s: illegal DFS rights character '%c'.\n",
398                         pn, tc);
399                 exit(1);
400             }
401         } else {
402             if (tc == 'r')
403                 mode |= PRSFS_READ;
404             else if (tc == 'l')
405                 mode |= PRSFS_LOOKUP;
406             else if (tc == 'i')
407                 mode |= PRSFS_INSERT;
408             else if (tc == 'd')
409                 mode |= PRSFS_DELETE;
410             else if (tc == 'w')
411                 mode |= PRSFS_WRITE;
412             else if (tc == 'k')
413                 mode |= PRSFS_LOCK;
414             else if (tc == 'a')
415                 mode |= PRSFS_ADMINISTER;
416             else if (tc == 'A')
417                 mode |= PRSFS_USR0;
418             else if (tc == 'B')
419                 mode |= PRSFS_USR1;
420             else if (tc == 'C')
421                 mode |= PRSFS_USR2;
422             else if (tc == 'D')
423                 mode |= PRSFS_USR3;
424             else if (tc == 'E')
425                 mode |= PRSFS_USR4;
426             else if (tc == 'F')
427                 mode |= PRSFS_USR5;
428             else if (tc == 'G')
429                 mode |= PRSFS_USR6;
430             else if (tc == 'H')
431                 mode |= PRSFS_USR7;
432             else {
433                 fprintf(stderr, "%s: illegal rights character '%c'.\n", pn,
434                         tc);
435                 exit(1);
436             }
437         }
438     }
439     return mode;
440 }
441
442 static struct AclEntry *
443 FindList(struct AclEntry *alist, char *aname)
444 {
445     while (alist) {
446         if (!foldcmp(alist->name, aname))
447             return alist;
448         alist = alist->next;
449     }
450     return 0;
451 }
452
453 /* if no parm specified in a particular slot, set parm to be "." instead */
454 static void
455 SetDotDefault(struct cmd_item **aitemp)
456 {
457     struct cmd_item *ti;
458     if (*aitemp)
459         return;                 /* already has value */
460     /* otherwise, allocate an item representing "." */
461     ti = (struct cmd_item *)malloc(sizeof(struct cmd_item));
462     assert(ti);
463     ti->next = (struct cmd_item *)0;
464     ti->data = (char *)malloc(2);
465     assert(ti->data);
466     strcpy(ti->data, ".");
467     *aitemp = ti;
468 }
469
470 static void
471 ChangeList(struct Acl *al, afs_int32 plus, char *aname, afs_int32 arights)
472 {
473     struct AclEntry *tlist;
474     tlist = (plus ? al->pluslist : al->minuslist);
475     tlist = FindList(tlist, aname);
476     if (tlist) {
477         /* Found the item already in the list. */
478         tlist->rights = arights;
479         if (plus)
480             al->nplus -= PruneList(&al->pluslist, al->dfs);
481         else
482             al->nminus -= PruneList(&al->minuslist, al->dfs);
483         return;
484     }
485     /* Otherwise we make a new item and plug in the new data. */
486     tlist = (struct AclEntry *)malloc(sizeof(struct AclEntry));
487     assert(tlist);
488     strcpy(tlist->name, aname);
489     tlist->rights = arights;
490     if (plus) {
491         tlist->next = al->pluslist;
492         al->pluslist = tlist;
493         al->nplus++;
494         if (arights == 0 || arights == -1)
495             al->nplus -= PruneList(&al->pluslist, al->dfs);
496     } else {
497         tlist->next = al->minuslist;
498         al->minuslist = tlist;
499         al->nminus++;
500         if (arights == 0)
501             al->nminus -= PruneList(&al->minuslist, al->dfs);
502     }
503 }
504
505 static void
506 ZapList(struct AclEntry *alist)
507 {
508     struct AclEntry *tp, *np;
509     for (tp = alist; tp; tp = np) {
510         np = tp->next;
511         free(tp);
512     }
513 }
514
515 static int
516 PruneList(struct AclEntry **ae, int dfs)
517 {
518     struct AclEntry **lp;
519     struct AclEntry *te, *ne;
520     afs_int32 ctr;
521     ctr = 0;
522     lp = ae;
523     for (te = *ae; te; te = ne) {
524         if ((!dfs && te->rights == 0) || te->rights == -1) {
525             *lp = te->next;
526             ne = te->next;
527             free(te);
528             ctr++;
529         } else {
530             ne = te->next;
531             lp = &te->next;
532         }
533     }
534     return ctr;
535 }
536
537 static char *
538 SkipLine(char *astr)
539 {
540     while (*astr != '\n')
541         astr++;
542     astr++;
543     return astr;
544 }
545
546 /*
547  * Create an empty acl, taking into account whether the acl pointed
548  * to by astr is an AFS or DFS acl. Only parse this minimally, so we
549  * can recover from problems caused by bogus ACL's (in that case, always
550  * assume that the acl is AFS: for DFS, the user can always resort to
551  * acl_edit, but for AFS there may be no other way out).
552  */
553 static struct Acl *
554 EmptyAcl(char *astr)
555 {
556     struct Acl *tp;
557     int junk;
558
559     tp = (struct Acl *)malloc(sizeof(struct Acl));
560     assert(tp);
561     tp->nplus = tp->nminus = 0;
562     tp->pluslist = tp->minuslist = 0;
563     tp->dfs = 0;
564     sscanf(astr, "%d dfs:%d %s", &junk, &tp->dfs, tp->cell);
565     return tp;
566 }
567
568 static struct Acl *
569 ParseAcl(char *astr)
570 {
571     int nplus, nminus, i, trights;
572     char tname[MAXNAME];
573     struct AclEntry *first, *last, *tl;
574     struct Acl *ta;
575
576     ta = (struct Acl *)malloc(sizeof(struct Acl));
577     assert(ta);
578     ta->dfs = 0;
579     sscanf(astr, "%d dfs:%d %s", &ta->nplus, &ta->dfs, ta->cell);
580     astr = SkipLine(astr);
581     sscanf(astr, "%d", &ta->nminus);
582     astr = SkipLine(astr);
583
584     nplus = ta->nplus;
585     nminus = ta->nminus;
586
587     last = 0;
588     first = 0;
589     for (i = 0; i < nplus; i++) {
590         sscanf(astr, "%100s %d", tname, &trights);
591         astr = SkipLine(astr);
592         tl = (struct AclEntry *)malloc(sizeof(struct AclEntry));
593         assert(tl);
594         if (!first)
595             first = tl;
596         strcpy(tl->name, tname);
597         tl->rights = trights;
598         tl->next = 0;
599         if (last)
600             last->next = tl;
601         last = tl;
602     }
603     ta->pluslist = first;
604
605     last = 0;
606     first = 0;
607     for (i = 0; i < nminus; i++) {
608         sscanf(astr, "%100s %d", tname, &trights);
609         astr = SkipLine(astr);
610         tl = (struct AclEntry *)malloc(sizeof(struct AclEntry));
611         assert(tl);
612         if (!first)
613             first = tl;
614         strcpy(tl->name, tname);
615         tl->rights = trights;
616         tl->next = 0;
617         if (last)
618             last->next = tl;
619         last = tl;
620     }
621     ta->minuslist = first;
622
623     return ta;
624 }
625
626 static int
627 PrintStatus(VolumeStatus * status, char *name, char *offmsg)
628 {
629     printf("Volume status for vid = %u named %s\n", status->Vid, name);
630     if (*offmsg != 0)
631         printf("Current offline message is %s\n", offmsg);
632     printf("Current disk quota is ");
633     if (status->MaxQuota != 0)
634         printf("%d\n", status->MaxQuota);
635     else
636         printf("unlimited\n");
637     printf("Current blocks used are %d\n", status->BlocksInUse);
638     printf("The partition has %d blocks available out of %d\n\n",
639            status->PartBlocksAvail, status->PartMaxBlocks);
640     return 0;
641 }
642
643 static const char power_letter[] = {
644   'K',  /* kibi */
645   'M',  /* mebi */
646   'G',  /* gibi */
647   'T',  /* tebi */
648   'P',  /* pebi */
649 };
650
651 static void
652 HumanPrintSpace(afs_int32 int_space)
653 {
654     int exponent = 0;
655     int exponent_max = sizeof(power_letter) - 1;
656     float space = int_space;
657
658     while (space >= 1024 && exponent < exponent_max) {
659         exponent++;
660         space /= 1024;
661     }
662     printf("%9.1f%c", space, power_letter[exponent]);
663 }
664
665 static int
666 QuickPrintStatus(VolumeStatus * status, char *name, int human)
667 {
668     double QuotaUsed = 0.0;
669     double PartUsed = 0.0;
670     int WARN = 0;
671     printf("%-25.25s", name);
672
673     if (status->MaxQuota != 0) {
674         if (human) {
675             printf(" ");
676             HumanPrintSpace(status->MaxQuota);
677             printf(" ");
678             HumanPrintSpace(status->BlocksInUse);
679         }
680         else
681             printf(" %10d %10d", status->MaxQuota, status->BlocksInUse);
682         QuotaUsed =
683             ((((double)status->BlocksInUse) / status->MaxQuota) * 100.0);
684     } else {
685         printf("   no limit ");
686         if (human)
687             HumanPrintSpace(status->BlocksInUse);
688         else
689             printf("%10d", status->BlocksInUse);
690     }
691     if (QuotaUsed > 90.0) {
692         printf("%5.0f%%<<", QuotaUsed);
693         WARN = 1;
694     } else
695         printf("%5.0f%%  ", QuotaUsed);
696     PartUsed =
697         (100.0 -
698          ((((double)status->PartBlocksAvail) / status->PartMaxBlocks) *
699           100.0));
700     if (PartUsed > 97.0) {
701         printf("%9.0f%%<<", PartUsed);
702         WARN = 1;
703     } else
704         printf("%9.0f%%  ", PartUsed);
705     if (WARN) {
706         printf("  <<WARNING\n");
707     } else
708         printf("\n");
709     return 0;
710 }
711
712 static int
713 QuickPrintSpace(VolumeStatus * status, char *name, int human)
714 {
715     double PartUsed = 0.0;
716     int WARN = 0;
717     printf("%-25.25s", name);
718
719     if (human) {
720         HumanPrintSpace(status->PartMaxBlocks);
721         HumanPrintSpace(status->PartMaxBlocks - status->PartBlocksAvail);
722         HumanPrintSpace(status->PartBlocksAvail);
723     }
724     else
725         printf("%10d%10d%10d", status->PartMaxBlocks,
726                status->PartMaxBlocks - status->PartBlocksAvail,
727                status->PartBlocksAvail);
728
729     PartUsed =
730         (100.0 -
731          ((((double)status->PartBlocksAvail) / status->PartMaxBlocks) *
732           100.0));
733     if (PartUsed > 90.0) {
734         printf(" %4.0f%%<<", PartUsed);
735         WARN = 1;
736     } else
737         printf(" %4.0f%%  ", PartUsed);
738     if (WARN) {
739         printf("  <<WARNING\n");
740     } else
741         printf("\n");
742     return 0;
743 }
744
745 static char *
746 AclToString(struct Acl *acl)
747 {
748     static char mydata[AFS_PIOCTL_MAXSIZE];
749     char tstring[AFS_PIOCTL_MAXSIZE];
750     char dfsstring[30];
751     struct AclEntry *tp;
752
753     if (acl->dfs)
754         sprintf(dfsstring, " dfs:%d %s", acl->dfs, acl->cell);
755     else
756         dfsstring[0] = '\0';
757     sprintf(mydata, "%d%s\n%d\n", acl->nplus, dfsstring, acl->nminus);
758     for (tp = acl->pluslist; tp; tp = tp->next) {
759         sprintf(tstring, "%s %d\n", tp->name, tp->rights);
760         strcat(mydata, tstring);
761     }
762     for (tp = acl->minuslist; tp; tp = tp->next) {
763         sprintf(tstring, "%s %d\n", tp->name, tp->rights);
764         strcat(mydata, tstring);
765     }
766     return mydata;
767 }
768
769 static int
770 SetACLCmd(struct cmd_syndesc *as, void *arock)
771 {
772     afs_int32 code;
773     struct ViceIoctl blob;
774     struct Acl *ta = 0;
775     struct cmd_item *ti, *ui;
776     int plusp;
777     afs_int32 rights;
778     int clear;
779     int idf = getidf(as, parm_setacl_id);
780     int error = 0;
781
782     if (as->parms[2].items)
783         clear = 1;
784     else
785         clear = 0;
786     plusp = !(as->parms[3].items);
787     for (ti = as->parms[0].items; ti; ti = ti->next) {
788         blob.out_size = AFS_PIOCTL_MAXSIZE;
789         blob.in_size = idf;
790         blob.in = blob.out = space;
791         code = pioctl(ti->data, VIOCGETAL, &blob, 1);
792         if (code) {
793             Die(errno, ti->data);
794             error = 1;
795             continue;
796         }
797
798         if (ta)
799             ZapAcl(ta);
800         ta = ParseAcl(space);
801         if (!plusp && ta->dfs) {
802             fprintf(stderr,
803                     "%s: %s: you may not use the -negative switch with DFS acl's.\n%s",
804                     pn, ti->data,
805                     "(you may specify \"null\" to revoke all rights, however)\n");
806             error = 1;
807             continue;
808         }
809
810         if (ta)
811             ZapAcl(ta);
812         if (clear)
813             ta = EmptyAcl(space);
814         else
815             ta = ParseAcl(space);
816         CleanAcl(ta, ti->data);
817         for (ui = as->parms[1].items; ui; ui = ui->next->next) {
818             enum rtype rtype;
819             if (!ui->next) {
820                 fprintf(stderr,
821                         "%s: Missing second half of user/access pair.\n", pn);
822                 ZapAcl(ta);
823                 return 1;
824             }
825             rights = Convert(ui->next->data, ta->dfs, &rtype);
826             if (rtype == destroy && !ta->dfs) {
827                 struct AclEntry *tlist;
828
829                 tlist = (plusp ? ta->pluslist : ta->minuslist);
830                 if (!FindList(tlist, ui->data))
831                     continue;
832             }
833             if (rtype == deny && !ta->dfs)
834                 plusp = 0;
835             if (rtype == destroy && ta->dfs)
836                 rights = -1;
837             ChangeList(ta, plusp, ui->data, rights);
838         }
839         blob.in = AclToString(ta);
840         blob.out_size = 0;
841         blob.in_size = 1 + strlen(blob.in);
842         code = pioctl(ti->data, VIOCSETAL, &blob, 1);
843         if (code) {
844             if (errno == EINVAL) {
845                 if (ta->dfs) {
846                     static char *fsenv = 0;
847                     if (!fsenv) {
848                         fsenv = (char *)getenv("FS_EXPERT");
849                     }
850                     fprintf(stderr,
851                             "%s: \"Invalid argument\" was returned when you tried to store a DFS access list.\n",
852                             pn);
853                     if (!fsenv) {
854                         fprintf(stderr,
855                                 "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
856                                 "\nPossible reasons for this include:\n\n",
857                                 " -You may have specified an inappropriate combination of rights.\n",
858                                 "  For example, some DFS-supported filesystems may not allow you to\n",
859                                 "  drop the \"c\" right from \"user_obj\".\n\n",
860                                 " -A mask_obj may be required (it is likely required by the underlying\n",
861                                 "  filesystem if you try to set anything other than the basic \"user_obj\"\n",
862                                 "  \"mask_obj\", or \"group_obj\" entries). Unlike acl_edit, the fs command\n",
863                                 "  does not automatically create or update the mask_obj. Try setting\n",
864                                 "  the rights \"mask_obj all\" with \"fs sa\" before adding any explicit\n",
865                                 "  users or groups. You can do this with a single command, such as\n",
866                                 "  \"fs sa mask_obj all user:somename read\"\n\n",
867                                 " -A specified user or group may not exist.\n\n",
868                                 " -You may have tried to delete \"user_obj\", \"group_obj\", or \"other_obj\".\n",
869                                 "  This is probably not allowed by the underlying file system.\n\n",
870                                 " -If you add a user or group to a DFS ACL, remember that it must be\n",
871                                 "  fully specified as \"user:username\" or \"group:groupname\". In addition, there\n",
872                                 "  may be local requirements on the format of the user or group name.\n",
873                                 "  Check with your cell administrator.\n\n",
874                                 " -Or numerous other possibilities. It would be great if we could be more\n",
875                                 "  precise about the actual problem, but for various reasons, this is\n",
876                                 "  impractical via this interface.  If you can't figure it out, you\n",
877                                 "  might try logging into a DCE-equipped machine and use acl_edit (or\n",
878                                 "  whatever is provided). You may get better results. Good luck!\n\n",
879                                 " (You may inhibit this message by setting \"FS_EXPERT\" in your environment)\n");
880                     }
881                 } else {
882                     fprintf(stderr,
883                             "%s: Invalid argument, possible reasons include:\n",
884                             pn);
885                     fprintf(stderr, "\t-File not in AFS\n");
886                     fprintf(stderr,
887                             "\t-Too many users on access control list\n");
888                     fprintf(stderr,
889                             "\t-Tried to add non-existent user to access control list\n");
890                 }
891             } else {
892                 Die(errno, ti->data);
893             }
894             error = 1;
895         }
896     }
897     if (ta)
898         ZapAcl(ta);
899     return error;
900 }
901
902
903 static int
904 CopyACLCmd(struct cmd_syndesc *as, void *arock)
905 {
906     afs_int32 code;
907     struct ViceIoctl blob;
908     struct Acl *fa, *ta = 0;
909     struct AclEntry *tp;
910     struct cmd_item *ti;
911     int clear;
912     int idf = getidf(as, parm_copyacl_id);
913     int error = 0;
914
915     if (as->parms[2].items)
916         clear = 1;
917     else
918         clear = 0;
919     blob.out_size = AFS_PIOCTL_MAXSIZE;
920     blob.in_size = idf;
921     blob.in = blob.out = space;
922     code = pioctl(as->parms[0].items->data, VIOCGETAL, &blob, 1);
923     if (code) {
924         Die(errno, as->parms[0].items->data);
925         return 1;
926     }
927     fa = ParseAcl(space);
928     CleanAcl(fa, as->parms[0].items->data);
929     for (ti = as->parms[1].items; ti; ti = ti->next) {
930         blob.out_size = AFS_PIOCTL_MAXSIZE;
931         blob.in_size = idf;
932         blob.in = blob.out = space;
933         code = pioctl(ti->data, VIOCGETAL, &blob, 1);
934         if (code) {
935             Die(errno, ti->data);
936             error = 1;
937             continue;
938         }
939
940         if (ta)
941             ZapAcl(ta);
942         if (clear)
943             ta = EmptyAcl(space);
944         else
945             ta = ParseAcl(space);
946         CleanAcl(ta, ti->data);
947         if (ta->dfs != fa->dfs) {
948             fprintf(stderr,
949                     "%s: incompatible file system types: acl not copied to %s; aborted\n",
950                     pn, ti->data);
951             error = 1;
952             continue;
953         }
954         if (ta->dfs) {
955             if (!clear && strcmp(ta->cell, fa->cell) != 0) {
956                 fprintf(stderr,
957                         "%s: default DCE cell differs for file %s: use \"-clear\" switch; acl not merged\n",
958                         pn, ti->data);
959                 error = 1;
960                 continue;
961             }
962             strcpy(ta->cell, fa->cell);
963         }
964         for (tp = fa->pluslist; tp; tp = tp->next)
965             ChangeList(ta, 1, tp->name, tp->rights);
966         for (tp = fa->minuslist; tp; tp = tp->next)
967             ChangeList(ta, 0, tp->name, tp->rights);
968         blob.in = AclToString(ta);
969         blob.out_size = 0;
970         blob.in_size = 1 + strlen(blob.in);
971         code = pioctl(ti->data, VIOCSETAL, &blob, 1);
972         if (code) {
973             if (errno == EINVAL) {
974                 fprintf(stderr,
975                         "%s: Invalid argument, possible reasons include:\n",
976                         pn);
977                 fprintf(stderr, "\t-File not in AFS\n");
978             } else {
979                 Die(errno, ti->data);
980             }
981             error = 1;
982         }
983     }
984     if (ta)
985         ZapAcl(ta);
986     ZapAcl(fa);
987     return error;
988 }
989
990 /* pioctl() call to get the cellname of a pathname */
991 static afs_int32
992 GetCell(char *fname, char *cellname)
993 {
994     afs_int32 code;
995     struct ViceIoctl blob;
996
997     blob.in_size = 0;
998     blob.out_size = MAXCELLCHARS;
999     blob.out = cellname;
1000
1001     code = pioctl(fname, VIOC_FILE_CELL_NAME, &blob, 1);
1002     return code ? errno : 0;
1003 }
1004
1005 /* Check if a username is valid: If it contains only digits (or a
1006  * negative sign), then it might be bad. We then query the ptserver
1007  * to see.
1008  */
1009 static int
1010 BadName(char *aname, char *fname)
1011 {
1012     afs_int32 tc, code, id;
1013     char *nm;
1014     char cell[MAXCELLCHARS];
1015
1016     for (nm = aname; (tc = *nm); nm++) {
1017         /* all must be '-' or digit to be bad */
1018         if (tc != '-' && (tc < '0' || tc > '9'))
1019             return 0;
1020     }
1021
1022     /* Go to the PRDB and see if this all number username is valid */
1023     code = GetCell(fname, cell);
1024     if (code)
1025         return 0;
1026
1027     pr_Initialize(1, AFSDIR_CLIENT_ETC_DIRPATH, cell);
1028     code = pr_SNameToId(aname, &id);
1029     pr_End();
1030
1031     /* 1=>Not-valid; 0=>Valid */
1032     return ((!code && (id == ANONYMOUSID)) ? 1 : 0);
1033 }
1034
1035
1036 /* clean up an access control list of its bad entries; return 1 if we made
1037    any changes to the list, and 0 otherwise */
1038 static int
1039 CleanAcl(struct Acl *aa, char *fname)
1040 {
1041     struct AclEntry *te, **le, *ne;
1042     int changes;
1043
1044     /* Don't correct DFS ACL's for now */
1045     if (aa->dfs)
1046         return 0;
1047
1048     /* prune out bad entries */
1049     changes = 0;                /* count deleted entries */
1050     le = &aa->pluslist;
1051     for (te = aa->pluslist; te; te = ne) {
1052         ne = te->next;
1053         if (BadName(te->name, fname)) {
1054             /* zap this dude */
1055             *le = te->next;
1056             aa->nplus--;
1057             free(te);
1058             changes++;
1059         } else {
1060             le = &te->next;
1061         }
1062     }
1063     le = &aa->minuslist;
1064     for (te = aa->minuslist; te; te = ne) {
1065         ne = te->next;
1066         if (BadName(te->name, fname)) {
1067             /* zap this dude */
1068             *le = te->next;
1069             aa->nminus--;
1070             free(te);
1071             changes++;
1072         } else {
1073             le = &te->next;
1074         }
1075     }
1076     return changes;
1077 }
1078
1079
1080 /* clean up an acl to not have bogus entries */
1081 static int
1082 CleanACLCmd(struct cmd_syndesc *as, void *arock)
1083 {
1084     afs_int32 code;
1085     struct Acl *ta = 0;
1086     struct ViceIoctl blob;
1087     int changes;
1088     struct cmd_item *ti;
1089     struct AclEntry *te;
1090     int error = 0;
1091
1092     SetDotDefault(&as->parms[0].items);
1093     for (ti = as->parms[0].items; ti; ti = ti->next) {
1094         blob.out_size = AFS_PIOCTL_MAXSIZE;
1095         blob.in_size = 0;
1096         blob.out = space;
1097         code = pioctl(ti->data, VIOCGETAL, &blob, 1);
1098         if (code) {
1099             Die(errno, ti->data);
1100             error = 1;
1101             continue;
1102         }
1103
1104         if (ta)
1105             ZapAcl(ta);
1106         ta = ParseAcl(space);
1107         if (ta->dfs) {
1108             fprintf(stderr,
1109                     "%s: cleanacl is not supported for DFS access lists.\n",
1110                     pn);
1111             error = 1;
1112             continue;
1113         }
1114
1115         changes = CleanAcl(ta, ti->data);
1116
1117         if (changes) {
1118             /* now set the acl */
1119             blob.in = AclToString(ta);
1120             blob.in_size = strlen(blob.in) + 1;
1121             blob.out_size = 0;
1122             code = pioctl(ti->data, VIOCSETAL, &blob, 1);
1123             if (code) {
1124                 if (errno == EINVAL) {
1125                     fprintf(stderr,
1126                             "%s: Invalid argument, possible reasons include\n",
1127                             pn);
1128                     fprintf(stderr, "%s: File not in vice or\n", pn);
1129                     fprintf(stderr,
1130                             "%s: Too many users on access control list or\n",
1131                             pn);
1132                 } else {
1133                     Die(errno, ti->data);
1134                 }
1135                 error = 1;
1136                 continue;
1137             }
1138
1139             /* now list the updated acl */
1140             printf("Access list for %s is now\n", ti->data);
1141             if (ta->nplus > 0) {
1142                 if (!ta->dfs)
1143                     printf("Normal rights:\n");
1144                 for (te = ta->pluslist; te; te = te->next) {
1145                     printf("  %s ", te->name);
1146                     PRights(te->rights, ta->dfs);
1147                     printf("\n");
1148                 }
1149             }
1150             if (ta->nminus > 0) {
1151                 printf("Negative rights:\n");
1152                 for (te = ta->minuslist; te; te = te->next) {
1153                     printf("  %s ", te->name);
1154                     PRights(te->rights, ta->dfs);
1155                     printf("\n");
1156                 }
1157             }
1158             if (ti->next)
1159                 printf("\n");
1160         } else
1161             printf("Access list for %s is fine.\n", ti->data);
1162     }
1163     if (ta)
1164         ZapAcl(ta);
1165     return error;
1166 }
1167
1168 static int
1169 ListACLCmd(struct cmd_syndesc *as, void *arock)
1170 {
1171     afs_int32 code;
1172     struct Acl *ta;
1173     struct ViceIoctl blob;
1174     struct AclEntry *te;
1175     struct cmd_item *ti;
1176     int idf = getidf(as, parm_listacl_id);
1177     int error = 0;
1178
1179     SetDotDefault(&as->parms[0].items);
1180     for (ti = as->parms[0].items; ti; ti = ti->next) {
1181         blob.out_size = AFS_PIOCTL_MAXSIZE;
1182         blob.in_size = idf;
1183         blob.in = blob.out = space;
1184         code = pioctl(ti->data, VIOCGETAL, &blob, 1);
1185         if (code) {
1186             Die(errno, ti->data);
1187             error = 1;
1188             continue;
1189         }
1190         ta = ParseAcl(space);
1191         if (as->parms[3].items) {                       /* -cmd */
1192             printf("fs setacl -dir %s -acl ", ti->data);
1193             if (ta->nplus > 0) {
1194                 for (te = ta->pluslist; te; te = te->next) {
1195                     printf("  %s ", te->name);
1196                     PRights(te->rights, ta->dfs);
1197                 }
1198             }
1199             printf("\n");
1200             if (ta->nminus > 0) {
1201                 printf("fs setacl -dir %s -acl ", ti->data);
1202                 for (te = ta->minuslist; te; te = te->next) {
1203                     printf("  %s ", te->name);
1204                     PRights(te->rights, ta->dfs);
1205                 }
1206                 printf(" -negative\n");
1207             }
1208         } else {
1209             switch (ta->dfs) {
1210             case 0:
1211                 printf("Access list for %s is\n", ti->data);
1212                 break;
1213             case 1:
1214                 printf("DFS access list for %s is\n", ti->data);
1215                 break;
1216             case 2:
1217                 printf("DFS initial directory access list of %s is\n", ti->data);
1218                 break;
1219             case 3:
1220                 printf("DFS initial file access list of %s is\n", ti->data);
1221                 break;
1222             }
1223             if (ta->dfs) {
1224                 printf("  Default cell = %s\n", ta->cell);
1225             }
1226             if (ta->nplus > 0) {
1227                 if (!ta->dfs)
1228                     printf("Normal rights:\n");
1229                 for (te = ta->pluslist; te; te = te->next) {
1230                     printf("  %s ", te->name);
1231                     PRights(te->rights, ta->dfs);
1232                     printf("\n");
1233                 }
1234             }
1235             if (ta->nminus > 0) {
1236                 printf("Negative rights:\n");
1237                 for (te = ta->minuslist; te; te = te->next) {
1238                     printf("  %s ", te->name);
1239                     PRights(te->rights, ta->dfs);
1240                     printf("\n");
1241                 }
1242             }
1243             if (ti->next)
1244                 printf("\n");
1245         }
1246         ZapAcl(ta);
1247     }
1248     return error;
1249 }
1250
1251 static int
1252 GetCallerAccess(struct cmd_syndesc *as, void *arock)
1253 {
1254     struct cmd_item *ti;
1255     int error = 0;
1256
1257     SetDotDefault(&as->parms[0].items);
1258     for (ti = as->parms[0].items; ti; ti = ti->next) {
1259         afs_int32 code;
1260         struct ViceIoctl blob;
1261         struct vcxstat2 stat;
1262         blob.out_size = sizeof(struct vcxstat2);
1263         blob.in_size = 0;
1264         blob.out = (void *)&stat;
1265         code = pioctl(ti->data, VIOC_GETVCXSTATUS2, &blob, 1);
1266         if (code) {
1267             Die(errno, ti->data);
1268             error = 1;
1269             continue;
1270         }
1271         printf("Callers access to %s is ", ti->data);
1272         PRights(stat.callerAccess, 0);
1273         printf("\n");
1274     }
1275     return error;
1276 }
1277
1278 static int
1279 FlushVolumeCmd(struct cmd_syndesc *as, void *arock)
1280 {
1281     afs_int32 code;
1282     struct ViceIoctl blob;
1283     struct cmd_item *ti;
1284     int error = 0;
1285
1286     SetDotDefault(&as->parms[0].items);
1287     for (ti = as->parms[0].items; ti; ti = ti->next) {
1288         blob.in_size = blob.out_size = 0;
1289         code = pioctl(ti->data, VIOC_FLUSHVOLUME, &blob, 0);
1290         if (code) {
1291             fprintf(stderr, "Error flushing volume ");
1292             perror(ti->data);
1293             error = 1;
1294             continue;
1295         }
1296     }
1297     return error;
1298 }
1299
1300 /*
1301  * The Windows version of UuidCmd displays the UUID.
1302  * When the UNIX version is updated to do the same
1303  * be sure to replace the CMD_REQUIRED flag with
1304  * CMD_OPTIONAL in the cmd_AddParam(-generate) call
1305  */
1306 static int
1307 UuidCmd(struct cmd_syndesc *as, void *arock)
1308 {
1309     afs_int32 code;
1310     struct ViceIoctl blob;
1311
1312     blob.in_size = 0;
1313     blob.out_size = 0;
1314
1315     if (as->parms[0].items) {
1316         if (geteuid()) {
1317             fprintf (stderr, "Permission denied: requires root access.\n");
1318             return EACCES;
1319         }
1320
1321         /* generate new UUID */
1322         code = pioctl(0, VIOC_NEWUUID, &blob, 1);
1323
1324         if (code) {
1325             Die(errno, 0);
1326             return 1;
1327         }
1328
1329         printf("New uuid generated.\n");
1330     } else {
1331         /* This will never execute */
1332         printf("Please add the '-generate' option to generate a new UUID.\n");
1333     }
1334     return 0;
1335 }
1336
1337 #if defined(AFS_CACHE_BYPASS)
1338 /*
1339  * Set cache-bypass threshold.  Files larger than this size will not be cached.
1340  * With a threshold of 0, the cache is always bypassed.  With a threshold of -1,
1341  * cache bypass is disabled.
1342  */
1343
1344 static int
1345 BypassThresholdCmd(struct cmd_syndesc *as, void *arock)
1346 {
1347     afs_int32 code;
1348     struct ViceIoctl blob;
1349     afs_int32 threshold_i, threshold_o;
1350     char *tp;
1351
1352     /* if new threshold supplied, then set and confirm, else,
1353      * get current threshold and print
1354      */
1355
1356     if(as->parms[0].items) {
1357         int digit, ix, len;
1358
1359         tp = as->parms[0].items->data;
1360         len = strlen(tp);
1361         digit = 1;
1362         for(ix = 0; ix < len; ++ix) {
1363             if(!isdigit(tp[0])) {
1364                 digit = 0;
1365                 break;
1366             }
1367         }
1368         if (digit == 0) {
1369             fprintf(stderr, "fs bypassthreshold -size: %s must be an undecorated digit string.\n", tp);
1370             return EINVAL;
1371         }
1372         threshold_i = atoi(tp);
1373         if(ix > 9 && threshold_i < 2147483647)
1374             threshold_i = 2147483647;
1375         blob.in = (char *) &threshold_i;
1376         blob.in_size = sizeof(threshold_i);
1377     } else {
1378         blob.in = NULL;
1379         blob.in_size = 0;
1380     }
1381
1382     blob.out = (char *) &threshold_o;
1383     blob.out_size = sizeof(threshold_o);
1384     code = pioctl(0, VIOC_SETBYPASS_THRESH, &blob, 1);
1385     if (code) {
1386         Die(errno, NULL);
1387         return 1;
1388     } else {
1389         printf("Cache bypass threshold %d", threshold_o);
1390         if(threshold_o ==  -1)
1391             printf(" (disabled)");
1392         printf("\n");
1393     }
1394
1395     return 0;
1396 }
1397
1398 #endif
1399
1400 static int
1401 FlushCmd(struct cmd_syndesc *as, void *arock)
1402 {
1403     afs_int32 code;
1404     struct ViceIoctl blob;
1405     struct cmd_item *ti;
1406     int error = 0;
1407
1408     for (ti = as->parms[0].items; ti; ti = ti->next) {
1409         blob.in_size = blob.out_size = 0;
1410         code = pioctl(ti->data, VIOCFLUSH, &blob, 0);
1411         if (code) {
1412             if (errno == EMFILE) {
1413                 fprintf(stderr, "%s: Can't flush active file %s\n", pn,
1414                         ti->data);
1415             } else {
1416                 fprintf(stderr, "%s: Error flushing file ", pn);
1417                 perror(ti->data);
1418             }
1419             error = 1;
1420             continue;
1421         }
1422     }
1423     return error;
1424 }
1425
1426 /* all this command does is repackage its args and call SetVolCmd */
1427 static int
1428 SetQuotaCmd(struct cmd_syndesc *as, void *arock)
1429 {
1430     struct cmd_syndesc ts;
1431
1432     /* copy useful stuff from our command slot; we may later have to reorder */
1433     memcpy(&ts, as, sizeof(ts));        /* copy whole thing */
1434     return SetVolCmd(&ts, arock);
1435 }
1436
1437 static int
1438 SetVolCmd(struct cmd_syndesc *as, void *arock)
1439 {
1440     afs_int32 code;
1441     struct ViceIoctl blob;
1442     struct cmd_item *ti;
1443     struct VolumeStatus *status;
1444     char *offmsg, *input;
1445     int error = 0;
1446
1447     SetDotDefault(&as->parms[0].items);
1448     for (ti = as->parms[0].items; ti; ti = ti->next) {
1449         /* once per file */
1450         blob.out_size = AFS_PIOCTL_MAXSIZE;
1451         blob.in_size = sizeof(*status) + 3;     /* for the three terminating nulls */
1452         blob.out = space;
1453         blob.in = space;
1454         status = (VolumeStatus *) space;
1455         status->MinQuota = status->MaxQuota = -1;
1456         offmsg = NULL;
1457         if (as->parms[1].items) {
1458             code = util_GetHumanInt32(as->parms[1].items->data, &status->MaxQuota);
1459             if (code) {
1460                 fprintf(stderr, "%s: bad integer specified for quota.\n", pn);
1461                 error = 1;
1462                 continue;
1463             }
1464         }
1465         if (as->parms[2].items)
1466             offmsg = as->parms[2].items->data;
1467         input = (char *)status + sizeof(*status);
1468         *(input++) = '\0';      /* never set name: this call doesn't change vldb */
1469         if (offmsg) {
1470             if (strlen(offmsg) >= VMSGSIZE) {
1471                 fprintf(stderr,
1472                         "%s: message must be shorter than %d characters\n",
1473                         pn, VMSGSIZE);
1474                 error = 1;
1475                 continue;
1476             }
1477             strcpy(input, offmsg);
1478             blob.in_size += strlen(offmsg);
1479             input += strlen(offmsg) + 1;
1480         } else
1481             *(input++) = '\0';
1482         *(input++) = '\0';      /* Pad for old style volume "motd" */
1483         code = pioctl(ti->data, VIOCSETVOLSTAT, &blob, 1);
1484         if (code) {
1485             Die(errno, ti->data);
1486             error = 1;
1487         }
1488     }
1489     return error;
1490 }
1491
1492 /*
1493  * Why is VenusFid declared in the kernel-only section of afs.h,
1494  * if it's the exported interface of the cache manager?
1495  */
1496 struct VenusFid {
1497     afs_int32 Cell;
1498     AFSFid Fid;
1499 };
1500
1501 static int
1502 ExamineCmd(struct cmd_syndesc *as, void *arock)
1503 {
1504     afs_int32 code;
1505     struct ViceIoctl blob;
1506     struct cmd_item *ti;
1507     struct VolumeStatus *status;
1508     char *name, *offmsg;
1509     int error = 0;
1510
1511     SetDotDefault(&as->parms[0].items);
1512     for (ti = as->parms[0].items; ti; ti = ti->next) {
1513         struct VenusFid vfid;
1514
1515         /* once per file */
1516         blob.out_size = AFS_PIOCTL_MAXSIZE;
1517         blob.in_size = 0;
1518         blob.out = space;
1519         code = pioctl(ti->data, VIOCGETVOLSTAT, &blob, 1);
1520         if (code) {
1521             Die(errno, ti->data);
1522             error = 1;
1523             continue;
1524         }
1525         status = (VolumeStatus *) space;
1526         name = (char *)status + sizeof(*status);
1527         offmsg = name + strlen(name) + 1;
1528
1529         blob.out_size = sizeof(struct VenusFid);
1530         blob.out = (char *) &vfid;
1531         if (0 == pioctl(ti->data, VIOCGETFID, &blob, 1)) {
1532             printf("File %s (%u.%u.%u) contained in volume %u\n",
1533                    ti->data, vfid.Fid.Volume, vfid.Fid.Vnode, vfid.Fid.Unique,
1534                    vfid.Fid.Volume);
1535         }
1536
1537         PrintStatus(status, name, offmsg);
1538     }
1539     return error;
1540 }
1541
1542 static int
1543 ListQuotaCmd(struct cmd_syndesc *as, void *arock)
1544 {
1545     afs_int32 code;
1546     struct ViceIoctl blob;
1547     struct cmd_item *ti;
1548     struct VolumeStatus *status;
1549     char *name;
1550     int error = 0;
1551     int human = 0;
1552
1553     if (as->parms[1].items)
1554         human = 1;
1555
1556     printf("%-25s%-11s%-11s%-7s%-11s\n", "Volume Name", "      Quota",
1557            "       Used", " %Used", "  Partition");
1558     SetDotDefault(&as->parms[0].items);
1559     for (ti = as->parms[0].items; ti; ti = ti->next) {
1560         /* once per file */
1561         blob.out_size = AFS_PIOCTL_MAXSIZE;
1562         blob.in_size = 0;
1563         blob.out = space;
1564         code = pioctl(ti->data, VIOCGETVOLSTAT, &blob, 1);
1565         if (code) {
1566             Die(errno, ti->data);
1567             error = 1;
1568             continue;
1569         }
1570         status = (VolumeStatus *) space;
1571         name = (char *)status + sizeof(*status);
1572         QuickPrintStatus(status, name, human);
1573     }
1574     return error;
1575 }
1576
1577 static int
1578 WhereIsCmd(struct cmd_syndesc *as, void *arock)
1579 {
1580     afs_int32 code;
1581     struct ViceIoctl blob;
1582     struct cmd_item *ti;
1583     int j;
1584     afs_int32 *hosts;
1585     char *tp;
1586     int error = 0;
1587
1588     SetDotDefault(&as->parms[0].items);
1589     for (ti = as->parms[0].items; ti; ti = ti->next) {
1590         /* once per file */
1591         blob.out_size = AFS_PIOCTL_MAXSIZE;
1592         blob.in_size = 0;
1593         blob.out = space;
1594         memset(space, 0, sizeof(space));
1595         code = pioctl(ti->data, VIOCWHEREIS, &blob, 1);
1596         if (code) {
1597             Die(errno, ti->data);
1598             error = 1;
1599             continue;
1600         }
1601         hosts = (afs_int32 *) space;
1602         printf("File %s is on host%s ", ti->data,
1603                (hosts[0] && !hosts[1]) ? "" : "s");
1604         for (j = 0; j < AFS_MAXHOSTS; j++) {
1605             if (hosts[j] == 0)
1606                 break;
1607             tp = hostutil_GetNameByINet(hosts[j]);
1608             printf("%s ", tp);
1609         }
1610         printf("\n");
1611     }
1612     return error;
1613 }
1614
1615
1616 static int
1617 DiskFreeCmd(struct cmd_syndesc *as, void *arock)
1618 {
1619     afs_int32 code;
1620     struct ViceIoctl blob;
1621     struct cmd_item *ti;
1622     char *name;
1623     struct VolumeStatus *status;
1624     int error = 0;
1625     int human = 0;
1626
1627     if (as->parms[1].items)
1628         human = 1;
1629
1630     printf("%-25s%10s%10s%10s%6s\n", "Volume Name",
1631            human ? "total" : "kbytes", "used", "avail", "%used");
1632     SetDotDefault(&as->parms[0].items);
1633     for (ti = as->parms[0].items; ti; ti = ti->next) {
1634         /* once per file */
1635         blob.out_size = AFS_PIOCTL_MAXSIZE;
1636         blob.in_size = 0;
1637         blob.out = space;
1638         code = pioctl(ti->data, VIOCGETVOLSTAT, &blob, 1);
1639         if (code) {
1640             Die(errno, ti->data);
1641             error = 1;
1642             continue;
1643         }
1644         status = (VolumeStatus *) space;
1645         name = (char *)status + sizeof(*status);
1646         QuickPrintSpace(status, name, human);
1647     }
1648     return error;
1649 }
1650
1651 static int
1652 QuotaCmd(struct cmd_syndesc *as, void *arock)
1653 {
1654     afs_int32 code;
1655     struct ViceIoctl blob;
1656     struct cmd_item *ti;
1657     double quotaPct;
1658     struct VolumeStatus *status;
1659     int error = 0;
1660
1661     SetDotDefault(&as->parms[0].items);
1662     for (ti = as->parms[0].items; ti; ti = ti->next) {
1663         /* once per file */
1664         blob.out_size = AFS_PIOCTL_MAXSIZE;
1665         blob.in_size = 0;
1666         blob.out = space;
1667         code = pioctl(ti->data, VIOCGETVOLSTAT, &blob, 1);
1668         if (code) {
1669             Die(errno, ti->data);
1670             error = 1;
1671             continue;
1672         }
1673         status = (VolumeStatus *) space;
1674         if (status->MaxQuota)
1675             quotaPct =
1676                 ((((double)status->BlocksInUse) / status->MaxQuota) * 100.0);
1677         else
1678             quotaPct = 0.0;
1679         printf("%2.0f%% of quota used.\n", quotaPct);
1680     }
1681     return error;
1682 }
1683
1684 static int
1685 ListMountCmd(struct cmd_syndesc *as, void *arock)
1686 {
1687     afs_int32 code;
1688     struct ViceIoctl blob;
1689     struct cmd_item *ti;
1690     char orig_name[1024];       /*Original name, may be modified */
1691     char true_name[1024];       /*``True'' dirname (e.g., symlink target) */
1692     char parent_dir[1024];      /*Parent directory of true name */
1693     char *last_component;       /*Last component of true name */
1694     struct stat statbuff;       /*Buffer for status info */
1695     int link_chars_read;        /*Num chars read in readlink() */
1696     int thru_symlink;           /*Did we get to a mount point via a symlink? */
1697     int error = 0;
1698
1699     for (ti = as->parms[0].items; ti; ti = ti->next) {
1700         /* once per file */
1701         thru_symlink = 0;
1702         sprintf(orig_name, "%s%s", (ti->data[0] == '/') ? "" : "./",
1703                 ti->data);
1704
1705         if (lstat(orig_name, &statbuff) < 0) {
1706             /* if lstat fails, we should still try the pioctl, since it
1707              * may work (for example, lstat will fail, but pioctl will
1708              * work if the volume of offline (returning ENODEV). */
1709             statbuff.st_mode = S_IFDIR; /* lie like pros */
1710         }
1711
1712         /*
1713          * The lstat succeeded.  If the given file is a symlink, substitute
1714          * the file name with the link name.
1715          */
1716         if ((statbuff.st_mode & S_IFMT) == S_IFLNK) {
1717             thru_symlink = 1;
1718             /*
1719              * Read name of resolved file.
1720              */
1721             link_chars_read = readlink(orig_name, true_name, 1024);
1722             if (link_chars_read <= 0) {
1723                 fprintf(stderr,
1724                         "%s: Can't read target name for '%s' symbolic link!\n",
1725                         pn, orig_name);
1726                 error = 1;
1727                 continue;
1728             }
1729
1730             /*
1731              * Add a trailing null to what was read, bump the length.
1732              */
1733             true_name[link_chars_read++] = 0;
1734
1735             /*
1736              * If the symlink is an absolute pathname, we're fine.  Otherwise, we
1737              * have to create a full pathname using the original name and the
1738              * relative symlink name.  Find the rightmost slash in the original
1739              * name (we know there is one) and splice in the symlink value.
1740              */
1741             if (true_name[0] != '/') {
1742                 last_component = (char *)strrchr(orig_name, '/');
1743                 strcpy(++last_component, true_name);
1744                 strcpy(true_name, orig_name);
1745             }
1746         } else
1747             strcpy(true_name, orig_name);
1748
1749         /*
1750          * Find rightmost slash, if any.
1751          */
1752         last_component = (char *)strrchr(true_name, '/');
1753         if (last_component == (char *)true_name) {
1754             strcpy(parent_dir, "/");
1755             last_component++;
1756         }
1757         else if (last_component != (char *)NULL) {
1758             /*
1759              * Found it.  Designate everything before it as the parent directory,
1760              * everything after it as the final component.
1761              */
1762             strncpy(parent_dir, true_name, last_component - true_name);
1763             parent_dir[last_component - true_name] = 0;
1764             last_component++;   /*Skip the slash */
1765         } else {
1766             /*
1767              * No slash appears in the given file name.  Set parent_dir to the current
1768              * directory, and the last component as the given name.
1769              */
1770             strcpy(parent_dir, ".");
1771             last_component = true_name;
1772         }
1773
1774         if (strcmp(last_component, ".") == 0
1775             || strcmp(last_component, "..") == 0) {
1776             fprintf(stderr,
1777                     "%s: you may not use '.' or '..' as the last component\n",
1778                     pn);
1779             fprintf(stderr, "%s: of a name in the 'fs lsmount' command.\n",
1780                     pn);
1781             error = 1;
1782             continue;
1783         }
1784
1785         blob.in = last_component;
1786         blob.in_size = strlen(last_component) + 1;
1787         blob.out_size = AFS_PIOCTL_MAXSIZE;
1788         blob.out = space;
1789         memset(space, 0, AFS_PIOCTL_MAXSIZE);
1790
1791         code = pioctl(parent_dir, VIOC_AFS_STAT_MT_PT, &blob, 1);
1792
1793         if (code == 0) {
1794             printf("'%s' is a %smount point for volume '%s'\n", ti->data,
1795                    (thru_symlink ? "symbolic link, leading to a " : ""),
1796                    space);
1797         } else {
1798             if (errno == EINVAL) {
1799                 fprintf(stderr, "'%s' is not a mount point.\n", ti->data);
1800             } else {
1801                 Die(errno, (ti->data ? ti->data : parent_dir));
1802             }
1803             error = 1;
1804         }
1805     }
1806     return error;
1807 }
1808
1809 static int
1810 MakeMountCmd(struct cmd_syndesc *as, void *arock)
1811 {
1812     afs_int32 code;
1813     char *cellName, *volName, *tmpName;
1814     struct afsconf_cell info;
1815     struct vldbentry vldbEntry;
1816     struct ViceIoctl blob;
1817
1818 /*
1819
1820 defect #3069
1821
1822     if (as->parms[5].items && !as->parms[2].items) {
1823         fprintf(stderr, "%s: must provide cell when creating cellular mount point.\n", pn);
1824         return 1;
1825     }
1826 */
1827
1828     if (as->parms[2].items)     /* cell name specified */
1829         cellName = as->parms[2].items->data;
1830     else
1831         cellName = NULL;
1832     volName = as->parms[1].items->data;
1833
1834     if (strlen(volName) >= 64) {
1835         fprintf(stderr,
1836                 "%s: volume name too long (length must be < 64 characters)\n",
1837                 pn);
1838         return 1;
1839     }
1840
1841     /* Check for a cellname in the volume specification, and complain
1842      * if it doesn't match what was specified with -cell */
1843     if ((tmpName = strchr(volName, ':'))) {
1844         *tmpName = '\0';
1845         if (cellName) {
1846             if (strcasecmp(cellName, volName)) {
1847                 fprintf(stderr, "%s: cellnames do not match.\n", pn);
1848                 return 1;
1849             }
1850         }
1851         cellName = volName;
1852         volName = ++tmpName;
1853     }
1854
1855     if (!InAFS(Parent(as->parms[0].items->data))) {
1856         fprintf(stderr,
1857                 "%s: mount points must be created within the AFS file system\n",
1858                 pn);
1859         return 1;
1860     }
1861
1862     if (!cellName) {
1863         blob.in_size = 0;
1864         blob.out_size = AFS_PIOCTL_MAXSIZE;
1865         blob.out = space;
1866         code =
1867             pioctl(Parent(as->parms[0].items->data), VIOC_FILE_CELL_NAME,
1868                    &blob, 1);
1869     }
1870
1871     code = GetCellName(cellName ? cellName : space, &info);
1872     if (code) {
1873         return 1;
1874     }
1875     if (!(as->parms[4].items)) {
1876         /* not fast, check which cell the mountpoint is being created in */
1877         /* not fast, check name with VLDB */
1878         code = VLDBInit(1, &info);
1879         if (code == 0) {
1880             /* make the check.  Don't complain if there are problems with init */
1881             code =
1882                 ubik_VL_GetEntryByNameO(uclient, 0, volName,
1883                           &vldbEntry);
1884             if (code == VL_NOENT) {
1885                 fprintf(stderr,
1886                         "%s: warning, volume %s does not exist in cell %s.\n",
1887                         pn, volName, cellName ? cellName : space);
1888             }
1889         }
1890     }
1891
1892     if (as->parms[3].items)     /* if -rw specified */
1893         strcpy(space, "%");
1894     else
1895         strcpy(space, "#");
1896     if (cellName) {
1897         /* cellular mount point, prepend cell prefix */
1898         strcat(space, info.name);
1899         strcat(space, ":");
1900     }
1901     strcat(space, volName);     /* append volume name */
1902     strcat(space, ".");         /* stupid convention; these end with a period */
1903     code = symlink(space, as->parms[0].items->data);
1904     if (code) {
1905         Die(errno, as->parms[0].items->data);
1906         return 1;
1907     }
1908     return 0;
1909 }
1910
1911 /*
1912  * Delete AFS mount points.  Variables are used as follows:
1913  *      tbuffer: Set to point to the null-terminated directory name of the mount point
1914  *          (or ``.'' if none is provided)
1915  *      tp: Set to point to the actual name of the mount point to nuke.
1916  */
1917 static int
1918 RemoveMountCmd(struct cmd_syndesc *as, void *arock)
1919 {
1920     afs_int32 code = 0;
1921     struct ViceIoctl blob;
1922     struct cmd_item *ti;
1923     char tbuffer[1024];
1924     char lsbuffer[1024];
1925     char *tp;
1926     int error = 0;
1927
1928     for (ti = as->parms[0].items; ti; ti = ti->next) {
1929         /* once per file */
1930         tp = (char *)strrchr(ti->data, '/');
1931         if (tp) {
1932             strncpy(tbuffer, ti->data, code = tp - ti->data);   /* the dir name */
1933             tbuffer[code] = 0;
1934             tp++;               /* skip the slash */
1935         } else {
1936             strcpy(tbuffer, ".");
1937             tp = ti->data;
1938         }
1939         blob.in = tp;
1940         blob.in_size = strlen(tp) + 1;
1941         blob.out = lsbuffer;
1942         blob.out_size = sizeof(lsbuffer);
1943         code = pioctl(tbuffer, VIOC_AFS_STAT_MT_PT, &blob, 1);
1944         if (code) {
1945             if (errno == EINVAL) {
1946                 fprintf(stderr, "%s: '%s' is not a mount point.\n", pn,
1947                         ti->data);
1948             } else {
1949                 Die(errno, ti->data);
1950             }
1951             error = 1;
1952             continue;           /* don't bother trying */
1953         }
1954         blob.out_size = 0;
1955         blob.in = tp;
1956         blob.in_size = strlen(tp) + 1;
1957         code = pioctl(tbuffer, VIOC_AFS_DELETE_MT_PT, &blob, 1);
1958         if (code) {
1959             Die(errno, ti->data);
1960             error = 1;
1961         }
1962     }
1963     return error;
1964 }
1965
1966 /*
1967 */
1968
1969 static int
1970 CheckServersCmd(struct cmd_syndesc *as, void *arock)
1971 {
1972     afs_int32 code;
1973     struct ViceIoctl blob;
1974     afs_int32 j;
1975     afs_int32 temp;
1976     char *tp;
1977     struct afsconf_cell info;
1978     struct chservinfo checkserv;
1979
1980     memset(&checkserv, 0, sizeof(struct chservinfo));
1981     blob.in_size = sizeof(struct chservinfo);
1982     blob.in = (caddr_t) & checkserv;
1983
1984     blob.out_size = AFS_PIOCTL_MAXSIZE;
1985     blob.out = space;
1986     memset(space, 0, sizeof(afs_int32));        /* so we assure zero when nothing is copied back */
1987
1988     /* prepare flags for checkservers command */
1989     temp = 2;                   /* default to checking local cell only */
1990     if (as->parms[2].items)
1991         temp |= 1;              /* set fast flag */
1992     if (as->parms[1].items)
1993         temp &= ~2;             /* turn off local cell check */
1994
1995     checkserv.magic = 0x12345678;       /* XXX */
1996     checkserv.tflags = temp;
1997
1998     /* now copy in optional cell name, if specified */
1999     if (as->parms[0].items) {
2000         code = GetCellName(as->parms[0].items->data, &info);
2001         if (code) {
2002             return 1;
2003         }
2004         strcpy(checkserv.tbuffer, info.name);
2005         checkserv.tsize = strlen(info.name) + 1;
2006     } else {
2007         strcpy(checkserv.tbuffer, "\0");
2008         checkserv.tsize = 0;
2009     }
2010
2011     if (as->parms[3].items) {
2012         checkserv.tinterval = atol(as->parms[3].items->data);
2013
2014         /* sanity check */
2015         if (checkserv.tinterval < 0) {
2016             printf
2017                 ("Warning: The negative -interval is ignored; treated as an inquiry\n");
2018             checkserv.tinterval = 0;
2019         } else if (checkserv.tinterval > 600) {
2020             printf
2021                 ("Warning: The maximum -interval value is 10 mins (600 secs)\n");
2022             checkserv.tinterval = 600;  /* 10 min max interval */
2023         }
2024     } else {
2025         checkserv.tinterval = -1;       /* don't change current interval */
2026     }
2027
2028     code = pioctl(0, VIOCCKSERV, &blob, 1);
2029     if (code) {
2030         if ((errno == EACCES) && (checkserv.tinterval > 0)) {
2031             printf("Must be root to change -interval\n");
2032             return 1;
2033         }
2034         Die(errno, 0);
2035         return 1;
2036     }
2037     memcpy(&temp, space, sizeof(afs_int32));
2038     if (checkserv.tinterval >= 0) {
2039         if (checkserv.tinterval > 0)
2040             printf
2041                 ("The new down server probe interval (%d secs) is now in effect (old interval was %d secs)\n",
2042                  checkserv.tinterval, temp);
2043         else
2044             printf("The current down server probe interval is %d secs\n",
2045                    temp);
2046         return 0;
2047     }
2048     if (temp == 0) {
2049         printf("All servers are running.\n");
2050     } else {
2051         printf
2052             ("These servers unavailable due to network or server problems: ");
2053         for (j = 0;; j++) {
2054             memcpy(&temp, space + j * sizeof(afs_int32), sizeof(afs_int32));
2055             if (temp == 0)
2056                 break;
2057             tp = hostutil_GetNameByINet(temp);
2058             printf(" %s", tp);
2059         }
2060         printf(".\n");
2061         code = 1;               /* XXX */
2062     }
2063     return code;
2064 }
2065
2066 static int
2067 MessagesCmd(struct cmd_syndesc *as, void *arock)
2068 {
2069     afs_int32 code = 0;
2070     struct ViceIoctl blob;
2071     struct gaginfo gagflags;
2072     struct cmd_item *show;
2073
2074     memset(&gagflags, 0, sizeof(struct gaginfo));
2075     blob.in_size = sizeof(struct gaginfo);
2076     blob.in = (caddr_t) & gagflags;
2077     blob.out_size = AFS_PIOCTL_MAXSIZE;
2078     blob.out = space;
2079     memset(space, 0, sizeof(afs_int32));        /* so we assure zero when nothing is copied back */
2080
2081     if ((show = as->parms[0].items)) {
2082         if (!strcasecmp(show->data, "user"))
2083             gagflags.showflags |= GAGUSER;
2084         else if (!strcasecmp(show->data, "console"))
2085             gagflags.showflags |= GAGCONSOLE;
2086         else if (!strcasecmp(show->data, "all"))
2087             gagflags.showflags |= GAGCONSOLE | GAGUSER;
2088         else if (!strcasecmp(show->data, "none"))
2089             /* do nothing */ ;
2090         else {
2091             fprintf(stderr,
2092                     "unrecognized flag %s: must be in {user,console,all,none}\n",
2093                     show->data);
2094             code = EINVAL;
2095         }
2096     }
2097
2098     if (code)
2099         return 1;
2100
2101     code = pioctl(0, VIOC_GAG, &blob, 1);
2102     if (code) {
2103         Die(errno, 0);
2104         return 1;
2105     }
2106
2107     return 0;
2108 }
2109
2110 static int
2111 CheckVolumesCmd(struct cmd_syndesc *as, void *arock)
2112 {
2113     afs_int32 code;
2114     struct ViceIoctl blob;
2115
2116     blob.in_size = 0;
2117     blob.out_size = 0;
2118     code = pioctl(0, VIOCCKBACK, &blob, 1);
2119     if (code) {
2120         Die(errno, 0);
2121         return 1;
2122     }
2123
2124     printf("All volumeID/name mappings checked.\n");
2125     return 0;
2126 }
2127
2128 static int
2129 PreCacheCmd(struct cmd_syndesc *as, void *arock)
2130 {
2131     afs_int32 code;
2132     struct ViceIoctl blob;
2133     afs_int32 temp;
2134
2135     if (!as->parms[0].items && !as->parms[1].items) {
2136         fprintf(stderr, "%s: syntax error in precache cmd.\n", pn);
2137         return 1;
2138     }
2139     if (as->parms[0].items) {
2140         code = util_GetInt32(as->parms[0].items->data, &temp);
2141         if (code) {
2142             fprintf(stderr, "%s: bad integer specified for precache size.\n",
2143                     pn);
2144             return 1;
2145         }
2146     } else
2147         temp = 0;
2148     blob.in = (char *)&temp;
2149     blob.in_size = sizeof(afs_int32);
2150     blob.out_size = 0;
2151     code = pioctl(0, VIOCPRECACHE, &blob, 1);
2152     if (code) {
2153         Die(errno, NULL);
2154         return 1;
2155     }
2156
2157     printf("New precache size set.\n");
2158     return 0;
2159 }
2160
2161 static int
2162 SetCacheSizeCmd(struct cmd_syndesc *as, void *arock)
2163 {
2164     afs_int32 code;
2165     struct ViceIoctl blob;
2166     afs_int32 temp;
2167
2168     if (!as->parms[0].items && !as->parms[1].items) {
2169         fprintf(stderr, "%s: syntax error in setcachesize cmd.\n", pn);
2170         return 1;
2171     }
2172     if (as->parms[0].items) {
2173         code = util_GetHumanInt32(as->parms[0].items->data, &temp);
2174         if (code) {
2175             fprintf(stderr, "%s: bad integer specified for cache size.\n",
2176                     pn);
2177             return 1;
2178         }
2179     } else
2180         temp = 0;
2181     blob.in = (char *)&temp;
2182     blob.in_size = sizeof(afs_int32);
2183     blob.out_size = 0;
2184     code = pioctl(0, VIOCSETCACHESIZE, &blob, 1);
2185     if (code) {
2186         if (errno == EROFS) {
2187             printf
2188                 ("'fs setcache' not allowed on memory cache based cache managers.\n");
2189         } else {
2190             Die(errno, NULL);
2191         }
2192         return 1;
2193     }
2194
2195     printf("New cache size set.\n");
2196     return 0;
2197 }
2198
2199 #define MAXGCSIZE       16
2200 static int
2201 GetCacheParmsCmd(struct cmd_syndesc *as, void *arock)
2202 {
2203     afs_int32 code, filesUsed;
2204     struct ViceIoctl blob;
2205     afs_int32 parms[MAXGCSIZE];
2206     double percentFiles, percentBlocks;
2207     afs_int32 flags = 0;
2208
2209     if (as->parms[0].items){ /* -files */
2210         flags = 1;
2211     } else if (as->parms[1].items){ /* -excessive */
2212         flags = 2;
2213     } else {
2214         flags = 0;
2215     }
2216
2217     memset(parms, '\0', sizeof parms);  /* avoid Purify UMR error */
2218     if (flags){
2219         blob.in = (char *)&flags;
2220         blob.in_size = sizeof(afs_int32);
2221     } else {    /* be backward compatible */
2222         blob.in = NULL;
2223         blob.in_size = 0;
2224     }
2225     blob.out_size = sizeof(parms);
2226     blob.out = (char *)parms;
2227     code = pioctl(0, VIOCGETCACHEPARMS, &blob, 1);
2228     if (code) {
2229         Die(errno, NULL);
2230         return 1;
2231     }
2232
2233     if (!flags){
2234         printf("AFS using %d of the cache's available %d 1K byte blocks.\n",
2235                 parms[1], parms[0]);
2236         if (parms[1] > parms[0])
2237                 printf("[Cache guideline temporarily deliberately exceeded; it will be adjusted down but you may wish to increase the cache size.]\n");
2238         return 0;
2239     }
2240
2241     percentBlocks = ((double)parms[1]/parms[0]) * 100;
2242     printf("AFS using %5.0f%% of cache blocks (%d of %d 1k blocks)\n",
2243            percentBlocks, parms[1], parms[0]);
2244
2245     if (parms[2] == 0)
2246         return 0;
2247
2248     filesUsed = parms[2] - parms[3];
2249     percentFiles = ((double)filesUsed/parms[2]) * 100;
2250     printf("          %5.0f%% of the cache files (%d of %d files)\n",
2251             percentFiles, filesUsed, parms[2]);
2252     if (flags == 2){
2253         printf("        afs_cacheFiles: %10d\n", parms[2]);
2254         printf("        IFFree:         %10d\n", parms[3]);
2255         printf("        IFEverUsed:     %10d\n", parms[4]);
2256         printf("        IFDataMod:      %10d\n", parms[5]);
2257         printf("        IFDirtyPages:   %10d\n", parms[6]);
2258         printf("        IFAnyPages:     %10d\n", parms[7]);
2259         printf("        IFDiscarded:    %10d\n", parms[8]);
2260         printf("        DCentries:  %10d\n", parms[9]);
2261         printf("          0k-   4K: %10d\n", parms[10]);
2262         printf("          4k-  16k: %10d\n", parms[11]);
2263         printf("         16k-  64k: %10d\n", parms[12]);
2264         printf("         64k- 256k: %10d\n", parms[13]);
2265         printf("        256k-   1M: %10d\n", parms[14]);
2266         printf("              >=1M: %10d\n", parms[15]);
2267     }
2268
2269     if (percentBlocks > 90)
2270         printf("[cache size usage over 90%%, consider increasing cache size]\n");
2271     if (percentFiles > 90)
2272         printf("[cache file usage over 90%%, consider increasing '-files' argument to afsd]\n");
2273
2274     return 0;
2275 }
2276
2277 static int
2278 ListCellsCmd(struct cmd_syndesc *as, void *arock)
2279 {
2280     afs_int32 code;
2281     afs_int32 i, j;
2282     char *tp;
2283     struct ViceIoctl blob;
2284     int resolve;
2285
2286     resolve = !(as->parms[0].items);    /* -numeric */
2287
2288     for (i = 0;; i++) {
2289         tp = space;
2290         memcpy(tp, &i, sizeof(afs_int32));
2291         blob.out_size = AFS_PIOCTL_MAXSIZE;
2292         blob.in_size = sizeof(afs_int32);
2293         blob.in = space;
2294         blob.out = space;
2295         code = pioctl(0, VIOCGETCELL, &blob, 1);
2296         if (code < 0) {
2297             if (errno == EDOM)
2298                 break;          /* done with the list */
2299             Die(errno, 0);
2300             return 1;
2301         }
2302         tp = space;
2303         printf("Cell %s on hosts", tp + AFS_MAXCELLHOSTS * sizeof(afs_int32));
2304         for (j = 0; j < AFS_MAXCELLHOSTS; j++) {
2305             afs_int32 addr;
2306             char *name, tbuffer[20];
2307
2308             memcpy(&addr, tp + j * sizeof(afs_int32), sizeof(afs_int32));
2309             if (addr == 0)
2310                 break;
2311
2312             if (resolve) {
2313                 name = hostutil_GetNameByINet(addr);
2314             } else {
2315                 addr = ntohl(addr);
2316                 sprintf(tbuffer, "%d.%d.%d.%d", (addr >> 24) & 0xff,
2317                         (addr >> 16) & 0xff, (addr >> 8) & 0xff, addr & 0xff);
2318                 name = tbuffer;
2319             }
2320             printf(" %s", name);
2321         }
2322         printf(".\n");
2323     }
2324     return 0;
2325 }
2326
2327 static int
2328 ListAliasesCmd(struct cmd_syndesc *as, void *arock)
2329 {
2330     afs_int32 code, i;
2331     char *tp, *aliasName, *realName;
2332     struct ViceIoctl blob;
2333
2334     for (i = 0;; i++) {
2335         tp = space;
2336         memcpy(tp, &i, sizeof(afs_int32));
2337         blob.out_size = AFS_PIOCTL_MAXSIZE;
2338         blob.in_size = sizeof(afs_int32);
2339         blob.in = space;
2340         blob.out = space;
2341         code = pioctl(0, VIOC_GETALIAS, &blob, 1);
2342         if (code < 0) {
2343             if (errno == EDOM)
2344                 break;          /* done with the list */
2345             Die(errno, 0);
2346             return 1;
2347         }
2348         tp = space;
2349         aliasName = tp;
2350         tp += strlen(aliasName) + 1;
2351         realName = tp;
2352         printf("Alias %s for cell %s\n", aliasName, realName);
2353     }
2354     return 0;
2355 }
2356
2357 static int
2358 CallBackRxConnCmd(struct cmd_syndesc *as, void *arock)
2359 {
2360     afs_int32 code;
2361     struct ViceIoctl blob;
2362     struct cmd_item *ti;
2363     afs_int32 hostAddr;
2364     struct hostent *thp;
2365     int setp;
2366
2367     ti = as->parms[0].items;
2368     setp = 1;
2369     if (ti) {
2370         thp = hostutil_GetHostByName(ti->data);
2371         if (!thp) {
2372             fprintf(stderr, "host %s not found in host table.\n", ti->data);
2373             return 1;
2374         }
2375         else memcpy(&hostAddr, thp->h_addr, sizeof(afs_int32));
2376     } else {
2377         hostAddr = 0;   /* means don't set host */
2378         setp = 0;       /* aren't setting host */
2379     }
2380
2381     /* now do operation */
2382     blob.in_size = sizeof(afs_int32);
2383     blob.out_size = sizeof(afs_int32);
2384     blob.in = (char *) &hostAddr;
2385     blob.out = (char *) &hostAddr;
2386
2387     code = pioctl(0, VIOC_CBADDR, &blob, 1);
2388     if (code < 0) {
2389         Die(errno, 0);
2390         return 1;
2391     }
2392     return 0;
2393 }
2394
2395 static int
2396 NukeNFSCredsCmd(struct cmd_syndesc *as, void *arock)
2397 {
2398     afs_int32 code;
2399     struct ViceIoctl blob;
2400     struct cmd_item *ti;
2401     afs_int32 hostAddr;
2402     struct hostent *thp;
2403
2404     ti = as->parms[0].items;
2405     thp = hostutil_GetHostByName(ti->data);
2406     if (!thp) {
2407         fprintf(stderr, "host %s not found in host table.\n", ti->data);
2408         return 1;
2409     }
2410     else memcpy(&hostAddr, thp->h_addr, sizeof(afs_int32));
2411
2412     /* now do operation */
2413     blob.in_size = sizeof(afs_int32);
2414     blob.out_size = sizeof(afs_int32);
2415     blob.in = (char *) &hostAddr;
2416     blob.out = (char *) &hostAddr;
2417
2418     code = pioctl(0, VIOC_NFS_NUKE_CREDS, &blob, 1);
2419     if (code < 0) {
2420         Die(errno, 0);
2421         return 1;
2422     }
2423     return 0;
2424 }
2425
2426 static int
2427 NewCellCmd(struct cmd_syndesc *as, void *arock)
2428 {
2429     afs_int32 code, linkedstate = 0, size = 0, *lp;
2430     struct ViceIoctl blob;
2431     struct cmd_item *ti;
2432     char *tp, *cellname = 0;
2433     struct hostent *thp;
2434     afs_int32 fsport = 0, vlport = 0;
2435     afs_int32 scount;           /* Number of servers to pass in pioctl call */
2436
2437     /* Yuck!
2438      * With the NEWCELL pioctl call, 3.4 clients take an array of
2439      * AFS_MAXHOSTS (13) servers while 3.5 clients take an array of
2440      * AFS_MAXCELLHOSTS (8) servers. To determine which we are talking to,
2441      * do a GETCELL pioctl and pass it a magic number. If an array of
2442      * 8 comes back, its a 3.5 client. If not, its a 3.4 client.
2443      * If we get back EDOM, there are no cells in the kernel yet,
2444      * and we'll assume a 3.5 client.
2445      */
2446     tp = space;
2447     lp = (afs_int32 *) tp;
2448     *lp++ = 0;                  /* first cell entry */
2449     *lp = 0x12345678;           /* magic */
2450     blob.out_size = AFS_PIOCTL_MAXSIZE;
2451     blob.in_size = sizeof(afs_int32) + sizeof(afs_int32);
2452     blob.in = space;
2453     blob.out = space;
2454     code = pioctl(0, VIOCGETCELL, &blob, 1);
2455     if (code < 0 && errno != EDOM) {
2456         Die(errno, 0);
2457         return 1;
2458     }
2459     if (code < 1 && errno == EDOM) {
2460         scount = AFS_MAXHOSTS;
2461     } else {
2462         tp = space;
2463         cellname = tp + AFS_MAXCELLHOSTS * sizeof(afs_int32);
2464         scount = ((cellname[0] != '\0') ? AFS_MAXCELLHOSTS : AFS_MAXHOSTS);
2465     }
2466
2467     /* Now setup and do the NEWCELL pioctl call */
2468     memset(space, 0, (scount + 1) * sizeof(afs_int32));
2469     tp = space;
2470     lp = (afs_int32 *) tp;
2471     *lp++ = 0x12345678;
2472     tp += sizeof(afs_int32);
2473     for (ti = as->parms[1].items; ti; ti = ti->next) {
2474         thp = hostutil_GetHostByName(ti->data);
2475         if (!thp) {
2476             fprintf(stderr,
2477                     "%s: Host %s not found in host table, skipping it.\n", pn,
2478                     ti->data);
2479         } else {
2480             memcpy(tp, thp->h_addr, sizeof(afs_int32));
2481             tp += sizeof(afs_int32);
2482         }
2483     }
2484     if (as->parms[2].items) {
2485         /*
2486          * Link the cell, for the purposes of volume location, to the specified
2487          * cell.
2488          */
2489         cellname = as->parms[2].items->data;
2490         linkedstate = 1;
2491     }
2492 #ifdef FS_ENABLE_SERVER_DEBUG_PORTS
2493     if (as->parms[3].items) {
2494         code = util_GetInt32(as->parms[3].items->data, &vlport);
2495         if (code) {
2496             fprintf(stderr,
2497                     "%s: bad integer specified for the fileserver port.\n",
2498                     pn);
2499             return 1;
2500         }
2501     }
2502     if (as->parms[4].items) {
2503         code = util_GetInt32(as->parms[4].items->data, &fsport);
2504         if (code) {
2505             fprintf(stderr,
2506                     "%s: bad integer specified for the vldb server port.\n",
2507                     pn);
2508             return 1;
2509         }
2510     }
2511 #endif
2512     tp = (char *)(space + (scount + 1) * sizeof(afs_int32));
2513     lp = (afs_int32 *) tp;
2514     *lp++ = fsport;
2515     *lp++ = vlport;
2516     *lp = linkedstate;
2517     strcpy(space + ((scount + 4) * sizeof(afs_int32)),
2518            as->parms[0].items->data);
2519     size = ((scount + 4) * sizeof(afs_int32))
2520         + strlen(as->parms[0].items->data)
2521         + 1 /* for null */ ;
2522     tp = (char *)(space + size);
2523     if (linkedstate) {
2524         strcpy(tp, cellname);
2525         size += strlen(cellname) + 1;
2526     }
2527     blob.in_size = size;
2528     blob.in = space;
2529     blob.out_size = 0;
2530     code = pioctl(0, VIOCNEWCELL, &blob, 1);
2531     if (code < 0) {
2532         Die(errno, 0);
2533         return 1;
2534     }
2535     return 0;
2536 }
2537
2538 static int
2539 NewAliasCmd(struct cmd_syndesc *as, void *arock)
2540 {
2541     afs_int32 code;
2542     struct ViceIoctl blob;
2543     char *tp;
2544     char *aliasName, *realName;
2545
2546     /* Now setup and do the NEWCELL pioctl call */
2547     aliasName = as->parms[0].items->data;
2548     realName = as->parms[1].items->data;
2549     tp = space;
2550     strcpy(tp, aliasName);
2551     tp += strlen(aliasName) + 1;
2552     strcpy(tp, realName);
2553     tp += strlen(realName) + 1;
2554
2555     blob.in_size = tp - space;
2556     blob.in = space;
2557     blob.out_size = 0;
2558     blob.out = space;
2559     code = pioctl(0, VIOC_NEWALIAS, &blob, 1);
2560     if (code < 0) {
2561         if (errno == EEXIST) {
2562             fprintf(stderr,
2563                     "%s: cell name `%s' in use by an existing cell.\n", pn,
2564                     aliasName);
2565         } else {
2566             Die(errno, 0);
2567         }
2568         return 1;
2569     }
2570     return 0;
2571 }
2572
2573 static int
2574 WhichCellCmd(struct cmd_syndesc *as, void *arock)
2575 {
2576     afs_int32 code;
2577     struct cmd_item *ti;
2578     int error = 0;
2579     char cell[MAXCELLCHARS];
2580
2581     SetDotDefault(&as->parms[0].items);
2582     for (ti = as->parms[0].items; ti; ti = ti->next) {
2583         code = GetCell(ti->data, cell);
2584         if (code) {
2585             if (errno == ENOENT)
2586                 fprintf(stderr, "%s: no such cell as '%s'\n", pn, ti->data);
2587             else
2588                 Die(errno, ti->data);
2589             error = 1;
2590             continue;
2591         }
2592
2593         printf("File %s lives in cell '%s'\n", ti->data, cell);
2594     }
2595     return error;
2596 }
2597
2598 static int
2599 WSCellCmd(struct cmd_syndesc *as, void *arock)
2600 {
2601     afs_int32 code;
2602     struct ViceIoctl blob;
2603
2604     blob.in_size = 0;
2605     blob.in = NULL;
2606     blob.out_size = AFS_PIOCTL_MAXSIZE;
2607     blob.out = space;
2608
2609     code = pioctl(NULL, VIOC_GET_WS_CELL, &blob, 1);
2610     if (code) {
2611         Die(errno, NULL);
2612         return 1;
2613     }
2614
2615     printf("This workstation belongs to cell '%s'\n", space);
2616     return 0;
2617 }
2618
2619 /*
2620 static PrimaryCellCmd(as)
2621     struct cmd_syndesc *as;
2622 {
2623     fprintf(stderr, "This command is obsolete, as is the concept of a primary token.\n");
2624     return 0;
2625 }
2626 */
2627
2628 static int
2629 MonitorCmd(struct cmd_syndesc *as, void *arock)
2630 {
2631     afs_int32 code;
2632     struct ViceIoctl blob;
2633     struct cmd_item *ti;
2634     afs_int32 hostAddr;
2635     struct hostent *thp;
2636     char *tp;
2637     int setp;
2638
2639     ti = as->parms[0].items;
2640     setp = 1;
2641     if (ti) {
2642         /* set the host */
2643         if (!strcmp(ti->data, "off"))
2644             hostAddr = 0xffffffff;
2645         else {
2646             thp = hostutil_GetHostByName(ti->data);
2647             if (!thp) {
2648                 if (!strcmp(ti->data, "localhost")) {
2649                     fprintf(stderr,
2650                             "localhost not in host table, assuming 127.0.0.1\n");
2651                     hostAddr = htonl(0x7f000001);
2652                 } else {
2653                     fprintf(stderr, "host %s not found in host table.\n",
2654                             ti->data);
2655                     return 1;
2656                 }
2657             } else
2658                 memcpy(&hostAddr, thp->h_addr, sizeof(afs_int32));
2659         }
2660     } else {
2661         hostAddr = 0;           /* means don't set host */
2662         setp = 0;               /* aren't setting host */
2663     }
2664
2665     /* now do operation */
2666     blob.in_size = sizeof(afs_int32);
2667     blob.out_size = sizeof(afs_int32);
2668     blob.in = (char *)&hostAddr;
2669     blob.out = (char *)&hostAddr;
2670     code = pioctl(0, VIOC_AFS_MARINER_HOST, &blob, 1);
2671     if (code) {
2672         Die(errno, 0);
2673         return 1;
2674     }
2675     if (setp) {
2676         printf("%s: new monitor host set.\n", pn);
2677     } else {
2678         /* now decode old address */
2679         if (hostAddr == 0xffffffff) {
2680             printf("Cache monitoring is currently disabled.\n");
2681         } else {
2682             tp = hostutil_GetNameByINet(hostAddr);
2683             printf("Using host %s for monitor services.\n", tp);
2684         }
2685     }
2686     return 0;
2687 }
2688
2689 static int
2690 SysNameCmd(struct cmd_syndesc *as, void *arock)
2691 {
2692     afs_int32 code;
2693     struct ViceIoctl blob;
2694     struct cmd_item *ti;
2695     char *input = space;
2696     afs_int32 setp = 0;
2697
2698     ti = as->parms[0].items;
2699     blob.in = space;
2700     blob.out = space;
2701     blob.out_size = AFS_PIOCTL_MAXSIZE;
2702     blob.in_size = sizeof(afs_int32);
2703     input += sizeof(afs_int32);
2704     for (; ti; ti = ti->next) {
2705         setp++;
2706         blob.in_size += strlen(ti->data) + 1;
2707         if (blob.in_size > AFS_PIOCTL_MAXSIZE) {
2708             fprintf(stderr, "%s: sysname%s too long.\n", pn,
2709                     setp > 1 ? "s" : "");
2710             return 1;
2711         }
2712         strcpy(input, ti->data);
2713         input += strlen(ti->data);
2714         *(input++) = '\0';
2715     }
2716     memcpy(space, &setp, sizeof(afs_int32));
2717     code = pioctl(0, VIOC_AFS_SYSNAME, &blob, 1);
2718     if (code) {
2719         Die(errno, 0);
2720         return 1;
2721     }
2722     if (setp) {
2723         printf("%s: new sysname%s set.\n", pn, setp > 1 ? " list" : "");
2724         return 0;
2725     }
2726     input = space;
2727     memcpy(&setp, input, sizeof(afs_int32));
2728     input += sizeof(afs_int32);
2729     if (!setp) {
2730         fprintf(stderr, "No sysname name value was found\n");
2731         return 1;
2732     }
2733     printf("Current sysname%s is", setp > 1 ? " list" : "");
2734     for (; setp > 0; --setp) {
2735         printf(" \'%s\'", input);
2736         input += strlen(input) + 1;
2737     }
2738     printf("\n");
2739     return 0;
2740 }
2741
2742 static char *exported_types[] = { "null", "nfs", "" };
2743 static int
2744 ExportAfsCmd(struct cmd_syndesc *as, void *arock)
2745 {
2746     afs_int32 code;
2747     struct ViceIoctl blob;
2748     struct cmd_item *ti;
2749     int export = 0, type = 0, mode = 0, exp = 0, exportcall, pwsync =
2750         0, smounts = 0, clipags = 0, pagcb = 0;
2751
2752     ti = as->parms[0].items;
2753     if (strcmp(ti->data, "nfs") == 0)
2754         type = 0x71;            /* NFS */
2755     else {
2756         fprintf(stderr,
2757                 "Invalid exporter type, '%s', Only the 'nfs' exporter is currently supported\n",
2758                 ti->data);
2759         return 1;
2760     }
2761     ti = as->parms[1].items;
2762     if (ti) {
2763         if (strcmp(ti->data, "on") == 0)
2764             export = 3;
2765         else if (strcmp(ti->data, "off") == 0)
2766             export = 2;
2767         else {
2768             fprintf(stderr, "Illegal argument %s\n", ti->data);
2769             return 1;
2770         }
2771         exp = 1;
2772     }
2773     if ((ti = as->parms[2].items)) {    /* -noconvert */
2774         if (strcmp(ti->data, "on") == 0)
2775             mode = 2;
2776         else if (strcmp(ti->data, "off") == 0)
2777             mode = 3;
2778         else {
2779             fprintf(stderr, "Illegal argument %s\n", ti->data);
2780             return 1;
2781         }
2782     }
2783     if ((ti = as->parms[3].items)) {    /* -uidcheck */
2784         if (strcmp(ti->data, "on") == 0)
2785             pwsync = 3;
2786         else if (strcmp(ti->data, "off") == 0)
2787             pwsync = 2;
2788         else {
2789             fprintf(stderr, "Illegal argument %s\n", ti->data);
2790             return 1;
2791         }
2792     }
2793     if ((ti = as->parms[4].items)) {    /* -submounts */
2794         if (strcmp(ti->data, "on") == 0)
2795             smounts = 3;
2796         else if (strcmp(ti->data, "off") == 0)
2797             smounts = 2;
2798         else {
2799             fprintf(stderr, "Illegal argument %s\n", ti->data);
2800             return 1;
2801         }
2802     }
2803     if ((ti = as->parms[5].items)) {    /* -clipags */
2804         if (strcmp(ti->data, "on") == 0)
2805             clipags = 3;
2806         else if (strcmp(ti->data, "off") == 0)
2807             clipags = 2;
2808         else {
2809             fprintf(stderr, "Illegal argument %s\n", ti->data);
2810             return 1;
2811         }
2812     }
2813     if ((ti = as->parms[6].items)) {    /* -pagcb */
2814         if (strcmp(ti->data, "on") == 0)
2815             pagcb = 3;
2816         else if (strcmp(ti->data, "off") == 0)
2817             pagcb = 2;
2818         else {
2819             fprintf(stderr, "Illegal argument %s\n", ti->data);
2820             return 1;
2821         }
2822     }
2823     exportcall =
2824         (type << 24) | (pagcb << 10) | (clipags << 8) |
2825         (mode << 6) | (pwsync << 4) | (smounts << 2) | export;
2826     type &= ~0x70;
2827     /* make the call */
2828     blob.in = (char *)&exportcall;
2829     blob.in_size = sizeof(afs_int32);
2830     blob.out = (char *)&exportcall;
2831     blob.out_size = sizeof(afs_int32);
2832     code = pioctl(0, VIOC_EXPORTAFS, &blob, 1);
2833     if (code) {
2834         if (errno == ENODEV) {
2835             fprintf(stderr,
2836                     "Sorry, the %s-exporter type is currently not supported on this AFS client\n",
2837                     exported_types[type]);
2838         } else {
2839             Die(errno, 0);
2840         }
2841         return 1;
2842     }
2843
2844     if (exportcall & 1) {
2845         printf("'%s' translator is enabled with the following options:\n",
2846                exported_types[type]);
2847         printf("\tRunning in %s mode\n",
2848                (exportcall & 2 ? "strict unix" :
2849                 "convert owner mode bits to world/other"));
2850         printf("\tRunning in %s mode\n",
2851                (exportcall & 4 ? "strict 'passwd sync'" :
2852                 "no 'passwd sync'"));
2853         printf("\t%s\n",
2854                (exportcall & 8 ? "Allow mounts of /afs/.. subdirs" :
2855                 "Only mounts to /afs allowed"));
2856         printf("\t%s\n",
2857                (exportcall & 16 ? "Client-assigned PAG's are used" :
2858                 "Client-assigned PAG's are not used"));
2859         printf("\t%s\n",
2860                (exportcall & 32 ?
2861                 "Callbacks are made to get creds from new clients" :
2862                 "Callbacks are not made to get creds from new clients"));
2863     } else {
2864         printf("'%s' translator is disabled\n", exported_types[type]);
2865     }
2866     return 0;
2867 }
2868
2869
2870 static int
2871 GetCellCmd(struct cmd_syndesc *as, void *arock)
2872 {
2873     afs_int32 code;
2874     struct ViceIoctl blob;
2875     struct afsconf_cell info;
2876     struct cmd_item *ti;
2877     struct a {
2878         afs_int32 stat;
2879         afs_int32 junk;
2880     } args;
2881     int error = 0;
2882
2883     memset(&args, '\0', sizeof args);   /* avoid Purify UMR error */
2884     for (ti = as->parms[0].items; ti; ti = ti->next) {
2885         /* once per cell */
2886         blob.out_size = sizeof(args);
2887         blob.out = (caddr_t) & args;
2888         code = GetCellName(ti->data, &info);
2889         if (code) {
2890             error = 1;
2891             continue;
2892         }
2893         blob.in_size = 1 + strlen(info.name);
2894         blob.in = info.name;
2895         code = pioctl(0, VIOC_GETCELLSTATUS, &blob, 1);
2896         if (code) {
2897             if (errno == ENOENT)
2898                 fprintf(stderr, "%s: the cell named '%s' does not exist\n",
2899                         pn, info.name);
2900             else
2901                 Die(errno, info.name);
2902             error = 1;
2903             continue;
2904         }
2905         printf("Cell %s status: ", info.name);
2906 #ifdef notdef
2907         if (args.stat & 1)
2908             printf("primary ");
2909 #endif
2910         if (args.stat & 2)
2911             printf("no setuid allowed");
2912         else
2913             printf("setuid allowed");
2914         if (args.stat & 4)
2915             printf(", using old VLDB");
2916         printf("\n");
2917     }
2918     return error;
2919 }
2920
2921 static int
2922 SetCellCmd(struct cmd_syndesc *as, void *arock)
2923 {
2924     afs_int32 code;
2925     struct ViceIoctl blob;
2926     struct afsconf_cell info;
2927     struct cmd_item *ti;
2928     struct a {
2929         afs_int32 stat;
2930         afs_int32 junk;
2931         char cname[64];
2932     } args;
2933     int error = 0;
2934
2935     /* Check arguments. */
2936     if (as->parms[1].items && as->parms[2].items) {
2937         fprintf(stderr, "Cannot specify both -suid and -nosuid.\n");
2938         return 1;
2939     }
2940
2941     /* figure stuff to set */
2942     args.stat = 0;
2943     args.junk = 0;
2944
2945     if (!as->parms[1].items)
2946         args.stat |= 2;         /* default to -nosuid */
2947
2948     /* set stat for all listed cells */
2949     for (ti = as->parms[0].items; ti; ti = ti->next) {
2950         /* once per cell */
2951         code = GetCellName(ti->data, &info);
2952         if (code) {
2953             error = 1;
2954             continue;
2955         }
2956         strcpy(args.cname, info.name);
2957         blob.in_size = sizeof(args);
2958         blob.in = (caddr_t) & args;
2959         blob.out_size = 0;
2960         blob.out = (caddr_t) 0;
2961         code = pioctl(0, VIOC_SETCELLSTATUS, &blob, 1);
2962         if (code) {
2963             Die(errno, info.name);      /* XXX added cell name to Die() call */
2964             error = 1;
2965         }
2966     }
2967     return error;
2968 }
2969
2970 static int
2971 GetCellName(char *cellName, struct afsconf_cell *info)
2972 {
2973     struct afsconf_dir *tdir;
2974     int code;
2975
2976     tdir = afsconf_Open(AFSDIR_CLIENT_ETC_DIRPATH);
2977     if (!tdir) {
2978         fprintf(stderr,
2979                 "Could not process files in configuration directory (%s).\n",
2980                 AFSDIR_CLIENT_ETC_DIRPATH);
2981         return -1;
2982     }
2983
2984     code = afsconf_GetCellInfo(tdir, cellName, AFSCONF_VLDBSERVICE, info);
2985     if (code) {
2986         fprintf(stderr, "%s: cell %s not in %s\n", pn, cellName,
2987                 AFSDIR_CLIENT_CELLSERVDB_FILEPATH);
2988         return code;
2989     }
2990
2991     return 0;
2992 }
2993
2994
2995 static int
2996 VLDBInit(int noAuthFlag, struct afsconf_cell *info)
2997 {
2998     afs_int32 code;
2999
3000     code = ugen_ClientInit(noAuthFlag, (char *) AFSDIR_CLIENT_ETC_DIRPATH,
3001                            info->name, 0, &uclient,
3002                            NULL, pn, rxkad_clear,
3003                            VLDB_MAXSERVERS, AFSCONF_VLDBSERVICE, 50,
3004                            0, 0, USER_SERVICE_ID);
3005     rxInitDone = 1;
3006     return code;
3007 }
3008
3009 static struct ViceIoctl gblob;
3010 static int debug = 0;
3011 /*
3012  * here follow some routines in suport of the setserverprefs and
3013  * getserverprefs commands.  They are:
3014  * SetPrefCmd  "top-level" routine
3015  * addServer   adds a server to the list of servers to be poked into the
3016  *             kernel.  Will poke the list into the kernel if it threatens
3017  *             to get too large.
3018  * pokeServers pokes the existing list of servers and ranks into the kernel
3019  * GetPrefCmd  reads the Cache Manager's current list of server ranks
3020  */
3021
3022 /*
3023  * returns -1 if error message printed,
3024  * 0 on success,
3025  * errno value if error and no error message printed
3026  */
3027 static int
3028 pokeServers(void)
3029 {
3030     int code;
3031
3032     code = pioctl(0, VIOC_SETSPREFS, &gblob, 1);
3033     if (code && (errno == EINVAL)) {
3034         struct setspref *ssp;
3035         ssp = (struct setspref *)gblob.in;
3036         if (!(ssp->flags & DBservers)) {
3037             gblob.in = (void *)&(ssp->servers[0]);
3038             gblob.in_size -= ((char *)&(ssp->servers[0])) - (char *)ssp;
3039             code = pioctl(0, VIOC_SETSPREFS33, &gblob, 1);
3040             return code ? errno : 0;
3041         }
3042         fprintf(stderr,
3043                 "This cache manager does not support VL server preferences.\n");
3044         return -1;
3045     }
3046
3047     return code ? errno : 0;
3048 }
3049
3050 /*
3051  * returns -1 if error message printed,
3052  * 0 on success,
3053  * errno value if error and no error message printed
3054  */
3055 static int
3056 addServer(char *name, afs_int32 rank)
3057 {
3058     int t, code;
3059     struct setspref *ssp;
3060     struct spref *sp;
3061     struct hostent *thostent;
3062     int error = 0;
3063
3064 #ifndef MAXUSHORT
3065 #ifdef MAXSHORT
3066 #define MAXUSHORT ((unsigned short) 2*MAXSHORT+1)       /* assumes two's complement binary system */
3067 #else
3068 #define MAXUSHORT ((unsigned short) ~0)
3069 #endif
3070 #endif
3071
3072     thostent = hostutil_GetHostByName(name);
3073     if (!thostent) {
3074         fprintf(stderr, "%s: couldn't resolve name.\n", name);
3075         return -1;
3076     }
3077
3078     ssp = (struct setspref *)(gblob.in);
3079
3080     for (t = 0; thostent->h_addr_list[t]; t++) {
3081         if (gblob.in_size > MAXINSIZE - sizeof(struct spref)) {
3082             code = pokeServers();
3083             if (code)
3084                 error = code;
3085             ssp->num_servers = 0;
3086         }
3087
3088         sp = (struct spref *)(gblob.in + gblob.in_size);
3089         memcpy(&(sp->server.s_addr), thostent->h_addr_list[t],
3090                sizeof(afs_uint32));
3091         sp->rank = (rank > MAXUSHORT ? MAXUSHORT : rank);
3092         gblob.in_size += sizeof(struct spref);
3093         ssp->num_servers++;
3094
3095         if (debug)
3096             fprintf(stderr, "adding server %s, rank %d, ip addr 0x%lx\n",
3097                     name, sp->rank, (long unsigned int) sp->server.s_addr);
3098     }
3099
3100     return error;
3101 }
3102
3103
3104 static int
3105 SetPrefCmd(struct cmd_syndesc *as, void *arock)
3106 {
3107     FILE *infd;
3108     afs_int32 code;
3109     struct cmd_item *ti;
3110     char name[80];
3111     afs_int32 rank;
3112     struct setspref *ssp;
3113     int error = 0;              /* -1 means error message printed,
3114                                  * >0 means errno value for unprinted message */
3115
3116     ssp = (struct setspref *)space;
3117     ssp->flags = 0;
3118     ssp->num_servers = 0;
3119     gblob.in_size = ((char *)&(ssp->servers[0])) - (char *)ssp;
3120     gblob.in = space;
3121     gblob.out = space;
3122     gblob.out_size = AFS_PIOCTL_MAXSIZE;
3123
3124
3125     if (geteuid()) {
3126         fprintf(stderr, "Permission denied: requires root access.\n");
3127         return 1;
3128     }
3129
3130     ti = as->parms[2].items;    /* -file */
3131     if (ti) {
3132         if (debug)
3133             fprintf(stderr, "opening file %s\n", ti->data);
3134         if (!(infd = fopen(ti->data, "r"))) {
3135             perror(ti->data);
3136             error = -1;
3137         } else {
3138             while (fscanf(infd, "%79s%ld", name, (long int *)&rank) != EOF) {
3139                 code = addServer(name, (unsigned short)rank);
3140                 if (code)
3141                     error = code;
3142             }
3143         }
3144     }
3145
3146     ti = as->parms[3].items;    /* -stdin */
3147     if (ti) {
3148         while (scanf("%79s%ld", name, (long int *)&rank) != EOF) {
3149             code = addServer(name, (unsigned short)rank);
3150             if (code)
3151                 error = code;
3152         }
3153     }
3154
3155     for (ti = as->parms[0].items; ti; ti = ti->next) {  /* list of servers, ranks */
3156         if (ti) {
3157             if (!ti->next) {
3158                 break;
3159             }
3160             code = addServer(ti->data, (unsigned short)atol(ti->next->data));
3161             if (code)
3162                 error = code;
3163             if (debug)
3164                 printf("set fs prefs %s %s\n", ti->data, ti->next->data);
3165             ti = ti->next;
3166         }
3167     }
3168     code = pokeServers();
3169     if (code)
3170         error = code;
3171     if (debug)
3172         printf("now working on vlservers, code=%d\n", code);
3173
3174     ssp = (struct setspref *)space;
3175     ssp->flags = DBservers;
3176     ssp->num_servers = 0;
3177     gblob.in_size = ((char *)&(ssp->servers[0])) - (char *)ssp;
3178     gblob.in = space;
3179
3180     for (ti = as->parms[1].items; ti; ti = ti->next) {  /* list of dbservers, ranks */
3181         if (ti) {
3182             if (!ti->next) {
3183                 break;
3184             }
3185             code = addServer(ti->data, (unsigned short)atol(ti->next->data));
3186             if (code)
3187                 error = code;
3188             if (debug)
3189                 printf("set vl prefs %s %s\n", ti->data, ti->next->data);
3190             ti = ti->next;
3191         }
3192     }
3193
3194     if (as->parms[1].items) {
3195         if (debug)
3196             printf("now poking vlservers\n");
3197         code = pokeServers();
3198         if (code)
3199             error = code;
3200     }
3201
3202     if (error > 0)
3203         Die(error, 0);
3204
3205     return error ? 1 : 0;
3206 }
3207
3208
3209
3210 static int
3211 GetPrefCmd(struct cmd_syndesc *as, void *arock)
3212 {
3213     afs_int32 code;
3214     struct cmd_item *ti;
3215     char *name, tbuffer[20];
3216     afs_int32 addr;
3217     FILE *outfd;
3218     int resolve;
3219     int vlservers = 0;
3220     struct ViceIoctl blob;
3221     struct sprefrequest *in;
3222     struct sprefinfo *out;
3223     int i;
3224
3225     ti = as->parms[0].items;    /* -file */
3226     if (ti) {
3227         if (debug)
3228             fprintf(stderr, "opening file %s\n", ti->data);
3229         if (!(outfd = freopen(ti->data, "w", stdout))) {
3230             perror(ti->data);
3231             return 1;
3232         }
3233     }
3234
3235     ti = as->parms[1].items;    /* -numeric */
3236     resolve = !(ti);
3237     ti = as->parms[2].items;    /* -vlservers */
3238     vlservers |= (ti ? DBservers : 0);
3239     /*  ti = as->parms[3].items;   -cell */
3240
3241     in = (struct sprefrequest *)space;
3242     in->offset = 0;
3243
3244     do {
3245         blob.in_size = sizeof(struct sprefrequest);
3246         blob.in = (char *)in;
3247         blob.out = space;
3248         blob.out_size = AFS_PIOCTL_MAXSIZE;
3249
3250         in->num_servers =
3251             (AFS_PIOCTL_MAXSIZE - 2 * sizeof(short)) / sizeof(struct spref);
3252         in->flags = vlservers;
3253
3254         do {
3255             code = pioctl(0, VIOC_GETSPREFS, &blob, 1);
3256             if (code) {
3257                 if ((errno != E2BIG) || (2 * blob.out_size > 0x7FFF)) {
3258                     perror("getserverprefs pioctl");
3259                     return 1;
3260                 }
3261                 blob.out_size *= 2;
3262                 if (blob.out == space)
3263                     blob.out = malloc(blob.out_size);
3264                 else
3265                     blob.out = realloc(blob.out, blob.out_size);
3266             }
3267         } while (code != 0);
3268
3269         out = (struct sprefinfo *)blob.out;
3270
3271         for (i = 0; i < out->num_servers; i++) {
3272             if (resolve) {
3273                 name = hostutil_GetNameByINet(out->servers[i].server.s_addr);
3274             } else {
3275                 addr = ntohl(out->servers[i].server.s_addr);
3276                 sprintf(tbuffer, "%d.%d.%d.%d", (addr >> 24) & 0xff,
3277                         (addr >> 16) & 0xff, (addr >> 8) & 0xff, addr & 0xff);
3278                 name = tbuffer;
3279             }
3280             printf("%-50s %5u\n", name, out->servers[i].rank);
3281         }
3282
3283         in->offset = out->next_offset;
3284     } while (out->next_offset > 0);
3285
3286     if (blob.out != space)
3287         free(blob.out);
3288
3289     return 0;
3290 }
3291
3292 static int
3293 StoreBehindCmd(struct cmd_syndesc *as, void *arock)
3294 {
3295     afs_int32 code = 0;
3296     struct ViceIoctl blob;
3297     struct cmd_item *ti;
3298     struct sbstruct tsb, tsb2;
3299     int verbose = 0;
3300     afs_int32 allfiles;
3301     char *t;
3302     int error = 0;
3303
3304     tsb.sb_thisfile = -1;
3305     ti = as->parms[0].items;    /* -kbytes */
3306     if (ti) {
3307         if (!as->parms[1].items) {
3308             fprintf(stderr, "%s: you must specify -files with -kbytes.\n",
3309                     pn);
3310             return 1;
3311         }
3312         tsb.sb_thisfile = strtol(ti->data, &t, 10) * 1024;
3313         if ((tsb.sb_thisfile < 0) || (t != ti->data + strlen(ti->data))) {
3314             fprintf(stderr, "%s: %s must be 0 or a positive number.\n", pn,
3315                     ti->data);
3316             return 1;
3317         }
3318     }
3319
3320     allfiles = tsb.sb_default = -1;     /* Don't set allfiles yet */
3321     ti = as->parms[2].items;    /* -allfiles */
3322     if (ti) {
3323         allfiles = strtol(ti->data, &t, 10) * 1024;
3324         if ((allfiles < 0) || (t != ti->data + strlen(ti->data))) {
3325             fprintf(stderr, "%s: %s must be 0 or a positive number.\n", pn,
3326                     ti->data);
3327             return 1;
3328         }
3329     }
3330
3331     /* -verbose or -file only or no options */
3332     if (as->parms[3].items || (as->parms[1].items && !as->parms[0].items)
3333         || (!as->parms[0].items && !as->parms[1].items
3334             && !as->parms[2].items))
3335         verbose = 1;
3336
3337     blob.in = (char *)&tsb;
3338     blob.in_size = sizeof(struct sbstruct);
3339
3340     /* once per -file */
3341     for (ti = as->parms[1].items; ti; ti = ti->next) {
3342         /* Do this solely to see if the file is there */
3343
3344         blob.out = space;
3345         blob.out_size = AFS_PIOCTL_MAXSIZE;
3346         code = pioctl(ti->data, VIOCWHEREIS, &blob, 1);
3347         if (code) {
3348             Die(errno, ti->data);
3349             error = 1;
3350             continue;
3351         }
3352
3353         memset(&tsb2, 0, sizeof(tsb2));
3354         blob.out = (char *)&tsb2;
3355         blob.out_size = sizeof(struct sbstruct);
3356         code = pioctl(ti->data, VIOC_STORBEHIND, &blob, 1);
3357         if (code) {
3358             Die(errno, ti->data);
3359             error = 1;
3360             continue;
3361         }
3362
3363         if (verbose && (blob.out_size == sizeof(tsb2))) {
3364             if (tsb2.sb_thisfile == -1) {
3365                 fprintf(stdout, "Will store %s according to default.\n",
3366                         ti->data);
3367             } else {
3368                 fprintf(stdout,
3369                         "Will store up to %d kbytes of %s asynchronously.\n",
3370                         (tsb2.sb_thisfile / 1024), ti->data);
3371             }
3372         }
3373     }
3374
3375     /* If no files - make at least one pioctl call, or
3376      * set the allfiles default if we need to.
3377      */
3378     if (!as->parms[1].items || (allfiles != -1)) {
3379         tsb.sb_default = allfiles;
3380         memset(&tsb2, 0, sizeof(tsb2));
3381         blob.out = (char *)&tsb2;
3382         blob.out_size = sizeof(struct sbstruct);
3383         code = pioctl(0, VIOC_STORBEHIND, &blob, 1);
3384         if (code) {
3385             Die(errno, ((allfiles == -1) ? 0 : "-allfiles"));
3386             error = 1;
3387         }
3388     }
3389
3390     /* Having no arguments also reports the default store asynchrony */
3391     if (!error && verbose && (blob.out_size == sizeof(tsb2))) {
3392         fprintf(stdout, "Default store asynchrony is %d kbytes.\n",
3393                 (tsb2.sb_default / 1024));
3394     }
3395
3396     return error;
3397 }
3398
3399
3400 static int
3401 SetCryptCmd(struct cmd_syndesc *as, void *arock)
3402 {
3403     afs_int32 code = 0, flag;
3404     struct ViceIoctl blob;
3405     char *tp;
3406
3407     tp = as->parms[0].items->data;
3408     if (strcmp(tp, "on") == 0)
3409         flag = 1;
3410     else if (strcmp(tp, "off") == 0)
3411         flag = 0;
3412     else {
3413         fprintf(stderr, "%s: %s must be \"on\" or \"off\".\n", pn, tp);
3414         return EINVAL;
3415     }
3416
3417     blob.in = (char *)&flag;
3418     blob.in_size = sizeof(flag);
3419     blob.out_size = 0;
3420     code = pioctl(0, VIOC_SETRXKCRYPT, &blob, 1);
3421     if (code)
3422         Die(errno, NULL);
3423     return 0;
3424 }
3425
3426
3427 static int
3428 GetCryptCmd(struct cmd_syndesc *as, void *arock)
3429 {
3430     afs_int32 code = 0, flag;
3431     struct ViceIoctl blob;
3432     char *tp;
3433
3434     blob.in = NULL;
3435     blob.in_size = 0;
3436     blob.out_size = sizeof(flag);
3437     blob.out = space;
3438
3439     code = pioctl(0, VIOC_GETRXKCRYPT, &blob, 1);
3440
3441     if (code)
3442         Die(errno, NULL);
3443     else {
3444         tp = space;
3445         memcpy(&flag, tp, sizeof(afs_int32));
3446         printf("Security level is currently ");
3447         if (flag == 1)
3448             printf("crypt (data security).\n");
3449         else
3450             printf("clear.\n");
3451     }
3452     return 0;
3453 }
3454
3455 static char *modenames[] = {
3456     "offline",
3457     "online",
3458     "readonly",  /* Not currently supported */
3459     "fetchonly", /* Not currently supported */
3460     "partial",   /* Not currently supported */
3461     NULL
3462 };
3463
3464 static char *policynames[] = {
3465     "client",
3466     "server",
3467     "closer",  /* Not currently supported. */
3468     "manual",  /* Not currently supported. */
3469     NULL
3470 };
3471
3472 static int
3473 DisconCmd(struct cmd_syndesc *as, void *arock)
3474 {
3475     struct cmd_item *ti;
3476     char *modename;
3477     char *policyname;
3478     int modelen, policylen;
3479     afs_int32 mode, policy, code, unixuid = 0;
3480     struct ViceIoctl blob;
3481
3482     blob.in = NULL;
3483     blob.in_size = 0;
3484
3485     space[0] = space[1] = space[2] = space[3] = 0;
3486
3487     ti = as->parms[0].items;
3488     if (ti) {
3489         modename = ti->data;
3490         modelen = strlen(modename);
3491         for (mode = 0; modenames[mode] != NULL; mode++)
3492             if (!strncasecmp(modename, modenames[mode], modelen))
3493                 break;
3494         if (modenames[mode] == NULL)
3495             printf("Unknown discon mode \"%s\"\n", modename);
3496         else {
3497             space[0] = mode + 1;
3498         }
3499     }
3500     ti = as->parms[1].items;
3501     if (ti) {
3502         policyname = ti->data;
3503         policylen = strlen(policyname);
3504         for (policy = 0; policynames[policy] != NULL; policy++)
3505             if (!strncasecmp(policyname, policynames[policy], policylen))
3506                 break;
3507         if (policynames[policy] == NULL)
3508             printf("Unknown discon mode \"%s\"\n", policyname);
3509         else {
3510             space[1] = policy + 1;
3511         }
3512     }
3513
3514     if (as->parms[2].items) {
3515         space[2] = 1;
3516         printf("force on\n");
3517     }
3518
3519     ti = as->parms[3].items;
3520     if (ti) {
3521         code = util_GetInt32(ti->data, &unixuid);
3522         if (code) {
3523             fprintf(stderr, "%s: bad integer specified for uid.\n", pn);
3524             return 1;
3525         }
3526         space[3] = unixuid;
3527     } else
3528         space[3] = 0;
3529
3530     blob.in = space;
3531     blob.in_size = 4 * sizeof(afs_int32);
3532
3533     blob.out_size = sizeof(mode);
3534     blob.out = space;
3535     code = pioctl(0, VIOC_DISCON, &blob, 1);
3536     if (code)
3537         Die(errno, NULL);
3538     else {
3539         memcpy(&mode, space, sizeof mode);
3540         if (mode < sizeof modenames / sizeof (char *))
3541             printf("Discon mode is now \"%s\"\n", modenames[mode]);
3542         else
3543             printf("Unknown discon mode %d\n", mode);
3544     }
3545
3546     return 0;
3547 }
3548
3549 #include "AFS_component_version_number.c"
3550
3551 int
3552 main(int argc, char **argv)
3553 {
3554     afs_int32 code;
3555     struct cmd_syndesc *ts;
3556
3557 #ifdef  AFS_AIX32_ENV
3558     /*
3559      * The following signal action for AIX is necessary so that in case of a
3560      * crash (i.e. core is generated) we can include the user's data section
3561      * in the core dump. Unfortunately, by default, only a partial core is
3562      * generated which, in many cases, isn't too useful.
3563      */
3564     struct sigaction nsa;
3565
3566     sigemptyset(&nsa.sa_mask);
3567     nsa.sa_handler = SIG_DFL;
3568     nsa.sa_flags = SA_FULLDUMP;
3569     sigaction(SIGSEGV, &nsa, NULL);
3570 #endif
3571
3572     /* try to find volume location information */
3573     ts = cmd_CreateSyntax("getclientaddrs", GetClientAddrsCmd, NULL,
3574                           "get client network interface addresses");
3575     cmd_CreateAlias(ts, "gc");
3576
3577     ts = cmd_CreateSyntax("setclientaddrs", SetClientAddrsCmd, NULL,
3578                           "set client network interface addresses");
3579     cmd_AddParm(ts, "-address", CMD_LIST, CMD_OPTIONAL | CMD_EXPANDS,
3580                 "client network interfaces");
3581     cmd_CreateAlias(ts, "sc");
3582
3583     ts = cmd_CreateSyntax("setserverprefs", SetPrefCmd, NULL,
3584                           "set server ranks");
3585     cmd_AddParm(ts, "-servers", CMD_LIST, CMD_OPTIONAL | CMD_EXPANDS,
3586                 "fileserver names and ranks");
3587     cmd_AddParm(ts, "-vlservers", CMD_LIST, CMD_OPTIONAL | CMD_EXPANDS,
3588                 "VL server names and ranks");
3589     cmd_AddParm(ts, "-file", CMD_SINGLE, CMD_OPTIONAL,
3590                 "input from named file");
3591     cmd_AddParm(ts, "-stdin", CMD_FLAG, CMD_OPTIONAL, "input from stdin");
3592     cmd_CreateAlias(ts, "sp");
3593
3594     ts = cmd_CreateSyntax("getserverprefs", GetPrefCmd, NULL,
3595                           "get server ranks");
3596     cmd_AddParm(ts, "-file", CMD_SINGLE, CMD_OPTIONAL,
3597                 "output to named file");
3598     cmd_AddParm(ts, "-numeric", CMD_FLAG, CMD_OPTIONAL, "addresses only");
3599     cmd_AddParm(ts, "-vlservers", CMD_FLAG, CMD_OPTIONAL, "VL servers");
3600 /*    cmd_AddParm(ts, "-cell", CMD_FLAG, CMD_OPTIONAL, "cellname"); */
3601     cmd_CreateAlias(ts, "gp");
3602
3603     ts = cmd_CreateSyntax("setacl", SetACLCmd, NULL, "set access control list");
3604     cmd_AddParm(ts, "-dir", CMD_LIST, 0, "directory");
3605     cmd_AddParm(ts, "-acl", CMD_LIST, 0, "access list entries");
3606     cmd_AddParm(ts, "-clear", CMD_FLAG, CMD_OPTIONAL, "clear access list");
3607     cmd_AddParm(ts, "-negative", CMD_FLAG, CMD_OPTIONAL,
3608                 "apply to negative rights");
3609     parm_setacl_id = ts->nParms;
3610     cmd_AddParm(ts, "-id", CMD_FLAG, CMD_OPTIONAL,
3611                 "initial directory acl (DFS only)");
3612     cmd_AddParm(ts, "-if", CMD_FLAG, CMD_OPTIONAL,
3613                 "initial file acl (DFS only)");
3614     cmd_CreateAlias(ts, "sa");
3615
3616     ts = cmd_CreateSyntax("listacl", ListACLCmd, NULL,
3617                           "list access control list");
3618     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3619     parm_listacl_id = ts->nParms;
3620     cmd_AddParm(ts, "-id", CMD_FLAG, CMD_OPTIONAL, "initial directory acl");
3621     cmd_AddParm(ts, "-if", CMD_FLAG, CMD_OPTIONAL, "initial file acl");
3622     cmd_AddParm(ts, "-cmd", CMD_FLAG, CMD_OPTIONAL, "output as 'fs setacl' command");
3623     cmd_CreateAlias(ts, "la");
3624
3625     ts = cmd_CreateSyntax("getcalleraccess", GetCallerAccess, NULL,
3626             "list callers access");
3627     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3628     cmd_CreateAlias(ts, "gca");
3629
3630     ts = cmd_CreateSyntax("cleanacl", CleanACLCmd, NULL,
3631                           "clean up access control list");
3632     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3633
3634     ts = cmd_CreateSyntax("copyacl", CopyACLCmd, NULL,
3635                           "copy access control list");
3636     cmd_AddParm(ts, "-fromdir", CMD_SINGLE, 0,
3637                 "source directory (or DFS file)");
3638     cmd_AddParm(ts, "-todir", CMD_LIST, 0,
3639                 "destination directory (or DFS file)");
3640     cmd_AddParm(ts, "-clear", CMD_FLAG, CMD_OPTIONAL,
3641                 "first clear dest access list");
3642     parm_copyacl_id = ts->nParms;
3643     cmd_AddParm(ts, "-id", CMD_FLAG, CMD_OPTIONAL, "initial directory acl");
3644     cmd_AddParm(ts, "-if", CMD_FLAG, CMD_OPTIONAL, "initial file acl");
3645
3646     cmd_CreateAlias(ts, "ca");
3647
3648     ts = cmd_CreateSyntax("flush", FlushCmd, NULL, "flush file from cache");
3649     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3650     ts = cmd_CreateSyntax("flushmount", FlushMountCmd, NULL,
3651                           "flush mount symlink from cache");
3652     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3653
3654     ts = cmd_CreateSyntax("setvol", SetVolCmd, NULL, "set volume status");
3655     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3656     cmd_AddParm(ts, "-max", CMD_SINGLE, CMD_OPTIONAL,
3657                 "disk space quota in 1K units");
3658 #ifdef notdef
3659     cmd_AddParm(ts, "-min", CMD_SINGLE, CMD_OPTIONAL,
3660                 "disk space guaranteed");
3661     cmd_AddParm(ts, "-motd", CMD_SINGLE, CMD_OPTIONAL, "message of the day");
3662 #endif
3663     cmd_AddParm(ts, "-offlinemsg", CMD_SINGLE, CMD_OPTIONAL,
3664                 "offline message");
3665     cmd_CreateAlias(ts, "sv");
3666
3667     ts = cmd_CreateSyntax("messages", MessagesCmd, NULL,
3668                           "control Cache Manager messages");
3669     cmd_AddParm(ts, "-show", CMD_SINGLE, CMD_OPTIONAL,
3670                 "[user|console|all|none]");
3671
3672     ts = cmd_CreateSyntax("examine", ExamineCmd, NULL, "display file/volume status");
3673     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3674     cmd_CreateAlias(ts, "lv");
3675     cmd_CreateAlias(ts, "listvol");
3676
3677     ts = cmd_CreateSyntax("listquota", ListQuotaCmd, NULL, "list volume quota");
3678     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3679     cmd_AddParm(ts, "-human", CMD_FLAG, CMD_OPTIONAL, "human-readable listing");
3680     cmd_CreateAlias(ts, "lq");
3681
3682     ts = cmd_CreateSyntax("diskfree", DiskFreeCmd, NULL,
3683                           "show server disk space usage");
3684     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3685     cmd_AddParm(ts, "-human", CMD_FLAG, CMD_OPTIONAL, "human-readable listing");
3686     cmd_CreateAlias(ts, "df");
3687
3688     ts = cmd_CreateSyntax("quota", QuotaCmd, NULL, "show volume quota usage");
3689     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3690
3691     ts = cmd_CreateSyntax("lsmount", ListMountCmd, NULL, "list mount point");
3692     cmd_AddParm(ts, "-dir", CMD_LIST, 0, "directory");
3693
3694     ts = cmd_CreateSyntax("mkmount", MakeMountCmd, NULL, "make mount point");
3695     cmd_AddParm(ts, "-dir", CMD_SINGLE, 0, "directory");
3696     cmd_AddParm(ts, "-vol", CMD_SINGLE, 0, "volume name");
3697     cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cell name");
3698     cmd_AddParm(ts, "-rw", CMD_FLAG, CMD_OPTIONAL, "force r/w volume");
3699     cmd_AddParm(ts, "-fast", CMD_FLAG, CMD_OPTIONAL,
3700                 "don't check name with VLDB");
3701
3702 #if defined(AFS_CACHE_BYPASS)
3703         ts = cmd_CreateSyntax("bypassthreshold", BypassThresholdCmd, 0,
3704                 "get/set cache bypass file size threshold");
3705         cmd_AddParm(ts, "-size", CMD_SINGLE, CMD_OPTIONAL, "file size");
3706 #endif
3707
3708 /*
3709
3710 defect 3069
3711
3712     cmd_AddParm(ts, "-root", CMD_FLAG, CMD_OPTIONAL, "create cellular mount point");
3713 */
3714
3715
3716     ts = cmd_CreateSyntax("rmmount", RemoveMountCmd, NULL, "remove mount point");
3717     cmd_AddParm(ts, "-dir", CMD_LIST, 0, "directory");
3718
3719     ts = cmd_CreateSyntax("checkservers", CheckServersCmd, NULL,
3720                           "check local cell's servers");
3721     cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cell to check");
3722     cmd_AddParm(ts, "-all", CMD_FLAG, CMD_OPTIONAL, "check all cells");
3723     cmd_AddParm(ts, "-fast", CMD_FLAG, CMD_OPTIONAL,
3724                 "just list, don't check");
3725     cmd_AddParm(ts, "-interval", CMD_SINGLE, CMD_OPTIONAL,
3726                 "seconds between probes");
3727
3728     ts = cmd_CreateSyntax("checkvolumes", CheckVolumesCmd, NULL,
3729                           "check volumeID/name mappings");
3730     cmd_CreateAlias(ts, "checkbackups");
3731
3732
3733     ts = cmd_CreateSyntax("setcachesize", SetCacheSizeCmd, NULL,
3734                           "set cache size");
3735     cmd_AddParm(ts, "-blocks", CMD_SINGLE, CMD_OPTIONAL,
3736                 "size in 1K byte blocks (0 => reset)");
3737     cmd_CreateAlias(ts, "cachesize");
3738
3739     cmd_AddParm(ts, "-reset", CMD_FLAG, CMD_OPTIONAL,
3740                 "reset size back to boot value");
3741
3742     ts = cmd_CreateSyntax("getcacheparms", GetCacheParmsCmd, NULL,
3743                           "get cache usage info");
3744     cmd_AddParm(ts, "-files", CMD_FLAG, CMD_OPTIONAL, "Show cach files used as well");
3745     cmd_AddParm(ts, "-excessive", CMD_FLAG, CMD_OPTIONAL, "excessively verbose cache stats");
3746
3747     ts = cmd_CreateSyntax("listcells", ListCellsCmd, NULL,
3748                           "list configured cells");
3749     cmd_AddParm(ts, "-numeric", CMD_FLAG, CMD_OPTIONAL, "addresses only");
3750
3751     ts = cmd_CreateSyntax("listaliases", ListAliasesCmd, NULL,
3752                           "list configured cell aliases");
3753
3754     ts = cmd_CreateSyntax("setquota", SetQuotaCmd, NULL, "set volume quota");
3755     cmd_AddParm(ts, "-path", CMD_SINGLE, CMD_OPTIONAL, "dir/file path");
3756     cmd_AddParm(ts, "-max", CMD_SINGLE, 0, "max quota in kbytes");
3757 #ifdef notdef
3758     cmd_AddParm(ts, "-min", CMD_SINGLE, CMD_OPTIONAL, "min quota in kbytes");
3759 #endif
3760     cmd_CreateAlias(ts, "sq");
3761
3762     ts = cmd_CreateSyntax("newcell", NewCellCmd, NULL, "configure new cell");
3763     cmd_AddParm(ts, "-name", CMD_SINGLE, 0, "cell name");
3764     cmd_AddParm(ts, "-servers", CMD_LIST, CMD_REQUIRED, "primary servers");
3765     cmd_AddParm(ts, "-linkedcell", CMD_SINGLE, CMD_OPTIONAL,
3766                 "linked cell name");
3767
3768     ts = cmd_CreateSyntax("newalias", NewAliasCmd, NULL,
3769                           "configure new cell alias");
3770     cmd_AddParm(ts, "-alias", CMD_SINGLE, 0, "alias name");
3771     cmd_AddParm(ts, "-name", CMD_SINGLE, 0, "real name of cell");
3772
3773 #ifdef FS_ENABLE_SERVER_DEBUG_PORTS
3774 /*
3775  * Turn this on only if you wish to be able to talk to a server which is listening
3776  * on alternative ports. This is not intended for general use and may not be
3777  * supported in the cache manager. It is not a way to run two servers at the
3778  * same host, since the cache manager cannot properly distinguish those two hosts.
3779  */
3780     cmd_AddParm(ts, "-fsport", CMD_SINGLE, CMD_OPTIONAL,
3781                 "cell's fileserver port");
3782     cmd_AddParm(ts, "-vlport", CMD_SINGLE, CMD_OPTIONAL,
3783                 "cell's vldb server port");
3784 #endif
3785
3786     ts = cmd_CreateSyntax("whichcell", WhichCellCmd, NULL, "list file's cell");
3787     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3788
3789     ts = cmd_CreateSyntax("whereis", WhereIsCmd, NULL, "list file's location");
3790     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3791
3792     ts = cmd_CreateSyntax("wscell", WSCellCmd, NULL, "list workstation's cell");
3793
3794 /*
3795     ts = cmd_CreateSyntax("primarycell", PrimaryCellCmd, NULL, "obsolete (listed primary cell)");
3796 */
3797
3798     /* set cache monitor host address */
3799     ts = cmd_CreateSyntax("monitor", MonitorCmd, NULL, (char *)CMD_HIDDEN);
3800     cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_OPTIONAL,
3801                 "host name or 'off'");
3802     cmd_CreateAlias(ts, "mariner");
3803
3804     ts = cmd_CreateSyntax("getcellstatus", GetCellCmd, NULL, "get cell status");
3805     cmd_AddParm(ts, "-cell", CMD_LIST, 0, "cell name");
3806
3807     ts = cmd_CreateSyntax("setcell", SetCellCmd, NULL, "set cell status");
3808     cmd_AddParm(ts, "-cell", CMD_LIST, 0, "cell name");
3809     cmd_AddParm(ts, "-suid", CMD_FLAG, CMD_OPTIONAL, "allow setuid programs");
3810     cmd_AddParm(ts, "-nosuid", CMD_FLAG, CMD_OPTIONAL,
3811                 "disallow setuid programs");
3812
3813     ts = cmd_CreateSyntax("flushvolume", FlushVolumeCmd, NULL,
3814                           "flush all data in volume");
3815     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3816
3817     ts = cmd_CreateSyntax("sysname", SysNameCmd, NULL,
3818                           "get/set sysname (i.e. @sys) value");
3819     cmd_AddParm(ts, "-newsys", CMD_LIST, CMD_OPTIONAL, "new sysname");
3820
3821     ts = cmd_CreateSyntax("exportafs", ExportAfsCmd, NULL,
3822                           "enable/disable translators to AFS");
3823     cmd_AddParm(ts, "-type", CMD_SINGLE, 0, "exporter name");
3824     cmd_AddParm(ts, "-start", CMD_SINGLE, CMD_OPTIONAL,
3825                 "start/stop translator (on | off)");
3826     cmd_AddParm(ts, "-convert", CMD_SINGLE, CMD_OPTIONAL,
3827                 "convert from afs to unix mode (on | off)");
3828     cmd_AddParm(ts, "-uidcheck", CMD_SINGLE, CMD_OPTIONAL,
3829                 "run on strict 'uid check' mode (on | off)");
3830     cmd_AddParm(ts, "-submounts", CMD_SINGLE, CMD_OPTIONAL,
3831                 "allow nfs mounts to subdirs of /afs/.. (on  | off)");
3832     cmd_AddParm(ts, "-clipags", CMD_SINGLE, CMD_OPTIONAL,
3833                 "enable use of client-assigned PAG's (on  | off)");
3834     cmd_AddParm(ts, "-pagcb", CMD_SINGLE, CMD_OPTIONAL,
3835                 "enable callbacks to get creds from new clients (on  | off)");
3836
3837
3838     ts = cmd_CreateSyntax("storebehind", StoreBehindCmd, NULL,
3839                           "store to server after file close");
3840     cmd_AddParm(ts, "-kbytes", CMD_SINGLE, CMD_OPTIONAL,
3841                 "asynchrony for specified names");
3842     cmd_AddParm(ts, "-files", CMD_LIST, CMD_OPTIONAL, "specific pathnames");
3843     cmd_AddParm(ts, "-allfiles", CMD_SINGLE, CMD_OPTIONAL,
3844                 "new default (KB)");
3845     cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL, "show status");
3846     cmd_CreateAlias(ts, "sb");
3847
3848     ts = cmd_CreateSyntax("setcrypt", SetCryptCmd, NULL,
3849                           "set cache manager encryption flag");
3850     cmd_AddParm(ts, "-crypt", CMD_SINGLE, 0, "on or off");
3851
3852     ts = cmd_CreateSyntax("getcrypt", GetCryptCmd, NULL,
3853                           "get cache manager encryption flag");
3854
3855     ts = cmd_CreateSyntax("rxstatproc", RxStatProcCmd, NULL,
3856                           "Manage per process RX statistics");
3857     cmd_AddParm(ts, "-enable", CMD_FLAG, CMD_OPTIONAL, "Enable RX stats");
3858     cmd_AddParm(ts, "-disable", CMD_FLAG, CMD_OPTIONAL, "Disable RX stats");
3859     cmd_AddParm(ts, "-clear", CMD_FLAG, CMD_OPTIONAL, "Clear RX stats");
3860
3861     ts = cmd_CreateSyntax("rxstatpeer", RxStatPeerCmd, NULL,
3862                           "Manage per peer RX statistics");
3863     cmd_AddParm(ts, "-enable", CMD_FLAG, CMD_OPTIONAL, "Enable RX stats");
3864     cmd_AddParm(ts, "-disable", CMD_FLAG, CMD_OPTIONAL, "Disable RX stats");
3865     cmd_AddParm(ts, "-clear", CMD_FLAG, CMD_OPTIONAL, "Clear RX stats");
3866
3867     ts = cmd_CreateSyntax("setcbaddr", CallBackRxConnCmd, NULL, "configure callback connection address");
3868     cmd_AddParm(ts, "-addr", CMD_SINGLE, CMD_OPTIONAL, "host name or address");
3869
3870     /* try to find volume location information */
3871     ts = cmd_CreateSyntax("getfid", GetFidCmd, NULL,
3872                           "get fid for file(s)");
3873     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3874
3875     ts = cmd_CreateSyntax("discon", DisconCmd, NULL,
3876                           "disconnection mode");
3877     cmd_AddParm(ts, "-mode", CMD_SINGLE, CMD_REQUIRED, "offline | online");
3878     cmd_AddParm(ts, "-policy", CMD_SINGLE, CMD_OPTIONAL, "client | server");
3879     cmd_AddParm(ts, "-force", CMD_FLAG, CMD_OPTIONAL, "Force reconnection, despite any synchronization issues.");
3880     cmd_AddParm(ts, "-uid", CMD_SINGLE, CMD_OPTIONAL, "Numeric UID of user whose tokens to use at reconnect.");
3881
3882     ts = cmd_CreateSyntax("nukenfscreds", NukeNFSCredsCmd, NULL, "nuke credentials for NFS client");
3883     cmd_AddParm(ts, "-addr", CMD_SINGLE, 0, "host name or address");
3884
3885     ts = cmd_CreateSyntax("uuid", UuidCmd, NULL, "manage the UUID for the cache manager");
3886     cmd_AddParm(ts, "-generate", CMD_FLAG, CMD_REQUIRED, "generate a new UUID");
3887
3888     ts = cmd_CreateSyntax("precache", PreCacheCmd, 0,
3889                           "set precache size");
3890     cmd_AddParm(ts, "-blocks", CMD_SINGLE, CMD_OPTIONAL,
3891                 "size in 1K byte blocks (0 => disable)");
3892
3893     code = cmd_Dispatch(argc, argv);
3894     if (rxInitDone)
3895         rx_Finalize();
3896
3897     return code;
3898 }
3899
3900 static void
3901 Die(int errnum, char *filename)
3902 {
3903     switch (errnum) {
3904     case EINVAL:
3905         if (filename)
3906             fprintf(stderr,
3907                     "%s: Invalid argument; it is possible that %s is not in AFS.\n",
3908                     pn, filename);
3909         else
3910             fprintf(stderr, "%s: Invalid argument.\n", pn);
3911         break;
3912     case ENOENT:
3913         if (filename)
3914             fprintf(stderr, "%s: File '%s' doesn't exist\n", pn, filename);
3915         else
3916             fprintf(stderr, "%s: no such file returned\n", pn);
3917         break;
3918     case EROFS:
3919         fprintf(stderr,
3920                 "%s: You can not change a backup or readonly volume\n", pn);
3921         break;
3922     case EACCES:
3923     case EPERM:
3924         if (filename)
3925             fprintf(stderr,
3926                     "%s: You don't have the required access rights on '%s'\n",
3927                     pn, filename);
3928         else
3929             fprintf(stderr,
3930                     "%s: You do not have the required rights to do this operation\n",
3931                     pn);
3932         break;
3933     default:
3934         if (filename)
3935             fprintf(stderr, "%s:'%s'", pn, filename);
3936         else
3937             fprintf(stderr, "%s", pn);
3938         fprintf(stderr, ": %s\n", afs_error_message(errnum));
3939         break;
3940     }
3941 }
3942
3943 /* get clients interface addresses */
3944 static int
3945 GetClientAddrsCmd(struct cmd_syndesc *as, void *arock)
3946 {
3947     afs_int32 code;
3948     struct ViceIoctl blob;
3949     struct sprefrequest *in;
3950     struct sprefinfo *out;
3951
3952     in = (struct sprefrequest *)space;
3953     in->offset = 0;
3954
3955     do {
3956         blob.in_size = sizeof(struct sprefrequest);
3957         blob.in = (char *)in;
3958         blob.out = space;
3959         blob.out_size = AFS_PIOCTL_MAXSIZE;
3960
3961         in->num_servers =
3962             (AFS_PIOCTL_MAXSIZE - 2 * sizeof(short)) / sizeof(struct spref);
3963         /* returns addr in network byte order */
3964         code = pioctl(0, VIOC_GETCPREFS, &blob, 1);
3965         if (code) {
3966             perror("getClientInterfaceAddr pioctl");
3967             return 1;
3968         }
3969
3970         {
3971             int i;
3972             out = (struct sprefinfo *)blob.out;
3973             for (i = 0; i < out->num_servers; i++) {
3974                 afs_int32 addr;
3975                 char tbuffer[32];
3976                 addr = ntohl(out->servers[i].server.s_addr);
3977                 sprintf(tbuffer, "%d.%d.%d.%d", (addr >> 24) & 0xff,
3978                         (addr >> 16) & 0xff, (addr >> 8) & 0xff, addr & 0xff);
3979                 printf("%-50s\n", tbuffer);
3980             }
3981             in->offset = out->next_offset;
3982         }
3983     } while (out->next_offset > 0);
3984
3985     return 0;
3986 }
3987
3988 static int
3989 SetClientAddrsCmd(struct cmd_syndesc *as, void *arock)
3990 {
3991     afs_int32 code, addr;
3992     struct cmd_item *ti;
3993     struct ViceIoctl blob;
3994     struct setspref *ssp;
3995     int sizeUsed = 0, i, flag;
3996     afs_uint32 existingAddr[1024];      /* existing addresses on this host */
3997     int existNu;
3998     int error = 0;
3999
4000     ssp = (struct setspref *)space;
4001     ssp->num_servers = 0;
4002     blob.in = space;
4003     blob.out = space;
4004     blob.out_size = AFS_PIOCTL_MAXSIZE;
4005
4006     if (geteuid()) {
4007         fprintf(stderr, "Permission denied: requires root access.\n");
4008         return 1;
4009     }
4010
4011     /* extract all existing interface addresses */
4012     existNu = rx_getAllAddr(existingAddr, 1024);
4013     if (existNu < 0)
4014         return 1;
4015
4016     sizeUsed = sizeof(struct setspref); /* space used in ioctl buffer */
4017     for (ti = as->parms[0].items; ti; ti = ti->next) {
4018         if (sizeUsed >= sizeof(space)) {
4019             fprintf(stderr, "No more space\n");
4020             return 1;
4021         }
4022         addr = extractAddr(ti->data, 20);       /* network order */
4023         if ((addr == AFS_IPINVALID) || (addr == AFS_IPINVALIDIGNORE)) {
4024             fprintf(stderr, "Error in specifying address: %s..ignoring\n",
4025                     ti->data);
4026             error = 1;
4027             continue;
4028         }
4029         /* see if it is an address that really exists */
4030         for (flag = 0, i = 0; i < existNu; i++)
4031             if (existingAddr[i] == addr) {
4032                 flag = 1;
4033                 break;
4034             }
4035         if (!flag) {            /* this is an nonexistent address */
4036             fprintf(stderr, "Nonexistent address: 0x%08x..ignoring\n", addr);
4037             error = 1;
4038             continue;
4039         }
4040         /* copy all specified addr into ioctl buffer */
4041         (ssp->servers[ssp->num_servers]).server.s_addr = addr;
4042         printf("Adding 0x%08x\n", addr);
4043         ssp->num_servers++;
4044         sizeUsed += sizeof(struct spref);
4045     }
4046     if (ssp->num_servers < 1) {
4047         fprintf(stderr, "No addresses specified\n");
4048         return 1;
4049     }
4050     blob.in_size = sizeUsed - sizeof(struct spref);
4051
4052     code = pioctl(0, VIOC_SETCPREFS, &blob, 1); /* network order */
4053     if (code) {
4054         Die(errno, 0);
4055         error = 1;
4056     }
4057
4058     return error;
4059 }
4060
4061 static int
4062 FlushMountCmd(struct cmd_syndesc *as, void *arock)
4063 {
4064     afs_int32 code;
4065     struct ViceIoctl blob;
4066     struct cmd_item *ti;
4067     char orig_name[1024];       /*Original name, may be modified */
4068     char true_name[1024];       /*``True'' dirname (e.g., symlink target) */
4069     char parent_dir[1024];      /*Parent directory of true name */
4070     char *last_component;       /*Last component of true name */
4071     struct stat statbuff;       /*Buffer for status info */
4072     int link_chars_read;        /*Num chars read in readlink() */
4073     int thru_symlink;           /*Did we get to a mount point via a symlink? */
4074     int error = 0;
4075
4076     for (ti = as->parms[0].items; ti; ti = ti->next) {
4077         /* once per file */
4078         thru_symlink = 0;
4079         sprintf(orig_name, "%s%s", (ti->data[0] == '/') ? "" : "./",
4080                 ti->data);
4081
4082         if (lstat(orig_name, &statbuff) < 0) {
4083             /* if lstat fails, we should still try the pioctl, since it
4084              * may work (for example, lstat will fail, but pioctl will
4085              * work if the volume of offline (returning ENODEV). */
4086             statbuff.st_mode = S_IFDIR; /* lie like pros */
4087         }
4088
4089         /*
4090          * The lstat succeeded.  If the given file is a symlink, substitute
4091          * the file name with the link name.
4092          */
4093         if ((statbuff.st_mode & S_IFMT) == S_IFLNK) {
4094             thru_symlink = 1;
4095             /*
4096              * Read name of resolved file.
4097              */
4098             link_chars_read = readlink(orig_name, true_name, 1024);
4099             if (link_chars_read <= 0) {
4100                 fprintf(stderr,
4101                         "%s: Can't read target name for '%s' symbolic link!\n",
4102                         pn, orig_name);
4103                 error = 1;
4104                 continue;
4105             }
4106
4107             /*
4108              * Add a trailing null to what was read, bump the length.
4109              */
4110             true_name[link_chars_read++] = 0;
4111
4112             /*
4113              * If the symlink is an absolute pathname, we're fine.  Otherwise, we
4114              * have to create a full pathname using the original name and the
4115              * relative symlink name.  Find the rightmost slash in the original
4116              * name (we know there is one) and splice in the symlink value.
4117              */
4118             if (true_name[0] != '/') {
4119                 last_component = (char *)strrchr(orig_name, '/');
4120                 strcpy(++last_component, true_name);
4121                 strcpy(true_name, orig_name);
4122             }
4123         } else
4124             strcpy(true_name, orig_name);
4125
4126         /*
4127          * Find rightmost slash, if any.
4128          */
4129         last_component = (char *)strrchr(true_name, '/');
4130         if (last_component == (char *)true_name) {
4131             strcpy(parent_dir, "/");
4132             last_component++;
4133         }
4134         else if (last_component != (char *)NULL) {
4135             /*
4136              * Found it.  Designate everything before it as the parent directory,
4137              * everything after it as the final component.
4138              */
4139             strncpy(parent_dir, true_name, last_component - true_name);
4140             parent_dir[last_component - true_name] = 0;
4141             last_component++;   /*Skip the slash */
4142         } else {
4143             /*
4144              * No slash appears in the given file name.  Set parent_dir to the current
4145              * directory, and the last component as the given name.
4146              */
4147             strcpy(parent_dir, ".");
4148             last_component = true_name;
4149         }
4150
4151         if (strcmp(last_component, ".") == 0
4152             || strcmp(last_component, "..") == 0) {
4153             fprintf(stderr,
4154                     "%s: you may not use '.' or '..' as the last component\n",
4155                     pn);
4156             fprintf(stderr, "%s: of a name in the 'fs flushmount' command.\n",
4157                     pn);
4158             error = 1;
4159             continue;
4160         }
4161
4162         blob.in = last_component;
4163         blob.in_size = strlen(last_component) + 1;
4164         blob.out_size = 0;
4165         memset(space, 0, AFS_PIOCTL_MAXSIZE);
4166
4167         code = pioctl(parent_dir, VIOC_AFS_FLUSHMOUNT, &blob, 1);
4168
4169         if (code != 0) {
4170             if (errno == EINVAL) {
4171                 fprintf(stderr, "'%s' is not a mount point.\n", ti->data);
4172             } else {
4173                 Die(errno, (ti->data ? ti->data : parent_dir));
4174             }
4175             error = 1;
4176         }
4177     }
4178     return error;
4179 }
4180
4181 static int
4182 RxStatProcCmd(struct cmd_syndesc *as, void *arock)
4183 {
4184     afs_int32 code;
4185     afs_int32 flags = 0;
4186     struct ViceIoctl blob;
4187
4188     if (as->parms[0].items) {   /* -enable */
4189         flags |= AFSCALL_RXSTATS_ENABLE;
4190     }
4191     if (as->parms[1].items) {   /* -disable */
4192         flags |= AFSCALL_RXSTATS_DISABLE;
4193     }
4194     if (as->parms[2].items) {   /* -clear */
4195         flags |= AFSCALL_RXSTATS_CLEAR;
4196     }
4197     if (flags == 0) {
4198         fprintf(stderr, "You must specify at least one argument\n");
4199         return 1;
4200     }
4201
4202     blob.in = (char *)&flags;
4203     blob.in_size = sizeof(afs_int32);
4204     blob.out_size = 0;
4205
4206     code = pioctl(NULL, VIOC_RXSTAT_PROC, &blob, 1);
4207     if (code != 0) {
4208         Die(errno, NULL);
4209         return 1;
4210     }
4211
4212     return 0;
4213 }
4214
4215 static int
4216 RxStatPeerCmd(struct cmd_syndesc *as, void *arock)
4217 {
4218     afs_int32 code;
4219     afs_int32 flags = 0;
4220     struct ViceIoctl blob;
4221
4222     if (as->parms[0].items) {   /* -enable */
4223         flags |= AFSCALL_RXSTATS_ENABLE;
4224     }
4225     if (as->parms[1].items) {   /* -disable */
4226         flags |= AFSCALL_RXSTATS_DISABLE;
4227     }
4228     if (as->parms[2].items) {   /* -clear */
4229         flags |= AFSCALL_RXSTATS_CLEAR;
4230     }
4231     if (flags == 0) {
4232         fprintf(stderr, "You must specify at least one argument\n");
4233         return 1;
4234     }
4235
4236     blob.in = (char *)&flags;
4237     blob.in_size = sizeof(afs_int32);
4238     blob.out_size = 0;
4239
4240     code = pioctl(NULL, VIOC_RXSTAT_PEER, &blob, 1);
4241     if (code != 0) {
4242         Die(errno, NULL);
4243         return 1;
4244     }
4245
4246     return 0;
4247 }
4248
4249 static int
4250 GetFidCmd(struct cmd_syndesc *as, void *arock)
4251 {
4252     struct ViceIoctl blob;
4253     struct cmd_item *ti;
4254
4255     afs_int32 code;
4256     int error = 0;
4257
4258     for (ti = as->parms[0].items; ti; ti = ti->next) {
4259         struct VenusFid vfid;
4260
4261         blob.out_size = sizeof(struct VenusFid);
4262         blob.out = (char *) &vfid;
4263         blob.in_size = 0;
4264
4265         code = pioctl(ti->data, VIOCGETFID, &blob, 1);
4266         if (code) {
4267             Die(errno,ti->data);
4268             error = 1;
4269             continue;
4270         }
4271
4272         printf("File %s (%u.%u.%u) contained in volume %u\n",
4273                ti->data, vfid.Fid.Volume, vfid.Fid.Vnode, vfid.Fid.Unique,
4274                vfid.Fid.Volume);
4275
4276     }
4277
4278     return error;
4279 }
4280