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