8f27a42a6f35940ce19f5cdb17df8c999b27a84a
[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,