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