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