libadmin: Don't overflow volume name
[openafs.git] / src / libadmin / vos / vsprocs.c
index f313f41..d5f6f6e 100644 (file)
 #include <afsconfig.h>
 #include <afs/param.h>
 
+#include <roken.h>
+
+#include <afs/afs_AdminErrors.h>
 
 #include "vsprocs.h"
 #include "vosutils.h"
 #include "lockprocs.h"
 #include "../adminutil/afs_AdminInternal.h"
-#include <afs/afs_AdminErrors.h>
 #include "afs_vosAdmin.h"
-#include <string.h>
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
-#endif
-#ifdef AFS_NT40_ENV
-#include <io.h>
-#endif
 
 static afs_int32 GroupEntries(struct rx_connection *server, volintInfo * pntr, afs_int32 count,
              struct qHead *myQueue, afs_int32 apart);
@@ -64,7 +59,6 @@ CheckAndDeleteVolume(struct rx_connection *aconn, afs_int32 apart,
     afs_int32 error, code, tid, rcode;
 
     error = 0;
-    code = 0;
 
     if (okvol == 0) {
        code = AFSVolTransCreate(aconn, delvol, apart, ITOffline, &tid);
@@ -398,8 +392,7 @@ UV_MoveVolume(afs_cell_handle_p cellHandle, afs_uint32 afromvol,
     struct destServer destination;
 
     struct nvldbentry entry;
-    int islocked, pntg;
-    afs_int32 error;
+    int islocked;
     int same;
     afs_int32 store_flags;
 
@@ -414,9 +407,7 @@ UV_MoveVolume(afs_cell_handle_p cellHandle, afs_uint32 afromvol,
     fromtid = 0;
     totid = 0;
     clonetid = 0;
-    error = 0;
     volid = 0;
-    pntg = 0;
     backupId = 0;
     newVol = 0;
 
@@ -464,7 +455,6 @@ UV_MoveVolume(afs_cell_handle_p cellHandle, afs_uint32 afromvol,
         */
        fromconn = UV_Bind(cellHandle, afromserver, AFSCONF_VOLUMEPORT);
        fromtid = 0;
-       pntg = 1;
 
        tst =
            AFSVolTransCreate(fromconn, afromvol, afrompart, ITOffline,
@@ -519,7 +509,6 @@ UV_MoveVolume(afs_cell_handle_p cellHandle, afs_uint32 afromvol,
        }
 
        fromtid = 0;
-       error = 0;
        goto fail_UV_MoveVolume;
     }
 
@@ -538,7 +527,6 @@ UV_MoveVolume(afs_cell_handle_p cellHandle, afs_uint32 afromvol,
        }
     }
 
-    pntg = 1;
     toconn = UV_Bind(cellHandle, atoserver, AFSCONF_VOLUMEPORT);       /* get connections to the servers */
     fromconn = UV_Bind(cellHandle, afromserver, AFSCONF_VOLUMEPORT);
     fromtid = totid = 0;       /* initialize to uncreated */
@@ -1178,15 +1166,13 @@ static int
 DelVol(struct rx_connection *conn, afs_uint32 vid, afs_int32 part,
        afs_int32 flags)
 {
-    afs_int32 acode, ccode, rcode, tid;
-    ccode = rcode = tid = 0;
+    afs_int32 acode, rcode, tid;
+    rcode = tid = 0;
 
     acode = AFSVolTransCreate(conn, vid, part, flags, &tid);
     if (!acode) {              /* It really was there */
        acode = AFSVolDeleteVolume(conn, tid);
-       ccode = AFSVolEndTrans(conn, tid, &rcode);
-       if (!ccode)
-           ccode = rcode;
+       AFSVolEndTrans(conn, tid, &rcode);
     }
 
     return acode;
@@ -1308,7 +1294,7 @@ GetTrans(afs_cell_handle_p cellHandle, struct nvldbentry *vldbEntryPtr,
         afs_int32 * transPtr, afs_int32 * timePtr, afs_status_p st)
 {
     int rc = 0;
-    afs_status_t tst = 0, etst = 0;
+    afs_status_t tst = 0;
     afs_uint32 volid;
     struct volser_status tstatus;
     int rcode;
@@ -1337,14 +1323,18 @@ GetTrans(afs_cell_handle_p cellHandle, struct nvldbentry *vldbEntryPtr,
 
     /* If the volume does not exist, create it */
     if (!volid || tst) {
-       char volname[64];
+       char volname[VL_MAXNAMELEN];
 
        if (volid && (tst != VNOVOL)) {
            goto fail_GetTrans;
        }
 
-       strcpy(volname, vldbEntryPtr->name);
-       strcat(volname, ".readonly");
+       strlcpy(volname, vldbEntryPtr->name, sizeof(volname));
+       if (strlcat(volname, ".readonly", sizeof(volname))
+               >= sizeof(volname)) {
+           tst = ENOMEM;
+           goto fail_GetTrans;
+       }
 
        tst =
            AFSVolCreateVolume(*connPtr, vldbEntryPtr->serverPartition[index],
@@ -1380,10 +1370,8 @@ GetTrans(afs_cell_handle_p cellHandle, struct nvldbentry *vldbEntryPtr,
   fail_GetTrans:
 
     if ((rc == 0) && (*transPtr)) {
-       etst = AFSVolEndTrans(*connPtr, *transPtr, &rcode);
+       AFSVolEndTrans(*connPtr, *transPtr, &rcode);
        *transPtr = 0;
-       if (!etst)
-           etst = rcode;
     }
 
     if (st != NULL) {
@@ -1710,24 +1698,15 @@ UV_ReleaseVolume(afs_cell_handle_p cellHandle, afs_uint32 afromvol,
     cookie.clone = 0;
 
     nservers = entry.nServers / 2;     /* how many to do at once, excluding clone */
-    replicas =
-       (struct replica *)malloc(sizeof(struct replica) * nservers + 1);
-    times = (struct release *)malloc(sizeof(struct release) * nservers + 1);
-    toconns =
-       (struct rx_connection **)malloc(sizeof(struct rx_connection *) *
-                                       nservers + 1);
-    results.manyResults_val =
-       (afs_int32 *) malloc(sizeof(afs_int32) * nservers + 1);
+    replicas = calloc(nservers + 1, sizeof(struct replica));
+    times = calloc(nservers + 1, sizeof(struct release));
+    toconns = calloc(nservers + 1, sizeof(struct rx_connection *));
+    results.manyResults_val = calloc(nservers + 1, sizeof(afs_int32));
     if (!replicas || !times || !!!results.manyResults_val || !toconns) {
        tst = ADMNOMEM;
        goto fail_UV_ReleaseVolume;
     }
 
-    memset(replicas, 0, (sizeof(struct replica) * nservers + 1));
-    memset(times, 0, (sizeof(struct release) * nservers + 1));
-    memset(toconns, 0, (sizeof(struct rx_connection *) * nservers + 1));
-    memset(results.manyResults_val, 0, (sizeof(afs_int32) * nservers + 1));
-
     /* Create a transaction on the cloned volume */
     tst =
        AFSVolTransCreate(fromconn, cloneVolId, afrompart, ITBusy, &fromtid);
@@ -2015,7 +1994,7 @@ t structure!! */
     }
 #endif
     nbytes = blockSize;
-    buffer = (char *)malloc(blockSize);
+    buffer = malloc(blockSize);
     if (!buffer) {
        return ADMNOMEM;
     }
@@ -2055,7 +2034,6 @@ DumpFunction(struct rx_call *call, const char *filename)
     afs_int32 error, code;
 
     error = 0;
-    fd = -1;
 
     fd = open(filename, O_CREAT | O_TRUNC | O_WRONLY, 0666);
     if (fd < 0 || fstat(fd, &status) < 0) {
@@ -2099,18 +2077,14 @@ UV_DumpVolume(afs_cell_handle_p cellHandle, afs_uint32 afromvol,
     afs_int32 rcode;
 
     struct nvldbentry entry;
-    afs_int32 error;
     int islocked;
 
     islocked = 0;
-    error = 0;
     rxError = 0;
-    fromcall = (struct rx_call *)0;
     fromconn = (struct rx_connection *)0;
     fromtid = 0;
     fromcall = (struct rx_call *)0;
 
-    islocked = 0;
     if (!aVLDB_GetEntryByID(cellHandle, afromvol, -1, &entry, &tst)) {
        goto fail_UV_DumpVolume;
     }
@@ -2191,7 +2165,6 @@ SendFile(int fd, struct rx_call *call,
     int blockSize;
     fd_set in;
     afs_int32 error = 0;
-    int done = 0;
     int nbytes;
 
 #ifdef AFS_NT40_ENV
@@ -2212,12 +2185,12 @@ t structure!! */
        blockSize = 4096;
     }
 #endif
-    buffer = (char *)malloc(blockSize);
+    buffer = malloc(blockSize);
     if (!buffer) {
        return ADMNOMEM;
     }
 
-    while (!error && !done) {
+    while (!error) {
        FD_ZERO(&in);
        FD_SET(fd, &in);
 #ifndef AFS_NT40_ENV           /* NT csn't select on non-socket fd's */
@@ -2228,10 +2201,10 @@ t structure!! */
            error = ADMVOSRESTOREFILEREADFAIL;
            break;
        }
-       if (nbytes == 0) {
-           done = 1;
+
+       if (nbytes == 0)
            break;
-       }
+
        if (rx_Write(call, buffer, nbytes) != nbytes) {
            error = ADMVOSRESTOREFILEWRITEFAIL;
            break;
@@ -2250,7 +2223,6 @@ WriteData(struct rx_call *call, const char *filename)
     afs_int32 error, code;
 
     error = 0;
-    fd = -1;
 
     fd = open(filename, 0);
     if (fd < 0 || fstat(fd, &status) < 0) {
@@ -2300,7 +2272,6 @@ UV_RestoreVolume(afs_cell_handle_p cellHandle, afs_int32 toserver,
     afs_int32 temptid;
     int success;
     struct nvldbentry entry;
-    afs_int32 error;
     int islocked;
     struct restoreCookie cookie;
     int reuseID;
@@ -2310,11 +2281,8 @@ UV_RestoreVolume(afs_cell_handle_p cellHandle, afs_int32 toserver,
 
     memset(&cookie, 0, sizeof(cookie));
     islocked = 0;
-    success = 0;
-    error = 0;
     reuseID = 1;
     tocall = (struct rx_call *)0;
-    toconn = (struct rx_connection *)0;
     tempconn = (struct rx_connection *)0;
     totid = 0;
     temptid = 0;
@@ -2998,7 +2966,7 @@ ProcessEntries(afs_cell_handle_p cellHandle, struct qHead *myQueue,
 {
     struct aqueue elem;
     int success, temp;
-    afs_uint32 temp1, temp2;
+    afs_uint32 temp2;
     afs_int32 vcode;
     afs_uint32 maxVolid = 0;
     struct nvldbentry entry;
@@ -3030,30 +2998,33 @@ ProcessEntries(afs_cell_handle_p cellHandle, struct qHead *myQueue,
            continue;
        }
        if (maxVolid <= elem.ids[RWVOL]) {
-           temp1 = maxVolid;
            temp2 = elem.ids[RWVOL] - maxVolid + 1;
            maxVolid = 0;
-           vcode =
-               ubik_VL_GetNewVolumeId(cellHandle->vos, 0, temp2,
-                         &maxVolid);
+           vcode = ubik_VL_GetNewVolumeId(cellHandle->vos, 0, temp2,
+                                          &maxVolid);
+           if (vcode)
+               return vcode;
+
            maxVolid += temp2;
        }
        if (maxVolid <= elem.ids[ROVOL]) {
-           temp1 = maxVolid;
            temp2 = elem.ids[ROVOL] - maxVolid + 1;
            maxVolid = 0;
-           vcode =
-               ubik_VL_GetNewVolumeId(cellHandle->vos, 0, temp2,
-                         &maxVolid);
+           vcode = ubik_VL_GetNewVolumeId(cellHandle->vos, 0, temp2,
+                                          &maxVolid);
+           if (vcode)
+               return vcode;
+
            maxVolid += temp2;
        }
        if (maxVolid <= elem.ids[BACKVOL]) {
-           temp1 = maxVolid;
-           temp2 = elem.ids[BACKVOL] - temp1 + 1;
+           temp2 = elem.ids[BACKVOL] - maxVolid + 1;
            maxVolid = 0;
-           vcode =
-               ubik_VL_GetNewVolumeId(cellHandle->vos, 0, temp2,
-                         &maxVolid);
+           vcode = ubik_VL_GetNewVolumeId(cellHandle->vos, 0, temp2,
+                                          &maxVolid);
+           if (vcode)
+               return vcode;
+
            maxVolid += temp2;
        }
        aVLDB_GetEntryByID(cellHandle, elem.ids[RWVOL], RWVOL, &entry, &tst);
@@ -3519,7 +3490,6 @@ CheckVldb(afs_cell_handle_p cellHandle, struct nvldbentry *entry,
     int islocked = 0;
     int pass = 0;
     afs_int32 modentry = 0;
-    afs_int32 delentry = 0;
 
     if (modified) {
        *modified = 0;
@@ -3583,7 +3553,6 @@ CheckVldb(afs_cell_handle_p cellHandle, struct nvldbentry *entry,
            if (tst) {
                goto fail_CheckVldb;
            }
-           delentry = 1;
        } else {
            /* Replace old entry with our new one */
            if (!VLDB_ReplaceEntry
@@ -3626,7 +3595,6 @@ UV_SyncServer(afs_cell_handle_p cellHandle, struct rx_connection *server,
 {
     int rc = 0;
     afs_status_t tst = 0;
-    afs_int32 code, vcode;
     int noError;
     afs_int32 nentries, tentries = 0;
     struct VldbListByAttributes attributes;
@@ -3637,8 +3605,6 @@ UV_SyncServer(afs_cell_handle_p cellHandle, struct rx_connection *server,
     afs_int32 si, nsi;
     afs_int32 modified = 0;
 
-    code = 0;
-    vcode = 0;
     noError = 1;
     arrayEntries.nbulkentries_val = 0;
 
@@ -3701,14 +3667,13 @@ UV_RenameVolume(afs_cell_handle_p cellHandle, struct nvldbentry *entry,
     int rc = 0;
     afs_status_t tst = 0;
     afs_status_t etst = 0;
-    afs_int32 rcode, error;
+    afs_int32 rcode;
     int i, index;
     char nameBuffer[256];
     afs_int32 tid;
     struct rx_connection *aconn;
     int islocked;
 
-    error = 0;
     aconn = (struct rx_connection *)0;
     tid = 0;
     islocked = 0;
@@ -3981,7 +3946,7 @@ GroupEntries(struct rx_connection *server, volintInfo * pntr, afs_int32 count,
                        error = VOLSERBADOP;
                }
            } else {            /*create a fresh entry */
-               qPtr = (struct aqueue *)malloc(sizeof(struct aqueue));
+               qPtr = malloc(sizeof(struct aqueue));
                if (pntr->type == RWVOL) {
                    qPtr->isValid[RWVOL] = 1;
                    qPtr->isValid[BACKVOL] = 0;
@@ -4065,12 +4030,10 @@ int
 UV_VolumeZap(afs_cell_handle_p cellHandle, struct rx_connection *server,
             unsigned int partition, afs_uint32 volumeId, afs_status_p st)
 {
-    afs_int32 rcode, ttid, error, code;
+    afs_int32 rcode, ttid;
     int rc = 0;
     afs_status_t tst = 0;
 
-    code = 0;
-    error = 0;
     ttid = 0;
 
     tst = AFSVolTransCreate(server, volumeId, partition, ITOffline, &ttid);