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