s4: Make unixid optional
[Samba/ekacnet.git] / librpc / ndr / uuid.c
blob1899afbbca616f8f81b5934a640f8a0de4b51028
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 ndr_err = ndr_push_struct_blob(b, mem_ctx, NULL, guid,
35 (ndr_push_flags_fn_t)ndr_push_GUID);
36 return ndr_map_error2ntstatus(ndr_err);
40 /**
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;
46 TALLOC_CTX *mem_ctx;
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, NULL, guid,
52 (ndr_pull_flags_fn_t)ndr_pull_GUID);
53 talloc_free(mem_ctx);
54 return ndr_map_error2ntstatus(ndr_err);
58 /**
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;
64 uint32_t time_low;
65 uint32_t time_mid, time_hi_and_version;
66 uint32_t clock_seq[2];
67 uint32_t node[6];
68 uint8_t buf16[16];
70 DATA_BLOB blob16 = data_blob_const(buf16, sizeof(buf16));
71 int i;
73 if (s->data == NULL) {
74 return NT_STATUS_INVALID_PARAMETER;
77 if (s->length == 36) {
78 TALLOC_CTX *mem_ctx;
79 const char *string;
81 mem_ctx = talloc_new(NULL);
82 NT_STATUS_HAVE_NO_MEMORY(mem_ctx);
83 string = talloc_strndup(mem_ctx, (const char *)s->data, s->length);
84 NT_STATUS_HAVE_NO_MEMORY(string);
85 if (11 == sscanf(string,
86 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
87 &time_low, &time_mid, &time_hi_and_version,
88 &clock_seq[0], &clock_seq[1],
89 &node[0], &node[1], &node[2], &node[3], &node[4], &node[5])) {
90 status = NT_STATUS_OK;
92 talloc_free(mem_ctx);
94 } else if (s->length == 38) {
95 TALLOC_CTX *mem_ctx;
96 const char *string;
98 mem_ctx = talloc_new(NULL);
99 NT_STATUS_HAVE_NO_MEMORY(mem_ctx);
100 string = talloc_strndup(mem_ctx, (const char *)s->data, s->length);
101 NT_STATUS_HAVE_NO_MEMORY(string);
102 if (11 == sscanf((const char *)s->data,
103 "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
104 &time_low, &time_mid, &time_hi_and_version,
105 &clock_seq[0], &clock_seq[1],
106 &node[0], &node[1], &node[2], &node[3], &node[4], &node[5])) {
107 status = NT_STATUS_OK;
109 talloc_free(mem_ctx);
111 } else if (s->length == 32) {
112 size_t rlen = strhex_to_str((char *)blob16.data, blob16.length,
113 (const char *)s->data, s->length);
114 if (rlen == blob16.length) {
115 /* goto the ndr_pull_struct_blob() path */
116 status = NT_STATUS_OK;
117 s = &blob16;
121 if (s->length == 16) {
122 return GUID_from_ndr_blob(s, guid);
125 if (!NT_STATUS_IS_OK(status)) {
126 return status;
129 guid->time_low = time_low;
130 guid->time_mid = time_mid;
131 guid->time_hi_and_version = time_hi_and_version;
132 guid->clock_seq[0] = clock_seq[0];
133 guid->clock_seq[1] = clock_seq[1];
134 for (i=0;i<6;i++) {
135 guid->node[i] = node[i];
138 return NT_STATUS_OK;
142 build a GUID from a string
144 _PUBLIC_ NTSTATUS GUID_from_string(const char *s, struct GUID *guid)
146 DATA_BLOB blob = data_blob_string_const(s);
147 return GUID_from_data_blob(&blob, guid);
151 build a GUID from a string
153 _PUBLIC_ NTSTATUS NS_GUID_from_string(const char *s, struct GUID *guid)
155 NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
156 uint32_t time_low;
157 uint32_t time_mid, time_hi_and_version;
158 uint32_t clock_seq[2];
159 uint32_t node[6];
160 int i;
162 if (s == NULL) {
163 return NT_STATUS_INVALID_PARAMETER;
166 if (11 == sscanf(s, "%08x-%04x%04x-%02x%02x%02x%02x-%02x%02x%02x%02x",
167 &time_low, &time_mid, &time_hi_and_version,
168 &clock_seq[0], &clock_seq[1],
169 &node[0], &node[1], &node[2], &node[3], &node[4], &node[5])) {
170 status = NT_STATUS_OK;
173 if (!NT_STATUS_IS_OK(status)) {
174 return status;
177 guid->time_low = time_low;
178 guid->time_mid = time_mid;
179 guid->time_hi_and_version = time_hi_and_version;
180 guid->clock_seq[0] = clock_seq[0];
181 guid->clock_seq[1] = clock_seq[1];
182 for (i=0;i<6;i++) {
183 guid->node[i] = node[i];
186 return NT_STATUS_OK;
190 * generate a random GUID
192 _PUBLIC_ struct GUID GUID_random(void)
194 struct GUID guid;
196 generate_random_buffer((uint8_t *)&guid, sizeof(guid));
197 guid.clock_seq[0] = (guid.clock_seq[0] & 0x3F) | 0x80;
198 guid.time_hi_and_version = (guid.time_hi_and_version & 0x0FFF) | 0x4000;
200 return guid;
204 * generate an empty GUID
206 _PUBLIC_ struct GUID GUID_zero(void)
208 struct GUID guid;
210 ZERO_STRUCT(guid);
212 return guid;
215 _PUBLIC_ bool GUID_all_zero(const struct GUID *u)
217 if (u->time_low != 0 ||
218 u->time_mid != 0 ||
219 u->time_hi_and_version != 0 ||
220 u->clock_seq[0] != 0 ||
221 u->clock_seq[1] != 0 ||
222 !all_zero(u->node, 6)) {
223 return false;
225 return true;
228 _PUBLIC_ bool GUID_equal(const struct GUID *u1, const struct GUID *u2)
230 if (u1->time_low != u2->time_low ||
231 u1->time_mid != u2->time_mid ||
232 u1->time_hi_and_version != u2->time_hi_and_version ||
233 u1->clock_seq[0] != u2->clock_seq[0] ||
234 u1->clock_seq[1] != u2->clock_seq[1] ||
235 memcmp(u1->node, u2->node, 6) != 0) {
236 return false;
238 return true;
241 _PUBLIC_ int GUID_compare(const struct GUID *u1, const struct GUID *u2)
243 if (u1->time_low != u2->time_low) {
244 return u1->time_low > u2->time_low ? 1 : -1;
247 if (u1->time_mid != u2->time_mid) {
248 return u1->time_mid > u2->time_mid ? 1 : -1;
251 if (u1->time_hi_and_version != u2->time_hi_and_version) {
252 return u1->time_hi_and_version > u2->time_hi_and_version ? 1 : -1;
255 if (u1->clock_seq[0] != u2->clock_seq[0]) {
256 return u1->clock_seq[0] > u2->clock_seq[0] ? 1 : -1;
259 if (u1->clock_seq[1] != u2->clock_seq[1]) {
260 return u1->clock_seq[1] > u2->clock_seq[1] ? 1 : -1;
263 return memcmp(u1->node, u2->node, 6);
267 its useful to be able to display these in debugging messages
269 _PUBLIC_ char *GUID_string(TALLOC_CTX *mem_ctx, const struct GUID *guid)
271 return talloc_asprintf(mem_ctx,
272 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
273 guid->time_low, guid->time_mid,
274 guid->time_hi_and_version,
275 guid->clock_seq[0],
276 guid->clock_seq[1],
277 guid->node[0], guid->node[1],
278 guid->node[2], guid->node[3],
279 guid->node[4], guid->node[5]);
282 _PUBLIC_ char *GUID_string2(TALLOC_CTX *mem_ctx, const struct GUID *guid)
284 char *ret, *s = GUID_string(mem_ctx, guid);
285 ret = talloc_asprintf(mem_ctx, "{%s}", s);
286 talloc_free(s);
287 return ret;
290 _PUBLIC_ char *GUID_hexstring(TALLOC_CTX *mem_ctx, const struct GUID *guid)
292 char *ret;
293 DATA_BLOB guid_blob;
294 TALLOC_CTX *tmp_mem;
295 NTSTATUS status;
297 tmp_mem = talloc_new(mem_ctx);
298 if (!tmp_mem) {
299 return NULL;
301 status = GUID_to_ndr_blob(guid, tmp_mem, &guid_blob);
302 if (!NT_STATUS_IS_OK(status)) {
303 talloc_free(tmp_mem);
304 return NULL;
307 ret = data_blob_hex_string_upper(mem_ctx, &guid_blob);
308 talloc_free(tmp_mem);
309 return ret;
312 _PUBLIC_ char *NS_GUID_string(TALLOC_CTX *mem_ctx, const struct GUID *guid)
314 return talloc_asprintf(mem_ctx,
315 "%08x-%04x%04x-%02x%02x%02x%02x-%02x%02x%02x%02x",
316 guid->time_low, guid->time_mid,
317 guid->time_hi_and_version,
318 guid->clock_seq[0],
319 guid->clock_seq[1],
320 guid->node[0], guid->node[1],
321 guid->node[2], guid->node[3],
322 guid->node[4], guid->node[5]);
325 _PUBLIC_ bool policy_handle_empty(struct policy_handle *h)
327 return (h->handle_type == 0 && GUID_all_zero(&h->uuid));