r23779: Change from v2 or later to v3 or later.
[kai/samba.git] / source3 / librpc / ndr / ndr_misc.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    UUID/GUID/policy_handle functions
5
6    Copyright (C) Theodore Ts'o               1996, 1997,
7    Copyright (C) Jim McDonough                     2002.
8    Copyright (C) Andrew Tridgell                   2003.
9    Copyright (C) Stefan (metze) Metzmacher         2004.
10    
11    This program is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 3 of the License, or
14    (at your option) any later version.
15    
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20    
21    You should have received a copy of the GNU General Public License
22    along with this program; if not, write to the Free Software
23    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 */
25
26 #include "includes.h"
27
28 NTSTATUS ndr_push_GUID(struct ndr_push *ndr, int ndr_flags, const struct GUID *r)
29 {
30         if (ndr_flags & NDR_SCALARS) {
31                 NDR_CHECK(ndr_push_align(ndr, 4));
32                 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, r->time_low));
33                 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, r->time_mid));
34                 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, r->time_hi_and_version));
35                 NDR_CHECK(ndr_push_array_uint8(ndr, NDR_SCALARS, r->clock_seq, 2));
36                 NDR_CHECK(ndr_push_array_uint8(ndr, NDR_SCALARS, r->node, 6));
37         }
38         if (ndr_flags & NDR_BUFFERS) {
39         }
40         return NT_STATUS_OK;
41 }
42
43 NTSTATUS ndr_pull_GUID(struct ndr_pull *ndr, int ndr_flags, struct GUID *r)
44 {
45         if (ndr_flags & NDR_SCALARS) {
46                 NDR_CHECK(ndr_pull_align(ndr, 4));
47                 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &r->time_low));
48                 NDR_CHECK(ndr_pull_uint16(ndr, NDR_SCALARS, &r->time_mid));
49                 NDR_CHECK(ndr_pull_uint16(ndr, NDR_SCALARS, &r->time_hi_and_version));
50                 NDR_CHECK(ndr_pull_array_uint8(ndr, NDR_SCALARS, r->clock_seq, 2));
51                 NDR_CHECK(ndr_pull_array_uint8(ndr, NDR_SCALARS, r->node, 6));
52         }
53         if (ndr_flags & NDR_BUFFERS) {
54         }
55         return NT_STATUS_OK;
56 }
57
58 size_t ndr_size_GUID(const struct GUID *r, int flags)
59 {
60         return ndr_size_struct(r, flags, (ndr_push_flags_fn_t)ndr_push_GUID);
61 }
62
63 /**
64   build a GUID from a string
65 */
66 NTSTATUS GUID_from_string(const char *s, struct GUID *guid)
67 {
68         NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
69         uint32_t time_low;
70         uint32_t time_mid, time_hi_and_version;
71         uint32_t clock_seq[2];
72         uint32_t node[6];
73         int i;
74
75         if (s == NULL) {
76                 return NT_STATUS_INVALID_PARAMETER;
77         }
78
79         if (11 == sscanf(s, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
80                          &time_low, &time_mid, &time_hi_and_version, 
81                          &clock_seq[0], &clock_seq[1],
82                          &node[0], &node[1], &node[2], &node[3], &node[4], &node[5])) {
83                 status = NT_STATUS_OK;
84         } else if (11 == sscanf(s, "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
85                                 &time_low, &time_mid, &time_hi_and_version, 
86                                 &clock_seq[0], &clock_seq[1],
87                                 &node[0], &node[1], &node[2], &node[3], &node[4], &node[5])) {
88                 status = NT_STATUS_OK;
89         }
90
91         if (!NT_STATUS_IS_OK(status)) {
92                 return status;
93         }
94
95         guid->time_low = time_low;
96         guid->time_mid = time_mid;
97         guid->time_hi_and_version = time_hi_and_version;
98         guid->clock_seq[0] = clock_seq[0];
99         guid->clock_seq[1] = clock_seq[1];
100         for (i=0;i<6;i++) {
101                 guid->node[i] = node[i];
102         }
103
104         return NT_STATUS_OK;
105 }
106
107 /**
108  * generate a random GUID
109  */
110 struct GUID GUID_random(void)
111 {
112         struct GUID guid;
113
114         generate_random_buffer((uint8_t *)&guid, sizeof(guid));
115         guid.clock_seq[0] = (guid.clock_seq[0] & 0x3F) | 0x80;
116         guid.time_hi_and_version = (guid.time_hi_and_version & 0x0FFF) | 0x4000;
117
118         return guid;
119 }
120
121 /**
122  * generate an empty GUID 
123  */
124 struct GUID GUID_zero(void)
125 {
126         struct GUID guid;
127
128         ZERO_STRUCT(guid);
129
130         return guid;
131 }
132
133 /**
134  * see if a range of memory is all zero. A NULL pointer is considered
135  * to be all zero 
136  */
137 BOOL all_zero(const uint8_t *ptr, size_t size)
138 {
139         int i;
140         if (!ptr) return True;
141         for (i=0;i<size;i++) {
142                 if (ptr[i]) return False;
143         }
144         return True;
145 }
146
147
148 BOOL GUID_all_zero(const struct GUID *u)
149 {
150         if (u->time_low != 0 ||
151             u->time_mid != 0 ||
152             u->time_hi_and_version != 0 ||
153             u->clock_seq[0] != 0 ||
154             u->clock_seq[1] != 0 ||
155             !all_zero(u->node, 6)) {
156                 return False;
157         }
158         return True;
159 }
160
161 BOOL GUID_equal(const struct GUID *u1, const struct GUID *u2)
162 {
163         if (u1->time_low != u2->time_low ||
164             u1->time_mid != u2->time_mid ||
165             u1->time_hi_and_version != u2->time_hi_and_version ||
166             u1->clock_seq[0] != u2->clock_seq[0] ||
167             u1->clock_seq[1] != u2->clock_seq[1] ||
168             memcmp(u1->node, u2->node, 6) != 0) {
169                 return False;
170         }
171         return True;
172 }
173
174 /**
175   its useful to be able to display these in debugging messages
176 */
177 char *GUID_string(TALLOC_CTX *mem_ctx, const struct GUID *guid)
178 {
179         return talloc_asprintf(mem_ctx, 
180                                "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
181                                guid->time_low, guid->time_mid,
182                                guid->time_hi_and_version,
183                                guid->clock_seq[0],
184                                guid->clock_seq[1],
185                                guid->node[0], guid->node[1],
186                                guid->node[2], guid->node[3],
187                                guid->node[4], guid->node[5]);
188 }
189
190 char *GUID_string2(TALLOC_CTX *mem_ctx, const struct GUID *guid)
191 {
192         char *ret, *s = GUID_string(mem_ctx, guid);
193         ret = talloc_asprintf(mem_ctx, "{%s}", s);
194         talloc_free(s);
195         return ret;
196 }
197
198 void ndr_print_GUID(struct ndr_print *ndr, const char *name, const struct GUID *guid)
199 {
200         ndr->print(ndr, "%-25s: %s", name, GUID_string(ndr, guid));
201 }
202
203 BOOL policy_handle_empty(struct policy_handle *h) 
204 {
205         return (h->handle_type == 0 && GUID_all_zero(&h->uuid));
206 }
207
208 NTSTATUS ndr_push_policy_handle(struct ndr_push *ndr, int ndr_flags, const struct policy_handle *r)
209 {
210         if (ndr_flags & NDR_SCALARS) {
211                 NDR_CHECK(ndr_push_align(ndr, 4));
212                 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, r->handle_type));
213                 NDR_CHECK(ndr_push_GUID(ndr, NDR_SCALARS, &r->uuid));
214         }
215         if (ndr_flags & NDR_BUFFERS) {
216         }
217         return NT_STATUS_OK;
218 }
219
220 NTSTATUS ndr_pull_policy_handle(struct ndr_pull *ndr, int ndr_flags, struct policy_handle *r)
221 {
222         if (ndr_flags & NDR_SCALARS) {
223                 NDR_CHECK(ndr_pull_align(ndr, 4));
224                 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &r->handle_type));
225                 NDR_CHECK(ndr_pull_GUID(ndr, NDR_SCALARS, &r->uuid));
226         }
227         if (ndr_flags & NDR_BUFFERS) {
228         }
229         return NT_STATUS_OK;
230 }
231
232 void ndr_print_policy_handle(struct ndr_print *ndr, const char *name, const struct policy_handle *r)
233 {
234         ndr_print_struct(ndr, name, "policy_handle");
235         ndr->depth++;
236         ndr_print_uint32(ndr, "handle_type", r->handle_type);
237         ndr_print_GUID(ndr, "uuid", &r->uuid);
238         ndr->depth--;
239 }
240
241 NTSTATUS ndr_push_server_id(struct ndr_push *ndr, int ndr_flags, const struct server_id *r)
242 {
243         if (ndr_flags & NDR_SCALARS) {
244                 NDR_CHECK(ndr_push_align(ndr, 4));
245                 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS,
246                                           (uint32_t)r->pid));
247 #ifdef CLUSTER_SUPPORT
248                 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS,
249                                           (uint32_t)r->vnn));
250 #endif
251         }
252         if (ndr_flags & NDR_BUFFERS) {
253         }
254         return NT_STATUS_OK;
255 }
256
257 NTSTATUS ndr_pull_server_id(struct ndr_pull *ndr, int ndr_flags, struct server_id *r)
258 {
259         if (ndr_flags & NDR_SCALARS) {
260                 uint32_t pid;
261                 NDR_CHECK(ndr_pull_align(ndr, 4));
262                 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &pid));
263 #ifdef CLUSTER_SUPPORT
264                 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &r->vnn));
265 #endif
266                 r->pid = (pid_t)pid;
267         }
268         if (ndr_flags & NDR_BUFFERS) {
269         }
270         return NT_STATUS_OK;
271 }
272
273 void ndr_print_server_id(struct ndr_print *ndr, const char *name, const struct server_id *r)
274 {
275         ndr_print_struct(ndr, name, "server_id");
276         ndr->depth++;
277         ndr_print_uint32(ndr, "id", (uint32_t)r->pid);
278 #ifdef CLUSTER_SUPPORT
279         ndr_print_uint32(ndr, "vnn", (uint32_t)r->vnn);
280 #endif
281         ndr->depth--;
282 }