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