secret_conf.c 8.9 KB
Newer Older
1 2 3
/*
 * secret_conf.c: internal <secret> XML handling
 *
P
Phil Petty 已提交
4
 * Copyright (C) 2009, 2011 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16
 *
 * 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
17
 * License along with this library.  If not, see
O
Osier Yang 已提交
18
 * <http://www.gnu.org/licenses/>.
19 20 21 22 23 24 25
 *
 * Red Hat Author: Miloslav Trmač <mitr@redhat.com>
 */

#include <config.h>

#include "internal.h"
26
#include "virbuffer.h"
27
#include "datatypes.h"
28
#include "virlog.h"
29
#include "viralloc.h"
30
#include "secret_conf.h"
31
#include "virerror.h"
32
#include "virxml.h"
33
#include "viruuid.h"
34 35 36

#define VIR_FROM_THIS VIR_FROM_SECRET

S
Sage Weil 已提交
37
VIR_ENUM_IMPL(virSecretUsageType, VIR_SECRET_USAGE_TYPE_LAST,
38
              "none", "volume", "ceph", "iscsi")
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54

void
virSecretDefFree(virSecretDefPtr def)
{
    if (def == NULL)
        return;

    VIR_FREE(def->description);
    switch (def->usage_type) {
    case VIR_SECRET_USAGE_TYPE_NONE:
        break;

    case VIR_SECRET_USAGE_TYPE_VOLUME:
        VIR_FREE(def->usage.volume);
        break;

S
Sage Weil 已提交
55 56
    case VIR_SECRET_USAGE_TYPE_CEPH:
        VIR_FREE(def->usage.ceph);
E
Eric Blake 已提交
57
        break;
S
Sage Weil 已提交
58

59 60 61 62
    case VIR_SECRET_USAGE_TYPE_ISCSI:
        VIR_FREE(def->usage.target);
        break;

63 64 65 66 67 68 69 70
    default:
        VIR_ERROR(_("unexpected secret usage type %d"), def->usage_type);
        break;
    }
    VIR_FREE(def);
}

static int
71
virSecretDefParseUsage(xmlXPathContextPtr ctxt,
72 73 74 75 76
                       virSecretDefPtr def)
{
    char *type_str;
    int type;

77
    type_str = virXPathString("string(./usage/@type)", ctxt);
78
    if (type_str == NULL) {
79 80
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("unknown secret usage type"));
81 82 83 84
        return -1;
    }
    type = virSecretUsageTypeTypeFromString(type_str);
    if (type < 0) {
85 86
        virReportError(VIR_ERR_XML_ERROR,
                       _("unknown secret usage type %s"), type_str);
87 88 89 90 91 92 93 94 95 96
        VIR_FREE(type_str);
        return -1;
    }
    VIR_FREE(type_str);
    def->usage_type = type;
    switch (def->usage_type) {
    case VIR_SECRET_USAGE_TYPE_NONE:
        break;

    case VIR_SECRET_USAGE_TYPE_VOLUME:
97
        def->usage.volume = virXPathString("string(./usage/volume)", ctxt);
98
        if (!def->usage.volume) {
99 100
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("volume usage specified, but volume path is missing"));
101 102
            return -1;
        }
103 104
        break;

S
Sage Weil 已提交
105 106 107
    case VIR_SECRET_USAGE_TYPE_CEPH:
        def->usage.ceph = virXPathString("string(./usage/name)", ctxt);
        if (!def->usage.ceph) {
108 109
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("Ceph usage specified, but name is missing"));
S
Sage Weil 已提交
110 111 112 113
            return -1;
        }
        break;

114 115 116 117 118 119 120 121 122
    case VIR_SECRET_USAGE_TYPE_ISCSI:
        def->usage.target = virXPathString("string(./usage/target)", ctxt);
        if (!def->usage.target) {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("iSCSI usage specified, but target is missing"));
            return -1;
        }
        break;

123
    default:
124 125 126
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unexpected secret usage type %d"),
                       def->usage_type);
127 128 129 130 131 132
        return -1;
    }
    return 0;
}

static virSecretDefPtr
133
secretXMLParseNode(xmlDocPtr xml, xmlNodePtr root)
134 135 136 137
{
    xmlXPathContextPtr ctxt = NULL;
    virSecretDefPtr def = NULL, ret = NULL;
    char *prop = NULL;
138
    char *uuidstr = NULL;
139 140

    if (!xmlStrEqual(root->name, BAD_CAST "secret")) {
141 142 143 144
        virReportError(VIR_ERR_XML_ERROR,
                       _("unexpected root element <%s>, "
                         "expecting <secret>"),
                       root->name);
145 146 147 148 149
        goto cleanup;
    }

    ctxt = xmlXPathNewContext(xml);
    if (ctxt == NULL) {
150
        virReportOOMError();
151 152 153 154 155
        goto cleanup;
    }
    ctxt->node = root;

    if (VIR_ALLOC(def) < 0) {
156
        virReportOOMError();
157 158 159
        goto cleanup;
    }

160
    prop = virXPathString("string(./@ephemeral)", ctxt);
161 162
    if (prop != NULL) {
        if (STREQ(prop, "yes"))
163
            def->ephemeral = true;
164
        else if (STREQ(prop, "no"))
165
            def->ephemeral = false;
166
        else {
167 168
            virReportError(VIR_ERR_XML_ERROR, "%s",
                           _("invalid value of 'ephemeral'"));
169 170 171 172 173
            goto cleanup;
        }
        VIR_FREE(prop);
    }

174
    prop = virXPathString("string(./@private)", ctxt);
175 176
    if (prop != NULL) {
        if (STREQ(prop, "yes"))
177
            def->private = true;
178
        else if (STREQ(prop, "no"))
179
            def->private = false;
180
        else {
181 182
            virReportError(VIR_ERR_XML_ERROR, "%s",
                           _("invalid value of 'private'"));
183 184 185 186 187
            goto cleanup;
        }
        VIR_FREE(prop);
    }

188
    uuidstr = virXPathString("string(./uuid)", ctxt);
189 190
    if (!uuidstr) {
        if (virUUIDGenerate(def->uuid)) {
191 192
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           "%s", _("Failed to generate UUID"));
193 194 195 196
            goto cleanup;
        }
    } else {
        if (virUUIDParse(uuidstr, def->uuid) < 0) {
197 198
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           "%s", _("malformed uuid element"));
199 200 201 202 203
            goto cleanup;
        }
        VIR_FREE(uuidstr);
    }

204 205
    def->description = virXPathString("string(./description)", ctxt);
    if (virXPathNode("./usage", ctxt) != NULL
206
        && virSecretDefParseUsage(ctxt, def) < 0)
207 208 209 210 211 212
        goto cleanup;
    ret = def;
    def = NULL;

 cleanup:
    VIR_FREE(prop);
P
Phil Petty 已提交
213
    VIR_FREE(uuidstr);
214 215 216 217 218 219
    virSecretDefFree(def);
    xmlXPathFreeContext(ctxt);
    return ret;
}

static virSecretDefPtr
J
Jiri Denemark 已提交
220 221
virSecretDefParse(const char *xmlStr,
                  const char *filename)
222
{
J
Jiri Denemark 已提交
223
    xmlDocPtr xml;
224 225
    virSecretDefPtr ret = NULL;

226
    if ((xml = virXMLParse(filename, xmlStr, _("(definition_of_secret)")))) {
J
Jiri Denemark 已提交
227 228
        ret = secretXMLParseNode(xml, xmlDocGetRootElement(xml));
        xmlFreeDoc(xml);
229 230 231 232 233 234
    }

    return ret;
}

virSecretDefPtr
235
virSecretDefParseString(const char *xmlStr)
236
{
237
    return virSecretDefParse(xmlStr, NULL);
238 239 240
}

virSecretDefPtr
241
virSecretDefParseFile(const char *filename)
242
{
243
    return virSecretDefParse(NULL, filename);
244 245 246
}

static int
247
virSecretDefFormatUsage(virBufferPtr buf,
248 249 250 251 252 253
                        const virSecretDefPtr def)
{
    const char *type;

    type = virSecretUsageTypeTypeToString(def->usage_type);
    if (type == NULL) {
254 255 256
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unexpected secret usage type %d"),
                       def->usage_type);
257 258
        return -1;
    }
259
    virBufferAsprintf(buf, "  <usage type='%s'>\n", type);
260 261 262 263 264 265 266 267 268 269
    switch (def->usage_type) {
    case VIR_SECRET_USAGE_TYPE_NONE:
        break;

    case VIR_SECRET_USAGE_TYPE_VOLUME:
        if (def->usage.volume != NULL)
            virBufferEscapeString(buf, "    <volume>%s</volume>\n",
                                  def->usage.volume);
        break;

S
Sage Weil 已提交
270 271 272 273 274 275 276
    case VIR_SECRET_USAGE_TYPE_CEPH:
        if (def->usage.ceph != NULL) {
            virBufferEscapeString(buf, "    <name>%s</name>\n",
                                  def->usage.ceph);
        }
        break;

277 278 279 280 281 282 283
    case VIR_SECRET_USAGE_TYPE_ISCSI:
        if (def->usage.target != NULL) {
            virBufferEscapeString(buf, "    <target>%s</target>\n",
                                  def->usage.target);
        }
        break;

284
    default:
285 286 287
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unexpected secret usage type %d"),
                       def->usage_type);
288 289 290 291 292 293 294 295
        return -1;
    }
    virBufferAddLit(buf, "  </usage>\n");

    return 0;
}

char *
296
virSecretDefFormat(const virSecretDefPtr def)
297 298
{
    virBuffer buf = VIR_BUFFER_INITIALIZER;
299 300
    unsigned char *uuid;
    char uuidstr[VIR_UUID_STRING_BUFLEN];
301

302
    virBufferAsprintf(&buf, "<secret ephemeral='%s' private='%s'>\n",
303 304
                      def->ephemeral ? "yes" : "no",
                      def->private ? "yes" : "no");
305 306 307 308

    uuid = def->uuid;
    virUUIDFormat(uuid, uuidstr);
    virBufferEscapeString(&buf, "  <uuid>%s</uuid>\n", uuidstr);
309 310 311 312
    if (def->description != NULL)
        virBufferEscapeString(&buf, "  <description>%s</description>\n",
                              def->description);
    if (def->usage_type != VIR_SECRET_USAGE_TYPE_NONE &&
313
        virSecretDefFormatUsage(&buf, def) < 0)
314 315 316 317 318 319 320 321 322
        goto error;
    virBufferAddLit(&buf, "</secret>\n");

    if (virBufferError(&buf))
        goto no_memory;

    return virBufferContentAndReset(&buf);

 no_memory:
323
    virReportOOMError();
324
 error:
325
    virBufferFreeAndReset(&buf);
326 327
    return NULL;
}