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