|
Packit |
a07778 |
/*
|
|
Packit |
a07778 |
* libvirt-gobject-secret.c: libvirt glib integration
|
|
Packit |
a07778 |
*
|
|
Packit |
a07778 |
* Copyright (C) 2008 Daniel P. Berrange
|
|
Packit |
a07778 |
* Copyright (C) 2010-2011 Red Hat, Inc.
|
|
Packit |
a07778 |
*
|
|
Packit |
a07778 |
* This library is free software; you can redistribute it and/or
|
|
Packit |
a07778 |
* modify it under the terms of the GNU Lesser General Public
|
|
Packit |
a07778 |
* License as published by the Free Software Foundation; either
|
|
Packit |
a07778 |
* version 2.1 of the License, or (at your option) any later version.
|
|
Packit |
a07778 |
*
|
|
Packit |
a07778 |
* This library is distributed in the hope that it will be useful,
|
|
Packit |
a07778 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
a07778 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
a07778 |
* Lesser General Public License for more details.
|
|
Packit |
a07778 |
*
|
|
Packit |
a07778 |
* You should have received a copy of the GNU Lesser General Public
|
|
Packit |
a07778 |
* License along with this library. If not, see
|
|
Packit |
a07778 |
* <http://www.gnu.org/licenses/>.
|
|
Packit |
a07778 |
*
|
|
Packit |
a07778 |
* Author: Daniel P. Berrange <berrange@redhat.com>
|
|
Packit |
a07778 |
*/
|
|
Packit |
a07778 |
|
|
Packit |
a07778 |
#include <config.h>
|
|
Packit |
a07778 |
|
|
Packit |
a07778 |
#include <libvirt/virterror.h>
|
|
Packit |
a07778 |
#include <string.h>
|
|
Packit |
a07778 |
|
|
Packit |
a07778 |
#include "libvirt-glib/libvirt-glib.h"
|
|
Packit |
a07778 |
#include "libvirt-gobject/libvirt-gobject.h"
|
|
Packit |
a07778 |
#include "libvirt-gobject-compat.h"
|
|
Packit |
a07778 |
|
|
Packit |
a07778 |
#define GVIR_SECRET_GET_PRIVATE(obj) \
|
|
Packit |
a07778 |
(G_TYPE_INSTANCE_GET_PRIVATE((obj), GVIR_TYPE_SECRET, GVirSecretPrivate))
|
|
Packit |
a07778 |
|
|
Packit |
a07778 |
struct _GVirSecretPrivate
|
|
Packit |
a07778 |
{
|
|
Packit |
a07778 |
virSecretPtr handle;
|
|
Packit |
a07778 |
gchar uuid[VIR_UUID_STRING_BUFLEN];
|
|
Packit |
a07778 |
};
|
|
Packit |
a07778 |
|
|
Packit |
a07778 |
G_DEFINE_TYPE_WITH_PRIVATE(GVirSecret, gvir_secret, G_TYPE_OBJECT);
|
|
Packit |
a07778 |
|
|
Packit |
a07778 |
|
|
Packit |
a07778 |
enum {
|
|
Packit |
a07778 |
PROP_0,
|
|
Packit |
a07778 |
PROP_HANDLE,
|
|
Packit |
a07778 |
};
|
|
Packit |
a07778 |
|
|
Packit |
a07778 |
|
|
Packit |
a07778 |
#define GVIR_SECRET_ERROR gvir_secret_error_quark()
|
|
Packit |
a07778 |
|
|
Packit |
a07778 |
|
|
Packit |
a07778 |
static GQuark
|
|
Packit |
a07778 |
gvir_secret_error_quark(void)
|
|
Packit |
a07778 |
{
|
|
Packit |
a07778 |
return g_quark_from_static_string("gvir-secret");
|
|
Packit |
a07778 |
}
|
|
Packit |
a07778 |
|
|
Packit |
a07778 |
static void gvir_secret_get_property(GObject *object,
|
|
Packit |
a07778 |
guint prop_id,
|
|
Packit |
a07778 |
GValue *value,
|
|
Packit |
a07778 |
GParamSpec *pspec)
|
|
Packit |
a07778 |
{
|
|
Packit |
a07778 |
GVirSecret *secret = GVIR_SECRET(object);
|
|
Packit |
a07778 |
GVirSecretPrivate *priv = secret->priv;
|
|
Packit |
a07778 |
|
|
Packit |
a07778 |
switch (prop_id) {
|
|
Packit |
a07778 |
case PROP_HANDLE:
|
|
Packit |
a07778 |
g_value_set_boxed(value, priv->handle);
|
|
Packit |
a07778 |
break;
|
|
Packit |
a07778 |
|
|
Packit |
a07778 |
default:
|
|
Packit |
a07778 |
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
|
|
Packit |
a07778 |
}
|
|
Packit |
a07778 |
}
|
|
Packit |
a07778 |
|
|
Packit |
a07778 |
|
|
Packit |
a07778 |
static void gvir_secret_set_property(GObject *object,
|
|
Packit |
a07778 |
guint prop_id,
|
|
Packit |
a07778 |
const GValue *value,
|
|
Packit |
a07778 |
GParamSpec *pspec)
|
|
Packit |
a07778 |
{
|
|
Packit |
a07778 |
GVirSecret *secret = GVIR_SECRET(object);
|
|
Packit |
a07778 |
GVirSecretPrivate *priv = secret->priv;
|
|
Packit |
a07778 |
|
|
Packit |
a07778 |
switch (prop_id) {
|
|
Packit |
a07778 |
case PROP_HANDLE:
|
|
Packit |
a07778 |
if (priv->handle)
|
|
Packit |
a07778 |
virSecretFree(priv->handle);
|
|
Packit |
a07778 |
priv->handle = g_value_dup_boxed(value);
|
|
Packit |
a07778 |
break;
|
|
Packit |
a07778 |
|
|
Packit |
a07778 |
default:
|
|
Packit |
a07778 |
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
|
|
Packit |
a07778 |
}
|
|
Packit |
a07778 |
}
|
|
Packit |
a07778 |
|
|
Packit |
a07778 |
|
|
Packit |
a07778 |
static void gvir_secret_finalize(GObject *object)
|
|
Packit |
a07778 |
{
|
|
Packit |
a07778 |
GVirSecret *secret = GVIR_SECRET(object);
|
|
Packit |
a07778 |
GVirSecretPrivate *priv = secret->priv;
|
|
Packit |
a07778 |
|
|
Packit |
a07778 |
virSecretFree(priv->handle);
|
|
Packit |
a07778 |
|
|
Packit |
a07778 |
G_OBJECT_CLASS(gvir_secret_parent_class)->finalize(object);
|
|
Packit |
a07778 |
}
|
|
Packit |
a07778 |
|
|
Packit |
a07778 |
|
|
Packit |
a07778 |
static void gvir_secret_constructed(GObject *object)
|
|
Packit |
a07778 |
{
|
|
Packit |
a07778 |
GVirSecret *secret = GVIR_SECRET(object);
|
|
Packit |
a07778 |
GVirSecretPrivate *priv = secret->priv;
|
|
Packit |
a07778 |
|
|
Packit |
a07778 |
G_OBJECT_CLASS(gvir_secret_parent_class)->constructed(object);
|
|
Packit |
a07778 |
|
|
Packit |
a07778 |
/* xxx we may want to turn this into an initable */
|
|
Packit |
a07778 |
if (virSecretGetUUIDString(priv->handle, priv->uuid) < 0)
|
|
Packit |
a07778 |
gvir_warning("Failed to get secret UUID on %p", priv->handle);
|
|
Packit |
a07778 |
}
|
|
Packit |
a07778 |
|
|
Packit |
a07778 |
|
|
Packit |
a07778 |
static void gvir_secret_class_init(GVirSecretClass *klass)
|
|
Packit |
a07778 |
{
|
|
Packit |
a07778 |
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
|
Packit |
a07778 |
|
|
Packit |
a07778 |
object_class->finalize = gvir_secret_finalize;
|
|
Packit |
a07778 |
object_class->get_property = gvir_secret_get_property;
|
|
Packit |
a07778 |
object_class->set_property = gvir_secret_set_property;
|
|
Packit |
a07778 |
object_class->constructed = gvir_secret_constructed;
|
|
Packit |
a07778 |
|
|
Packit |
a07778 |
g_object_class_install_property(object_class,
|
|
Packit |
a07778 |
PROP_HANDLE,
|
|
Packit |
a07778 |
g_param_spec_boxed("handle",
|
|
Packit |
a07778 |
"Handle",
|
|
Packit |
a07778 |
"The secret handle",
|
|
Packit |
a07778 |
GVIR_TYPE_SECRET_HANDLE,
|
|
Packit |
a07778 |
G_PARAM_READABLE |
|
|
Packit |
a07778 |
G_PARAM_WRITABLE |
|
|
Packit |
a07778 |
G_PARAM_CONSTRUCT_ONLY |
|
|
Packit |
a07778 |
G_PARAM_STATIC_STRINGS));
|
|
Packit |
a07778 |
}
|
|
Packit |
a07778 |
|
|
Packit |
a07778 |
|
|
Packit |
a07778 |
static void gvir_secret_init(GVirSecret *secret)
|
|
Packit |
a07778 |
{
|
|
Packit |
a07778 |
secret->priv = GVIR_SECRET_GET_PRIVATE(secret);
|
|
Packit |
a07778 |
}
|
|
Packit |
a07778 |
|
|
Packit |
a07778 |
typedef struct virSecret GVirSecretHandle;
|
|
Packit |
a07778 |
|
|
Packit |
a07778 |
static GVirSecretHandle*
|
|
Packit |
a07778 |
gvir_secret_handle_copy(GVirSecretHandle *src)
|
|
Packit |
a07778 |
{
|
|
Packit |
a07778 |
virSecretRef((virSecretPtr)src);
|
|
Packit |
a07778 |
return src;
|
|
Packit |
a07778 |
}
|
|
Packit |
a07778 |
|
|
Packit |
a07778 |
static void
|
|
Packit |
a07778 |
gvir_secret_handle_free(GVirSecretHandle *src)
|
|
Packit |
a07778 |
{
|
|
Packit |
a07778 |
virSecretFree((virSecretPtr)src);
|
|
Packit |
a07778 |
}
|
|
Packit |
a07778 |
|
|
Packit |
a07778 |
G_DEFINE_BOXED_TYPE(GVirSecretHandle, gvir_secret_handle,
|
|
Packit |
a07778 |
gvir_secret_handle_copy, gvir_secret_handle_free)
|
|
Packit |
a07778 |
|
|
Packit |
a07778 |
const gchar *gvir_secret_get_uuid(GVirSecret *secret)
|
|
Packit |
a07778 |
{
|
|
Packit |
a07778 |
g_return_val_if_fail(GVIR_IS_SECRET(secret), NULL);
|
|
Packit |
a07778 |
|
|
Packit |
a07778 |
return secret->priv->uuid;
|
|
Packit |
a07778 |
}
|
|
Packit |
a07778 |
|
|
Packit |
a07778 |
|
|
Packit |
a07778 |
/**
|
|
Packit |
a07778 |
* gvir_secret_get_config:
|
|
Packit |
a07778 |
* @secret: the secret
|
|
Packit |
a07778 |
* @flags: the flags
|
|
Packit |
a07778 |
* @err: Place-holder for possible errors
|
|
Packit |
a07778 |
*
|
|
Packit |
a07778 |
* Returns: (transfer full): the config. The returned object should be
|
|
Packit |
a07778 |
* unreffed with g_object_unref() when no longer needed.
|
|
Packit |
a07778 |
|
|
Packit |
a07778 |
*/
|
|
Packit |
a07778 |
GVirConfigSecret *gvir_secret_get_config(GVirSecret *secret,
|
|
Packit |
a07778 |
guint flags,
|
|
Packit |
a07778 |
GError **err)
|
|
Packit |
a07778 |
{
|
|
Packit |
a07778 |
GVirSecretPrivate *priv;
|
|
Packit |
a07778 |
gchar *xml;
|
|
Packit |
a07778 |
|
|
Packit |
a07778 |
g_return_val_if_fail(GVIR_IS_SECRET(secret), NULL);
|
|
Packit |
a07778 |
g_return_val_if_fail(err == NULL || *err == NULL, NULL);
|
|
Packit |
a07778 |
|
|
Packit |
a07778 |
priv = secret->priv;
|
|
Packit |
a07778 |
if (!(xml = virSecretGetXMLDesc(priv->handle, flags))) {
|
|
Packit |
a07778 |
gvir_set_error_literal(err, GVIR_SECRET_ERROR,
|
|
Packit |
a07778 |
0,
|
|
Packit |
a07778 |
"Unable to get secret XML config");
|
|
Packit |
a07778 |
return NULL;
|
|
Packit |
a07778 |
}
|
|
Packit |
a07778 |
|
|
Packit |
a07778 |
GVirConfigSecret *conf = gvir_config_secret_new_from_xml(xml, err);
|
|
Packit |
a07778 |
|
|
Packit |
a07778 |
free(xml);
|
|
Packit |
a07778 |
return conf;
|
|
Packit |
a07778 |
}
|