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