2 Unix SMB/CIFS implementation.
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/>.
25 #include "librpc/ndr/libndr.h"
26 #include "librpc/gen_ndr/ndr_misc.h"
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
);
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
)
61 if ((hexchars
& 1) || hexchars
> 16) {
62 return NT_STATUS_INVALID_PARAMETER
;
65 for (i
= 0; i
< hexchars
; i
++) {
68 if (c
>= '0' && c
<= '9') {
71 else if (c
>= 'a' && c
<= 'f') {
74 else if (c
>= 'A' && c
<= 'F') {
78 /* BAD character (including '\0') */
79 return NT_STATUS_INVALID_PARAMETER
;
87 static NTSTATUS
parse_guid_string(const char *s
,
90 uint32_t *time_hi_and_version
,
97 /* "e12b56b6-0a95-11d1-adbb-00c04fd8d5cd"
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
;
112 status
= read_hex_bytes(s
, 4, &tmp
);
113 if (!NT_STATUS_IS_OK(status
) || s
[4] != '-') {
114 return NT_STATUS_INVALID_PARAMETER
;
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
;
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
;
135 return NT_STATUS_INVALID_PARAMETER
;
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
;
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
;
159 uint32_t time_mid
, time_hi_and_version
;
160 uint32_t clock_seq
[2];
164 DATA_BLOB blob16
= data_blob_const(buf16
, sizeof(buf16
));
167 if (s
->data
== NULL
) {
168 return NT_STATUS_INVALID_PARAMETER
;
174 status
= parse_guid_string((char *)s
->data
,
177 &time_hi_and_version
,
184 if (s
->data
[0] != '{' || s
->data
[37] != '}') {
188 status
= parse_guid_string((char *)s
->data
+ 1,
191 &time_hi_and_version
,
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
;
205 return GUID_from_ndr_blob(s
, guid
);
208 return GUID_from_ndr_blob(s
, guid
);
210 status
= NT_STATUS_INVALID_PARAMETER
;
214 if (!NT_STATUS_IS_OK(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];
224 guid
->node
[i
] = node
[i
];
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)
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;
254 * generate an empty GUID
256 _PUBLIC_
struct GUID
GUID_zero(void)
265 _PUBLIC_
bool GUID_all_zero(const struct GUID
*u
)
267 if (u
->time_low
!= 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)) {
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
)
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
,
333 guid
->node
[0], guid
->node
[1],
334 guid
->node
[2], guid
->node
[3],
335 guid
->node
[4], guid
->node
[5]);
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
);
347 _PUBLIC_
char *GUID_hexstring(TALLOC_CTX
*mem_ctx
, const struct GUID
*guid
)
354 tmp_mem
= talloc_new(mem_ctx
);
358 status
= GUID_to_ndr_blob(guid
, tmp_mem
, &guid_blob
);
359 if (!NT_STATUS_IS_OK(status
)) {
360 talloc_free(tmp_mem
);
364 ret
= data_blob_hex_string_upper(mem_ctx
, &guid_blob
);
365 talloc_free(tmp_mem
);
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
) {
381 return (memcmp(hnd1
, hnd2
, sizeof(*hnd1
)) == 0);