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 mark the start of a structure
412 NTSTATUS ndr_pull_struct_start(struct ndr_pull *ndr)
418 mark the end of a structure
420 void ndr_pull_struct_end(struct ndr_pull *ndr)
425 mark the start of a structure
427 NTSTATUS ndr_push_struct_start(struct ndr_push *ndr)
433 mark the end of a structure
435 void ndr_push_struct_end(struct ndr_push *ndr)
440 store a token in the ndr context, for later retrieval
442 static NTSTATUS ndr_token_store(TALLOC_CTX *mem_ctx,
443 struct ndr_token_list **list,
447 struct ndr_token_list *tok;
448 tok = talloc(mem_ctx, struct ndr_token_list);
450 return NT_STATUS_NO_MEMORY;
454 DLIST_ADD((*list), tok);
459 retrieve a token from a ndr context
461 static NTSTATUS ndr_token_retrieve(struct ndr_token_list **list, const void *key, uint32_t *v)
463 struct ndr_token_list *tok;
464 for (tok=*list;tok;tok=tok->next) {
465 if (tok->key == key) {
466 DLIST_REMOVE((*list), tok);
471 return ndr_map_error(NDR_ERR_TOKEN);
475 peek at but don't removed a token from a ndr context
477 static uint32_t ndr_token_peek(struct ndr_token_list **list, const void *key)
479 struct ndr_token_list *tok;
480 for (tok=*list;tok;tok=tok->next) {
481 if (tok->key == key) {
489 pull an array size field and add it to the array_size_list token list
491 NTSTATUS ndr_pull_array_size(struct ndr_pull *ndr, const void *p)
494 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &size));
495 return ndr_token_store(ndr, &ndr->array_size_list, p, size);
499 get the stored array size field
501 uint32_t ndr_get_array_size(struct ndr_pull *ndr, const void *p)
503 return ndr_token_peek(&ndr->array_size_list, p);
507 check the stored array size field
509 NTSTATUS ndr_check_array_size(struct ndr_pull *ndr, void *p, uint32_t size)
512 stored = ndr_token_peek(&ndr->array_size_list, p);
513 if (stored != size) {
514 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE,
515 "Bad array size - got %u expected %u\n",
522 pull an array length field and add it to the array_length_list token list
524 NTSTATUS ndr_pull_array_length(struct ndr_pull *ndr, const void *p)
526 uint32_t length, offset;
527 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &offset));
529 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE,
530 "non-zero array offset %u\n", offset);
532 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &length));
533 return ndr_token_store(ndr, &ndr->array_length_list, p, length);
537 get the stored array length field
539 uint32_t ndr_get_array_length(struct ndr_pull *ndr, const void *p)
541 return ndr_token_peek(&ndr->array_length_list, p);
545 check the stored array length field
547 NTSTATUS ndr_check_array_length(struct ndr_pull *ndr, void *p, uint32_t length)
550 stored = ndr_token_peek(&ndr->array_length_list, p);
551 if (stored != length) {
552 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE,
553 "Bad array length - got %u expected %u\n",
562 NTSTATUS ndr_push_set_switch_value(struct ndr_push *ndr, const void *p, uint32_t val)
564 return ndr_token_store(ndr, &ndr->switch_list, p, val);
567 NTSTATUS ndr_pull_set_switch_value(struct ndr_pull *ndr, const void *p, uint32_t val)
569 return ndr_token_store(ndr, &ndr->switch_list, p, val);
572 NTSTATUS ndr_print_set_switch_value(struct ndr_print *ndr, const void *p, uint32_t val)
574 return ndr_token_store(ndr, &ndr->switch_list, p, val);
578 retrieve a switch value
580 uint32_t ndr_push_get_switch_value(struct ndr_push *ndr, const void *p)
582 return ndr_token_peek(&ndr->switch_list, p);
585 uint32_t ndr_pull_get_switch_value(struct ndr_pull *ndr, const void *p)
587 return ndr_token_peek(&ndr->switch_list, p);
590 uint32_t ndr_print_get_switch_value(struct ndr_print *ndr, const void *p)
592 return ndr_token_peek(&ndr->switch_list, p);
596 pull a relative object - stage1
597 called during SCALARS processing
599 NTSTATUS ndr_pull_relative_ptr1(struct ndr_pull *ndr, const void *p, uint32_t rel_offset)
601 if (ndr->flags & LIBNDR_FLAG_RELATIVE_CURRENT) {
602 return ndr_token_store(ndr, &ndr->relative_list, p,
603 rel_offset + ndr->offset - 4);
605 return ndr_token_store(ndr, &ndr->relative_list, p, rel_offset);
610 pull a relative object - stage2
611 called during BUFFERS processing
613 NTSTATUS ndr_pull_relative_ptr2(struct ndr_pull *ndr, const void *p)
616 NDR_CHECK(ndr_token_retrieve(&ndr->relative_list, p, &rel_offset));
617 return ndr_pull_set_offset(ndr, rel_offset);
621 push a relative object - stage1
622 this is called during SCALARS processing
624 NTSTATUS ndr_push_relative_ptr1(struct ndr_push *ndr, const void *p)
627 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0));
630 NDR_CHECK(ndr_push_align(ndr, 4));
631 NDR_CHECK(ndr_token_store(ndr, &ndr->relative_list, p, ndr->offset));
632 return ndr_push_uint32(ndr, NDR_SCALARS, 0xFFFFFFFF);
636 push a relative object - stage2
637 this is called during buffers processing
639 NTSTATUS ndr_push_relative_ptr2(struct ndr_push *ndr, const void *p)
641 struct ndr_push_save save;
645 NDR_CHECK(ndr_push_align(ndr, 4));
646 ndr_push_save(ndr, &save);
647 NDR_CHECK(ndr_token_retrieve(&ndr->relative_list, p, &ndr->offset));
648 if (ndr->flags & LIBNDR_FLAG_RELATIVE_CURRENT) {
649 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, save.offset - ndr->offset));
651 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, save.offset));
653 ndr_push_restore(ndr, &save);
658 pull a struct from a blob using NDR
660 NTSTATUS ndr_pull_struct_blob(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p,
661 ndr_pull_flags_fn_t fn)
663 struct ndr_pull *ndr;
664 ndr = ndr_pull_init_blob(blob, mem_ctx);
666 return NT_STATUS_NO_MEMORY;
668 return fn(ndr, NDR_SCALARS|NDR_BUFFERS, p);
672 pull a struct from a blob using NDR - failing if all bytes are not consumed
674 NTSTATUS ndr_pull_struct_blob_all(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p,
675 ndr_pull_flags_fn_t fn)
677 struct ndr_pull *ndr;
680 ndr = ndr_pull_init_blob(blob, mem_ctx);
682 return NT_STATUS_NO_MEMORY;
684 status = fn(ndr, NDR_SCALARS|NDR_BUFFERS, p);
685 if (!NT_STATUS_IS_OK(status)) return status;
686 if (ndr->offset != ndr->data_size) {
687 return NT_STATUS_BUFFER_TOO_SMALL;
693 pull a union from a blob using NDR, given the union discriminator
695 NTSTATUS ndr_pull_union_blob(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p,
696 uint32_t level, ndr_pull_flags_fn_t fn)
698 struct ndr_pull *ndr;
701 ndr = ndr_pull_init_blob(blob, mem_ctx);
703 return NT_STATUS_NO_MEMORY;
705 ndr_pull_set_switch_value(ndr, p, level);
706 status = fn(ndr, NDR_SCALARS|NDR_BUFFERS, p);
707 if (!NT_STATUS_IS_OK(status)) return status;
708 if (ndr->offset != ndr->data_size) {
709 return NT_STATUS_BUFFER_TOO_SMALL;
715 push a struct to a blob using NDR
717 NTSTATUS ndr_push_struct_blob(DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p,
718 ndr_push_flags_fn_t fn)
721 struct ndr_push *ndr;
722 ndr = ndr_push_init_ctx(mem_ctx);
724 return NT_STATUS_NO_MEMORY;
726 status = fn(ndr, NDR_SCALARS|NDR_BUFFERS, p);
727 if (!NT_STATUS_IS_OK(status)) {
731 *blob = ndr_push_blob(ndr);
737 push a union to a blob using NDR
739 NTSTATUS ndr_push_union_blob(DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p,
740 uint32_t level, ndr_push_flags_fn_t fn)
743 struct ndr_push *ndr;
744 ndr = ndr_push_init_ctx(mem_ctx);
746 return NT_STATUS_NO_MEMORY;
748 ndr_push_set_switch_value(ndr, p, level);
749 status = fn(ndr, NDR_SCALARS|NDR_BUFFERS, p);
750 if (!NT_STATUS_IS_OK(status)) {
754 *blob = ndr_push_blob(ndr);
760 generic ndr_size_*() handler for structures
762 size_t ndr_size_struct(const void *p, int flags, ndr_push_flags_fn_t push)
764 struct ndr_push *ndr;
768 /* avoid recursion */
769 if (flags & LIBNDR_FLAG_NO_NDR_SIZE) return 0;
771 ndr = ndr_push_init_ctx(NULL);
773 ndr->flags |= flags | LIBNDR_FLAG_NO_NDR_SIZE;
774 status = push(ndr, NDR_SCALARS|NDR_BUFFERS, discard_const(p));
775 if (!NT_STATUS_IS_OK(status)) {
784 generic ndr_size_*() handler for unions
786 size_t ndr_size_union(const void *p, int flags, uint32_t level, ndr_push_flags_fn_t push)
788 struct ndr_push *ndr;
792 /* avoid recursion */
793 if (flags & LIBNDR_FLAG_NO_NDR_SIZE) return 0;
795 ndr = ndr_push_init_ctx(NULL);
797 ndr->flags |= flags | LIBNDR_FLAG_NO_NDR_SIZE;
798 ndr_push_set_switch_value(ndr, p, level);
799 status = push(ndr, NDR_SCALARS|NDR_BUFFERS, discard_const(p));
800 if (!NT_STATUS_IS_OK(status)) {