1 /* Copyright (c) 2011-2018 Dovecot authors, see the included COPYING file */
2 
3 #include "lib.h"
4 #include "ioloop.h"
5 #include "buffer.h"
6 #include "str.h"
7 #include "sha1.h"
8 #include "hash.h"
9 #include "hex-binary.h"
10 #include "hostpid.h"
11 #include "guid.h"
12 
13 #include <unistd.h>
14 #include <time.h>
15 
guid_generate(void)16 const char *guid_generate(void)
17 {
18 	static struct timespec ts = { 0, 0 };
19 	static unsigned int pid = 0;
20 
21 	/* we'll use the current time in nanoseconds as the initial 64bit
22 	   counter. */
23 	if (ts.tv_sec == 0) {
24 		if (clock_gettime(CLOCK_REALTIME, &ts) < 0)
25 			i_fatal("clock_gettime() failed: %m");
26 		pid = getpid();
27 	} else if ((uint32_t)ts.tv_nsec < (uint32_t)-1) {
28 		ts.tv_nsec++;
29 	} else {
30 		ts.tv_sec++;
31 		ts.tv_nsec = 0;
32 	}
33 	return t_strdup_printf("%08x%08lx.%x.%s",
34 			       (unsigned int)ts.tv_nsec,
35 			       (unsigned long)ts.tv_sec,
36 			       pid, my_hostname);
37 }
38 
guid_128_host_hash_get(const char * host,unsigned char hash_r[STATIC_ARRAY GUID_128_HOST_HASH_SIZE])39 void guid_128_host_hash_get(const char *host,
40 			    unsigned char hash_r[STATIC_ARRAY GUID_128_HOST_HASH_SIZE])
41 {
42 	unsigned char full_hash[SHA1_RESULTLEN];
43 
44 	sha1_get_digest(host, strlen(host), full_hash);
45 	memcpy(hash_r, full_hash + sizeof(full_hash)-GUID_128_HOST_HASH_SIZE,
46 	       GUID_128_HOST_HASH_SIZE);
47 }
48 
guid_128_generate(guid_128_t guid_r)49 void guid_128_generate(guid_128_t guid_r)
50 {
51 #if GUID_128_HOST_HASH_SIZE != 4
52 #  error GUID_128_HOST_HASH_SIZE must be 4
53 #endif
54 	static struct timespec ts = { 0, 0 };
55 	static uint8_t guid_static[8];
56 	uint32_t pid;
57 
58 	/* we'll use the current time in nanoseconds as the initial 64bit
59 	   counter. */
60 	if (ts.tv_sec == 0) {
61 		if (clock_gettime(CLOCK_REALTIME, &ts) < 0)
62 			i_fatal("clock_gettime() failed: %m");
63 		pid = getpid();
64 
65 		guid_static[0] = (pid & 0x000000ff);
66 		guid_static[1] = (pid & 0x0000ff00) >> 8;
67 		guid_static[2] = (pid & 0x00ff0000) >> 16;
68 		guid_static[3] = (pid & 0xff000000) >> 24;
69 		guid_128_host_hash_get(my_hostdomain(), guid_static+4);
70 	} else if (ioloop_timeval.tv_sec > ts.tv_sec ||
71 		   (ioloop_timeval.tv_sec == ts.tv_sec &&
72 		    ioloop_timeval.tv_usec * 1000 > ts.tv_nsec)) {
73 		/* use ioloop's time since we have it. it doesn't provide any
74 		   more uniqueness, but it allows finding out more reliably
75 		   when a GUID was created. */
76 		ts.tv_sec = ioloop_timeval.tv_sec;
77 		ts.tv_nsec = ioloop_timeval.tv_usec*1000;
78 	} else if (ts.tv_nsec < 999999999L) {
79 		ts.tv_nsec++;
80 	} else {
81 		ts.tv_sec++;
82 		ts.tv_nsec = 0;
83 	}
84 
85 	guid_r[0] = (ts.tv_nsec & 0x000000ff);
86 	guid_r[1] = (ts.tv_nsec & 0x0000ff00) >> 8;
87 	guid_r[2] = (ts.tv_nsec & 0x00ff0000) >> 16;
88 	guid_r[3] = (ts.tv_nsec & 0xff000000) >> 24;
89 	guid_r[4] = (ts.tv_sec & 0x000000ff);
90 	guid_r[5] = (ts.tv_sec & 0x0000ff00) >> 8;
91 	guid_r[6] = (ts.tv_sec & 0x00ff0000) >> 16;
92 	guid_r[7] = (ts.tv_sec & 0xff000000) >> 24;
93 	memcpy(guid_r + 8, guid_static, 8);
94 }
95 
guid_128_is_empty(const guid_128_t guid)96 bool guid_128_is_empty(const guid_128_t guid)
97 {
98 	unsigned int i;
99 
100 	for (i = 0; i < GUID_128_SIZE; i++) {
101 		if (guid[i] != 0)
102 			return FALSE;
103 	}
104 	return TRUE;
105 }
106 
guid_128_equals(const guid_128_t guid1,const guid_128_t guid2)107 bool guid_128_equals(const guid_128_t guid1, const guid_128_t guid2)
108 {
109 	return memcmp(guid1, guid2, GUID_128_SIZE) == 0;
110 }
111 
guid_128_from_string(const char * str,guid_128_t guid_r)112 int guid_128_from_string(const char *str, guid_128_t guid_r)
113 {
114 	buffer_t buf;
115 
116 	buffer_create_from_data(&buf, guid_r, GUID_128_SIZE);
117 	return strlen(str) == GUID_128_SIZE*2 &&
118 		hex_to_binary(str, &buf) == 0 &&
119 		buf.used == GUID_128_SIZE ? 0 : -1;
120 }
121 
guid_128_to_string(const guid_128_t guid)122 const char *guid_128_to_string(const guid_128_t guid)
123 {
124 	return binary_to_hex(guid, GUID_128_SIZE);
125 }
126 
guid_128_hash(const guid_128_t guid)127 unsigned int guid_128_hash(const guid_128_t guid)
128 {
129 	return mem_hash(guid, GUID_128_SIZE);
130 }
131 
guid_128_cmp(const guid_128_t guid1,const guid_128_t guid2)132 int guid_128_cmp(const guid_128_t guid1, const guid_128_t guid2)
133 {
134 	return memcmp(guid1, guid2, GUID_128_SIZE);
135 }
136 
guid_128_to_uuid_string(const guid_128_t guid,enum uuid_format format)137 const char *guid_128_to_uuid_string(const guid_128_t guid, enum uuid_format format)
138 {
139 	switch(format) {
140 	case FORMAT_COMPACT:
141 		return guid_128_to_string(guid);
142 	case FORMAT_RECORD:
143 		return t_strdup_printf("%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
144 				guid[0], guid[1], guid[2], guid[3], guid[4],
145 				guid[5], guid[6], guid[7], guid[8], guid[9],
146 				guid[10], guid[11], guid[12], guid[13], guid[14],
147 				guid[15]);
148 	case FORMAT_MICROSOFT:
149 		return t_strdup_printf("{%s}", guid_128_to_uuid_string(guid, FORMAT_RECORD));
150 	}
151 	i_unreached();
152 }
153 
guid_128_from_uuid_string(const char * str,guid_128_t guid_r)154 int guid_128_from_uuid_string(const char *str, guid_128_t guid_r)
155 {
156 	size_t i,len,m=0;
157 	int ret;
158 	T_BEGIN {
159 		len = strlen(str);
160 		string_t *str2 = t_str_new(len);
161 		for(i=0; i < len; i++) {
162 			/* Microsoft format */
163 			if (i==0 && str[i] == '{') { m=1; continue; }
164 			else if (i == len-1 && str[i] == '}') continue;
165 			/* 8-4-4-4-12 */
166 			if (((i==8+m) || (i==13+m) || (i==18+m) || (i==23+m)) &&
167 			    str[i] == '-') continue;
168 			str_append_c(str2, str[i]);
169 		}
170 		ret = guid_128_from_string(str_c(str2), guid_r);
171 	} T_END;
172 
173 	return ret;
174 }
175