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