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