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