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