2
* secret_conf.c: internal <secret> XML handling
4
* Copyright (C) 2009 Red Hat, Inc.
6
* This library is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU Lesser General Public
8
* License as published by the Free Software Foundation; either
9
* version 2.1 of the License, or (at your option) any later version.
11
* This library is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* Lesser General Public License for more details.
16
* You should have received a copy of the GNU Lesser General Public
17
* License along with this library; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
* Red Hat Author: Miloslav Trmač <mitr@redhat.com>
27
#include "datatypes.h"
30
#include "secret_conf.h"
31
#include "virterror_internal.h"
36
#define VIR_FROM_THIS VIR_FROM_SECRET
38
VIR_ENUM_IMPL(virSecretUsageType, VIR_SECRET_USAGE_TYPE_VOLUME + 1, "none", "volume")
41
virSecretDefFree(virSecretDefPtr def)
46
VIR_FREE(def->description);
47
switch (def->usage_type) {
48
case VIR_SECRET_USAGE_TYPE_NONE:
51
case VIR_SECRET_USAGE_TYPE_VOLUME:
52
VIR_FREE(def->usage.volume);
56
VIR_ERROR(_("unexpected secret usage type %d"), def->usage_type);
63
virSecretDefParseUsage(virConnectPtr conn, xmlXPathContextPtr ctxt,
69
type_str = virXPathString(conn, "string(./usage/@type)", ctxt);
70
if (type_str == NULL) {
71
virSecretReportError(conn, VIR_ERR_XML_ERROR, "%s",
72
_("unknown secret usage type"));
75
type = virSecretUsageTypeTypeFromString(type_str);
77
virSecretReportError(conn, VIR_ERR_XML_ERROR,
78
_("unknown secret usage type %s"), type_str);
83
def->usage_type = type;
84
switch (def->usage_type) {
85
case VIR_SECRET_USAGE_TYPE_NONE:
88
case VIR_SECRET_USAGE_TYPE_VOLUME:
89
def->usage.volume = virXPathString(conn, "string(./usage/volume)",
91
if (!def->usage.volume) {
92
virSecretReportError(conn, VIR_ERR_INTERNAL_ERROR, "%s",
93
_("volume usage specified, but volume path is missing"));
99
virSecretReportError(conn, VIR_ERR_INTERNAL_ERROR,
100
_("unexpected secret usage type %d"),
107
static virSecretDefPtr
108
secretXMLParseNode(virConnectPtr conn, xmlDocPtr xml, xmlNodePtr root)
110
xmlXPathContextPtr ctxt = NULL;
111
virSecretDefPtr def = NULL, ret = NULL;
113
char *uuidstr = NULL;
115
if (!xmlStrEqual(root->name, BAD_CAST "secret")) {
116
virSecretReportError(conn, VIR_ERR_XML_ERROR, "%s",
117
_("incorrect root element"));
121
ctxt = xmlXPathNewContext(xml);
123
virReportOOMError(conn);
128
if (VIR_ALLOC(def) < 0) {
129
virReportOOMError(conn);
133
prop = virXPathString(conn, "string(./@ephemeral)", ctxt);
135
if (STREQ(prop, "yes"))
137
else if (STREQ(prop, "no"))
140
virSecretReportError(conn, VIR_ERR_XML_ERROR, "%s",
141
_("invalid value of 'ephemeral'"));
147
prop = virXPathString(conn, "string(./@private)", ctxt);
149
if (STREQ(prop, "yes"))
151
else if (STREQ(prop, "no"))
154
virSecretReportError(conn, VIR_ERR_XML_ERROR, "%s",
155
_("invalid value of 'private'"));
161
uuidstr = virXPathString(conn, "string(./uuid)", ctxt);
163
if (virUUIDGenerate(def->uuid)) {
164
virSecretReportError(conn, VIR_ERR_INTERNAL_ERROR,
165
"%s", _("Failed to generate UUID"));
169
if (virUUIDParse(uuidstr, def->uuid) < 0) {
170
virSecretReportError(conn, VIR_ERR_INTERNAL_ERROR,
171
"%s", _("malformed uuid element"));
177
def->description = virXPathString(conn, "string(./description)", ctxt);
178
if (virXPathNode(conn, "./usage", ctxt) != NULL
179
&& virSecretDefParseUsage(conn, ctxt, def) < 0)
186
virSecretDefFree(def);
187
xmlXPathFreeContext(ctxt);
191
/* Called from SAX on parsing errors in the XML. */
193
catchXMLError(void *ctx, const char *msg ATTRIBUTE_UNUSED, ...)
195
xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
198
virConnectPtr conn = ctxt->_private;
200
if (virGetLastError() == NULL &&
201
ctxt->lastError.level == XML_ERR_FATAL &&
202
ctxt->lastError.message != NULL) {
203
virSecretReportError(conn, VIR_ERR_XML_DETAIL, _("at line %d: %s"),
204
ctxt->lastError.line, ctxt->lastError.message);
209
static virSecretDefPtr
210
virSecretDefParse(virConnectPtr conn, const char *xmlStr, const char *filename)
212
xmlParserCtxtPtr pctxt;
213
xmlDocPtr xml = NULL;
215
virSecretDefPtr ret = NULL;
217
pctxt = xmlNewParserCtxt();
218
if (pctxt == NULL || pctxt->sax == NULL)
220
pctxt->sax->error = catchXMLError;
221
pctxt->_private = conn;
223
if (filename != NULL)
224
xml = xmlCtxtReadFile(pctxt, filename, NULL,
225
XML_PARSE_NOENT | XML_PARSE_NONET |
226
XML_PARSE_NOWARNING);
228
xml = xmlCtxtReadDoc(pctxt, BAD_CAST xmlStr, "secret.xml", NULL,
229
XML_PARSE_NOENT | XML_PARSE_NONET |
230
XML_PARSE_NOWARNING);
232
if (conn->err.code == VIR_ERR_NONE)
233
virSecretReportError(conn, VIR_ERR_XML_ERROR, "%s",
234
_("failed to parse xml document"));
238
root = xmlDocGetRootElement(xml);
240
virSecretReportError(conn, VIR_ERR_INTERNAL_ERROR, "%s",
241
_("missing root element"));
245
ret = secretXMLParseNode(conn, xml, root);
249
xmlFreeParserCtxt(pctxt);
254
virSecretDefParseString(virConnectPtr conn, const char *xmlStr)
256
return virSecretDefParse(conn, xmlStr, NULL);
260
virSecretDefParseFile(virConnectPtr conn, const char *filename)
262
return virSecretDefParse(conn, NULL, filename);
266
virSecretDefFormatUsage(virConnectPtr conn, virBufferPtr buf,
267
const virSecretDefPtr def)
271
type = virSecretUsageTypeTypeToString(def->usage_type);
273
virSecretReportError(conn, VIR_ERR_INTERNAL_ERROR,
274
_("unexpected secret usage type %d"),
278
virBufferVSprintf(buf, " <usage type='%s'>\n", type);
279
switch (def->usage_type) {
280
case VIR_SECRET_USAGE_TYPE_NONE:
283
case VIR_SECRET_USAGE_TYPE_VOLUME:
284
if (def->usage.volume != NULL)
285
virBufferEscapeString(buf, " <volume>%s</volume>\n",
290
virSecretReportError(conn, VIR_ERR_INTERNAL_ERROR,
291
_("unexpected secret usage type %d"),
295
virBufferAddLit(buf, " </usage>\n");
301
virSecretDefFormat(virConnectPtr conn, const virSecretDefPtr def)
303
virBuffer buf = VIR_BUFFER_INITIALIZER;
305
char uuidstr[VIR_UUID_STRING_BUFLEN];
308
virBufferVSprintf(&buf, "<secret ephemeral='%s' private='%s'>\n",
309
def->ephemeral ? "yes" : "no",
310
def->private ? "yes" : "no");
313
virUUIDFormat(uuid, uuidstr);
314
virBufferEscapeString(&buf, " <uuid>%s</uuid>\n", uuidstr);
315
if (def->description != NULL)
316
virBufferEscapeString(&buf, " <description>%s</description>\n",
318
if (def->usage_type != VIR_SECRET_USAGE_TYPE_NONE &&
319
virSecretDefFormatUsage(conn, &buf, def) < 0)
321
virBufferAddLit(&buf, "</secret>\n");
323
if (virBufferError(&buf))
326
return virBufferContentAndReset(&buf);
329
virReportOOMError(conn);
331
tmp = virBufferContentAndReset(&buf);