smbd: avoid a panic in close_directory()
[samba4-gss.git] / librpc / ndr / uuid.c
blobce094cc9a50df706de5b9085ac8ffc9f72572b44
1 /*
2 Unix SMB/CIFS implementation.
4 UUID/GUID functions
6 Copyright (C) Theodore Ts'o 1996, 1997,
7 Copyright (C) Jim McDonough 2002.
8 Copyright (C) Andrew Tridgell 2003.
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.
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.
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/>.
24 #include "replace.h"
25 #include "lib/util/debug.h"
26 #include "lib/util/samba_util.h"
27 #include "lib/util/genrand.h"
28 #include "librpc/ndr/libndr.h"
29 #include "librpc/gen_ndr/ndr_misc.h"
30 #include "lib/util/util_str_hex.h"
32 _PUBLIC_ void GUID_to_ndr_buf(const struct GUID *guid,
33 struct GUID_ndr_buf *buf)
35 DATA_BLOB b = { .data = buf->buf, .length = sizeof(buf->buf), };
36 enum ndr_err_code ndr_err;
38 ndr_err = ndr_push_struct_into_fixed_blob(
39 &b, guid, (ndr_push_flags_fn_t)ndr_push_GUID);
40 SMB_ASSERT(NDR_ERR_CODE_IS_SUCCESS(ndr_err));
43 /**
44 build a NDR blob from a GUID
46 _PUBLIC_ NTSTATUS GUID_to_ndr_blob(const struct GUID *guid, TALLOC_CTX *mem_ctx, DATA_BLOB *b)
48 struct GUID_ndr_buf buf = { .buf = {0}, };
50 GUID_to_ndr_buf(guid, &buf);
52 *b = data_blob_talloc(mem_ctx, buf.buf, sizeof(buf.buf));
53 if (b->data == NULL) {
54 return NT_STATUS_NO_MEMORY;
56 return NT_STATUS_OK;
60 /**
61 build a GUID from a NDR data blob
63 _PUBLIC_ NTSTATUS GUID_from_ndr_blob(const DATA_BLOB *b, struct GUID *guid)
65 enum ndr_err_code ndr_err =
66 ndr_pull_struct_blob_all_noalloc(b, guid,
67 (ndr_pull_flags_fn_t)ndr_pull_GUID);
68 return ndr_map_error2ntstatus(ndr_err);
72 /**
73 build a GUID from a string
75 _PUBLIC_ NTSTATUS GUID_from_data_blob(const DATA_BLOB *s, struct GUID *guid)
77 bool ok;
79 if (s->data == NULL) {
80 return NT_STATUS_INVALID_PARAMETER;
83 if (s->length == 36) {
84 ok = parse_guid_string((char *)s->data, guid);
85 return ok ? NT_STATUS_OK : NT_STATUS_INVALID_PARAMETER;
88 if (s->length == 38) {
89 if (s->data[0] != '{' || s->data[37] != '}') {
90 return NT_STATUS_INVALID_PARAMETER;
92 ok = parse_guid_string((char *)s->data + 1, guid);
93 return ok ? NT_STATUS_OK : NT_STATUS_INVALID_PARAMETER;
96 if (s->length == 32) {
97 uint8_t buf16[16] = {0};
98 DATA_BLOB blob16 = { .data = buf16, .length = sizeof(buf16) };
99 size_t rlen = strhex_to_str((char *)blob16.data, blob16.length,
100 (const char *)s->data, s->length);
101 if (rlen != blob16.length) {
102 return NT_STATUS_INVALID_PARAMETER;
105 return GUID_from_ndr_blob(&blob16, guid);
108 if (s->length == 16) {
109 return GUID_from_ndr_blob(s, guid);
112 return NT_STATUS_INVALID_PARAMETER;
116 build a GUID from a string
118 _PUBLIC_ NTSTATUS GUID_from_string(const char *s, struct GUID *guid)
120 DATA_BLOB blob = data_blob_string_const(s);
121 return GUID_from_data_blob(&blob, guid);
125 * generate a random GUID
127 _PUBLIC_ struct GUID GUID_random(void)
129 struct GUID guid;
131 generate_random_buffer((uint8_t *)&guid, sizeof(guid));
132 guid.clock_seq[0] = (guid.clock_seq[0] & 0x3F) | 0x80;
133 guid.time_hi_and_version = (guid.time_hi_and_version & 0x0FFF) | 0x4000;
135 return guid;
139 * generate an empty GUID
141 _PUBLIC_ struct GUID GUID_zero(void)
143 return (struct GUID) { .time_low = 0 };
146 _PUBLIC_ bool GUID_all_zero(const struct GUID *u)
148 if (u->time_low != 0 ||
149 u->time_mid != 0 ||
150 u->time_hi_and_version != 0 ||
151 u->clock_seq[0] != 0 ||
152 u->clock_seq[1] != 0 ||
153 !all_zero(u->node, 6)) {
154 return false;
156 return true;
159 _PUBLIC_ bool GUID_equal(const struct GUID *u1, const struct GUID *u2)
161 return (GUID_compare(u1, u2) == 0);
164 _PUBLIC_ int GUID_compare(const struct GUID *u1, const struct GUID *u2)
166 if (u1->time_low != u2->time_low) {
167 return u1->time_low > u2->time_low ? 1 : -1;
170 if (u1->time_mid != u2->time_mid) {
171 return u1->time_mid > u2->time_mid ? 1 : -1;
174 if (u1->time_hi_and_version != u2->time_hi_and_version) {
175 return u1->time_hi_and_version > u2->time_hi_and_version ? 1 : -1;
178 if (u1->clock_seq[0] != u2->clock_seq[0]) {
179 return u1->clock_seq[0] > u2->clock_seq[0] ? 1 : -1;
182 if (u1->clock_seq[1] != u2->clock_seq[1]) {
183 return u1->clock_seq[1] > u2->clock_seq[1] ? 1 : -1;
186 return memcmp(u1->node, u2->node, 6);
190 its useful to be able to display these in debugging messages
192 _PUBLIC_ char *GUID_string(TALLOC_CTX *mem_ctx, const struct GUID *guid)
194 struct GUID_txt_buf buf;
195 return talloc_strdup(mem_ctx, GUID_buf_string(guid, &buf));
199 * Does the same without allocating memory, using the structure buffer.
200 * Useful for debug messages, so that you do not have to talloc_free the result
202 _PUBLIC_ char* GUID_buf_string(const struct GUID *guid,
203 struct GUID_txt_buf *dst)
205 if (!guid) {
206 return NULL;
209 if (sizeof(dst->buf) < 37) {
210 return NULL;
213 dst->buf[0] = nybble_to_hex_lower(guid->time_low >> 28);
214 dst->buf[1] = nybble_to_hex_lower(guid->time_low >> 24);
215 dst->buf[2] = nybble_to_hex_lower(guid->time_low >> 20);
216 dst->buf[3] = nybble_to_hex_lower(guid->time_low >> 16);
217 dst->buf[4] = nybble_to_hex_lower(guid->time_low >> 12);
218 dst->buf[5] = nybble_to_hex_lower(guid->time_low >> 8);
219 dst->buf[6] = nybble_to_hex_lower(guid->time_low >> 4);
220 dst->buf[7] = nybble_to_hex_lower(guid->time_low);
222 dst->buf[8] = '-';
224 dst->buf[9] = nybble_to_hex_lower(guid->time_mid >> 12);
225 dst->buf[10] = nybble_to_hex_lower(guid->time_mid >> 8);
226 dst->buf[11] = nybble_to_hex_lower(guid->time_mid >> 4);
227 dst->buf[12] = nybble_to_hex_lower(guid->time_mid);
229 dst->buf[13] = '-';
231 dst->buf[14] = nybble_to_hex_lower(guid->time_hi_and_version >> 12);
232 dst->buf[15] = nybble_to_hex_lower(guid->time_hi_and_version >> 8);
233 dst->buf[16] = nybble_to_hex_lower(guid->time_hi_and_version >> 4);
234 dst->buf[17] = nybble_to_hex_lower(guid->time_hi_and_version);
236 dst->buf[18] = '-';
238 dst->buf[19] = nybble_to_hex_lower(guid->clock_seq[0] >> 4);
239 dst->buf[20] = nybble_to_hex_lower(guid->clock_seq[0]);
240 dst->buf[21] = nybble_to_hex_lower(guid->clock_seq[1] >> 4);
241 dst->buf[22] = nybble_to_hex_lower(guid->clock_seq[1]);
243 dst->buf[23] = '-';
245 dst->buf[24] = nybble_to_hex_lower(guid->node[0] >> 4);
246 dst->buf[25] = nybble_to_hex_lower(guid->node[0]);
247 dst->buf[26] = nybble_to_hex_lower(guid->node[1] >> 4);
248 dst->buf[27] = nybble_to_hex_lower(guid->node[1]);
249 dst->buf[28] = nybble_to_hex_lower(guid->node[2] >> 4);
250 dst->buf[29] = nybble_to_hex_lower(guid->node[2]);
251 dst->buf[30] = nybble_to_hex_lower(guid->node[3] >> 4);
252 dst->buf[31] = nybble_to_hex_lower(guid->node[3]);
253 dst->buf[32] = nybble_to_hex_lower(guid->node[4] >> 4);
254 dst->buf[33] = nybble_to_hex_lower(guid->node[4]);
255 dst->buf[34] = nybble_to_hex_lower(guid->node[5] >> 4);
256 dst->buf[35] = nybble_to_hex_lower(guid->node[5]);
258 dst->buf[36] = '\0';
260 return dst->buf;
263 _PUBLIC_ char *GUID_string2(TALLOC_CTX *mem_ctx, const struct GUID *guid)
265 struct GUID_txt_buf buf;
266 char *ret = talloc_asprintf(
267 mem_ctx, "{%s}", GUID_buf_string(guid, &buf));
268 return ret;
271 _PUBLIC_ char *GUID_hexstring(TALLOC_CTX *mem_ctx, const struct GUID *guid)
273 char *ret = NULL;
274 DATA_BLOB guid_blob = { .data = NULL };
275 NTSTATUS status;
277 status = GUID_to_ndr_blob(guid, mem_ctx, &guid_blob);
278 if (NT_STATUS_IS_OK(status)) {
279 ret = data_blob_hex_string_upper(mem_ctx, &guid_blob);
281 TALLOC_FREE(guid_blob.data);
282 return ret;
285 _PUBLIC_ bool ndr_policy_handle_empty(const struct policy_handle *h)
287 return (h->handle_type == 0 && GUID_all_zero(&h->uuid));
290 _PUBLIC_ bool ndr_policy_handle_equal(const struct policy_handle *hnd1,
291 const struct policy_handle *hnd2)
293 if (!hnd1 || !hnd2) {
294 return false;
297 return (memcmp(hnd1, hnd2, sizeof(*hnd1)) == 0);