drsuapi: share drsuapi idl and helper code.
[kamenim/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
29 void ndr_print_drsuapi_DsReplicaObjectListItem(struct ndr_print *ndr, const char *name,
30                                                const struct drsuapi_DsReplicaObjectListItem *r)
31 {
32         ndr_print_struct(ndr, name, "drsuapi_DsReplicaObjectListItem");
33         ndr->depth++;
34         ndr_print_ptr(ndr, "next_object", r->next_object);
35         ndr_print_drsuapi_DsReplicaObject(ndr, "object", &r->object);
36         ndr->depth--;
37         if (r->next_object) {
38                 ndr_print_drsuapi_DsReplicaObjectListItem(ndr, "next_object", r->next_object);
39         }
40 }
41
42 void ndr_print_drsuapi_DsReplicaObjectListItemEx(struct ndr_print *ndr, const char *name, const struct drsuapi_DsReplicaObjectListItemEx *r)
43 {
44         ndr_print_struct(ndr, name, "drsuapi_DsReplicaObjectListItemEx");
45         ndr->depth++;
46         ndr_print_ptr(ndr, "next_object", r->next_object);
47         ndr_print_drsuapi_DsReplicaObject(ndr, "object", &r->object);
48         ndr_print_uint32(ndr, "is_nc_prefix", r->is_nc_prefix);
49         ndr_print_ptr(ndr, "parent_object_guid", r->parent_object_guid);
50         ndr->depth++;
51         if (r->parent_object_guid) {
52                 ndr_print_GUID(ndr, "parent_object_guid", r->parent_object_guid);
53         }
54         ndr->depth--;
55         ndr_print_ptr(ndr, "meta_data_ctr", r->meta_data_ctr);
56         ndr->depth++;
57         if (r->meta_data_ctr) {
58                 ndr_print_drsuapi_DsReplicaMetaDataCtr(ndr, "meta_data_ctr", r->meta_data_ctr);
59         }
60         ndr->depth--;
61         ndr->depth--;
62         if (r->next_object) {
63                 ndr_print_drsuapi_DsReplicaObjectListItemEx(ndr, "next_object", r->next_object);
64         }
65 }
66
67 #define _OID_PUSH_CHECK(call) do { \
68         bool _status; \
69         _status = call; \
70         if (_status != true) { \
71                 return ndr_push_error(ndr, NDR_ERR_SUBCONTEXT, "OID Conversion Error: %s\n", __location__); \
72         } \
73 } while (0)
74
75 #define _OID_PULL_CHECK(call) do { \
76         bool _status; \
77         _status = call; \
78         if (_status != true) { \
79                 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "OID Conversion Error: %s\n", __location__); \
80         } \
81 } while (0)
82
83 enum ndr_err_code ndr_push_drsuapi_DsReplicaOID(struct ndr_push *ndr, int ndr_flags, const struct drsuapi_DsReplicaOID *r)
84 {
85         if (ndr_flags & NDR_SCALARS) {
86                 NDR_CHECK(ndr_push_align(ndr, 4));
87                 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, ndr_size_drsuapi_DsReplicaOID_oid(r->oid, 0)));
88                 NDR_CHECK(ndr_push_unique_ptr(ndr, r->oid));
89         }
90         if (ndr_flags & NDR_BUFFERS) {
91                 if (r->oid) {
92                         DATA_BLOB blob;
93
94                         if (strncasecmp("ff", r->oid, 2) == 0) {
95                                 blob = strhex_to_data_blob(ndr, r->oid);
96                                 if (!blob.data) {
97                                         return ndr_push_error(ndr, NDR_ERR_SUBCONTEXT,
98                                                               "HEX String Conversion Error: %s\n",
99                                                               __location__);
100                                 }
101                         } else {
102                                 _OID_PUSH_CHECK(ber_write_OID_String(&blob, r->oid));
103                         }
104
105                         NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, blob.length));
106                         NDR_CHECK(ndr_push_array_uint8(ndr, NDR_SCALARS, blob.data, blob.length));
107                 }
108         }
109         return NDR_ERR_SUCCESS;
110 }
111
112 enum ndr_err_code ndr_pull_drsuapi_DsReplicaOID(struct ndr_pull *ndr, int ndr_flags, struct drsuapi_DsReplicaOID *r)
113 {
114         uint32_t _ptr_oid;
115         TALLOC_CTX *_mem_save_oid_0;
116         if (ndr_flags & NDR_SCALARS) {
117                 NDR_CHECK(ndr_pull_align(ndr, 4));
118                 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &r->__ndr_size));
119                 if (r->__ndr_size < 0 || r->__ndr_size > 10000) {
120                         return ndr_pull_error(ndr, NDR_ERR_RANGE, "value out of range");
121                 }
122                 NDR_CHECK(ndr_pull_generic_ptr(ndr, &_ptr_oid));
123                 if (_ptr_oid) {
124                         NDR_PULL_ALLOC(ndr, r->oid);
125                 } else {
126                         r->oid = NULL;
127                 }
128         }
129         if (ndr_flags & NDR_BUFFERS) {
130                 if (r->oid) {
131                         DATA_BLOB _oid_array;
132                         const char *_oid;
133
134                         _mem_save_oid_0 = NDR_PULL_GET_MEM_CTX(ndr);
135                         NDR_PULL_SET_MEM_CTX(ndr, ndr, 0);
136                         NDR_CHECK(ndr_pull_array_size(ndr, &r->oid));
137                         _oid_array.length = ndr_get_array_size(ndr, &r->oid);
138                         NDR_PULL_ALLOC_N(ndr, _oid_array.data, _oid_array.length);
139                         NDR_CHECK(ndr_pull_array_uint8(ndr, NDR_SCALARS, _oid_array.data, _oid_array.length));
140                         NDR_PULL_SET_MEM_CTX(ndr, _mem_save_oid_0, 0);
141
142                         if (_oid_array.length && _oid_array.data[0] == 0xFF) {
143                                 _oid = data_blob_hex_string(ndr, &_oid_array);
144                                 NDR_ERR_HAVE_NO_MEMORY(_oid);
145                         } else {
146                                 _OID_PULL_CHECK(ber_read_OID_String(ndr, _oid_array, &_oid));
147                         }
148                         data_blob_free(&_oid_array);
149                         talloc_steal(r->oid, _oid);
150                         r->oid = _oid;
151                 }
152                 if (r->oid) {
153                         NDR_CHECK(ndr_check_array_size(ndr, (void*)&r->oid, r->__ndr_size));
154                 }
155         }
156         return NDR_ERR_SUCCESS;
157 }
158
159 size_t ndr_size_drsuapi_DsReplicaOID_oid(const char *oid, int flags)
160 {
161         DATA_BLOB _blob;
162         size_t ret = 0;
163
164         if (!oid) return 0;
165
166         if (strncasecmp("ff", oid, 2) == 0) {
167                 _blob = strhex_to_data_blob(NULL, oid);
168                 if (_blob.data) {
169                         ret = _blob.length;
170                 }
171         } else {
172                 if (ber_write_OID_String(&_blob, oid)) {
173                         ret = _blob.length;
174                 }
175         }
176         data_blob_free(&_blob);
177         return ret;
178 }
179
180 enum ndr_err_code ndr_push_drsuapi_DsGetNCChangesMSZIPCtr1(struct ndr_push *ndr, int ndr_flags, const struct drsuapi_DsGetNCChangesMSZIPCtr1 *r)
181 {
182         if (ndr_flags & NDR_SCALARS) {
183                 uint32_t decompressed_length = 0;
184                 uint32_t compressed_length = 0;
185                 if (r->ts) {
186                         {
187                                 struct ndr_push *_ndr_ts;
188                                 NDR_CHECK(ndr_push_subcontext_start(ndr, &_ndr_ts, 4, -1));
189                                 {
190                                         struct ndr_push *_ndr_ts_compressed;
191                                         NDR_CHECK(ndr_push_compression_start(_ndr_ts, &_ndr_ts_compressed, NDR_COMPRESSION_MSZIP, -1));
192                                         NDR_CHECK(ndr_push_drsuapi_DsGetNCChangesCtr1TS(_ndr_ts_compressed, NDR_SCALARS|NDR_BUFFERS, r->ts));
193                                         decompressed_length = _ndr_ts_compressed->offset;
194                                         NDR_CHECK(ndr_push_compression_end(_ndr_ts, _ndr_ts_compressed, NDR_COMPRESSION_MSZIP, -1));
195                                 }
196                                 compressed_length = _ndr_ts->offset;
197                                 talloc_free(_ndr_ts);
198                         }
199                 }
200                 NDR_CHECK(ndr_push_align(ndr, 4));
201                 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, decompressed_length));
202                 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, compressed_length));
203                 NDR_CHECK(ndr_push_unique_ptr(ndr, r->ts));
204         }
205         if (ndr_flags & NDR_BUFFERS) {
206                 if (r->ts) {
207                         {
208                                 struct ndr_push *_ndr_ts;
209                                 NDR_CHECK(ndr_push_subcontext_start(ndr, &_ndr_ts, 4, -1));
210                                 {
211                                         struct ndr_push *_ndr_ts_compressed;
212                                         NDR_CHECK(ndr_push_compression_start(_ndr_ts, &_ndr_ts_compressed, NDR_COMPRESSION_MSZIP, -1));
213                                         NDR_CHECK(ndr_push_drsuapi_DsGetNCChangesCtr1TS(_ndr_ts_compressed, NDR_SCALARS|NDR_BUFFERS, r->ts));
214                                         NDR_CHECK(ndr_push_compression_end(_ndr_ts, _ndr_ts_compressed, NDR_COMPRESSION_MSZIP, -1));
215                                 }
216                                 NDR_CHECK(ndr_push_subcontext_end(ndr, _ndr_ts, 4, -1));
217                         }
218                 }
219         }
220         return NDR_ERR_SUCCESS;
221 }
222
223 enum ndr_err_code ndr_push_drsuapi_DsGetNCChangesMSZIPCtr6(struct ndr_push *ndr, int ndr_flags, const struct drsuapi_DsGetNCChangesMSZIPCtr6 *r)
224 {
225         if (ndr_flags & NDR_SCALARS) {
226                 uint32_t decompressed_length = 0;
227                 uint32_t compressed_length = 0;
228                 if (r->ts) {
229                         {
230                                 struct ndr_push *_ndr_ts;
231                                 NDR_CHECK(ndr_push_subcontext_start(ndr, &_ndr_ts, 4, -1));
232                                 {
233                                         struct ndr_push *_ndr_ts_compressed;
234                                         NDR_CHECK(ndr_push_compression_start(_ndr_ts, &_ndr_ts_compressed, NDR_COMPRESSION_MSZIP, -1));
235                                         NDR_CHECK(ndr_push_drsuapi_DsGetNCChangesCtr6TS(_ndr_ts_compressed, NDR_SCALARS|NDR_BUFFERS, r->ts));
236                                         decompressed_length = _ndr_ts_compressed->offset;
237                                         NDR_CHECK(ndr_push_compression_end(_ndr_ts, _ndr_ts_compressed, NDR_COMPRESSION_MSZIP, -1));
238                                 }
239                                 compressed_length = _ndr_ts->offset;
240                                 talloc_free(_ndr_ts);
241                         }
242                 }
243                 NDR_CHECK(ndr_push_align(ndr, 4));
244                 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, decompressed_length));
245                 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, compressed_length));
246                 NDR_CHECK(ndr_push_unique_ptr(ndr, r->ts));
247         }
248         if (ndr_flags & NDR_BUFFERS) {
249                 if (r->ts) {
250                         {
251                                 struct ndr_push *_ndr_ts;
252                                 NDR_CHECK(ndr_push_subcontext_start(ndr, &_ndr_ts, 4, -1));
253                                 {
254                                         struct ndr_push *_ndr_ts_compressed;
255                                         NDR_CHECK(ndr_push_compression_start(_ndr_ts, &_ndr_ts_compressed, NDR_COMPRESSION_MSZIP, -1));
256                                         NDR_CHECK(ndr_push_drsuapi_DsGetNCChangesCtr6TS(_ndr_ts_compressed, NDR_SCALARS|NDR_BUFFERS, r->ts));
257                                         NDR_CHECK(ndr_push_compression_end(_ndr_ts, _ndr_ts_compressed, NDR_COMPRESSION_MSZIP, -1));
258                                 }
259                                 NDR_CHECK(ndr_push_subcontext_end(ndr, _ndr_ts, 4, -1));
260                         }
261                 }
262         }
263         return NDR_ERR_SUCCESS;
264 }
265
266 enum ndr_err_code ndr_push_drsuapi_DsGetNCChangesXPRESSCtr1(struct ndr_push *ndr, int ndr_flags, const struct drsuapi_DsGetNCChangesXPRESSCtr1 *r)
267 {
268         if (ndr_flags & NDR_SCALARS) {
269                 uint32_t decompressed_length = 0;
270                 uint32_t compressed_length = 0;
271                 if (r->ts) {
272                         {
273                                 struct ndr_push *_ndr_ts;
274                                 NDR_CHECK(ndr_push_subcontext_start(ndr, &_ndr_ts, 4, -1));
275                                 {
276                                         struct ndr_push *_ndr_ts_compressed;
277                                         NDR_CHECK(ndr_push_compression_start(_ndr_ts, &_ndr_ts_compressed, NDR_COMPRESSION_XPRESS, -1));
278                                         NDR_CHECK(ndr_push_drsuapi_DsGetNCChangesCtr1TS(_ndr_ts_compressed, NDR_SCALARS|NDR_BUFFERS, r->ts));
279                                         decompressed_length = _ndr_ts_compressed->offset;
280                                         NDR_CHECK(ndr_push_compression_end(_ndr_ts, _ndr_ts_compressed, NDR_COMPRESSION_XPRESS, -1));
281                                 }
282                                 compressed_length = _ndr_ts->offset;
283                                 talloc_free(_ndr_ts);
284                         }
285                 }
286                 NDR_CHECK(ndr_push_align(ndr, 4));
287                 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, decompressed_length));
288                 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, compressed_length));
289                 NDR_CHECK(ndr_push_unique_ptr(ndr, r->ts));
290         }
291         if (ndr_flags & NDR_BUFFERS) {
292                 if (r->ts) {
293                         {
294                                 struct ndr_push *_ndr_ts;
295                                 NDR_CHECK(ndr_push_subcontext_start(ndr, &_ndr_ts, 4, -1));
296                                 {
297                                         struct ndr_push *_ndr_ts_compressed;
298                                         NDR_CHECK(ndr_push_compression_start(_ndr_ts, &_ndr_ts_compressed, NDR_COMPRESSION_XPRESS, -1));
299                                         NDR_CHECK(ndr_push_drsuapi_DsGetNCChangesCtr1TS(_ndr_ts_compressed, NDR_SCALARS|NDR_BUFFERS, r->ts));
300                                         NDR_CHECK(ndr_push_compression_end(_ndr_ts, _ndr_ts_compressed, NDR_COMPRESSION_XPRESS, -1));
301                                 }
302                                 NDR_CHECK(ndr_push_subcontext_end(ndr, _ndr_ts, 4, -1));
303                         }
304                 }
305         }
306         return NDR_ERR_SUCCESS;
307 }
308
309 enum ndr_err_code ndr_push_drsuapi_DsGetNCChangesXPRESSCtr6(struct ndr_push *ndr, int ndr_flags, const struct drsuapi_DsGetNCChangesXPRESSCtr6 *r)
310 {
311         if (ndr_flags & NDR_SCALARS) {
312                 uint32_t decompressed_length = 0;
313                 uint32_t compressed_length = 0;
314                 if (r->ts) {
315                         {
316                                 struct ndr_push *_ndr_ts;
317                                 NDR_CHECK(ndr_push_subcontext_start(ndr, &_ndr_ts, 4, -1));
318                                 {
319                                         struct ndr_push *_ndr_ts_compressed;
320                                         NDR_CHECK(ndr_push_compression_start(_ndr_ts, &_ndr_ts_compressed, NDR_COMPRESSION_XPRESS, -1));
321                                         NDR_CHECK(ndr_push_drsuapi_DsGetNCChangesCtr6TS(_ndr_ts_compressed, NDR_SCALARS|NDR_BUFFERS, r->ts));
322                                         decompressed_length = _ndr_ts_compressed->offset;
323                                         NDR_CHECK(ndr_push_compression_end(_ndr_ts, _ndr_ts_compressed, NDR_COMPRESSION_XPRESS, -1));
324                                 }
325                                 compressed_length = _ndr_ts->offset;
326                                 talloc_free(_ndr_ts);
327                         }
328                 }
329                 NDR_CHECK(ndr_push_align(ndr, 4));
330                 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, decompressed_length));
331                 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, compressed_length));
332                 NDR_CHECK(ndr_push_unique_ptr(ndr, r->ts));
333         }
334         if (ndr_flags & NDR_BUFFERS) {
335                 if (r->ts) {
336                         {
337                                 struct ndr_push *_ndr_ts;
338                                 NDR_CHECK(ndr_push_subcontext_start(ndr, &_ndr_ts, 4, -1));
339                                 {
340                                         struct ndr_push *_ndr_ts_compressed;
341                                         NDR_CHECK(ndr_push_compression_start(_ndr_ts, &_ndr_ts_compressed, NDR_COMPRESSION_XPRESS, -1));
342                                         NDR_CHECK(ndr_push_drsuapi_DsGetNCChangesCtr6TS(_ndr_ts_compressed, NDR_SCALARS|NDR_BUFFERS, r->ts));
343                                         NDR_CHECK(ndr_push_compression_end(_ndr_ts, _ndr_ts_compressed, NDR_COMPRESSION_XPRESS, -1));
344                                 }
345                                 NDR_CHECK(ndr_push_subcontext_end(ndr, _ndr_ts, 4, -1));
346                         }
347                 }
348         }
349         return NDR_ERR_SUCCESS;
350 }