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