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"
30 build a GUID from a NDR data blob
32 _PUBLIC_ NTSTATUS
GUID_from_ndr_blob(const DATA_BLOB
*b
, struct GUID
*guid
)
34 enum ndr_err_code ndr_err
;
37 mem_ctx
= talloc_new(NULL
);
38 NT_STATUS_HAVE_NO_MEMORY(mem_ctx
);
40 ndr_err
= ndr_pull_struct_blob_all(b
, mem_ctx
, NULL
, guid
,
41 (ndr_pull_flags_fn_t
)ndr_pull_GUID
);
43 return ndr_map_error2ntstatus(ndr_err
);
48 build a GUID from a string
50 _PUBLIC_ NTSTATUS
GUID_from_data_blob(const DATA_BLOB
*s
, struct GUID
*guid
)
52 NTSTATUS status
= NT_STATUS_INVALID_PARAMETER
;
54 uint32_t time_mid
, time_hi_and_version
;
55 uint32_t clock_seq
[2];
59 DATA_BLOB blob16
= data_blob_const(buf16
, sizeof(buf16
));
62 if (s
->data
== NULL
) {
63 return NT_STATUS_INVALID_PARAMETER
;
66 if (s
->length
== 36) {
70 mem_ctx
= talloc_new(NULL
);
71 NT_STATUS_HAVE_NO_MEMORY(mem_ctx
);
72 string
= talloc_strndup(mem_ctx
, (const char *)s
->data
, s
->length
);
73 NT_STATUS_HAVE_NO_MEMORY(string
);
74 if (11 == sscanf(string
,
75 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
76 &time_low
, &time_mid
, &time_hi_and_version
,
77 &clock_seq
[0], &clock_seq
[1],
78 &node
[0], &node
[1], &node
[2], &node
[3], &node
[4], &node
[5])) {
79 status
= NT_STATUS_OK
;
83 } else if (s
->length
== 38) {
87 mem_ctx
= talloc_new(NULL
);
88 NT_STATUS_HAVE_NO_MEMORY(mem_ctx
);
89 string
= talloc_strndup(mem_ctx
, (const char *)s
->data
, s
->length
);
90 NT_STATUS_HAVE_NO_MEMORY(string
);
91 if (11 == sscanf((const char *)s
->data
,
92 "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
93 &time_low
, &time_mid
, &time_hi_and_version
,
94 &clock_seq
[0], &clock_seq
[1],
95 &node
[0], &node
[1], &node
[2], &node
[3], &node
[4], &node
[5])) {
96 status
= NT_STATUS_OK
;
100 } else if (s
->length
== 32) {
101 size_t rlen
= strhex_to_str((char *)blob16
.data
, blob16
.length
,
102 (const char *)s
->data
, s
->length
);
103 if (rlen
!= blob16
.length
) {
104 return NT_STATUS_INVALID_PARAMETER
;
108 return GUID_from_ndr_blob(s
, guid
);
111 if (s
->length
== 16) {
112 return GUID_from_ndr_blob(s
, guid
);
115 if (!NT_STATUS_IS_OK(status
)) {
119 guid
->time_low
= time_low
;
120 guid
->time_mid
= time_mid
;
121 guid
->time_hi_and_version
= time_hi_and_version
;
122 guid
->clock_seq
[0] = clock_seq
[0];
123 guid
->clock_seq
[1] = clock_seq
[1];
125 guid
->node
[i
] = node
[i
];
132 build a GUID from a string
134 _PUBLIC_ NTSTATUS
GUID_from_string(const char *s
, struct GUID
*guid
)
136 DATA_BLOB blob
= data_blob_string_const(s
);
137 return GUID_from_data_blob(&blob
, guid
);
142 build a GUID from a string
144 _PUBLIC_ NTSTATUS
NS_GUID_from_string(const char *s
, struct GUID
*guid
)
146 NTSTATUS status
= NT_STATUS_INVALID_PARAMETER
;
148 uint32_t time_mid
, time_hi_and_version
;
149 uint32_t clock_seq
[2];
154 return NT_STATUS_INVALID_PARAMETER
;
157 if (11 == sscanf(s
, "%08x-%04x%04x-%02x%02x%02x%02x-%02x%02x%02x%02x",
158 &time_low
, &time_mid
, &time_hi_and_version
,
159 &clock_seq
[0], &clock_seq
[1],
160 &node
[0], &node
[1], &node
[2], &node
[3], &node
[4], &node
[5])) {
161 status
= NT_STATUS_OK
;
164 if (!NT_STATUS_IS_OK(status
)) {
168 guid
->time_low
= time_low
;
169 guid
->time_mid
= time_mid
;
170 guid
->time_hi_and_version
= time_hi_and_version
;
171 guid
->clock_seq
[0] = clock_seq
[0];
172 guid
->clock_seq
[1] = clock_seq
[1];
174 guid
->node
[i
] = node
[i
];
181 * generate a random GUID
183 _PUBLIC_
struct GUID
GUID_random(void)
187 generate_random_buffer((uint8_t *)&guid
, sizeof(guid
));
188 guid
.clock_seq
[0] = (guid
.clock_seq
[0] & 0x3F) | 0x80;
189 guid
.time_hi_and_version
= (guid
.time_hi_and_version
& 0x0FFF) | 0x4000;
195 * generate an empty GUID
197 _PUBLIC_
struct GUID
GUID_zero(void)
206 _PUBLIC_
bool GUID_all_zero(const struct GUID
*u
)
208 if (u
->time_low
!= 0 ||
210 u
->time_hi_and_version
!= 0 ||
211 u
->clock_seq
[0] != 0 ||
212 u
->clock_seq
[1] != 0 ||
213 !all_zero(u
->node
, 6)) {
219 _PUBLIC_
bool GUID_equal(const struct GUID
*u1
, const struct GUID
*u2
)
221 if (u1
->time_low
!= u2
->time_low
||
222 u1
->time_mid
!= u2
->time_mid
||
223 u1
->time_hi_and_version
!= u2
->time_hi_and_version
||
224 u1
->clock_seq
[0] != u2
->clock_seq
[0] ||
225 u1
->clock_seq
[1] != u2
->clock_seq
[1] ||
226 memcmp(u1
->node
, u2
->node
, 6) != 0) {
232 _PUBLIC_
int GUID_compare(const struct GUID
*u1
, const struct GUID
*u2
)
234 if (u1
->time_low
!= u2
->time_low
) {
235 return u1
->time_low
- u2
->time_low
;
238 if (u1
->time_mid
!= u2
->time_mid
) {
239 return u1
->time_mid
- u2
->time_mid
;
242 if (u1
->time_hi_and_version
!= u2
->time_hi_and_version
) {
243 return u1
->time_hi_and_version
- u2
->time_hi_and_version
;
246 if (u1
->clock_seq
[0] != u2
->clock_seq
[0]) {
247 return u1
->clock_seq
[0] - u2
->clock_seq
[0];
250 if (u1
->clock_seq
[1] != u2
->clock_seq
[1]) {
251 return u1
->clock_seq
[1] - u2
->clock_seq
[1];
254 return memcmp(u1
->node
, u2
->node
, 6);
258 its useful to be able to display these in debugging messages
260 _PUBLIC_
char *GUID_string(TALLOC_CTX
*mem_ctx
, const struct GUID
*guid
)
262 return talloc_asprintf(mem_ctx
,
263 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
264 guid
->time_low
, guid
->time_mid
,
265 guid
->time_hi_and_version
,
268 guid
->node
[0], guid
->node
[1],
269 guid
->node
[2], guid
->node
[3],
270 guid
->node
[4], guid
->node
[5]);
273 _PUBLIC_
char *GUID_string2(TALLOC_CTX
*mem_ctx
, const struct GUID
*guid
)
275 char *ret
, *s
= GUID_string(mem_ctx
, guid
);
276 ret
= talloc_asprintf(mem_ctx
, "{%s}", s
);
281 _PUBLIC_
char *GUID_hexstring(TALLOC_CTX
*mem_ctx
, const struct GUID
*guid
)
285 enum ndr_err_code ndr_err
;
288 tmp_mem
= talloc_new(mem_ctx
);
292 ndr_err
= ndr_push_struct_blob(&guid_blob
, tmp_mem
,
295 (ndr_push_flags_fn_t
)ndr_push_GUID
);
296 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
297 talloc_free(tmp_mem
);
301 ret
= data_blob_hex_string(mem_ctx
, &guid_blob
);
302 talloc_free(tmp_mem
);
306 _PUBLIC_
char *NS_GUID_string(TALLOC_CTX
*mem_ctx
, const struct GUID
*guid
)
308 return talloc_asprintf(mem_ctx
,
309 "%08x-%04x%04x-%02x%02x%02x%02x-%02x%02x%02x%02x",
310 guid
->time_low
, guid
->time_mid
,
311 guid
->time_hi_and_version
,
314 guid
->node
[0], guid
->node
[1],
315 guid
->node
[2], guid
->node
[3],
316 guid
->node
[4], guid
->node
[5]);
319 _PUBLIC_
bool policy_handle_empty(struct policy_handle
*h
)
321 return (h
->handle_type
== 0 && GUID_all_zero(&h
->uuid
));