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 <berrange@redhat.com>
This commit is contained in:
Daniel P. Berrange 2014-03-05 12:39:00 +00:00
parent 3a7fe8d508
commit ed839f9aef
2 changed files with 12 additions and 62 deletions

View File

@ -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);

View File

@ -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;
}