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