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
32 #define NDR_BASE_MARSHALL_SIZE 1024
35 work out the number of bytes needed to align on a n byte boundary
37 size_t ndr_align_size(uint32_t offset, size_t n)
39 if ((offset & (n-1)) == 0) return 0;
40 return n - (offset & (n-1));
44 initialise a ndr parse structure from a data blob
46 struct ndr_pull *ndr_pull_init_blob(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx)
50 ndr = talloc(mem_ctx, sizeof(*ndr));
51 if (!ndr) return NULL;
54 ndr->data = blob->data;
55 ndr->data_size = blob->length;
58 ndr->relative_list = NULL;
64 create an ndr sub-context based on an existing context. The new context starts
65 at the current offset, with the given size limit
67 NTSTATUS ndr_pull_subcontext(struct ndr_pull *ndr, struct ndr_pull *ndr2, uint32_t size)
69 NDR_PULL_NEED_BYTES(ndr, size);
71 ndr2->data += ndr2->offset;
73 ndr2->data_size = size;
74 ndr2->flags = ndr->flags;
80 advance by 'size' bytes
82 NTSTATUS ndr_pull_advance(struct ndr_pull *ndr, uint32_t size)
85 if (ndr->offset > ndr->data_size) {
86 return ndr_pull_error(ndr, NDR_ERR_BUFSIZE,
87 "ndr_pull_advance by %u failed",
94 set the parse offset to 'ofs'
96 NTSTATUS ndr_pull_set_offset(struct ndr_pull *ndr, uint32_t ofs)
99 if (ndr->offset > ndr->data_size) {
100 return ndr_pull_error(ndr, NDR_ERR_BUFSIZE,
101 "ndr_pull_set_offset %u failed",
107 /* save the offset/size of the current ndr state */
108 void ndr_pull_save(struct ndr_pull *ndr, struct ndr_pull_save *save)
110 save->offset = ndr->offset;
111 save->data_size = ndr->data_size;
114 /* restore the size/offset of a ndr structure */
115 void ndr_pull_restore(struct ndr_pull *ndr, struct ndr_pull_save *save)
117 ndr->offset = save->offset;
118 ndr->data_size = save->data_size;
122 /* create a ndr_push structure, ready for some marshalling */
123 struct ndr_push *ndr_push_init_ctx(TALLOC_CTX *mem_ctx)
125 struct ndr_push *ndr;
127 ndr = talloc(mem_ctx, sizeof(*ndr));
133 ndr->alloc_size = NDR_BASE_MARSHALL_SIZE;
134 ndr->data = talloc(ndr, ndr->alloc_size);
140 ndr->relative_list = NULL;
146 /* create a ndr_push structure, ready for some marshalling */
147 struct ndr_push *ndr_push_init(void)
149 return ndr_push_init_ctx(NULL);
152 /* free a ndr_push structure */
153 void ndr_push_free(struct ndr_push *ndr)
159 /* return a DATA_BLOB structure for the current ndr_push marshalled data */
160 DATA_BLOB ndr_push_blob(struct ndr_push *ndr)
163 blob.data = ndr->data;
164 blob.length = ndr->offset;
170 expand the available space in the buffer to 'size'
172 NTSTATUS ndr_push_expand(struct ndr_push *ndr, uint32_t size)
174 if (ndr->alloc_size >= size) {
178 ndr->alloc_size += NDR_BASE_MARSHALL_SIZE;
179 if (size > ndr->alloc_size) {
180 ndr->alloc_size = size;
182 ndr->data = talloc_realloc(ndr->data, ndr->alloc_size);
184 return ndr_push_error(ndr, NDR_ERR_ALLOC, "Failed to push_expand to %u",
192 set the push offset to 'ofs'
194 NTSTATUS ndr_push_set_offset(struct ndr_push *ndr, uint32_t ofs)
196 NDR_CHECK(ndr_push_expand(ndr, ofs));
204 NTSTATUS ndr_push_array(struct ndr_push *ndr, int ndr_flags, void *base,
205 size_t elsize, uint32_t count,
206 NTSTATUS (*push_fn)(struct ndr_push *, int, void *))
210 if (!(ndr_flags & NDR_SCALARS)) goto buffers;
211 for (i=0;i<count;i++) {
212 NDR_CHECK(push_fn(ndr, NDR_SCALARS, p));
215 if (!(ndr_flags & NDR_BUFFERS)) goto done;
218 for (i=0;i<count;i++) {
219 NDR_CHECK(push_fn(ndr, NDR_BUFFERS, p));
227 pull a constant sized array
229 NTSTATUS ndr_pull_array(struct ndr_pull *ndr, int ndr_flags, void *base,
230 size_t elsize, uint32_t count,
231 NTSTATUS (*pull_fn)(struct ndr_pull *, int, void *))
236 if (!(ndr_flags & NDR_SCALARS)) goto buffers;
237 for (i=0;i<count;i++) {
238 NDR_CHECK(pull_fn(ndr, NDR_SCALARS, p));
241 if (!(ndr_flags & NDR_BUFFERS)) goto done;
244 for (i=0;i<count;i++) {
245 NDR_CHECK(pull_fn(ndr, NDR_BUFFERS, p));
254 print a generic array
256 void ndr_print_array(struct ndr_print *ndr, const char *name, void *base,
257 size_t elsize, uint32_t count,
258 void (*print_fn)(struct ndr_print *, const char *, void *))
262 ndr->print(ndr, "%s: ARRAY(%d)", name, count);
264 for (i=0;i<count;i++) {
266 asprintf(&idx, "[%d]", i);
268 print_fn(ndr, idx, p);
278 void ndr_print_debug_helper(struct ndr_print *ndr, const char *format, ...) _PRINTF_ATTRIBUTE(2,3)
284 va_start(ap, format);
285 vasprintf(&s, format, ap);
288 for (i=0;i<ndr->depth;i++) {
292 DEBUG(0,("%s\n", s));
297 a useful helper function for printing idl structures via DEBUG()
299 void ndr_print_debug(void (*fn)(struct ndr_print *, const char *, void *),
303 struct ndr_print *ndr;
305 ndr = talloc_p(NULL, struct ndr_print);
307 ndr->print = ndr_print_debug_helper;
316 a useful helper function for printing idl unions via DEBUG()
318 void ndr_print_union_debug(void (*fn)(struct ndr_print *, const char *, uint32_t, void *),
323 struct ndr_print *ndr;
325 ndr = talloc_p(NULL, struct ndr_print);
327 ndr->print = ndr_print_debug_helper;
330 fn(ndr, name, level, ptr);
335 a useful helper function for printing idl function calls via DEBUG()
337 void ndr_print_function_debug(void (*fn)(struct ndr_print *, const char *, int , void *),
342 struct ndr_print *ndr;
344 ndr = talloc_p(NULL, struct ndr_print);
346 ndr->print = ndr_print_debug_helper;
349 fn(ndr, name, flags, ptr);
354 static NTSTATUS ndr_map_error(enum ndr_err_code err)
357 case NDR_ERR_BUFSIZE:
358 return NT_STATUS_BUFFER_TOO_SMALL;
360 return NT_STATUS_NO_MEMORY;
363 /* we should all error codes to different status codes */
364 return NT_STATUS_INVALID_PARAMETER;
368 return and possibly log an NDR error
370 NTSTATUS ndr_pull_error(struct ndr_pull *ndr,
371 enum ndr_err_code err, const char *format, ...) _PRINTF_ATTRIBUTE(3,4)
376 va_start(ap, format);
377 vasprintf(&s, format, ap);
380 DEBUG(3,("ndr_pull_error(%u): %s\n", err, s));
384 return ndr_map_error(err);
388 return and possibly log an NDR error
390 NTSTATUS ndr_push_error(struct ndr_push *ndr, enum ndr_err_code err, const char *format, ...) _PRINTF_ATTRIBUTE(3,4)
395 va_start(ap, format);
396 vasprintf(&s, format, ap);
399 DEBUG(3,("ndr_push_error(%u): %s\n", err, s));
403 return ndr_map_error(err);
408 handle subcontext buffers, which in midl land are user-marshalled, but
409 we use magic in pidl to make them easier to cope with
411 static NTSTATUS ndr_pull_subcontext_header(struct ndr_pull *ndr,
413 struct ndr_pull *ndr2)
417 uint32_t size = ndr->data_size - ndr->offset;
418 if (size == 0) return NT_STATUS_OK;
419 NDR_CHECK(ndr_pull_subcontext(ndr, ndr2, size));
425 NDR_CHECK(ndr_pull_uint16(ndr, &size));
426 if (size == 0) return NT_STATUS_OK;
427 NDR_CHECK(ndr_pull_subcontext(ndr, ndr2, size));
433 NDR_CHECK(ndr_pull_uint32(ndr, &size));
434 if (size == 0) return NT_STATUS_OK;
435 NDR_CHECK(ndr_pull_subcontext(ndr, ndr2, size));
439 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext size %d",
446 handle subcontext buffers, which in midl land are user-marshalled, but
447 we use magic in pidl to make them easier to cope with
449 NTSTATUS ndr_pull_subcontext_fn(struct ndr_pull *ndr,
452 NTSTATUS (*fn)(struct ndr_pull *, void *))
454 struct ndr_pull *ndr2;
455 NDR_ALLOC(ndr, ndr2);
456 NDR_CHECK(ndr_pull_subcontext_header(ndr, sub_size, ndr2));
457 NDR_CHECK(fn(ndr2, base));
459 NDR_CHECK(ndr_pull_advance(ndr, ndr2->data_size));
461 NDR_CHECK(ndr_pull_advance(ndr, ndr2->offset));
467 NTSTATUS ndr_pull_subcontext_flags_fn(struct ndr_pull *ndr,
470 NTSTATUS (*fn)(struct ndr_pull *, int , void *))
472 struct ndr_pull *ndr2;
473 NDR_ALLOC(ndr, ndr2);
474 NDR_CHECK(ndr_pull_subcontext_header(ndr, sub_size, ndr2));
475 NDR_CHECK(fn(ndr2, NDR_SCALARS|NDR_BUFFERS, base));
477 NDR_CHECK(ndr_pull_advance(ndr, ndr2->data_size));
479 NDR_CHECK(ndr_pull_advance(ndr, ndr2->offset));
484 NTSTATUS ndr_pull_subcontext_union_fn(struct ndr_pull *ndr,
488 NTSTATUS (*fn)(struct ndr_pull *, int , uint32_t , void *))
490 struct ndr_pull *ndr2;
492 NDR_ALLOC(ndr, ndr2);
493 NDR_CHECK(ndr_pull_subcontext_header(ndr, sub_size, ndr2));
494 NDR_CHECK(fn(ndr2, NDR_SCALARS|NDR_BUFFERS, level, base));
496 NDR_CHECK(ndr_pull_advance(ndr, ndr2->data_size));
498 NDR_CHECK(ndr_pull_advance(ndr, ndr2->offset));
505 push a subcontext header
507 static NTSTATUS ndr_push_subcontext_header(struct ndr_push *ndr,
509 struct ndr_push *ndr2)
516 NDR_CHECK(ndr_push_uint16(ndr, ndr2->offset));
520 NDR_CHECK(ndr_push_uint32(ndr, ndr2->offset));
524 return ndr_push_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext size %d",
531 handle subcontext buffers, which in midl land are user-marshalled, but
532 we use magic in pidl to make them easier to cope with
534 NTSTATUS ndr_push_subcontext_fn(struct ndr_push *ndr,
537 NTSTATUS (*fn)(struct ndr_push *, void *))
539 struct ndr_push *ndr2;
541 ndr2 = ndr_push_init_ctx(ndr);
542 if (!ndr2) return NT_STATUS_NO_MEMORY;
544 ndr2->flags = ndr->flags;
545 NDR_CHECK(fn(ndr2, base));
546 NDR_CHECK(ndr_push_subcontext_header(ndr, sub_size, ndr2));
547 NDR_CHECK(ndr_push_bytes(ndr, ndr2->data, ndr2->offset));
552 handle subcontext buffers for function that take a flags arg
554 NTSTATUS ndr_push_subcontext_flags_fn(struct ndr_push *ndr,
557 NTSTATUS (*fn)(struct ndr_push *, int, void *))
559 struct ndr_push *ndr2;
561 ndr2 = ndr_push_init_ctx(ndr);
562 if (!ndr2) return NT_STATUS_NO_MEMORY;
564 ndr2->flags = ndr->flags;
565 NDR_CHECK(fn(ndr2, NDR_SCALARS|NDR_BUFFERS, base));
566 NDR_CHECK(ndr_push_subcontext_header(ndr, sub_size, ndr2));
567 NDR_CHECK(ndr_push_bytes(ndr, ndr2->data, ndr2->offset));
572 handle subcontext buffers for function that take a union
574 NTSTATUS ndr_push_subcontext_union_fn(struct ndr_push *ndr,
578 NTSTATUS (*fn)(struct ndr_push *, int, uint32_t, void *))
580 struct ndr_push *ndr2;
582 ndr2 = ndr_push_init_ctx(ndr);
583 if (!ndr2) return NT_STATUS_NO_MEMORY;
585 ndr2->flags = ndr->flags;
586 NDR_CHECK(fn(ndr2, NDR_SCALARS|NDR_BUFFERS, level, base));
587 NDR_CHECK(ndr_push_subcontext_header(ndr, sub_size, ndr2));
588 NDR_CHECK(ndr_push_bytes(ndr, ndr2->data, ndr2->offset));
594 mark the start of a structure
596 NTSTATUS ndr_pull_struct_start(struct ndr_pull *ndr)
602 mark the end of a structure
604 void ndr_pull_struct_end(struct ndr_pull *ndr)
609 mark the start of a structure
611 NTSTATUS ndr_push_struct_start(struct ndr_push *ndr)
617 mark the end of a structure
619 void ndr_push_struct_end(struct ndr_push *ndr)
624 store a token in the ndr context, for later retrieval
626 static NTSTATUS ndr_token_store(TALLOC_CTX *mem_ctx,
627 struct ndr_token_list **list,
631 struct ndr_token_list *tok;
632 tok = talloc_p(mem_ctx, struct ndr_token_list);
634 return NT_STATUS_NO_MEMORY;
638 DLIST_ADD((*list), tok);
643 retrieve a token from a ndr context
645 static uint32_t ndr_token_retrieve(struct ndr_token_list **list, const void *key)
647 struct ndr_token_list *tok;
648 for (tok=*list;tok;tok=tok->next) {
649 if (tok->key == key) {
650 DLIST_REMOVE((*list), tok);
659 pull a relative object - stage1
660 called during SCALARS processing
662 NTSTATUS ndr_pull_relative1(struct ndr_pull *ndr, const void *p, uint32_t rel_offset)
664 if (ndr->flags & LIBNDR_FLAG_RELATIVE_CURRENT) {
665 return ndr_token_store(ndr, &ndr->relative_list, p,
666 rel_offset + ndr->offset);
668 return ndr_token_store(ndr, &ndr->relative_list, p, rel_offset);
673 pull a relative object - stage2
674 called during BUFFERS processing
676 NTSTATUS ndr_pull_relative2(struct ndr_pull *ndr, const void *p)
679 rel_offset = ndr_token_retrieve(&ndr->relative_list, p);
680 if (rel_offset == 0) {
681 return NT_STATUS_INTERNAL_ERROR;
683 return ndr_pull_set_offset(ndr, rel_offset);
687 push a relative object - stage1
688 this is called during SCALARS processing
690 NTSTATUS ndr_push_relative1(struct ndr_push *ndr, const void *p)
693 NDR_CHECK(ndr_push_uint32(ndr, 0));
696 NDR_CHECK(ndr_push_align(ndr, 4));
697 NDR_CHECK(ndr_token_store(ndr, &ndr->relative_list, p, ndr->offset));
698 return ndr_push_uint32(ndr, 0xFFFFFFFF);
702 push a relative object - stage2
703 this is called during buffers processing
705 NTSTATUS ndr_push_relative2(struct ndr_push *ndr, const void *p)
707 struct ndr_push_save save;
711 NDR_CHECK(ndr_push_align(ndr, 4));
712 ndr_push_save(ndr, &save);
713 ndr->offset = ndr_token_retrieve(&ndr->relative_list, p);
714 if (ndr->offset == 0) {
715 return NT_STATUS_INTERNAL_ERROR;
717 if (ndr->flags & LIBNDR_FLAG_RELATIVE_CURRENT) {
718 NDR_CHECK(ndr_push_uint32(ndr, save.offset - ndr->offset));
720 NDR_CHECK(ndr_push_uint32(ndr, save.offset));
722 ndr_push_restore(ndr, &save);
728 pull a union from a blob using NDR
730 NTSTATUS ndr_pull_union_blob(DATA_BLOB *blob, TALLOC_CTX *mem_ctx, uint32_t level, void *p,
731 NTSTATUS (*fn)(struct ndr_pull *, int ndr_flags, uint32_t, void *))
733 struct ndr_pull *ndr;
734 ndr = ndr_pull_init_blob(blob, mem_ctx);
736 return NT_STATUS_NO_MEMORY;
738 return fn(ndr, NDR_SCALARS|NDR_BUFFERS, level, p);
742 pull a struct from a blob using NDR
744 NTSTATUS ndr_pull_struct_blob(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p,
745 NTSTATUS (*fn)(struct ndr_pull *, int , void *))
747 struct ndr_pull *ndr;
748 ndr = ndr_pull_init_blob(blob, mem_ctx);
750 return NT_STATUS_NO_MEMORY;
752 return fn(ndr, NDR_SCALARS|NDR_BUFFERS, p);
756 push a struct to a blob using NDR
758 NTSTATUS ndr_push_struct_blob(DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p,
759 NTSTATUS (*fn)(struct ndr_push *, int , void *))
762 struct ndr_push *ndr;
763 ndr = ndr_push_init_ctx(mem_ctx);
765 return NT_STATUS_NO_MEMORY;
767 status = fn(ndr, NDR_SCALARS|NDR_BUFFERS, p);
768 if (!NT_STATUS_IS_OK(status)) {
772 *blob = ndr_push_blob(ndr);