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