spoolss: rework spoolss_GetPrinterDataEx and spoolss_SetPrinterDataEx.
[sfrench/samba-autobuild/.git] / librpc / ndr / uuid.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    UUID/GUID functions
5
6    Copyright (C) Theodore Ts'o               1996, 1997,
7    Copyright (C) Jim McDonough                     2002.
8    Copyright (C) Andrew Tridgell                   2003.
9    
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.
14    
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.
19    
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/>.
22 */
23
24 #include "includes.h"
25 #include "librpc/ndr/libndr.h"
26 #include "librpc/gen_ndr/ndr_misc.h"
27
28 /**
29   build a GUID from a string
30 */
31 _PUBLIC_ NTSTATUS GUID_from_data_blob(const DATA_BLOB *s, struct GUID *guid)
32 {
33         NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
34         uint32_t time_low;
35         uint32_t time_mid, time_hi_and_version;
36         uint32_t clock_seq[2];
37         uint32_t node[6];
38         uint8_t buf16[16];
39
40         DATA_BLOB blob16 = data_blob_const(buf16, sizeof(buf16));
41         int i;
42
43         if (s->data == NULL) {
44                 return NT_STATUS_INVALID_PARAMETER;
45         }
46
47         if (s->length == 36) {
48                 TALLOC_CTX *mem_ctx;
49                 const char *string;
50
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;
61                 }
62                 talloc_free(mem_ctx);
63
64         } else if (s->length == 38) {
65                 TALLOC_CTX *mem_ctx;
66                 const char *string;
67
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;
78                 }
79                 talloc_free(mem_ctx);
80
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                         /* goto the ndr_pull_struct_blob() path */
86                         status = NT_STATUS_OK;
87                         s = &blob16;
88                 }
89         }
90
91         if (s->length == 16) {
92                 enum ndr_err_code ndr_err;
93                 struct GUID guid2;
94                 TALLOC_CTX *mem_ctx;
95
96                 mem_ctx = talloc_new(NULL);
97                 NT_STATUS_HAVE_NO_MEMORY(mem_ctx);
98
99                 ndr_err = ndr_pull_struct_blob(s, mem_ctx, NULL, &guid2,
100                                                (ndr_pull_flags_fn_t)ndr_pull_GUID);
101                 talloc_free(mem_ctx);
102                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
103                         return ndr_map_error2ntstatus(ndr_err);
104                 }
105                 *guid = guid2;
106                 return NT_STATUS_OK;
107         }
108
109         if (!NT_STATUS_IS_OK(status)) {
110                 return status;
111         }
112
113         guid->time_low = time_low;
114         guid->time_mid = time_mid;
115         guid->time_hi_and_version = time_hi_and_version;
116         guid->clock_seq[0] = clock_seq[0];
117         guid->clock_seq[1] = clock_seq[1];
118         for (i=0;i<6;i++) {
119                 guid->node[i] = node[i];
120         }
121
122         return NT_STATUS_OK;
123 }
124
125 /**
126   build a GUID from a string
127 */
128 _PUBLIC_ NTSTATUS GUID_from_string(const char *s, struct GUID *guid)
129 {
130         DATA_BLOB blob = data_blob_string_const(s);
131         return GUID_from_data_blob(&blob, guid);
132 }
133
134 /**
135   build a GUID from a string
136 */
137 _PUBLIC_ NTSTATUS NS_GUID_from_string(const char *s, struct GUID *guid)
138 {
139         NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
140         uint32_t time_low;
141         uint32_t time_mid, time_hi_and_version;
142         uint32_t clock_seq[2];
143         uint32_t node[6];
144         int i;
145
146         if (s == NULL) {
147                 return NT_STATUS_INVALID_PARAMETER;
148         }
149
150         if (11 == sscanf(s, "%08x-%04x%04x-%02x%02x%02x%02x-%02x%02x%02x%02x",
151                          &time_low, &time_mid, &time_hi_and_version, 
152                          &clock_seq[0], &clock_seq[1],
153                          &node[0], &node[1], &node[2], &node[3], &node[4], &node[5])) {
154                 status = NT_STATUS_OK;
155         }
156
157         if (!NT_STATUS_IS_OK(status)) {
158                 return status;
159         }
160
161         guid->time_low = time_low;
162         guid->time_mid = time_mid;
163         guid->time_hi_and_version = time_hi_and_version;
164         guid->clock_seq[0] = clock_seq[0];
165         guid->clock_seq[1] = clock_seq[1];
166         for (i=0;i<6;i++) {
167                 guid->node[i] = node[i];
168         }
169
170         return NT_STATUS_OK;
171 }
172
173 /**
174  * generate a random GUID
175  */
176 _PUBLIC_ struct GUID GUID_random(void)
177 {
178         struct GUID guid;
179
180         generate_random_buffer((uint8_t *)&guid, sizeof(guid));
181         guid.clock_seq[0] = (guid.clock_seq[0] & 0x3F) | 0x80;
182         guid.time_hi_and_version = (guid.time_hi_and_version & 0x0FFF) | 0x4000;
183
184         return guid;
185 }
186
187 /**
188  * generate an empty GUID 
189  */
190 _PUBLIC_ struct GUID GUID_zero(void)
191 {
192         struct GUID guid;
193
194         ZERO_STRUCT(guid);
195
196         return guid;
197 }
198
199 _PUBLIC_ bool GUID_all_zero(const struct GUID *u)
200 {
201         if (u->time_low != 0 ||
202             u->time_mid != 0 ||
203             u->time_hi_and_version != 0 ||
204             u->clock_seq[0] != 0 ||
205             u->clock_seq[1] != 0 ||
206             !all_zero(u->node, 6)) {
207                 return false;
208         }
209         return true;
210 }
211
212 _PUBLIC_ bool GUID_equal(const struct GUID *u1, const struct GUID *u2)
213 {
214         if (u1->time_low != u2->time_low ||
215             u1->time_mid != u2->time_mid ||
216             u1->time_hi_and_version != u2->time_hi_and_version ||
217             u1->clock_seq[0] != u2->clock_seq[0] ||
218             u1->clock_seq[1] != u2->clock_seq[1] ||
219             memcmp(u1->node, u2->node, 6) != 0) {
220                 return false;
221         }
222         return true;
223 }
224
225 _PUBLIC_ int GUID_compare(const struct GUID *u1, const struct GUID *u2)
226 {
227         if (u1->time_low != u2->time_low) {
228                 return u1->time_low - u2->time_low;
229         }
230
231         if (u1->time_mid != u2->time_mid) {
232                 return u1->time_mid - u2->time_mid;
233         }
234
235         if (u1->time_hi_and_version != u2->time_hi_and_version) {
236                 return u1->time_hi_and_version - u2->time_hi_and_version;
237         }
238
239         if (u1->clock_seq[0] != u2->clock_seq[0]) {
240                 return u1->clock_seq[0] - u2->clock_seq[0];
241         }
242
243         if (u1->clock_seq[1] != u2->clock_seq[1]) {
244                 return u1->clock_seq[1] - u2->clock_seq[1];
245         }
246
247         return memcmp(u1->node, u2->node, 6);
248 }
249
250 /**
251   its useful to be able to display these in debugging messages
252 */
253 _PUBLIC_ char *GUID_string(TALLOC_CTX *mem_ctx, const struct GUID *guid)
254 {
255         return talloc_asprintf(mem_ctx, 
256                                "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
257                                guid->time_low, guid->time_mid,
258                                guid->time_hi_and_version,
259                                guid->clock_seq[0],
260                                guid->clock_seq[1],
261                                guid->node[0], guid->node[1],
262                                guid->node[2], guid->node[3],
263                                guid->node[4], guid->node[5]);
264 }
265
266 _PUBLIC_ char *GUID_string2(TALLOC_CTX *mem_ctx, const struct GUID *guid)
267 {
268         char *ret, *s = GUID_string(mem_ctx, guid);
269         ret = talloc_asprintf(mem_ctx, "{%s}", s);
270         talloc_free(s);
271         return ret;
272 }
273
274 _PUBLIC_ char *GUID_hexstring(TALLOC_CTX *mem_ctx, const struct GUID *guid)
275 {
276         char *ret;
277         DATA_BLOB guid_blob;
278         enum ndr_err_code ndr_err;
279         TALLOC_CTX *tmp_mem;
280
281         tmp_mem = talloc_new(mem_ctx);
282         if (!tmp_mem) {
283                 return NULL;
284         }
285         ndr_err = ndr_push_struct_blob(&guid_blob, tmp_mem,
286                                        NULL,
287                                        guid,
288                                        (ndr_push_flags_fn_t)ndr_push_GUID);
289         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
290                 talloc_free(tmp_mem);
291                 return NULL;
292         }
293
294         ret = data_blob_hex_string_upper(mem_ctx, &guid_blob);
295         talloc_free(tmp_mem);
296         return ret;
297 }
298
299 _PUBLIC_ char *NS_GUID_string(TALLOC_CTX *mem_ctx, const struct GUID *guid)
300 {
301         return talloc_asprintf(mem_ctx, 
302                                "%08x-%04x%04x-%02x%02x%02x%02x-%02x%02x%02x%02x",
303                                guid->time_low, guid->time_mid,
304                                guid->time_hi_and_version,
305                                guid->clock_seq[0],
306                                guid->clock_seq[1],
307                                guid->node[0], guid->node[1],
308                                guid->node[2], guid->node[3],
309                                guid->node[4], guid->node[5]);
310 }
311
312 _PUBLIC_ bool policy_handle_empty(struct policy_handle *h) 
313 {
314         return (h->handle_type == 0 && GUID_all_zero(&h->uuid));
315 }