storage_encryption_conf.c 8.2 KB
Newer Older
1
/*
2
 * storage_encryption_conf.c: volume encryption information
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * Copyright (C) 2009 Red Hat, Inc.
 *
 * 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
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 *
 * Red Hat Author: Miloslav Trmač <mitr@redhat.com>
 */

#include <config.h>

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

28 29 30 31 32 33 34 35 36
#include "internal.h"

#include "buf.h"
#include "memory.h"
#include "storage_conf.h"
#include "storage_encryption_conf.h"
#include "util.h"
#include "xml.h"
#include "virterror_internal.h"
37
#include "uuid.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

#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);
}

70 71
#ifndef PROXY

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

    if (VIR_ALLOC(ret) < 0) {
83
        virReportOOMError();
84 85 86 87 88 89
        return NULL;
    }

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

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

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

  cleanup:
    virStorageEncryptionSecretFree(ret);
126
    VIR_FREE(uuidstr);
127 128 129 130 131 132 133 134 135 136 137 138 139
    ctxt->node = old_node;
    return NULL;
}

static virStorageEncryptionPtr
virStorageEncryptionParseXML(virConnectPtr conn, xmlXPathContextPtr ctxt)
{
    xmlNodePtr *nodes = NULL;
    virStorageEncryptionPtr ret;
    char *format_str;
    int format, i, n;

    if (VIR_ALLOC(ret) < 0) {
140
        virReportOOMError();
141 142 143
        return NULL;
    }

144
    format_str = virXPathString("string(./@format)", ctxt);
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
    if (format_str == NULL) {
        virStorageReportError(conn, VIR_ERR_XML_ERROR, "%s",
                              _("unknown volume encryption format"));
        goto cleanup;
    }
    format = virStorageEncryptionFormatTypeFromString(format_str);
    if (format < 0) {
        virStorageReportError(conn, VIR_ERR_XML_ERROR,
                              _("unknown volume encryption format type %s"),
                              format_str);
        VIR_FREE(format_str);
        goto cleanup;
    }
    VIR_FREE(format_str);
    ret->format = format;

161
    n = virXPathNodeSet("./secret", ctxt, &nodes);
162 163 164 165 166 167
    if (n < 0){
        virStorageReportError(conn, VIR_ERR_INTERNAL_ERROR, "%s",
                              _("cannot extract volume encryption secrets"));
        goto cleanup;
    }
    if (n != 0 && VIR_ALLOC_N(ret->secrets, n) < 0) {
168
        virReportOOMError();
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
        goto cleanup;
    }
    ret->nsecrets = n;
    for (i = 0; i < n; i++) {
        ret->secrets[i] = virStorageEncryptionSecretParse(conn, ctxt, nodes[i]);
        if (ret->secrets[i] == NULL)
            goto cleanup;
    }
    VIR_FREE(nodes);

    return ret;

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

virStorageEncryptionPtr
virStorageEncryptionParseNode(virConnectPtr conn,
                              xmlDocPtr xml, xmlNodePtr root)
{
    xmlXPathContextPtr ctxt = NULL;
    virStorageEncryptionPtr enc = NULL;

    if (STRNEQ((const char *) root->name, "encryption")) {
        virStorageReportError(conn, VIR_ERR_XML_ERROR,
                              "%s", _("unknown root element for volume "
                                      "encryption information"));
        goto cleanup;
    }

    ctxt = xmlXPathNewContext(xml);
    if (ctxt == NULL) {
203
        virReportOOMError();
204 205 206 207 208 209 210 211 212 213
        goto cleanup;
    }

    ctxt->node = root;
    enc = virStorageEncryptionParseXML(conn, ctxt);

  cleanup:
    xmlXPathFreeContext(ctxt);
    return enc;
}
214 215
#endif /* ! PROXY */

216 217 218 219 220 221 222

static int
virStorageEncryptionSecretFormat(virConnectPtr conn,
                                 virBufferPtr buf,
                                 virStorageEncryptionSecretPtr secret)
{
    const char *type;
223
    char uuidstr[VIR_UUID_STRING_BUFLEN];
224 225 226 227 228 229 230 231

    type = virStorageEncryptionSecretTypeTypeToString(secret->type);
    if (!type) {
        virStorageReportError(conn, VIR_ERR_INTERNAL_ERROR, "%s",
                              _("unexpected volume encryption secret type"));
        return -1;
    }

232 233
    virUUIDFormat(secret->uuid, uuidstr);
    virBufferVSprintf(buf, "      <secret type='%s' uuid='%s'/>\n", type, uuidstr);
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
    return 0;
}

int
virStorageEncryptionFormat(virConnectPtr conn,
                           virBufferPtr buf,
                           virStorageEncryptionPtr enc)
{
    const char *format;
    size_t i;

    format = virStorageEncryptionFormatTypeToString(enc->format);
    if (!format) {
        virStorageReportError(conn, VIR_ERR_INTERNAL_ERROR,
                              "%s", _("unexpected encryption format"));
        return -1;
    }
251
    virBufferVSprintf(buf, "    <encryption format='%s'>\n", format);
252 253 254 255 256 257

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

258
    virBufferAddLit(buf, "    </encryption>\n");
259 260 261

    return 0;
}
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295

int
virStorageGenerateQcowPassphrase(virConnectPtr conn, unsigned char *dest)
{
    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) {
        virStorageReportError(conn, VIR_ERR_INTERNAL_ERROR, "%s",
                              _("Cannot open /dev/urandom"));
        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) {
            virStorageReportError(conn, VIR_ERR_INTERNAL_ERROR, "%s",
                                  _("Cannot read from /dev/urandom"));
            close(fd);
            return -1;
        }
        if (dest[i] >= 0x20 && dest[i] <= 0x7E)
            i++; /* Got an acceptable character */
    }
    close(fd);
    return 0;
}