2 Unix SMB/CIFS implementation.
6 Copyright (C) Andrew Tridgell 2003
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 2 of the License, or
11 (at your option) any later version.
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.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 this provides the core routines for NDR parsing functions
26 see http://www.opengroup.org/onlinepubs/9629399/chap14.htm for details
31 #include "dlinklist.h"
33 #define NDR_BASE_MARSHALL_SIZE 1024
36 work out the number of bytes needed to align on a n byte boundary
38 size_t ndr_align_size(uint32_t offset, size_t n)
40 if ((offset & (n-1)) == 0) return 0;
41 return n - (offset & (n-1));
45 initialise a ndr parse structure from a data blob
47 struct ndr_pull *ndr_pull_init_blob(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx)
51 ndr = talloc_zero(mem_ctx, struct ndr_pull);
52 if (!ndr) return NULL;
54 ndr->data = blob->data;
55 ndr->data_size = blob->length;
61 create an ndr sub-context based on an existing context. The new context starts
62 at the current offset, with the given size limit
64 NTSTATUS ndr_pull_subcontext(struct ndr_pull *ndr, struct ndr_pull *ndr2, uint32_t size)
66 NDR_PULL_NEED_BYTES(ndr, size);
68 ndr2->data += ndr2->offset;
70 ndr2->data_size = size;
71 ndr2->flags = ndr->flags;
77 advance by 'size' bytes
79 NTSTATUS ndr_pull_advance(struct ndr_pull *ndr, uint32_t size)
82 if (ndr->offset > ndr->data_size) {
83 return ndr_pull_error(ndr, NDR_ERR_BUFSIZE,
84 "ndr_pull_advance by %u failed",
91 set the parse offset to 'ofs'
93 static NTSTATUS ndr_pull_set_offset(struct ndr_pull *ndr, uint32_t ofs)
96 if (ndr->offset > ndr->data_size) {
97 return ndr_pull_error(ndr, NDR_ERR_BUFSIZE,
98 "ndr_pull_set_offset %u failed",
104 /* save the offset/size of the current ndr state */
105 void ndr_pull_save(struct ndr_pull *ndr, struct ndr_pull_save *save)
107 save->offset = ndr->offset;
108 save->data_size = ndr->data_size;
111 /* restore the size/offset of a ndr structure */
112 void ndr_pull_restore(struct ndr_pull *ndr, struct ndr_pull_save *save)
114 ndr->offset = save->offset;
115 ndr->data_size = save->data_size;
119 /* create a ndr_push structure, ready for some marshalling */
120 struct ndr_push *ndr_push_init_ctx(TALLOC_CTX *mem_ctx)
122 struct ndr_push *ndr;
124 ndr = talloc_zero(mem_ctx, struct ndr_push);
130 ndr->alloc_size = NDR_BASE_MARSHALL_SIZE;
131 ndr->data = talloc_array(ndr, uint8_t, ndr->alloc_size);
140 /* create a ndr_push structure, ready for some marshalling */
141 struct ndr_push *ndr_push_init(void)
143 return ndr_push_init_ctx(NULL);
146 /* free a ndr_push structure */
147 void ndr_push_free(struct ndr_push *ndr)
153 /* return a DATA_BLOB structure for the current ndr_push marshalled data */
154 DATA_BLOB ndr_push_blob(struct ndr_push *ndr)
157 blob.data = ndr->data;
158 blob.length = ndr->offset;
164 expand the available space in the buffer to 'size'
166 NTSTATUS ndr_push_expand(struct ndr_push *ndr, uint32_t size)
168 if (ndr->alloc_size >= size) {
172 ndr->alloc_size += NDR_BASE_MARSHALL_SIZE;
173 if (size > ndr->alloc_size) {
174 ndr->alloc_size = size;
176 ndr->data = talloc_realloc(ndr, ndr->data, uint8_t, ndr->alloc_size);
178 return ndr_push_error(ndr, NDR_ERR_ALLOC, "Failed to push_expand to %u",
185 void ndr_print_debug_helper(struct ndr_print *ndr, const char *format, ...) _PRINTF_ATTRIBUTE(2,3)
191 va_start(ap, format);
192 vasprintf(&s, format, ap);
195 for (i=0;i<ndr->depth;i++) {
199 DEBUG(0,("%s\n", s));
204 a useful helper function for printing idl structures via DEBUG()
206 void ndr_print_debug(ndr_print_fn_t fn, const char *name, void *ptr)
208 struct ndr_print *ndr;
210 ndr = talloc_zero(NULL, struct ndr_print);
212 ndr->print = ndr_print_debug_helper;
220 a useful helper function for printing idl unions via DEBUG()
222 void ndr_print_union_debug(ndr_print_fn_t fn, const char *name, uint32_t level, void *ptr)
224 struct ndr_print *ndr;
226 ndr = talloc_zero(NULL, struct ndr_print);
228 ndr->print = ndr_print_debug_helper;
231 ndr_print_set_switch_value(ndr, ptr, level);
237 a useful helper function for printing idl function calls via DEBUG()
239 void ndr_print_function_debug(ndr_print_function_t fn, const char *name, int flags, void *ptr)
241 struct ndr_print *ndr;
243 ndr = talloc_zero(NULL, struct ndr_print);
245 ndr->print = ndr_print_debug_helper;
248 fn(ndr, name, flags, ptr);
252 void ndr_set_flags(uint32_t *pflags, uint32_t new_flags)
254 /* the big/little endian flags are inter-dependent */
255 if (new_flags & LIBNDR_FLAG_LITTLE_ENDIAN) {
256 (*pflags) &= ~LIBNDR_FLAG_BIGENDIAN;
258 if (new_flags & LIBNDR_FLAG_BIGENDIAN) {
259 (*pflags) &= ~LIBNDR_FLAG_LITTLE_ENDIAN;
261 (*pflags) |= new_flags;
264 static NTSTATUS ndr_map_error(enum ndr_err_code err)
267 case NDR_ERR_BUFSIZE:
268 return NT_STATUS_BUFFER_TOO_SMALL;
270 return NT_STATUS_INTERNAL_ERROR;
272 return NT_STATUS_NO_MEMORY;
273 case NDR_ERR_ARRAY_SIZE:
274 return NT_STATUS_ARRAY_BOUNDS_EXCEEDED;
279 /* we should map all error codes to different status codes */
280 return NT_STATUS_INVALID_PARAMETER;
284 return and possibly log an NDR error
286 NTSTATUS ndr_pull_error(struct ndr_pull *ndr,
287 enum ndr_err_code err, const char *format, ...) _PRINTF_ATTRIBUTE(3,4)
292 va_start(ap, format);
293 vasprintf(&s, format, ap);
296 DEBUG(3,("ndr_pull_error(%u): %s\n", err, s));
300 return ndr_map_error(err);
304 return and possibly log an NDR error
306 NTSTATUS ndr_push_error(struct ndr_push *ndr, enum ndr_err_code err, const char *format, ...) _PRINTF_ATTRIBUTE(3,4)
311 va_start(ap, format);
312 vasprintf(&s, format, ap);
315 DEBUG(3,("ndr_push_error(%u): %s\n", err, s));
319 return ndr_map_error(err);
324 handle subcontext buffers, which in midl land are user-marshalled, but
325 we use magic in pidl to make them easier to cope with
327 NTSTATUS ndr_pull_subcontext_header(struct ndr_pull *ndr,
330 struct ndr_pull *ndr2)
332 ndr2->flags = ndr->flags;
334 switch (header_size) {
336 uint32_t content_size = ndr->data_size - ndr->offset;
338 content_size = size_is;
340 NDR_CHECK(ndr_pull_subcontext(ndr, ndr2, content_size));
345 uint16_t content_size;
346 NDR_CHECK(ndr_pull_uint16(ndr, NDR_SCALARS, &content_size));
347 if (size_is >= 0 && size_is != content_size) {
348 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
349 size_is, content_size);
351 NDR_CHECK(ndr_pull_subcontext(ndr, ndr2, content_size));
356 uint32_t content_size;
357 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &content_size));
358 if (size_is >= 0 && size_is != content_size) {
359 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
360 size_is, content_size);
362 NDR_CHECK(ndr_pull_subcontext(ndr, ndr2, content_size));
366 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) header_size %d",
373 push a subcontext header
375 NTSTATUS ndr_push_subcontext_header(struct ndr_push *ndr,
378 struct ndr_push *ndr2)
381 ssize_t padding_len = size_is - ndr2->offset;
382 if (padding_len > 0) {
383 NDR_CHECK(ndr_push_zero(ndr2, padding_len));
384 } else if (padding_len < 0) {
385 return ndr_push_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PUSH) content_size %d is larger than size_is(%d)",
386 ndr2->offset, size_is);
390 switch (header_size) {
395 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, ndr2->offset));
399 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, ndr2->offset));
403 return ndr_push_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext header size %d",
410 store a token in the ndr context, for later retrieval
412 NTSTATUS ndr_token_store(TALLOC_CTX *mem_ctx,
413 struct ndr_token_list **list,
417 struct ndr_token_list *tok;
418 tok = talloc(mem_ctx, struct ndr_token_list);
420 return NT_STATUS_NO_MEMORY;
424 DLIST_ADD((*list), tok);
429 retrieve a token from a ndr context, using cmp_fn to match the tokens
431 NTSTATUS ndr_token_retrieve_cmp_fn(struct ndr_token_list **list, const void *key, uint32_t *v,
432 comparison_fn_t _cmp_fn, BOOL _remove_tok)
434 struct ndr_token_list *tok;
435 for (tok=*list;tok;tok=tok->next) {
436 if (_cmp_fn && _cmp_fn(tok->key,key)==0) goto found;
437 else if (!_cmp_fn && tok->key == key) goto found;
439 return ndr_map_error(NDR_ERR_TOKEN);
443 DLIST_REMOVE((*list), tok);
450 retrieve a token from a ndr context
452 NTSTATUS ndr_token_retrieve(struct ndr_token_list **list, const void *key, uint32_t *v)
454 return ndr_token_retrieve_cmp_fn(list, key, v, NULL, True);
458 peek at but don't removed a token from a ndr context
460 uint32_t ndr_token_peek(struct ndr_token_list **list, const void *key)
464 status = ndr_token_retrieve_cmp_fn(list, key, &v, NULL, False);
465 if (NT_STATUS_IS_OK(status)) return v;
470 pull an array size field and add it to the array_size_list token list
472 NTSTATUS ndr_pull_array_size(struct ndr_pull *ndr, const void *p)
475 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &size));
476 return ndr_token_store(ndr, &ndr->array_size_list, p, size);
480 get the stored array size field
482 uint32_t ndr_get_array_size(struct ndr_pull *ndr, const void *p)
484 return ndr_token_peek(&ndr->array_size_list, p);
488 check the stored array size field
490 NTSTATUS ndr_check_array_size(struct ndr_pull *ndr, void *p, uint32_t size)
493 stored = ndr_token_peek(&ndr->array_size_list, p);
494 if (stored != size) {
495 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE,
496 "Bad array size - got %u expected %u\n",
503 pull an array length field and add it to the array_length_list token list
505 NTSTATUS ndr_pull_array_length(struct ndr_pull *ndr, const void *p)
507 uint32_t length, offset;
508 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &offset));
510 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE,
511 "non-zero array offset %u\n", offset);
513 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &length));
514 return ndr_token_store(ndr, &ndr->array_length_list, p, length);
518 get the stored array length field
520 uint32_t ndr_get_array_length(struct ndr_pull *ndr, const void *p)
522 return ndr_token_peek(&ndr->array_length_list, p);
526 check the stored array length field
528 NTSTATUS ndr_check_array_length(struct ndr_pull *ndr, void *p, uint32_t length)
531 stored = ndr_token_peek(&ndr->array_length_list, p);
532 if (stored != length) {
533 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE,
534 "Bad array length - got %u expected %u\n",
543 NTSTATUS ndr_push_set_switch_value(struct ndr_push *ndr, const void *p, uint32_t val)
545 return ndr_token_store(ndr, &ndr->switch_list, p, val);
548 NTSTATUS ndr_pull_set_switch_value(struct ndr_pull *ndr, const void *p, uint32_t val)
550 return ndr_token_store(ndr, &ndr->switch_list, p, val);
553 NTSTATUS ndr_print_set_switch_value(struct ndr_print *ndr, const void *p, uint32_t val)
555 return ndr_token_store(ndr, &ndr->switch_list, p, val);
559 retrieve a switch value
561 uint32_t ndr_push_get_switch_value(struct ndr_push *ndr, const void *p)
563 return ndr_token_peek(&ndr->switch_list, p);
566 uint32_t ndr_pull_get_switch_value(struct ndr_pull *ndr, const void *p)
568 return ndr_token_peek(&ndr->switch_list, p);
571 uint32_t ndr_print_get_switch_value(struct ndr_print *ndr, const void *p)
573 return ndr_token_peek(&ndr->switch_list, p);
577 pull a struct from a blob using NDR
579 NTSTATUS ndr_pull_struct_blob(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p,
580 ndr_pull_flags_fn_t fn)
582 struct ndr_pull *ndr;
583 ndr = ndr_pull_init_blob(blob, mem_ctx);
585 return NT_STATUS_NO_MEMORY;
587 return fn(ndr, NDR_SCALARS|NDR_BUFFERS, p);
591 pull a struct from a blob using NDR - failing if all bytes are not consumed
593 NTSTATUS ndr_pull_struct_blob_all(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p,
594 ndr_pull_flags_fn_t fn)
596 struct ndr_pull *ndr;
599 ndr = ndr_pull_init_blob(blob, mem_ctx);
601 return NT_STATUS_NO_MEMORY;
603 status = fn(ndr, NDR_SCALARS|NDR_BUFFERS, p);
604 if (!NT_STATUS_IS_OK(status)) return status;
605 if (ndr->offset != ndr->data_size) {
606 return NT_STATUS_BUFFER_TOO_SMALL;
612 pull a union from a blob using NDR, given the union discriminator
614 NTSTATUS ndr_pull_union_blob(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p,
615 uint32_t level, ndr_pull_flags_fn_t fn)
617 struct ndr_pull *ndr;
620 ndr = ndr_pull_init_blob(blob, mem_ctx);
622 return NT_STATUS_NO_MEMORY;
624 ndr_pull_set_switch_value(ndr, p, level);
625 status = fn(ndr, NDR_SCALARS|NDR_BUFFERS, p);
626 if (!NT_STATUS_IS_OK(status)) return status;
627 if (ndr->offset != ndr->data_size) {
628 return NT_STATUS_BUFFER_TOO_SMALL;
634 push a struct to a blob using NDR
636 NTSTATUS ndr_push_struct_blob(DATA_BLOB *blob, TALLOC_CTX *mem_ctx, const void *p,
637 ndr_push_flags_fn_t fn)
640 struct ndr_push *ndr;
641 ndr = ndr_push_init_ctx(mem_ctx);
643 return NT_STATUS_NO_MEMORY;
645 status = fn(ndr, NDR_SCALARS|NDR_BUFFERS, p);
646 if (!NT_STATUS_IS_OK(status)) {
650 *blob = ndr_push_blob(ndr);
656 push a union to a blob using NDR
658 NTSTATUS ndr_push_union_blob(DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p,
659 uint32_t level, ndr_push_flags_fn_t fn)
662 struct ndr_push *ndr;
663 ndr = ndr_push_init_ctx(mem_ctx);
665 return NT_STATUS_NO_MEMORY;
667 ndr_push_set_switch_value(ndr, p, level);
668 status = fn(ndr, NDR_SCALARS|NDR_BUFFERS, p);
669 if (!NT_STATUS_IS_OK(status)) {
673 *blob = ndr_push_blob(ndr);
679 generic ndr_size_*() handler for structures
681 size_t ndr_size_struct(const void *p, int flags, ndr_push_flags_fn_t push)
683 struct ndr_push *ndr;
687 /* avoid recursion */
688 if (flags & LIBNDR_FLAG_NO_NDR_SIZE) return 0;
690 ndr = ndr_push_init_ctx(NULL);
692 ndr->flags |= flags | LIBNDR_FLAG_NO_NDR_SIZE;
693 status = push(ndr, NDR_SCALARS|NDR_BUFFERS, discard_const(p));
694 if (!NT_STATUS_IS_OK(status)) {
703 generic ndr_size_*() handler for unions
705 size_t ndr_size_union(const void *p, int flags, uint32_t level, ndr_push_flags_fn_t push)
707 struct ndr_push *ndr;
711 /* avoid recursion */
712 if (flags & LIBNDR_FLAG_NO_NDR_SIZE) return 0;
714 ndr = ndr_push_init_ctx(NULL);
716 ndr->flags |= flags | LIBNDR_FLAG_NO_NDR_SIZE;
717 ndr_push_set_switch_value(ndr, p, level);
718 status = push(ndr, NDR_SCALARS|NDR_BUFFERS, p);
719 if (!NT_STATUS_IS_OK(status)) {
728 get the current base for relative pointers for the push
730 uint32_t ndr_push_get_relative_base_offset(struct ndr_push *ndr)
732 return ndr->relative_base_offset;
736 restore the old base for relative pointers for the push
738 void ndr_push_restore_relative_base_offset(struct ndr_push *ndr, uint32_t offset)
740 ndr->relative_base_offset = offset;
744 setup the current base for relative pointers for the push
745 called in the NDR_SCALAR stage
747 NTSTATUS ndr_push_setup_relative_base_offset1(struct ndr_push *ndr, const void *p, uint32_t offset)
749 ndr->relative_base_offset = offset;
750 return ndr_token_store(ndr, &ndr->relative_base_list, p, offset);
754 setup the current base for relative pointers for the push
755 called in the NDR_BUFFERS stage
757 NTSTATUS ndr_push_setup_relative_base_offset2(struct ndr_push *ndr, const void *p)
759 return ndr_token_retrieve(&ndr->relative_base_list, p, &ndr->relative_base_offset);
763 push a relative object - stage1
764 this is called during SCALARS processing
766 NTSTATUS ndr_push_relative_ptr1(struct ndr_push *ndr, const void *p)
769 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0));
772 NDR_CHECK(ndr_push_align(ndr, 4));
773 NDR_CHECK(ndr_token_store(ndr, &ndr->relative_list, p, ndr->offset));
774 return ndr_push_uint32(ndr, NDR_SCALARS, 0xFFFFFFFF);
778 push a relative object - stage2
779 this is called during buffers processing
781 NTSTATUS ndr_push_relative_ptr2(struct ndr_push *ndr, const void *p)
783 struct ndr_push_save save;
784 uint32_t ptr_offset = 0xFFFFFFFF;
788 ndr_push_save(ndr, &save);
789 NDR_CHECK(ndr_token_retrieve(&ndr->relative_list, p, &ptr_offset));
790 if (ptr_offset > ndr->offset) {
791 return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
792 "ndr_push_relative_ptr2 ptr_offset(%u) > ndr->offset(%u)",
793 ptr_offset, ndr->offset);
795 ndr->offset = ptr_offset;
796 if (save.offset < ndr->relative_base_offset) {
797 return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
798 "ndr_push_relative_ptr2 save.offset(%u) < ndr->relative_base_offset(%u)",
799 save.offset, ndr->relative_base_offset);
801 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, save.offset - ndr->relative_base_offset));
802 ndr_push_restore(ndr, &save);
807 get the current base for relative pointers for the pull
809 uint32_t ndr_pull_get_relative_base_offset(struct ndr_pull *ndr)
811 return ndr->relative_base_offset;
815 restore the old base for relative pointers for the pull
817 void ndr_pull_restore_relative_base_offset(struct ndr_pull *ndr, uint32_t offset)
819 ndr->relative_base_offset = offset;
823 setup the current base for relative pointers for the pull
824 called in the NDR_SCALAR stage
826 NTSTATUS ndr_pull_setup_relative_base_offset1(struct ndr_pull *ndr, const void *p, uint32_t offset)
828 ndr->relative_base_offset = offset;
829 return ndr_token_store(ndr, &ndr->relative_base_list, p, offset);
833 setup the current base for relative pointers for the pull
834 called in the NDR_BUFFERS stage
836 NTSTATUS ndr_pull_setup_relative_base_offset2(struct ndr_pull *ndr, const void *p)
838 return ndr_token_retrieve(&ndr->relative_base_list, p, &ndr->relative_base_offset);
842 pull a relative object - stage1
843 called during SCALARS processing
845 NTSTATUS ndr_pull_relative_ptr1(struct ndr_pull *ndr, const void *p, uint32_t rel_offset)
847 rel_offset += ndr->relative_base_offset;
848 if (rel_offset > ndr->data_size) {
849 return ndr_pull_error(ndr, NDR_ERR_BUFSIZE,
850 "ndr_pull_relative_ptr1 rel_offset(%u) > ndr->data_size(%u)",
851 rel_offset, ndr->data_size);
853 return ndr_token_store(ndr, &ndr->relative_list, p, rel_offset);
857 pull a relative object - stage2
858 called during BUFFERS processing
860 NTSTATUS ndr_pull_relative_ptr2(struct ndr_pull *ndr, const void *p)
863 NDR_CHECK(ndr_token_retrieve(&ndr->relative_list, p, &rel_offset));
864 return ndr_pull_set_offset(ndr, rel_offset);