From ed839f9aefca059904c408998b5561ef24c750f1 Mon Sep 17 00:00:00 2001 From: "Daniel P. Berrange" Date: Wed, 5 Mar 2014 12:39:00 +0000 Subject: [PATCH] Convert lock driver plugins to use new crypto APIs Convert 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 --- 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 f3b9467fe5..1b92d680cc 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 f11f3c633c..0c87048f98 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; } -- GitLab