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