e7c987d7c03d87968d2f6c41fd798711a2d2b1e7
[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 <assert.h>
22 #include <rx/rx_globals.h>
23
24 #include <osi.h>
25 #include <afsint.h>
26 #include <afs/cellconfig.h>
27 #include <afs/ptserver.h>
28 #include <afs/ptuser.h>
29 #include <afs/volser.h>
30 #include <WINNT\afsreg.h>
31
32 #include "fs.h"
33 #include "fs_utils.h"
34 #include "cmd.h"
35 #include "afsd.h"
36 #include "cm_ioctl.h"
37
38 #define MAXHOSTS 13
39 #define OMAXHOSTS 8
40 #define MAXCELLHOSTS 8
41 #define MAXNAME 100
42 #define MAXSIZE 2048
43 #define MAXINSIZE 1300    /* pioctl complains if data is larger than this */
44 #define VMSGSIZE 128      /* size of msg buf in volume hdr */
45 #define MAXCELLCHARS            64
46 #define MAXHOSTCHARS            64
47
48 static char space[MAXSIZE];
49 static char tspace[1024];
50
51 static struct ubik_client *uclient;
52
53 static int GetClientAddrsCmd(struct cmd_syndesc *asp, void *arock);
54 static int SetClientAddrsCmd(struct cmd_syndesc *asp, void *arock);
55 static int FlushMountCmd(struct cmd_syndesc *asp, void *arock);
56 static int RxStatProcCmd(struct cmd_syndesc *asp, void *arock);
57 static int RxStatPeerCmd(struct cmd_syndesc *asp, void *arock);
58
59 extern struct cmd_syndesc *cmd_CreateSyntax();
60
61 static int MemDumpCmd(struct cmd_syndesc *asp, void *arock);
62 static int CSCPolicyCmd(struct cmd_syndesc *asp, void *arock);
63 static int MiniDumpCmd(struct cmd_syndesc *asp, void *arock);
64
65 static char pn[] = "fs";
66 static int rxInitDone = 0;
67
68 /*
69  * Character to use between name and rights in printed representation for
70  * DFS ACL's.
71  */
72 #define DFS_SEPARATOR   ' '
73
74 typedef char sec_rgy_name_t[1025];      /* A DCE definition */
75
76 struct Acl {
77     int dfs;            /* Originally true if a dfs acl; now also the type
78                          * of the acl (1, 2, or 3, corresponding to object,
79                          * initial dir, or initial object). */
80     sec_rgy_name_t cell; /* DFS cell name */
81     int nplus;
82     int nminus;
83     struct AclEntry *pluslist;
84     struct AclEntry *minuslist;
85 };
86
87 struct AclEntry {
88     struct AclEntry *next;
89     char name[MAXNAME];
90     afs_int32 rights;
91 };
92
93 static void 
94 ZapAcl (struct Acl *acl)
95 {
96     if (!acl)
97         return;
98
99     ZapList(acl->pluslist);
100     ZapList(acl->minuslist);
101     free(acl);
102 }
103
104 /*
105  * Mods for the AFS/DFS protocol translator.
106  *
107  * DFS rights. It's ugly to put these definitions here, but they 
108  * *cannot* change, because they're part of the wire protocol.
109  * In any event, the protocol translator will guarantee these
110  * assignments for AFS cache managers.
111  */
112 #define DFS_READ          0x01
113 #define DFS_WRITE         0x02
114 #define DFS_EXECUTE       0x04
115 #define DFS_CONTROL       0x08
116 #define DFS_INSERT        0x10
117 #define DFS_DELETE        0x20
118
119 /* the application definable ones (backwards from AFS) */
120 #define DFS_USR0 0x80000000      /* "A" bit */
121 #define DFS_USR1 0x40000000      /* "B" bit */
122 #define DFS_USR2 0x20000000      /* "C" bit */
123 #define DFS_USR3 0x10000000      /* "D" bit */
124 #define DFS_USR4 0x08000000      /* "E" bit */
125 #define DFS_USR5 0x04000000      /* "F" bit */
126 #define DFS_USR6 0x02000000      /* "G" bit */
127 #define DFS_USR7 0x01000000      /* "H" bit */
128 #define DFS_USRALL      (DFS_USR0 | DFS_USR1 | DFS_USR2 | DFS_USR3 |\
129                          DFS_USR4 | DFS_USR5 | DFS_USR6 | DFS_USR7)
130
131 /*
132  * Offset of -id switch in command structure for various commands.
133  * The -if switch is the next switch always.
134  */
135 static int parm_setacl_id, parm_copyacl_id, parm_listacl_id;
136
137 /*
138  * Determine whether either the -id or -if switches are present, and
139  * return 0, 1 or 2, as appropriate. Abort if both switches are present.
140  */
141 /* int id; Offset of -id switch; -if is next switch */
142 static int 
143 getidf(struct cmd_syndesc *as, int id)
144 {
145     int idf = 0;
146
147     if (as->parms[id].items) {
148         idf |= 1;
149     }
150     if (as->parms[id + 1].items) {
151         idf |= 2;
152     }
153     if (idf == 3) {
154         fprintf(stderr,
155              "%s: you may specify either -id or -if, but not both switches\n",
156              pn);
157         exit(1);
158     }
159     return idf;
160 }
161
162 static int
163 PRights(afs_int32 arights, int dfs)
164 {
165     if (!dfs) {
166         if (arights & PRSFS_READ) 
167             printf("r");
168         if (arights & PRSFS_LOOKUP) 
169             printf("l");
170         if (arights & PRSFS_INSERT) 
171             printf("i");
172         if (arights & PRSFS_DELETE) 
173             printf("d");
174         if (arights & PRSFS_WRITE) 
175             printf("w");
176         if (arights & PRSFS_LOCK) 
177             printf("k");
178         if (arights & PRSFS_ADMINISTER) 
179             printf("a");
180         if (arights & PRSFS_USR0) 
181             printf("A");
182         if (arights & PRSFS_USR1) 
183             printf("B");
184         if (arights & PRSFS_USR2) 
185             printf("C");
186         if (arights & PRSFS_USR3) 
187             printf("D");
188         if (arights & PRSFS_USR4) 
189             printf("E");
190         if (arights & PRSFS_USR5) 
191             printf("F");
192         if (arights & PRSFS_USR6) 
193             printf("G");
194         if (arights & PRSFS_USR7) 
195             printf("H");
196     } else {
197         if (arights & DFS_READ) 
198             printf("r");
199         else 
200             printf("-");
201         if (arights & DFS_WRITE) 
202             printf("w"); 
203         else 
204             printf("-");
205         if (arights & DFS_EXECUTE) 
206             printf("x"); 
207         else 
208             printf("-");
209         if (arights & DFS_CONTROL) 
210             printf("c"); 
211         else 
212             printf("-");
213         if (arights & DFS_INSERT) 
214             printf("i"); 
215         else 
216             printf("-");
217         if (arights & DFS_DELETE) 
218             printf("d"); 
219         else 
220             printf("-");
221         if (arights & (DFS_USRALL)) 
222             printf("+");
223         if (arights & DFS_USR0) 
224             printf("A");
225         if (arights & DFS_USR1) 
226             printf("B");
227         if (arights & DFS_USR2) 
228             printf("C");
229         if (arights & DFS_USR3) 
230             printf("D");
231         if (arights & DFS_USR4) 
232             printf("E");
233         if (arights & DFS_USR5) 
234             printf("F");
235         if (arights & DFS_USR6) 
236             printf("G");
237         if (arights & DFS_USR7) 
238             printf("H");
239     }   
240     return 0;
241 }
242
243 /* this function returns TRUE (1) if the file is in AFS, otherwise false (0) */
244 static int 
245 InAFS(char *apath)
246 {
247     struct ViceIoctl blob;
248     afs_int32 code;
249
250     blob.in_size = 0;
251     blob.out_size = MAXSIZE;
252     blob.out = space;
253
254     code = pioctl(apath, VIOC_FILE_CELL_NAME, &blob, 1);
255     if (code) {
256         if ((errno == EINVAL) || (errno == ENOENT)) 
257             return 0;
258     }
259     return 1;
260 }
261
262 static int 
263 IsFreelanceRoot(char *apath)
264 {
265     struct ViceIoctl blob;
266     afs_int32 code;
267
268     blob.in_size = 0;
269     blob.out_size = MAXSIZE;
270     blob.out = space;
271
272     code = pioctl(apath, VIOC_FILE_CELL_NAME, &blob, 1);
273     if (code == 0)
274         return !stricmp("Freelance.Local.Root",space);
275     return 1;   /* assume it is because it is more restrictive that way */
276 }
277
278 /* return a static pointer to a buffer */
279 static char *
280 Parent(char *apath)
281 {
282     char *tp;
283     strcpy(tspace, apath);
284     tp = strrchr(tspace, '\\');
285     if (tp) {
286         *(tp+1) = 0;    /* lv trailing slash so Parent("k:\foo") is "k:\" not "k:" */
287     }
288     else {
289         fs_ExtractDriveLetter(apath, tspace);
290         strcat(tspace, ".");
291     }
292     return tspace;
293 }
294
295 enum rtype {add, destroy, deny};
296
297 static afs_int32 
298 Convert(char *arights, int dfs, enum rtype *rtypep)
299 {
300     int i, len;
301     afs_int32 mode;
302     char tc;
303
304     *rtypep = add;      /* add rights, by default */
305
306     if (dfs) {
307         if (!strcmp(arights, "null")) {
308             *rtypep = deny;
309             return 0;
310         }
311         if (!strcmp(arights,"read")) 
312             return DFS_READ | DFS_EXECUTE;
313         if (!strcmp(arights, "write")) 
314             return DFS_READ | DFS_EXECUTE | DFS_INSERT | DFS_DELETE | 
315                 DFS_WRITE;
316         if (!strcmp(arights, "all")) 
317             return DFS_READ | DFS_EXECUTE | DFS_INSERT | DFS_DELETE | 
318                 DFS_WRITE | DFS_CONTROL;
319     } else {
320         if (!strcmp(arights,"read")) 
321             return PRSFS_READ | PRSFS_LOOKUP;
322         if (!strcmp(arights, "write")) 
323             return PRSFS_READ | PRSFS_LOOKUP | PRSFS_INSERT | PRSFS_DELETE | 
324                 PRSFS_WRITE | PRSFS_LOCK;
325         if (!strcmp(arights, "mail")) 
326             return PRSFS_INSERT | PRSFS_LOCK | PRSFS_LOOKUP;
327         if (!strcmp(arights, "all")) 
328             return PRSFS_READ | PRSFS_LOOKUP | PRSFS_INSERT | PRSFS_DELETE | 
329                 PRSFS_WRITE | PRSFS_LOCK | PRSFS_ADMINISTER;
330     }
331     if (!strcmp(arights, "none")) {
332         *rtypep = destroy; /* Remove entire entry */
333         return 0;
334     }
335     len = (int)strlen(arights);
336     mode = 0;
337     for(i=0;i<len;i++) {
338         tc = *arights++;
339         if (dfs) {
340             if (tc == '-') 
341                 continue;
342             else if (tc == 'r') 
343                 mode |= DFS_READ;
344             else if (tc == 'w') 
345                 mode |= DFS_WRITE;
346             else if (tc == 'x') 
347                 mode |= DFS_EXECUTE;
348             else if (tc == 'c') 
349                 mode |= DFS_CONTROL;
350             else if (tc == 'i') 
351                 mode |= DFS_INSERT;
352             else if (tc == 'd') 
353                 mode |= DFS_DELETE;
354             else if (tc == 'A') 
355                 mode |= DFS_USR0;
356             else if (tc == 'B') 
357                 mode |= DFS_USR1;
358             else if (tc == 'C') 
359                 mode |= DFS_USR2;
360             else if (tc == 'D') 
361                 mode |= DFS_USR3;
362             else if (tc == 'E') 
363                 mode |= DFS_USR4;
364             else if (tc == 'F') 
365                 mode |= DFS_USR5;
366             else if (tc == 'G') 
367                 mode |= DFS_USR6;
368             else if (tc == 'H') 
369                 mode |= DFS_USR7;
370             else {
371                 fprintf(stderr, "%s: illegal DFS rights character '%c'.\n", 
372                          pn, tc);
373                 exit(1);
374             }
375         } else {
376             if (tc == 'r') 
377                 mode |= PRSFS_READ;
378             else if (tc == 'l') 
379                 mode |= PRSFS_LOOKUP;
380             else if (tc == 'i') 
381                 mode |= PRSFS_INSERT;
382             else if (tc == 'd') 
383                 mode |= PRSFS_DELETE;
384             else if (tc == 'w') 
385                 mode |= PRSFS_WRITE;
386             else if (tc == 'k') 
387                 mode |= PRSFS_LOCK;
388             else if (tc == 'a') 
389                 mode |= PRSFS_ADMINISTER;
390             else if (tc == 'A') 
391                 mode |= PRSFS_USR0;
392             else if (tc == 'B') 
393                 mode |= PRSFS_USR1;
394             else if (tc == 'C') 
395                 mode |= PRSFS_USR2;
396             else if (tc == 'D') 
397                 mode |= PRSFS_USR3;
398             else if (tc == 'E') 
399                 mode |= PRSFS_USR4;
400             else if (tc == 'F') 
401                 mode |= PRSFS_USR5;
402             else if (tc == 'G') 
403                 mode |= PRSFS_USR6;
404             else if (tc == 'H') 
405                 mode |= PRSFS_USR7;
406             else {
407                 fprintf(stderr, "%s: illegal rights character '%c'.\n", pn, 
408                          tc);
409                 exit(1);
410             }
411         }
412     }
413     return mode;
414 }
415
416 static struct AclEntry *
417 FindList (struct AclEntry *alist, char *aname)
418 {
419     while (alist) {
420         if (!strcasecmp(alist->name, aname)) 
421             return alist;
422         alist = alist->next;
423     }
424     return 0;
425 }
426
427 /* if no parm specified in a particular slot, set parm to be "." instead */
428 static void 
429 SetDotDefault(struct cmd_item **aitemp)
430 {
431     struct cmd_item *ti;
432     if (*aitemp) 
433         return;                 /* already has value */
434     /* otherwise, allocate an item representing "." */
435     ti = (struct cmd_item *) malloc(sizeof(struct cmd_item));
436     assert(ti);
437     ti->next = (struct cmd_item *) 0;
438     ti->data = (char *) malloc(2);
439     assert(ti->data);
440     strcpy(ti->data, ".");
441     *aitemp = ti;
442 }
443
444 static void 
445 ChangeList (struct Acl *al, afs_int32 plus, char *aname, afs_int32 arights)
446 {
447     struct AclEntry *tlist;
448     tlist = (plus ? al->pluslist : al->minuslist);
449     tlist = FindList (tlist, aname);
450     if (tlist) {
451         /* Found the item already in the list. */
452         tlist->rights = arights;
453         if (plus)
454             al->nplus -= PruneList(&al->pluslist, al->dfs);
455         else
456             al->nminus -= PruneList(&al->minuslist, al->dfs);
457         return;
458     }
459     /* Otherwise we make a new item and plug in the new data. */
460     tlist = (struct AclEntry *) malloc(sizeof (struct AclEntry));
461     assert(tlist);
462     strcpy(tlist->name, aname);
463     tlist->rights = arights;
464     if (plus) {
465         tlist->next = al->pluslist;
466         al->pluslist = tlist;
467         al->nplus++;
468         if (arights == 0 || arights == -1)
469             al->nplus -= PruneList(&al->pluslist, al->dfs);
470     } else {
471         tlist->next = al->minuslist;
472         al->minuslist = tlist;
473         al->nminus++;
474         if (arights == 0) 
475             al->nminus -= PruneList(&al->minuslist, al->dfs);
476     }
477 }
478
479 static void 
480 ZapList (struct AclEntry *alist)
481 {
482     struct AclEntry *tp, *np;
483     for (tp = alist; tp; tp = np) {
484         np = tp->next;
485         free(tp);
486     }
487 }
488
489 static int 
490 PruneList (struct AclEntry **ae, int dfs)
491 {
492     struct AclEntry **lp;
493     struct AclEntry *te, *ne;
494     afs_int32 ctr;
495     ctr = 0;
496     lp = ae;
497     for(te = *ae;te;te=ne) {
498         if ((!dfs && te->rights == 0) || te->rights == -1) {
499             *lp = te->next;
500             ne = te->next;
501             free(te);
502             ctr++;
503         } else {
504             ne = te->next;
505             lp = &te->next;
506         }
507     }
508     return ctr;
509 }
510
511 static char *
512 SkipLine (char *astr)
513 {
514     while (*astr !='\n') 
515         astr++;
516     astr++;
517     return astr;
518 }
519
520 /*
521  * Create an empty acl, taking into account whether the acl pointed
522  * to by astr is an AFS or DFS acl. Only parse this minimally, so we
523  * can recover from problems caused by bogus ACL's (in that case, always
524  * assume that the acl is AFS: for DFS, the user can always resort to
525  * acl_edit, but for AFS there may be no other way out).
526  */
527 static struct Acl *
528 EmptyAcl(char *astr)
529 {
530     struct Acl *tp;
531     int junk;
532
533     tp = (struct Acl *)malloc(sizeof (struct Acl));
534     assert(tp);
535     tp->nplus = tp->nminus = 0;
536     tp->pluslist = tp->minuslist = 0;
537     tp->dfs = 0;
538     sscanf(astr, "%d dfs:%d %s", &junk, &tp->dfs, tp->cell);
539     return tp;
540 }
541
542 static struct Acl *
543 ParseAcl (char *astr)
544 {
545     int nplus, nminus, i, trights;
546     char tname[MAXNAME];
547     struct AclEntry *first, *last, *tl;
548     struct Acl *ta;
549
550     ta = (struct Acl *) malloc (sizeof (struct Acl));
551     assert(ta);
552     ta->dfs = 0;
553     sscanf(astr, "%d dfs:%d %s", &ta->nplus, &ta->dfs, ta->cell);
554     astr = SkipLine(astr);
555     sscanf(astr, "%d", &ta->nminus);
556     astr = SkipLine(astr);
557
558     nplus = ta->nplus;
559     nminus = ta->nminus;
560
561     last = 0;
562     first = 0;
563     for(i=0;i<nplus;i++) {
564         sscanf(astr, "%100s %d", tname, &trights);
565         astr = SkipLine(astr);
566         tl = (struct AclEntry *) malloc(sizeof (struct AclEntry));
567         assert(tl);
568         if (!first) 
569             first = tl;
570         strcpy(tl->name, tname);
571         tl->rights = trights;
572         tl->next = 0;
573         if (last) 
574             last->next = tl;
575         last = tl;
576     }
577     ta->pluslist = first;
578
579     last = 0;
580     first = 0;
581     for(i=0;i<nminus;i++) {
582         sscanf(astr, "%100s %d", tname, &trights);
583         astr = SkipLine(astr);
584         tl = (struct AclEntry *) malloc(sizeof (struct AclEntry));
585         assert(tl);
586         if (!first) 
587             first = tl;
588         strcpy(tl->name, tname);
589         tl->rights = trights;
590         tl->next = 0;
591         if (last) 
592             last->next = tl;
593         last = tl;
594     }
595     ta->minuslist = first;
596
597     return ta;
598 }
599
600 static int
601 PrintStatus(VolumeStatus *status, char *name, char *motd, char *offmsg)
602 {
603     printf("Volume status for vid = %u named %s is\n",status->Vid, name);
604     if (*offmsg != 0)
605         printf("Current offline message is %s\n",offmsg);
606     if (*motd != 0)
607         printf("Current message of the day is %s\n",motd);
608     printf("Current disk quota is ");
609     if (status->MaxQuota != 0) 
610         printf("%d\n", status->MaxQuota);
611     else 
612         printf("unlimited\n");
613     printf("Current blocks used are %d\n",status->BlocksInUse);
614     printf("The partition has %d blocks available out of %d\n",
615             status->PartBlocksAvail, status->PartMaxBlocks);
616     return 0;
617 }
618
619 static int
620 QuickPrintStatus(VolumeStatus *status, char *name)
621 {
622     double QuotaUsed =0.0;
623     double PartUsed =0.0;
624     int WARN = 0;
625     printf("%-25.25s",name);
626
627     if (status->MaxQuota != 0) {
628         printf("%10d%10d", status->MaxQuota, status->BlocksInUse);
629         QuotaUsed = ((((double)status->BlocksInUse)/status->MaxQuota) * 100.0);
630     } else {
631         printf("no limit%10d", status->BlocksInUse);
632     }
633     if (QuotaUsed > 90.0){
634         printf(" %5.0f%%<<", QuotaUsed);
635         WARN = 1;
636     } else 
637         printf(" %5.0f%%  ", QuotaUsed);
638     PartUsed = (100.0 - ((((double)status->PartBlocksAvail)/status->PartMaxBlocks) * 100.0));
639     if (PartUsed > 97.0){
640         printf(" %9.0f%%<<", PartUsed);
641         WARN = 1;
642     } else 
643         printf(" %9.0f%%  ", PartUsed);
644     if (WARN){
645         printf("  <<WARNING\n");
646     } else 
647         printf("\n");
648     return 0;
649 }
650
651 static int
652 QuickPrintSpace(VolumeStatus *status, char *name)
653 {
654     double PartUsed =0.0;
655     int WARN = 0;
656     printf("%-25.25s",name);
657
658     printf("%10d%10d%10d", status->PartMaxBlocks, status->PartMaxBlocks - status->PartBlocksAvail, status->PartBlocksAvail);
659         
660     PartUsed = (100.0 - ((((double)status->PartBlocksAvail)/status->PartMaxBlocks) * 100.0));
661     if (PartUsed > 90.0){
662         printf(" %4.0f%%<<", PartUsed);
663         WARN = 1;
664     } else 
665         printf(" %4.0f%%  ", PartUsed);
666     if (WARN){
667         printf("  <<WARNING\n");
668     } else 
669         printf("\n");
670     return 0;
671 }
672
673 static char *
674 AclToString(struct Acl *acl)
675 {
676     static char mydata[MAXSIZE];
677     char tstring[MAXSIZE];
678     char dfsstring[30];
679     struct AclEntry *tp;
680     
681     if (acl->dfs) 
682         sprintf(dfsstring, " dfs:%d %s", acl->dfs, acl->cell);
683     else 
684         dfsstring[0] = '\0';
685     sprintf(mydata, "%d%s\n%d\n", acl->nplus, dfsstring, acl->nminus);
686     for (tp = acl->pluslist;tp;tp=tp->next) {
687         sprintf(tstring, "%s %d\n", tp->name, tp->rights);
688         strcat(mydata, tstring);
689     }
690     for (tp = acl->minuslist;tp;tp=tp->next) {
691         sprintf(tstring, "%s %d\n", tp->name, tp->rights);
692         strcat(mydata, tstring);
693     }
694     return mydata;
695 }
696
697 static DWORD IsFreelance(void)
698 {
699     HKEY  parmKey;
700     DWORD code;
701     DWORD dummyLen;
702     DWORD enabled = 0;
703
704     code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSREG_CLT_SVC_PARAM_SUBKEY,
705                          0, (IsWow64()?KEY_WOW64_64KEY:0)|KEY_QUERY_VALUE, &parmKey);
706     if (code == ERROR_SUCCESS) {
707         dummyLen = sizeof(cm_freelanceEnabled);
708         code = RegQueryValueEx(parmKey, "FreelanceClient", NULL, NULL,
709                             (BYTE *) &enabled, &dummyLen);
710         RegCloseKey (parmKey);
711     }
712     return enabled;
713 }
714
715 static const char * NetbiosName(void)
716 {
717     static char buffer[1024] = "AFS";
718     HKEY  parmKey;
719     DWORD code;
720     DWORD dummyLen;
721     DWORD enabled = 0;
722
723     code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSREG_CLT_SVC_PARAM_SUBKEY,
724                          0, (IsWow64()?KEY_WOW64_64KEY:0)|KEY_QUERY_VALUE, &parmKey);
725     if (code == ERROR_SUCCESS) {
726         dummyLen = sizeof(buffer);
727         code = RegQueryValueEx(parmKey, "NetbiosName", NULL, NULL,
728                                buffer, &dummyLen);
729         RegCloseKey (parmKey);
730     } else {
731         strcpy(buffer, "AFS");
732     }
733     return buffer;
734 }
735
736 #define AFSCLIENT_ADMIN_GROUPNAME "AFS Client Admins"
737
738 static BOOL IsAdmin (void)
739 {
740     static BOOL fAdmin = FALSE;
741     static BOOL fTested = FALSE;
742
743     if (!fTested)
744     {
745         /* Obtain the SID for the AFS client admin group.  If the group does
746          * not exist, then assume we have AFS client admin privileges.
747          */
748         PSID psidAdmin = NULL;
749         DWORD dwSize, dwSize2;
750         char pszAdminGroup[ MAX_COMPUTERNAME_LENGTH + sizeof(AFSCLIENT_ADMIN_GROUPNAME) + 2 ];
751         char *pszRefDomain = NULL;
752         SID_NAME_USE snu = SidTypeGroup;
753
754         dwSize = sizeof(pszAdminGroup);
755
756         if (!GetComputerName(pszAdminGroup, &dwSize)) {
757             /* Can't get computer name.  We return false in this case.
758                Retain fAdmin and fTested. This shouldn't happen.*/
759             return FALSE;
760         }
761
762         dwSize = 0;
763         dwSize2 = 0;
764
765         strcat(pszAdminGroup,"\\");
766         strcat(pszAdminGroup, AFSCLIENT_ADMIN_GROUPNAME);
767
768         LookupAccountName(NULL, pszAdminGroup, NULL, &dwSize, NULL, &dwSize2, &snu);
769         /* that should always fail. */
770
771         if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
772             /* if we can't find the group, then we allow the operation */
773             fAdmin = TRUE;
774             return TRUE;
775         }
776
777         if (dwSize == 0 || dwSize2 == 0) {
778             /* Paranoia */
779             fAdmin = TRUE;
780             return TRUE;
781         }
782
783         psidAdmin = (PSID)malloc(dwSize); memset(psidAdmin,0,dwSize);
784         assert(psidAdmin);
785         pszRefDomain = (char *)malloc(dwSize2);
786         assert(pszRefDomain);
787
788         if (!LookupAccountName(NULL, pszAdminGroup, psidAdmin, &dwSize, pszRefDomain, &dwSize2, &snu)) {
789             /* We can't lookup the group now even though we looked it up earlier.  
790                Could this happen? */
791             fAdmin = TRUE;
792         } else {
793             /* Then open our current ProcessToken */
794             HANDLE hToken;
795
796             if (OpenProcessToken (GetCurrentProcess(), TOKEN_QUERY, &hToken))
797             {
798
799                 if (!CheckTokenMembership(hToken, psidAdmin, &fAdmin)) {
800                     /* We'll have to allocate a chunk of memory to store the list of
801                      * groups to which this user belongs; find out how much memory
802                      * we'll need.
803                      */
804                     DWORD dwSize = 0;
805                     PTOKEN_GROUPS pGroups;
806
807                     GetTokenInformation (hToken, TokenGroups, NULL, dwSize, &dwSize);
808
809                     pGroups = (PTOKEN_GROUPS)malloc(dwSize);
810                     assert(pGroups);
811
812                     /* Allocate that buffer, and read in the list of groups. */
813                     if (GetTokenInformation (hToken, TokenGroups, pGroups, dwSize, &dwSize))
814                     {
815                         /* Look through the list of group SIDs and see if any of them
816                          * matches the AFS Client Admin group SID.
817                          */
818                         size_t iGroup = 0;
819                         for (; (!fAdmin) && (iGroup < pGroups->GroupCount); ++iGroup)
820                         {
821                             if (EqualSid (psidAdmin, pGroups->Groups[ iGroup ].Sid)) {
822                                 fAdmin = TRUE;
823                             }
824                         }
825                     }
826
827                     if (pGroups)
828                         free(pGroups);
829                 }
830
831                 /* if do not have permission because we were not explicitly listed
832                  * in the Admin Client Group let's see if we are the SYSTEM account
833                  */
834                 if (!fAdmin) {
835                     PTOKEN_USER pTokenUser;
836                     SID_IDENTIFIER_AUTHORITY SIDAuth = SECURITY_NT_AUTHORITY;
837                     PSID pSidLocalSystem = 0;
838                     DWORD gle;
839
840                     GetTokenInformation(hToken, TokenUser, NULL, 0, &dwSize);
841
842                     pTokenUser = (PTOKEN_USER)malloc(dwSize);
843                     assert(pTokenUser);
844
845                     if (!GetTokenInformation(hToken, TokenUser, pTokenUser, dwSize, &dwSize))
846                         gle = GetLastError();
847
848                     if (AllocateAndInitializeSid( &SIDAuth, 1,
849                                                   SECURITY_LOCAL_SYSTEM_RID,
850                                                   0, 0, 0, 0, 0, 0, 0,
851                                                   &pSidLocalSystem))
852                     {
853                         if (EqualSid(pTokenUser->User.Sid, pSidLocalSystem)) {
854                             fAdmin = TRUE;
855                         }
856
857                         FreeSid(pSidLocalSystem);
858                     }
859
860                     if ( pTokenUser )
861                         free(pTokenUser);
862                 }
863             }
864         }
865
866         free(psidAdmin);
867         free(pszRefDomain);
868
869         fTested = TRUE;
870     }
871
872     return fAdmin;
873 }
874
875 static int
876 SetACLCmd(struct cmd_syndesc *as, void *arock)
877 {
878     afs_int32 code;
879     struct ViceIoctl blob;
880     struct Acl *ta = 0;
881     struct cmd_item *ti, *ui;
882     int plusp;
883     afs_int32 rights;
884     int clear;
885     int idf = getidf(as, parm_setacl_id);
886
887     int error = 0;
888
889     if (as->parms[2].items)
890         clear = 1;
891     else
892         clear = 0;
893     plusp = !(as->parms[3].items);
894     for(ti=as->parms[0].items; ti;ti=ti->next) {
895         blob.out_size = MAXSIZE;
896         blob.in_size = idf;
897         blob.in = blob.out = space;
898         code = pioctl(ti->data, VIOCGETAL, &blob, 1);
899         if (code) {
900             Die(errno, ti->data);
901             error = 1;
902             continue;
903         }
904         if (ta)
905             ZapAcl(ta);
906         ta = ParseAcl(space);
907         if (!plusp && ta->dfs) {
908             fprintf(stderr,
909                     "fs: %s: you may not use the -negative switch with DFS acl's.\n%s",
910                     ti->data,
911                     "(you may specify \"null\" to revoke all rights, however)\n");
912             error = 1;
913             continue;
914         }
915         if (ta)
916             ZapAcl(ta);
917         if (clear) 
918             ta = EmptyAcl(space);
919         else 
920             ta = ParseAcl(space);
921         CleanAcl(ta, ti->data);
922         for(ui=as->parms[1].items; ui; ui=ui->next->next) {
923             enum rtype rtype;
924             if (!ui->next) {
925                 fprintf(stderr,
926                         "%s: Missing second half of user/access pair.\n", pn);
927                 ZapAcl(ta);
928                 return 1;
929             }
930             rights = Convert(ui->next->data, ta->dfs, &rtype);
931             if (rtype == destroy && !ta->dfs) {
932                 struct AclEntry *tlist;
933
934                 tlist = (plusp ? ta->pluslist : ta->minuslist);
935                 if (!FindList(tlist, ui->data))
936                     continue;
937             }
938             if (rtype == deny && !ta->dfs) 
939                 plusp = 0;
940             if (rtype == destroy && ta->dfs) 
941                 rights = -1;
942             ChangeList(ta, plusp, ui->data, rights);
943         }
944         blob.in = AclToString(ta);
945         blob.out_size=0;
946         blob.in_size = 1+(long)strlen(blob.in);
947         code = pioctl(ti->data, VIOCSETAL, &blob, 1);
948         if (code) {
949             if (errno == EINVAL) {
950                 if (ta->dfs) {
951                     static char *fsenv = 0;
952                     if (!fsenv) {
953                         fsenv = (char *)getenv("FS_EXPERT");
954                     }
955                     fprintf(stderr, "fs: \"Invalid argument\" was returned when you tried to store a DFS access list.\n");
956                     if (!fsenv) {
957                         fprintf(stderr,
958     "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
959     "\nPossible reasons for this include:\n\n",                     
960     " -You may have specified an inappropriate combination of rights.\n",
961     "  For example, some DFS-supported filesystems may not allow you to\n",
962     "  drop the \"c\" right from \"user_obj\".\n\n",
963     " -A mask_obj may be required (it is likely required by the underlying\n",
964     "  filesystem if you try to set anything other than the basic \"user_obj\"\n",
965     "  \"mask_obj\", or \"group_obj\" entries). Unlike acl_edit, the fs command\n",
966     "  does not automatically create or update the mask_obj. Try setting\n",
967     "  the rights \"mask_obj all\" with \"fs sa\" before adding any explicit\n",
968     "  users or groups. You can do this with a single command, such as\n",
969     "  \"fs sa mask_obj all user:somename read\"\n\n",
970     " -A specified user or group may not exist.\n\n",
971     " -You may have tried to delete \"user_obj\", \"group_obj\", or \"other_obj\".\n",
972     "  This is probably not allowed by the underlying file system.\n\n",
973     " -If you add a user or group to a DFS ACL, remember that it must be\n",
974     "  fully specified as \"user:username\" or \"group:groupname\". In addition, there\n",
975     "  may be local requirements on the format of the user or group name.\n",
976     "  Check with your cell administrator.\n\n",                            
977     " -Or numerous other possibilities. It would be great if we could be more\n",
978     "  precise about the actual problem, but for various reasons, this is\n",
979     "  impractical via this interface.  If you can't figure it out, you\n",
980     "  might try logging into a DCE-equipped machine and use acl_edit (or\n",
981     "  whatever is provided). You may get better results. Good luck!\n\n",
982     " (You may inhibit this message by setting \"FS_EXPERT\" in your environment)\n");
983                     }
984                 } else {
985                     fprintf(stderr,
986                             "%s: Invalid argument, possible reasons include:\n", 
987                              pn);
988                     fprintf(stderr,"\t-File not in AFS\n");
989                     fprintf(stderr,
990                             "\t-Too many users on access control list\n");
991                     fprintf(stderr,
992                             "\t-Tried to add non-existent user to access control list\n");
993                 }
994             } else {
995                 Die(errno, ti->data);
996             }
997             error = 1;
998         }
999     }
1000     if (ta)
1001         ZapAcl(ta);
1002     return error;
1003 }
1004
1005 static int 
1006 CopyACLCmd(struct cmd_syndesc *as, void *arock)
1007 {
1008     afs_int32 code;
1009     struct ViceIoctl blob;
1010     struct Acl *fa, *ta = 0;
1011     struct AclEntry *tp;
1012     struct cmd_item *ti;
1013     int clear;
1014     int idf = getidf(as, parm_copyacl_id);
1015     int error = 0;
1016
1017     if (as->parms[2].items) 
1018         clear=1;
1019     else 
1020         clear=0;
1021     blob.out_size = MAXSIZE;
1022     blob.in_size = idf;
1023     blob.in = blob.out = space;
1024     code = pioctl(as->parms[0].items->data, VIOCGETAL, &blob, 1);
1025     if (code) {
1026         Die(errno, as->parms[0].items->data);
1027         return 1;
1028     }
1029     fa = ParseAcl(space);
1030     CleanAcl(fa, as->parms[0].items->data);
1031     for (ti=as->parms[1].items; ti;ti=ti->next) {
1032         blob.out_size = MAXSIZE;
1033         blob.in_size = idf;
1034         blob.in = blob.out = space;
1035         code = pioctl(ti->data, VIOCGETAL, &blob, 1);
1036         if (code) {
1037             Die(errno, ti->data);
1038             error = 1;
1039             continue;
1040         }
1041         if (ta)
1042             ZapAcl(ta);
1043         if (clear) 
1044             ta = EmptyAcl(space);
1045         else 
1046             ta = ParseAcl(space);
1047         CleanAcl(ta, ti->data);
1048         if (ta->dfs != fa->dfs) {
1049             fprintf(stderr, 
1050                     "%s: incompatible file system types: acl not copied to %s; aborted\n", 
1051                     pn, ti->data);
1052             error = 1;
1053             continue;
1054         }
1055         if (ta->dfs) {
1056             if (! clear && strcmp(ta->cell, fa->cell) != 0) {
1057                 fprintf(stderr, 
1058                         "%s: default DCE cell differs for file %s: use \"-clear\" switch; acl not merged\n", 
1059                         pn, ti->data);
1060                 error = 1;
1061                 continue;
1062             }
1063             strcpy(ta->cell, fa->cell);
1064         }
1065         for (tp = fa->pluslist;tp;tp=tp->next) 
1066             ChangeList(ta, 1, tp->name, tp->rights);
1067         for (tp = fa->minuslist;tp;tp=tp->next) 
1068             ChangeList(ta, 0, tp->name, tp->rights);
1069         blob.in = AclToString(ta);
1070         blob.out_size=0;
1071         blob.in_size = 1+(long)strlen(blob.in);
1072         code = pioctl(ti->data, VIOCSETAL, &blob, 1);
1073         if (code) {
1074             if (errno == EINVAL) {
1075                 fprintf(stderr,
1076                         "%s: Invalid argument, possible reasons include:\n", pn);
1077                 fprintf(stderr,"\t-File not in AFS\n");
1078             } else {
1079                 Die(errno, ti->data);
1080             }
1081             error = 1;
1082         }
1083     } 
1084     if (ta)
1085         ZapAcl(ta);
1086     ZapAcl(fa);
1087     return error;
1088 }
1089
1090 /* pioctl() call to get the cellname of a pathname */
1091 static afs_int32
1092 GetCell(char *fname, char *cellname)
1093 {
1094     afs_int32 code;
1095     struct ViceIoctl blob;
1096
1097     blob.in_size = 0;
1098     blob.out_size = MAXCELLCHARS;
1099     blob.out = cellname;
1100
1101     code = pioctl(fname, VIOC_FILE_CELL_NAME, &blob, 1);
1102     return code;
1103 }
1104
1105 /* Check if a username is valid: If it contains only digits (or a
1106  * negative sign), then it might be bad.  We then query the ptserver
1107  * to see.
1108  */
1109 static int
1110 BadName(char *aname, char *fname)
1111 {
1112     afs_int32 tc, code, id;
1113     char *nm;
1114     char cell[MAXCELLCHARS];
1115     char confDir[257];
1116
1117     for ( nm = aname; tc = *nm; nm++) {
1118         /* all must be '-' or digit to be bad */
1119         if (tc != '-' && (tc < '0' || tc > '9'))
1120             return 0;
1121     }
1122
1123     /* Go to the PRDB and see if this all number username is valid */
1124     code = GetCell(fname, cell);
1125     if (code)
1126         return 0;
1127
1128     cm_GetConfigDir(confDir, sizeof(confDir));
1129
1130     pr_Initialize(1, confDir, cell);
1131     code = pr_SNameToId(aname, &id);
1132     pr_End();
1133
1134     /* 1=>Not-valid; 0=>Valid */
1135     return ((!code && (id == ANONYMOUSID)) ? 1 : 0);
1136 }
1137
1138
1139 /* clean up an access control list of its bad entries; return 1 if we made
1140    any changes to the list, and 0 otherwise */
1141 static int 
1142 CleanAcl(struct Acl *aa, char *fname)
1143 {
1144     struct AclEntry *te, **le, *ne;
1145     int changes;
1146
1147     /* Don't correct DFS ACL's for now */
1148     if (aa->dfs)
1149         return 0;
1150
1151     /* prune out bad entries */
1152     changes = 0;            /* count deleted entries */
1153     le = &aa->pluslist;
1154     for(te = aa->pluslist; te; te=ne) {
1155         ne = te->next;
1156         if (BadName(te->name, fname)) {
1157             /* zap this dude */
1158             *le = te->next;
1159             aa->nplus--;
1160             free(te);
1161             changes++;
1162         } else {
1163             le = &te->next;
1164         }
1165     }
1166     le = &aa->minuslist;
1167     for(te = aa->minuslist; te; te=ne) {
1168         ne = te->next;
1169         if (BadName(te->name, fname)) {
1170             /* zap this dude */
1171             *le = te->next;
1172             aa->nminus--;
1173             free(te);
1174             changes++;
1175         } else {
1176             le = &te->next;
1177         }
1178     }
1179     return changes;
1180 }
1181
1182
1183 /* clean up an acl to not have bogus entries */
1184 static int 
1185 CleanACLCmd(struct cmd_syndesc *as, void *arock)
1186 {
1187     afs_int32 code;
1188     struct Acl *ta = 0;
1189     struct ViceIoctl blob;
1190     int changes;
1191     struct cmd_item *ti;
1192     struct AclEntry *te;
1193     int error = 0;
1194
1195     SetDotDefault(&as->parms[0].items);
1196     for(ti=as->parms[0].items; ti; ti=ti->next) {
1197         blob.out_size = MAXSIZE;
1198         blob.in_size = 0;
1199         blob.out = space;
1200         code = pioctl(ti->data, VIOCGETAL, &blob, 1);
1201         if (code) {
1202             Die(errno, ti->data);
1203             error = 1;
1204             continue;
1205         }
1206         if (ta)
1207             ZapAcl(ta);
1208         ta = ParseAcl(space);
1209
1210         if (ta->dfs) {
1211             fprintf(stderr,
1212                     "%s: cleanacl is not supported for DFS access lists.\n",
1213                     pn);
1214             error = 1;
1215             continue;
1216         }
1217
1218         changes = CleanAcl(ta, ti->data);
1219
1220         if (changes) {
1221             /* now set the acl */
1222             blob.in=AclToString(ta);
1223             blob.in_size = (long)strlen(blob.in)+1;
1224             blob.out_size = 0;
1225             code = pioctl(ti->data, VIOCSETAL, &blob, 1);
1226             if (code) {
1227                 if (errno == EINVAL) {
1228                     fprintf(stderr,
1229                             "%s: Invalid argument, possible reasons include\n", 
1230                              pn);
1231                     fprintf(stderr,"%s: File not in vice or\n", pn);
1232                     fprintf(stderr,
1233                             "%s: Too many users on access control list or\n", 
1234                             pn);
1235                 } else {
1236                     Die(errno, ti->data);
1237                 }
1238                 error = 1;
1239                 continue;
1240             }
1241
1242             /* now list the updated acl */
1243             printf("Access list for %s is now\n", ti->data);
1244             if (ta->nplus > 0) {
1245                 if (!ta->dfs) 
1246                     printf("Normal rights:\n");
1247                 for(te = ta->pluslist;te;te=te->next) {
1248                     printf("  %s ", te->name);
1249                     PRights(te->rights, ta->dfs);
1250                     printf("\n");
1251                 }
1252             }
1253             if (ta->nminus > 0) {
1254                 printf("Negative rights:\n");
1255                 for(te = ta->minuslist;te;te=te->next) {
1256                     printf("  %s ", te->name);
1257                     PRights(te->rights, ta->dfs);
1258                     printf("\n");
1259                 }
1260             }
1261             if (ti->next) 
1262                 printf("\n");
1263         } else
1264             printf("Access list for %s is fine.\n", ti->data);
1265     }
1266     if (ta)
1267         ZapAcl(ta);
1268     return error;
1269 }
1270
1271 static int 
1272 ListACLCmd(struct cmd_syndesc *as, void *arock) 
1273 {
1274     afs_int32 code;
1275     struct Acl *ta = 0;
1276     struct ViceIoctl blob;
1277     struct AclEntry *te;
1278     struct cmd_item *ti;
1279     int idf = getidf(as, parm_listacl_id);
1280     int error = 0;
1281
1282     SetDotDefault(&as->parms[0].items);
1283     for(ti=as->parms[0].items; ti; ti=ti->next) {
1284         char separator;
1285         blob.out_size = MAXSIZE;
1286         blob.in_size = idf;
1287         blob.in = blob.out = space;
1288         code = pioctl(ti->data, VIOCGETAL, &blob, 1);
1289         if (code) {
1290             Die(errno, ti->data);
1291             error = 1;
1292             continue;
1293         }
1294         ta = ParseAcl(space);
1295         switch (ta->dfs) {
1296           case 0:
1297             printf("Access list for %s is\n", ti->data);
1298             break;
1299           case 1:
1300             printf("DFS access list for %s is\n", ti->data);
1301             break;
1302           case 2:
1303             printf("DFS initial directory access list of %s is\n", ti->data);
1304             break;
1305           case 3:
1306             printf("DFS initial file access list of %s is\n", ti->data);
1307             break;
1308         }
1309         if (ta->dfs) {
1310             printf("  Default cell = %s\n", ta->cell);
1311         }
1312         separator = ta->dfs? DFS_SEPARATOR : ' ';
1313         if (ta->nplus > 0) {
1314             if (!ta->dfs) 
1315                 printf("Normal rights:\n");
1316             for(te = ta->pluslist;te;te=te->next) {
1317                 printf("  %s%c", te->name, separator);
1318                 PRights(te->rights, ta->dfs);
1319                 printf("\n");
1320             }
1321         }
1322         if (ta->nminus > 0) {
1323             printf("Negative rights:\n");
1324             for(te = ta->minuslist;te;te=te->next) {
1325                 printf("  %s ", te->name);
1326                 PRights(te->rights, ta->dfs);
1327                 printf("\n");
1328             }
1329         }
1330         if (ti->next) 
1331             printf("\n");
1332         ZapAcl(ta);
1333     }
1334     return error;
1335 }
1336
1337 static int
1338 FlushAllCmd(struct cmd_syndesc *as, void *arock)
1339 {
1340     afs_int32 code;
1341     struct ViceIoctl blob;
1342
1343     blob.in_size = blob.out_size = 0;
1344     code = pioctl(NULL, VIOC_FLUSHALL, &blob, 0);
1345     if (code) {
1346         fprintf(stderr, "Error flushing all ");
1347         return 1;
1348     }
1349     return 0;
1350 }
1351
1352 static int
1353 FlushVolumeCmd(struct cmd_syndesc *as, void *arock)
1354 {
1355     afs_int32 code;
1356     struct ViceIoctl blob;
1357     struct cmd_item *ti;
1358     int error = 0;
1359
1360     SetDotDefault(&as->parms[0].items);
1361     for(ti=as->parms[0].items; ti; ti=ti->next) {
1362         blob.in_size = blob.out_size = 0;
1363         code = pioctl(ti->data, VIOC_FLUSHVOLUME, &blob, 0);
1364         if (code) {
1365             fprintf(stderr, "Error flushing volume ");
1366             perror(ti->data);
1367             error = 1;
1368             continue;
1369         }
1370     }
1371     return error;
1372 }
1373
1374 static int 
1375 FlushCmd(struct cmd_syndesc *as, void *arock) 
1376 {
1377     afs_int32 code;
1378     struct ViceIoctl blob;
1379     struct cmd_item *ti;
1380     int error = 0;
1381     int literal = 0;
1382     cm_ioctlQueryOptions_t options;
1383
1384     if (as->parms[1].items)
1385         literal = 1;
1386     
1387     for(ti=as->parms[0].items; ti; ti=ti->next) {
1388         /* once per file */
1389         memset(&options, 0, sizeof(options));
1390         options.size = sizeof(options);
1391         options.field_flags |= CM_IOCTL_QOPTS_FIELD_LITERAL;
1392         options.literal = literal;
1393         blob.in_size = options.size;    /* no variable length data */
1394         blob.in = &options;
1395
1396         blob.out_size = 0;
1397         code = pioctl(ti->data, VIOCFLUSH, &blob, 0);
1398         if (code) {
1399             if (errno == EMFILE) {
1400                 fprintf(stderr, "%s: Can't flush active file %s\n", pn, 
1401                         ti->data);
1402             } else {
1403                 fprintf(stderr, "%s: Error flushing file ", pn);
1404                 perror(ti->data);
1405             }
1406             error = 1;
1407             continue;
1408         }
1409     }
1410     return error;
1411 }
1412
1413 /* all this command does is repackage its args and call SetVolCmd */
1414 static int
1415 SetQuotaCmd(struct cmd_syndesc *as, void *arock) {
1416     struct cmd_syndesc ts;
1417
1418     /* copy useful stuff from our command slot; we may later have to reorder */
1419     memcpy(&ts, as, sizeof(ts));        /* copy whole thing */
1420     return SetVolCmd(&ts, arock);
1421 }
1422
1423 static int
1424 SetVolCmd(struct cmd_syndesc *as, void *arock) {
1425     afs_int32 code;
1426     struct ViceIoctl blob;
1427     struct cmd_item *ti;
1428     struct VolumeStatus *status;
1429     char *motd, *offmsg, *input;
1430     int error = 0;
1431
1432     SetDotDefault(&as->parms[0].items);
1433     for(ti=as->parms[0].items; ti; ti=ti->next) {
1434         /* once per file */
1435         blob.out_size = MAXSIZE;
1436         blob.in_size = sizeof(*status) + 3;     /* for the three terminating nulls */
1437         blob.out = space;
1438         blob.in = space;
1439         status = (VolumeStatus *)space;
1440         status->MinQuota = status->MaxQuota = -1;
1441         motd = offmsg = NULL;
1442         if (as->parms[1].items) {
1443             code = util_GetInt32(as->parms[1].items->data, &status->MaxQuota);
1444             if (code) {
1445                 fprintf(stderr,"%s: bad integer specified for quota.\n", pn);
1446                 error = 1;
1447                 continue;
1448             }
1449         }
1450         if (as->parms[2].items) 
1451             motd = as->parms[2].items->data;
1452         if (as->parms[3].items) 
1453             offmsg = as->parms[3].items->data;
1454         input = (char *)status + sizeof(*status);
1455         *(input++) = '\0';      /* never set name: this call doesn't change vldb */
1456         if(offmsg) {
1457             if (strlen(offmsg) >= VMSGSIZE) {
1458                 fprintf(stderr,"%s: message must be shorter than %d characters\n",
1459                          pn, VMSGSIZE);
1460                 error = 1;
1461                 continue;
1462             }
1463             strcpy(input,offmsg);
1464             blob.in_size += (long)strlen(offmsg);
1465             input += strlen(offmsg) + 1;
1466         } else 
1467             *(input++) = '\0';
1468         if(motd) {
1469             if (strlen(motd) >= VMSGSIZE) {
1470                 fprintf(stderr,"%s: message must be shorter than %d characters\n",
1471                          pn, VMSGSIZE);
1472                 return code;
1473             }
1474             strcpy(input,motd);
1475             blob.in_size += (long)strlen(motd);
1476             input += strlen(motd) + 1;
1477         } else 
1478             *(input++) = '\0';
1479         code = pioctl(ti->data,VIOCSETVOLSTAT, &blob, 1);
1480         if (code) {
1481             Die(errno, ti->data);
1482             error = 1;
1483         }
1484     }
1485     return error;
1486 }
1487
1488 /* values match cache manager File Types */
1489 static char *
1490 filetypestr(afs_uint32 type)
1491 {
1492     char * s = "Object";
1493
1494     switch (type) {
1495     case 1:     /* file */
1496         s = "File";
1497         break;
1498     case 2:
1499         s = "Directory";
1500         break;
1501     case 3:
1502         s = "Symlink";
1503         break;
1504     case 4:
1505         s = "Mountpoint";
1506         break;
1507     case 5:
1508         s = "DfsLink";
1509         break;
1510     }
1511     return s;
1512 }
1513
1514 static int 
1515 ExamineCmd(struct cmd_syndesc *as, void *arock)
1516 {
1517     afs_int32 code;
1518     struct ViceIoctl blob;
1519     struct cmd_item *ti;
1520     struct VolumeStatus *status;
1521     char *name, *offmsg, *motd;
1522     int error = 0;
1523     int literal = 0;
1524     cm_ioctlQueryOptions_t options;
1525
1526     if (as->parms[1].items)
1527         literal = 1;
1528
1529     SetDotDefault(&as->parms[0].items);
1530     for(ti=as->parms[0].items; ti; ti=ti->next) {
1531         cm_fid_t fid;
1532         afs_uint32 filetype;
1533         afs_uint32 owner[2];
1534         char cell[MAXCELLCHARS];
1535
1536         /* once per file */
1537         memset(&fid, 0, sizeof(fid));
1538         memset(&options, 0, sizeof(options));
1539         filetype = 0;
1540         options.size = sizeof(options);
1541         options.field_flags |= CM_IOCTL_QOPTS_FIELD_LITERAL;
1542         options.literal = literal;
1543         blob.in_size = options.size;    /* no variable length data */
1544         blob.in = &options;
1545
1546         blob.out_size = sizeof(cm_fid_t);
1547         blob.out = (char *) &fid;
1548         if (0 == pioctl(ti->data, VIOCGETFID, &blob, 1)) {
1549             options.field_flags |= CM_IOCTL_QOPTS_FIELD_FID;
1550             options.fid = fid;
1551         } else {
1552             Die(errno, ti->data);
1553             error = 1;
1554             continue;
1555         }
1556
1557         blob.out_size = sizeof(filetype);
1558         blob.out = &filetype;
1559
1560         code = pioctl(ti->data, VIOC_GETFILETYPE, &blob, 1);
1561
1562         blob.out_size = MAXCELLCHARS;
1563         blob.out = cell;
1564
1565         code = pioctl(ti->data, VIOC_FILE_CELL_NAME, &blob, 1);
1566         printf("%s %s (%u.%u.%u) contained in cell %s\n",
1567                 filetypestr(filetype),
1568                 ti->data, fid.volume, fid.vnode, fid.unique,
1569                 code ? "unknown-cell" : cell);
1570
1571         blob.out_size = 2 * sizeof(afs_uint32);
1572         blob.out = (char *) &owner;
1573         if (0 == pioctl(ti->data, VIOCGETOWNER, &blob, 1)) {
1574             char oname[PR_MAXNAMELEN] = "(unknown)";
1575             char confDir[257];
1576
1577             /* Go to the PRDB and see if this all number username is valid */
1578             cm_GetConfigDir(confDir, sizeof(confDir));
1579
1580             pr_Initialize(1, confDir, cell);
1581             pr_SIdToName(owner[0], oname);
1582             printf("Owner %s (%u) Group %u\n", oname, owner[0], owner[1]);
1583         }
1584
1585         blob.out = space;
1586         blob.out_size = MAXSIZE;
1587         code = pioctl(ti->data, VIOCGETVOLSTAT, &blob, 1);
1588         if (code == 0) {
1589             status = (VolumeStatus *)space;
1590             name = (char *)status + sizeof(*status);
1591             offmsg = name + strlen(name) + 1;
1592             motd = offmsg + strlen(offmsg) + 1;
1593
1594             PrintStatus(status, name, motd, offmsg);
1595         } else {
1596             Die(errno, ti->data);
1597         }
1598
1599         errno = 0;
1600         code = pioctl(ti->data, VIOC_PATH_AVAILABILITY, &blob, 1);
1601         switch (errno) {
1602         case 0:
1603             printf("Volume is online\n");
1604             break;
1605         case ENXIO:
1606             printf("Volume is offline\n");
1607             break;
1608         case ENOSYS:
1609             printf("All Volume servers are down\n");
1610             break;
1611         case EBUSY:
1612             printf("All volume servers are busy\n");
1613             break;
1614         default:
1615             printf("Unknown volume state\n");
1616             Die(errno, ti->data);
1617         }
1618         printf("\n");
1619     }
1620     return error;
1621 }
1622
1623 static int
1624 ListQuotaCmd(struct cmd_syndesc *as, void *arock) 
1625 {
1626     afs_int32 code;
1627     struct ViceIoctl blob;
1628     struct cmd_item *ti;
1629     struct VolumeStatus *status;
1630     char *name;
1631
1632     int error = 0;
1633     
1634     printf("%-25s%-10s%-10s%-7s%-13s\n", 
1635            "Volume Name", "     Quota", "      Used", "  %Used", "    Partition");
1636     SetDotDefault(&as->parms[0].items);
1637     for(ti=as->parms[0].items; ti; ti=ti->next) {
1638         /* once per file */
1639         blob.out_size = MAXSIZE;
1640         blob.in_size = 0;
1641         blob.out = space;
1642         code = pioctl(ti->data, VIOCGETVOLSTAT, &blob, 1);
1643         if (code) {
1644             Die(errno, ti->data);
1645             error = 1;
1646             continue;
1647         }
1648         status = (VolumeStatus *)space;
1649         name = (char *)status + sizeof(*status);
1650         QuickPrintStatus(status, name);
1651     }
1652     return error;
1653 }
1654
1655 static int
1656 WhereIsCmd(struct cmd_syndesc *as, void *arock)
1657 {
1658     afs_int32 code;
1659     struct ViceIoctl blob;
1660     struct cmd_item *ti;
1661     int j;
1662     afs_int32 *hosts;
1663     char *tp;
1664     int error = 0;
1665     int literal = 0;
1666     cm_ioctlQueryOptions_t options;
1667
1668     if (as->parms[1].items)
1669         literal = 1;
1670     
1671     SetDotDefault(&as->parms[0].items);
1672     for(ti=as->parms[0].items; ti; ti=ti->next) {
1673         cm_fid_t fid;
1674         afs_uint32 filetype;
1675
1676         /* once per file */
1677         memset(&fid, 0, sizeof(fid));
1678         memset(&options, 0, sizeof(options));
1679         filetype = 0;
1680         options.size = sizeof(options);
1681         options.field_flags |= CM_IOCTL_QOPTS_FIELD_LITERAL;
1682         options.literal = literal;
1683         blob.in_size = options.size;    /* no variable length data */
1684         blob.in = &options;
1685         
1686         blob.out_size = sizeof(cm_fid_t);
1687         blob.out = (char *) &fid;
1688         if (0 == pioctl(ti->data, VIOCGETFID, &blob, 1)) {
1689             options.field_flags |= CM_IOCTL_QOPTS_FIELD_FID;
1690             options.fid = fid;
1691         } else {
1692             Die(errno, ti->data);
1693             error = 1;
1694             continue;
1695         }
1696
1697         blob.out_size = sizeof(filetype);
1698         blob.out = &filetype;
1699
1700         code = pioctl(ti->data, VIOC_GETFILETYPE, &blob, 1);
1701
1702         blob.out_size = MAXSIZE;
1703         blob.out = space;
1704         memset(space, 0, sizeof(space));
1705         code = pioctl(ti->data, VIOCWHEREIS, &blob, 1);
1706         if (code) {
1707             Die(errno, ti->data);
1708             error = 1;
1709             continue;
1710         }
1711         hosts = (afs_int32 *) space;
1712         printf("%s %s is on host%s ", 
1713                 filetypestr(filetype),
1714                 ti->data,
1715                 (hosts[0] && !hosts[1]) ? "": "s");
1716         for(j=0; j<MAXHOSTS; j++) {
1717             if (hosts[j] == 0) 
1718                 break;
1719             tp = hostutil_GetNameByINet(hosts[j]);
1720             printf("%s ", tp);
1721         }
1722         printf("\n");
1723     }
1724     return error;
1725 }
1726
1727
1728 static int
1729 DiskFreeCmd(struct cmd_syndesc *as, void *arock)
1730 {
1731     afs_int32 code;
1732     struct ViceIoctl blob;
1733     struct cmd_item *ti;
1734     char *name;
1735     struct VolumeStatus *status;
1736     int error = 0;
1737     
1738     printf("%-25s%-10s%-10s%-10s%-6s\n", "Volume Name", "    kbytes",
1739            "      used", "     avail", " %used");
1740     SetDotDefault(&as->parms[0].items);
1741     for(ti=as->parms[0].items; ti; ti=ti->next) {
1742         /* once per file */
1743         blob.out_size = MAXSIZE;
1744         blob.in_size = 0;
1745         blob.out = space;
1746         code = pioctl(ti->data, VIOCGETVOLSTAT, &blob, 1);
1747         if (code) {
1748             Die(errno, ti->data);
1749             error = 1;
1750             continue;
1751         }
1752         status = (VolumeStatus *)space;
1753         name = (char *)status + sizeof(*status);
1754         QuickPrintSpace(status, name);
1755     }
1756     return error;
1757 }
1758
1759 static int
1760 QuotaCmd(struct cmd_syndesc *as, void *arock)
1761 {
1762     afs_int32 code;
1763     struct ViceIoctl blob;
1764     struct cmd_item *ti;
1765     double quotaPct;
1766     struct VolumeStatus *status;
1767     int error = 0;
1768     
1769     SetDotDefault(&as->parms[0].items);
1770     for(ti=as->parms[0].items; ti; ti=ti->next) {
1771         /* once per file */
1772         blob.out_size = MAXSIZE;
1773         blob.in_size = 0;
1774         blob.out = space;
1775         code = pioctl(ti->data, VIOCGETVOLSTAT, &blob, 1);
1776         if (code) {
1777             Die(errno, ti->data);
1778             error = 1;
1779             continue;
1780         }
1781         status = (VolumeStatus *)space;
1782         if (status->MaxQuota) 
1783             quotaPct = ((((double)status->BlocksInUse)/status->MaxQuota) * 100.0);
1784         else 
1785             quotaPct = 0.0;
1786         printf("%2.0f%% of quota used.\n", quotaPct);
1787     }
1788     return error;
1789 }
1790
1791 static int
1792 ListMountCmd(struct cmd_syndesc *as, void *arock)
1793 {
1794     afs_int32 code;
1795     struct ViceIoctl blob;
1796     struct cmd_item *ti;
1797     char orig_name[1024];               /*Original name, may be modified*/
1798     char true_name[1024];               /*``True'' dirname (e.g., symlink target)*/
1799     char parent_dir[1024];              /*Parent directory of true name*/
1800     char *last_component;       /*Last component of true name*/
1801 #ifndef WIN32
1802     struct stat statbuff;               /*Buffer for status info*/
1803 #endif /* not WIN32 */
1804 #ifndef WIN32
1805     int link_chars_read;                /*Num chars read in readlink()*/
1806 #endif /* not WIN32 */
1807     int thru_symlink;                   /*Did we get to a mount point via a symlink?*/
1808     
1809     int error = 0;
1810     for(ti=as->parms[0].items; ti; ti=ti->next) {
1811         /* once per file */
1812         thru_symlink = 0;
1813 #ifdef WIN32
1814         strcpy(orig_name, ti->data);
1815 #else /* not WIN32 */
1816         sprintf(orig_name, "%s%s",
1817                 (ti->data[0] == '/') ? "" : "./",
1818                 ti->data);
1819 #endif /* not WIN32 */
1820
1821 #ifndef WIN32
1822         if (lstat(orig_name, &statbuff) < 0) {
1823             /* if lstat fails, we should still try the pioctl, since it
1824              * may work (for example, lstat will fail, but pioctl will
1825              * work if the volume of offline (returning ENODEV). */
1826             statbuff.st_mode = S_IFDIR; /* lie like pros */
1827         }
1828
1829         /*
1830          * The lstat succeeded.  If the given file is a symlink, substitute
1831          * the file name with the link name.
1832          */
1833         if ((statbuff.st_mode & S_IFMT) == S_IFLNK) {
1834             thru_symlink = 1;
1835             /*
1836              * Read name of resolved file.
1837              */
1838             link_chars_read = readlink(orig_name, true_name, 1024);
1839             if (link_chars_read <= 0) {
1840                 fprintf(stderr,
1841                         "%s: Can't read target name for '%s' symbolic link!\n",
1842                        pn, orig_name);
1843                 error = 1;
1844                 continue;
1845             }
1846
1847             /*
1848              * Add a trailing null to what was read, bump the length.
1849              */
1850             true_name[link_chars_read++] = 0;
1851
1852             /*
1853              * If the symlink is an absolute pathname, we're fine.  Otherwise, we
1854              * have to create a full pathname using the original name and the
1855              * relative symlink name.  Find the rightmost slash in the original
1856              * name (we know there is one) and splice in the symlink value.
1857              */
1858             if (true_name[0] != '\\') {
1859                 last_component = (char *) strrchr(orig_name, '\\');
1860                 strcpy(++last_component, true_name);
1861                 strcpy(true_name, orig_name);
1862             }
1863         } else
1864             strcpy(true_name, orig_name);
1865 #else   /* WIN32 */
1866         strcpy(true_name, orig_name);
1867 #endif /* WIN32 */
1868
1869         /*
1870          * Find rightmost slash, if any.
1871          */
1872 #ifdef WIN32
1873         last_component = (char *) strrchr(true_name, '\\');
1874         if (!last_component)
1875 #endif /* WIN32 */
1876             last_component = (char *) strrchr(true_name, '/');
1877         if (last_component) {
1878             /*
1879              * Found it.  Designate everything before it as the parent directory,
1880              * everything after it as the final component.
1881              */
1882             strncpy(parent_dir, true_name, last_component - true_name + 1);
1883             parent_dir[last_component - true_name + 1] = 0;
1884             last_component++;   /*Skip the slash*/
1885 #ifdef WIN32
1886             if (!InAFS(parent_dir)) {
1887                 const char * nbname = NetbiosName();
1888                 int len = (int)strlen(nbname);
1889
1890                 if (parent_dir[0] == '\\' && parent_dir[1] == '\\' &&
1891                     parent_dir[len+2] == '\\' &&
1892                     parent_dir[len+3] == '\0' &&
1893                     !strnicmp(nbname,&parent_dir[2],len))
1894                 {
1895                     sprintf(parent_dir,"\\\\%s\\all\\", nbname);
1896                 }
1897             }
1898 #endif
1899         } else {
1900             /*
1901              * No slash appears in the given file name.  Set parent_dir to the current
1902              * directory, and the last component as the given name.
1903              */
1904             fs_ExtractDriveLetter(true_name, parent_dir);
1905             strcat(parent_dir, ".");
1906             last_component = true_name;
1907             fs_StripDriveLetter(true_name, true_name, sizeof(true_name));
1908         }
1909
1910         if (strcmp(last_component, ".") == 0 || strcmp(last_component, "..") == 0) {
1911             fprintf(stderr,"%s: you may not use '.' or '..' as the last component\n",pn);
1912             fprintf(stderr,"%s: of a name in the 'fs lsmount' command.\n",pn);
1913             error = 1;
1914             continue;
1915         }
1916
1917         blob.in = last_component;
1918         blob.in_size = (long)strlen(last_component)+1;
1919         blob.out_size = MAXSIZE;
1920         blob.out = space;
1921         memset(space, 0, MAXSIZE);
1922
1923         code = pioctl(parent_dir, VIOC_AFS_STAT_MT_PT, &blob, 1);
1924
1925         if (code == 0) {
1926             printf("'%s' is a %smount point for volume '%s'\n",
1927                    ti->data,
1928                    (thru_symlink ? "symbolic link, leading to a " : ""),
1929                    space);
1930
1931         } else {
1932             if (errno == EINVAL) {
1933                 fprintf(stderr,"'%s' is not a mount point.\n", ti->data);
1934             } else {
1935                 Die(errno, (ti->data ? ti->data : parent_dir));
1936             }
1937             error = 1;
1938         }
1939     }
1940     return error;
1941 }
1942
1943 static int
1944 MakeMountCmd(struct cmd_syndesc *as, void *arock)
1945 {
1946     afs_int32 code;
1947     char *cellName, *volName, *tmpName;
1948 #ifdef WIN32
1949     char localCellName[128];
1950 #endif
1951     char path[1024] = "";
1952     struct afsconf_cell info;
1953     struct vldbentry vldbEntry;
1954     struct ViceIoctl blob;
1955     char * parent;
1956
1957     if (as->parms[2].items)     /* cell name specified */
1958         cellName = as->parms[2].items->data;
1959     else
1960         cellName = NULL;
1961     volName = as->parms[1].items->data;
1962
1963     if (strlen(volName) >= 64) {
1964         fprintf(stderr,"%s: volume name too long (length must be < 64 characters)\n", pn);
1965         return 1;
1966     }
1967
1968     /* Check for a cellname in the volume specification, and complain
1969      * if it doesn't match what was specified with -cell */
1970     if (tmpName = strchr(volName, ':')) {
1971         *tmpName = '\0';
1972         if (cellName) {
1973             if (strcasecmp(cellName,volName)) {
1974                 fprintf(stderr,"fs: cellnames do not match.\n");
1975                 return 1;
1976             }
1977         }
1978         cellName = volName;
1979         volName = ++tmpName;
1980     }
1981
1982     parent = Parent(as->parms[0].items->data);
1983     if (!InAFS(parent)) {
1984 #ifdef WIN32
1985         const char * nbname = NetbiosName();
1986         int len = (int)strlen(nbname);
1987
1988         if (parent[0] == '\\' && parent[1] == '\\' &&
1989             parent[len+2] == '\\' &&
1990             parent[len+3] == '\0' &&
1991             !strnicmp(nbname,&parent[2],len))
1992         {
1993             sprintf(path,"%sall\\%s", parent, &as->parms[0].items->data[strlen(parent)]);
1994             parent = Parent(path);
1995             if (!InAFS(parent)) {
1996                 fprintf(stderr,"%s: mount points must be created within the AFS file system\n", pn);
1997                 return 1;
1998             }
1999         } else 
2000 #endif
2001         {
2002             fprintf(stderr,"%s: mount points must be created within the AFS file system\n", pn);
2003             return 1;
2004         }
2005     }
2006
2007     if ( strlen(path) == 0 )
2008         strcpy(path, as->parms[0].items->data);
2009
2010     if ( IsFreelanceRoot(parent) ) {
2011         if ( !IsAdmin() ) {
2012             fprintf(stderr,"%s: Only AFS Client Administrators may alter the root.afs volume\n", pn);
2013             return 1;
2014         }
2015
2016         if (!cellName) {
2017             blob.in_size = 0;
2018             blob.out_size = sizeof(localCellName);
2019             blob.out = localCellName;
2020             code = pioctl(parent, VIOC_GET_WS_CELL, &blob, 1);
2021             if (!code)
2022                 cellName = localCellName;
2023         }
2024     } else {
2025         if (!cellName) {
2026             code = GetCell(parent,space);
2027             if (code)
2028                 return 1;
2029         }
2030     }
2031
2032     code = GetCellName(cellName?cellName:space, &info);
2033     if (code) {
2034         return 1;
2035     }
2036     if (!(as->parms[4].items)) {
2037       /* not fast, check which cell the mountpoint is being created in */
2038       code = 0;
2039         /* not fast, check name with VLDB */
2040       if (!code)
2041         code = VLDBInit(1, &info);
2042       if (code == 0) {
2043           /* make the check.  Don't complain if there are problems with init */
2044           code = ubik_VL_GetEntryByNameO(uclient, 0, volName, &vldbEntry);
2045           if (code == VL_NOENT) {
2046               fprintf(stderr,"%s: warning, volume %s does not exist in cell %s.\n",
2047                       pn, volName, cellName ? cellName : space);
2048           }
2049       }
2050     }
2051
2052     if (as->parms[3].items)     /* if -rw specified */
2053         strcpy(space, "%");
2054     else
2055         strcpy(space, "#");
2056     if (cellName) {
2057         /* cellular mount point, prepend cell prefix */
2058         strcat(space, info.name);
2059         strcat(space, ":");
2060     }
2061     strcat(space, volName);     /* append volume name */
2062     strcat(space, ".");         /* stupid convention; these end with a period */
2063 #ifdef WIN32
2064     /* create symlink with a special pioctl for Windows NT, since it doesn't
2065      * have a symlink system call.
2066      */
2067     blob.out_size = 0;
2068     blob.in_size = 1 + (long)strlen(space);
2069     blob.in = space;
2070     blob.out = NULL;
2071     code = pioctl(path, VIOC_AFS_CREATE_MT_PT, &blob, 0);
2072 #else /* not WIN32 */
2073     code = symlink(space, path);
2074 #endif /* not WIN32 */
2075     if (code) {
2076         Die(errno, path);
2077         return 1;
2078     }
2079     return 0;
2080 }
2081
2082 /*
2083  * Delete AFS mount points.  Variables are used as follows:
2084  *       tbuffer: Set to point to the null-terminated directory name of the mount point
2085  *          (or ``.'' if none is provided)
2086  *      tp: Set to point to the actual name of the mount point to nuke.
2087  */
2088 static int
2089 RemoveMountCmd(struct cmd_syndesc *as, void *arock) {
2090     afs_int32 code=0;
2091     struct ViceIoctl blob;
2092     struct cmd_item *ti;
2093     char tbuffer[1024];
2094     char lsbuffer[1024];
2095     char *tp;
2096     int error = 0;
2097     
2098     for(ti=as->parms[0].items; ti; ti=ti->next) {
2099         /* once per file */
2100         tp = (char *) strrchr(ti->data, '\\');
2101         if (!tp)
2102             tp = (char *) strrchr(ti->data, '/');
2103         if (tp) {
2104             strncpy(tbuffer, ti->data, code=(afs_int32)(tp-ti->data+1));  /* the dir name */
2105             tbuffer[code] = 0;
2106             tp++;   /* skip the slash */
2107
2108 #ifdef WIN32
2109             if (!InAFS(tbuffer)) {
2110                 const char * nbname = NetbiosName();
2111                 int len = (int)strlen(nbname);
2112
2113                 if (tbuffer[0] == '\\' && tbuffer[1] == '\\' &&
2114                     tbuffer[len+2] == '\\' &&
2115                     tbuffer[len+3] == '\0' &&
2116                     !strnicmp(nbname,&tbuffer[2],len))
2117                 {
2118                     sprintf(tbuffer,"\\\\%s\\all\\", nbname);
2119                 }
2120             }
2121 #endif
2122         } else {
2123             fs_ExtractDriveLetter(ti->data, tbuffer);
2124             strcat(tbuffer, ".");
2125             tp = ti->data;
2126             fs_StripDriveLetter(tp, tp, 0);
2127         }
2128         blob.in = tp;
2129         blob.in_size = (long)strlen(tp)+1;
2130         blob.out = lsbuffer;
2131         blob.out_size = sizeof(lsbuffer);
2132         code = pioctl(tbuffer, VIOC_AFS_STAT_MT_PT, &blob, 0);
2133         if (code) {
2134             if (errno == EINVAL) {
2135                 fprintf(stderr,"%s: '%s' is not a mount point.\n", pn, ti->data);
2136             } else {
2137                 Die(errno, ti->data);
2138             }
2139             error = 1;
2140             continue;   /* don't bother trying */
2141         }
2142
2143         if ( IsFreelanceRoot(tbuffer) && !IsAdmin() ) {
2144             fprintf(stderr,"%s: Only AFS Client Administrators may alter the root.afs volume\n", pn);
2145             error = 1;
2146             continue;   /* skip */
2147         }
2148
2149         blob.out_size = 0;
2150         blob.in = tp;
2151         blob.in_size = (long)strlen(tp)+1;
2152         code = pioctl(tbuffer, VIOC_AFS_DELETE_MT_PT, &blob, 0);
2153         if (code) {
2154             Die(errno, ti->data);
2155             error = 1;
2156         }
2157     }
2158     return error;
2159 }
2160
2161 /*
2162 */
2163
2164 static int
2165 CheckServersCmd(struct cmd_syndesc *as, void *arock)
2166 {
2167     afs_int32 code;
2168     struct ViceIoctl blob;
2169     afs_int32 j;
2170     afs_int32 temp;
2171     char *tp;
2172     struct afsconf_cell info;
2173     struct chservinfo checkserv;
2174
2175     memset(&checkserv, 0, sizeof(struct chservinfo));
2176     blob.in_size=sizeof(struct chservinfo);
2177     blob.in=(caddr_t)&checkserv;
2178
2179     blob.out_size = MAXSIZE;
2180     blob.out = space;
2181     memset(space, 0, sizeof(afs_int32));        /* so we assure zero when nothing is copied back */
2182
2183     /* prepare flags for checkservers command */
2184     temp = 2;   /* default to checking local cell only */
2185     if (as->parms[2].items) 
2186         temp |= 1;      /* set fast flag */
2187     if (as->parms[1].items) 
2188         temp &= ~2;     /* turn off local cell check */
2189     
2190     checkserv.magic = 0x12345678;       /* XXX */
2191     checkserv.tflags=temp;
2192
2193     /* now copy in optional cell name, if specified */
2194     if (as->parms[0].items) {
2195         code = GetCellName(as->parms[0].items->data, &info);
2196         if (code) {
2197             return 1;
2198         }
2199         strcpy(checkserv.tbuffer,info.name);
2200         checkserv.tsize=(int)strlen(info.name)+1;
2201     } else {
2202         strcpy(checkserv.tbuffer,"\0");
2203         checkserv.tsize=0;
2204     }
2205
2206     if(as->parms[3].items) {
2207         checkserv.tinterval=atol(as->parms[3].items->data);
2208
2209         /* sanity check */
2210         if(checkserv.tinterval<0) {
2211             printf("Warning: The negative -interval is ignored; treated as an inquiry\n");
2212             checkserv.tinterval=-1;
2213         } else if(checkserv.tinterval> 600) {
2214             printf("Warning: The maximum -interval value is 10 mins (600 secs)\n");
2215             checkserv.tinterval=600;    /* 10 min max interval */
2216         }       
2217     } else {
2218         checkserv.tinterval = -1;       /* don't change current interval */
2219     }
2220
2221     if ( checkserv.tinterval >= 0 ) {
2222 #ifdef WIN32
2223         if ( !IsAdmin() ) {
2224             fprintf (stderr,"Permission denied: requires AFS Client Administrator access.\n");
2225             return EACCES;
2226         }
2227 #else /* WIN32 */
2228         if (geteuid()) {
2229             fprintf (stderr,"Permission denied: requires root access.\n");
2230             return EACCES;
2231         }
2232 #endif /* WIN32 */
2233     }
2234
2235     code = pioctl(0, VIOCCKSERV, &blob, 1);
2236     if (code) {
2237         if ((errno == EACCES) && (checkserv.tinterval > 0)) {
2238             printf("Must be root to change -interval\n");
2239             return code;
2240         }
2241         Die(errno, 0);
2242         return 1;
2243     }
2244     memcpy(&temp, space, sizeof(afs_int32));
2245     if (checkserv.tinterval >= 0) {
2246         if (checkserv.tinterval > 0) 
2247             printf("The new down server probe interval (%d secs) is now in effect (old interval was %d secs)\n", 
2248                    checkserv.tinterval, temp);
2249         else 
2250             printf("The current down server probe interval is %d secs\n", temp);
2251         return 0;
2252     }
2253     if (temp == 0) {
2254         printf("All servers are running.\n");
2255     } else {
2256         printf("These servers unavailable due to network or server problems: ");
2257         for(j=0; j < MAXHOSTS; j++) {
2258             memcpy(&temp, space + j*sizeof(afs_int32), sizeof(afs_int32));
2259             if (temp == 0) 
2260                 break;
2261             tp = hostutil_GetNameByINet(temp);
2262             printf(" %s", tp);
2263         }
2264         printf(".\n");
2265         code = 1;       /* XXX */
2266     }
2267     return code;
2268 }
2269
2270 static int
2271 MessagesCmd(struct cmd_syndesc *as, void *arock)
2272 {
2273     afs_int32 code=0;
2274     struct ViceIoctl blob;
2275     struct gaginfo gagflags;
2276     struct cmd_item *show;
2277     
2278     memset(&gagflags, 0, sizeof(struct gaginfo));
2279     blob.in_size = sizeof(struct gaginfo);
2280     blob.in = (caddr_t ) &gagflags;
2281     blob.out_size = MAXSIZE;
2282     blob.out = space;
2283     memset(space, 0, sizeof(afs_int32));        /* so we assure zero when nothing is copied back */
2284
2285     if (show = as->parms[0].items) {
2286         if (!strcasecmp (show->data, "user"))
2287             gagflags.showflags |= GAGUSER;
2288         else if (!strcasecmp (show->data, "console"))
2289             gagflags.showflags |= GAGCONSOLE;
2290         else if (!strcasecmp (show->data, "all"))
2291             gagflags.showflags |= GAGCONSOLE | GAGUSER;
2292         else if (!strcasecmp (show->data, "none"))
2293             /* do nothing */ ;
2294         else {
2295             fprintf(stderr, 
2296                      "unrecognized flag %s: must be in {user,console,all,none}\n", 
2297                      show->data);
2298             code = EINVAL;
2299         }
2300     }
2301  
2302     if (code)
2303         return 1;
2304
2305     code = pioctl(0, VIOC_GAG, &blob, 1);
2306     if (code) {
2307         Die(errno, 0);
2308         return 1;
2309     }
2310     return 0;
2311 }
2312
2313 static int
2314 CheckVolumesCmd(struct cmd_syndesc *as, void *arock)
2315 {
2316     afs_int32 code;
2317     struct ViceIoctl blob;
2318     
2319     blob.in_size = 0;
2320     blob.out_size = 0;
2321     code = pioctl(0, VIOCCKBACK, &blob, 1);
2322     if (code) {
2323         Die(errno, 0);
2324         return 1;
2325     }
2326     printf("All volumeID/name mappings checked.\n");
2327     
2328     return 0;
2329 }
2330
2331 static int
2332 SetCacheSizeCmd(struct cmd_syndesc *as, void *arock)
2333 {
2334     afs_int32 code;
2335     struct ViceIoctl blob;
2336     afs_int32 temp;
2337     
2338 #ifdef WIN32
2339     if ( !IsAdmin() ) {
2340         fprintf (stderr,"Permission denied: requires AFS Client Administrator access.\n");
2341         return EACCES;
2342     }
2343 #else /* WIN32 */
2344     if (geteuid()) {
2345         fprintf (stderr,"Permission denied: requires root access.\n");
2346         return EACCES;
2347     }
2348 #endif /* WIN32 */
2349
2350     if (!as->parms[0].items && !as->parms[1].items) {
2351         fprintf(stderr,"%s: syntax error in set cache size cmd.\n", pn);
2352         return 1;
2353     }
2354     if (as->parms[0].items) {
2355         code = util_GetInt32(as->parms[0].items->data, &temp);
2356         if (code) {
2357             fprintf(stderr,"%s: bad integer specified for cache size.\n", pn);
2358             return 1;
2359         }
2360     } else
2361         temp = 0;
2362     blob.in = (char *) &temp;
2363     blob.in_size = sizeof(afs_int32);
2364     blob.out_size = 0;
2365     code = pioctl(0, VIOCSETCACHESIZE, &blob, 1);
2366     if (code) {
2367         Die(errno, (char *) 0);
2368         return 1;
2369     } 
2370       
2371     printf("New cache size set.\n");
2372     return 0;
2373 }
2374
2375 static int
2376 GetCacheParmsCmd(struct cmd_syndesc *as, void *arock)
2377 {
2378     afs_int32 code;
2379     struct ViceIoctl blob;
2380     cm_cacheParms_t parms;
2381
2382     memset(&parms, 0, sizeof(parms));
2383     blob.in = NULL;
2384     blob.in_size = 0;
2385     blob.out_size = sizeof(parms);
2386     blob.out = (char *) &parms;
2387     code = pioctl(0, VIOCGETCACHEPARMS, &blob, 1);
2388     if (code) {
2389         Die(errno, NULL);
2390         return 1;
2391     }
2392      
2393     printf("AFS using %I64u of the cache's available %I64u 1K byte blocks.\n",
2394            parms.parms[1], parms.parms[0]);
2395     if (parms.parms[1] > parms.parms[0])
2396         printf("[Cache guideline temporarily deliberately exceeded; it will be adjusted down but you may wish to increase the cache size.]\n");
2397     return 0;
2398 }
2399
2400 static int
2401 ListCellsCmd(struct cmd_syndesc *as, void *arock)
2402 {
2403     afs_int32 code;
2404     afs_int32 i, j, *lp, magic, size;
2405     char *tp;
2406     afs_int32 addr, maxa = OMAXHOSTS;
2407     struct ViceIoctl blob;
2408     int resolve;
2409
2410     resolve = !(as->parms[0].items);    /* -numeric */
2411     
2412     for(i=0;i<1000;i++) {
2413         tp = space;
2414         memcpy(tp, &i, sizeof(afs_int32));
2415         tp = (char *)(space + sizeof(afs_int32));
2416         lp = (afs_int32 *)tp;
2417         *lp++ = 0x12345678;
2418         size = sizeof(afs_int32) + sizeof(afs_int32);
2419         blob.out_size = MAXSIZE;
2420         blob.in_size = sizeof(afs_int32);
2421         blob.in = space;
2422         blob.out = space;
2423         code = pioctl(0, VIOCGETCELL, &blob, 1);
2424         if (code < 0) {
2425             if (errno == EDOM) 
2426                 break;  /* done with the list */
2427             Die(errno, 0);
2428             return 1;
2429         }       
2430         tp = space;
2431         memcpy(&magic, tp, sizeof(afs_int32));  
2432         if (magic == 0x12345678) {
2433             maxa = MAXHOSTS;
2434             tp += sizeof(afs_int32);
2435         }
2436         printf("Cell %s on hosts", tp+maxa*sizeof(afs_int32));
2437         for(j=0; j < maxa; j++) {
2438             char *name, tbuffer[20];
2439
2440             memcpy(&addr, tp + j*sizeof(afs_int32), sizeof(afs_int32));
2441             if (addr == 0) 
2442                 break;
2443
2444             if (resolve) {
2445                 name = hostutil_GetNameByINet(addr);
2446             } else {
2447                 addr = ntohl(addr);
2448                 sprintf(tbuffer, "%d.%d.%d.%d", (addr >> 24) & 0xff,
2449                          (addr >> 16) & 0xff, (addr >> 8) & 0xff, addr & 0xff);
2450                 name = tbuffer;
2451             }
2452             printf(" %s", name);
2453         }
2454         printf(".\n");
2455     }
2456     return 0;
2457 }
2458
2459 #ifndef WIN32
2460 static int
2461 ListAliasesCmd(struct cmd_syndesc *as, void *arock)
2462 {
2463     afs_int32 code, i;
2464     char *tp, *aliasName, *realName;
2465     struct ViceIoctl blob;
2466
2467     for (i = 0;; i++) {
2468         tp = space;
2469         memcpy(tp, &i, sizeof(afs_int32));
2470         blob.out_size = MAXSIZE;
2471         blob.in_size = sizeof(afs_int32);
2472         blob.in = space;
2473         blob.out = space;
2474         code = pioctl(0, VIOC_GETALIAS, &blob, 1);
2475         if (code < 0) {
2476             if (errno == EDOM)
2477                 break;          /* done with the list */
2478             Die(errno, 0);
2479             return 1;
2480         }
2481         tp = space;
2482         aliasName = tp;
2483         tp += strlen(aliasName) + 1;
2484         realName = tp;
2485         printf("Alias %s for cell %s\n", aliasName, realName);
2486     }
2487     return 0;
2488 }
2489
2490 static int
2491 CallBackRxConnCmd(struct cmd_syndesc *as, void *arock)
2492 {
2493     afs_int32 code;
2494     struct ViceIoctl blob;
2495     struct cmd_item *ti;
2496     afs_int32 hostAddr;
2497     struct hostent *thp;
2498     char *tp;
2499     int setp;
2500     
2501     ti = as->parms[0].items;
2502     setp = 1;
2503     if (ti) {
2504         thp = hostutil_GetHostByName(ti->data);
2505         if (!thp) {
2506             fprintf(stderr, "host %s not found in host table.\n", ti->data);
2507             return 1;
2508         }
2509         else memcpy(&hostAddr, thp->h_addr, sizeof(afs_int32));
2510     } else {
2511         hostAddr = 0;   /* means don't set host */
2512         setp = 0;       /* aren't setting host */
2513     }
2514     
2515     /* now do operation */
2516     blob.in_size = sizeof(afs_int32);
2517     blob.out_size = sizeof(afs_int32);
2518     blob.in = (char *) &hostAddr;
2519     blob.out = (char *) &hostAddr;
2520     
2521     code = pioctl(0, VIOC_CBADDR, &blob, 1);
2522     if (code < 0) {
2523         Die(errno, 0);
2524         return 1;
2525     }
2526     return 0;
2527 }
2528 #endif /* WIN32 */
2529
2530 static int
2531 NewCellCmd(struct cmd_syndesc *as, void *arock)
2532 {
2533 #ifndef WIN32
2534     afs_int32 code, linkedstate=0, size=0, *lp;
2535     struct ViceIoctl blob;
2536     struct cmd_item *ti;
2537     char *tp, *cellname=0;
2538     struct hostent *thp;
2539     afs_int32 fsport = 0, vlport = 0;
2540
2541     memset(space, 0, MAXHOSTS * sizeof(afs_int32));
2542     tp = space;
2543     lp = (afs_int32 *)tp;
2544     *lp++ = 0x12345678;
2545     tp += sizeof(afs_int32);
2546     for(ti=as->parms[1].items; ti; ti=ti->next) {
2547         thp = hostutil_GetHostByName(ti->data);
2548         if (!thp) {
2549             fprintf(stderr,"%s: Host %s not found in host table, skipping it.\n",
2550                    pn, ti->data);
2551         }
2552         else {
2553             memcpy(tp, thp->h_addr, sizeof(afs_int32));
2554             tp += sizeof(afs_int32);
2555         }
2556     }
2557     if (as->parms[2].items) {
2558         /*
2559          * Link the cell, for the purposes of volume location, to the specified
2560          * cell.
2561          */
2562         cellname = as->parms[2].items->data;
2563         linkedstate = 1;
2564     }
2565 #ifdef FS_ENABLE_SERVER_DEBUG_PORTS
2566     if (as->parms[3].items) {
2567         code = util_GetInt32(as->parms[3].items->data, &vlport);
2568         if (code) {
2569             fprintf(stderr,"fs: bad integer specified for the fileserver port.\n");
2570             return code;
2571         }
2572     }
2573     if (as->parms[4].items) {
2574         code = util_GetInt32(as->parms[4].items->data, &fsport);
2575         if (code) {
2576             fprintf(stderr,"fs: bad integer specified for the vldb server port.\n");
2577             return code;
2578         }
2579     }
2580 #endif
2581     tp = (char *)(space + (MAXHOSTS+1) *sizeof(afs_int32));
2582     lp = (afs_int32 *)tp;    
2583     *lp++ = fsport;
2584     *lp++ = vlport;
2585     *lp = linkedstate;
2586     strcpy(space +  ((MAXHOSTS+4) * sizeof(afs_int32)), as->parms[0].items->data);
2587     size = ((MAXHOSTS+4) * sizeof(afs_int32)) + strlen(as->parms[0].items->data) + 1 /* for null */;
2588     tp = (char *)(space + size);
2589     if (linkedstate) {
2590         strcpy(tp, cellname);
2591         size += strlen(cellname) + 1;
2592     }
2593     blob.in_size = size;
2594     blob.in = space;
2595     blob.out_size = 0;
2596     code = pioctl(0, VIOCNEWCELL, &blob, 1);
2597     if (code < 0)
2598         Die(errno, 0);
2599     return 0;
2600 #else /* WIN32 */
2601     afs_int32 code;
2602     struct ViceIoctl blob;
2603     
2604     if ( !IsAdmin() ) {
2605         fprintf (stderr,"Permission denied: requires AFS Client Administrator access.\n");
2606         return EACCES;
2607     }
2608
2609     blob.in_size = 0;
2610     blob.in = (char *) 0;
2611     blob.out_size = MAXSIZE;
2612     blob.out = space;
2613
2614     code = pioctl((char *) 0, VIOCNEWCELL, &blob, 1);
2615
2616     if (code) {
2617         Die(errno, (char *) 0);
2618         return 1;
2619     }
2620     
2621     printf("Cell servers information refreshed\n");
2622     return 0;
2623 #endif /* WIN32 */
2624 }
2625
2626 #ifndef WIN32
2627 static int
2628 NewAliasCmd(struct cmd_syndesc *as, void *arock)
2629 {
2630     afs_int32 code;
2631     struct ViceIoctl blob;
2632     char *tp;
2633     char *aliasName, *realName;
2634
2635     /* Setup and do the NEWALIAS pioctl call */
2636     aliasName = as->parms[0].items->data;
2637     realName = as->parms[1].items->data;
2638     tp = space;
2639     strcpy(tp, aliasName);
2640     tp += strlen(aliasName) + 1;
2641     strcpy(tp, realName);
2642     tp += strlen(realName) + 1;
2643
2644     blob.in_size = tp - space;
2645     blob.in = space;
2646     blob.out_size = 0;
2647     blob.out = space;
2648     code = pioctl(0, VIOC_NEWALIAS, &blob, 1);
2649     if (code < 0) {
2650         if (errno == EEXIST) {
2651             fprintf(stderr,
2652                     "%s: cell name `%s' in use by an existing cell.\n", pn,
2653                     aliasName);
2654         } else {
2655             Die(errno, 0);
2656         }
2657         return 1;
2658     }
2659     return 0;
2660 }
2661 #endif /* WIN32 */
2662
2663 static int
2664 WhichCellCmd(struct cmd_syndesc *as, void *arock)
2665 {
2666     afs_int32 code;
2667     struct cmd_item *ti;
2668     struct ViceIoctl blob;
2669     int error = 0;
2670     int literal = 0;
2671     cm_ioctlQueryOptions_t options;
2672
2673     if (as->parms[1].items)
2674         literal = 1;
2675     
2676     SetDotDefault(&as->parms[0].items);
2677     for(ti=as->parms[0].items; ti; ti=ti->next) {
2678         cm_fid_t fid;
2679         afs_uint32 filetype;
2680         char cell[MAXCELLCHARS];
2681
2682         /* once per file */
2683         memset(&fid, 0, sizeof(fid));
2684         memset(&options, 0, sizeof(options));
2685         filetype = 0;
2686         options.size = sizeof(options);
2687         options.field_flags |= CM_IOCTL_QOPTS_FIELD_LITERAL;
2688         options.literal = literal;
2689         blob.in_size = options.size;    /* no variable length data */
2690         blob.in = &options;
2691
2692         blob.out_size = sizeof(cm_fid_t);
2693         blob.out = (char *) &fid;
2694         if (0 == pioctl(ti->data, VIOCGETFID, &blob, 1)) {
2695             options.field_flags |= CM_IOCTL_QOPTS_FIELD_FID;
2696             options.fid = fid;
2697         } else {
2698             Die(errno, ti->data);
2699             error = 1;
2700             continue;
2701         }
2702
2703         blob.out_size = sizeof(filetype);
2704         blob.out = &filetype;
2705
2706         code = pioctl(ti->data, VIOC_GETFILETYPE, &blob, 1);
2707
2708         blob.out_size = MAXCELLCHARS;
2709         blob.out = cell;
2710
2711         code = pioctl(ti->data, VIOC_FILE_CELL_NAME, &blob, 1);
2712         if (code) {
2713             if (errno == ENOENT)
2714                 fprintf(stderr,"%s: no such cell as '%s'\n", pn, ti->data);
2715             else
2716                 Die(errno, ti->data);
2717             error = 1;
2718             continue;
2719         }
2720         printf("%s %s lives in cell '%s'\n",
2721                 filetypestr(filetype),
2722                 ti->data, cell);
2723     }
2724     return error;
2725 }
2726
2727 static int
2728 WSCellCmd(struct cmd_syndesc *as, void *arock)
2729 {
2730     afs_int32 code;
2731     struct ViceIoctl blob;
2732     
2733     blob.in_size = 0;
2734     blob.in = NULL;
2735     blob.out_size = MAXSIZE;
2736     blob.out = space;
2737
2738     code = pioctl(NULL, VIOC_GET_WS_CELL, &blob, 1);
2739
2740     if (code) {
2741         Die(errno, NULL);
2742         return 1;
2743     }
2744
2745     printf("This workstation belongs to cell '%s'\n", space);
2746     return 0;
2747 }
2748
2749 /*
2750 static int
2751 PrimaryCellCmd(struct cmd_syndesc *as, void *arock)
2752 {
2753     fprintf(stderr,"This command is obsolete, as is the concept of a primary token.\n");
2754     return 0;
2755 }
2756 */
2757
2758 #ifndef AFS_NT40_ENV
2759 static int
2760 MonitorCmd(struct cmd_syndesc *as, void *arock)
2761 {
2762     afs_int32 code;
2763     struct ViceIoctl blob;
2764     struct cmd_item *ti;
2765     afs_int32 hostAddr;
2766     struct hostent *thp;
2767     char *tp;
2768     int setp;
2769     
2770     ti = as->parms[0].items;
2771     setp = 1;
2772     if (ti) {
2773         /* set the host */
2774         if (!strcmp(ti->data, "off")) {
2775             hostAddr = 0xffffffff;
2776         } else {
2777             thp = hostutil_GetHostByName(ti->data);
2778             if (!thp) {
2779                 if (!strcmp(ti->data, "localhost")) {
2780                     fprintf(stderr,"localhost not in host table, assuming 127.0.0.1\n");
2781                     hostAddr = htonl(0x7f000001);
2782                 } else {
2783                     fprintf(stderr,"host %s not found in host table.\n", ti->data);
2784                     return 1;
2785                 }
2786             } else {
2787                 memcpy(&hostAddr, thp->h_addr, sizeof(afs_int32));
2788             }
2789         }
2790     } else {
2791         hostAddr = 0;   /* means don't set host */
2792         setp = 0;       /* aren't setting host */
2793     }
2794
2795     /* now do operation */
2796     blob.in_size = sizeof(afs_int32);
2797     blob.out_size = sizeof(afs_int32);
2798     blob.in = (char *) &hostAddr;
2799     blob.out = (char *) &hostAddr;
2800     code = pioctl(0, VIOC_AFS_MARINER_HOST, &blob, 1);
2801     if (code) {
2802         Die(errno, 0);
2803         return 1;
2804     }
2805     if (setp) {
2806         printf("%s: new monitor host set.\n", pn);
2807     } else {
2808         /* now decode old address */
2809         if (hostAddr == 0xffffffff) {
2810             printf("Cache monitoring is currently disabled.\n");
2811         } else {
2812             tp = hostutil_GetNameByINet(hostAddr);
2813             printf("Using host %s for monitor services.\n", tp);
2814         }
2815     }
2816     return 0;
2817 }
2818 #endif /* AFS_NT40_ENV */
2819
2820 static int
2821 SysNameCmd(struct cmd_syndesc *as, void *arock)
2822 {
2823     afs_int32 code;
2824     struct ViceIoctl blob;
2825     struct cmd_item *ti;
2826     char *input = space;
2827     afs_int32 setp = 0;
2828     
2829     ti = as->parms[0].items;
2830     if (ti) {
2831 #ifdef WIN32
2832     if ( !IsAdmin() ) {
2833         fprintf (stderr,"Permission denied: requires AFS Client Administrator access.\n");
2834         return EACCES;
2835     }
2836 #else /* WIN32 */
2837     if (geteuid()) {
2838         fprintf (stderr,"Permission denied: requires root access.\n");
2839         return EACCES;
2840     }
2841 #endif /* WIN32 */
2842     }
2843
2844     blob.in = space;
2845     blob.out = space;
2846     blob.out_size = MAXSIZE;
2847     blob.in_size = sizeof(afs_int32);
2848     memcpy(input, &setp, sizeof(afs_int32));
2849     input += sizeof(afs_int32);
2850     for (; ti; ti = ti->next) {
2851         setp++;
2852         blob.in_size += (long)strlen(ti->data) + 1;
2853         if (blob.in_size > MAXSIZE) {
2854             fprintf(stderr, "%s: sysname%s too long.\n", pn,
2855                      setp > 1 ? "s" : "");
2856             return 1;
2857         }
2858         strcpy(input, ti->data);
2859         input += strlen(ti->data);
2860         *(input++) = '\0';
2861     }
2862     memcpy(space, &setp, sizeof(afs_int32));
2863     code = pioctl(0, VIOC_AFS_SYSNAME, &blob, 1);
2864     if (code) {
2865         Die(errno, 0);
2866         return 1;
2867     }    
2868     if (setp) {
2869         printf("%s: new sysname%s set.\n", pn, setp > 1 ? " list" : "");
2870         return 0;
2871     }
2872
2873     input = space;
2874     memcpy(&setp, input, sizeof(afs_int32));
2875     input += sizeof(afs_int32);
2876     if (!setp) {
2877         fprintf(stderr,"No sysname name value was found\n");
2878         return 1;
2879     } 
2880     
2881     printf("Current sysname%s is", setp > 1 ? " list" : "");
2882     for (; setp > 0; --setp ) {
2883         printf(" \'%s\'", input);
2884         input += strlen(input) + 1;
2885     }
2886     printf("\n");
2887     return 0;
2888 }
2889
2890 #ifndef AFS_NT40_ENV
2891 static char *exported_types[] = {"null", "nfs", ""};
2892 static int ExportAfsCmd(struct cmd_syndesc *as, void *arock)
2893 {
2894     afs_int32 code;
2895     struct ViceIoctl blob;
2896     struct cmd_item *ti;
2897     int export = 0, type = 0, mode = 0, exp = 0, gstat = 0;
2898     int exportcall, pwsync = 0, smounts = 0;
2899     
2900 #ifdef WIN32
2901     if ( !IsAdmin() ) {
2902         fprintf (stderr,"Permission denied: requires AFS Client Administrator access.\n");
2903         return EACCES;
2904     }
2905 #else /* WIN32 */
2906     if (geteuid()) {
2907         fprintf (stderr,"Permission denied: requires root access.\n");
2908         return EACCES;
2909     }
2910 #endif /* WIN32 */
2911
2912     ti = as->parms[0].items;
2913     if (strcmp(ti->data, "nfs") == 0) 
2914         type = 0x71; /* NFS */
2915     else {
2916         fprintf(stderr,
2917                 "Invalid exporter type, '%s', Only the 'nfs' exporter is currently supported\n", ti->data);
2918         return 1;
2919     }
2920     ti = as->parms[1].items;
2921     if (ti) {
2922         if (strcmp(ti->data, "on") == 0) 
2923             export = 3;
2924         else if (strcmp(ti->data, "off") == 0) 
2925             export = 2;
2926         else {
2927             fprintf(stderr, "Illegal argument %s\n", ti->data);
2928             return 1;
2929         }
2930         exp = 1;
2931     }
2932     if (ti = as->parms[2].items) {      /* -noconvert */
2933         if (strcmp(ti->data, "on") == 0) 
2934             mode = 2;
2935         else if (strcmp(ti->data, "off") == 0) 
2936             mode = 3;
2937         else {
2938             fprintf(stderr, "Illegal argument %s\n", ti->data);
2939             return 1;
2940         }
2941     }
2942     if (ti = as->parms[3].items) {      /* -uidcheck */
2943         if (strcmp(ti->data, "on") == 0) 
2944             pwsync = 3;
2945         else if (strcmp(ti->data, "off") == 0) 
2946             pwsync = 2;
2947         else {
2948             fprintf(stderr, "Illegal argument %s\n", ti->data);
2949             return 1;
2950         }
2951     }
2952     if (ti = as->parms[4].items) {      /* -submounts */
2953         if (strcmp(ti->data, "on") == 0) 
2954             smounts = 3;
2955         else if (strcmp(ti->data, "off") == 0) 
2956             smounts = 2;
2957         else {
2958             fprintf(stderr, "Illegal argument %s\n", ti->data);
2959             return 1;
2960         }
2961     }
2962     exportcall =  (type << 24) | (mode << 6) | (pwsync << 4) | (smounts << 2) | export;
2963     type &= ~0x70;
2964     /* make the call */
2965     blob.in = (char *) &exportcall;
2966     blob.in_size = sizeof(afs_int32);
2967     blob.out = (char *) &exportcall;
2968     blob.out_size = sizeof(afs_int32);
2969     code = pioctl(0, VIOC_EXPORTAFS, &blob, 1);
2970     if (code) {
2971         if (errno == ENODEV) {
2972             fprintf(stderr,
2973                     "Sorry, the %s-exporter type is currently not supported on this AFS client\n", exported_types[type]);
2974         } else {
2975             Die(errno, 0);
2976         }
2977         return 1;
2978     } else {
2979         if (!gstat) {
2980             if (exportcall & 1) {
2981                 printf("'%s' translator is enabled with the following options:\n\tRunning in %s mode\n\tRunning in %s mode\n\t%s\n", 
2982                        exported_types[type], (exportcall & 2 ? "strict unix" : "convert owner mode bits to world/other"),
2983                        (exportcall & 4 ? "strict 'passwd sync'" : "no 'passwd sync'"),
2984                        (exportcall & 8 ? "Allow mounts of /afs/.. subdirs" : "Only mounts to /afs allowed"));
2985             } else {
2986                 printf("'%s' translator is disabled\n", exported_types[type]);
2987             }
2988         }
2989     }
2990     return 0;
2991 }
2992 #endif
2993
2994 static int
2995 GetCellCmd(struct cmd_syndesc *as, void *arock)
2996 {
2997     afs_int32 code;
2998     struct ViceIoctl blob;
2999     struct afsconf_cell info;
3000     struct cmd_item *ti;
3001     struct a {
3002         afs_int32 stat;
3003         afs_int32 junk;
3004     } args;
3005     int error = 0;
3006
3007     memset(&args, 0, sizeof(args));      /* avoid Purify UMR error */
3008     for(ti=as->parms[0].items; ti; ti=ti->next) {
3009         /* once per cell */
3010         blob.out_size = sizeof(args);
3011         blob.out = (caddr_t) &args;
3012         code = GetCellName(ti->data, &info);
3013         if (code) {
3014             error = 1;
3015             continue;
3016         }
3017         blob.in_size = 1+(long)strlen(info.name);
3018         blob.in = info.name;
3019         code = pioctl(0, VIOC_GETCELLSTATUS, &blob, 1);
3020         if (code) {
3021             if (errno == ENOENT)
3022                 fprintf(stderr,"%s: the cell named '%s' does not exist\n", pn, info.name);
3023             else
3024                 Die(errno, info.name);
3025             error = 1;
3026             continue;
3027         }
3028         printf("Cell %s status: ", info.name);
3029 #ifdef notdef
3030         if (args.stat & 1) 
3031             printf("primary ");
3032 #endif
3033         if (args.stat & 2) 
3034             printf("no setuid allowed");
3035         else 
3036             printf("setuid allowed");
3037         if (args.stat & 4) 
3038             printf(", using old VLDB");
3039         printf("\n");
3040     }
3041     return error;
3042 }
3043
3044 static int SetCellCmd(struct cmd_syndesc *as, void *arock)
3045 {
3046     afs_int32 code;
3047     struct ViceIoctl blob;
3048     struct afsconf_cell info;
3049     struct cmd_item *ti;
3050     struct a {
3051         afs_int32 stat;
3052         afs_int32 junk;
3053         char cname[64];
3054     } args;
3055     int error = 0;
3056
3057     /* Check arguments. */
3058     if (as->parms[1].items && as->parms[2].items) {
3059         fprintf(stderr, "Cannot specify both -suid and -nosuid.\n");
3060         return 1;
3061     }
3062
3063     /* figure stuff to set */
3064     args.stat = 0;
3065     args.junk = 0;
3066
3067 #ifdef WIN32
3068     if ( !IsAdmin() ) {
3069         fprintf (stderr,"Permission denied: requires AFS Client Administrator access.\n");
3070         return EACCES;
3071     }
3072 #else /* WIN32 */
3073     if (geteuid()) {
3074         fprintf (stderr,"Permission denied: requires root access.\n");
3075         return EACCES;
3076     }
3077 #endif /* WIN32 */
3078
3079     if (! as->parms[1].items) 
3080         args.stat |= CM_SETCELLFLAG_SUID; /* default to -nosuid */
3081
3082     /* set stat for all listed cells */
3083     for(ti=as->parms[0].items; ti; ti=ti->next) {
3084         /* once per cell */
3085         code = GetCellName(ti->data, &info);
3086         if (code) {
3087             error = 1;
3088             continue;
3089         }
3090         strcpy(args.cname, info.name);
3091         blob.in_size = sizeof(args);
3092         blob.in = (caddr_t) &args;
3093         blob.out_size = 0;
3094         blob.out = (caddr_t) 0;
3095         code = pioctl(0, VIOC_SETCELLSTATUS, &blob, 1);
3096         if (code) {
3097             Die(errno, info.name);      /* XXX added cell name to Die() call */
3098             error = 1;
3099         }
3100     }
3101     return error;
3102 }
3103
3104 static int
3105 GetCellName(char *cellNamep, struct afsconf_cell *infop)
3106 {
3107     strcpy(infop->name, cellNamep);
3108     return 0;
3109 }
3110
3111 static int
3112 VLDBInit(int noAuthFlag, struct afsconf_cell *info)
3113 {
3114     afs_int32 code;
3115     char confDir[257];
3116
3117     cm_GetConfigDir(confDir, sizeof(confDir));
3118
3119     code = ugen_ClientInit(noAuthFlag, confDir, 
3120                            info->name, 0, &uclient, 
3121                            NULL, pn, rxkad_clear,
3122                            VLDB_MAXSERVERS, AFSCONF_VLDBSERVICE, 50,
3123                            0, 0, USER_SERVICE_ID);
3124     rxInitDone = 1;
3125     return code;
3126 }
3127
3128 static struct ViceIoctl gblob;
3129 static int debug = 0;
3130 /* 
3131  * here follow some routines in suport of the setserverprefs and
3132  * getserverprefs commands.  They are:
3133  * SetPrefCmd  "top-level" routine
3134  * addServer   adds a server to the list of servers to be poked into the
3135  *             kernel.  Will poke the list into the kernel if it threatens
3136  *             to get too large.
3137  * pokeServers pokes the existing list of servers and ranks into the kernel
3138  * GetPrefCmd  reads the Cache Manager's current list of server ranks
3139  */
3140
3141 #ifdef WIN32
3142 static int 
3143 pokeServers(void)
3144 {
3145     int code;
3146     cm_SSetPref_t *ssp;
3147     code = pioctl(0, VIOC_SETSPREFS, &gblob, 1);
3148
3149     ssp = (cm_SSetPref_t *)space;
3150     gblob.in_size = (long)(((char *)&(ssp->servers[0])) - (char *)ssp);
3151     gblob.in = space;
3152     return code;
3153 }
3154 #else
3155 /*
3156  * returns -1 if error message printed,
3157  * 0 on success,
3158  * errno value if error and no error message printed
3159  */
3160 static int
3161 pokeServers(void)
3162 {
3163     int code;
3164
3165     code = pioctl(0, VIOC_SETSPREFS, &gblob, 1);
3166     if (code && (errno == EINVAL)) {
3167         struct setspref *ssp;
3168         ssp = (struct setspref *)gblob.in;
3169         if (!(ssp->flags & DBservers)) {
3170             gblob.in = (void *)&(ssp->servers[0]);
3171             gblob.in_size -= ((char *)&(ssp->servers[0])) - (char *)ssp;
3172             code = pioctl(0, VIOC_SETSPREFS33, &gblob, 1);
3173             return code ? errno : 0;
3174         }
3175         fprintf(stderr,
3176                 "This cache manager does not support VL server preferences.\n");
3177         return -1;
3178     }
3179
3180     return code ? errno : 0;
3181 }
3182 #endif /* WIN32 */
3183
3184 #ifdef WIN32
3185 static int
3186 addServer(char *name, unsigned short rank)
3187 {  
3188     int code;
3189     cm_SSetPref_t *ssp;
3190     cm_SPref_t *sp;
3191     struct hostent *thostent;
3192
3193 #ifndef MAXUSHORT
3194 #ifdef MAXSHORT
3195 #define MAXUSHORT ((unsigned short) 2*MAXSHORT+1)  /* assumes two's complement binary system */
3196 #else
3197 #define MAXUSHORT ((unsigned short) ~0)
3198 #endif
3199 #endif
3200
3201     code = 0;
3202     thostent = hostutil_GetHostByName(name);
3203     if (!thostent) {
3204         fprintf (stderr, "%s: couldn't resolve name.\n", name);
3205         return EINVAL;
3206     }
3207
3208     ssp = (cm_SSetPref_t *)(gblob.in);
3209
3210     if (gblob.in_size > MAXINSIZE - sizeof(cm_SPref_t)) {
3211         code = pokeServers();
3212         ssp->num_servers = 0;
3213     }
3214
3215     sp = (cm_SPref_t *)((char*)gblob.in + gblob.in_size);
3216     memcpy (&(sp->host.s_addr), thostent->h_addr, sizeof(afs_uint32));
3217     sp->rank = (rank > MAXUSHORT ? MAXUSHORT : rank);
3218     gblob.in_size += sizeof(cm_SPref_t);
3219     ssp->num_servers++;
3220
3221     if (debug) fprintf(stderr, "adding server %s, rank %d, ip addr 0x%lx\n",name,sp->rank,sp->host.s_addr);
3222
3223     return code;
3224 }
3225 #else
3226 /*
3227  * returns -1 if error message printed,
3228  * 0 on success,
3229  * errno value if error and no error message printed
3230  */
3231 static int
3232 addServer(char *name, afs_int32 rank)
3233 {
3234     int t, code;
3235     struct setspref *ssp;
3236     struct spref *sp;
3237     struct hostent *thostent;
3238     afs_uint32 addr;
3239     int error = 0;
3240
3241 #ifndef MAXUSHORT
3242 #ifdef MAXSHORT
3243 #define MAXUSHORT ((unsigned short) 2*MAXSHORT+1)       /* assumes two's complement binary system */
3244 #else
3245 #define MAXUSHORT ((unsigned short) ~0)
3246 #endif
3247 #endif
3248
3249     thostent = hostutil_GetHostByName(name);
3250     if (!thostent) {
3251         fprintf(stderr, "%s: couldn't resolve name.\n", name);
3252         return -1;
3253     }
3254
3255     ssp = (struct setspref *)(gblob.in);
3256
3257     for (t = 0; thostent->h_addr_list[t]; t++) {
3258         if (gblob.in_size > MAXINSIZE - sizeof(struct spref)) {
3259             code = pokeServers();
3260             if (code)
3261                 error = code;
3262             ssp->num_servers = 0;
3263         }
3264
3265         sp = (struct spref *)(gblob.in + gblob.in_size);
3266         memcpy(&(sp->server.s_addr), thostent->h_addr_list[t],
3267                sizeof(afs_uint32));
3268         sp->rank = (rank > MAXUSHORT ? MAXUSHORT : rank);
3269         gblob.in_size += sizeof(struct spref);
3270         ssp->num_servers++;
3271
3272         if (debug)
3273             fprintf(stderr, "adding server %s, rank %d, ip addr 0x%lx\n",
3274                     name, sp->rank, sp->server.s_addr);
3275     }
3276
3277     return error;
3278 }
3279 #endif /* WIN32 */
3280
3281 #ifdef WIN32
3282 static BOOL IsWindowsNT (void)
3283 {
3284     static BOOL fChecked = FALSE;
3285     static BOOL fIsWinNT = FALSE;
3286
3287     if (!fChecked)
3288     {
3289         OSVERSIONINFO Version;
3290
3291         fChecked = TRUE;
3292
3293         memset (&Version, 0x00, sizeof(Version));
3294         Version.dwOSVersionInfoSize = sizeof(Version);
3295
3296         if (GetVersionEx (&Version))
3297         {
3298             if (Version.dwPlatformId == VER_PLATFORM_WIN32_NT)
3299                 fIsWinNT = TRUE;
3300         }
3301     }
3302     return fIsWinNT;
3303 }
3304 #endif /* WIN32 */
3305
3306 #ifdef WIN32
3307 static int
3308 SetPrefCmd(struct cmd_syndesc *as, void * arock)
3309 {
3310     FILE *infd;
3311     afs_int32 code;
3312     struct cmd_item *ti;
3313     char name[80];
3314     afs_int32 rank;
3315     cm_SSetPref_t *ssp;
3316     
3317     ssp = (cm_SSetPref_t *)space;
3318     ssp->flags = 0;
3319     ssp->num_servers = 0;
3320     gblob.in_size = (long)(((char*)&(ssp->servers[0])) - (char *)ssp);
3321     gblob.in = space;
3322     gblob.out = space;
3323     gblob.out_size = MAXSIZE;
3324
3325     if ( !IsAdmin() ) {
3326         fprintf (stderr,"Permission denied: requires AFS Client Administrator access.\n");
3327         return EACCES;
3328     }
3329
3330     code = 0;
3331
3332     ti = as->parms[2].items;  /* -file */
3333     if (ti) {
3334         if (debug) fprintf(stderr,"opening file %s\n",ti->data);
3335         if (!(infd = fopen(ti->data,"r" ))) {
3336             code = errno;
3337             Die(errno,ti->data);
3338         }
3339         else
3340             while ( fscanf(infd, "%79s%ld", name, &rank) != EOF) {
3341                 code = addServer (name, (unsigned short) rank);
3342             }
3343     }
3344
3345     ti = as->parms[3].items;  /* -stdin */
3346     if (ti) {
3347         while ( scanf("%79s%ld", name, &rank) != EOF) {
3348             code = addServer (name, (unsigned short) rank);
3349         }
3350     }
3351
3352     for (ti = as->parms[0].items;ti;ti=ti->next) {/*list of servers, ranks */
3353         if (ti) {
3354             if (!ti->next) {
3355                 break;
3356             }
3357             code = addServer (ti->data, (unsigned short) atol(ti->next->data));
3358             if (debug)
3359                 printf("set fs prefs %s %s\n", ti->data, ti->next->data);
3360             ti=ti->next;
3361         }
3362     }
3363     code = pokeServers();
3364     if (debug) 
3365         printf("now working on vlservers, code=%d, errno=%d\n",code,errno);
3366
3367     ssp = (cm_SSetPref_t *)space;
3368     gblob.in_size = (long)(((char*)&(ssp->servers[0])) - (char *)ssp);
3369     gblob.in = space;
3370     ssp->flags = CM_SPREF_VLONLY;
3371     ssp->num_servers = 0;
3372
3373     for (ti = as->parms[1].items;ti;ti=ti->next) { /* list of dbservers, ranks */
3374         if (ti) {
3375             if (!ti->next) {
3376                 break;
3377             }
3378             code = addServer (ti->data, (unsigned short) atol(ti->next->data));
3379             if (debug) 
3380                 printf("set vl prefs %s %s\n", ti->data, ti->next->data);
3381             ti=ti->next;
3382         }
3383     }
3384
3385     if (as->parms[1].items) {
3386         if (debug) 
3387             printf("now poking vlservers\n");
3388         code = pokeServers();
3389     }
3390
3391     if (code) 
3392         Die(errno,0);
3393
3394     return code;
3395 }
3396 #else
3397 static int
3398 SetPrefCmd(struct cmd_syndesc *as, void *arock)
3399 {
3400     FILE *infd;
3401     afs_int32 code;
3402     struct cmd_item *ti;
3403     char name[80];
3404     afs_int32 rank;
3405     struct setspref *ssp;
3406     int error = 0;              /* -1 means error message printed,
3407                                  * >0 means errno value for unprinted message */
3408
3409     ssp = (struct setspref *)space;
3410     ssp->flags = 0;
3411     ssp->num_servers = 0;
3412     gblob.in_size = ((char *)&(ssp->servers[0])) - (char *)ssp;
3413     gblob.in = space;
3414     gblob.out = space;
3415     gblob.out_size = MAXSIZE;
3416
3417
3418     if (geteuid()) {
3419         fprintf(stderr, "Permission denied: requires root access.\n");
3420         return 1;
3421     }
3422
3423     ti = as->parms[2].items;    /* -file */
3424     if (ti) {
3425         if (debug)
3426             fprintf(stderr, "opening file %s\n", ti->data);
3427         if (!(infd = fopen(ti->data, "r"))) {
3428             perror(ti->data);
3429             error = -1;
3430         } else {
3431             while (fscanf(infd, "%79s%ld", name, &rank) != EOF) {
3432                 code = addServer(name, (unsigned short)rank);
3433                 if (code)
3434                     error = code;
3435             }
3436         }
3437     }
3438
3439     ti = as->parms[3].items;    /* -stdin */
3440     if (ti) {
3441         while (scanf("%79s%ld", name, &rank) != EOF) {
3442             code = addServer(name, (unsigned short)rank);
3443             if (code)
3444                 error = code;
3445         }
3446     }
3447
3448     for (ti = as->parms[0].items; ti; ti = ti->next) {  /* list of servers, ranks */
3449         if (ti) {
3450             if (!ti->next) {
3451                 break;
3452             }
3453             code = addServer(ti->data, (unsigned short)atol(ti->next->data));
3454             if (code)
3455                 error = code;
3456             if (debug)
3457                 printf("set fs prefs %s %s\n", ti->data, ti->next->data);
3458             ti = ti->next;
3459         }
3460     }
3461     code = pokeServers();
3462     if (code)
3463         error = code;
3464     if (debug)
3465         printf("now working on vlservers, code=%d\n", code);
3466
3467     ssp = (struct setspref *)space;
3468     ssp->flags = DBservers;
3469     ssp->num_servers = 0;
3470     gblob.in_size = ((char *)&(ssp->servers[0])) - (char *)ssp;
3471     gblob.in = space;
3472
3473     for (ti = as->parms[1].items; ti; ti = ti->next) {  /* list of dbservers, ranks */
3474         if (ti) {
3475             if (!ti->next) {
3476                 break;
3477             }
3478             code = addServer(ti->data, (unsigned short)atol(ti->next->data));
3479             if (code)
3480                 error = code;
3481             if (debug)
3482                 printf("set vl prefs %s %s\n", ti->data, ti->next->data);
3483             ti = ti->next;
3484         }
3485     }
3486
3487     if (as->parms[1].items) {
3488         if (debug)
3489             printf("now poking vlservers\n");
3490         code = pokeServers();
3491         if (code)
3492             error = code;
3493     }
3494
3495     if (error > 0)
3496         Die(error, 0);
3497
3498     return error ? 1 : 0;
3499 }
3500 #endif /* WIN32 */
3501
3502 #ifdef WIN32
3503 static int 
3504 GetPrefCmd(struct cmd_syndesc *as, void *arock)
3505 {
3506     afs_int32 code;
3507     struct cmd_item *ti;
3508     char *name, tbuffer[20];
3509     afs_int32 addr;
3510     FILE * outfd;
3511     int resolve;
3512     int vlservers;
3513     struct ViceIoctl blob;
3514     struct cm_SPrefRequest *in;
3515     struct cm_SPrefInfo *out;
3516     int i;
3517     
3518     code = 0;
3519     ti = as->parms[0].items;  /* -file */
3520     if (ti) {
3521         if (debug) fprintf(stderr,"opening file %s\n",ti->data);
3522         if (!(outfd = freopen(ti->data,"w",stdout))) {
3523             Die(errno,ti->data);
3524             return errno;
3525         }
3526     }
3527
3528     ti = as->parms[1].items;  /* -numeric */
3529     resolve = !(ti);
3530     ti = as->parms[2].items;  /* -vlservers */
3531     vlservers = (ti ? CM_SPREF_VLONLY : 0);
3532     /*  ti = as->parms[3].items;   -cell */
3533
3534     in = (struct cm_SPrefRequest *)space;
3535     in->offset = 0;
3536
3537     do {
3538         blob.in_size=sizeof(struct cm_SPrefRequest);
3539         blob.in = (char *)in;
3540         blob.out = space;
3541         blob.out_size = MAXSIZE;
3542
3543         in->num_servers = (MAXSIZE - 2*sizeof(short))/sizeof(struct cm_SPref);
3544         in->flags = vlservers; 
3545
3546         code = pioctl(0, VIOC_GETSPREFS, &blob, 1);
3547         if (code){
3548             perror("getserverprefs pioctl");
3549             Die (errno,0);
3550         }
3551         else {
3552             out = (struct cm_SPrefInfo *) blob.out;
3553
3554             for (i=0;i<out->num_servers;i++) {
3555                 if (resolve) {
3556                     name = hostutil_GetNameByINet(out->servers[i].host.s_addr);
3557                 }
3558                 else {
3559                     addr = ntohl(out->servers[i].host.s_addr);
3560                     sprintf(tbuffer, "%d.%d.%d.%d", (addr>>24) & 0xff, (addr>>16) & 0xff,
3561                              (addr>>8) & 0xff, addr & 0xff);
3562                     name=tbuffer;
3563                 }
3564                 printf ("%-50s %5u\n",name,out->servers[i].rank);      
3565             }
3566
3567             in->offset = out->next_offset;
3568         }
3569     } while (!code && out->next_offset > 0);
3570
3571     return code;
3572 }
3573 #else
3574 static int
3575 GetPrefCmd(struct cmd_syndesc *as, void *arock)
3576 {
3577     afs_int32 code;
3578     struct cmd_item *ti;
3579     char *name, tbuffer[20];
3580     afs_int32 rank, addr;
3581     FILE *outfd;
3582     int resolve;
3583     int vlservers = 0;
3584     struct ViceIoctl blob;
3585     struct sprefrequest *in;
3586     struct sprefinfo *out;
3587     int i;
3588
3589     ti = as->parms[0].items;    /* -file */
3590     if (ti) {
3591         if (debug)
3592             fprintf(stderr, "opening file %s\n", ti->data);
3593         if (!(outfd = freopen(ti->data, "w", stdout))) {
3594             perror(ti->data);
3595             return 1;
3596         }
3597     }
3598
3599     ti = as->parms[1].items;    /* -numeric */
3600     resolve = !(ti);
3601     ti = as->parms[2].items;    /* -vlservers */
3602     vlservers |= (ti ? DBservers : 0);
3603     /*  ti = as->parms[3].items;   -cell */
3604
3605     in = (struct sprefrequest *)space;
3606     in->offset = 0;
3607
3608     do {
3609         blob.in_size = sizeof(struct sprefrequest);
3610         blob.in = (char *)in;
3611         blob.out = space;
3612         blob.out_size = MAXSIZE;
3613
3614         in->num_servers =
3615             (MAXSIZE - 2 * sizeof(short)) / sizeof(struct spref);
3616         in->flags = vlservers;
3617
3618         code = pioctl(0, VIOC_GETSPREFS, &blob, 1);
3619         if (code) {
3620             perror("getserverprefs pioctl");
3621             return 1;
3622         }
3623
3624         out = (struct sprefinfo *)blob.out;
3625
3626         for (i = 0; i < out->num_servers; i++) {
3627             if (resolve) {
3628                 name = hostutil_GetNameByINet(out->servers[i].server.s_addr);
3629             } else {
3630                 addr = ntohl(out->servers[i].server.s_addr);
3631                 sprintf(tbuffer, "%d.%d.%d.%d", (addr >> 24) & 0xff,
3632                         (addr >> 16) & 0xff, (addr >> 8) & 0xff, addr & 0xff);
3633                 name = tbuffer;
3634             }
3635             printf("%-50s %5u\n", name, out->servers[i].rank);
3636         }
3637
3638         in->offset = out->next_offset;
3639     } while (out->next_offset > 0);
3640
3641     return 0;
3642 }
3643 #endif /* WIN32 */
3644
3645 static int
3646 UuidCmd(struct cmd_syndesc *asp, void *arock)
3647 {
3648     long code;
3649     long inValue;
3650     afsUUID outValue;
3651     struct ViceIoctl blob;
3652     char * uuidstring = NULL;
3653
3654     if (asp->parms[0].items) {
3655 #ifdef WIN32
3656         if ( !IsAdmin() ) {
3657             fprintf (stderr,"Permission denied: requires AFS Client Administrator access.\n");
3658             return EACCES;
3659         }
3660 #else
3661         if (geteuid()) {
3662             fprintf (stderr, "Permission denied: requires root access.\n");
3663             return EACCES;
3664         }
3665 #endif
3666         inValue = 1;            /* generate new UUID */
3667     } else {
3668         inValue = 0;            /* just show the current UUID */
3669     }
3670
3671     blob.in_size = sizeof(inValue);
3672     blob.in = (char *) &inValue;
3673     blob.out_size = sizeof(outValue);
3674     blob.out = (char *) &outValue;
3675
3676     code = pioctl(NULL, VIOC_UUIDCTL, &blob, 1);
3677     if (code) {
3678         Die(errno, NULL);
3679         return code;
3680     }
3681
3682     UuidToString((UUID *) &outValue, &uuidstring);
3683
3684     printf("%sUUID: %s",
3685            ((inValue == 1)?"New ":""),
3686            uuidstring);
3687
3688     if (uuidstring)
3689         RpcStringFree(&uuidstring);
3690
3691     return 0;
3692 }
3693
3694 static int
3695 TraceCmd(struct cmd_syndesc *asp, void *arock)
3696 {
3697     long code;
3698     struct ViceIoctl blob;
3699     long inValue;
3700     long outValue;
3701     
3702 #ifdef WIN32
3703     if ( !IsAdmin() ) {
3704         fprintf (stderr,"Permission denied: requires AFS Client Administrator access.\n");
3705         return EACCES;
3706     }
3707 #else /* WIN32 */
3708         if (geteuid()) {
3709             fprintf (stderr,"Permission denied: requires root access.\n");
3710             return EACCES;
3711         }
3712 #endif /* WIN32 */
3713
3714     if ((asp->parms[0].items && asp->parms[1].items)) {
3715         fprintf(stderr, "fs trace: must use at most one of '-off' or '-on'\n");
3716         return EINVAL;
3717     }
3718         
3719     /* determine if we're turning this tracing on or off */
3720     inValue = 0;
3721     if (asp->parms[0].items)
3722         inValue = 3;            /* enable */
3723     else if (asp->parms[1].items) 
3724         inValue = 2;    /* disable */
3725     if (asp->parms[2].items) 
3726         inValue |= 4;           /* do reset */
3727     if (asp->parms[3].items) 
3728         inValue |= 8;           /* dump */
3729         
3730     blob.in_size = sizeof(long);
3731     blob.in = (char *) &inValue;
3732     blob.out_size = sizeof(long);
3733     blob.out = (char *) &outValue;
3734         
3735     code = pioctl(NULL, VIOC_TRACECTL, &blob, 1);
3736     if (code) {
3737         Die(errno, NULL);
3738         return code;
3739     }
3740
3741     if (outValue) 
3742         printf("AFS tracing enabled.\n");
3743     else 
3744         printf("AFS tracing disabled.\n");
3745
3746     return 0;
3747 }
3748
3749 static void sbusage(void)
3750 {
3751     fprintf(stderr, "example usage: %s storebehind -files *.o -kb 99999 -default 0\n", pn);
3752     fprintf(stderr, "               %s sb 50000 *.[ao] -default 10\n", pn);
3753 }       
3754
3755 /* fs sb -kbytes 9999 -files *.o -default 64 */
3756 static int
3757 StoreBehindCmd(struct cmd_syndesc *as, void *arock)
3758 {
3759     afs_int32 code = 0;
3760     struct ViceIoctl blob;
3761     struct cmd_item *ti;
3762     struct sbstruct tsb, tsb2;
3763     int verbose = 0;
3764     afs_int32 allfiles;
3765     char *t;
3766     int error = 0;
3767
3768 #ifdef WIN32
3769     if ( !IsAdmin() ) {
3770         fprintf (stderr,"Permission denied: requires AFS Client Administrator access.\n");  
3771         return EACCES;
3772     }
3773 #endif /* WIN32 */
3774
3775     tsb.sb_thisfile = -1;
3776     ti = as->parms[0].items;    /* -kbytes */
3777     if (ti) {
3778         if (!as->parms[1].items) {
3779             fprintf(stderr, "%s: you must specify -files with -kbytes.\n",
3780                     pn);
3781             return 1;
3782         }
3783         tsb.sb_thisfile = strtol(ti->data, &t, 10) * 1024;
3784         if ((tsb.sb_thisfile < 0) || (t != ti->data + strlen(ti->data))) {
3785             fprintf(stderr, "%s: %s must be 0 or a positive number.\n", pn,
3786                     ti->data);
3787             return 1;
3788         }
3789     }
3790
3791     allfiles = tsb.sb_default = -1;     /* Don't set allfiles yet */
3792     ti = as->parms[2].items;    /* -allfiles */
3793     if (ti) {
3794         allfiles = strtol(ti->data, &t, 10) * 1024;
3795         if ((allfiles < 0) || (t != ti->data + strlen(ti->data))) {
3796             fprintf(stderr, "%s: %s must be 0 or a positive number.\n", pn,
3797                     ti->data);
3798             return 1;
3799         }
3800     }
3801
3802     /* -verbose or -file only or no options */
3803     if (as->parms[3].items || (as->parms[1].items && !as->parms[0].items)
3804         || (!as->parms[0].items && !as->parms[1].items
3805             && !as->parms[2].items))
3806         verbose = 1;
3807
3808     blob.in = (char *)&tsb;
3809     blob.out = (char *)&tsb2;
3810     blob.in_size = blob.out_size = sizeof(struct sbstruct);
3811     memset(&tsb2, 0, sizeof(tsb2));
3812
3813     /* once per -file */
3814     for (ti = as->parms[1].items; ti; ti = ti->next) {
3815         /* Do this solely to see if the file is there */
3816         code = pioctl(ti->data, VIOCWHEREIS, &blob, 1);
3817         if (code) {
3818             Die(errno, ti->data);
3819             error = 1;
3820             continue;
3821         }
3822
3823         code = pioctl(ti->data, VIOC_STOREBEHIND, &blob, 1);
3824         if (code) {
3825             Die(errno, ti->data);
3826             error = 1;
3827             continue;
3828         }
3829
3830         if (verbose && (blob.out_size == sizeof(tsb2))) {
3831             if (tsb2.sb_thisfile == -1) {
3832                 fprintf(stdout, "Will store %s according to default.\n",
3833                         ti->data);
3834             } else {
3835                 fprintf(stdout,
3836                         "Will store up to %d kbytes of %s asynchronously.\n",
3837                         (tsb2.sb_thisfile / 1024), ti->data);
3838             }
3839         }
3840     }
3841
3842     /* If no files - make at least one pioctl call, or
3843      * set the allfiles default if we need to.
3844      */
3845     if (!as->parms[1].items || (allfiles != -1)) {
3846         tsb.sb_default = allfiles;
3847         code = pioctl(0, VIOC_STOREBEHIND, &blob, 1);
3848         if (code) {
3849             Die(errno, ((allfiles == -1) ? 0 : "-allfiles"));
3850             error = 1;
3851         }
3852     }
3853
3854     /* Having no arguments also reports the default store asynchrony */
3855     if (verbose && (blob.out_size == sizeof(tsb2))) {
3856         fprintf(stdout, "Default store asynchrony is %d kbytes.\n",
3857                 (tsb2.sb_default / 1024));
3858     }
3859
3860     return error;
3861 }
3862
3863 static afs_int32 
3864 SetCryptCmd(struct cmd_syndesc *as, void *arock)
3865 {
3866     afs_int32 code = 0, flag;
3867     struct ViceIoctl blob;
3868     char *tp;
3869  
3870 #ifdef WIN32
3871     if ( !IsAdmin() ) {
3872         fprintf (stderr,"Permission denied: requires AFS Client Administrator access.\n");
3873         return EACCES;
3874     }
3875 #endif /* WIN32 */
3876
3877     tp = as->parms[0].items->data;
3878     if (strcmp(tp, "on") == 0)
3879       flag = 1;
3880     else if (strcmp(tp, "off") == 0)
3881       flag = 0;
3882     else {
3883       fprintf (stderr, "%s: %s must be \"on\" or \"off\".\n", pn, tp);
3884       return EINVAL;
3885     }
3886
3887     blob.in = (char *) &flag;
3888     blob.in_size = sizeof(flag);
3889     blob.out_size = 0;
3890     code = pioctl(0, VIOC_SETRXKCRYPT, &blob, 1);
3891     if (code)
3892         Die(code, NULL);
3893     return 0;
3894 }
3895
3896 static afs_int32 
3897 GetCryptCmd(struct cmd_syndesc *as, void *arock)
3898 {
3899     afs_int32 code = 0, flag;
3900     struct ViceIoctl blob;
3901     char *tp;
3902  
3903     blob.in = NULL;
3904     blob.in_size = 0;
3905     blob.out_size = sizeof(flag);
3906     blob.out = space;
3907
3908     code = pioctl(0, VIOC_GETRXKCRYPT, &blob, 1);
3909
3910     if (code) 
3911         Die(code, NULL);
3912     else {
3913       tp = space;
3914       memcpy(&flag, tp, sizeof(afs_int32));
3915       printf("Security level is currently ");
3916       if (flag == 1)
3917         printf("crypt (data security).\n");
3918       else
3919         printf("clear.\n");
3920     }
3921     return 0;
3922 }
3923
3924 static int
3925 MemDumpCmd(struct cmd_syndesc *asp, void *arock)
3926 {
3927     long code;
3928     struct ViceIoctl blob;
3929     long inValue = 0;
3930     long outValue;
3931
3932     if ( !IsAdmin() ) {
3933         fprintf (stderr,"Permission denied: requires AFS Client Administrator access.\n");
3934         return EACCES;
3935     }
3936
3937     if ((asp->parms[0].items && asp->parms[1].items)) {
3938         fprintf(stderr, "%s trace: must use at most one of '-begin' or '-end'\n", pn);
3939         return EINVAL;
3940     }
3941
3942     /* determine if we're turning this tracing on or off */
3943     if (asp->parms[0].items)
3944         inValue = 1;            /* begin */
3945     else if (asp->parms[1].items)
3946         inValue = 0;            /* end */
3947
3948
3949     blob.in_size = sizeof(long);
3950     blob.in = (char *) &inValue;
3951     blob.out_size = sizeof(long);
3952     blob.out = (char *) &outValue;
3953
3954     code = pioctl(NULL, VIOC_TRACEMEMDUMP, &blob, 1);
3955     if (code) {
3956         Die(errno, NULL);
3957         return code;
3958     }
3959
3960     if (!outValue) { 
3961         printf("AFS memdump created.\n");
3962         return 0;
3963     } else {
3964         printf("AFS memdump failed.\n");
3965         return -1;
3966     }
3967 }
3968
3969 static int
3970 MiniDumpCmd(struct cmd_syndesc *asp, void *arock)
3971 {
3972     BOOL success = 0;
3973     SERVICE_STATUS status;
3974     SC_HANDLE hManager = NULL;
3975     SC_HANDLE hService = NULL;
3976
3977     if ( !IsAdmin() ) {
3978         fprintf (stderr,"Permission denied: requires AFS Client Administrator access.\n");
3979         return EACCES;
3980     }
3981
3982     hManager = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT);
3983     if (!hManager)
3984         goto failure;
3985
3986     hService = OpenService(hManager, "TransarcAFSDaemon", SERVICE_USER_DEFINED_CONTROL);
3987     if (!hService)
3988         goto failure;
3989
3990     success = ControlService(hService, SERVICE_CONTROL_CUSTOM_DUMP, &status);
3991
3992     if (success) {
3993         CloseServiceHandle(hService);
3994         CloseServiceHandle(hManager);
3995
3996         printf("AFS minidump generated.\n");
3997         return 0;
3998     }
3999
4000   failure: 
4001     if (hService)
4002         CloseServiceHandle(hService);
4003     if (hManager)
4004         CloseServiceHandle(hManager);
4005
4006     printf("AFS minidump failed.\n");
4007     return -1;
4008 }
4009
4010 static int
4011 CSCPolicyCmd(struct cmd_syndesc *asp, void *arock)
4012 {
4013     struct cmd_item *ti;
4014     char *share = NULL;
4015     HKEY hkCSCPolicy;
4016
4017     if ( !IsAdmin() ) {
4018         fprintf (stderr,"Permission denied: requires AFS Client Administrator access.\n");
4019         return EACCES;
4020     }
4021
4022     for(ti=asp->parms[0].items; ti;ti=ti->next) {
4023         share = ti->data;
4024         if (share)
4025         {
4026             break;
4027         }
4028     }
4029
4030     if (share)
4031     {
4032         char *policy;
4033
4034         RegCreateKeyEx( HKEY_LOCAL_MACHINE, 
4035                          AFSREG_CLT_OPENAFS_SUBKEY "\\CSCPolicy",
4036                         0, 
4037                         "AFS", 
4038                         REG_OPTION_NON_VOLATILE,
4039                         (IsWow64()?KEY_WOW64_64KEY:0)|KEY_WRITE,
4040                         NULL, 
4041                         &hkCSCPolicy,
4042                         NULL );
4043
4044         if ( hkCSCPolicy == NULL ) {
4045             fprintf (stderr,"Permission denied: requires Administrator access.\n");
4046             return EACCES;
4047         }
4048
4049         if ( !IsAdmin() ) {
4050             fprintf (stderr,"Permission denied: requires AFS Client Administrator access.\n");
4051             RegCloseKey(hkCSCPolicy);
4052             return EACCES;
4053         }
4054
4055         policy = "manual";
4056                 
4057         if (asp->parms[1].items)
4058             policy = "manual";
4059         if (asp->parms[2].items)
4060             policy = "programs";
4061         if (asp->parms[3].items)
4062             policy = "documents";
4063         if (asp->parms[4].items)
4064             policy = "disable";
4065                 
4066         RegSetValueEx( hkCSCPolicy, share, 0, REG_SZ, policy, (DWORD)strlen(policy)+1);
4067                 
4068         printf("CSC policy on share \"%s\" changed to \"%s\".\n\n", share, policy);
4069         printf("Close all applications that accessed files on this share or restart AFS Client for the change to take effect.\n"); 
4070     }
4071     else
4072     {
4073         DWORD dwIndex, dwPolicies;
4074         char policyName[256];
4075         DWORD policyNameLen;
4076         char policy[256];
4077         DWORD policyLen;
4078         DWORD dwType;
4079
4080         /* list current csc policies */
4081
4082         RegCreateKeyEx( HKEY_LOCAL_MACHINE, 
4083                         AFSREG_CLT_OPENAFS_SUBKEY "\\CSCPolicy",
4084                         0, 
4085                         "AFS", 
4086                         REG_OPTION_NON_VOLATILE,
4087                         (IsWow64()?KEY_WOW64_64KEY:0)|KEY_READ|KEY_QUERY_VALUE,
4088                         NULL, 
4089                         &hkCSCPolicy,
4090                         NULL );
4091
4092         RegQueryInfoKey( hkCSCPolicy,
4093                          NULL,  /* lpClass */
4094                          NULL,  /* lpcClass */
4095                          NULL,  /* lpReserved */
4096                          NULL,  /* lpcSubKeys */
4097                          NULL,  /* lpcMaxSubKeyLen */
4098                          NULL,  /* lpcMaxClassLen */
4099                          &dwPolicies, /* lpcValues */
4100                          NULL,  /* lpcMaxValueNameLen */
4101                          NULL,  /* lpcMaxValueLen */
4102                          NULL,  /* lpcbSecurityDescriptor */
4103                          NULL   /* lpftLastWriteTime */
4104                          );
4105                 
4106         printf("Current CSC policies:\n");
4107         for ( dwIndex = 0; dwIndex < dwPolicies; dwIndex ++ ) {
4108
4109             policyNameLen = sizeof(policyName);
4110             policyLen = sizeof(policy);
4111             RegEnumValue( hkCSCPolicy, dwIndex, policyName, &policyNameLen, NULL,
4112                           &dwType, policy, &policyLen);
4113
4114             printf("  %s = %s\n", policyName, policy);
4115         }
4116     }
4117
4118     RegCloseKey(hkCSCPolicy);
4119     return (0);
4120 }
4121
4122 #ifndef WIN32
4123 /* get clients interface addresses */
4124 static int
4125 GetClientAddrsCmd(struct cmd_syndesc *as, void *arock)
4126 {
4127     afs_int32 code;
4128     struct cmd_item *ti;
4129     char *name;
4130     struct ViceIoctl blob;
4131     struct sprefrequest *in;
4132     struct sprefinfo *out;
4133
4134     in = (struct sprefrequest *)space;
4135     in->offset = 0;
4136
4137     do {
4138         blob.in_size = sizeof(struct sprefrequest);
4139         blob.in = (char *)in;
4140         blob.out = space;
4141         blob.out_size = MAXSIZE;
4142
4143         in->num_servers =
4144             (MAXSIZE - 2 * sizeof(short)) / sizeof(struct spref);
4145         /* returns addr in network byte order */
4146         code = pioctl(0, VIOC_GETCPREFS, &blob, 1);
4147         if (code) {
4148             perror("getClientInterfaceAddr pioctl");
4149             return 1;
4150         }
4151
4152         {
4153             int i;
4154             out = (struct sprefinfo *)blob.out;
4155             for (i = 0; i < out->num_servers; i++) {
4156                 afs_int32 addr;
4157                 char tbuffer[32];
4158                 addr = ntohl(out->servers[i].server.s_addr);
4159                 sprintf(tbuffer, "%d.%d.%d.%d", (addr >> 24) & 0xff,
4160                         (addr >> 16) & 0xff, (addr >> 8) & 0xff, addr & 0xff);
4161                 printf("%-50s\n", tbuffer);
4162             }
4163             in->offset = out->next_offset;
4164         }
4165     } while (out->next_offset > 0);
4166
4167     return 0;
4168 }
4169
4170 static int
4171 SetClientAddrsCmd(struct cmd_syndesc *as, void *arock)
4172 {
4173     afs_int32 code, addr;
4174     struct cmd_item *ti;
4175     char name[80];
4176     struct ViceIoctl blob;
4177     struct setspref *ssp;
4178     int sizeUsed = 0, i, flag;
4179     afs_int32 existingAddr[1024];       /* existing addresses on this host */
4180     int existNu;
4181     int error = 0;
4182
4183     ssp = (struct setspref *)space;
4184     ssp->num_servers = 0;
4185     blob.in = space;
4186     blob.out = space;
4187     blob.out_size = MAXSIZE;
4188
4189     if (geteuid()) {
4190         fprintf(stderr, "Permission denied: requires root access.\n");
4191         return 1;
4192     }
4193
4194     /* extract all existing interface addresses */
4195     existNu = rx_getAllAddr(existingAddr, 1024);
4196     if (existNu < 0)
4197         return 1;
4198
4199     sizeUsed = sizeof(struct setspref); /* space used in ioctl buffer */
4200     for (ti = as->parms[0].items; ti; ti = ti->next) {
4201         if (sizeUsed >= sizeof(space)) {
4202             fprintf(stderr, "No more space\n");
4203             return 1;
4204         }
4205         addr = extractAddr(ti->data, 20);       /* network order */
4206         if ((addr == AFS_IPINVALID) || (addr == AFS_IPINVALIDIGNORE)) {
4207             fprintf(stderr, "Error in specifying address: %s..ignoring\n",
4208                     ti->data);
4209             error = 1;
4210             continue;
4211         }
4212         /* see if it is an address that really exists */
4213         for (flag = 0, i = 0; i < existNu; i++)
4214             if (existingAddr[i] == addr) {
4215                 flag = 1;
4216                 break;
4217             }
4218         if (!flag) {            /* this is an nonexistent address */
4219             fprintf(stderr, "Nonexistent address: 0x%08x..ignoring\n", addr);
4220             error = 1;
4221             continue;
4222         }
4223         /* copy all specified addr into ioctl buffer */
4224         (ssp->servers[ssp->num_servers]).server.s_addr = addr;
4225         printf("Adding 0x%08x\n", addr);
4226         ssp->num_servers++;
4227         sizeUsed += sizeof(struct spref);
4228     }
4229     if (ssp->num_servers < 1) {
4230         fprintf(stderr, "No addresses specified\n");
4231         return 1;
4232     }
4233     blob.in_size = sizeUsed - sizeof(struct spref);
4234
4235     code = pioctl(0, VIOC_SETCPREFS, &blob, 1); /* network order */
4236     if (code) {
4237         Die(errno, 0);
4238         error = 1;
4239     }
4240
4241     return error;
4242 }
4243
4244 static int
4245 FlushMountCmd(struct cmd_syndesc *as, void *arock)
4246 {
4247     afs_int32 code;
4248     struct ViceIoctl blob;
4249     struct cmd_item *ti;
4250     char orig_name[1024];       /*Original name, may be modified */
4251     char true_name[1024];       /*``True'' dirname (e.g., symlink target) */
4252     char parent_dir[1024];      /*Parent directory of true name */
4253     char *last_component;       /*Last component of true name */
4254     struct stat statbuff;       /*Buffer for status info */
4255     int link_chars_read;        /*Num chars read in readlink() */
4256     int thru_symlink;           /*Did we get to a mount point via a symlink? */
4257     int error = 0;
4258
4259     for (ti = as->parms[0].items; ti; ti = ti->next) {
4260         /* once per file */
4261         thru_symlink = 0;
4262         sprintf(orig_name, "%s%s", (ti->data[0] == '/') ? "" : "./",
4263                 ti->data);
4264
4265         if (lstat(orig_name, &statbuff) < 0) {
4266             /* if lstat fails, we should still try the pioctl, since it
4267              * may work (for example, lstat will fail, but pioctl will
4268              * work if the volume of offline (returning ENODEV). */
4269             statbuff.st_mode = S_IFDIR; /* lie like pros */
4270         }
4271
4272         /*
4273          * The lstat succeeded.  If the given file is a symlink, substitute
4274          * the file name with the link name.
4275          */
4276         if ((statbuff.st_mode & S_IFMT) == S_IFLNK) {
4277             thru_symlink = 1;
4278             /*
4279              * Read name of resolved file.
4280              */
4281             link_chars_read = readlink(orig_name, true_name, 1024);
4282             if (link_chars_read <= 0) {
4283                 fprintf(stderr,
4284                         "%s: Can't read target name for '%s' symbolic link!\n",
4285                         pn, orig_name);
4286                 error = 1;
4287                 continue;
4288             }
4289
4290             /*
4291              * Add a trailing null to what was read, bump the length.
4292              */
4293             true_name[link_chars_read++] = 0;
4294
4295             /*
4296              * If the symlink is an absolute pathname, we're fine.  Otherwise, we
4297              * have to create a full pathname using the original name and the
4298              * relative symlink name.  Find the rightmost slash in the original
4299              * name (we know there is one) and splice in the symlink value.
4300              */
4301             if (true_name[0] != '/') {
4302                 last_component = (char *)strrchr(orig_name, '/');
4303                 strcpy(++last_component, true_name);
4304                 strcpy(true_name, orig_name);
4305             }
4306         } else
4307             strcpy(true_name, orig_name);
4308
4309         /*
4310          * Find rightmost slash, if any.
4311          */
4312         last_component = (char *)strrchr(true_name, '/');
4313         if (last_component) {
4314             /*
4315              * Found it.  Designate everything before it as the parent directory,
4316              * everything after it as the final component.
4317              */
4318             strncpy(parent_dir, true_name, last_component - true_name);
4319             parent_dir[last_component - true_name] = 0;
4320             last_component++;   /*Skip the slash */
4321         } else {
4322             /*
4323              * No slash appears in the given file name.  Set parent_dir to the current
4324              * directory, and the last component as the given name.
4325              */
4326             strcpy(parent_dir, ".");
4327             last_component = true_name;
4328         }
4329
4330         if (strcmp(last_component, ".") == 0
4331             || strcmp(last_component, "..") == 0) {
4332             fprintf(stderr,
4333                     "%s: you may not use '.' or '..' as the last component\n",
4334                     pn);
4335             fprintf(stderr, "%s: of a name in the 'fs flushmount' command.\n",
4336                     pn);
4337             error = 1;
4338             continue;
4339         }
4340
4341         blob.in = last_component;
4342         blob.in_size = strlen(last_component) + 1;
4343         blob.out_size = 0;
4344         memset(space, 0, MAXSIZE);
4345
4346         code = pioctl(parent_dir, VIOC_AFS_FLUSHMOUNT, &blob, 1);
4347
4348         if (code != 0) {
4349             if (errno == EINVAL) {
4350                 fprintf(stderr, "'%s' is not a mount point.\n", ti->data);
4351             } else {
4352                 Die(errno, (ti->data ? ti->data : parent_dir));
4353             }
4354             error = 1;
4355         }
4356     }
4357     return error;
4358 }
4359 #endif /* WIN32 */
4360
4361 static int
4362 RxStatProcCmd(struct cmd_syndesc *as, void *arock)
4363 {
4364     afs_int32 code;
4365     afs_int32 flags = 0;
4366     struct ViceIoctl blob;
4367
4368     if (as->parms[0].items) {   /* -enable */
4369         flags |= AFSCALL_RXSTATS_ENABLE;
4370     }
4371     if (as->parms[1].items) {   /* -disable */
4372         flags |= AFSCALL_RXSTATS_DISABLE;
4373     }
4374     if (as->parms[2].items) {   /* -clear */
4375         flags |= AFSCALL_RXSTATS_CLEAR;
4376     }
4377     if (flags == 0) {
4378         fprintf(stderr, "You must specify at least one argument\n");
4379         return 1;
4380     }
4381
4382     blob.in = (char *)&flags;
4383     blob.in_size = sizeof(afs_int32);
4384     blob.out_size = 0;
4385
4386     code = pioctl(NULL, VIOC_RXSTAT_PROC, &blob, 1);
4387     if (code != 0) {
4388         Die(errno, NULL);
4389         return 1;
4390     }
4391
4392     return 0;
4393 }
4394
4395 static int
4396 RxStatPeerCmd(struct cmd_syndesc *as, void *arock)
4397 {
4398     afs_int32 code;
4399     afs_int32 flags = 0;
4400     struct ViceIoctl blob;
4401
4402     if (as->parms[0].items) {   /* -enable */
4403         flags |= AFSCALL_RXSTATS_ENABLE;
4404     }
4405     if (as->parms[1].items) {   /* -disable */
4406         flags |= AFSCALL_RXSTATS_DISABLE;
4407     }
4408     if (as->parms[2].items) {   /* -clear */
4409         flags |= AFSCALL_RXSTATS_CLEAR;
4410     }
4411     if (flags == 0) {
4412         fprintf(stderr, "You must specify at least one argument\n");
4413         return 1;
4414     }
4415
4416     blob.in = (char *)&flags;
4417     blob.in_size = sizeof(afs_int32);
4418     blob.out_size = 0;
4419
4420     code = pioctl(NULL, VIOC_RXSTAT_PEER, &blob, 1);
4421     if (code != 0) {
4422         Die(errno, NULL);
4423         return 1;
4424     }
4425
4426     return 0;
4427 }
4428
4429 static int
4430 TestVolStatCmd(struct cmd_syndesc *as, void *arock)
4431 {
4432     afs_int32 code;
4433     struct VolStatTest test;
4434     struct ViceIoctl blob;
4435     char * tp;
4436     afs_uint32 n;
4437
4438     memset(&test, 0, sizeof(test));
4439
4440     if (as->parms[0].items) {   /* -network */
4441         tp = as->parms[0].items->data;
4442         if (strcmp(tp, "up") == 0)
4443             test.flags |= VOLSTAT_TEST_NETWORK_UP;
4444         else if (strcmp(tp, "down") == 0)
4445             test.flags |= VOLSTAT_TEST_NETWORK_DOWN;
4446         else {
4447             fprintf (stderr, "%s: %s must be \"up\" or \"down\".\n", pn, tp);
4448             return EINVAL;
4449         }
4450     }
4451     if (as->parms[1].items) {   /* check */
4452         test.flags |= VOLSTAT_TEST_CHECK_VOLUME;
4453     }
4454     if (as->parms[2].items) {   /* cell */
4455         tp = as->parms[2].items->data;
4456         n = atoi(tp);
4457         if (n != 0)
4458             test.fid.cell = n;
4459         else {
4460             strncpy(test.cellname, tp, sizeof(test.cellname));
4461             test.cellname[sizeof(test.cellname)-1] = '\0';
4462         }
4463     }
4464     if (as->parms[3].items) {   /* volume */
4465         tp = as->parms[3].items->data;
4466         n = atoi(tp);
4467         if (n != 0)
4468             test.fid.volume = n;
4469         else {
4470             strncpy(test.volname, tp, sizeof(test.volname));
4471             test.volname[sizeof(test.volname)-1] = '\0';
4472         }
4473     }
4474     if (as->parms[4].items) {   /* state */
4475         tp = as->parms[4].items->data;
4476         if (strcmp(tp, "online") == 0)
4477             test.state = vl_online;
4478         else if (strcmp(tp, "busy") == 0)
4479             test.state = vl_busy;
4480         else if (strcmp(tp, "offline") == 0)
4481             test.state = vl_offline;
4482         else if (strcmp(tp, "down") == 0)
4483             test.state = vl_alldown;
4484         else {
4485             fprintf (stderr, "%s: %s must be \"online\", \"busy\", \"offline\" or \"down\".\n", pn, tp);
4486             return EINVAL;
4487         }
4488     }
4489
4490     if ((test.fid.cell || test.cellname[0]) && !(test.fid.volume || test.volname[0]) ||
4491          !(test.fid.cell || test.cellname[0]) && (test.fid.volume || test.volname[0])) {
4492         fprintf (stderr, "%s: both a cell and a volume must be specified.\n", pn, tp);
4493         return EINVAL;
4494     }
4495
4496     blob.in = (char *)&test;
4497     blob.in_size = sizeof(test);
4498     blob.out_size = 0;
4499
4500     code = pioctl(NULL, VIOC_VOLSTAT_TEST, &blob, 1);
4501     if (code != 0) {
4502         Die(errno, NULL);
4503         return 1;
4504     }
4505
4506     return 0;
4507 }
4508
4509 #ifndef WIN32
4510 #include "AFS_component_version_number.c"
4511 #endif
4512
4513 main(int argc, char **argv)
4514 {
4515     afs_int32 code;
4516     struct cmd_syndesc *ts;
4517
4518 #ifdef  AFS_AIX32_ENV
4519     /*
4520      * The following signal action for AIX is necessary so that in case of a 
4521      * crash (i.e. core is generated) we can include the user's data section 
4522      * in the core dump. Unfortunately, by default, only a partial core is
4523      * generated which, in many cases, isn't too useful.
4524      */
4525     struct sigaction nsa;
4526     
4527     sigemptyset(&nsa.sa_mask);
4528     nsa.sa_handler = SIG_DFL;
4529     nsa.sa_flags = SA_FULLDUMP;
4530     sigaction(SIGSEGV, &nsa, NULL);
4531 #endif
4532
4533 #ifdef WIN32
4534     WSADATA WSAjunk;
4535     WSAStartup(0x0101, &WSAjunk);
4536 #endif /* WIN32 */
4537
4538     /* try to find volume location information */
4539     osi_Init();
4540
4541 #ifndef WIN32
4542     ts = cmd_CreateSyntax("getclientaddrs", GetClientAddrsCmd, NULL,
4543                           "get client network interface addresses");
4544     cmd_CreateAlias(ts, "gc");
4545
4546     ts = cmd_CreateSyntax("setclientaddrs", SetClientAddrsCmd, NULL,
4547                           "set client network interface addresses");
4548     cmd_AddParm(ts, "-address", CMD_LIST, CMD_OPTIONAL | CMD_EXPANDS,
4549                 "client network interfaces");
4550     cmd_CreateAlias(ts, "sc");
4551 #endif /* WIN32 */
4552
4553     ts = cmd_CreateSyntax("setserverprefs", SetPrefCmd, NULL, "set server ranks");
4554     cmd_AddParm(ts, "-servers", CMD_LIST, CMD_OPTIONAL|CMD_EXPANDS, "fileserver names and ranks");
4555     cmd_AddParm(ts, "-vlservers", CMD_LIST, CMD_OPTIONAL|CMD_EXPANDS, "VL server names and ranks");
4556     cmd_AddParm(ts, "-file", CMD_SINGLE, CMD_OPTIONAL, "input from named file");
4557     cmd_AddParm(ts, "-stdin", CMD_FLAG, CMD_OPTIONAL, "input from stdin");
4558     cmd_CreateAlias(ts, "sp");
4559
4560     ts = cmd_CreateSyntax("getserverprefs", GetPrefCmd, NULL, "get server ranks");
4561     cmd_AddParm(ts, "-file", CMD_SINGLE, CMD_OPTIONAL, "output to named file");
4562     cmd_AddParm(ts, "-numeric", CMD_FLAG, CMD_OPTIONAL, "addresses only");
4563     cmd_AddParm(ts, "-vlservers", CMD_FLAG, CMD_OPTIONAL, "VL servers");
4564     /* cmd_AddParm(ts, "-cell", CMD_FLAG, CMD_OPTIONAL, "cellname"); */
4565     cmd_CreateAlias(ts, "gp");
4566
4567     ts = cmd_CreateSyntax("setacl", SetACLCmd, NULL, "set access control list");
4568     cmd_AddParm(ts, "-dir", CMD_LIST, 0, "directory");
4569     cmd_AddParm(ts, "-acl", CMD_LIST, 0, "access list entries");
4570     cmd_AddParm(ts, "-clear", CMD_FLAG, CMD_OPTIONAL, "clear access list");
4571     cmd_AddParm(ts, "-negative", CMD_FLAG, CMD_OPTIONAL, "apply to negative rights");
4572     parm_setacl_id = ts->nParms;
4573     cmd_AddParm(ts, "-id", CMD_FLAG, CMD_OPTIONAL, "initial directory acl (DFS only)");
4574     cmd_AddParm(ts, "-if", CMD_FLAG, CMD_OPTIONAL, "initial file acl (DFS only)");
4575     cmd_CreateAlias(ts, "sa");
4576     
4577     ts = cmd_CreateSyntax("listacl", ListACLCmd, NULL, "list access control list");
4578     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
4579     parm_listacl_id = ts->nParms;
4580     cmd_AddParm(ts, "-id", CMD_FLAG, CMD_OPTIONAL, "initial directory acl");
4581     cmd_AddParm(ts, "-if", CMD_FLAG, CMD_OPTIONAL, "initial file acl");
4582     cmd_CreateAlias(ts, "la");
4583     
4584     ts = cmd_CreateSyntax("cleanacl", CleanACLCmd, NULL, "clean up access control list");
4585     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
4586     
4587     ts = cmd_CreateSyntax("copyacl", CopyACLCmd, NULL, "copy access control list");
4588     cmd_AddParm(ts, "-fromdir", CMD_SINGLE, 0, "source directory (or DFS file)");
4589     cmd_AddParm(ts, "-todir", CMD_LIST, 0, "destination directory (or DFS file)");
4590     cmd_AddParm(ts, "-clear", CMD_FLAG, CMD_OPTIONAL, "first clear dest access list");
4591     parm_copyacl_id = ts->nParms;
4592     cmd_AddParm(ts, "-id", CMD_FLAG, CMD_OPTIONAL, "initial directory acl");
4593     cmd_AddParm(ts, "-if", CMD_FLAG, CMD_OPTIONAL, "initial file acl");
4594     
4595     cmd_CreateAlias(ts, "ca");
4596
4597     ts = cmd_CreateSyntax("flush", FlushCmd, NULL, "flush file from cache");
4598     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
4599     cmd_AddParm(ts, "-literal", CMD_FLAG, CMD_OPTIONAL, "literal evaluation of mountpoints and symlinks");
4600     
4601 #ifndef WIN32
4602     ts = cmd_CreateSyntax("flushmount", FlushMountCmd, NULL,
4603                            "flush mount symlink from cache");
4604     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
4605 #endif
4606
4607     ts = cmd_CreateSyntax("setvol", SetVolCmd, NULL, "set volume status");
4608     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
4609     cmd_AddParm(ts, "-max", CMD_SINGLE, CMD_OPTIONAL, "disk space quota in 1K units");
4610 #ifdef notdef
4611     cmd_AddParm(ts, "-min", CMD_SINGLE, CMD_OPTIONAL, "disk space guaranteed");
4612 #endif
4613     cmd_AddParm(ts, "-motd", CMD_SINGLE, CMD_OPTIONAL, "message of the day");
4614     cmd_AddParm(ts, "-offlinemsg", CMD_SINGLE, CMD_OPTIONAL, "offline message");
4615     cmd_CreateAlias(ts, "sv");
4616     
4617     ts = cmd_CreateSyntax("messages", MessagesCmd, NULL, "control Cache Manager messages");
4618     cmd_AddParm(ts, "-show", CMD_SINGLE, CMD_OPTIONAL, "[user|console|all|none]");
4619
4620     ts = cmd_CreateSyntax("examine", ExamineCmd, NULL, "display file/volume status");
4621     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
4622     cmd_AddParm(ts, "-literal", CMD_FLAG, CMD_OPTIONAL, "literal evaluation of mountpoints and symlinks");
4623     cmd_CreateAlias(ts, "lv");
4624     cmd_CreateAlias(ts, "listvol");
4625     
4626     ts = cmd_CreateSyntax("listquota", ListQuotaCmd, NULL, "list volume quota");
4627     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
4628     cmd_CreateAlias(ts, "lq");
4629     
4630     ts = cmd_CreateSyntax("diskfree", DiskFreeCmd, NULL, "show server disk space usage");
4631     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
4632     cmd_CreateAlias(ts, "df");
4633     
4634     ts = cmd_CreateSyntax("quota", QuotaCmd, NULL, "show volume quota usage");
4635     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
4636     
4637     ts = cmd_CreateSyntax("lsmount", ListMountCmd, NULL, "list mount point");    
4638     cmd_AddParm(ts, "-dir", CMD_LIST, 0, "directory");
4639     
4640     ts = cmd_CreateSyntax("mkmount", MakeMountCmd, NULL, "make mount point");
4641     cmd_AddParm(ts, "-dir", CMD_SINGLE, 0, "directory");
4642     cmd_AddParm(ts, "-vol", CMD_SINGLE, 0, "volume name");
4643     cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cell name");
4644     cmd_AddParm(ts, "-rw", CMD_FLAG, CMD_OPTIONAL, "force r/w volume");
4645     cmd_AddParm(ts, "-fast", CMD_FLAG, CMD_OPTIONAL, "don't check name with VLDB");
4646
4647     /*
4648      *
4649      * defect 3069
4650      * 
4651     cmd_AddParm(ts, "-root", CMD_FLAG, CMD_OPTIONAL, "create cellular mount point");
4652     */
4653
4654     
4655     ts = cmd_CreateSyntax("rmmount", RemoveMountCmd, NULL, "remove mount point");
4656     cmd_AddParm(ts, "-dir", CMD_LIST, 0, "directory");
4657     
4658     ts = cmd_CreateSyntax("checkservers", CheckServersCmd, NULL, "check local cell's servers");
4659     cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cell to check");
4660     cmd_AddParm(ts, "-all", CMD_FLAG, CMD_OPTIONAL, "check all cells");
4661     cmd_AddParm(ts, "-fast", CMD_FLAG, CMD_OPTIONAL, "just list, don't check");
4662     cmd_AddParm(ts,"-interval",CMD_SINGLE,CMD_OPTIONAL,"seconds between probes");
4663     
4664     ts = cmd_CreateSyntax("checkvolumes", CheckVolumesCmd, NULL, "check volumeID/name mappings");
4665     cmd_CreateAlias(ts, "checkbackups");
4666
4667     
4668     ts = cmd_CreateSyntax("setcachesize", SetCacheSizeCmd, NULL, "set cache size");
4669     cmd_AddParm(ts, "-blocks", CMD_SINGLE, CMD_OPTIONAL, "size in 1K byte blocks (0 => reset)");
4670     cmd_CreateAlias(ts, "cachesize");
4671
4672     cmd_AddParm(ts, "-reset", CMD_FLAG, CMD_OPTIONAL, "reset size back to boot value");
4673     
4674     ts = cmd_CreateSyntax("getcacheparms", GetCacheParmsCmd, NULL, "get cache usage info");
4675
4676     ts = cmd_CreateSyntax("listcells", ListCellsCmd, NULL, "list configured cells");
4677     cmd_AddParm(ts, "-numeric", CMD_FLAG, CMD_OPTIONAL, "addresses only");
4678     
4679     ts = cmd_CreateSyntax("setquota", SetQuotaCmd, NULL, "set volume quota");
4680     cmd_AddParm(ts, "-path", CMD_SINGLE, CMD_OPTIONAL, "dir/file path");
4681     cmd_AddParm(ts, "-max", CMD_SINGLE, 0, "max quota in kbytes");
4682 #ifdef notdef
4683     cmd_AddParm(ts, "-min", CMD_SINGLE, CMD_OPTIONAL, "min quota in kbytes");
4684 #endif
4685     cmd_CreateAlias(ts, "sq");
4686
4687     ts = cmd_CreateSyntax("newcell", NewCellCmd, NULL, "configure new cell");
4688 #ifndef WIN32
4689     cmd_AddParm(ts, "-name", CMD_SINGLE, 0, "cell name");
4690     cmd_AddParm(ts, "-servers", CMD_LIST, CMD_REQUIRED, "primary servers");
4691     cmd_AddParm(ts, "-linkedcell", CMD_SINGLE, CMD_OPTIONAL, "linked cell name");
4692
4693 #ifdef FS_ENABLE_SERVER_DEBUG_PORTS
4694     /*
4695      * Turn this on only if you wish to be able to talk to a server which is listening
4696      * on alternative ports. This is not intended for general use and may not be
4697      * supported in the cache manager. It is not a way to run two servers at the
4698      * same host, since the cache manager cannot properly distinguish those two hosts.
4699      */
4700     cmd_AddParm(ts, "-fsport", CMD_SINGLE, CMD_OPTIONAL, "cell's fileserver port");
4701     cmd_AddParm(ts, "-vlport", CMD_SINGLE, CMD_OPTIONAL, "cell's vldb server port");
4702 #endif
4703
4704     ts = cmd_CreateSyntax("newalias", NewAliasCmd, NULL,
4705                           "configure new cell alias");
4706     cmd_AddParm(ts, "-alias", CMD_SINGLE, 0, "alias name");
4707     cmd_AddParm(ts, "-name", CMD_SINGLE, 0, "real name of cell");
4708 #endif
4709
4710     ts = cmd_CreateSyntax("whichcell", WhichCellCmd, NULL, "list file's cell");
4711     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
4712     cmd_AddParm(ts, "-literal", CMD_FLAG, CMD_OPTIONAL, "literal evaluation of mountpoints and symlinks");
4713
4714     ts = cmd_CreateSyntax("whereis", WhereIsCmd, NULL, "list file's location");
4715     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
4716     cmd_AddParm(ts, "-literal", CMD_FLAG, CMD_OPTIONAL, "literal evaluation of mountpoints and symlinks");
4717
4718     ts = cmd_CreateSyntax("wscell", WSCellCmd, NULL, "list workstation's cell");
4719     
4720     /*
4721      ts = cmd_CreateSyntax("primarycell", PrimaryCellCmd, 0, "obsolete (listed primary cell)");
4722      */
4723     
4724 #ifndef AFS_NT40_ENV
4725     ts = cmd_CreateSyntax("monitor", MonitorCmd, NULL, "set cache monitor host address");
4726     cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_OPTIONAL, "host name or 'off'");
4727     cmd_CreateAlias(ts, "mariner");
4728 #endif
4729
4730     ts = cmd_CreateSyntax("getcellstatus", GetCellCmd, NULL, "get cell status");
4731     cmd_AddParm(ts, "-cell", CMD_LIST, 0, "cell name");
4732     
4733     ts = cmd_CreateSyntax("setcell", SetCellCmd, NULL, "set cell status");
4734     cmd_AddParm(ts, "-cell", CMD_LIST, 0, "cell name");
4735     cmd_AddParm(ts, "-suid", CMD_FLAG, CMD_OPTIONAL, "allow setuid programs");
4736     cmd_AddParm(ts, "-nosuid", CMD_FLAG, CMD_OPTIONAL, "disallow setuid programs");
4737
4738     ts = cmd_CreateSyntax("flushall", FlushAllCmd, NULL, "flush all data");
4739
4740     ts = cmd_CreateSyntax("flushvolume", FlushVolumeCmd, NULL, "flush all data in volume");
4741     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
4742
4743     ts = cmd_CreateSyntax("sysname", SysNameCmd, NULL, "get/set sysname (i.e. @sys) value");
4744     cmd_AddParm(ts, "-newsys", CMD_LIST, CMD_OPTIONAL, "new sysname");
4745
4746 #ifndef AFS_NT40_ENV
4747     ts = cmd_CreateSyntax("exportafs", ExportAfsCmd, NULL, "enable/disable translators to AFS");
4748     cmd_AddParm(ts, "-type", CMD_SINGLE, 0, "exporter name");
4749     cmd_AddParm(ts, "-start", CMD_SINGLE, CMD_OPTIONAL, "start/stop translator ('on' or 'off')");
4750     cmd_AddParm(ts, "-convert", CMD_SINGLE, CMD_OPTIONAL, "convert from afs to unix mode ('on or 'off')");
4751     cmd_AddParm(ts, "-uidcheck", CMD_SINGLE, CMD_OPTIONAL, "run on strict 'uid check' mode ('on' or 'off')");
4752     cmd_AddParm(ts, "-submounts", CMD_SINGLE, CMD_OPTIONAL, "allow nfs mounts to subdirs of /afs/.. ('on' or 'off')");
4753 #endif
4754
4755     ts = cmd_CreateSyntax("storebehind", StoreBehindCmd, NULL, 
4756                           "store to server after file close");
4757     cmd_AddParm(ts, "-kbytes", CMD_SINGLE, CMD_OPTIONAL, "asynchrony for specified names");
4758     cmd_AddParm(ts, "-files", CMD_LIST, CMD_OPTIONAL, "specific pathnames");
4759     cmd_AddParm(ts, "-allfiles", CMD_SINGLE, CMD_OPTIONAL, "new default (KB)");
4760     cmd_CreateAlias(ts, "sb");
4761
4762     ts = cmd_CreateSyntax("setcrypt", SetCryptCmd, NULL, "set cache manager encryption flag");
4763     cmd_AddParm(ts, "-crypt", CMD_SINGLE, 0, "on or off");
4764
4765     ts = cmd_CreateSyntax("getcrypt", GetCryptCmd, NULL, "get cache manager encryption flag");
4766
4767     ts = cmd_CreateSyntax("rxstatproc", RxStatProcCmd, NULL,
4768                           "Manage per process RX statistics");
4769     cmd_AddParm(ts, "-enable", CMD_FLAG, CMD_OPTIONAL, "Enable RX stats");
4770     cmd_AddParm(ts, "-disable", CMD_FLAG, CMD_OPTIONAL, "Disable RX stats");
4771     cmd_AddParm(ts, "-clear", CMD_FLAG, CMD_OPTIONAL, "Clear RX stats");
4772
4773     ts = cmd_CreateSyntax("rxstatpeer", RxStatPeerCmd, NULL,
4774                           "Manage per peer RX statistics");
4775     cmd_AddParm(ts, "-enable", CMD_FLAG, CMD_OPTIONAL, "Enable RX stats");
4776     cmd_AddParm(ts, "-disable", CMD_FLAG, CMD_OPTIONAL, "Disable RX stats");
4777     cmd_AddParm(ts, "-clear", CMD_FLAG, CMD_OPTIONAL, "Clear RX stats");
4778
4779 #ifndef WIN32
4780     ts = cmd_CreateSyntax("setcbaddr", CallBackRxConnCmd, NULL, "configure callback connection address");
4781     cmd_AddParm(ts, "-addr", CMD_SINGLE, CMD_OPTIONAL, "host name or address");
4782 #endif
4783
4784     ts = cmd_CreateSyntax("trace", TraceCmd, NULL, "enable or disable CM tracing");
4785     cmd_AddParm(ts, "-on", CMD_FLAG, CMD_OPTIONAL, "enable tracing");
4786     cmd_AddParm(ts, "-off", CMD_FLAG, CMD_OPTIONAL, "disable tracing");
4787     cmd_AddParm(ts, "-reset", CMD_FLAG, CMD_OPTIONAL, "reset log contents");
4788     cmd_AddParm(ts, "-dump", CMD_FLAG, CMD_OPTIONAL, "dump log contents");
4789     cmd_CreateAlias(ts, "tr");
4790
4791     ts = cmd_CreateSyntax("uuid", UuidCmd, NULL, "manage the UUID for the cache manager");
4792     cmd_AddParm(ts, "-generate", CMD_FLAG, CMD_OPTIONAL, "generate a new UUID");
4793
4794     ts = cmd_CreateSyntax("memdump", MemDumpCmd, NULL, "dump memory allocs in debug builds");
4795     cmd_AddParm(ts, "-begin", CMD_FLAG, CMD_OPTIONAL, "set a memory checkpoint");
4796     cmd_AddParm(ts, "-end", CMD_FLAG, CMD_OPTIONAL, "dump memory allocs");
4797     
4798     ts = cmd_CreateSyntax("cscpolicy", CSCPolicyCmd, NULL, "change client side caching policy for AFS shares");
4799     cmd_AddParm(ts, "-share", CMD_SINGLE, CMD_OPTIONAL, "AFS share");
4800     cmd_AddParm(ts, "-manual", CMD_FLAG, CMD_OPTIONAL, "manual caching of documents");
4801     cmd_AddParm(ts, "-programs", CMD_FLAG, CMD_OPTIONAL, "automatic caching of programs and documents");
4802     cmd_AddParm(ts, "-documents", CMD_FLAG, CMD_OPTIONAL, "automatic caching of documents");
4803     cmd_AddParm(ts, "-disable", CMD_FLAG, CMD_OPTIONAL, "disable caching");
4804
4805     ts = cmd_CreateSyntax("minidump", MiniDumpCmd, NULL, "Generate MiniDump of current service state");
4806
4807     ts = cmd_CreateSyntax("test_volstat", TestVolStatCmd, NULL, (char *)CMD_HIDDEN);
4808     cmd_AddParm(ts, "-network", CMD_SINGLE, CMD_OPTIONAL, "set network state up or down");
4809     cmd_AddParm(ts, "-check",   CMD_FLAG,   CMD_OPTIONAL, "check state of offline volumes");
4810     cmd_AddParm(ts, "-cell",    CMD_SINGLE, CMD_OPTIONAL, "cell name or number");
4811     cmd_AddParm(ts, "-volume",  CMD_SINGLE, CMD_OPTIONAL, "volume name or number");
4812     cmd_AddParm(ts, "-state",   CMD_SINGLE, CMD_OPTIONAL, "new volume state: online, busy, offline, down");
4813
4814     code = cmd_Dispatch(argc, argv);
4815
4816     if (rxInitDone) 
4817         rx_Finalize();
4818     
4819     return code;
4820 }
4821
4822 static void 
4823 Die(int code, char *filename)
4824 { /*Die*/
4825
4826     if (code == EINVAL) {
4827         if (filename)
4828             fprintf(stderr,"%s: Invalid argument; it is possible that %s is not in AFS.\n", pn, filename);
4829         else 
4830             fprintf(stderr,"%s: Invalid argument.\n", pn);
4831     }
4832     else if (code == ENOENT) {
4833         if (filename) 
4834             fprintf(stderr,"%s: File '%s' doesn't exist\n", pn, filename);
4835         else 
4836             fprintf(stderr,"%s: no such file returned\n", pn);
4837     }
4838     else if (code == EROFS)  
4839         fprintf(stderr,"%s: You can not change a backup or readonly volume\n", pn);
4840     else if (code == EACCES || code == EPERM) {
4841         if (filename) 
4842             fprintf(stderr,"%s: You don't have the required access rights on '%s'\n", pn, filename);
4843         else 
4844             fprintf(stderr,"%s: You do not have the required rights to do this operation\n", pn);
4845     }
4846     else if (code == ENODEV) {
4847         fprintf(stderr,"%s: AFS service may not have started.\n", pn);
4848     }
4849     else if (code == ESRCH) {   /* hack */
4850         fprintf(stderr,"%s: Cell name not recognized.\n", pn);
4851     }
4852     else if (code == EPIPE) {   /* hack */
4853         fprintf(stderr,"%s: Volume name or ID not recognized.\n", pn);
4854     }
4855     else if (code == EFBIG) {
4856         fprintf(stderr,"%s: Cache size too large.\n", pn);
4857     }
4858     else if (code == ETIMEDOUT) {
4859         if (filename)
4860             fprintf(stderr,"%s:'%s': Connection timed out", pn, filename);
4861         else
4862             fprintf(stderr,"%s: Connection timed out", pn);
4863     }
4864     else if (code == EBUSY) {
4865         if (filename) 
4866             fprintf(stderr,"%s: All servers are busy on which '%s' resides\n", pn, filename);
4867         else 
4868             fprintf(stderr,"%s: All servers are busy\n", pn);
4869     } 
4870     else if (code == ENXIO) {
4871         if (filename) 
4872             fprintf(stderr,"%s: All volume instances are offline on which '%s' resides\n", pn, filename);
4873         else 
4874             fprintf(stderr,"%s: All volume instances are offline\n", pn);
4875     } 
4876     else if (code == ENOSYS) {
4877         if (filename) 
4878             fprintf(stderr,"%s: All servers are down on which '%s' resides\n", pn, filename);
4879         else 
4880             fprintf(stderr,"%s: All servers are down\n", pn);
4881     } 
4882     else {
4883         if (filename) 
4884             fprintf(stderr,"%s:'%s'", pn, filename);
4885         else 
4886             fprintf(stderr,"%s", pn);
4887 #ifdef WIN32
4888         fprintf(stderr, ": code 0x%x\n", code);
4889 #else /* not WIN32 */
4890         fprintf(stderr,": %s\n", afs_error_message(code));
4891 #endif /* not WIN32 */
4892     }
4893 } /*Die*/
4894