Windows: Secure C String usage in src\WINNT\afsd\fs.c
[openafs.git] / src / WINNT / afsd / fs.c
index 205d607..bf08850 100644 (file)
@@ -9,11 +9,13 @@
 
 #include <afs/param.h>
 #include <afs/stds.h>
+#include <afs/com_err.h>
 
 #include <windows.h>
 #include <stdlib.h>
 #include <malloc.h>
 #include <string.h>
+#include <strsafe.h>
 #include <stdio.h>
 #include <time.h>
 #include <winsock2.h>
@@ -23,6 +25,7 @@
 
 #include <osi.h>
 #include <afsint.h>
+#include <afs/afs_consts.h>
 #include <afs/cellconfig.h>
 #include <afs/ptserver.h>
 #include <afs/ptuser.h>
 #include "afsd.h"
 #include "cm_ioctl.h"
 
-#define        MAXHOSTS 13
-#define        OMAXHOSTS 8
-#define MAXCELLHOSTS 8
 #define MAXNAME 100
-#define        MAXSIZE 2048
 #define MAXINSIZE 1300    /* pioctl complains if data is larger than this */
 #define VMSGSIZE 128      /* size of msg buf in volume hdr */
-#define MAXCELLCHARS           64
+#define CELL_MAXNAMELEN                256
 #define MAXHOSTCHARS           64
 
-static char space[MAXSIZE];
+static char space[AFS_PIOCTL_MAXSIZE];
 static char tspace[1024];
 
 static struct ubik_client *uclient;
@@ -245,13 +244,20 @@ static int
 InAFS(char *apath)
 {
     struct ViceIoctl blob;
+    cm_ioctlQueryOptions_t options;
+    cm_fid_t fid;
     afs_int32 code;
 
-    blob.in_size = 0;
-    blob.out_size = MAXSIZE;
-    blob.out = space;
+    memset(&options, 0, sizeof(options));
+    options.size = sizeof(options);
+    options.field_flags |= CM_IOCTL_QOPTS_FIELD_LITERAL;
+    options.literal = 1;
+    blob.in_size = options.size;    /* no variable length data */
+    blob.in = &options;
+    blob.out_size = sizeof(cm_fid_t);
+    blob.out = (char *) &fid;
 
-    code = pioctl(apath, VIOC_FILE_CELL_NAME, &blob, 1);
+    code = pioctl_utf8(apath, VIOCGETFID, &blob, 1);
     if (code) {
        if ((errno == EINVAL) || (errno == ENOENT)) 
             return 0;
@@ -266,12 +272,13 @@ IsFreelanceRoot(char *apath)
     afs_int32 code;
 
     blob.in_size = 0;
-    blob.out_size = MAXSIZE;
+    blob.out_size = AFS_PIOCTL_MAXSIZE;
     blob.out = space;
 
-    code = pioctl(apath, VIOC_FILE_CELL_NAME, &blob, 1);
-    if (code == 0)
-        return !stricmp("Freelance.Local.Root",space);
+    code = pioctl_utf8(apath, VIOC_FILE_CELL_NAME, &blob, 1);
+    if (code == 0) {
+        return !cm_strnicmp_utf8N("Freelance.Local.Root",space, blob.out_size);
+    }
     return 1;   /* assume it is because it is more restrictive that way */
 }
 
@@ -280,14 +287,20 @@ static char *
 Parent(char *apath)
 {
     char *tp;
-    strcpy(tspace, apath);
+    if( FAILED(StringCbCopy(tspace, sizeof(tspace), apath))) {
+        fprintf (stderr, "tspace - not enough space");
+        exit(1);
+    }
     tp = strrchr(tspace, '\\');
     if (tp) {
        *(tp+1) = 0;    /* lv trailing slash so Parent("k:\foo") is "k:\" not "k:" */
     }
     else {
        fs_ExtractDriveLetter(apath, tspace);
-       strcat(tspace, ".");
+       if( FAILED(StringCbCat(tspace, sizeof(tspace), "."))) {
+           fprintf (stderr, "tspace - not enough space");
+           exit(1);
+       }
     }
     return tspace;
 }
@@ -297,7 +310,6 @@ enum rtype {add, destroy, deny};
 static afs_int32 
 Convert(char *arights, int dfs, enum rtype *rtypep)
 {
-    int i, len;
     afs_int32 mode;
     char tc;
 
@@ -332,10 +344,8 @@ Convert(char *arights, int dfs, enum rtype *rtypep)
        *rtypep = destroy; /* Remove entire entry */
        return 0;
     }
-    len = (int)strlen(arights);
     mode = 0;
-    for(i=0;i<len;i++) {
-        tc = *arights++;
+    for(; (tc = *arights) != '\0'; arights++) {
        if (dfs) {
            if (tc == '-') 
                 continue;
@@ -429,15 +439,20 @@ static void
 SetDotDefault(struct cmd_item **aitemp)
 {
     struct cmd_item *ti;
+    int len_data = 2;
+
     if (*aitemp) 
         return;                        /* already has value */
     /* otherwise, allocate an item representing "." */
     ti = (struct cmd_item *) malloc(sizeof(struct cmd_item));
     assert(ti);
     ti->next = (struct cmd_item *) 0;
-    ti->data = (char *) malloc(2);
+    ti->data = (char *) malloc(len_data);
     assert(ti->data);
-    strcpy(ti->data, ".");
+    if( FAILED(StringCbCopy(ti->data, len_data, "."))) {
+        fprintf (stderr, "data - not enough space");
+        exit(1);
+    }
     *aitemp = ti;
 }
 
@@ -459,7 +474,10 @@ ChangeList (struct Acl *al, afs_int32 plus, char *aname, afs_int32 arights)
     /* Otherwise we make a new item and plug in the new data. */
     tlist = (struct AclEntry *) malloc(sizeof (struct AclEntry));
     assert(tlist);
-    strcpy(tlist->name, aname);
+    if( FAILED(StringCbCopy(tlist->name, sizeof(tlist->name), aname))) {
+       fprintf (stderr, "name - not enough space");
+        exit(1);
+    }
     tlist->rights = arights;
     if (plus) {
         tlist->next = al->pluslist;
@@ -535,24 +553,56 @@ EmptyAcl(char *astr)
     tp->nplus = tp->nminus = 0;
     tp->pluslist = tp->minuslist = 0;
     tp->dfs = 0;
-    sscanf(astr, "%d dfs:%d %s", &junk, &tp->dfs, tp->cell);
+#if _MSC_VER < 1400
+    if (astr == NULL || sscanf(astr, "%d dfs:%d %s", &junk, &tp->dfs, tp->cell) <= 0) {
+        tp->dfs = 0;
+        tp->cell[0] = '\0';
+    }
+#else
+    if (astr == NULL || sscanf_s(astr, "%d dfs:%d %s", &junk, &tp->dfs, tp->cell, sizeof(tp->cell)) <= 0) {
+        tp->dfs = 0;
+        tp->cell[0] = '\0';
+    }
+#endif
     return tp;
 }
 
 static struct Acl *
-ParseAcl (char *astr)
+ParseAcl (char *astr, int astr_size)
 {
-    int nplus, nminus, i, trights;
+    int nplus, nminus, i, trights, ret;
+    size_t len;
     char tname[MAXNAME];
-    struct AclEntry *first, *last, *tl;
+    struct AclEntry *first, *next, *last, *tl;
     struct Acl *ta;
 
-    ta = (struct Acl *) malloc (sizeof (struct Acl));
-    assert(ta);
-    ta->dfs = 0;
-    sscanf(astr, "%d dfs:%d %s", &ta->nplus, &ta->dfs, ta->cell);
+    ta = EmptyAcl(NULL);
+    if( FAILED(StringCbLength(astr, astr_size, &len))) {
+        fprintf (stderr, "StringCbLength failure on astr");
+        exit(1);
+    }
+    if (astr == NULL || len == 0)
+        return ta;
+
+#if _MSC_VER < 1400
+    ret = sscanf(astr, "%d dfs:%d %s", &ta->nplus, &ta->dfs, ta->cell);
+#else
+    ret = sscanf_s(astr, "%d dfs:%d %s", &ta->nplus, &ta->dfs, ta->cell, sizeof(ta->cell));
+#endif
+    if (ret <= 0) {
+        free(ta);
+        return NULL;
+    }
     astr = SkipLine(astr);
-    sscanf(astr, "%d", &ta->nminus);
+#if _MSC_VER < 1400
+    ret = sscanf(astr, "%d", &ta->nminus);
+#else
+    ret = sscanf_s(astr, "%d", &ta->nminus);
+#endif
+    if (ret <= 0) {
+        free(ta);
+        return NULL;
+    }
     astr = SkipLine(astr);
 
     nplus = ta->nplus;
@@ -561,13 +611,23 @@ ParseAcl (char *astr)
     last = 0;
     first = 0;
     for(i=0;i<nplus;i++) {
-        sscanf(astr, "%100s %d", tname, &trights);
+#if _MSC_VER < 1400
+        ret = sscanf(astr, "%100s %d", tname, &trights); 
+#else
+        ret = sscanf_s(astr, "%100s %d", tname, sizeof(tname), &trights);
+#endif
+        if (ret <= 0)
+            goto nplus_err;
         astr = SkipLine(astr);
         tl = (struct AclEntry *) malloc(sizeof (struct AclEntry));
-        assert(tl);
+        if (tl == NULL)
+            goto nplus_err;
         if (!first) 
             first = tl;
-        strcpy(tl->name, tname);
+        if( FAILED(StringCbCopy(tl->name, sizeof(tl->name), tname))) {
+            fprintf (stderr, "name - not enough space");
+            exit(1);
+        }
         tl->rights = trights;
         tl->next = 0;
         if (last) 
@@ -579,13 +639,23 @@ ParseAcl (char *astr)
     last = 0;
     first = 0;
     for(i=0;i<nminus;i++) {
-        sscanf(astr, "%100s %d", tname, &trights);
+#if _MSC_VER < 1400
+        ret = sscanf(astr, "%100s %d", tname, &trights);
+#else
+        ret = sscanf_s(astr, "%100s %d", tname, sizeof(tname), &trights);
+#endif
+        if (ret <= 0)
+            goto nminus_err;
         astr = SkipLine(astr);
         tl = (struct AclEntry *) malloc(sizeof (struct AclEntry));
-        assert(tl);
+        if (tl == NULL)
+            goto nminus_err;
         if (!first) 
             first = tl;
-        strcpy(tl->name, tname);
+        if( FAILED(StringCbCopy(tl->name, sizeof(tl->name), tname))) {
+            fprintf (stderr, "name - not enough space");
+            exit(1);
+        }
         tl->rights = trights;
         tl->next = 0;
         if (last) 
@@ -595,6 +665,21 @@ ParseAcl (char *astr)
     ta->minuslist = first;
 
     return ta;
+
+  nminus_err:
+    for (;first; first = next) {
+        next = first->next;
+        free(first);
+    }   
+    first = ta->pluslist;
+
+  nplus_err:
+    for (;first; first = next) {
+        next = first->next;
+        free(first);
+    }   
+    free(ta);
+    return NULL;
 }
 
 static int
@@ -625,10 +710,10 @@ QuickPrintStatus(VolumeStatus *status, char *name)
     printf("%-25.25s",name);
 
     if (status->MaxQuota != 0) {
-       printf("%10d%10d", status->MaxQuota, status->BlocksInUse);
+       printf(" %10d %10d", status->MaxQuota, status->BlocksInUse);
        QuotaUsed = ((((double)status->BlocksInUse)/status->MaxQuota) * 100.0);
     } else {
-       printf("no limit%10d", status->BlocksInUse);
+       printf("   no limit %10d", status->BlocksInUse);
     }
     if (QuotaUsed > 90.0){
        printf(" %5.0f%%<<", QuotaUsed);
@@ -673,23 +758,42 @@ QuickPrintSpace(VolumeStatus *status, char *name)
 static char *
 AclToString(struct Acl *acl)
 {
-    static char mydata[MAXSIZE];
-    char tstring[MAXSIZE];
+    static char mydata[AFS_PIOCTL_MAXSIZE];
+    char tstring[AFS_PIOCTL_MAXSIZE];
     char dfsstring[30];
     struct AclEntry *tp;
-    
-    if (acl->dfs) 
-        sprintf(dfsstring, " dfs:%d %s", acl->dfs, acl->cell);
-    else 
+
+    if (acl->dfs) {
+        if( FAILED(StringCbPrintf(dfsstring, sizeof(dfsstring), " dfs:%d %s", acl->dfs, acl->cell))) {
+            fprintf (stderr, "dfsstring - cannot be populated");
+            exit(1);
+        }
+    } else {
         dfsstring[0] = '\0';
-    sprintf(mydata, "%d%s\n%d\n", acl->nplus, dfsstring, acl->nminus);
+    }
+    if( FAILED(StringCbPrintf(mydata, sizeof(mydata), "%d%s\n%d\n", acl->nplus, dfsstring, acl->nminus))) {
+        fprintf (stderr, "mydata - cannot be populated");
+        exit(1);
+    }
     for (tp = acl->pluslist;tp;tp=tp->next) {
-        sprintf(tstring, "%s %d\n", tp->name, tp->rights);
-        strcat(mydata, tstring);
+        if( FAILED(StringCbPrintf(tstring, sizeof(tstring), "%s %d\n", tp->name, tp->rights))) {
+            fprintf (stderr, "tstring - cannot be populated");
+            exit(1);
+        }
+        if( FAILED(StringCbCat(mydata, sizeof(mydata), tstring))) {
+            fprintf (stderr, "mydata - not enough space");
+            exit(1);
+        }
     }
     for (tp = acl->minuslist;tp;tp=tp->next) {
-        sprintf(tstring, "%s %d\n", tp->name, tp->rights);
-        strcat(mydata, tstring);
+        if( FAILED(StringCbPrintf(tstring, sizeof(tstring), "%s %d\n", tp->name, tp->rights))) {
+            fprintf (stderr, "tstring - cannot be populated");
+            exit(1);
+        }
+        if( FAILED(StringCbCat(mydata, sizeof(mydata), tstring))) {
+            fprintf (stderr, "mydata - not enough space");
+            exit(1);
+        }
     }
     return mydata;
 }
@@ -702,7 +806,7 @@ static DWORD IsFreelance(void)
     DWORD enabled = 0;
 
     code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSREG_CLT_SVC_PARAM_SUBKEY,
-                         0, KEY_QUERY_VALUE, &parmKey);
+                         0, (IsWow64()?KEY_WOW64_64KEY:0)|KEY_QUERY_VALUE, &parmKey);
     if (code == ERROR_SUCCESS) {
         dummyLen = sizeof(cm_freelanceEnabled);
         code = RegQueryValueEx(parmKey, "FreelanceClient", NULL, NULL,
@@ -712,23 +816,27 @@ static DWORD IsFreelance(void)
     return enabled;
 }
 
+#define NETBIOSNAMESZ 1024
 static const char * NetbiosName(void)
 {
-    static char buffer[1024] = "AFS";
+    static char buffer[NETBIOSNAMESZ] = "AFS";
     HKEY  parmKey;
     DWORD code;
     DWORD dummyLen;
     DWORD enabled = 0;
 
     code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSREG_CLT_SVC_PARAM_SUBKEY,
-                         0, KEY_QUERY_VALUE, &parmKey);
+                         0, (IsWow64()?KEY_WOW64_64KEY:0)|KEY_QUERY_VALUE, &parmKey);
     if (code == ERROR_SUCCESS) {
         dummyLen = sizeof(buffer);
         code = RegQueryValueEx(parmKey, "NetbiosName", NULL, NULL,
                               buffer, &dummyLen);
         RegCloseKey (parmKey);
     } else {
-       strcpy(buffer, "AFS");
+       if( FAILED(StringCbCopy(buffer, sizeof(buffer), "AFS"))) {
+           fprintf (stderr, "buffer - not enough space");
+            exit(1);
+       }
     }
     return buffer;
 }
@@ -762,8 +870,15 @@ static BOOL IsAdmin (void)
         dwSize = 0;
         dwSize2 = 0;
 
-        strcat(pszAdminGroup,"\\");
-        strcat(pszAdminGroup, AFSCLIENT_ADMIN_GROUPNAME);
+       if( FAILED(StringCbCat(pszAdminGroup, MAX_COMPUTERNAME_LENGTH + sizeof(AFSCLIENT_ADMIN_GROUPNAME) + 2,"\\"))) {
+           fprintf (stderr, "pszAdminGroup - not enough space");
+            exit(1);
+       }
+       if( FAILED(StringCbCat(pszAdminGroup, MAX_COMPUTERNAME_LENGTH +
+           sizeof(AFSCLIENT_ADMIN_GROUPNAME) + 2, AFSCLIENT_ADMIN_GROUPNAME))) {
+           fprintf (stderr, "pszAdminGroup - not enough space");
+            exit(1);
+       }
 
         LookupAccountName(NULL, pszAdminGroup, NULL, &dwSize, NULL, &dwSize2, &snu);
         /* that should always fail. */
@@ -883,7 +998,7 @@ SetACLCmd(struct cmd_syndesc *as, void *arock)
     afs_int32 rights;
     int clear;
     int idf = getidf(as, parm_setacl_id);
-
+    size_t len;
     int error = 0;
 
     if (as->parms[2].items)
@@ -892,10 +1007,10 @@ SetACLCmd(struct cmd_syndesc *as, void *arock)
         clear = 0;
     plusp = !(as->parms[3].items);
     for(ti=as->parms[0].items; ti;ti=ti->next) {
-       blob.out_size = MAXSIZE;
+       blob.out_size = AFS_PIOCTL_MAXSIZE;
        blob.in_size = idf;
        blob.in = blob.out = space;
-       code = pioctl(ti->data, VIOCGETAL, &blob, 1);
+       code = pioctl_utf8(ti->data, VIOCGETAL, &blob, 1);
        if (code) {
            Die(errno, ti->data);
            error = 1;
@@ -903,7 +1018,14 @@ SetACLCmd(struct cmd_syndesc *as, void *arock)
        }
         if (ta)
             ZapAcl(ta);
-       ta = ParseAcl(space);
+       ta = ParseAcl(space, AFS_PIOCTL_MAXSIZE);
+        if (!ta) {
+            fprintf(stderr,
+                    "fs: %s: invalid acl data returned from VIOCGETAL\n",
+                     ti->data);
+            error = 1;
+            continue;
+        }
        if (!plusp && ta->dfs) {
            fprintf(stderr,
                    "fs: %s: you may not use the -negative switch with DFS acl's.\n%s",
@@ -917,7 +1039,14 @@ SetACLCmd(struct cmd_syndesc *as, void *arock)
        if (clear) 
             ta = EmptyAcl(space);
        else 
-            ta = ParseAcl(space);
+            ta = ParseAcl(space, AFS_PIOCTL_MAXSIZE);
+        if (!ta) {
+            fprintf(stderr,
+                    "fs: %s: invalid acl data returned from VIOCGETAL\n",
+                     ti->data);
+            error = 1;
+            continue;
+        }
        CleanAcl(ta, ti->data);
        for(ui=as->parms[1].items; ui; ui=ui->next->next) {
            enum rtype rtype;
@@ -943,8 +1072,12 @@ SetACLCmd(struct cmd_syndesc *as, void *arock)
        }
        blob.in = AclToString(ta);
        blob.out_size=0;
-       blob.in_size = 1+(long)strlen(blob.in);
-       code = pioctl(ti->data, VIOCSETAL, &blob, 1);
+       if( FAILED(StringCbLength(blob.in, sizeof(space), &len))) {
+           fprintf (stderr, "StringCbLength failure on blob.in");
+           exit(1);
+       }
+       blob.in_size = 1+(long)len;
+       code = pioctl_utf8(ti->data, VIOCSETAL, &blob, 1);
        if (code) {
            if (errno == EINVAL) {
                if (ta->dfs) {
@@ -1013,26 +1146,33 @@ CopyACLCmd(struct cmd_syndesc *as, void *arock)
     int clear;
     int idf = getidf(as, parm_copyacl_id);
     int error = 0;
+    size_t len;
 
     if (as->parms[2].items) 
         clear=1;
     else 
         clear=0;
-    blob.out_size = MAXSIZE;
+    blob.out_size = AFS_PIOCTL_MAXSIZE;
     blob.in_size = idf;
     blob.in = blob.out = space;
-    code = pioctl(as->parms[0].items->data, VIOCGETAL, &blob, 1);
+    code = pioctl_utf8(as->parms[0].items->data, VIOCGETAL, &blob, 1);
     if (code) {
        Die(errno, as->parms[0].items->data);
        return 1;
     }
-    fa = ParseAcl(space);
+    fa = ParseAcl(space, AFS_PIOCTL_MAXSIZE);
+    if (!fa) {
+        fprintf(stderr,
+                 "fs: %s: invalid acl data returned from VIOCGETAL\n",
+                 as->parms[0].items->data);
+        return 1;
+    }
     CleanAcl(fa, as->parms[0].items->data);
     for (ti=as->parms[1].items; ti;ti=ti->next) {
-       blob.out_size = MAXSIZE;
+       blob.out_size = AFS_PIOCTL_MAXSIZE;
        blob.in_size = idf;
        blob.in = blob.out = space;
-       code = pioctl(ti->data, VIOCGETAL, &blob, 1);
+       code = pioctl_utf8(ti->data, VIOCGETAL, &blob, 1);
        if (code) {
            Die(errno, ti->data);
            error = 1;
@@ -1043,7 +1183,14 @@ CopyACLCmd(struct cmd_syndesc *as, void *arock)
        if (clear) 
             ta = EmptyAcl(space);
        else 
-            ta = ParseAcl(space);
+            ta = ParseAcl(space, AFS_PIOCTL_MAXSIZE);
+        if (!ta) {
+            fprintf(stderr,
+                    "fs: %s: invalid acl data returned from VIOCGETAL\n",
+                     ti->data);
+            error = 1;
+            continue;
+        }
        CleanAcl(ta, ti->data);
        if (ta->dfs != fa->dfs) {
            fprintf(stderr, 
@@ -1060,7 +1207,10 @@ CopyACLCmd(struct cmd_syndesc *as, void *arock)
                 error = 1;
                continue;
            }
-           strcpy(ta->cell, fa->cell);
+           if( FAILED(StringCbCopy(ta->cell, sizeof(ta->cell), fa->cell))) {
+               fprintf (stderr, "cell - not enough space");
+                exit(1);
+           }
        }
        for (tp = fa->pluslist;tp;tp=tp->next) 
            ChangeList(ta, 1, tp->name, tp->rights);
@@ -1068,8 +1218,12 @@ CopyACLCmd(struct cmd_syndesc *as, void *arock)
            ChangeList(ta, 0, tp->name, tp->rights);
        blob.in = AclToString(ta);
        blob.out_size=0;
-       blob.in_size = 1+(long)strlen(blob.in);
-       code = pioctl(ti->data, VIOCSETAL, &blob, 1);
+       if( FAILED(StringCbLength(blob.in, sizeof(space), &len))) {
+           fprintf (stderr, "StringCbLength failure on blob.in");
+           exit(1);
+       }
+       blob.in_size = 1+(long)len;
+       code = pioctl_utf8(ti->data, VIOCSETAL, &blob, 1);
        if (code) {
            if (errno == EINVAL) {
                fprintf(stderr,
@@ -1087,7 +1241,7 @@ CopyACLCmd(struct cmd_syndesc *as, void *arock)
     return error;
 }
 
-/* pioctl() call to get the cellname of a pathname */
+/* pioctl_utf8() call to get the cellname of a pathname */
 static afs_int32
 GetCell(char *fname, char *cellname)
 {
@@ -1095,10 +1249,12 @@ GetCell(char *fname, char *cellname)
     struct ViceIoctl blob;
 
     blob.in_size = 0;
-    blob.out_size = MAXCELLCHARS;
+    blob.out_size = CELL_MAXNAMELEN;
     blob.out = cellname;
 
-    code = pioctl(fname, VIOC_FILE_CELL_NAME, &blob, 1);
+    code = pioctl_utf8(fname, VIOC_FILE_CELL_NAME, &blob, 1);
+    if (code == 0)
+        cellname[blob.out_size - 1] = '\0';
     return code;
 }
 
@@ -1111,7 +1267,8 @@ BadName(char *aname, char *fname)
 {
     afs_int32 tc, code, id;
     char *nm;
-    char cell[MAXCELLCHARS];
+    char cell[CELL_MAXNAMELEN];
+    char confDir[257];
 
     for ( nm = aname; tc = *nm; nm++) {
        /* all must be '-' or digit to be bad */
@@ -1124,7 +1281,9 @@ BadName(char *aname, char *fname)
     if (code)
         return 0;
 
-    pr_Initialize(1, AFSDIR_CLIENT_ETC_DIRPATH, cell);
+    cm_GetConfigDir(confDir, sizeof(confDir));
+
+    pr_Initialize(1, confDir, cell);
     code = pr_SNameToId(aname, &id);
     pr_End();
 
@@ -1188,13 +1347,14 @@ CleanACLCmd(struct cmd_syndesc *as, void *arock)
     struct cmd_item *ti;
     struct AclEntry *te;
     int error = 0;
+    size_t len;
 
     SetDotDefault(&as->parms[0].items);
     for(ti=as->parms[0].items; ti; ti=ti->next) {
-       blob.out_size = MAXSIZE;
+       blob.out_size = AFS_PIOCTL_MAXSIZE;
        blob.in_size = 0;
        blob.out = space;
-       code = pioctl(ti->data, VIOCGETAL, &blob, 1);
+       code = pioctl_utf8(ti->data, VIOCGETAL, &blob, 1);
        if (code) {
            Die(errno, ti->data);
             error = 1;
@@ -1202,8 +1362,14 @@ CleanACLCmd(struct cmd_syndesc *as, void *arock)
        }
         if (ta)
             ZapAcl(ta);
-       ta = ParseAcl(space);
-
+       ta = ParseAcl(space, AFS_PIOCTL_MAXSIZE);
+        if (!ta) {
+            fprintf(stderr,
+                    "fs: %s: invalid acl data returned from VIOCGETAL\n",
+                     ti->data);
+            error = 1;
+            continue;
+        }
        if (ta->dfs) {
            fprintf(stderr,
                    "%s: cleanacl is not supported for DFS access lists.\n",
@@ -1217,9 +1383,13 @@ CleanACLCmd(struct cmd_syndesc *as, void *arock)
        if (changes) {
            /* now set the acl */
            blob.in=AclToString(ta);
-           blob.in_size = (long)strlen(blob.in)+1;
+           if( FAILED(StringCbLength(blob.in, sizeof(space), &len))) {
+               fprintf (stderr, "StringCbLength failure on blob.in");
+               exit(1);
+           }
+           blob.in_size = (long)len+1;
            blob.out_size = 0;
-           code = pioctl(ti->data, VIOCSETAL, &blob, 1);
+           code = pioctl_utf8(ti->data, VIOCSETAL, &blob, 1);
            if (code) {
                if (errno == EINVAL) {
                    fprintf(stderr,
@@ -1279,53 +1449,79 @@ ListACLCmd(struct cmd_syndesc *as, void *arock)
     SetDotDefault(&as->parms[0].items);
     for(ti=as->parms[0].items; ti; ti=ti->next) {
        char separator;
-       blob.out_size = MAXSIZE;
+       blob.out_size = AFS_PIOCTL_MAXSIZE;
        blob.in_size = idf;
        blob.in = blob.out = space;
-       code = pioctl(ti->data, VIOCGETAL, &blob, 1);
+       code = pioctl_utf8(ti->data, VIOCGETAL, &blob, 1);
        if (code) {
            Die(errno, ti->data);
             error = 1;
            continue;
        }
-       ta = ParseAcl(space);
-       switch (ta->dfs) {
-         case 0:
-           printf("Access list for %s is\n", ti->data);
-           break;
-         case 1:
-           printf("DFS access list for %s is\n", ti->data);
-           break;
-         case 2:
-           printf("DFS initial directory access list of %s is\n", ti->data);
-           break;
-         case 3:
-           printf("DFS initial file access list of %s is\n", ti->data);
-           break;
-       }
-       if (ta->dfs) {
-           printf("  Default cell = %s\n", ta->cell);
-       }
-       separator = ta->dfs? DFS_SEPARATOR : ' ';
-       if (ta->nplus > 0) {
-           if (!ta->dfs) 
-                printf("Normal rights:\n");
-           for(te = ta->pluslist;te;te=te->next) {
-               printf("  %s%c", te->name, separator);
-               PRights(te->rights, ta->dfs);
-               printf("\n");
-           }
-       }
-       if (ta->nminus > 0) {
-           printf("Negative rights:\n");
-           for(te = ta->minuslist;te;te=te->next) {
-               printf("  %s ", te->name);
-               PRights(te->rights, ta->dfs);
-               printf("\n");
-           }
-       }
-       if (ti->next) 
+       ta = ParseAcl(space, AFS_PIOCTL_MAXSIZE);
+        if (!ta) {
+            fprintf(stderr,
+                    "fs: %s: invalid acl data returned from VIOCGETAL\n",
+                     ti->data);
+            error = 1;
+            continue;
+        }
+        if (as->parms[3].items) {                       /* -cmd */
+            printf("fs setacl -dir %s -acl ", ti->data);
+            if (ta->nplus > 0) {
+                for (te = ta->pluslist; te; te = te->next) {
+                    printf("  %s ", te->name);
+                    PRights(te->rights, ta->dfs);
+                }
+            }
             printf("\n");
+            if (ta->nminus > 0) {
+                printf("fs setacl -dir %s -acl ", ti->data);
+                for (te = ta->minuslist; te; te = te->next) {
+                    printf("  %s ", te->name);
+                    PRights(te->rights, ta->dfs);
+                }
+                printf(" -negative\n");
+            }
+        } else {
+            switch (ta->dfs) {
+            case 0:
+                printf("Access list for %s is\n", ti->data);
+                break;
+            case 1:
+                printf("DFS access list for %s is\n", ti->data);
+                break;
+            case 2:
+                printf("DFS initial directory access list of %s is\n", ti->data);
+                break;
+            case 3:
+                printf("DFS initial file access list of %s is\n", ti->data);
+                break;
+            }
+            if (ta->dfs) {
+                printf("  Default cell = %s\n", ta->cell);
+            }
+            separator = ta->dfs? DFS_SEPARATOR : ' ';
+            if (ta->nplus > 0) {
+                if (!ta->dfs)
+                    printf("Normal rights:\n");
+                for(te = ta->pluslist;te;te=te->next) {
+                    printf("  %s%c", te->name, separator);
+                    PRights(te->rights, ta->dfs);
+                    printf("\n");
+                }
+            }
+            if (ta->nminus > 0) {
+                printf("Negative rights:\n");
+                for(te = ta->minuslist;te;te=te->next) {
+                    printf("  %s ", te->name);
+                    PRights(te->rights, ta->dfs);
+                    printf("\n");
+                }
+            }
+            if (ti->next)
+                printf("\n");
+        }
         ZapAcl(ta);
     }
     return error;
@@ -1338,7 +1534,7 @@ FlushAllCmd(struct cmd_syndesc *as, void *arock)
     struct ViceIoctl blob;
 
     blob.in_size = blob.out_size = 0;
-    code = pioctl(NULL, VIOC_FLUSHALL, &blob, 0);
+    code = pioctl_utf8(NULL, VIOC_FLUSHALL, &blob, 0);
     if (code) {
        fprintf(stderr, "Error flushing all ");
        return 1;
@@ -1357,7 +1553,7 @@ FlushVolumeCmd(struct cmd_syndesc *as, void *arock)
     SetDotDefault(&as->parms[0].items);
     for(ti=as->parms[0].items; ti; ti=ti->next) {
        blob.in_size = blob.out_size = 0;
-       code = pioctl(ti->data, VIOC_FLUSHVOLUME, &blob, 0);
+       code = pioctl_utf8(ti->data, VIOC_FLUSHVOLUME, &blob, 0);
        if (code) {
            fprintf(stderr, "Error flushing volume ");
             perror(ti->data);
@@ -1374,12 +1570,24 @@ FlushCmd(struct cmd_syndesc *as, void *arock)
     afs_int32 code;
     struct ViceIoctl blob;
     struct cmd_item *ti;
-
     int error = 0;
+    int literal = 0;
+    cm_ioctlQueryOptions_t options;
 
+    if (as->parms[1].items)
+        literal = 1;
+    
     for(ti=as->parms[0].items; ti; ti=ti->next) {
-       blob.in_size = blob.out_size = 0;
-       code = pioctl(ti->data, VIOCFLUSH, &blob, 0);
+        /* once per file */
+        memset(&options, 0, sizeof(options));
+        options.size = sizeof(options);
+        options.field_flags |= CM_IOCTL_QOPTS_FIELD_LITERAL;
+        options.literal = literal;
+       blob.in_size = options.size;    /* no variable length data */
+        blob.in = &options;
+
+       blob.out_size = 0;
+       code = pioctl_utf8(ti->data, VIOCFLUSH, &blob, 0);
        if (code) {
            if (errno == EMFILE) {
                fprintf(stderr, "%s: Can't flush active file %s\n", pn, 
@@ -1399,9 +1607,17 @@ FlushCmd(struct cmd_syndesc *as, void *arock)
 static int
 SetQuotaCmd(struct cmd_syndesc *as, void *arock) {
     struct cmd_syndesc ts;
-
+    errno_t err;
     /* copy useful stuff from our command slot; we may later have to reorder */
-    memcpy(&ts, as, sizeof(ts));       /* copy whole thing */
+#if _MSC_VER < 1400
+    memcpy(&ts, as, sizeof(ts));    /* copy whole thing */
+#else
+    err = memcpy_s(&ts, sizeof(ts), as, sizeof(ts));  /* copy whole thing */
+    if ( err ) {
+        fprintf (stderr, "memcpy_s failure on ts");
+        exit(1);
+    }
+#endif
     return SetVolCmd(&ts, arock);
 }
 
@@ -1411,13 +1627,16 @@ SetVolCmd(struct cmd_syndesc *as, void *arock) {
     struct ViceIoctl blob;
     struct cmd_item *ti;
     struct VolumeStatus *status;
-    char *motd, *offmsg, *input;
+    char *motd, *offmsg, *input, *destEnd;
+    size_t destRemaining;
     int error = 0;
+    size_t len;
 
     SetDotDefault(&as->parms[0].items);
     for(ti=as->parms[0].items; ti; ti=ti->next) {
        /* once per file */
-       blob.out_size = MAXSIZE;
+        destRemaining = sizeof(space);
+       blob.out_size = AFS_PIOCTL_MAXSIZE;
        blob.in_size = sizeof(*status) + 3;     /* for the three terminating nulls */
        blob.out = space;
        blob.in = space;
@@ -1425,7 +1644,7 @@ SetVolCmd(struct cmd_syndesc *as, void *arock) {
        status->MinQuota = status->MaxQuota = -1;
        motd = offmsg = NULL;
        if (as->parms[1].items) {
-           code = util_GetInt32(as->parms[1].items->data, &status->MaxQuota);
+           code = util_GetHumanInt32(as->parms[1].items->data, &status->MaxQuota);
            if (code) {
                fprintf(stderr,"%s: bad integer specified for quota.\n", pn);
                error = 1;
@@ -1438,30 +1657,43 @@ SetVolCmd(struct cmd_syndesc *as, void *arock) {
             offmsg = as->parms[3].items->data;
        input = (char *)status + sizeof(*status);
        *(input++) = '\0';      /* never set name: this call doesn't change vldb */
+        destRemaining -= sizeof(*status) + 1;
        if(offmsg) {
-            if (strlen(offmsg) >= VMSGSIZE) {
-                fprintf(stderr,"%s: message must be shorter than %d characters\n",
+            if( FAILED(StringCbLength(offmsg, VMSGSIZE, &len))) {
+               fprintf(stderr,"%s: message must be shorter than %d characters\n",
                          pn, VMSGSIZE);
                 error = 1;
                 continue;
-            }
-           strcpy(input,offmsg);
-           blob.in_size += (long)strlen(offmsg);
-           input += strlen(offmsg) + 1;
-       } else 
+           }
+            if( FAILED(StringCbCopyEx(input, destRemaining, offmsg, &destEnd, &destRemaining, STRSAFE_FILL_ON_FAILURE))) {
+               fprintf (stderr, "input - not enough space");
+                exit(1);
+           }
+           blob.in_size += destEnd - input;
+           input = destEnd + 1;
+           destRemaining -= sizeof(char);
+       } else {
             *(input++) = '\0';
+            destRemaining -= sizeof(char);
+        }
        if(motd) {
-            if (strlen(motd) >= VMSGSIZE) {
-                fprintf(stderr,"%s: message must be shorter than %d characters\n",
-                         pn, VMSGSIZE);
+            if( FAILED(StringCbLength(motd, VMSGSIZE, &len))) {
+               fprintf(stderr,"%s: message must be shorter than %d characters\n",
+                    pn, VMSGSIZE);
                 return code;
-            }
-           strcpy(input,motd);
-           blob.in_size += (long)strlen(motd);
-           input += strlen(motd) + 1;
-       } else 
+           }
+           if( FAILED(StringCbCopyEx(input, destRemaining, motd, &destEnd, &destRemaining, STRSAFE_FILL_ON_FAILURE))) {
+                fprintf (stderr, "input - not enough space");
+                exit(1);
+           }
+           blob.in_size += (long)(destEnd - input);
+           input = destEnd + 1;
+           destRemaining -= sizeof(char);
+       } else {
             *(input++) = '\0';
-       code = pioctl(ti->data,VIOCSETVOLSTAT, &blob, 1);
+            destRemaining -= sizeof(char);
+        }
+       code = pioctl_utf8(ti->data,VIOCSETVOLSTAT, &blob, 1);
        if (code) {
            Die(errno, ti->data);
            error = 1;
@@ -1470,16 +1702,31 @@ SetVolCmd(struct cmd_syndesc *as, void *arock) {
     return error;
 }
 
-#ifndef WIN32
-/* 
- * Why is VenusFid declared in the kernel-only section of afs.h, 
- * if it's the exported interface of the (UNIX) cache manager?
- */
-struct VenusFid {
-    afs_int32 Cell;
-    AFSFid Fid;
-};
-#endif /* WIN32 */
+/* values match cache manager File Types */
+static char *
+filetypestr(afs_uint32 type)
+{
+    char * s = "Object";
+
+    switch (type) {
+    case 1:     /* file */
+        s = "File";
+        break;
+    case 2:
+        s = "Directory";
+        break;
+    case 3:
+        s = "Symlink";
+        break;
+    case 4:
+        s = "Mountpoint";
+        break;
+    case 5:
+        s = "DfsLink";
+        break;
+    }
+    return s;
+}
 
 static int 
 ExamineCmd(struct cmd_syndesc *as, void *arock)
@@ -1490,58 +1737,104 @@ ExamineCmd(struct cmd_syndesc *as, void *arock)
     struct VolumeStatus *status;
     char *name, *offmsg, *motd;
     int error = 0;
-    
+    int literal = 0;
+    cm_ioctlQueryOptions_t options;
+    int len;
+
+    if (as->parms[1].items)
+        literal = 1;
+
     SetDotDefault(&as->parms[0].items);
     for(ti=as->parms[0].items; ti; ti=ti->next) {
         cm_fid_t fid;
-       afs_uint32 owner[2];
-       char cell[MAXCELLCHARS];
+        afs_uint32 filetype;
+       afs_int32 owner[2];
+       char cell[CELL_MAXNAMELEN];
+
+        /* once per file */
+        memset(&fid, 0, sizeof(fid));
+        memset(&options, 0, sizeof(options));
+        filetype = 0;
+        options.size = sizeof(options);
+        options.field_flags |= CM_IOCTL_QOPTS_FIELD_LITERAL;
+        options.literal = literal;
+       blob.in_size = options.size;    /* no variable length data */
+        blob.in = &options;
 
-       code = GetCell(ti->data, cell);
-       if (code) {
+        blob.out_size = sizeof(cm_fid_t);
+        blob.out = (char *) &fid;
+        if (0 == pioctl_utf8(ti->data, VIOCGETFID, &blob, 1) &&
+            blob.out_size == sizeof(cm_fid_t)) {
+            options.field_flags |= CM_IOCTL_QOPTS_FIELD_FID;
+            options.fid = fid;
+        } else {
            Die(errno, ti->data);
            error = 1;
            continue;
-       }
+        }
 
-       /* once per file */
-       blob.in_size = 0;
+        blob.out_size = sizeof(filetype);
+        blob.out = &filetype;
 
-        blob.out_size = sizeof(cm_fid_t);
-        blob.out = (char *) &fid;
-        if (0 == pioctl(ti->data, VIOCGETFID, &blob, 1)) {
-            printf("File %s (%u.%u.%u) contained in cell %s\n",
-                    ti->data, fid.volume, fid.vnode, fid.unique,
-                    cell);
+        code = pioctl_utf8(ti->data, VIOC_GETFILETYPE, &blob, 1);
+        if (code || blob.out_size != sizeof(filetype)) {
+           Die(errno, ti->data);
+           error = 1;
+           continue;
         }
 
+        blob.out_size = CELL_MAXNAMELEN;
+        blob.out = cell;
+
+        code = pioctl_utf8(ti->data, VIOC_FILE_CELL_NAME, &blob, 1);
+        if (code == 0)
+            cell[blob.out_size-1] = '\0';
+        printf("%s %s (%u.%u.%u) contained in cell %s\n",
+                filetypestr(filetype),
+                ti->data, fid.volume, fid.vnode, fid.unique,
+                code ? "unknown-cell" : cell);
+
        blob.out_size = 2 * sizeof(afs_uint32);
         blob.out = (char *) &owner;
-       if (0 == pioctl(ti->data, VIOCGETOWNER, &blob, 1)) {
+       if (0 == pioctl_utf8(ti->data, VIOCGETOWNER, &blob, 1) &&
+            blob.out_size == 2 * sizeof(afs_uint32)) {
            char oname[PR_MAXNAMELEN] = "(unknown)";
+           char gname[PR_MAXNAMELEN] = "(unknown)";
+            char confDir[257];
 
            /* Go to the PRDB and see if this all number username is valid */
-           pr_Initialize(1, AFSDIR_CLIENT_ETC_DIRPATH, cell);
+            cm_GetConfigDir(confDir, sizeof(confDir));
+
+            pr_Initialize(1, confDir, cell);
            pr_SIdToName(owner[0], oname);
-           printf("Owner %s (%u) Group %u\n", oname, owner[0], owner[1]);
+           pr_SIdToName(owner[1], gname);
+           printf("Owner %s (%d) Group %s (%d)\n", oname, owner[0], gname, owner[1]);
         }
 
        blob.out = space;
-       blob.out_size = MAXSIZE;
-       code = pioctl(ti->data, VIOCGETVOLSTAT, &blob, 1);
+       blob.out_size = AFS_PIOCTL_MAXSIZE;
+       code = pioctl_utf8(ti->data, VIOCGETVOLSTAT, &blob, 1);
        if (code == 0) {
+            space[blob.out_size - 1] = '\0';
             status = (VolumeStatus *)space;
             name = (char *)status + sizeof(*status);
-            offmsg = name + strlen(name) + 1;
-            motd = offmsg + strlen(offmsg) + 1;
-
+            if( FAILED(StringCbLength(name, sizeof(space) - (name - space), &len))) {
+               fprintf (stderr, "StringCbLength failure on name");
+               exit(1);
+           }
+            offmsg = name + len + 1;
+            if( FAILED(StringCbLength(offmsg, sizeof(space) - (offmsg - space), &len))) {
+               fprintf (stderr, "StringCbLength failure on offmsg");
+               exit(1);
+           }
+            motd = offmsg + len + 1;
             PrintStatus(status, name, motd, offmsg);
         } else {
             Die(errno, ti->data);
         }
 
         errno = 0;
-        code = pioctl(ti->data, VIOC_PATH_AVAILABILITY, &blob, 1);
+        code = pioctl_utf8(ti->data, VIOC_PATH_AVAILABILITY, &blob, 1);
         switch (errno) {
         case 0:
             printf("Volume is online\n");
@@ -1575,20 +1868,21 @@ ListQuotaCmd(struct cmd_syndesc *as, void *arock)
 
     int error = 0;
     
-    printf("%-25s%-10s%-10s%-7s%-13s\n", 
-           "Volume Name", "     Quota", "      Used", "  %Used", "    Partition");
+    printf("%-25s%-11s%-11s%-7s%-13s\n", "Volume Name", "      Quota",
+          "       Used", "  %Used", "    Partition");
     SetDotDefault(&as->parms[0].items);
     for(ti=as->parms[0].items; ti; ti=ti->next) {
        /* once per file */
-       blob.out_size = MAXSIZE;
+       blob.out_size = AFS_PIOCTL_MAXSIZE;
        blob.in_size = 0;
        blob.out = space;
-       code = pioctl(ti->data, VIOCGETVOLSTAT, &blob, 1);
+       code = pioctl_utf8(ti->data, VIOCGETVOLSTAT, &blob, 1);
        if (code) {
            Die(errno, ti->data);
             error = 1;
            continue;
        }
+        space[blob.out_size - 1] = '\0';
        status = (VolumeStatus *)space;
        name = (char *)status + sizeof(*status);
        QuickPrintStatus(status, name);
@@ -1606,24 +1900,63 @@ WhereIsCmd(struct cmd_syndesc *as, void *arock)
     afs_int32 *hosts;
     char *tp;
     int error = 0;
+    int literal = 0;
+    cm_ioctlQueryOptions_t options;
+
+    if (as->parms[1].items)
+        literal = 1;
     
     SetDotDefault(&as->parms[0].items);
     for(ti=as->parms[0].items; ti; ti=ti->next) {
-       /* once per file */
-       blob.out_size = MAXSIZE;
-       blob.in_size = 0;
+        cm_fid_t fid;
+        afs_uint32 filetype;
+
+        /* once per file */
+        memset(&fid, 0, sizeof(fid));
+        memset(&options, 0, sizeof(options));
+        filetype = 0;
+        options.size = sizeof(options);
+        options.field_flags |= CM_IOCTL_QOPTS_FIELD_LITERAL;
+        options.literal = literal;
+       blob.in_size = options.size;    /* no variable length data */
+        blob.in = &options;
+        
+        blob.out_size = sizeof(cm_fid_t);
+        blob.out = (char *) &fid;
+        if (0 == pioctl_utf8(ti->data, VIOCGETFID, &blob, 1) &&
+            blob.out_size == sizeof(cm_fid_t)) {
+            options.field_flags |= CM_IOCTL_QOPTS_FIELD_FID;
+            options.fid = fid;
+        } else {
+           Die(errno, ti->data);
+           error = 1;
+           continue;
+        }
+
+        blob.out_size = sizeof(filetype);
+        blob.out = &filetype;
+
+        code = pioctl_utf8(ti->data, VIOC_GETFILETYPE, &blob, 1);
+        if (code || blob.out_size != sizeof(filetype)) {
+           Die(errno, ti->data);
+            error = 1;
+           continue;
+        }
+        blob.out_size = AFS_PIOCTL_MAXSIZE;
        blob.out = space;
        memset(space, 0, sizeof(space));
-       code = pioctl(ti->data, VIOCWHEREIS, &blob, 1);
+       code = pioctl_utf8(ti->data, VIOCWHEREIS, &blob, 1);
        if (code) {
            Die(errno, ti->data);
             error = 1;
            continue;
        }
-       hosts = (afs_int32 *) space;
-       printf("File %s is on host%s ", ti->data, 
+        hosts = (afs_int32 *) space;
+       printf("%s %s is on host%s ", 
+                filetypestr(filetype),
+                ti->data,
                 (hosts[0] && !hosts[1]) ? "": "s");
-       for(j=0; j<MAXHOSTS; j++) {
+       for(j=0; j<AFS_MAXHOSTS; j++) {
            if (hosts[j] == 0) 
                 break;
            tp = hostutil_GetNameByINet(hosts[j]);
@@ -1650,15 +1983,16 @@ DiskFreeCmd(struct cmd_syndesc *as, void *arock)
     SetDotDefault(&as->parms[0].items);
     for(ti=as->parms[0].items; ti; ti=ti->next) {
        /* once per file */
-       blob.out_size = MAXSIZE;
+       blob.out_size = AFS_PIOCTL_MAXSIZE;
        blob.in_size = 0;
        blob.out = space;
-       code = pioctl(ti->data, VIOCGETVOLSTAT, &blob, 1);
+       code = pioctl_utf8(ti->data, VIOCGETVOLSTAT, &blob, 1);
        if (code) {
            Die(errno, ti->data);
             error = 1;
            continue;
        }
+        space[blob.out_size - 1] = '\0';
        status = (VolumeStatus *)space;
        name = (char *)status + sizeof(*status);
        QuickPrintSpace(status, name);
@@ -1679,16 +2013,20 @@ QuotaCmd(struct cmd_syndesc *as, void *arock)
     SetDotDefault(&as->parms[0].items);
     for(ti=as->parms[0].items; ti; ti=ti->next) {
        /* once per file */
-       blob.out_size = MAXSIZE;
+       blob.out_size = AFS_PIOCTL_MAXSIZE;
        blob.in_size = 0;
        blob.out = space;
-       code = pioctl(ti->data, VIOCGETVOLSTAT, &blob, 1);
-       if (code) {
+       code = pioctl_utf8(ti->data, VIOCGETVOLSTAT, &blob, 1);
+        /*
+         * The response is VolumeStatus, volume name, offline message, and motd
+         */
+       if (code || blob.out_size < sizeof(*status)) {
            Die(errno, ti->data);
             error = 1;
            continue;
        }
-       status = (VolumeStatus *)space;
+
+        status = (VolumeStatus *)space;
        if (status->MaxQuota) 
             quotaPct = ((((double)status->BlocksInUse)/status->MaxQuota) * 100.0);
        else 
@@ -1708,6 +2046,8 @@ ListMountCmd(struct cmd_syndesc *as, void *arock)
     char true_name[1024];              /*``True'' dirname (e.g., symlink target)*/
     char parent_dir[1024];             /*Parent directory of true name*/
     char *last_component;      /*Last component of true name*/
+    size_t len;
+
 #ifndef WIN32
     struct stat statbuff;              /*Buffer for status info*/
 #endif /* not WIN32 */
@@ -1721,11 +2061,18 @@ ListMountCmd(struct cmd_syndesc *as, void *arock)
        /* once per file */
        thru_symlink = 0;
 #ifdef WIN32
-       strcpy(orig_name, ti->data);
+    if( FAILED(StringCbCopy(orig_name, sizeof(orig_name), ti->data))) {
+        fprintf (stderr, "orig_name - not enough space");
+        exit(1);
+    }
 #else /* not WIN32 */
-       sprintf(orig_name, "%s%s",
-               (ti->data[0] == '/') ? "" : "./",
-               ti->data);
+
+    if( FAILED(StringCbPrintf(orig_name, sizeof(orig_name), "%s%s",
+        (ti->data[0] == '/') ? "" : "./",
+        ti->data))) {
+        fprintf (stderr, "orig_name - cannot be populated");
+        exit(1);
+    }
 #endif /* not WIN32 */
 
 #ifndef WIN32
@@ -1766,14 +2113,27 @@ ListMountCmd(struct cmd_syndesc *as, void *arock)
             * name (we know there is one) and splice in the symlink value.
             */
            if (true_name[0] != '\\') {
-               last_component = (char *) strrchr(orig_name, '\\');
-               strcpy(++last_component, true_name);
-               strcpy(true_name, orig_name);
+               last_component = (char *) strrchr(orig_name, '\\');
+               if( FAILED(StringCbCopy(++last_component, sizeof(orig_name) - (last_component - orig_name) * sizeof(char), true_name))) {
+                   fprintf (stderr, "last_component - not enough space");
+                    exit(1);
+               }
+               if( FAILED(StringCbCopy(true_name, sizeof(true_name), orig_name))) {
+                   fprintf (stderr, "true_name - not enough space");
+                    exit(1);
+               }
            }
-       } else
-           strcpy(true_name, orig_name);
+       } else {
+           if( FAILED(StringCbCopy(true_name, sizeof(true_name), orig_name))) {
+               fprintf (stderr, "true_name - not enough space");
+                exit(1);
+            }
+       }
 #else  /* WIN32 */
-       strcpy(true_name, orig_name);
+       if( FAILED(StringCbCopy(true_name, sizeof(true_name), orig_name))) {
+           fprintf (stderr, "true_name - not enough space");
+            exit(1);
+        }
 #endif /* WIN32 */
 
        /*
@@ -1789,20 +2149,28 @@ ListMountCmd(struct cmd_syndesc *as, void *arock)
             * Found it.  Designate everything before it as the parent directory,
             * everything after it as the final component.
             */
-           strncpy(parent_dir, true_name, last_component - true_name + 1);
+           if( FAILED(StringCchCopyN(parent_dir, sizeof(parent_dir) / sizeof(char), true_name, last_component - true_name + 1))) {
+                fprintf (stderr, "parent_dir - not enough space");
+                exit(1);
+           }
            parent_dir[last_component - true_name + 1] = 0;
            last_component++;   /*Skip the slash*/
 #ifdef WIN32
            if (!InAFS(parent_dir)) {
                const char * nbname = NetbiosName();
-               int len = (int)strlen(nbname);
-
+               if( FAILED(StringCbLength(nbname, NETBIOSNAMESZ, &len))) {
+                   fprintf (stderr, "StringCbLength failure on nbname");
+                   exit(1);
+               }
                if (parent_dir[0] == '\\' && parent_dir[1] == '\\' &&
                    parent_dir[len+2] == '\\' &&
                    parent_dir[len+3] == '\0' &&
                    !strnicmp(nbname,&parent_dir[2],len))
                {
-                   sprintf(parent_dir,"\\\\%s\\all\\", nbname);
+                   if( FAILED(StringCbPrintf(parent_dir, sizeof(parent_dir),"\\\\%s\\all\\", nbname))) {
+                       fprintf (stderr, "parent_dir - cannot be populated");
+                        exit(1);
+                   }
                }
            }
 #endif
@@ -1812,7 +2180,10 @@ ListMountCmd(struct cmd_syndesc *as, void *arock)
             * directory, and the last component as the given name.
             */
            fs_ExtractDriveLetter(true_name, parent_dir);
-           strcat(parent_dir, ".");
+           if( FAILED(StringCbCat(parent_dir, sizeof(parent_dir), "."))) {
+               fprintf (stderr, "parent_dir - not enough space");
+                exit(1);
+           }
            last_component = true_name;
             fs_StripDriveLetter(true_name, true_name, sizeof(true_name));
        }
@@ -1825,18 +2196,23 @@ ListMountCmd(struct cmd_syndesc *as, void *arock)
        }
 
        blob.in = last_component;
-       blob.in_size = (long)strlen(last_component)+1;
-       blob.out_size = MAXSIZE;
+       if( FAILED(StringCchLength(last_component, sizeof(true_name) / sizeof(char) - (last_component - true_name), &len))) {
+           fprintf (stderr, "StringCbLength failure on last_component");
+           exit(1);
+       }
+       blob.in_size = (long)len+1;
+       blob.out_size = AFS_PIOCTL_MAXSIZE;
        blob.out = space;
-       memset(space, 0, MAXSIZE);
+       memset(space, 0, AFS_PIOCTL_MAXSIZE);
 
-       code = pioctl(parent_dir, VIOC_AFS_STAT_MT_PT, &blob, 1);
+       code = pioctl_utf8(parent_dir, VIOC_AFS_STAT_MT_PT, &blob, 1);
 
        if (code == 0) {
-           printf("'%s' is a %smount point for volume '%s'\n",
+           printf("'%s' is a %smount point for volume '%.*s'\n",
                   ti->data,
                   (thru_symlink ? "symbolic link, leading to a " : ""),
-                  space);
+                   blob.out_size,
+                   space);
 
        } else {
            if (errno == EINVAL) {
@@ -1863,15 +2239,17 @@ MakeMountCmd(struct cmd_syndesc *as, void *arock)
     struct vldbentry vldbEntry;
     struct ViceIoctl blob;
     char * parent;
+    size_t len;
+
+    memset(&info, 0, sizeof(info));
 
     if (as->parms[2].items)    /* cell name specified */
        cellName = as->parms[2].items->data;
     else
        cellName = NULL;
     volName = as->parms[1].items->data;
-
-    if (strlen(volName) >= 64) {
-       fprintf(stderr,"%s: volume name too long (length must be < 64 characters)\n", pn);
+    if( FAILED(StringCbLength(volName, VL_MAXNAMELEN, &len))) {
+        fprintf(stderr,"%s: volume name too long (length must be <= 64 characters)\n", pn);
        return 1;
     }
 
@@ -1893,14 +2271,19 @@ MakeMountCmd(struct cmd_syndesc *as, void *arock)
     if (!InAFS(parent)) {
 #ifdef WIN32
        const char * nbname = NetbiosName();
-       int len = (int)strlen(nbname);
-
+       if ( FAILED(StringCbLength(nbname, NETBIOSNAMESZ, &len))) {
+           fprintf (stderr, "StringCbLength failure on nbname");
+           exit(1);
+       }
        if (parent[0] == '\\' && parent[1] == '\\' &&
            parent[len+2] == '\\' &&
            parent[len+3] == '\0' &&
            !strnicmp(nbname,&parent[2],len))
        {
-           sprintf(path,"%sall\\%s", parent, &as->parms[0].items->data[strlen(parent)]);
+           if( FAILED(StringCbPrintf(path, sizeof(path),"%sall\\%s", parent, &as->parms[0].items->data[len+2]))) {
+               fprintf (stderr, "path - cannot be populated");
+                exit(1);
+            }
            parent = Parent(path);
            if (!InAFS(parent)) {
                fprintf(stderr,"%s: mount points must be created within the AFS file system\n", pn);
@@ -1914,9 +2297,16 @@ MakeMountCmd(struct cmd_syndesc *as, void *arock)
        }
     }
 
-    if ( strlen(path) == 0 )
-       strcpy(path, as->parms[0].items->data);
-
+    if( FAILED(StringCbLength(path, sizeof(path), &len))) {
+        fprintf (stderr, "StringCbLength failure on path");
+        exit(1);
+    }
+    if ( len == 0 ) {
+       if( FAILED(StringCbCopy(path, sizeof(path), as->parms[0].items->data))) {
+           fprintf (stderr, "path - not enough space");
+            exit(1);
+       }
+    }
     if ( IsFreelanceRoot(parent) ) {
        if ( !IsAdmin() ) {
            fprintf(stderr,"%s: Only AFS Client Administrators may alter the root.afs volume\n", pn);
@@ -1927,9 +2317,11 @@ MakeMountCmd(struct cmd_syndesc *as, void *arock)
            blob.in_size = 0;
            blob.out_size = sizeof(localCellName);
            blob.out = localCellName;
-           code = pioctl(parent, VIOC_GET_WS_CELL, &blob, 1);
-           if (!code)
+           code = pioctl_utf8(parent, VIOC_GET_WS_CELL, &blob, 1);
+           if (!code) {
+                localCellName[sizeof(localCellName) - 1] = '\0';
                cellName = localCellName;
+            }
        }
     } else {
        if (!cellName) {
@@ -1959,29 +2351,56 @@ MakeMountCmd(struct cmd_syndesc *as, void *arock)
       }
     }
 
-    if (as->parms[3].items)    /* if -rw specified */
-       strcpy(space, "%");
-    else
-       strcpy(space, "#");
+    if (as->parms[3].items) {  /* if -rw specified */
+       if( FAILED(StringCbCopy(space, sizeof(space), "%"))) {
+           fprintf (stderr, "space arr - not enough space");
+            exit(1);
+       }
+       } else {
+           if( FAILED(StringCbCopy(space, sizeof(space), "#"))) {
+              fprintf (stderr, "space arr - not enough space");
+               exit(1);
+           }
+       }
     if (cellName) {
        /* cellular mount point, prepend cell prefix */
-       strcat(space, info.name);
-       strcat(space, ":");
+       if( FAILED(StringCbCat(space, sizeof(space), info.name))) {
+           fprintf (stderr, "space arr - not enough space");
+            exit(1);
+       }
+       if( FAILED(StringCbCat(space, sizeof(space), ":"))) {
+           fprintf (stderr, "space arr - not enough space");
+            exit(1);
+       }
+    }
+    if( FAILED(StringCbCat(space, sizeof(space), volName))) {    /* append volume name */
+        fprintf (stderr, "space arr - not enough space");
+        exit(1);
+    }
+    if( FAILED(StringCbCat(space, sizeof(space), "."))) {    /* stupid convention; these end with a period */
+        fprintf (stderr, "space arr - not enough space");
+        exit(1);
     }
-    strcat(space, volName);    /* append volume name */
-    strcat(space, ".");                /* stupid convention; these end with a period */
 #ifdef WIN32
     /* create symlink with a special pioctl for Windows NT, since it doesn't
      * have a symlink system call.
      */
     blob.out_size = 0;
-    blob.in_size = 1 + (long)strlen(space);
+    if( FAILED(StringCbLength(space, sizeof(space), &len))) {
+        fprintf (stderr, "StringCbLength failure on space");
+        exit(1);
+    }
+    blob.in_size = 1 + (long)len;
     blob.in = space;
     blob.out = NULL;
-    code = pioctl(path, VIOC_AFS_CREATE_MT_PT, &blob, 0);
+    code = pioctl_utf8(path, VIOC_AFS_CREATE_MT_PT, &blob, 0);
 #else /* not WIN32 */
     code = symlink(space, path);
 #endif /* not WIN32 */
+
+    if (info.linkedCell)
+        free(info.linkedCell);
+
     if (code) {
        Die(errno, path);
        return 1;
@@ -2004,42 +2423,58 @@ RemoveMountCmd(struct cmd_syndesc *as, void *arock) {
     char lsbuffer[1024];
     char *tp;
     int error = 0;
-    
+    size_t len;
+
     for(ti=as->parms[0].items; ti; ti=ti->next) {
        /* once per file */
        tp = (char *) strrchr(ti->data, '\\');
        if (!tp)
            tp = (char *) strrchr(ti->data, '/');
        if (tp) {
-           strncpy(tbuffer, ti->data, code=(afs_int32)(tp-ti->data+1));  /* the dir name */
-            tbuffer[code] = 0;
+            if( FAILED(StringCchCopyN(tbuffer, sizeof(tbuffer) / sizeof(char), ti->data, code=(afs_int32)(tp-ti->data+1)))) {    /* the dir name */
+               fprintf (stderr, "tbuffer - not enough space");
+                exit(1);
+            }
            tp++;   /* skip the slash */
 
 #ifdef WIN32
            if (!InAFS(tbuffer)) {
                const char * nbname = NetbiosName();
-               int len = (int)strlen(nbname);
+               if( FAILED(StringCbLength(nbname, NETBIOSNAMESZ, &len))) {
+                   fprintf (stderr, "StringCbLength failure on nbname");
+                   exit(1);
+               }
 
                if (tbuffer[0] == '\\' && tbuffer[1] == '\\' &&
                    tbuffer[len+2] == '\\' &&
                    tbuffer[len+3] == '\0' &&
                    !strnicmp(nbname,&tbuffer[2],len))
                {
-                   sprintf(tbuffer,"\\\\%s\\all\\", nbname);
+                   if( FAILED(StringCbPrintf(tbuffer, sizeof(tbuffer),"\\\\%s\\all\\", nbname))) {
+                       fprintf (stderr, "tbuffer - cannot be populated");
+                        exit(1);
+                   }
                }
            }
 #endif
        } else {
            fs_ExtractDriveLetter(ti->data, tbuffer);
-           strcat(tbuffer, ".");
+           if( FAILED(StringCbCat(tbuffer, sizeof(tbuffer), "."))) {
+               fprintf (stderr, "tbuffer - not enough space");
+                exit(1);
+           }
            tp = ti->data;
             fs_StripDriveLetter(tp, tp, 0);
        }
        blob.in = tp;
-       blob.in_size = (long)strlen(tp)+1;
+       if( FAILED(StringCbLength(tp, AFS_PIOCTL_MAXSIZE, &len))) {
+           fprintf (stderr, "StringCbLength failure on tp");
+           exit(1);
+       }
+       blob.in_size = (long)len+1;
        blob.out = lsbuffer;
        blob.out_size = sizeof(lsbuffer);
-       code = pioctl(tbuffer, VIOC_AFS_STAT_MT_PT, &blob, 0);
+       code = pioctl_utf8(tbuffer, VIOC_AFS_STAT_MT_PT, &blob, 0);
        if (code) {
            if (errno == EINVAL) {
                fprintf(stderr,"%s: '%s' is not a mount point.\n", pn, ti->data);
@@ -2058,8 +2493,12 @@ RemoveMountCmd(struct cmd_syndesc *as, void *arock) {
 
         blob.out_size = 0;
        blob.in = tp;
-       blob.in_size = (long)strlen(tp)+1;
-       code = pioctl(tbuffer, VIOC_AFS_DELETE_MT_PT, &blob, 0);
+       if( FAILED(StringCbLength(tp, AFS_PIOCTL_MAXSIZE, &len))) {
+           fprintf (stderr, "StringCbLength failure on tp");
+           exit(1);
+       }
+       blob.in_size = (long)len+1;
+       code = pioctl_utf8(tbuffer, VIOC_AFS_DELETE_MT_PT, &blob, 0);
        if (code) {
            Die(errno, ti->data);
             error = 1;
@@ -2081,12 +2520,15 @@ CheckServersCmd(struct cmd_syndesc *as, void *arock)
     char *tp;
     struct afsconf_cell info;
     struct chservinfo checkserv;
+    errno_t err;
+    size_t len;
 
+    memset(&info, 0, sizeof(info));
     memset(&checkserv, 0, sizeof(struct chservinfo));
     blob.in_size=sizeof(struct chservinfo);
     blob.in=(caddr_t)&checkserv;
 
-    blob.out_size = MAXSIZE;
+    blob.out_size = AFS_PIOCTL_MAXSIZE;
     blob.out = space;
     memset(space, 0, sizeof(afs_int32));       /* so we assure zero when nothing is copied back */
 
@@ -2106,10 +2548,22 @@ CheckServersCmd(struct cmd_syndesc *as, void *arock)
        if (code) {
            return 1;
        }
-       strcpy(checkserv.tbuffer,info.name);
-       checkserv.tsize=(int)strlen(info.name)+1;
+       if( FAILED(StringCbCopy(checkserv.tbuffer, sizeof(checkserv.tbuffer), info.name))) {
+           fprintf (stderr, "tbuffer - not enough space");
+            exit(1);
+       }
+       if( FAILED(StringCbLength(info.name, sizeof(info.name), &len))) {
+           fprintf (stderr, "StringCbLength failure on info.name");
+           exit(1);
+       }
+       checkserv.tsize=(int)len+1;
+        if (info.linkedCell)
+            free(info.linkedCell);
     } else {
-        strcpy(checkserv.tbuffer,"\0");
+       if( FAILED(StringCbCopy(checkserv.tbuffer, sizeof(checkserv.tbuffer),"\0"))) {
+           fprintf (stderr, "tbuffer - not enough space");
+            exit(1);
+       }
         checkserv.tsize=0;
     }
 
@@ -2142,7 +2596,7 @@ CheckServersCmd(struct cmd_syndesc *as, void *arock)
 #endif /* WIN32 */
     }
 
-    code = pioctl(0, VIOCCKSERV, &blob, 1);
+    code = pioctl_utf8(0, VIOCCKSERV, &blob, 1);
     if (code) {
        if ((errno == EACCES) && (checkserv.tinterval > 0)) {
            printf("Must be root to change -interval\n");
@@ -2151,7 +2605,16 @@ CheckServersCmd(struct cmd_syndesc *as, void *arock)
        Die(errno, 0);
         return 1;
     }
+#if _MSC_VER < 1400
     memcpy(&temp, space, sizeof(afs_int32));
+#else
+    err = memcpy_s(&temp, sizeof(temp), space, sizeof(afs_int32));
+    if ( err ) {
+        fprintf (stderr, "memcpy_s failure on temp");
+        exit(1);
+    }
+#endif
+
     if (checkserv.tinterval >= 0) {
        if (checkserv.tinterval > 0) 
            printf("The new down server probe interval (%d secs) is now in effect (old interval was %d secs)\n", 
@@ -2164,8 +2627,17 @@ CheckServersCmd(struct cmd_syndesc *as, void *arock)
        printf("All servers are running.\n");
     } else {
        printf("These servers unavailable due to network or server problems: ");
-       for(j=0; j < MAXHOSTS; j++) {
-           memcpy(&temp, space + j*sizeof(afs_int32), sizeof(afs_int32));
+       for(j=0; j < AFS_MAXHOSTS; j++) {
+#if _MSC_VER < 1400
+            memcpy(&temp, space + j*sizeof(afs_int32), sizeof(afs_int32));
+#else
+            err = memcpy_s(&temp, sizeof(temp), space + j*sizeof(afs_int32), sizeof(afs_int32));
+           if ( err ) {
+                fprintf (stderr, "memcpy_s failure on temp");
+                exit(1);
+            }
+#endif
+
            if (temp == 0) 
                 break;
            tp = hostutil_GetNameByINet(temp);
@@ -2188,7 +2660,7 @@ MessagesCmd(struct cmd_syndesc *as, void *arock)
     memset(&gagflags, 0, sizeof(struct gaginfo));
     blob.in_size = sizeof(struct gaginfo);
     blob.in = (caddr_t ) &gagflags;
-    blob.out_size = MAXSIZE;
+    blob.out_size = AFS_PIOCTL_MAXSIZE;
     blob.out = space;
     memset(space, 0, sizeof(afs_int32));       /* so we assure zero when nothing is copied back */
 
@@ -2212,7 +2684,7 @@ MessagesCmd(struct cmd_syndesc *as, void *arock)
     if (code)
         return 1;
 
-    code = pioctl(0, VIOC_GAG, &blob, 1);
+    code = pioctl_utf8(0, VIOC_GAG, &blob, 1);
     if (code) {
        Die(errno, 0);
         return 1;
@@ -2228,7 +2700,7 @@ CheckVolumesCmd(struct cmd_syndesc *as, void *arock)
     
     blob.in_size = 0;
     blob.out_size = 0;
-    code = pioctl(0, VIOCCKBACK, &blob, 1);
+    code = pioctl_utf8(0, VIOCCKBACK, &blob, 1);
     if (code) {
        Die(errno, 0);
        return 1;
@@ -2262,7 +2734,7 @@ SetCacheSizeCmd(struct cmd_syndesc *as, void *arock)
        return 1;
     }
     if (as->parms[0].items) {
-       code = util_GetInt32(as->parms[0].items->data, &temp);
+       code = util_GetHumanInt32(as->parms[0].items->data, &temp);
        if (code) {
            fprintf(stderr,"%s: bad integer specified for cache size.\n", pn);
            return 1;
@@ -2272,7 +2744,7 @@ SetCacheSizeCmd(struct cmd_syndesc *as, void *arock)
     blob.in = (char *) &temp;
     blob.in_size = sizeof(afs_int32);
     blob.out_size = 0;
-    code = pioctl(0, VIOCSETCACHESIZE, &blob, 1);
+    code = pioctl_utf8(0, VIOCSETCACHESIZE, &blob, 1);
     if (code) {
        Die(errno, (char *) 0);
         return 1;
@@ -2294,8 +2766,8 @@ GetCacheParmsCmd(struct cmd_syndesc *as, void *arock)
     blob.in_size = 0;
     blob.out_size = sizeof(parms);
     blob.out = (char *) &parms;
-    code = pioctl(0, VIOCGETCACHEPARMS, &blob, 1);
-    if (code) {
+    code = pioctl_utf8(0, VIOCGETCACHEPARMS, &blob, 1);
+    if (code || blob.out_size != sizeof(parms)) {
        Die(errno, NULL);
         return 1;
     }
@@ -2313,24 +2785,33 @@ ListCellsCmd(struct cmd_syndesc *as, void *arock)
     afs_int32 code;
     afs_int32 i, j, *lp, magic, size;
     char *tp;
-    afs_int32 addr, maxa = OMAXHOSTS;
+    afs_int32 addr, maxa = AFS_OMAXHOSTS;
     struct ViceIoctl blob;
     int resolve;
+    errno_t err;
 
     resolve = !(as->parms[0].items);    /* -numeric */
     
     for(i=0;i<1000;i++) {
        tp = space;
-       memcpy(tp, &i, sizeof(afs_int32));
+#if _MSC_VER < 1400
+        memcpy(tp, &i, sizeof(afs_int32));
+#else
+        err = memcpy_s(tp, sizeof(space), &i, sizeof(afs_int32));
+       if ( err ) {
+            fprintf (stderr, "memcpy_s failure on tp");
+            exit(1);
+        }
+#endif
        tp = (char *)(space + sizeof(afs_int32));
        lp = (afs_int32 *)tp;
        *lp++ = 0x12345678;
        size = sizeof(afs_int32) + sizeof(afs_int32);
-       blob.out_size = MAXSIZE;
+       blob.out_size = AFS_PIOCTL_MAXSIZE;
        blob.in_size = sizeof(afs_int32);
        blob.in = space;
        blob.out = space;
-       code = pioctl(0, VIOCGETCELL, &blob, 1);
+       code = pioctl_utf8(0, VIOCGETCELL, &blob, 1);
        if (code < 0) {
            if (errno == EDOM) 
                 break; /* done with the list */
@@ -2338,16 +2819,31 @@ ListCellsCmd(struct cmd_syndesc *as, void *arock)
             return 1;
        }       
        tp = space;
-       memcpy(&magic, tp, sizeof(afs_int32));  
+#if _MSC_VER < 1400
+        memcpy(&magic, tp, sizeof(afs_int32));
+#else
+        err = memcpy_s(&magic, sizeof(magic), tp, sizeof(afs_int32));
+       if ( err ) {
+            fprintf (stderr, "memcpy_s failure on magic");
+            exit(1);
+        }
+#endif
        if (magic == 0x12345678) {
-           maxa = MAXHOSTS;
+           maxa = AFS_MAXHOSTS;
            tp += sizeof(afs_int32);
        }
        printf("Cell %s on hosts", tp+maxa*sizeof(afs_int32));
-       for(j=0; j < maxa; j++) {
+       for(j=0; j < maxa && j*sizeof(afs_int32) < AFS_PIOCTL_MAXSIZE; j++) {
             char *name, tbuffer[20];
-
-           memcpy(&addr, tp + j*sizeof(afs_int32), sizeof(afs_int32));
+#if _MSC_VER < 1400
+            memcpy(&addr, tp + j*sizeof(afs_int32), sizeof(afs_int32));
+#else
+            err = memcpy_s(&addr, sizeof(addr), tp + j*sizeof(afs_int32), sizeof(afs_int32));
+            if ( err ) {
+                fprintf (stderr, "memcpy_s failure on addr");
+                exit(1);
+           }
+#endif
            if (addr == 0) 
                 break;
 
@@ -2355,8 +2851,11 @@ ListCellsCmd(struct cmd_syndesc *as, void *arock)
                 name = hostutil_GetNameByINet(addr);
             } else {
                 addr = ntohl(addr);
-                sprintf(tbuffer, "%d.%d.%d.%d", (addr >> 24) & 0xff,
-                         (addr >> 16) & 0xff, (addr >> 8) & 0xff, addr & 0xff);
+               if( FAILED(StringCbPrintf(tbuffer, sizeof(tbuffer), "%d.%d.%d.%d", (addr >> 24) & 0xff,
+                         (addr >> 16) & 0xff, (addr >> 8) & 0xff, addr & 0xff))) {
+                   fprintf (stderr, "tbuffer - cannot be populated");
+                    exit(1);
+               }
                 name = tbuffer;
             }
            printf(" %s", name);
@@ -2373,24 +2872,39 @@ ListAliasesCmd(struct cmd_syndesc *as, void *arock)
     afs_int32 code, i;
     char *tp, *aliasName, *realName;
     struct ViceIoctl blob;
+    errno_t err;
+    size_t len;
 
     for (i = 0;; i++) {
        tp = space;
-       memcpy(tp, &i, sizeof(afs_int32));
-       blob.out_size = MAXSIZE;
+#if _MSC_VER < 1400
+        memcpy(tp, &i, sizeof(afs_int32));
+#else
+        err = memcpy_s(tp, sizeof(space), &i, sizeof(afs_int32));
+       if ( err ) {
+            fprintf (stderr, "memcpy_s failure on tp");
+            exit(1);
+       }
+#endif
+       blob.out_size = AFS_PIOCTL_MAXSIZE;
        blob.in_size = sizeof(afs_int32);
        blob.in = space;
        blob.out = space;
-       code = pioctl(0, VIOC_GETALIAS, &blob, 1);
+       code = pioctl_utf8(0, VIOC_GETALIAS, &blob, 1);
        if (code < 0) {
            if (errno == EDOM)
                break;          /* done with the list */
            Die(errno, 0);
            return 1;
        }
+        space[blob.out_size - 1] = '\0';
        tp = space;
        aliasName = tp;
-       tp += strlen(aliasName) + 1;
+       if( FAILED(StringCbLength(aliasName, sizeof(space), &len))) {
+           fprintf (stderr, "StringCbLength failure on aliasName");
+           exit(1);
+       }
+       tp += len + 1;
        realName = tp;
        printf("Alias %s for cell %s\n", aliasName, realName);
     }
@@ -2415,8 +2929,16 @@ CallBackRxConnCmd(struct cmd_syndesc *as, void *arock)
        if (!thp) {
            fprintf(stderr, "host %s not found in host table.\n", ti->data);
            return 1;
-       }
-       else memcpy(&hostAddr, thp->h_addr, sizeof(afs_int32));
+       } else {
+#if _MSC_VER < 1400
+            memcpy(&hostAddr, thp->h_addr, sizeof(afs_int32));
+#else
+            err = memcpy_s(&hostAddr, sizeof(hostAddr), thp->h_addr, sizeof(afs_int32));
+           if ( err ) {
+                fprintf (stderr, "memcpy_s failure on hostAddr");
+                exit(1);
+            }
+#endif
     } else {
         hostAddr = 0;   /* means don't set host */
        setp = 0;       /* aren't setting host */
@@ -2428,7 +2950,7 @@ CallBackRxConnCmd(struct cmd_syndesc *as, void *arock)
     blob.in = (char *) &hostAddr;
     blob.out = (char *) &hostAddr;
     
-    code = pioctl(0, VIOC_CBADDR, &blob, 1);
+    code = pioctl_utf8(0, VIOC_CBADDR, &blob, 1);
     if (code < 0) {
        Die(errno, 0);
        return 1;
@@ -2444,11 +2966,14 @@ NewCellCmd(struct cmd_syndesc *as, void *arock)
     afs_int32 code, linkedstate=0, size=0, *lp;
     struct ViceIoctl blob;
     struct cmd_item *ti;
-    char *tp, *cellname=0;
+    char *destEnd, *tp, *cellname=0;
     struct hostent *thp;
     afs_int32 fsport = 0, vlport = 0;
+    errno_t err;
+    int len;
+    size_t destRemaining;
 
-    memset(space, 0, MAXHOSTS * sizeof(afs_int32));
+    memset(space, 0, AFS_MAXHOSTS * sizeof(afs_int32));
     tp = space;
     lp = (afs_int32 *)tp;
     *lp++ = 0x12345678;
@@ -2460,7 +2985,15 @@ NewCellCmd(struct cmd_syndesc *as, void *arock)
                   pn, ti->data);
        }
        else {
-           memcpy(tp, thp->h_addr, sizeof(afs_int32));
+#if _MSC_VER < 1400
+            memcpy(tp, thp->h_addr, sizeof(afs_int32));
+#else
+            err = memcpy_s(tp, sizeof(space) - (tp - space) * sizeof(char), thp->h_addr, sizeof(afs_int32));
+           if ( err ) {
+                fprintf (stderr, "memcpy_s failure on tp");
+                exit(1);
+            }
+#endif
            tp += sizeof(afs_int32);
        }
     }
@@ -2488,22 +3021,29 @@ NewCellCmd(struct cmd_syndesc *as, void *arock)
        }
     }
 #endif
-    tp = (char *)(space + (MAXHOSTS+1) *sizeof(afs_int32));
+    tp = (char *)(space + (AFS_MAXHOSTS+1) *sizeof(afs_int32));
     lp = (afs_int32 *)tp;    
     *lp++ = fsport;
     *lp++ = vlport;
     *lp = linkedstate;
-    strcpy(space +  ((MAXHOSTS+4) * sizeof(afs_int32)), as->parms[0].items->data);
-    size = ((MAXHOSTS+4) * sizeof(afs_int32)) + strlen(as->parms[0].items->data) + 1 /* for null */;
-    tp = (char *)(space + size);
+    if( FAILED(StringCbCopyEx(space +  ((AFS_MAXHOSTS+4) * sizeof(afs_int32)), sizeof(space) - (AFS_MAXHOSTS+4) * sizeof(afs_int32)
+        , as->parms[0].items->data, &tp, &destRemaining, STRSAFE_FILL_ON_FAILURE))) {
+        fprintf (stderr, "var - not enough space");
+        exit(1);
+    }
+    tp++;   /* for null */
+    destRemaining -= sizeof(char);
     if (linkedstate) {
-       strcpy(tp, cellname);
-       size += strlen(cellname) + 1;
+        if( FAILED(StringCbCopyEx(tp, sizeof(space) - size, cellname, &destEnd, &destRemaining, STRSAFE_FILL_ON_FAILURE))) {
+            fprintf (tp, "space arr - not enough space");
+            exit(1);
+       }
+        size += destEnd - tp + 1;
     }
     blob.in_size = size;
     blob.in = space;
     blob.out_size = 0;
-    code = pioctl(0, VIOCNEWCELL, &blob, 1);
+    code = pioctl_utf8(0, VIOCNEWCELL, &blob, 1);
     if (code < 0)
        Die(errno, 0);
     return 0;
@@ -2518,10 +3058,10 @@ NewCellCmd(struct cmd_syndesc *as, void *arock)
 
     blob.in_size = 0;
     blob.in = (char *) 0;
-    blob.out_size = MAXSIZE;
+    blob.out_size = AFS_PIOCTL_MAXSIZE;
     blob.out = space;
 
-    code = pioctl((char *) 0, VIOCNEWCELL, &blob, 1);
+    code = pioctl_utf8((char *) 0, VIOCNEWCELL, &blob, 1);
 
     if (code) {
         Die(errno, (char *) 0);
@@ -2541,21 +3081,30 @@ NewAliasCmd(struct cmd_syndesc *as, void *arock)
     struct ViceIoctl blob;
     char *tp;
     char *aliasName, *realName;
+    size_t destRemaining = sizeof(space);
 
     /* Setup and do the NEWALIAS pioctl call */
     aliasName = as->parms[0].items->data;
     realName = as->parms[1].items->data;
     tp = space;
-    strcpy(tp, aliasName);
-    tp += strlen(aliasName) + 1;
-    strcpy(tp, realName);
-    tp += strlen(realName) + 1;
+    if( FAILED(StringCbCopyEx(tp, destRemaining, aliasName, &tp, &destRemaining, STRSAFE_FILL_ON_FAILURE))) {
+        fprintf (stderr, "tp - not enough space");
+        exit(1);
+    }
+    tp++;
+    destRemaining -= sizeof(char);
+    if( FAILED(StringCbCopyEx(tp, destRemaining, realName, &tp, &destRemaining, STRSAFE_FILL_ON_FAILURE))) {
+        fprintf (stderr, "tp - not enough space");
+        exit(1);
+    }
+    tp++;
+    destRemaining -= sizeof(char);
 
     blob.in_size = tp - space;
     blob.in = space;
     blob.out_size = 0;
     blob.out = space;
-    code = pioctl(0, VIOC_NEWALIAS, &blob, 1);
+    code = pioctl_utf8(0, VIOC_NEWALIAS, &blob, 1);
     if (code < 0) {
        if (errno == EEXIST) {
            fprintf(stderr,
@@ -2575,21 +3124,67 @@ WhichCellCmd(struct cmd_syndesc *as, void *arock)
 {
     afs_int32 code;
     struct cmd_item *ti;
+    struct ViceIoctl blob;
     int error = 0;
-    char cell[MAXCELLCHARS]="";
+    int literal = 0;
+    cm_ioctlQueryOptions_t options;
+
+    if (as->parms[1].items)
+        literal = 1;
     
     SetDotDefault(&as->parms[0].items);
     for(ti=as->parms[0].items; ti; ti=ti->next) {
-        code = GetCell(ti->data, cell);
-       if (code) {
-           if (errno == ENOENT)
-               fprintf(stderr,"%s: no such cell as '%s'\n", pn, ti->data);
-           else
-               Die(errno, ti->data);
-           error = 1;
+        cm_fid_t fid;
+        afs_uint32 filetype;
+       char cell[CELL_MAXNAMELEN];
+
+        /* once per file */
+        memset(&fid, 0, sizeof(fid));
+        memset(&options, 0, sizeof(options));
+        filetype = 0;
+        options.size = sizeof(options);
+        options.field_flags |= CM_IOCTL_QOPTS_FIELD_LITERAL;
+        options.literal = literal;
+       blob.in_size = options.size;    /* no variable length data */
+        blob.in = &options;
+
+        blob.out_size = sizeof(cm_fid_t);
+        blob.out = (char *) &fid;
+        if (0 == pioctl_utf8(ti->data, VIOCGETFID, &blob, 1) &&
+            blob.out_size == sizeof(cm_fid_t)) {
+            options.field_flags |= CM_IOCTL_QOPTS_FIELD_FID;
+            options.fid = fid;
+        } else {
+           Die(errno, ti->data);
+           error = 1;
+           continue;
+        }
+
+        blob.out_size = sizeof(filetype);
+        blob.out = &filetype;
+
+        code = pioctl_utf8(ti->data, VIOC_GETFILETYPE, &blob, 1);
+        if (code || blob.out_size != sizeof(filetype)) {
+           Die(errno, ti->data);
+           error = 1;
+           continue;
+        }
+        blob.out_size = CELL_MAXNAMELEN;
+        blob.out = cell;
+
+        code = pioctl_utf8(ti->data, VIOC_FILE_CELL_NAME, &blob, 1);
+       if (code) {
+           if (errno == ENOENT)
+               fprintf(stderr,"%s: no such cell as '%s'\n", pn, ti->data);
+           else
+               Die(errno, ti->data);
+           error = 1;
            continue;
        }
-        printf("File %s lives in cell '%s'\n", ti->data, cell);
+        cell[CELL_MAXNAMELEN - 1] = '\0';
+        printf("%s %s lives in cell '%s'\n",
+                filetypestr(filetype),
+                ti->data, cell);
     }
     return error;
 }
@@ -2602,16 +3197,16 @@ WSCellCmd(struct cmd_syndesc *as, void *arock)
     
     blob.in_size = 0;
     blob.in = NULL;
-    blob.out_size = MAXSIZE;
+    blob.out_size = AFS_PIOCTL_MAXSIZE;
     blob.out = space;
 
-    code = pioctl(NULL, VIOC_GET_WS_CELL, &blob, 1);
+    code = pioctl_utf8(NULL, VIOC_GET_WS_CELL, &blob, 1);
 
     if (code) {
        Die(errno, NULL);
         return 1;
     }
-
+    space[AFS_PIOCTL_MAXSIZE - 1] = '\0';
     printf("This workstation belongs to cell '%s'\n", space);
     return 0;
 }
@@ -2625,6 +3220,7 @@ PrimaryCellCmd(struct cmd_syndesc *as, void *arock)
 }
 */
 
+#ifndef AFS_NT40_ENV
 static int
 MonitorCmd(struct cmd_syndesc *as, void *arock)
 {
@@ -2635,6 +3231,7 @@ MonitorCmd(struct cmd_syndesc *as, void *arock)
     struct hostent *thp;
     char *tp;
     int setp;
+    errno_t err;
     
     ti = as->parms[0].items;
     setp = 1;
@@ -2653,8 +3250,16 @@ MonitorCmd(struct cmd_syndesc *as, void *arock)
                    return 1;
                }
            } else {
+#if _MSC_VER < 1400
                 memcpy(&hostAddr, thp->h_addr, sizeof(afs_int32));
-            }
+#else
+                err = memcpy_s(&hostAddr, sizeof(hostAddr), thp->h_addr, sizeof(afs_int32));
+               if ( err ) {
+                    fprintf (stderr, "memcpy_s failure on hostAddr");
+                    exit(1);
+                }
+#endif
+        }
        }
     } else {
        hostAddr = 0;   /* means don't set host */
@@ -2666,8 +3271,8 @@ MonitorCmd(struct cmd_syndesc *as, void *arock)
     blob.out_size = sizeof(afs_int32);
     blob.in = (char *) &hostAddr;
     blob.out = (char *) &hostAddr;
-    code = pioctl(0, VIOC_AFS_MARINER_HOST, &blob, 1);
-    if (code) {
+    code = pioctl_utf8(0, VIOC_AFS_MARINER_HOST, &blob, 1);
+    if (code || blob.out_size != sizeof(afs_int32)) {
        Die(errno, 0);
        return 1;
     }
@@ -2684,6 +3289,7 @@ MonitorCmd(struct cmd_syndesc *as, void *arock)
     }
     return 0;
 }
+#endif /* AFS_NT40_ENV */
 
 static int
 SysNameCmd(struct cmd_syndesc *as, void *arock)
@@ -2693,6 +3299,9 @@ SysNameCmd(struct cmd_syndesc *as, void *arock)
     struct cmd_item *ti;
     char *input = space;
     afs_int32 setp = 0;
+    errno_t err;
+    size_t destRemaining = sizeof(space);
+    size_t len;
     
     ti = as->parms[0].items;
     if (ti) {
@@ -2711,24 +3320,41 @@ SysNameCmd(struct cmd_syndesc *as, void *arock)
 
     blob.in = space;
     blob.out = space;
-    blob.out_size = MAXSIZE;
+    blob.out_size = AFS_PIOCTL_MAXSIZE;
     blob.in_size = sizeof(afs_int32);
+#if _MSC_VER < 1400
     memcpy(input, &setp, sizeof(afs_int32));
+#else
+    err = memcpy_s(input, destRemaining, &setp, sizeof(afs_int32));
+    if ( err ) {
+        fprintf (stderr, "memcpy_s failure on input");
+        exit(1);
+    }
+#endif
     input += sizeof(afs_int32);
+    destRemaining -= sizeof(afs_int32);
     for (; ti; ti = ti->next) {
         setp++;
-        blob.in_size += (long)strlen(ti->data) + 1;
-        if (blob.in_size > MAXSIZE) {
+        if( FAILED(StringCbCopyEx(input, destRemaining, ti->data, &input, &destRemaining, STRSAFE_FILL_ON_FAILURE))) {
             fprintf(stderr, "%s: sysname%s too long.\n", pn,
                      setp > 1 ? "s" : "");
             return 1;
         }
-        strcpy(input, ti->data);
-        input += strlen(ti->data);
-        *(input++) = '\0';
+        input++;
+        destRemaining -= sizeof(char);
     }
+    blob.in_size = (input - space) * sizeof(char);
+#if _MSC_VER < 1400
     memcpy(space, &setp, sizeof(afs_int32));
-    code = pioctl(0, VIOC_AFS_SYSNAME, &blob, 1);
+#else
+    err = memcpy_s(space, sizeof(space), &setp, sizeof(afs_int32));
+    if ( err ) {
+        fprintf (stderr, "memcpy_s failure on space");
+        exit(1);
+    }
+#endif
+
+    code = pioctl_utf8(0, VIOC_AFS_SYSNAME, &blob, 1);
     if (code) {
         Die(errno, 0);
         return 1;
@@ -2739,22 +3365,35 @@ SysNameCmd(struct cmd_syndesc *as, void *arock)
     }
 
     input = space;
+#if _MSC_VER < 1400
     memcpy(&setp, input, sizeof(afs_int32));
+#else
+    err = memcpy_s(&setp, sizeof(setp), input, sizeof(afs_int32));
+    if ( err ) {
+        fprintf (stderr, "memcpy_s failure on setp");
+        exit(1);
+    }
+#endif
     input += sizeof(afs_int32);
     if (!setp) {
         fprintf(stderr,"No sysname name value was found\n");
         return 1;
     } 
-    
+    space[blob.out_size - 1] = '\0';
     printf("Current sysname%s is", setp > 1 ? " list" : "");
     for (; setp > 0; --setp ) {
         printf(" \'%s\'", input);
-        input += strlen(input) + 1;
+        if( FAILED(StringCbLength(input, sizeof(space) - (input - space), &len))) {
+            fprintf (stderr, "StringCbLength failure on input");
+            exit(1);
+        }
+        input += len + 1;
     }
     printf("\n");
     return 0;
 }
 
+#ifndef AFS_NT40_ENV
 static char *exported_types[] = {"null", "nfs", ""};
 static int ExportAfsCmd(struct cmd_syndesc *as, void *arock)
 {
@@ -2833,7 +3472,7 @@ static int ExportAfsCmd(struct cmd_syndesc *as, void *arock)
     blob.in_size = sizeof(afs_int32);
     blob.out = (char *) &exportcall;
     blob.out_size = sizeof(afs_int32);
-    code = pioctl(0, VIOC_EXPORTAFS, &blob, 1);
+    code = pioctl_utf8(0, VIOC_EXPORTAFS, &blob, 1);
     if (code) {
        if (errno == ENODEV) {
            fprintf(stderr,
@@ -2856,7 +3495,7 @@ static int ExportAfsCmd(struct cmd_syndesc *as, void *arock)
     }
     return 0;
 }
-
+#endif
 
 static int
 GetCellCmd(struct cmd_syndesc *as, void *arock)
@@ -2870,7 +3509,9 @@ GetCellCmd(struct cmd_syndesc *as, void *arock)
        afs_int32 junk;
     } args;
     int error = 0;
+    size_t len;
 
+    memset(&info, 0, sizeof(info));
     memset(&args, 0, sizeof(args));      /* avoid Purify UMR error */
     for(ti=as->parms[0].items; ti; ti=ti->next) {
        /* once per cell */
@@ -2881,9 +3522,15 @@ GetCellCmd(struct cmd_syndesc *as, void *arock)
             error = 1;
            continue;
        }
-       blob.in_size = 1+(long)strlen(info.name);
+        if (info.linkedCell)
+            free(info.linkedCell);
+        if( FAILED(StringCbLength(info.name, sizeof(info.name), &len))) {
+           fprintf (stderr, "StringCbLength failure on info.name");
+           exit(1);
+       }
+       blob.in_size = 1+(long)len;
        blob.in = info.name;
-       code = pioctl(0, VIOC_GETCELLSTATUS, &blob, 1);
+       code = pioctl_utf8(0, VIOC_GETCELLSTATUS, &blob, 1);
        if (code) {
            if (errno == ENOENT)
                fprintf(stderr,"%s: the cell named '%s' does not exist\n", pn, info.name);
@@ -2921,6 +3568,8 @@ static int SetCellCmd(struct cmd_syndesc *as, void *arock)
     } args;
     int error = 0;
 
+    memset(&info, 0, sizeof(info));
+
     /* Check arguments. */
     if (as->parms[1].items && as->parms[2].items) {
         fprintf(stderr, "Cannot specify both -suid and -nosuid.\n");
@@ -2954,12 +3603,17 @@ static int SetCellCmd(struct cmd_syndesc *as, void *arock)
             error = 1;
            continue;
        }
-       strcpy(args.cname, info.name);
+        if (info.linkedCell)
+            free(info.linkedCell);
+       if( FAILED(StringCbCopy(args.cname, sizeof(args.cname), info.name))) {
+           fprintf (stderr, "cname - not enough space");
+            exit(1);
+       }
        blob.in_size = sizeof(args);
        blob.in = (caddr_t) &args;
        blob.out_size = 0;
        blob.out = (caddr_t) 0;
-       code = pioctl(0, VIOC_SETCELLSTATUS, &blob, 1);
+       code = pioctl_utf8(0, VIOC_SETCELLSTATUS, &blob, 1);
        if (code) {
            Die(errno, info.name);      /* XXX added cell name to Die() call */
             error = 1;
@@ -2968,45 +3622,25 @@ static int SetCellCmd(struct cmd_syndesc *as, void *arock)
     return error;
 }
 
-#ifdef WIN32
 static int
 GetCellName(char *cellNamep, struct afsconf_cell *infop)
 {
-    strcpy(infop->name, cellNamep);
-    return 0;
-}
-#else
-static int
-GetCellName(char *cellName, struct afsconf_cell *info)
-{
-    struct afsconf_dir *tdir;
-    int code;
-
-    tdir = afsconf_Open(AFSDIR_CLIENT_ETC_CLIENTNAME);
-    if (!tdir) {
-       fprintf(stderr,
-                "Could not process files in configuration directory (%s).\n",
-                 AFSDIR_CLIENT_ETC_CLIENTNAME);
-       return -1;
-    }
-
-    code = afsconf_GetCellInfo(tdir, cellName, AFSCONF_VLDBSERVICE, info);
-    if (code) {
-       fprintf(stderr,"fs: cell %s not in %s/CellServDB\n", cellName, 
-                AFSDIR_CLIENT_ETC_CLIENTNAME);
-       return code;
+    if( FAILED(StringCbCopy(infop->name, sizeof(infop->name), cellNamep))) {
+        fprintf (stderr, "name - not enough space");
+        exit(1);
     }
-
     return 0;
 }
-#endif /* not WIN32 */
 
 static int
 VLDBInit(int noAuthFlag, struct afsconf_cell *info)
 {
     afs_int32 code;
+    char confDir[257];
+
+    cm_GetConfigDir(confDir, sizeof(confDir));
 
-    code = ugen_ClientInit(noAuthFlag, AFSDIR_CLIENT_ETC_DIRPATH, 
+    code = ugen_ClientInit(noAuthFlag, confDir, 
                           info->name, 0, &uclient, 
                            NULL, pn, rxkad_clear,
                            VLDB_MAXSERVERS, AFSCONF_VLDBSERVICE, 50,
@@ -3034,7 +3668,7 @@ pokeServers(void)
 {
     int code;
     cm_SSetPref_t *ssp;
-    code = pioctl(0, VIOC_SETSPREFS, &gblob, 1);
+    code = pioctl_utf8(0, VIOC_SETSPREFS, &gblob, 1);
 
     ssp = (cm_SSetPref_t *)space;
     gblob.in_size = (long)(((char *)&(ssp->servers[0])) - (char *)ssp);
@@ -3052,14 +3686,14 @@ pokeServers(void)
 {
     int code;
 
-    code = pioctl(0, VIOC_SETSPREFS, &gblob, 1);
+    code = pioctl_utf8(0, VIOC_SETSPREFS, &gblob, 1);
     if (code && (errno == EINVAL)) {
        struct setspref *ssp;
        ssp = (struct setspref *)gblob.in;
        if (!(ssp->flags & DBservers)) {
            gblob.in = (void *)&(ssp->servers[0]);
            gblob.in_size -= ((char *)&(ssp->servers[0])) - (char *)ssp;
-           code = pioctl(0, VIOC_SETSPREFS33, &gblob, 1);
+           code = pioctl_utf8(0, VIOC_SETSPREFS33, &gblob, 1);
            return code ? errno : 0;
        }
        fprintf(stderr,
@@ -3079,6 +3713,7 @@ addServer(char *name, unsigned short rank)
     cm_SSetPref_t *ssp;
     cm_SPref_t *sp;
     struct hostent *thostent;
+    errno_t err;
 
 #ifndef MAXUSHORT
 #ifdef MAXSHORT
@@ -3103,7 +3738,16 @@ addServer(char *name, unsigned short rank)
     }
 
     sp = (cm_SPref_t *)((char*)gblob.in + gblob.in_size);
+#if _MSC_VER < 1400
     memcpy (&(sp->host.s_addr), thostent->h_addr, sizeof(afs_uint32));
+#else
+    err = memcpy_s (&(sp->host.s_addr), sizeof(afs_uint32), thostent->h_addr, sizeof(afs_uint32));
+    if ( err ) {
+        fprintf (stderr, "memcpy_s failure on sp->host.s_addr");
+        exit(1);
+    }
+#endif
+
     sp->rank = (rank > MAXUSHORT ? MAXUSHORT : rank);
     gblob.in_size += sizeof(cm_SPref_t);
     ssp->num_servers++;
@@ -3127,6 +3771,7 @@ addServer(char *name, afs_int32 rank)
     struct hostent *thostent;
     afs_uint32 addr;
     int error = 0;
+    errno_t err;
 
 #ifndef MAXUSHORT
 #ifdef MAXSHORT
@@ -3153,8 +3798,18 @@ addServer(char *name, afs_int32 rank)
        }
 
        sp = (struct spref *)(gblob.in + gblob.in_size);
-       memcpy(&(sp->server.s_addr), thostent->h_addr_list[t],
+#if _MSC_VER < 1400
+        memcpy(&(sp->server.s_addr), thostent->h_addr_list[t],
               sizeof(afs_uint32));
+#else
+        err = memcpy_s(&(sp->server.s_addr), sizeof(afs_uint32), thostent->h_addr_list[t],
+              sizeof(afs_uint32));
+        if ( err ) {
+            fprintf (stderr, "memcpy_s failure on sp->server.s_addr");
+            exit(1);
+        }
+#endif
+
        sp->rank = (rank > MAXUSHORT ? MAXUSHORT : rank);
        gblob.in_size += sizeof(struct spref);
        ssp->num_servers++;
@@ -3210,7 +3865,7 @@ SetPrefCmd(struct cmd_syndesc *as, void * arock)
     gblob.in_size = (long)(((char*)&(ssp->servers[0])) - (char *)ssp);
     gblob.in = space;
     gblob.out = space;
-    gblob.out_size = MAXSIZE;
+    gblob.out_size = AFS_PIOCTL_MAXSIZE;
 
     if ( !IsAdmin() ) {
         fprintf (stderr,"Permission denied: requires AFS Client Administrator access.\n");
@@ -3225,18 +3880,31 @@ SetPrefCmd(struct cmd_syndesc *as, void * arock)
         if (!(infd = fopen(ti->data,"r" ))) {
             code = errno;
             Die(errno,ti->data);
-        }
-        else
+        } else {
+#if _MSC_VER < 1400
             while ( fscanf(infd, "%79s%ld", name, &rank) != EOF) {
                 code = addServer (name, (unsigned short) rank);
             }
+#else
+            while ( fscanf_s(infd, "%79s%ld", name, sizeof(name), &rank) != EOF) {
+                code = addServer (name, (unsigned short) rank);
+            }
+#endif
+        }
     }
 
+
     ti = as->parms[3].items;  /* -stdin */
     if (ti) {
-        while ( scanf("%79s%ld", name, &rank) != EOF) {
+#if _MSC_VER < 1400
+    while ( scanf("%79s%ld", name, &rank) != EOF) {
+            code = addServer (name, (unsigned short) rank);
+        }
+#else
+    while ( scanf_s("%79s%ld", name, sizeof(name), &rank) != EOF) {
             code = addServer (name, (unsigned short) rank);
         }
+#endif
     }
 
     for (ti = as->parms[0].items;ti;ti=ti->next) {/*list of servers, ranks */
@@ -3302,7 +3970,7 @@ SetPrefCmd(struct cmd_syndesc *as, void *arock)
     gblob.in_size = ((char *)&(ssp->servers[0])) - (char *)ssp;
     gblob.in = space;
     gblob.out = space;
-    gblob.out_size = MAXSIZE;
+    gblob.out_size = AFS_PIOCTL_MAXSIZE;
 
 
     if (geteuid()) {
@@ -3318,21 +3986,38 @@ SetPrefCmd(struct cmd_syndesc *as, void *arock)
            perror(ti->data);
            error = -1;
        } else {
-           while (fscanf(infd, "%79s%ld", name, &rank) != EOF) {
+#if _MSC_VER < 1400
+            while (fscanf(infd, "%79s%ld", name, &rank) != EOF) {
+               code = addServer(name, (unsigned short)rank);
+               if (code)
+                   error = code;
+           }
+#else
+            while (fscanf_s(infd, "%79s%ld", name, sizeof(name), &rank) != EOF) {
                code = addServer(name, (unsigned short)rank);
                if (code)
                    error = code;
            }
+#endif
+
        }
     }
 
     ti = as->parms[3].items;   /* -stdin */
     if (ti) {
+#if _MSC_VER < 1400
        while (scanf("%79s%ld", name, &rank) != EOF) {
            code = addServer(name, (unsigned short)rank);
            if (code)
                error = code;
        }
+#else
+        while (scanf_s("%79s%ld", name, sizeof(name), &rank) != EOF) {
+           code = addServer(name, (unsigned short)rank);
+           if (code)
+               error = code;
+       }
+#endif
     }
 
     for (ti = as->parms[0].items; ti; ti = ti->next) { /* list of servers, ranks */
@@ -3428,12 +4113,12 @@ GetPrefCmd(struct cmd_syndesc *as, void *arock)
         blob.in_size=sizeof(struct cm_SPrefRequest);
         blob.in = (char *)in;
         blob.out = space;
-        blob.out_size = MAXSIZE;
+        blob.out_size = AFS_PIOCTL_MAXSIZE;
 
-        in->num_servers = (MAXSIZE - 2*sizeof(short))/sizeof(struct cm_SPref);
+        in->num_servers = (AFS_PIOCTL_MAXSIZE - 2*sizeof(short))/sizeof(struct cm_SPref);
         in->flags = vlservers; 
 
-        code = pioctl(0, VIOC_GETSPREFS, &blob, 1);
+        code = pioctl_utf8(0, VIOC_GETSPREFS, &blob, 1);
         if (code){
             perror("getserverprefs pioctl");
             Die (errno,0);
@@ -3447,8 +4132,11 @@ GetPrefCmd(struct cmd_syndesc *as, void *arock)
                 }
                 else {
                     addr = ntohl(out->servers[i].host.s_addr);
-                    sprintf(tbuffer, "%d.%d.%d.%d", (addr>>24) & 0xff, (addr>>16) & 0xff,
-                             (addr>>8) & 0xff, addr & 0xff);
+                   if( FAILED(StringCbPrintf(tbuffer, sizeof(tbuffer), "%d.%d.%d.%d", (addr>>24) & 0xff, (addr>>16) & 0xff,
+                        (addr>>8) & 0xff, addr & 0xff))) {
+                       fprintf (stderr, "tbuffer - cannot be populated");
+                        exit(1);
+                   }
                     name=tbuffer;
                 }
                 printf ("%-50s %5u\n",name,out->servers[i].rank);      
@@ -3499,13 +4187,13 @@ GetPrefCmd(struct cmd_syndesc *as, void *arock)
        blob.in_size = sizeof(struct sprefrequest);
        blob.in = (char *)in;
        blob.out = space;
-       blob.out_size = MAXSIZE;
+       blob.out_size = AFS_PIOCTL_MAXSIZE;
 
        in->num_servers =
-           (MAXSIZE - 2 * sizeof(short)) / sizeof(struct spref);
+           (AFS_PIOCTL_MAXSIZE - 2 * sizeof(short)) / sizeof(struct spref);
        in->flags = vlservers;
 
-       code = pioctl(0, VIOC_GETSPREFS, &blob, 1);
+       code = pioctl_utf8(0, VIOC_GETSPREFS, &blob, 1);
        if (code) {
            perror("getserverprefs pioctl");
            return 1;
@@ -3518,8 +4206,11 @@ GetPrefCmd(struct cmd_syndesc *as, void *arock)
                name = hostutil_GetNameByINet(out->servers[i].server.s_addr);
            } else {
                addr = ntohl(out->servers[i].server.s_addr);
-               sprintf(tbuffer, "%d.%d.%d.%d", (addr >> 24) & 0xff,
-                       (addr >> 16) & 0xff, (addr >> 8) & 0xff, addr & 0xff);
+               if( FAILED(StringCbPrintf(tbuffer, sizeof(tbuffer), "%d.%d.%d.%d", (addr >> 24) & 0xff,
+                   (addr >> 16) & 0xff, (addr >> 8) & 0xff, addr & 0xff))) {
+                   fprintf (stderr, "tbuffer - cannot be populated");
+                    exit(1);
+               }
                name = tbuffer;
            }
            printf("%-50s %5u\n", name, out->servers[i].rank);
@@ -3532,6 +4223,116 @@ GetPrefCmd(struct cmd_syndesc *as, void *arock)
 }
 #endif /* WIN32 */
 
+static afs_int32
+SmbUnicodeCmd(struct cmd_syndesc * asp, void * arock)
+{
+    long inValue = 0;
+    long outValue = 0;
+    long code;
+
+    struct ViceIoctl blob;
+
+    if (asp->parms[0].items) {
+        /* On */
+
+        inValue = 3;
+    } else if (asp->parms[1].items) {
+        /* Off */
+
+        inValue = 2;
+    }
+
+    if (inValue != 0 && !IsAdmin()) {
+        fprintf (stderr, "Permission denied: Requires AFS Client Administrator access.\n");
+        return EACCES;
+    }
+
+    blob.in_size = sizeof(inValue);
+    blob.in = (char *) &inValue;
+    blob.out_size = sizeof(outValue);
+    blob.out = (char *) &outValue;
+
+    code = pioctl_utf8(NULL, VIOC_UNICODECTL, &blob, 1);
+    if (code) {
+        Die(errno, NULL);
+        return code;
+    }
+
+    if (outValue != 2) {
+        printf("Unicode support is %s%s.\n",
+               ((outValue != 0)? "enabled":"disabled"),
+               ((inValue != 0)? " for new SMB connections":""));
+    } else {
+        printf("Unicode support is absent in this installation of OpenAFS.\n");
+    }
+
+    return 0;
+}
+
+static int
+GetFidCmd(struct cmd_syndesc *as, void *arock)
+{
+    afs_int32 code;
+    struct ViceIoctl blob;
+    struct cmd_item *ti;
+    int error = 0;
+    int literal = 0;
+    cm_ioctlQueryOptions_t options;
+
+    if (as->parms[1].items)
+        literal = 1;
+
+    SetDotDefault(&as->parms[0].items);
+    for(ti=as->parms[0].items; ti; ti=ti->next) {
+        cm_fid_t fid;
+        afs_uint32 filetype;
+       char cell[CELL_MAXNAMELEN];
+
+        /* once per file */
+        memset(&fid, 0, sizeof(fid));
+        memset(&options, 0, sizeof(options));
+        filetype = 0;
+        options.size = sizeof(options);
+        options.field_flags |= CM_IOCTL_QOPTS_FIELD_LITERAL;
+        options.literal = literal;
+       blob.in_size = options.size;    /* no variable length data */
+        blob.in = &options;
+
+        blob.out_size = sizeof(cm_fid_t);
+        blob.out = (char *) &fid;
+        if (0 == pioctl_utf8(ti->data, VIOCGETFID, &blob, 1) &&
+            blob.out_size == sizeof(cm_fid_t)) {
+            options.field_flags |= CM_IOCTL_QOPTS_FIELD_FID;
+            options.fid = fid;
+        } else {
+           Die(errno, ti->data);
+           error = 1;
+           continue;
+        }
+
+        blob.out_size = sizeof(filetype);
+        blob.out = &filetype;
+
+        code = pioctl_utf8(ti->data, VIOC_GETFILETYPE, &blob, 1);
+        if (code || blob.out_size != sizeof(filetype)) {
+           Die(errno, ti->data);
+           error = 1;
+           continue;
+        }
+        blob.out_size = CELL_MAXNAMELEN;
+        blob.out = cell;
+
+        code = pioctl_utf8(ti->data, VIOC_FILE_CELL_NAME, &blob, 1);
+        if (code == 0)
+            cell[CELL_MAXNAMELEN - 1] = '\0';
+        printf("%s %s (%u.%u.%u) contained in cell %s\n",
+                filetypestr(filetype),
+                ti->data, fid.volume, fid.vnode, fid.unique,
+                code ? "unknown-cell" : cell);
+    }
+    return error;
+}
+
 static int
 UuidCmd(struct cmd_syndesc *asp, void *arock)
 {
@@ -3563,8 +4364,8 @@ UuidCmd(struct cmd_syndesc *asp, void *arock)
     blob.out_size = sizeof(outValue);
     blob.out = (char *) &outValue;
 
-    code = pioctl(NULL, VIOC_UUIDCTL, &blob, 1);
-    if (code) {
+    code = pioctl_utf8(NULL, VIOC_UUIDCTL, &blob, 1);
+    if (code || blob.out_size != sizeof(outValue)) {
         Die(errno, NULL);
         return code;
     }
@@ -3622,8 +4423,8 @@ TraceCmd(struct cmd_syndesc *asp, void *arock)
     blob.out_size = sizeof(long);
     blob.out = (char *) &outValue;
         
-    code = pioctl(NULL, VIOC_TRACECTL, &blob, 1);
-    if (code) {
+    code = pioctl_utf8(NULL, VIOC_TRACECTL, &blob, 1);
+    if (code || blob.out_size != sizeof(long)) {
         Die(errno, NULL);
         return code;
     }
@@ -3671,7 +4472,11 @@ StoreBehindCmd(struct cmd_syndesc *as, void *arock)
            return 1;
        }
        tsb.sb_thisfile = strtol(ti->data, &t, 10) * 1024;
-       if ((tsb.sb_thisfile < 0) || (t != ti->data + strlen(ti->data))) {
+       if (errno == ERANGE) {
+           fprintf(stderr, "%s: ti->data must within long int range", pn);
+           return 1;
+       }
+       if ((tsb.sb_thisfile < 0) || (*t != '\0')) {
            fprintf(stderr, "%s: %s must be 0 or a positive number.\n", pn,
                    ti->data);
            return 1;
@@ -3682,7 +4487,11 @@ StoreBehindCmd(struct cmd_syndesc *as, void *arock)
     ti = as->parms[2].items;   /* -allfiles */
     if (ti) {
        allfiles = strtol(ti->data, &t, 10) * 1024;
-       if ((allfiles < 0) || (t != ti->data + strlen(ti->data))) {
+       if (errno == ERANGE) {
+           fprintf(stderr, "%s: ti->data must within long int range", pn);
+           return 1;
+       }
+       if ((allfiles < 0) || (*t != '\0')) {
            fprintf(stderr, "%s: %s must be 0 or a positive number.\n", pn,
                    ti->data);
            return 1;
@@ -3703,14 +4512,14 @@ StoreBehindCmd(struct cmd_syndesc *as, void *arock)
     /* once per -file */
     for (ti = as->parms[1].items; ti; ti = ti->next) {
        /* Do this solely to see if the file is there */
-       code = pioctl(ti->data, VIOCWHEREIS, &blob, 1);
+       code = pioctl_utf8(ti->data, VIOCWHEREIS, &blob, 1);
        if (code) {
            Die(errno, ti->data);
            error = 1;
            continue;
        }
 
-       code = pioctl(ti->data, VIOC_STOREBEHIND, &blob, 1);
+       code = pioctl_utf8(ti->data, VIOC_STOREBEHIND, &blob, 1);
        if (code) {
            Die(errno, ti->data);
            error = 1;
@@ -3734,7 +4543,7 @@ StoreBehindCmd(struct cmd_syndesc *as, void *arock)
      */
     if (!as->parms[1].items || (allfiles != -1)) {
        tsb.sb_default = allfiles;
-       code = pioctl(0, VIOC_STOREBEHIND, &blob, 1);
+       code = pioctl_utf8(0, VIOC_STOREBEHIND, &blob, 1);
        if (code) {
            Die(errno, ((allfiles == -1) ? 0 : "-allfiles"));
            error = 1;
@@ -3769,15 +4578,17 @@ SetCryptCmd(struct cmd_syndesc *as, void *arock)
       flag = 1;
     else if (strcmp(tp, "off") == 0)
       flag = 0;
+    else if (strcmp(tp, "auth") == 0)
+      flag = 2;
     else {
-      fprintf (stderr, "%s: %s must be \"on\" or \"off\".\n", pn, tp);
+      fprintf (stderr, "%s: %s must be \"on\", \"auth\", or \"off\".\n", pn, tp);
       return EINVAL;
     }
 
     blob.in = (char *) &flag;
     blob.in_size = sizeof(flag);
     blob.out_size = 0;
-    code = pioctl(0, VIOC_SETRXKCRYPT, &blob, 1);
+    code = pioctl_utf8(0, VIOC_SETRXKCRYPT, &blob, 1);
     if (code)
         Die(code, NULL);
     return 0;
@@ -3789,21 +4600,33 @@ GetCryptCmd(struct cmd_syndesc *as, void *arock)
     afs_int32 code = 0, flag;
     struct ViceIoctl blob;
     char *tp;
+    errno_t err;
  
     blob.in = NULL;
     blob.in_size = 0;
     blob.out_size = sizeof(flag);
     blob.out = space;
 
-    code = pioctl(0, VIOC_GETRXKCRYPT, &blob, 1);
+    code = pioctl_utf8(0, VIOC_GETRXKCRYPT, &blob, 1);
 
-    if (code) 
+    if (code || blob.out_size != sizeof(flag))
         Die(code, NULL);
     else {
-      tp = space;
-      memcpy(&flag, tp, sizeof(afs_int32));
+        tp = space;
+#if _MSC_VER < 1400
+        memcpy(&flag, tp, sizeof(afs_int32));
+#else
+        err = memcpy_s(&flag, sizeof(flag), tp, sizeof(afs_int32));
+        if ( err ) {
+            fprintf (stderr, "memcpy_s failure on flag");
+            exit(1);
+        }
+#endif
+
       printf("Security level is currently ");
-      if (flag == 1)
+      if (flag == 2)
+          printf("auth (data integrity).\n");
+      else if (flag == 1)
         printf("crypt (data security).\n");
       else
         printf("clear.\n");
@@ -3841,8 +4664,8 @@ MemDumpCmd(struct cmd_syndesc *asp, void *arock)
     blob.out_size = sizeof(long);
     blob.out = (char *) &outValue;
 
-    code = pioctl(NULL, VIOC_TRACEMEMDUMP, &blob, 1);
-    if (code) {
+    code = pioctl_utf8(NULL, VIOC_TRACEMEMDUMP, &blob, 1);
+    if (code || blob.out_size != sizeof(long)) {
         Die(errno, NULL);
         return code;
     }
@@ -3903,6 +4726,7 @@ CSCPolicyCmd(struct cmd_syndesc *asp, void *arock)
     struct cmd_item *ti;
     char *share = NULL;
     HKEY hkCSCPolicy;
+    size_t len;
 
     if ( !IsAdmin() ) {
         fprintf (stderr,"Permission denied: requires AFS Client Administrator access.\n");
@@ -3926,7 +4750,7 @@ CSCPolicyCmd(struct cmd_syndesc *asp, void *arock)
                         0, 
                         "AFS", 
                         REG_OPTION_NON_VOLATILE,
-                        KEY_WRITE,
+                        (IsWow64()?KEY_WOW64_64KEY:0)|KEY_WRITE,
                         NULL, 
                         &hkCSCPolicy,
                         NULL );
@@ -3943,17 +4767,25 @@ CSCPolicyCmd(struct cmd_syndesc *asp, void *arock)
         }
 
         policy = "manual";
+        len = 6;
                
-        if (asp->parms[1].items)
+        if (asp->parms[1].items) {
             policy = "manual";
-        if (asp->parms[2].items)
+            len = 6;
+        }
+        if (asp->parms[2].items) {
             policy = "programs";
-        if (asp->parms[3].items)
+            len = 8;
+        }
+        if (asp->parms[3].items) {
             policy = "documents";
-        if (asp->parms[4].items)
+            len = 9;
+        }
+        if (asp->parms[4].items) {
             policy = "disable";
-               
-        RegSetValueEx( hkCSCPolicy, share, 0, REG_SZ, policy, (DWORD)strlen(policy)+1);
+            len = 7;
+        }
+        RegSetValueEx( hkCSCPolicy, share, 0, REG_SZ, policy, (DWORD)len+1);
                
         printf("CSC policy on share \"%s\" changed to \"%s\".\n\n", share, policy);
         printf("Close all applications that accessed files on this share or restart AFS Client for the change to take effect.\n"); 
@@ -3974,7 +4806,7 @@ CSCPolicyCmd(struct cmd_syndesc *asp, void *arock)
                         0, 
                         "AFS", 
                         REG_OPTION_NON_VOLATILE,
-                        KEY_READ|KEY_QUERY_VALUE,
+                        (IsWow64()?KEY_WOW64_64KEY:0)|KEY_READ|KEY_QUERY_VALUE,
                         NULL, 
                         &hkCSCPolicy,
                         NULL );
@@ -4028,12 +4860,12 @@ GetClientAddrsCmd(struct cmd_syndesc *as, void *arock)
        blob.in_size = sizeof(struct sprefrequest);
        blob.in = (char *)in;
        blob.out = space;
-       blob.out_size = MAXSIZE;
+       blob.out_size = AFS_PIOCTL_MAXSIZE;
 
        in->num_servers =
-           (MAXSIZE - 2 * sizeof(short)) / sizeof(struct spref);
+           (AFS_PIOCTL_MAXSIZE - 2 * sizeof(short)) / sizeof(struct spref);
        /* returns addr in network byte order */
-       code = pioctl(0, VIOC_GETCPREFS, &blob, 1);
+       code = pioctl_utf8(0, VIOC_GETCPREFS, &blob, 1);
        if (code) {
            perror("getClientInterfaceAddr pioctl");
            return 1;
@@ -4046,8 +4878,11 @@ GetClientAddrsCmd(struct cmd_syndesc *as, void *arock)
                afs_int32 addr;
                char tbuffer[32];
                addr = ntohl(out->servers[i].server.s_addr);
-               sprintf(tbuffer, "%d.%d.%d.%d", (addr >> 24) & 0xff,
-                       (addr >> 16) & 0xff, (addr >> 8) & 0xff, addr & 0xff);
+               if( FAILED(StringCbPrintf(tbuffer, sizeof(tbuffer), "%d.%d.%d.%d", (addr >> 24) & 0xff,
+                   (addr >> 16) & 0xff, (addr >> 8) & 0xff, addr & 0xff))) {
+                   fprintf (stderr, "tbuffer - cannot be populated");
+                    exit(1);
+               }
                printf("%-50s\n", tbuffer);
            }
            in->offset = out->next_offset;
@@ -4074,7 +4909,7 @@ SetClientAddrsCmd(struct cmd_syndesc *as, void *arock)
     ssp->num_servers = 0;
     blob.in = space;
     blob.out = space;
-    blob.out_size = MAXSIZE;
+    blob.out_size = AFS_PIOCTL_MAXSIZE;
 
     if (geteuid()) {
        fprintf(stderr, "Permission denied: requires root access.\n");
@@ -4122,7 +4957,7 @@ SetClientAddrsCmd(struct cmd_syndesc *as, void *arock)
     }
     blob.in_size = sizeUsed - sizeof(struct spref);
 
-    code = pioctl(0, VIOC_SETCPREFS, &blob, 1);        /* network order */
+    code = pioctl_utf8(0, VIOC_SETCPREFS, &blob, 1);   /* network order */
     if (code) {
        Die(errno, 0);
        error = 1;
@@ -4145,12 +4980,16 @@ FlushMountCmd(struct cmd_syndesc *as, void *arock)
     int link_chars_read;       /*Num chars read in readlink() */
     int thru_symlink;          /*Did we get to a mount point via a symlink? */
     int error = 0;
+    size_t len;
 
     for (ti = as->parms[0].items; ti; ti = ti->next) {
        /* once per file */
        thru_symlink = 0;
-       sprintf(orig_name, "%s%s", (ti->data[0] == '/') ? "" : "./",
-               ti->data);
+       if( FAILED(StringCbPrintf(orig_name, sizeof(orig_name), "%s%s", (ti->data[0] == '/') ? "" : "./",
+           ti->data))) {
+           fprintf (stderr, "orig_name - cannot be populated");
+            exit(1);
+       }
 
        if (lstat(orig_name, &statbuff) < 0) {
            /* if lstat fails, we should still try the pioctl, since it
@@ -4190,12 +5029,21 @@ FlushMountCmd(struct cmd_syndesc *as, void *arock)
             */
            if (true_name[0] != '/') {
                last_component = (char *)strrchr(orig_name, '/');
-               strcpy(++last_component, true_name);
-               strcpy(true_name, orig_name);
+               if( FAILED(StringCbCopy(++last_component, sizeof(orig_name) - (last_component - orig_name) * sizeof(char), true_name))) {
+                   fprintf (stderr, "last_component - not enough space");
+                    exit(1);
+               }
+               if( FAILED(StringCbCopy(true_name, sizeof(true_name), orig_name))) {
+                   fprintf (stderr, "true_name - not enough space");
+                    exit(1);
+               }
            }
-       } else
-           strcpy(true_name, orig_name);
-
+       } else {
+           if( FAILED(StringCbCopy(true_name, sizeof(true_name), orig_name))) {
+               fprintf (stderr, "true_name - not enough space");
+                exit(1);
+           }
+       }
        /*
         * Find rightmost slash, if any.
         */
@@ -4205,7 +5053,10 @@ FlushMountCmd(struct cmd_syndesc *as, void *arock)
             * Found it.  Designate everything before it as the parent directory,
             * everything after it as the final component.
             */
-           strncpy(parent_dir, true_name, last_component - true_name);
+           if( FAILED(StringCchCopyN(parent_dir, sizeof(parent_dir) / sizeof(char), true_name, last_component - true_name))) {
+               fprintf (stderr, "parent_dir - not enough space");
+                exit(1);
+           }
            parent_dir[last_component - true_name] = 0;
            last_component++;   /*Skip the slash */
        } else {
@@ -4213,7 +5064,10 @@ FlushMountCmd(struct cmd_syndesc *as, void *arock)
             * No slash appears in the given file name.  Set parent_dir to the current
             * directory, and the last component as the given name.
             */
-           strcpy(parent_dir, ".");
+           if( FAILED(StringCbCopy(parent_dir, sizeof(parent_dir), "."))) {
+               fprintf (stderr, "parent_dir - not enough space");
+                exit(1);
+           }
            last_component = true_name;
        }
 
@@ -4229,11 +5083,15 @@ FlushMountCmd(struct cmd_syndesc *as, void *arock)
        }
 
        blob.in = last_component;
-       blob.in_size = strlen(last_component) + 1;
+       if( FAILED(StringCbLength(last_component, sizeof(true_name) - (last_component - true_name), &len))) {
+           fprintf (stderr, "StringCbLength failure on last_component");
+           exit(1);
+       }
+       blob.in_size = len + 1;
        blob.out_size = 0;
-       memset(space, 0, MAXSIZE);
+       memset(space, 0, AFS_PIOCTL_MAXSIZE);
 
-       code = pioctl(parent_dir, VIOC_AFS_FLUSHMOUNT, &blob, 1);
+       code = pioctl_utf8(parent_dir, VIOC_AFS_FLUSHMOUNT, &blob, 1);
 
        if (code != 0) {
            if (errno == EINVAL) {
@@ -4273,7 +5131,7 @@ RxStatProcCmd(struct cmd_syndesc *as, void *arock)
     blob.in_size = sizeof(afs_int32);
     blob.out_size = 0;
 
-    code = pioctl(NULL, VIOC_RXSTAT_PROC, &blob, 1);
+    code = pioctl_utf8(NULL, VIOC_RXSTAT_PROC, &blob, 1);
     if (code != 0) {
        Die(errno, NULL);
        return 1;
@@ -4307,7 +5165,7 @@ RxStatPeerCmd(struct cmd_syndesc *as, void *arock)
     blob.in_size = sizeof(afs_int32);
     blob.out_size = 0;
 
-    code = pioctl(NULL, VIOC_RXSTAT_PEER, &blob, 1);
+    code = pioctl_utf8(NULL, VIOC_RXSTAT_PEER, &blob, 1);
     if (code != 0) {
        Die(errno, NULL);
        return 1;
@@ -4347,7 +5205,10 @@ TestVolStatCmd(struct cmd_syndesc *as, void *arock)
         if (n != 0)
             test.fid.cell = n;
         else {
-            strncpy(test.cellname, tp, sizeof(test.cellname));
+            if( FAILED(StringCbCopy(test.cellname, sizeof(test.cellname), tp))) {
+               fprintf (stderr, "cellname - not enough space");
+                exit(1);
+           }
             test.cellname[sizeof(test.cellname)-1] = '\0';
         }
     }
@@ -4357,7 +5218,10 @@ TestVolStatCmd(struct cmd_syndesc *as, void *arock)
         if (n != 0)
             test.fid.volume = n;
         else {
-            strncpy(test.volname, tp, sizeof(test.volname));
+            if( FAILED(StringCbCopy(test.volname, sizeof(test.volname), tp))) {
+               fprintf (stderr, "volname - not enough space");
+                exit(1);
+           }
             test.volname[sizeof(test.volname)-1] = '\0';
         }
     }
@@ -4387,7 +5251,7 @@ TestVolStatCmd(struct cmd_syndesc *as, void *arock)
     blob.in_size = sizeof(test);
     blob.out_size = 0;
 
-    code = pioctl(NULL, VIOC_VOLSTAT_TEST, &blob, 1);
+    code = pioctl_utf8(NULL, VIOC_VOLSTAT_TEST, &blob, 1);
     if (code != 0) {
        Die(errno, NULL);
        return 1;
@@ -4396,14 +5260,255 @@ TestVolStatCmd(struct cmd_syndesc *as, void *arock)
     return 0;
 }
 
+static int 
+ChOwnCmd(struct cmd_syndesc *as, void *arock)
+{
+    afs_int32 code;
+    struct ViceIoctl blob;
+    struct cmd_item *ti;
+    int error = 0;
+    int literal = 0;
+    struct { 
+        cm_ioctlQueryOptions_t options;
+        afs_uint32 owner;
+    } inData;
+    afs_uint32 ownerId;
+    char * ownerStr;
+    char confDir[257];
+
+    cm_GetConfigDir(confDir, sizeof(confDir));
+
+    if (as->parms[2].items)
+        literal = 1;
+
+    ownerStr = as->parms[0].items->data;
+    ownerId = atoi(ownerStr);
+
+    SetDotDefault(&as->parms[1].items);
+    for(ti=as->parms[1].items; ti; ti=ti->next) {
+        cm_fid_t fid;
+        afs_uint32 filetype;
+       char cell[CELL_MAXNAMELEN];
+
+        /* once per file */
+        memset(&fid, 0, sizeof(fid));
+        memset(&inData, 0, sizeof(inData));
+        filetype = 0;
+        inData.options.size = sizeof(inData.options);
+        inData.options.field_flags |= CM_IOCTL_QOPTS_FIELD_LITERAL;
+        inData.options.literal = literal;
+       blob.in_size = inData.options.size;    /* no variable length data */
+        blob.in = &inData;
+
+        blob.out_size = sizeof(cm_fid_t);
+        blob.out = (char *) &fid;
+        if (0 == pioctl_utf8(ti->data, VIOCGETFID, &blob, 1) &&
+            blob.out_size == sizeof(cm_fid_t)) {
+            inData.options.field_flags |= CM_IOCTL_QOPTS_FIELD_FID;
+            inData.options.fid = fid;
+        } else {
+           Die(errno, ti->data);
+           error = 1;
+           continue;
+        }
+
+        /* 
+         * if the owner was specified as a numeric value,
+         * then we can just use it.  Otherwise, we need 
+         * to know the cell of the path to determine which
+         * ptserver to contact in order to convert the name
+         * to a numeric value.
+         */
+        if (ownerId == 0) {
+            blob.out_size = CELL_MAXNAMELEN;
+            blob.out = cell;
+
+            code = pioctl_utf8(ti->data, VIOC_FILE_CELL_NAME, &blob, 1);
+            if (code) {
+                Die(errno, ti->data);
+                error = 1;
+                continue;
+            }
+            cell[CELL_MAXNAMELEN - 1] = '\0';
+            /* 
+             * We now know the cell for the target and we need to
+             * convert the ownerStr to the Id for this user 
+             */
+            pr_Initialize(1, confDir, cell);
+            code = pr_SNameToId(ownerStr, &inData.owner);
+            pr_End();
+
+            if (code || inData.owner == ANONYMOUSID ) {
+                Die(ECHILD, ti->data);
+                error = 1;
+                continue;
+            }
+        } else {
+            inData.owner = ownerId;
+        }
+
+        blob.in_size = sizeof(inData);
+       blob.out = NULL;
+       blob.out_size = 0;
+       code = pioctl_utf8(ti->data, VIOC_SETOWNER, &blob, 1);
+       if (code) {
+            Die(errno, ti->data);
+        }
+    }
+    return error;
+}
+
+static int 
+ChGrpCmd(struct cmd_syndesc *as, void *arock)
+{
+    afs_int32 code;
+    struct ViceIoctl blob;
+    struct cmd_item *ti;
+    int error = 0;
+    int literal = 0;
+    struct { 
+        cm_ioctlQueryOptions_t options;
+        afs_uint32 group;
+    } inData;
+    afs_uint32 groupId;
+    char * groupStr;
+    char confDir[257];
+
+    cm_GetConfigDir(confDir, sizeof(confDir));
+
+    if (as->parms[2].items)
+        literal = 1;
+
+    groupStr = as->parms[0].items->data;
+    groupId = atoi(groupStr);
+
+    SetDotDefault(&as->parms[1].items);
+    for(ti=as->parms[1].items; ti; ti=ti->next) {
+        cm_fid_t fid;
+        afs_uint32 filetype;
+       char cell[CELL_MAXNAMELEN];
+
+        /* once per file */
+        memset(&fid, 0, sizeof(fid));
+        memset(&inData, 0, sizeof(inData));
+        filetype = 0;
+        inData.options.size = sizeof(inData.options);
+        inData.options.field_flags |= CM_IOCTL_QOPTS_FIELD_LITERAL;
+        inData.options.literal = literal;
+       blob.in_size = inData.options.size;    /* no variable length data */
+        blob.in = &inData;
+
+        blob.out_size = sizeof(cm_fid_t);
+        blob.out = (char *) &fid;
+        if (0 == pioctl_utf8(ti->data, VIOCGETFID, &blob, 1) &&
+            blob.out_size == sizeof(cm_fid_t)) {
+            inData.options.field_flags |= CM_IOCTL_QOPTS_FIELD_FID;
+            inData.options.fid = fid;
+        } else {
+           Die(errno, ti->data);
+           error = 1;
+           continue;
+        }
+
+        /* 
+         * if the group was specified as a numeric value,
+         * then we can just use it.  Otherwise, we need 
+         * to know the cell of the path to determine which
+         * ptserver to contact in order to convert the name
+         * to a numeric value.
+         */
+        if (groupId == 0) {
+            blob.out_size = CELL_MAXNAMELEN;
+            blob.out = cell;
+
+            code = pioctl_utf8(ti->data, VIOC_FILE_CELL_NAME, &blob, 1);
+            if (code) {
+                Die(errno, ti->data);
+                error = 1;
+                continue;
+            }
+            cell[CELL_MAXNAMELEN - 1] = '\0';
+            /* 
+             * We now know the cell for the target and we need to
+             * convert the groupStr to the Id for this user 
+             */
+            pr_Initialize(1, confDir, cell);
+            code = pr_SNameToId(groupStr, &inData.group);
+            pr_End();
+
+            if (code || inData.group == ANONYMOUSID ) {
+                Die(ECHILD, ti->data);
+                error = 1;
+                continue;
+            }
+        } else {
+            inData.group = groupId;
+        }
+
+        blob.in_size = sizeof(inData);
+       blob.out = NULL;
+       blob.out_size = 0;
+       code = pioctl_utf8(ti->data, VIOC_SETGROUP, &blob, 1);
+       if (code) {
+            Die(errno, ti->data);
+        }
+    }
+    return error;
+}
+
 #ifndef WIN32
 #include "AFS_component_version_number.c"
 #endif
 
-main(int argc, char **argv)
+static void
+FreeUtf8CmdLine(int argc, char ** argv)
+{
+    int i;
+    for (i=0; i < argc; i++) {
+        if (argv[i])
+            free(argv[i]);
+    }
+    free(argv);
+}
+
+static char **
+MakeUtf8Cmdline(int argc, const wchar_t **wargv)
+{
+    char ** argv;
+    int i;
+
+    argv = calloc(argc, sizeof(argv[0]));
+    if (argv == NULL)
+        return NULL;
+
+    for (i=0; i < argc; i++) {
+        int s;
+
+        s = WideCharToMultiByte(CP_UTF8, 0, wargv[i], -1, NULL, 0, NULL, FALSE);
+        if (s == 0 ||
+            (argv[i] = calloc(s+1, sizeof(char))) == NULL) {
+            break;
+        }
+
+        s = WideCharToMultiByte(CP_UTF8, 0, wargv[i], -1, argv[i], s+1, NULL, FALSE);
+        if (s == 0) {
+            break;
+        }
+    }
+
+    if (i < argc) {
+        FreeUtf8CmdLine(argc, argv);
+        return NULL;
+    }
+
+    return argv;
+}
+
+int wmain(int argc, wchar_t **wargv)
 {
     afs_int32 code;
     struct cmd_syndesc *ts;
+    char ** argv;
 
 #ifdef AFS_AIX32_ENV
     /*
@@ -4425,6 +5530,8 @@ main(int argc, char **argv)
     WSAStartup(0x0101, &WSAjunk);
 #endif /* WIN32 */
 
+    argv = MakeUtf8Cmdline(argc, wargv);
+
     /* try to find volume location information */
     osi_Init();
 
@@ -4469,6 +5576,7 @@ main(int argc, char **argv)
     parm_listacl_id = ts->nParms;
     cmd_AddParm(ts, "-id", CMD_FLAG, CMD_OPTIONAL, "initial directory acl");
     cmd_AddParm(ts, "-if", CMD_FLAG, CMD_OPTIONAL, "initial file acl");
+    cmd_AddParm(ts, "-cmd", CMD_FLAG, CMD_OPTIONAL, "output as 'fs setacl' command");
     cmd_CreateAlias(ts, "la");
     
     ts = cmd_CreateSyntax("cleanacl", CleanACLCmd, NULL, "clean up access control list");
@@ -4486,6 +5594,7 @@ main(int argc, char **argv)
 
     ts = cmd_CreateSyntax("flush", FlushCmd, NULL, "flush file from cache");
     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
+    cmd_AddParm(ts, "-literal", CMD_FLAG, CMD_OPTIONAL, "literal evaluation of mountpoints and symlinks");
     
 #ifndef WIN32
     ts = cmd_CreateSyntax("flushmount", FlushMountCmd, NULL,
@@ -4508,6 +5617,7 @@ main(int argc, char **argv)
 
     ts = cmd_CreateSyntax("examine", ExamineCmd, NULL, "display file/volume status");
     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
+    cmd_AddParm(ts, "-literal", CMD_FLAG, CMD_OPTIONAL, "literal evaluation of mountpoints and symlinks");
     cmd_CreateAlias(ts, "lv");
     cmd_CreateAlias(ts, "listvol");
     
@@ -4597,9 +5707,11 @@ main(int argc, char **argv)
 
     ts = cmd_CreateSyntax("whichcell", WhichCellCmd, NULL, "list file's cell");
     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
+    cmd_AddParm(ts, "-literal", CMD_FLAG, CMD_OPTIONAL, "literal evaluation of mountpoints and symlinks");
 
     ts = cmd_CreateSyntax("whereis", WhereIsCmd, NULL, "list file's location");
     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
+    cmd_AddParm(ts, "-literal", CMD_FLAG, CMD_OPTIONAL, "literal evaluation of mountpoints and symlinks");
 
     ts = cmd_CreateSyntax("wscell", WSCellCmd, NULL, "list workstation's cell");
     
@@ -4607,11 +5719,12 @@ main(int argc, char **argv)
      ts = cmd_CreateSyntax("primarycell", PrimaryCellCmd, 0, "obsolete (listed primary cell)");
      */
     
+#ifndef AFS_NT40_ENV
     ts = cmd_CreateSyntax("monitor", MonitorCmd, NULL, "set cache monitor host address");
     cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_OPTIONAL, "host name or 'off'");
     cmd_CreateAlias(ts, "mariner");
-    
-   
+#endif
+
     ts = cmd_CreateSyntax("getcellstatus", GetCellCmd, NULL, "get cell status");
     cmd_AddParm(ts, "-cell", CMD_LIST, 0, "cell name");
     
@@ -4628,13 +5741,14 @@ main(int argc, char **argv)
     ts = cmd_CreateSyntax("sysname", SysNameCmd, NULL, "get/set sysname (i.e. @sys) value");
     cmd_AddParm(ts, "-newsys", CMD_LIST, CMD_OPTIONAL, "new sysname");
 
+#ifndef AFS_NT40_ENV
     ts = cmd_CreateSyntax("exportafs", ExportAfsCmd, NULL, "enable/disable translators to AFS");
     cmd_AddParm(ts, "-type", CMD_SINGLE, 0, "exporter name");
     cmd_AddParm(ts, "-start", CMD_SINGLE, CMD_OPTIONAL, "start/stop translator ('on' or 'off')");
     cmd_AddParm(ts, "-convert", CMD_SINGLE, CMD_OPTIONAL, "convert from afs to unix mode ('on or 'off')");
     cmd_AddParm(ts, "-uidcheck", CMD_SINGLE, CMD_OPTIONAL, "run on strict 'uid check' mode ('on' or 'off')");
     cmd_AddParm(ts, "-submounts", CMD_SINGLE, CMD_OPTIONAL, "allow nfs mounts to subdirs of /afs/.. ('on' or 'off')");
-
+#endif
 
     ts = cmd_CreateSyntax("storebehind", StoreBehindCmd, NULL, 
                          "store to server after file close");
@@ -4695,11 +5809,31 @@ main(int argc, char **argv)
     cmd_AddParm(ts, "-volume",  CMD_SINGLE, CMD_OPTIONAL, "volume name or number");
     cmd_AddParm(ts, "-state",   CMD_SINGLE, CMD_OPTIONAL, "new volume state: online, busy, offline, down");
 
+    ts = cmd_CreateSyntax("smbunicode", SmbUnicodeCmd, NULL, "enable or disable Unicode on new SMB connections");
+    cmd_AddParm(ts, "-on", CMD_FLAG, CMD_OPTIONAL, "enable Unicode on new connections");
+    cmd_AddParm(ts, "-off", CMD_FLAG, CMD_OPTIONAL, "disable Unicode on new connections");
+
+    ts = cmd_CreateSyntax("getfid", GetFidCmd, NULL, "get file id for object(s) in afs");
+    cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
+    cmd_AddParm(ts, "-literal", CMD_FLAG, CMD_OPTIONAL, "literal evaluation of mountpoints and symlinks");
+
+    ts = cmd_CreateSyntax("chown", ChOwnCmd, NULL, "set owner for object(s) in afs");
+    cmd_AddParm(ts, "-owner", CMD_SINGLE, 0, "user name or id");
+    cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
+    cmd_AddParm(ts, "-literal", CMD_FLAG, CMD_OPTIONAL, "literal evaluation of mountpoints and symlinks");
+
+    ts = cmd_CreateSyntax("chgrp", ChGrpCmd, NULL, "set owner for object(s) in afs");
+    cmd_AddParm(ts, "-group", CMD_SINGLE, 0, "user/group name or id");
+    cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
+    cmd_AddParm(ts, "-literal", CMD_FLAG, CMD_OPTIONAL, "literal evaluation of mountpoints and symlinks");
+
     code = cmd_Dispatch(argc, argv);
 
     if (rxInitDone) 
         rx_Finalize();
     
+    FreeUtf8CmdLine(argc, argv);
+    
     return code;
 }
 
@@ -4763,6 +5897,12 @@ Die(int code, char *filename)
        else 
             fprintf(stderr,"%s: All servers are down\n", pn);
     } 
+    else if (code == ECHILD) {  /* hack */
+       if (filename) 
+            fprintf(stderr,"%s: Invalid owner specified for '%s'\n", pn, filename);
+       else 
+            fprintf(stderr,"%s: Invalid owner specified\n", pn);
+    } 
     else {
        if (filename) 
             fprintf(stderr,"%s:'%s'", pn, filename);