a8c17123eaeb2da37f127cf99dbc874457277fe4
[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
1362         if (!strcmp(tp,"-1")) {
1363             threshold_i = -1;
1364         } else {
1365             digit = 1;
1366             for(ix = 0; ix < len; ++ix) {
1367                 if(!isdigit(tp[0])) {
1368                     digit = 0;
1369                     break;
1370                 }
1371             }
1372             if (digit == 0) {
1373                 fprintf(stderr, "fs bypassthreshold -size: %s must be an integer between -1 and 2^31\n", tp);
1374                 return EINVAL;
1375             }
1376             threshold_i = atoi(tp);
1377             if(ix > 9 && threshold_i < 2147483647)
1378                 threshold_i = 2147483647;
1379         }
1380         blob.in = (char *) &threshold_i;
1381         blob.in_size = sizeof(threshold_i);
1382     } else {
1383         blob.in = NULL;
1384         blob.in_size = 0;
1385     }
1386
1387     blob.out = (char *) &threshold_o;
1388     blob.out_size = sizeof(threshold_o);
1389     code = pioctl(0, VIOC_SETBYPASS_THRESH, &blob, 1);
1390     if (code) {
1391         Die(errno, NULL);
1392         return 1;
1393     } else {
1394         printf("Cache bypass threshold %d", threshold_o);
1395         if(threshold_o ==  -1)
1396             printf(" (disabled)");
1397         printf("\n");
1398     }
1399
1400     return 0;
1401 }
1402
1403 #endif
1404
1405 static int
1406 FlushCmd(struct cmd_syndesc *as, void *arock)
1407 {
1408     afs_int32 code;
1409     struct ViceIoctl blob;
1410     struct cmd_item *ti;
1411     int error = 0;
1412
1413     for (ti = as->parms[0].items; ti; ti = ti->next) {
1414         blob.in_size = blob.out_size = 0;
1415         code = pioctl(ti->data, VIOCFLUSH, &blob, 0);
1416         if (code) {
1417             if (errno == EMFILE) {
1418                 fprintf(stderr, "%s: Can't flush active file %s\n", pn,
1419                         ti->data);
1420             } else {
1421                 fprintf(stderr, "%s: Error flushing file ", pn);
1422                 perror(ti->data);
1423             }
1424             error = 1;
1425             continue;
1426         }
1427     }
1428     return error;
1429 }
1430
1431 /* all this command does is repackage its args and call SetVolCmd */
1432 static int
1433 SetQuotaCmd(struct cmd_syndesc *as, void *arock)
1434 {
1435     struct cmd_syndesc ts;
1436
1437     /* copy useful stuff from our command slot; we may later have to reorder */
1438     memcpy(&ts, as, sizeof(ts));        /* copy whole thing */
1439     return SetVolCmd(&ts, arock);
1440 }
1441
1442 static int
1443 SetVolCmd(struct cmd_syndesc *as, void *arock)
1444 {
1445     afs_int32 code;
1446     struct ViceIoctl blob;
1447     struct cmd_item *ti;
1448     struct VolumeStatus *status;
1449     char *offmsg, *input;
1450     int error = 0;
1451
1452     SetDotDefault(&as->parms[0].items);
1453     for (ti = as->parms[0].items; ti; ti = ti->next) {
1454         /* once per file */
1455         blob.out_size = AFS_PIOCTL_MAXSIZE;
1456         blob.in_size = sizeof(*status) + 3;     /* for the three terminating nulls */
1457         blob.out = space;
1458         blob.in = space;
1459         status = (VolumeStatus *) space;
1460         status->MinQuota = status->MaxQuota = -1;
1461         offmsg = NULL;
1462         if (as->parms[1].items) {
1463             code = util_GetHumanInt32(as->parms[1].items->data, &status->MaxQuota);
1464             if (code) {
1465                 fprintf(stderr, "%s: bad integer specified for quota.\n", pn);
1466                 error = 1;
1467                 continue;
1468             }
1469         }
1470         if (as->parms[2].items)
1471             offmsg = as->parms[2].items->data;
1472         input = (char *)status + sizeof(*status);
1473         *(input++) = '\0';      /* never set name: this call doesn't change vldb */
1474         if (offmsg) {
1475             if (strlen(offmsg) >= VMSGSIZE) {
1476                 fprintf(stderr,
1477                         "%s: message must be shorter than %d characters\n",
1478                         pn, VMSGSIZE);
1479                 error = 1;
1480                 continue;
1481             }
1482             strcpy(input, offmsg);
1483             blob.in_size += strlen(offmsg);
1484             input += strlen(offmsg) + 1;
1485         } else
1486             *(input++) = '\0';
1487         *(input++) = '\0';      /* Pad for old style volume "motd" */
1488         code = pioctl(ti->data, VIOCSETVOLSTAT, &blob, 1);
1489         if (code) {
1490             Die(errno, ti->data);
1491             error = 1;
1492         }
1493     }
1494     return error;
1495 }
1496
1497 /*
1498  * Why is VenusFid declared in the kernel-only section of afs.h,
1499  * if it's the exported interface of the cache manager?
1500  */
1501 struct VenusFid {
1502     afs_int32 Cell;
1503     AFSFid Fid;
1504 };
1505
1506 static int
1507 ExamineCmd(struct cmd_syndesc *as, void *arock)
1508 {
1509     afs_int32 code;
1510     struct ViceIoctl blob;
1511     struct cmd_item *ti;
1512     struct VolumeStatus *status;
1513     char *name, *offmsg;
1514     int error = 0;
1515
1516     SetDotDefault(&as->parms[0].items);
1517     for (ti = as->parms[0].items; ti; ti = ti->next) {
1518         struct VenusFid vfid;
1519
1520         /* once per file */
1521         blob.out_size = AFS_PIOCTL_MAXSIZE;
1522         blob.in_size = 0;
1523         blob.out = space;
1524         code = pioctl(ti->data, VIOCGETVOLSTAT, &blob, 1);
1525         if (code) {
1526             Die(errno, ti->data);
1527             error = 1;
1528             continue;
1529         }
1530         status = (VolumeStatus *) space;
1531         name = (char *)status + sizeof(*status);
1532         offmsg = name + strlen(name) + 1;
1533
1534         blob.out_size = sizeof(struct VenusFid);
1535         blob.out = (char *) &vfid;
1536         if (0 == pioctl(ti->data, VIOCGETFID, &blob, 1)) {
1537             printf("File %s (%u.%u.%u) contained in volume %u\n",
1538                    ti->data, vfid.Fid.Volume, vfid.Fid.Vnode, vfid.Fid.Unique,
1539                    vfid.Fid.Volume);
1540         }
1541
1542         PrintStatus(status, name, offmsg);
1543     }
1544     return error;
1545 }
1546
1547 static int
1548 ListQuotaCmd(struct cmd_syndesc *as, void *arock)
1549 {
1550     afs_int32 code;
1551     struct ViceIoctl blob;
1552     struct cmd_item *ti;
1553     struct VolumeStatus *status;
1554     char *name;
1555     int error = 0;
1556     int human = 0;
1557
1558     if (as->parms[1].items)
1559         human = 1;
1560
1561     printf("%-25s%-11s%-11s%-7s%-11s\n", "Volume Name", "      Quota",
1562            "       Used", " %Used", "  Partition");
1563     SetDotDefault(&as->parms[0].items);
1564     for (ti = as->parms[0].items; ti; ti = ti->next) {
1565         /* once per file */
1566         blob.out_size = AFS_PIOCTL_MAXSIZE;
1567         blob.in_size = 0;
1568         blob.out = space;
1569         code = pioctl(ti->data, VIOCGETVOLSTAT, &blob, 1);
1570         if (code) {
1571             Die(errno, ti->data);
1572             error = 1;
1573             continue;
1574         }
1575         status = (VolumeStatus *) space;
1576         name = (char *)status + sizeof(*status);
1577         QuickPrintStatus(status, name, human);
1578     }
1579     return error;
1580 }
1581
1582 static int
1583 WhereIsCmd(struct cmd_syndesc *as, void *arock)
1584 {
1585     afs_int32 code;
1586     struct ViceIoctl blob;
1587     struct cmd_item *ti;
1588     int j;
1589     afs_int32 *hosts;
1590     char *tp;
1591     int error = 0;
1592
1593     SetDotDefault(&as->parms[0].items);
1594     for (ti = as->parms[0].items; ti; ti = ti->next) {
1595         /* once per file */
1596         blob.out_size = AFS_PIOCTL_MAXSIZE;
1597         blob.in_size = 0;
1598         blob.out = space;
1599         memset(space, 0, sizeof(space));
1600         code = pioctl(ti->data, VIOCWHEREIS, &blob, 1);
1601         if (code) {
1602             Die(errno, ti->data);
1603             error = 1;
1604             continue;
1605         }
1606         hosts = (afs_int32 *) space;
1607         printf("File %s is on host%s ", ti->data,
1608                (hosts[0] && !hosts[1]) ? "" : "s");
1609         for (j = 0; j < AFS_MAXHOSTS; j++) {
1610             if (hosts[j] == 0)
1611                 break;
1612             tp = hostutil_GetNameByINet(hosts[j]);
1613             printf("%s ", tp);
1614         }
1615         printf("\n");
1616     }
1617     return error;
1618 }
1619
1620
1621 static int
1622 DiskFreeCmd(struct cmd_syndesc *as, void *arock)
1623 {
1624     afs_int32 code;
1625     struct ViceIoctl blob;
1626     struct cmd_item *ti;
1627     char *name;
1628     struct VolumeStatus *status;
1629     int error = 0;
1630     int human = 0;
1631
1632     if (as->parms[1].items)
1633         human = 1;
1634
1635     printf("%-25s%10s%10s%10s%6s\n", "Volume Name",
1636            human ? "total" : "kbytes", "used", "avail", "%used");
1637     SetDotDefault(&as->parms[0].items);
1638     for (ti = as->parms[0].items; ti; ti = ti->next) {
1639         /* once per file */
1640         blob.out_size = AFS_PIOCTL_MAXSIZE;
1641         blob.in_size = 0;
1642         blob.out = space;
1643         code = pioctl(ti->data, VIOCGETVOLSTAT, &blob, 1);
1644         if (code) {
1645             Die(errno, ti->data);
1646             error = 1;
1647             continue;
1648         }
1649         status = (VolumeStatus *) space;
1650         name = (char *)status + sizeof(*status);
1651         QuickPrintSpace(status, name, human);
1652     }
1653     return error;
1654 }
1655
1656 static int
1657 QuotaCmd(struct cmd_syndesc *as, void *arock)
1658 {
1659     afs_int32 code;
1660     struct ViceIoctl blob;
1661     struct cmd_item *ti;
1662     double quotaPct;
1663     struct VolumeStatus *status;
1664     int error = 0;
1665
1666     SetDotDefault(&as->parms[0].items);
1667     for (ti = as->parms[0].items; ti; ti = ti->next) {
1668         /* once per file */
1669         blob.out_size = AFS_PIOCTL_MAXSIZE;
1670         blob.in_size = 0;
1671         blob.out = space;
1672         code = pioctl(ti->data, VIOCGETVOLSTAT, &blob, 1);
1673         if (code) {
1674             Die(errno, ti->data);
1675             error = 1;
1676             continue;
1677         }
1678         status = (VolumeStatus *) space;
1679         if (status->MaxQuota)
1680             quotaPct =
1681                 ((((double)status->BlocksInUse) / status->MaxQuota) * 100.0);
1682         else
1683             quotaPct = 0.0;
1684         printf("%2.0f%% of quota used.\n", quotaPct);
1685     }
1686     return error;
1687 }
1688
1689 static int
1690 ListMountCmd(struct cmd_syndesc *as, void *arock)
1691 {
1692     afs_int32 code;
1693     struct ViceIoctl blob;
1694     struct cmd_item *ti;
1695     char orig_name[1024];       /*Original name, may be modified */
1696     char true_name[1024];       /*``True'' dirname (e.g., symlink target) */
1697     char parent_dir[1024];      /*Parent directory of true name */
1698     char *last_component;       /*Last component of true name */
1699     struct stat statbuff;       /*Buffer for status info */
1700     int link_chars_read;        /*Num chars read in readlink() */
1701     int thru_symlink;           /*Did we get to a mount point via a symlink? */
1702     int error = 0;
1703
1704     for (ti = as->parms[0].items; ti; ti = ti->next) {
1705         /* once per file */
1706         thru_symlink = 0;
1707         sprintf(orig_name, "%s%s", (ti->data[0] == '/') ? "" : "./",
1708                 ti->data);
1709
1710         if (lstat(orig_name, &statbuff) < 0) {
1711             /* if lstat fails, we should still try the pioctl, since it
1712              * may work (for example, lstat will fail, but pioctl will
1713              * work if the volume of offline (returning ENODEV). */
1714             statbuff.st_mode = S_IFDIR; /* lie like pros */
1715         }
1716
1717         /*
1718          * The lstat succeeded.  If the given file is a symlink, substitute
1719          * the file name with the link name.
1720          */
1721         if ((statbuff.st_mode & S_IFMT) == S_IFLNK) {
1722             thru_symlink = 1;
1723             /*
1724              * Read name of resolved file.
1725              */
1726             link_chars_read = readlink(orig_name, true_name, 1024);
1727             if (link_chars_read <= 0) {
1728                 fprintf(stderr,
1729                         "%s: Can't read target name for '%s' symbolic link!\n",
1730                         pn, orig_name);
1731                 error = 1;
1732                 continue;
1733             }
1734
1735             /*
1736              * Add a trailing null to what was read, bump the length.
1737              */
1738             true_name[link_chars_read++] = 0;
1739
1740             /*
1741              * If the symlink is an absolute pathname, we're fine.  Otherwise, we
1742              * have to create a full pathname using the original name and the
1743              * relative symlink name.  Find the rightmost slash in the original
1744              * name (we know there is one) and splice in the symlink value.
1745              */
1746             if (true_name[0] != '/') {
1747                 last_component = (char *)strrchr(orig_name, '/');
1748                 strcpy(++last_component, true_name);
1749                 strcpy(true_name, orig_name);
1750             }
1751         } else
1752             strcpy(true_name, orig_name);
1753
1754         /*
1755          * Find rightmost slash, if any.
1756          */
1757         last_component = (char *)strrchr(true_name, '/');
1758         if (last_component == (char *)true_name) {
1759             strcpy(parent_dir, "/");
1760             last_component++;
1761         }
1762         else if (last_component != (char *)NULL) {
1763             /*
1764              * Found it.  Designate everything before it as the parent directory,
1765              * everything after it as the final component.
1766              */
1767             strncpy(parent_dir, true_name, last_component - true_name);
1768             parent_dir[last_component - true_name] = 0;
1769             last_component++;   /*Skip the slash */
1770         } else {
1771             /*
1772              * No slash appears in the given file name.  Set parent_dir to the current
1773              * directory, and the last component as the given name.
1774              */
1775             strcpy(parent_dir, ".");
1776             last_component = true_name;
1777         }
1778
1779         if (strcmp(last_component, ".") == 0
1780             || strcmp(last_component, "..") == 0) {
1781             fprintf(stderr,
1782                     "%s: you may not use '.' or '..' as the last component\n",
1783                     pn);
1784             fprintf(stderr, "%s: of a name in the 'fs lsmount' command.\n",
1785                     pn);
1786             error = 1;
1787             continue;
1788         }
1789
1790         blob.in = last_component;
1791         blob.in_size = strlen(last_component) + 1;
1792         blob.out_size = AFS_PIOCTL_MAXSIZE;
1793         blob.out = space;
1794         memset(space, 0, AFS_PIOCTL_MAXSIZE);
1795
1796         code = pioctl(parent_dir, VIOC_AFS_STAT_MT_PT, &blob, 1);
1797
1798         if (code == 0) {
1799             printf("'%s' is a %smount point for volume '%s'\n", ti->data,
1800                    (thru_symlink ? "symbolic link, leading to a " : ""),
1801                    space);
1802         } else {
1803             if (errno == EINVAL) {
1804                 fprintf(stderr, "'%s' is not a mount point.\n", ti->data);
1805             } else {
1806                 Die(errno, (ti->data ? ti->data : parent_dir));
1807             }
1808             error = 1;
1809         }
1810     }
1811     return error;
1812 }
1813
1814 static int
1815 MakeMountCmd(struct cmd_syndesc *as, void *arock)
1816 {
1817     afs_int32 code;
1818     char *cellName, *volName, *tmpName;
1819     struct afsconf_cell info;
1820     struct vldbentry vldbEntry;
1821     struct ViceIoctl blob;
1822
1823 /*
1824
1825 defect #3069
1826
1827     if (as->parms[5].items && !as->parms[2].items) {
1828         fprintf(stderr, "%s: must provide cell when creating cellular mount point.\n", pn);
1829         return 1;
1830     }
1831 */
1832
1833     if (as->parms[2].items)     /* cell name specified */
1834         cellName = as->parms[2].items->data;
1835     else
1836         cellName = NULL;
1837     volName = as->parms[1].items->data;
1838
1839     if (strlen(volName) >= 64) {
1840         fprintf(stderr,
1841                 "%s: volume name too long (length must be < 64 characters)\n",
1842                 pn);
1843         return 1;
1844     }
1845
1846     /* Check for a cellname in the volume specification, and complain
1847      * if it doesn't match what was specified with -cell */
1848     if ((tmpName = strchr(volName, ':'))) {
1849         *tmpName = '\0';
1850         if (cellName) {
1851             if (strcasecmp(cellName, volName)) {
1852                 fprintf(stderr, "%s: cellnames do not match.\n", pn);
1853                 return 1;
1854             }
1855         }
1856         cellName = volName;
1857         volName = ++tmpName;
1858     }
1859
1860     if (!InAFS(Parent(as->parms[0].items->data))) {
1861         fprintf(stderr,
1862                 "%s: mount points must be created within the AFS file system\n",
1863                 pn);
1864         return 1;
1865     }
1866
1867     if (!cellName) {
1868         blob.in_size = 0;
1869         blob.out_size = AFS_PIOCTL_MAXSIZE;
1870         blob.out = space;
1871         code =
1872             pioctl(Parent(as->parms[0].items->data), VIOC_FILE_CELL_NAME,
1873                    &blob, 1);
1874     }
1875
1876     code = GetCellName(cellName ? cellName : space, &info);
1877     if (code) {
1878         return 1;
1879     }
1880     if (!(as->parms[4].items)) {
1881         /* not fast, check which cell the mountpoint is being created in */
1882         /* not fast, check name with VLDB */
1883         code = VLDBInit(1, &info);
1884         if (code == 0) {
1885             /* make the check.  Don't complain if there are problems with init */
1886             code =
1887                 ubik_VL_GetEntryByNameO(uclient, 0, volName,
1888                           &vldbEntry);
1889             if (code == VL_NOENT) {
1890                 fprintf(stderr,
1891                         "%s: warning, volume %s does not exist in cell %s.\n",
1892                         pn, volName, cellName ? cellName : space);
1893             }
1894         }
1895     }
1896
1897     if (as->parms[3].items)     /* if -rw specified */
1898         strcpy(space, "%");
1899     else
1900         strcpy(space, "#");
1901     if (cellName) {
1902         /* cellular mount point, prepend cell prefix */
1903         strcat(space, info.name);
1904         strcat(space, ":");
1905     }
1906     strcat(space, volName);     /* append volume name */
1907     strcat(space, ".");         /* stupid convention; these end with a period */
1908     code = symlink(space, as->parms[0].items->data);
1909     if (code) {
1910         Die(errno, as->parms[0].items->data);
1911         return 1;
1912     }
1913     return 0;
1914 }
1915
1916 /*
1917  * Delete AFS mount points.  Variables are used as follows:
1918  *      tbuffer: Set to point to the null-terminated directory name of the mount point
1919  *          (or ``.'' if none is provided)
1920  *      tp: Set to point to the actual name of the mount point to nuke.
1921  */
1922 static int
1923 RemoveMountCmd(struct cmd_syndesc *as, void *arock)
1924 {
1925     afs_int32 code = 0;
1926     struct ViceIoctl blob;
1927     struct cmd_item *ti;
1928     char tbuffer[1024];
1929     char lsbuffer[1024];
1930     char *tp;
1931     int error = 0;
1932
1933     for (ti = as->parms[0].items; ti; ti = ti->next) {
1934         /* once per file */
1935         tp = (char *)strrchr(ti->data, '/');
1936         if (tp) {
1937             strncpy(tbuffer, ti->data, code = tp - ti->data);   /* the dir name */
1938             tbuffer[code] = 0;
1939             tp++;               /* skip the slash */
1940         } else {
1941             strcpy(tbuffer, ".");
1942             tp = ti->data;
1943         }
1944         blob.in = tp;
1945         blob.in_size = strlen(tp) + 1;
1946         blob.out = lsbuffer;
1947         blob.out_size = sizeof(lsbuffer);
1948         code = pioctl(tbuffer, VIOC_AFS_STAT_MT_PT, &blob, 1);
1949         if (code) {
1950             if (errno == EINVAL) {
1951                 fprintf(stderr, "%s: '%s' is not a mount point.\n", pn,
1952                         ti->data);
1953             } else {
1954                 Die(errno, ti->data);
1955             }
1956             error = 1;
1957             continue;           /* don't bother trying */
1958         }
1959         blob.out_size = 0;
1960         blob.in = tp;
1961         blob.in_size = strlen(tp) + 1;
1962         code = pioctl(tbuffer, VIOC_AFS_DELETE_MT_PT, &blob, 1);
1963         if (code) {
1964             Die(errno, ti->data);
1965             error = 1;
1966         }
1967     }
1968     return error;
1969 }
1970
1971 /*
1972 */
1973
1974 static int
1975 CheckServersCmd(struct cmd_syndesc *as, void *arock)
1976 {
1977     afs_int32 code;
1978     struct ViceIoctl blob;
1979     afs_int32 j;
1980     afs_int32 temp;
1981     char *tp;
1982     struct afsconf_cell info;
1983     struct chservinfo checkserv;
1984
1985     memset(&checkserv, 0, sizeof(struct chservinfo));
1986     blob.in_size = sizeof(struct chservinfo);
1987     blob.in = (caddr_t) & checkserv;
1988
1989     blob.out_size = AFS_PIOCTL_MAXSIZE;
1990     blob.out = space;
1991     memset(space, 0, sizeof(afs_int32));        /* so we assure zero when nothing is copied back */
1992
1993     /* prepare flags for checkservers command */
1994     temp = 2;                   /* default to checking local cell only */
1995     if (as->parms[2].items)
1996         temp |= 1;              /* set fast flag */
1997     if (as->parms[1].items)
1998         temp &= ~2;             /* turn off local cell check */
1999
2000     checkserv.magic = 0x12345678;       /* XXX */
2001     checkserv.tflags = temp;
2002
2003     /* now copy in optional cell name, if specified */
2004     if (as->parms[0].items) {
2005         code = GetCellName(as->parms[0].items->data, &info);
2006         if (code) {
2007             return 1;
2008         }
2009         strcpy(checkserv.tbuffer, info.name);
2010         checkserv.tsize = strlen(info.name) + 1;
2011     } else {
2012         strcpy(checkserv.tbuffer, "\0");
2013         checkserv.tsize = 0;
2014     }
2015
2016     if (as->parms[3].items) {
2017         checkserv.tinterval = atol(as->parms[3].items->data);
2018
2019         /* sanity check */
2020         if (checkserv.tinterval < 0) {
2021             printf
2022                 ("Warning: The negative -interval is ignored; treated as an inquiry\n");
2023             checkserv.tinterval = 0;
2024         } else if (checkserv.tinterval > 600) {
2025             printf
2026                 ("Warning: The maximum -interval value is 10 mins (600 secs)\n");
2027             checkserv.tinterval = 600;  /* 10 min max interval */
2028         }
2029     } else {
2030         checkserv.tinterval = -1;       /* don't change current interval */
2031     }
2032
2033     code = pioctl(0, VIOCCKSERV, &blob, 1);
2034     if (code) {
2035         if ((errno == EACCES) && (checkserv.tinterval > 0)) {
2036             printf("Must be root to change -interval\n");
2037             return 1;
2038         }
2039         Die(errno, 0);
2040         return 1;
2041     }
2042     memcpy(&temp, space, sizeof(afs_int32));
2043     if (checkserv.tinterval >= 0) {
2044         if (checkserv.tinterval > 0)
2045             printf
2046                 ("The new down server probe interval (%d secs) is now in effect (old interval was %d secs)\n",
2047                  checkserv.tinterval, temp);
2048         else
2049             printf("The current down server probe interval is %d secs\n",
2050                    temp);
2051         return 0;
2052     }
2053     if (temp == 0) {
2054         printf("All servers are running.\n");
2055     } else {
2056         printf
2057             ("These servers unavailable due to network or server problems: ");
2058         for (j = 0;; j++) {
2059             memcpy(&temp, space + j * sizeof(afs_int32), sizeof(afs_int32));
2060             if (temp == 0)
2061                 break;
2062             tp = hostutil_GetNameByINet(temp);
2063             printf(" %s", tp);
2064         }
2065         printf(".\n");
2066         code = 1;               /* XXX */
2067     }
2068     return code;
2069 }
2070
2071 static int
2072 MessagesCmd(struct cmd_syndesc *as, void *arock)
2073 {
2074     afs_int32 code = 0;
2075     struct ViceIoctl blob;
2076     struct gaginfo gagflags;
2077     struct cmd_item *show;
2078
2079     memset(&gagflags, 0, sizeof(struct gaginfo));
2080     blob.in_size = sizeof(struct gaginfo);
2081     blob.in = (caddr_t) & gagflags;
2082     blob.out_size = AFS_PIOCTL_MAXSIZE;
2083     blob.out = space;
2084     memset(space, 0, sizeof(afs_int32));        /* so we assure zero when nothing is copied back */
2085
2086     if ((show = as->parms[0].items)) {
2087         if (!strcasecmp(show->data, "user"))
2088             gagflags.showflags |= GAGUSER;
2089         else if (!strcasecmp(show->data, "console"))
2090             gagflags.showflags |= GAGCONSOLE;
2091         else if (!strcasecmp(show->data, "all"))
2092             gagflags.showflags |= GAGCONSOLE | GAGUSER;
2093         else if (!strcasecmp(show->data, "none"))
2094             /* do nothing */ ;
2095         else {
2096             fprintf(stderr,
2097                     "unrecognized flag %s: must be in {user,console,all,none}\n",
2098                     show->data);
2099             code = EINVAL;
2100         }
2101     }
2102
2103     if (code)
2104         return 1;
2105
2106     code = pioctl(0, VIOC_GAG, &blob, 1);
2107     if (code) {
2108         Die(errno, 0);
2109         return 1;
2110     }
2111
2112     return 0;
2113 }
2114
2115 static int
2116 CheckVolumesCmd(struct cmd_syndesc *as, void *arock)
2117 {
2118     afs_int32 code;
2119     struct ViceIoctl blob;
2120
2121     blob.in_size = 0;
2122     blob.out_size = 0;
2123     code = pioctl(0, VIOCCKBACK, &blob, 1);
2124     if (code) {
2125         Die(errno, 0);
2126         return 1;
2127     }
2128
2129     printf("All volumeID/name mappings checked.\n");
2130     return 0;
2131 }
2132
2133 static int
2134 PreCacheCmd(struct cmd_syndesc *as, void *arock)
2135 {
2136     afs_int32 code;
2137     struct ViceIoctl blob;
2138     afs_int32 temp;
2139
2140     if (!as->parms[0].items && !as->parms[1].items) {
2141         fprintf(stderr, "%s: syntax error in precache cmd.\n", pn);
2142         return 1;
2143     }
2144     if (as->parms[0].items) {
2145         code = util_GetInt32(as->parms[0].items->data, &temp);
2146         if (code) {
2147             fprintf(stderr, "%s: bad integer specified for precache size.\n",
2148                     pn);
2149             return 1;
2150         }
2151     } else
2152         temp = 0;
2153     blob.in = (char *)&temp;
2154     blob.in_size = sizeof(afs_int32);
2155     blob.out_size = 0;
2156     code = pioctl(0, VIOCPRECACHE, &blob, 1);
2157     if (code) {
2158         Die(errno, NULL);
2159         return 1;
2160     }
2161
2162     printf("New precache size set.\n");
2163     return 0;
2164 }
2165
2166 static int
2167 SetCacheSizeCmd(struct cmd_syndesc *as, void *arock)
2168 {
2169     afs_int32 code;
2170     struct ViceIoctl blob;
2171     afs_int32 temp;
2172
2173     if (!as->parms[0].items && !as->parms[1].items) {
2174         fprintf(stderr, "%s: syntax error in setcachesize cmd.\n", pn);
2175         return 1;
2176     }
2177     if (as->parms[0].items) {
2178         code = util_GetHumanInt32(as->parms[0].items->data, &temp);
2179         if (code) {
2180             fprintf(stderr, "%s: bad integer specified for cache size.\n",
2181                     pn);
2182             return 1;
2183         }
2184     } else
2185         temp = 0;
2186     blob.in = (char *)&temp;
2187     blob.in_size = sizeof(afs_int32);
2188     blob.out_size = 0;
2189     code = pioctl(0, VIOCSETCACHESIZE, &blob, 1);
2190     if (code) {
2191         if (errno == EROFS) {
2192             printf
2193                 ("'fs setcache' not allowed on memory cache based cache managers.\n");
2194         } else {
2195             Die(errno, NULL);
2196         }
2197         return 1;
2198     }
2199
2200     printf("New cache size set.\n");
2201     return 0;
2202 }
2203
2204 #define MAXGCSIZE       16
2205 static int
2206 GetCacheParmsCmd(struct cmd_syndesc *as, void *arock)
2207 {
2208     afs_int32 code, filesUsed;
2209     struct ViceIoctl blob;
2210     afs_int32 parms[MAXGCSIZE];
2211     double percentFiles, percentBlocks;
2212     afs_int32 flags = 0;
2213
2214     if (as->parms[0].items){ /* -files */
2215         flags = 1;
2216     } else if (as->parms[1].items){ /* -excessive */
2217         flags = 2;
2218     } else {
2219         flags = 0;
2220     }
2221
2222     memset(parms, '\0', sizeof parms);  /* avoid Purify UMR error */
2223     if (flags){
2224         blob.in = (char *)&flags;
2225         blob.in_size = sizeof(afs_int32);
2226     } else {    /* be backward compatible */
2227         blob.in = NULL;
2228         blob.in_size = 0;
2229     }
2230     blob.out_size = sizeof(parms);
2231     blob.out = (char *)parms;
2232     code = pioctl(0, VIOCGETCACHEPARMS, &blob, 1);
2233     if (code) {
2234         Die(errno, NULL);
2235         return 1;
2236     }
2237
2238     if (!flags){
2239         printf("AFS using %d of the cache's available %d 1K byte blocks.\n",
2240                 parms[1], parms[0]);
2241         if (parms[1] > parms[0])
2242                 printf("[Cache guideline temporarily deliberately exceeded; it will be adjusted down but you may wish to increase the cache size.]\n");
2243         return 0;
2244     }
2245
2246     percentBlocks = ((double)parms[1]/parms[0]) * 100;
2247     printf("AFS using %5.0f%% of cache blocks (%d of %d 1k blocks)\n",
2248            percentBlocks, parms[1], parms[0]);
2249
2250     if (parms[2] == 0)
2251         return 0;
2252
2253     filesUsed = parms[2] - parms[3];
2254     percentFiles = ((double)filesUsed/parms[2]) * 100;
2255     printf("          %5.0f%% of the cache files (%d of %d files)\n",
2256             percentFiles, filesUsed, parms[2]);
2257     if (flags == 2){
2258         printf("        afs_cacheFiles: %10d\n", parms[2]);
2259         printf("        IFFree:         %10d\n", parms[3]);
2260         printf("        IFEverUsed:     %10d\n", parms[4]);
2261         printf("        IFDataMod:      %10d\n", parms[5]);
2262         printf("        IFDirtyPages:   %10d\n", parms[6]);
2263         printf("        IFAnyPages:     %10d\n", parms[7]);
2264         printf("        IFDiscarded:    %10d\n", parms[8]);
2265         printf("        DCentries:  %10d\n", parms[9]);
2266         printf("          0k-   4K: %10d\n", parms[10]);
2267         printf("          4k-  16k: %10d\n", parms[11]);
2268         printf("         16k-  64k: %10d\n", parms[12]);
2269         printf("         64k- 256k: %10d\n", parms[13]);
2270         printf("        256k-   1M: %10d\n", parms[14]);
2271         printf("              >=1M: %10d\n", parms[15]);
2272     }
2273
2274     if (percentBlocks > 90)
2275         printf("[cache size usage over 90%%, consider increasing cache size]\n");
2276     if (percentFiles > 90)
2277         printf("[cache file usage over 90%%, consider increasing '-files' argument to afsd]\n");
2278
2279     return 0;
2280 }
2281
2282 static int
2283 ListCellsCmd(struct cmd_syndesc *as, void *arock)
2284 {
2285     afs_int32 code;
2286     afs_int32 i, j;
2287     char *tp;
2288     struct ViceIoctl blob;
2289     int resolve;
2290
2291     resolve = !(as->parms[0].items);    /* -numeric */
2292
2293     for (i = 0;; i++) {
2294         tp = space;
2295         memcpy(tp, &i, sizeof(afs_int32));
2296         blob.out_size = AFS_PIOCTL_MAXSIZE;
2297         blob.in_size = sizeof(afs_int32);
2298         blob.in = space;
2299         blob.out = space;
2300         code = pioctl(0, VIOCGETCELL, &blob, 1);
2301         if (code < 0) {
2302             if (errno == EDOM)
2303                 break;          /* done with the list */
2304             Die(errno, 0);
2305             return 1;
2306         }
2307         tp = space;
2308         printf("Cell %s on hosts", tp + AFS_MAXCELLHOSTS * sizeof(afs_int32));
2309         for (j = 0; j < AFS_MAXCELLHOSTS; j++) {
2310             afs_int32 addr;
2311             char *name, tbuffer[20];
2312
2313             memcpy(&addr, tp + j * sizeof(afs_int32), sizeof(afs_int32));
2314             if (addr == 0)
2315                 break;
2316
2317             if (resolve) {
2318                 name = hostutil_GetNameByINet(addr);
2319             } else {
2320                 addr = ntohl(addr);
2321                 sprintf(tbuffer, "%d.%d.%d.%d", (addr >> 24) & 0xff,
2322                         (addr >> 16) & 0xff, (addr >> 8) & 0xff, addr & 0xff);
2323                 name = tbuffer;
2324             }
2325             printf(" %s", name);
2326         }
2327         printf(".\n");
2328     }
2329     return 0;
2330 }
2331
2332 static int
2333 ListAliasesCmd(struct cmd_syndesc *as, void *arock)
2334 {
2335     afs_int32 code, i;
2336     char *tp, *aliasName, *realName;
2337     struct ViceIoctl blob;
2338
2339     for (i = 0;; i++) {
2340         tp = space;
2341         memcpy(tp, &i, sizeof(afs_int32));
2342         blob.out_size = AFS_PIOCTL_MAXSIZE;
2343         blob.in_size = sizeof(afs_int32);
2344         blob.in = space;
2345         blob.out = space;
2346         code = pioctl(0, VIOC_GETALIAS, &blob, 1);
2347         if (code < 0) {
2348             if (errno == EDOM)
2349                 break;          /* done with the list */
2350             Die(errno, 0);
2351             return 1;
2352         }
2353         tp = space;
2354         aliasName = tp;
2355         tp += strlen(aliasName) + 1;
2356         realName = tp;
2357         printf("Alias %s for cell %s\n", aliasName, realName);
2358     }
2359     return 0;
2360 }
2361
2362 static int
2363 CallBackRxConnCmd(struct cmd_syndesc *as, void *arock)
2364 {
2365     afs_int32 code;
2366     struct ViceIoctl blob;
2367     struct cmd_item *ti;
2368     afs_int32 hostAddr;
2369     struct hostent *thp;
2370     int setp;
2371
2372     ti = as->parms[0].items;
2373     setp = 1;
2374     if (ti) {
2375         thp = hostutil_GetHostByName(ti->data);
2376         if (!thp) {
2377             fprintf(stderr, "host %s not found in host table.\n", ti->data);
2378             return 1;
2379         }
2380         else memcpy(&hostAddr, thp->h_addr, sizeof(afs_int32));
2381     } else {
2382         hostAddr = 0;   /* means don't set host */
2383         setp = 0;       /* aren't setting host */
2384     }
2385
2386     /* now do operation */
2387     blob.in_size = sizeof(afs_int32);
2388     blob.out_size = sizeof(afs_int32);
2389     blob.in = (char *) &hostAddr;
2390     blob.out = (char *) &hostAddr;
2391
2392     code = pioctl(0, VIOC_CBADDR, &blob, 1);
2393     if (code < 0) {
2394         Die(errno, 0);
2395         return 1;
2396     }
2397     return 0;
2398 }
2399
2400 static int
2401 NukeNFSCredsCmd(struct cmd_syndesc *as, void *arock)
2402 {
2403     afs_int32 code;
2404     struct ViceIoctl blob;
2405     struct cmd_item *ti;
2406     afs_int32 hostAddr;
2407     struct hostent *thp;
2408
2409     ti = as->parms[0].items;
2410     thp = hostutil_GetHostByName(ti->data);
2411     if (!thp) {
2412         fprintf(stderr, "host %s not found in host table.\n", ti->data);
2413         return 1;
2414     }
2415     else memcpy(&hostAddr, thp->h_addr, sizeof(afs_int32));
2416
2417     /* now do operation */
2418     blob.in_size = sizeof(afs_int32);
2419     blob.out_size = sizeof(afs_int32);
2420     blob.in = (char *) &hostAddr;
2421     blob.out = (char *) &hostAddr;
2422
2423     code = pioctl(0, VIOC_NFS_NUKE_CREDS, &blob, 1);
2424     if (code < 0) {
2425         Die(errno, 0);
2426         return 1;
2427     }
2428     return 0;
2429 }
2430
2431 static int
2432 NewCellCmd(struct cmd_syndesc *as, void *arock)
2433 {
2434     afs_int32 code, linkedstate = 0, size = 0, *lp;
2435     struct ViceIoctl blob;
2436     struct cmd_item *ti;
2437     char *tp, *cellname = 0;
2438     struct hostent *thp;
2439     afs_int32 fsport = 0, vlport = 0;
2440     afs_int32 scount;           /* Number of servers to pass in pioctl call */
2441
2442     /* Yuck!
2443      * With the NEWCELL pioctl call, 3.4 clients take an array of
2444      * AFS_MAXHOSTS (13) servers while 3.5 clients take an array of
2445      * AFS_MAXCELLHOSTS (8) servers. To determine which we are talking to,
2446      * do a GETCELL pioctl and pass it a magic number. If an array of
2447      * 8 comes back, its a 3.5 client. If not, its a 3.4 client.
2448      * If we get back EDOM, there are no cells in the kernel yet,
2449      * and we'll assume a 3.5 client.
2450      */
2451     tp = space;
2452     lp = (afs_int32 *) tp;
2453     *lp++ = 0;                  /* first cell entry */
2454     *lp = 0x12345678;           /* magic */
2455     blob.out_size = AFS_PIOCTL_MAXSIZE;
2456     blob.in_size = sizeof(afs_int32) + sizeof(afs_int32);
2457     blob.in = space;
2458     blob.out = space;
2459     code = pioctl(0, VIOCGETCELL, &blob, 1);
2460     if (code < 0 && errno != EDOM) {
2461         Die(errno, 0);
2462         return 1;
2463     }
2464     if (code < 1 && errno == EDOM) {
2465         scount = AFS_MAXHOSTS;
2466     } else {
2467         tp = space;
2468         cellname = tp + AFS_MAXCELLHOSTS * sizeof(afs_int32);
2469         scount = ((cellname[0] != '\0') ? AFS_MAXCELLHOSTS : AFS_MAXHOSTS);
2470     }
2471
2472     /* Now setup and do the NEWCELL pioctl call */
2473     memset(space, 0, (scount + 1) * sizeof(afs_int32));
2474     tp = space;
2475     lp = (afs_int32 *) tp;
2476     *lp++ = 0x12345678;
2477     tp += sizeof(afs_int32);
2478     for (ti = as->parms[1].items; ti; ti = ti->next) {
2479         thp = hostutil_GetHostByName(ti->data);
2480         if (!thp) {
2481             fprintf(stderr,
2482                     "%s: Host %s not found in host table, skipping it.\n", pn,
2483                     ti->data);
2484         } else {
2485             memcpy(tp, thp->h_addr, sizeof(afs_int32));
2486             tp += sizeof(afs_int32);
2487         }
2488     }
2489     if (as->parms[2].items) {
2490         /*
2491          * Link the cell, for the purposes of volume location, to the specified
2492          * cell.
2493          */
2494         cellname = as->parms[2].items->data;
2495         linkedstate = 1;
2496     }
2497 #ifdef FS_ENABLE_SERVER_DEBUG_PORTS
2498     if (as->parms[3].items) {
2499         code = util_GetInt32(as->parms[3].items->data, &vlport);
2500         if (code) {
2501             fprintf(stderr,
2502                     "%s: bad integer specified for the fileserver port.\n",
2503                     pn);
2504             return 1;
2505         }
2506     }
2507     if (as->parms[4].items) {
2508         code = util_GetInt32(as->parms[4].items->data, &fsport);
2509         if (code) {
2510             fprintf(stderr,
2511                     "%s: bad integer specified for the vldb server port.\n",
2512                     pn);
2513             return 1;
2514         }
2515     }
2516 #endif
2517     tp = (char *)(space + (scount + 1) * sizeof(afs_int32));
2518     lp = (afs_int32 *) tp;
2519     *lp++ = fsport;
2520     *lp++ = vlport;
2521     *lp = linkedstate;
2522     strcpy(space + ((scount + 4) * sizeof(afs_int32)),
2523            as->parms[0].items->data);
2524     size = ((scount + 4) * sizeof(afs_int32))
2525         + strlen(as->parms[0].items->data)
2526         + 1 /* for null */ ;
2527     tp = (char *)(space + size);
2528     if (linkedstate) {
2529         strcpy(tp, cellname);
2530         size += strlen(cellname) + 1;
2531     }
2532     blob.in_size = size;
2533     blob.in = space;
2534     blob.out_size = 0;
2535     code = pioctl(0, VIOCNEWCELL, &blob, 1);
2536     if (code < 0) {
2537         Die(errno, 0);
2538         return 1;
2539     }
2540     return 0;
2541 }
2542
2543 static int
2544 NewAliasCmd(struct cmd_syndesc *as, void *arock)
2545 {
2546     afs_int32 code;
2547     struct ViceIoctl blob;
2548     char *tp;
2549     char *aliasName, *realName;
2550
2551     /* Now setup and do the NEWCELL pioctl call */
2552     aliasName = as->parms[0].items->data;
2553     realName = as->parms[1].items->data;
2554     tp = space;
2555     strcpy(tp, aliasName);
2556     tp += strlen(aliasName) + 1;
2557     strcpy(tp, realName);
2558     tp += strlen(realName) + 1;
2559
2560     blob.in_size = tp - space;
2561     blob.in = space;
2562     blob.out_size = 0;
2563     blob.out = space;
2564     code = pioctl(0, VIOC_NEWALIAS, &blob, 1);
2565     if (code < 0) {
2566         if (errno == EEXIST) {
2567             fprintf(stderr,
2568                     "%s: cell name `%s' in use by an existing cell.\n", pn,
2569                     aliasName);
2570         } else {
2571             Die(errno, 0);
2572         }
2573         return 1;
2574     }
2575     return 0;
2576 }
2577
2578 static int
2579 WhichCellCmd(struct cmd_syndesc *as, void *arock)
2580 {
2581     afs_int32 code;
2582     struct cmd_item *ti;
2583     int error = 0;
2584     char cell[MAXCELLCHARS];
2585
2586     SetDotDefault(&as->parms[0].items);
2587     for (ti = as->parms[0].items; ti; ti = ti->next) {
2588         code = GetCell(ti->data, cell);
2589         if (code) {
2590             if (errno == ENOENT)
2591                 fprintf(stderr, "%s: no such cell as '%s'\n", pn, ti->data);
2592             else
2593                 Die(errno, ti->data);
2594             error = 1;
2595             continue;
2596         }
2597
2598         printf("File %s lives in cell '%s'\n", ti->data, cell);
2599     }
2600     return error;
2601 }
2602
2603 static int
2604 WSCellCmd(struct cmd_syndesc *as, void *arock)
2605 {
2606     afs_int32 code;
2607     struct ViceIoctl blob;
2608
2609     blob.in_size = 0;
2610     blob.in = NULL;
2611     blob.out_size = AFS_PIOCTL_MAXSIZE;
2612     blob.out = space;
2613
2614     code = pioctl(NULL, VIOC_GET_WS_CELL, &blob, 1);
2615     if (code) {
2616         Die(errno, NULL);
2617         return 1;
2618     }
2619
2620     printf("This workstation belongs to cell '%s'\n", space);
2621     return 0;
2622 }
2623
2624 /*
2625 static PrimaryCellCmd(as)
2626     struct cmd_syndesc *as;
2627 {
2628     fprintf(stderr, "This command is obsolete, as is the concept of a primary token.\n");
2629     return 0;
2630 }
2631 */
2632
2633 static int
2634 MonitorCmd(struct cmd_syndesc *as, void *arock)
2635 {
2636     afs_int32 code;
2637     struct ViceIoctl blob;
2638     struct cmd_item *ti;
2639     afs_int32 hostAddr;
2640     struct hostent *thp;
2641     char *tp;
2642     int setp;
2643
2644     ti = as->parms[0].items;
2645     setp = 1;
2646     if (ti) {
2647         /* set the host */
2648         if (!strcmp(ti->data, "off"))
2649             hostAddr = 0xffffffff;
2650         else {
2651             thp = hostutil_GetHostByName(ti->data);
2652             if (!thp) {
2653                 if (!strcmp(ti->data, "localhost")) {
2654                     fprintf(stderr,
2655                             "localhost not in host table, assuming 127.0.0.1\n");
2656                     hostAddr = htonl(0x7f000001);
2657                 } else {
2658                     fprintf(stderr, "host %s not found in host table.\n",
2659                             ti->data);
2660                     return 1;
2661                 }
2662             } else
2663                 memcpy(&hostAddr, thp->h_addr, sizeof(afs_int32));
2664         }
2665     } else {
2666         hostAddr = 0;           /* means don't set host */
2667         setp = 0;               /* aren't setting host */
2668     }
2669
2670     /* now do operation */
2671     blob.in_size = sizeof(afs_int32);
2672     blob.out_size = sizeof(afs_int32);
2673     blob.in = (char *)&hostAddr;
2674     blob.out = (char *)&hostAddr;
2675     code = pioctl(0, VIOC_AFS_MARINER_HOST, &blob, 1);
2676     if (code) {
2677         Die(errno, 0);
2678         return 1;
2679     }
2680     if (setp) {
2681         printf("%s: new monitor host set.\n", pn);
2682     } else {
2683         /* now decode old address */
2684         if (hostAddr == 0xffffffff) {
2685             printf("Cache monitoring is currently disabled.\n");
2686         } else {
2687             tp = hostutil_GetNameByINet(hostAddr);
2688             printf("Using host %s for monitor services.\n", tp);
2689         }
2690     }
2691     return 0;
2692 }
2693
2694 static int
2695 SysNameCmd(struct cmd_syndesc *as, void *arock)
2696 {
2697     afs_int32 code;
2698     struct ViceIoctl blob;
2699     struct cmd_item *ti;
2700     char *input = space;
2701     afs_int32 setp = 0;
2702
2703     ti = as->parms[0].items;
2704     blob.in = space;
2705     blob.out = space;
2706     blob.out_size = AFS_PIOCTL_MAXSIZE;
2707     blob.in_size = sizeof(afs_int32);
2708     input += sizeof(afs_int32);
2709     for (; ti; ti = ti->next) {
2710         setp++;
2711         blob.in_size += strlen(ti->data) + 1;
2712         if (blob.in_size > AFS_PIOCTL_MAXSIZE) {
2713             fprintf(stderr, "%s: sysname%s too long.\n", pn,
2714                     setp > 1 ? "s" : "");
2715             return 1;
2716         }
2717         strcpy(input, ti->data);
2718         input += strlen(ti->data);
2719         *(input++) = '\0';
2720     }
2721     memcpy(space, &setp, sizeof(afs_int32));
2722     code = pioctl(0, VIOC_AFS_SYSNAME, &blob, 1);
2723     if (code) {
2724         Die(errno, 0);
2725         return 1;
2726     }
2727     if (setp) {
2728         printf("%s: new sysname%s set.\n", pn, setp > 1 ? " list" : "");
2729         return 0;
2730     }
2731     input = space;
2732     memcpy(&setp, input, sizeof(afs_int32));
2733     input += sizeof(afs_int32);
2734     if (!setp) {
2735         fprintf(stderr, "No sysname name value was found\n");
2736         return 1;
2737     }
2738     printf("Current sysname%s is", setp > 1 ? " list" : "");
2739     for (; setp > 0; --setp) {
2740         printf(" \'%s\'", input);
2741         input += strlen(input) + 1;
2742     }
2743     printf("\n");
2744     return 0;
2745 }
2746
2747 static char *exported_types[] = { "null", "nfs", "" };
2748 static int
2749 ExportAfsCmd(struct cmd_syndesc *as, void *arock)
2750 {
2751     afs_int32 code;
2752     struct ViceIoctl blob;
2753     struct cmd_item *ti;
2754     int export = 0, type = 0, mode = 0, exp = 0, exportcall, pwsync =
2755         0, smounts = 0, clipags = 0, pagcb = 0;
2756
2757     ti = as->parms[0].items;
2758     if (strcmp(ti->data, "nfs") == 0)
2759         type = 0x71;            /* NFS */
2760     else {
2761         fprintf(stderr,
2762                 "Invalid exporter type, '%s', Only the 'nfs' exporter is currently supported\n",
2763                 ti->data);
2764         return 1;
2765     }
2766     ti = as->parms[1].items;
2767     if (ti) {
2768         if (strcmp(ti->data, "on") == 0)
2769             export = 3;
2770         else if (strcmp(ti->data, "off") == 0)
2771             export = 2;
2772         else {
2773             fprintf(stderr, "Illegal argument %s\n", ti->data);
2774             return 1;
2775         }
2776         exp = 1;
2777     }
2778     if ((ti = as->parms[2].items)) {    /* -noconvert */
2779         if (strcmp(ti->data, "on") == 0)
2780             mode = 2;
2781         else if (strcmp(ti->data, "off") == 0)
2782             mode = 3;
2783         else {
2784             fprintf(stderr, "Illegal argument %s\n", ti->data);
2785             return 1;
2786         }
2787     }
2788     if ((ti = as->parms[3].items)) {    /* -uidcheck */
2789         if (strcmp(ti->data, "on") == 0)
2790             pwsync = 3;
2791         else if (strcmp(ti->data, "off") == 0)
2792             pwsync = 2;
2793         else {
2794             fprintf(stderr, "Illegal argument %s\n", ti->data);
2795             return 1;
2796         }
2797     }
2798     if ((ti = as->parms[4].items)) {    /* -submounts */
2799         if (strcmp(ti->data, "on") == 0)
2800             smounts = 3;
2801         else if (strcmp(ti->data, "off") == 0)
2802             smounts = 2;
2803         else {
2804             fprintf(stderr, "Illegal argument %s\n", ti->data);
2805             return 1;
2806         }
2807     }
2808     if ((ti = as->parms[5].items)) {    /* -clipags */
2809         if (strcmp(ti->data, "on") == 0)
2810             clipags = 3;
2811         else if (strcmp(ti->data, "off") == 0)
2812             clipags = 2;
2813         else {
2814             fprintf(stderr, "Illegal argument %s\n", ti->data);
2815             return 1;
2816         }
2817     }
2818     if ((ti = as->parms[6].items)) {    /* -pagcb */
2819         if (strcmp(ti->data, "on") == 0)
2820             pagcb = 3;
2821         else if (strcmp(ti->data, "off") == 0)
2822             pagcb = 2;
2823         else {
2824             fprintf(stderr, "Illegal argument %s\n", ti->data);
2825             return 1;
2826         }
2827     }
2828     exportcall =
2829         (type << 24) | (pagcb << 10) | (clipags << 8) |
2830         (mode << 6) | (pwsync << 4) | (smounts << 2) | export;
2831     type &= ~0x70;
2832     /* make the call */
2833     blob.in = (char *)&exportcall;
2834     blob.in_size = sizeof(afs_int32);
2835     blob.out = (char *)&exportcall;
2836     blob.out_size = sizeof(afs_int32);
2837     code = pioctl(0, VIOC_EXPORTAFS, &blob, 1);
2838     if (code) {
2839         if (errno == ENODEV) {
2840             fprintf(stderr,
2841                     "Sorry, the %s-exporter type is currently not supported on this AFS client\n",
2842                     exported_types[type]);
2843         } else {
2844             Die(errno, 0);
2845         }
2846         return 1;
2847     }
2848
2849     if (exportcall & 1) {
2850         printf("'%s' translator is enabled with the following options:\n",
2851                exported_types[type]);
2852         printf("\tRunning in %s mode\n",
2853                (exportcall & 2 ? "strict unix" :
2854                 "convert owner mode bits to world/other"));
2855         printf("\tRunning in %s mode\n",
2856                (exportcall & 4 ? "strict 'passwd sync'" :
2857                 "no 'passwd sync'"));
2858         printf("\t%s\n",
2859                (exportcall & 8 ? "Allow mounts of /afs/.. subdirs" :
2860                 "Only mounts to /afs allowed"));
2861         printf("\t%s\n",
2862                (exportcall & 16 ? "Client-assigned PAG's are used" :
2863                 "Client-assigned PAG's are not used"));
2864         printf("\t%s\n",
2865                (exportcall & 32 ?
2866                 "Callbacks are made to get creds from new clients" :
2867                 "Callbacks are not made to get creds from new clients"));
2868     } else {
2869         printf("'%s' translator is disabled\n", exported_types[type]);
2870     }
2871     return 0;
2872 }
2873
2874
2875 static int
2876 GetCellCmd(struct cmd_syndesc *as, void *arock)
2877 {
2878     afs_int32 code;
2879     struct ViceIoctl blob;
2880     struct afsconf_cell info;
2881     struct cmd_item *ti;
2882     struct a {
2883         afs_int32 stat;
2884         afs_int32 junk;
2885     } args;
2886     int error = 0;
2887
2888     memset(&args, '\0', sizeof args);   /* avoid Purify UMR error */
2889     for (ti = as->parms[0].items; ti; ti = ti->next) {
2890         /* once per cell */
2891         blob.out_size = sizeof(args);
2892         blob.out = (caddr_t) & args;
2893         code = GetCellName(ti->data, &info);
2894         if (code) {
2895             error = 1;
2896             continue;
2897         }
2898         blob.in_size = 1 + strlen(info.name);
2899         blob.in = info.name;
2900         code = pioctl(0, VIOC_GETCELLSTATUS, &blob, 1);
2901         if (code) {
2902             if (errno == ENOENT)
2903                 fprintf(stderr, "%s: the cell named '%s' does not exist\n",
2904                         pn, info.name);
2905             else
2906                 Die(errno, info.name);
2907             error = 1;
2908             continue;
2909         }
2910         printf("Cell %s status: ", info.name);
2911 #ifdef notdef
2912         if (args.stat & 1)
2913             printf("primary ");
2914 #endif
2915         if (args.stat & 2)
2916             printf("no setuid allowed");
2917         else
2918             printf("setuid allowed");
2919         if (args.stat & 4)
2920             printf(", using old VLDB");
2921         printf("\n");
2922     }
2923     return error;
2924 }
2925
2926 static int
2927 SetCellCmd(struct cmd_syndesc *as, void *arock)
2928 {
2929     afs_int32 code;
2930     struct ViceIoctl blob;
2931     struct afsconf_cell info;
2932     struct cmd_item *ti;
2933     struct a {
2934         afs_int32 stat;
2935         afs_int32 junk;
2936         char cname[64];
2937     } args;
2938     int error = 0;
2939
2940     /* Check arguments. */
2941     if (as->parms[1].items && as->parms[2].items) {
2942         fprintf(stderr, "Cannot specify both -suid and -nosuid.\n");
2943         return 1;
2944     }
2945
2946     /* figure stuff to set */
2947     args.stat = 0;
2948     args.junk = 0;
2949
2950     if (!as->parms[1].items)
2951         args.stat |= 2;         /* default to -nosuid */
2952
2953     /* set stat for all listed cells */
2954     for (ti = as->parms[0].items; ti; ti = ti->next) {
2955         /* once per cell */
2956         code = GetCellName(ti->data, &info);
2957         if (code) {
2958             error = 1;
2959             continue;
2960         }
2961         strcpy(args.cname, info.name);
2962         blob.in_size = sizeof(args);
2963         blob.in = (caddr_t) & args;
2964         blob.out_size = 0;
2965         blob.out = (caddr_t) 0;
2966         code = pioctl(0, VIOC_SETCELLSTATUS, &blob, 1);
2967         if (code) {
2968             Die(errno, info.name);      /* XXX added cell name to Die() call */
2969             error = 1;
2970         }
2971     }
2972     return error;
2973 }
2974
2975 static int
2976 GetCellName(char *cellName, struct afsconf_cell *info)
2977 {
2978     struct afsconf_dir *tdir;
2979     int code;
2980
2981     tdir = afsconf_Open(AFSDIR_CLIENT_ETC_DIRPATH);
2982     if (!tdir) {
2983         fprintf(stderr,
2984                 "Could not process files in configuration directory (%s).\n",
2985                 AFSDIR_CLIENT_ETC_DIRPATH);
2986         return -1;
2987     }
2988
2989     code = afsconf_GetCellInfo(tdir, cellName, AFSCONF_VLDBSERVICE, info);
2990     if (code) {
2991         fprintf(stderr, "%s: cell %s not in %s\n", pn, cellName,
2992                 AFSDIR_CLIENT_CELLSERVDB_FILEPATH);
2993         return code;
2994     }
2995
2996     return 0;
2997 }
2998
2999
3000 static int
3001 VLDBInit(int noAuthFlag, struct afsconf_cell *info)
3002 {
3003     afs_int32 code;
3004
3005     code = ugen_ClientInit(noAuthFlag, (char *) AFSDIR_CLIENT_ETC_DIRPATH,
3006                            info->name, 0, &uclient,
3007                            NULL, pn, rxkad_clear,
3008                            VLDB_MAXSERVERS, AFSCONF_VLDBSERVICE, 50,
3009                            0, 0, USER_SERVICE_ID);
3010     rxInitDone = 1;
3011     return code;
3012 }
3013
3014 static struct ViceIoctl gblob;
3015 static int debug = 0;
3016 /*
3017  * here follow some routines in suport of the setserverprefs and
3018  * getserverprefs commands.  They are:
3019  * SetPrefCmd  "top-level" routine
3020  * addServer   adds a server to the list of servers to be poked into the
3021  *             kernel.  Will poke the list into the kernel if it threatens
3022  *             to get too large.
3023  * pokeServers pokes the existing list of servers and ranks into the kernel
3024  * GetPrefCmd  reads the Cache Manager's current list of server ranks
3025  */
3026
3027 /*
3028  * returns -1 if error message printed,
3029  * 0 on success,
3030  * errno value if error and no error message printed
3031  */
3032 static int
3033 pokeServers(void)
3034 {
3035     int code;
3036
3037     code = pioctl(0, VIOC_SETSPREFS, &gblob, 1);
3038     if (code && (errno == EINVAL)) {
3039         struct setspref *ssp;
3040         ssp = (struct setspref *)gblob.in;
3041         if (!(ssp->flags & DBservers)) {
3042             gblob.in = (void *)&(ssp->servers[0]);
3043             gblob.in_size -= ((char *)&(ssp->servers[0])) - (char *)ssp;
3044             code = pioctl(0, VIOC_SETSPREFS33, &gblob, 1);
3045             return code ? errno : 0;
3046         }
3047         fprintf(stderr,
3048                 "This cache manager does not support VL server preferences.\n");
3049         return -1;
3050     }
3051
3052     return code ? errno : 0;
3053 }
3054
3055 /*
3056  * returns -1 if error message printed,
3057  * 0 on success,
3058  * errno value if error and no error message printed
3059  */
3060 static int
3061 addServer(char *name, afs_int32 rank)
3062 {
3063     int t, code;
3064     struct setspref *ssp;
3065     struct spref *sp;
3066     struct hostent *thostent;
3067     int error = 0;
3068
3069 #ifndef MAXUSHORT
3070 #ifdef MAXSHORT
3071 #define MAXUSHORT ((unsigned short) 2*MAXSHORT+1)       /* assumes two's complement binary system */
3072 #else
3073 #define MAXUSHORT ((unsigned short) ~0)
3074 #endif
3075 #endif
3076
3077     thostent = hostutil_GetHostByName(name);
3078     if (!thostent) {
3079         fprintf(stderr, "%s: couldn't resolve name.\n", name);
3080         return -1;
3081     }
3082
3083     ssp = (struct setspref *)(gblob.in);
3084
3085     for (t = 0; thostent->h_addr_list[t]; t++) {
3086         if (gblob.in_size > MAXINSIZE - sizeof(struct spref)) {
3087             code = pokeServers();
3088             if (code)
3089                 error = code;
3090             ssp->num_servers = 0;
3091         }
3092
3093         sp = (struct spref *)(gblob.in + gblob.in_size);
3094         memcpy(&(sp->server.s_addr), thostent->h_addr_list[t],
3095                sizeof(afs_uint32));
3096         sp->rank = (rank > MAXUSHORT ? MAXUSHORT : rank);
3097         gblob.in_size += sizeof(struct spref);
3098         ssp->num_servers++;
3099
3100         if (debug)
3101             fprintf(stderr, "adding server %s, rank %d, ip addr 0x%lx\n",
3102                     name, sp->rank, (long unsigned int) sp->server.s_addr);
3103     }
3104
3105     return error;
3106 }
3107
3108
3109 static int
3110 SetPrefCmd(struct cmd_syndesc *as, void *arock)
3111 {
3112     FILE *infd;
3113     afs_int32 code;
3114     struct cmd_item *ti;
3115     char name[80];
3116     afs_int32 rank;
3117     struct setspref *ssp;
3118     int error = 0;              /* -1 means error message printed,
3119                                  * >0 means errno value for unprinted message */
3120
3121     ssp = (struct setspref *)space;
3122     ssp->flags = 0;
3123     ssp->num_servers = 0;
3124     gblob.in_size = ((char *)&(ssp->servers[0])) - (char *)ssp;
3125     gblob.in = space;
3126     gblob.out = space;
3127     gblob.out_size = AFS_PIOCTL_MAXSIZE;
3128
3129
3130     if (geteuid()) {
3131         fprintf(stderr, "Permission denied: requires root access.\n");
3132         return 1;
3133     }
3134
3135     ti = as->parms[2].items;    /* -file */
3136     if (ti) {
3137         if (debug)
3138             fprintf(stderr, "opening file %s\n", ti->data);
3139         if (!(infd = fopen(ti->data, "r"))) {
3140             perror(ti->data);
3141             error = -1;
3142         } else {
3143             while (fscanf(infd, "%79s%ld", name, (long int *)&rank) != EOF) {
3144                 code = addServer(name, (unsigned short)rank);
3145                 if (code)
3146                     error = code;
3147             }
3148         }
3149     }
3150
3151     ti = as->parms[3].items;    /* -stdin */
3152     if (ti) {
3153         while (scanf("%79s%ld", name, (long int *)&rank) != EOF) {
3154             code = addServer(name, (unsigned short)rank);
3155             if (code)
3156                 error = code;
3157         }
3158     }
3159
3160     for (ti = as->parms[0].items; ti; ti = ti->next) {  /* list of servers, ranks */
3161         if (ti) {
3162             if (!ti->next) {
3163                 break;
3164             }
3165             code = addServer(ti->data, (unsigned short)atol(ti->next->data));
3166             if (code)
3167                 error = code;
3168             if (debug)
3169                 printf("set fs prefs %s %s\n", ti->data, ti->next->data);
3170             ti = ti->next;
3171         }
3172     }
3173     code = pokeServers();
3174     if (code)
3175         error = code;
3176     if (debug)
3177         printf("now working on vlservers, code=%d\n", code);
3178
3179     ssp = (struct setspref *)space;
3180     ssp->flags = DBservers;
3181     ssp->num_servers = 0;
3182     gblob.in_size = ((char *)&(ssp->servers[0])) - (char *)ssp;
3183     gblob.in = space;
3184
3185     for (ti = as->parms[1].items; ti; ti = ti->next) {  /* list of dbservers, ranks */
3186         if (ti) {
3187             if (!ti->next) {
3188                 break;
3189             }
3190             code = addServer(ti->data, (unsigned short)atol(ti->next->data));
3191             if (code)
3192                 error = code;
3193             if (debug)
3194                 printf("set vl prefs %s %s\n", ti->data, ti->next->data);
3195             ti = ti->next;
3196         }
3197     }
3198
3199     if (as->parms[1].items) {
3200         if (debug)
3201             printf("now poking vlservers\n");
3202         code = pokeServers();
3203         if (code)
3204             error = code;
3205     }
3206
3207     if (error > 0)
3208         Die(error, 0);
3209
3210     return error ? 1 : 0;
3211 }
3212
3213
3214
3215 static int
3216 GetPrefCmd(struct cmd_syndesc *as, void *arock)
3217 {
3218     afs_int32 code;
3219     struct cmd_item *ti;
3220     char *name, tbuffer[20];
3221     afs_int32 addr;
3222     FILE *outfd;
3223     int resolve;
3224     int vlservers = 0;
3225     struct ViceIoctl blob;
3226     struct sprefrequest *in;
3227     struct sprefinfo *out;
3228     int i;
3229
3230     ti = as->parms[0].items;    /* -file */
3231     if (ti) {
3232         if (debug)
3233             fprintf(stderr, "opening file %s\n", ti->data);
3234         if (!(outfd = freopen(ti->data, "w", stdout))) {
3235             perror(ti->data);
3236             return 1;
3237         }
3238     }
3239
3240     ti = as->parms[1].items;    /* -numeric */
3241     resolve = !(ti);
3242     ti = as->parms[2].items;    /* -vlservers */
3243     vlservers |= (ti ? DBservers : 0);
3244     /*  ti = as->parms[3].items;   -cell */
3245
3246     in = (struct sprefrequest *)space;
3247     in->offset = 0;
3248
3249     do {
3250         blob.in_size = sizeof(struct sprefrequest);
3251         blob.in = (char *)in;
3252         blob.out = space;
3253         blob.out_size = AFS_PIOCTL_MAXSIZE;
3254
3255         in->num_servers =
3256             (AFS_PIOCTL_MAXSIZE - 2 * sizeof(short)) / sizeof(struct spref);
3257         in->flags = vlservers;
3258
3259         do {
3260             code = pioctl(0, VIOC_GETSPREFS, &blob, 1);
3261             if (code) {
3262                 if ((errno != E2BIG) || (2 * blob.out_size > 0x7FFF)) {
3263                     perror("getserverprefs pioctl");
3264                     return 1;
3265                 }
3266                 blob.out_size *= 2;
3267                 if (blob.out == space)
3268                     blob.out = malloc(blob.out_size);
3269                 else
3270                     blob.out = realloc(blob.out, blob.out_size);
3271             }
3272         } while (code != 0);
3273
3274         out = (struct sprefinfo *)blob.out;
3275
3276         for (i = 0; i < out->num_servers; i++) {
3277             if (resolve) {
3278                 name = hostutil_GetNameByINet(out->servers[i].server.s_addr);
3279             } else {
3280                 addr = ntohl(out->servers[i].server.s_addr);
3281                 sprintf(tbuffer, "%d.%d.%d.%d", (addr >> 24) & 0xff,
3282                         (addr >> 16) & 0xff, (addr >> 8) & 0xff, addr & 0xff);
3283                 name = tbuffer;
3284             }
3285             printf("%-50s %5u\n", name, out->servers[i].rank);
3286         }
3287
3288         in->offset = out->next_offset;
3289     } while (out->next_offset > 0);
3290
3291     if (blob.out != space)
3292         free(blob.out);
3293
3294     return 0;
3295 }
3296
3297 static int
3298 StoreBehindCmd(struct cmd_syndesc *as, void *arock)
3299 {
3300     afs_int32 code = 0;
3301     struct ViceIoctl blob;
3302     struct cmd_item *ti;
3303     struct sbstruct tsb, tsb2;
3304     int verbose = 0;
3305     afs_int32 allfiles;
3306     char *t;
3307     int error = 0;
3308
3309     tsb.sb_thisfile = -1;
3310     ti = as->parms[0].items;    /* -kbytes */
3311     if (ti) {
3312         if (!as->parms[1].items) {
3313             fprintf(stderr, "%s: you must specify -files with -kbytes.\n",
3314                     pn);
3315             return 1;
3316         }
3317         tsb.sb_thisfile = strtol(ti->data, &t, 10) * 1024;
3318         if ((tsb.sb_thisfile < 0) || (t != ti->data + strlen(ti->data))) {
3319             fprintf(stderr, "%s: %s must be 0 or a positive number.\n", pn,
3320                     ti->data);
3321             return 1;
3322         }
3323     }
3324
3325     allfiles = tsb.sb_default = -1;     /* Don't set allfiles yet */
3326     ti = as->parms[2].items;    /* -allfiles */
3327     if (ti) {
3328         allfiles = strtol(ti->data, &t, 10) * 1024;
3329         if ((allfiles < 0) || (t != ti->data + strlen(ti->data))) {
3330             fprintf(stderr, "%s: %s must be 0 or a positive number.\n", pn,
3331                     ti->data);
3332             return 1;
3333         }
3334     }
3335
3336     /* -verbose or -file only or no options */
3337     if (as->parms[3].items || (as->parms[1].items && !as->parms[0].items)
3338         || (!as->parms[0].items && !as->parms[1].items
3339             && !as->parms[2].items))
3340         verbose = 1;
3341
3342     blob.in = (char *)&tsb;
3343     blob.in_size = sizeof(struct sbstruct);
3344
3345     /* once per -file */
3346     for (ti = as->parms[1].items; ti; ti = ti->next) {
3347         /* Do this solely to see if the file is there */
3348
3349         blob.out = space;
3350         blob.out_size = AFS_PIOCTL_MAXSIZE;
3351         code = pioctl(ti->data, VIOCWHEREIS, &blob, 1);
3352         if (code) {
3353             Die(errno, ti->data);
3354             error = 1;
3355             continue;
3356         }
3357
3358         memset(&tsb2, 0, sizeof(tsb2));
3359         blob.out = (char *)&tsb2;
3360         blob.out_size = sizeof(struct sbstruct);
3361         code = pioctl(ti->data, VIOC_STORBEHIND, &blob, 1);
3362         if (code) {
3363             Die(errno, ti->data);
3364             error = 1;
3365             continue;
3366         }
3367
3368         if (verbose && (blob.out_size == sizeof(tsb2))) {
3369             if (tsb2.sb_thisfile == -1) {
3370                 fprintf(stdout, "Will store %s according to default.\n",
3371                         ti->data);
3372             } else {
3373                 fprintf(stdout,
3374                         "Will store up to %d kbytes of %s asynchronously.\n",
3375                         (tsb2.sb_thisfile / 1024), ti->data);
3376             }
3377         }
3378     }
3379
3380     /* If no files - make at least one pioctl call, or
3381      * set the allfiles default if we need to.
3382      */
3383     if (!as->parms[1].items || (allfiles != -1)) {
3384         tsb.sb_default = allfiles;
3385         memset(&tsb2, 0, sizeof(tsb2));
3386         blob.out = (char *)&tsb2;
3387         blob.out_size = sizeof(struct sbstruct);
3388         code = pioctl(0, VIOC_STORBEHIND, &blob, 1);
3389         if (code) {
3390             Die(errno, ((allfiles == -1) ? 0 : "-allfiles"));
3391             error = 1;
3392         }
3393     }
3394
3395     /* Having no arguments also reports the default store asynchrony */
3396     if (!error && verbose && (blob.out_size == sizeof(tsb2))) {
3397         fprintf(stdout, "Default store asynchrony is %d kbytes.\n",
3398                 (tsb2.sb_default / 1024));
3399     }
3400
3401     return error;
3402 }
3403
3404
3405 static int
3406 SetCryptCmd(struct cmd_syndesc *as, void *arock)
3407 {
3408     afs_int32 code = 0, flag;
3409     struct ViceIoctl blob;
3410     char *tp;
3411
3412     tp = as->parms[0].items->data;
3413     if (strcmp(tp, "on") == 0)
3414         flag = 1;
3415     else if (strcmp(tp, "off") == 0)
3416         flag = 0;
3417     else {
3418         fprintf(stderr, "%s: %s must be \"on\" or \"off\".\n", pn, tp);
3419         return EINVAL;
3420     }
3421
3422     blob.in = (char *)&flag;
3423     blob.in_size = sizeof(flag);
3424     blob.out_size = 0;
3425     code = pioctl(0, VIOC_SETRXKCRYPT, &blob, 1);
3426     if (code)
3427         Die(errno, NULL);
3428     return 0;
3429 }
3430
3431
3432 static int
3433 GetCryptCmd(struct cmd_syndesc *as, void *arock)
3434 {
3435     afs_int32 code = 0, flag;
3436     struct ViceIoctl blob;
3437     char *tp;
3438
3439     blob.in = NULL;
3440     blob.in_size = 0;
3441     blob.out_size = sizeof(flag);
3442     blob.out = space;
3443
3444     code = pioctl(0, VIOC_GETRXKCRYPT, &blob, 1);
3445
3446     if (code)
3447         Die(errno, NULL);
3448     else {
3449         tp = space;
3450         memcpy(&flag, tp, sizeof(afs_int32));
3451         printf("Security level is currently ");
3452         if (flag == 1)
3453             printf("crypt (data security).\n");
3454         else
3455             printf("clear.\n");
3456     }
3457     return 0;
3458 }
3459
3460 static char *modenames[] = {
3461     "offline",
3462     "online",
3463     "readonly",  /* Not currently supported */
3464     "fetchonly", /* Not currently supported */
3465     "partial",   /* Not currently supported */
3466     NULL
3467 };
3468
3469 static char *policynames[] = {
3470     "client",
3471     "server",
3472     "closer",  /* Not currently supported. */
3473     "manual",  /* Not currently supported. */
3474     NULL
3475 };
3476
3477 static int
3478 DisconCmd(struct cmd_syndesc *as, void *arock)
3479 {
3480     struct cmd_item *ti;
3481     char *modename;
3482     char *policyname;
3483     int modelen, policylen;
3484     afs_int32 mode, policy, code, unixuid = 0;
3485     struct ViceIoctl blob;
3486
3487     blob.in = NULL;
3488     blob.in_size = 0;
3489
3490     space[0] = space[1] = space[2] = space[3] = 0;
3491
3492     ti = as->parms[0].items;
3493     if (ti) {
3494         modename = ti->data;
3495         modelen = strlen(modename);
3496         for (mode = 0; modenames[mode] != NULL; mode++)
3497             if (!strncasecmp(modename, modenames[mode], modelen))
3498                 break;
3499         if (modenames[mode] == NULL)
3500             printf("Unknown discon mode \"%s\"\n", modename);
3501         else {
3502             space[0] = mode + 1;
3503         }
3504     }
3505     ti = as->parms[1].items;
3506     if (ti) {
3507         policyname = ti->data;
3508         policylen = strlen(policyname);
3509         for (policy = 0; policynames[policy] != NULL; policy++)
3510             if (!strncasecmp(policyname, policynames[policy], policylen))
3511                 break;
3512         if (policynames[policy] == NULL)
3513             printf("Unknown discon mode \"%s\"\n", policyname);
3514         else {
3515             space[1] = policy + 1;
3516         }
3517     }
3518
3519     if (as->parms[2].items) {
3520         space[2] = 1;
3521         printf("force on\n");
3522     }
3523
3524     ti = as->parms[3].items;
3525     if (ti) {
3526         code = util_GetInt32(ti->data, &unixuid);
3527         if (code) {
3528             fprintf(stderr, "%s: bad integer specified for uid.\n", pn);
3529             return 1;
3530         }
3531         space[3] = unixuid;
3532     } else
3533         space[3] = 0;
3534
3535     blob.in = space;
3536     blob.in_size = 4 * sizeof(afs_int32);
3537
3538     blob.out_size = sizeof(mode);
3539     blob.out = space;
3540     code = pioctl(0, VIOC_DISCON, &blob, 1);
3541     if (code)
3542         Die(errno, NULL);
3543     else {
3544         memcpy(&mode, space, sizeof mode);
3545         if (mode < sizeof modenames / sizeof (char *))
3546             printf("Discon mode is now \"%s\"\n", modenames[mode]);
3547         else
3548             printf("Unknown discon mode %d\n", mode);
3549     }
3550
3551     return 0;
3552 }
3553
3554 #include "AFS_component_version_number.c"
3555
3556 int
3557 main(int argc, char **argv)
3558 {
3559     afs_int32 code;
3560     struct cmd_syndesc *ts;
3561
3562 #ifdef  AFS_AIX32_ENV
3563     /*
3564      * The following signal action for AIX is necessary so that in case of a
3565      * crash (i.e. core is generated) we can include the user's data section
3566      * in the core dump. Unfortunately, by default, only a partial core is
3567      * generated which, in many cases, isn't too useful.
3568      */
3569     struct sigaction nsa;
3570
3571     sigemptyset(&nsa.sa_mask);
3572     nsa.sa_handler = SIG_DFL;
3573     nsa.sa_flags = SA_FULLDUMP;
3574     sigaction(SIGSEGV, &nsa, NULL);
3575 #endif
3576
3577     /* try to find volume location information */
3578     ts = cmd_CreateSyntax("getclientaddrs", GetClientAddrsCmd, NULL,
3579                           "get client network interface addresses");
3580     cmd_CreateAlias(ts, "gc");
3581
3582     ts = cmd_CreateSyntax("setclientaddrs", SetClientAddrsCmd, NULL,
3583                           "set client network interface addresses");
3584     cmd_AddParm(ts, "-address", CMD_LIST, CMD_OPTIONAL | CMD_EXPANDS,
3585                 "client network interfaces");
3586     cmd_CreateAlias(ts, "sc");
3587
3588     ts = cmd_CreateSyntax("setserverprefs", SetPrefCmd, NULL,
3589                           "set server ranks");
3590     cmd_AddParm(ts, "-servers", CMD_LIST, CMD_OPTIONAL | CMD_EXPANDS,
3591                 "fileserver names and ranks");
3592     cmd_AddParm(ts, "-vlservers", CMD_LIST, CMD_OPTIONAL | CMD_EXPANDS,
3593                 "VL server names and ranks");
3594     cmd_AddParm(ts, "-file", CMD_SINGLE, CMD_OPTIONAL,
3595                 "input from named file");
3596     cmd_AddParm(ts, "-stdin", CMD_FLAG, CMD_OPTIONAL, "input from stdin");
3597     cmd_CreateAlias(ts, "sp");
3598
3599     ts = cmd_CreateSyntax("getserverprefs", GetPrefCmd, NULL,
3600                           "get server ranks");
3601     cmd_AddParm(ts, "-file", CMD_SINGLE, CMD_OPTIONAL,
3602                 "output to named file");
3603     cmd_AddParm(ts, "-numeric", CMD_FLAG, CMD_OPTIONAL, "addresses only");
3604     cmd_AddParm(ts, "-vlservers", CMD_FLAG, CMD_OPTIONAL, "VL servers");
3605 /*    cmd_AddParm(ts, "-cell", CMD_FLAG, CMD_OPTIONAL, "cellname"); */
3606     cmd_CreateAlias(ts, "gp");
3607
3608     ts = cmd_CreateSyntax("setacl", SetACLCmd, NULL, "set access control list");
3609     cmd_AddParm(ts, "-dir", CMD_LIST, 0, "directory");
3610     cmd_AddParm(ts, "-acl", CMD_LIST, 0, "access list entries");
3611     cmd_AddParm(ts, "-clear", CMD_FLAG, CMD_OPTIONAL, "clear access list");
3612     cmd_AddParm(ts, "-negative", CMD_FLAG, CMD_OPTIONAL,
3613                 "apply to negative rights");
3614     parm_setacl_id = ts->nParms;
3615     cmd_AddParm(ts, "-id", CMD_FLAG, CMD_OPTIONAL,
3616                 "initial directory acl (DFS only)");
3617     cmd_AddParm(ts, "-if", CMD_FLAG, CMD_OPTIONAL,
3618                 "initial file acl (DFS only)");
3619     cmd_CreateAlias(ts, "sa");
3620
3621     ts = cmd_CreateSyntax("listacl", ListACLCmd, NULL,
3622                           "list access control list");
3623     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3624     parm_listacl_id = ts->nParms;
3625     cmd_AddParm(ts, "-id", CMD_FLAG, CMD_OPTIONAL, "initial directory acl");
3626     cmd_AddParm(ts, "-if", CMD_FLAG, CMD_OPTIONAL, "initial file acl");
3627     cmd_AddParm(ts, "-cmd", CMD_FLAG, CMD_OPTIONAL, "output as 'fs setacl' command");
3628     cmd_CreateAlias(ts, "la");
3629
3630     ts = cmd_CreateSyntax("getcalleraccess", GetCallerAccess, NULL,
3631             "list callers access");
3632     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3633     cmd_CreateAlias(ts, "gca");
3634
3635     ts = cmd_CreateSyntax("cleanacl", CleanACLCmd, NULL,
3636                           "clean up access control list");
3637     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3638
3639     ts = cmd_CreateSyntax("copyacl", CopyACLCmd, NULL,
3640                           "copy access control list");
3641     cmd_AddParm(ts, "-fromdir", CMD_SINGLE, 0,
3642                 "source directory (or DFS file)");
3643     cmd_AddParm(ts, "-todir", CMD_LIST, 0,
3644                 "destination directory (or DFS file)");
3645     cmd_AddParm(ts, "-clear", CMD_FLAG, CMD_OPTIONAL,
3646                 "first clear dest access list");
3647     parm_copyacl_id = ts->nParms;
3648     cmd_AddParm(ts, "-id", CMD_FLAG, CMD_OPTIONAL, "initial directory acl");
3649     cmd_AddParm(ts, "-if", CMD_FLAG, CMD_OPTIONAL, "initial file acl");
3650
3651     cmd_CreateAlias(ts, "ca");
3652
3653     ts = cmd_CreateSyntax("flush", FlushCmd, NULL, "flush file from cache");
3654     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3655     ts = cmd_CreateSyntax("flushmount", FlushMountCmd, NULL,
3656                           "flush mount symlink from cache");
3657     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3658
3659     ts = cmd_CreateSyntax("setvol", SetVolCmd, NULL, "set volume status");
3660     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3661     cmd_AddParm(ts, "-max", CMD_SINGLE, CMD_OPTIONAL,
3662                 "disk space quota in 1K units");
3663 #ifdef notdef
3664     cmd_AddParm(ts, "-min", CMD_SINGLE, CMD_OPTIONAL,
3665                 "disk space guaranteed");
3666     cmd_AddParm(ts, "-motd", CMD_SINGLE, CMD_OPTIONAL, "message of the day");
3667 #endif
3668     cmd_AddParm(ts, "-offlinemsg", CMD_SINGLE, CMD_OPTIONAL,
3669                 "offline message");
3670     cmd_CreateAlias(ts, "sv");
3671
3672     ts = cmd_CreateSyntax("messages", MessagesCmd, NULL,
3673                           "control Cache Manager messages");
3674     cmd_AddParm(ts, "-show", CMD_SINGLE, CMD_OPTIONAL,
3675                 "[user|console|all|none]");
3676
3677     ts = cmd_CreateSyntax("examine", ExamineCmd, NULL, "display file/volume status");
3678     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3679     cmd_CreateAlias(ts, "lv");
3680     cmd_CreateAlias(ts, "listvol");
3681
3682     ts = cmd_CreateSyntax("listquota", ListQuotaCmd, NULL, "list volume quota");
3683     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3684     cmd_AddParm(ts, "-human", CMD_FLAG, CMD_OPTIONAL, "human-readable listing");
3685     cmd_CreateAlias(ts, "lq");
3686
3687     ts = cmd_CreateSyntax("diskfree", DiskFreeCmd, NULL,
3688                           "show server disk space usage");
3689     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3690     cmd_AddParm(ts, "-human", CMD_FLAG, CMD_OPTIONAL, "human-readable listing");
3691     cmd_CreateAlias(ts, "df");
3692
3693     ts = cmd_CreateSyntax("quota", QuotaCmd, NULL, "show volume quota usage");
3694     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3695
3696     ts = cmd_CreateSyntax("lsmount", ListMountCmd, NULL, "list mount point");
3697     cmd_AddParm(ts, "-dir", CMD_LIST, 0, "directory");
3698
3699     ts = cmd_CreateSyntax("mkmount", MakeMountCmd, NULL, "make mount point");
3700     cmd_AddParm(ts, "-dir", CMD_SINGLE, 0, "directory");
3701     cmd_AddParm(ts, "-vol", CMD_SINGLE, 0, "volume name");
3702     cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cell name");
3703     cmd_AddParm(ts, "-rw", CMD_FLAG, CMD_OPTIONAL, "force r/w volume");
3704     cmd_AddParm(ts, "-fast", CMD_FLAG, CMD_OPTIONAL,
3705                 "don't check name with VLDB");
3706
3707 #if defined(AFS_CACHE_BYPASS)
3708         ts = cmd_CreateSyntax("bypassthreshold", BypassThresholdCmd, 0,
3709                 "get/set cache bypass file size threshold");
3710         cmd_AddParm(ts, "-size", CMD_SINGLE, CMD_OPTIONAL, "file size");
3711 #endif
3712
3713 /*
3714
3715 defect 3069
3716
3717     cmd_AddParm(ts, "-root", CMD_FLAG, CMD_OPTIONAL, "create cellular mount point");
3718 */
3719
3720
3721     ts = cmd_CreateSyntax("rmmount", RemoveMountCmd, NULL, "remove mount point");
3722     cmd_AddParm(ts, "-dir", CMD_LIST, 0, "directory");
3723
3724     ts = cmd_CreateSyntax("checkservers", CheckServersCmd, NULL,
3725                           "check local cell's servers");
3726     cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cell to check");
3727     cmd_AddParm(ts, "-all", CMD_FLAG, CMD_OPTIONAL, "check all cells");
3728     cmd_AddParm(ts, "-fast", CMD_FLAG, CMD_OPTIONAL,
3729                 "just list, don't check");
3730     cmd_AddParm(ts, "-interval", CMD_SINGLE, CMD_OPTIONAL,
3731                 "seconds between probes");
3732
3733     ts = cmd_CreateSyntax("checkvolumes", CheckVolumesCmd, NULL,
3734                           "check volumeID/name mappings");
3735     cmd_CreateAlias(ts, "checkbackups");
3736
3737
3738     ts = cmd_CreateSyntax("setcachesize", SetCacheSizeCmd, NULL,
3739                           "set cache size");
3740     cmd_AddParm(ts, "-blocks", CMD_SINGLE, CMD_OPTIONAL,
3741                 "size in 1K byte blocks (0 => reset)");
3742     cmd_CreateAlias(ts, "cachesize");
3743
3744     cmd_AddParm(ts, "-reset", CMD_FLAG, CMD_OPTIONAL,
3745                 "reset size back to boot value");
3746
3747     ts = cmd_CreateSyntax("getcacheparms", GetCacheParmsCmd, NULL,
3748                           "get cache usage info");
3749     cmd_AddParm(ts, "-files", CMD_FLAG, CMD_OPTIONAL, "Show cach files used as well");
3750     cmd_AddParm(ts, "-excessive", CMD_FLAG, CMD_OPTIONAL, "excessively verbose cache stats");
3751
3752     ts = cmd_CreateSyntax("listcells", ListCellsCmd, NULL,
3753                           "list configured cells");
3754     cmd_AddParm(ts, "-numeric", CMD_FLAG, CMD_OPTIONAL, "addresses only");
3755
3756     ts = cmd_CreateSyntax("listaliases", ListAliasesCmd, NULL,
3757                           "list configured cell aliases");
3758
3759     ts = cmd_CreateSyntax("setquota", SetQuotaCmd, NULL, "set volume quota");
3760     cmd_AddParm(ts, "-path", CMD_SINGLE, CMD_OPTIONAL, "dir/file path");
3761     cmd_AddParm(ts, "-max", CMD_SINGLE, 0, "max quota in kbytes");
3762 #ifdef notdef
3763     cmd_AddParm(ts, "-min", CMD_SINGLE, CMD_OPTIONAL, "min quota in kbytes");
3764 #endif
3765     cmd_CreateAlias(ts, "sq");
3766
3767     ts = cmd_CreateSyntax("newcell", NewCellCmd, NULL, "configure new cell");
3768     cmd_AddParm(ts, "-name", CMD_SINGLE, 0, "cell name");
3769     cmd_AddParm(ts, "-servers", CMD_LIST, CMD_REQUIRED, "primary servers");
3770     cmd_AddParm(ts, "-linkedcell", CMD_SINGLE, CMD_OPTIONAL,
3771                 "linked cell name");
3772
3773     ts = cmd_CreateSyntax("newalias", NewAliasCmd, NULL,
3774                           "configure new cell alias");
3775     cmd_AddParm(ts, "-alias", CMD_SINGLE, 0, "alias name");
3776     cmd_AddParm(ts, "-name", CMD_SINGLE, 0, "real name of cell");
3777
3778 #ifdef FS_ENABLE_SERVER_DEBUG_PORTS
3779 /*
3780  * Turn this on only if you wish to be able to talk to a server which is listening
3781  * on alternative ports. This is not intended for general use and may not be
3782  * supported in the cache manager. It is not a way to run two servers at the
3783  * same host, since the cache manager cannot properly distinguish those two hosts.
3784  */
3785     cmd_AddParm(ts, "-fsport", CMD_SINGLE, CMD_OPTIONAL,
3786                 "cell's fileserver port");
3787     cmd_AddParm(ts, "-vlport", CMD_SINGLE, CMD_OPTIONAL,
3788                 "cell's vldb server port");
3789 #endif
3790
3791     ts = cmd_CreateSyntax("whichcell", WhichCellCmd, NULL, "list file's cell");
3792     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3793
3794     ts = cmd_CreateSyntax("whereis", WhereIsCmd, NULL, "list file's location");
3795     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3796
3797     ts = cmd_CreateSyntax("wscell", WSCellCmd, NULL, "list workstation's cell");
3798
3799 /*
3800     ts = cmd_CreateSyntax("primarycell", PrimaryCellCmd, NULL, "obsolete (listed primary cell)");
3801 */
3802
3803     /* set cache monitor host address */
3804     ts = cmd_CreateSyntax("monitor", MonitorCmd, NULL, (char *)CMD_HIDDEN);
3805     cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_OPTIONAL,
3806                 "host name or 'off'");
3807     cmd_CreateAlias(ts, "mariner");
3808
3809     ts = cmd_CreateSyntax("getcellstatus", GetCellCmd, NULL, "get cell status");
3810     cmd_AddParm(ts, "-cell", CMD_LIST, 0, "cell name");
3811
3812     ts = cmd_CreateSyntax("setcell", SetCellCmd, NULL, "set cell status");
3813     cmd_AddParm(ts, "-cell", CMD_LIST, 0, "cell name");
3814     cmd_AddParm(ts, "-suid", CMD_FLAG, CMD_OPTIONAL, "allow setuid programs");
3815     cmd_AddParm(ts, "-nosuid", CMD_FLAG, CMD_OPTIONAL,
3816                 "disallow setuid programs");
3817
3818     ts = cmd_CreateSyntax("flushvolume", FlushVolumeCmd, NULL,
3819                           "flush all data in volume");
3820     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3821
3822     ts = cmd_CreateSyntax("sysname", SysNameCmd, NULL,
3823                           "get/set sysname (i.e. @sys) value");
3824     cmd_AddParm(ts, "-newsys", CMD_LIST, CMD_OPTIONAL, "new sysname");
3825
3826     ts = cmd_CreateSyntax("exportafs", ExportAfsCmd, NULL,
3827                           "enable/disable translators to AFS");
3828     cmd_AddParm(ts, "-type", CMD_SINGLE, 0, "exporter name");
3829     cmd_AddParm(ts, "-start", CMD_SINGLE, CMD_OPTIONAL,
3830                 "start/stop translator (on | off)");
3831     cmd_AddParm(ts, "-convert", CMD_SINGLE, CMD_OPTIONAL,
3832                 "convert from afs to unix mode (on | off)");
3833     cmd_AddParm(ts, "-uidcheck", CMD_SINGLE, CMD_OPTIONAL,
3834                 "run on strict 'uid check' mode (on | off)");
3835     cmd_AddParm(ts, "-submounts", CMD_SINGLE, CMD_OPTIONAL,
3836                 "allow nfs mounts to subdirs of /afs/.. (on  | off)");
3837     cmd_AddParm(ts, "-clipags", CMD_SINGLE, CMD_OPTIONAL,
3838                 "enable use of client-assigned PAG's (on  | off)");
3839     cmd_AddParm(ts, "-pagcb", CMD_SINGLE, CMD_OPTIONAL,
3840                 "enable callbacks to get creds from new clients (on  | off)");
3841
3842
3843     ts = cmd_CreateSyntax("storebehind", StoreBehindCmd, NULL,
3844                           "store to server after file close");
3845     cmd_AddParm(ts, "-kbytes", CMD_SINGLE, CMD_OPTIONAL,
3846                 "asynchrony for specified names");
3847     cmd_AddParm(ts, "-files", CMD_LIST, CMD_OPTIONAL, "specific pathnames");
3848     cmd_AddParm(ts, "-allfiles", CMD_SINGLE, CMD_OPTIONAL,
3849                 "new default (KB)");
3850     cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL, "show status");
3851     cmd_CreateAlias(ts, "sb");
3852
3853     ts = cmd_CreateSyntax("setcrypt", SetCryptCmd, NULL,
3854                           "set cache manager encryption flag");
3855     cmd_AddParm(ts, "-crypt", CMD_SINGLE, 0, "on or off");
3856
3857     ts = cmd_CreateSyntax("getcrypt", GetCryptCmd, NULL,
3858                           "get cache manager encryption flag");
3859
3860     ts = cmd_CreateSyntax("rxstatproc", RxStatProcCmd, NULL,
3861                           "Manage per process RX statistics");
3862     cmd_AddParm(ts, "-enable", CMD_FLAG, CMD_OPTIONAL, "Enable RX stats");
3863     cmd_AddParm(ts, "-disable", CMD_FLAG, CMD_OPTIONAL, "Disable RX stats");
3864     cmd_AddParm(ts, "-clear", CMD_FLAG, CMD_OPTIONAL, "Clear RX stats");
3865
3866     ts = cmd_CreateSyntax("rxstatpeer", RxStatPeerCmd, NULL,
3867                           "Manage per peer RX statistics");
3868     cmd_AddParm(ts, "-enable", CMD_FLAG, CMD_OPTIONAL, "Enable RX stats");
3869     cmd_AddParm(ts, "-disable", CMD_FLAG, CMD_OPTIONAL, "Disable RX stats");
3870     cmd_AddParm(ts, "-clear", CMD_FLAG, CMD_OPTIONAL, "Clear RX stats");
3871
3872     ts = cmd_CreateSyntax("setcbaddr", CallBackRxConnCmd, NULL, "configure callback connection address");
3873     cmd_AddParm(ts, "-addr", CMD_SINGLE, CMD_OPTIONAL, "host name or address");
3874
3875     /* try to find volume location information */
3876     ts = cmd_CreateSyntax("getfid", GetFidCmd, NULL,
3877                           "get fid for file(s)");
3878     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3879
3880     ts = cmd_CreateSyntax("discon", DisconCmd, NULL,
3881                           "disconnection mode");
3882     cmd_AddParm(ts, "-mode", CMD_SINGLE, CMD_REQUIRED, "offline | online");
3883     cmd_AddParm(ts, "-policy", CMD_SINGLE, CMD_OPTIONAL, "client | server");
3884     cmd_AddParm(ts, "-force", CMD_FLAG, CMD_OPTIONAL, "Force reconnection, despite any synchronization issues.");
3885     cmd_AddParm(ts, "-uid", CMD_SINGLE, CMD_OPTIONAL, "Numeric UID of user whose tokens to use at reconnect.");
3886
3887     ts = cmd_CreateSyntax("nukenfscreds", NukeNFSCredsCmd, NULL, "nuke credentials for NFS client");
3888     cmd_AddParm(ts, "-addr", CMD_SINGLE, 0, "host name or address");
3889
3890     ts = cmd_CreateSyntax("uuid", UuidCmd, NULL, "manage the UUID for the cache manager");
3891     cmd_AddParm(ts, "-generate", CMD_FLAG, CMD_REQUIRED, "generate a new UUID");
3892
3893     ts = cmd_CreateSyntax("precache", PreCacheCmd, 0,
3894                           "set precache size");
3895     cmd_AddParm(ts, "-blocks", CMD_SINGLE, CMD_OPTIONAL,
3896                 "size in 1K byte blocks (0 => disable)");
3897
3898     code = cmd_Dispatch(argc, argv);
3899     if (rxInitDone)
3900         rx_Finalize();
3901
3902     return code;
3903 }
3904
3905 static void
3906 Die(int errnum, char *filename)
3907 {
3908     switch (errnum) {
3909     case EINVAL:
3910         if (filename)
3911             fprintf(stderr,
3912                     "%s: Invalid argument; it is possible that %s is not in AFS.\n",
3913                     pn, filename);
3914         else
3915             fprintf(stderr, "%s: Invalid argument.\n", pn);
3916         break;
3917     case ENOENT:
3918         if (filename)
3919             fprintf(stderr, "%s: File '%s' doesn't exist\n", pn, filename);
3920         else
3921             fprintf(stderr, "%s: no such file returned\n", pn);
3922         break;
3923     case EROFS:
3924         fprintf(stderr,
3925                 "%s: You can not change a backup or readonly volume\n", pn);
3926         break;
3927     case EACCES:
3928     case EPERM:
3929         if (filename)
3930             fprintf(stderr,
3931                     "%s: You don't have the required access rights on '%s'\n",
3932                     pn, filename);
3933         else
3934             fprintf(stderr,
3935                     "%s: You do not have the required rights to do this operation\n",
3936                     pn);
3937         break;
3938     default:
3939         if (filename)
3940             fprintf(stderr, "%s:'%s'", pn, filename);
3941         else
3942             fprintf(stderr, "%s", pn);
3943         fprintf(stderr, ": %s\n", afs_error_message(errnum));
3944         break;
3945     }
3946 }
3947
3948 /* get clients interface addresses */
3949 static int
3950 GetClientAddrsCmd(struct cmd_syndesc *as, void *arock)
3951 {
3952     afs_int32 code;
3953     struct ViceIoctl blob;
3954     struct sprefrequest *in;
3955     struct sprefinfo *out;
3956
3957     in = (struct sprefrequest *)space;
3958     in->offset = 0;
3959
3960     do {
3961         blob.in_size = sizeof(struct sprefrequest);
3962         blob.in = (char *)in;
3963         blob.out = space;
3964         blob.out_size = AFS_PIOCTL_MAXSIZE;
3965
3966         in->num_servers =
3967             (AFS_PIOCTL_MAXSIZE - 2 * sizeof(short)) / sizeof(struct spref);
3968         /* returns addr in network byte order */
3969         code = pioctl(0, VIOC_GETCPREFS, &blob, 1);
3970         if (code) {
3971             perror("getClientInterfaceAddr pioctl");
3972             return 1;
3973         }
3974
3975         {
3976             int i;
3977             out = (struct sprefinfo *)blob.out;
3978             for (i = 0; i < out->num_servers; i++) {
3979                 afs_int32 addr;
3980                 char tbuffer[32];
3981                 addr = ntohl(out->servers[i].server.s_addr);
3982                 sprintf(tbuffer, "%d.%d.%d.%d", (addr >> 24) & 0xff,
3983                         (addr >> 16) & 0xff, (addr >> 8) & 0xff, addr & 0xff);
3984                 printf("%-50s\n", tbuffer);
3985             }
3986             in->offset = out->next_offset;
3987         }
3988     } while (out->next_offset > 0);
3989
3990     return 0;
3991 }
3992
3993 static int
3994 SetClientAddrsCmd(struct cmd_syndesc *as, void *arock)
3995 {
3996     afs_int32 code, addr;
3997     struct cmd_item *ti;
3998     struct ViceIoctl blob;
3999     struct setspref *ssp;
4000     int sizeUsed = 0, i, flag;
4001     afs_uint32 existingAddr[1024];      /* existing addresses on this host */
4002     int existNu;
4003     int error = 0;
4004
4005     ssp = (struct setspref *)space;
4006     ssp->num_servers = 0;
4007     blob.in = space;
4008     blob.out = space;
4009     blob.out_size = AFS_PIOCTL_MAXSIZE;
4010
4011     if (geteuid()) {
4012         fprintf(stderr, "Permission denied: requires root access.\n");
4013         return 1;
4014     }
4015
4016     /* extract all existing interface addresses */
4017     existNu = rx_getAllAddr(existingAddr, 1024);
4018     if (existNu < 0)
4019         return 1;
4020
4021     sizeUsed = sizeof(struct setspref); /* space used in ioctl buffer */
4022     for (ti = as->parms[0].items; ti; ti = ti->next) {
4023         if (sizeUsed >= sizeof(space)) {
4024             fprintf(stderr, "No more space\n");
4025             return 1;
4026         }
4027         addr = extractAddr(ti->data, 20);       /* network order */
4028         if ((addr == AFS_IPINVALID) || (addr == AFS_IPINVALIDIGNORE)) {
4029             fprintf(stderr, "Error in specifying address: %s..ignoring\n",
4030                     ti->data);
4031             error = 1;
4032             continue;
4033         }
4034         /* see if it is an address that really exists */
4035         for (flag = 0, i = 0; i < existNu; i++)
4036             if (existingAddr[i] == addr) {
4037                 flag = 1;
4038                 break;
4039             }
4040         if (!flag) {            /* this is an nonexistent address */
4041             fprintf(stderr, "Nonexistent address: 0x%08x..ignoring\n", addr);
4042             error = 1;
4043             continue;
4044         }
4045         /* copy all specified addr into ioctl buffer */
4046         (ssp->servers[ssp->num_servers]).server.s_addr = addr;
4047         printf("Adding 0x%08x\n", addr);
4048         ssp->num_servers++;
4049         sizeUsed += sizeof(struct spref);
4050     }
4051     if (ssp->num_servers < 1) {
4052         fprintf(stderr, "No addresses specified\n");
4053         return 1;
4054     }
4055     blob.in_size = sizeUsed - sizeof(struct spref);
4056
4057     code = pioctl(0, VIOC_SETCPREFS, &blob, 1); /* network order */
4058     if (code) {
4059         Die(errno, 0);
4060         error = 1;
4061     }
4062
4063     return error;
4064 }
4065
4066 static int
4067 FlushMountCmd(struct cmd_syndesc *as, void *arock)
4068 {
4069     afs_int32 code;
4070     struct ViceIoctl blob;
4071     struct cmd_item *ti;
4072     char orig_name[1024];       /*Original name, may be modified */
4073     char true_name[1024];       /*``True'' dirname (e.g., symlink target) */
4074     char parent_dir[1024];      /*Parent directory of true name */
4075     char *last_component;       /*Last component of true name */
4076     struct stat statbuff;       /*Buffer for status info */
4077     int link_chars_read;        /*Num chars read in readlink() */
4078     int thru_symlink;           /*Did we get to a mount point via a symlink? */
4079     int error = 0;
4080
4081     for (ti = as->parms[0].items; ti; ti = ti->next) {
4082         /* once per file */
4083         thru_symlink = 0;
4084         sprintf(orig_name, "%s%s", (ti->data[0] == '/') ? "" : "./",
4085                 ti->data);
4086
4087         if (lstat(orig_name, &statbuff) < 0) {
4088             /* if lstat fails, we should still try the pioctl, since it
4089              * may work (for example, lstat will fail, but pioctl will
4090              * work if the volume of offline (returning ENODEV). */
4091             statbuff.st_mode = S_IFDIR; /* lie like pros */
4092         }
4093
4094         /*
4095          * The lstat succeeded.  If the given file is a symlink, substitute
4096          * the file name with the link name.
4097          */
4098         if ((statbuff.st_mode & S_IFMT) == S_IFLNK) {
4099             thru_symlink = 1;
4100             /*
4101              * Read name of resolved file.
4102              */
4103             link_chars_read = readlink(orig_name, true_name, 1024);
4104             if (link_chars_read <= 0) {
4105                 fprintf(stderr,
4106                         "%s: Can't read target name for '%s' symbolic link!\n",
4107                         pn, orig_name);
4108                 error = 1;
4109                 continue;
4110             }
4111
4112             /*
4113              * Add a trailing null to what was read, bump the length.
4114              */
4115             true_name[link_chars_read++] = 0;
4116
4117             /*
4118              * If the symlink is an absolute pathname, we're fine.  Otherwise, we
4119              * have to create a full pathname using the original name and the
4120              * relative symlink name.  Find the rightmost slash in the original
4121              * name (we know there is one) and splice in the symlink value.
4122              */
4123             if (true_name[0] != '/') {
4124                 last_component = (char *)strrchr(orig_name, '/');
4125                 strcpy(++last_component, true_name);
4126                 strcpy(true_name, orig_name);
4127             }
4128         } else
4129             strcpy(true_name, orig_name);
4130
4131         /*
4132          * Find rightmost slash, if any.
4133          */
4134         last_component = (char *)strrchr(true_name, '/');
4135         if (last_component == (char *)true_name) {
4136             strcpy(parent_dir, "/");
4137             last_component++;
4138         }
4139         else if (last_component != (char *)NULL) {
4140             /*
4141              * Found it.  Designate everything before it as the parent directory,
4142              * everything after it as the final component.
4143              */
4144             strncpy(parent_dir, true_name, last_component - true_name);
4145             parent_dir[last_component - true_name] = 0;
4146             last_component++;   /*Skip the slash */
4147         } else {
4148             /*
4149              * No slash appears in the given file name.  Set parent_dir to the current
4150              * directory, and the last component as the given name.
4151              */
4152             strcpy(parent_dir, ".");
4153             last_component = true_name;
4154         }
4155
4156         if (strcmp(last_component, ".") == 0
4157             || strcmp(last_component, "..") == 0) {
4158             fprintf(stderr,
4159                     "%s: you may not use '.' or '..' as the last component\n",
4160                     pn);
4161             fprintf(stderr, "%s: of a name in the 'fs flushmount' command.\n",
4162                     pn);
4163             error = 1;
4164             continue;
4165         }
4166
4167         blob.in = last_component;
4168         blob.in_size = strlen(last_component) + 1;
4169         blob.out_size = 0;
4170         memset(space, 0, AFS_PIOCTL_MAXSIZE);
4171
4172         code = pioctl(parent_dir, VIOC_AFS_FLUSHMOUNT, &blob, 1);
4173
4174         if (code != 0) {
4175             if (errno == EINVAL) {
4176                 fprintf(stderr, "'%s' is not a mount point.\n", ti->data);
4177             } else {
4178                 Die(errno, (ti->data ? ti->data : parent_dir));
4179             }
4180             error = 1;
4181         }
4182     }
4183     return error;
4184 }
4185
4186 static int
4187 RxStatProcCmd(struct cmd_syndesc *as, void *arock)
4188 {
4189     afs_int32 code;
4190     afs_int32 flags = 0;
4191     struct ViceIoctl blob;
4192
4193     if (as->parms[0].items) {   /* -enable */
4194         flags |= AFSCALL_RXSTATS_ENABLE;
4195     }
4196     if (as->parms[1].items) {   /* -disable */
4197         flags |= AFSCALL_RXSTATS_DISABLE;
4198     }
4199     if (as->parms[2].items) {   /* -clear */
4200         flags |= AFSCALL_RXSTATS_CLEAR;
4201     }
4202     if (flags == 0) {
4203         fprintf(stderr, "You must specify at least one argument\n");
4204         return 1;
4205     }
4206
4207     blob.in = (char *)&flags;
4208     blob.in_size = sizeof(afs_int32);
4209     blob.out_size = 0;
4210
4211     code = pioctl(NULL, VIOC_RXSTAT_PROC, &blob, 1);
4212     if (code != 0) {
4213         Die(errno, NULL);
4214         return 1;
4215     }
4216
4217     return 0;
4218 }
4219
4220 static int
4221 RxStatPeerCmd(struct cmd_syndesc *as, void *arock)
4222 {
4223     afs_int32 code;
4224     afs_int32 flags = 0;
4225     struct ViceIoctl blob;
4226
4227     if (as->parms[0].items) {   /* -enable */
4228         flags |= AFSCALL_RXSTATS_ENABLE;
4229     }
4230     if (as->parms[1].items) {   /* -disable */
4231         flags |= AFSCALL_RXSTATS_DISABLE;
4232     }
4233     if (as->parms[2].items) {   /* -clear */
4234         flags |= AFSCALL_RXSTATS_CLEAR;
4235     }
4236     if (flags == 0) {
4237         fprintf(stderr, "You must specify at least one argument\n");
4238         return 1;
4239     }
4240
4241     blob.in = (char *)&flags;
4242     blob.in_size = sizeof(afs_int32);
4243     blob.out_size = 0;
4244
4245     code = pioctl(NULL, VIOC_RXSTAT_PEER, &blob, 1);
4246     if (code != 0) {
4247         Die(errno, NULL);
4248         return 1;
4249     }
4250
4251     return 0;
4252 }
4253
4254 static int
4255 GetFidCmd(struct cmd_syndesc *as, void *arock)
4256 {
4257     struct ViceIoctl blob;
4258     struct cmd_item *ti;
4259
4260     afs_int32 code;
4261     int error = 0;
4262     char cell[MAXCELLCHARS];
4263
4264     SetDotDefault(&as->parms[0].items);
4265     for (ti = as->parms[0].items; ti; ti = ti->next) {
4266         struct VenusFid vfid;
4267
4268         blob.out_size = sizeof(struct VenusFid);
4269         blob.out = (char *) &vfid;
4270         blob.in_size = 0;
4271
4272         code = pioctl(ti->data, VIOCGETFID, &blob, 1);
4273         if (code) {
4274             Die(errno,ti->data);
4275             error = 1;
4276             continue;
4277         }
4278
4279         code = GetCell(ti->data, cell);
4280         if (code) {
4281             if (errno == ENOENT)
4282                 fprintf(stderr, "%s: no such cell as '%s'\n", pn, ti->data);
4283             else
4284                 Die(errno, ti->data);
4285             error = 1;
4286             continue;
4287         }
4288
4289         printf("File %s (%u.%u.%u) located in cell %s\n",
4290                ti->data, vfid.Fid.Volume, vfid.Fid.Vnode, vfid.Fid.Unique,
4291                cell);
4292
4293     }
4294
4295     return error;
4296 }
4297