vlserver: Use correct literal in bounds check
[openafs.git] / src / vlserver / cnvldb.c
index f03f68b..f31d31c 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Copyright 2000, International Business Machines Corporation and others.
  * All Rights Reserved.
- * 
+ *
  * This software has been released under the terms of the IBM Public
  * License.  For details, see the LICENSE file in the top-level source
  * directory or online at http://www.openafs.org/dl/license10.html
@@ -9,51 +9,49 @@
 
 #include <afsconfig.h>
 #include <afs/param.h>
+#include <afs/stds.h>
 
-RCSID
-    ("$Header$");
+#include <roken.h>
 
-#include <afs/stds.h>
-#include <sys/types.h>
-#include <errno.h>
-#include <stdio.h>
 #include <sys/file.h>
-#include <string.h>
 
-#include "cnvldb.h"            /* CHANGEME! */
-#include <netinet/in.h>
 #include <afs/venus.h>
 #include <afs/cmd.h>
 #include <afs/afsutil.h>
 #include <afs/fileutil.h>
 
 #include "vlserver.h"
+#include "cnvldb.h"            /* CHANGEME! */
 
-#define MAXSIZE 2048           /* most I'll get back from PIOCTL */
 #define        BADSERVERID     255     /* XXX */
 
 
 static char pn[] = "cnvldb";
 static char tempname[] = "XXnewvldb";
-static char space[MAXSIZE];
 static int MaxServers[3] = { 30, 254, 254 };   /* max server # permitted in this version */
 
-static afs_int32 Conv4to3();
+static afs_int32 Conv4to3(afs_uint32 addr);
 
-static int convert_vlentry();
-static int rewrite_header();
+static void convert_vlentry(int, int, int, struct vlheader_1 *,
+                           struct vlheader_1 *, struct vlentry_1 *);
+static void rewrite_header(int, int, void *);
+static void readheader(int fd, int version, void *addr);
+static int readentry(int fd, int version, void *addr);
+static void printentry(int version, void *addr);
 
 static char tspace[1024];      /* chdir can't handle anything bigger, anyway */
 
 void read_mhentries(afs_uint32 mh_addr, int oldfd);
 void convert_mhentries(int oldfd, int newfd, struct vlheader_2 *header, int fromver, int tover);
 
+static int convert_header(int ofd, int fd, int fromv, int tov, void *fromaddr,
+                         void *toaddr);
+
 /* return a static pointer to a buffer */
 static char *
-Parent(apath)
-     char *apath;
+Parent(const char *apath)
 {
-    register char *tp;
+    char *tp;
     strcpy(tspace, apath);
     tp = strrchr(tspace, '/');
     if (tp) {
@@ -67,22 +65,19 @@ int oldpos = 0;
 int fromvers = 0, tovers = 0, showversion = 0;
 afs_uint32 mhaddr;
 afs_int32 dbsize;
-char *pathname = NULL;
+const char *pathname = NULL;
 const char *dbPath;
 
 static int
 handleit(struct cmd_syndesc *as, void *arock)
 {
-    register struct cmd_item *ti;
-    register afs_int32 code;
     int w, old, new, rc, dump = 0, fromv = 0;
-    short uvers;
     char ubik[80];             /* space for some ubik header */
     union {
        struct vlheader_1 header1;
        struct vlheader_2 header2;
        struct vlheader_3 header3;
-    } oldheader, oldheader1, newheader;        /* large enough for either */
+    } oldheader, newheader;    /* large enough for either */
 
     union {
        struct vlentry_1 entry1;
@@ -110,7 +105,7 @@ handleit(struct cmd_syndesc *as, void *arock)
     read(old, &fromv, sizeof(int));
     fromv = ntohl(fromv);
     if ((fromv < 1) || (fromv > 4)) {
-       fprintf(stderr, pn);
+       fprintf(stderr, "%s", pn);
        fprintf(stderr, ": Unrecognized VLDB version %d.\n", fromv);
        exit(-1);
     }
@@ -167,9 +162,9 @@ handleit(struct cmd_syndesc *as, void *arock)
     }
 
     if ((fromvers < 1) || (fromvers > 4)) {
-       fprintf(stderr, pn);
+       fprintf(stderr, "%s", pn);
        fprintf(stderr, ": VLDB version %d is not supported.\n", fromvers);
-       fprintf(stderr, pn);
+       fprintf(stderr, "%s", pn);
        fprintf(stderr, ": Only versions 1-4 are currently supported.\n");
        exit(-1);
     }
@@ -178,15 +173,15 @@ handleit(struct cmd_syndesc *as, void *arock)
        tovers = fromvers + 1;
 
     if (tovers < 1 || tovers > 4) {
-       fprintf(stderr, pn);
+       fprintf(stderr, "%s", pn);
        fprintf(stderr, ": VLDB version %d is not supported.\n", tovers);
-       fprintf(stderr, pn);
+       fprintf(stderr, "%s", pn);
        fprintf(stderr, ": Only versions 1 - 4 are currently supported.\n");
        exit(-1);
     }
 
     if (mhaddr && (tovers < 3)) {
-       fprintf(stderr, pn);
+       fprintf(stderr, "%s", pn);
        fprintf(stderr, ": Cannot convert. VLDB contains multihome info.\n");
        exit(-1);
     }
@@ -211,7 +206,7 @@ handleit(struct cmd_syndesc *as, void *arock)
        exit(1);
     }
 
-    /* Because we know that all the vldb entries are the same size and type we 
+    /* Because we know that all the vldb entries are the same size and type we
      * can just read them sequentially, fiddle with the fields, and write
      * them out again.  If we invent a vldb format that has different
      * types of entries, then we're going to have to invent new logic for
@@ -224,14 +219,17 @@ handleit(struct cmd_syndesc *as, void *arock)
        rc = readentry(old, fromvers, &xvlentry);
        if ((rc == 0) || (rc == EOF))
            break;
-       convert_vlentry(new, fromvers, tovers, &oldheader, &newheader,
-                       &xvlentry);
+       convert_vlentry(new, fromvers, tovers,
+                       (struct vlheader_1 *)&oldheader,
+                       (struct vlheader_1 *)&newheader,
+                       (struct vlentry_1 *)&xvlentry);
     }
 
     /* We have now finished sequentially reading and writing the database.
      * Now randomly offset into database and update multihome entries.
      */
-    convert_mhentries(old, new, &newheader, fromvers, tovers);
+    convert_mhentries(old, new, (struct vlheader_2 *)&newheader,
+                     fromvers, tovers);
     rewrite_header(new, tovers, &newheader);
 
     close(old);
@@ -241,16 +239,14 @@ handleit(struct cmd_syndesc *as, void *arock)
     }
     close(new);
 
-    renamefile(tempname, pathname);
+    rk_rename(tempname, pathname);
     sleep(5);
     exit(0);
 }
 
 
-readheader(fd, version, addr)
-     int fd;
-     int version;
-     char *addr;
+static void
+readheader(int fd, int version, void *addr)
 {
     int hdrsize, size = 0;
 
@@ -267,14 +263,13 @@ readheader(fd, version, addr)
     return;
 }
 
-readentry(fd, version, addr)
-     int fd;
-     int version;
-     char *addr;
+static int
+readentry(int fd, int version, void *addr)
 {
     int rc, rc1;
     struct vlentry_3 *vl3p = (struct vlentry_3 *)addr;
     int toread;
+    char *caddr = (char *)addr;
 
     toread =
        ((version ==
@@ -290,7 +285,7 @@ readentry(fd, version, addr)
        if (!mhaddr)            /* Remember first mh block */
            mhaddr = oldpos - rc;
 
-       rc1 = read(fd, &addr[rc], VL_ADDREXTBLK_SIZE - rc);
+       rc1 = read(fd, &caddr[rc], VL_ADDREXTBLK_SIZE - rc);
        if (rc1 != VL_ADDREXTBLK_SIZE - rc)
            printf("Partial read of mhblock at pos %u: %d\n", oldpos + rc,
                   rc1);
@@ -303,9 +298,8 @@ readentry(fd, version, addr)
     return rc;
 }
 
-printentry(version, addr)
-     int version;
-     char *addr;
+static void
+printentry(int version, void *addr)
 {
     struct vlentry_2 *vl2p = (struct vlentry_2 *)addr;
     struct vlentry_3 *vl3p = (struct vlentry_3 *)addr;
@@ -320,7 +314,7 @@ printentry(version, addr)
        printf("%s\t%5d [%10d:%10d:%10d]%8X%8d\n", vl2p->name, vl2p->spares3,
               vl2p->volumeId[0], vl2p->volumeId[1], vl2p->volumeId[2],
               vl2p->flags, vl2p->LockAfsId);
-       printf("\t%8d%8d%8d [%7d%7d%7d]%7d% [%4d%4d%4d%4d][%4d%4d%4d%4d]\n",
+       printf("\t%8d%8d%8d [%7d%7d%7d]%7d [%4d%4d%4d%4d][%4d%4d%4d%4d]\n",
               vl2p->LockTimestamp, vl2p->cloneId, vl2p->spares0,
               vl2p->nextIdHash[0], vl2p->nextIdHash[1], vl2p->nextIdHash[2],
               vl2p->nextNameHash, vl2p->serverNumber[0],
@@ -335,7 +329,7 @@ printentry(version, addr)
 
        if (vl3p->flags == VLFREE)
            return;
-       printf("%s\tPos=%lu NextIdHash=[%d:%d:%d] NextNameHash=%d\n",
+       printf("%s\tPos=%" AFS_SIZET_FMT " NextIdHash=[%d:%d:%d] NextNameHash=%d\n",
               vl3p->name, (oldpos - sizeof(struct vlentry_3)),
               vl3p->nextIdHash[0], vl3p->nextIdHash[1], vl3p->nextIdHash[2],
               vl3p->nextNameHash);
@@ -362,9 +356,7 @@ struct extentaddr *base[VL_MAX_ADDREXTBLKS];
  * If it's not good, then don't read the block in.
  */
 void
-read_mhentries(mh_addr, oldfd)
-     int oldfd;
-     afs_uint32 mh_addr;
+read_mhentries(afs_uint32 mh_addr, int oldfd)
 {
     afs_uint32 sit, a;
     afs_int32 code;
@@ -381,7 +373,7 @@ read_mhentries(mh_addr, oldfd)
     if (!mh_addr)
        return;
 
-    /* Check if the first extent block is beyond eof. If 
+    /* Check if the first extent block is beyond eof. If
      * it is, it's not real.
      */
     if (mh_addr > dbsize - VL_ADDREXTBLK_SIZE)
@@ -393,7 +385,7 @@ read_mhentries(mh_addr, oldfd)
        perror("seek MH block");
        exit(1);
     }
-    base[0] = (struct extentaddr *)malloc(VL_ADDREXTBLK_SIZE);
+    base[0] = malloc(VL_ADDREXTBLK_SIZE);
     if (!base[0]) {
        perror("malloc1");
        exit(1);
@@ -407,7 +399,7 @@ read_mhentries(mh_addr, oldfd)
     }
 
     /* Verify that this block is the right one */
-    if (ntohl(base[0]->ex_flags) != VLCONTBLOCK) {     /* check if flag is correct */
+    if (ntohl(base[0]->ex_hdrflags) != VLCONTBLOCK) {  /* check if flag is correct */
        free(base[0]);
        base[0] = 0;
        return;
@@ -423,7 +415,7 @@ read_mhentries(mh_addr, oldfd)
 
        sit = ntohl(base[0]->ex_contaddrs[j]);
 
-       /* Every time we allocate a new extent block, it is allocated after 
+       /* Every time we allocate a new extent block, it is allocated after
         * the previous ones. But it must be before the EOF.
         */
        if ((sit < (a + VL_ADDREXTBLK_SIZE))
@@ -438,7 +430,7 @@ read_mhentries(mh_addr, oldfd)
            perror("seek MH block");
            exit(1);
        }
-       base[j] = (struct extentaddr *)malloc(VL_ADDREXTBLK_SIZE);
+       base[j] = malloc(VL_ADDREXTBLK_SIZE);
        if (!base[j]) {
            perror("malloc1");
            exit(1);
@@ -450,7 +442,7 @@ read_mhentries(mh_addr, oldfd)
        }
 
        /* Verify that this block knows its an extent block */
-       if (ntohl(base[j]->ex_flags) != VLCONTBLOCK) {
+       if (ntohl(base[j]->ex_hdrflags) != VLCONTBLOCK) {
            free(base[j]);
            base[j] = 0;
            continue;
@@ -465,16 +457,13 @@ read_mhentries(mh_addr, oldfd)
  * extent blocks and verify that the pointers are good. And fix.
  * Then convert the multihomed addresses to single address if we
  * are converting back from version 4.
- * 
+ *
  * Before this can be called, the routine read_mhentries must be called.
  */
 void
-convert_mhentries(oldfd, newfd, header, fromver, tover)
-     int oldfd, newfd;
-     struct vlheader_2 *header;
-     int fromver, tover;
+convert_mhentries(int oldfd, int newfd, struct vlheader_2 *header,
+                 int fromver, int tover)
 {
-    afs_uint32 sit;
     afs_int32 code;
     int i, j, modified = 0, w;
     afs_uint32 raddr, addr;
@@ -532,7 +521,7 @@ convert_mhentries(oldfd, newfd, header, fromver, tover)
        }
     }
 
-    /* If we are converting from version 4 to version 3, then 
+    /* If we are converting from version 4 to version 3, then
      * translate any multihome ptrs in the IpMappedAddr array
      * to true IP addresses.
      */
@@ -546,7 +535,7 @@ convert_mhentries(oldfd, newfd, header, fromver, tover)
                basei = (addr >> 16) & 0xff;
                index = addr & 0xffff;
 
-               if ((basei >= VL_ADDREXTBLK_SIZE) || !base[basei]) {
+               if ((basei >= VL_MAX_ADDREXTBLKS) || !base[basei]) {
                    fprintf(stderr,
                            "Warning: mh entry %d has no IP address; ignored!!\n",
                            i);
@@ -599,9 +588,9 @@ convert_mhentries(oldfd, newfd, header, fromver, tover)
 }
 
 
-convert_header(ofd, fd, fromv, tov, fromaddr, toaddr)
-     int ofd, fd, fromv, tov;
-     char *fromaddr, *toaddr;
+int
+convert_header(int ofd, int fd, int fromv, int tov, void *fromaddr,
+              void *toaddr)
 {
     struct vlheader_1 *tvp1;
     struct vlheader_2 *tvp2;
@@ -741,7 +730,7 @@ convert_header(ofd, fd, fromv, tov, fromaddr, toaddr)
 
 /* Convert an address pointer to a vlentry from version 4 to version 3.
  * This involves checking if the address is after any of the four
- * MH block and if it is, subtract the size of the MH block. 
+ * MH block and if it is, subtract the size of the MH block.
  *
  * In going from version 4 to 3, the mh blocks go away and all entries
  * move up in their place. The adresses then need to be updated.
@@ -749,10 +738,9 @@ convert_header(ofd, fd, fromv, tov, fromaddr, toaddr)
  * Before this can be called, the routine read_mhentries must be called.
  */
 static afs_int32
-Conv4to3(addr)
-     afs_int32 addr;
+Conv4to3(afs_uint32 addr)
 {
-    afs_int32 raddr;
+    afs_uint32 raddr;
     int i;
 
     if (!base[0] || !addr)
@@ -768,16 +756,15 @@ Conv4to3(addr)
     return (raddr);
 }
 
-/* this only works because the vlheader struct is essentially the same 
+/* this only works because the vlheader struct is essentially the same
  * from version 1 to version 2 -- that is, the first bunch of fields
  * aren't any more or any larger, so they match up pretty well.
 */
 
-static int
-convert_vlentry(new, fromvers, tovers, oldheader, newheader, vlentryp)
-     int new, fromvers, tovers;
-     struct vlheader_1 *oldheader, *newheader; /* close enough */
-     struct vlentry_1 *vlentryp;       /* 1 and 2 are identical */
+static void
+convert_vlentry(int new, int fromvers, int tovers,
+               struct vlheader_1 *oldheader, struct vlheader_1 *newheader,
+               struct vlentry_1 *vlentryp)
 {
     int diff, i, s, w;
     struct vlentry_3 *vl3p = (struct vlentry_3 *)vlentryp;
@@ -795,7 +782,7 @@ convert_vlentry(new, fromvers, tovers, oldheader, newheader, vlentryp)
                exit(1);
            }
        }
-       return 0;
+       return;
     }
 
     if (fromvers == 2 && tovers == 3) {
@@ -827,12 +814,12 @@ convert_vlentry(new, fromvers, tovers, oldheader, newheader, vlentryp)
            exit(1);
        }
 
-       return 0;
+       return;
     } else if (fromvers == 3 && tovers == 2) {
        struct vlentry_2 vl;
        struct vlentry_3 *xnvlentry = (struct vlentry_3 *)vlentryp;
 
-       memset((char *)&vl, 0, sizeof(struct vlentry_2));
+       memset(&vl, 0, sizeof(struct vlentry_2));
        vl.volumeId[0] = xnvlentry->volumeId[0];
        vl.volumeId[1] = xnvlentry->volumeId[1];
        vl.volumeId[2] = xnvlentry->volumeId[2];
@@ -857,7 +844,7 @@ convert_vlentry(new, fromvers, tovers, oldheader, newheader, vlentryp)
            printf("Write of entry failed %d; error %u\n", w, errno);
            exit(1);
        }
-       return 0;
+       return;
     } else if (fromvers == 3 && tovers == 1) {
        struct vlentry_1 vl;
        struct vlentry_3 *xnvlentry = (struct vlentry_3 *)vlentryp;
@@ -867,7 +854,7 @@ convert_vlentry(new, fromvers, tovers, oldheader, newheader, vlentryp)
             1 ? sizeof(struct vlheader_1) : sizeof(struct vlheader_2))
            - (fromvers ==
               1 ? sizeof(struct vlheader_1) : sizeof(struct vlheader_2));
-       memset((char *)&vl, 0, sizeof(struct vlentry_1));
+       memset(&vl, 0, sizeof(struct vlentry_1));
        vl.volumeId[0] = xnvlentry->volumeId[0];
        vl.volumeId[1] = xnvlentry->volumeId[1];
        vl.volumeId[2] = xnvlentry->volumeId[2];
@@ -906,7 +893,7 @@ convert_vlentry(new, fromvers, tovers, oldheader, newheader, vlentryp)
            printf("Write of entry failed %d; error %u\n", w, errno);
            exit(1);
        }
-       return 0;
+       return;
     } else if (fromvers == 4 && tovers == 3) {
        struct vlentry_3 vl;
        /* We are converting from version 4 to 3. In this conversion, mh info
@@ -924,7 +911,7 @@ convert_vlentry(new, fromvers, tovers, oldheader, newheader, vlentryp)
            printf("Write of entry failed %d; error %u\n", w, errno);
            exit(1);
        }
-       return 0;
+       return;
     }
 
     if (tovers == 1) {
@@ -955,10 +942,8 @@ convert_vlentry(new, fromvers, tovers, oldheader, newheader, vlentryp)
     return;
 }
 
-static int
-rewrite_header(new, tovers, newheader)
-     int new, tovers;
-     char *newheader;
+static void
+rewrite_header(int new, int tovers, void *newheader)
 {
     int pos, w, towrite;
 
@@ -987,11 +972,10 @@ rewrite_header(new, tovers, newheader)
 
 #include "AFS_component_version_number.c"
 
-main(argc, argv)
-     int argc;
-     char **argv;
+int
+main(int argc, char **argv)
 {
-    register struct cmd_syndesc *ts;
+    struct cmd_syndesc *ts;
     afs_int32 code;
 
     ts = cmd_CreateSyntax("initcmd", handleit, NULL, "optional");