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-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;
|
2021-03-11 07:16:13 +00:00
|
|
|
virSecretDef *def;
|
2016-03-08 00:40:58 +00:00
|
|
|
unsigned char *value; /* May be NULL */
|
|
|
|
size_t value_size;
|
|
|
|
};
|
|
|
|
|
2021-03-11 07:16:13 +00:00
|
|
|
static virClass *virSecretObjClass;
|
|
|
|
static virClass *virSecretObjListClass;
|
2016-02-27 11:49:12 +00:00
|
|
|
static void virSecretObjDispose(void *obj);
|
|
|
|
static void virSecretObjListDispose(void *obj);
|
|
|
|
|
|
|
|
struct _virSecretObjList {
|
2017-10-09 18:24:32 +00:00
|
|
|
virObjectRWLockable parent;
|
2016-02-27 11:49:12 +00:00
|
|
|
|
|
|
|
/* uuid string -> virSecretObj mapping
|
|
|
|
* for O(1), lockless lookup-by-uuid */
|
2020-10-22 17:04:18 +00:00
|
|
|
GHashTable *objs;
|
2016-02-27 11:49:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct virSecretSearchData {
|
|
|
|
int usageType;
|
|
|
|
const char *usageID;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
virSecretObjOnceInit(void)
|
|
|
|
{
|
2018-04-17 15:42:33 +00:00
|
|
|
if (!VIR_CLASS_NEW(virSecretObj, virClassForObjectLockable()))
|
2016-02-27 11:49:12 +00:00
|
|
|
return -1;
|
|
|
|
|
2018-04-17 15:42:33 +00:00
|
|
|
if (!VIR_CLASS_NEW(virSecretObjList, virClassForObjectRWLockable()))
|
2016-02-27 11:49:12 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-01-20 17:23:29 +00:00
|
|
|
VIR_ONCE_GLOBAL_INIT(virSecretObj);
|
2016-02-27 11:49:12 +00:00
|
|
|
|
2021-03-11 07:16:13 +00:00
|
|
|
static virSecretObj *
|
2016-02-27 11:49:12 +00:00
|
|
|
virSecretObjNew(void)
|
|
|
|
{
|
2021-03-11 07:16:13 +00:00
|
|
|
virSecretObj *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
|
2021-03-11 07:16:13 +00:00
|
|
|
virSecretObjEndAPI(virSecretObj **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
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-11 07:16:13 +00:00
|
|
|
virSecretObjList *
|
2016-02-27 11:49:12 +00:00
|
|
|
virSecretObjListNew(void)
|
|
|
|
{
|
2021-03-11 07:16:13 +00:00
|
|
|
virSecretObjList *secrets;
|
2016-02-27 11:49:12 +00:00
|
|
|
|
|
|
|
if (virSecretObjInitialize() < 0)
|
|
|
|
return NULL;
|
|
|
|
|
2017-10-09 18:24:32 +00:00
|
|
|
if (!(secrets = virObjectRWLockableNew(virSecretObjListClass)))
|
2016-02-27 11:49:12 +00:00
|
|
|
return NULL;
|
|
|
|
|
2020-10-20 16:43:20 +00:00
|
|
|
if (!(secrets->objs = virHashNew(virObjectFreeHashData))) {
|
2016-02-27 11:49:12 +00:00
|
|
|
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
|
|
|
{
|
2021-03-11 07:16:13 +00:00
|
|
|
virSecretObj *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);
|
2021-02-03 20:15:35 +00:00
|
|
|
g_free(obj->value);
|
2016-02-27 11:49:12 +00:00
|
|
|
}
|
2021-02-03 20:15:35 +00:00
|
|
|
g_free(obj->configFile);
|
|
|
|
g_free(obj->base64File);
|
2016-02-27 11:49:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
virSecretObjListDispose(void *obj)
|
|
|
|
{
|
2021-03-11 07:16:13 +00:00
|
|
|
virSecretObjList *secrets = obj;
|
2016-02-27 11:49:12 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
2021-03-11 07:16:13 +00:00
|
|
|
static virSecretObj *
|
|
|
|
virSecretObjListFindByUUIDLocked(virSecretObjList *secrets,
|
2017-04-21 15:43:37 +00:00
|
|
|
const char *uuidstr)
|
2016-04-19 19:13:00 +00:00
|
|
|
{
|
|
|
|
return virObjectRef(virHashLookup(secrets->objs, uuidstr));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virSecretObjFindByUUID:
|
|
|
|
* @secrets: list of secret objects
|
2017-04-21 15:43:37 +00:00
|
|
|
* @uuidstr: secret uuid to find
|
2016-04-19 19:13:00 +00:00
|
|
|
*
|
|
|
|
* This function locks @secrets and finds the secret object which
|
|
|
|
* corresponds to @uuid.
|
|
|
|
*
|
|
|
|
* Returns: locked and ref'd secret object.
|
|
|
|
*/
|
2021-03-11 07:16:13 +00:00
|
|
|
virSecretObj *
|
|
|
|
virSecretObjListFindByUUID(virSecretObjList *secrets,
|
2017-04-21 15:43:37 +00:00
|
|
|
const char *uuidstr)
|
2016-04-19 19:13:00 +00:00
|
|
|
{
|
2021-03-11 07:16:13 +00:00
|
|
|
virSecretObj *obj;
|
2016-04-19 19:13:00 +00:00
|
|
|
|
2017-10-09 18:24:32 +00:00
|
|
|
virObjectRWLockRead(secrets);
|
2017-04-21 15:43:37 +00:00
|
|
|
obj = virSecretObjListFindByUUIDLocked(secrets, uuidstr);
|
2017-10-09 18:24:32 +00:00
|
|
|
virObjectRWUnlock(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,
|
2020-10-21 11:31:16 +00:00
|
|
|
const char *name G_GNUC_UNUSED,
|
2016-04-19 19:13:00 +00:00
|
|
|
const void *opaque)
|
|
|
|
{
|
2021-03-11 07:16:13 +00:00
|
|
|
virSecretObj *obj = (virSecretObj *) payload;
|
|
|
|
virSecretDef *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.
|
|
|
|
*/
|
2021-03-11 07:16:13 +00:00
|
|
|
static virSecretObj *
|
|
|
|
virSecretObjListFindByUsageLocked(virSecretObjList *secrets,
|
2016-04-19 19:13:00 +00:00
|
|
|
int usageType,
|
|
|
|
const char *usageID)
|
|
|
|
{
|
2021-03-11 07:16:13 +00:00
|
|
|
virSecretObj *obj = NULL;
|
2016-04-19 19:13:00 +00:00
|
|
|
struct virSecretSearchData data = { .usageType = usageType,
|
|
|
|
.usageID = usageID };
|
|
|
|
|
2017-06-13 13:56:14 +00:00
|
|
|
obj = virHashSearch(secrets->objs, virSecretObjSearchName, &data, NULL);
|
2017-04-01 15:46:36 +00:00
|
|
|
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.
|
|
|
|
*/
|
2021-03-11 07:16:13 +00:00
|
|
|
virSecretObj *
|
|
|
|
virSecretObjListFindByUsage(virSecretObjList *secrets,
|
2016-04-19 19:13:00 +00:00
|
|
|
int usageType,
|
|
|
|
const char *usageID)
|
|
|
|
{
|
2021-03-11 07:16:13 +00:00
|
|
|
virSecretObj *obj;
|
2016-04-19 19:13:00 +00:00
|
|
|
|
2017-10-09 18:24:32 +00:00
|
|
|
virObjectRWLockRead(secrets);
|
2017-04-01 15:46:36 +00:00
|
|
|
obj = virSecretObjListFindByUsageLocked(secrets, usageType, usageID);
|
2017-10-09 18:24:32 +00:00
|
|
|
virObjectRWUnlock(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
|
2021-03-11 07:16:13 +00:00
|
|
|
virSecretObjListRemove(virSecretObjList *secrets,
|
|
|
|
virSecretObj *obj)
|
2016-04-19 20:05:38 +00:00
|
|
|
{
|
|
|
|
char uuidstr[VIR_UUID_STRING_BUFLEN];
|
2021-03-11 07:16:13 +00:00
|
|
|
virSecretDef *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
|
|
|
|
2017-10-09 18:24:32 +00:00
|
|
|
virObjectRWLockWrite(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);
|
2017-10-09 18:24:32 +00:00
|
|
|
virObjectRWUnlock(secrets);
|
2016-04-19 20:05:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2017-04-20 11:33:26 +00:00
|
|
|
* virSecretObjListAdd:
|
2016-04-19 20:05:38 +00:00
|
|
|
* @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
|
|
|
*
|
2017-04-20 11:33:26 +00:00
|
|
|
* Returns: locked and ref'd secret or NULL if failure to add
|
2016-04-19 20:05:38 +00:00
|
|
|
*/
|
2021-03-11 07:16:13 +00:00
|
|
|
virSecretObj *
|
|
|
|
virSecretObjListAdd(virSecretObjList *secrets,
|
|
|
|
virSecretDef *newdef,
|
2017-04-20 11:33:26 +00:00
|
|
|
const char *configDir,
|
2021-03-11 07:16:13 +00:00
|
|
|
virSecretDef **oldDef)
|
2016-04-19 20:05:38 +00:00
|
|
|
{
|
2021-03-11 07:16:13 +00:00
|
|
|
virSecretObj *obj;
|
|
|
|
virSecretDef *objdef;
|
|
|
|
virSecretObj *ret = NULL;
|
2016-04-19 20:05:38 +00:00
|
|
|
char uuidstr[VIR_UUID_STRING_BUFLEN];
|
|
|
|
|
2017-10-09 18:24:32 +00:00
|
|
|
virObjectRWLockWrite(secrets);
|
2017-04-20 11:33:26 +00:00
|
|
|
|
2016-04-19 20:05:38 +00:00
|
|
|
if (oldDef)
|
|
|
|
*oldDef = NULL;
|
|
|
|
|
2017-04-21 15:43:37 +00:00
|
|
|
virUUIDFormat(newdef->uuid, uuidstr);
|
|
|
|
|
2016-04-19 20:05:38 +00:00
|
|
|
/* Is there a secret already matching this UUID */
|
2017-04-21 15:43:37 +00:00
|
|
|
if ((obj = virSecretObjListFindByUUIDLocked(secrets, uuidstr))) {
|
2017-04-01 15:46:36 +00:00
|
|
|
virObjectLock(obj);
|
2017-05-31 18:54:27 +00:00
|
|
|
objdef = obj->def;
|
2016-04-19 20:05:38 +00:00
|
|
|
|
2017-05-31 18:54:27 +00:00
|
|
|
if (STRNEQ_NULLABLE(objdef->usage_id, newdef->usage_id)) {
|
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-05-31 18:54:27 +00:00
|
|
|
uuidstr, objdef->usage_id);
|
2016-04-19 20:05:38 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2017-05-31 18:54:27 +00:00
|
|
|
if (objdef->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-05-31 18:54:27 +00:00
|
|
|
*oldDef = objdef;
|
2016-04-19 20:05:38 +00:00
|
|
|
else
|
2017-05-31 18:54:27 +00:00
|
|
|
virSecretDefFree(objdef);
|
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-05-31 18:54:27 +00:00
|
|
|
objdef = obj->def;
|
|
|
|
virUUIDFormat(objdef->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;
|
|
|
|
}
|
|
|
|
|
2017-05-31 19:11:28 +00:00
|
|
|
if (!(obj = virSecretObjNew()))
|
|
|
|
goto cleanup;
|
|
|
|
|
2016-04-19 20:05:38 +00:00
|
|
|
/* Generate the possible configFile and base64File strings
|
|
|
|
* using the configDir, uuidstr, and appropriate suffix
|
|
|
|
*/
|
2017-05-31 19:11:28 +00:00
|
|
|
if (!(obj->configFile = virFileBuildPath(configDir, uuidstr, ".xml")) ||
|
|
|
|
!(obj->base64File = virFileBuildPath(configDir, uuidstr, ".base64")))
|
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;
|
|
|
|
virObjectRef(obj);
|
2016-04-19 20:05:38 +00:00
|
|
|
}
|
|
|
|
|
2019-10-16 11:43:01 +00:00
|
|
|
ret = g_steal_pointer(&obj);
|
2016-04-19 20:05:38 +00:00
|
|
|
|
|
|
|
cleanup:
|
2017-04-01 15:46:36 +00:00
|
|
|
virSecretObjEndAPI(&obj);
|
2017-10-09 18:24:32 +00:00
|
|
|
virObjectRWUnlock(secrets);
|
2017-04-20 11:33:26 +00:00
|
|
|
return ret;
|
2016-04-19 20:05:38 +00:00
|
|
|
}
|
2016-02-28 14:34:21 +00:00
|
|
|
|
|
|
|
|
2017-04-03 13:03:47 +00:00
|
|
|
struct virSecretCountData {
|
|
|
|
virConnectPtr conn;
|
2017-08-16 13:26:41 +00:00
|
|
|
virSecretObjListACLFilter filter;
|
2017-04-03 13:03:47 +00:00
|
|
|
int count;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
virSecretObjListNumOfSecretsCallback(void *payload,
|
2020-10-21 11:31:16 +00:00
|
|
|
const char *name G_GNUC_UNUSED,
|
2017-04-03 13:03:47 +00:00
|
|
|
void *opaque)
|
|
|
|
{
|
|
|
|
struct virSecretCountData *data = opaque;
|
2021-03-11 07:16:13 +00:00
|
|
|
virSecretObj *obj = payload;
|
|
|
|
virSecretDef *def;
|
2017-04-03 13:03:47 +00:00
|
|
|
|
|
|
|
virObjectLock(obj);
|
|
|
|
def = obj->def;
|
|
|
|
|
2017-08-16 13:26:41 +00:00
|
|
|
if (data->filter && !data->filter(data->conn, def))
|
2017-04-03 13:03:47 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
data->count++;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
virObjectUnlock(obj);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct virSecretListData {
|
2016-02-28 14:34:21 +00:00
|
|
|
virConnectPtr conn;
|
2017-08-16 13:26:41 +00:00
|
|
|
virSecretObjListACLFilter filter;
|
2016-02-28 15:56:17 +00:00
|
|
|
int nuuids;
|
2017-04-19 12:34:04 +00:00
|
|
|
char **uuids;
|
|
|
|
int maxuuids;
|
2016-02-28 15:56:17 +00:00
|
|
|
bool error;
|
2016-02-28 14:34:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2017-04-03 13:03:47 +00:00
|
|
|
virSecretObjListGetUUIDsCallback(void *payload,
|
2020-10-21 11:31:16 +00:00
|
|
|
const char *name G_GNUC_UNUSED,
|
2017-04-03 13:03:47 +00:00
|
|
|
void *opaque)
|
2016-02-28 14:34:21 +00:00
|
|
|
{
|
2017-04-03 13:03:47 +00:00
|
|
|
struct virSecretListData *data = opaque;
|
2021-03-11 07:16:13 +00:00
|
|
|
virSecretObj *obj = payload;
|
|
|
|
virSecretDef *def;
|
2016-02-28 14:34:21 +00:00
|
|
|
|
2016-02-28 15:56:17 +00:00
|
|
|
if (data->error)
|
|
|
|
return 0;
|
|
|
|
|
2017-04-19 12:34:04 +00:00
|
|
|
if (data->maxuuids >= 0 && data->nuuids == data->maxuuids)
|
2016-02-28 15:56:17 +00:00
|
|
|
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-08-16 13:26:41 +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;
|
|
|
|
|
2020-10-07 19:15:03 +00:00
|
|
|
uuidstr = g_new0(char, VIR_UUID_STRING_BUFLEN);
|
2016-02-28 15:56:17 +00:00
|
|
|
|
2017-04-03 12:11:32 +00:00
|
|
|
virUUIDFormat(def->uuid, uuidstr);
|
2017-04-03 13:03:47 +00:00
|
|
|
data->uuids[data->nuuids++] = uuidstr;
|
2016-02-28 15:56:17 +00:00
|
|
|
}
|
|
|
|
|
2016-02-28 14:34:21 +00:00
|
|
|
cleanup:
|
|
|
|
virObjectUnlock(obj);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2021-03-11 07:16:13 +00:00
|
|
|
virSecretObjListNumOfSecrets(virSecretObjList *secrets,
|
2017-08-16 13:26:41 +00:00
|
|
|
virSecretObjListACLFilter filter,
|
2016-02-28 14:34:21 +00:00
|
|
|
virConnectPtr conn)
|
|
|
|
{
|
2017-04-03 13:03:47 +00:00
|
|
|
struct virSecretCountData data = {
|
2017-08-16 13:26:41 +00:00
|
|
|
.conn = conn, .filter = filter, .count = 0 };
|
2016-02-28 14:34:21 +00:00
|
|
|
|
2017-10-09 18:24:32 +00:00
|
|
|
virObjectRWLockRead(secrets);
|
2017-04-03 13:03:47 +00:00
|
|
|
virHashForEach(secrets->objs, virSecretObjListNumOfSecretsCallback, &data);
|
2017-10-09 18:24:32 +00:00
|
|
|
virObjectRWUnlock(secrets);
|
2016-02-28 14:34:21 +00:00
|
|
|
|
2017-04-03 13:03:47 +00:00
|
|
|
return data.count;
|
2016-02-28 14:34:21 +00:00
|
|
|
}
|
2016-02-29 11:52:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
#define MATCH(FLAG) (flags & (FLAG))
|
|
|
|
static bool
|
2021-03-11 07:16:13 +00:00
|
|
|
virSecretObjMatch(virSecretObj *obj,
|
2018-08-13 15:55:16 +00:00
|
|
|
unsigned int flags)
|
2016-02-29 11:52:00 +00:00
|
|
|
{
|
2021-03-11 07:16:13 +00:00
|
|
|
virSecretDef *def = obj->def;
|
2017-04-03 12:11:32 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2018-08-13 15:55:14 +00:00
|
|
|
typedef struct _virSecretObjListExportData virSecretObjListExportData;
|
|
|
|
struct _virSecretObjListExportData {
|
2016-02-29 11:52:00 +00:00
|
|
|
virConnectPtr conn;
|
|
|
|
virSecretPtr *secrets;
|
2017-08-16 13:26:41 +00:00
|
|
|
virSecretObjListACLFilter filter;
|
2016-02-29 11:52:00 +00:00
|
|
|
unsigned int flags;
|
|
|
|
int nsecrets;
|
|
|
|
bool error;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
2017-04-24 16:50:12 +00:00
|
|
|
virSecretObjListExportCallback(void *payload,
|
2020-10-21 11:31:16 +00:00
|
|
|
const char *name G_GNUC_UNUSED,
|
2017-04-24 16:50:12 +00:00
|
|
|
void *opaque)
|
2016-02-29 11:52:00 +00:00
|
|
|
{
|
2021-03-11 07:16:13 +00:00
|
|
|
virSecretObjListExportData *data = opaque;
|
|
|
|
virSecretObj *obj = payload;
|
|
|
|
virSecretDef *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-08-16 13:26:41 +00:00
|
|
|
if (data->filter && !data->filter(data->conn, def))
|
2016-02-29 11:52:00 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
2018-08-13 15:55:16 +00:00
|
|
|
if (!virSecretObjMatch(obj, data->flags))
|
2016-02-29 11:52:00 +00:00
|
|
|
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,
|
2021-03-11 07:16:13 +00:00
|
|
|
virSecretObjList *secretobjs,
|
2016-02-29 11:52:00 +00:00
|
|
|
virSecretPtr **secrets,
|
2017-08-16 13:26:41 +00:00
|
|
|
virSecretObjListACLFilter filter,
|
2016-02-29 11:52:00 +00:00
|
|
|
unsigned int flags)
|
|
|
|
{
|
2018-08-13 15:55:14 +00:00
|
|
|
virSecretObjListExportData data = {
|
2016-02-29 11:52:00 +00:00
|
|
|
.conn = conn, .secrets = NULL,
|
2017-08-16 13:26:41 +00:00
|
|
|
.filter = filter, .flags = flags,
|
2016-02-29 11:52:00 +00:00
|
|
|
.nsecrets = 0, .error = false };
|
|
|
|
|
2017-10-09 18:24:32 +00:00
|
|
|
virObjectRWLockRead(secretobjs);
|
2020-10-07 19:15:03 +00:00
|
|
|
if (secrets)
|
2020-10-12 17:54:36 +00:00
|
|
|
data.secrets = g_new0(virSecretPtr, virHashSize(secretobjs->objs) + 1);
|
2016-02-29 11:52:00 +00:00
|
|
|
|
2017-04-24 16:50:12 +00:00
|
|
|
virHashForEach(secretobjs->objs, virSecretObjListExportCallback, &data);
|
2017-10-09 18:24:32 +00:00
|
|
|
virObjectRWUnlock(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 */
|
2021-03-19 23:37:05 +00:00
|
|
|
VIR_REALLOC_N(data.secrets, data.nsecrets + 1);
|
2016-02-29 11:52:00 +00:00
|
|
|
*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
|
2021-03-11 07:16:13 +00:00
|
|
|
virSecretObjListGetUUIDs(virSecretObjList *secrets,
|
2016-02-28 15:56:17 +00:00
|
|
|
char **uuids,
|
2017-04-19 12:34:04 +00:00
|
|
|
int maxuuids,
|
2017-08-16 13:26:41 +00:00
|
|
|
virSecretObjListACLFilter filter,
|
2016-02-28 15:56:17 +00:00
|
|
|
virConnectPtr conn)
|
|
|
|
{
|
2017-04-03 13:03:47 +00:00
|
|
|
struct virSecretListData data = {
|
2017-08-16 13:26:41 +00:00
|
|
|
.conn = conn, .filter = filter, .uuids = uuids, .nuuids = 0,
|
2017-04-03 13:03:47 +00:00
|
|
|
.maxuuids = maxuuids, .error = false };
|
2016-02-28 15:56:17 +00:00
|
|
|
|
2017-10-09 18:24:32 +00:00
|
|
|
virObjectRWLockRead(secrets);
|
2017-04-03 13:03:47 +00:00
|
|
|
virHashForEach(secrets->objs, virSecretObjListGetUUIDsCallback, &data);
|
2017-10-09 18:24:32 +00:00
|
|
|
virObjectRWUnlock(secrets);
|
2016-02-28 15:56:17 +00:00
|
|
|
|
|
|
|
if (data.error)
|
2017-04-25 13:51:29 +00:00
|
|
|
goto error;
|
2016-02-28 15:56:17 +00:00
|
|
|
|
2017-04-19 12:34:04 +00:00
|
|
|
return data.nuuids;
|
2016-02-28 15:56:17 +00:00
|
|
|
|
2017-04-25 13:51:29 +00:00
|
|
|
error:
|
2017-04-19 12:34:04 +00:00
|
|
|
while (--data.nuuids)
|
|
|
|
VIR_FREE(data.uuids[data.nuuids]);
|
2017-04-25 13:51:29 +00:00
|
|
|
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
|
2021-03-11 07:16:13 +00:00
|
|
|
virSecretObjDeleteConfig(virSecretObj *obj)
|
2016-03-07 23:06:57 +00:00
|
|
|
{
|
2021-03-11 07:16:13 +00:00
|
|
|
virSecretDef *def = obj->def;
|
2017-04-03 12:11:32 +00:00
|
|
|
|
|
|
|
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
|
2021-03-11 07:16:13 +00:00
|
|
|
virSecretObjDeleteData(virSecretObj *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 */
|
2018-09-20 12:08:19 +00:00
|
|
|
unlink(obj->base64File);
|
2016-03-07 23:06:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-18 14:17:25 +00:00
|
|
|
/* Permanent secret storage */
|
|
|
|
|
2021-03-11 07:16:13 +00:00
|
|
|
/* Secrets are stored in virSecretDriverState *->configDir. Each secret
|
2016-03-18 14:17:25 +00:00
|
|
|
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
|
2021-03-11 07:16:13 +00:00
|
|
|
virSecretObjSaveConfig(virSecretObj *obj)
|
2016-03-18 14:17:25 +00:00
|
|
|
{
|
2019-10-04 11:21:07 +00:00
|
|
|
g_autofree char *xml = NULL;
|
2016-03-18 14:17:25 +00:00
|
|
|
|
2017-04-01 15:46:36 +00:00
|
|
|
if (!(xml = virSecretDefFormat(obj->def)))
|
2019-10-04 11:21:07 +00:00
|
|
|
return -1;
|
2016-03-18 14:17:25 +00:00
|
|
|
|
2017-04-01 15:46:36 +00:00
|
|
|
if (virFileRewriteStr(obj->configFile, S_IRUSR | S_IWUSR, xml) < 0)
|
2019-10-04 11:21:07 +00:00
|
|
|
return -1;
|
2016-03-18 14:17:25 +00:00
|
|
|
|
2019-10-04 11:21:07 +00:00
|
|
|
return 0;
|
2016-03-18 14:17:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2021-03-11 07:16:13 +00:00
|
|
|
virSecretObjSaveData(virSecretObj *obj)
|
2016-03-18 14:17:25 +00:00
|
|
|
{
|
2019-10-04 11:21:07 +00:00
|
|
|
g_autofree char *base64 = NULL;
|
2016-03-18 14:17:25 +00:00
|
|
|
|
2017-04-01 15:46:36 +00:00
|
|
|
if (!obj->value)
|
2016-03-18 14:17:25 +00:00
|
|
|
return 0;
|
|
|
|
|
2019-09-16 12:29:20 +00:00
|
|
|
base64 = g_base64_encode(obj->value, obj->value_size);
|
2016-03-18 14:17:25 +00:00
|
|
|
|
2017-04-01 15:46:36 +00:00
|
|
|
if (virFileRewriteStr(obj->base64File, S_IRUSR | S_IWUSR, base64) < 0)
|
2019-10-04 11:21:07 +00:00
|
|
|
return -1;
|
2016-03-18 14:17:25 +00:00
|
|
|
|
2019-10-04 11:21:07 +00:00
|
|
|
return 0;
|
2016-03-18 14:17:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-11 07:16:13 +00:00
|
|
|
virSecretDef *
|
|
|
|
virSecretObjGetDef(virSecretObj *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
|
2021-03-11 07:16:13 +00:00
|
|
|
virSecretObjSetDef(virSecretObj *obj,
|
|
|
|
virSecretDef *def)
|
2016-03-08 00:22:04 +00:00
|
|
|
{
|
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 *
|
2021-03-11 07:16:13 +00:00
|
|
|
virSecretObjGetValue(virSecretObj *obj)
|
2016-03-08 00:40:58 +00:00
|
|
|
{
|
2021-03-11 07:16:13 +00:00
|
|
|
virSecretDef *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);
|
2019-10-21 18:18:51 +00:00
|
|
|
return NULL;
|
2016-03-08 00:40:58 +00:00
|
|
|
}
|
|
|
|
|
2020-10-07 19:15:03 +00:00
|
|
|
ret = g_new0(unsigned char, obj->value_size);
|
2017-04-01 15:46:36 +00:00
|
|
|
memcpy(ret, obj->value, obj->value_size);
|
2016-03-08 00:40:58 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2021-03-11 07:16:13 +00:00
|
|
|
virSecretObjSetValue(virSecretObj *obj,
|
2016-03-08 00:40:58 +00:00
|
|
|
const unsigned char *value,
|
|
|
|
size_t value_size)
|
|
|
|
{
|
2021-03-11 07:16:13 +00:00
|
|
|
virSecretDef *def = obj->def;
|
2019-10-04 11:21:07 +00:00
|
|
|
g_autofree unsigned char *old_value = NULL;
|
|
|
|
g_autofree unsigned char *new_value = NULL;
|
2016-03-08 00:40:58 +00:00
|
|
|
size_t old_value_size;
|
|
|
|
|
2020-10-07 19:15:03 +00:00
|
|
|
new_value = g_new0(unsigned char, value_size);
|
2016-03-08 00:40:58 +00:00
|
|
|
|
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);
|
2019-10-04 11:21:07 +00:00
|
|
|
obj->value = g_steal_pointer(&new_value);
|
2017-04-01 15:46:36 +00:00
|
|
|
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 */
|
2019-10-04 11:21:07 +00:00
|
|
|
if (old_value)
|
2016-03-08 00:40:58 +00:00
|
|
|
memset(old_value, 0, old_value_size);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
error:
|
|
|
|
/* Error - restore previous state and free new value */
|
2019-10-04 11:21:07 +00:00
|
|
|
new_value = g_steal_pointer(&obj->value);
|
|
|
|
obj->value = g_steal_pointer(&old_value);
|
2017-04-01 15:46:36 +00:00
|
|
|
obj->value_size = old_value_size;
|
2016-03-08 00:40:58 +00:00
|
|
|
memset(new_value, 0, value_size);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
size_t
|
2021-03-11 07:16:13 +00:00
|
|
|
virSecretObjGetValueSize(virSecretObj *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
|
2021-03-11 07:16:13 +00:00
|
|
|
virSecretObjSetValueSize(virSecretObj *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
|
2021-03-11 07:16:13 +00:00
|
|
|
virSecretLoadValidateUUID(virSecretDef *def,
|
2016-03-02 18:10:54 +00:00
|
|
|
const char *file)
|
|
|
|
{
|
|
|
|
char uuidstr[VIR_UUID_STRING_BUFLEN];
|
|
|
|
|
|
|
|
virUUIDFormat(def->uuid, uuidstr);
|
|
|
|
|
2019-03-06 14:45:15 +00:00
|
|
|
if (!virStringMatchesNameSuffix(file, uuidstr, ".xml")) {
|
2016-03-02 18:10:54 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("<uuid> does not match secret file name '%s'"),
|
|
|
|
file);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2021-03-11 07:16:13 +00:00
|
|
|
virSecretLoadValue(virSecretObj *obj)
|
2016-03-02 18:10:54 +00:00
|
|
|
{
|
|
|
|
int ret = -1, fd = -1;
|
|
|
|
struct stat st;
|
2019-10-04 11:21:07 +00:00
|
|
|
g_autofree char *contents = NULL;
|
2016-03-02 18:10:54 +00:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-10-07 19:15:03 +00:00
|
|
|
contents = g_new0(char, st.st_size + 1);
|
2016-03-02 18:10:54 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2019-09-16 12:29:20 +00:00
|
|
|
contents[st.st_size] = '\0';
|
2016-03-02 18:10:54 +00:00
|
|
|
|
|
|
|
VIR_FORCE_CLOSE(fd);
|
|
|
|
|
2019-09-16 12:29:20 +00:00
|
|
|
obj->value = g_base64_decode(contents, &obj->value_size);
|
2016-03-02 18:10:54 +00:00
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
cleanup:
|
2019-10-04 11:21:07 +00:00
|
|
|
if (contents != NULL)
|
2016-03-02 18:10:54 +00:00
|
|
|
memset(contents, 0, st.st_size);
|
|
|
|
VIR_FORCE_CLOSE(fd);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-11 07:16:13 +00:00
|
|
|
static virSecretObj *
|
|
|
|
virSecretLoad(virSecretObjList *secrets,
|
2016-03-02 18:10:54 +00:00
|
|
|
const char *file,
|
|
|
|
const char *path,
|
|
|
|
const char *configDir)
|
|
|
|
{
|
2021-03-11 07:16:13 +00:00
|
|
|
virSecretDef *def = NULL;
|
|
|
|
virSecretObj *obj = 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-07-25 13:02:09 +00:00
|
|
|
if (virSecretLoadValue(obj) < 0) {
|
|
|
|
virSecretObjListRemove(secrets, obj);
|
|
|
|
virObjectUnref(obj);
|
|
|
|
obj = NULL;
|
|
|
|
}
|
2016-03-02 18:10:54 +00:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
virSecretDefFree(def);
|
2017-07-25 13:02:09 +00:00
|
|
|
return obj;
|
2016-03-02 18:10:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2021-03-11 07:16:13 +00:00
|
|
|
virSecretLoadAllConfigs(virSecretObjList *secrets,
|
2016-03-02 18:10:54 +00:00
|
|
|
const char *configDir)
|
|
|
|
{
|
2020-10-25 21:50:51 +00:00
|
|
|
g_autoptr(DIR) dir = NULL;
|
2016-03-02 18:10:54 +00:00
|
|
|
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;
|
2021-03-11 07:16:13 +00:00
|
|
|
virSecretObj *obj;
|
2016-03-02 18:10:54 +00:00
|
|
|
|
2019-03-07 08:40:54 +00:00
|
|
|
if (!virStringHasSuffix(de->d_name, ".xml"))
|
2016-03-02 18:10:54 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|