Consolidate duplicate definitions
[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 <netinet/in.h>
23 #include <sys/stat.h>
24 #include <afs/stds.h>
25 #include <afs/vice.h>
26 #include <afs/venus.h>
27 #include <afs/com_err.h>
28 #include <afs/afs_consts.h>
29 #ifdef  AFS_AIX32_ENV
30 #include <signal.h>
31 #endif
32
33 #include <string.h>
34
35 #undef VIRTUE
36 #undef VICE
37 #include "afs/prs_fs.h"
38 #include <afs/afsint.h>
39 #include <afs/cellconfig.h>
40 #include <ubik.h>
41 #include <rx/rxkad.h>
42 #include <rx/rx_globals.h>
43 #include <afs/vldbint.h>
44 #include <afs/volser.h>
45 #include <afs/vlserver.h>
46 #include <afs/cmd.h>
47 #include <afs/afsutil.h>
48 #include <afs/com_err.h>
49 #include <stdlib.h>
50 #include <assert.h>
51 #include <afs/ptclient.h>
52 #include <afs/ptuser.h>
53 #include <afs/afsutil.h>
54 #include <afs/sys_prototypes.h>
55
56 #define MAXNAME 100
57 #define MAXINSIZE 1300          /* pioctl complains if data is larger than this */
58 #define VMSGSIZE 128            /* size of msg buf in volume hdr */
59
60 static char space[MAXSIZE];
61 static char tspace[1024];
62 static struct ubik_client *uclient;
63
64 static int GetClientAddrsCmd(struct cmd_syndesc *, void *);
65 static int SetClientAddrsCmd(struct cmd_syndesc *, void *);
66 static int FlushMountCmd(struct cmd_syndesc *, void *);
67 static int RxStatProcCmd(struct cmd_syndesc *, void *);
68 static int RxStatPeerCmd(struct cmd_syndesc *, void *);
69 static int GetFidCmd(struct cmd_syndesc *, void *);
70 static int UuidCmd(struct cmd_syndesc *, void *);
71
72 static char pn[] = "fs";
73 static int rxInitDone = 0;
74
75 struct AclEntry;
76 struct Acl;
77 static void ZapList(struct AclEntry *);
78 static int PruneList(struct AclEntry **, int);
79 static int CleanAcl(struct Acl *, char *);
80 static int SetVolCmd(struct cmd_syndesc *as, void *arock);
81 static int GetCellName(char *, struct afsconf_cell *);
82 static int VLDBInit(int, struct afsconf_cell *);
83 static void Die(int, char *);
84
85 /*
86  * Character to use between name and rights in printed representation for
87  * DFS ACL's.
88  */
89 #define DFS_SEPARATOR   ' '
90
91 typedef char sec_rgy_name_t[1025];      /* A DCE definition */
92
93 struct Acl {
94     int dfs;                    /* Originally true if a dfs acl; now also the type
95                                  * of the acl (1, 2, or 3, corresponding to object,
96                                  * initial dir, or initial object). */
97     sec_rgy_name_t cell;        /* DFS cell name */
98     int nplus;
99     int nminus;
100     struct AclEntry *pluslist;
101     struct AclEntry *minuslist;
102 };
103
104 struct AclEntry {
105     struct AclEntry *next;
106     char name[MAXNAME];
107     afs_int32 rights;
108 };
109
110 struct vcxstat2 {
111     afs_int32 callerAccess;
112     afs_int32 cbExpires;
113     afs_int32 anyAccess;
114     char mvstat;
115 };
116
117 static void
118 ZapAcl(struct Acl *acl)
119 {
120     if (!acl)
121         return;
122     ZapList(acl->pluslist);
123     ZapList(acl->minuslist);
124     free(acl);
125 }
126
127 static int
128 foldcmp(char *a, char *b)
129 {
130     char t, u;
131     while (1) {
132         t = *a++;
133         u = *b++;
134         if (t >= 'A' && t <= 'Z')
135             t += 0x20;
136         if (u >= 'A' && u <= 'Z')
137             u += 0x20;
138         if (t != u)
139             return 1;
140         if (t == 0)
141             return 0;
142     }
143 }
144
145 /*
146  * Mods for the AFS/DFS protocol translator.
147  *
148  * DFS rights. It's ugly to put these definitions here, but they
149  * *cannot* change, because they're part of the wire protocol.
150  * In any event, the protocol translator will guarantee these
151  * assignments for AFS cache managers.
152  */
153 #define DFS_READ          0x01
154 #define DFS_WRITE         0x02
155 #define DFS_EXECUTE       0x04
156 #define DFS_CONTROL       0x08
157 #define DFS_INSERT        0x10
158 #define DFS_DELETE        0x20
159
160 /* the application definable ones (backwards from AFS) */
161 #define DFS_USR0 0x80000000     /* "A" bit */
162 #define DFS_USR1 0x40000000     /* "B" bit */
163 #define DFS_USR2 0x20000000     /* "C" bit */
164 #define DFS_USR3 0x10000000     /* "D" bit */
165 #define DFS_USR4 0x08000000     /* "E" bit */
166 #define DFS_USR5 0x04000000     /* "F" bit */
167 #define DFS_USR6 0x02000000     /* "G" bit */
168 #define DFS_USR7 0x01000000     /* "H" bit */
169 #define DFS_USRALL      (DFS_USR0 | DFS_USR1 | DFS_USR2 | DFS_USR3 |\
170                          DFS_USR4 | DFS_USR5 | DFS_USR6 | DFS_USR7)
171
172 /*
173  * Offset of -id switch in command structure for various commands.
174  * The -if switch is the next switch always.
175  */
176 static int parm_setacl_id, parm_copyacl_id, parm_listacl_id;
177
178 /*
179  * Determine whether either the -id or -if switches are present, and
180  * return 0, 1 or 2, as appropriate. Abort if both switches are present.
181  */
182 /*    int id;   Offset of -id switch; -if is next switch */
183 static int
184 getidf(struct cmd_syndesc *as, int id)
185 {
186     int idf = 0;
187
188     if (as->parms[id].items) {
189         idf |= 1;
190     }
191     if (as->parms[id + 1].items) {
192         idf |= 2;
193     }
194     if (idf == 3) {
195         fprintf(stderr,
196                 "%s: you may specify either -id or -if, but not both switches\n",
197                 pn);
198         exit(1);
199     }
200     return idf;
201 }
202
203 static int
204 PRights(afs_int32 arights, int dfs)
205 {
206     if (!dfs) {
207         if (arights & PRSFS_READ)
208             printf("r");
209         if (arights & PRSFS_LOOKUP)
210             printf("l");
211         if (arights & PRSFS_INSERT)
212             printf("i");
213         if (arights & PRSFS_DELETE)
214             printf("d");
215         if (arights & PRSFS_WRITE)
216             printf("w");
217         if (arights & PRSFS_LOCK)
218             printf("k");
219         if (arights & PRSFS_ADMINISTER)
220             printf("a");
221         if (arights & PRSFS_USR0)
222             printf("A");
223         if (arights & PRSFS_USR1)
224             printf("B");
225         if (arights & PRSFS_USR2)
226             printf("C");
227         if (arights & PRSFS_USR3)
228             printf("D");
229         if (arights & PRSFS_USR4)
230             printf("E");
231         if (arights & PRSFS_USR5)
232             printf("F");
233         if (arights & PRSFS_USR6)
234             printf("G");
235         if (arights & PRSFS_USR7)
236             printf("H");
237     } else {
238         if (arights & DFS_READ)
239             printf("r");
240         else
241             printf("-");
242         if (arights & DFS_WRITE)
243             printf("w");
244         else
245             printf("-");
246         if (arights & DFS_EXECUTE)
247             printf("x");
248         else
249             printf("-");
250         if (arights & DFS_CONTROL)
251             printf("c");
252         else
253             printf("-");
254         if (arights & DFS_INSERT)
255             printf("i");
256         else
257             printf("-");
258         if (arights & DFS_DELETE)
259             printf("d");
260         else
261             printf("-");
262         if (arights & (DFS_USRALL))
263             printf("+");
264         if (arights & DFS_USR0)
265             printf("A");
266         if (arights & DFS_USR1)
267             printf("B");
268         if (arights & DFS_USR2)
269             printf("C");
270         if (arights & DFS_USR3)
271             printf("D");
272         if (arights & DFS_USR4)
273             printf("E");
274         if (arights & DFS_USR5)
275             printf("F");
276         if (arights & DFS_USR6)
277             printf("G");
278         if (arights & DFS_USR7)
279             printf("H");
280     }
281     return 0;
282 }
283
284 /* this function returns TRUE (1) if the file is in AFS, otherwise false (0) */
285 static int
286 InAFS(char *apath)
287 {
288     struct ViceIoctl blob;
289     afs_int32 code;
290
291     blob.in_size = 0;
292     blob.out_size = MAXSIZE;
293     blob.out = space;
294
295     code = pioctl(apath, VIOC_FILE_CELL_NAME, &blob, 1);
296     if (code) {
297         if ((errno == EINVAL) || (errno == ENOENT))
298             return 0;
299     }
300     return 1;
301 }
302
303 /* return a static pointer to a buffer */
304 static char *
305 Parent(char *apath)
306 {
307     char *tp;
308     strlcpy(tspace, apath, sizeof(tspace));
309     tp = strrchr(tspace, '/');
310     if (tp == (char *)tspace)
311         tp++;
312     else if (tp == (char *)NULL) {
313         tp      = (char *)tspace;
314         *(tp++) = '.';
315     }
316     *tp = '\0';
317     return tspace;
318 }
319
320 enum rtype { add, destroy, deny };
321
322 static afs_int32
323 Convert(char *arights, int dfs, enum rtype *rtypep)
324 {
325     int i, len;
326     afs_int32 mode;
327     char tc;
328
329     *rtypep = add;              /* add rights, by default */
330
331     if (dfs) {
332         if (!strcmp(arights, "null")) {
333             *rtypep = deny;
334             return 0;
335         }
336         if (!strcmp(arights, "read"))
337             return DFS_READ | DFS_EXECUTE;
338         if (!strcmp(arights, "write"))
339             return DFS_READ | DFS_EXECUTE | DFS_INSERT | DFS_DELETE |
340                 DFS_WRITE;
341         if (!strcmp(arights, "all"))
342             return DFS_READ | DFS_EXECUTE | DFS_INSERT | DFS_DELETE |
343                 DFS_WRITE | DFS_CONTROL;
344     } else {
345         if (!strcmp(arights, "read"))
346             return PRSFS_READ | PRSFS_LOOKUP;
347         if (!strcmp(arights, "write"))
348             return PRSFS_READ | PRSFS_LOOKUP | PRSFS_INSERT | PRSFS_DELETE |
349                 PRSFS_WRITE | PRSFS_LOCK;
350         if (!strcmp(arights, "mail"))
351             return PRSFS_INSERT | PRSFS_LOCK | PRSFS_LOOKUP;
352         if (!strcmp(arights, "all"))
353             return PRSFS_READ | PRSFS_LOOKUP | PRSFS_INSERT | PRSFS_DELETE |
354                 PRSFS_WRITE | PRSFS_LOCK | PRSFS_ADMINISTER;
355     }
356     if (!strcmp(arights, "none")) {
357         *rtypep = destroy;      /* Remove entire entry */
358         return 0;
359     }
360     len = strlen(arights);
361     mode = 0;
362     for (i = 0; i < len; i++) {
363         tc = *arights++;
364         if (dfs) {
365             if (tc == '-')
366                 continue;
367             else if (tc == 'r')
368                 mode |= DFS_READ;
369             else if (tc == 'w')
370                 mode |= DFS_WRITE;
371             else if (tc == 'x')
372                 mode |= DFS_EXECUTE;
373             else if (tc == 'c')
374                 mode |= DFS_CONTROL;
375             else if (tc == 'i')
376                 mode |= DFS_INSERT;
377             else if (tc == 'd')
378                 mode |= DFS_DELETE;
379             else if (tc == 'A')
380                 mode |= DFS_USR0;
381             else if (tc == 'B')
382                 mode |= DFS_USR1;
383             else if (tc == 'C')
384                 mode |= DFS_USR2;
385             else if (tc == 'D')
386                 mode |= DFS_USR3;
387             else if (tc == 'E')
388                 mode |= DFS_USR4;
389             else if (tc == 'F')
390                 mode |= DFS_USR5;
391             else if (tc == 'G')
392                 mode |= DFS_USR6;
393             else if (tc == 'H')
394                 mode |= DFS_USR7;
395             else {
396                 fprintf(stderr, "%s: illegal DFS rights character '%c'.\n",
397                         pn, tc);
398                 exit(1);
399             }
400         } else {
401             if (tc == 'r')
402                 mode |= PRSFS_READ;
403             else if (tc == 'l')
404                 mode |= PRSFS_LOOKUP;
405             else if (tc == 'i')
406                 mode |= PRSFS_INSERT;
407             else if (tc == 'd')
408                 mode |= PRSFS_DELETE;
409             else if (tc == 'w')
410                 mode |= PRSFS_WRITE;
411             else if (tc == 'k')
412                 mode |= PRSFS_LOCK;
413             else if (tc == 'a')
414                 mode |= PRSFS_ADMINISTER;
415             else if (tc == 'A')
416                 mode |= PRSFS_USR0;
417             else if (tc == 'B')
418                 mode |= PRSFS_USR1;
419             else if (tc == 'C')
420                 mode |= PRSFS_USR2;
421             else if (tc == 'D')
422                 mode |= PRSFS_USR3;
423             else if (tc == 'E')
424                 mode |= PRSFS_USR4;
425             else if (tc == 'F')
426                 mode |= PRSFS_USR5;
427             else if (tc == 'G')
428                 mode |= PRSFS_USR6;
429             else if (tc == 'H')
430                 mode |= PRSFS_USR7;
431             else {
432                 fprintf(stderr, "%s: illegal rights character '%c'.\n", pn,
433                         tc);
434                 exit(1);
435             }
436         }
437     }
438     return mode;
439 }
440
441 static struct AclEntry *
442 FindList(struct AclEntry *alist, char *aname)
443 {
444     while (alist) {
445         if (!foldcmp(alist->name, aname))
446             return alist;
447         alist = alist->next;
448     }
449     return 0;
450 }
451
452 /* if no parm specified in a particular slot, set parm to be "." instead */
453 static void
454 SetDotDefault(struct cmd_item **aitemp)
455 {
456     struct cmd_item *ti;
457     if (*aitemp)
458         return;                 /* already has value */
459     /* otherwise, allocate an item representing "." */
460     ti = (struct cmd_item *)malloc(sizeof(struct cmd_item));
461     assert(ti);
462     ti->next = (struct cmd_item *)0;
463     ti->data = (char *)malloc(2);
464     assert(ti->data);
465     strcpy(ti->data, ".");
466     *aitemp = ti;
467 }
468
469 static void
470 ChangeList(struct Acl *al, afs_int32 plus, char *aname, afs_int32 arights)
471 {
472     struct AclEntry *tlist;
473     tlist = (plus ? al->pluslist : al->minuslist);
474     tlist = FindList(tlist, aname);
475     if (tlist) {
476         /* Found the item already in the list. */
477         tlist->rights = arights;
478         if (plus)
479             al->nplus -= PruneList(&al->pluslist, al->dfs);
480         else
481             al->nminus -= PruneList(&al->minuslist, al->dfs);
482         return;
483     }
484     /* Otherwise we make a new item and plug in the new data. */
485     tlist = (struct AclEntry *)malloc(sizeof(struct AclEntry));
486     assert(tlist);
487     strcpy(tlist->name, aname);
488     tlist->rights = arights;
489     if (plus) {
490         tlist->next = al->pluslist;
491         al->pluslist = tlist;
492         al->nplus++;
493         if (arights == 0 || arights == -1)
494             al->nplus -= PruneList(&al->pluslist, al->dfs);
495     } else {
496         tlist->next = al->minuslist;
497         al->minuslist = tlist;
498         al->nminus++;
499         if (arights == 0)
500             al->nminus -= PruneList(&al->minuslist, al->dfs);
501     }
502 }
503
504 static void
505 ZapList(struct AclEntry *alist)
506 {
507     struct AclEntry *tp, *np;
508     for (tp = alist; tp; tp = np) {
509         np = tp->next;
510         free(tp);
511     }
512 }
513
514 static int
515 PruneList(struct AclEntry **ae, int dfs)
516 {
517     struct AclEntry **lp;
518     struct AclEntry *te, *ne;
519     afs_int32 ctr;
520     ctr = 0;
521     lp = ae;
522     for (te = *ae; te; te = ne) {
523         if ((!dfs && te->rights == 0) || te->rights == -1) {
524             *lp = te->next;
525             ne = te->next;
526             free(te);
527             ctr++;
528         } else {
529             ne = te->next;
530             lp = &te->next;
531         }
532     }
533     return ctr;
534 }
535
536 static char *
537 SkipLine(char *astr)
538 {
539     while (*astr != '\n')
540         astr++;
541     astr++;
542     return astr;
543 }
544
545 /*
546  * Create an empty acl, taking into account whether the acl pointed
547  * to by astr is an AFS or DFS acl. Only parse this minimally, so we
548  * can recover from problems caused by bogus ACL's (in that case, always
549  * assume that the acl is AFS: for DFS, the user can always resort to
550  * acl_edit, but for AFS there may be no other way out).
551  */
552 static struct Acl *
553 EmptyAcl(char *astr)
554 {
555     struct Acl *tp;
556     int junk;
557
558     tp = (struct Acl *)malloc(sizeof(struct Acl));
559     assert(tp);
560     tp->nplus = tp->nminus = 0;
561     tp->pluslist = tp->minuslist = 0;
562     tp->dfs = 0;
563     sscanf(astr, "%d dfs:%d %s", &junk, &tp->dfs, tp->cell);
564     return tp;
565 }
566
567 static struct Acl *
568 ParseAcl(char *astr)
569 {
570     int nplus, nminus, i, trights;
571     char tname[MAXNAME];
572     struct AclEntry *first, *last, *tl;
573     struct Acl *ta;
574
575     ta = (struct Acl *)malloc(sizeof(struct Acl));
576     assert(ta);
577     ta->dfs = 0;
578     sscanf(astr, "%d dfs:%d %s", &ta->nplus, &ta->dfs, ta->cell);
579     astr = SkipLine(astr);
580     sscanf(astr, "%d", &ta->nminus);
581     astr = SkipLine(astr);
582
583     nplus = ta->nplus;
584     nminus = ta->nminus;
585
586     last = 0;
587     first = 0;
588     for (i = 0; i < nplus; i++) {
589         sscanf(astr, "%100s %d", tname, &trights);
590         astr = SkipLine(astr);
591         tl = (struct AclEntry *)malloc(sizeof(struct AclEntry));
592         assert(tl);
593         if (!first)
594             first = tl;
595         strcpy(tl->name, tname);
596         tl->rights = trights;
597         tl->next = 0;
598         if (last)
599             last->next = tl;
600         last = tl;
601     }
602     ta->pluslist = first;
603
604     last = 0;
605     first = 0;
606     for (i = 0; i < nminus; i++) {
607         sscanf(astr, "%100s %d", tname, &trights);
608         astr = SkipLine(astr);
609         tl = (struct AclEntry *)malloc(sizeof(struct AclEntry));
610         assert(tl);
611         if (!first)
612             first = tl;
613         strcpy(tl->name, tname);
614         tl->rights = trights;
615         tl->next = 0;
616         if (last)
617             last->next = tl;
618         last = tl;
619     }
620     ta->minuslist = first;
621
622     return ta;
623 }
624
625 static int
626 PrintStatus(VolumeStatus * status, char *name, char *offmsg)
627 {
628     printf("Volume status for vid = %u named %s\n", status->Vid, name);
629     if (*offmsg != 0)
630         printf("Current offline message is %s\n", offmsg);
631     printf("Current disk quota is ");
632     if (status->MaxQuota != 0)
633         printf("%d\n", status->MaxQuota);
634     else
635         printf("unlimited\n");
636     printf("Current blocks used are %d\n", status->BlocksInUse);
637     printf("The partition has %d blocks available out of %d\n\n",
638            status->PartBlocksAvail, status->PartMaxBlocks);
639     return 0;
640 }
641
642 static int
643 QuickPrintStatus(VolumeStatus * status, char *name)
644 {
645     double QuotaUsed = 0.0;
646     double PartUsed = 0.0;
647     int WARN = 0;
648     printf("%-25.25s", name);
649
650     if (status->MaxQuota != 0) {
651         printf(" %10d %10d", status->MaxQuota, status->BlocksInUse);
652         QuotaUsed =
653             ((((double)status->BlocksInUse) / status->MaxQuota) * 100.0);
654     } else {
655         printf("   no limit %10d", status->BlocksInUse);
656     }
657     if (QuotaUsed > 90.0) {
658         printf("%5.0f%%<<", QuotaUsed);
659         WARN = 1;
660     } else
661         printf("%5.0f%%  ", QuotaUsed);
662     PartUsed =
663         (100.0 -
664          ((((double)status->PartBlocksAvail) / status->PartMaxBlocks) *
665           100.0));
666     if (PartUsed > 97.0) {
667         printf("%9.0f%%<<", PartUsed);
668         WARN = 1;
669     } else
670         printf("%9.0f%%  ", PartUsed);
671     if (WARN) {
672         printf("  <<WARNING\n");
673     } else
674         printf("\n");
675     return 0;
676 }
677
678 static int
679 QuickPrintSpace(VolumeStatus * status, char *name)
680 {
681     double PartUsed = 0.0;
682     int WARN = 0;
683     printf("%-25.25s", name);
684
685     printf("%10d%10d%10d", status->PartMaxBlocks,
686            status->PartMaxBlocks - status->PartBlocksAvail,
687            status->PartBlocksAvail);
688
689     PartUsed =
690         (100.0 -
691          ((((double)status->PartBlocksAvail) / status->PartMaxBlocks) *
692           100.0));
693     if (PartUsed > 90.0) {
694         printf(" %4.0f%%<<", PartUsed);
695         WARN = 1;
696     } else
697         printf(" %4.0f%%  ", PartUsed);
698     if (WARN) {
699         printf("  <<WARNING\n");
700     } else
701         printf("\n");
702     return 0;
703 }
704
705 static char *
706 AclToString(struct Acl *acl)
707 {
708     static char mydata[MAXSIZE];
709     char tstring[MAXSIZE];
710     char dfsstring[30];
711     struct AclEntry *tp;
712
713     if (acl->dfs)
714         sprintf(dfsstring, " dfs:%d %s", acl->dfs, acl->cell);
715     else
716         dfsstring[0] = '\0';
717     sprintf(mydata, "%d%s\n%d\n", acl->nplus, dfsstring, acl->nminus);
718     for (tp = acl->pluslist; tp; tp = tp->next) {
719         sprintf(tstring, "%s %d\n", tp->name, tp->rights);
720         strcat(mydata, tstring);
721     }
722     for (tp = acl->minuslist; tp; tp = tp->next) {
723         sprintf(tstring, "%s %d\n", tp->name, tp->rights);
724         strcat(mydata, tstring);
725     }
726     return mydata;
727 }
728
729 static int
730 SetACLCmd(struct cmd_syndesc *as, void *arock)
731 {
732     afs_int32 code;
733     struct ViceIoctl blob;
734     struct Acl *ta = 0;
735     struct cmd_item *ti, *ui;
736     int plusp;
737     afs_int32 rights;
738     int clear;
739     int idf = getidf(as, parm_setacl_id);
740     int error = 0;
741
742     if (as->parms[2].items)
743         clear = 1;
744     else
745         clear = 0;
746     plusp = !(as->parms[3].items);
747     for (ti = as->parms[0].items; ti; ti = ti->next) {
748         blob.out_size = MAXSIZE;
749         blob.in_size = idf;
750         blob.in = blob.out = space;
751         code = pioctl(ti->data, VIOCGETAL, &blob, 1);
752         if (code) {
753             Die(errno, ti->data);
754             error = 1;
755             continue;
756         }
757
758         if (ta)
759             ZapAcl(ta);
760         ta = ParseAcl(space);
761         if (!plusp && ta->dfs) {
762             fprintf(stderr,
763                     "%s: %s: you may not use the -negative switch with DFS acl's.\n%s",
764                     pn, ti->data,
765                     "(you may specify \"null\" to revoke all rights, however)\n");
766             error = 1;
767             continue;
768         }
769
770         if (ta)
771             ZapAcl(ta);
772         if (clear)
773             ta = EmptyAcl(space);
774         else
775             ta = ParseAcl(space);
776         CleanAcl(ta, ti->data);
777         for (ui = as->parms[1].items; ui; ui = ui->next->next) {
778             enum rtype rtype;
779             if (!ui->next) {
780                 fprintf(stderr,
781                         "%s: Missing second half of user/access pair.\n", pn);
782                 ZapAcl(ta);
783                 return 1;
784             }
785             rights = Convert(ui->next->data, ta->dfs, &rtype);
786             if (rtype == destroy && !ta->dfs) {
787                 struct AclEntry *tlist;
788
789                 tlist = (plusp ? ta->pluslist : ta->minuslist);
790                 if (!FindList(tlist, ui->data))
791                     continue;
792             }
793             if (rtype == deny && !ta->dfs)
794                 plusp = 0;
795             if (rtype == destroy && ta->dfs)
796                 rights = -1;
797             ChangeList(ta, plusp, ui->data, rights);
798         }
799         blob.in = AclToString(ta);
800         blob.out_size = 0;
801         blob.in_size = 1 + strlen(blob.in);
802         code = pioctl(ti->data, VIOCSETAL, &blob, 1);
803         if (code) {
804             if (errno == EINVAL) {
805                 if (ta->dfs) {
806                     static char *fsenv = 0;
807                     if (!fsenv) {
808                         fsenv = (char *)getenv("FS_EXPERT");
809                     }
810                     fprintf(stderr,
811                             "%s: \"Invalid argument\" was returned when you tried to store a DFS access list.\n",
812                             pn);
813                     if (!fsenv) {
814                         fprintf(stderr,
815                                 "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
816                                 "\nPossible reasons for this include:\n\n",
817                                 " -You may have specified an inappropriate combination of rights.\n",
818                                 "  For example, some DFS-supported filesystems may not allow you to\n",
819                                 "  drop the \"c\" right from \"user_obj\".\n\n",
820                                 " -A mask_obj may be required (it is likely required by the underlying\n",
821                                 "  filesystem if you try to set anything other than the basic \"user_obj\"\n",
822                                 "  \"mask_obj\", or \"group_obj\" entries). Unlike acl_edit, the fs command\n",
823                                 "  does not automatically create or update the mask_obj. Try setting\n",
824                                 "  the rights \"mask_obj all\" with \"fs sa\" before adding any explicit\n",
825                                 "  users or groups. You can do this with a single command, such as\n",
826                                 "  \"fs sa mask_obj all user:somename read\"\n\n",
827                                 " -A specified user or group may not exist.\n\n",
828                                 " -You may have tried to delete \"user_obj\", \"group_obj\", or \"other_obj\".\n",
829                                 "  This is probably not allowed by the underlying file system.\n\n",
830                                 " -If you add a user or group to a DFS ACL, remember that it must be\n",
831                                 "  fully specified as \"user:username\" or \"group:groupname\". In addition, there\n",
832                                 "  may be local requirements on the format of the user or group name.\n",
833                                 "  Check with your cell administrator.\n\n",
834                                 " -Or numerous other possibilities. It would be great if we could be more\n",
835                                 "  precise about the actual problem, but for various reasons, this is\n",
836                                 "  impractical via this interface.  If you can't figure it out, you\n",
837                                 "  might try logging into a DCE-equipped machine and use acl_edit (or\n",
838                                 "  whatever is provided). You may get better results. Good luck!\n\n",
839                                 " (You may inhibit this message by setting \"FS_EXPERT\" in your environment)\n");
840                     }
841                 } else {
842                     fprintf(stderr,
843                             "%s: Invalid argument, possible reasons include:\n",
844                             pn);
845                     fprintf(stderr, "\t-File not in AFS\n");
846                     fprintf(stderr,
847                             "\t-Too many users on access control list\n");
848                     fprintf(stderr,
849                             "\t-Tried to add non-existent user to access control list\n");
850                 }
851             } else {
852                 Die(errno, ti->data);
853             }
854             error = 1;
855         }
856     }
857     if (ta)
858         ZapAcl(ta);
859     return error;
860 }
861
862
863 static int
864 CopyACLCmd(struct cmd_syndesc *as, void *arock)
865 {
866     afs_int32 code;
867     struct ViceIoctl blob;
868     struct Acl *fa, *ta = 0;
869     struct AclEntry *tp;
870     struct cmd_item *ti;
871     int clear;
872     int idf = getidf(as, parm_copyacl_id);
873     int error = 0;
874
875     if (as->parms[2].items)
876         clear = 1;
877     else
878         clear = 0;
879     blob.out_size = MAXSIZE;
880     blob.in_size = idf;
881     blob.in = blob.out = space;
882     code = pioctl(as->parms[0].items->data, VIOCGETAL, &blob, 1);
883     if (code) {
884         Die(errno, as->parms[0].items->data);
885         return 1;
886     }
887     fa = ParseAcl(space);
888     CleanAcl(fa, as->parms[0].items->data);
889     for (ti = as->parms[1].items; ti; ti = ti->next) {
890         blob.out_size = MAXSIZE;
891         blob.in_size = idf;
892         blob.in = blob.out = space;
893         code = pioctl(ti->data, VIOCGETAL, &blob, 1);
894         if (code) {
895             Die(errno, ti->data);
896             error = 1;
897             continue;
898         }
899
900         if (ta)
901             ZapAcl(ta);
902         if (clear)
903             ta = EmptyAcl(space);
904         else
905             ta = ParseAcl(space);
906         CleanAcl(ta, ti->data);
907         if (ta->dfs != fa->dfs) {
908             fprintf(stderr,
909                     "%s: incompatible file system types: acl not copied to %s; aborted\n",
910                     pn, ti->data);
911             error = 1;
912             continue;
913         }
914         if (ta->dfs) {
915             if (!clear && strcmp(ta->cell, fa->cell) != 0) {
916                 fprintf(stderr,
917                         "%s: default DCE cell differs for file %s: use \"-clear\" switch; acl not merged\n",
918                         pn, ti->data);
919                 error = 1;
920                 continue;
921             }
922             strcpy(ta->cell, fa->cell);
923         }
924         for (tp = fa->pluslist; tp; tp = tp->next)
925             ChangeList(ta, 1, tp->name, tp->rights);
926         for (tp = fa->minuslist; tp; tp = tp->next)
927             ChangeList(ta, 0, tp->name, tp->rights);
928         blob.in = AclToString(ta);
929         blob.out_size = 0;
930         blob.in_size = 1 + strlen(blob.in);
931         code = pioctl(ti->data, VIOCSETAL, &blob, 1);
932         if (code) {
933             if (errno == EINVAL) {
934                 fprintf(stderr,
935                         "%s: Invalid argument, possible reasons include:\n",
936                         pn);
937                 fprintf(stderr, "\t-File not in AFS\n");
938             } else {
939                 Die(errno, ti->data);
940             }
941             error = 1;
942         }
943     }
944     if (ta)
945         ZapAcl(ta);
946     ZapAcl(fa);
947     return error;
948 }
949
950 /* pioctl() call to get the cellname of a pathname */
951 static afs_int32
952 GetCell(char *fname, char *cellname)
953 {
954     afs_int32 code;
955     struct ViceIoctl blob;
956
957     blob.in_size = 0;
958     blob.out_size = MAXCELLCHARS;
959     blob.out = cellname;
960
961     code = pioctl(fname, VIOC_FILE_CELL_NAME, &blob, 1);
962     return code ? errno : 0;
963 }
964
965 /* Check if a username is valid: If it contains only digits (or a
966  * negative sign), then it might be bad. We then query the ptserver
967  * to see.
968  */
969 static int
970 BadName(char *aname, char *fname)
971 {
972     afs_int32 tc, code, id;
973     char *nm;
974     char cell[MAXCELLCHARS];
975
976     for (nm = aname; (tc = *nm); nm++) {
977         /* all must be '-' or digit to be bad */
978         if (tc != '-' && (tc < '0' || tc > '9'))
979             return 0;
980     }
981
982     /* Go to the PRDB and see if this all number username is valid */
983     code = GetCell(fname, cell);
984     if (code)
985         return 0;
986
987     pr_Initialize(1, AFSDIR_CLIENT_ETC_DIRPATH, cell);
988     code = pr_SNameToId(aname, &id);
989     pr_End();
990
991     /* 1=>Not-valid; 0=>Valid */
992     return ((!code && (id == ANONYMOUSID)) ? 1 : 0);
993 }
994
995
996 /* clean up an access control list of its bad entries; return 1 if we made
997    any changes to the list, and 0 otherwise */
998 static int
999 CleanAcl(struct Acl *aa, char *fname)
1000 {
1001     struct AclEntry *te, **le, *ne;
1002     int changes;
1003
1004     /* Don't correct DFS ACL's for now */
1005     if (aa->dfs)
1006         return 0;
1007
1008     /* prune out bad entries */
1009     changes = 0;                /* count deleted entries */
1010     le = &aa->pluslist;
1011     for (te = aa->pluslist; te; te = ne) {
1012         ne = te->next;
1013         if (BadName(te->name, fname)) {
1014             /* zap this dude */
1015             *le = te->next;
1016             aa->nplus--;
1017             free(te);
1018             changes++;
1019         } else {
1020             le = &te->next;
1021         }
1022     }
1023     le = &aa->minuslist;
1024     for (te = aa->minuslist; te; te = ne) {
1025         ne = te->next;
1026         if (BadName(te->name, fname)) {
1027             /* zap this dude */
1028             *le = te->next;
1029             aa->nminus--;
1030             free(te);
1031             changes++;
1032         } else {
1033             le = &te->next;
1034         }
1035     }
1036     return changes;
1037 }
1038
1039
1040 /* clean up an acl to not have bogus entries */
1041 static int
1042 CleanACLCmd(struct cmd_syndesc *as, void *arock)
1043 {
1044     afs_int32 code;
1045     struct Acl *ta = 0;
1046     struct ViceIoctl blob;
1047     int changes;
1048     struct cmd_item *ti;
1049     struct AclEntry *te;
1050     int error = 0;
1051
1052     SetDotDefault(&as->parms[0].items);
1053     for (ti = as->parms[0].items; ti; ti = ti->next) {
1054         blob.out_size = MAXSIZE;
1055         blob.in_size = 0;
1056         blob.out = space;
1057         code = pioctl(ti->data, VIOCGETAL, &blob, 1);
1058         if (code) {
1059             Die(errno, ti->data);
1060             error = 1;
1061             continue;
1062         }
1063
1064         if (ta)
1065             ZapAcl(ta);
1066         ta = ParseAcl(space);
1067         if (ta->dfs) {
1068             fprintf(stderr,
1069                     "%s: cleanacl is not supported for DFS access lists.\n",
1070                     pn);
1071             error = 1;
1072             continue;
1073         }
1074
1075         changes = CleanAcl(ta, ti->data);
1076
1077         if (changes) {
1078             /* now set the acl */
1079             blob.in = AclToString(ta);
1080             blob.in_size = strlen(blob.in) + 1;
1081             blob.out_size = 0;
1082             code = pioctl(ti->data, VIOCSETAL, &blob, 1);
1083             if (code) {
1084                 if (errno == EINVAL) {
1085                     fprintf(stderr,
1086                             "%s: Invalid argument, possible reasons include\n",
1087                             pn);
1088                     fprintf(stderr, "%s: File not in vice or\n", pn);
1089                     fprintf(stderr,
1090                             "%s: Too many users on access control list or\n",
1091                             pn);
1092                 } else {
1093                     Die(errno, ti->data);
1094                 }
1095                 error = 1;
1096                 continue;
1097             }
1098
1099             /* now list the updated acl */
1100             printf("Access list for %s is now\n", ti->data);
1101             if (ta->nplus > 0) {
1102                 if (!ta->dfs)
1103                     printf("Normal rights:\n");
1104                 for (te = ta->pluslist; te; te = te->next) {
1105                     printf("  %s ", te->name);
1106                     PRights(te->rights, ta->dfs);
1107                     printf("\n");
1108                 }
1109             }
1110             if (ta->nminus > 0) {
1111                 printf("Negative rights:\n");
1112                 for (te = ta->minuslist; te; te = te->next) {
1113                     printf("  %s ", te->name);
1114                     PRights(te->rights, ta->dfs);
1115                     printf("\n");
1116                 }
1117             }
1118             if (ti->next)
1119                 printf("\n");
1120         } else
1121             printf("Access list for %s is fine.\n", ti->data);
1122     }
1123     if (ta)
1124         ZapAcl(ta);
1125     return error;
1126 }
1127
1128 static int
1129 ListACLCmd(struct cmd_syndesc *as, void *arock)
1130 {
1131     afs_int32 code;
1132     struct Acl *ta;
1133     struct ViceIoctl blob;
1134     struct AclEntry *te;
1135     struct cmd_item *ti;
1136     int idf = getidf(as, parm_listacl_id);
1137     int error = 0;
1138
1139     SetDotDefault(&as->parms[0].items);
1140     for (ti = as->parms[0].items; ti; ti = ti->next) {
1141         blob.out_size = MAXSIZE;
1142         blob.in_size = idf;
1143         blob.in = blob.out = space;
1144         code = pioctl(ti->data, VIOCGETAL, &blob, 1);
1145         if (code) {
1146             Die(errno, ti->data);
1147             error = 1;
1148             continue;
1149         }
1150         ta = ParseAcl(space);
1151         if (as->parms[3].items) {                       /* -cmd */
1152             printf("fs setacl -dir %s -acl ", ti->data);
1153             if (ta->nplus > 0) {
1154                 for (te = ta->pluslist; te; te = te->next) {
1155                     printf("  %s ", te->name);
1156                     PRights(te->rights, ta->dfs);
1157                 }
1158             }
1159             printf("\n");
1160             if (ta->nminus > 0) {
1161                 printf("fs setacl -dir %s -acl ", ti->data);
1162                 for (te = ta->minuslist; te; te = te->next) {
1163                     printf("  %s ", te->name);
1164                     PRights(te->rights, ta->dfs);
1165                 }
1166                 printf(" -negative\n");
1167             }
1168         } else {
1169             switch (ta->dfs) {
1170             case 0:
1171                 printf("Access list for %s is\n", ti->data);
1172                 break;
1173             case 1:
1174                 printf("DFS access list for %s is\n", ti->data);
1175                 break;
1176             case 2:
1177                 printf("DFS initial directory access list of %s is\n", ti->data);
1178                 break;
1179             case 3:
1180                 printf("DFS initial file access list of %s is\n", ti->data);
1181                 break;
1182             }
1183             if (ta->dfs) {
1184                 printf("  Default cell = %s\n", ta->cell);
1185             }
1186             if (ta->nplus > 0) {
1187                 if (!ta->dfs)
1188                     printf("Normal rights:\n");
1189                 for (te = ta->pluslist; te; te = te->next) {
1190                     printf("  %s ", te->name);
1191                     PRights(te->rights, ta->dfs);
1192                     printf("\n");
1193                 }
1194             }
1195             if (ta->nminus > 0) {
1196                 printf("Negative rights:\n");
1197                 for (te = ta->minuslist; te; te = te->next) {
1198                     printf("  %s ", te->name);
1199                     PRights(te->rights, ta->dfs);
1200                     printf("\n");
1201                 }
1202             }
1203             if (ti->next)
1204                 printf("\n");
1205         }
1206         ZapAcl(ta);
1207     }
1208     return error;
1209 }
1210
1211 static int
1212 GetCallerAccess(struct cmd_syndesc *as, void *arock)
1213 {
1214     struct cmd_item *ti;
1215     int error = 0;
1216
1217     SetDotDefault(&as->parms[0].items);
1218     for (ti = as->parms[0].items; ti; ti = ti->next) {
1219         afs_int32 code;
1220         struct ViceIoctl blob;
1221         struct vcxstat2 stat;
1222         blob.out_size = sizeof(struct vcxstat2);
1223         blob.in_size = 0;
1224         blob.out = (void *)&stat;
1225         code = pioctl(ti->data, VIOC_GETVCXSTATUS2, &blob, 1);
1226         if (code) {
1227             Die(errno, ti->data);
1228             error = 1;
1229             continue;
1230         }
1231         printf("Callers access to %s is ", ti->data);
1232         PRights(stat.callerAccess, 0);
1233         printf("\n");
1234     }
1235     return error;
1236 }
1237
1238 static int
1239 FlushVolumeCmd(struct cmd_syndesc *as, void *arock)
1240 {
1241     afs_int32 code;
1242     struct ViceIoctl blob;
1243     struct cmd_item *ti;
1244     int error = 0;
1245
1246     SetDotDefault(&as->parms[0].items);
1247     for (ti = as->parms[0].items; ti; ti = ti->next) {
1248         blob.in_size = blob.out_size = 0;
1249         code = pioctl(ti->data, VIOC_FLUSHVOLUME, &blob, 0);
1250         if (code) {
1251             fprintf(stderr, "Error flushing volume ");
1252             perror(ti->data);
1253             error = 1;
1254             continue;
1255         }
1256     }
1257     return error;
1258 }
1259
1260 /* 
1261  * The Windows version of UuidCmd displays the UUID.
1262  * When the UNIX version is updated to do the same
1263  * be sure to replace the CMD_REQUIRED flag with
1264  * CMD_OPTIONAL in the cmd_AddParam(-generate) call 
1265  */
1266 static int
1267 UuidCmd(struct cmd_syndesc *as, void *arock)
1268 {
1269     afs_int32 code;
1270     struct ViceIoctl blob;
1271
1272     blob.in_size = 0;
1273     blob.out_size = 0;
1274     
1275     if (as->parms[0].items) {
1276         if (geteuid()) {
1277             fprintf (stderr, "Permission denied: requires root access.\n");
1278             return EACCES;
1279         }
1280
1281         /* generate new UUID */
1282         code = pioctl(0, VIOC_NEWUUID, &blob, 1);
1283
1284         if (code) {
1285             Die(errno, 0);
1286             return 1;
1287         }
1288
1289         printf("New uuid generated.\n");
1290     } else {
1291         /* This will never execute */
1292         printf("Please add the '-generate' option to generate a new UUID.\n");
1293     }
1294     return 0;
1295 }
1296
1297 #if defined(AFS_CACHE_BYPASS)
1298 /*
1299  * Set cache-bypass threshold.  Files larger than this size will not be cached.
1300  * With a threshold of 0, the cache is always bypassed.  With a threshold of -1,
1301  * cache bypass is disabled.
1302  */
1303  
1304 static int
1305 BypassThresholdCmd(struct cmd_syndesc *as, char *arock)
1306 {
1307     afs_int32 code;
1308     afs_int32 size;
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 = 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 = 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 = 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 = 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 < 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 = 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 = 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 = MAXSIZE;
1741         blob.out = space;
1742         memset(space, 0, 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 = 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, 0);
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, 0);
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 = 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 = 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 = 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 + MAXCELLHOSTS * sizeof(afs_int32));
2257         for (j = 0; j < 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 = 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      * MAXHOSTS (13) servers while 3.5 clients take an array of
2393      * 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 = 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 = MAXHOSTS;
2414     } else {
2415         tp = space;
2416         cellname = tp + MAXCELLHOSTS * sizeof(afs_int32);
2417         scount = ((cellname[0] != '\0') ? MAXCELLHOSTS : 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 = 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 = 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 > 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 = 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 = MAXSIZE;
3202
3203         in->num_servers =
3204             (MAXSIZE - 2 * sizeof(short)) / sizeof(struct spref);
3205         in->flags = vlservers;
3206
3207         code = pioctl(0, VIOC_GETSPREFS, &blob, 1);
3208         if (code) {
3209             perror("getserverprefs pioctl");
3210             return 1;
3211         }
3212
3213         out = (struct sprefinfo *)blob.out;
3214
3215         for (i = 0; i < out->num_servers; i++) {
3216             if (resolve) {
3217                 name = hostutil_GetNameByINet(out->servers[i].server.s_addr);
3218             } else {
3219                 addr = ntohl(out->servers[i].server.s_addr);
3220                 sprintf(tbuffer, "%d.%d.%d.%d", (addr >> 24) & 0xff,
3221                         (addr >> 16) & 0xff, (addr >> 8) & 0xff, addr & 0xff);
3222                 name = tbuffer;
3223             }
3224             printf("%-50s %5u\n", name, out->servers[i].rank);
3225         }
3226
3227         in->offset = out->next_offset;
3228     } while (out->next_offset > 0);
3229
3230     return 0;
3231 }
3232
3233 static int
3234 StoreBehindCmd(struct cmd_syndesc *as, void *arock)
3235 {
3236     afs_int32 code = 0;
3237     struct ViceIoctl blob;
3238     struct cmd_item *ti;
3239     struct sbstruct tsb, tsb2;
3240     int verbose = 0;
3241     afs_int32 allfiles;
3242     char *t;
3243     int error = 0;
3244
3245     tsb.sb_thisfile = -1;
3246     ti = as->parms[0].items;    /* -kbytes */
3247     if (ti) {
3248         if (!as->parms[1].items) {
3249             fprintf(stderr, "%s: you must specify -files with -kbytes.\n",
3250                     pn);
3251             return 1;
3252         }
3253         tsb.sb_thisfile = strtol(ti->data, &t, 10) * 1024;
3254         if ((tsb.sb_thisfile < 0) || (t != ti->data + strlen(ti->data))) {
3255             fprintf(stderr, "%s: %s must be 0 or a positive number.\n", pn,
3256                     ti->data);
3257             return 1;
3258         }
3259     }
3260
3261     allfiles = tsb.sb_default = -1;     /* Don't set allfiles yet */
3262     ti = as->parms[2].items;    /* -allfiles */
3263     if (ti) {
3264         allfiles = strtol(ti->data, &t, 10) * 1024;
3265         if ((allfiles < 0) || (t != ti->data + strlen(ti->data))) {
3266             fprintf(stderr, "%s: %s must be 0 or a positive number.\n", pn,
3267                     ti->data);
3268             return 1;
3269         }
3270     }
3271
3272     /* -verbose or -file only or no options */
3273     if (as->parms[3].items || (as->parms[1].items && !as->parms[0].items)
3274         || (!as->parms[0].items && !as->parms[1].items
3275             && !as->parms[2].items))
3276         verbose = 1;
3277
3278     blob.in = (char *)&tsb;
3279     blob.out = (char *)&tsb2;
3280     blob.in_size = blob.out_size = sizeof(struct sbstruct);
3281     memset(&tsb2, 0, sizeof(tsb2));
3282
3283     /* once per -file */
3284     for (ti = as->parms[1].items; ti; ti = ti->next) {
3285         /* Do this solely to see if the file is there */
3286         code = pioctl(ti->data, VIOCWHEREIS, &blob, 1);
3287         if (code) {
3288             Die(errno, ti->data);
3289             error = 1;
3290             continue;
3291         }
3292
3293         code = pioctl(ti->data, VIOC_STORBEHIND, &blob, 1);
3294         if (code) {
3295             Die(errno, ti->data);
3296             error = 1;
3297             continue;
3298         }
3299
3300         if (verbose && (blob.out_size == sizeof(tsb2))) {
3301             if (tsb2.sb_thisfile == -1) {
3302                 fprintf(stdout, "Will store %s according to default.\n",
3303                         ti->data);
3304             } else {
3305                 fprintf(stdout,
3306                         "Will store up to %d kbytes of %s asynchronously.\n",
3307                         (tsb2.sb_thisfile / 1024), ti->data);
3308             }
3309         }
3310     }
3311
3312     /* If no files - make at least one pioctl call, or
3313      * set the allfiles default if we need to.
3314      */
3315     if (!as->parms[1].items || (allfiles != -1)) {
3316         tsb.sb_default = allfiles;
3317         code = pioctl(0, VIOC_STORBEHIND, &blob, 1);
3318         if (code) {
3319             Die(errno, ((allfiles == -1) ? 0 : "-allfiles"));
3320             error = 1;
3321         }
3322     }
3323
3324     /* Having no arguments also reports the default store asynchrony */
3325     if (verbose && (blob.out_size == sizeof(tsb2))) {
3326         fprintf(stdout, "Default store asynchrony is %d kbytes.\n",
3327                 (tsb2.sb_default / 1024));
3328     }
3329
3330     return error;
3331 }
3332
3333
3334 static int
3335 SetCryptCmd(struct cmd_syndesc *as, void *arock)
3336 {
3337     afs_int32 code = 0, flag;
3338     struct ViceIoctl blob;
3339     char *tp;
3340
3341     tp = as->parms[0].items->data;
3342     if (strcmp(tp, "on") == 0)
3343         flag = 1;
3344     else if (strcmp(tp, "off") == 0)
3345         flag = 0;
3346     else {
3347         fprintf(stderr, "%s: %s must be \"on\" or \"off\".\n", pn, tp);
3348         return EINVAL;
3349     }
3350
3351     blob.in = (char *)&flag;
3352     blob.in_size = sizeof(flag);
3353     blob.out_size = 0;
3354     code = pioctl(0, VIOC_SETRXKCRYPT, &blob, 1);
3355     if (code)
3356         Die(errno, NULL);
3357     return 0;
3358 }
3359
3360
3361 static int
3362 GetCryptCmd(struct cmd_syndesc *as, void *arock)
3363 {
3364     afs_int32 code = 0, flag;
3365     struct ViceIoctl blob;
3366     char *tp;
3367
3368     blob.in = NULL;
3369     blob.in_size = 0;
3370     blob.out_size = sizeof(flag);
3371     blob.out = space;
3372
3373     code = pioctl(0, VIOC_GETRXKCRYPT, &blob, 1);
3374
3375     if (code)
3376         Die(errno, NULL);
3377     else {
3378         tp = space;
3379         memcpy(&flag, tp, sizeof(afs_int32));
3380         printf("Security level is currently ");
3381         if (flag == 1)
3382             printf("crypt (data security).\n");
3383         else
3384             printf("clear.\n");
3385     }
3386     return 0;
3387 }
3388
3389 #ifdef AFS_DISCON_ENV
3390 static char *modenames[] = {
3391     "offline",
3392     "online",
3393     "readonly",  /* Not currently supported */
3394     "fetchonly", /* Not currently supported */
3395     "partial",   /* Not currently supported */
3396     NULL
3397 };
3398
3399 static char *policynames[] = {
3400     "client",
3401     "server",
3402     "closer",  /* Not currently supported. */
3403     "manual",  /* Not currently supported. */
3404     NULL
3405 };
3406
3407 static int
3408 DisconCmd(struct cmd_syndesc *as, void *arock)
3409 {
3410     struct cmd_item *ti;
3411     char *modename;
3412     char *policyname;
3413     int modelen, policylen;
3414     afs_int32 mode, policy, code;
3415     struct ViceIoctl blob;
3416
3417     blob.in = NULL;
3418     blob.in_size = 0;
3419
3420     space[0] = space[1] = space[2] = 0;
3421
3422     ti = as->parms[0].items;
3423     if (ti) {
3424         modename = ti->data;
3425         modelen = strlen(modename);
3426         for (mode = 0; modenames[mode] != NULL; mode++)
3427             if (!strncasecmp(modename, modenames[mode], modelen))
3428                 break;
3429         if (modenames[mode] == NULL)
3430             printf("Unknown discon mode \"%s\"\n", modename);
3431         else {
3432             space[0] = mode + 1;
3433         }
3434     }
3435     ti = as->parms[1].items;
3436     if (ti) {
3437         policyname = ti->data;
3438         policylen = strlen(policyname);
3439         for (policy = 0; policynames[policy] != NULL; policy++)
3440             if (!strncasecmp(policyname, policynames[policy], policylen))
3441                 break;
3442         if (policynames[policy] == NULL)
3443             printf("Unknown discon mode \"%s\"\n", policyname);
3444         else {
3445             space[1] = policy + 1;
3446         }
3447     }
3448
3449     if (as->parms[2].items) {
3450         space[2] = 1;
3451         printf("force on\n");
3452     }
3453
3454     blob.in = space;
3455     blob.in_size = 3 * sizeof(afs_int32);
3456
3457     blob.out_size = sizeof(mode);
3458     blob.out = space;
3459     code = pioctl(0, VIOC_DISCON, &blob, 1);
3460     if (code)
3461         Die(errno, NULL);
3462     else {
3463         memcpy(&mode, space, sizeof mode);
3464         if (mode < sizeof modenames / sizeof (char *))
3465             printf("Discon mode is now \"%s\"\n", modenames[mode]);
3466         else
3467             printf("Unknown discon mode %d\n", mode);
3468     }
3469
3470     return 0;
3471 }
3472 #endif
3473
3474 #include "AFS_component_version_number.c"
3475
3476 int
3477 main(int argc, char **argv)
3478 {
3479     afs_int32 code;
3480     struct cmd_syndesc *ts;
3481
3482 #ifdef  AFS_AIX32_ENV
3483     /*
3484      * The following signal action for AIX is necessary so that in case of a
3485      * crash (i.e. core is generated) we can include the user's data section
3486      * in the core dump. Unfortunately, by default, only a partial core is
3487      * generated which, in many cases, isn't too useful.
3488      */
3489     struct sigaction nsa;
3490
3491     sigemptyset(&nsa.sa_mask);
3492     nsa.sa_handler = SIG_DFL;
3493     nsa.sa_flags = SA_FULLDUMP;
3494     sigaction(SIGSEGV, &nsa, NULL);
3495 #endif
3496
3497     /* try to find volume location information */
3498     ts = cmd_CreateSyntax("getclientaddrs", GetClientAddrsCmd, NULL,
3499                           "get client network interface addresses");
3500     cmd_CreateAlias(ts, "gc");
3501
3502     ts = cmd_CreateSyntax("setclientaddrs", SetClientAddrsCmd, NULL,
3503                           "set client network interface addresses");
3504     cmd_AddParm(ts, "-address", CMD_LIST, CMD_OPTIONAL | CMD_EXPANDS,
3505                 "client network interfaces");
3506     cmd_CreateAlias(ts, "sc");
3507
3508     ts = cmd_CreateSyntax("setserverprefs", SetPrefCmd, NULL,
3509                           "set server ranks");
3510     cmd_AddParm(ts, "-servers", CMD_LIST, CMD_OPTIONAL | CMD_EXPANDS,
3511                 "fileserver names and ranks");
3512     cmd_AddParm(ts, "-vlservers", CMD_LIST, CMD_OPTIONAL | CMD_EXPANDS,
3513                 "VL server names and ranks");
3514     cmd_AddParm(ts, "-file", CMD_SINGLE, CMD_OPTIONAL,
3515                 "input from named file");
3516     cmd_AddParm(ts, "-stdin", CMD_FLAG, CMD_OPTIONAL, "input from stdin");
3517     cmd_CreateAlias(ts, "sp");
3518
3519     ts = cmd_CreateSyntax("getserverprefs", GetPrefCmd, NULL,
3520                           "get server ranks");
3521     cmd_AddParm(ts, "-file", CMD_SINGLE, CMD_OPTIONAL,
3522                 "output to named file");
3523     cmd_AddParm(ts, "-numeric", CMD_FLAG, CMD_OPTIONAL, "addresses only");
3524     cmd_AddParm(ts, "-vlservers", CMD_FLAG, CMD_OPTIONAL, "VL servers");
3525 /*    cmd_AddParm(ts, "-cell", CMD_FLAG, CMD_OPTIONAL, "cellname"); */
3526     cmd_CreateAlias(ts, "gp");
3527
3528     ts = cmd_CreateSyntax("setacl", SetACLCmd, NULL, "set access control list");
3529     cmd_AddParm(ts, "-dir", CMD_LIST, 0, "directory");
3530     cmd_AddParm(ts, "-acl", CMD_LIST, 0, "access list entries");
3531     cmd_AddParm(ts, "-clear", CMD_FLAG, CMD_OPTIONAL, "clear access list");
3532     cmd_AddParm(ts, "-negative", CMD_FLAG, CMD_OPTIONAL,
3533                 "apply to negative rights");
3534     parm_setacl_id = ts->nParms;
3535     cmd_AddParm(ts, "-id", CMD_FLAG, CMD_OPTIONAL,
3536                 "initial directory acl (DFS only)");
3537     cmd_AddParm(ts, "-if", CMD_FLAG, CMD_OPTIONAL,
3538                 "initial file acl (DFS only)");
3539     cmd_CreateAlias(ts, "sa");
3540
3541     ts = cmd_CreateSyntax("listacl", ListACLCmd, NULL,
3542                           "list access control list");
3543     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3544     parm_listacl_id = ts->nParms;
3545     cmd_AddParm(ts, "-id", CMD_FLAG, CMD_OPTIONAL, "initial directory acl");
3546     cmd_AddParm(ts, "-if", CMD_FLAG, CMD_OPTIONAL, "initial file acl");
3547     cmd_AddParm(ts, "-cmd", CMD_FLAG, CMD_OPTIONAL, "output as 'fs setacl' command");
3548     cmd_CreateAlias(ts, "la");
3549
3550     ts = cmd_CreateSyntax("getcalleraccess", GetCallerAccess, NULL,
3551             "list callers access");
3552     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3553     cmd_CreateAlias(ts, "gca");
3554
3555     ts = cmd_CreateSyntax("cleanacl", CleanACLCmd, NULL,
3556                           "clean up access control list");
3557     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3558
3559     ts = cmd_CreateSyntax("copyacl", CopyACLCmd, NULL,
3560                           "copy access control list");
3561     cmd_AddParm(ts, "-fromdir", CMD_SINGLE, 0,
3562                 "source directory (or DFS file)");
3563     cmd_AddParm(ts, "-todir", CMD_LIST, 0,
3564                 "destination directory (or DFS file)");
3565     cmd_AddParm(ts, "-clear", CMD_FLAG, CMD_OPTIONAL,
3566                 "first clear dest access list");
3567     parm_copyacl_id = ts->nParms;
3568     cmd_AddParm(ts, "-id", CMD_FLAG, CMD_OPTIONAL, "initial directory acl");
3569     cmd_AddParm(ts, "-if", CMD_FLAG, CMD_OPTIONAL, "initial file acl");
3570
3571     cmd_CreateAlias(ts, "ca");
3572
3573     ts = cmd_CreateSyntax("flush", FlushCmd, NULL, "flush file from cache");
3574     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3575     ts = cmd_CreateSyntax("flushmount", FlushMountCmd, NULL,
3576                           "flush mount symlink from cache");
3577     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3578
3579     ts = cmd_CreateSyntax("setvol", SetVolCmd, NULL, "set volume status");
3580     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3581     cmd_AddParm(ts, "-max", CMD_SINGLE, CMD_OPTIONAL,
3582                 "disk space quota in 1K units");
3583 #ifdef notdef
3584     cmd_AddParm(ts, "-min", CMD_SINGLE, CMD_OPTIONAL,
3585                 "disk space guaranteed");
3586     cmd_AddParm(ts, "-motd", CMD_SINGLE, CMD_OPTIONAL, "message of the day");
3587 #endif
3588     cmd_AddParm(ts, "-offlinemsg", CMD_SINGLE, CMD_OPTIONAL,
3589                 "offline message");
3590     cmd_CreateAlias(ts, "sv");
3591
3592     ts = cmd_CreateSyntax("messages", MessagesCmd, NULL,
3593                           "control Cache Manager messages");
3594     cmd_AddParm(ts, "-show", CMD_SINGLE, CMD_OPTIONAL,
3595                 "[user|console|all|none]");
3596
3597     ts = cmd_CreateSyntax("examine", ExamineCmd, NULL, "display file/volume status");
3598     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3599     cmd_CreateAlias(ts, "lv");
3600     cmd_CreateAlias(ts, "listvol");
3601
3602     ts = cmd_CreateSyntax("listquota", ListQuotaCmd, NULL, "list volume quota");
3603     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3604     cmd_CreateAlias(ts, "lq");
3605
3606     ts = cmd_CreateSyntax("diskfree", DiskFreeCmd, NULL,
3607                           "show server disk space usage");
3608     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3609     cmd_CreateAlias(ts, "df");
3610
3611     ts = cmd_CreateSyntax("quota", QuotaCmd, NULL, "show volume quota usage");
3612     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3613
3614     ts = cmd_CreateSyntax("lsmount", ListMountCmd, NULL, "list mount point");
3615     cmd_AddParm(ts, "-dir", CMD_LIST, 0, "directory");
3616
3617     ts = cmd_CreateSyntax("mkmount", MakeMountCmd, NULL, "make mount point");
3618     cmd_AddParm(ts, "-dir", CMD_SINGLE, 0, "directory");
3619     cmd_AddParm(ts, "-vol", CMD_SINGLE, 0, "volume name");
3620     cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cell name");
3621     cmd_AddParm(ts, "-rw", CMD_FLAG, CMD_OPTIONAL, "force r/w volume");
3622     cmd_AddParm(ts, "-fast", CMD_FLAG, CMD_OPTIONAL,
3623                 "don't check name with VLDB");
3624
3625 #if defined(AFS_CACHE_BYPASS)
3626         ts = cmd_CreateSyntax("bypassthreshold", BypassThresholdCmd, 0,
3627                 "get/set cache bypass file size threshold");
3628         cmd_AddParm(ts, "-size", CMD_SINGLE, CMD_OPTIONAL, "file size");
3629 #endif
3630
3631 /*
3632
3633 defect 3069
3634
3635     cmd_AddParm(ts, "-root", CMD_FLAG, CMD_OPTIONAL, "create cellular mount point");
3636 */
3637
3638
3639     ts = cmd_CreateSyntax("rmmount", RemoveMountCmd, NULL, "remove mount point");
3640     cmd_AddParm(ts, "-dir", CMD_LIST, 0, "directory");
3641
3642     ts = cmd_CreateSyntax("checkservers", CheckServersCmd, NULL,
3643                           "check local cell's servers");
3644     cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cell to check");
3645     cmd_AddParm(ts, "-all", CMD_FLAG, CMD_OPTIONAL, "check all cells");
3646     cmd_AddParm(ts, "-fast", CMD_FLAG, CMD_OPTIONAL,
3647                 "just list, don't check");
3648     cmd_AddParm(ts, "-interval", CMD_SINGLE, CMD_OPTIONAL,
3649                 "seconds between probes");
3650
3651     ts = cmd_CreateSyntax("checkvolumes", CheckVolumesCmd, NULL,
3652                           "check volumeID/name mappings");
3653     cmd_CreateAlias(ts, "checkbackups");
3654
3655
3656     ts = cmd_CreateSyntax("setcachesize", SetCacheSizeCmd, NULL,
3657                           "set cache size");
3658     cmd_AddParm(ts, "-blocks", CMD_SINGLE, CMD_OPTIONAL,
3659                 "size in 1K byte blocks (0 => reset)");
3660     cmd_CreateAlias(ts, "cachesize");
3661
3662     cmd_AddParm(ts, "-reset", CMD_FLAG, CMD_OPTIONAL,
3663                 "reset size back to boot value");
3664
3665     ts = cmd_CreateSyntax("getcacheparms", GetCacheParmsCmd, NULL,
3666                           "get cache usage info");
3667     cmd_AddParm(ts, "-files", CMD_FLAG, CMD_OPTIONAL, "Show cach files used as well");
3668     cmd_AddParm(ts, "-excessive", CMD_FLAG, CMD_OPTIONAL, "excessively verbose cache stats");
3669
3670     ts = cmd_CreateSyntax("listcells", ListCellsCmd, NULL,
3671                           "list configured cells");
3672     cmd_AddParm(ts, "-numeric", CMD_FLAG, CMD_OPTIONAL, "addresses only");
3673
3674     ts = cmd_CreateSyntax("listaliases", ListAliasesCmd, NULL,
3675                           "list configured cell aliases");
3676
3677     ts = cmd_CreateSyntax("setquota", SetQuotaCmd, NULL, "set volume quota");
3678     cmd_AddParm(ts, "-path", CMD_SINGLE, CMD_OPTIONAL, "dir/file path");
3679     cmd_AddParm(ts, "-max", CMD_SINGLE, 0, "max quota in kbytes");
3680 #ifdef notdef
3681     cmd_AddParm(ts, "-min", CMD_SINGLE, CMD_OPTIONAL, "min quota in kbytes");
3682 #endif
3683     cmd_CreateAlias(ts, "sq");
3684
3685     ts = cmd_CreateSyntax("newcell", NewCellCmd, NULL, "configure new cell");
3686     cmd_AddParm(ts, "-name", CMD_SINGLE, 0, "cell name");
3687     cmd_AddParm(ts, "-servers", CMD_LIST, CMD_REQUIRED, "primary servers");
3688     cmd_AddParm(ts, "-linkedcell", CMD_SINGLE, CMD_OPTIONAL,
3689                 "linked cell name");
3690
3691     ts = cmd_CreateSyntax("newalias", NewAliasCmd, NULL,
3692                           "configure new cell alias");
3693     cmd_AddParm(ts, "-alias", CMD_SINGLE, 0, "alias name");
3694     cmd_AddParm(ts, "-name", CMD_SINGLE, 0, "real name of cell");
3695
3696 #ifdef FS_ENABLE_SERVER_DEBUG_PORTS
3697 /*
3698  * Turn this on only if you wish to be able to talk to a server which is listening
3699  * on alternative ports. This is not intended for general use and may not be
3700  * supported in the cache manager. It is not a way to run two servers at the
3701  * same host, since the cache manager cannot properly distinguish those two hosts.
3702  */
3703     cmd_AddParm(ts, "-fsport", CMD_SINGLE, CMD_OPTIONAL,
3704                 "cell's fileserver port");
3705     cmd_AddParm(ts, "-vlport", CMD_SINGLE, CMD_OPTIONAL,
3706                 "cell's vldb server port");
3707 #endif
3708
3709     ts = cmd_CreateSyntax("whichcell", WhichCellCmd, NULL, "list file's cell");
3710     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3711
3712     ts = cmd_CreateSyntax("whereis", WhereIsCmd, NULL, "list file's location");
3713     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3714
3715     ts = cmd_CreateSyntax("wscell", WSCellCmd, NULL, "list workstation's cell");
3716
3717 /*
3718     ts = cmd_CreateSyntax("primarycell", PrimaryCellCmd, NULL, "obsolete (listed primary cell)");
3719 */
3720
3721     /* set cache monitor host address */
3722     ts = cmd_CreateSyntax("monitor", MonitorCmd, NULL, (char *)CMD_HIDDEN);
3723     cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_OPTIONAL,
3724                 "host name or 'off'");
3725     cmd_CreateAlias(ts, "mariner");
3726
3727     ts = cmd_CreateSyntax("getcellstatus", GetCellCmd, NULL, "get cell status");
3728     cmd_AddParm(ts, "-cell", CMD_LIST, 0, "cell name");
3729
3730     ts = cmd_CreateSyntax("setcell", SetCellCmd, NULL, "set cell status");
3731     cmd_AddParm(ts, "-cell", CMD_LIST, 0, "cell name");
3732     cmd_AddParm(ts, "-suid", CMD_FLAG, CMD_OPTIONAL, "allow setuid programs");
3733     cmd_AddParm(ts, "-nosuid", CMD_FLAG, CMD_OPTIONAL,
3734                 "disallow setuid programs");
3735
3736     ts = cmd_CreateSyntax("flushvolume", FlushVolumeCmd, NULL,
3737                           "flush all data in volume");
3738     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3739
3740     ts = cmd_CreateSyntax("sysname", SysNameCmd, NULL,
3741                           "get/set sysname (i.e. @sys) value");
3742     cmd_AddParm(ts, "-newsys", CMD_LIST, CMD_OPTIONAL, "new sysname");
3743
3744     ts = cmd_CreateSyntax("exportafs", ExportAfsCmd, NULL,
3745                           "enable/disable translators to AFS");
3746     cmd_AddParm(ts, "-type", CMD_SINGLE, 0, "exporter name");
3747     cmd_AddParm(ts, "-start", CMD_SINGLE, CMD_OPTIONAL,
3748                 "start/stop translator (on | off)");
3749     cmd_AddParm(ts, "-convert", CMD_SINGLE, CMD_OPTIONAL,
3750                 "convert from afs to unix mode (on | off)");
3751     cmd_AddParm(ts, "-uidcheck", CMD_SINGLE, CMD_OPTIONAL,
3752                 "run on strict 'uid check' mode (on | off)");
3753     cmd_AddParm(ts, "-submounts", CMD_SINGLE, CMD_OPTIONAL,
3754                 "allow nfs mounts to subdirs of /afs/.. (on  | off)");
3755     cmd_AddParm(ts, "-clipags", CMD_SINGLE, CMD_OPTIONAL,
3756                 "enable use of client-assigned PAG's (on  | off)");
3757     cmd_AddParm(ts, "-pagcb", CMD_SINGLE, CMD_OPTIONAL,
3758                 "enable callbacks to get creds from new clients (on  | off)");
3759
3760
3761     ts = cmd_CreateSyntax("storebehind", StoreBehindCmd, NULL,
3762                           "store to server after file close");
3763     cmd_AddParm(ts, "-kbytes", CMD_SINGLE, CMD_OPTIONAL,
3764                 "asynchrony for specified names");
3765     cmd_AddParm(ts, "-files", CMD_LIST, CMD_OPTIONAL, "specific pathnames");
3766     cmd_AddParm(ts, "-allfiles", CMD_SINGLE, CMD_OPTIONAL,
3767                 "new default (KB)");
3768     cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL, "show status");
3769     cmd_CreateAlias(ts, "sb");
3770
3771     ts = cmd_CreateSyntax("setcrypt", SetCryptCmd, NULL,
3772                           "set cache manager encryption flag");
3773     cmd_AddParm(ts, "-crypt", CMD_SINGLE, 0, "on or off");
3774
3775     ts = cmd_CreateSyntax("getcrypt", GetCryptCmd, NULL,
3776                           "get cache manager encryption flag");
3777
3778     ts = cmd_CreateSyntax("rxstatproc", RxStatProcCmd, NULL,
3779                           "Manage per process RX statistics");
3780     cmd_AddParm(ts, "-enable", CMD_FLAG, CMD_OPTIONAL, "Enable RX stats");
3781     cmd_AddParm(ts, "-disable", CMD_FLAG, CMD_OPTIONAL, "Disable RX stats");
3782     cmd_AddParm(ts, "-clear", CMD_FLAG, CMD_OPTIONAL, "Clear RX stats");
3783
3784     ts = cmd_CreateSyntax("rxstatpeer", RxStatPeerCmd, NULL,
3785                           "Manage per peer RX statistics");
3786     cmd_AddParm(ts, "-enable", CMD_FLAG, CMD_OPTIONAL, "Enable RX stats");
3787     cmd_AddParm(ts, "-disable", CMD_FLAG, CMD_OPTIONAL, "Disable RX stats");
3788     cmd_AddParm(ts, "-clear", CMD_FLAG, CMD_OPTIONAL, "Clear RX stats");
3789
3790     ts = cmd_CreateSyntax("setcbaddr", CallBackRxConnCmd, NULL, "configure callback connection address");
3791     cmd_AddParm(ts, "-addr", CMD_SINGLE, CMD_OPTIONAL, "host name or address");
3792
3793     /* try to find volume location information */
3794     ts = cmd_CreateSyntax("getfid", GetFidCmd, NULL,
3795                           "get fid for file(s)");
3796     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3797
3798 #ifdef AFS_DISCON_ENV
3799     ts = cmd_CreateSyntax("discon", DisconCmd, NULL,
3800                           "disconnection mode");
3801     cmd_AddParm(ts, "-mode", CMD_SINGLE, CMD_REQUIRED, "offline | online");
3802     cmd_AddParm(ts, "-policy", CMD_SINGLE, CMD_OPTIONAL, "client | server");
3803     cmd_AddParm(ts, "-force", CMD_FLAG, CMD_OPTIONAL, "Force reconnection, despite any synchronization issues.");
3804 #endif
3805
3806     ts = cmd_CreateSyntax("nukenfscreds", NukeNFSCredsCmd, NULL, "nuke credentials for NFS client");
3807     cmd_AddParm(ts, "-addr", CMD_SINGLE, 0, "host name or address");
3808
3809     ts = cmd_CreateSyntax("uuid", UuidCmd, NULL, "manage the UUID for the cache manager");
3810     cmd_AddParm(ts, "-generate", CMD_FLAG, CMD_REQUIRED, "generate a new UUID");
3811
3812     ts = cmd_CreateSyntax("precache", PreCacheCmd, 0,
3813                           "set precache size");
3814     cmd_AddParm(ts, "-blocks", CMD_SINGLE, CMD_OPTIONAL,
3815                 "size in 1K byte blocks (0 => disable)");
3816
3817     code = cmd_Dispatch(argc, argv);
3818     if (rxInitDone)
3819         rx_Finalize();
3820
3821     return code;
3822 }
3823
3824 static void
3825 Die(int errnum, char *filename)
3826 {
3827     switch (errnum) {
3828     case EINVAL:
3829         if (filename)
3830             fprintf(stderr,
3831                     "%s: Invalid argument; it is possible that %s is not in AFS.\n",
3832                     pn, filename);
3833         else
3834             fprintf(stderr, "%s: Invalid argument.\n", pn);
3835         break;
3836     case ENOENT:
3837         if (filename)
3838             fprintf(stderr, "%s: File '%s' doesn't exist\n", pn, filename);
3839         else
3840             fprintf(stderr, "%s: no such file returned\n", pn);
3841         break;
3842     case EROFS:
3843         fprintf(stderr,
3844                 "%s: You can not change a backup or readonly volume\n", pn);
3845         break;
3846     case EACCES:
3847     case EPERM:
3848         if (filename)
3849             fprintf(stderr,
3850                     "%s: You don't have the required access rights on '%s'\n",
3851                     pn, filename);
3852         else
3853             fprintf(stderr,
3854                     "%s: You do not have the required rights to do this operation\n",
3855                     pn);
3856         break;
3857     default:
3858         if (filename)
3859             fprintf(stderr, "%s:'%s'", pn, filename);
3860         else
3861             fprintf(stderr, "%s", pn);
3862         fprintf(stderr, ": %s\n", afs_error_message(errnum));
3863         break;
3864     }
3865 }
3866
3867 /* get clients interface addresses */
3868 static int
3869 GetClientAddrsCmd(struct cmd_syndesc *as, void *arock)
3870 {
3871     afs_int32 code;
3872     struct ViceIoctl blob;
3873     struct sprefrequest *in;
3874     struct sprefinfo *out;
3875
3876     in = (struct sprefrequest *)space;
3877     in->offset = 0;
3878
3879     do {
3880         blob.in_size = sizeof(struct sprefrequest);
3881         blob.in = (char *)in;
3882         blob.out = space;
3883         blob.out_size = MAXSIZE;
3884
3885         in->num_servers =
3886             (MAXSIZE - 2 * sizeof(short)) / sizeof(struct spref);
3887         /* returns addr in network byte order */
3888         code = pioctl(0, VIOC_GETCPREFS, &blob, 1);
3889         if (code) {
3890             perror("getClientInterfaceAddr pioctl");
3891             return 1;
3892         }
3893
3894         {
3895             int i;
3896             out = (struct sprefinfo *)blob.out;
3897             for (i = 0; i < out->num_servers; i++) {
3898                 afs_int32 addr;
3899                 char tbuffer[32];
3900                 addr = ntohl(out->servers[i].server.s_addr);
3901                 sprintf(tbuffer, "%d.%d.%d.%d", (addr >> 24) & 0xff,
3902                         (addr >> 16) & 0xff, (addr >> 8) & 0xff, addr & 0xff);
3903                 printf("%-50s\n", tbuffer);
3904             }
3905             in->offset = out->next_offset;
3906         }
3907     } while (out->next_offset > 0);
3908
3909     return 0;
3910 }
3911
3912 static int
3913 SetClientAddrsCmd(struct cmd_syndesc *as, void *arock)
3914 {
3915     afs_int32 code, addr;
3916     struct cmd_item *ti;
3917     struct ViceIoctl blob;
3918     struct setspref *ssp;
3919     int sizeUsed = 0, i, flag;
3920     afs_uint32 existingAddr[1024];      /* existing addresses on this host */
3921     int existNu;
3922     int error = 0;
3923
3924     ssp = (struct setspref *)space;
3925     ssp->num_servers = 0;
3926     blob.in = space;
3927     blob.out = space;
3928     blob.out_size = MAXSIZE;
3929
3930     if (geteuid()) {
3931         fprintf(stderr, "Permission denied: requires root access.\n");
3932         return 1;
3933     }
3934
3935     /* extract all existing interface addresses */
3936     existNu = rx_getAllAddr(existingAddr, 1024);
3937     if (existNu < 0)
3938         return 1;
3939
3940     sizeUsed = sizeof(struct setspref); /* space used in ioctl buffer */
3941     for (ti = as->parms[0].items; ti; ti = ti->next) {
3942         if (sizeUsed >= sizeof(space)) {
3943             fprintf(stderr, "No more space\n");
3944             return 1;
3945         }
3946         addr = extractAddr(ti->data, 20);       /* network order */
3947         if ((addr == AFS_IPINVALID) || (addr == AFS_IPINVALIDIGNORE)) {
3948             fprintf(stderr, "Error in specifying address: %s..ignoring\n",
3949                     ti->data);
3950             error = 1;
3951             continue;
3952         }
3953         /* see if it is an address that really exists */
3954         for (flag = 0, i = 0; i < existNu; i++)
3955             if (existingAddr[i] == addr) {
3956                 flag = 1;
3957                 break;
3958             }
3959         if (!flag) {            /* this is an nonexistent address */
3960             fprintf(stderr, "Nonexistent address: 0x%08x..ignoring\n", addr);
3961             error = 1;
3962             continue;
3963         }
3964         /* copy all specified addr into ioctl buffer */
3965         (ssp->servers[ssp->num_servers]).server.s_addr = addr;
3966         printf("Adding 0x%08x\n", addr);
3967         ssp->num_servers++;
3968         sizeUsed += sizeof(struct spref);
3969     }
3970     if (ssp->num_servers < 1) {
3971         fprintf(stderr, "No addresses specified\n");
3972         return 1;
3973     }
3974     blob.in_size = sizeUsed - sizeof(struct spref);
3975
3976     code = pioctl(0, VIOC_SETCPREFS, &blob, 1); /* network order */
3977     if (code) {
3978         Die(errno, 0);
3979         error = 1;
3980     }
3981
3982     return error;
3983 }
3984
3985 static int
3986 FlushMountCmd(struct cmd_syndesc *as, void *arock)
3987 {
3988     afs_int32 code;
3989     struct ViceIoctl blob;
3990     struct cmd_item *ti;
3991     char orig_name[1024];       /*Original name, may be modified */
3992     char true_name[1024];       /*``True'' dirname (e.g., symlink target) */
3993     char parent_dir[1024];      /*Parent directory of true name */
3994     char *last_component;       /*Last component of true name */
3995     struct stat statbuff;       /*Buffer for status info */
3996     int link_chars_read;        /*Num chars read in readlink() */
3997     int thru_symlink;           /*Did we get to a mount point via a symlink? */
3998     int error = 0;
3999
4000     for (ti = as->parms[0].items; ti; ti = ti->next) {
4001         /* once per file */
4002         thru_symlink = 0;
4003         sprintf(orig_name, "%s%s", (ti->data[0] == '/') ? "" : "./",
4004                 ti->data);
4005
4006         if (lstat(orig_name, &statbuff) < 0) {
4007             /* if lstat fails, we should still try the pioctl, since it
4008              * may work (for example, lstat will fail, but pioctl will
4009              * work if the volume of offline (returning ENODEV). */
4010             statbuff.st_mode = S_IFDIR; /* lie like pros */
4011         }
4012
4013         /*
4014          * The lstat succeeded.  If the given file is a symlink, substitute
4015          * the file name with the link name.
4016          */
4017         if ((statbuff.st_mode & S_IFMT) == S_IFLNK) {
4018             thru_symlink = 1;
4019             /*
4020              * Read name of resolved file.
4021              */
4022             link_chars_read = readlink(orig_name, true_name, 1024);
4023             if (link_chars_read <= 0) {
4024                 fprintf(stderr,
4025                         "%s: Can't read target name for '%s' symbolic link!\n",
4026                         pn, orig_name);
4027                 error = 1;
4028                 continue;
4029             }
4030
4031             /*
4032              * Add a trailing null to what was read, bump the length.
4033              */
4034             true_name[link_chars_read++] = 0;
4035
4036             /*
4037              * If the symlink is an absolute pathname, we're fine.  Otherwise, we
4038              * have to create a full pathname using the original name and the
4039              * relative symlink name.  Find the rightmost slash in the original
4040              * name (we know there is one) and splice in the symlink value.
4041              */
4042             if (true_name[0] != '/') {
4043                 last_component = (char *)strrchr(orig_name, '/');
4044                 strcpy(++last_component, true_name);
4045                 strcpy(true_name, orig_name);
4046             }
4047         } else
4048             strcpy(true_name, orig_name);
4049
4050         /*
4051          * Find rightmost slash, if any.
4052          */
4053         last_component = (char *)strrchr(true_name, '/');
4054         if (last_component == (char *)true_name) {
4055             strcpy(parent_dir, "/");
4056             last_component++;
4057         }
4058         else if (last_component != (char *)NULL) {
4059             /*
4060              * Found it.  Designate everything before it as the parent directory,
4061              * everything after it as the final component.
4062              */
4063             strncpy(parent_dir, true_name, last_component - true_name);
4064             parent_dir[last_component - true_name] = 0;
4065             last_component++;   /*Skip the slash */
4066         } else {
4067             /*
4068              * No slash appears in the given file name.  Set parent_dir to the current
4069              * directory, and the last component as the given name.
4070              */
4071             strcpy(parent_dir, ".");
4072             last_component = true_name;
4073         }
4074
4075         if (strcmp(last_component, ".") == 0
4076             || strcmp(last_component, "..") == 0) {
4077             fprintf(stderr,
4078                     "%s: you may not use '.' or '..' as the last component\n",
4079                     pn);
4080             fprintf(stderr, "%s: of a name in the 'fs flushmount' command.\n",
4081                     pn);
4082             error = 1;
4083             continue;
4084         }
4085
4086         blob.in = last_component;
4087         blob.in_size = strlen(last_component) + 1;
4088         blob.out_size = 0;
4089         memset(space, 0, MAXSIZE);
4090
4091         code = pioctl(parent_dir, VIOC_AFS_FLUSHMOUNT, &blob, 1);
4092
4093         if (code != 0) {
4094             if (errno == EINVAL) {
4095                 fprintf(stderr, "'%s' is not a mount point.\n", ti->data);
4096             } else {
4097                 Die(errno, (ti->data ? ti->data : parent_dir));
4098             }
4099             error = 1;
4100         }
4101     }
4102     return error;
4103 }
4104
4105 static int
4106 RxStatProcCmd(struct cmd_syndesc *as, void *arock)
4107 {
4108     afs_int32 code;
4109     afs_int32 flags = 0;
4110     struct ViceIoctl blob;
4111
4112     if (as->parms[0].items) {   /* -enable */
4113         flags |= AFSCALL_RXSTATS_ENABLE;
4114     }
4115     if (as->parms[1].items) {   /* -disable */
4116         flags |= AFSCALL_RXSTATS_DISABLE;
4117     }
4118     if (as->parms[2].items) {   /* -clear */
4119         flags |= AFSCALL_RXSTATS_CLEAR;
4120     }
4121     if (flags == 0) {
4122         fprintf(stderr, "You must specify at least one argument\n");
4123         return 1;
4124     }
4125
4126     blob.in = (char *)&flags;
4127     blob.in_size = sizeof(afs_int32);
4128     blob.out_size = 0;
4129
4130     code = pioctl(NULL, VIOC_RXSTAT_PROC, &blob, 1);
4131     if (code != 0) {
4132         Die(errno, NULL);
4133         return 1;
4134     }
4135
4136     return 0;
4137 }
4138
4139 static int
4140 RxStatPeerCmd(struct cmd_syndesc *as, void *arock)
4141 {
4142     afs_int32 code;
4143     afs_int32 flags = 0;
4144     struct ViceIoctl blob;
4145
4146     if (as->parms[0].items) {   /* -enable */
4147         flags |= AFSCALL_RXSTATS_ENABLE;
4148     }
4149     if (as->parms[1].items) {   /* -disable */
4150         flags |= AFSCALL_RXSTATS_DISABLE;
4151     }
4152     if (as->parms[2].items) {   /* -clear */
4153         flags |= AFSCALL_RXSTATS_CLEAR;
4154     }
4155     if (flags == 0) {
4156         fprintf(stderr, "You must specify at least one argument\n");
4157         return 1;
4158     }
4159
4160     blob.in = (char *)&flags;
4161     blob.in_size = sizeof(afs_int32);
4162     blob.out_size = 0;
4163
4164     code = pioctl(NULL, VIOC_RXSTAT_PEER, &blob, 1);
4165     if (code != 0) {
4166         Die(errno, NULL);
4167         return 1;
4168     }
4169
4170     return 0;
4171 }
4172
4173 static int
4174 GetFidCmd(struct cmd_syndesc *as, void *arock)
4175 {
4176     struct ViceIoctl blob;
4177     struct cmd_item *ti;
4178     for (ti = as->parms[0].items; ti; ti = ti->next) {
4179       struct VenusFid vfid;
4180       
4181       blob.out_size = sizeof(struct VenusFid);
4182       blob.out = (char *) &vfid;
4183       blob.in_size = 0;
4184       
4185       if (0 == pioctl(ti->data, VIOCGETFID, &blob, 1)) {
4186         printf("File %s (%u.%u.%u) contained in volume %u\n",
4187                ti->data, vfid.Fid.Volume, vfid.Fid.Vnode, vfid.Fid.Unique,
4188                vfid.Fid.Volume);
4189       }
4190     }
4191
4192     return 0;
4193 }
4194