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