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(NULL, struct ndr_print);
320 ndr->print = ndr_print_debug_helper;
329 a useful helper function for printing idl unions via DEBUG()
331 void ndr_print_union_debug(ndr_print_union_fn_t fn, const char *name, uint32_t level, void *ptr)
333 struct ndr_print *ndr;
335 ndr = talloc(NULL, struct ndr_print);
337 ndr->print = ndr_print_debug_helper;
340 fn(ndr, name, level, ptr);
345 a useful helper function for printing idl function calls via DEBUG()
347 void ndr_print_function_debug(ndr_print_function_t fn, const char *name, int flags, void *ptr)
349 struct ndr_print *ndr;
351 ndr = talloc(NULL, struct ndr_print);
353 ndr->print = ndr_print_debug_helper;
356 fn(ndr, name, flags, ptr);
360 void ndr_set_flags(uint32_t *pflags, uint32_t new_flags)
362 /* the big/little endian flags are inter-dependent */
363 if (new_flags & LIBNDR_FLAG_LITTLE_ENDIAN) {
364 (*pflags) &= ~LIBNDR_FLAG_BIGENDIAN;
366 if (new_flags & LIBNDR_FLAG_BIGENDIAN) {
367 (*pflags) &= ~LIBNDR_FLAG_LITTLE_ENDIAN;
369 (*pflags) |= new_flags;
372 static NTSTATUS ndr_map_error(enum ndr_err_code err)
375 case NDR_ERR_BUFSIZE:
376 return NT_STATUS_BUFFER_TOO_SMALL;
378 return NT_STATUS_INTERNAL_ERROR;
380 return NT_STATUS_NO_MEMORY;
381 case NDR_ERR_ARRAY_SIZE:
382 return NT_STATUS_ARRAY_BOUNDS_EXCEEDED;
387 /* we should all error codes to different status codes */
388 return NT_STATUS_INVALID_PARAMETER;
392 return and possibly log an NDR error
394 NTSTATUS ndr_pull_error(struct ndr_pull *ndr,
395 enum ndr_err_code err, const char *format, ...) _PRINTF_ATTRIBUTE(3,4)
400 va_start(ap, format);
401 vasprintf(&s, format, ap);
404 DEBUG(3,("ndr_pull_error(%u): %s\n", err, s));
408 return ndr_map_error(err);
412 return and possibly log an NDR error
414 NTSTATUS ndr_push_error(struct ndr_push *ndr, enum ndr_err_code err, const char *format, ...) _PRINTF_ATTRIBUTE(3,4)
419 va_start(ap, format);
420 vasprintf(&s, format, ap);
423 DEBUG(3,("ndr_push_error(%u): %s\n", err, s));
427 return ndr_map_error(err);
432 handle subcontext buffers, which in midl land are user-marshalled, but
433 we use magic in pidl to make them easier to cope with
435 static NTSTATUS ndr_pull_subcontext_header(struct ndr_pull *ndr,
437 struct ndr_pull *ndr2)
439 ndr2->flags = ndr->flags;
443 uint32_t size = ndr->data_size - ndr->offset;
444 if (size == 0) return NT_STATUS_OK;
445 NDR_CHECK(ndr_pull_subcontext(ndr, ndr2, size));
451 NDR_CHECK(ndr_pull_uint16(ndr, &size));
452 if (size == 0) return NT_STATUS_OK;
453 NDR_CHECK(ndr_pull_subcontext(ndr, ndr2, size));
459 NDR_CHECK(ndr_pull_uint32(ndr, &size));
460 if (size == 0) return NT_STATUS_OK;
461 NDR_CHECK(ndr_pull_subcontext(ndr, ndr2, size));
465 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext size %d",
472 handle subcontext buffers, which in midl land are user-marshalled, but
473 we use magic in pidl to make them easier to cope with
475 NTSTATUS ndr_pull_subcontext_fn(struct ndr_pull *ndr, size_t sub_size,
476 void *base, ndr_pull_fn_t fn)
478 struct ndr_pull *ndr2;
479 NDR_ALLOC(ndr, ndr2);
480 NDR_CHECK(ndr_pull_subcontext_header(ndr, sub_size, ndr2));
481 NDR_CHECK(fn(ndr2, base));
483 NDR_CHECK(ndr_pull_advance(ndr, ndr2->data_size));
485 NDR_CHECK(ndr_pull_advance(ndr, ndr2->offset));
491 NTSTATUS ndr_pull_subcontext_flags_fn(struct ndr_pull *ndr, size_t sub_size,
492 void *base, ndr_pull_flags_fn_t fn)
494 struct ndr_pull *ndr2;
495 NDR_ALLOC(ndr, ndr2);
496 NDR_CHECK(ndr_pull_subcontext_header(ndr, sub_size, ndr2));
497 NDR_CHECK(fn(ndr2, NDR_SCALARS|NDR_BUFFERS, base));
499 NDR_CHECK(ndr_pull_advance(ndr, ndr2->data_size));
501 NDR_CHECK(ndr_pull_advance(ndr, ndr2->offset));
506 NTSTATUS ndr_pull_subcontext_union_fn(struct ndr_pull *ndr, size_t sub_size,
507 uint32_t level, void *base, ndr_pull_union_fn_t fn)
509 struct ndr_pull *ndr2;
511 NDR_ALLOC(ndr, ndr2);
512 NDR_CHECK(ndr_pull_subcontext_header(ndr, sub_size, ndr2));
513 NDR_CHECK(fn(ndr2, NDR_SCALARS|NDR_BUFFERS, level, base));
515 NDR_CHECK(ndr_pull_advance(ndr, ndr2->data_size));
517 NDR_CHECK(ndr_pull_advance(ndr, ndr2->offset));
524 push a subcontext header
526 static NTSTATUS ndr_push_subcontext_header(struct ndr_push *ndr,
528 struct ndr_push *ndr2)
535 NDR_CHECK(ndr_push_uint16(ndr, ndr2->offset));
539 NDR_CHECK(ndr_push_uint32(ndr, ndr2->offset));
543 return ndr_push_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext size %d",
550 handle subcontext buffers, which in midl land are user-marshalled, but
551 we use magic in pidl to make them easier to cope with
553 NTSTATUS ndr_push_subcontext_fn(struct ndr_push *ndr, size_t sub_size,
554 void *base, ndr_push_fn_t fn)
556 struct ndr_push *ndr2;
558 ndr2 = ndr_push_init_ctx(ndr);
559 if (!ndr2) return NT_STATUS_NO_MEMORY;
561 ndr2->flags = ndr->flags;
562 NDR_CHECK(fn(ndr2, base));
563 NDR_CHECK(ndr_push_subcontext_header(ndr, sub_size, ndr2));
564 NDR_CHECK(ndr_push_bytes(ndr, ndr2->data, ndr2->offset));
569 handle subcontext buffers for function that take a flags arg
571 NTSTATUS ndr_push_subcontext_flags_fn(struct ndr_push *ndr, size_t sub_size,
572 void *base, ndr_push_flags_fn_t fn)
574 struct ndr_push *ndr2;
576 ndr2 = ndr_push_init_ctx(ndr);
577 if (!ndr2) return NT_STATUS_NO_MEMORY;
579 ndr2->flags = ndr->flags;
580 NDR_CHECK(fn(ndr2, NDR_SCALARS|NDR_BUFFERS, base));
581 NDR_CHECK(ndr_push_subcontext_header(ndr, sub_size, ndr2));
582 NDR_CHECK(ndr_push_bytes(ndr, ndr2->data, ndr2->offset));
587 handle subcontext buffers for function that take a union
589 NTSTATUS ndr_push_subcontext_union_fn(struct ndr_push *ndr, size_t sub_size,
590 uint32_t level, void *base, ndr_push_union_fn_t fn)
592 struct ndr_push *ndr2;
594 ndr2 = ndr_push_init_ctx(ndr);
595 if (!ndr2) return NT_STATUS_NO_MEMORY;
597 ndr2->flags = ndr->flags;
598 NDR_CHECK(fn(ndr2, NDR_SCALARS|NDR_BUFFERS, level, base));
599 NDR_CHECK(ndr_push_subcontext_header(ndr, sub_size, ndr2));
600 NDR_CHECK(ndr_push_bytes(ndr, ndr2->data, ndr2->offset));
606 mark the start of a structure
608 NTSTATUS ndr_pull_struct_start(struct ndr_pull *ndr)
614 mark the end of a structure
616 void ndr_pull_struct_end(struct ndr_pull *ndr)
621 mark the start of a structure
623 NTSTATUS ndr_push_struct_start(struct ndr_push *ndr)
629 mark the end of a structure
631 void ndr_push_struct_end(struct ndr_push *ndr)
636 store a token in the ndr context, for later retrieval
638 static NTSTATUS ndr_token_store(TALLOC_CTX *mem_ctx,
639 struct ndr_token_list **list,
643 struct ndr_token_list *tok;
644 tok = talloc(mem_ctx, struct ndr_token_list);
646 return NT_STATUS_NO_MEMORY;
650 DLIST_ADD((*list), tok);
655 retrieve a token from a ndr context
657 static NTSTATUS ndr_token_retrieve(struct ndr_token_list **list, const void *key, uint32_t *v)
659 struct ndr_token_list *tok;
660 for (tok=*list;tok;tok=tok->next) {
661 if (tok->key == key) {
662 DLIST_REMOVE((*list), tok);
667 return ndr_map_error(NDR_ERR_TOKEN);
671 peek at but don't removed a token from a ndr context
673 static uint32_t ndr_token_peek(struct ndr_token_list **list, const void *key)
675 struct ndr_token_list *tok;
676 for (tok=*list;tok;tok=tok->next) {
677 if (tok->key == key) {
685 pull an array size field and add it to the array_size_list token list
687 NTSTATUS ndr_pull_array_size(struct ndr_pull *ndr, const void *p)
690 NDR_CHECK(ndr_pull_uint32(ndr, &size));
691 return ndr_token_store(ndr, &ndr->array_size_list, p, size);
695 get the stored array size field
697 uint32_t ndr_get_array_size(struct ndr_pull *ndr, const void *p)
699 return ndr_token_peek(&ndr->array_size_list, p);
703 check the stored array size field
705 NTSTATUS ndr_check_array_size(struct ndr_pull *ndr, void *p, uint32_t size)
708 NDR_CHECK(ndr_token_retrieve(&ndr->array_size_list, p, &stored));
709 if (stored != size) {
710 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE,
711 "Bad array size - got %u expected %u\n",
718 pull an array length field and add it to the array_length_list token list
720 NTSTATUS ndr_pull_array_length(struct ndr_pull *ndr, const void *p)
722 uint32_t length, offset;
723 NDR_CHECK(ndr_pull_uint32(ndr, &offset));
725 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE,
726 "non-zero array offset %u\n", offset);
728 NDR_CHECK(ndr_pull_uint32(ndr, &length));
729 return ndr_token_store(ndr, &ndr->array_length_list, p, length);
733 get the stored array length field
735 uint32_t ndr_get_array_length(struct ndr_pull *ndr, const void *p)
737 return ndr_token_peek(&ndr->array_length_list, p);
741 check the stored array length field
743 NTSTATUS ndr_check_array_length(struct ndr_pull *ndr, void *p, uint32_t length)
746 NDR_CHECK(ndr_token_retrieve(&ndr->array_length_list, p, &stored));
747 if (stored != length) {
748 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE,
749 "Bad array length - got %u expected %u\n",
756 pull a relative object - stage1
757 called during SCALARS processing
759 NTSTATUS ndr_pull_relative1(struct ndr_pull *ndr, const void *p, uint32_t rel_offset)
761 if (ndr->flags & LIBNDR_FLAG_RELATIVE_CURRENT) {
762 return ndr_token_store(ndr, &ndr->relative_list, p,
763 rel_offset + ndr->offset - 4);
765 return ndr_token_store(ndr, &ndr->relative_list, p, rel_offset);
770 pull a relative object - stage2
771 called during BUFFERS processing
773 NTSTATUS ndr_pull_relative2(struct ndr_pull *ndr, const void *p)
776 NDR_CHECK(ndr_token_retrieve(&ndr->relative_list, p, &rel_offset));
777 return ndr_pull_set_offset(ndr, rel_offset);
781 push a relative object - stage1
782 this is called during SCALARS processing
784 NTSTATUS ndr_push_relative1(struct ndr_push *ndr, const void *p)
787 NDR_CHECK(ndr_push_uint32(ndr, 0));
790 NDR_CHECK(ndr_push_align(ndr, 4));
791 NDR_CHECK(ndr_token_store(ndr, &ndr->relative_list, p, ndr->offset));
792 return ndr_push_uint32(ndr, 0xFFFFFFFF);
796 push a relative object - stage2
797 this is called during buffers processing
799 NTSTATUS ndr_push_relative2(struct ndr_push *ndr, const void *p)
801 struct ndr_push_save save;
805 NDR_CHECK(ndr_push_align(ndr, 4));
806 ndr_push_save(ndr, &save);
807 NDR_CHECK(ndr_token_retrieve(&ndr->relative_list, p, &ndr->offset));
808 if (ndr->flags & LIBNDR_FLAG_RELATIVE_CURRENT) {
809 NDR_CHECK(ndr_push_uint32(ndr, save.offset - ndr->offset));
811 NDR_CHECK(ndr_push_uint32(ndr, save.offset));
813 ndr_push_restore(ndr, &save);
819 pull a union from a blob using NDR
821 NTSTATUS ndr_pull_union_blob(DATA_BLOB *blob, TALLOC_CTX *mem_ctx, uint32_t level, void *p,
822 ndr_pull_union_fn_t fn)
824 struct ndr_pull *ndr;
825 ndr = ndr_pull_init_blob(blob, mem_ctx);
827 return NT_STATUS_NO_MEMORY;
829 return fn(ndr, NDR_SCALARS|NDR_BUFFERS, level, p);
833 pull a struct from a blob using NDR
835 NTSTATUS ndr_pull_struct_blob(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p,
836 ndr_pull_flags_fn_t fn)
838 struct ndr_pull *ndr;
839 ndr = ndr_pull_init_blob(blob, mem_ctx);
841 return NT_STATUS_NO_MEMORY;
843 return fn(ndr, NDR_SCALARS|NDR_BUFFERS, p);
847 push a struct to a blob using NDR
849 NTSTATUS ndr_push_struct_blob(DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p,
850 ndr_push_flags_fn_t fn)
853 struct ndr_push *ndr;
854 ndr = ndr_push_init_ctx(mem_ctx);
856 return NT_STATUS_NO_MEMORY;
858 status = fn(ndr, NDR_SCALARS|NDR_BUFFERS, p);
859 if (!NT_STATUS_IS_OK(status)) {
863 *blob = ndr_push_blob(ndr);
869 generic ndr_size_*() handler for structures
871 size_t ndr_size_struct(const void *p, int flags, ndr_push_flags_fn_t push)
873 struct ndr_push *ndr;
877 ndr = ndr_push_init_ctx(NULL);
880 status = push(ndr, NDR_SCALARS|NDR_BUFFERS, discard_const(p));
881 if (!NT_STATUS_IS_OK(status)) {
890 generic ndr_size_*() handler for unions
892 size_t ndr_size_union(const void *p, int flags, uint32_t level, ndr_push_union_fn_t push)
894 struct ndr_push *ndr;
898 ndr = ndr_push_init_ctx(NULL);
901 status = push(ndr, NDR_SCALARS|NDR_BUFFERS, level, discard_const(p));
902 if (!NT_STATUS_IS_OK(status)) {