storage_encryption_conf.c 7.5 KB
Newer Older
1
/*
2
 * storage_encryption_conf.c: volume encryption information
3
 *
4
 * Copyright (C) 2009-2014 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
 *
 * Red Hat Author: Miloslav Trmač <mitr@redhat.com>
 */

#include <config.h>

25 26 27
#include <fcntl.h>
#include <unistd.h>

28 29
#include "internal.h"

30
#include "virbuffer.h"
31
#include "viralloc.h"
32 33
#include "storage_conf.h"
#include "storage_encryption_conf.h"
34
#include "virxml.h"
35
#include "virerror.h"
36
#include "viruuid.h"
E
Eric Blake 已提交
37
#include "virfile.h"
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

#define VIR_FROM_THIS VIR_FROM_STORAGE

VIR_ENUM_IMPL(virStorageEncryptionSecretType,
              VIR_STORAGE_ENCRYPTION_SECRET_TYPE_LAST, "passphrase")

VIR_ENUM_IMPL(virStorageEncryptionFormat,
              VIR_STORAGE_ENCRYPTION_FORMAT_LAST,
              "default", "qcow")

static void
virStorageEncryptionSecretFree(virStorageEncryptionSecretPtr secret)
{
    if (!secret)
        return;
    VIR_FREE(secret);
}

void
virStorageEncryptionFree(virStorageEncryptionPtr enc)
{
    size_t i;

    if (!enc)
        return;

    for (i = 0; i < enc->nsecrets; i++)
        virStorageEncryptionSecretFree(enc->secrets[i]);
    VIR_FREE(enc->secrets);
    VIR_FREE(enc);
}

static virStorageEncryptionSecretPtr
71
virStorageEncryptionSecretParse(xmlXPathContextPtr ctxt,
72 73 74 75 76 77
                                xmlNodePtr node)
{
    xmlNodePtr old_node;
    virStorageEncryptionSecretPtr ret;
    char *type_str;
    int type;
78
    char *uuidstr = NULL;
79

80
    if (VIR_ALLOC(ret) < 0)
81 82 83 84 85
        return NULL;

    old_node = ctxt->node;
    ctxt->node = node;

86
    type_str = virXPathString("string(./@type)", ctxt);
87
    if (type_str == NULL) {
88 89
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("unknown volume encryption secret type"));
90 91 92 93
        goto cleanup;
    }
    type = virStorageEncryptionSecretTypeTypeFromString(type_str);
    if (type < 0) {
94
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
95 96
                       _("unknown volume encryption secret type %s"),
                       type_str);
97 98 99 100 101 102
        VIR_FREE(type_str);
        goto cleanup;
    }
    VIR_FREE(type_str);
    ret->type = type;

103
    uuidstr = virXPathString("string(./@uuid)", ctxt);
104 105
    if (uuidstr) {
        if (virUUIDParse(uuidstr, ret->uuid) < 0) {
106 107 108
            virReportError(VIR_ERR_XML_ERROR,
                           _("malformed volume encryption uuid '%s'"),
                           uuidstr);
109 110
            goto cleanup;
        }
111
        VIR_FREE(uuidstr);
112
    } else {
113 114
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("missing volume encryption uuid"));
115 116
        goto cleanup;
    }
117 118 119 120 121
    ctxt->node = old_node;
    return ret;

  cleanup:
    virStorageEncryptionSecretFree(ret);
122
    VIR_FREE(uuidstr);
123 124 125 126 127
    ctxt->node = old_node;
    return NULL;
}

static virStorageEncryptionPtr
128
virStorageEncryptionParseXML(xmlXPathContextPtr ctxt)
129 130 131 132
{
    xmlNodePtr *nodes = NULL;
    virStorageEncryptionPtr ret;
    char *format_str;
133 134
    int format, n;
    size_t i;
135

136
    if (VIR_ALLOC(ret) < 0)
137 138
        return NULL;

139
    format_str = virXPathString("string(./@format)", ctxt);
140
    if (format_str == NULL) {
141 142
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("unknown volume encryption format"));
143 144 145 146
        goto cleanup;
    }
    format = virStorageEncryptionFormatTypeFromString(format_str);
    if (format < 0) {
147
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
148 149
                       _("unknown volume encryption format type %s"),
                       format_str);
150 151 152 153 154 155
        VIR_FREE(format_str);
        goto cleanup;
    }
    VIR_FREE(format_str);
    ret->format = format;

156
    n = virXPathNodeSet("./secret", ctxt, &nodes);
157 158 159
    if (n < 0){
        goto cleanup;
    }
160
    if (n != 0 && VIR_ALLOC_N(ret->secrets, n) < 0)
161 162 163
        goto cleanup;
    ret->nsecrets = n;
    for (i = 0; i < n; i++) {
164
        ret->secrets[i] = virStorageEncryptionSecretParse(ctxt, nodes[i]);
165 166 167 168 169 170 171 172 173 174 175 176 177 178
        if (ret->secrets[i] == NULL)
            goto cleanup;
    }
    VIR_FREE(nodes);

    return ret;

  cleanup:
    VIR_FREE(nodes);
    virStorageEncryptionFree(ret);
    return NULL;
}

virStorageEncryptionPtr
179
virStorageEncryptionParseNode(xmlDocPtr xml, xmlNodePtr root)
180 181 182 183 184
{
    xmlXPathContextPtr ctxt = NULL;
    virStorageEncryptionPtr enc = NULL;

    if (STRNEQ((const char *) root->name, "encryption")) {
185 186 187
        virReportError(VIR_ERR_XML_ERROR,
                       "%s", _("unknown root element for volume "
                               "encryption information"));
188 189 190 191 192
        goto cleanup;
    }

    ctxt = xmlXPathNewContext(xml);
    if (ctxt == NULL) {
193
        virReportOOMError();
194 195 196 197
        goto cleanup;
    }

    ctxt->node = root;
198
    enc = virStorageEncryptionParseXML(ctxt);
199 200 201 202 203

  cleanup:
    xmlXPathFreeContext(ctxt);
    return enc;
}
204

205 206

static int
207
virStorageEncryptionSecretFormat(virBufferPtr buf,
208
                                 virStorageEncryptionSecretPtr secret)
209 210
{
    const char *type;
211
    char uuidstr[VIR_UUID_STRING_BUFLEN];
212 213 214

    type = virStorageEncryptionSecretTypeTypeToString(secret->type);
    if (!type) {
215 216
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("unexpected volume encryption secret type"));
217 218 219
        return -1;
    }

220
    virUUIDFormat(secret->uuid, uuidstr);
221
    virBufferAsprintf(buf, "<secret type='%s' uuid='%s'/>\n",
222
                      type, uuidstr);
223 224 225 226
    return 0;
}

int
227
virStorageEncryptionFormat(virBufferPtr buf,
228
                           virStorageEncryptionPtr enc)
229 230 231 232 233 234
{
    const char *format;
    size_t i;

    format = virStorageEncryptionFormatTypeToString(enc->format);
    if (!format) {
235 236
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("unexpected encryption format"));
237 238
        return -1;
    }
239
    virBufferAsprintf(buf, "<encryption format='%s'>\n", format);
240
    virBufferAdjustIndent(buf, 2);
241 242

    for (i = 0; i < enc->nsecrets; i++) {
243
        if (virStorageEncryptionSecretFormat(buf, enc->secrets[i]) < 0)
244 245 246
            return -1;
    }

247
    virBufferAdjustIndent(buf, -2);
248
    virBufferAddLit(buf, "</encryption>\n");
249 250 251

    return 0;
}
252 253

int
254
virStorageGenerateQcowPassphrase(unsigned char *dest)
255 256 257 258 259 260 261 262 263
{
    int fd;
    size_t i;

    /* A qcow passphrase is up to 16 bytes, with any data following a NUL
       ignored.  Prohibit control and non-ASCII characters to avoid possible
       unpleasant surprises with the qemu monitor input mechanism. */
    fd = open("/dev/urandom", O_RDONLY);
    if (fd < 0) {
264 265
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Cannot open /dev/urandom"));
266 267 268 269 270 271 272 273 274
        return -1;
    }
    i = 0;
    while (i < VIR_STORAGE_QCOW_PASSPHRASE_SIZE) {
        ssize_t r;

        while ((r = read(fd, dest + i, 1)) == -1 && errno == EINTR)
            ;
        if (r <= 0) {
275 276
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("Cannot read from /dev/urandom"));
277
            VIR_FORCE_CLOSE(fd);
278 279 280 281 282
            return -1;
        }
        if (dest[i] >= 0x20 && dest[i] <= 0x7E)
            i++; /* Got an acceptable character */
    }
283
    VIR_FORCE_CLOSE(fd);
284 285
    return 0;
}