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