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 GUID from a string
31 _PUBLIC_ NTSTATUS
GUID_from_data_blob(const DATA_BLOB
*s
, struct GUID
*guid
)
33 NTSTATUS status
= NT_STATUS_INVALID_PARAMETER
;
35 uint32_t time_mid
, time_hi_and_version
;
36 uint32_t clock_seq
[2];
40 DATA_BLOB blob16
= data_blob_const(buf16
, sizeof(buf16
));
43 if (s
->data
== NULL
) {
44 return NT_STATUS_INVALID_PARAMETER
;
47 if (s
->length
== 36) {
51 mem_ctx
= talloc_new(NULL
);
52 NT_STATUS_HAVE_NO_MEMORY(mem_ctx
);
53 string
= talloc_strndup(mem_ctx
, (const char *)s
->data
, s
->length
);
54 NT_STATUS_HAVE_NO_MEMORY(string
);
55 if (11 == sscanf(string
,
56 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
57 &time_low
, &time_mid
, &time_hi_and_version
,
58 &clock_seq
[0], &clock_seq
[1],
59 &node
[0], &node
[1], &node
[2], &node
[3], &node
[4], &node
[5])) {
60 status
= NT_STATUS_OK
;
64 } else if (s
->length
== 38) {
68 mem_ctx
= talloc_new(NULL
);
69 NT_STATUS_HAVE_NO_MEMORY(mem_ctx
);
70 string
= talloc_strndup(mem_ctx
, (const char *)s
->data
, s
->length
);
71 NT_STATUS_HAVE_NO_MEMORY(string
);
72 if (11 == sscanf((const char *)s
->data
,
73 "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
74 &time_low
, &time_mid
, &time_hi_and_version
,
75 &clock_seq
[0], &clock_seq
[1],
76 &node
[0], &node
[1], &node
[2], &node
[3], &node
[4], &node
[5])) {
77 status
= NT_STATUS_OK
;
81 } else if (s
->length
== 32) {
82 size_t rlen
= strhex_to_str((char *)blob16
.data
, blob16
.length
,
83 (const char *)s
->data
, s
->length
);
84 if (rlen
!= blob16
.length
) {
85 return NT_STATUS_INVALID_PARAMETER
;
89 return GUID_from_ndr_blob(s
, guid
);
92 if (s
->length
== 16) {
93 enum ndr_err_code ndr_err
;
97 mem_ctx
= talloc_new(NULL
);
98 NT_STATUS_HAVE_NO_MEMORY(mem_ctx
);
100 ndr_err
= ndr_pull_struct_blob(s
, mem_ctx
, NULL
, &guid2
,
101 (ndr_pull_flags_fn_t
)ndr_pull_GUID
);
102 talloc_free(mem_ctx
);
103 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
104 return ndr_map_error2ntstatus(ndr_err
);
110 if (!NT_STATUS_IS_OK(status
)) {
114 guid
->time_low
= time_low
;
115 guid
->time_mid
= time_mid
;
116 guid
->time_hi_and_version
= time_hi_and_version
;
117 guid
->clock_seq
[0] = clock_seq
[0];
118 guid
->clock_seq
[1] = clock_seq
[1];
120 guid
->node
[i
] = node
[i
];
127 build a GUID from a string
129 _PUBLIC_ NTSTATUS
GUID_from_string(const char *s
, struct GUID
*guid
)
131 DATA_BLOB blob
= data_blob_string_const(s
);
132 return GUID_from_data_blob(&blob
, guid
);
137 build a GUID from a string
139 _PUBLIC_ NTSTATUS
NS_GUID_from_string(const char *s
, struct GUID
*guid
)
141 NTSTATUS status
= NT_STATUS_INVALID_PARAMETER
;
143 uint32_t time_mid
, time_hi_and_version
;
144 uint32_t clock_seq
[2];
149 return NT_STATUS_INVALID_PARAMETER
;
152 if (11 == sscanf(s
, "%08x-%04x%04x-%02x%02x%02x%02x-%02x%02x%02x%02x",
153 &time_low
, &time_mid
, &time_hi_and_version
,
154 &clock_seq
[0], &clock_seq
[1],
155 &node
[0], &node
[1], &node
[2], &node
[3], &node
[4], &node
[5])) {
156 status
= NT_STATUS_OK
;
159 if (!NT_STATUS_IS_OK(status
)) {
163 guid
->time_low
= time_low
;
164 guid
->time_mid
= time_mid
;
165 guid
->time_hi_and_version
= time_hi_and_version
;
166 guid
->clock_seq
[0] = clock_seq
[0];
167 guid
->clock_seq
[1] = clock_seq
[1];
169 guid
->node
[i
] = node
[i
];
176 * generate a random GUID
178 _PUBLIC_
struct GUID
GUID_random(void)
182 generate_random_buffer((uint8_t *)&guid
, sizeof(guid
));
183 guid
.clock_seq
[0] = (guid
.clock_seq
[0] & 0x3F) | 0x80;
184 guid
.time_hi_and_version
= (guid
.time_hi_and_version
& 0x0FFF) | 0x4000;
190 * generate an empty GUID
192 _PUBLIC_
struct GUID
GUID_zero(void)
201 _PUBLIC_
bool GUID_all_zero(const struct GUID
*u
)
203 if (u
->time_low
!= 0 ||
205 u
->time_hi_and_version
!= 0 ||
206 u
->clock_seq
[0] != 0 ||
207 u
->clock_seq
[1] != 0 ||
208 !all_zero(u
->node
, 6)) {
214 _PUBLIC_
bool GUID_equal(const struct GUID
*u1
, const struct GUID
*u2
)
216 if (u1
->time_low
!= u2
->time_low
||
217 u1
->time_mid
!= u2
->time_mid
||
218 u1
->time_hi_and_version
!= u2
->time_hi_and_version
||
219 u1
->clock_seq
[0] != u2
->clock_seq
[0] ||
220 u1
->clock_seq
[1] != u2
->clock_seq
[1] ||
221 memcmp(u1
->node
, u2
->node
, 6) != 0) {
227 _PUBLIC_
int GUID_compare(const struct GUID
*u1
, const struct GUID
*u2
)
229 if (u1
->time_low
!= u2
->time_low
) {
230 return u1
->time_low
- u2
->time_low
;
233 if (u1
->time_mid
!= u2
->time_mid
) {
234 return u1
->time_mid
- u2
->time_mid
;
237 if (u1
->time_hi_and_version
!= u2
->time_hi_and_version
) {
238 return u1
->time_hi_and_version
- u2
->time_hi_and_version
;
241 if (u1
->clock_seq
[0] != u2
->clock_seq
[0]) {
242 return u1
->clock_seq
[0] - u2
->clock_seq
[0];
245 if (u1
->clock_seq
[1] != u2
->clock_seq
[1]) {
246 return u1
->clock_seq
[1] - u2
->clock_seq
[1];
249 return memcmp(u1
->node
, u2
->node
, 6);
253 its useful to be able to display these in debugging messages
255 _PUBLIC_
char *GUID_string(TALLOC_CTX
*mem_ctx
, const struct GUID
*guid
)
257 return talloc_asprintf(mem_ctx
,
258 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
259 guid
->time_low
, guid
->time_mid
,
260 guid
->time_hi_and_version
,
263 guid
->node
[0], guid
->node
[1],
264 guid
->node
[2], guid
->node
[3],
265 guid
->node
[4], guid
->node
[5]);
268 _PUBLIC_
char *GUID_string2(TALLOC_CTX
*mem_ctx
, const struct GUID
*guid
)
270 char *ret
, *s
= GUID_string(mem_ctx
, guid
);
271 ret
= talloc_asprintf(mem_ctx
, "{%s}", s
);
276 _PUBLIC_
char *GUID_hexstring(TALLOC_CTX
*mem_ctx
, const struct GUID
*guid
)
280 enum ndr_err_code ndr_err
;
283 tmp_mem
= talloc_new(mem_ctx
);
287 ndr_err
= ndr_push_struct_blob(&guid_blob
, tmp_mem
,
290 (ndr_push_flags_fn_t
)ndr_push_GUID
);
291 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
292 talloc_free(tmp_mem
);
296 ret
= data_blob_hex_string(mem_ctx
, &guid_blob
);
297 talloc_free(tmp_mem
);
301 _PUBLIC_
char *NS_GUID_string(TALLOC_CTX
*mem_ctx
, const struct GUID
*guid
)
303 return talloc_asprintf(mem_ctx
,
304 "%08x-%04x%04x-%02x%02x%02x%02x-%02x%02x%02x%02x",
305 guid
->time_low
, guid
->time_mid
,
306 guid
->time_hi_and_version
,
309 guid
->node
[0], guid
->node
[1],
310 guid
->node
[2], guid
->node
[3],
311 guid
->node
[4], guid
->node
[5]);
314 _PUBLIC_
bool policy_handle_empty(struct policy_handle
*h
)
316 return (h
->handle_type
== 0 && GUID_all_zero(&h
->uuid
));