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