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 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",
186 set the push offset to 'ofs'
188 NTSTATUS ndr_push_set_offset(struct ndr_push *ndr, uint32_t ofs)
190 NDR_CHECK(ndr_push_expand(ndr, ofs));
198 NTSTATUS ndr_push_array(struct ndr_push *ndr, int ndr_flags, void *base,
199 size_t elsize, uint32_t count,
200 NTSTATUS (*push_fn)(struct ndr_push *, int, void *))
204 if (!(ndr_flags & NDR_SCALARS)) goto buffers;
205 for (i=0;i<count;i++) {
206 NDR_CHECK(push_fn(ndr, NDR_SCALARS, p));
209 if (!(ndr_flags & NDR_BUFFERS)) goto done;
212 for (i=0;i<count;i++) {
213 NDR_CHECK(push_fn(ndr, NDR_BUFFERS, p));
221 pull a constant sized array
223 NTSTATUS ndr_pull_array(struct ndr_pull *ndr, int ndr_flags, void *base,
224 size_t elsize, uint32_t count,
225 NTSTATUS (*pull_fn)(struct ndr_pull *, int, void *))
230 if (!(ndr_flags & NDR_SCALARS)) goto buffers;
231 for (i=0;i<count;i++) {
232 NDR_CHECK(pull_fn(ndr, NDR_SCALARS, p));
235 if (!(ndr_flags & NDR_BUFFERS)) goto done;
238 for (i=0;i<count;i++) {
239 NDR_CHECK(pull_fn(ndr, NDR_BUFFERS, p));
247 pull a constant size array of structures
249 NTSTATUS ndr_pull_struct_array(struct ndr_pull *ndr, uint32_t count,
250 size_t elsize, void **info,
251 NTSTATUS (*pull_fn)(struct ndr_pull *, int, void *))
256 NDR_ALLOC_N_SIZE(ndr, *info, count, elsize);
257 base = (char *)*info;
259 for (i = 0; i < count; i++) {
260 ndr->data += ndr->offset;
262 NDR_CHECK(pull_fn(ndr, NDR_SCALARS|NDR_BUFFERS, &base[count * elsize]));
269 print a generic array
271 void ndr_print_array(struct ndr_print *ndr, const char *name, void *base,
272 size_t elsize, uint32_t count,
273 void (*print_fn)(struct ndr_print *, const char *, void *))
277 ndr->print(ndr, "%s: ARRAY(%d)", name, count);
279 for (i=0;i<count;i++) {
281 asprintf(&idx, "[%d]", i);
283 print_fn(ndr, idx, p);
293 void ndr_print_debug_helper(struct ndr_print *ndr, const char *format, ...) _PRINTF_ATTRIBUTE(2,3)
299 va_start(ap, format);
300 vasprintf(&s, format, ap);
303 for (i=0;i<ndr->depth;i++) {
307 DEBUG(0,("%s\n", s));
312 a useful helper function for printing idl structures via DEBUG()
314 void ndr_print_debug(ndr_print_fn_t fn, const char *name, void *ptr)
316 struct ndr_print *ndr;
318 ndr = talloc_zero(NULL, struct ndr_print);
320 ndr->print = ndr_print_debug_helper;
328 a useful helper function for printing idl function calls via DEBUG()
330 void ndr_print_function_debug(ndr_print_function_t fn, const char *name, int flags, void *ptr)
332 struct ndr_print *ndr;
334 ndr = talloc_zero(NULL, struct ndr_print);
336 ndr->print = ndr_print_debug_helper;
339 fn(ndr, name, flags, ptr);
343 void ndr_set_flags(uint32_t *pflags, uint32_t new_flags)
345 /* the big/little endian flags are inter-dependent */
346 if (new_flags & LIBNDR_FLAG_LITTLE_ENDIAN) {
347 (*pflags) &= ~LIBNDR_FLAG_BIGENDIAN;
349 if (new_flags & LIBNDR_FLAG_BIGENDIAN) {
350 (*pflags) &= ~LIBNDR_FLAG_LITTLE_ENDIAN;
352 (*pflags) |= new_flags;
355 static NTSTATUS ndr_map_error(enum ndr_err_code err)
358 case NDR_ERR_BUFSIZE:
359 return NT_STATUS_BUFFER_TOO_SMALL;
361 return NT_STATUS_INTERNAL_ERROR;
363 return NT_STATUS_NO_MEMORY;
364 case NDR_ERR_ARRAY_SIZE:
365 return NT_STATUS_ARRAY_BOUNDS_EXCEEDED;
370 /* we should all error codes to different status codes */
371 return NT_STATUS_INVALID_PARAMETER;
375 return and possibly log an NDR error
377 NTSTATUS ndr_pull_error(struct ndr_pull *ndr,
378 enum ndr_err_code err, const char *format, ...) _PRINTF_ATTRIBUTE(3,4)
383 va_start(ap, format);
384 vasprintf(&s, format, ap);
387 DEBUG(3,("ndr_pull_error(%u): %s\n", err, s));
391 return ndr_map_error(err);
395 return and possibly log an NDR error
397 NTSTATUS ndr_push_error(struct ndr_push *ndr, enum ndr_err_code err, const char *format, ...) _PRINTF_ATTRIBUTE(3,4)
402 va_start(ap, format);
403 vasprintf(&s, format, ap);
406 DEBUG(3,("ndr_push_error(%u): %s\n", err, s));
410 return ndr_map_error(err);
415 handle subcontext buffers, which in midl land are user-marshalled, but
416 we use magic in pidl to make them easier to cope with
418 NTSTATUS ndr_pull_subcontext_header(struct ndr_pull *ndr,
421 struct ndr_pull *ndr2)
423 ndr2->flags = ndr->flags;
425 switch (header_size) {
427 uint32_t content_size = ndr->data_size - ndr->offset;
429 content_size = size_is;
431 NDR_CHECK(ndr_pull_subcontext(ndr, ndr2, content_size));
436 uint16_t content_size;
437 NDR_CHECK(ndr_pull_uint16(ndr, NDR_SCALARS, &content_size));
438 if (size_is >= 0 && size_is != content_size) {
439 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
440 size_is, content_size);
442 NDR_CHECK(ndr_pull_subcontext(ndr, ndr2, content_size));
447 uint32_t content_size;
448 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &content_size));
449 if (size_is >= 0 && size_is != content_size) {
450 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
451 size_is, content_size);
453 NDR_CHECK(ndr_pull_subcontext(ndr, ndr2, content_size));
457 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) header_size %d",
464 push a subcontext header
466 NTSTATUS ndr_push_subcontext_header(struct ndr_push *ndr,
469 struct ndr_push *ndr2)
472 ssize_t padding_len = size_is - ndr2->offset;
473 if (padding_len > 0) {
474 NDR_CHECK(ndr_push_zero(ndr2, padding_len));
475 } else if (padding_len < 0) {
476 return ndr_push_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PUSH) content_size %d is larger than size_is(%d)",
477 ndr2->offset, size_is);
481 switch (header_size) {
486 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, ndr2->offset));
490 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, ndr2->offset));
494 return ndr_push_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext header size %d",
501 mark the start of a structure
503 NTSTATUS ndr_pull_struct_start(struct ndr_pull *ndr)
509 mark the end of a structure
511 void ndr_pull_struct_end(struct ndr_pull *ndr)
516 mark the start of a structure
518 NTSTATUS ndr_push_struct_start(struct ndr_push *ndr)
524 mark the end of a structure
526 void ndr_push_struct_end(struct ndr_push *ndr)
531 store a token in the ndr context, for later retrieval
533 static NTSTATUS ndr_token_store(TALLOC_CTX *mem_ctx,
534 struct ndr_token_list **list,
538 struct ndr_token_list *tok;
539 tok = talloc(mem_ctx, struct ndr_token_list);
541 return NT_STATUS_NO_MEMORY;
545 DLIST_ADD((*list), tok);
550 retrieve a token from a ndr context
552 static NTSTATUS ndr_token_retrieve(struct ndr_token_list **list, const void *key, uint32_t *v)
554 struct ndr_token_list *tok;
555 for (tok=*list;tok;tok=tok->next) {
556 if (tok->key == key) {
557 DLIST_REMOVE((*list), tok);
562 return ndr_map_error(NDR_ERR_TOKEN);
566 peek at but don't removed a token from a ndr context
568 static uint32_t ndr_token_peek(struct ndr_token_list **list, const void *key)
570 struct ndr_token_list *tok;
571 for (tok=*list;tok;tok=tok->next) {
572 if (tok->key == key) {
580 pull an array size field and add it to the array_size_list token list
582 NTSTATUS ndr_pull_array_size(struct ndr_pull *ndr, const void *p)
585 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &size));
586 return ndr_token_store(ndr, &ndr->array_size_list, p, size);
590 get the stored array size field
592 uint32_t ndr_get_array_size(struct ndr_pull *ndr, const void *p)
594 return ndr_token_peek(&ndr->array_size_list, p);
598 check the stored array size field
600 NTSTATUS ndr_check_array_size(struct ndr_pull *ndr, void *p, uint32_t size)
603 NDR_CHECK(ndr_token_retrieve(&ndr->array_size_list, p, &stored));
604 if (stored != size) {
605 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE,
606 "Bad array size - got %u expected %u\n",
613 pull an array length field and add it to the array_length_list token list
615 NTSTATUS ndr_pull_array_length(struct ndr_pull *ndr, const void *p)
617 uint32_t length, offset;
618 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &offset));
620 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE,
621 "non-zero array offset %u\n", offset);
623 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &length));
624 return ndr_token_store(ndr, &ndr->array_length_list, p, length);
628 get the stored array length field
630 uint32_t ndr_get_array_length(struct ndr_pull *ndr, const void *p)
632 return ndr_token_peek(&ndr->array_length_list, p);
636 check the stored array length field
638 NTSTATUS ndr_check_array_length(struct ndr_pull *ndr, void *p, uint32_t length)
641 NDR_CHECK(ndr_token_retrieve(&ndr->array_length_list, p, &stored));
642 if (stored != length) {
643 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE,
644 "Bad array length - got %u expected %u\n",
653 NTSTATUS ndr_push_set_switch_value(struct ndr_push *ndr, const void *p, uint32_t val)
655 return ndr_token_store(ndr, &ndr->switch_list, p, val);
658 NTSTATUS ndr_pull_set_switch_value(struct ndr_pull *ndr, const void *p, uint32_t val)
660 return ndr_token_store(ndr, &ndr->switch_list, p, val);
663 NTSTATUS ndr_print_set_switch_value(struct ndr_print *ndr, const void *p, uint32_t val)
665 return ndr_token_store(ndr, &ndr->switch_list, p, val);
669 retrieve a switch value
671 uint32_t ndr_push_get_switch_value(struct ndr_push *ndr, const void *p)
673 return ndr_token_peek(&ndr->switch_list, p);
676 uint32_t ndr_pull_get_switch_value(struct ndr_pull *ndr, const void *p)
678 return ndr_token_peek(&ndr->switch_list, p);
681 uint32_t ndr_print_get_switch_value(struct ndr_print *ndr, const void *p)
683 return ndr_token_peek(&ndr->switch_list, p);
687 pull a relative object - stage1
688 called during SCALARS processing
690 NTSTATUS ndr_pull_relative_ptr1(struct ndr_pull *ndr, const void *p, uint32_t rel_offset)
692 if (ndr->flags & LIBNDR_FLAG_RELATIVE_CURRENT) {
693 return ndr_token_store(ndr, &ndr->relative_list, p,
694 rel_offset + ndr->offset - 4);
696 return ndr_token_store(ndr, &ndr->relative_list, p, rel_offset);
701 pull a relative object - stage2
702 called during BUFFERS processing
704 NTSTATUS ndr_pull_relative_ptr2(struct ndr_pull *ndr, const void *p)
707 NDR_CHECK(ndr_token_retrieve(&ndr->relative_list, p, &rel_offset));
708 return ndr_pull_set_offset(ndr, rel_offset);
712 push a relative object - stage1
713 this is called during SCALARS processing
715 NTSTATUS ndr_push_relative_ptr1(struct ndr_push *ndr, const void *p)
718 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0));
721 NDR_CHECK(ndr_push_align(ndr, 4));
722 NDR_CHECK(ndr_token_store(ndr, &ndr->relative_list, p, ndr->offset));
723 return ndr_push_uint32(ndr, NDR_SCALARS, 0xFFFFFFFF);
727 push a relative object - stage2
728 this is called during buffers processing
730 NTSTATUS ndr_push_relative_ptr2(struct ndr_push *ndr, const void *p)
732 struct ndr_push_save save;
736 NDR_CHECK(ndr_push_align(ndr, 4));
737 ndr_push_save(ndr, &save);
738 NDR_CHECK(ndr_token_retrieve(&ndr->relative_list, p, &ndr->offset));
739 if (ndr->flags & LIBNDR_FLAG_RELATIVE_CURRENT) {
740 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, save.offset - ndr->offset));
742 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, save.offset));
744 ndr_push_restore(ndr, &save);
749 pull a struct from a blob using NDR
751 NTSTATUS ndr_pull_struct_blob(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p,
752 ndr_pull_flags_fn_t fn)
754 struct ndr_pull *ndr;
755 ndr = ndr_pull_init_blob(blob, mem_ctx);
757 return NT_STATUS_NO_MEMORY;
759 return fn(ndr, NDR_SCALARS|NDR_BUFFERS, p);
763 pull a struct from a blob using NDR - failing if all bytes are not consumed
765 NTSTATUS ndr_pull_struct_blob_all(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p,
766 ndr_pull_flags_fn_t fn)
768 struct ndr_pull *ndr;
771 ndr = ndr_pull_init_blob(blob, mem_ctx);
773 return NT_STATUS_NO_MEMORY;
775 status = fn(ndr, NDR_SCALARS|NDR_BUFFERS, p);
776 if (!NT_STATUS_IS_OK(status)) return status;
777 if (ndr->offset != ndr->data_size) {
778 return NT_STATUS_BUFFER_TOO_SMALL;
784 push a struct to a blob using NDR
786 NTSTATUS ndr_push_struct_blob(DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p,
787 ndr_push_flags_fn_t fn)
790 struct ndr_push *ndr;
791 ndr = ndr_push_init_ctx(mem_ctx);
793 return NT_STATUS_NO_MEMORY;
795 status = fn(ndr, NDR_SCALARS|NDR_BUFFERS, p);
796 if (!NT_STATUS_IS_OK(status)) {
800 *blob = ndr_push_blob(ndr);
806 generic ndr_size_*() handler for structures
808 size_t ndr_size_struct(const void *p, int flags, ndr_push_flags_fn_t push)
810 struct ndr_push *ndr;
814 /* avoid recursion */
815 if (flags & LIBNDR_FLAG_NO_NDR_SIZE) return 0;
817 ndr = ndr_push_init_ctx(NULL);
819 ndr->flags |= flags | LIBNDR_FLAG_NO_NDR_SIZE;
820 status = push(ndr, NDR_SCALARS|NDR_BUFFERS, discard_const(p));
821 if (!NT_STATUS_IS_OK(status)) {
830 generic ndr_size_*() handler for unions
832 size_t ndr_size_union(const void *p, int flags, uint32_t level, ndr_push_flags_fn_t push)
834 struct ndr_push *ndr;
838 /* avoid recursion */
839 if (flags & LIBNDR_FLAG_NO_NDR_SIZE) return 0;
841 ndr = ndr_push_init_ctx(NULL);
843 ndr->flags |= flags | LIBNDR_FLAG_NO_NDR_SIZE;
844 ndr_push_set_switch_value(ndr, p, level);
845 status = push(ndr, NDR_SCALARS|NDR_BUFFERS, discard_const(p));
846 if (!NT_STATUS_IS_OK(status)) {