12f95a9ceb63df03f084bb704e34924658a13117
[jra/samba/.git] / librpc / ndr / ndr.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    libndr interface
5
6    Copyright (C) Andrew Tridgell 2003
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   this provides the core routines for NDR parsing functions
24
25   see http://www.opengroup.org/onlinepubs/9629399/chap14.htm for details
26   of NDR encoding rules
27 */
28
29 #include "includes.h"
30 #include "librpc/ndr/libndr.h"
31 #include "../lib/util/dlinklist.h"
32 #if _SAMBA_BUILD_ == 4
33 #include "param/param.h"
34 #endif
35
36 #define NDR_BASE_MARSHALL_SIZE 1024
37
38 /* this guid indicates NDR encoding in a protocol tower */
39 const struct ndr_syntax_id ndr_transfer_syntax = {
40   { 0x8a885d04, 0x1ceb, 0x11c9, {0x9f, 0xe8}, {0x08,0x00,0x2b,0x10,0x48,0x60} },
41   2
42 };
43
44 const struct ndr_syntax_id ndr64_transfer_syntax = {
45   { 0x71710533, 0xbeba, 0x4937, {0x83, 0x19}, {0xb5,0xdb,0xef,0x9c,0xcc,0x36} },
46   1
47 };
48
49 /*
50   work out the number of bytes needed to align on a n byte boundary
51 */
52 _PUBLIC_ size_t ndr_align_size(uint32_t offset, size_t n)
53 {
54         if ((offset & (n-1)) == 0) return 0;
55         return n - (offset & (n-1));
56 }
57
58 /*
59   initialise a ndr parse structure from a data blob
60 */
61 _PUBLIC_ struct ndr_pull *ndr_pull_init_blob(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx, struct smb_iconv_convenience *iconv_convenience)
62 {
63         struct ndr_pull *ndr;
64
65         ndr = talloc_zero(mem_ctx, struct ndr_pull);
66         if (!ndr) return NULL;
67         ndr->current_mem_ctx = mem_ctx;
68
69         ndr->data = blob->data;
70         ndr->data_size = blob->length;
71         ndr->iconv_convenience = talloc_reference(ndr, iconv_convenience);
72
73         return ndr;
74 }
75
76 /*
77   advance by 'size' bytes
78 */
79 _PUBLIC_ enum ndr_err_code ndr_pull_advance(struct ndr_pull *ndr, uint32_t size)
80 {
81         ndr->offset += size;
82         if (ndr->offset > ndr->data_size) {
83                 return ndr_pull_error(ndr, NDR_ERR_BUFSIZE, 
84                                       "ndr_pull_advance by %u failed",
85                                       size);
86         }
87         return NDR_ERR_SUCCESS;
88 }
89
90 /*
91   set the parse offset to 'ofs'
92 */
93 static enum ndr_err_code ndr_pull_set_offset(struct ndr_pull *ndr, uint32_t ofs)
94 {
95         ndr->offset = ofs;
96         if (ndr->offset > ndr->data_size) {
97                 return ndr_pull_error(ndr, NDR_ERR_BUFSIZE, 
98                                       "ndr_pull_set_offset %u failed",
99                                       ofs);
100         }
101         return NDR_ERR_SUCCESS;
102 }
103
104 /* create a ndr_push structure, ready for some marshalling */
105 _PUBLIC_ struct ndr_push *ndr_push_init_ctx(TALLOC_CTX *mem_ctx, struct smb_iconv_convenience *iconv_convenience)
106 {
107         struct ndr_push *ndr;
108
109         ndr = talloc_zero(mem_ctx, struct ndr_push);
110         if (!ndr) {
111                 return NULL;
112         }
113
114         ndr->flags = 0;
115         ndr->alloc_size = NDR_BASE_MARSHALL_SIZE;
116         ndr->data = talloc_array(ndr, uint8_t, ndr->alloc_size);
117         if (!ndr->data) {
118                 return NULL;
119         }
120         ndr->iconv_convenience = talloc_reference(ndr, iconv_convenience);
121
122         return ndr;
123 }
124
125 /* return a DATA_BLOB structure for the current ndr_push marshalled data */
126 _PUBLIC_ DATA_BLOB ndr_push_blob(struct ndr_push *ndr)
127 {
128         DATA_BLOB blob;
129         blob = data_blob_const(ndr->data, ndr->offset);
130         if (ndr->alloc_size > ndr->offset) {
131                 ndr->data[ndr->offset] = 0;
132         }
133         return blob;
134 }
135
136
137 /*
138   expand the available space in the buffer to ndr->offset + extra_size
139 */
140 _PUBLIC_ enum ndr_err_code ndr_push_expand(struct ndr_push *ndr, uint32_t extra_size)
141 {
142         uint32_t size = extra_size + ndr->offset;
143
144         if (size < ndr->offset) {
145                 /* extra_size overflowed the offset */
146                 return ndr_push_error(ndr, NDR_ERR_BUFSIZE, "Overflow in push_expand to %u",
147                                       size);
148         }
149
150         if (ndr->alloc_size > size) {
151                 return NDR_ERR_SUCCESS;
152         }
153
154         ndr->alloc_size += NDR_BASE_MARSHALL_SIZE;
155         if (size+1 > ndr->alloc_size) {
156                 ndr->alloc_size = size+1;
157         }
158         ndr->data = talloc_realloc(ndr, ndr->data, uint8_t, ndr->alloc_size);
159         if (!ndr->data) {
160                 return ndr_push_error(ndr, NDR_ERR_ALLOC, "Failed to push_expand to %u",
161                                       ndr->alloc_size);
162         }
163
164         return NDR_ERR_SUCCESS;
165 }
166
167 _PUBLIC_ void ndr_print_debug_helper(struct ndr_print *ndr, const char *format, ...) 
168 {
169         va_list ap;
170         char *s = NULL;
171         int i, ret;
172
173         va_start(ap, format);
174         ret = vasprintf(&s, format, ap);
175         va_end(ap);
176
177         if (ret == -1) {
178                 return;
179         }
180
181         for (i=0;i<ndr->depth;i++) {
182                 DEBUGADD(0,("    "));
183         }
184
185         DEBUGADD(0,("%s\n", s));
186         free(s);
187 }
188
189 _PUBLIC_ void ndr_print_string_helper(struct ndr_print *ndr, const char *format, ...)
190 {
191         va_list ap;
192         int i;
193
194         for (i=0;i<ndr->depth;i++) {
195                 ndr->private_data = talloc_asprintf_append_buffer(
196                                         (char *)ndr->private_data, "    ");
197         }
198
199         va_start(ap, format);
200         ndr->private_data = talloc_vasprintf_append_buffer((char *)ndr->private_data, 
201                                                     format, ap);
202         va_end(ap);
203         ndr->private_data = talloc_asprintf_append_buffer((char *)ndr->private_data, 
204                                                    "\n");
205 }
206
207 /*
208   a useful helper function for printing idl structures via DEBUG()
209 */
210 _PUBLIC_ void ndr_print_debug(ndr_print_fn_t fn, const char *name, void *ptr)
211 {
212         struct ndr_print *ndr;
213
214         ndr = talloc_zero(NULL, struct ndr_print);
215         if (!ndr) return;
216         ndr->print = ndr_print_debug_helper;
217         ndr->depth = 1;
218         ndr->flags = 0;
219         fn(ndr, name, ptr);
220         talloc_free(ndr);
221 }
222
223 /*
224   a useful helper function for printing idl unions via DEBUG()
225 */
226 _PUBLIC_ void ndr_print_union_debug(ndr_print_fn_t fn, const char *name, uint32_t level, void *ptr)
227 {
228         struct ndr_print *ndr;
229
230         ndr = talloc_zero(NULL, struct ndr_print);
231         if (!ndr) return;
232         ndr->print = ndr_print_debug_helper;
233         ndr->depth = 1;
234         ndr->flags = 0;
235         ndr_print_set_switch_value(ndr, ptr, level);
236         fn(ndr, name, ptr);
237         talloc_free(ndr);
238 }
239
240 /*
241   a useful helper function for printing idl function calls via DEBUG()
242 */
243 _PUBLIC_ void ndr_print_function_debug(ndr_print_function_t fn, const char *name, int flags, void *ptr)
244 {
245         struct ndr_print *ndr;
246
247         ndr = talloc_zero(NULL, struct ndr_print);
248         if (!ndr) return;
249         ndr->print = ndr_print_debug_helper;
250         ndr->depth = 1;
251         ndr->flags = 0;
252         fn(ndr, name, flags, ptr);
253         talloc_free(ndr);
254 }
255
256 /*
257   a useful helper function for printing idl structures to a string
258 */
259 _PUBLIC_ char *ndr_print_struct_string(TALLOC_CTX *mem_ctx, ndr_print_fn_t fn, const char *name, void *ptr)
260 {
261         struct ndr_print *ndr;
262         char *ret = NULL;
263
264         ndr = talloc_zero(mem_ctx, struct ndr_print);
265         if (!ndr) return NULL;
266         ndr->private_data = talloc_strdup(ndr, "");
267         if (!ndr->private_data) {
268                 goto failed;
269         }
270         ndr->print = ndr_print_string_helper;
271         ndr->depth = 1;
272         ndr->flags = 0;
273         fn(ndr, name, ptr);
274         ret = talloc_steal(mem_ctx, (char *)ndr->private_data);
275 failed:
276         talloc_free(ndr);
277         return ret;
278 }
279
280 /*
281   a useful helper function for printing idl unions to a string
282 */
283 _PUBLIC_ char *ndr_print_union_string(TALLOC_CTX *mem_ctx, ndr_print_fn_t fn, const char *name, uint32_t level, void *ptr)
284 {
285         struct ndr_print *ndr;
286         char *ret = NULL;
287
288         ndr = talloc_zero(mem_ctx, struct ndr_print);
289         if (!ndr) return NULL;
290         ndr->private_data = talloc_strdup(ndr, "");
291         if (!ndr->private_data) {
292                 goto failed;
293         }
294         ndr->print = ndr_print_string_helper;
295         ndr->depth = 1;
296         ndr->flags = 0;
297         ndr_print_set_switch_value(ndr, ptr, level);
298         fn(ndr, name, ptr);
299         ret = talloc_steal(mem_ctx, (char *)ndr->private_data);
300 failed:
301         talloc_free(ndr);
302         return ret;
303 }
304
305 /*
306   a useful helper function for printing idl function calls to a string
307 */
308 _PUBLIC_ char *ndr_print_function_string(TALLOC_CTX *mem_ctx,
309                                 ndr_print_function_t fn, const char *name, 
310                                 int flags, void *ptr)
311 {
312         struct ndr_print *ndr;
313         char *ret = NULL;
314
315         ndr = talloc_zero(mem_ctx, struct ndr_print);
316         if (!ndr) return NULL;
317         ndr->private_data = talloc_strdup(ndr, "");
318         if (!ndr->private_data) {
319                 goto failed;
320         }
321         ndr->print = ndr_print_string_helper;
322         ndr->depth = 1;
323         ndr->flags = 0;
324         fn(ndr, name, flags, ptr);
325         ret = talloc_steal(mem_ctx, (char *)ndr->private_data);
326 failed:
327         talloc_free(ndr);
328         return ret;
329 }
330
331 _PUBLIC_ void ndr_set_flags(uint32_t *pflags, uint32_t new_flags)
332 {
333         /* the big/little endian flags are inter-dependent */
334         if (new_flags & LIBNDR_FLAG_LITTLE_ENDIAN) {
335                 (*pflags) &= ~LIBNDR_FLAG_BIGENDIAN;
336         }
337         if (new_flags & LIBNDR_FLAG_BIGENDIAN) {
338                 (*pflags) &= ~LIBNDR_FLAG_LITTLE_ENDIAN;
339         }
340         if (new_flags & LIBNDR_FLAG_REMAINING) {
341                 (*pflags) &= ~LIBNDR_ALIGN_FLAGS;
342         }
343         if (new_flags & LIBNDR_ALIGN_FLAGS) {
344                 (*pflags) &= ~LIBNDR_FLAG_REMAINING;
345         }
346         (*pflags) |= new_flags;
347 }
348
349 /*
350   return and possibly log an NDR error
351 */
352 _PUBLIC_ enum ndr_err_code ndr_pull_error(struct ndr_pull *ndr,
353                                  enum ndr_err_code ndr_err,
354                                  const char *format, ...)
355 {
356         char *s=NULL;
357         va_list ap;
358         int ret;
359
360         va_start(ap, format);
361         ret = vasprintf(&s, format, ap);
362         va_end(ap);
363
364         if (ret == -1) {
365                 return NDR_ERR_ALLOC;
366         }
367
368         DEBUG(1,("ndr_pull_error(%u): %s\n", ndr_err, s));
369
370         free(s);
371
372         return ndr_err;
373 }
374
375 /*
376   return and possibly log an NDR error
377 */
378 _PUBLIC_ enum ndr_err_code ndr_push_error(struct ndr_push *ndr,
379                                  enum ndr_err_code ndr_err,
380                                  const char *format, ...) 
381 {
382         char *s=NULL;
383         va_list ap;
384         int ret;
385
386         va_start(ap, format);
387         ret = vasprintf(&s, format, ap);
388         va_end(ap);
389
390         if (ret == -1) {
391                 return NDR_ERR_ALLOC;
392         }
393
394         DEBUG(1,("ndr_push_error(%u): %s\n", ndr_err, s));
395
396         free(s);
397
398         return ndr_err;
399 }
400
401 /*
402   handle subcontext buffers, which in midl land are user-marshalled, but
403   we use magic in pidl to make them easier to cope with
404 */
405 _PUBLIC_ enum ndr_err_code ndr_pull_subcontext_start(struct ndr_pull *ndr,
406                                    struct ndr_pull **_subndr,
407                                    size_t header_size,
408                                    ssize_t size_is)
409 {
410         struct ndr_pull *subndr;
411         uint32_t r_content_size;
412         bool force_le = false;
413         bool force_be = false;
414
415         switch (header_size) {
416         case 0: {
417                 uint32_t content_size = ndr->data_size - ndr->offset;
418                 if (size_is >= 0) {
419                         content_size = size_is;
420                 }
421                 r_content_size = content_size;
422                 break;
423         }
424
425         case 2: {
426                 uint16_t content_size;
427                 NDR_CHECK(ndr_pull_uint16(ndr, NDR_SCALARS, &content_size));
428                 if (size_is >= 0 && size_is != content_size) {
429                         return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d", 
430                                                 (int)size_is, (int)content_size);
431                 }
432                 r_content_size = content_size;
433                 break;
434         }
435
436         case 4: {
437                 uint32_t content_size;
438                 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &content_size));
439                 if (size_is >= 0 && size_is != content_size) {
440                         return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d", 
441                                                 (int)size_is, (int)content_size);
442                 }
443                 r_content_size = content_size;
444                 break;
445         }
446         case 0xFFFFFC01: {
447                 /*
448                  * Common Type Header for the Serialization Stream
449                  * See [MS-RPCE] 2.2.6 Type Serialization Version 1
450                  */
451                 uint8_t version;
452                 uint8_t drep;
453                 uint16_t hdrlen;
454                 uint32_t filler;
455                 uint32_t content_size;
456                 uint32_t reserved;
457
458                 /* version */
459                 NDR_CHECK(ndr_pull_uint8(ndr, NDR_SCALARS, &version));
460
461                 if (version != 1) {
462                         return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT,
463                                               "Bad subcontext (PULL) Common Type Header version %d != 1",
464                                               (int)version);
465                 }
466
467                 /*
468                  * 0x10 little endian
469                  * 0x00 big endian
470                  */
471                 NDR_CHECK(ndr_pull_uint8(ndr, NDR_SCALARS, &drep));
472                 if (drep == 0x10) {
473                         force_le = true;
474                 } else if (drep == 0x00) {
475                         force_be = true;
476                 } else {
477                         return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT,
478                                               "Bad subcontext (PULL) Common Type Header invalid drep 0x%02X",
479                                               (unsigned int)drep);
480                 }
481
482                 /* length of the "Private Header for Constructed Type" */
483                 NDR_CHECK(ndr_pull_uint16(ndr, NDR_SCALARS, &hdrlen));
484                 if (hdrlen != 8) {
485                         return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT,
486                                               "Bad subcontext (PULL) Common Type Header length %d != 8",
487                                               (int)hdrlen);
488                 }
489
490                 /* filler should be ignored */
491                 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &filler));
492
493                 /*
494                  * Private Header for Constructed Type
495                  */
496                 /* length - will be updated latter */
497                 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &content_size));
498                 if (size_is >= 0 && size_is != content_size) {
499                         return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
500                                               (int)size_is, (int)content_size);
501                 }
502                 /* the content size must be a multiple of 8 */
503                 if ((content_size % 8) != 0) {
504                         return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT,
505                                               "Bad subcontext (PULL) size_is(%d) not padded to 8 content_size %d",
506                                               (int)size_is, (int)content_size);
507                 }
508                 r_content_size = content_size;
509
510                 /* reserved */
511                 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &reserved));
512                 break;
513         }
514         default:
515                 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) header_size %d", 
516                                       (int)header_size);
517         }
518
519         NDR_PULL_NEED_BYTES(ndr, r_content_size);
520
521         subndr = talloc_zero(ndr, struct ndr_pull);
522         NDR_ERR_HAVE_NO_MEMORY(subndr);
523         subndr->flags           = ndr->flags;
524         subndr->current_mem_ctx = ndr->current_mem_ctx;
525
526         subndr->data = ndr->data + ndr->offset;
527         subndr->offset = 0;
528         subndr->data_size = r_content_size;
529         subndr->iconv_convenience = talloc_reference(subndr, ndr->iconv_convenience);
530
531         if (force_le) {
532                 ndr_set_flags(&ndr->flags, LIBNDR_FLAG_LITTLE_ENDIAN);
533         } else if (force_be) {
534                 ndr_set_flags(&ndr->flags, LIBNDR_FLAG_BIGENDIAN);
535         }
536
537         *_subndr = subndr;
538         return NDR_ERR_SUCCESS;
539 }
540
541 _PUBLIC_ enum ndr_err_code ndr_pull_subcontext_end(struct ndr_pull *ndr,
542                                  struct ndr_pull *subndr,
543                                  size_t header_size,
544                                  ssize_t size_is)
545 {
546         uint32_t advance;
547         if (size_is >= 0) {
548                 advance = size_is;
549         } else if (header_size > 0) {
550                 advance = subndr->data_size;
551         } else {
552                 advance = subndr->offset;
553         }
554         NDR_CHECK(ndr_pull_advance(ndr, advance));
555         return NDR_ERR_SUCCESS;
556 }
557
558 _PUBLIC_ enum ndr_err_code ndr_push_subcontext_start(struct ndr_push *ndr,
559                                    struct ndr_push **_subndr,
560                                    size_t header_size,
561                                    ssize_t size_is)
562 {
563         struct ndr_push *subndr;
564
565         subndr = ndr_push_init_ctx(ndr, ndr->iconv_convenience);
566         NDR_ERR_HAVE_NO_MEMORY(subndr);
567         subndr->flags   = ndr->flags;
568
569         *_subndr = subndr;
570         return NDR_ERR_SUCCESS;
571 }
572
573 /*
574   push a subcontext header 
575 */
576 _PUBLIC_ enum ndr_err_code ndr_push_subcontext_end(struct ndr_push *ndr,
577                                  struct ndr_push *subndr,
578                                  size_t header_size,
579                                  ssize_t size_is)
580 {
581         ssize_t padding_len;
582
583         if (size_is >= 0) {
584                 padding_len = size_is - subndr->offset;
585                 if (padding_len > 0) {
586                         NDR_CHECK(ndr_push_zero(subndr, padding_len));
587                 } else if (padding_len < 0) {
588                         return ndr_push_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PUSH) content_size %d is larger than size_is(%d)",
589                                               (int)subndr->offset, (int)size_is);
590                 }
591         }
592
593         switch (header_size) {
594         case 0: 
595                 break;
596
597         case 2: 
598                 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, subndr->offset));
599                 break;
600
601         case 4: 
602                 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, subndr->offset));
603                 break;
604
605         case 0xFFFFFC01:
606                 /*
607                  * Common Type Header for the Serialization Stream
608                  * See [MS-RPCE] 2.2.6 Type Serialization Version 1
609                  */
610                 padding_len = NDR_ROUND(subndr->offset, 8) - subndr->offset;
611                 if (padding_len > 0) {
612                         NDR_CHECK(ndr_push_zero(subndr, padding_len));
613                 }
614
615                 /* version */
616                 NDR_CHECK(ndr_push_uint8(ndr, NDR_SCALARS, 1));
617
618                 /*
619                  * 0x10 little endian
620                  * 0x00 big endian
621                  */
622                 NDR_CHECK(ndr_push_uint8(ndr, NDR_SCALARS, NDR_BE(ndr)?0x00:0x10));
623
624                 /* length of the "Private Header for Constructed Type" */
625                 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, 8));
626
627                 /* filler */
628                 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0xCCCCCCCC));
629
630                 /*
631                  * Private Header for Constructed Type
632                  */
633                 /* length - will be updated latter */
634                 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, subndr->offset));
635
636                 /* reserved */
637                 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0));
638                 break;
639
640         default:
641                 return ndr_push_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext header size %d", 
642                                       (int)header_size);
643         }
644
645         NDR_CHECK(ndr_push_bytes(ndr, subndr->data, subndr->offset));
646         return NDR_ERR_SUCCESS;
647 }
648
649 /*
650   store a token in the ndr context, for later retrieval
651 */
652 _PUBLIC_ enum ndr_err_code ndr_token_store(TALLOC_CTX *mem_ctx,
653                          struct ndr_token_list **list, 
654                          const void *key, 
655                          uint32_t value)
656 {
657         struct ndr_token_list *tok;
658         tok = talloc(mem_ctx, struct ndr_token_list);
659         NDR_ERR_HAVE_NO_MEMORY(tok);
660         tok->key = key;
661         tok->value = value;
662         DLIST_ADD((*list), tok);
663         return NDR_ERR_SUCCESS;
664 }
665
666 /*
667   retrieve a token from a ndr context, using cmp_fn to match the tokens
668 */
669 _PUBLIC_ enum ndr_err_code ndr_token_retrieve_cmp_fn(struct ndr_token_list **list, const void *key, uint32_t *v,
670                                    comparison_fn_t _cmp_fn, bool _remove_tok)
671 {
672         struct ndr_token_list *tok;
673         for (tok=*list;tok;tok=tok->next) {
674                 if (_cmp_fn && _cmp_fn(tok->key,key)==0) goto found;
675                 else if (!_cmp_fn && tok->key == key) goto found;
676         }
677         return NDR_ERR_TOKEN;
678 found:
679         *v = tok->value;
680         if (_remove_tok) {
681                 DLIST_REMOVE((*list), tok);
682                 talloc_free(tok);
683         }
684         return NDR_ERR_SUCCESS;
685 }
686
687 /*
688   retrieve a token from a ndr context
689 */
690 _PUBLIC_ enum ndr_err_code ndr_token_retrieve(struct ndr_token_list **list, const void *key, uint32_t *v)
691 {
692         return ndr_token_retrieve_cmp_fn(list, key, v, NULL, true);
693 }
694
695 /*
696   peek at but don't removed a token from a ndr context
697 */
698 _PUBLIC_ uint32_t ndr_token_peek(struct ndr_token_list **list, const void *key)
699 {
700         enum ndr_err_code status;
701         uint32_t v;
702
703         status = ndr_token_retrieve_cmp_fn(list, key, &v, NULL, false);
704         if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
705                 return 0;
706         }
707
708         return v;
709 }
710
711 /*
712   pull an array size field and add it to the array_size_list token list
713 */
714 _PUBLIC_ enum ndr_err_code ndr_pull_array_size(struct ndr_pull *ndr, const void *p)
715 {
716         uint32_t size;
717         NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &size));
718         return ndr_token_store(ndr, &ndr->array_size_list, p, size);
719 }
720
721 /*
722   get the stored array size field
723 */
724 _PUBLIC_ uint32_t ndr_get_array_size(struct ndr_pull *ndr, const void *p)
725 {
726         return ndr_token_peek(&ndr->array_size_list, p);
727 }
728
729 /*
730   check the stored array size field
731 */
732 _PUBLIC_ enum ndr_err_code ndr_check_array_size(struct ndr_pull *ndr, void *p, uint32_t size)
733 {
734         uint32_t stored;
735         stored = ndr_token_peek(&ndr->array_size_list, p);
736         if (stored != size) {
737                 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE, 
738                                       "Bad array size - got %u expected %u\n",
739                                       stored, size);
740         }
741         return NDR_ERR_SUCCESS;
742 }
743
744 /*
745   pull an array length field and add it to the array_length_list token list
746 */
747 _PUBLIC_ enum ndr_err_code ndr_pull_array_length(struct ndr_pull *ndr, const void *p)
748 {
749         uint32_t length, offset;
750         NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &offset));
751         if (offset != 0) {
752                 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE, 
753                                       "non-zero array offset %u\n", offset);
754         }
755         NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &length));
756         return ndr_token_store(ndr, &ndr->array_length_list, p, length);
757 }
758
759 /*
760   get the stored array length field
761 */
762 _PUBLIC_ uint32_t ndr_get_array_length(struct ndr_pull *ndr, const void *p)
763 {
764         return ndr_token_peek(&ndr->array_length_list, p);
765 }
766
767 /*
768   check the stored array length field
769 */
770 _PUBLIC_ enum ndr_err_code ndr_check_array_length(struct ndr_pull *ndr, void *p, uint32_t length)
771 {
772         uint32_t stored;
773         stored = ndr_token_peek(&ndr->array_length_list, p);
774         if (stored != length) {
775                 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE, 
776                                       "Bad array length - got %u expected %u\n",
777                                       stored, length);
778         }
779         return NDR_ERR_SUCCESS;
780 }
781
782 /*
783   store a switch value
784  */
785 _PUBLIC_ enum ndr_err_code ndr_push_set_switch_value(struct ndr_push *ndr, const void *p, uint32_t val)
786 {
787         return ndr_token_store(ndr, &ndr->switch_list, p, val);
788 }
789
790 _PUBLIC_ enum ndr_err_code ndr_pull_set_switch_value(struct ndr_pull *ndr, const void *p, uint32_t val)
791 {
792         return ndr_token_store(ndr, &ndr->switch_list, p, val);
793 }
794
795 _PUBLIC_ enum ndr_err_code ndr_print_set_switch_value(struct ndr_print *ndr, const void *p, uint32_t val)
796 {
797         return ndr_token_store(ndr, &ndr->switch_list, p, val);
798 }
799
800 /*
801   retrieve a switch value
802  */
803 _PUBLIC_ uint32_t ndr_push_get_switch_value(struct ndr_push *ndr, const void *p)
804 {
805         return ndr_token_peek(&ndr->switch_list, p);
806 }
807
808 _PUBLIC_ uint32_t ndr_pull_get_switch_value(struct ndr_pull *ndr, const void *p)
809 {
810         return ndr_token_peek(&ndr->switch_list, p);
811 }
812
813 _PUBLIC_ uint32_t ndr_print_get_switch_value(struct ndr_print *ndr, const void *p)
814 {
815         return ndr_token_peek(&ndr->switch_list, p);
816 }
817
818 /*
819   pull a struct from a blob using NDR
820 */
821 _PUBLIC_ enum ndr_err_code ndr_pull_struct_blob(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx, struct smb_iconv_convenience *iconv_convenience, void *p,
822                               ndr_pull_flags_fn_t fn)
823 {
824         struct ndr_pull *ndr;
825         ndr = ndr_pull_init_blob(blob, mem_ctx, iconv_convenience);
826         NDR_ERR_HAVE_NO_MEMORY(ndr);
827         NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
828         return NDR_ERR_SUCCESS;
829 }
830
831 /*
832   pull a struct from a blob using NDR - failing if all bytes are not consumed
833 */
834 _PUBLIC_ enum ndr_err_code ndr_pull_struct_blob_all(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx, 
835                                                     struct smb_iconv_convenience *iconv_convenience,
836                                                     void *p, ndr_pull_flags_fn_t fn)
837 {
838         struct ndr_pull *ndr;
839         ndr = ndr_pull_init_blob(blob, mem_ctx, iconv_convenience);
840         NDR_ERR_HAVE_NO_MEMORY(ndr);
841         NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
842         if (ndr->offset < ndr->data_size) {
843                 return ndr_pull_error(ndr, NDR_ERR_UNREAD_BYTES,
844                                       "not all bytes consumed ofs[%u] size[%u]",
845                                       ndr->offset, ndr->data_size);
846         }
847         return NDR_ERR_SUCCESS;
848 }
849
850 /*
851   pull a union from a blob using NDR, given the union discriminator
852 */
853 _PUBLIC_ enum ndr_err_code ndr_pull_union_blob(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx, 
854                                                struct smb_iconv_convenience *iconv_convenience, void *p,
855                              uint32_t level, ndr_pull_flags_fn_t fn)
856 {
857         struct ndr_pull *ndr;
858         ndr = ndr_pull_init_blob(blob, mem_ctx, iconv_convenience);
859         NDR_ERR_HAVE_NO_MEMORY(ndr);
860         NDR_CHECK(ndr_pull_set_switch_value(ndr, p, level));
861         NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
862         return NDR_ERR_SUCCESS;
863 }
864
865 /*
866   pull a union from a blob using NDR, given the union discriminator,
867   failing if all bytes are not consumed
868 */
869 _PUBLIC_ enum ndr_err_code ndr_pull_union_blob_all(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx, 
870                                                    struct smb_iconv_convenience *iconv_convenience, void *p,
871                              uint32_t level, ndr_pull_flags_fn_t fn)
872 {
873         struct ndr_pull *ndr;
874         ndr = ndr_pull_init_blob(blob, mem_ctx, iconv_convenience);
875         NDR_ERR_HAVE_NO_MEMORY(ndr);
876         NDR_CHECK(ndr_pull_set_switch_value(ndr, p, level));
877         NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
878         if (ndr->offset < ndr->data_size) {
879                 return ndr_pull_error(ndr, NDR_ERR_UNREAD_BYTES,
880                                       "not all bytes consumed ofs[%u] size[%u]",
881                                       ndr->offset, ndr->data_size);
882         }
883         return NDR_ERR_SUCCESS;
884 }
885
886 /*
887   push a struct to a blob using NDR
888 */
889 _PUBLIC_ enum ndr_err_code ndr_push_struct_blob(DATA_BLOB *blob, TALLOC_CTX *mem_ctx, struct smb_iconv_convenience *iconv_convenience, const void *p, ndr_push_flags_fn_t fn)
890 {
891         struct ndr_push *ndr;
892         ndr = ndr_push_init_ctx(mem_ctx, iconv_convenience);
893         NDR_ERR_HAVE_NO_MEMORY(ndr);
894
895         NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
896
897         *blob = ndr_push_blob(ndr);
898         talloc_steal(mem_ctx, blob->data);
899         talloc_free(ndr);
900
901         return NDR_ERR_SUCCESS;
902 }
903
904 /*
905   push a union to a blob using NDR
906 */
907 _PUBLIC_ enum ndr_err_code ndr_push_union_blob(DATA_BLOB *blob, TALLOC_CTX *mem_ctx, struct smb_iconv_convenience *iconv_convenience, void *p,
908                              uint32_t level, ndr_push_flags_fn_t fn)
909 {
910         struct ndr_push *ndr;
911         ndr = ndr_push_init_ctx(mem_ctx, iconv_convenience);
912         NDR_ERR_HAVE_NO_MEMORY(ndr);
913
914         NDR_CHECK(ndr_push_set_switch_value(ndr, p, level));
915         NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
916
917         *blob = ndr_push_blob(ndr);
918         talloc_steal(mem_ctx, blob->data);
919         talloc_free(ndr);
920
921         return NDR_ERR_SUCCESS;
922 }
923
924 /*
925   generic ndr_size_*() handler for structures
926 */
927 _PUBLIC_ size_t ndr_size_struct(const void *p, int flags, ndr_push_flags_fn_t push)
928 {
929         struct ndr_push *ndr;
930         enum ndr_err_code status;
931         size_t ret;
932
933         /* avoid recursion */
934         if (flags & LIBNDR_FLAG_NO_NDR_SIZE) return 0;
935
936         ndr = ndr_push_init_ctx(NULL, global_iconv_convenience);
937         if (!ndr) return 0;
938         ndr->flags |= flags | LIBNDR_FLAG_NO_NDR_SIZE;
939         status = push(ndr, NDR_SCALARS|NDR_BUFFERS, discard_const(p));
940         if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
941                 talloc_free(ndr);
942                 return 0;
943         }
944         ret = ndr->offset;
945         talloc_free(ndr);
946         return ret;
947 }
948
949 /*
950   generic ndr_size_*() handler for unions
951 */
952 _PUBLIC_ size_t ndr_size_union(const void *p, int flags, uint32_t level, ndr_push_flags_fn_t push)
953 {
954         struct ndr_push *ndr;
955         enum ndr_err_code status;
956         size_t ret;
957
958         /* avoid recursion */
959         if (flags & LIBNDR_FLAG_NO_NDR_SIZE) return 0;
960
961         ndr = ndr_push_init_ctx(NULL, global_iconv_convenience);
962         if (!ndr) return 0;
963         ndr->flags |= flags | LIBNDR_FLAG_NO_NDR_SIZE;
964
965         status = ndr_push_set_switch_value(ndr, p, level);
966         if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
967                 talloc_free(ndr);
968                 return 0;
969         }
970         status = push(ndr, NDR_SCALARS|NDR_BUFFERS, p);
971         if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
972                 talloc_free(ndr);
973                 return 0;
974         }
975         ret = ndr->offset;
976         talloc_free(ndr);
977         return ret;
978 }
979
980 /*
981   get the current base for relative pointers for the push
982 */
983 _PUBLIC_ uint32_t ndr_push_get_relative_base_offset(struct ndr_push *ndr)
984 {
985         return ndr->relative_base_offset;
986 }
987
988 /*
989   restore the old base for relative pointers for the push
990 */
991 _PUBLIC_ void ndr_push_restore_relative_base_offset(struct ndr_push *ndr, uint32_t offset)
992 {
993         ndr->relative_base_offset = offset;
994 }
995
996 /*
997   setup the current base for relative pointers for the push
998   called in the NDR_SCALAR stage
999 */
1000 _PUBLIC_ enum ndr_err_code ndr_push_setup_relative_base_offset1(struct ndr_push *ndr, const void *p, uint32_t offset)
1001 {
1002         ndr->relative_base_offset = offset;
1003         return ndr_token_store(ndr, &ndr->relative_base_list, p, offset);
1004 }
1005
1006 /*
1007   setup the current base for relative pointers for the push
1008   called in the NDR_BUFFERS stage
1009 */
1010 _PUBLIC_ enum ndr_err_code ndr_push_setup_relative_base_offset2(struct ndr_push *ndr, const void *p)
1011 {
1012         return ndr_token_retrieve(&ndr->relative_base_list, p, &ndr->relative_base_offset);
1013 }
1014
1015 /*
1016   push a relative object - stage1
1017   this is called during SCALARS processing
1018 */
1019 _PUBLIC_ enum ndr_err_code ndr_push_relative_ptr1(struct ndr_push *ndr, const void *p)
1020 {
1021         if (p == NULL) {
1022                 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0));
1023                 return NDR_ERR_SUCCESS;
1024         }
1025         NDR_CHECK(ndr_push_align(ndr, 4));
1026         NDR_CHECK(ndr_token_store(ndr, &ndr->relative_list, p, ndr->offset));
1027         return ndr_push_uint32(ndr, NDR_SCALARS, 0xFFFFFFFF);
1028 }
1029
1030 /*
1031   push a relative object - stage2
1032   this is called during buffers processing
1033 */
1034 _PUBLIC_ enum ndr_err_code ndr_push_relative_ptr2(struct ndr_push *ndr, const void *p)
1035 {
1036         uint32_t save_offset;
1037         uint32_t ptr_offset = 0xFFFFFFFF;
1038         if (p == NULL) {
1039                 return NDR_ERR_SUCCESS;
1040         }
1041         save_offset = ndr->offset;
1042         NDR_CHECK(ndr_token_retrieve(&ndr->relative_list, p, &ptr_offset));
1043         if (ptr_offset > ndr->offset) {
1044                 return ndr_push_error(ndr, NDR_ERR_BUFSIZE, 
1045                                       "ndr_push_relative_ptr2 ptr_offset(%u) > ndr->offset(%u)",
1046                                       ptr_offset, ndr->offset);
1047         }
1048         ndr->offset = ptr_offset;
1049         if (save_offset < ndr->relative_base_offset) {
1050                 return ndr_push_error(ndr, NDR_ERR_BUFSIZE, 
1051                                       "ndr_push_relative_ptr2 save_offset(%u) < ndr->relative_base_offset(%u)",
1052                                       save_offset, ndr->relative_base_offset);
1053         }       
1054         NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, save_offset - ndr->relative_base_offset));
1055         ndr->offset = save_offset;
1056         return NDR_ERR_SUCCESS;
1057 }
1058
1059 /*
1060   get the current base for relative pointers for the pull
1061 */
1062 _PUBLIC_ uint32_t ndr_pull_get_relative_base_offset(struct ndr_pull *ndr)
1063 {
1064         return ndr->relative_base_offset;
1065 }
1066
1067 /*
1068   restore the old base for relative pointers for the pull
1069 */
1070 _PUBLIC_ void ndr_pull_restore_relative_base_offset(struct ndr_pull *ndr, uint32_t offset)
1071 {
1072         ndr->relative_base_offset = offset;
1073 }
1074
1075 /*
1076   setup the current base for relative pointers for the pull
1077   called in the NDR_SCALAR stage
1078 */
1079 _PUBLIC_ enum ndr_err_code ndr_pull_setup_relative_base_offset1(struct ndr_pull *ndr, const void *p, uint32_t offset)
1080 {
1081         ndr->relative_base_offset = offset;
1082         return ndr_token_store(ndr, &ndr->relative_base_list, p, offset);
1083 }
1084
1085 /*
1086   setup the current base for relative pointers for the pull
1087   called in the NDR_BUFFERS stage
1088 */
1089 _PUBLIC_ enum ndr_err_code ndr_pull_setup_relative_base_offset2(struct ndr_pull *ndr, const void *p)
1090 {
1091         return ndr_token_retrieve(&ndr->relative_base_list, p, &ndr->relative_base_offset);
1092 }
1093
1094 /*
1095   pull a relative object - stage1
1096   called during SCALARS processing
1097 */
1098 _PUBLIC_ enum ndr_err_code ndr_pull_relative_ptr1(struct ndr_pull *ndr, const void *p, uint32_t rel_offset)
1099 {
1100         rel_offset += ndr->relative_base_offset;
1101         if (rel_offset > ndr->data_size) {
1102                 return ndr_pull_error(ndr, NDR_ERR_BUFSIZE, 
1103                                       "ndr_pull_relative_ptr1 rel_offset(%u) > ndr->data_size(%u)",
1104                                       rel_offset, ndr->data_size);
1105         }
1106         return ndr_token_store(ndr, &ndr->relative_list, p, rel_offset);
1107 }
1108
1109 /*
1110   pull a relative object - stage2
1111   called during BUFFERS processing
1112 */
1113 _PUBLIC_ enum ndr_err_code ndr_pull_relative_ptr2(struct ndr_pull *ndr, const void *p)
1114 {
1115         uint32_t rel_offset;
1116         NDR_CHECK(ndr_token_retrieve(&ndr->relative_list, p, &rel_offset));
1117         return ndr_pull_set_offset(ndr, rel_offset);
1118 }