2016-02-27 11:49:12 +00:00
|
|
|
/*
|
|
|
|
* virsecretobj.c: internal <secret> objects handling
|
|
|
|
*
|
|
|
|
* Copyright (C) 2009-2016 Red Hat, Inc.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library. If not, see
|
|
|
|
* <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
2016-03-02 18:10:54 +00:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/stat.h>
|
2016-02-27 11:49:12 +00:00
|
|
|
|
|
|
|
#include "datatypes.h"
|
|
|
|
#include "virsecretobj.h"
|
|
|
|
#include "viralloc.h"
|
2016-04-19 20:05:38 +00:00
|
|
|
#include "virerror.h"
|
|
|
|
#include "virfile.h"
|
2016-02-27 11:49:12 +00:00
|
|
|
#include "virhash.h"
|
2016-04-19 20:05:38 +00:00
|
|
|
#include "virlog.h"
|
2016-05-13 11:15:15 +00:00
|
|
|
#include "virstring.h"
|
2016-03-02 18:10:54 +00:00
|
|
|
#include "base64.h"
|
2016-02-27 11:49:12 +00:00
|
|
|
|
2016-04-19 20:05:38 +00:00
|
|
|
#define VIR_FROM_THIS VIR_FROM_SECRET
|
|
|
|
|
|
|
|
VIR_LOG_INIT("conf.virsecretobj");
|
2016-02-27 11:49:12 +00:00
|
|
|
|
2016-03-08 00:40:58 +00:00
|
|
|
struct _virSecretObj {
|
|
|
|
virObjectLockable parent;
|
|
|
|
char *configFile;
|
|
|
|
char *base64File;
|
|
|
|
virSecretDefPtr def;
|
|
|
|
unsigned char *value; /* May be NULL */
|
|
|
|
size_t value_size;
|
|
|
|
};
|
|
|
|
|
2016-02-27 11:49:12 +00:00
|
|
|
static virClassPtr virSecretObjClass;
|
|
|
|
static virClassPtr virSecretObjListClass;
|
|
|
|
static void virSecretObjDispose(void *obj);
|
|
|
|
static void virSecretObjListDispose(void *obj);
|
|
|
|
|
|
|
|
struct _virSecretObjList {
|
|
|
|
virObjectLockable parent;
|
|
|
|
|
|
|
|
/* uuid string -> virSecretObj mapping
|
|
|
|
* for O(1), lockless lookup-by-uuid */
|
|
|
|
virHashTable *objs;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct virSecretSearchData {
|
|
|
|
int usageType;
|
|
|
|
const char *usageID;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
virSecretObjOnceInit(void)
|
|
|
|
{
|
|
|
|
if (!(virSecretObjClass = virClassNew(virClassForObjectLockable(),
|
|
|
|
"virSecretObj",
|
|
|
|
sizeof(virSecretObj),
|
|
|
|
virSecretObjDispose)))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (!(virSecretObjListClass = virClassNew(virClassForObjectLockable(),
|
|
|
|
"virSecretObjList",
|
|
|
|
sizeof(virSecretObjList),
|
|
|
|
virSecretObjListDispose)))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
VIR_ONCE_GLOBAL_INIT(virSecretObj)
|
|
|
|
|
2017-04-05 20:58:32 +00:00
|
|
|
static virSecretObjPtr
|
2016-02-27 11:49:12 +00:00
|
|
|
virSecretObjNew(void)
|
|
|
|
{
|
2017-04-01 15:46:36 +00:00
|
|
|
virSecretObjPtr obj;
|
2016-02-27 11:49:12 +00:00
|
|
|
|
|
|
|
if (virSecretObjInitialize() < 0)
|
|
|
|
return NULL;
|
|
|
|
|
2017-04-01 15:46:36 +00:00
|
|
|
if (!(obj = virObjectLockableNew(virSecretObjClass)))
|
2016-02-27 11:49:12 +00:00
|
|
|
return NULL;
|
|
|
|
|
2017-04-01 15:46:36 +00:00
|
|
|
virObjectLock(obj);
|
2017-04-20 15:22:21 +00:00
|
|
|
|
2017-04-01 15:46:36 +00:00
|
|
|
return obj;
|
2016-02-27 11:49:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2017-04-01 15:46:36 +00:00
|
|
|
virSecretObjEndAPI(virSecretObjPtr *obj)
|
2016-02-27 11:49:12 +00:00
|
|
|
{
|
2017-04-01 15:46:36 +00:00
|
|
|
if (!*obj)
|
2016-02-27 11:49:12 +00:00
|
|
|
return;
|
|
|
|
|
2017-04-01 15:46:36 +00:00
|
|
|
virObjectUnlock(*obj);
|
|
|
|
virObjectUnref(*obj);
|
|
|
|
*obj = NULL;
|
2016-02-27 11:49:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
virSecretObjListPtr
|
|
|
|
virSecretObjListNew(void)
|
|
|
|
{
|
|
|
|
virSecretObjListPtr secrets;
|
|
|
|
|
|
|
|
if (virSecretObjInitialize() < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!(secrets = virObjectLockableNew(virSecretObjListClass)))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!(secrets->objs = virHashCreate(50, virObjectFreeHashData))) {
|
|
|
|
virObjectUnref(secrets);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return secrets;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2017-04-01 15:46:36 +00:00
|
|
|
virSecretObjDispose(void *opaque)
|
2016-02-27 11:49:12 +00:00
|
|
|
{
|
2017-04-01 15:46:36 +00:00
|
|
|
virSecretObjPtr obj = opaque;
|
2016-02-27 11:49:12 +00:00
|
|
|
|
2017-04-01 15:46:36 +00:00
|
|
|
virSecretDefFree(obj->def);
|
|
|
|
if (obj->value) {
|
2016-02-27 11:49:12 +00:00
|
|
|
/* Wipe before free to ensure we don't leave a secret on the heap */
|
2017-04-01 15:46:36 +00:00
|
|
|
memset(obj->value, 0, obj->value_size);
|
|
|
|
VIR_FREE(obj->value);
|
2016-02-27 11:49:12 +00:00
|
|
|
}
|
2017-04-01 15:46:36 +00:00
|
|
|
VIR_FREE(obj->configFile);
|
|
|
|
VIR_FREE(obj->base64File);
|
2016-02-27 11:49:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
virSecretObjListDispose(void *obj)
|
|
|
|
{
|
|
|
|
virSecretObjListPtr secrets = obj;
|
|
|
|
|
|
|
|
virHashFree(secrets->objs);
|
|
|
|
}
|
2016-04-19 19:13:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virSecretObjFindByUUIDLocked:
|
|
|
|
* @secrets: list of secret objects
|
|
|
|
* @uuid: secret uuid to find
|
|
|
|
*
|
|
|
|
* This functions requires @secrets to be locked already!
|
|
|
|
*
|
|
|
|
* Returns: not locked, but ref'd secret object.
|
|
|
|
*/
|
2017-04-05 20:58:32 +00:00
|
|
|
static virSecretObjPtr
|
2016-04-19 19:13:00 +00:00
|
|
|
virSecretObjListFindByUUIDLocked(virSecretObjListPtr secrets,
|
|
|
|
const unsigned char *uuid)
|
|
|
|
{
|
|
|
|
char uuidstr[VIR_UUID_STRING_BUFLEN];
|
|
|
|
|
|
|
|
virUUIDFormat(uuid, uuidstr);
|
|
|
|
|
|
|
|
return virObjectRef(virHashLookup(secrets->objs, uuidstr));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virSecretObjFindByUUID:
|
|
|
|
* @secrets: list of secret objects
|
|
|
|
* @uuid: secret uuid to find
|
|
|
|
*
|
|
|
|
* This function locks @secrets and finds the secret object which
|
|
|
|
* corresponds to @uuid.
|
|
|
|
*
|
|
|
|
* Returns: locked and ref'd secret object.
|
|
|
|
*/
|
|
|
|
virSecretObjPtr
|
|
|
|
virSecretObjListFindByUUID(virSecretObjListPtr secrets,
|
|
|
|
const unsigned char *uuid)
|
|
|
|
{
|
2017-04-01 15:46:36 +00:00
|
|
|
virSecretObjPtr obj;
|
2016-04-19 19:13:00 +00:00
|
|
|
|
|
|
|
virObjectLock(secrets);
|
2017-04-01 15:46:36 +00:00
|
|
|
obj = virSecretObjListFindByUUIDLocked(secrets, uuid);
|
2016-04-19 19:13:00 +00:00
|
|
|
virObjectUnlock(secrets);
|
2017-04-01 15:46:36 +00:00
|
|
|
if (obj)
|
|
|
|
virObjectLock(obj);
|
|
|
|
return obj;
|
2016-04-19 19:13:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
virSecretObjSearchName(const void *payload,
|
|
|
|
const void *name ATTRIBUTE_UNUSED,
|
|
|
|
const void *opaque)
|
|
|
|
{
|
2017-04-01 15:46:36 +00:00
|
|
|
virSecretObjPtr obj = (virSecretObjPtr) payload;
|
2017-04-03 12:11:32 +00:00
|
|
|
virSecretDefPtr def;
|
2016-04-19 19:13:00 +00:00
|
|
|
struct virSecretSearchData *data = (struct virSecretSearchData *) opaque;
|
|
|
|
int found = 0;
|
|
|
|
|
2017-04-01 15:46:36 +00:00
|
|
|
virObjectLock(obj);
|
2017-04-03 12:11:32 +00:00
|
|
|
def = obj->def;
|
2016-04-19 19:13:00 +00:00
|
|
|
|
2017-04-03 12:11:32 +00:00
|
|
|
if (def->usage_type != data->usageType)
|
2016-04-19 19:13:00 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
2016-12-22 15:40:11 +00:00
|
|
|
if (data->usageType != VIR_SECRET_USAGE_TYPE_NONE &&
|
2017-04-03 12:11:32 +00:00
|
|
|
STREQ(def->usage_id, data->usageID))
|
2016-12-22 15:40:11 +00:00
|
|
|
found = 1;
|
2016-04-19 19:13:00 +00:00
|
|
|
|
|
|
|
cleanup:
|
2017-04-01 15:46:36 +00:00
|
|
|
virObjectUnlock(obj);
|
2016-04-19 19:13:00 +00:00
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virSecretObjFindByUsageLocked:
|
|
|
|
* @secrets: list of secret objects
|
|
|
|
* @usageType: secret usageType to find
|
|
|
|
* @usageID: secret usage string
|
|
|
|
*
|
|
|
|
* This functions requires @secrets to be locked already!
|
|
|
|
*
|
|
|
|
* Returns: not locked, but ref'd secret object.
|
|
|
|
*/
|
2017-04-05 20:58:32 +00:00
|
|
|
static virSecretObjPtr
|
2016-04-19 19:13:00 +00:00
|
|
|
virSecretObjListFindByUsageLocked(virSecretObjListPtr secrets,
|
|
|
|
int usageType,
|
|
|
|
const char *usageID)
|
|
|
|
{
|
2017-04-01 15:46:36 +00:00
|
|
|
virSecretObjPtr obj = NULL;
|
2016-04-19 19:13:00 +00:00
|
|
|
struct virSecretSearchData data = { .usageType = usageType,
|
|
|
|
.usageID = usageID };
|
|
|
|
|
2017-04-01 15:46:36 +00:00
|
|
|
obj = virHashSearch(secrets->objs, virSecretObjSearchName, &data);
|
|
|
|
if (obj)
|
|
|
|
virObjectRef(obj);
|
|
|
|
return obj;
|
2016-04-19 19:13:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virSecretObjFindByUsage:
|
|
|
|
* @secrets: list of secret objects
|
|
|
|
* @usageType: secret usageType to find
|
|
|
|
* @usageID: secret usage string
|
|
|
|
*
|
|
|
|
* This function locks @secrets and finds the secret object which
|
|
|
|
* corresponds to @usageID of @usageType.
|
|
|
|
*
|
|
|
|
* Returns: locked and ref'd secret object.
|
|
|
|
*/
|
|
|
|
virSecretObjPtr
|
|
|
|
virSecretObjListFindByUsage(virSecretObjListPtr secrets,
|
|
|
|
int usageType,
|
|
|
|
const char *usageID)
|
|
|
|
{
|
2017-04-01 15:46:36 +00:00
|
|
|
virSecretObjPtr obj;
|
2016-04-19 19:13:00 +00:00
|
|
|
|
|
|
|
virObjectLock(secrets);
|
2017-04-01 15:46:36 +00:00
|
|
|
obj = virSecretObjListFindByUsageLocked(secrets, usageType, usageID);
|
2016-04-19 19:13:00 +00:00
|
|
|
virObjectUnlock(secrets);
|
2017-04-01 15:46:36 +00:00
|
|
|
if (obj)
|
|
|
|
virObjectLock(obj);
|
|
|
|
return obj;
|
2016-04-19 19:13:00 +00:00
|
|
|
}
|
2016-04-19 20:05:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* virSecretObjListRemove:
|
|
|
|
* @secrets: list of secret objects
|
|
|
|
* @secret: a secret object
|
|
|
|
*
|
|
|
|
* Remove the object from the hash table. The caller must hold the lock
|
|
|
|
* on the driver owning @secrets and must have also locked @secret to
|
|
|
|
* ensure no one else is either waiting for @secret or still using it.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
virSecretObjListRemove(virSecretObjListPtr secrets,
|
2017-04-01 15:46:36 +00:00
|
|
|
virSecretObjPtr obj)
|
2016-04-19 20:05:38 +00:00
|
|
|
{
|
|
|
|
char uuidstr[VIR_UUID_STRING_BUFLEN];
|
2017-04-03 12:11:32 +00:00
|
|
|
virSecretDefPtr def;
|
2016-04-19 20:05:38 +00:00
|
|
|
|
2017-04-25 13:17:22 +00:00
|
|
|
if (!obj)
|
|
|
|
return;
|
2017-04-03 12:11:32 +00:00
|
|
|
def = obj->def;
|
2017-04-25 13:17:22 +00:00
|
|
|
|
2017-04-03 12:11:32 +00:00
|
|
|
virUUIDFormat(def->uuid, uuidstr);
|
2017-04-01 15:46:36 +00:00
|
|
|
virObjectRef(obj);
|
|
|
|
virObjectUnlock(obj);
|
2016-04-19 20:05:38 +00:00
|
|
|
|
|
|
|
virObjectLock(secrets);
|
2017-04-01 15:46:36 +00:00
|
|
|
virObjectLock(obj);
|
2016-04-19 20:05:38 +00:00
|
|
|
virHashRemoveEntry(secrets->objs, uuidstr);
|
2017-04-01 15:46:36 +00:00
|
|
|
virObjectUnlock(obj);
|
|
|
|
virObjectUnref(obj);
|
2016-04-19 20:05:38 +00:00
|
|
|
virObjectUnlock(secrets);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* virSecretObjListAddLocked:
|
|
|
|
* @secrets: list of secret objects
|
2017-04-01 15:46:36 +00:00
|
|
|
* @newdef: new secret definition
|
2016-04-19 20:05:38 +00:00
|
|
|
* @configDir: directory to place secret config files
|
|
|
|
* @oldDef: Former secret def (e.g. a reload path perhaps)
|
|
|
|
*
|
2017-04-01 15:46:36 +00:00
|
|
|
* Add the new @newdef to the secret obj table hash
|
2016-04-19 20:05:38 +00:00
|
|
|
*
|
|
|
|
* This functions requires @secrets to be locked already!
|
|
|
|
*
|
|
|
|
* Returns pointer to secret or NULL if failure to add
|
|
|
|
*/
|
2017-04-05 20:58:32 +00:00
|
|
|
static virSecretObjPtr
|
2016-04-19 20:05:38 +00:00
|
|
|
virSecretObjListAddLocked(virSecretObjListPtr secrets,
|
2017-04-01 15:46:36 +00:00
|
|
|
virSecretDefPtr newdef,
|
2016-04-19 20:05:38 +00:00
|
|
|
const char *configDir,
|
|
|
|
virSecretDefPtr *oldDef)
|
|
|
|
{
|
2017-04-01 15:46:36 +00:00
|
|
|
virSecretObjPtr obj;
|
2017-04-03 12:11:32 +00:00
|
|
|
virSecretDefPtr def;
|
2016-04-19 20:05:38 +00:00
|
|
|
virSecretObjPtr ret = NULL;
|
|
|
|
char uuidstr[VIR_UUID_STRING_BUFLEN];
|
|
|
|
char *configFile = NULL, *base64File = NULL;
|
|
|
|
|
|
|
|
if (oldDef)
|
|
|
|
*oldDef = NULL;
|
|
|
|
|
|
|
|
/* Is there a secret already matching this UUID */
|
2017-04-01 15:46:36 +00:00
|
|
|
if ((obj = virSecretObjListFindByUUIDLocked(secrets, newdef->uuid))) {
|
|
|
|
virObjectLock(obj);
|
2017-04-03 12:11:32 +00:00
|
|
|
def = obj->def;
|
2016-04-19 20:05:38 +00:00
|
|
|
|
2017-04-03 12:11:32 +00:00
|
|
|
if (STRNEQ_NULLABLE(def->usage_id, newdef->usage_id)) {
|
|
|
|
virUUIDFormat(def->uuid, uuidstr);
|
2016-04-19 20:05:38 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("a secret with UUID %s is already defined for "
|
|
|
|
"use with %s"),
|
2017-04-03 12:11:32 +00:00
|
|
|
uuidstr, def->usage_id);
|
2016-04-19 20:05:38 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2017-04-03 12:11:32 +00:00
|
|
|
if (def->isprivate && !newdef->isprivate) {
|
2016-04-19 20:05:38 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("cannot change private flag on existing secret"));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (oldDef)
|
2017-04-03 12:11:32 +00:00
|
|
|
*oldDef = def;
|
2016-04-19 20:05:38 +00:00
|
|
|
else
|
2017-04-03 12:11:32 +00:00
|
|
|
virSecretDefFree(def);
|
2017-04-01 15:46:36 +00:00
|
|
|
obj->def = newdef;
|
2016-04-19 20:05:38 +00:00
|
|
|
} else {
|
|
|
|
/* No existing secret with same UUID,
|
|
|
|
* try look for matching usage instead */
|
2017-04-01 15:46:36 +00:00
|
|
|
if ((obj = virSecretObjListFindByUsageLocked(secrets,
|
|
|
|
newdef->usage_type,
|
|
|
|
newdef->usage_id))) {
|
|
|
|
virObjectLock(obj);
|
2017-04-03 12:11:32 +00:00
|
|
|
def = obj->def;
|
|
|
|
virUUIDFormat(def->uuid, uuidstr);
|
2016-04-19 20:05:38 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("a secret with UUID %s already defined for "
|
|
|
|
"use with %s"),
|
2017-04-01 15:46:36 +00:00
|
|
|
uuidstr, newdef->usage_id);
|
2016-04-19 20:05:38 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Generate the possible configFile and base64File strings
|
|
|
|
* using the configDir, uuidstr, and appropriate suffix
|
|
|
|
*/
|
2017-04-01 15:46:36 +00:00
|
|
|
virUUIDFormat(newdef->uuid, uuidstr);
|
2016-04-19 20:05:38 +00:00
|
|
|
if (!(configFile = virFileBuildPath(configDir, uuidstr, ".xml")) ||
|
|
|
|
!(base64File = virFileBuildPath(configDir, uuidstr, ".base64")))
|
|
|
|
goto cleanup;
|
|
|
|
|
2017-04-01 15:46:36 +00:00
|
|
|
if (!(obj = virSecretObjNew()))
|
2016-04-19 20:05:38 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
2017-04-01 15:46:36 +00:00
|
|
|
if (virHashAddEntry(secrets->objs, uuidstr, obj) < 0)
|
2016-04-19 20:05:38 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
2017-04-01 15:46:36 +00:00
|
|
|
obj->def = newdef;
|
|
|
|
VIR_STEAL_PTR(obj->configFile, configFile);
|
|
|
|
VIR_STEAL_PTR(obj->base64File, base64File);
|
|
|
|
virObjectRef(obj);
|
2016-04-19 20:05:38 +00:00
|
|
|
}
|
|
|
|
|
2017-04-01 15:46:36 +00:00
|
|
|
ret = obj;
|
|
|
|
obj = NULL;
|
2016-04-19 20:05:38 +00:00
|
|
|
|
|
|
|
cleanup:
|
2017-04-01 15:46:36 +00:00
|
|
|
virSecretObjEndAPI(&obj);
|
2016-04-19 20:05:38 +00:00
|
|
|
VIR_FREE(configFile);
|
|
|
|
VIR_FREE(base64File);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
virSecretObjPtr
|
|
|
|
virSecretObjListAdd(virSecretObjListPtr secrets,
|
2017-04-01 15:46:36 +00:00
|
|
|
virSecretDefPtr newdef,
|
2016-04-19 20:05:38 +00:00
|
|
|
const char *configDir,
|
|
|
|
virSecretDefPtr *oldDef)
|
|
|
|
{
|
2017-04-01 15:46:36 +00:00
|
|
|
virSecretObjPtr obj;
|
2016-04-19 20:05:38 +00:00
|
|
|
|
|
|
|
virObjectLock(secrets);
|
2017-04-01 15:46:36 +00:00
|
|
|
obj = virSecretObjListAddLocked(secrets, newdef, configDir, oldDef);
|
2016-04-19 20:05:38 +00:00
|
|
|
virObjectUnlock(secrets);
|
2017-04-01 15:46:36 +00:00
|
|
|
return obj;
|
2016-04-19 20:05:38 +00:00
|
|
|
}
|
2016-02-28 14:34:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
struct virSecretObjListGetHelperData {
|
|
|
|
virConnectPtr conn;
|
|
|
|
virSecretObjListACLFilter filter;
|
|
|
|
int got;
|
2016-02-28 15:56:17 +00:00
|
|
|
char **uuids;
|
|
|
|
int nuuids;
|
|
|
|
bool error;
|
2016-02-28 14:34:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
virSecretObjListGetHelper(void *payload,
|
|
|
|
const void *name ATTRIBUTE_UNUSED,
|
|
|
|
void *opaque)
|
|
|
|
{
|
|
|
|
struct virSecretObjListGetHelperData *data = opaque;
|
|
|
|
virSecretObjPtr obj = payload;
|
2017-04-03 12:11:32 +00:00
|
|
|
virSecretDefPtr def;
|
2016-02-28 14:34:21 +00:00
|
|
|
|
2016-02-28 15:56:17 +00:00
|
|
|
if (data->error)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (data->nuuids >= 0 && data->got == data->nuuids)
|
|
|
|
return 0;
|
|
|
|
|
2016-02-28 14:34:21 +00:00
|
|
|
virObjectLock(obj);
|
2017-04-03 12:11:32 +00:00
|
|
|
def = obj->def;
|
2016-02-28 14:34:21 +00:00
|
|
|
|
2017-04-03 12:11:32 +00:00
|
|
|
if (data->filter && !data->filter(data->conn, def))
|
2016-02-28 14:34:21 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
2016-02-28 15:56:17 +00:00
|
|
|
if (data->uuids) {
|
|
|
|
char *uuidstr;
|
|
|
|
|
2017-04-14 16:35:26 +00:00
|
|
|
if (VIR_ALLOC_N(uuidstr, VIR_UUID_STRING_BUFLEN) < 0) {
|
|
|
|
data->error = true;
|
2016-02-28 15:56:17 +00:00
|
|
|
goto cleanup;
|
2017-04-14 16:35:26 +00:00
|
|
|
}
|
2016-02-28 15:56:17 +00:00
|
|
|
|
2017-04-03 12:11:32 +00:00
|
|
|
virUUIDFormat(def->uuid, uuidstr);
|
2016-02-28 15:56:17 +00:00
|
|
|
data->uuids[data->got] = uuidstr;
|
|
|
|
}
|
|
|
|
|
2016-02-28 14:34:21 +00:00
|
|
|
data->got++;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
virObjectUnlock(obj);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
virSecretObjListNumOfSecrets(virSecretObjListPtr secrets,
|
|
|
|
virSecretObjListACLFilter filter,
|
|
|
|
virConnectPtr conn)
|
|
|
|
{
|
|
|
|
struct virSecretObjListGetHelperData data = {
|
2016-02-28 15:56:17 +00:00
|
|
|
.conn = conn, .filter = filter, .got = 0,
|
|
|
|
.uuids = NULL, .nuuids = -1, .error = false };
|
2016-02-28 14:34:21 +00:00
|
|
|
|
|
|
|
virObjectLock(secrets);
|
|
|
|
virHashForEach(secrets->objs, virSecretObjListGetHelper, &data);
|
|
|
|
virObjectUnlock(secrets);
|
|
|
|
|
|
|
|
return data.got;
|
|
|
|
}
|
2016-02-29 11:52:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
#define MATCH(FLAG) (flags & (FLAG))
|
|
|
|
static bool
|
2017-04-01 15:46:36 +00:00
|
|
|
virSecretObjMatchFlags(virSecretObjPtr obj,
|
2016-02-29 11:52:00 +00:00
|
|
|
unsigned int flags)
|
|
|
|
{
|
2017-04-03 12:11:32 +00:00
|
|
|
virSecretDefPtr def = obj->def;
|
|
|
|
|
2016-02-29 11:52:00 +00:00
|
|
|
/* filter by whether it's ephemeral */
|
|
|
|
if (MATCH(VIR_CONNECT_LIST_SECRETS_FILTERS_EPHEMERAL) &&
|
|
|
|
!((MATCH(VIR_CONNECT_LIST_SECRETS_EPHEMERAL) &&
|
2017-04-03 12:11:32 +00:00
|
|
|
def->isephemeral) ||
|
2016-02-29 11:52:00 +00:00
|
|
|
(MATCH(VIR_CONNECT_LIST_SECRETS_NO_EPHEMERAL) &&
|
2017-04-03 12:11:32 +00:00
|
|
|
!def->isephemeral)))
|
2016-02-29 11:52:00 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
/* filter by whether it's private */
|
|
|
|
if (MATCH(VIR_CONNECT_LIST_SECRETS_FILTERS_PRIVATE) &&
|
|
|
|
!((MATCH(VIR_CONNECT_LIST_SECRETS_PRIVATE) &&
|
2017-04-03 12:11:32 +00:00
|
|
|
def->isprivate) ||
|
2016-02-29 11:52:00 +00:00
|
|
|
(MATCH(VIR_CONNECT_LIST_SECRETS_NO_PRIVATE) &&
|
2017-04-03 12:11:32 +00:00
|
|
|
!def->isprivate)))
|
2016-02-29 11:52:00 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#undef MATCH
|
|
|
|
|
|
|
|
|
|
|
|
struct virSecretObjListData {
|
|
|
|
virConnectPtr conn;
|
|
|
|
virSecretPtr *secrets;
|
|
|
|
virSecretObjListACLFilter filter;
|
|
|
|
unsigned int flags;
|
|
|
|
int nsecrets;
|
|
|
|
bool error;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
2017-04-24 16:50:12 +00:00
|
|
|
virSecretObjListExportCallback(void *payload,
|
|
|
|
const void *name ATTRIBUTE_UNUSED,
|
|
|
|
void *opaque)
|
2016-02-29 11:52:00 +00:00
|
|
|
{
|
|
|
|
struct virSecretObjListData *data = opaque;
|
|
|
|
virSecretObjPtr obj = payload;
|
2017-04-03 12:11:32 +00:00
|
|
|
virSecretDefPtr def;
|
2016-02-29 11:52:00 +00:00
|
|
|
virSecretPtr secret = NULL;
|
|
|
|
|
|
|
|
if (data->error)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
virObjectLock(obj);
|
2017-04-03 12:11:32 +00:00
|
|
|
def = obj->def;
|
2016-02-29 11:52:00 +00:00
|
|
|
|
2017-04-03 12:11:32 +00:00
|
|
|
if (data->filter && !data->filter(data->conn, def))
|
2016-02-29 11:52:00 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (!virSecretObjMatchFlags(obj, data->flags))
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (!data->secrets) {
|
|
|
|
data->nsecrets++;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2017-04-03 12:11:32 +00:00
|
|
|
if (!(secret = virGetSecret(data->conn, def->uuid,
|
|
|
|
def->usage_type,
|
|
|
|
def->usage_id))) {
|
2016-02-29 11:52:00 +00:00
|
|
|
data->error = true;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
data->secrets[data->nsecrets++] = secret;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
virObjectUnlock(obj);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
virSecretObjListExport(virConnectPtr conn,
|
|
|
|
virSecretObjListPtr secretobjs,
|
|
|
|
virSecretPtr **secrets,
|
|
|
|
virSecretObjListACLFilter filter,
|
|
|
|
unsigned int flags)
|
|
|
|
{
|
|
|
|
struct virSecretObjListData data = {
|
|
|
|
.conn = conn, .secrets = NULL,
|
|
|
|
.filter = filter, .flags = flags,
|
|
|
|
.nsecrets = 0, .error = false };
|
|
|
|
|
|
|
|
virObjectLock(secretobjs);
|
|
|
|
if (secrets &&
|
2017-04-24 16:50:12 +00:00
|
|
|
VIR_ALLOC_N(data.secrets, virHashSize(secretobjs->objs) + 1) < 0) {
|
|
|
|
virObjectUnlock(secretobjs);
|
|
|
|
return -1;
|
|
|
|
}
|
2016-02-29 11:52:00 +00:00
|
|
|
|
2017-04-24 16:50:12 +00:00
|
|
|
virHashForEach(secretobjs->objs, virSecretObjListExportCallback, &data);
|
|
|
|
virObjectUnlock(secretobjs);
|
2016-02-29 11:52:00 +00:00
|
|
|
|
|
|
|
if (data.error)
|
2017-04-24 16:50:12 +00:00
|
|
|
goto error;
|
2016-02-29 11:52:00 +00:00
|
|
|
|
|
|
|
if (data.secrets) {
|
|
|
|
/* trim the array to the final size */
|
|
|
|
ignore_value(VIR_REALLOC_N(data.secrets, data.nsecrets + 1));
|
|
|
|
*secrets = data.secrets;
|
|
|
|
}
|
|
|
|
|
2017-04-24 16:50:12 +00:00
|
|
|
return data.nsecrets;
|
2016-02-29 11:52:00 +00:00
|
|
|
|
2017-04-24 16:50:12 +00:00
|
|
|
error:
|
|
|
|
virObjectListFree(data.secrets);
|
|
|
|
return -1;
|
2016-02-29 11:52:00 +00:00
|
|
|
}
|
2016-02-28 15:56:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
virSecretObjListGetUUIDs(virSecretObjListPtr secrets,
|
|
|
|
char **uuids,
|
|
|
|
int nuuids,
|
|
|
|
virSecretObjListACLFilter filter,
|
|
|
|
virConnectPtr conn)
|
|
|
|
{
|
|
|
|
struct virSecretObjListGetHelperData data = {
|
|
|
|
.conn = conn, .filter = filter, .got = 0,
|
|
|
|
.uuids = uuids, .nuuids = nuuids, .error = false };
|
|
|
|
|
|
|
|
virObjectLock(secrets);
|
|
|
|
virHashForEach(secrets->objs, virSecretObjListGetHelper, &data);
|
|
|
|
virObjectUnlock(secrets);
|
|
|
|
|
|
|
|
if (data.error)
|
2017-04-25 13:51:29 +00:00
|
|
|
goto error;
|
2016-02-28 15:56:17 +00:00
|
|
|
|
2017-04-25 13:51:29 +00:00
|
|
|
return data.got;
|
2016-02-28 15:56:17 +00:00
|
|
|
|
2017-04-25 13:51:29 +00:00
|
|
|
error:
|
|
|
|
while (data.got)
|
|
|
|
VIR_FREE(data.uuids[--data.got]);
|
|
|
|
return -1;
|
2016-02-28 15:56:17 +00:00
|
|
|
}
|
2016-03-02 18:10:54 +00:00
|
|
|
|
|
|
|
|
2016-03-07 23:06:57 +00:00
|
|
|
int
|
2017-04-01 15:46:36 +00:00
|
|
|
virSecretObjDeleteConfig(virSecretObjPtr obj)
|
2016-03-07 23:06:57 +00:00
|
|
|
{
|
2017-04-03 12:11:32 +00:00
|
|
|
virSecretDefPtr def = obj->def;
|
|
|
|
|
|
|
|
if (!def->isephemeral &&
|
2017-04-01 15:46:36 +00:00
|
|
|
unlink(obj->configFile) < 0 && errno != ENOENT) {
|
2016-03-07 23:06:57 +00:00
|
|
|
virReportSystemError(errno, _("cannot unlink '%s'"),
|
2017-04-01 15:46:36 +00:00
|
|
|
obj->configFile);
|
2016-03-07 23:06:57 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2017-04-01 15:46:36 +00:00
|
|
|
virSecretObjDeleteData(virSecretObjPtr obj)
|
2016-03-07 23:06:57 +00:00
|
|
|
{
|
|
|
|
/* The configFile will already be removed, so secret won't be
|
|
|
|
* loaded again if this fails */
|
2017-04-01 15:46:36 +00:00
|
|
|
(void)unlink(obj->base64File);
|
2016-03-07 23:06:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-18 14:17:25 +00:00
|
|
|
/* Permanent secret storage */
|
|
|
|
|
|
|
|
/* Secrets are stored in virSecretDriverStatePtr->configDir. Each secret
|
|
|
|
has virSecretDef stored as XML in "$basename.xml". If a value of the
|
|
|
|
secret is defined, it is stored as base64 (with no formatting) in
|
|
|
|
"$basename.base64". "$basename" is in both cases the base64-encoded UUID. */
|
|
|
|
int
|
2017-04-01 15:46:36 +00:00
|
|
|
virSecretObjSaveConfig(virSecretObjPtr obj)
|
2016-03-18 14:17:25 +00:00
|
|
|
{
|
|
|
|
char *xml = NULL;
|
|
|
|
int ret = -1;
|
|
|
|
|
2017-04-01 15:46:36 +00:00
|
|
|
if (!(xml = virSecretDefFormat(obj->def)))
|
2016-03-18 14:17:25 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
2017-04-01 15:46:36 +00:00
|
|
|
if (virFileRewriteStr(obj->configFile, S_IRUSR | S_IWUSR, xml) < 0)
|
2016-03-18 14:17:25 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
VIR_FREE(xml);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2017-04-01 15:46:36 +00:00
|
|
|
virSecretObjSaveData(virSecretObjPtr obj)
|
2016-03-18 14:17:25 +00:00
|
|
|
{
|
|
|
|
char *base64 = NULL;
|
|
|
|
int ret = -1;
|
|
|
|
|
2017-04-01 15:46:36 +00:00
|
|
|
if (!obj->value)
|
2016-03-18 14:17:25 +00:00
|
|
|
return 0;
|
|
|
|
|
2017-04-01 15:46:36 +00:00
|
|
|
if (!(base64 = virStringEncodeBase64(obj->value, obj->value_size)))
|
2016-03-18 14:17:25 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
2017-04-01 15:46:36 +00:00
|
|
|
if (virFileRewriteStr(obj->base64File, S_IRUSR | S_IWUSR, base64) < 0)
|
2016-03-18 14:17:25 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
VIR_FREE(base64);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-08 00:22:04 +00:00
|
|
|
virSecretDefPtr
|
2017-04-01 15:46:36 +00:00
|
|
|
virSecretObjGetDef(virSecretObjPtr obj)
|
2016-03-08 00:22:04 +00:00
|
|
|
{
|
2017-04-01 15:46:36 +00:00
|
|
|
return obj->def;
|
2016-03-08 00:22:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2017-04-01 15:46:36 +00:00
|
|
|
virSecretObjSetDef(virSecretObjPtr obj,
|
2016-03-08 00:22:04 +00:00
|
|
|
virSecretDefPtr def)
|
|
|
|
{
|
2017-04-01 15:46:36 +00:00
|
|
|
obj->def = def;
|
2016-03-08 00:22:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-08 00:40:58 +00:00
|
|
|
unsigned char *
|
2017-04-01 15:46:36 +00:00
|
|
|
virSecretObjGetValue(virSecretObjPtr obj)
|
2016-03-08 00:40:58 +00:00
|
|
|
{
|
2017-04-03 12:11:32 +00:00
|
|
|
virSecretDefPtr def = obj->def;
|
2016-03-08 00:40:58 +00:00
|
|
|
unsigned char *ret = NULL;
|
|
|
|
|
2017-04-01 15:46:36 +00:00
|
|
|
if (!obj->value) {
|
2016-03-08 00:40:58 +00:00
|
|
|
char uuidstr[VIR_UUID_STRING_BUFLEN];
|
2017-04-03 12:11:32 +00:00
|
|
|
virUUIDFormat(def->uuid, uuidstr);
|
2016-03-08 00:40:58 +00:00
|
|
|
virReportError(VIR_ERR_NO_SECRET,
|
|
|
|
_("secret '%s' does not have a value"), uuidstr);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2017-04-01 15:46:36 +00:00
|
|
|
if (VIR_ALLOC_N(ret, obj->value_size) < 0)
|
2016-03-08 00:40:58 +00:00
|
|
|
goto cleanup;
|
2017-04-01 15:46:36 +00:00
|
|
|
memcpy(ret, obj->value, obj->value_size);
|
2016-03-08 00:40:58 +00:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2017-04-01 15:46:36 +00:00
|
|
|
virSecretObjSetValue(virSecretObjPtr obj,
|
2016-03-08 00:40:58 +00:00
|
|
|
const unsigned char *value,
|
|
|
|
size_t value_size)
|
|
|
|
{
|
2017-04-03 12:11:32 +00:00
|
|
|
virSecretDefPtr def = obj->def;
|
2016-03-08 00:40:58 +00:00
|
|
|
unsigned char *old_value, *new_value;
|
|
|
|
size_t old_value_size;
|
|
|
|
|
|
|
|
if (VIR_ALLOC_N(new_value, value_size) < 0)
|
|
|
|
return -1;
|
|
|
|
|
2017-04-01 15:46:36 +00:00
|
|
|
old_value = obj->value;
|
|
|
|
old_value_size = obj->value_size;
|
2016-03-08 00:40:58 +00:00
|
|
|
|
|
|
|
memcpy(new_value, value, value_size);
|
2017-04-01 15:46:36 +00:00
|
|
|
obj->value = new_value;
|
|
|
|
obj->value_size = value_size;
|
2016-03-08 00:40:58 +00:00
|
|
|
|
2017-04-03 12:11:32 +00:00
|
|
|
if (!def->isephemeral && virSecretObjSaveData(obj) < 0)
|
2016-03-08 00:40:58 +00:00
|
|
|
goto error;
|
|
|
|
|
|
|
|
/* Saved successfully - drop old value */
|
|
|
|
if (old_value) {
|
|
|
|
memset(old_value, 0, old_value_size);
|
|
|
|
VIR_FREE(old_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
error:
|
|
|
|
/* Error - restore previous state and free new value */
|
2017-04-01 15:46:36 +00:00
|
|
|
obj->value = old_value;
|
|
|
|
obj->value_size = old_value_size;
|
2016-03-08 00:40:58 +00:00
|
|
|
memset(new_value, 0, value_size);
|
|
|
|
VIR_FREE(new_value);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
size_t
|
2017-04-01 15:46:36 +00:00
|
|
|
virSecretObjGetValueSize(virSecretObjPtr obj)
|
2016-03-08 00:40:58 +00:00
|
|
|
{
|
2017-04-01 15:46:36 +00:00
|
|
|
return obj->value_size;
|
2016-03-08 00:40:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2017-04-01 15:46:36 +00:00
|
|
|
virSecretObjSetValueSize(virSecretObjPtr obj,
|
2016-03-08 00:40:58 +00:00
|
|
|
size_t value_size)
|
|
|
|
{
|
2017-04-01 15:46:36 +00:00
|
|
|
obj->value_size = value_size;
|
2016-03-08 00:40:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-02 18:10:54 +00:00
|
|
|
static int
|
|
|
|
virSecretLoadValidateUUID(virSecretDefPtr def,
|
|
|
|
const char *file)
|
|
|
|
{
|
|
|
|
char uuidstr[VIR_UUID_STRING_BUFLEN];
|
|
|
|
|
|
|
|
virUUIDFormat(def->uuid, uuidstr);
|
|
|
|
|
|
|
|
if (!virFileMatchesNameSuffix(file, uuidstr, ".xml")) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("<uuid> does not match secret file name '%s'"),
|
|
|
|
file);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2017-04-01 15:46:36 +00:00
|
|
|
virSecretLoadValue(virSecretObjPtr obj)
|
2016-03-02 18:10:54 +00:00
|
|
|
{
|
|
|
|
int ret = -1, fd = -1;
|
|
|
|
struct stat st;
|
|
|
|
char *contents = NULL, *value = NULL;
|
|
|
|
size_t value_size;
|
|
|
|
|
2017-04-01 15:46:36 +00:00
|
|
|
if ((fd = open(obj->base64File, O_RDONLY)) == -1) {
|
2016-03-02 18:10:54 +00:00
|
|
|
if (errno == ENOENT) {
|
|
|
|
ret = 0;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
virReportSystemError(errno, _("cannot open '%s'"),
|
2017-04-01 15:46:36 +00:00
|
|
|
obj->base64File);
|
2016-03-02 18:10:54 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fstat(fd, &st) < 0) {
|
|
|
|
virReportSystemError(errno, _("cannot stat '%s'"),
|
2017-04-01 15:46:36 +00:00
|
|
|
obj->base64File);
|
2016-03-02 18:10:54 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((size_t)st.st_size != st.st_size) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("'%s' file does not fit in memory"),
|
2017-04-01 15:46:36 +00:00
|
|
|
obj->base64File);
|
2016-03-02 18:10:54 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (VIR_ALLOC_N(contents, st.st_size) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (saferead(fd, contents, st.st_size) != st.st_size) {
|
|
|
|
virReportSystemError(errno, _("cannot read '%s'"),
|
2017-04-01 15:46:36 +00:00
|
|
|
obj->base64File);
|
2016-03-02 18:10:54 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
VIR_FORCE_CLOSE(fd);
|
|
|
|
|
|
|
|
if (!base64_decode_alloc(contents, st.st_size, &value, &value_size)) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("invalid base64 in '%s'"),
|
2017-04-01 15:46:36 +00:00
|
|
|
obj->base64File);
|
2016-03-02 18:10:54 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
if (value == NULL)
|
|
|
|
goto cleanup;
|
|
|
|
|
2017-04-01 15:46:36 +00:00
|
|
|
obj->value = (unsigned char *)value;
|
2016-03-02 18:10:54 +00:00
|
|
|
value = NULL;
|
2017-04-01 15:46:36 +00:00
|
|
|
obj->value_size = value_size;
|
2016-03-02 18:10:54 +00:00
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
if (value != NULL) {
|
|
|
|
memset(value, 0, value_size);
|
|
|
|
VIR_FREE(value);
|
|
|
|
}
|
|
|
|
if (contents != NULL) {
|
|
|
|
memset(contents, 0, st.st_size);
|
|
|
|
VIR_FREE(contents);
|
|
|
|
}
|
|
|
|
VIR_FORCE_CLOSE(fd);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static virSecretObjPtr
|
|
|
|
virSecretLoad(virSecretObjListPtr secrets,
|
|
|
|
const char *file,
|
|
|
|
const char *path,
|
|
|
|
const char *configDir)
|
|
|
|
{
|
|
|
|
virSecretDefPtr def = NULL;
|
2017-04-01 15:46:36 +00:00
|
|
|
virSecretObjPtr obj = NULL;
|
|
|
|
virSecretObjPtr ret = NULL;
|
2016-03-02 18:10:54 +00:00
|
|
|
|
|
|
|
if (!(def = virSecretDefParseFile(path)))
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (virSecretLoadValidateUUID(def, file) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
2017-04-01 15:46:36 +00:00
|
|
|
if (!(obj = virSecretObjListAdd(secrets, def, configDir, NULL)))
|
2016-03-02 18:10:54 +00:00
|
|
|
goto cleanup;
|
|
|
|
def = NULL;
|
|
|
|
|
2017-04-01 15:46:36 +00:00
|
|
|
if (virSecretLoadValue(obj) < 0)
|
2016-03-02 18:10:54 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
2017-04-01 15:46:36 +00:00
|
|
|
ret = obj;
|
|
|
|
obj = NULL;
|
2016-03-02 18:10:54 +00:00
|
|
|
|
|
|
|
cleanup:
|
2017-04-01 15:46:36 +00:00
|
|
|
virSecretObjListRemove(secrets, obj);
|
2016-03-02 18:10:54 +00:00
|
|
|
virSecretDefFree(def);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
virSecretLoadAllConfigs(virSecretObjListPtr secrets,
|
|
|
|
const char *configDir)
|
|
|
|
{
|
|
|
|
DIR *dir = NULL;
|
|
|
|
struct dirent *de;
|
2016-06-21 14:47:24 +00:00
|
|
|
int rc;
|
2016-03-02 18:10:54 +00:00
|
|
|
|
2016-06-21 14:47:24 +00:00
|
|
|
if ((rc = virDirOpenIfExists(&dir, configDir)) <= 0)
|
|
|
|
return rc;
|
2016-03-02 18:10:54 +00:00
|
|
|
|
|
|
|
/* Ignore errors reported by readdir or other calls within the
|
|
|
|
* loop (if any). It's better to keep the secrets we managed to find. */
|
|
|
|
while (virDirRead(dir, &de, NULL) > 0) {
|
|
|
|
char *path;
|
2017-04-01 15:46:36 +00:00
|
|
|
virSecretObjPtr obj;
|
2016-03-02 18:10:54 +00:00
|
|
|
|
|
|
|
if (!virFileHasSuffix(de->d_name, ".xml"))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!(path = virFileBuildPath(configDir, de->d_name, NULL)))
|
|
|
|
continue;
|
|
|
|
|
2017-04-01 15:46:36 +00:00
|
|
|
if (!(obj = virSecretLoad(secrets, de->d_name, path, configDir))) {
|
2016-03-02 18:10:54 +00:00
|
|
|
VIR_ERROR(_("Error reading secret: %s"),
|
2016-05-19 19:10:19 +00:00
|
|
|
virGetLastErrorMessage());
|
2016-03-02 18:10:54 +00:00
|
|
|
VIR_FREE(path);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
VIR_FREE(path);
|
2017-04-01 15:46:36 +00:00
|
|
|
virSecretObjEndAPI(&obj);
|
2016-03-02 18:10:54 +00:00
|
|
|
}
|
|
|
|
|
2016-06-21 10:40:29 +00:00
|
|
|
VIR_DIR_CLOSE(dir);
|
2016-03-02 18:10:54 +00:00
|
|
|
return 0;
|
|
|
|
}
|