Windows: Update fs newcell and add VIOCNEWCELL2
[openafs.git] / src / WINNT / afsd / fs.c
index 6cf53e5..c4a7eda 100644 (file)
@@ -15,6 +15,7 @@
 #include <stdlib.h>
 #include <malloc.h>
 #include <string.h>
+#include <strsafe.h>
 #include <stdio.h>
 #include <time.h>
 #include <winsock2.h>
@@ -43,7 +44,7 @@
 #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;
@@ -271,7 +272,7 @@ 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_utf8(apath, VIOC_FILE_CELL_NAME, &blob, 1);
@@ -286,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;
 }
@@ -303,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;
 
@@ -338,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;
@@ -435,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;
 }
 
@@ -465,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;
@@ -541,32 +553,52 @@ EmptyAcl(char *astr)
     tp->nplus = tp->nminus = 0;
     tp->pluslist = tp->minuslist = 0;
     tp->dfs = 0;
+#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, ret;
+    size_t len;
     char tname[MAXNAME];
     struct AclEntry *first, *next, *last, *tl;
     struct Acl *ta;
 
     ta = EmptyAcl(NULL);
-    if (astr == NULL || strlen(astr) == 0)
+    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);
+#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;
@@ -579,7 +611,11 @@ ParseAcl (char *astr)
     last = 0;
     first = 0;
     for(i=0;i<nplus;i++) {
+#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);
@@ -588,7 +624,10 @@ ParseAcl (char *astr)
             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) 
@@ -600,7 +639,11 @@ ParseAcl (char *astr)
     last = 0;
     first = 0;
     for(i=0;i<nminus;i++) {
+#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);
@@ -609,7 +652,10 @@ ParseAcl (char *astr)
             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) 
@@ -712,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;
 }
@@ -751,9 +816,10 @@ 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;
@@ -767,7 +833,10 @@ static const char * NetbiosName(void)
                               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;
 }
@@ -801,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. */
@@ -922,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)
@@ -931,7 +1007,12 @@ 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;
+        if ( IsFreelanceRoot(ti->data) ) {
+            fprintf(stderr,"%s: ACLs cannot be set on the Freelance root.afs volume.\n", pn);
+            error = 1;
+            continue;
+        }
+       blob.out_size = AFS_PIOCTL_MAXSIZE;
        blob.in_size = idf;
        blob.in = blob.out = space;
        code = pioctl_utf8(ti->data, VIOCGETAL, &blob, 1);
@@ -942,7 +1023,7 @@ 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",
@@ -963,7 +1044,7 @@ 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",
@@ -996,7 +1077,11 @@ SetACLCmd(struct cmd_syndesc *as, void *arock)
        }
        blob.in = AclToString(ta);
        blob.out_size=0;
-       blob.in_size = 1+(long)strlen(blob.in);
+       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) {
@@ -1066,12 +1151,13 @@ 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_utf8(as->parms[0].items->data, VIOCGETAL, &blob, 1);
@@ -1079,7 +1165,7 @@ CopyACLCmd(struct cmd_syndesc *as, void *arock)
        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",
@@ -1088,7 +1174,7 @@ CopyACLCmd(struct cmd_syndesc *as, void *arock)
     }
     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_utf8(ti->data, VIOCGETAL, &blob, 1);
@@ -1102,7 +1188,7 @@ 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",
@@ -1126,7 +1212,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);
@@ -1134,7 +1223,11 @@ 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);
+       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) {
@@ -1259,10 +1352,11 @@ 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_utf8(ti->data, VIOCGETAL, &blob, 1);
@@ -1273,7 +1367,7 @@ 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",
@@ -1294,7 +1388,11 @@ 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_utf8(ti->data, VIOCSETAL, &blob, 1);
            if (code) {
@@ -1356,7 +1454,14 @@ 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;
+
+        if ( IsFreelanceRoot(ti->data) ) {
+            fprintf(stderr,"%s: ACLs are not set on the Freelance root.afs volume.\n", pn);
+            error = 1;
+            continue;
+        }
+
+        blob.out_size = AFS_PIOCTL_MAXSIZE;
        blob.in_size = idf;
        blob.in = blob.out = space;
        code = pioctl_utf8(ti->data, VIOCGETAL, &blob, 1);
@@ -1365,7 +1470,7 @@ ListACLCmd(struct cmd_syndesc *as, void *arock)
             error = 1;
            continue;
        }
-       ta = ParseAcl(space);
+       ta = ParseAcl(space, AFS_PIOCTL_MAXSIZE);
         if (!ta) {
             fprintf(stderr,
                     "fs: %s: invalid acl data returned from VIOCGETAL\n",
@@ -1514,9 +1619,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);
 }
 
@@ -1526,13 +1639,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;
@@ -1553,29 +1669,42 @@ 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';
+            destRemaining -= sizeof(char);
+        }
        code = pioctl_utf8(ti->data,VIOCSETVOLSTAT, &blob, 1);
        if (code) {
            Die(errno, ti->data);
@@ -1622,6 +1751,7 @@ ExamineCmd(struct cmd_syndesc *as, void *arock)
     int error = 0;
     int literal = 0;
     cm_ioctlQueryOptions_t options;
+    size_t len;
 
     if (as->parms[1].items)
         literal = 1;
@@ -1630,7 +1760,7 @@ ExamineCmd(struct cmd_syndesc *as, void *arock)
     for(ti=as->parms[0].items; ti; ti=ti->next) {
         cm_fid_t fid;
         afs_uint32 filetype;
-       afs_uint32 owner[2];
+       afs_int32 owner[2];
        char cell[CELL_MAXNAMELEN];
 
         /* once per file */
@@ -1690,18 +1820,26 @@ ExamineCmd(struct cmd_syndesc *as, void *arock)
             pr_Initialize(1, confDir, cell);
            pr_SIdToName(owner[0], oname);
            pr_SIdToName(owner[1], gname);
-           printf("Owner %s (%u) Group %s (%u)\n", oname, owner[0], gname, owner[1]);
+           printf("Owner %s (%d) Group %s (%d)\n", oname, owner[0], gname, owner[1]);
         }
 
        blob.out = space;
-       blob.out_size = MAXSIZE;
+       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);
@@ -1747,7 +1885,7 @@ ListQuotaCmd(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_utf8(ti->data, VIOCGETVOLSTAT, &blob, 1);
@@ -1816,7 +1954,7 @@ WhereIsCmd(struct cmd_syndesc *as, void *arock)
             error = 1;
            continue;
         }
-        blob.out_size = MAXSIZE;
+        blob.out_size = AFS_PIOCTL_MAXSIZE;
        blob.out = space;
        memset(space, 0, sizeof(space));
        code = pioctl_utf8(ti->data, VIOCWHEREIS, &blob, 1);
@@ -1830,7 +1968,7 @@ WhereIsCmd(struct cmd_syndesc *as, void *arock)
                 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]);
@@ -1857,7 +1995,7 @@ 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_utf8(ti->data, VIOCGETVOLSTAT, &blob, 1);
@@ -1887,16 +2025,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_utf8(ti->data, VIOCGETVOLSTAT, &blob, 1);
-       if (code || blob.out_size != sizeof(*status)) {
+        /*
+         * 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 
@@ -1916,6 +2058,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 */
@@ -1929,11 +2073,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
@@ -1974,14 +2125,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 */
 
        /*
@@ -1997,20 +2161,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
@@ -2020,7 +2192,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));
        }
@@ -2033,10 +2208,14 @@ 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_utf8(parent_dir, VIOC_AFS_STAT_MT_PT, &blob, 1);
 
@@ -2072,6 +2251,7 @@ MakeMountCmd(struct cmd_syndesc *as, void *arock)
     struct vldbentry vldbEntry;
     struct ViceIoctl blob;
     char * parent;
+    size_t len;
 
     memset(&info, 0, sizeof(info));
 
@@ -2080,9 +2260,8 @@ MakeMountCmd(struct cmd_syndesc *as, void *arock)
     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;
     }
 
@@ -2104,14 +2283,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);
@@ -2125,12 +2309,19 @@ 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);
+           fprintf(stderr,"%s: Only AFS Client Administrators may alter the Freelance root.afs volume\n", pn);
            return 1;
        }
 
@@ -2172,23 +2363,46 @@ 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_utf8(path, VIOC_AFS_CREATE_MT_PT, &blob, 0);
@@ -2221,39 +2435,55 @@ 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_utf8(tbuffer, VIOC_AFS_STAT_MT_PT, &blob, 0);
@@ -2268,14 +2498,18 @@ RemoveMountCmd(struct cmd_syndesc *as, void *arock) {
        }
 
         if ( IsFreelanceRoot(tbuffer) && !IsAdmin() ) {
-            fprintf(stderr,"%s: Only AFS Client Administrators may alter the root.afs volume\n", pn);
+            fprintf(stderr,"%s: Only AFS Client Administrators may alter the Freelance root.afs volume\n", pn);
             error = 1;
             continue;   /* skip */
         }
 
         blob.out_size = 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;
        code = pioctl_utf8(tbuffer, VIOC_AFS_DELETE_MT_PT, &blob, 0);
        if (code) {
            Die(errno, ti->data);
@@ -2298,13 +2532,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 */
 
@@ -2324,12 +2560,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;
     }
 
@@ -2371,7 +2617,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", 
@@ -2384,8 +2639,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);
@@ -2408,7 +2672,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 */
 
@@ -2533,20 +2797,29 @@ 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;
@@ -2558,16 +2831,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*sizeof(afs_int32) < MAXSIZE; 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;
 
@@ -2575,8 +2863,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);
@@ -2593,11 +2884,21 @@ 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;
@@ -2611,7 +2912,11 @@ ListAliasesCmd(struct cmd_syndesc *as, void *arock)
         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);
     }
@@ -2636,8 +2941,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 */
@@ -2661,97 +2974,182 @@ CallBackRxConnCmd(struct cmd_syndesc *as, void *arock)
 static int
 NewCellCmd(struct cmd_syndesc *as, void *arock)
 {
-#ifndef WIN32
-    afs_int32 code, linkedstate=0, size=0, *lp;
+    afs_uint32 code, linkedstate=0, size=0, count=0, *lp;
+    afs_uint32 usedns=0, useregistry=0;
     struct ViceIoctl blob;
     struct cmd_item *ti;
-    char *tp, *cellname=0;
-    struct hostent *thp;
-    afs_int32 fsport = 0, vlport = 0;
+    char *tp, *cellname=0, *linked_cellname=0;
+    afs_uint32 fsport = 0, vlport = 0;
+    size_t destRemaining;
 
-    memset(space, 0, MAXHOSTS * sizeof(afs_int32));
-    tp = space;
-    lp = (afs_int32 *)tp;
-    *lp++ = 0x12345678;
-    tp += sizeof(afs_int32);
-    for(ti=as->parms[1].items; ti; ti=ti->next) {
-       thp = hostutil_GetHostByName(ti->data);
-       if (!thp) {
-           fprintf(stderr,"%s: Host %s not found in host table, skipping it.\n",
-                  pn, ti->data);
-       }
-       else {
-           memcpy(tp, thp->h_addr, sizeof(afs_int32));
-           tp += sizeof(afs_int32);
-       }
+    if ( !IsAdmin() ) {
+        fprintf (stderr,"Permission denied: requires AFS Client Administrator access.\n");
+        return EACCES;
     }
+
+    /* if there is no cell specified, use old Windows behavior */
+    if (as->parms[0].items == NULL) {
+        blob.in_size = 0;
+        blob.in = (char *) 0;
+        blob.out_size = AFS_PIOCTL_MAXSIZE;
+        blob.out = space;
+
+        code = pioctl_utf8((char *) 0, VIOCNEWCELL, &blob, 1);
+
+        if (code) {
+            Die(errno, (char *) 0);
+            return 1;
+        }
+
+        printf("Cell servers information refreshed\n");
+        return 0;
+    } else {
+        cellname = as->parms[0].items->data;
+    }
+
     if (as->parms[2].items) {
        /*
         * Link the cell, for the purposes of volume location, to the specified
         * cell.
         */
-       cellname = as->parms[2].items->data;
+       linked_cellname = as->parms[2].items->data;
        linkedstate = 1;
     }
-#ifdef FS_ENABLE_SERVER_DEBUG_PORTS
+
     if (as->parms[3].items) {
-       code = util_GetInt32(as->parms[3].items->data, &vlport);
+       code = util_GetInt32(as->parms[2].items->data, &vlport);
        if (code) {
            fprintf(stderr,"fs: bad integer specified for the fileserver port.\n");
            return code;
        }
     }
     if (as->parms[4].items) {
-       code = util_GetInt32(as->parms[4].items->data, &fsport);
+       code = util_GetInt32(as->parms[3].items->data, &fsport);
        if (code) {
            fprintf(stderr,"fs: bad integer specified for the vldb server port.\n");
            return code;
        }
     }
-#endif
-    tp = (char *)(space + (MAXHOSTS+1) *sizeof(afs_int32));
-    lp = (afs_int32 *)tp;    
+
+    if (as->parms[5].items) {
+        useregistry = 1;
+    }
+
+    if (as->parms[6].items) {
+        usedns = 1;
+    }
+
+    /* Count the number of hostnames */
+    for (ti=as->parms[1].items; ti && count < AFS_MAXHOSTS; ti=ti->next, count++);
+
+    if (!usedns && count == 0) {
+        fprintf( stderr, "fs: at least one vldb server must be specified.");
+        exit(1);
+    }
+
+    if (count > AFS_MAXHOSTS) {
+        fprintf( stderr, "fs: at most %u servers may be specified.", AFS_MAXHOSTS);
+        exit(1);
+    }
+
+    /*
+     * The pioctl data buffer consists of the following structure:
+     *
+     *  afs_uint32 flags
+     *  afs_uint32 alternative fs port
+     *  afs_uint32 alternative vl port
+     *  afs_uint32 count of vldb servers
+     *  char[]     cellname
+     *  char[]     linkedcell
+     *  n * char[] hostnames
+     */
+
+    memset(space, 0, sizeof(space));
+    tp = space;
+    lp = (afs_uint32 *)tp;
+
+    /* flags */
+    if (usedns)
+        *lp |= VIOC_NEWCELL2_FLAG_USEDNS;
+
+    if (useregistry)
+        *lp |= VIOC_NEWCELL2_FLAG_USEREG;
+
+    if (linkedstate)
+        *lp |= VIOC_NEWCELL2_FLAG_LINKED;
+    lp++;
+
+    /* alt fs port */
     *lp++ = fsport;
+
+    /* alt vl port */
     *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 (linkedstate) {
-       strcpy(tp, cellname);
-       size += strlen(cellname) + 1;
-    }
-    blob.in_size = size;
-    blob.in = space;
-    blob.out_size = 0;
-    code = pioctl_utf8(0, VIOCNEWCELL, &blob, 1);
-    if (code < 0)
-       Die(errno, 0);
-    return 0;
-#else /* WIN32 */
-    afs_int32 code;
-    struct ViceIoctl blob;
-    
-    if ( !IsAdmin() ) {
-        fprintf (stderr,"Permission denied: requires AFS Client Administrator access.\n");
-        return EACCES;
+
+    /* count of server names */
+    *lp++ = count;
+
+    /* Switch back to char pointer */
+    tp = (char *)lp;
+
+    /* Add nul-terminated cellname */
+    destRemaining = sizeof(space) - (tp - space);
+    if( FAILED(StringCbCopyEx( tp,
+                               destRemaining,
+                               as->parms[0].items->data,
+                               &tp,
+                               &destRemaining,
+                               STRSAFE_FILL_ON_FAILURE))) {
+        fprintf (stderr, " not enough space for cellname");
+        exit(1);
+    }
+    /* Move beyond the terminating nul */
+    tp++;
+    destRemaining -= sizeof(char);
+
+    /* Add nul-terminated linkname */
+    if( FAILED(StringCbCopyEx( tp,
+                               destRemaining,
+                               linkedstate ? linked_cellname : "",
+                               &tp,
+                               &destRemaining,
+                               STRSAFE_FILL_ON_FAILURE))) {
+        fprintf (stderr, " not enough space for linked cellname");
+        exit(1);
+    }
+    /* Move beyond the terminating nul */
+    tp++;
+    destRemaining -= sizeof(char);
+
+    /* Add the servers */
+    for (ti=as->parms[1].items; ti; ti=ti->next) {
+        if( FAILED(StringCbCopyEx( tp,
+                                   destRemaining,
+                                   ti->data,
+                                   &tp,
+                                   &destRemaining,
+                                   STRSAFE_FILL_ON_FAILURE))) {
+            fprintf (stderr, " not enough space for server %s", ti->data);
+            exit(1);
+        }
+        /* Move beyond the terminating nul */
+        tp++;
+        destRemaining -= sizeof(char);
     }
 
-    blob.in_size = 0;
-    blob.in = (char *) 0;
-    blob.out_size = MAXSIZE;
+    blob.in_size = (tp - space);
+    blob.in = space;
+    blob.out_size = 0;
     blob.out = space;
-
-    code = pioctl_utf8((char *) 0, VIOCNEWCELL, &blob, 1);
+    code = pioctl_utf8(NULL, VIOCNEWCELL2, &blob, 1);
 
     if (code) {
-        Die(errno, (char *) 0);
+        Die(errno, as->parms[0].items->data);
         return 1;
     }
     
-    printf("Cell servers information refreshed\n");
+    printf("Cell servers information for %s added or updated.\n",
+           as->parms[0].items->data);
     return 0;
-#endif /* WIN32 */
 }
 
 #ifndef WIN32
@@ -2762,15 +3160,24 @@ 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;
@@ -2869,7 +3276,7 @@ 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_utf8(NULL, VIOC_GET_WS_CELL, &blob, 1);
@@ -2878,7 +3285,7 @@ WSCellCmd(struct cmd_syndesc *as, void *arock)
        Die(errno, NULL);
         return 1;
     }
-    space[MAXSIZE - 1] = '\0';
+    space[AFS_PIOCTL_MAXSIZE - 1] = '\0';
     printf("This workstation belongs to cell '%s'\n", space);
     return 0;
 }
@@ -2903,6 +3310,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;
@@ -2921,8 +3329,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 */
@@ -2962,6 +3378,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) {
@@ -2980,23 +3399,40 @@ 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));
+#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);
@@ -3008,7 +3444,15 @@ 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");
@@ -3018,7 +3462,11 @@ SysNameCmd(struct cmd_syndesc *as, void *arock)
     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;
@@ -3140,6 +3588,7 @@ 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 */
@@ -3154,7 +3603,11 @@ GetCellCmd(struct cmd_syndesc *as, void *arock)
        }
         if (info.linkedCell)
             free(info.linkedCell);
-       blob.in_size = 1+(long)strlen(info.name);
+        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_utf8(0, VIOC_GETCELLSTATUS, &blob, 1);
        if (code) {
@@ -3231,7 +3684,10 @@ static int SetCellCmd(struct cmd_syndesc *as, void *arock)
        }
         if (info.linkedCell)
             free(info.linkedCell);
-       strcpy(args.cname, info.name);
+       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;
@@ -3248,7 +3704,10 @@ static int SetCellCmd(struct cmd_syndesc *as, void *arock)
 static int
 GetCellName(char *cellNamep, struct afsconf_cell *infop)
 {
-    strcpy(infop->name, cellNamep);
+    if( FAILED(StringCbCopy(infop->name, sizeof(infop->name), cellNamep))) {
+        fprintf (stderr, "name - not enough space");
+        exit(1);
+    }
     return 0;
 }
 
@@ -3333,6 +3792,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
@@ -3357,7 +3817,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++;
@@ -3381,6 +3850,7 @@ addServer(char *name, afs_int32 rank)
     struct hostent *thostent;
     afs_uint32 addr;
     int error = 0;
+    errno_t err;
 
 #ifndef MAXUSHORT
 #ifdef MAXSHORT
@@ -3407,8 +3877,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++;
@@ -3464,7 +3944,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");
@@ -3479,18 +3959,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 */
@@ -3556,7 +4049,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()) {
@@ -3572,21 +4065,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 */
@@ -3682,9 +4192,9 @@ 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_utf8(0, VIOC_GETSPREFS, &blob, 1);
@@ -3701,8 +4211,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);      
@@ -3753,10 +4266,10 @@ 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_utf8(0, VIOC_GETSPREFS, &blob, 1);
@@ -3772,8 +4285,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);
@@ -4035,7 +4551,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;
@@ -4046,7 +4566,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;
@@ -4155,6 +4679,7 @@ 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;
@@ -4166,8 +4691,17 @@ GetCryptCmd(struct cmd_syndesc *as, void *arock)
     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 == 2)
           printf("auth (data integrity).\n");
@@ -4271,6 +4805,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");
@@ -4311,17 +4846,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"); 
@@ -4396,10 +4939,10 @@ 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_utf8(0, VIOC_GETCPREFS, &blob, 1);
        if (code) {
@@ -4414,8 +4957,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;
@@ -4442,7 +4988,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");
@@ -4513,12 +5059,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
@@ -4558,12 +5108,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.
         */
@@ -4573,7 +5132,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 {
@@ -4581,7 +5143,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;
        }
 
@@ -4597,9 +5162,13 @@ 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_utf8(parent_dir, VIOC_AFS_FLUSHMOUNT, &blob, 1);
 
@@ -4715,7 +5284,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';
         }
     }
@@ -4725,7 +5297,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';
         }
     }
@@ -5187,12 +5762,10 @@ int wmain(int argc, wchar_t **wargv)
     cmd_CreateAlias(ts, "sq");
 
     ts = cmd_CreateSyntax("newcell", NewCellCmd, NULL, "configure new cell");
-#ifndef WIN32
-    cmd_AddParm(ts, "-name", CMD_SINGLE, 0, "cell name");
-    cmd_AddParm(ts, "-servers", CMD_LIST, CMD_REQUIRED, "primary servers");
+    cmd_AddParm(ts, "-name", CMD_SINGLE, CMD_OPTIONAL, "cell name");
+    cmd_AddParm(ts, "-servers", CMD_LIST, CMD_OPTIONAL, "primary servers");
     cmd_AddParm(ts, "-linkedcell", CMD_SINGLE, CMD_OPTIONAL, "linked cell name");
 
-#ifdef FS_ENABLE_SERVER_DEBUG_PORTS
     /*
      * Turn this on only if you wish to be able to talk to a server which is listening
      * on alternative ports. This is not intended for general use and may not be
@@ -5201,8 +5774,10 @@ int wmain(int argc, wchar_t **wargv)
      */
     cmd_AddParm(ts, "-fsport", CMD_SINGLE, CMD_OPTIONAL, "cell's fileserver port");
     cmd_AddParm(ts, "-vlport", CMD_SINGLE, CMD_OPTIONAL, "cell's vldb server port");
-#endif
+    cmd_AddParm(ts, "-registry", CMD_FLAG, CMD_OPTIONAL, "add cell info to registry cellservdb");
+    cmd_AddParm(ts, "-dns",    CMD_FLAG, CMD_OPTIONAL,   "force use of dns");
 
+#ifndef WIN32
     ts = cmd_CreateSyntax("newalias", NewAliasCmd, NULL,
                          "configure new cell alias");
     cmd_AddParm(ts, "-alias", CMD_SINGLE, 0, "alias name");