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