1 /*
2    Unix SMB/CIFS implementation.
3 
4    UUID/GUID functions
5 
6    Copyright (C) Theodore Ts'o               1996, 1997,
7    Copyright (C) Jim McDonough                     2002.
8    Copyright (C) Andrew Tridgell                   2003.
9 
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14 
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19 
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 */
23 
24 #include "includes.h"
25 #include "librpc/ndr/libndr.h"
26 #include "librpc/gen_ndr/ndr_misc.h"
27 #include "lib/util/util_str_hex.h"
28 /**
29   build a NDR blob from a GUID
30 */
GUID_to_ndr_blob(const struct GUID * guid,TALLOC_CTX * mem_ctx,DATA_BLOB * b)31 _PUBLIC_ NTSTATUS GUID_to_ndr_blob(const struct GUID *guid, TALLOC_CTX *mem_ctx, DATA_BLOB *b)
32 {
33 	enum ndr_err_code ndr_err;
34 	*b = data_blob_talloc(mem_ctx, NULL, 16);
35 	if (b->data == NULL) {
36 		return NT_STATUS_NO_MEMORY;
37 	}
38 	ndr_err = ndr_push_struct_into_fixed_blob(
39 		b, guid, (ndr_push_flags_fn_t)ndr_push_GUID);
40 	return ndr_map_error2ntstatus(ndr_err);
41 }
42 
43 
44 /**
45   build a GUID from a NDR data blob
46 */
GUID_from_ndr_blob(const DATA_BLOB * b,struct GUID * guid)47 _PUBLIC_ NTSTATUS GUID_from_ndr_blob(const DATA_BLOB *b, struct GUID *guid)
48 {
49 	enum ndr_err_code ndr_err =
50 		ndr_pull_struct_blob_all_noalloc(b, guid,
51 						 (ndr_pull_flags_fn_t)ndr_pull_GUID);
52 	return ndr_map_error2ntstatus(ndr_err);
53 }
54 
55 
56 /**
57   build a GUID from a string
58 */
GUID_from_data_blob(const DATA_BLOB * s,struct GUID * guid)59 _PUBLIC_ NTSTATUS GUID_from_data_blob(const DATA_BLOB *s, struct GUID *guid)
60 {
61 	NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
62 	uint32_t time_low = 0;
63 	uint32_t time_mid = 0;
64 	uint32_t time_hi_and_version = 0;
65 	uint32_t clock_seq[2] = {0};
66 	uint32_t node[6] = {0};
67 	uint8_t buf16[16] = {0};
68 
69 	DATA_BLOB blob16 = data_blob_const(buf16, sizeof(buf16));
70 	int i;
71 
72 	if (s->data == NULL) {
73 		return NT_STATUS_INVALID_PARAMETER;
74 	}
75 
76 	switch(s->length) {
77 	case 36:
78 	{
79 		status = parse_guid_string((char *)s->data,
80 					   &time_low,
81 					   &time_mid,
82 					   &time_hi_and_version,
83 					   clock_seq,
84 					   node);
85 		break;
86 	}
87 	case 38:
88 	{
89 		if (s->data[0] != '{' || s->data[37] != '}') {
90 			break;
91 		}
92 
93 		status = parse_guid_string((char *)s->data + 1,
94 					   &time_low,
95 					   &time_mid,
96 					   &time_hi_and_version,
97 					   clock_seq,
98 					   node);
99 		break;
100 	}
101 	case 32:
102 	{
103 		size_t rlen = strhex_to_str((char *)blob16.data, blob16.length,
104 					    (const char *)s->data, s->length);
105 		if (rlen != blob16.length) {
106 			return NT_STATUS_INVALID_PARAMETER;
107 		}
108 
109 		s = &blob16;
110 		return GUID_from_ndr_blob(s, guid);
111 	}
112 	case 16:
113 		return GUID_from_ndr_blob(s, guid);
114 	default:
115 		status = NT_STATUS_INVALID_PARAMETER;
116 		break;
117 	}
118 
119 	if (!NT_STATUS_IS_OK(status)) {
120 		return status;
121 	}
122 
123 	guid->time_low = time_low;
124 	guid->time_mid = time_mid;
125 	guid->time_hi_and_version = time_hi_and_version;
126 	guid->clock_seq[0] = clock_seq[0];
127 	guid->clock_seq[1] = clock_seq[1];
128 	for (i=0;i<6;i++) {
129 		guid->node[i] = node[i];
130 	}
131 
132 	return NT_STATUS_OK;
133 }
134 
135 /**
136   build a GUID from a string
137 */
GUID_from_string(const char * s,struct GUID * guid)138 _PUBLIC_ NTSTATUS GUID_from_string(const char *s, struct GUID *guid)
139 {
140 	DATA_BLOB blob = data_blob_string_const(s);
141 	return GUID_from_data_blob(&blob, guid);
142 }
143 
144 /**
145  * generate a random GUID
146  */
GUID_random(void)147 _PUBLIC_ struct GUID GUID_random(void)
148 {
149 	struct GUID guid;
150 
151 	generate_random_buffer((uint8_t *)&guid, sizeof(guid));
152 	guid.clock_seq[0] = (guid.clock_seq[0] & 0x3F) | 0x80;
153 	guid.time_hi_and_version = (guid.time_hi_and_version & 0x0FFF) | 0x4000;
154 
155 	return guid;
156 }
157 
158 /**
159  * generate an empty GUID
160  */
GUID_zero(void)161 _PUBLIC_ struct GUID GUID_zero(void)
162 {
163 	struct GUID guid;
164 
165 	ZERO_STRUCT(guid);
166 
167 	return guid;
168 }
169 
GUID_all_zero(const struct GUID * u)170 _PUBLIC_ bool GUID_all_zero(const struct GUID *u)
171 {
172 	if (u->time_low != 0 ||
173 	    u->time_mid != 0 ||
174 	    u->time_hi_and_version != 0 ||
175 	    u->clock_seq[0] != 0 ||
176 	    u->clock_seq[1] != 0 ||
177 	    !all_zero(u->node, 6)) {
178 		return false;
179 	}
180 	return true;
181 }
182 
GUID_equal(const struct GUID * u1,const struct GUID * u2)183 _PUBLIC_ bool GUID_equal(const struct GUID *u1, const struct GUID *u2)
184 {
185 	return (GUID_compare(u1, u2) == 0);
186 }
187 
GUID_compare(const struct GUID * u1,const struct GUID * u2)188 _PUBLIC_ int GUID_compare(const struct GUID *u1, const struct GUID *u2)
189 {
190 	if (u1->time_low != u2->time_low) {
191 		return u1->time_low > u2->time_low ? 1 : -1;
192 	}
193 
194 	if (u1->time_mid != u2->time_mid) {
195 		return u1->time_mid > u2->time_mid ? 1 : -1;
196 	}
197 
198 	if (u1->time_hi_and_version != u2->time_hi_and_version) {
199 		return u1->time_hi_and_version > u2->time_hi_and_version ? 1 : -1;
200 	}
201 
202 	if (u1->clock_seq[0] != u2->clock_seq[0]) {
203 		return u1->clock_seq[0] > u2->clock_seq[0] ? 1 : -1;
204 	}
205 
206 	if (u1->clock_seq[1] != u2->clock_seq[1]) {
207 		return u1->clock_seq[1] > u2->clock_seq[1] ? 1 : -1;
208 	}
209 
210 	return memcmp(u1->node, u2->node, 6);
211 }
212 
213 /**
214   its useful to be able to display these in debugging messages
215 */
GUID_string(TALLOC_CTX * mem_ctx,const struct GUID * guid)216 _PUBLIC_ char *GUID_string(TALLOC_CTX *mem_ctx, const struct GUID *guid)
217 {
218 	struct GUID_txt_buf buf;
219 	return talloc_strdup(mem_ctx, GUID_buf_string(guid, &buf));
220 }
221 
222 /**
223  * Does the same without allocating memory, using the structure buffer.
224  * Useful for debug messages, so that you do not have to talloc_free the result
225  */
GUID_buf_string(const struct GUID * guid,struct GUID_txt_buf * dst)226 _PUBLIC_ char* GUID_buf_string(const struct GUID *guid,
227 			       struct GUID_txt_buf *dst)
228 {
229 	if (!guid) {
230 		return NULL;
231 	}
232 	snprintf(dst->buf, sizeof(dst->buf),
233 		 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
234 		 guid->time_low, guid->time_mid,
235 		 guid->time_hi_and_version,
236 		 guid->clock_seq[0],
237 		 guid->clock_seq[1],
238 		 guid->node[0], guid->node[1],
239 		 guid->node[2], guid->node[3],
240 		 guid->node[4], guid->node[5]);
241 	return dst->buf;
242 }
243 
GUID_string2(TALLOC_CTX * mem_ctx,const struct GUID * guid)244 _PUBLIC_ char *GUID_string2(TALLOC_CTX *mem_ctx, const struct GUID *guid)
245 {
246 	char *ret, *s = GUID_string(mem_ctx, guid);
247 	ret = talloc_asprintf(mem_ctx, "{%s}", s);
248 	talloc_free(s);
249 	return ret;
250 }
251 
GUID_hexstring(TALLOC_CTX * mem_ctx,const struct GUID * guid)252 _PUBLIC_ char *GUID_hexstring(TALLOC_CTX *mem_ctx, const struct GUID *guid)
253 {
254 	char *ret;
255 	DATA_BLOB guid_blob;
256 	TALLOC_CTX *tmp_mem;
257 	NTSTATUS status;
258 
259 	tmp_mem = talloc_new(mem_ctx);
260 	if (!tmp_mem) {
261 		return NULL;
262 	}
263 	status = GUID_to_ndr_blob(guid, tmp_mem, &guid_blob);
264 	if (!NT_STATUS_IS_OK(status)) {
265 		talloc_free(tmp_mem);
266 		return NULL;
267 	}
268 
269 	ret = data_blob_hex_string_upper(mem_ctx, &guid_blob);
270 	talloc_free(tmp_mem);
271 	return ret;
272 }
273 
ndr_policy_handle_empty(const struct policy_handle * h)274 _PUBLIC_ bool ndr_policy_handle_empty(const struct policy_handle *h)
275 {
276 	return (h->handle_type == 0 && GUID_all_zero(&h->uuid));
277 }
278 
ndr_policy_handle_equal(const struct policy_handle * hnd1,const struct policy_handle * hnd2)279 _PUBLIC_ bool ndr_policy_handle_equal(const struct policy_handle *hnd1,
280 				  const struct policy_handle *hnd2)
281 {
282 	if (!hnd1 || !hnd2) {
283 		return false;
284 	}
285 
286 	return (memcmp(hnd1, hnd2, sizeof(*hnd1)) == 0);
287 }
288