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