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