secret_conf.c 8.3 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 31
#include "secret_conf.h"
#include "virterror_internal.h"
32
#include "virutil.h"
33
#include "xml.h"
34
#include "viruuid.h"
35 36 37

#define VIR_FROM_THIS VIR_FROM_SECRET

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

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 已提交
56 57
    case VIR_SECRET_USAGE_TYPE_CEPH:
        VIR_FREE(def->usage.ceph);
E
Eric Blake 已提交
58
        break;
S
Sage Weil 已提交
59

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

static int
68
virSecretDefParseUsage(xmlXPathContextPtr ctxt,
69 70 71 72 73
                       virSecretDefPtr def)
{
    char *type_str;
    int type;

74
    type_str = virXPathString("string(./usage/@type)", ctxt);
75
    if (type_str == NULL) {
76 77
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("unknown secret usage type"));
78 79 80 81
        return -1;
    }
    type = virSecretUsageTypeTypeFromString(type_str);
    if (type < 0) {
82 83
        virReportError(VIR_ERR_XML_ERROR,
                       _("unknown secret usage type %s"), type_str);
84 85 86 87 88 89 90 91 92 93
        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:
94
        def->usage.volume = virXPathString("string(./usage/volume)", ctxt);
95
        if (!def->usage.volume) {
96 97
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("volume usage specified, but volume path is missing"));
98 99
            return -1;
        }
100 101
        break;

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

111
    default:
112 113 114
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unexpected secret usage type %d"),
                       def->usage_type);
115 116 117 118 119 120
        return -1;
    }
    return 0;
}

static virSecretDefPtr
121
secretXMLParseNode(xmlDocPtr xml, xmlNodePtr root)
122 123 124 125
{
    xmlXPathContextPtr ctxt = NULL;
    virSecretDefPtr def = NULL, ret = NULL;
    char *prop = NULL;
126
    char *uuidstr = NULL;
127 128

    if (!xmlStrEqual(root->name, BAD_CAST "secret")) {
129 130 131 132
        virReportError(VIR_ERR_XML_ERROR,
                       _("unexpected root element <%s>, "
                         "expecting <secret>"),
                       root->name);
133 134 135 136 137
        goto cleanup;
    }

    ctxt = xmlXPathNewContext(xml);
    if (ctxt == NULL) {
138
        virReportOOMError();
139 140 141 142 143
        goto cleanup;
    }
    ctxt->node = root;

    if (VIR_ALLOC(def) < 0) {
144
        virReportOOMError();
145 146 147
        goto cleanup;
    }

148
    prop = virXPathString("string(./@ephemeral)", ctxt);
149 150 151 152 153 154
    if (prop != NULL) {
        if (STREQ(prop, "yes"))
            def->ephemeral = 1;
        else if (STREQ(prop, "no"))
            def->ephemeral = 0;
        else {
155 156
            virReportError(VIR_ERR_XML_ERROR, "%s",
                           _("invalid value of 'ephemeral'"));
157 158 159 160 161
            goto cleanup;
        }
        VIR_FREE(prop);
    }

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

176
    uuidstr = virXPathString("string(./uuid)", ctxt);
177 178
    if (!uuidstr) {
        if (virUUIDGenerate(def->uuid)) {
179 180
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           "%s", _("Failed to generate UUID"));
181 182 183 184
            goto cleanup;
        }
    } else {
        if (virUUIDParse(uuidstr, def->uuid) < 0) {
185 186
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           "%s", _("malformed uuid element"));
187 188 189 190 191
            goto cleanup;
        }
        VIR_FREE(uuidstr);
    }

192 193
    def->description = virXPathString("string(./description)", ctxt);
    if (virXPathNode("./usage", ctxt) != NULL
194
        && virSecretDefParseUsage(ctxt, def) < 0)
195 196 197 198 199 200
        goto cleanup;
    ret = def;
    def = NULL;

 cleanup:
    VIR_FREE(prop);
P
Phil Petty 已提交
201
    VIR_FREE(uuidstr);
202 203 204 205 206 207
    virSecretDefFree(def);
    xmlXPathFreeContext(ctxt);
    return ret;
}

static virSecretDefPtr
J
Jiri Denemark 已提交
208 209
virSecretDefParse(const char *xmlStr,
                  const char *filename)
210
{
J
Jiri Denemark 已提交
211
    xmlDocPtr xml;
212 213
    virSecretDefPtr ret = NULL;

214
    if ((xml = virXMLParse(filename, xmlStr, _("(definition_of_secret)")))) {
J
Jiri Denemark 已提交
215 216
        ret = secretXMLParseNode(xml, xmlDocGetRootElement(xml));
        xmlFreeDoc(xml);
217 218 219 220 221 222
    }

    return ret;
}

virSecretDefPtr
223
virSecretDefParseString(const char *xmlStr)
224
{
225
    return virSecretDefParse(xmlStr, NULL);
226 227 228
}

virSecretDefPtr
229
virSecretDefParseFile(const char *filename)
230
{
231
    return virSecretDefParse(NULL, filename);
232 233 234
}

static int
235
virSecretDefFormatUsage(virBufferPtr buf,
236 237 238 239 240 241
                        const virSecretDefPtr def)
{
    const char *type;

    type = virSecretUsageTypeTypeToString(def->usage_type);
    if (type == NULL) {
242 243 244
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unexpected secret usage type %d"),
                       def->usage_type);
245 246
        return -1;
    }
247
    virBufferAsprintf(buf, "  <usage type='%s'>\n", type);
248 249 250 251 252 253 254 255 256 257
    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 已提交
258 259 260 261 262 263 264
    case VIR_SECRET_USAGE_TYPE_CEPH:
        if (def->usage.ceph != NULL) {
            virBufferEscapeString(buf, "    <name>%s</name>\n",
                                  def->usage.ceph);
        }
        break;

265
    default:
266 267 268
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unexpected secret usage type %d"),
                       def->usage_type);
269 270 271 272 273 274 275 276
        return -1;
    }
    virBufferAddLit(buf, "  </usage>\n");

    return 0;
}

char *
277
virSecretDefFormat(const virSecretDefPtr def)
278 279
{
    virBuffer buf = VIR_BUFFER_INITIALIZER;
280 281
    unsigned char *uuid;
    char uuidstr[VIR_UUID_STRING_BUFLEN];
282

283
    virBufferAsprintf(&buf, "<secret ephemeral='%s' private='%s'>\n",
284 285
                      def->ephemeral ? "yes" : "no",
                      def->private ? "yes" : "no");
286 287 288 289

    uuid = def->uuid;
    virUUIDFormat(uuid, uuidstr);
    virBufferEscapeString(&buf, "  <uuid>%s</uuid>\n", uuidstr);
290 291 292 293
    if (def->description != NULL)
        virBufferEscapeString(&buf, "  <description>%s</description>\n",
                              def->description);
    if (def->usage_type != VIR_SECRET_USAGE_TYPE_NONE &&
294
        virSecretDefFormatUsage(&buf, def) < 0)
295 296 297 298 299 300 301 302 303
        goto error;
    virBufferAddLit(&buf, "</secret>\n");

    if (virBufferError(&buf))
        goto no_memory;

    return virBufferContentAndReset(&buf);

 no_memory:
304
    virReportOOMError();
305
 error:
306
    virBufferFreeAndReset(&buf);
307 308
    return NULL;
}