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