auth/common: add support for auth4_ctx->check_ntlm_password_send/recv()
[Samba.git] / librpc / ndr / uuid.c
blob037dd909fce3e83c8de92334f433ed152a4b8889
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 "includes.h"
25 #include "librpc/ndr/libndr.h"
26 #include "librpc/gen_ndr/ndr_misc.h"
28 /**
29 build a NDR blob from a GUID
31 _PUBLIC_ NTSTATUS GUID_to_ndr_blob(const struct GUID *guid, TALLOC_CTX *mem_ctx, DATA_BLOB *b)
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;
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);
44 /**
45 build a GUID from a NDR data blob
47 _PUBLIC_ NTSTATUS GUID_from_ndr_blob(const DATA_BLOB *b, struct GUID *guid)
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);
55 static NTSTATUS read_hex_bytes(const char *s, uint hexchars, uint64_t *dest)
57 uint64_t x = 0;
58 uint i;
59 char c;
61 if ((hexchars & 1) || hexchars > 16) {
62 return NT_STATUS_INVALID_PARAMETER;
65 for (i = 0; i < hexchars; i++) {
66 x <<= 4;
67 c = s[i];
68 if (c >= '0' && c <= '9') {
69 x += c - '0';
71 else if (c >= 'a' && c <= 'f') {
72 x += c - 'a' + 10;
74 else if (c >= 'A' && c <= 'F') {
75 x += c - 'A' + 10;
77 else {
78 /* BAD character (including '\0') */
79 return NT_STATUS_INVALID_PARAMETER;
82 *dest = x;
83 return NT_STATUS_OK;
87 static NTSTATUS parse_guid_string(const char *s,
88 uint32_t *time_low,
89 uint32_t *time_mid,
90 uint32_t *time_hi_and_version,
91 uint32_t *clock_seq,
92 uint32_t *node)
94 uint64_t tmp;
95 NTSTATUS status;
96 int i;
97 /* "e12b56b6-0a95-11d1-adbb-00c04fd8d5cd"
98 | | | | |
99 | | | | \ node[6]
100 | | | \_____ clock_seq[2]
101 | | \__________ time_hi_and_version
102 | \_______________ time_mid
103 \_____________________ time_low
105 status = read_hex_bytes(s, 8, &tmp);
106 if (!NT_STATUS_IS_OK(status) || s[8] != '-') {
107 return NT_STATUS_INVALID_PARAMETER;
109 *time_low = tmp;
110 s += 9;
112 status = read_hex_bytes(s, 4, &tmp);
113 if (!NT_STATUS_IS_OK(status) || s[4] != '-') {
114 return NT_STATUS_INVALID_PARAMETER;
116 *time_mid = tmp;
117 s += 5;
119 status = read_hex_bytes(s, 4, &tmp);
120 if (!NT_STATUS_IS_OK(status) || s[4] != '-') {
121 return NT_STATUS_INVALID_PARAMETER;
123 *time_hi_and_version = tmp;
124 s += 5;
126 for (i = 0; i < 2; i++) {
127 status = read_hex_bytes(s, 2, &tmp);
128 if (!NT_STATUS_IS_OK(status)) {
129 return NT_STATUS_INVALID_PARAMETER;
131 clock_seq[i] = tmp;
132 s += 2;
134 if (s[0] != '-') {
135 return NT_STATUS_INVALID_PARAMETER;
138 s++;
140 for (i = 0; i < 6; i++) {
141 status = read_hex_bytes(s, 2, &tmp);
142 if (!NT_STATUS_IS_OK(status)) {
143 return NT_STATUS_INVALID_PARAMETER;
145 node[i] = tmp;
146 s += 2;
149 return NT_STATUS_OK;
153 build a GUID from a string
155 _PUBLIC_ NTSTATUS GUID_from_data_blob(const DATA_BLOB *s, struct GUID *guid)
157 NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
158 uint32_t time_low;
159 uint32_t time_mid, time_hi_and_version;
160 uint32_t clock_seq[2];
161 uint32_t node[6];
162 uint8_t buf16[16];
164 DATA_BLOB blob16 = data_blob_const(buf16, sizeof(buf16));
165 int i;
167 if (s->data == NULL) {
168 return NT_STATUS_INVALID_PARAMETER;
171 switch(s->length) {
172 case 36:
174 status = parse_guid_string((char *)s->data,
175 &time_low,
176 &time_mid,
177 &time_hi_and_version,
178 clock_seq,
179 node);
180 break;
182 case 38:
184 if (s->data[0] != '{' || s->data[37] != '}') {
185 break;
188 status = parse_guid_string((char *)s->data + 1,
189 &time_low,
190 &time_mid,
191 &time_hi_and_version,
192 clock_seq,
193 node);
194 break;
196 case 32:
198 size_t rlen = strhex_to_str((char *)blob16.data, blob16.length,
199 (const char *)s->data, s->length);
200 if (rlen != blob16.length) {
201 return NT_STATUS_INVALID_PARAMETER;
204 s = &blob16;
205 return GUID_from_ndr_blob(s, guid);
207 case 16:
208 return GUID_from_ndr_blob(s, guid);
209 default:
210 status = NT_STATUS_INVALID_PARAMETER;
211 break;
214 if (!NT_STATUS_IS_OK(status)) {
215 return status;
218 guid->time_low = time_low;
219 guid->time_mid = time_mid;
220 guid->time_hi_and_version = time_hi_and_version;
221 guid->clock_seq[0] = clock_seq[0];
222 guid->clock_seq[1] = clock_seq[1];
223 for (i=0;i<6;i++) {
224 guid->node[i] = node[i];
227 return NT_STATUS_OK;
231 build a GUID from a string
233 _PUBLIC_ NTSTATUS GUID_from_string(const char *s, struct GUID *guid)
235 DATA_BLOB blob = data_blob_string_const(s);
236 return GUID_from_data_blob(&blob, guid);
240 * generate a random GUID
242 _PUBLIC_ struct GUID GUID_random(void)
244 struct GUID guid;
246 generate_random_buffer((uint8_t *)&guid, sizeof(guid));
247 guid.clock_seq[0] = (guid.clock_seq[0] & 0x3F) | 0x80;
248 guid.time_hi_and_version = (guid.time_hi_and_version & 0x0FFF) | 0x4000;
250 return guid;
254 * generate an empty GUID
256 _PUBLIC_ struct GUID GUID_zero(void)
258 struct GUID guid;
260 ZERO_STRUCT(guid);
262 return guid;
265 _PUBLIC_ bool GUID_all_zero(const struct GUID *u)
267 if (u->time_low != 0 ||
268 u->time_mid != 0 ||
269 u->time_hi_and_version != 0 ||
270 u->clock_seq[0] != 0 ||
271 u->clock_seq[1] != 0 ||
272 !all_zero(u->node, 6)) {
273 return false;
275 return true;
278 _PUBLIC_ bool GUID_equal(const struct GUID *u1, const struct GUID *u2)
280 return (GUID_compare(u1, u2) == 0);
283 _PUBLIC_ int GUID_compare(const struct GUID *u1, const struct GUID *u2)
285 if (u1->time_low != u2->time_low) {
286 return u1->time_low > u2->time_low ? 1 : -1;
289 if (u1->time_mid != u2->time_mid) {
290 return u1->time_mid > u2->time_mid ? 1 : -1;
293 if (u1->time_hi_and_version != u2->time_hi_and_version) {
294 return u1->time_hi_and_version > u2->time_hi_and_version ? 1 : -1;
297 if (u1->clock_seq[0] != u2->clock_seq[0]) {
298 return u1->clock_seq[0] > u2->clock_seq[0] ? 1 : -1;
301 if (u1->clock_seq[1] != u2->clock_seq[1]) {
302 return u1->clock_seq[1] > u2->clock_seq[1] ? 1 : -1;
305 return memcmp(u1->node, u2->node, 6);
309 its useful to be able to display these in debugging messages
311 _PUBLIC_ char *GUID_string(TALLOC_CTX *mem_ctx, const struct GUID *guid)
313 struct GUID_txt_buf buf;
314 return talloc_strdup(mem_ctx, GUID_buf_string(guid, &buf));
318 * Does the same without allocating memory, using the structure buffer.
319 * Useful for debug messages, so that you do not have to talloc_free the result
321 _PUBLIC_ char* GUID_buf_string(const struct GUID *guid,
322 struct GUID_txt_buf *dst)
324 if (!guid) {
325 return NULL;
327 snprintf(dst->buf, sizeof(dst->buf),
328 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
329 guid->time_low, guid->time_mid,
330 guid->time_hi_and_version,
331 guid->clock_seq[0],
332 guid->clock_seq[1],
333 guid->node[0], guid->node[1],
334 guid->node[2], guid->node[3],
335 guid->node[4], guid->node[5]);
336 return dst->buf;
339 _PUBLIC_ char *GUID_string2(TALLOC_CTX *mem_ctx, const struct GUID *guid)
341 char *ret, *s = GUID_string(mem_ctx, guid);
342 ret = talloc_asprintf(mem_ctx, "{%s}", s);
343 talloc_free(s);
344 return ret;
347 _PUBLIC_ char *GUID_hexstring(TALLOC_CTX *mem_ctx, const struct GUID *guid)
349 char *ret;
350 DATA_BLOB guid_blob;
351 TALLOC_CTX *tmp_mem;
352 NTSTATUS status;
354 tmp_mem = talloc_new(mem_ctx);
355 if (!tmp_mem) {
356 return NULL;
358 status = GUID_to_ndr_blob(guid, tmp_mem, &guid_blob);
359 if (!NT_STATUS_IS_OK(status)) {
360 talloc_free(tmp_mem);
361 return NULL;
364 ret = data_blob_hex_string_upper(mem_ctx, &guid_blob);
365 talloc_free(tmp_mem);
366 return ret;
369 _PUBLIC_ bool ndr_policy_handle_empty(const struct policy_handle *h)
371 return (h->handle_type == 0 && GUID_all_zero(&h->uuid));
374 _PUBLIC_ bool ndr_policy_handle_equal(const struct policy_handle *hnd1,
375 const struct policy_handle *hnd2)
377 if (!hnd1 || !hnd2) {
378 return false;
381 return (memcmp(hnd1, hnd2, sizeof(*hnd1)) == 0);