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