wb_queryuser: avoid idmap_child() and use idmap_child_handle() instead
[Samba.git] / librpc / ndr / uuid.c
blobad5c9d37d1139e4539f5e7674fb9b7bf74f007f7
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/samba_util.h"
26 #include "lib/util/genrand.h"
27 #include "librpc/ndr/libndr.h"
28 #include "librpc/gen_ndr/ndr_misc.h"
29 #include "lib/util/util_str_hex.h"
31 _PUBLIC_ NTSTATUS GUID_to_ndr_buf(
32 const struct GUID *guid, struct GUID_ndr_buf *buf)
34 DATA_BLOB b = { .data = buf->buf, .length = sizeof(buf->buf), };
35 enum ndr_err_code ndr_err;
37 ndr_err = ndr_push_struct_into_fixed_blob(
38 &b, guid, (ndr_push_flags_fn_t)ndr_push_GUID);
39 return ndr_map_error2ntstatus(ndr_err);
42 /**
43 build a NDR blob from a GUID
45 _PUBLIC_ NTSTATUS GUID_to_ndr_blob(const struct GUID *guid, TALLOC_CTX *mem_ctx, DATA_BLOB *b)
47 struct GUID_ndr_buf buf = { .buf = {0}, };
48 NTSTATUS status;
50 status = GUID_to_ndr_buf(guid, &buf);
51 if (!NT_STATUS_IS_OK(status)) {
52 return status;
55 *b = data_blob_talloc(mem_ctx, buf.buf, sizeof(buf.buf));
56 if (b->data == NULL) {
57 return NT_STATUS_NO_MEMORY;
59 return NT_STATUS_OK;
63 /**
64 build a GUID from a NDR data blob
66 _PUBLIC_ NTSTATUS GUID_from_ndr_blob(const DATA_BLOB *b, struct GUID *guid)
68 enum ndr_err_code ndr_err =
69 ndr_pull_struct_blob_all_noalloc(b, guid,
70 (ndr_pull_flags_fn_t)ndr_pull_GUID);
71 return ndr_map_error2ntstatus(ndr_err);
75 /**
76 build a GUID from a string
78 _PUBLIC_ NTSTATUS GUID_from_data_blob(const DATA_BLOB *s, struct GUID *guid)
80 NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
81 uint32_t time_low = 0;
82 uint32_t time_mid = 0;
83 uint32_t time_hi_and_version = 0;
84 uint32_t clock_seq[2] = {0};
85 uint32_t node[6] = {0};
86 uint8_t buf16[16] = {0};
88 DATA_BLOB blob16 = data_blob_const(buf16, sizeof(buf16));
89 int i;
91 if (s->data == NULL) {
92 return NT_STATUS_INVALID_PARAMETER;
95 switch(s->length) {
96 case 36:
98 status = parse_guid_string((char *)s->data,
99 &time_low,
100 &time_mid,
101 &time_hi_and_version,
102 clock_seq,
103 node);
104 break;
106 case 38:
108 if (s->data[0] != '{' || s->data[37] != '}') {
109 break;
112 status = parse_guid_string((char *)s->data + 1,
113 &time_low,
114 &time_mid,
115 &time_hi_and_version,
116 clock_seq,
117 node);
118 break;
120 case 32:
122 size_t rlen = strhex_to_str((char *)blob16.data, blob16.length,
123 (const char *)s->data, s->length);
124 if (rlen != blob16.length) {
125 return NT_STATUS_INVALID_PARAMETER;
128 s = &blob16;
129 return GUID_from_ndr_blob(s, guid);
131 case 16:
132 return GUID_from_ndr_blob(s, guid);
133 default:
134 status = NT_STATUS_INVALID_PARAMETER;
135 break;
138 if (!NT_STATUS_IS_OK(status)) {
139 return status;
142 guid->time_low = time_low;
143 guid->time_mid = time_mid;
144 guid->time_hi_and_version = time_hi_and_version;
145 guid->clock_seq[0] = clock_seq[0];
146 guid->clock_seq[1] = clock_seq[1];
147 for (i=0;i<6;i++) {
148 guid->node[i] = node[i];
151 return NT_STATUS_OK;
155 build a GUID from a string
157 _PUBLIC_ NTSTATUS GUID_from_string(const char *s, struct GUID *guid)
159 DATA_BLOB blob = data_blob_string_const(s);
160 return GUID_from_data_blob(&blob, guid);
164 * generate a random GUID
166 _PUBLIC_ struct GUID GUID_random(void)
168 struct GUID guid;
170 generate_random_buffer((uint8_t *)&guid, sizeof(guid));
171 guid.clock_seq[0] = (guid.clock_seq[0] & 0x3F) | 0x80;
172 guid.time_hi_and_version = (guid.time_hi_and_version & 0x0FFF) | 0x4000;
174 return guid;
178 * generate an empty GUID
180 _PUBLIC_ struct GUID GUID_zero(void)
182 struct GUID guid;
184 ZERO_STRUCT(guid);
186 return guid;
189 _PUBLIC_ bool GUID_all_zero(const struct GUID *u)
191 if (u->time_low != 0 ||
192 u->time_mid != 0 ||
193 u->time_hi_and_version != 0 ||
194 u->clock_seq[0] != 0 ||
195 u->clock_seq[1] != 0 ||
196 !all_zero(u->node, 6)) {
197 return false;
199 return true;
202 _PUBLIC_ bool GUID_equal(const struct GUID *u1, const struct GUID *u2)
204 return (GUID_compare(u1, u2) == 0);
207 _PUBLIC_ int GUID_compare(const struct GUID *u1, const struct GUID *u2)
209 if (u1->time_low != u2->time_low) {
210 return u1->time_low > u2->time_low ? 1 : -1;
213 if (u1->time_mid != u2->time_mid) {
214 return u1->time_mid > u2->time_mid ? 1 : -1;
217 if (u1->time_hi_and_version != u2->time_hi_and_version) {
218 return u1->time_hi_and_version > u2->time_hi_and_version ? 1 : -1;
221 if (u1->clock_seq[0] != u2->clock_seq[0]) {
222 return u1->clock_seq[0] > u2->clock_seq[0] ? 1 : -1;
225 if (u1->clock_seq[1] != u2->clock_seq[1]) {
226 return u1->clock_seq[1] > u2->clock_seq[1] ? 1 : -1;
229 return memcmp(u1->node, u2->node, 6);
233 its useful to be able to display these in debugging messages
235 _PUBLIC_ char *GUID_string(TALLOC_CTX *mem_ctx, const struct GUID *guid)
237 struct GUID_txt_buf buf;
238 return talloc_strdup(mem_ctx, GUID_buf_string(guid, &buf));
242 * Does the same without allocating memory, using the structure buffer.
243 * Useful for debug messages, so that you do not have to talloc_free the result
245 _PUBLIC_ char* GUID_buf_string(const struct GUID *guid,
246 struct GUID_txt_buf *dst)
248 if (!guid) {
249 return NULL;
251 snprintf(dst->buf, sizeof(dst->buf),
252 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
253 guid->time_low, guid->time_mid,
254 guid->time_hi_and_version,
255 guid->clock_seq[0],
256 guid->clock_seq[1],
257 guid->node[0], guid->node[1],
258 guid->node[2], guid->node[3],
259 guid->node[4], guid->node[5]);
260 return dst->buf;
263 _PUBLIC_ char *GUID_string2(TALLOC_CTX *mem_ctx, const struct GUID *guid)
265 char *ret, *s = GUID_string(mem_ctx, guid);
266 ret = talloc_asprintf(mem_ctx, "{%s}", s);
267 talloc_free(s);
268 return ret;
271 _PUBLIC_ char *GUID_hexstring(TALLOC_CTX *mem_ctx, const struct GUID *guid)
273 char *ret;
274 DATA_BLOB guid_blob;
275 TALLOC_CTX *tmp_mem;
276 NTSTATUS status;
278 tmp_mem = talloc_new(mem_ctx);
279 if (!tmp_mem) {
280 return NULL;
282 status = GUID_to_ndr_blob(guid, tmp_mem, &guid_blob);
283 if (!NT_STATUS_IS_OK(status)) {
284 talloc_free(tmp_mem);
285 return NULL;
288 ret = data_blob_hex_string_upper(mem_ctx, &guid_blob);
289 talloc_free(tmp_mem);
290 return ret;
293 _PUBLIC_ bool ndr_policy_handle_empty(const struct policy_handle *h)
295 return (h->handle_type == 0 && GUID_all_zero(&h->uuid));
298 _PUBLIC_ bool ndr_policy_handle_equal(const struct policy_handle *hnd1,
299 const struct policy_handle *hnd2)
301 if (!hnd1 || !hnd2) {
302 return false;
305 return (memcmp(hnd1, hnd2, sizeof(*hnd1)) == 0);