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