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 ndr_err
= ndr_push_struct_blob(b
, mem_ctx
, guid
,
35 (ndr_push_flags_fn_t
)ndr_push_GUID
);
36 return ndr_map_error2ntstatus(ndr_err
);
41 build a GUID from a NDR data blob
43 _PUBLIC_ NTSTATUS
GUID_from_ndr_blob(const DATA_BLOB
*b
, struct GUID
*guid
)
45 enum ndr_err_code ndr_err
;
48 mem_ctx
= talloc_new(NULL
);
49 NT_STATUS_HAVE_NO_MEMORY(mem_ctx
);
51 ndr_err
= ndr_pull_struct_blob_all(b
, mem_ctx
, guid
,
52 (ndr_pull_flags_fn_t
)ndr_pull_GUID
);
54 return ndr_map_error2ntstatus(ndr_err
);
59 build a GUID from a string
61 _PUBLIC_ NTSTATUS
GUID_from_data_blob(const DATA_BLOB
*s
, struct GUID
*guid
)
63 NTSTATUS status
= NT_STATUS_INVALID_PARAMETER
;
65 uint32_t time_mid
, time_hi_and_version
;
66 uint32_t clock_seq
[2];
70 DATA_BLOB blob16
= data_blob_const(buf16
, sizeof(buf16
));
73 if (s
->data
== NULL
) {
74 return NT_STATUS_INVALID_PARAMETER
;
83 mem_ctx
= talloc_new(NULL
);
84 NT_STATUS_HAVE_NO_MEMORY(mem_ctx
);
85 string
= talloc_strndup(mem_ctx
, (const char *)s
->data
, s
->length
);
86 NT_STATUS_HAVE_NO_MEMORY(string
);
87 if (11 == sscanf(string
,
88 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
89 &time_low
, &time_mid
, &time_hi_and_version
,
90 &clock_seq
[0], &clock_seq
[1],
91 &node
[0], &node
[1], &node
[2], &node
[3], &node
[4], &node
[5])) {
92 status
= NT_STATUS_OK
;
102 mem_ctx
= talloc_new(NULL
);
103 NT_STATUS_HAVE_NO_MEMORY(mem_ctx
);
104 string
= talloc_strndup(mem_ctx
, (const char *)s
->data
, s
->length
);
105 NT_STATUS_HAVE_NO_MEMORY(string
);
106 if (11 == sscanf((const char *)s
->data
,
107 "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
108 &time_low
, &time_mid
, &time_hi_and_version
,
109 &clock_seq
[0], &clock_seq
[1],
110 &node
[0], &node
[1], &node
[2], &node
[3], &node
[4], &node
[5])) {
111 status
= NT_STATUS_OK
;
113 talloc_free(mem_ctx
);
118 size_t rlen
= strhex_to_str((char *)blob16
.data
, blob16
.length
,
119 (const char *)s
->data
, s
->length
);
120 if (rlen
!= blob16
.length
) {
121 return NT_STATUS_INVALID_PARAMETER
;
125 return GUID_from_ndr_blob(s
, guid
);
128 return GUID_from_ndr_blob(s
, guid
);
130 status
= NT_STATUS_INVALID_PARAMETER
;
134 if (!NT_STATUS_IS_OK(status
)) {
138 guid
->time_low
= time_low
;
139 guid
->time_mid
= time_mid
;
140 guid
->time_hi_and_version
= time_hi_and_version
;
141 guid
->clock_seq
[0] = clock_seq
[0];
142 guid
->clock_seq
[1] = clock_seq
[1];
144 guid
->node
[i
] = node
[i
];
151 build a GUID from a string
153 _PUBLIC_ NTSTATUS
GUID_from_string(const char *s
, struct GUID
*guid
)
155 DATA_BLOB blob
= data_blob_string_const(s
);
156 return GUID_from_data_blob(&blob
, guid
);
160 * generate a random GUID
162 _PUBLIC_
struct GUID
GUID_random(void)
166 generate_random_buffer((uint8_t *)&guid
, sizeof(guid
));
167 guid
.clock_seq
[0] = (guid
.clock_seq
[0] & 0x3F) | 0x80;
168 guid
.time_hi_and_version
= (guid
.time_hi_and_version
& 0x0FFF) | 0x4000;
174 * generate an empty GUID
176 _PUBLIC_
struct GUID
GUID_zero(void)
185 _PUBLIC_
bool GUID_all_zero(const struct GUID
*u
)
187 if (u
->time_low
!= 0 ||
189 u
->time_hi_and_version
!= 0 ||
190 u
->clock_seq
[0] != 0 ||
191 u
->clock_seq
[1] != 0 ||
192 !all_zero(u
->node
, 6)) {
198 _PUBLIC_
bool GUID_equal(const struct GUID
*u1
, const struct GUID
*u2
)
200 if (u1
->time_low
!= u2
->time_low
||
201 u1
->time_mid
!= u2
->time_mid
||
202 u1
->time_hi_and_version
!= u2
->time_hi_and_version
||
203 u1
->clock_seq
[0] != u2
->clock_seq
[0] ||
204 u1
->clock_seq
[1] != u2
->clock_seq
[1] ||
205 memcmp(u1
->node
, u2
->node
, 6) != 0) {
211 _PUBLIC_
int GUID_compare(const struct GUID
*u1
, const struct GUID
*u2
)
213 if (u1
->time_low
!= u2
->time_low
) {
214 return u1
->time_low
> u2
->time_low
? 1 : -1;
217 if (u1
->time_mid
!= u2
->time_mid
) {
218 return u1
->time_mid
> u2
->time_mid
? 1 : -1;
221 if (u1
->time_hi_and_version
!= u2
->time_hi_and_version
) {
222 return u1
->time_hi_and_version
> u2
->time_hi_and_version
? 1 : -1;
225 if (u1
->clock_seq
[0] != u2
->clock_seq
[0]) {
226 return u1
->clock_seq
[0] > u2
->clock_seq
[0] ? 1 : -1;
229 if (u1
->clock_seq
[1] != u2
->clock_seq
[1]) {
230 return u1
->clock_seq
[1] > u2
->clock_seq
[1] ? 1 : -1;
233 return memcmp(u1
->node
, u2
->node
, 6);
237 its useful to be able to display these in debugging messages
239 _PUBLIC_
char *GUID_string(TALLOC_CTX
*mem_ctx
, const struct GUID
*guid
)
241 return talloc_asprintf(mem_ctx
,
242 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
243 guid
->time_low
, guid
->time_mid
,
244 guid
->time_hi_and_version
,
247 guid
->node
[0], guid
->node
[1],
248 guid
->node
[2], guid
->node
[3],
249 guid
->node
[4], guid
->node
[5]);
252 _PUBLIC_
char *GUID_string2(TALLOC_CTX
*mem_ctx
, const struct GUID
*guid
)
254 char *ret
, *s
= GUID_string(mem_ctx
, guid
);
255 ret
= talloc_asprintf(mem_ctx
, "{%s}", s
);
260 _PUBLIC_
char *GUID_hexstring(TALLOC_CTX
*mem_ctx
, const struct GUID
*guid
)
267 tmp_mem
= talloc_new(mem_ctx
);
271 status
= GUID_to_ndr_blob(guid
, tmp_mem
, &guid_blob
);
272 if (!NT_STATUS_IS_OK(status
)) {
273 talloc_free(tmp_mem
);
277 ret
= data_blob_hex_string_upper(mem_ctx
, &guid_blob
);
278 talloc_free(tmp_mem
);
282 _PUBLIC_
bool ndr_policy_handle_empty(const struct policy_handle
*h
)
284 return (h
->handle_type
== 0 && GUID_all_zero(&h
->uuid
));
287 _PUBLIC_
bool ndr_policy_handle_equal(const struct policy_handle
*hnd1
,
288 const struct policy_handle
*hnd2
)
290 if (!hnd1
|| !hnd2
) {
294 return (memcmp(hnd1
, hnd2
, sizeof(*hnd1
)) == 0);