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