bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2011-2018 Dovecot authors, see the included COPYING file */
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen#include "lib.h"
c1684fd2affe6159e99bc6de0fd387ce802f0ca6Timo Sirainen#include "ioloop.h"
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen#include "buffer.h"
7c681809344b880aae61e99b6ab74ae19945ff9eAki Tuomi#include "str.h"
74f436ccd2887395bd61ab6f43ce70b7b85bf2c5Timo Sirainen#include "sha1.h"
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen#include "hash.h"
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen#include "hex-binary.h"
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen#include "hostpid.h"
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen#include "guid.h"
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen#include <unistd.h>
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen#include <time.h>
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainenconst char *guid_generate(void)
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen{
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen static struct timespec ts = { 0, 0 };
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen static unsigned int pid = 0;
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen /* we'll use the current time in nanoseconds as the initial 64bit
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen counter. */
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen if (ts.tv_sec == 0) {
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen if (clock_gettime(CLOCK_REALTIME, &ts) < 0)
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen i_fatal("clock_gettime() failed: %m");
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen pid = getpid();
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen } else if ((uint32_t)ts.tv_nsec < (uint32_t)-1) {
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen ts.tv_nsec++;
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen } else {
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen ts.tv_sec++;
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen ts.tv_nsec = 0;
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen }
5b02d2191f2dd9da567c70b383243395ec0ce13dMartti Rannanjärvi return t_strdup_printf("%08x%08lx.%x.%s",
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen (unsigned int)ts.tv_nsec,
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen (unsigned long)ts.tv_sec,
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen pid, my_hostname);
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen}
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen
af49da69a12b5383b89c7b2fa574c2a85b9ca310Timo Sirainenvoid guid_128_host_hash_get(const char *host,
9625595c47c665f5aee57ebfcb1fcbe9ad1bf3a0Martti Rannanjärvi unsigned char hash_r[STATIC_ARRAY GUID_128_HOST_HASH_SIZE])
af49da69a12b5383b89c7b2fa574c2a85b9ca310Timo Sirainen{
af49da69a12b5383b89c7b2fa574c2a85b9ca310Timo Sirainen unsigned char full_hash[SHA1_RESULTLEN];
af49da69a12b5383b89c7b2fa574c2a85b9ca310Timo Sirainen
af49da69a12b5383b89c7b2fa574c2a85b9ca310Timo Sirainen sha1_get_digest(host, strlen(host), full_hash);
af49da69a12b5383b89c7b2fa574c2a85b9ca310Timo Sirainen memcpy(hash_r, full_hash + sizeof(full_hash)-GUID_128_HOST_HASH_SIZE,
af49da69a12b5383b89c7b2fa574c2a85b9ca310Timo Sirainen GUID_128_HOST_HASH_SIZE);
af49da69a12b5383b89c7b2fa574c2a85b9ca310Timo Sirainen}
af49da69a12b5383b89c7b2fa574c2a85b9ca310Timo Sirainen
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainenvoid guid_128_generate(guid_128_t guid_r)
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen{
af49da69a12b5383b89c7b2fa574c2a85b9ca310Timo Sirainen#if GUID_128_HOST_HASH_SIZE != 4
af49da69a12b5383b89c7b2fa574c2a85b9ca310Timo Sirainen# error GUID_128_HOST_HASH_SIZE must be 4
af49da69a12b5383b89c7b2fa574c2a85b9ca310Timo Sirainen#endif
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen static struct timespec ts = { 0, 0 };
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen static uint8_t guid_static[8];
74f436ccd2887395bd61ab6f43ce70b7b85bf2c5Timo Sirainen uint32_t pid;
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen /* we'll use the current time in nanoseconds as the initial 64bit
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen counter. */
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen if (ts.tv_sec == 0) {
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen if (clock_gettime(CLOCK_REALTIME, &ts) < 0)
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen i_fatal("clock_gettime() failed: %m");
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen pid = getpid();
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen guid_static[0] = (pid & 0x000000ff);
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen guid_static[1] = (pid & 0x0000ff00) >> 8;
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen guid_static[2] = (pid & 0x00ff0000) >> 16;
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen guid_static[3] = (pid & 0xff000000) >> 24;
af49da69a12b5383b89c7b2fa574c2a85b9ca310Timo Sirainen guid_128_host_hash_get(my_hostdomain(), guid_static+4);
c1684fd2affe6159e99bc6de0fd387ce802f0ca6Timo Sirainen } else if (ioloop_timeval.tv_sec > ts.tv_sec ||
c1684fd2affe6159e99bc6de0fd387ce802f0ca6Timo Sirainen (ioloop_timeval.tv_sec == ts.tv_sec &&
c104ea8f180db4be2b491ef5b1f2626958dce80dJosef 'Jeff' Sipek ioloop_timeval.tv_usec * 1000 > ts.tv_nsec)) {
c1684fd2affe6159e99bc6de0fd387ce802f0ca6Timo Sirainen /* use ioloop's time since we have it. it doesn't provide any
c1684fd2affe6159e99bc6de0fd387ce802f0ca6Timo Sirainen more uniqueness, but it allows finding out more reliably
c1684fd2affe6159e99bc6de0fd387ce802f0ca6Timo Sirainen when a GUID was created. */
c1684fd2affe6159e99bc6de0fd387ce802f0ca6Timo Sirainen ts.tv_sec = ioloop_timeval.tv_sec;
c1684fd2affe6159e99bc6de0fd387ce802f0ca6Timo Sirainen ts.tv_nsec = ioloop_timeval.tv_usec*1000;
8b2aaea36ac44fc0a07996333ae8565a4e80ea18Josef 'Jeff' Sipek } else if (ts.tv_nsec < 999999999L) {
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen ts.tv_nsec++;
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen } else {
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen ts.tv_sec++;
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen ts.tv_nsec = 0;
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen }
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen guid_r[0] = (ts.tv_nsec & 0x000000ff);
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen guid_r[1] = (ts.tv_nsec & 0x0000ff00) >> 8;
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen guid_r[2] = (ts.tv_nsec & 0x00ff0000) >> 16;
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen guid_r[3] = (ts.tv_nsec & 0xff000000) >> 24;
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen guid_r[4] = (ts.tv_sec & 0x000000ff);
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen guid_r[5] = (ts.tv_sec & 0x0000ff00) >> 8;
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen guid_r[6] = (ts.tv_sec & 0x00ff0000) >> 16;
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen guid_r[7] = (ts.tv_sec & 0xff000000) >> 24;
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen memcpy(guid_r + 8, guid_static, 8);
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen}
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainenbool guid_128_is_empty(const guid_128_t guid)
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen{
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen unsigned int i;
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen for (i = 0; i < GUID_128_SIZE; i++) {
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen if (guid[i] != 0)
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen return FALSE;
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen }
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen return TRUE;
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen}
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen
40ac30b9267c710f5fcdd4b2f6bcd7718a631843Timo Sirainenbool guid_128_equals(const guid_128_t guid1, const guid_128_t guid2)
40ac30b9267c710f5fcdd4b2f6bcd7718a631843Timo Sirainen{
40ac30b9267c710f5fcdd4b2f6bcd7718a631843Timo Sirainen return memcmp(guid1, guid2, GUID_128_SIZE) == 0;
40ac30b9267c710f5fcdd4b2f6bcd7718a631843Timo Sirainen}
40ac30b9267c710f5fcdd4b2f6bcd7718a631843Timo Sirainen
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainenint guid_128_from_string(const char *str, guid_128_t guid_r)
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen{
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen buffer_t buf;
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen
3281669db44d09a087a203201248abbc81b3cc1aTimo Sirainen buffer_create_from_data(&buf, guid_r, GUID_128_SIZE);
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen return strlen(str) == GUID_128_SIZE*2 &&
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen hex_to_binary(str, &buf) == 0 &&
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen buf.used == GUID_128_SIZE ? 0 : -1;
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen}
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainenconst char *guid_128_to_string(const guid_128_t guid)
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen{
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen return binary_to_hex(guid, GUID_128_SIZE);
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen}
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen
efaa1272c1b682bc2e105b2f374152a6a1f45825Phil Carmodyunsigned int guid_128_hash(const guid_128_t guid)
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen{
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen return mem_hash(guid, GUID_128_SIZE);
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen}
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen
efaa1272c1b682bc2e105b2f374152a6a1f45825Phil Carmodyint guid_128_cmp(const guid_128_t guid1, const guid_128_t guid2)
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen{
678d0463849ba777106eb7875f27db07a5d8e3dfTimo Sirainen return memcmp(guid1, guid2, GUID_128_SIZE);
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen}
7c681809344b880aae61e99b6ab74ae19945ff9eAki Tuomi
7c681809344b880aae61e99b6ab74ae19945ff9eAki Tuomiconst char *guid_128_to_uuid_string(const guid_128_t guid, enum uuid_format format)
7c681809344b880aae61e99b6ab74ae19945ff9eAki Tuomi{
7c681809344b880aae61e99b6ab74ae19945ff9eAki Tuomi switch(format) {
7c681809344b880aae61e99b6ab74ae19945ff9eAki Tuomi case FORMAT_COMPACT:
7c681809344b880aae61e99b6ab74ae19945ff9eAki Tuomi return guid_128_to_string(guid);
7c681809344b880aae61e99b6ab74ae19945ff9eAki Tuomi case FORMAT_RECORD:
7c681809344b880aae61e99b6ab74ae19945ff9eAki Tuomi return t_strdup_printf("%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
7c681809344b880aae61e99b6ab74ae19945ff9eAki Tuomi guid[0], guid[1], guid[2], guid[3], guid[4],
7c681809344b880aae61e99b6ab74ae19945ff9eAki Tuomi guid[5], guid[6], guid[7], guid[8], guid[9],
7c681809344b880aae61e99b6ab74ae19945ff9eAki Tuomi guid[10], guid[11], guid[12], guid[13], guid[14],
7c681809344b880aae61e99b6ab74ae19945ff9eAki Tuomi guid[15]);
7c681809344b880aae61e99b6ab74ae19945ff9eAki Tuomi case FORMAT_MICROSOFT:
7c681809344b880aae61e99b6ab74ae19945ff9eAki Tuomi return t_strdup_printf("{%s}", guid_128_to_uuid_string(guid, FORMAT_RECORD));
7c681809344b880aae61e99b6ab74ae19945ff9eAki Tuomi }
7c681809344b880aae61e99b6ab74ae19945ff9eAki Tuomi i_unreached();
7c681809344b880aae61e99b6ab74ae19945ff9eAki Tuomi}
7c681809344b880aae61e99b6ab74ae19945ff9eAki Tuomi
7c681809344b880aae61e99b6ab74ae19945ff9eAki Tuomiint guid_128_from_uuid_string(const char *str, guid_128_t guid_r)
7c681809344b880aae61e99b6ab74ae19945ff9eAki Tuomi{
7c681809344b880aae61e99b6ab74ae19945ff9eAki Tuomi size_t i,len,m=0;
7c681809344b880aae61e99b6ab74ae19945ff9eAki Tuomi int ret;
7c681809344b880aae61e99b6ab74ae19945ff9eAki Tuomi T_BEGIN {
7c681809344b880aae61e99b6ab74ae19945ff9eAki Tuomi len = strlen(str);
7c681809344b880aae61e99b6ab74ae19945ff9eAki Tuomi string_t *str2 = t_str_new(len);
7c681809344b880aae61e99b6ab74ae19945ff9eAki Tuomi for(i=0; i < len; i++) {
7c681809344b880aae61e99b6ab74ae19945ff9eAki Tuomi /* Microsoft format */
7c681809344b880aae61e99b6ab74ae19945ff9eAki Tuomi if (i==0 && str[i] == '{') { m=1; continue; }
7c681809344b880aae61e99b6ab74ae19945ff9eAki Tuomi else if (i == len-1 && str[i] == '}') continue;
7c681809344b880aae61e99b6ab74ae19945ff9eAki Tuomi /* 8-4-4-4-12 */
7c681809344b880aae61e99b6ab74ae19945ff9eAki Tuomi if (((i==8+m) || (i==13+m) || (i==18+m) || (i==23+m)) &&
7c681809344b880aae61e99b6ab74ae19945ff9eAki Tuomi str[i] == '-') continue;
7c681809344b880aae61e99b6ab74ae19945ff9eAki Tuomi str_append_c(str2, str[i]);
7c681809344b880aae61e99b6ab74ae19945ff9eAki Tuomi }
7c681809344b880aae61e99b6ab74ae19945ff9eAki Tuomi ret = guid_128_from_string(str_c(str2), guid_r);
7c681809344b880aae61e99b6ab74ae19945ff9eAki Tuomi } T_END;
7c681809344b880aae61e99b6ab74ae19945ff9eAki Tuomi
7c681809344b880aae61e99b6ab74ae19945ff9eAki Tuomi return ret;
7c681809344b880aae61e99b6ab74ae19945ff9eAki Tuomi}