Merge branch 'master' of ssh://git.samba.org/data/git/samba into crypt
[jra/samba/.git] / librpc / ndr / ndr_drsuapi.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    routines for printing some linked list structs in DRSUAPI
5
6    Copyright (C) Stefan (metze) Metzmacher 2005
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22
23 #include "includes.h"
24 #include "librpc/gen_ndr/ndr_drsuapi.h"
25 #include "librpc/gen_ndr/ndr_misc.h"
26 #include "../lib/util/asn1.h"
27 #include "librpc/ndr/ndr_compression.h"
28 /* We don't need multibyte if we're just comparing to 'ff' */
29 #undef strncasecmp
30
31 void ndr_print_drsuapi_DsReplicaObjectListItem(struct ndr_print *ndr, const char *name,
32                                                const struct drsuapi_DsReplicaObjectListItem *r)
33 {
34         ndr_print_struct(ndr, name, "drsuapi_DsReplicaObjectListItem");
35         ndr->depth++;
36         ndr_print_ptr(ndr, "next_object", r->next_object);
37         ndr_print_drsuapi_DsReplicaObject(ndr, "object", &r->object);
38         ndr->depth--;
39         if (r->next_object) {
40                 ndr_print_drsuapi_DsReplicaObjectListItem(ndr, "next_object", r->next_object);
41         }
42 }
43
44 void ndr_print_drsuapi_DsReplicaObjectListItemEx(struct ndr_print *ndr, const char *name, const struct drsuapi_DsReplicaObjectListItemEx *r)
45 {
46         ndr_print_struct(ndr, name, "drsuapi_DsReplicaObjectListItemEx");
47         ndr->depth++;
48         ndr_print_ptr(ndr, "next_object", r->next_object);
49         ndr_print_drsuapi_DsReplicaObject(ndr, "object", &r->object);
50         ndr_print_uint32(ndr, "is_nc_prefix", r->is_nc_prefix);
51         ndr_print_ptr(ndr, "parent_object_guid", r->parent_object_guid);
52         ndr->depth++;
53         if (r->parent_object_guid) {
54                 ndr_print_GUID(ndr, "parent_object_guid", r->parent_object_guid);
55         }
56         ndr->depth--;
57         ndr_print_ptr(ndr, "meta_data_ctr", r->meta_data_ctr);
58         ndr->depth++;
59         if (r->meta_data_ctr) {
60                 ndr_print_drsuapi_DsReplicaMetaDataCtr(ndr, "meta_data_ctr", r->meta_data_ctr);
61         }
62         ndr->depth--;
63         ndr->depth--;
64         if (r->next_object) {
65                 ndr_print_drsuapi_DsReplicaObjectListItemEx(ndr, "next_object", r->next_object);
66         }
67 }
68
69 #define _OID_PUSH_CHECK(call) do { \
70         bool _status; \
71         _status = call; \
72         if (_status != true) { \
73                 return ndr_push_error(ndr, NDR_ERR_SUBCONTEXT, "OID Conversion Error: %s\n", __location__); \
74         } \
75 } while (0)
76
77 #define _OID_PULL_CHECK(call) do { \
78         bool _status; \
79         _status = call; \
80         if (_status != true) { \
81                 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "OID Conversion Error: %s\n", __location__); \
82         } \
83 } while (0)
84
85 enum ndr_err_code ndr_push_drsuapi_DsReplicaOID(struct ndr_push *ndr, int ndr_flags, const struct drsuapi_DsReplicaOID *r)
86 {
87         if (ndr_flags & NDR_SCALARS) {
88                 NDR_CHECK(ndr_push_align(ndr, 4));
89                 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, ndr_size_drsuapi_DsReplicaOID_oid(r->oid, 0)));
90                 NDR_CHECK(ndr_push_unique_ptr(ndr, r->oid));
91         }
92         if (ndr_flags & NDR_BUFFERS) {
93                 if (r->oid) {
94                         DATA_BLOB blob;
95
96                         if (strncasecmp("ff", r->oid, 2) == 0) {
97                                 blob = strhex_to_data_blob(ndr, r->oid);
98                                 if (!blob.data) {
99                                         return ndr_push_error(ndr, NDR_ERR_SUBCONTEXT,
100                                                               "HEX String Conversion Error: %s\n",
101                                                               __location__);
102                                 }
103                         } else {
104                                 _OID_PUSH_CHECK(ber_write_OID_String(&blob, r->oid));
105                         }
106
107                         NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, blob.length));
108                         NDR_CHECK(ndr_push_array_uint8(ndr, NDR_SCALARS, blob.data, blob.length));
109                 }
110         }
111         return NDR_ERR_SUCCESS;
112 }
113
114 enum ndr_err_code ndr_pull_drsuapi_DsReplicaOID(struct ndr_pull *ndr, int ndr_flags, struct drsuapi_DsReplicaOID *r)
115 {
116         uint32_t _ptr_oid;
117         TALLOC_CTX *_mem_save_oid_0;
118         if (ndr_flags & NDR_SCALARS) {
119                 NDR_CHECK(ndr_pull_align(ndr, 4));
120                 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &r->__ndr_size));
121                 if (r->__ndr_size < 0 || r->__ndr_size > 10000) {
122                         return ndr_pull_error(ndr, NDR_ERR_RANGE, "value out of range");
123                 }
124                 NDR_CHECK(ndr_pull_generic_ptr(ndr, &_ptr_oid));
125                 if (_ptr_oid) {
126                         NDR_PULL_ALLOC(ndr, r->oid);
127                 } else {
128                         r->oid = NULL;
129                 }
130         }
131         if (ndr_flags & NDR_BUFFERS) {
132                 if (r->oid) {
133                         DATA_BLOB _oid_array;
134                         const char *_oid;
135
136                         _mem_save_oid_0 = NDR_PULL_GET_MEM_CTX(ndr);
137                         NDR_PULL_SET_MEM_CTX(ndr, ndr, 0);
138                         NDR_CHECK(ndr_pull_array_size(ndr, &r->oid));
139                         _oid_array.length = ndr_get_array_size(ndr, &r->oid);
140                         NDR_PULL_ALLOC_N(ndr, _oid_array.data, _oid_array.length);
141                         NDR_CHECK(ndr_pull_array_uint8(ndr, NDR_SCALARS, _oid_array.data, _oid_array.length));
142                         NDR_PULL_SET_MEM_CTX(ndr, _mem_save_oid_0, 0);
143
144                         if (_oid_array.length && _oid_array.data[0] == 0xFF) {
145                                 _oid = data_blob_hex_string(ndr, &_oid_array);
146                                 NDR_ERR_HAVE_NO_MEMORY(_oid);
147                         } else {
148                                 _OID_PULL_CHECK(ber_read_OID_String(ndr, _oid_array, &_oid));
149                         }
150                         data_blob_free(&_oid_array);
151                         talloc_steal(r->oid, _oid);
152                         r->oid = _oid;
153                 }
154                 if (r->oid) {
155                         NDR_CHECK(ndr_check_array_size(ndr, (void*)&r->oid, r->__ndr_size));
156                 }
157         }
158         return NDR_ERR_SUCCESS;
159 }
160
161 size_t ndr_size_drsuapi_DsReplicaOID_oid(const char *oid, int flags)
162 {
163         DATA_BLOB _blob;
164         size_t ret = 0;
165
166         if (!oid) return 0;
167
168         if (strncasecmp("ff", oid, 2) == 0) {
169                 _blob = strhex_to_data_blob(NULL, oid);
170                 if (_blob.data) {
171                         ret = _blob.length;
172                 }
173         } else {
174                 if (ber_write_OID_String(&_blob, oid)) {
175                         ret = _blob.length;
176                 }
177         }
178         data_blob_free(&_blob);
179         return ret;
180 }
181
182 enum ndr_err_code ndr_push_drsuapi_DsGetNCChangesMSZIPCtr1(struct ndr_push *ndr, int ndr_flags, const struct drsuapi_DsGetNCChangesMSZIPCtr1 *r)
183 {
184         if (ndr_flags & NDR_SCALARS) {
185                 uint32_t decompressed_length = 0;
186                 uint32_t compressed_length = 0;
187                 if (r->ts) {
188                         {
189                                 struct ndr_push *_ndr_ts;
190                                 NDR_CHECK(ndr_push_subcontext_start(ndr, &_ndr_ts, 4, -1));
191                                 {
192                                         struct ndr_push *_ndr_ts_compressed;
193                                         NDR_CHECK(ndr_push_compression_start(_ndr_ts, &_ndr_ts_compressed, NDR_COMPRESSION_MSZIP, -1));
194                                         NDR_CHECK(ndr_push_drsuapi_DsGetNCChangesCtr1TS(_ndr_ts_compressed, NDR_SCALARS|NDR_BUFFERS, r->ts));
195                                         decompressed_length = _ndr_ts_compressed->offset;
196                                         NDR_CHECK(ndr_push_compression_end(_ndr_ts, _ndr_ts_compressed, NDR_COMPRESSION_MSZIP, -1));
197                                 }
198                                 compressed_length = _ndr_ts->offset;
199                                 talloc_free(_ndr_ts);
200                         }
201                 }
202                 NDR_CHECK(ndr_push_align(ndr, 4));
203                 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, decompressed_length));
204                 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, compressed_length));
205                 NDR_CHECK(ndr_push_unique_ptr(ndr, r->ts));
206         }
207         if (ndr_flags & NDR_BUFFERS) {
208                 if (r->ts) {
209                         {
210                                 struct ndr_push *_ndr_ts;
211                                 NDR_CHECK(ndr_push_subcontext_start(ndr, &_ndr_ts, 4, -1));
212                                 {
213                                         struct ndr_push *_ndr_ts_compressed;
214                                         NDR_CHECK(ndr_push_compression_start(_ndr_ts, &_ndr_ts_compressed, NDR_COMPRESSION_MSZIP, -1));
215                                         NDR_CHECK(ndr_push_drsuapi_DsGetNCChangesCtr1TS(_ndr_ts_compressed, NDR_SCALARS|NDR_BUFFERS, r->ts));
216                                         NDR_CHECK(ndr_push_compression_end(_ndr_ts, _ndr_ts_compressed, NDR_COMPRESSION_MSZIP, -1));
217                                 }
218                                 NDR_CHECK(ndr_push_subcontext_end(ndr, _ndr_ts, 4, -1));
219                         }
220                 }
221         }
222         return NDR_ERR_SUCCESS;
223 }
224
225 enum ndr_err_code ndr_push_drsuapi_DsGetNCChangesMSZIPCtr6(struct ndr_push *ndr, int ndr_flags, const struct drsuapi_DsGetNCChangesMSZIPCtr6 *r)
226 {
227         if (ndr_flags & NDR_SCALARS) {
228                 uint32_t decompressed_length = 0;
229                 uint32_t compressed_length = 0;
230                 if (r->ts) {
231                         {
232                                 struct ndr_push *_ndr_ts;
233                                 NDR_CHECK(ndr_push_subcontext_start(ndr, &_ndr_ts, 4, -1));
234                                 {
235                                         struct ndr_push *_ndr_ts_compressed;
236                                         NDR_CHECK(ndr_push_compression_start(_ndr_ts, &_ndr_ts_compressed, NDR_COMPRESSION_MSZIP, -1));
237                                         NDR_CHECK(ndr_push_drsuapi_DsGetNCChangesCtr6TS(_ndr_ts_compressed, NDR_SCALARS|NDR_BUFFERS, r->ts));
238                                         decompressed_length = _ndr_ts_compressed->offset;
239                                         NDR_CHECK(ndr_push_compression_end(_ndr_ts, _ndr_ts_compressed, NDR_COMPRESSION_MSZIP, -1));
240                                 }
241                                 compressed_length = _ndr_ts->offset;
242                                 talloc_free(_ndr_ts);
243                         }
244                 }
245                 NDR_CHECK(ndr_push_align(ndr, 4));
246                 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, decompressed_length));
247                 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, compressed_length));
248                 NDR_CHECK(ndr_push_unique_ptr(ndr, r->ts));
249         }
250         if (ndr_flags & NDR_BUFFERS) {
251                 if (r->ts) {
252                         {
253                                 struct ndr_push *_ndr_ts;
254                                 NDR_CHECK(ndr_push_subcontext_start(ndr, &_ndr_ts, 4, -1));
255                                 {
256                                         struct ndr_push *_ndr_ts_compressed;
257                                         NDR_CHECK(ndr_push_compression_start(_ndr_ts, &_ndr_ts_compressed, NDR_COMPRESSION_MSZIP, -1));
258                                         NDR_CHECK(ndr_push_drsuapi_DsGetNCChangesCtr6TS(_ndr_ts_compressed, NDR_SCALARS|NDR_BUFFERS, r->ts));
259                                         NDR_CHECK(ndr_push_compression_end(_ndr_ts, _ndr_ts_compressed, NDR_COMPRESSION_MSZIP, -1));
260                                 }
261                                 NDR_CHECK(ndr_push_subcontext_end(ndr, _ndr_ts, 4, -1));
262                         }
263                 }
264         }
265         return NDR_ERR_SUCCESS;
266 }
267
268 enum ndr_err_code ndr_push_drsuapi_DsGetNCChangesXPRESSCtr1(struct ndr_push *ndr, int ndr_flags, const struct drsuapi_DsGetNCChangesXPRESSCtr1 *r)
269 {
270         if (ndr_flags & NDR_SCALARS) {
271                 uint32_t decompressed_length = 0;
272                 uint32_t compressed_length = 0;
273                 if (r->ts) {
274                         {
275                                 struct ndr_push *_ndr_ts;
276                                 NDR_CHECK(ndr_push_subcontext_start(ndr, &_ndr_ts, 4, -1));
277                                 {
278                                         struct ndr_push *_ndr_ts_compressed;
279                                         NDR_CHECK(ndr_push_compression_start(_ndr_ts, &_ndr_ts_compressed, NDR_COMPRESSION_XPRESS, -1));
280                                         NDR_CHECK(ndr_push_drsuapi_DsGetNCChangesCtr1TS(_ndr_ts_compressed, NDR_SCALARS|NDR_BUFFERS, r->ts));
281                                         decompressed_length = _ndr_ts_compressed->offset;
282                                         NDR_CHECK(ndr_push_compression_end(_ndr_ts, _ndr_ts_compressed, NDR_COMPRESSION_XPRESS, -1));
283                                 }
284                                 compressed_length = _ndr_ts->offset;
285                                 talloc_free(_ndr_ts);
286                         }
287                 }
288                 NDR_CHECK(ndr_push_align(ndr, 4));
289                 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, decompressed_length));
290                 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, compressed_length));
291                 NDR_CHECK(ndr_push_unique_ptr(ndr, r->ts));
292         }
293         if (ndr_flags & NDR_BUFFERS) {
294                 if (r->ts) {
295                         {
296                                 struct ndr_push *_ndr_ts;
297                                 NDR_CHECK(ndr_push_subcontext_start(ndr, &_ndr_ts, 4, -1));
298                                 {
299                                         struct ndr_push *_ndr_ts_compressed;
300                                         NDR_CHECK(ndr_push_compression_start(_ndr_ts, &_ndr_ts_compressed, NDR_COMPRESSION_XPRESS, -1));
301                                         NDR_CHECK(ndr_push_drsuapi_DsGetNCChangesCtr1TS(_ndr_ts_compressed, NDR_SCALARS|NDR_BUFFERS, r->ts));
302                                         NDR_CHECK(ndr_push_compression_end(_ndr_ts, _ndr_ts_compressed, NDR_COMPRESSION_XPRESS, -1));
303                                 }
304                                 NDR_CHECK(ndr_push_subcontext_end(ndr, _ndr_ts, 4, -1));
305                         }
306                 }
307         }
308         return NDR_ERR_SUCCESS;
309 }
310
311 enum ndr_err_code ndr_push_drsuapi_DsGetNCChangesXPRESSCtr6(struct ndr_push *ndr, int ndr_flags, const struct drsuapi_DsGetNCChangesXPRESSCtr6 *r)
312 {
313         if (ndr_flags & NDR_SCALARS) {
314                 uint32_t decompressed_length = 0;
315                 uint32_t compressed_length = 0;
316                 if (r->ts) {
317                         {
318                                 struct ndr_push *_ndr_ts;
319                                 NDR_CHECK(ndr_push_subcontext_start(ndr, &_ndr_ts, 4, -1));
320                                 {
321                                         struct ndr_push *_ndr_ts_compressed;
322                                         NDR_CHECK(ndr_push_compression_start(_ndr_ts, &_ndr_ts_compressed, NDR_COMPRESSION_XPRESS, -1));
323                                         NDR_CHECK(ndr_push_drsuapi_DsGetNCChangesCtr6TS(_ndr_ts_compressed, NDR_SCALARS|NDR_BUFFERS, r->ts));
324                                         decompressed_length = _ndr_ts_compressed->offset;
325                                         NDR_CHECK(ndr_push_compression_end(_ndr_ts, _ndr_ts_compressed, NDR_COMPRESSION_XPRESS, -1));
326                                 }
327                                 compressed_length = _ndr_ts->offset;
328                                 talloc_free(_ndr_ts);
329                         }
330                 }
331                 NDR_CHECK(ndr_push_align(ndr, 4));
332                 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, decompressed_length));
333                 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, compressed_length));
334                 NDR_CHECK(ndr_push_unique_ptr(ndr, r->ts));
335         }
336         if (ndr_flags & NDR_BUFFERS) {
337                 if (r->ts) {
338                         {
339                                 struct ndr_push *_ndr_ts;
340                                 NDR_CHECK(ndr_push_subcontext_start(ndr, &_ndr_ts, 4, -1));
341                                 {
342                                         struct ndr_push *_ndr_ts_compressed;
343                                         NDR_CHECK(ndr_push_compression_start(_ndr_ts, &_ndr_ts_compressed, NDR_COMPRESSION_XPRESS, -1));
344                                         NDR_CHECK(ndr_push_drsuapi_DsGetNCChangesCtr6TS(_ndr_ts_compressed, NDR_SCALARS|NDR_BUFFERS, r->ts));
345                                         NDR_CHECK(ndr_push_compression_end(_ndr_ts, _ndr_ts_compressed, NDR_COMPRESSION_XPRESS, -1));
346                                 }
347                                 NDR_CHECK(ndr_push_subcontext_end(ndr, _ndr_ts, 4, -1));
348                         }
349                 }
350         }
351         return NDR_ERR_SUCCESS;
352 }