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