callback-rxcon-move-20040602
[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 #ifdef  AFS_AIX32_ENV
29 #include <signal.h>
30 #endif
31
32 #ifdef HAVE_STRING_H
33 #include <string.h>
34 #else
35 #ifdef HAVE_STRINGS_H
36 #include <strings.h>
37 #endif
38 #endif
39
40 #undef VIRTUE
41 #undef VICE
42 #include "afs/prs_fs.h"
43 #include <afs/afsint.h>
44 #include <afs/auth.h>
45 #include <afs/cellconfig.h>
46 #include <ubik.h>
47 #include <rx/rxkad.h>
48 #include <rx/rx_globals.h>
49 #include <afs/vldbint.h>
50 #include <afs/volser.h>
51 #include <afs/vlserver.h>
52 #include <afs/cmd.h>
53 #include <afs/afsutil.h>
54 #include <stdlib.h>
55 #include <assert.h>
56 #include <afs/ptclient.h>
57
58
59 #define MAXHOSTS 13
60 #define OMAXHOSTS 8
61 #define MAXCELLHOSTS 8
62 #define MAXNAME 100
63 #define MAXSIZE 2048
64 #define MAXINSIZE 1300          /* pioctl complains if data is larger than this */
65 #define VMSGSIZE 128            /* size of msg buf in volume hdr */
66
67 static char space[MAXSIZE];
68 static char tspace[1024];
69 static struct ubik_client *uclient;
70
71 static int GetClientAddrsCmd(), SetClientAddrsCmd(), FlushMountCmd();
72 static int RxStatProcCmd(), RxStatPeerCmd();
73
74 extern char *hostutil_GetNameByINet();
75 extern struct hostent *hostutil_GetHostByName();
76
77
78 extern struct cmd_syndesc *cmd_CreateSyntax();
79 static char pn[] = "fs";
80 static int rxInitDone = 0;
81
82 static void ZapList();
83 static int PruneList();
84 static CleanAcl();
85 static SetVolCmd();
86 static GetCellName();
87 static VLDBInit();
88 static void Die();
89
90 /*
91  * Character to use between name and rights in printed representation for
92  * DFS ACL's.
93  */
94 #define DFS_SEPARATOR   ' '
95
96 typedef char sec_rgy_name_t[1025];      /* A DCE definition */
97
98 struct Acl {
99     int dfs;                    /* Originally true if a dfs acl; now also the type
100                                  * of the acl (1, 2, or 3, corresponding to object,
101                                  * initial dir, or initial object). */
102     sec_rgy_name_t cell;        /* DFS cell name */
103     int nplus;
104     int nminus;
105     struct AclEntry *pluslist;
106     struct AclEntry *minuslist;
107 };
108
109 struct AclEntry {
110     struct AclEntry *next;
111     char name[MAXNAME];
112     afs_int32 rights;
113 };
114
115 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)
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)
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)
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)
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 FlushVolumeCmd(struct cmd_syndesc *as)
1193 {
1194     afs_int32 code;
1195     struct ViceIoctl blob;
1196     struct cmd_item *ti;
1197     int error = 0;
1198
1199     SetDotDefault(&as->parms[0].items);
1200     for (ti = as->parms[0].items; ti; ti = ti->next) {
1201         blob.in_size = blob.out_size = 0;
1202         code = pioctl(ti->data, VIOC_FLUSHVOLUME, &blob, 0);
1203         if (code) {
1204             fprintf(stderr, "Error flushing volume ");
1205             perror(ti->data);
1206             error = 1;
1207             continue;
1208         }
1209     }
1210     return error;
1211 }
1212
1213 static int
1214 FlushCmd(struct cmd_syndesc *as)
1215 {
1216     afs_int32 code;
1217     struct ViceIoctl blob;
1218     struct cmd_item *ti;
1219     int error = 0;
1220
1221     for (ti = as->parms[0].items; ti; ti = ti->next) {
1222         blob.in_size = blob.out_size = 0;
1223         code = pioctl(ti->data, VIOCFLUSH, &blob, 0);
1224         if (code) {
1225             if (errno == EMFILE) {
1226                 fprintf(stderr, "%s: Can't flush active file %s\n", pn,
1227                         ti->data);
1228             } else {
1229                 fprintf(stderr, "%s: Error flushing file ", pn);
1230                 perror(ti->data);
1231             }
1232             error = 1;
1233             continue;
1234         }
1235     }
1236     return error;
1237 }
1238
1239 /* all this command does is repackage its args and call SetVolCmd */
1240 static int
1241 SetQuotaCmd(struct cmd_syndesc *as)
1242 {
1243     struct cmd_syndesc ts;
1244
1245     /* copy useful stuff from our command slot; we may later have to reorder */
1246     memcpy(&ts, as, sizeof(ts));        /* copy whole thing */
1247     return SetVolCmd(&ts);
1248 }
1249
1250 static int
1251 SetVolCmd(struct cmd_syndesc *as)
1252 {
1253     afs_int32 code;
1254     struct ViceIoctl blob;
1255     struct cmd_item *ti;
1256     struct VolumeStatus *status;
1257     char *offmsg, *input;
1258     int error = 0;
1259
1260     SetDotDefault(&as->parms[0].items);
1261     for (ti = as->parms[0].items; ti; ti = ti->next) {
1262         /* once per file */
1263         blob.out_size = MAXSIZE;
1264         blob.in_size = sizeof(*status) + 3;     /* for the three terminating nulls */
1265         blob.out = space;
1266         blob.in = space;
1267         status = (VolumeStatus *) space;
1268         status->MinQuota = status->MaxQuota = -1;
1269         offmsg = NULL;
1270         if (as->parms[1].items) {
1271             code = util_GetInt32(as->parms[1].items->data, &status->MaxQuota);
1272             if (code) {
1273                 fprintf(stderr, "%s: bad integer specified for quota.\n", pn);
1274                 error = 1;
1275                 continue;
1276             }
1277         }
1278         if (as->parms[2].items)
1279             offmsg = as->parms[2].items->data;
1280         input = (char *)status + sizeof(*status);
1281         *(input++) = '\0';      /* never set name: this call doesn't change vldb */
1282         if (offmsg) {
1283             if (strlen(offmsg) >= VMSGSIZE) {
1284                 fprintf(stderr,
1285                         "%s: message must be shorter than %d characters\n",
1286                         pn, VMSGSIZE);
1287                 error = 1;
1288                 continue;
1289             }
1290             strcpy(input, offmsg);
1291             blob.in_size += strlen(offmsg);
1292             input += strlen(offmsg) + 1;
1293         } else
1294             *(input++) = '\0';
1295         *(input++) = '\0';      /* Pad for old style volume "motd" */
1296         code = pioctl(ti->data, VIOCSETVOLSTAT, &blob, 1);
1297         if (code) {
1298             Die(errno, ti->data);
1299             error = 1;
1300         }
1301     }
1302     return error;
1303 }
1304
1305 /*
1306  * Why is VenusFid declared in the kernel-only section of afs.h,
1307  * if it's the exported interface of the cache manager?
1308  */
1309 struct VenusFid {
1310     afs_int32 Cell;
1311     AFSFid Fid;
1312 };
1313
1314 static int
1315 ExamineCmd(struct cmd_syndesc *as)
1316 {
1317     afs_int32 code;
1318     struct ViceIoctl blob;
1319     struct cmd_item *ti;
1320     struct VolumeStatus *status;
1321     char *name, *offmsg;
1322     int error = 0;
1323
1324     SetDotDefault(&as->parms[0].items);
1325     for (ti = as->parms[0].items; ti; ti = ti->next) {
1326         struct VenusFid vfid;
1327
1328         /* once per file */
1329         blob.out_size = MAXSIZE;
1330         blob.in_size = 0;
1331         blob.out = space;
1332         code = pioctl(ti->data, VIOCGETVOLSTAT, &blob, 1);
1333         if (code) {
1334             Die(errno, ti->data);
1335             error = 1;
1336             continue;
1337         }
1338         status = (VolumeStatus *) space;
1339         name = (char *)status + sizeof(*status);
1340         offmsg = name + strlen(name) + 1;
1341
1342         blob.out_size = sizeof(struct VenusFid);
1343         blob.out = (char *) &vfid;
1344         if (0 == pioctl(ti->data, VIOCGETFID, &blob, 1)) {
1345             printf("File %s (%u.%u.%u) contained in volume %u\n",
1346                    ti->data, vfid.Fid.Volume, vfid.Fid.Vnode, vfid.Fid.Unique,
1347                    vfid.Fid.Volume);
1348         }
1349
1350         PrintStatus(status, name, offmsg);
1351     }
1352     return error;
1353 }
1354
1355 static int
1356 ListQuotaCmd(struct cmd_syndesc *as)
1357 {
1358     afs_int32 code;
1359     struct ViceIoctl blob;
1360     struct cmd_item *ti;
1361     struct VolumeStatus *status;
1362     char *name;
1363     int error = 0;
1364
1365     printf("%-25s%-10s%-10s%-7s%-11s\n", "Volume Name", "     Quota",
1366            "      Used", " %Used", "  Partition");
1367     SetDotDefault(&as->parms[0].items);
1368     for (ti = as->parms[0].items; ti; ti = ti->next) {
1369         /* once per file */
1370         blob.out_size = MAXSIZE;
1371         blob.in_size = 0;
1372         blob.out = space;
1373         code = pioctl(ti->data, VIOCGETVOLSTAT, &blob, 1);
1374         if (code) {
1375             Die(errno, ti->data);
1376             error = 1;
1377             continue;
1378         }
1379         status = (VolumeStatus *) space;
1380         name = (char *)status + sizeof(*status);
1381         QuickPrintStatus(status, name);
1382     }
1383     return error;
1384 }
1385
1386 static int
1387 WhereIsCmd(struct cmd_syndesc *as)
1388 {
1389     afs_int32 code;
1390     struct ViceIoctl blob;
1391     struct cmd_item *ti;
1392     int j;
1393     afs_int32 *hosts;
1394     char *tp;
1395     int error = 0;
1396
1397     SetDotDefault(&as->parms[0].items);
1398     for (ti = as->parms[0].items; ti; ti = ti->next) {
1399         /* once per file */
1400         blob.out_size = MAXSIZE;
1401         blob.in_size = 0;
1402         blob.out = space;
1403         memset(space, 0, sizeof(space));
1404         code = pioctl(ti->data, VIOCWHEREIS, &blob, 1);
1405         if (code) {
1406             Die(errno, ti->data);
1407             error = 1;
1408             continue;
1409         }
1410         hosts = (afs_int32 *) space;
1411         printf("File %s is on host%s ", ti->data,
1412                (hosts[0] && !hosts[1]) ? "" : "s");
1413         for (j = 0; j < MAXHOSTS; j++) {
1414             if (hosts[j] == 0)
1415                 break;
1416             tp = hostutil_GetNameByINet(hosts[j]);
1417             printf("%s ", tp);
1418         }
1419         printf("\n");
1420     }
1421     return error;
1422 }
1423
1424
1425 static int
1426 DiskFreeCmd(struct cmd_syndesc *as)
1427 {
1428     afs_int32 code;
1429     struct ViceIoctl blob;
1430     struct cmd_item *ti;
1431     char *name;
1432     struct VolumeStatus *status;
1433     int error = 0;
1434
1435     printf("%-25s%-10s%-10s%-10s%-6s\n", "Volume Name", "    kbytes",
1436            "      used", "     avail", " %used");
1437     SetDotDefault(&as->parms[0].items);
1438     for (ti = as->parms[0].items; ti; ti = ti->next) {
1439         /* once per file */
1440         blob.out_size = MAXSIZE;
1441         blob.in_size = 0;
1442         blob.out = space;
1443         code = pioctl(ti->data, VIOCGETVOLSTAT, &blob, 1);
1444         if (code) {
1445             Die(errno, ti->data);
1446             error = 1;
1447             continue;
1448         }
1449         status = (VolumeStatus *) space;
1450         name = (char *)status + sizeof(*status);
1451         QuickPrintSpace(status, name);
1452     }
1453     return error;
1454 }
1455
1456 static int
1457 QuotaCmd(struct cmd_syndesc *as)
1458 {
1459     afs_int32 code;
1460     struct ViceIoctl blob;
1461     struct cmd_item *ti;
1462     double quotaPct;
1463     struct VolumeStatus *status;
1464     int error = 0;
1465
1466     SetDotDefault(&as->parms[0].items);
1467     for (ti = as->parms[0].items; ti; ti = ti->next) {
1468         /* once per file */
1469         blob.out_size = MAXSIZE;
1470         blob.in_size = 0;
1471         blob.out = space;
1472         code = pioctl(ti->data, VIOCGETVOLSTAT, &blob, 1);
1473         if (code) {
1474             Die(errno, ti->data);
1475             error = 1;
1476             continue;
1477         }
1478         status = (VolumeStatus *) space;
1479         if (status->MaxQuota)
1480             quotaPct =
1481                 ((((double)status->BlocksInUse) / status->MaxQuota) * 100.0);
1482         else
1483             quotaPct = 0.0;
1484         printf("%2.0f%% of quota used.\n", quotaPct);
1485     }
1486     return error;
1487 }
1488
1489 static int
1490 ListMountCmd(struct cmd_syndesc *as)
1491 {
1492     afs_int32 code;
1493     struct ViceIoctl blob;
1494     struct cmd_item *ti;
1495     char orig_name[1024];       /*Original name, may be modified */
1496     char true_name[1024];       /*``True'' dirname (e.g., symlink target) */
1497     char parent_dir[1024];      /*Parent directory of true name */
1498     char *last_component;       /*Last component of true name */
1499     struct stat statbuff;       /*Buffer for status info */
1500     int link_chars_read;        /*Num chars read in readlink() */
1501     int thru_symlink;           /*Did we get to a mount point via a symlink? */
1502     int error = 0;
1503
1504     for (ti = as->parms[0].items; ti; ti = ti->next) {
1505         /* once per file */
1506         thru_symlink = 0;
1507         sprintf(orig_name, "%s%s", (ti->data[0] == '/') ? "" : "./",
1508                 ti->data);
1509
1510         if (lstat(orig_name, &statbuff) < 0) {
1511             /* if lstat fails, we should still try the pioctl, since it
1512              * may work (for example, lstat will fail, but pioctl will
1513              * work if the volume of offline (returning ENODEV). */
1514             statbuff.st_mode = S_IFDIR; /* lie like pros */
1515         }
1516
1517         /*
1518          * The lstat succeeded.  If the given file is a symlink, substitute
1519          * the file name with the link name.
1520          */
1521         if ((statbuff.st_mode & S_IFMT) == S_IFLNK) {
1522             thru_symlink = 1;
1523             /*
1524              * Read name of resolved file.
1525              */
1526             link_chars_read = readlink(orig_name, true_name, 1024);
1527             if (link_chars_read <= 0) {
1528                 fprintf(stderr,
1529                         "%s: Can't read target name for '%s' symbolic link!\n",
1530                         pn, orig_name);
1531                 error = 1;
1532                 continue;
1533             }
1534
1535             /*
1536              * Add a trailing null to what was read, bump the length.
1537              */
1538             true_name[link_chars_read++] = 0;
1539
1540             /*
1541              * If the symlink is an absolute pathname, we're fine.  Otherwise, we
1542              * have to create a full pathname using the original name and the
1543              * relative symlink name.  Find the rightmost slash in the original
1544              * name (we know there is one) and splice in the symlink value.
1545              */
1546             if (true_name[0] != '/') {
1547                 last_component = (char *)strrchr(orig_name, '/');
1548                 strcpy(++last_component, true_name);
1549                 strcpy(true_name, orig_name);
1550             }
1551         } else
1552             strcpy(true_name, orig_name);
1553
1554         /*
1555          * Find rightmost slash, if any.
1556          */
1557         last_component = (char *)strrchr(true_name, '/');
1558         if (last_component) {
1559             /*
1560              * Found it.  Designate everything before it as the parent directory,
1561              * everything after it as the final component.
1562              */
1563             strncpy(parent_dir, true_name, last_component - true_name);
1564             parent_dir[last_component - true_name] = 0;
1565             last_component++;   /*Skip the slash */
1566         } else {
1567             /*
1568              * No slash appears in the given file name.  Set parent_dir to the current
1569              * directory, and the last component as the given name.
1570              */
1571             strcpy(parent_dir, ".");
1572             last_component = true_name;
1573         }
1574
1575         if (strcmp(last_component, ".") == 0
1576             || strcmp(last_component, "..") == 0) {
1577             fprintf(stderr,
1578                     "%s: you may not use '.' or '..' as the last component\n",
1579                     pn);
1580             fprintf(stderr, "%s: of a name in the 'fs lsmount' command.\n",
1581                     pn);
1582             error = 1;
1583             continue;
1584         }
1585
1586         blob.in = last_component;
1587         blob.in_size = strlen(last_component) + 1;
1588         blob.out_size = MAXSIZE;
1589         blob.out = space;
1590         memset(space, 0, MAXSIZE);
1591
1592         code = pioctl(parent_dir, VIOC_AFS_STAT_MT_PT, &blob, 1);
1593
1594         if (code == 0) {
1595             printf("'%s' is a %smount point for volume '%s'\n", ti->data,
1596                    (thru_symlink ? "symbolic link, leading to a " : ""),
1597                    space);
1598         } else {
1599             if (errno == EINVAL) {
1600                 fprintf(stderr, "'%s' is not a mount point.\n", ti->data);
1601             } else {
1602                 Die(errno, (ti->data ? ti->data : parent_dir));
1603             }
1604             error = 1;
1605         }
1606     }
1607     return error;
1608 }
1609
1610 static
1611 MakeMountCmd(struct cmd_syndesc *as)
1612 {
1613     afs_int32 code;
1614     char *cellName, *volName, *tmpName;
1615     struct afsconf_cell info;
1616     struct vldbentry vldbEntry;
1617     struct ViceIoctl blob;
1618
1619 /*
1620
1621 defect #3069
1622
1623     if (as->parms[5].items && !as->parms[2].items) {
1624         fprintf(stderr, "%s: must provide cell when creating cellular mount point.\n", pn);
1625         return 1;
1626     }
1627 */
1628
1629     if (as->parms[2].items)     /* cell name specified */
1630         cellName = as->parms[2].items->data;
1631     else
1632         cellName = NULL;
1633     volName = as->parms[1].items->data;
1634
1635     if (strlen(volName) >= 64) {
1636         fprintf(stderr,
1637                 "%s: volume name too long (length must be < 64 characters)\n",
1638                 pn);
1639         return 1;
1640     }
1641
1642     /* Check for a cellname in the volume specification, and complain
1643      * if it doesn't match what was specified with -cell */
1644     if (tmpName = strchr(volName, ':')) {
1645         *tmpName = '\0';
1646         if (cellName) {
1647             if (strcasecmp(cellName, volName)) {
1648                 fprintf(stderr, "%s: cellnames do not match.\n", pn);
1649                 return 1;
1650             }
1651         }
1652         cellName = volName;
1653         volName = ++tmpName;
1654     }
1655
1656     if (!InAFS(Parent(as->parms[0].items->data))) {
1657         fprintf(stderr,
1658                 "%s: mount points must be created within the AFS file system\n",
1659                 pn);
1660         return 1;
1661     }
1662
1663     if (!cellName) {
1664         blob.in_size = 0;
1665         blob.out_size = MAXSIZE;
1666         blob.out = space;
1667         code =
1668             pioctl(Parent(as->parms[0].items->data), VIOC_FILE_CELL_NAME,
1669                    &blob, 1);
1670     }
1671
1672     code = GetCellName(cellName ? cellName : space, &info);
1673     if (code) {
1674         return 1;
1675     }
1676     if (!(as->parms[4].items)) {
1677         /* not fast, check which cell the mountpoint is being created in */
1678         /* not fast, check name with VLDB */
1679         code = VLDBInit(1, &info);
1680         if (code == 0) {
1681             /* make the check.  Don't complain if there are problems with init */
1682             code =
1683                 ubik_Call(VL_GetEntryByNameO, uclient, 0, volName,
1684                           &vldbEntry);
1685             if (code == VL_NOENT) {
1686                 fprintf(stderr,
1687                         "%s: warning, volume %s does not exist in cell %s.\n",
1688                         pn, volName, cellName ? cellName : space);
1689             }
1690         }
1691     }
1692
1693     if (as->parms[3].items)     /* if -rw specified */
1694         strcpy(space, "%");
1695     else
1696         strcpy(space, "#");
1697     if (cellName) {
1698         /* cellular mount point, prepend cell prefix */
1699         strcat(space, info.name);
1700         strcat(space, ":");
1701     }
1702     strcat(space, volName);     /* append volume name */
1703     strcat(space, ".");         /* stupid convention; these end with a period */
1704     code = symlink(space, as->parms[0].items->data);
1705     if (code) {
1706         Die(errno, as->parms[0].items->data);
1707         return 1;
1708     }
1709     return 0;
1710 }
1711
1712 /*
1713  * Delete AFS mount points.  Variables are used as follows:
1714  *      tbuffer: Set to point to the null-terminated directory name of the mount point
1715  *          (or ``.'' if none is provided)
1716  *      tp: Set to point to the actual name of the mount point to nuke.
1717  */
1718 static int
1719 RemoveMountCmd(struct cmd_syndesc *as)
1720 {
1721     afs_int32 code = 0;
1722     struct ViceIoctl blob;
1723     struct cmd_item *ti;
1724     char tbuffer[1024];
1725     char lsbuffer[1024];
1726     char *tp;
1727     int error = 0;
1728
1729     for (ti = as->parms[0].items; ti; ti = ti->next) {
1730         /* once per file */
1731         tp = (char *)strrchr(ti->data, '/');
1732         if (tp) {
1733             strncpy(tbuffer, ti->data, code = tp - ti->data);   /* the dir name */
1734             tbuffer[code] = 0;
1735             tp++;               /* skip the slash */
1736         } else {
1737             strcpy(tbuffer, ".");
1738             tp = ti->data;
1739         }
1740         blob.in = tp;
1741         blob.in_size = strlen(tp) + 1;
1742         blob.out = lsbuffer;
1743         blob.out_size = sizeof(lsbuffer);
1744         code = pioctl(tbuffer, VIOC_AFS_STAT_MT_PT, &blob, 0);
1745         if (code) {
1746             if (errno == EINVAL) {
1747                 fprintf(stderr, "%s: '%s' is not a mount point.\n", pn,
1748                         ti->data);
1749             } else {
1750                 Die(errno, ti->data);
1751             }
1752             error = 1;
1753             continue;           /* don't bother trying */
1754         }
1755         blob.out_size = 0;
1756         blob.in = tp;
1757         blob.in_size = strlen(tp) + 1;
1758         code = pioctl(tbuffer, VIOC_AFS_DELETE_MT_PT, &blob, 0);
1759         if (code) {
1760             Die(errno, ti->data);
1761             error = 1;
1762         }
1763     }
1764     return error;
1765 }
1766
1767 /*
1768 */
1769
1770 static int
1771 CheckServersCmd(struct cmd_syndesc *as)
1772 {
1773     afs_int32 code;
1774     struct ViceIoctl blob;
1775     afs_int32 j;
1776     afs_int32 temp;
1777     char *tp;
1778     char tbuffer[128];
1779     afs_int32 interval;
1780     struct afsconf_cell info;
1781     struct chservinfo checkserv;
1782
1783     memset(&checkserv, 0, sizeof(struct chservinfo));
1784     blob.in_size = sizeof(struct chservinfo);
1785     blob.in = (caddr_t) & checkserv;
1786
1787     blob.out_size = MAXSIZE;
1788     blob.out = space;
1789     memset(space, 0, sizeof(afs_int32));        /* so we assure zero when nothing is copied back */
1790
1791     /* prepare flags for checkservers command */
1792     temp = 2;                   /* default to checking local cell only */
1793     if (as->parms[2].items)
1794         temp |= 1;              /* set fast flag */
1795     if (as->parms[1].items)
1796         temp &= ~2;             /* turn off local cell check */
1797
1798     checkserv.magic = 0x12345678;       /* XXX */
1799     checkserv.tflags = temp;
1800
1801     /* now copy in optional cell name, if specified */
1802     if (as->parms[0].items) {
1803         code = GetCellName(as->parms[0].items->data, &info);
1804         if (code) {
1805             return 1;
1806         }
1807         strcpy(checkserv.tbuffer, info.name);
1808         checkserv.tsize = strlen(info.name) + 1;
1809     } else {
1810         strcpy(checkserv.tbuffer, "\0");
1811         checkserv.tsize = 0;
1812     }
1813
1814     if (as->parms[3].items) {
1815         checkserv.tinterval = atol(as->parms[3].items->data);
1816
1817         /* sanity check */
1818         if (checkserv.tinterval < 0) {
1819             printf
1820                 ("Warning: The negative -interval is ignored; treated as an inquiry\n");
1821             checkserv.tinterval = 0;
1822         } else if (checkserv.tinterval > 600) {
1823             printf
1824                 ("Warning: The maximum -interval value is 10 mins (600 secs)\n");
1825             checkserv.tinterval = 600;  /* 10 min max interval */
1826         }
1827     } else {
1828         checkserv.tinterval = -1;       /* don't change current interval */
1829     }
1830
1831     code = pioctl(0, VIOCCKSERV, &blob, 1);
1832     if (code) {
1833         if ((errno == EACCES) && (checkserv.tinterval > 0)) {
1834             printf("Must be root to change -interval\n");
1835             return 1;
1836         }
1837         Die(errno, 0);
1838         return 1;
1839     }
1840     memcpy(&temp, space, sizeof(afs_int32));
1841     if (checkserv.tinterval >= 0) {
1842         if (checkserv.tinterval > 0)
1843             printf
1844                 ("The new down server probe interval (%d secs) is now in effect (old interval was %d secs)\n",
1845                  checkserv.tinterval, temp);
1846         else
1847             printf("The current down server probe interval is %d secs\n",
1848                    temp);
1849         return 0;
1850     }
1851     if (temp == 0) {
1852         printf("All servers are running.\n");
1853     } else {
1854         printf
1855             ("These servers unavailable due to network or server problems: ");
1856         for (j = 0;; j++) {
1857             memcpy(&temp, space + j * sizeof(afs_int32), sizeof(afs_int32));
1858             if (temp == 0)
1859                 break;
1860             tp = hostutil_GetNameByINet(temp);
1861             printf(" %s", tp);
1862         }
1863         printf(".\n");
1864         code = 1;               /* XXX */
1865     }
1866     return code;
1867 }
1868
1869 static int
1870 MessagesCmd(struct cmd_syndesc *as)
1871 {
1872     afs_int32 code = 0;
1873     struct ViceIoctl blob;
1874     afs_int32 j;
1875     afs_int32 temp;
1876     struct gaginfo gagflags;
1877     struct cmd_item *show;
1878
1879     memset(&gagflags, 0, sizeof(struct gaginfo));
1880     blob.in_size = sizeof(struct gaginfo);
1881     blob.in = (caddr_t) & gagflags;
1882     blob.out_size = MAXSIZE;
1883     blob.out = space;
1884     memset(space, 0, sizeof(afs_int32));        /* so we assure zero when nothing is copied back */
1885
1886     if (show = as->parms[0].items) {
1887         if (!strcasecmp(show->data, "user"))
1888             gagflags.showflags |= GAGUSER;
1889         else if (!strcasecmp(show->data, "console"))
1890             gagflags.showflags |= GAGCONSOLE;
1891         else if (!strcasecmp(show->data, "all"))
1892             gagflags.showflags |= GAGCONSOLE | GAGUSER;
1893         else if (!strcasecmp(show->data, "none"))
1894             /* do nothing */ ;
1895         else {
1896             fprintf(stderr,
1897                     "unrecognized flag %s: must be in {user,console,all,none}\n",
1898                     show->data);
1899             code = EINVAL;
1900         }
1901     }
1902
1903     if (code)
1904         return 1;
1905
1906     code = pioctl(0, VIOC_GAG, &blob, 1);
1907     if (code) {
1908         Die(errno, 0);
1909         return 1;
1910     }
1911
1912     return 0;
1913 }
1914
1915 static int
1916 CheckVolumesCmd(struct cmd_syndesc *as)
1917 {
1918     afs_int32 code;
1919     struct ViceIoctl blob;
1920
1921     blob.in_size = 0;
1922     blob.out_size = 0;
1923     code = pioctl(0, VIOCCKBACK, &blob, 1);
1924     if (code) {
1925         Die(errno, 0);
1926         return 1;
1927     }
1928
1929     printf("All volumeID/name mappings checked.\n");
1930     return 0;
1931 }
1932
1933 static int
1934 SetCacheSizeCmd(struct cmd_syndesc *as)
1935 {
1936     afs_int32 code;
1937     struct ViceIoctl blob;
1938     afs_int32 temp;
1939
1940     if (!as->parms[0].items && !as->parms[1].items) {
1941         fprintf(stderr, "%s: syntax error in setcachesize cmd.\n", pn);
1942         return 1;
1943     }
1944     if (as->parms[0].items) {
1945         code = util_GetInt32(as->parms[0].items->data, &temp);
1946         if (code) {
1947             fprintf(stderr, "%s: bad integer specified for cache size.\n",
1948                     pn);
1949             return 1;
1950         }
1951     } else
1952         temp = 0;
1953     blob.in = (char *)&temp;
1954     blob.in_size = sizeof(afs_int32);
1955     blob.out_size = 0;
1956     code = pioctl(0, VIOCSETCACHESIZE, &blob, 1);
1957     if (code) {
1958         if (errno == EROFS) {
1959             printf
1960                 ("'fs setcache' not allowed on memory cache based cache managers.\n");
1961         } else {
1962             Die(errno, NULL);
1963         }
1964         return 1;
1965     }
1966
1967     printf("New cache size set.\n");
1968     return 0;
1969 }
1970
1971 #define MAXGCSIZE       16
1972 static int
1973 GetCacheParmsCmd(struct cmd_syndesc *as)
1974 {
1975     afs_int32 code;
1976     struct ViceIoctl blob;
1977     afs_int32 parms[MAXGCSIZE];
1978
1979     memset(parms, '\0', sizeof parms);  /* avoid Purify UMR error */
1980     blob.in = NULL;
1981     blob.in_size = 0;
1982     blob.out_size = sizeof(parms);
1983     blob.out = (char *)parms;
1984     code = pioctl(0, VIOCGETCACHEPARMS, &blob, 1);
1985     if (code) {
1986         Die(errno, NULL);
1987         return 1;
1988     }
1989     printf("AFS using %d of the cache's available %d 1K byte blocks.\n",
1990            parms[1], parms[0]);
1991     if (parms[1] > parms[0])
1992         printf
1993             ("[Cache guideline temporarily deliberately exceeded; it will be adjusted down but you may wish to increase the cache size.]\n");
1994     return 0;
1995 }
1996
1997 static int
1998 ListCellsCmd(struct cmd_syndesc *as)
1999 {
2000     afs_int32 code;
2001     afs_int32 i, j;
2002     char *tcp, *tp;
2003     afs_int32 clear;
2004     struct ViceIoctl blob;
2005     int resolve;
2006
2007     resolve = !(as->parms[0].items);    /* -numeric */
2008
2009     for (i = 0;; i++) {
2010         tp = space;
2011         memcpy(tp, &i, sizeof(afs_int32));
2012         blob.out_size = MAXSIZE;
2013         blob.in_size = sizeof(afs_int32);
2014         blob.in = space;
2015         blob.out = space;
2016         code = pioctl(0, VIOCGETCELL, &blob, 1);
2017         if (code < 0) {
2018             if (errno == EDOM)
2019                 break;          /* done with the list */
2020             Die(errno, 0);
2021             return 1;
2022         }
2023         tp = space;
2024         printf("Cell %s on hosts", tp + MAXCELLHOSTS * sizeof(afs_int32));
2025         for (j = 0; j < MAXCELLHOSTS; j++) {
2026             afs_int32 addr;
2027             char *name, tbuffer[20];
2028
2029             memcpy(&addr, tp + j * sizeof(afs_int32), sizeof(afs_int32));
2030             if (addr == 0)
2031                 break;
2032
2033             if (resolve) {
2034                 name = hostutil_GetNameByINet(addr);
2035             } else {
2036                 addr = ntohl(addr);
2037                 sprintf(tbuffer, "%d.%d.%d.%d", (addr >> 24) & 0xff,
2038                         (addr >> 16) & 0xff, (addr >> 8) & 0xff, addr & 0xff);
2039                 name = tbuffer;
2040             }
2041             printf(" %s", name);
2042         }
2043         printf(".\n");
2044     }
2045     return 0;
2046 }
2047
2048 static int
2049 ListAliasesCmd(struct cmd_syndesc *as)
2050 {
2051     afs_int32 code, i;
2052     char *tp, *aliasName, *realName;
2053     struct ViceIoctl blob;
2054
2055     for (i = 0;; i++) {
2056         tp = space;
2057         memcpy(tp, &i, sizeof(afs_int32));
2058         blob.out_size = MAXSIZE;
2059         blob.in_size = sizeof(afs_int32);
2060         blob.in = space;
2061         blob.out = space;
2062         code = pioctl(0, VIOC_GETALIAS, &blob, 1);
2063         if (code < 0) {
2064             if (errno == EDOM)
2065                 break;          /* done with the list */
2066             Die(errno, 0);
2067             return 1;
2068         }
2069         tp = space;
2070         aliasName = tp;
2071         tp += strlen(aliasName) + 1;
2072         realName = tp;
2073         printf("Alias %s for cell %s\n", aliasName, realName);
2074     }
2075     return 0;
2076 }
2077
2078 static int
2079 CallBackRxConnCmd(struct cmd_syndesc *as)
2080 {
2081     afs_int32 code;
2082     struct ViceIoctl blob;
2083     struct cmd_item *ti;
2084     afs_int32 hostAddr;
2085     struct hostent *thp;
2086     char *tp;
2087     int setp;
2088     
2089     ti = as->parms[0].items;
2090     setp = 1;
2091     if (ti) {
2092         thp = hostutil_GetHostByName(ti->data);
2093         if (!thp) {
2094             fprintf(stderr, "host %s not found in host table.\n", ti->data);
2095             return 1;
2096         }
2097         else memcpy(&hostAddr, thp->h_addr, sizeof(afs_int32));
2098     } else {
2099         hostAddr = 0;   /* means don't set host */
2100         setp = 0;       /* aren't setting host */
2101     }
2102     
2103     /* now do operation */
2104     blob.in_size = sizeof(afs_int32);
2105     blob.out_size = sizeof(afs_int32);
2106     blob.in = (char *) &hostAddr;
2107     blob.out = (char *) &hostAddr;
2108     
2109     code = pioctl(0, VIOC_CBADDR, &blob, 1);
2110     if (code < 0) {
2111         Die(errno, 0);
2112         return 1;
2113     }
2114     return 0;
2115 }
2116
2117 static int
2118 NewCellCmd(struct cmd_syndesc *as)
2119 {
2120     afs_int32 code, linkedstate = 0, size = 0, *lp;
2121     struct ViceIoctl blob;
2122     struct cmd_item *ti;
2123     char *tp, *cellname = 0;
2124     struct hostent *thp;
2125     afs_int32 fsport = 0, vlport = 0;
2126     afs_int32 magic, scount;    /* Number of servers to pass in pioctl call */
2127
2128     /* Yuck!
2129      * With the NEWCELL pioctl call, 3.4 clients take an array of
2130      * MAXHOSTS (13) servers while 3.5 clients take an array of
2131      * MAXCELLHOSTS (8) servers. To determine which we are talking to,
2132      * do a GETCELL pioctl and pass it a magic number. If an array of
2133      * 8 comes back, its a 3.5 client. If not, its a 3.4 client.
2134      * If we get back EDOM, there are no cells in the kernel yet,
2135      * and we'll assume a 3.5 client.
2136      */
2137     tp = space;
2138     lp = (afs_int32 *) tp;
2139     *lp++ = 0;                  /* first cell entry */
2140     *lp = 0x12345678;           /* magic */
2141     blob.out_size = MAXSIZE;
2142     blob.in_size = sizeof(afs_int32) + sizeof(afs_int32);
2143     blob.in = space;
2144     blob.out = space;
2145     code = pioctl(0, VIOCGETCELL, &blob, 1);
2146     if (code < 0 && errno != EDOM) {
2147         Die(errno, 0);
2148         return 1;
2149     }
2150     if (code < 1 && errno == EDOM) {
2151         scount = MAXHOSTS;
2152     } else {
2153         tp = space;
2154         cellname = tp + MAXCELLHOSTS * sizeof(afs_int32);
2155         scount = ((cellname[0] != '\0') ? MAXCELLHOSTS : MAXHOSTS);
2156     }
2157
2158     /* Now setup and do the NEWCELL pioctl call */
2159     memset(space, 0, (scount + 1) * sizeof(afs_int32));
2160     tp = space;
2161     lp = (afs_int32 *) tp;
2162     *lp++ = 0x12345678;
2163     tp += sizeof(afs_int32);
2164     for (ti = as->parms[1].items; ti; ti = ti->next) {
2165         thp = hostutil_GetHostByName(ti->data);
2166         if (!thp) {
2167             fprintf(stderr,
2168                     "%s: Host %s not found in host table, skipping it.\n", pn,
2169                     ti->data);
2170         } else {
2171             memcpy(tp, thp->h_addr, sizeof(afs_int32));
2172             tp += sizeof(afs_int32);
2173         }
2174     }
2175     if (as->parms[2].items) {
2176         /*
2177          * Link the cell, for the purposes of volume location, to the specified
2178          * cell.
2179          */
2180         cellname = as->parms[2].items->data;
2181         linkedstate = 1;
2182     }
2183 #ifdef FS_ENABLE_SERVER_DEBUG_PORTS
2184     if (as->parms[3].items) {
2185         code = util_GetInt32(as->parms[3].items->data, &vlport);
2186         if (code) {
2187             fprintf(stderr,
2188                     "%s: bad integer specified for the fileserver port.\n",
2189                     pn);
2190             return 1;
2191         }
2192     }
2193     if (as->parms[4].items) {
2194         code = util_GetInt32(as->parms[4].items->data, &fsport);
2195         if (code) {
2196             fprintf(stderr,
2197                     "%s: bad integer specified for the vldb server port.\n",
2198                     pn);
2199             return 1;
2200         }
2201     }
2202 #endif
2203     tp = (char *)(space + (scount + 1) * sizeof(afs_int32));
2204     lp = (afs_int32 *) tp;
2205     *lp++ = fsport;
2206     *lp++ = vlport;
2207     *lp = linkedstate;
2208     strcpy(space + ((scount + 4) * sizeof(afs_int32)),
2209            as->parms[0].items->data);
2210     size = ((scount + 4) * sizeof(afs_int32))
2211         + strlen(as->parms[0].items->data)
2212         + 1 /* for null */ ;
2213     tp = (char *)(space + size);
2214     if (linkedstate) {
2215         strcpy(tp, cellname);
2216         size += strlen(cellname) + 1;
2217     }
2218     blob.in_size = size;
2219     blob.in = space;
2220     blob.out_size = 0;
2221     code = pioctl(0, VIOCNEWCELL, &blob, 1);
2222     if (code < 0) {
2223         Die(errno, 0);
2224         return 1;
2225     }
2226     return 0;
2227 }
2228
2229 static int
2230 NewAliasCmd(struct cmd_syndesc *as)
2231 {
2232     afs_int32 code;
2233     struct ViceIoctl blob;
2234     char *tp;
2235     char *aliasName, *realName;
2236
2237     /* Now setup and do the NEWCELL pioctl call */
2238     aliasName = as->parms[0].items->data;
2239     realName = as->parms[1].items->data;
2240     tp = space;
2241     strcpy(tp, aliasName);
2242     tp += strlen(aliasName) + 1;
2243     strcpy(tp, realName);
2244     tp += strlen(realName) + 1;
2245
2246     blob.in_size = tp - space;
2247     blob.in = space;
2248     blob.out_size = 0;
2249     blob.out = space;
2250     code = pioctl(0, VIOC_NEWALIAS, &blob, 1);
2251     if (code < 0) {
2252         if (errno == EEXIST) {
2253             fprintf(stderr,
2254                     "%s: cell name `%s' in use by an existing cell.\n", pn,
2255                     aliasName);
2256         } else {
2257             Die(errno, 0);
2258         }
2259         return 1;
2260     }
2261     return 0;
2262 }
2263
2264 static int
2265 WhichCellCmd(struct cmd_syndesc *as)
2266 {
2267     afs_int32 code;
2268     struct cmd_item *ti;
2269     int error = 0;
2270     char cell[MAXCELLCHARS];
2271
2272     SetDotDefault(&as->parms[0].items);
2273     for (ti = as->parms[0].items; ti; ti = ti->next) {
2274         code = GetCell(ti->data, cell);
2275         if (code) {
2276             if (errno == ENOENT)
2277                 fprintf(stderr, "%s: no such cell as '%s'\n", pn, ti->data);
2278             else
2279                 Die(errno, ti->data);
2280             error = 1;
2281             continue;
2282         }
2283
2284         printf("File %s lives in cell '%s'\n", ti->data, cell);
2285     }
2286     return error;
2287 }
2288
2289 static int
2290 WSCellCmd(struct cmd_syndesc *as)
2291 {
2292     afs_int32 code;
2293     struct ViceIoctl blob;
2294
2295     blob.in_size = 0;
2296     blob.in = NULL;
2297     blob.out_size = MAXSIZE;
2298     blob.out = space;
2299
2300     code = pioctl(NULL, VIOC_GET_WS_CELL, &blob, 1);
2301     if (code) {
2302         Die(errno, NULL);
2303         return 1;
2304     }
2305
2306     printf("This workstation belongs to cell '%s'\n", space);
2307     return 0;
2308 }
2309
2310 /*
2311 static PrimaryCellCmd(as)
2312     struct cmd_syndesc *as;
2313 {
2314     fprintf(stderr, "This command is obsolete, as is the concept of a primary token.\n");
2315     return 0;
2316 }
2317 */
2318
2319 static int
2320 MonitorCmd(struct cmd_syndesc *as)
2321 {
2322     afs_int32 code;
2323     struct ViceIoctl blob;
2324     struct cmd_item *ti;
2325     afs_int32 hostAddr;
2326     struct hostent *thp;
2327     char *tp;
2328     int setp;
2329
2330     ti = as->parms[0].items;
2331     setp = 1;
2332     if (ti) {
2333         /* set the host */
2334         if (!strcmp(ti->data, "off"))
2335             hostAddr = 0xffffffff;
2336         else {
2337             thp = hostutil_GetHostByName(ti->data);
2338             if (!thp) {
2339                 if (!strcmp(ti->data, "localhost")) {
2340                     fprintf(stderr,
2341                             "localhost not in host table, assuming 127.0.0.1\n");
2342                     hostAddr = htonl(0x7f000001);
2343                 } else {
2344                     fprintf(stderr, "host %s not found in host table.\n",
2345                             ti->data);
2346                     return 1;
2347                 }
2348             } else
2349                 memcpy(&hostAddr, thp->h_addr, sizeof(afs_int32));
2350         }
2351     } else {
2352         hostAddr = 0;           /* means don't set host */
2353         setp = 0;               /* aren't setting host */
2354     }
2355
2356     /* now do operation */
2357     blob.in_size = sizeof(afs_int32);
2358     blob.out_size = sizeof(afs_int32);
2359     blob.in = (char *)&hostAddr;
2360     blob.out = (char *)&hostAddr;
2361     code = pioctl(0, VIOC_AFS_MARINER_HOST, &blob, 1);
2362     if (code) {
2363         Die(errno, 0);
2364         return 1;
2365     }
2366     if (setp) {
2367         printf("%s: new monitor host set.\n", pn);
2368     } else {
2369         /* now decode old address */
2370         if (hostAddr == 0xffffffff) {
2371             printf("Cache monitoring is currently disabled.\n");
2372         } else {
2373             tp = hostutil_GetNameByINet(hostAddr);
2374             printf("Using host %s for monitor services.\n", tp);
2375         }
2376     }
2377     return 0;
2378 }
2379
2380 static int
2381 SysNameCmd(struct cmd_syndesc *as)
2382 {
2383     afs_int32 code;
2384     struct ViceIoctl blob;
2385     struct cmd_item *ti;
2386     char *input = space;
2387     afs_int32 setp = 0;
2388
2389     ti = as->parms[0].items;
2390     blob.in = space;
2391     blob.out = space;
2392     blob.out_size = MAXSIZE;
2393     blob.in_size = sizeof(afs_int32);
2394     input += sizeof(afs_int32);
2395     for (; ti; ti = ti->next) {
2396         setp++;
2397         blob.in_size += strlen(ti->data) + 1;
2398         if (blob.in_size > MAXSIZE) {
2399             fprintf(stderr, "%s: sysname%s too long.\n", pn,
2400                     setp > 1 ? "s" : "");
2401             return 1;
2402         }
2403         strcpy(input, ti->data);
2404         input += strlen(ti->data);
2405         *(input++) = '\0';
2406     }
2407     memcpy(space, &setp, sizeof(afs_int32));
2408     code = pioctl(0, VIOC_AFS_SYSNAME, &blob, 1);
2409     if (code) {
2410         Die(errno, 0);
2411         return 1;
2412     }
2413     if (setp) {
2414         printf("%s: new sysname%s set.\n", pn, setp > 1 ? " list" : "");
2415         return 0;
2416     }
2417     input = space;
2418     memcpy(&setp, input, sizeof(afs_int32));
2419     input += sizeof(afs_int32);
2420     if (!setp) {
2421         fprintf(stderr, "No sysname name value was found\n");
2422         return 1;
2423     }
2424     printf("Current sysname%s is", setp > 1 ? " list" : "");
2425     for (; setp > 0; --setp) {
2426         printf(" \'%s\'", input);
2427         input += strlen(input) + 1;
2428     }
2429     printf("\n");
2430     return 0;
2431 }
2432
2433 static char *exported_types[] = { "null", "nfs", "" };
2434 static int
2435 ExportAfsCmd(struct cmd_syndesc *as)
2436 {
2437     afs_int32 code;
2438     struct ViceIoctl blob;
2439     struct cmd_item *ti;
2440     int export = 0, type = 0, mode = 0, exp = 0, exportcall, pwsync =
2441         0, smounts = 0;
2442
2443     ti = as->parms[0].items;
2444     if (strcmp(ti->data, "nfs") == 0)
2445         type = 0x71;            /* NFS */
2446     else {
2447         fprintf(stderr,
2448                 "Invalid exporter type, '%s', Only the 'nfs' exporter is currently supported\n",
2449                 ti->data);
2450         return 1;
2451     }
2452     ti = as->parms[1].items;
2453     if (ti) {
2454         if (strcmp(ti->data, "on") == 0)
2455             export = 3;
2456         else if (strcmp(ti->data, "off") == 0)
2457             export = 2;
2458         else {
2459             fprintf(stderr, "Illegal argument %s\n", ti->data);
2460             return 1;
2461         }
2462         exp = 1;
2463     }
2464     if (ti = as->parms[2].items) {      /* -noconvert */
2465         if (strcmp(ti->data, "on") == 0)
2466             mode = 2;
2467         else if (strcmp(ti->data, "off") == 0)
2468             mode = 3;
2469         else {
2470             fprintf(stderr, "Illegal argument %s\n", ti->data);
2471             return 1;
2472         }
2473     }
2474     if (ti = as->parms[3].items) {      /* -uidcheck */
2475         if (strcmp(ti->data, "on") == 0)
2476             pwsync = 3;
2477         else if (strcmp(ti->data, "off") == 0)
2478             pwsync = 2;
2479         else {
2480             fprintf(stderr, "Illegal argument %s\n", ti->data);
2481             return 1;
2482         }
2483     }
2484     if (ti = as->parms[4].items) {      /* -submounts */
2485         if (strcmp(ti->data, "on") == 0)
2486             smounts = 3;
2487         else if (strcmp(ti->data, "off") == 0)
2488             smounts = 2;
2489         else {
2490             fprintf(stderr, "Illegal argument %s\n", ti->data);
2491             return 1;
2492         }
2493     }
2494     exportcall =
2495         (type << 24) | (mode << 6) | (pwsync << 4) | (smounts << 2) | export;
2496     type &= ~0x70;
2497     /* make the call */
2498     blob.in = (char *)&exportcall;
2499     blob.in_size = sizeof(afs_int32);
2500     blob.out = (char *)&exportcall;
2501     blob.out_size = sizeof(afs_int32);
2502     code = pioctl(0, VIOC_EXPORTAFS, &blob, 1);
2503     if (code) {
2504         if (errno == ENODEV) {
2505             fprintf(stderr,
2506                     "Sorry, the %s-exporter type is currently not supported on this AFS client\n",
2507                     exported_types[type]);
2508         } else {
2509             Die(errno, 0);
2510         }
2511         return 1;
2512     }
2513
2514     if (exportcall & 1) {
2515         printf("'%s' translator is enabled with the following options:\n",
2516                exported_types[type]);
2517         printf("\tRunning in %s mode\n",
2518                (exportcall & 2 ? "strict unix" :
2519                 "convert owner mode bits to world/other"));
2520         printf("\tRunning in %s mode\n",
2521                (exportcall & 4 ? "strict 'passwd sync'" :
2522                 "no 'passwd sync'"));
2523         printf("\t%s\n",
2524                (exportcall & 8 ? "Allow mounts of /afs/.. subdirs" :
2525                 "Only mounts to /afs allowed"));
2526     } else {
2527         printf("'%s' translator is disabled\n", exported_types[type]);
2528     }
2529     return 0;
2530 }
2531
2532
2533 static int
2534 GetCellCmd(struct cmd_syndesc *as)
2535 {
2536     afs_int32 code;
2537     struct ViceIoctl blob;
2538     struct afsconf_cell info;
2539     struct cmd_item *ti;
2540     struct a {
2541         afs_int32 stat;
2542         afs_int32 junk;
2543     } args;
2544     int error = 0;
2545
2546     memset(&args, '\0', sizeof args);   /* avoid Purify UMR error */
2547     for (ti = as->parms[0].items; ti; ti = ti->next) {
2548         /* once per cell */
2549         blob.out_size = sizeof(args);
2550         blob.out = (caddr_t) & args;
2551         code = GetCellName(ti->data, &info);
2552         if (code) {
2553             error = 1;
2554             continue;
2555         }
2556         blob.in_size = 1 + strlen(info.name);
2557         blob.in = info.name;
2558         code = pioctl(0, VIOC_GETCELLSTATUS, &blob, 1);
2559         if (code) {
2560             if (errno == ENOENT)
2561                 fprintf(stderr, "%s: the cell named '%s' does not exist\n",
2562                         pn, info.name);
2563             else
2564                 Die(errno, info.name);
2565             error = 1;
2566             continue;
2567         }
2568         printf("Cell %s status: ", info.name);
2569 #ifdef notdef
2570         if (args.stat & 1)
2571             printf("primary ");
2572 #endif
2573         if (args.stat & 2)
2574             printf("no setuid allowed");
2575         else
2576             printf("setuid allowed");
2577         if (args.stat & 4)
2578             printf(", using old VLDB");
2579         printf("\n");
2580     }
2581     return error;
2582 }
2583
2584 static int
2585 SetCellCmd(struct cmd_syndesc *as)
2586 {
2587     afs_int32 code;
2588     struct ViceIoctl blob;
2589     struct afsconf_cell info;
2590     struct cmd_item *ti;
2591     struct a {
2592         afs_int32 stat;
2593         afs_int32 junk;
2594         char cname[64];
2595     } args;
2596     int error = 0;
2597
2598     /* Check arguments. */
2599     if (as->parms[1].items && as->parms[2].items) {
2600         fprintf(stderr, "Cannot specify both -suid and -nosuid.\n");
2601         return 1;
2602     }
2603
2604     /* figure stuff to set */
2605     args.stat = 0;
2606     args.junk = 0;
2607
2608     if (!as->parms[1].items)
2609         args.stat |= 2;         /* default to -nosuid */
2610
2611     /* set stat for all listed cells */
2612     for (ti = as->parms[0].items; ti; ti = ti->next) {
2613         /* once per cell */
2614         code = GetCellName(ti->data, &info);
2615         if (code) {
2616             error = 1;
2617             continue;
2618         }
2619         strcpy(args.cname, info.name);
2620         blob.in_size = sizeof(args);
2621         blob.in = (caddr_t) & args;
2622         blob.out_size = 0;
2623         blob.out = (caddr_t) 0;
2624         code = pioctl(0, VIOC_SETCELLSTATUS, &blob, 1);
2625         if (code) {
2626             Die(errno, info.name);      /* XXX added cell name to Die() call */
2627             error = 1;
2628         }
2629     }
2630     return error;
2631 }
2632
2633 static int
2634 GetCellName(char *cellName, struct afsconf_cell *info)
2635 {
2636     struct afsconf_dir *tdir;
2637     int code;
2638
2639     tdir = afsconf_Open(AFSDIR_CLIENT_ETC_DIRPATH);
2640     if (!tdir) {
2641         fprintf(stderr,
2642                 "Could not process files in configuration directory (%s).\n",
2643                 AFSDIR_CLIENT_ETC_DIRPATH);
2644         return -1;
2645     }
2646
2647     code = afsconf_GetCellInfo(tdir, cellName, AFSCONF_VLDBSERVICE, info);
2648     if (code) {
2649         fprintf(stderr, "%s: cell %s not in %s\n", pn, cellName,
2650                 AFSDIR_CLIENT_CELLSERVDB_FILEPATH);
2651         return code;
2652     }
2653
2654     return 0;
2655 }
2656
2657
2658 static int
2659 VLDBInit(int noAuthFlag, struct afsconf_cell *info)
2660 {
2661     afs_int32 code;
2662     struct ktc_principal sname;
2663     struct ktc_token ttoken;
2664     afs_int32 scIndex;
2665     struct rx_securityClass *sc;
2666     struct rx_connection *serverconns[VLDB_MAXSERVERS];
2667     afs_int32 i;
2668
2669     code = rx_Init(0);
2670     if (code) {
2671         fprintf(stderr, "%s: could not initialize rx.\n", pn);
2672         return code;
2673     }
2674     rxInitDone = 1;
2675     rx_SetRxDeadTime(50);
2676     if (!noAuthFlag) {          /* we don't need tickets for null */
2677         strcpy(sname.cell, info->name);
2678         sname.instance[0] = 0;
2679         strcpy(sname.name, "afs");
2680         code = ktc_GetToken(&sname, &ttoken, sizeof(ttoken), NULL);
2681         if (code) {
2682             fprintf(stderr,
2683                     "%s: Could not get afs tokens, running unauthenticated.\n",
2684                     pn);
2685             scIndex = 0;
2686         } else {
2687             /* got a ticket */
2688             if (ttoken.kvno >= 0 && ttoken.kvno <= 255)
2689                 scIndex = 2;    /* kerberos */
2690             else {
2691                 fprintf(stderr, "%s: funny kvno (%d) in ticket, proceeding\n",
2692                         pn, ttoken.kvno);
2693                 scIndex = 2;
2694             }
2695         }
2696     } else
2697         scIndex = 0;            /* don't authenticate */
2698     switch (scIndex) {
2699     case 0:
2700         sc = rxnull_NewClientSecurityObject();
2701         break;
2702
2703     case 1:
2704         break;
2705     case 2:
2706         sc = (struct rx_securityClass *)
2707             rxkad_NewClientSecurityObject(rxkad_clear, &ttoken.sessionKey,
2708                                           ttoken.kvno, ttoken.ticketLen,
2709                                           ttoken.ticket);
2710         break;
2711     }
2712     if (info->numServers > VLDB_MAXSERVERS) {
2713         fprintf(stderr, "%s: info.numServers=%d (> VLDB_MAXSERVERS=%d)\n", pn,
2714                 info->numServers, VLDB_MAXSERVERS);
2715         exit(1);
2716     }
2717     memset(serverconns, 0, sizeof(serverconns));
2718     for (i = 0; i < info->numServers; i++)
2719         serverconns[i] =
2720             rx_NewConnection(info->hostAddr[i].sin_addr.s_addr,
2721                              info->hostAddr[i].sin_port, USER_SERVICE_ID, sc,
2722                              scIndex);
2723
2724     code = ubik_ClientInit(serverconns, &uclient);
2725
2726     if (code) {
2727         fprintf(stderr, "%s: ubik client init failed.\n", pn);
2728         return code;
2729     }
2730     return 0;
2731 }
2732
2733 static struct ViceIoctl gblob;
2734 static int debug = 0;
2735 /*
2736  * here follow some routines in suport of the setserverprefs and
2737  * getserverprefs commands.  They are:
2738  * SetPrefCmd  "top-level" routine
2739  * addServer   adds a server to the list of servers to be poked into the
2740  *             kernel.  Will poke the list into the kernel if it threatens
2741  *             to get too large.
2742  * pokeServers pokes the existing list of servers and ranks into the kernel
2743  * GetPrefCmd  reads the Cache Manager's current list of server ranks
2744  */
2745
2746 /*
2747  * returns -1 if error message printed,
2748  * 0 on success,
2749  * errno value if error and no error message printed
2750  */
2751 static int
2752 pokeServers(void)
2753 {
2754     int code;
2755
2756     code = pioctl(0, VIOC_SETSPREFS, &gblob, 1);
2757     if (code && (errno == EINVAL)) {
2758         struct setspref *ssp;
2759         ssp = (struct setspref *)gblob.in;
2760         if (!(ssp->flags & DBservers)) {
2761             gblob.in = (void *)&(ssp->servers[0]);
2762             gblob.in_size -= ((char *)&(ssp->servers[0])) - (char *)ssp;
2763             code = pioctl(0, VIOC_SETSPREFS33, &gblob, 1);
2764             return code ? errno : 0;
2765         }
2766         fprintf(stderr,
2767                 "This cache manager does not support VL server preferences.\n");
2768         return -1;
2769     }
2770
2771     return code ? errno : 0;
2772 }
2773
2774 /*
2775  * returns -1 if error message printed,
2776  * 0 on success,
2777  * errno value if error and no error message printed
2778  */
2779 static int
2780 addServer(char *name, afs_int32 rank)
2781 {
2782     int t, code;
2783     struct setspref *ssp;
2784     struct spref *sp;
2785     struct hostent *thostent;
2786     afs_uint32 addr;
2787     int error = 0;
2788
2789 #ifndef MAXUSHORT
2790 #ifdef MAXSHORT
2791 #define MAXUSHORT ((unsigned short) 2*MAXSHORT+1)       /* assumes two's complement binary system */
2792 #else
2793 #define MAXUSHORT ((unsigned short) ~0)
2794 #endif
2795 #endif
2796
2797     thostent = hostutil_GetHostByName(name);
2798     if (!thostent) {
2799         fprintf(stderr, "%s: couldn't resolve name.\n", name);
2800         return -1;
2801     }
2802
2803     ssp = (struct setspref *)(gblob.in);
2804
2805     for (t = 0; thostent->h_addr_list[t]; t++) {
2806         if (gblob.in_size > MAXINSIZE - sizeof(struct spref)) {
2807             code = pokeServers();
2808             if (code)
2809                 error = code;
2810             ssp->num_servers = 0;
2811         }
2812
2813         sp = (struct spref *)(gblob.in + gblob.in_size);
2814         memcpy(&(sp->server.s_addr), thostent->h_addr_list[t],
2815                sizeof(afs_uint32));
2816         sp->rank = (rank > MAXUSHORT ? MAXUSHORT : rank);
2817         gblob.in_size += sizeof(struct spref);
2818         ssp->num_servers++;
2819
2820         if (debug)
2821             fprintf(stderr, "adding server %s, rank %d, ip addr 0x%lx\n",
2822                     name, sp->rank, sp->server.s_addr);
2823     }
2824
2825     return error;
2826 }
2827
2828
2829 static int
2830 SetPrefCmd(struct cmd_syndesc *as)
2831 {
2832     FILE *infd;
2833     afs_int32 code;
2834     struct cmd_item *ti;
2835     char name[80];
2836     afs_int32 rank;
2837     struct setspref *ssp;
2838     int error = 0;              /* -1 means error message printed,
2839                                  * >0 means errno value for unprinted message */
2840
2841     ssp = (struct setspref *)space;
2842     ssp->flags = 0;
2843     ssp->num_servers = 0;
2844     gblob.in_size = ((char *)&(ssp->servers[0])) - (char *)ssp;
2845     gblob.in = space;
2846     gblob.out = space;
2847     gblob.out_size = MAXSIZE;
2848
2849
2850     if (geteuid()) {
2851         fprintf(stderr, "Permission denied: requires root access.\n");
2852         return 1;
2853     }
2854
2855     ti = as->parms[2].items;    /* -file */
2856     if (ti) {
2857         if (debug)
2858             fprintf(stderr, "opening file %s\n", ti->data);
2859         if (!(infd = fopen(ti->data, "r"))) {
2860             perror(ti->data);
2861             error = -1;
2862         } else {
2863             while (fscanf(infd, "%79s%ld", name, &rank) != EOF) {
2864                 code = addServer(name, (unsigned short)rank);
2865                 if (code)
2866                     error = code;
2867             }
2868         }
2869     }
2870
2871     ti = as->parms[3].items;    /* -stdin */
2872     if (ti) {
2873         while (scanf("%79s%ld", name, &rank) != EOF) {
2874             code = addServer(name, (unsigned short)rank);
2875             if (code)
2876                 error = code;
2877         }
2878     }
2879
2880     for (ti = as->parms[0].items; ti; ti = ti->next) {  /* list of servers, ranks */
2881         if (ti) {
2882             if (!ti->next) {
2883                 break;
2884             }
2885             code = addServer(ti->data, (unsigned short)atol(ti->next->data));
2886             if (code)
2887                 error = code;
2888             if (debug)
2889                 printf("set fs prefs %s %s\n", ti->data, ti->next->data);
2890             ti = ti->next;
2891         }
2892     }
2893     code = pokeServers();
2894     if (code)
2895         error = code;
2896     if (debug)
2897         printf("now working on vlservers, code=%d\n", code);
2898
2899     ssp = (struct setspref *)space;
2900     ssp->flags = DBservers;
2901     ssp->num_servers = 0;
2902     gblob.in_size = ((char *)&(ssp->servers[0])) - (char *)ssp;
2903     gblob.in = space;
2904
2905     for (ti = as->parms[1].items; ti; ti = ti->next) {  /* list of dbservers, ranks */
2906         if (ti) {
2907             if (!ti->next) {
2908                 break;
2909             }
2910             code = addServer(ti->data, (unsigned short)atol(ti->next->data));
2911             if (code)
2912                 error = code;
2913             if (debug)
2914                 printf("set vl prefs %s %s\n", ti->data, ti->next->data);
2915             ti = ti->next;
2916         }
2917     }
2918
2919     if (as->parms[1].items) {
2920         if (debug)
2921             printf("now poking vlservers\n");
2922         code = pokeServers();
2923         if (code)
2924             error = code;
2925     }
2926
2927     if (error > 0)
2928         Die(error, 0);
2929
2930     return error ? 1 : 0;
2931 }
2932
2933
2934
2935 static int
2936 GetPrefCmd(struct cmd_syndesc *as)
2937 {
2938     afs_int32 code;
2939     struct cmd_item *ti;
2940     char *name, tbuffer[20];
2941     afs_int32 rank, addr;
2942     FILE *outfd;
2943     int resolve;
2944     int vlservers = 0;
2945     struct ViceIoctl blob;
2946     struct sprefrequest *in;
2947     struct sprefinfo *out;
2948     int i;
2949
2950     ti = as->parms[0].items;    /* -file */
2951     if (ti) {
2952         if (debug)
2953             fprintf(stderr, "opening file %s\n", ti->data);
2954         if (!(outfd = freopen(ti->data, "w", stdout))) {
2955             perror(ti->data);
2956             return 1;
2957         }
2958     }
2959
2960     ti = as->parms[1].items;    /* -numeric */
2961     resolve = !(ti);
2962     ti = as->parms[2].items;    /* -vlservers */
2963     vlservers |= (ti ? DBservers : 0);
2964     /*  ti = as->parms[3].items;   -cell */
2965
2966     in = (struct sprefrequest *)space;
2967     in->offset = 0;
2968
2969     do {
2970         blob.in_size = sizeof(struct sprefrequest);
2971         blob.in = (char *)in;
2972         blob.out = space;
2973         blob.out_size = MAXSIZE;
2974
2975         in->num_servers =
2976             (MAXSIZE - 2 * sizeof(short)) / sizeof(struct spref);
2977         in->flags = vlservers;
2978
2979         code = pioctl(0, VIOC_GETSPREFS, &blob, 1);
2980         if (code) {
2981             perror("getserverprefs pioctl");
2982             return 1;
2983         }
2984
2985         out = (struct sprefinfo *)blob.out;
2986
2987         for (i = 0; i < out->num_servers; i++) {
2988             if (resolve) {
2989                 name = hostutil_GetNameByINet(out->servers[i].server.s_addr);
2990             } else {
2991                 addr = ntohl(out->servers[i].server.s_addr);
2992                 sprintf(tbuffer, "%d.%d.%d.%d", (addr >> 24) & 0xff,
2993                         (addr >> 16) & 0xff, (addr >> 8) & 0xff, addr & 0xff);
2994                 name = tbuffer;
2995             }
2996             printf("%-50s %5u\n", name, out->servers[i].rank);
2997         }
2998
2999         in->offset = out->next_offset;
3000     } while (out->next_offset > 0);
3001
3002     return 0;
3003 }
3004
3005 static int
3006 StoreBehindCmd(struct cmd_syndesc *as)
3007 {
3008     afs_int32 code = 0;
3009     struct ViceIoctl blob;
3010     struct cmd_item *ti;
3011     struct sbstruct tsb, tsb2;
3012     int verbose = 0;
3013     afs_int32 allfiles;
3014     char *t;
3015     int error = 0;
3016
3017     tsb.sb_thisfile = -1;
3018     ti = as->parms[0].items;    /* -kbytes */
3019     if (ti) {
3020         if (!as->parms[1].items) {
3021             fprintf(stderr, "%s: you must specify -files with -kbytes.\n",
3022                     pn);
3023             return 1;
3024         }
3025         tsb.sb_thisfile = strtol(ti->data, &t, 10) * 1024;
3026         if ((tsb.sb_thisfile < 0) || (t != ti->data + strlen(ti->data))) {
3027             fprintf(stderr, "%s: %s must be 0 or a positive number.\n", pn,
3028                     ti->data);
3029             return 1;
3030         }
3031     }
3032
3033     allfiles = tsb.sb_default = -1;     /* Don't set allfiles yet */
3034     ti = as->parms[2].items;    /* -allfiles */
3035     if (ti) {
3036         allfiles = strtol(ti->data, &t, 10) * 1024;
3037         if ((allfiles < 0) || (t != ti->data + strlen(ti->data))) {
3038             fprintf(stderr, "%s: %s must be 0 or a positive number.\n", pn,
3039                     ti->data);
3040             return 1;
3041         }
3042     }
3043
3044     /* -verbose or -file only or no options */
3045     if (as->parms[3].items || (as->parms[1].items && !as->parms[0].items)
3046         || (!as->parms[0].items && !as->parms[1].items
3047             && !as->parms[2].items))
3048         verbose = 1;
3049
3050     blob.in = (char *)&tsb;
3051     blob.out = (char *)&tsb2;
3052     blob.in_size = blob.out_size = sizeof(struct sbstruct);
3053     memset(&tsb2, 0, sizeof(tsb2));
3054
3055     /* once per -file */
3056     for (ti = as->parms[1].items; ti; ti = ti->next) {
3057         /* Do this solely to see if the file is there */
3058         code = pioctl(ti->data, VIOCWHEREIS, &blob, 1);
3059         if (code) {
3060             Die(errno, ti->data);
3061             error = 1;
3062             continue;
3063         }
3064
3065         code = pioctl(ti->data, VIOC_STORBEHIND, &blob, 1);
3066         if (code) {
3067             Die(errno, ti->data);
3068             error = 1;
3069             continue;
3070         }
3071
3072         if (verbose && (blob.out_size == sizeof(tsb2))) {
3073             if (tsb2.sb_thisfile == -1) {
3074                 fprintf(stdout, "Will store %s according to default.\n",
3075                         ti->data);
3076             } else {
3077                 fprintf(stdout,
3078                         "Will store up to %d kbytes of %s asynchronously.\n",
3079                         (tsb2.sb_thisfile / 1024), ti->data);
3080             }
3081         }
3082     }
3083
3084     /* If no files - make at least one pioctl call, or
3085      * set the allfiles default if we need to.
3086      */
3087     if (!as->parms[1].items || (allfiles != -1)) {
3088         tsb.sb_default = allfiles;
3089         code = pioctl(0, VIOC_STORBEHIND, &blob, 1);
3090         if (code) {
3091             Die(errno, ((allfiles == -1) ? 0 : "-allfiles"));
3092             error = 1;
3093         }
3094     }
3095
3096     /* Having no arguments also reports the default store asynchrony */
3097     if (verbose && (blob.out_size == sizeof(tsb2))) {
3098         fprintf(stdout, "Default store asynchrony is %d kbytes.\n",
3099                 (tsb2.sb_default / 1024));
3100     }
3101
3102     return error;
3103 }
3104
3105
3106 static afs_int32
3107 SetCryptCmd(struct cmd_syndesc *as)
3108 {
3109     afs_int32 code = 0, flag;
3110     struct ViceIoctl blob;
3111     char *tp;
3112
3113     tp = as->parms[0].items->data;
3114     if (strcmp(tp, "on") == 0)
3115         flag = 1;
3116     else if (strcmp(tp, "off") == 0)
3117         flag = 0;
3118     else {
3119         fprintf(stderr, "%s: %s must be \"on\" or \"off\".\n", pn, tp);
3120         return EINVAL;
3121     }
3122
3123     blob.in = (char *)&flag;
3124     blob.in_size = sizeof(flag);
3125     blob.out_size = 0;
3126     code = pioctl(0, VIOC_SETRXKCRYPT, &blob, 1);
3127     if (code)
3128         Die(errno, NULL);
3129     return 0;
3130 }
3131
3132
3133 static afs_int32
3134 GetCryptCmd(struct cmd_syndesc *as)
3135 {
3136     afs_int32 code = 0, flag;
3137     struct ViceIoctl blob;
3138     char *tp;
3139
3140     blob.in = NULL;
3141     blob.in_size = 0;
3142     blob.out_size = sizeof(flag);
3143     blob.out = space;
3144
3145     code = pioctl(0, VIOC_GETRXKCRYPT, &blob, 1);
3146
3147     if (code)
3148         Die(errno, NULL);
3149     else {
3150         tp = space;
3151         memcpy(&flag, tp, sizeof(afs_int32));
3152         printf("Security level is currently ");
3153         if (flag == 1)
3154             printf("crypt (data security).\n");
3155         else
3156             printf("clear.\n");
3157     }
3158     return 0;
3159 }
3160
3161 #include "AFS_component_version_number.c"
3162
3163 int
3164 main(int argc, char **argv)
3165 {
3166     afs_int32 code;
3167     struct cmd_syndesc *ts;
3168
3169 #ifdef  AFS_AIX32_ENV
3170     /*
3171      * The following signal action for AIX is necessary so that in case of a
3172      * crash (i.e. core is generated) we can include the user's data section
3173      * in the core dump. Unfortunately, by default, only a partial core is
3174      * generated which, in many cases, isn't too useful.
3175      */
3176     struct sigaction nsa;
3177
3178     sigemptyset(&nsa.sa_mask);
3179     nsa.sa_handler = SIG_DFL;
3180     nsa.sa_flags = SA_FULLDUMP;
3181     sigaction(SIGSEGV, &nsa, NULL);
3182 #endif
3183
3184     /* try to find volume location information */
3185     ts = cmd_CreateSyntax("getclientaddrs", GetClientAddrsCmd, 0,
3186                           "get client network interface addresses");
3187     cmd_CreateAlias(ts, "gc");
3188
3189     ts = cmd_CreateSyntax("setclientaddrs", SetClientAddrsCmd, 0,
3190                           "set client network interface addresses");
3191     cmd_AddParm(ts, "-address", CMD_LIST, CMD_OPTIONAL | CMD_EXPANDS,
3192                 "client network interfaces");
3193     cmd_CreateAlias(ts, "sc");
3194
3195     ts = cmd_CreateSyntax("setserverprefs", SetPrefCmd, 0,
3196                           "set server ranks");
3197     cmd_AddParm(ts, "-servers", CMD_LIST, CMD_OPTIONAL | CMD_EXPANDS,
3198                 "fileserver names and ranks");
3199     cmd_AddParm(ts, "-vlservers", CMD_LIST, CMD_OPTIONAL | CMD_EXPANDS,
3200                 "VL server names and ranks");
3201     cmd_AddParm(ts, "-file", CMD_SINGLE, CMD_OPTIONAL,
3202                 "input from named file");
3203     cmd_AddParm(ts, "-stdin", CMD_FLAG, CMD_OPTIONAL, "input from stdin");
3204     cmd_CreateAlias(ts, "sp");
3205
3206     ts = cmd_CreateSyntax("getserverprefs", GetPrefCmd, 0,
3207                           "get server ranks");
3208     cmd_AddParm(ts, "-file", CMD_SINGLE, CMD_OPTIONAL,
3209                 "output to named file");
3210     cmd_AddParm(ts, "-numeric", CMD_FLAG, CMD_OPTIONAL, "addresses only");
3211     cmd_AddParm(ts, "-vlservers", CMD_FLAG, CMD_OPTIONAL, "VL servers");
3212 /*    cmd_AddParm(ts, "-cell", CMD_FLAG, CMD_OPTIONAL, "cellname"); */
3213     cmd_CreateAlias(ts, "gp");
3214
3215     ts = cmd_CreateSyntax("setacl", SetACLCmd, 0, "set access control list");
3216     cmd_AddParm(ts, "-dir", CMD_LIST, 0, "directory");
3217     cmd_AddParm(ts, "-acl", CMD_LIST, 0, "access list entries");
3218     cmd_AddParm(ts, "-clear", CMD_FLAG, CMD_OPTIONAL, "clear access list");
3219     cmd_AddParm(ts, "-negative", CMD_FLAG, CMD_OPTIONAL,
3220                 "apply to negative rights");
3221     parm_setacl_id = ts->nParms;
3222     cmd_AddParm(ts, "-id", CMD_FLAG, CMD_OPTIONAL,
3223                 "initial directory acl (DFS only)");
3224     cmd_AddParm(ts, "-if", CMD_FLAG, CMD_OPTIONAL,
3225                 "initial file acl (DFS only)");
3226     cmd_CreateAlias(ts, "sa");
3227
3228     ts = cmd_CreateSyntax("listacl", ListACLCmd, 0,
3229                           "list access control list");
3230     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3231     parm_listacl_id = ts->nParms;
3232     cmd_AddParm(ts, "-id", CMD_FLAG, CMD_OPTIONAL, "initial directory acl");
3233     cmd_AddParm(ts, "-if", CMD_FLAG, CMD_OPTIONAL, "initial file acl");
3234     cmd_CreateAlias(ts, "la");
3235
3236     ts = cmd_CreateSyntax("cleanacl", CleanACLCmd, 0,
3237                           "clean up access control list");
3238     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3239
3240     ts = cmd_CreateSyntax("copyacl", CopyACLCmd, 0,
3241                           "copy access control list");
3242     cmd_AddParm(ts, "-fromdir", CMD_SINGLE, 0,
3243                 "source directory (or DFS file)");
3244     cmd_AddParm(ts, "-todir", CMD_LIST, 0,
3245                 "destination directory (or DFS file)");
3246     cmd_AddParm(ts, "-clear", CMD_FLAG, CMD_OPTIONAL,
3247                 "first clear dest access list");
3248     parm_copyacl_id = ts->nParms;
3249     cmd_AddParm(ts, "-id", CMD_FLAG, CMD_OPTIONAL, "initial directory acl");
3250     cmd_AddParm(ts, "-if", CMD_FLAG, CMD_OPTIONAL, "initial file acl");
3251
3252     cmd_CreateAlias(ts, "ca");
3253
3254     ts = cmd_CreateSyntax("flush", FlushCmd, 0, "flush file from cache");
3255     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3256     ts = cmd_CreateSyntax("flushmount", FlushMountCmd, 0,
3257                           "flush mount symlink from cache");
3258     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3259
3260     ts = cmd_CreateSyntax("setvol", SetVolCmd, 0, "set volume status");
3261     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3262     cmd_AddParm(ts, "-max", CMD_SINGLE, CMD_OPTIONAL,
3263                 "disk space quota in 1K units");
3264 #ifdef notdef
3265     cmd_AddParm(ts, "-min", CMD_SINGLE, CMD_OPTIONAL,
3266                 "disk space guaranteed");
3267     cmd_AddParm(ts, "-motd", CMD_SINGLE, CMD_OPTIONAL, "message of the day");
3268 #endif
3269     cmd_AddParm(ts, "-offlinemsg", CMD_SINGLE, CMD_OPTIONAL,
3270                 "offline message");
3271     cmd_CreateAlias(ts, "sv");
3272
3273     ts = cmd_CreateSyntax("messages", MessagesCmd, 0,
3274                           "control Cache Manager messages");
3275     cmd_AddParm(ts, "-show", CMD_SINGLE, CMD_OPTIONAL,
3276                 "[user|console|all|none]");
3277
3278     ts = cmd_CreateSyntax("examine", ExamineCmd, 0, "display file/volume status");
3279     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3280     cmd_CreateAlias(ts, "lv");
3281     cmd_CreateAlias(ts, "listvol");
3282
3283     ts = cmd_CreateSyntax("listquota", ListQuotaCmd, 0, "list volume quota");
3284     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3285     cmd_CreateAlias(ts, "lq");
3286
3287     ts = cmd_CreateSyntax("diskfree", DiskFreeCmd, 0,
3288                           "show server disk space usage");
3289     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3290     cmd_CreateAlias(ts, "df");
3291
3292     ts = cmd_CreateSyntax("quota", QuotaCmd, 0, "show volume quota usage");
3293     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3294
3295     ts = cmd_CreateSyntax("lsmount", ListMountCmd, 0, "list mount point");
3296     cmd_AddParm(ts, "-dir", CMD_LIST, 0, "directory");
3297
3298     ts = cmd_CreateSyntax("mkmount", MakeMountCmd, 0, "make mount point");
3299     cmd_AddParm(ts, "-dir", CMD_SINGLE, 0, "directory");
3300     cmd_AddParm(ts, "-vol", CMD_SINGLE, 0, "volume name");
3301     cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cell name");
3302     cmd_AddParm(ts, "-rw", CMD_FLAG, CMD_OPTIONAL, "force r/w volume");
3303     cmd_AddParm(ts, "-fast", CMD_FLAG, CMD_OPTIONAL,
3304                 "don't check name with VLDB");
3305
3306 /*
3307
3308 defect 3069
3309
3310     cmd_AddParm(ts, "-root", CMD_FLAG, CMD_OPTIONAL, "create cellular mount point");
3311 */
3312
3313
3314     ts = cmd_CreateSyntax("rmmount", RemoveMountCmd, 0, "remove mount point");
3315     cmd_AddParm(ts, "-dir", CMD_LIST, 0, "directory");
3316
3317     ts = cmd_CreateSyntax("checkservers", CheckServersCmd, 0,
3318                           "check local cell's servers");
3319     cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cell to check");
3320     cmd_AddParm(ts, "-all", CMD_FLAG, CMD_OPTIONAL, "check all cells");
3321     cmd_AddParm(ts, "-fast", CMD_FLAG, CMD_OPTIONAL,
3322                 "just list, don't check");
3323     cmd_AddParm(ts, "-interval", CMD_SINGLE, CMD_OPTIONAL,
3324                 "seconds between probes");
3325
3326     ts = cmd_CreateSyntax("checkvolumes", CheckVolumesCmd, 0,
3327                           "check volumeID/name mappings");
3328     cmd_CreateAlias(ts, "checkbackups");
3329
3330
3331     ts = cmd_CreateSyntax("setcachesize", SetCacheSizeCmd, 0,
3332                           "set cache size");
3333     cmd_AddParm(ts, "-blocks", CMD_SINGLE, CMD_OPTIONAL,
3334                 "size in 1K byte blocks (0 => reset)");
3335     cmd_CreateAlias(ts, "cachesize");
3336
3337     cmd_AddParm(ts, "-reset", CMD_FLAG, CMD_OPTIONAL,
3338                 "reset size back to boot value");
3339
3340     ts = cmd_CreateSyntax("getcacheparms", GetCacheParmsCmd, 0,
3341                           "get cache usage info");
3342
3343     ts = cmd_CreateSyntax("listcells", ListCellsCmd, 0,
3344                           "list configured cells");
3345     cmd_AddParm(ts, "-numeric", CMD_FLAG, CMD_OPTIONAL, "addresses only");
3346
3347     ts = cmd_CreateSyntax("listaliases", ListAliasesCmd, 0,
3348                           "list configured cell aliases");
3349
3350     ts = cmd_CreateSyntax("setquota", SetQuotaCmd, 0, "set volume quota");
3351     cmd_AddParm(ts, "-path", CMD_SINGLE, CMD_OPTIONAL, "dir/file path");
3352     cmd_AddParm(ts, "-max", CMD_SINGLE, 0, "max quota in kbytes");
3353 #ifdef notdef
3354     cmd_AddParm(ts, "-min", CMD_SINGLE, CMD_OPTIONAL, "min quota in kbytes");
3355 #endif
3356     cmd_CreateAlias(ts, "sq");
3357
3358     ts = cmd_CreateSyntax("newcell", NewCellCmd, 0, "configure new cell");
3359     cmd_AddParm(ts, "-name", CMD_SINGLE, 0, "cell name");
3360     cmd_AddParm(ts, "-servers", CMD_LIST, CMD_REQUIRED, "primary servers");
3361     cmd_AddParm(ts, "-linkedcell", CMD_SINGLE, CMD_OPTIONAL,
3362                 "linked cell name");
3363
3364     ts = cmd_CreateSyntax("newalias", NewAliasCmd, 0,
3365                           "configure new cell alias");
3366     cmd_AddParm(ts, "-alias", CMD_SINGLE, 0, "alias name");
3367     cmd_AddParm(ts, "-name", CMD_SINGLE, 0, "real name of cell");
3368
3369 #ifdef FS_ENABLE_SERVER_DEBUG_PORTS
3370 /*
3371  * Turn this on only if you wish to be able to talk to a server which is listening
3372  * on alternative ports. This is not intended for general use and may not be
3373  * supported in the cache manager. It is not a way to run two servers at the
3374  * same host, since the cache manager cannot properly distinguish those two hosts.
3375  */
3376     cmd_AddParm(ts, "-fsport", CMD_SINGLE, CMD_OPTIONAL,
3377                 "cell's fileserver port");
3378     cmd_AddParm(ts, "-vlport", CMD_SINGLE, CMD_OPTIONAL,
3379                 "cell's vldb server port");
3380 #endif
3381
3382     ts = cmd_CreateSyntax("whichcell", WhichCellCmd, 0, "list file's cell");
3383     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3384
3385     ts = cmd_CreateSyntax("whereis", WhereIsCmd, 0, "list file's location");
3386     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3387
3388     ts = cmd_CreateSyntax("wscell", WSCellCmd, 0, "list workstation's cell");
3389
3390 /*
3391     ts = cmd_CreateSyntax("primarycell", PrimaryCellCmd, 0, "obsolete (listed primary cell)");
3392 */
3393
3394     /* set cache monitor host address */
3395     ts = cmd_CreateSyntax("monitor", MonitorCmd, 0, (char *)CMD_HIDDEN);
3396     cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_OPTIONAL,
3397                 "host name or 'off'");
3398     cmd_CreateAlias(ts, "mariner");
3399
3400     ts = cmd_CreateSyntax("getcellstatus", GetCellCmd, 0, "get cell status");
3401     cmd_AddParm(ts, "-cell", CMD_LIST, 0, "cell name");
3402
3403     ts = cmd_CreateSyntax("setcell", SetCellCmd, 0, "set cell status");
3404     cmd_AddParm(ts, "-cell", CMD_LIST, 0, "cell name");
3405     cmd_AddParm(ts, "-suid", CMD_FLAG, CMD_OPTIONAL, "allow setuid programs");
3406     cmd_AddParm(ts, "-nosuid", CMD_FLAG, CMD_OPTIONAL,
3407                 "disallow setuid programs");
3408
3409     ts = cmd_CreateSyntax("flushvolume", FlushVolumeCmd, 0,
3410                           "flush all data in volume");
3411     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3412
3413     ts = cmd_CreateSyntax("sysname", SysNameCmd, 0,
3414                           "get/set sysname (i.e. @sys) value");
3415     cmd_AddParm(ts, "-newsys", CMD_LIST, CMD_OPTIONAL, "new sysname");
3416
3417     ts = cmd_CreateSyntax("exportafs", ExportAfsCmd, 0,
3418                           "enable/disable translators to AFS");
3419     cmd_AddParm(ts, "-type", CMD_SINGLE, 0, "exporter name");
3420     cmd_AddParm(ts, "-start", CMD_SINGLE, CMD_OPTIONAL,
3421                 "start/stop translator (on | off)");
3422     cmd_AddParm(ts, "-convert", CMD_SINGLE, CMD_OPTIONAL,
3423                 "convert from afs to unix mode (on | off)");
3424     cmd_AddParm(ts, "-uidcheck", CMD_SINGLE, CMD_OPTIONAL,
3425                 "run on strict 'uid check' mode (on | off)");
3426     cmd_AddParm(ts, "-submounts", CMD_SINGLE, CMD_OPTIONAL,
3427                 "allow nfs mounts to subdirs of /afs/.. (on  | off)");
3428
3429
3430     ts = cmd_CreateSyntax("storebehind", StoreBehindCmd, 0,
3431                           "store to server after file close");
3432     cmd_AddParm(ts, "-kbytes", CMD_SINGLE, CMD_OPTIONAL,
3433                 "asynchrony for specified names");
3434     cmd_AddParm(ts, "-files", CMD_LIST, CMD_OPTIONAL, "specific pathnames");
3435     cmd_AddParm(ts, "-allfiles", CMD_SINGLE, CMD_OPTIONAL,
3436                 "new default (KB)");
3437     cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL, "show status");
3438     cmd_CreateAlias(ts, "sb");
3439
3440     ts = cmd_CreateSyntax("setcrypt", SetCryptCmd, 0,
3441                           "set cache manager encryption flag");
3442     cmd_AddParm(ts, "-crypt", CMD_SINGLE, 0, "on or off");
3443
3444     ts = cmd_CreateSyntax("getcrypt", GetCryptCmd, 0,
3445                           "set cache manager encryption flag");
3446
3447     ts = cmd_CreateSyntax("rxstatproc", RxStatProcCmd, 0,
3448                           "Manage per process RX statistics");
3449     cmd_AddParm(ts, "-enable", CMD_FLAG, CMD_OPTIONAL, "Enable RX stats");
3450     cmd_AddParm(ts, "-disable", CMD_FLAG, CMD_OPTIONAL, "Disable RX stats");
3451     cmd_AddParm(ts, "-clear", CMD_FLAG, CMD_OPTIONAL, "Clear RX stats");
3452
3453     ts = cmd_CreateSyntax("rxstatpeer", RxStatPeerCmd, 0,
3454                           "Manage per peer RX statistics");
3455     cmd_AddParm(ts, "-enable", CMD_FLAG, CMD_OPTIONAL, "Enable RX stats");
3456     cmd_AddParm(ts, "-disable", CMD_FLAG, CMD_OPTIONAL, "Disable RX stats");
3457     cmd_AddParm(ts, "-clear", CMD_FLAG, CMD_OPTIONAL, "Clear RX stats");
3458
3459     ts = cmd_CreateSyntax("setcbaddr", CallBackRxConnCmd, 0, "configure callback connection address");
3460     cmd_AddParm(ts, "-addr", CMD_SINGLE, CMD_OPTIONAL, "host name or address");
3461
3462     code = cmd_Dispatch(argc, argv);
3463     if (rxInitDone)
3464         rx_Finalize();
3465
3466     return code;
3467 }
3468
3469 static void
3470 Die(int errnum, char *filename)
3471 {
3472     switch (errnum) {
3473     case EINVAL:
3474         if (filename)
3475             fprintf(stderr,
3476                     "%s: Invalid argument; it is possible that %s is not in AFS.\n",
3477                     pn, filename);
3478         else
3479             fprintf(stderr, "%s: Invalid argument.\n", pn);
3480         break;
3481     case ENOENT:
3482         if (filename)
3483             fprintf(stderr, "%s: File '%s' doesn't exist\n", pn, filename);
3484         else
3485             fprintf(stderr, "%s: no such file returned\n", pn);
3486         break;
3487     case EROFS:
3488         fprintf(stderr,
3489                 "%s: You can not change a backup or readonly volume\n", pn);
3490         break;
3491     case EACCES:
3492     case EPERM:
3493         if (filename)
3494             fprintf(stderr,
3495                     "%s: You don't have the required access rights on '%s'\n",
3496                     pn, filename);
3497         else
3498             fprintf(stderr,
3499                     "%s: You do not have the required rights to do this operation\n",
3500                     pn);
3501         break;
3502     default:
3503         if (filename)
3504             fprintf(stderr, "%s:'%s'", pn, filename);
3505         else
3506             fprintf(stderr, "%s", pn);
3507         fprintf(stderr, ": %s\n", error_message(errnum));
3508         break;
3509     }
3510 }
3511
3512 /* get clients interface addresses */
3513 static int
3514 GetClientAddrsCmd(struct cmd_syndesc *as)
3515 {
3516     afs_int32 code;
3517     struct cmd_item *ti;
3518     char *name;
3519     struct ViceIoctl blob;
3520     struct sprefrequest *in;
3521     struct sprefinfo *out;
3522
3523     in = (struct sprefrequest *)space;
3524     in->offset = 0;
3525
3526     do {
3527         blob.in_size = sizeof(struct sprefrequest);
3528         blob.in = (char *)in;
3529         blob.out = space;
3530         blob.out_size = MAXSIZE;
3531
3532         in->num_servers =
3533             (MAXSIZE - 2 * sizeof(short)) / sizeof(struct spref);
3534         /* returns addr in network byte order */
3535         code = pioctl(0, VIOC_GETCPREFS, &blob, 1);
3536         if (code) {
3537             perror("getClientInterfaceAddr pioctl");
3538             return 1;
3539         }
3540
3541         {
3542             int i;
3543             out = (struct sprefinfo *)blob.out;
3544             for (i = 0; i < out->num_servers; i++) {
3545                 afs_int32 addr;
3546                 char tbuffer[32];
3547                 addr = ntohl(out->servers[i].server.s_addr);
3548                 sprintf(tbuffer, "%d.%d.%d.%d", (addr >> 24) & 0xff,
3549                         (addr >> 16) & 0xff, (addr >> 8) & 0xff, addr & 0xff);
3550                 printf("%-50s\n", tbuffer);
3551             }
3552             in->offset = out->next_offset;
3553         }
3554     } while (out->next_offset > 0);
3555
3556     return 0;
3557 }
3558
3559 static int
3560 SetClientAddrsCmd(struct cmd_syndesc *as)
3561 {
3562     afs_int32 code, addr;
3563     struct cmd_item *ti;
3564     char name[80];
3565     struct ViceIoctl blob;
3566     struct setspref *ssp;
3567     int sizeUsed = 0, i, flag;
3568     afs_int32 existingAddr[1024];       /* existing addresses on this host */
3569     int existNu;
3570     int error = 0;
3571
3572     ssp = (struct setspref *)space;
3573     ssp->num_servers = 0;
3574     blob.in = space;
3575     blob.out = space;
3576     blob.out_size = MAXSIZE;
3577
3578     if (geteuid()) {
3579         fprintf(stderr, "Permission denied: requires root access.\n");
3580         return 1;
3581     }
3582
3583     /* extract all existing interface addresses */
3584     existNu = rx_getAllAddr(existingAddr, 1024);
3585     if (existNu < 0)
3586         return 1;
3587
3588     sizeUsed = sizeof(struct setspref); /* space used in ioctl buffer */
3589     for (ti = as->parms[0].items; ti; ti = ti->next) {
3590         if (sizeUsed >= sizeof(space)) {
3591             fprintf(stderr, "No more space\n");
3592             return 1;
3593         }
3594         addr = extractAddr(ti->data, 20);       /* network order */
3595         if ((addr == AFS_IPINVALID) || (addr == AFS_IPINVALIDIGNORE)) {
3596             fprintf(stderr, "Error in specifying address: %s..ignoring\n",
3597                     ti->data);
3598             error = 1;
3599             continue;
3600         }
3601         /* see if it is an address that really exists */
3602         for (flag = 0, i = 0; i < existNu; i++)
3603             if (existingAddr[i] == addr) {
3604                 flag = 1;
3605                 break;
3606             }
3607         if (!flag) {            /* this is an nonexistent address */
3608             fprintf(stderr, "Nonexistent address: 0x%08x..ignoring\n", addr);
3609             error = 1;
3610             continue;
3611         }
3612         /* copy all specified addr into ioctl buffer */
3613         (ssp->servers[ssp->num_servers]).server.s_addr = addr;
3614         printf("Adding 0x%08x\n", addr);
3615         ssp->num_servers++;
3616         sizeUsed += sizeof(struct spref);
3617     }
3618     if (ssp->num_servers < 1) {
3619         fprintf(stderr, "No addresses specified\n");
3620         return 1;
3621     }
3622     blob.in_size = sizeUsed - sizeof(struct spref);
3623
3624     code = pioctl(0, VIOC_SETCPREFS, &blob, 1); /* network order */
3625     if (code) {
3626         Die(errno, 0);
3627         error = 1;
3628     }
3629
3630     return error;
3631 }
3632
3633 static int
3634 FlushMountCmd(struct cmd_syndesc *as)
3635 {
3636     afs_int32 code;
3637     struct ViceIoctl blob;
3638     struct cmd_item *ti;
3639     char orig_name[1024];       /*Original name, may be modified */
3640     char true_name[1024];       /*``True'' dirname (e.g., symlink target) */
3641     char parent_dir[1024];      /*Parent directory of true name */
3642     char *last_component;       /*Last component of true name */
3643     struct stat statbuff;       /*Buffer for status info */
3644     int link_chars_read;        /*Num chars read in readlink() */
3645     int thru_symlink;           /*Did we get to a mount point via a symlink? */
3646     int error = 0;
3647
3648     for (ti = as->parms[0].items; ti; ti = ti->next) {
3649         /* once per file */
3650         thru_symlink = 0;
3651         sprintf(orig_name, "%s%s", (ti->data[0] == '/') ? "" : "./",
3652                 ti->data);
3653
3654         if (lstat(orig_name, &statbuff) < 0) {
3655             /* if lstat fails, we should still try the pioctl, since it
3656              * may work (for example, lstat will fail, but pioctl will
3657              * work if the volume of offline (returning ENODEV). */
3658             statbuff.st_mode = S_IFDIR; /* lie like pros */
3659         }
3660
3661         /*
3662          * The lstat succeeded.  If the given file is a symlink, substitute
3663          * the file name with the link name.
3664          */
3665         if ((statbuff.st_mode & S_IFMT) == S_IFLNK) {
3666             thru_symlink = 1;
3667             /*
3668              * Read name of resolved file.
3669              */
3670             link_chars_read = readlink(orig_name, true_name, 1024);
3671             if (link_chars_read <= 0) {
3672                 fprintf(stderr,
3673                         "%s: Can't read target name for '%s' symbolic link!\n",
3674                         pn, orig_name);
3675                 error = 1;
3676                 continue;
3677             }
3678
3679             /*
3680              * Add a trailing null to what was read, bump the length.
3681              */
3682             true_name[link_chars_read++] = 0;
3683
3684             /*
3685              * If the symlink is an absolute pathname, we're fine.  Otherwise, we
3686              * have to create a full pathname using the original name and the
3687              * relative symlink name.  Find the rightmost slash in the original
3688              * name (we know there is one) and splice in the symlink value.
3689              */
3690             if (true_name[0] != '/') {
3691                 last_component = (char *)strrchr(orig_name, '/');
3692                 strcpy(++last_component, true_name);
3693                 strcpy(true_name, orig_name);
3694             }
3695         } else
3696             strcpy(true_name, orig_name);
3697
3698         /*
3699          * Find rightmost slash, if any.
3700          */
3701         last_component = (char *)strrchr(true_name, '/');
3702         if (last_component) {
3703             /*
3704              * Found it.  Designate everything before it as the parent directory,
3705              * everything after it as the final component.
3706              */
3707             strncpy(parent_dir, true_name, last_component - true_name);
3708             parent_dir[last_component - true_name] = 0;
3709             last_component++;   /*Skip the slash */
3710         } else {
3711             /*
3712              * No slash appears in the given file name.  Set parent_dir to the current
3713              * directory, and the last component as the given name.
3714              */
3715             strcpy(parent_dir, ".");
3716             last_component = true_name;
3717         }
3718
3719         if (strcmp(last_component, ".") == 0
3720             || strcmp(last_component, "..") == 0) {
3721             fprintf(stderr,
3722                     "%s: you may not use '.' or '..' as the last component\n",
3723                     pn);
3724             fprintf(stderr, "%s: of a name in the 'fs flushmount' command.\n",
3725                     pn);
3726             error = 1;
3727             continue;
3728         }
3729
3730         blob.in = last_component;
3731         blob.in_size = strlen(last_component) + 1;
3732         blob.out_size = 0;
3733         memset(space, 0, MAXSIZE);
3734
3735         code = pioctl(parent_dir, VIOC_AFS_FLUSHMOUNT, &blob, 1);
3736
3737         if (code != 0) {
3738             if (errno == EINVAL) {
3739                 fprintf(stderr, "'%s' is not a mount point.\n", ti->data);
3740             } else {
3741                 Die(errno, (ti->data ? ti->data : parent_dir));
3742             }
3743             error = 1;
3744         }
3745     }
3746     return error;
3747 }
3748
3749 static int
3750 RxStatProcCmd(struct cmd_syndesc *as)
3751 {
3752     afs_int32 code;
3753     afs_int32 flags = 0;
3754     struct ViceIoctl blob;
3755     struct cmd_item *ti;
3756
3757     if (as->parms[0].items) {   /* -enable */
3758         flags |= AFSCALL_RXSTATS_ENABLE;
3759     }
3760     if (as->parms[1].items) {   /* -disable */
3761         flags |= AFSCALL_RXSTATS_DISABLE;
3762     }
3763     if (as->parms[2].items) {   /* -clear */
3764         flags |= AFSCALL_RXSTATS_CLEAR;
3765     }
3766     if (flags == 0) {
3767         fprintf(stderr, "You must specify at least one argument\n");
3768         return 1;
3769     }
3770
3771     blob.in = (char *)&flags;
3772     blob.in_size = sizeof(afs_int32);
3773     blob.out_size = 0;
3774
3775     code = pioctl(NULL, VIOC_RXSTAT_PROC, &blob, 1);
3776     if (code != 0) {
3777         Die(errno, NULL);
3778         return 1;
3779     }
3780
3781     return 0;
3782 }
3783
3784 static int
3785 RxStatPeerCmd(struct cmd_syndesc *as)
3786 {
3787     afs_int32 code;
3788     afs_int32 flags = 0;
3789     struct ViceIoctl blob;
3790     struct cmd_item *ti;
3791
3792     if (as->parms[0].items) {   /* -enable */
3793         flags |= AFSCALL_RXSTATS_ENABLE;
3794     }
3795     if (as->parms[1].items) {   /* -disable */
3796         flags |= AFSCALL_RXSTATS_DISABLE;
3797     }
3798     if (as->parms[2].items) {   /* -clear */
3799         flags |= AFSCALL_RXSTATS_CLEAR;
3800     }
3801     if (flags == 0) {
3802         fprintf(stderr, "You must specify at least one argument\n");
3803         return 1;
3804     }
3805
3806     blob.in = (char *)&flags;
3807     blob.in_size = sizeof(afs_int32);
3808     blob.out_size = 0;
3809
3810     code = pioctl(NULL, VIOC_RXSTAT_PEER, &blob, 1);
3811     if (code != 0) {
3812         Die(errno, NULL);
3813         return 1;
3814     }
3815
3816     return 0;
3817 }
3818