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_p(mem_ctx, struct ndr_pull);
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, 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));
253 pull a constant size array of structures
255 NTSTATUS ndr_pull_struct_array(struct ndr_pull *ndr, uint32_t count,
256 size_t elsize, void **info,
257 NTSTATUS (*pull_fn)(struct ndr_pull *, int, void *))
262 NDR_ALLOC_N_SIZE(ndr, *info, count, elsize);
263 base = (char *)*info;
265 for (i = 0; i < count; i++) {
266 ndr->data += ndr->offset;
268 NDR_CHECK(pull_fn(ndr, NDR_SCALARS|NDR_BUFFERS, &base[count * elsize]));
275 print a generic array
277 void ndr_print_array(struct ndr_print *ndr, const char *name, void *base,
278 size_t elsize, uint32_t count,
279 void (*print_fn)(struct ndr_print *, const char *, void *))
283 ndr->print(ndr, "%s: ARRAY(%d)", name, count);
285 for (i=0;i<count;i++) {
287 asprintf(&idx, "[%d]", i);
289 print_fn(ndr, idx, p);
299 void ndr_print_debug_helper(struct ndr_print *ndr, const char *format, ...) _PRINTF_ATTRIBUTE(2,3)
305 va_start(ap, format);
306 vasprintf(&s, format, ap);
309 for (i=0;i<ndr->depth;i++) {
313 DEBUG(0,("%s\n", s));
318 a useful helper function for printing idl structures via DEBUG()
320 void ndr_print_debug(void (*fn)(struct ndr_print *, const char *, void *),
324 struct ndr_print *ndr;
326 ndr = talloc_p(NULL, struct ndr_print);
328 ndr->print = ndr_print_debug_helper;
337 a useful helper function for printing idl unions via DEBUG()
339 void ndr_print_union_debug(void (*fn)(struct ndr_print *, const char *, uint32_t, void *),
344 struct ndr_print *ndr;
346 ndr = talloc_p(NULL, struct ndr_print);
348 ndr->print = ndr_print_debug_helper;
351 fn(ndr, name, level, ptr);
356 a useful helper function for printing idl function calls via DEBUG()
358 void ndr_print_function_debug(void (*fn)(struct ndr_print *, const char *, int , void *),
363 struct ndr_print *ndr;
365 ndr = talloc_p(NULL, struct ndr_print);
367 ndr->print = ndr_print_debug_helper;
370 fn(ndr, name, flags, ptr);
374 void ndr_set_flags(uint32_t *pflags, uint32_t new_flags)
376 /* the big/little endian flags are inter-dependent */
377 if (new_flags & LIBNDR_FLAG_LITTLE_ENDIAN) {
378 (*pflags) &= ~LIBNDR_FLAG_BIGENDIAN;
380 if (new_flags & LIBNDR_FLAG_BIGENDIAN) {
381 (*pflags) &= ~LIBNDR_FLAG_LITTLE_ENDIAN;
383 (*pflags) |= new_flags;
386 static NTSTATUS ndr_map_error(enum ndr_err_code err)
389 case NDR_ERR_BUFSIZE:
390 return NT_STATUS_BUFFER_TOO_SMALL;
392 return NT_STATUS_NO_MEMORY;
395 /* we should all error codes to different status codes */
396 return NT_STATUS_INVALID_PARAMETER;
400 return and possibly log an NDR error
402 NTSTATUS ndr_pull_error(struct ndr_pull *ndr,
403 enum ndr_err_code err, const char *format, ...) _PRINTF_ATTRIBUTE(3,4)
408 va_start(ap, format);
409 vasprintf(&s, format, ap);
412 DEBUG(3,("ndr_pull_error(%u): %s\n", err, s));
416 return ndr_map_error(err);
420 return and possibly log an NDR error
422 NTSTATUS ndr_push_error(struct ndr_push *ndr, enum ndr_err_code err, const char *format, ...) _PRINTF_ATTRIBUTE(3,4)
427 va_start(ap, format);
428 vasprintf(&s, format, ap);
431 DEBUG(3,("ndr_push_error(%u): %s\n", err, s));
435 return ndr_map_error(err);
440 handle subcontext buffers, which in midl land are user-marshalled, but
441 we use magic in pidl to make them easier to cope with
443 static NTSTATUS ndr_pull_subcontext_header(struct ndr_pull *ndr,
445 struct ndr_pull *ndr2)
449 uint32_t size = ndr->data_size - ndr->offset;
450 if (size == 0) return NT_STATUS_OK;
451 NDR_CHECK(ndr_pull_subcontext(ndr, ndr2, size));
457 NDR_CHECK(ndr_pull_uint16(ndr, &size));
458 if (size == 0) return NT_STATUS_OK;
459 NDR_CHECK(ndr_pull_subcontext(ndr, ndr2, size));
465 NDR_CHECK(ndr_pull_uint32(ndr, &size));
466 if (size == 0) return NT_STATUS_OK;
467 NDR_CHECK(ndr_pull_subcontext(ndr, ndr2, size));
471 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext size %d",
478 handle subcontext buffers, which in midl land are user-marshalled, but
479 we use magic in pidl to make them easier to cope with
481 NTSTATUS ndr_pull_subcontext_fn(struct ndr_pull *ndr,
484 NTSTATUS (*fn)(struct ndr_pull *, void *))
486 struct ndr_pull *ndr2;
487 NDR_ALLOC(ndr, ndr2);
488 NDR_CHECK(ndr_pull_subcontext_header(ndr, sub_size, ndr2));
489 NDR_CHECK(fn(ndr2, base));
491 NDR_CHECK(ndr_pull_advance(ndr, ndr2->data_size));
493 NDR_CHECK(ndr_pull_advance(ndr, ndr2->offset));
499 NTSTATUS ndr_pull_subcontext_flags_fn(struct ndr_pull *ndr,
502 NTSTATUS (*fn)(struct ndr_pull *, int , void *))
504 struct ndr_pull *ndr2;
505 NDR_ALLOC(ndr, ndr2);
506 NDR_CHECK(ndr_pull_subcontext_header(ndr, sub_size, ndr2));
507 NDR_CHECK(fn(ndr2, NDR_SCALARS|NDR_BUFFERS, base));
509 NDR_CHECK(ndr_pull_advance(ndr, ndr2->data_size));
511 NDR_CHECK(ndr_pull_advance(ndr, ndr2->offset));
516 NTSTATUS ndr_pull_subcontext_union_fn(struct ndr_pull *ndr,
520 NTSTATUS (*fn)(struct ndr_pull *, int , uint32_t , void *))
522 struct ndr_pull *ndr2;
524 NDR_ALLOC(ndr, ndr2);
525 NDR_CHECK(ndr_pull_subcontext_header(ndr, sub_size, ndr2));
526 NDR_CHECK(fn(ndr2, NDR_SCALARS|NDR_BUFFERS, level, base));
528 NDR_CHECK(ndr_pull_advance(ndr, ndr2->data_size));
530 NDR_CHECK(ndr_pull_advance(ndr, ndr2->offset));
537 push a subcontext header
539 static NTSTATUS ndr_push_subcontext_header(struct ndr_push *ndr,
541 struct ndr_push *ndr2)
548 NDR_CHECK(ndr_push_uint16(ndr, ndr2->offset));
552 NDR_CHECK(ndr_push_uint32(ndr, ndr2->offset));
556 return ndr_push_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext size %d",
563 handle subcontext buffers, which in midl land are user-marshalled, but
564 we use magic in pidl to make them easier to cope with
566 NTSTATUS ndr_push_subcontext_fn(struct ndr_push *ndr,
569 NTSTATUS (*fn)(struct ndr_push *, void *))
571 struct ndr_push *ndr2;
573 ndr2 = ndr_push_init_ctx(ndr);
574 if (!ndr2) return NT_STATUS_NO_MEMORY;
576 ndr2->flags = ndr->flags;
577 NDR_CHECK(fn(ndr2, base));
578 NDR_CHECK(ndr_push_subcontext_header(ndr, sub_size, ndr2));
579 NDR_CHECK(ndr_push_bytes(ndr, ndr2->data, ndr2->offset));
584 handle subcontext buffers for function that take a flags arg
586 NTSTATUS ndr_push_subcontext_flags_fn(struct ndr_push *ndr,
589 NTSTATUS (*fn)(struct ndr_push *, int, void *))
591 struct ndr_push *ndr2;
593 ndr2 = ndr_push_init_ctx(ndr);
594 if (!ndr2) return NT_STATUS_NO_MEMORY;
596 ndr2->flags = ndr->flags;
597 NDR_CHECK(fn(ndr2, NDR_SCALARS|NDR_BUFFERS, base));
598 NDR_CHECK(ndr_push_subcontext_header(ndr, sub_size, ndr2));
599 NDR_CHECK(ndr_push_bytes(ndr, ndr2->data, ndr2->offset));
604 handle subcontext buffers for function that take a union
606 NTSTATUS ndr_push_subcontext_union_fn(struct ndr_push *ndr,
610 NTSTATUS (*fn)(struct ndr_push *, int, uint32_t, void *))
612 struct ndr_push *ndr2;
614 ndr2 = ndr_push_init_ctx(ndr);
615 if (!ndr2) return NT_STATUS_NO_MEMORY;
617 ndr2->flags = ndr->flags;
618 NDR_CHECK(fn(ndr2, NDR_SCALARS|NDR_BUFFERS, level, base));
619 NDR_CHECK(ndr_push_subcontext_header(ndr, sub_size, ndr2));
620 NDR_CHECK(ndr_push_bytes(ndr, ndr2->data, ndr2->offset));
626 mark the start of a structure
628 NTSTATUS ndr_pull_struct_start(struct ndr_pull *ndr)
634 mark the end of a structure
636 void ndr_pull_struct_end(struct ndr_pull *ndr)
641 mark the start of a structure
643 NTSTATUS ndr_push_struct_start(struct ndr_push *ndr)
649 mark the end of a structure
651 void ndr_push_struct_end(struct ndr_push *ndr)
656 store a token in the ndr context, for later retrieval
658 static NTSTATUS ndr_token_store(TALLOC_CTX *mem_ctx,
659 struct ndr_token_list **list,
663 struct ndr_token_list *tok;
664 tok = talloc_p(mem_ctx, struct ndr_token_list);
666 return NT_STATUS_NO_MEMORY;
670 DLIST_ADD((*list), tok);
675 retrieve a token from a ndr context
677 static uint32_t ndr_token_retrieve(struct ndr_token_list **list, const void *key)
679 struct ndr_token_list *tok;
680 for (tok=*list;tok;tok=tok->next) {
681 if (tok->key == key) {
682 DLIST_REMOVE((*list), tok);
691 pull a relative object - stage1
692 called during SCALARS processing
694 NTSTATUS ndr_pull_relative1(struct ndr_pull *ndr, const void *p, uint32_t rel_offset)
696 if (ndr->flags & LIBNDR_FLAG_RELATIVE_CURRENT) {
697 return ndr_token_store(ndr, &ndr->relative_list, p,
698 rel_offset + ndr->offset - 4);
700 return ndr_token_store(ndr, &ndr->relative_list, p, rel_offset);
705 pull a relative object - stage2
706 called during BUFFERS processing
708 NTSTATUS ndr_pull_relative2(struct ndr_pull *ndr, const void *p)
711 rel_offset = ndr_token_retrieve(&ndr->relative_list, p);
712 if (rel_offset == 0) {
713 return NT_STATUS_INTERNAL_ERROR;
715 return ndr_pull_set_offset(ndr, rel_offset);
719 push a relative object - stage1
720 this is called during SCALARS processing
722 NTSTATUS ndr_push_relative1(struct ndr_push *ndr, const void *p)
725 NDR_CHECK(ndr_push_uint32(ndr, 0));
728 NDR_CHECK(ndr_push_align(ndr, 4));
729 NDR_CHECK(ndr_token_store(ndr, &ndr->relative_list, p, ndr->offset));
730 return ndr_push_uint32(ndr, 0xFFFFFFFF);
734 push a relative object - stage2
735 this is called during buffers processing
737 NTSTATUS ndr_push_relative2(struct ndr_push *ndr, const void *p)
739 struct ndr_push_save save;
743 NDR_CHECK(ndr_push_align(ndr, 4));
744 ndr_push_save(ndr, &save);
745 ndr->offset = ndr_token_retrieve(&ndr->relative_list, p);
746 if (ndr->offset == 0) {
747 return NT_STATUS_INTERNAL_ERROR;
749 if (ndr->flags & LIBNDR_FLAG_RELATIVE_CURRENT) {
750 NDR_CHECK(ndr_push_uint32(ndr, save.offset - ndr->offset));
752 NDR_CHECK(ndr_push_uint32(ndr, save.offset));
754 ndr_push_restore(ndr, &save);
760 pull a union from a blob using NDR
762 NTSTATUS ndr_pull_union_blob(DATA_BLOB *blob, TALLOC_CTX *mem_ctx, uint32_t level, void *p,
763 NTSTATUS (*fn)(struct ndr_pull *, int ndr_flags, uint32_t, void *))
765 struct ndr_pull *ndr;
766 ndr = ndr_pull_init_blob(blob, mem_ctx);
768 return NT_STATUS_NO_MEMORY;
770 return fn(ndr, NDR_SCALARS|NDR_BUFFERS, level, p);
774 pull a struct from a blob using NDR
776 NTSTATUS ndr_pull_struct_blob(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p,
777 NTSTATUS (*fn)(struct ndr_pull *, int , void *))
779 struct ndr_pull *ndr;
780 ndr = ndr_pull_init_blob(blob, mem_ctx);
782 return NT_STATUS_NO_MEMORY;
784 return fn(ndr, NDR_SCALARS|NDR_BUFFERS, p);
788 push a struct to a blob using NDR
790 NTSTATUS ndr_push_struct_blob(DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p,
791 NTSTATUS (*fn)(struct ndr_push *, int , void *))
794 struct ndr_push *ndr;
795 ndr = ndr_push_init_ctx(mem_ctx);
797 return NT_STATUS_NO_MEMORY;
799 status = fn(ndr, NDR_SCALARS|NDR_BUFFERS, p);
800 if (!NT_STATUS_IS_OK(status)) {
804 *blob = ndr_push_blob(ndr);