winbind: Keep "force_reauth" in invalidate_cm_connection
[Samba.git] / librpc / ndr / uuid.c
blob53540c7ddc8ba4fef2e0a5ba783a6036b21fb1df
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"
27 #include "lib/util/util_str_hex.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 *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);
44 /**
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);
56 /**
57 build a GUID from a string
59 _PUBLIC_ NTSTATUS GUID_from_data_blob(const DATA_BLOB *s, struct GUID *guid)
61 NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
62 uint32_t time_low;
63 uint32_t time_mid, time_hi_and_version;
64 uint32_t clock_seq[2];
65 uint32_t node[6];
66 uint8_t buf16[16];
68 DATA_BLOB blob16 = data_blob_const(buf16, sizeof(buf16));
69 int i;
71 if (s->data == NULL) {
72 return NT_STATUS_INVALID_PARAMETER;
75 switch(s->length) {
76 case 36:
78 status = parse_guid_string((char *)s->data,
79 &time_low,
80 &time_mid,
81 &time_hi_and_version,
82 clock_seq,
83 node);
84 break;
86 case 38:
88 if (s->data[0] != '{' || s->data[37] != '}') {
89 break;
92 status = parse_guid_string((char *)s->data + 1,
93 &time_low,
94 &time_mid,
95 &time_hi_and_version,
96 clock_seq,
97 node);
98 break;
100 case 32:
102 size_t rlen = strhex_to_str((char *)blob16.data, blob16.length,
103 (const char *)s->data, s->length);
104 if (rlen != blob16.length) {
105 return NT_STATUS_INVALID_PARAMETER;
108 s = &blob16;
109 return GUID_from_ndr_blob(s, guid);
111 case 16:
112 return GUID_from_ndr_blob(s, guid);
113 default:
114 status = NT_STATUS_INVALID_PARAMETER;
115 break;
118 if (!NT_STATUS_IS_OK(status)) {
119 return status;
122 guid->time_low = time_low;
123 guid->time_mid = time_mid;
124 guid->time_hi_and_version = time_hi_and_version;
125 guid->clock_seq[0] = clock_seq[0];
126 guid->clock_seq[1] = clock_seq[1];
127 for (i=0;i<6;i++) {
128 guid->node[i] = node[i];
131 return NT_STATUS_OK;
135 build a GUID from a string
137 _PUBLIC_ NTSTATUS GUID_from_string(const char *s, struct GUID *guid)
139 DATA_BLOB blob = data_blob_string_const(s);
140 return GUID_from_data_blob(&blob, guid);
144 * generate a random GUID
146 _PUBLIC_ struct GUID GUID_random(void)
148 struct GUID guid;
150 generate_random_buffer((uint8_t *)&guid, sizeof(guid));
151 guid.clock_seq[0] = (guid.clock_seq[0] & 0x3F) | 0x80;
152 guid.time_hi_and_version = (guid.time_hi_and_version & 0x0FFF) | 0x4000;
154 return guid;
158 * generate an empty GUID
160 _PUBLIC_ struct GUID GUID_zero(void)
162 struct GUID guid;
164 ZERO_STRUCT(guid);
166 return guid;
169 _PUBLIC_ bool GUID_all_zero(const struct GUID *u)
171 if (u->time_low != 0 ||
172 u->time_mid != 0 ||
173 u->time_hi_and_version != 0 ||
174 u->clock_seq[0] != 0 ||
175 u->clock_seq[1] != 0 ||
176 !all_zero(u->node, 6)) {
177 return false;
179 return true;
182 _PUBLIC_ bool GUID_equal(const struct GUID *u1, const struct GUID *u2)
184 return (GUID_compare(u1, u2) == 0);
187 _PUBLIC_ int GUID_compare(const struct GUID *u1, const struct GUID *u2)
189 if (u1->time_low != u2->time_low) {
190 return u1->time_low > u2->time_low ? 1 : -1;
193 if (u1->time_mid != u2->time_mid) {
194 return u1->time_mid > u2->time_mid ? 1 : -1;
197 if (u1->time_hi_and_version != u2->time_hi_and_version) {
198 return u1->time_hi_and_version > u2->time_hi_and_version ? 1 : -1;
201 if (u1->clock_seq[0] != u2->clock_seq[0]) {
202 return u1->clock_seq[0] > u2->clock_seq[0] ? 1 : -1;
205 if (u1->clock_seq[1] != u2->clock_seq[1]) {
206 return u1->clock_seq[1] > u2->clock_seq[1] ? 1 : -1;
209 return memcmp(u1->node, u2->node, 6);
213 its useful to be able to display these in debugging messages
215 _PUBLIC_ char *GUID_string(TALLOC_CTX *mem_ctx, const struct GUID *guid)
217 struct GUID_txt_buf buf;
218 return talloc_strdup(mem_ctx, GUID_buf_string(guid, &buf));
222 * Does the same without allocating memory, using the structure buffer.
223 * Useful for debug messages, so that you do not have to talloc_free the result
225 _PUBLIC_ char* GUID_buf_string(const struct GUID *guid,
226 struct GUID_txt_buf *dst)
228 if (!guid) {
229 return NULL;
231 snprintf(dst->buf, sizeof(dst->buf),
232 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
233 guid->time_low, guid->time_mid,
234 guid->time_hi_and_version,
235 guid->clock_seq[0],
236 guid->clock_seq[1],
237 guid->node[0], guid->node[1],
238 guid->node[2], guid->node[3],
239 guid->node[4], guid->node[5]);
240 return dst->buf;
243 _PUBLIC_ char *GUID_string2(TALLOC_CTX *mem_ctx, const struct GUID *guid)
245 char *ret, *s = GUID_string(mem_ctx, guid);
246 ret = talloc_asprintf(mem_ctx, "{%s}", s);
247 talloc_free(s);
248 return ret;
251 _PUBLIC_ char *GUID_hexstring(TALLOC_CTX *mem_ctx, const struct GUID *guid)
253 char *ret;
254 DATA_BLOB guid_blob;
255 TALLOC_CTX *tmp_mem;
256 NTSTATUS status;
258 tmp_mem = talloc_new(mem_ctx);
259 if (!tmp_mem) {
260 return NULL;
262 status = GUID_to_ndr_blob(guid, tmp_mem, &guid_blob);
263 if (!NT_STATUS_IS_OK(status)) {
264 talloc_free(tmp_mem);
265 return NULL;
268 ret = data_blob_hex_string_upper(mem_ctx, &guid_blob);
269 talloc_free(tmp_mem);
270 return ret;
273 _PUBLIC_ bool ndr_policy_handle_empty(const struct policy_handle *h)
275 return (h->handle_type == 0 && GUID_all_zero(&h->uuid));
278 _PUBLIC_ bool ndr_policy_handle_equal(const struct policy_handle *hnd1,
279 const struct policy_handle *hnd2)
281 if (!hnd1 || !hnd2) {
282 return false;
285 return (memcmp(hnd1, hnd2, sizeof(*hnd1)) == 0);