mirror of https://gitee.com/openkylin/libvirt.git
util: use glib base64 encoding/decoding APIs
Replace use of the gnulib base64 module with glib's own base64 API family. Reviewed-by: Ján Tomko <jtomko@redhat.com> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
This commit is contained in:
parent
c87cfa1310
commit
6c748c8e2d
|
@ -20,7 +20,6 @@
|
||||||
gnulib_modules='
|
gnulib_modules='
|
||||||
accept
|
accept
|
||||||
areadlink
|
areadlink
|
||||||
base64
|
|
||||||
bind
|
bind
|
||||||
byteswap
|
byteswap
|
||||||
c-ctype
|
c-ctype
|
||||||
|
|
|
@ -899,11 +899,6 @@ test "x$lv_cv_static_analysis" = xyes && t=1
|
||||||
AC_DEFINE_UNQUOTED([STATIC_ANALYSIS], [$t],
|
AC_DEFINE_UNQUOTED([STATIC_ANALYSIS], [$t],
|
||||||
[Define to 1 when performing static analysis.])
|
[Define to 1 when performing static analysis.])
|
||||||
|
|
||||||
# Some GNULIB base64 symbols clash with a kerberos library
|
|
||||||
AC_DEFINE_UNQUOTED([isbase64],[libvirt_gl_isbase64],[Hack to avoid symbol clash])
|
|
||||||
AC_DEFINE_UNQUOTED([base64_encode],[libvirt_gl_base64_encode],[Hack to avoid symbol clash])
|
|
||||||
AC_DEFINE_UNQUOTED([base64_encode_alloc],[libvirt_gl_base64_encode_alloc],[Hack to avoid symbol clash])
|
|
||||||
|
|
||||||
GNUmakefile=GNUmakefile
|
GNUmakefile=GNUmakefile
|
||||||
m4_if(m4_version_compare([2.61a.100],
|
m4_if(m4_version_compare([2.61a.100],
|
||||||
m4_defn([m4_PACKAGE_VERSION])), [1], [],
|
m4_defn([m4_PACKAGE_VERSION])), [1], [],
|
||||||
|
|
|
@ -31,7 +31,6 @@
|
||||||
#include "virhash.h"
|
#include "virhash.h"
|
||||||
#include "virlog.h"
|
#include "virlog.h"
|
||||||
#include "virstring.h"
|
#include "virstring.h"
|
||||||
#include "base64.h"
|
|
||||||
|
|
||||||
#define VIR_FROM_THIS VIR_FROM_SECRET
|
#define VIR_FROM_THIS VIR_FROM_SECRET
|
||||||
|
|
||||||
|
@ -698,8 +697,7 @@ virSecretObjSaveData(virSecretObjPtr obj)
|
||||||
if (!obj->value)
|
if (!obj->value)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
if (!(base64 = virStringEncodeBase64(obj->value, obj->value_size)))
|
base64 = g_base64_encode(obj->value, obj->value_size);
|
||||||
return -1;
|
|
||||||
|
|
||||||
if (virFileRewriteStr(obj->base64File, S_IRUSR | S_IWUSR, base64) < 0)
|
if (virFileRewriteStr(obj->base64File, S_IRUSR | S_IWUSR, base64) < 0)
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -825,8 +823,6 @@ virSecretLoadValue(virSecretObjPtr obj)
|
||||||
int ret = -1, fd = -1;
|
int ret = -1, fd = -1;
|
||||||
struct stat st;
|
struct stat st;
|
||||||
g_autofree char *contents = NULL;
|
g_autofree char *contents = NULL;
|
||||||
char *value = NULL;
|
|
||||||
size_t value_size;
|
|
||||||
|
|
||||||
if ((fd = open(obj->base64File, O_RDONLY)) == -1) {
|
if ((fd = open(obj->base64File, O_RDONLY)) == -1) {
|
||||||
if (errno == ENOENT) {
|
if (errno == ENOENT) {
|
||||||
|
@ -851,7 +847,7 @@ virSecretLoadValue(virSecretObjPtr obj)
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (VIR_ALLOC_N(contents, st.st_size) < 0)
|
if (VIR_ALLOC_N(contents, st.st_size + 1) < 0)
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
|
|
||||||
if (saferead(fd, contents, st.st_size) != st.st_size) {
|
if (saferead(fd, contents, st.st_size) != st.st_size) {
|
||||||
|
@ -859,29 +855,15 @@ virSecretLoadValue(virSecretObjPtr obj)
|
||||||
obj->base64File);
|
obj->base64File);
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
contents[st.st_size] = '\0';
|
||||||
|
|
||||||
VIR_FORCE_CLOSE(fd);
|
VIR_FORCE_CLOSE(fd);
|
||||||
|
|
||||||
if (!base64_decode_alloc(contents, st.st_size, &value, &value_size)) {
|
obj->value = g_base64_decode(contents, &obj->value_size);
|
||||||
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
||||||
_("invalid base64 in '%s'"),
|
|
||||||
obj->base64File);
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
if (value == NULL)
|
|
||||||
goto cleanup;
|
|
||||||
|
|
||||||
obj->value = (unsigned char *)value;
|
|
||||||
value = NULL;
|
|
||||||
obj->value_size = value_size;
|
|
||||||
|
|
||||||
ret = 0;
|
ret = 0;
|
||||||
|
|
||||||
cleanup:
|
cleanup:
|
||||||
if (value != NULL) {
|
|
||||||
memset(value, 0, value_size);
|
|
||||||
VIR_FREE(value);
|
|
||||||
}
|
|
||||||
if (contents != NULL)
|
if (contents != NULL)
|
||||||
memset(contents, 0, st.st_size);
|
memset(contents, 0, st.st_size);
|
||||||
VIR_FORCE_CLOSE(fd);
|
VIR_FORCE_CLOSE(fd);
|
||||||
|
|
|
@ -3068,7 +3068,6 @@ virSkipSpacesBackwards;
|
||||||
virStrcpy;
|
virStrcpy;
|
||||||
virStrdup;
|
virStrdup;
|
||||||
virStringBufferIsPrintable;
|
virStringBufferIsPrintable;
|
||||||
virStringEncodeBase64;
|
|
||||||
virStringFilterChars;
|
virStringFilterChars;
|
||||||
virStringHasCaseSuffix;
|
virStringHasCaseSuffix;
|
||||||
virStringHasChars;
|
virStringHasChars;
|
||||||
|
|
|
@ -1004,8 +1004,7 @@ libxlMakeNetworkDiskSrc(virStorageSourcePtr src, char **srcstr)
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
|
|
||||||
/* RBD expects an encoded secret */
|
/* RBD expects an encoded secret */
|
||||||
if (!(base64secret = virStringEncodeBase64(secret, secretlen)))
|
base64secret = g_base64_encode(secret, secretlen);
|
||||||
goto cleanup;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!(*srcstr = libxlMakeNetworkDiskSrcStr(src, username, base64secret)))
|
if (!(*srcstr = libxlMakeNetworkDiskSrcStr(src, username, base64secret)))
|
||||||
|
|
|
@ -39,7 +39,6 @@
|
||||||
#include "virtime.h"
|
#include "virtime.h"
|
||||||
#include "virobject.h"
|
#include "virobject.h"
|
||||||
#include "virstring.h"
|
#include "virstring.h"
|
||||||
#include "base64.h"
|
|
||||||
#include "virenum.h"
|
#include "virenum.h"
|
||||||
|
|
||||||
#define VIR_FROM_THIS VIR_FROM_QEMU
|
#define VIR_FROM_THIS VIR_FROM_QEMU
|
||||||
|
@ -2518,9 +2517,8 @@ qemuAgentSetUserPassword(qemuAgentPtr mon,
|
||||||
virJSONValuePtr reply = NULL;
|
virJSONValuePtr reply = NULL;
|
||||||
char *password64 = NULL;
|
char *password64 = NULL;
|
||||||
|
|
||||||
if (!(password64 = virStringEncodeBase64((unsigned char *)password,
|
password64 = g_base64_encode((unsigned char *)password,
|
||||||
strlen(password))))
|
strlen(password));
|
||||||
goto cleanup;
|
|
||||||
|
|
||||||
if (!(cmd = qemuAgentMakeCommand("guest-set-user-password",
|
if (!(cmd = qemuAgentMakeCommand("guest-set-user-password",
|
||||||
"b:crypted", crypted,
|
"b:crypted", crypted,
|
||||||
|
|
|
@ -837,9 +837,8 @@ qemuBuildRBDSecinfoURI(virBufferPtr buf,
|
||||||
|
|
||||||
switch ((qemuDomainSecretInfoType) secinfo->type) {
|
switch ((qemuDomainSecretInfoType) secinfo->type) {
|
||||||
case VIR_DOMAIN_SECRET_INFO_TYPE_PLAIN:
|
case VIR_DOMAIN_SECRET_INFO_TYPE_PLAIN:
|
||||||
if (!(base64secret = virStringEncodeBase64(secinfo->s.plain.secret,
|
base64secret = g_base64_encode(secinfo->s.plain.secret,
|
||||||
secinfo->s.plain.secretlen)))
|
secinfo->s.plain.secretlen);
|
||||||
return -1;
|
|
||||||
virBufferEscape(buf, '\\', ":", ":id=%s", secinfo->s.plain.username);
|
virBufferEscape(buf, '\\', ":", ":id=%s", secinfo->s.plain.username);
|
||||||
virBufferEscape(buf, '\\', ":",
|
virBufferEscape(buf, '\\', ":",
|
||||||
":key=%s:auth_supported=cephx\\;none",
|
":key=%s:auth_supported=cephx\\;none",
|
||||||
|
|
|
@ -1470,8 +1470,7 @@ qemuDomainSecretAESSetup(qemuDomainObjPrivatePtr priv,
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
|
|
||||||
/* Encode the IV and save that since qemu will need it */
|
/* Encode the IV and save that since qemu will need it */
|
||||||
if (!(secinfo->s.aes.iv = virStringEncodeBase64(raw_iv, ivlen)))
|
secinfo->s.aes.iv = g_base64_encode(raw_iv, ivlen);
|
||||||
goto cleanup;
|
|
||||||
|
|
||||||
/* Grab the unencoded secret */
|
/* Grab the unencoded secret */
|
||||||
if (virSecretGetSecretString(conn, seclookupdef, usageType,
|
if (virSecretGetSecretString(conn, seclookupdef, usageType,
|
||||||
|
@ -1488,9 +1487,8 @@ qemuDomainSecretAESSetup(qemuDomainObjPrivatePtr priv,
|
||||||
memset(secret, 0, secretlen);
|
memset(secret, 0, secretlen);
|
||||||
|
|
||||||
/* Now encode the ciphertext and store to be passed to qemu */
|
/* Now encode the ciphertext and store to be passed to qemu */
|
||||||
if (!(secinfo->s.aes.ciphertext = virStringEncodeBase64(ciphertext,
|
secinfo->s.aes.ciphertext = g_base64_encode(ciphertext,
|
||||||
ciphertextlen)))
|
ciphertextlen);
|
||||||
goto cleanup;
|
|
||||||
|
|
||||||
ret = 0;
|
ret = 0;
|
||||||
|
|
||||||
|
|
|
@ -25,7 +25,6 @@
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "internal.h"
|
#include "internal.h"
|
||||||
#include "base64.h"
|
|
||||||
#include "datatypes.h"
|
#include "datatypes.h"
|
||||||
#include "driver.h"
|
#include "driver.h"
|
||||||
#include "virlog.h"
|
#include "virlog.h"
|
||||||
|
|
|
@ -28,7 +28,6 @@
|
||||||
#include "storage_conf.h"
|
#include "storage_conf.h"
|
||||||
#include "viralloc.h"
|
#include "viralloc.h"
|
||||||
#include "virlog.h"
|
#include "virlog.h"
|
||||||
#include "base64.h"
|
|
||||||
#include "viruuid.h"
|
#include "viruuid.h"
|
||||||
#include "virstring.h"
|
#include "virstring.h"
|
||||||
#include "virrandom.h"
|
#include "virrandom.h"
|
||||||
|
@ -218,8 +217,7 @@ virStorageBackendRBDOpenRADOSConn(virStorageBackendRBDStatePtr ptr,
|
||||||
&secret_value, &secret_value_size) < 0)
|
&secret_value, &secret_value_size) < 0)
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
|
|
||||||
if (!(rados_key = virStringEncodeBase64(secret_value, secret_value_size)))
|
rados_key = g_base64_encode(secret_value, secret_value_size);
|
||||||
goto cleanup;
|
|
||||||
|
|
||||||
if (virStorageBackendRBDRADOSConfSet(ptr->cluster,
|
if (virStorageBackendRBDRADOSConfSet(ptr->cluster,
|
||||||
"key", rados_key) < 0)
|
"key", rados_key) < 0)
|
||||||
|
|
|
@ -22,7 +22,6 @@
|
||||||
#include <regex.h>
|
#include <regex.h>
|
||||||
#include <locale.h>
|
#include <locale.h>
|
||||||
|
|
||||||
#include "base64.h"
|
|
||||||
#include "c-ctype.h"
|
#include "c-ctype.h"
|
||||||
#include "virstring.h"
|
#include "virstring.h"
|
||||||
#include "virthread.h"
|
#include "virthread.h"
|
||||||
|
@ -1438,26 +1437,6 @@ virStringBufferIsPrintable(const uint8_t *buf,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* virStringEncodeBase64:
|
|
||||||
* @buf: buffer of bytes to encode
|
|
||||||
* @buflen: number of bytes to encode
|
|
||||||
*
|
|
||||||
* Encodes @buf to base 64 and returns the resulting string. The caller is
|
|
||||||
* responsible for freeing the result.
|
|
||||||
*/
|
|
||||||
char *
|
|
||||||
virStringEncodeBase64(const uint8_t *buf, size_t buflen)
|
|
||||||
{
|
|
||||||
char *ret;
|
|
||||||
|
|
||||||
base64_encode_alloc((const char *) buf, buflen, &ret);
|
|
||||||
if (!ret)
|
|
||||||
abort();
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* virStringTrimOptionalNewline:
|
* virStringTrimOptionalNewline:
|
||||||
* @str: the string to modify in-place
|
* @str: the string to modify in-place
|
||||||
|
|
|
@ -291,8 +291,6 @@ void virStringFilterChars(char *str, const char *valid);
|
||||||
bool virStringIsPrintable(const char *str);
|
bool virStringIsPrintable(const char *str);
|
||||||
bool virStringBufferIsPrintable(const uint8_t *buf, size_t buflen);
|
bool virStringBufferIsPrintable(const uint8_t *buf, size_t buflen);
|
||||||
|
|
||||||
char *virStringEncodeBase64(const uint8_t *buf, size_t buflen);
|
|
||||||
|
|
||||||
void virStringTrimOptionalNewline(char *str);
|
void virStringTrimOptionalNewline(char *str);
|
||||||
|
|
||||||
int virStringParsePort(const char *str,
|
int virStringParsePort(const char *str,
|
||||||
|
|
|
@ -22,7 +22,6 @@
|
||||||
#include "virsh-secret.h"
|
#include "virsh-secret.h"
|
||||||
|
|
||||||
#include "internal.h"
|
#include "internal.h"
|
||||||
#include "base64.h"
|
|
||||||
#include "virbuffer.h"
|
#include "virbuffer.h"
|
||||||
#include "viralloc.h"
|
#include "viralloc.h"
|
||||||
#include "virfile.h"
|
#include "virfile.h"
|
||||||
|
@ -192,7 +191,7 @@ cmdSecretSetValue(vshControl *ctl, const vshCmd *cmd)
|
||||||
virSecretPtr secret;
|
virSecretPtr secret;
|
||||||
size_t value_size;
|
size_t value_size;
|
||||||
const char *base64 = NULL;
|
const char *base64 = NULL;
|
||||||
char *value;
|
unsigned char *value;
|
||||||
int res;
|
int res;
|
||||||
bool ret = false;
|
bool ret = false;
|
||||||
|
|
||||||
|
@ -202,16 +201,9 @@ cmdSecretSetValue(vshControl *ctl, const vshCmd *cmd)
|
||||||
if (vshCommandOptStringReq(ctl, cmd, "base64", &base64) < 0)
|
if (vshCommandOptStringReq(ctl, cmd, "base64", &base64) < 0)
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
|
|
||||||
if (!base64_decode_alloc(base64, strlen(base64), &value, &value_size)) {
|
value = g_base64_decode(base64, &value_size);
|
||||||
vshError(ctl, "%s", _("Invalid base64 data"));
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
if (value == NULL) {
|
|
||||||
vshError(ctl, "%s", _("Failed to allocate memory"));
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
|
|
||||||
res = virSecretSetValue(secret, (unsigned char *)value, value_size, 0);
|
res = virSecretSetValue(secret, value, value_size, 0);
|
||||||
memset(value, 0, value_size);
|
memset(value, 0, value_size);
|
||||||
VIR_FREE(value);
|
VIR_FREE(value);
|
||||||
|
|
||||||
|
@ -267,8 +259,7 @@ cmdSecretGetValue(vshControl *ctl, const vshCmd *cmd)
|
||||||
if (value == NULL)
|
if (value == NULL)
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
|
|
||||||
if (!(base64 = virStringEncodeBase64(value, value_size)))
|
base64 = g_base64_encode(value, value_size);
|
||||||
goto cleanup;
|
|
||||||
|
|
||||||
vshPrint(ctl, "%s", base64);
|
vshPrint(ctl, "%s", base64);
|
||||||
ret = true;
|
ret = true;
|
||||||
|
|
Loading…
Reference in New Issue