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