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;
57 ndr->mem_ctx = mem_ctx;
63 create an ndr sub-context based on an existing context. The new context starts
64 at the current offset, with the given size limit
66 NTSTATUS ndr_pull_subcontext(struct ndr_pull *ndr, struct ndr_pull *ndr2, uint32_t size)
68 NDR_PULL_NEED_BYTES(ndr, size);
70 ndr2->data += ndr2->offset;
72 ndr2->data_size = size;
73 ndr2->flags = ndr->flags;
79 advance by 'size' bytes
81 NTSTATUS ndr_pull_advance(struct ndr_pull *ndr, uint32_t size)
84 if (ndr->offset > ndr->data_size) {
85 return ndr_pull_error(ndr, NDR_ERR_BUFSIZE,
86 "ndr_pull_advance by %u failed",
93 set the parse offset to 'ofs'
95 NTSTATUS ndr_pull_set_offset(struct ndr_pull *ndr, uint32_t ofs)
98 if (ndr->offset > ndr->data_size) {
99 return ndr_pull_error(ndr, NDR_ERR_BUFSIZE,
100 "ndr_pull_set_offset %u failed",
106 /* save the offset/size of the current ndr state */
107 void ndr_pull_save(struct ndr_pull *ndr, struct ndr_pull_save *save)
109 save->offset = ndr->offset;
110 save->data_size = ndr->data_size;
113 /* restore the size/offset of a ndr structure */
114 void ndr_pull_restore(struct ndr_pull *ndr, struct ndr_pull_save *save)
116 ndr->offset = save->offset;
117 ndr->data_size = save->data_size;
121 /* create a ndr_push structure, ready for some marshalling */
122 struct ndr_push *ndr_push_init_ctx(TALLOC_CTX *mem_ctx)
124 struct ndr_push *ndr;
126 ndr = talloc(mem_ctx, sizeof(*ndr));
131 ndr->mem_ctx = mem_ctx;
133 ndr->alloc_size = NDR_BASE_MARSHALL_SIZE;
134 ndr->data = talloc(ndr->mem_ctx, ndr->alloc_size);
140 ndr->relative_list = NULL;
141 ndr->relative_list_end = NULL;
147 /* create a ndr_push structure, ready for some marshalling */
148 struct ndr_push *ndr_push_init(void)
150 struct ndr_push *ndr;
151 TALLOC_CTX *mem_ctx = talloc_init("ndr_push_init");
152 if (!mem_ctx) return NULL;
153 ndr = ndr_push_init_ctx(mem_ctx);
155 talloc_destroy(mem_ctx);
160 /* free a ndr_push structure */
161 void ndr_push_free(struct ndr_push *ndr)
163 talloc_destroy(ndr->mem_ctx);
167 /* return a DATA_BLOB structure for the current ndr_push marshalled data */
168 DATA_BLOB ndr_push_blob(struct ndr_push *ndr)
171 blob.data = ndr->data;
172 blob.length = ndr->offset;
178 expand the available space in the buffer to 'size'
180 NTSTATUS ndr_push_expand(struct ndr_push *ndr, uint32_t size)
182 if (ndr->alloc_size >= size) {
186 ndr->alloc_size += NDR_BASE_MARSHALL_SIZE;
187 if (size > ndr->alloc_size) {
188 ndr->alloc_size = size;
190 ndr->data = talloc_realloc(ndr->mem_ctx, ndr->data, ndr->alloc_size);
192 return ndr_push_error(ndr, NDR_ERR_ALLOC, "Failed to push_expand to %u",
200 set the push offset to 'ofs'
202 NTSTATUS ndr_push_set_offset(struct ndr_push *ndr, uint32_t ofs)
204 NDR_CHECK(ndr_push_expand(ndr, ofs));
212 NTSTATUS ndr_push_array(struct ndr_push *ndr, int ndr_flags, void *base,
213 size_t elsize, uint32_t count,
214 NTSTATUS (*push_fn)(struct ndr_push *, int, void *))
218 if (!(ndr_flags & NDR_SCALARS)) goto buffers;
219 for (i=0;i<count;i++) {
220 NDR_CHECK(push_fn(ndr, NDR_SCALARS, p));
223 if (!(ndr_flags & NDR_BUFFERS)) goto done;
226 for (i=0;i<count;i++) {
227 NDR_CHECK(push_fn(ndr, NDR_BUFFERS, p));
235 pull a constant sized array
237 NTSTATUS ndr_pull_array(struct ndr_pull *ndr, int ndr_flags, void *base,
238 size_t elsize, uint32_t count,
239 NTSTATUS (*pull_fn)(struct ndr_pull *, int, void *))
244 if (!(ndr_flags & NDR_SCALARS)) goto buffers;
245 for (i=0;i<count;i++) {
246 NDR_CHECK(pull_fn(ndr, NDR_SCALARS, p));
249 if (!(ndr_flags & NDR_BUFFERS)) goto done;
252 for (i=0;i<count;i++) {
253 NDR_CHECK(pull_fn(ndr, NDR_BUFFERS, p));
262 print a generic array
264 void ndr_print_array(struct ndr_print *ndr, const char *name, void *base,
265 size_t elsize, uint32_t count,
266 void (*print_fn)(struct ndr_print *, const char *, void *))
270 ndr->print(ndr, "%s: ARRAY(%d)", name, count);
272 for (i=0;i<count;i++) {
274 asprintf(&idx, "[%d]", i);
276 print_fn(ndr, idx, p);
286 void ndr_print_debug_helper(struct ndr_print *ndr, const char *format, ...)
292 va_start(ap, format);
293 vasprintf(&s, format, ap);
296 for (i=0;i<ndr->depth;i++) {
300 DEBUG(0,("%s\n", s));
305 a useful helper function for printing idl structures via DEBUG()
307 void ndr_print_debug(void (*fn)(struct ndr_print *, const char *, void *),
311 struct ndr_print ndr;
313 ndr.mem_ctx = talloc_init("ndr_print_debug");
314 if (!ndr.mem_ctx) return;
315 ndr.print = ndr_print_debug_helper;
318 talloc_destroy(ndr.mem_ctx);
323 a useful helper function for printing idl unions via DEBUG()
325 void ndr_print_union_debug(void (*fn)(struct ndr_print *, const char *, uint32_t, void *),
330 struct ndr_print ndr;
332 ndr.mem_ctx = talloc_init("ndr_print_union");
333 if (!ndr.mem_ctx) return;
334 ndr.print = ndr_print_debug_helper;
336 fn(&ndr, name, level, ptr);
337 talloc_destroy(ndr.mem_ctx);
341 a useful helper function for printing idl function calls via DEBUG()
343 void ndr_print_function_debug(void (*fn)(struct ndr_print *, const char *, int , void *),
348 struct ndr_print ndr;
350 ndr.mem_ctx = talloc_init("ndr_print_function");
351 if (!ndr.mem_ctx) return;
352 ndr.print = ndr_print_debug_helper;
355 fn(&ndr, name, flags, ptr);
356 talloc_destroy(ndr.mem_ctx);
360 static NTSTATUS ndr_map_error(enum ndr_err_code err)
363 case NDR_ERR_BUFSIZE:
364 return NT_STATUS_BUFFER_TOO_SMALL;
366 return NT_STATUS_NO_MEMORY;
369 /* we should all error codes to different status codes */
370 return NT_STATUS_INVALID_PARAMETER;
374 return and possibly log an NDR error
376 NTSTATUS ndr_pull_error(struct ndr_pull *ndr, enum ndr_err_code err, const char *format, ...)
381 va_start(ap, format);
382 vasprintf(&s, format, ap);
385 DEBUG(3,("ndr_pull_error(%u): %s\n", err, s));
389 return ndr_map_error(err);
393 return and possibly log an NDR error
395 NTSTATUS ndr_push_error(struct ndr_push *ndr, enum ndr_err_code err, const char *format, ...)
400 va_start(ap, format);
401 vasprintf(&s, format, ap);
404 DEBUG(3,("ndr_push_error(%u): %s\n", err, s));
408 return ndr_map_error(err);
413 handle subcontext buffers, which in midl land are user-marshalled, but
414 we use magic in pidl to make them easier to cope with
416 static NTSTATUS ndr_pull_subcontext_header(struct ndr_pull *ndr,
418 struct ndr_pull *ndr2)
422 uint32_t size = ndr->data_size - ndr->offset;
423 if (size == 0) return NT_STATUS_OK;
424 NDR_CHECK(ndr_pull_subcontext(ndr, ndr2, size));
430 NDR_CHECK(ndr_pull_uint16(ndr, &size));
431 if (size == 0) return NT_STATUS_OK;
432 NDR_CHECK(ndr_pull_subcontext(ndr, ndr2, size));
438 NDR_CHECK(ndr_pull_uint32(ndr, &size));
439 if (size == 0) return NT_STATUS_OK;
440 NDR_CHECK(ndr_pull_subcontext(ndr, ndr2, size));
444 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext size %d",
451 handle subcontext buffers, which in midl land are user-marshalled, but
452 we use magic in pidl to make them easier to cope with
454 NTSTATUS ndr_pull_subcontext_fn(struct ndr_pull *ndr,
457 NTSTATUS (*fn)(struct ndr_pull *, void *))
459 struct ndr_pull ndr2;
461 NDR_CHECK(ndr_pull_subcontext_header(ndr, sub_size, &ndr2));
462 NDR_CHECK(fn(&ndr2, base));
464 NDR_CHECK(ndr_pull_advance(ndr, ndr2.data_size));
466 NDR_CHECK(ndr_pull_advance(ndr, ndr2.offset));
472 NTSTATUS ndr_pull_subcontext_flags_fn(struct ndr_pull *ndr,
475 NTSTATUS (*fn)(struct ndr_pull *, int , void *))
477 struct ndr_pull ndr2;
479 NDR_CHECK(ndr_pull_subcontext_header(ndr, sub_size, &ndr2));
480 NDR_CHECK(fn(&ndr2, NDR_SCALARS|NDR_BUFFERS, base));
482 NDR_CHECK(ndr_pull_advance(ndr, ndr2.data_size));
484 NDR_CHECK(ndr_pull_advance(ndr, ndr2.offset));
489 NTSTATUS ndr_pull_subcontext_union_fn(struct ndr_pull *ndr,
493 NTSTATUS (*fn)(struct ndr_pull *, int , uint32_t , void *))
495 struct ndr_pull ndr2;
497 NDR_CHECK(ndr_pull_subcontext_header(ndr, sub_size, &ndr2));
498 NDR_CHECK(fn(&ndr2, NDR_SCALARS|NDR_BUFFERS, level, base));
500 NDR_CHECK(ndr_pull_advance(ndr, ndr2.data_size));
502 NDR_CHECK(ndr_pull_advance(ndr, ndr2.offset));
509 push a subcontext header
511 static NTSTATUS ndr_push_subcontext_header(struct ndr_push *ndr,
513 struct ndr_push *ndr2)
520 NDR_CHECK(ndr_push_uint16(ndr, ndr2->offset));
524 NDR_CHECK(ndr_push_uint32(ndr, ndr2->offset));
528 return ndr_push_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext size %d",
535 handle subcontext buffers, which in midl land are user-marshalled, but
536 we use magic in pidl to make them easier to cope with
538 NTSTATUS ndr_push_subcontext_fn(struct ndr_push *ndr,
541 NTSTATUS (*fn)(struct ndr_push *, void *))
543 struct ndr_push *ndr2;
545 ndr2 = ndr_push_init_ctx(ndr->mem_ctx);
546 if (!ndr2) return NT_STATUS_NO_MEMORY;
548 ndr2->flags = ndr->flags;
549 NDR_CHECK(fn(ndr2, base));
550 NDR_CHECK(ndr_push_subcontext_header(ndr, sub_size, ndr2));
551 NDR_CHECK(ndr_push_bytes(ndr, ndr2->data, ndr2->offset));
556 handle subcontext buffers for function that take a flags arg
558 NTSTATUS ndr_push_subcontext_flags_fn(struct ndr_push *ndr,
561 NTSTATUS (*fn)(struct ndr_push *, int, void *))
563 struct ndr_push *ndr2;
565 ndr2 = ndr_push_init_ctx(ndr->mem_ctx);
566 if (!ndr2) return NT_STATUS_NO_MEMORY;
568 ndr2->flags = ndr->flags;
569 NDR_CHECK(fn(ndr2, NDR_SCALARS|NDR_BUFFERS, base));
570 NDR_CHECK(ndr_push_subcontext_header(ndr, sub_size, ndr2));
571 NDR_CHECK(ndr_push_bytes(ndr, ndr2->data, ndr2->offset));
576 handle subcontext buffers for function that take a union
578 NTSTATUS ndr_push_subcontext_union_fn(struct ndr_push *ndr,
582 NTSTATUS (*fn)(struct ndr_push *, int, uint32_t, void *))
584 struct ndr_push *ndr2;
586 ndr2 = ndr_push_init_ctx(ndr->mem_ctx);
587 if (!ndr2) return NT_STATUS_NO_MEMORY;
589 ndr2->flags = ndr->flags;
590 NDR_CHECK(fn(ndr2, NDR_SCALARS|NDR_BUFFERS, level, base));
591 NDR_CHECK(ndr_push_subcontext_header(ndr, sub_size, ndr2));
592 NDR_CHECK(ndr_push_bytes(ndr, ndr2->data, ndr2->offset));
598 mark the start of a structure
600 NTSTATUS ndr_pull_struct_start(struct ndr_pull *ndr)
602 struct ndr_ofs_list *ofs;
604 ofs->offset = ndr->offset;
605 ofs->next = ndr->ofs_list;
611 mark the end of a structure
613 void ndr_pull_struct_end(struct ndr_pull *ndr)
615 ndr->ofs_list = ndr->ofs_list->next;
619 mark the start of a structure
621 NTSTATUS ndr_push_struct_start(struct ndr_push *ndr)
623 struct ndr_ofs_list *ofs;
624 NDR_PUSH_ALLOC(ndr, ofs);
625 ofs->offset = ndr->offset;
626 ofs->next = ndr->ofs_list;
632 mark the end of a structure
634 void ndr_push_struct_end(struct ndr_push *ndr)
636 ndr->ofs_list = ndr->ofs_list->next;
641 pull a relative structure
643 NTSTATUS ndr_pull_relative(struct ndr_pull *ndr, const void **buf, size_t size,
644 NTSTATUS (*fn)(struct ndr_pull *, int ndr_flags, void *))
646 struct ndr_pull ndr2;
648 struct ndr_pull_save save;
651 NDR_CHECK(ndr_pull_uint32(ndr, &ofs));
656 ndr_pull_save(ndr, &save);
657 NDR_CHECK(ndr_pull_set_offset(ndr, ofs + ndr->ofs_list->offset));
658 NDR_CHECK(ndr_pull_subcontext(ndr, &ndr2, ndr->data_size - ndr->offset));
659 /* strings must be allocated by the backend functions */
660 if (ndr->flags & LIBNDR_STRING_FLAGS) {
661 NDR_CHECK(fn(&ndr2, NDR_SCALARS|NDR_BUFFERS, &p));
663 NDR_ALLOC_SIZE(ndr, p, size);
664 NDR_CHECK(fn(&ndr2, NDR_SCALARS|NDR_BUFFERS, p));
667 ndr_pull_restore(ndr, &save);
672 push a relative structure
674 NTSTATUS ndr_push_relative(struct ndr_push *ndr, int ndr_flags, const void *p,
675 NTSTATUS (*fn)(struct ndr_push *, int , const void *))
677 struct ndr_ofs_list *ofs;
678 if (ndr_flags & NDR_SCALARS) {
680 NDR_CHECK(ndr_push_uint32(ndr, 0));
683 NDR_PUSH_ALLOC(ndr, ofs);
684 NDR_CHECK(ndr_push_align(ndr, 4));
685 ofs->offset = ndr->offset;
686 NDR_CHECK(ndr_push_uint32(ndr, 0xFFFFFFFF));
688 if (ndr->relative_list_end) {
689 ndr->relative_list_end->next = ofs;
691 ndr->relative_list = ofs;
693 ndr->relative_list_end = ofs;
695 if (ndr_flags & NDR_BUFFERS) {
696 struct ndr_push_save save;
700 ofs = ndr->relative_list;
702 return ndr_push_error(ndr, NDR_ERR_RELATIVE, "Empty relative stack");
704 ndr->relative_list = ndr->relative_list->next;
705 if (ndr->relative_list == NULL) {
706 ndr->relative_list_end = NULL;
708 NDR_CHECK(ndr_push_align(ndr, 4));
709 ndr_push_save(ndr, &save);
710 ndr->offset = ofs->offset;
711 NDR_CHECK(ndr_push_uint32(ndr, save.offset - ndr->ofs_list->offset));
712 ndr_push_restore(ndr, &save);
713 NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
720 pull a union from a blob using NDR
722 NTSTATUS ndr_pull_union_blob(DATA_BLOB *blob, TALLOC_CTX *mem_ctx, uint32_t level, void *p,
723 NTSTATUS (*fn)(struct ndr_pull *, int ndr_flags, uint32_t, void *))
725 struct ndr_pull *ndr;
726 ndr = ndr_pull_init_blob(blob, mem_ctx);
728 return NT_STATUS_NO_MEMORY;
730 return fn(ndr, NDR_SCALARS|NDR_BUFFERS, level, p);
734 pull a struct from a blob using NDR
736 NTSTATUS ndr_pull_struct_blob(DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p,
737 NTSTATUS (*fn)(struct ndr_pull *, int , void *))
739 struct ndr_pull *ndr;
740 ndr = ndr_pull_init_blob(blob, mem_ctx);
742 return NT_STATUS_NO_MEMORY;
744 return fn(ndr, NDR_SCALARS|NDR_BUFFERS, p);
748 push a struct to a blob using NDR
750 NTSTATUS ndr_push_struct_blob(DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p,
751 NTSTATUS (*fn)(struct ndr_push *, int , void *))
754 struct ndr_push *ndr;
755 ndr = ndr_push_init_ctx(mem_ctx);
757 return NT_STATUS_NO_MEMORY;
759 status = fn(ndr, NDR_SCALARS|NDR_BUFFERS, p);
760 if (!NT_STATUS_IS_OK(status)) {
764 *blob = ndr_push_blob(ndr);