2007-02-26 15:32:27 +00:00
|
|
|
/*
|
2014-03-07 13:38:51 +00:00
|
|
|
* viruuid.c: helper APIs for dealing with UUIDs
|
2012-12-13 18:01:25 +00:00
|
|
|
*
|
2014-03-18 08:14:35 +00:00
|
|
|
* Copyright (C) 2007-2014 Red Hat, Inc.
|
2007-02-26 15:32:27 +00:00
|
|
|
*
|
|
|
|
* 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
|
2012-09-20 22:30:55 +00:00
|
|
|
* License along with this library. If not, see
|
2012-07-21 10:06:23 +00:00
|
|
|
* <http://www.gnu.org/licenses/>.
|
2007-02-26 15:32:27 +00:00
|
|
|
*/
|
|
|
|
|
2008-01-29 18:15:54 +00:00
|
|
|
#include <config.h>
|
2007-02-26 15:32:27 +00:00
|
|
|
|
2012-12-13 18:01:25 +00:00
|
|
|
#include "viruuid.h"
|
2007-02-26 15:32:27 +00:00
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "internal.h"
|
2012-12-13 18:21:53 +00:00
|
|
|
#include "virerror.h"
|
2012-12-12 17:59:27 +00:00
|
|
|
#include "virlog.h"
|
2012-12-12 18:06:53 +00:00
|
|
|
#include "viralloc.h"
|
2011-07-19 18:32:58 +00:00
|
|
|
#include "virfile.h"
|
2012-01-25 15:17:46 +00:00
|
|
|
#include "virrandom.h"
|
2007-06-26 23:48:46 +00:00
|
|
|
|
2014-02-28 12:16:17 +00:00
|
|
|
VIR_LOG_INIT("util.uuid");
|
|
|
|
|
2010-05-25 14:33:51 +00:00
|
|
|
static unsigned char host_uuid[VIR_UUID_BUFLEN];
|
|
|
|
|
2007-06-29 13:23:13 +00:00
|
|
|
/**
|
|
|
|
* virUUIDGenerate:
|
2007-08-09 20:19:12 +00:00
|
|
|
* @uuid: array of VIR_UUID_BUFLEN bytes to store the new UUID
|
2007-06-29 13:23:13 +00:00
|
|
|
*
|
|
|
|
* Generates a randomized unique identifier.
|
|
|
|
*
|
|
|
|
* Returns 0 in case of success and -1 in case of failure
|
|
|
|
*/
|
2007-02-26 15:32:27 +00:00
|
|
|
int
|
2007-06-26 22:19:38 +00:00
|
|
|
virUUIDGenerate(unsigned char *uuid)
|
2007-02-26 15:32:27 +00:00
|
|
|
{
|
2007-06-29 13:23:13 +00:00
|
|
|
if (uuid == NULL)
|
2012-03-22 11:33:35 +00:00
|
|
|
return -1;
|
2007-06-29 13:23:13 +00:00
|
|
|
|
2018-05-29 06:35:13 +00:00
|
|
|
if (virRandomBytes(uuid, VIR_UUID_BUFLEN) < 0)
|
|
|
|
return -1;
|
2007-02-26 15:32:27 +00:00
|
|
|
|
2013-04-08 18:10:54 +00:00
|
|
|
/*
|
|
|
|
* 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);
|
|
|
|
|
2018-05-29 06:35:13 +00:00
|
|
|
return 0;
|
2007-02-26 15:32:27 +00:00
|
|
|
}
|
|
|
|
|
2007-06-29 13:23:13 +00:00
|
|
|
/**
|
|
|
|
* virUUIDParse:
|
2007-08-09 20:19:12 +00:00
|
|
|
* @uuidstr: zero terminated string representation of the UUID
|
|
|
|
* @uuid: array of VIR_UUID_BUFLEN bytes to store the raw UUID
|
2007-06-29 13:23:13 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2007-02-26 15:34:24 +00:00
|
|
|
int
|
2014-03-18 08:14:35 +00:00
|
|
|
virUUIDParse(const char *uuidstr, unsigned char *uuid)
|
|
|
|
{
|
2007-02-26 15:34:24 +00:00
|
|
|
const char *cur;
|
Convert 'int i' to 'size_t i' in src/util/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
size_t i;
|
2007-02-26 15:34:24 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* do a liberal scan allowing '-' and ' ' anywhere between character
|
2010-01-21 14:28:56 +00:00
|
|
|
* pairs, and surrounding whitespace, as long as there are exactly
|
|
|
|
* 32 hexadecimal digits the end.
|
2007-02-26 15:34:24 +00:00
|
|
|
*/
|
2007-08-09 20:19:12 +00:00
|
|
|
cur = uuidstr;
|
2019-11-18 14:10:02 +00:00
|
|
|
while (g_ascii_isspace(*cur))
|
2010-01-21 14:28:56 +00:00
|
|
|
cur++;
|
|
|
|
|
2013-05-24 16:58:25 +00:00
|
|
|
for (i = 0; i < VIR_UUID_BUFLEN;) {
|
2020-02-23 19:58:53 +00:00
|
|
|
int val;
|
2007-08-09 20:19:12 +00:00
|
|
|
uuid[i] = 0;
|
2007-02-26 15:34:24 +00:00
|
|
|
if (*cur == 0)
|
2020-01-06 21:57:45 +00:00
|
|
|
return -1;
|
2007-02-26 15:34:24 +00:00
|
|
|
if ((*cur == '-') || (*cur == ' ')) {
|
|
|
|
cur++;
|
|
|
|
continue;
|
|
|
|
}
|
2020-02-23 19:58:53 +00:00
|
|
|
if ((val = g_ascii_xdigit_value(*cur)) < 0)
|
2020-01-06 21:57:45 +00:00
|
|
|
return -1;
|
2020-02-23 19:58:53 +00:00
|
|
|
uuid[i] = 16 * val;
|
2007-02-26 15:34:24 +00:00
|
|
|
cur++;
|
|
|
|
if (*cur == 0)
|
2020-01-06 21:57:45 +00:00
|
|
|
return -1;
|
2020-02-23 19:58:53 +00:00
|
|
|
if ((val = g_ascii_xdigit_value(*cur)) < 0)
|
2020-01-06 21:57:45 +00:00
|
|
|
return -1;
|
2020-02-23 19:58:53 +00:00
|
|
|
uuid[i] += val;
|
2007-02-26 15:34:24 +00:00
|
|
|
i++;
|
|
|
|
cur++;
|
|
|
|
}
|
|
|
|
|
2010-01-21 14:28:56 +00:00
|
|
|
while (*cur) {
|
2019-11-18 14:10:02 +00:00
|
|
|
if (!g_ascii_isspace(*cur))
|
2020-01-06 21:57:45 +00:00
|
|
|
return -1;
|
2010-01-21 14:28:56 +00:00
|
|
|
cur++;
|
|
|
|
}
|
|
|
|
|
2007-02-26 15:34:24 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-08-09 20:19:12 +00:00
|
|
|
/**
|
|
|
|
* virUUIDFormat:
|
2019-08-08 07:36:17 +00:00
|
|
|
* @uuid: array of VIR_UUID_BUFLEN bytes to store the raw UUID
|
2007-08-09 20:19:12 +00:00
|
|
|
* @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 '-'
|
|
|
|
*
|
2012-08-02 18:06:58 +00:00
|
|
|
* Returns a pointer to the resulting character string.
|
2007-08-09 20:19:12 +00:00
|
|
|
*/
|
2012-08-02 18:06:58 +00:00
|
|
|
const char *
|
|
|
|
virUUIDFormat(const unsigned char *uuid, char *uuidstr)
|
2007-08-09 20:19:12 +00:00
|
|
|
{
|
2019-11-13 13:53:42 +00:00
|
|
|
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]);
|
2007-08-09 20:19:12 +00:00
|
|
|
uuidstr[VIR_UUID_STRING_BUFLEN-1] = '\0';
|
2012-08-02 18:06:58 +00:00
|
|
|
return uuidstr;
|
2007-08-09 20:19:12 +00:00
|
|
|
}
|
2010-05-25 14:33:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2020-11-24 10:12:27 +00:00
|
|
|
bool
|
|
|
|
virUUIDIsValid(const unsigned char *uuid)
|
2010-05-25 14:33:51 +00:00
|
|
|
{
|
Convert 'int i' to 'size_t i' in src/util/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
size_t i;
|
2010-05-25 14:33:51 +00:00
|
|
|
|
|
|
|
if (!uuid)
|
2020-11-24 10:12:27 +00:00
|
|
|
return false;
|
2010-05-25 14:33:51 +00:00
|
|
|
|
|
|
|
for (i = 1; i < VIR_UUID_BUFLEN; i++)
|
2020-11-24 10:12:27 +00:00
|
|
|
if (uuid[i] != uuid[0])
|
|
|
|
return true;
|
2010-05-25 14:33:51 +00:00
|
|
|
|
2020-11-24 10:12:27 +00:00
|
|
|
return false;
|
2010-05-25 14:33:51 +00:00
|
|
|
}
|
|
|
|
|
2020-11-24 10:12:27 +00:00
|
|
|
|
2010-05-25 14:33:51 +00:00
|
|
|
static int
|
|
|
|
getDMISystemUUID(char *uuid, int len)
|
|
|
|
{
|
Convert 'int i' to 'size_t i' in src/util/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
size_t i = 0;
|
2010-05-25 14:33:51 +00:00
|
|
|
const char *paths[] = {
|
|
|
|
"/sys/devices/virtual/dmi/id/product_uuid",
|
|
|
|
"/sys/class/dmi/id/product_uuid",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
while (paths[i]) {
|
2016-05-03 09:12:40 +00:00
|
|
|
if (virFileReadBufQuiet(paths[i], uuid, len) == len - 1)
|
|
|
|
return 0;
|
2010-05-25 14:33:51 +00:00
|
|
|
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) {
|
2010-07-30 14:19:51 +00:00
|
|
|
memset(dmiuuid, 0, sizeof(dmiuuid));
|
2012-05-04 17:22:22 +00:00
|
|
|
if (!getDMISystemUUID(dmiuuid, sizeof(dmiuuid))) {
|
2010-05-25 14:33:51 +00:00
|
|
|
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;
|
|
|
|
}
|