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