viruuid.c 6.4 KB
Newer Older
1
/*
2
 * viruuid.c: helper APIs for dealing with UUIDs
3
 *
4
 * Copyright (C) 2007-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
#include <config.h>
22

23
#include "viruuid.h"
24 25 26 27 28 29 30 31

#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>

#include "internal.h"
32
#include "virerror.h"
33
#include "virlog.h"
34
#include "viralloc.h"
E
Eric Blake 已提交
35
#include "virfile.h"
36
#include "virrandom.h"
37

38 39
VIR_LOG_INIT("util.uuid");

40 41
static unsigned char host_uuid[VIR_UUID_BUFLEN];

42 43
/**
 * virUUIDGenerate:
44
 * @uuid: array of VIR_UUID_BUFLEN bytes to store the new UUID
45 46 47 48 49
 *
 * Generates a randomized unique identifier.
 *
 * Returns 0 in case of success and -1 in case of failure
 */
50
int
D
Daniel P. Berrange 已提交
51
virUUIDGenerate(unsigned char *uuid)
52
{
53
    if (uuid == NULL)
54
        return -1;
55

56 57
    if (virRandomBytes(uuid, VIR_UUID_BUFLEN) < 0)
        return -1;
58

M
Milos Vyletel 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
    /*
     * Make UUID RFC 4122 compliant. Following form will be used:
     *
     * xxxxxxxx-xxxx-Axxx-Bxxx-xxxxxxxxxxxx
     *
     * where
     * A is version defined in 4.1.3 of RFC
     *  Msb0  Msb1  Msb2  Msb3   Version  Description
     *   0     1     0     0        4     The randomly or pseudo-
     *                                    randomly generated version
     *                                    specified in this document.
     *
     * B is variant defined in 4.1.1 of RFC
     *  Msb0  Msb1  Msb2  Description
     *   1     0     x    The variant specified in this document.
     */
    uuid[6] = (uuid[6] & 0x0F) | (4 << 4);
    uuid[8] = (uuid[8] & 0x3F) | (2 << 6);

78
    return 0;
79 80
}

81 82
/**
 * virUUIDParse:
83 84
 * @uuidstr: zero terminated string representation of the UUID
 * @uuid: array of VIR_UUID_BUFLEN bytes to store the raw UUID
85 86 87 88 89 90
 *
 * Parses the external string representation, allowing spaces and '-'
 * character in the sequence, and storing the result as a raw UUID
 *
 * Returns 0 in case of success and -1 in case of error.
 */
91
int
92 93
virUUIDParse(const char *uuidstr, unsigned char *uuid)
{
94
    const char *cur;
95
    size_t i;
96 97 98

    /*
     * do a liberal scan allowing '-' and ' ' anywhere between character
99 100
     * pairs, and surrounding whitespace, as long as there are exactly
     * 32 hexadecimal digits the end.
101
     */
102
    cur = uuidstr;
103
    while (g_ascii_isspace(*cur))
104 105
        cur++;

106
    for (i = 0; i < VIR_UUID_BUFLEN;) {
107
        int val;
108
        uuid[i] = 0;
109
        if (*cur == 0)
110
            return -1;
111 112 113 114
        if ((*cur == '-') || (*cur == ' ')) {
            cur++;
            continue;
        }
115
        if ((val = g_ascii_xdigit_value(*cur)) < 0)
116
            return -1;
117
        uuid[i] = 16 * val;
118 119
        cur++;
        if (*cur == 0)
120
            return -1;
121
        if ((val = g_ascii_xdigit_value(*cur)) < 0)
122
            return -1;
123
        uuid[i] += val;
124 125 126 127
        i++;
        cur++;
    }

128
    while (*cur) {
129
        if (!g_ascii_isspace(*cur))
130
            return -1;
131 132 133
        cur++;
    }

134 135 136
    return 0;
}

137 138
/**
 * virUUIDFormat:
139
 * @uuid: array of VIR_UUID_BUFLEN bytes to store the raw UUID
140 141 142 143 144 145
 * @uuidstr: array of VIR_UUID_STRING_BUFLEN bytes to store the
 * string representation of the UUID in. The resulting string
 * will be NULL terminated.
 *
 * Converts the raw UUID into printable format, with embedded '-'
 *
146
 * Returns a pointer to the resulting character string.
147
 */
148 149
const char *
virUUIDFormat(const unsigned char *uuid, char *uuidstr)
150
{
151 152 153 154 155 156
    g_snprintf(uuidstr, VIR_UUID_STRING_BUFLEN,
               "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
               uuid[0], uuid[1], uuid[2], uuid[3],
               uuid[4], uuid[5], uuid[6], uuid[7],
               uuid[8], uuid[9], uuid[10], uuid[11],
               uuid[12], uuid[13], uuid[14], uuid[15]);
157
    uuidstr[VIR_UUID_STRING_BUFLEN-1] = '\0';
158
    return uuidstr;
159
}
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175



/**
 * virUUIDIsValid
 *
 * @uuid: The UUID to test
 *
 * Do some basic tests to check whether the given UUID is
 * valid as a host UUID.
 * Basic tests:
 *  - Not all of the digits may be equal
 */
int
virUUIDIsValid(unsigned char *uuid)
{
176 177
    size_t i;
    unsigned int ctr = 1;
178 179 180 181 182 183 184 185 186 187 188
    unsigned char c;

    if (!uuid)
        return 0;

    c = uuid[0];

    for (i = 1; i < VIR_UUID_BUFLEN; i++)
        if (uuid[i] == c)
            ctr++;

189
    return ctr != VIR_UUID_BUFLEN;
190 191 192 193 194
}

static int
getDMISystemUUID(char *uuid, int len)
{
195
    size_t i = 0;
196 197 198 199 200 201 202
    const char *paths[] = {
        "/sys/devices/virtual/dmi/id/product_uuid",
        "/sys/class/dmi/id/product_uuid",
        NULL
    };

    while (paths[i]) {
203 204
        if (virFileReadBufQuiet(paths[i], uuid, len) == len - 1)
            return 0;
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
        i++;
    }

    return -1;
}


/**
 * setHostUUID
 *
 * @host_uuid: UUID that the host is supposed to have
 *
 * Set the UUID of the host if it hasn't been set, yet
 * Returns 0 in case of success, an error code in case of error.
 */
int
virSetHostUUIDStr(const char *uuid)
{
    int rc;
    char dmiuuid[VIR_UUID_STRING_BUFLEN];

    if (virUUIDIsValid(host_uuid))
        return EEXIST;

    if (!uuid) {
C
Chris Lalancette 已提交
230
        memset(dmiuuid, 0, sizeof(dmiuuid));
231
        if (!getDMISystemUUID(dmiuuid, sizeof(dmiuuid))) {
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
            if (!virUUIDParse(dmiuuid, host_uuid))
                return 0;
        }

        if (!virUUIDIsValid(host_uuid))
            return virUUIDGenerate(host_uuid);
    } else {
        rc = virUUIDParse(uuid, host_uuid);
        if (rc)
            return rc;
        if (!virUUIDIsValid(host_uuid))
            return EINVAL;
    }

    return 0;
}

/**
 * getHostUUID:
 *
 * @host_uuid: memory to store the host_uuid into
 *
 * Get the UUID of the host. Returns 0 in case of success,
 * an error code otherwise.
 * Returns 0 in case of success, an error code in case of error.
 */
int virGetHostUUID(unsigned char *uuid)
{
    int ret = 0;

    if (!virUUIDIsValid(host_uuid))
        ret = virSetHostUUIDStr(NULL);

    memcpy(uuid, host_uuid, sizeof(host_uuid));

    return ret;
}