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