sysname-list-instead-of-simple-name-20010605
[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 = 0;
2089
2090     ti = as->parms[0].items;
2091     blob.in = space;
2092     blob.out = space;
2093     blob.out_size = MAXSIZE;
2094     blob.in_size = sizeof(afs_int32);
2095     input += sizeof(afs_int32);
2096     for(; ti; ti=ti->next) {
2097         setp++;
2098         blob.in_size += strlen(ti->data) + 1;
2099         if (blob.in_size > MAXSIZE) {
2100           fprintf(stderr, "%s: sysname%s too long.\n", pn, setp > 1 ? "s" : "");
2101           return 1;
2102         }
2103         strcpy(input, ti->data);
2104         input += strlen(ti->data);
2105         *(input++) = '\0';
2106     }
2107     bcopy(&setp, space, sizeof(afs_int32));
2108     code = pioctl(0, VIOC_AFS_SYSNAME, &blob, 1);
2109     if (code) {
2110         Die(errno, 0);
2111         return 1;
2112     }
2113     if (setp) {
2114         printf("%s: new sysname%s set.\n", pn, setp > 1 ? " list" : "");
2115         return 0;
2116     }
2117     input = space;
2118     bcopy(input, &setp, sizeof(afs_int32));
2119     input += sizeof(afs_int32);
2120     if (!setp) {
2121         fprintf(stderr, "No sysname name value was found\n");
2122         return 1;
2123     }
2124     printf("Current sysname%s is:", setp>1 ? " list" : "");
2125     for(;setp>0;--setp) {
2126       printf(" %s", input);
2127       input += strlen(input) + 1;
2128     }
2129     printf("\n");
2130     return 0;
2131 }
2132
2133 static char *exported_types[] = {"null", "nfs", ""};
2134 static ExportAfsCmd(as)
2135     struct cmd_syndesc *as;
2136 {
2137     afs_int32 code;
2138     struct ViceIoctl blob;
2139     struct cmd_item *ti;
2140     int export=0, type=0, mode = 0, exp = 0, exportcall, pwsync=0, smounts=0;
2141
2142     ti = as->parms[0].items;
2143     if (strcmp(ti->data, "nfs") == 0) type = 0x71; /* NFS */
2144     else {
2145         fprintf(stderr, "Invalid exporter type, '%s', Only the 'nfs' exporter is currently supported\n", ti->data);
2146         return 1;
2147     }
2148     ti = as->parms[1].items;
2149     if (ti) {
2150         if (strcmp(ti->data, "on") == 0) export = 3;
2151         else if (strcmp(ti->data, "off") == 0) export = 2;
2152         else {
2153             fprintf(stderr, "Illegal argument %s\n", ti->data);
2154             return 1;
2155         }
2156         exp = 1;
2157     }
2158     if (ti = as->parms[2].items) {      /* -noconvert */
2159         if (strcmp(ti->data, "on") == 0) mode = 2;
2160         else if (strcmp(ti->data, "off") == 0) mode = 3;
2161         else {
2162             fprintf(stderr, "Illegal argument %s\n", ti->data);
2163             return 1;
2164         }
2165     }
2166     if (ti = as->parms[3].items) {      /* -uidcheck */
2167         if (strcmp(ti->data, "on") == 0) pwsync = 3;
2168         else if (strcmp(ti->data, "off") == 0) pwsync = 2;
2169         else {
2170             fprintf(stderr, "Illegal argument %s\n", ti->data);
2171             return 1;
2172         }
2173     }
2174     if (ti = as->parms[4].items) {      /* -submounts */
2175         if (strcmp(ti->data, "on") == 0) smounts = 3;
2176         else if (strcmp(ti->data, "off") == 0) smounts = 2;
2177         else {
2178             fprintf(stderr, "Illegal argument %s\n", ti->data);
2179             return 1;
2180         }
2181     }
2182     exportcall =  (type << 24) | (mode << 6) | (pwsync << 4) | (smounts << 2) | export;
2183     type &= ~0x70;
2184     /* make the call */
2185     blob.in = (char *) &exportcall;
2186     blob.in_size = sizeof(afs_int32);
2187     blob.out = (char *) &exportcall;
2188     blob.out_size = sizeof(afs_int32);
2189     code = pioctl(0, VIOC_EXPORTAFS, &blob, 1);
2190     if (code) {
2191         if (errno == ENODEV) {
2192             fprintf(stderr, "Sorry, the %s-exporter type is currently not supported on this AFS client\n", exported_types[type]);
2193         }
2194         else {
2195             Die(errno, 0);
2196         }
2197         return 1;
2198     }
2199
2200     if (exportcall & 1) {
2201         printf("'%s' translator is enabled with the following options:\n",
2202                exported_types[type]);
2203         printf("\tRunning in %s mode\n",
2204                (exportcall & 2 ? "strict unix"
2205                                : "convert owner mode bits to world/other"));
2206         printf("\tRunning in %s mode\n",
2207                (exportcall & 4 ? "strict 'passwd sync'"
2208                                : "no 'passwd sync'"));
2209         printf("\t%s\n",
2210                (exportcall & 8 ? "Allow mounts of /afs/.. subdirs"
2211                                : "Only mounts to /afs allowed"));
2212     }
2213     else {
2214         printf("'%s' translator is disabled\n", exported_types[type]);
2215     }
2216     return 0;
2217 }
2218
2219
2220 static GetCellCmd(as)
2221     struct cmd_syndesc *as;
2222 {
2223     afs_int32 code;
2224     struct ViceIoctl blob;
2225     struct afsconf_cell info;
2226     struct cmd_item *ti;
2227     struct a {
2228         afs_int32 stat;
2229         afs_int32 junk;
2230     } args;
2231     int error = 0;
2232
2233     memset(&args, '\0', sizeof args); /* avoid Purify UMR error */
2234     for(ti=as->parms[0].items; ti; ti=ti->next) {
2235         /* once per cell */
2236         blob.out_size = sizeof(args);
2237         blob.out = (caddr_t) &args;
2238         code = GetCellName(ti->data, &info);
2239         if (code) {
2240             error = 1;
2241             continue;
2242         }
2243         blob.in_size = 1+strlen(info.name);
2244         blob.in = info.name;
2245         code = pioctl(0, VIOC_GETCELLSTATUS, &blob, 1);
2246         if (code) {
2247             if (errno == ENOENT)
2248                 fprintf(stderr, "%s: the cell named '%s' does not exist\n", pn, info.name);
2249             else
2250                 Die(errno, info.name);
2251             error = 1;
2252             continue;
2253         }
2254         printf("Cell %s status: ", info.name);
2255 #ifdef notdef
2256         if (args.stat & 1) printf("primary ");
2257 #endif
2258         if (args.stat & 2) printf("no setuid allowed");
2259         else printf("setuid allowed");
2260         if (args.stat & 4) printf(", using old VLDB");
2261         printf("\n");
2262     }
2263     return error;
2264 }
2265
2266 static SetCellCmd(as)
2267     struct cmd_syndesc *as;
2268 {
2269     afs_int32 code;
2270     struct ViceIoctl blob;
2271     struct afsconf_cell info;
2272     struct cmd_item *ti;
2273     struct a {
2274         afs_int32 stat;
2275         afs_int32 junk;
2276         char cname[64];
2277     } args;
2278     int error = 0;
2279
2280     /* Check arguments. */
2281     if (as->parms[1].items && as->parms[2].items) {
2282         fprintf(stderr, "Cannot specify both -suid and -nosuid.\n");
2283         return 1;
2284     }
2285
2286     /* figure stuff to set */
2287     args.stat = 0;
2288     args.junk = 0;
2289
2290     if (! as->parms[1].items) args.stat |= 2; /* default to -nosuid */
2291
2292     /* set stat for all listed cells */
2293     for(ti=as->parms[0].items; ti; ti=ti->next) {
2294         /* once per cell */
2295         code = GetCellName(ti->data, &info);
2296         if (code) {
2297             error = 1;
2298             continue;
2299         }
2300         strcpy(args.cname, info.name);
2301         blob.in_size = sizeof(args);
2302         blob.in = (caddr_t) &args;
2303         blob.out_size = 0;
2304         blob.out = (caddr_t) 0;
2305         code = pioctl(0, VIOC_SETCELLSTATUS, &blob, 1);
2306         if (code) {
2307             Die(errno, info.name);      /* XXX added cell name to Die() call */
2308             error = 1;
2309         }
2310     }
2311     return error;
2312 }
2313
2314 static GetCellName(cellName, info)
2315     char *cellName;
2316     struct afsconf_cell *info;
2317 {
2318     struct afsconf_dir *tdir;
2319     int code;
2320
2321     tdir = afsconf_Open(AFSDIR_CLIENT_ETC_DIRPATH);
2322     if (!tdir) {
2323         fprintf(stderr, "Could not process files in configuration directory (%s).\n",
2324                 AFSDIR_CLIENT_ETC_DIRPATH);
2325         return -1;
2326     }
2327
2328     code = afsconf_GetCellInfo(tdir, cellName, AFSCONF_VLDBSERVICE, info);
2329     if (code) {
2330         fprintf(stderr, "%s: cell %s not in %s\n", pn, cellName,
2331                 AFSDIR_CLIENT_CELLSERVDB_FILEPATH);
2332         return code;
2333     }
2334
2335     return 0;
2336 }
2337
2338
2339 static VLDBInit(noAuthFlag, info)
2340     int noAuthFlag;
2341     struct afsconf_cell *info;
2342 {
2343     afs_int32 code;
2344     struct ktc_principal sname;
2345     struct ktc_token ttoken;
2346     afs_int32 scIndex;
2347     struct rx_securityClass *sc;
2348     struct rx_connection *serverconns[VLDB_MAXSERVERS];
2349     afs_int32 i;
2350
2351     code = rx_Init(0);
2352     if (code) {
2353         fprintf(stderr, "%s: could not initialize rx.\n", pn);
2354         return code;
2355     }
2356     rxInitDone = 1;
2357     rx_SetRxDeadTime(50);
2358     if (!noAuthFlag) {   /* we don't need tickets for null */
2359         strcpy(sname.cell, info->name);
2360         sname.instance[0] = 0;
2361         strcpy(sname.name, "afs");
2362         code = ktc_GetToken(&sname,&ttoken, sizeof(ttoken), (char *)0);
2363         if (code) {
2364             fprintf(stderr, "%s: Could not get afs tokens, running unauthenticated.\n", pn);
2365             scIndex = 0;
2366         }
2367         else {
2368             /* got a ticket */
2369             if (ttoken.kvno >= 0 && ttoken.kvno <= 255) scIndex = 2;    /* kerberos */
2370             else {
2371                 fprintf(stderr, "%s: funny kvno (%d) in ticket, proceeding\n",
2372                         pn, ttoken.kvno);
2373                 scIndex = 2;
2374             }
2375         }
2376     }
2377     else scIndex = 0;       /* don't authenticate */
2378     switch (scIndex) {
2379     case 0:
2380         sc = (struct rx_securityClass *) rxnull_NewClientSecurityObject();
2381         break;
2382
2383     case 1:
2384         break;
2385     case 2:
2386         sc = (struct rx_securityClass *)
2387             rxkad_NewClientSecurityObject(rxkad_clear, &ttoken.sessionKey,
2388                                           ttoken.kvno, ttoken.ticketLen,
2389                                           ttoken.ticket);
2390         break;
2391     }
2392     if (info->numServers > VLDB_MAXSERVERS) {
2393         fprintf(stderr, "%s: info.numServers=%d (> VLDB_MAXSERVERS=%d)\n",
2394                 pn, info->numServers, VLDB_MAXSERVERS);
2395         exit(1);
2396     }
2397     bzero(serverconns, sizeof(serverconns));
2398     for (i = 0;i<info->numServers;i++)
2399         serverconns[i] = rx_NewConnection(info->hostAddr[i].sin_addr.s_addr,
2400                                           info->hostAddr[i].sin_port, USER_SERVICE_ID,
2401                                           sc, scIndex);
2402
2403     code = ubik_ClientInit(serverconns, &uclient);
2404
2405     if (code) {
2406         fprintf(stderr, "%s: ubik client init failed.\n", pn);
2407         return code;
2408     }
2409     return 0;
2410 }
2411
2412 static struct ViceIoctl gblob;
2413 static int debug = 0;
2414 /*
2415  * here follow some routines in suport of the setserverprefs and
2416  * getserverprefs commands.  They are:
2417  * SetPrefCmd  "top-level" routine
2418  * addServer   adds a server to the list of servers to be poked into the
2419  *             kernel.  Will poke the list into the kernel if it threatens
2420  *             to get too large.
2421  * pokeServers pokes the existing list of servers and ranks into the kernel
2422  * GetPrefCmd  reads the Cache Manager's current list of server ranks
2423  */
2424
2425 /*
2426  * returns -1 if error message printed,
2427  * 0 on success,
2428  * errno value if error and no error message printed
2429  */
2430 static pokeServers()
2431 {
2432     int code;
2433
2434     code = pioctl(0, VIOC_SETSPREFS, &gblob, 1);
2435     if (code && (errno == EINVAL)) {
2436         struct setspref *ssp;
2437         ssp = (struct setspref *)gblob.in;
2438         if (!(ssp->flags & DBservers)) {
2439             gblob.in = (void *) &(ssp->servers[0]);
2440             gblob.in_size -= ((char *)&(ssp->servers[0])) - (char *)ssp;
2441             code = pioctl(0, VIOC_SETSPREFS33, &gblob, 1);
2442             return code ? errno : 0;
2443         }
2444         fprintf(stderr,
2445                 "This cache manager does not support VL server preferences.\n");
2446         return -1;
2447     }
2448
2449     return code ? errno : 0;
2450 }
2451
2452 /*
2453  * returns -1 if error message printed,
2454  * 0 on success,
2455  * errno value if error and no error message printed
2456  */
2457 static addServer(name, rank)
2458     char *name;
2459     afs_int32 rank;
2460 {
2461     int t,code;
2462     struct setspref *ssp;
2463     struct spref *sp;
2464     struct hostent *thostent;
2465     afs_uint32 addr;
2466     int error = 0;
2467
2468 #ifndef MAXUSHORT
2469 #ifdef MAXSHORT
2470 #define MAXUSHORT ((unsigned short) 2*MAXSHORT+1)  /* assumes two's complement binary system */
2471 #else
2472 #define MAXUSHORT ((unsigned short) ~0)
2473 #endif
2474 #endif
2475
2476     thostent = hostutil_GetHostByName(name);
2477     if (!thostent) {
2478         fprintf(stderr, "%s: couldn't resolve name.\n", name);
2479         return -1;
2480     }
2481
2482     ssp = (struct setspref *)(gblob.in);
2483
2484     for (t = 0; thostent->h_addr_list[t]; t++) {
2485         if (gblob.in_size > MAXINSIZE - sizeof(struct spref)) {
2486             code = pokeServers();
2487             if (code) error = code;
2488             ssp->num_servers = 0;
2489         }
2490
2491         sp = (struct spref *) (gblob.in + gblob.in_size);
2492         bcopy (thostent->h_addr_list[t], &(sp->server.s_addr), sizeof(afs_uint32));
2493         sp->rank = (rank > MAXUSHORT ? MAXUSHORT : rank);
2494         gblob.in_size += sizeof(struct spref);
2495         ssp->num_servers++;
2496
2497         if (debug)
2498             fprintf(stderr, "adding server %s, rank %d, ip addr 0x%lx\n",
2499                     name,sp->rank,sp->server.s_addr);
2500     }
2501
2502     return error;
2503 }
2504
2505
2506 static SetPrefCmd(as)
2507     struct cmd_syndesc *as;
2508 {
2509     FILE *infd;
2510     afs_int32 code;
2511     struct cmd_item *ti;
2512     char name[80];
2513     afs_int32 rank;
2514     struct setspref *ssp;
2515     int error = 0;      /* -1 means error message printed,
2516                          * >0 means errno value for unprinted message */
2517
2518     ssp = (struct setspref *)space;
2519     ssp->flags = 0;
2520     ssp->num_servers = 0;
2521     gblob.in_size = ((char*)&(ssp->servers[0])) - (char *)ssp;
2522     gblob.in = space;
2523     gblob.out = space;
2524     gblob.out_size = MAXSIZE;
2525
2526
2527     if (geteuid()) {
2528         fprintf(stderr, "Permission denied: requires root access.\n");
2529         return 1;
2530     }
2531
2532     ti = as->parms[2].items;  /* -file */
2533     if (ti) {
2534         if (debug) fprintf(stderr, "opening file %s\n",ti->data);
2535         if (!(infd = fopen(ti->data,"r"))) {
2536             perror(ti->data);
2537             error = -1;
2538         }
2539         else {
2540             while (fscanf(infd, "%79s%ld", name, &rank) != EOF) {
2541                 code = addServer(name, (unsigned short) rank);
2542                 if (code) error = code;
2543             }
2544         }
2545     }
2546
2547     ti = as->parms[3].items;  /* -stdin */
2548     if (ti) {
2549         while (scanf("%79s%ld", name, &rank) != EOF) {
2550             code = addServer(name, (unsigned short) rank);
2551             if (code) error = code;
2552         }
2553     }
2554
2555     for (ti = as->parms[0].items;ti;ti=ti->next) { /* list of servers, ranks */
2556         if (ti) {
2557             if (!ti->next) {
2558                 break;
2559             }
2560             code = addServer(ti->data, (unsigned short) atol(ti->next->data));
2561             if (code) error = code;
2562             if (debug)
2563                 printf("set fs prefs %s %s\n", ti->data, ti->next->data);
2564             ti=ti->next;
2565         }
2566     }
2567     code = pokeServers();
2568     if (code) error = code;
2569     if (debug)
2570         printf("now working on vlservers, code=%d\n",code);
2571
2572     ssp = (struct setspref *)space;
2573     ssp->flags = DBservers;
2574     ssp->num_servers = 0;
2575     gblob.in_size = ((char*)&(ssp->servers[0])) - (char *)ssp;
2576     gblob.in = space;
2577
2578     for (ti = as->parms[1].items;ti;ti=ti->next) { /* list of dbservers, ranks */
2579         if (ti) {
2580             if (!ti->next) {
2581                 break;
2582             }
2583             code = addServer(ti->data, (unsigned short) atol(ti->next->data));
2584             if (code) error = code;
2585             if (debug)
2586                 printf("set vl prefs %s %s\n", ti->data, ti->next->data);
2587             ti=ti->next;
2588         }
2589     }
2590
2591     if (as->parms[1].items) {
2592         if (debug)
2593             printf("now poking vlservers\n");
2594         code = pokeServers();
2595         if (code) error = code;
2596     }
2597
2598     if (error > 0)
2599         Die(error, 0);
2600
2601     return error ? 1 : 0;
2602 }
2603
2604
2605
2606 static GetPrefCmd(as)
2607     struct cmd_syndesc *as;
2608 {
2609     afs_int32 code;
2610     struct cmd_item *ti;
2611     char *name, tbuffer[20];
2612     afs_int32 rank,addr;
2613     FILE * outfd;
2614     int resolve;
2615     int vlservers = 0;
2616     struct ViceIoctl blob;
2617     struct sprefrequest *in;
2618     struct sprefinfo *out;
2619     int i;
2620
2621     ti = as->parms[0].items;  /* -file */
2622     if (ti) {
2623         if (debug) fprintf(stderr, "opening file %s\n",ti->data);
2624         if (!(outfd = freopen(ti->data,"w",stdout))) {
2625             perror(ti->data);
2626             return 1;
2627         }
2628     }
2629
2630     ti = as->parms[1].items;  /* -numeric */
2631     resolve = !(ti);
2632     ti = as->parms[2].items;  /* -vlservers */
2633     vlservers |= (ti ? DBservers : 0);
2634   /*  ti = as->parms[3].items;   -cell */
2635
2636     in = (struct sprefrequest *)space;
2637     in->offset = 0;
2638
2639     do {
2640         blob.in_size=sizeof(struct sprefrequest);
2641         blob.in = (char *)in;
2642         blob.out = space;
2643         blob.out_size = MAXSIZE;
2644
2645         in->num_servers = (MAXSIZE - 2*sizeof(short))/sizeof(struct spref);
2646         in->flags = vlservers;
2647
2648         code = pioctl(0, VIOC_GETSPREFS, &blob, 1);
2649         if (code) {
2650             perror("getserverprefs pioctl");
2651             return 1;
2652         }
2653
2654         out = (struct sprefinfo *) blob.out;
2655
2656         for (i=0;i<out->num_servers;i++) {
2657             if (resolve) {
2658                 name = hostutil_GetNameByINet(out->servers[i].server.s_addr);
2659             }
2660             else {
2661                 addr = ntohl(out->servers[i].server.s_addr);
2662                 sprintf(tbuffer, "%d.%d.%d.%d",
2663                         (addr>>24) & 0xff, (addr>>16) & 0xff,
2664                         (addr>>8) & 0xff, addr & 0xff);
2665                 name=tbuffer;
2666             }
2667             printf("%-50s %5u\n",name,out->servers[i].rank);
2668         }
2669
2670         in->offset = out->next_offset;
2671     } while (out->next_offset > 0);
2672
2673     return 0;
2674 }
2675
2676 static StoreBehindCmd(as)
2677     struct cmd_syndesc *as;
2678 {
2679     afs_int32 code=0;
2680     struct ViceIoctl blob;
2681     struct cmd_item *ti;
2682     struct sbstruct tsb, tsb2;
2683     int verbose = 0;
2684     afs_int32 allfiles;
2685     char *t;
2686     int error = 0;
2687
2688     tsb.sb_thisfile = -1;
2689     ti=as->parms[0].items;  /* -kbytes */
2690     if (ti) {
2691         if (!as->parms[1].items) {
2692             fprintf(stderr, "%s: you must specify -files with -kbytes.\n", pn);
2693             return 1;
2694         }
2695         tsb.sb_thisfile = strtol(ti->data,&t,10) * 1024;
2696         if ((tsb.sb_thisfile < 0) || (t != ti->data+strlen(ti->data))) {
2697             fprintf(stderr, "%s: %s must be 0 or a positive number.\n",
2698                     pn, ti->data);
2699             return 1;
2700         }
2701     }
2702
2703     allfiles = tsb.sb_default = -1;    /* Don't set allfiles yet */
2704     ti=as->parms[2].items;  /* -allfiles */
2705     if (ti) {
2706         allfiles = strtol(ti->data,&t,10) * 1024;
2707         if ((allfiles < 0) || (t != ti->data+strlen(ti->data))) {
2708             fprintf(stderr, "%s: %s must be 0 or a positive number.\n",
2709                     pn, ti->data);
2710             return 1;
2711         }
2712     }
2713
2714     /* -verbose or -file only or no options */
2715     if (as->parms[3].items ||
2716         (as->parms[1].items && !as->parms[0].items) ||
2717         (!as->parms[0].items && !as->parms[1].items && !as->parms[2].items))
2718         verbose = 1;
2719
2720     blob.in  = (char *)&tsb;
2721     blob.out = (char *)&tsb2;
2722     blob.in_size = blob.out_size = sizeof(struct sbstruct);
2723     bzero (&tsb2, sizeof(tsb2));
2724
2725     /* once per -file */
2726     for (ti=as->parms[1].items; ti; ti=ti->next) {
2727         /* Do this solely to see if the file is there */
2728         code = pioctl(ti->data, VIOCWHEREIS, &blob, 1);
2729         if (code) {
2730             Die(errno, ti->data);
2731             error = 1;
2732             continue;
2733         }
2734
2735         code = pioctl(ti->data, VIOC_STORBEHIND, &blob, 1);
2736         if (code) {
2737             Die(errno, ti->data);
2738             error = 1;
2739             continue;
2740         }
2741
2742         if (verbose && (blob.out_size == sizeof(tsb2))) {
2743             if (tsb2.sb_thisfile == -1) {
2744                 fprintf(stdout, "Will store %s according to default.\n",
2745                         ti->data);
2746             }
2747             else {
2748                 fprintf(stdout,
2749                         "Will store up to %d kbytes of %s asynchronously.\n",
2750                         (tsb2.sb_thisfile/1024), ti->data);
2751             }
2752         }
2753     }
2754
2755     /* If no files - make at least one pioctl call, or
2756      * set the allfiles default if we need to.
2757      */
2758     if (!as->parms[1].items || (allfiles != -1)) {
2759         tsb.sb_default = allfiles;
2760         code = pioctl(0, VIOC_STORBEHIND, &blob, 1);
2761         if (code) {
2762             Die(errno, ((allfiles == -1)?0:"-allfiles"));
2763             error = 1;
2764         }
2765     }
2766
2767     /* Having no arguments also reports the default store asynchrony */
2768     if (verbose && (blob.out_size == sizeof(tsb2))) {
2769         fprintf(stdout, "Default store asynchrony is %d kbytes.\n",
2770                 (tsb2.sb_default/1024));
2771     }
2772
2773     return error;
2774 }
2775
2776
2777 static afs_int32 SetCryptCmd(as)
2778     struct cmd_syndesc *as;
2779 {
2780     afs_int32 code = 0, flag;
2781     struct ViceIoctl blob;
2782     char *tp;
2783  
2784     tp = as->parms[0].items->data;
2785     if (strcmp(tp, "on") == 0)
2786       flag = 1;
2787     else if (strcmp(tp, "off") == 0)
2788       flag = 0;
2789     else {
2790       fprintf (stderr, "%s: %s must be \"on\" or \"off\".\n", pn, tp);
2791       return EINVAL;
2792     }
2793
2794     blob.in = (char *) &flag;
2795     blob.in_size = sizeof(flag);
2796     blob.out_size = 0;
2797     code = pioctl(0, VIOC_SETRXKCRYPT, &blob, 1);
2798     if (code)
2799       Die(code, (char *) 0);
2800     return 0;
2801 }
2802
2803
2804 static afs_int32 GetCryptCmd(as)
2805     struct cmd_syndesc *as;
2806 {
2807     afs_int32 code = 0, flag;
2808     struct ViceIoctl blob;
2809     char *tp;
2810  
2811     blob.in = (char *) 0;
2812     blob.in_size = 0;
2813     blob.out_size = sizeof(flag);
2814     blob.out = space;
2815
2816     code = pioctl(0, VIOC_GETRXKCRYPT, &blob, 1);
2817
2818     if (code) Die(code, (char *) 0);
2819     else {
2820       tp = space;
2821       bcopy(tp, &flag, sizeof(afs_int32));
2822       printf("Security level is currently ");
2823       if (flag == 1)
2824         printf("crypt (data security).\n");
2825       else
2826         printf("clear.\n");
2827     }
2828     return 0;
2829 }
2830
2831 #include "AFS_component_version_number.c"
2832
2833 main(argc, argv)
2834     int argc;
2835     char **argv;
2836 {
2837     afs_int32 code;
2838     struct cmd_syndesc *ts;
2839
2840 #ifdef  AFS_AIX32_ENV
2841     /*
2842      * The following signal action for AIX is necessary so that in case of a
2843      * crash (i.e. core is generated) we can include the user's data section
2844      * in the core dump. Unfortunately, by default, only a partial core is
2845      * generated which, in many cases, isn't too useful.
2846      */
2847     struct sigaction nsa;
2848
2849     sigemptyset(&nsa.sa_mask);
2850     nsa.sa_handler = SIG_DFL;
2851     nsa.sa_flags = SA_FULLDUMP;
2852     sigaction(SIGSEGV, &nsa, NULL);
2853 #endif
2854
2855     /* try to find volume location information */
2856     ts = cmd_CreateSyntax("getclientaddrs", GetClientAddrsCmd, 0, "get client network interface addresses");
2857     cmd_CreateAlias(ts, "gc");
2858
2859     ts = cmd_CreateSyntax("setclientaddrs", SetClientAddrsCmd, 0, "set client network interface addresses");
2860     cmd_AddParm(ts, "-address", CMD_LIST, CMD_OPTIONAL|CMD_EXPANDS, "client network interfaces");
2861     cmd_CreateAlias(ts, "sc");
2862
2863     ts = cmd_CreateSyntax("setserverprefs", SetPrefCmd, 0, "set server ranks");
2864     cmd_AddParm(ts, "-servers", CMD_LIST, CMD_OPTIONAL|CMD_EXPANDS, "fileserver names and ranks");
2865     cmd_AddParm(ts, "-vlservers", CMD_LIST, CMD_OPTIONAL|CMD_EXPANDS, "VL server names and ranks");
2866     cmd_AddParm(ts, "-file", CMD_SINGLE, CMD_OPTIONAL, "input from named file");
2867     cmd_AddParm(ts, "-stdin", CMD_FLAG, CMD_OPTIONAL, "input from stdin");
2868     cmd_CreateAlias(ts, "sp");
2869
2870     ts = cmd_CreateSyntax("getserverprefs", GetPrefCmd, 0, "get server ranks");
2871     cmd_AddParm(ts, "-file", CMD_SINGLE, CMD_OPTIONAL, "output to named file");
2872     cmd_AddParm(ts, "-numeric", CMD_FLAG, CMD_OPTIONAL, "addresses only");
2873     cmd_AddParm(ts, "-vlservers", CMD_FLAG, CMD_OPTIONAL, "VL servers");
2874 /*    cmd_AddParm(ts, "-cell", CMD_FLAG, CMD_OPTIONAL, "cellname"); */
2875     cmd_CreateAlias(ts, "gp");
2876
2877     ts = cmd_CreateSyntax("setacl", SetACLCmd, 0, "set access control list");
2878     cmd_AddParm(ts, "-dir", CMD_LIST, 0, "directory");
2879     cmd_AddParm(ts, "-acl", CMD_LIST, 0, "access list entries");
2880     cmd_AddParm(ts, "-clear", CMD_FLAG, CMD_OPTIONAL, "clear access list");
2881     cmd_AddParm(ts, "-negative", CMD_FLAG, CMD_OPTIONAL, "apply to negative rights");
2882     parm_setacl_id = ts->nParms;
2883     cmd_AddParm(ts, "-id", CMD_FLAG, CMD_OPTIONAL, "initial directory acl (DFS only)");
2884     cmd_AddParm(ts, "-if", CMD_FLAG, CMD_OPTIONAL, "initial file acl (DFS only)");
2885     cmd_CreateAlias(ts, "sa");
2886
2887     ts = cmd_CreateSyntax("listacl", ListACLCmd, 0, "list access control list");
2888     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
2889     parm_listacl_id = ts->nParms;
2890     cmd_AddParm(ts, "-id", CMD_FLAG, CMD_OPTIONAL, "initial directory acl");
2891     cmd_AddParm(ts, "-if", CMD_FLAG, CMD_OPTIONAL, "initial file acl");
2892     cmd_CreateAlias(ts, "la");
2893
2894     ts = cmd_CreateSyntax("cleanacl", CleanACLCmd, 0, "clean up access control list");
2895     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
2896
2897     ts = cmd_CreateSyntax("copyacl", CopyACLCmd, 0, "copy access control list");
2898     cmd_AddParm(ts, "-fromdir", CMD_SINGLE, 0, "source directory (or DFS file)");
2899     cmd_AddParm(ts, "-todir", CMD_LIST, 0, "destination directory (or DFS file)");
2900     cmd_AddParm(ts, "-clear", CMD_FLAG, CMD_OPTIONAL, "first clear dest access list");
2901     parm_copyacl_id = ts->nParms;
2902     cmd_AddParm(ts, "-id", CMD_FLAG, CMD_OPTIONAL, "initial directory acl");
2903     cmd_AddParm(ts, "-if", CMD_FLAG, CMD_OPTIONAL, "initial file acl");
2904
2905     cmd_CreateAlias(ts, "ca");
2906
2907     ts = cmd_CreateSyntax("flush", FlushCmd, 0, "flush file from cache");
2908     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
2909     ts = cmd_CreateSyntax("flushmount", FlushMountCmd, 0, "flush mount symlink from cache");
2910     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
2911
2912     ts = cmd_CreateSyntax("setvol", SetVolCmd, 0, "set volume status");
2913     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
2914     cmd_AddParm(ts, "-max", CMD_SINGLE, CMD_OPTIONAL, "disk space quota in 1K units");
2915 #ifdef notdef
2916     cmd_AddParm(ts, "-min", CMD_SINGLE, CMD_OPTIONAL, "disk space guaranteed");
2917     cmd_AddParm(ts, "-motd", CMD_SINGLE, CMD_OPTIONAL, "message of the day");
2918 #endif
2919     cmd_AddParm(ts, "-offlinemsg", CMD_SINGLE, CMD_OPTIONAL, "offline message");
2920     cmd_CreateAlias(ts, "sv");
2921
2922     ts = cmd_CreateSyntax("messages", MessagesCmd, 0, "control Cache Manager messages");
2923     cmd_AddParm(ts, "-show", CMD_SINGLE, CMD_OPTIONAL, "[user|console|all|none]");
2924
2925     ts = cmd_CreateSyntax("examine", ExamineCmd, 0, "display volume status");
2926     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
2927     cmd_CreateAlias(ts, "lv");
2928     cmd_CreateAlias(ts, "listvol");
2929
2930     ts = cmd_CreateSyntax("listquota", ListQuotaCmd, 0, "list volume quota");
2931     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
2932     cmd_CreateAlias(ts, "lq");
2933
2934     ts = cmd_CreateSyntax("diskfree", DiskFreeCmd, 0, "show server disk space usage");
2935     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
2936     cmd_CreateAlias(ts, "df");
2937
2938     ts = cmd_CreateSyntax("quota", QuotaCmd, 0, "show volume quota usage");
2939     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
2940
2941     ts = cmd_CreateSyntax("lsmount", ListMountCmd, 0, "list mount point");
2942     cmd_AddParm(ts, "-dir", CMD_LIST, 0, "directory");
2943
2944     ts = cmd_CreateSyntax("mkmount", MakeMountCmd, 0, "make mount point");
2945     cmd_AddParm(ts, "-dir", CMD_SINGLE, 0, "directory");
2946     cmd_AddParm(ts, "-vol", CMD_SINGLE, 0, "volume name");
2947     cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cell name");
2948     cmd_AddParm(ts, "-rw", CMD_FLAG, CMD_OPTIONAL, "force r/w volume");
2949     cmd_AddParm(ts, "-fast", CMD_FLAG, CMD_OPTIONAL, "don't check name with VLDB");
2950
2951 /*
2952
2953 defect 3069
2954
2955     cmd_AddParm(ts, "-root", CMD_FLAG, CMD_OPTIONAL, "create cellular mount point");
2956 */
2957
2958
2959     ts = cmd_CreateSyntax("rmmount", RemoveMountCmd, 0, "remove mount point");
2960     cmd_AddParm(ts, "-dir", CMD_LIST, 0, "directory");
2961
2962     ts = cmd_CreateSyntax("checkservers", CheckServersCmd, 0, "check local cell's servers");
2963     cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cell to check");
2964     cmd_AddParm(ts, "-all", CMD_FLAG, CMD_OPTIONAL, "check all cells");
2965     cmd_AddParm(ts, "-fast", CMD_FLAG, CMD_OPTIONAL, "just list, don't check");
2966     cmd_AddParm(ts,"-interval",CMD_SINGLE,CMD_OPTIONAL,"seconds between probes");
2967
2968     ts = cmd_CreateSyntax("checkvolumes", CheckVolumesCmd,0, "check volumeID/name mappings");
2969     cmd_CreateAlias(ts, "checkbackups");
2970
2971
2972     ts = cmd_CreateSyntax("setcachesize", SetCacheSizeCmd, 0, "set cache size");
2973     cmd_AddParm(ts, "-blocks", CMD_SINGLE, CMD_OPTIONAL, "size in 1K byte blocks (0 => reset)");
2974     cmd_CreateAlias(ts, "cachesize");
2975
2976     cmd_AddParm(ts, "-reset", CMD_FLAG, CMD_OPTIONAL, "reset size back to boot value");
2977
2978     ts = cmd_CreateSyntax("getcacheparms", GetCacheParmsCmd, 0, "get cache usage info");
2979
2980     ts = cmd_CreateSyntax("listcells", ListCellsCmd, 0, "list configured cells");
2981     cmd_AddParm(ts, "-numeric", CMD_FLAG, CMD_OPTIONAL, "addresses only");
2982
2983     ts = cmd_CreateSyntax("setquota", SetQuotaCmd, 0, "set volume quota");
2984     cmd_AddParm(ts, "-path", CMD_SINGLE, CMD_OPTIONAL, "dir/file path");
2985     cmd_AddParm(ts, "-max", CMD_SINGLE, 0, "max quota in kbytes");
2986 #ifdef notdef
2987     cmd_AddParm(ts, "-min", CMD_SINGLE, CMD_OPTIONAL, "min quota in kbytes");
2988 #endif
2989     cmd_CreateAlias(ts, "sq");
2990
2991     ts = cmd_CreateSyntax("newcell", NewCellCmd, 0, "configure new cell");
2992     cmd_AddParm(ts, "-name", CMD_SINGLE, 0, "cell name");
2993     cmd_AddParm(ts, "-servers", CMD_LIST, CMD_REQUIRED, "primary servers");
2994     cmd_AddParm(ts, "-linkedcell", CMD_SINGLE, CMD_OPTIONAL, "linked cell name");
2995
2996 #ifdef FS_ENABLE_SERVER_DEBUG_PORTS
2997 /*
2998  * Turn this on only if you wish to be able to talk to a server which is listening
2999  * on alternative ports. This is not intended for general use and may not be
3000  * supported in the cache manager. It is not a way to run two servers at the
3001  * same host, since the cache manager cannot properly distinguish those two hosts.
3002  */
3003     cmd_AddParm(ts, "-fsport", CMD_SINGLE, CMD_OPTIONAL, "cell's fileserver port");
3004     cmd_AddParm(ts, "-vlport", CMD_SINGLE, CMD_OPTIONAL, "cell's vldb server port");
3005 #endif
3006
3007     ts = cmd_CreateSyntax("whichcell", WhichCellCmd, 0, "list file's cell");
3008     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3009
3010     ts = cmd_CreateSyntax("whereis", WhereIsCmd, 0, "list file's location");
3011     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3012
3013     ts = cmd_CreateSyntax("wscell", WSCellCmd, 0, "list workstation's cell");
3014
3015 /*
3016     ts = cmd_CreateSyntax("primarycell", PrimaryCellCmd, 0, "obsolete (listed primary cell)");
3017 */
3018
3019     /* set cache monitor host address */
3020     ts = cmd_CreateSyntax("monitor", MonitorCmd, 0, (char *) CMD_HIDDEN);
3021     cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_OPTIONAL, "host name or 'off'");
3022     cmd_CreateAlias(ts, "mariner");
3023
3024     ts = cmd_CreateSyntax("getcellstatus", GetCellCmd, 0, "get cell status");
3025     cmd_AddParm(ts, "-cell", CMD_LIST, 0, "cell name");
3026
3027     ts = cmd_CreateSyntax("setcell", SetCellCmd, 0, "set cell status");
3028     cmd_AddParm(ts, "-cell", CMD_LIST, 0, "cell name");
3029     cmd_AddParm(ts, "-suid", CMD_FLAG, CMD_OPTIONAL, "allow setuid programs");
3030     cmd_AddParm(ts, "-nosuid", CMD_FLAG, CMD_OPTIONAL, "disallow setuid programs");
3031
3032     ts = cmd_CreateSyntax("flushvolume", FlushVolumeCmd, 0, "flush all data in volume");
3033     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3034
3035     ts = cmd_CreateSyntax("sysname", SysNameCmd, 0, "get/set sysname (i.e. @sys) value");
3036     cmd_AddParm(ts, "-newsys", CMD_LIST, CMD_OPTIONAL, "new sysname");
3037
3038     ts = cmd_CreateSyntax("exportafs", ExportAfsCmd, 0, "enable/disable translators to AFS");
3039     cmd_AddParm(ts, "-type", CMD_SINGLE, 0, "exporter name");
3040     cmd_AddParm(ts, "-start", CMD_SINGLE, CMD_OPTIONAL, "start/stop translator (on | off)");
3041     cmd_AddParm(ts, "-convert", CMD_SINGLE, CMD_OPTIONAL, "convert from afs to unix mode (on | off)");
3042     cmd_AddParm(ts, "-uidcheck", CMD_SINGLE, CMD_OPTIONAL, "run on strict 'uid check' mode (on | off)");
3043     cmd_AddParm(ts, "-submounts", CMD_SINGLE, CMD_OPTIONAL, "allow nfs mounts to subdirs of /afs/.. (on  | off)");
3044
3045
3046     ts = cmd_CreateSyntax("storebehind", StoreBehindCmd, 0,
3047                           "store to server after file close");
3048     cmd_AddParm(ts, "-kbytes", CMD_SINGLE, CMD_OPTIONAL, "asynchrony for specified names");
3049     cmd_AddParm(ts, "-files", CMD_LIST, CMD_OPTIONAL, "specific pathnames");
3050     cmd_AddParm(ts, "-allfiles", CMD_SINGLE, CMD_OPTIONAL, "new default (KB)");
3051     cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL, "show status");
3052     cmd_CreateAlias(ts, "sb");
3053
3054     ts = cmd_CreateSyntax("setcrypt", SetCryptCmd, 0, "set cache manager encryption flag");
3055     cmd_AddParm(ts, "-crypt", CMD_SINGLE, 0, "on or off");
3056
3057     ts = cmd_CreateSyntax("getcrypt", GetCryptCmd, 0, "set cache manager encryption flag");
3058
3059     ts = cmd_CreateSyntax("rxstatproc", RxStatProcCmd, 0,
3060                           "Manage per process RX statistics");
3061     cmd_AddParm(ts, "-enable", CMD_FLAG, CMD_OPTIONAL,
3062                 "Enable RX stats");
3063     cmd_AddParm(ts, "-disable", CMD_FLAG, CMD_OPTIONAL,
3064                 "Disable RX stats");
3065     cmd_AddParm(ts, "-clear", CMD_FLAG, CMD_OPTIONAL,
3066                 "Clear RX stats");
3067
3068     ts = cmd_CreateSyntax("rxstatpeer", RxStatPeerCmd, 0,
3069                           "Manage per peer RX statistics");
3070     cmd_AddParm(ts, "-enable", CMD_FLAG, CMD_OPTIONAL,
3071                 "Enable RX stats");
3072     cmd_AddParm(ts, "-disable", CMD_FLAG, CMD_OPTIONAL,
3073                 "Disable RX stats");
3074     cmd_AddParm(ts, "-clear", CMD_FLAG, CMD_OPTIONAL,
3075                 "Clear RX stats");
3076
3077     code = cmd_Dispatch(argc, argv);
3078     if (rxInitDone) rx_Finalize();
3079
3080     return code;
3081 }
3082
3083 static void Die(errnum, filename)
3084     int errnum;
3085     char *filename;
3086 {
3087     switch (errnum) {
3088     case EINVAL:
3089         if (filename)
3090             fprintf(stderr, "%s: Invalid argument; it is possible that %s is not in AFS.\n",
3091                     pn, filename);
3092         else
3093             fprintf(stderr, "%s: Invalid argument.\n", pn);
3094         break;
3095     case ENOENT:
3096         if (filename)
3097             fprintf(stderr, "%s: File '%s' doesn't exist\n", pn, filename);
3098         else
3099             fprintf(stderr, "%s: no such file returned\n", pn);
3100         break;
3101     case EROFS:
3102         fprintf(stderr, "%s: You can not change a backup or readonly volume\n", pn);
3103         break;
3104     case EACCES:
3105     case EPERM:
3106         if (filename)
3107             fprintf(stderr, "%s: You don't have the required access rights on '%s'\n",
3108                     pn, filename);
3109         else
3110             fprintf(stderr, "%s: You do not have the required rights to do this operation\n",
3111                     pn);
3112         break;
3113     default:
3114         if (filename)
3115             fprintf(stderr, "%s:'%s'", pn, filename);
3116         else
3117             fprintf(stderr, "%s", pn);
3118         fprintf(stderr, ": %s\n", error_message(errnum));
3119         break;
3120     }
3121 }
3122
3123 /* get clients interface addresses */
3124 static int
3125 GetClientAddrsCmd(as)
3126     struct cmd_syndesc *as;
3127 {
3128     afs_int32 code;
3129     struct cmd_item *ti;
3130     char *name;
3131     struct ViceIoctl blob;
3132     struct sprefrequest *in;
3133     struct sprefinfo *out;
3134
3135     in = (struct sprefrequest *)space;
3136     in->offset = 0;
3137
3138     do
3139     {
3140         blob.in_size=sizeof(struct sprefrequest);
3141         blob.in = (char *)in;
3142         blob.out = space;
3143         blob.out_size = MAXSIZE;
3144
3145         in->num_servers = (MAXSIZE - 2*sizeof(short))/sizeof(struct spref);
3146         /* returns addr in network byte order */
3147         code = pioctl(0, VIOC_GETCPREFS, &blob, 1);
3148         if (code) {
3149             perror("getClientInterfaceAddr pioctl");
3150             return 1;
3151         }
3152
3153         {
3154             int i;
3155             out = (struct sprefinfo *) blob.out;
3156             for (i=0; i < out->num_servers; i++) {
3157                 afs_int32 addr;
3158                 char tbuffer[32];
3159                 addr = ntohl(out->servers[i].server.s_addr);
3160                 sprintf(tbuffer, "%d.%d.%d.%d",
3161                         (addr>>24) & 0xff, (addr>>16) & 0xff,
3162                         (addr>>8) & 0xff, addr & 0xff);
3163                 printf ("%-50s\n", tbuffer);
3164             }
3165             in->offset = out->next_offset;
3166         }
3167     } while (out->next_offset > 0);
3168
3169     return 0;
3170 }
3171
3172 static int
3173 SetClientAddrsCmd(as)
3174     struct cmd_syndesc *as;
3175 {
3176     afs_int32 code, addr;
3177     struct cmd_item *ti;
3178     char name[80];
3179     struct ViceIoctl blob;
3180     struct setspref *ssp;
3181     int sizeUsed = 0, i, flag;
3182     afs_int32 existingAddr[1024]; /* existing addresses on this host */
3183     int existNu;
3184     int error = 0;
3185
3186     ssp = (struct setspref *)space;
3187     ssp->num_servers = 0;
3188     blob.in = space;
3189     blob.out = space;
3190     blob.out_size = MAXSIZE;
3191
3192     if (geteuid()) {
3193         fprintf(stderr, "Permission denied: requires root access.\n");
3194         return 1;
3195     }
3196
3197     /* extract all existing interface addresses */
3198     existNu = rx_getAllAddr(existingAddr,1024);
3199     if (existNu < 0)
3200         return 1;
3201
3202     sizeUsed = sizeof(struct setspref);  /* space used in ioctl buffer */
3203     for (ti = as->parms[0].items;ti;ti=ti->next) {
3204         if (sizeUsed >= sizeof(space)) {
3205             fprintf(stderr, "No more space\n");
3206             return 1;
3207         }
3208         addr = extractAddr(ti->data, 20); /* network order */
3209         if ((addr == AFS_IPINVALID) || (addr == AFS_IPINVALIDIGNORE)) {
3210             fprintf(stderr, "Error in specifying address: %s..ignoring\n", ti->data);
3211             error = 1;
3212             continue;
3213         }
3214         /* see if it is an address that really exists */
3215         for (flag = 0, i=0; i < existNu; i++)
3216             if (existingAddr[i] == addr) {
3217                 flag = 1;
3218                 break;
3219             }
3220         if (!flag) {    /* this is an nonexistent address */
3221             fprintf(stderr, "Nonexistent address: 0x%08x..ignoring\n", addr);
3222             error = 1;
3223             continue;
3224         }
3225         /* copy all specified addr into ioctl buffer */
3226         (ssp->servers[ssp->num_servers]).server.s_addr = addr;
3227         printf("Adding 0x%08x\n", addr);
3228         ssp->num_servers++;
3229         sizeUsed += sizeof(struct spref);
3230     }
3231     if (ssp->num_servers < 1) {
3232         fprintf(stderr, "No addresses specified\n");
3233         return 1;
3234     }
3235     blob.in_size = sizeUsed - sizeof(struct spref);
3236
3237     code = pioctl(0, VIOC_SETCPREFS, &blob, 1); /* network order */
3238     if (code) {
3239         Die(errno, 0);
3240         error = 1;
3241     }
3242
3243     return error;
3244 }
3245
3246 static int
3247 FlushMountCmd(as)
3248     struct cmd_syndesc *as;
3249 {
3250     afs_int32 code;
3251     struct ViceIoctl blob;
3252     struct cmd_item *ti;
3253     char orig_name[1024];               /*Original name, may be modified*/
3254     char true_name[1024];               /*``True'' dirname (e.g., symlink target)*/
3255     char parent_dir[1024];              /*Parent directory of true name*/
3256     char *last_component;               /*Last component of true name*/
3257     struct stat statbuff;               /*Buffer for status info*/
3258     int link_chars_read;                /*Num chars read in readlink()*/
3259     int thru_symlink;                   /*Did we get to a mount point via a symlink?*/
3260     int error = 0;
3261
3262     for(ti=as->parms[0].items; ti; ti=ti->next) {
3263         /* once per file */
3264         thru_symlink = 0;
3265         sprintf(orig_name, "%s%s",
3266                 (ti->data[0] == '/') ? "" : "./",
3267                 ti->data);
3268
3269         if (lstat(orig_name, &statbuff) < 0) {
3270             /* if lstat fails, we should still try the pioctl, since it
3271                 may work (for example, lstat will fail, but pioctl will
3272                     work if the volume of offline (returning ENODEV). */
3273             statbuff.st_mode = S_IFDIR; /* lie like pros */
3274         }
3275
3276         /*
3277          * The lstat succeeded.  If the given file is a symlink, substitute
3278          * the file name with the link name.
3279          */
3280         if ((statbuff.st_mode & S_IFMT) == S_IFLNK) {
3281             thru_symlink = 1;
3282             /*
3283              * Read name of resolved file.
3284              */
3285             link_chars_read = readlink(orig_name, true_name, 1024);
3286             if (link_chars_read <= 0) {
3287                 fprintf(stderr, "%s: Can't read target name for '%s' symbolic link!\n",
3288                        pn, orig_name);
3289                 error = 1;
3290                 continue;
3291             }
3292
3293             /*
3294              * Add a trailing null to what was read, bump the length.
3295              */
3296             true_name[link_chars_read++] = 0;
3297
3298             /*
3299              * If the symlink is an absolute pathname, we're fine.  Otherwise, we
3300              * have to create a full pathname using the original name and the
3301              * relative symlink name.  Find the rightmost slash in the original
3302              * name (we know there is one) and splice in the symlink value.
3303              */
3304             if (true_name[0] != '/') {
3305                 last_component = (char *) rindex(orig_name, '/');
3306                 strcpy(++last_component, true_name);
3307                 strcpy(true_name, orig_name);
3308             }
3309         }
3310         else
3311             strcpy(true_name, orig_name);
3312
3313         /*
3314          * Find rightmost slash, if any.
3315          */
3316         last_component = (char *) rindex(true_name, '/');
3317         if (last_component) {
3318             /*
3319              * Found it.  Designate everything before it as the parent directory,
3320              * everything after it as the final component.
3321              */
3322             strncpy(parent_dir, true_name, last_component - true_name);
3323             parent_dir[last_component - true_name] = 0;
3324             last_component++;   /*Skip the slash*/
3325         }
3326         else {
3327             /*
3328              * No slash appears in the given file name.  Set parent_dir to the current
3329              * directory, and the last component as the given name.
3330              */
3331             strcpy(parent_dir, ".");
3332             last_component = true_name;
3333         }
3334
3335         if (strcmp(last_component, ".") == 0 || strcmp(last_component, "..") == 0) {
3336             fprintf(stderr, "%s: you may not use '.' or '..' as the last component\n", pn);
3337             fprintf(stderr, "%s: of a name in the 'fs flushmount' command.\n", pn);
3338             error = 1;
3339             continue;
3340         }
3341
3342         blob.in = last_component;
3343         blob.in_size = strlen(last_component)+1;
3344         blob.out_size = 0;
3345         bzero(space, MAXSIZE);
3346
3347         code = pioctl(parent_dir, VIOC_AFS_FLUSHMOUNT, &blob, 1);
3348
3349         if (code != 0) {
3350             if (errno == EINVAL) {
3351                 fprintf(stderr, "'%s' is not a mount point.\n",
3352                        ti->data);
3353             }
3354             else {
3355                 Die(errno, (ti->data ? ti->data : parent_dir));
3356             }
3357             error = 1;
3358         }
3359     }
3360     return error;
3361 }
3362
3363 static int
3364 RxStatProcCmd(as)
3365     struct cmd_syndesc *as;
3366 {
3367     afs_int32 code;
3368     afs_int32 flags = 0;
3369     struct ViceIoctl blob;
3370     struct cmd_item *ti;
3371
3372     if (as->parms[0].items) { /* -enable*/
3373         flags |= AFSCALL_RXSTATS_ENABLE;
3374     }
3375     if (as->parms[1].items) { /* -disable*/
3376         flags |= AFSCALL_RXSTATS_DISABLE;
3377     }
3378     if (as->parms[2].items) { /* -clear*/
3379         flags |= AFSCALL_RXSTATS_CLEAR;
3380     }
3381     if (flags == 0) {
3382         fprintf(stderr, "You must specify at least one argument\n");
3383         return 1;
3384     }
3385
3386     blob.in = (char *)&flags;
3387     blob.in_size = sizeof(afs_int32);
3388     blob.out_size = 0;
3389
3390     code = pioctl(NULL, VIOC_RXSTAT_PROC, &blob, 1);
3391     if (code != 0) {
3392         Die(errno, NULL);
3393         return 1;
3394     }
3395
3396     return 0;
3397 }
3398
3399 static int
3400 RxStatPeerCmd(as)
3401     struct cmd_syndesc *as;
3402 {
3403     afs_int32 code;
3404     afs_int32 flags = 0;
3405     struct ViceIoctl blob;
3406     struct cmd_item *ti;
3407
3408     if (as->parms[0].items) { /* -enable*/
3409         flags |= AFSCALL_RXSTATS_ENABLE;
3410     }
3411     if (as->parms[1].items) { /* -disable*/
3412         flags |= AFSCALL_RXSTATS_DISABLE;
3413     }
3414     if (as->parms[2].items) { /* -clear*/
3415         flags |= AFSCALL_RXSTATS_CLEAR;
3416     }
3417     if (flags == 0) {
3418         fprintf(stderr, "You must specify at least one argument\n");
3419         return 1;
3420     }
3421
3422     blob.in = (char *)&flags;
3423     blob.in_size = sizeof(afs_int32);
3424     blob.out_size = 0;
3425
3426     code = pioctl(NULL, VIOC_RXSTAT_PEER, &blob, 1);
3427     if (code != 0) {
3428         Die(errno, NULL);
3429         return 1;
3430     }
3431
3432     return 0;
3433 }