[PATCH 2/4] Convert lock driver plugins to use new crypto APIs

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Conver the sanlock and lockd lock driver plugins over to use
the new virCryptoHashString APIs instead of having their own
duplicated code.

Signed-off-by: Daniel P. Berrange <berrange@xxxxxxxxxx>
---
 src/locking/lock_driver_lockd.c   | 32 ++---------------------------
 src/locking/lock_driver_sanlock.c | 42 ++++++++++-----------------------------
 2 files changed, 12 insertions(+), 62 deletions(-)

diff --git a/src/locking/lock_driver_lockd.c b/src/locking/lock_driver_lockd.c
index f3b9467..1b92d68 100644
--- a/src/locking/lock_driver_lockd.c
+++ b/src/locking/lock_driver_lockd.c
@@ -24,6 +24,7 @@
 #include "lock_driver.h"
 #include "virconf.h"
 #include "viralloc.h"
+#include "vircrypto.h"
 #include "virlog.h"
 #include "viruuid.h"
 #include "virfile.h"
@@ -31,7 +32,6 @@
 #include "rpc/virnetclient.h"
 #include "lock_protocol.h"
 #include "configmake.h"
-#include "sha256.h"
 #include "virstring.h"
 
 #define VIR_FROM_THIS VIR_FROM_LOCKING
@@ -505,34 +505,6 @@ static int virLockManagerLockDaemonNew(virLockManagerPtr lock,
 }
 
 
-static const char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7',
-                            '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
-
-static char *virLockManagerLockDaemonDiskLeaseName(const char *path)
-{
-    unsigned char buf[SHA256_DIGEST_SIZE];
-    char *ret;
-    size_t i;
-
-    if (!(sha256_buffer(path, strlen(path), buf))) {
-        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
-                       _("Unable to compute sha256 checksum"));
-        return NULL;
-    }
-
-    if (VIR_ALLOC_N(ret, (SHA256_DIGEST_SIZE * 2) + 1) < 0)
-        return NULL;
-
-    for (i = 0; i < SHA256_DIGEST_SIZE; i++) {
-        ret[i*2] = hex[(buf[i] >> 4) & 0xf];
-        ret[(i*2)+1] = hex[buf[i] & 0xf];
-    }
-    ret[(SHA256_DIGEST_SIZE * 2) + 1] = '\0';
-
-    return ret;
-}
-
-
 static int virLockManagerLockDaemonAddResource(virLockManagerPtr lock,
                                                unsigned int type,
                                                const char *name,
@@ -605,7 +577,7 @@ static int virLockManagerLockDaemonAddResource(virLockManagerPtr lock,
         if (driver->fileLockSpaceDir) {
             if (VIR_STRDUP(newLockspace, driver->fileLockSpaceDir) < 0)
                 goto error;
-            if (!(newName = virLockManagerLockDaemonDiskLeaseName(name)))
+            if (virCryptoHashString(VIR_CRYPTO_HASH_SHA256, name, &newName) < 0)
                 goto error;
             autoCreate = true;
             VIR_DEBUG("Using indirect lease %s for %s", newName, name);
diff --git a/src/locking/lock_driver_sanlock.c b/src/locking/lock_driver_sanlock.c
index f11f3c6..0c87048 100644
--- a/src/locking/lock_driver_sanlock.c
+++ b/src/locking/lock_driver_sanlock.c
@@ -40,8 +40,8 @@
 #include "virlog.h"
 #include "virerror.h"
 #include "viralloc.h"
+#include "vircrypto.h"
 #include "virfile.h"
-#include "md5.h"
 #include "virconf.h"
 #include "virstring.h"
 
@@ -509,36 +509,6 @@ static void virLockManagerSanlockFree(virLockManagerPtr lock)
 }
 
 
-static const char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7',
-                            '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
-
-static int virLockManagerSanlockDiskLeaseName(const char *path,
-                                              char *str,
-                                              size_t strbuflen)
-{
-    unsigned char buf[MD5_DIGEST_SIZE];
-    size_t i;
-
-    if (strbuflen < ((MD5_DIGEST_SIZE * 2) + 1)) {
-        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
-                       _("String length too small to store md5 checksum"));
-        return -1;
-    }
-
-    if (!(md5_buffer(path, strlen(path), buf))) {
-        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
-                       _("Unable to compute md5 checksum"));
-        return -1;
-    }
-
-    for (i = 0; i < MD5_DIGEST_SIZE; i++) {
-        str[i*2] = hex[(buf[i] >> 4) & 0xf];
-        str[(i*2)+1] = hex[buf[i] & 0xf];
-    }
-    str[(MD5_DIGEST_SIZE*2)+1] = '\0';
-    return 0;
-}
-
 static int virLockManagerSanlockAddLease(virLockManagerPtr lock,
                                          const char *name,
                                          size_t nparams,
@@ -606,6 +576,7 @@ static int virLockManagerSanlockAddDisk(virLockManagerPtr lock,
     int ret = -1;
     struct sanlk_resource *res = NULL;
     char *path = NULL;
+    char *hash = NULL;
 
     if (nparams) {
         virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
@@ -618,8 +589,14 @@ static int virLockManagerSanlockAddDisk(virLockManagerPtr lock,
 
     res->flags = shared ? SANLK_RES_SHARED : 0;
     res->num_disks = 1;
-    if (virLockManagerSanlockDiskLeaseName(name, res->name, SANLK_NAME_LEN) < 0)
+    if (virCryptoHashString(VIR_CRYPTO_HASH_MD5, name, &hash) < 0)
+        goto cleanup;
+    if (!virStrcpy(res->name, hash, SANLK_NAME_LEN)) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("MD5 hash '%s' unexpectedly larger than %d characters"),
+                       hash, (SANLK_NAME_LEN - 1));
         goto cleanup;
+    }
 
     if (virAsprintf(&path, "%s/%s",
                     driver->autoDiskLeasePath, res->name) < 0)
@@ -649,6 +626,7 @@ cleanup:
     if (ret == -1)
         VIR_FREE(res);
     VIR_FREE(path);
+    VIR_FREE(hash);
     return ret;
 }
 
-- 
1.8.5.3

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]