2 * Unix SMB/CIFS implementation.
3 * RPC Pipe client / server routines
4 * Copyright (C) Andrew Tridgell 1992-1997,
5 * Copyright (C) Luke Kenneth Casson Leighton 1996-1997,
6 * Copyright (C) Paul Ashton 1997.
7 * Copyright (C) Gerald (Jerry) Carter 2005
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #define DBGC_CLASS DBGC_RPC_PARSE
29 /****************************************************************************
30 A temporary TALLOC context for things like unistrs, that is valid for
31 the life of a complete RPC call.
32 ****************************************************************************/
34 static TALLOC_CTX *current_rpc_talloc = NULL;
36 static TALLOC_CTX *get_current_rpc_talloc(void)
38 return current_rpc_talloc;
41 void set_current_rpc_talloc( TALLOC_CTX *ctx)
43 current_rpc_talloc = ctx;
46 static TALLOC_CTX *main_loop_talloc = NULL;
48 /*******************************************************************
49 free up temporary memory - called from the main loop
50 ********************************************************************/
52 void main_loop_TALLOC_FREE(void)
54 if (!main_loop_talloc)
56 talloc_destroy(main_loop_talloc);
57 main_loop_talloc = NULL;
60 /*******************************************************************
61 Get a talloc context that is freed in the main loop...
62 ********************************************************************/
64 TALLOC_CTX *main_loop_talloc_get(void)
66 if (!main_loop_talloc) {
67 main_loop_talloc = talloc_init("main loop talloc (mainly parse_misc)");
68 if (!main_loop_talloc)
69 smb_panic("main_loop_talloc: malloc fail\n");
72 return main_loop_talloc;
75 /*******************************************************************
76 Try and get a talloc context. Get the rpc one if possible, else
77 get the main loop one. The main loop one is more dangerous as it
78 goes away between packets, the rpc one will stay around for as long
79 as a current RPC lasts.
80 ********************************************************************/
82 TALLOC_CTX *get_talloc_ctx(void)
84 TALLOC_CTX *tc = get_current_rpc_talloc();
88 return main_loop_talloc_get();
91 /*******************************************************************
92 Reads or writes a UTIME type.
93 ********************************************************************/
95 static BOOL smb_io_utime(const char *desc, UTIME *t, prs_struct *ps, int depth)
100 prs_debug(ps, depth, desc, "smb_io_utime");
106 if(!prs_uint32 ("time", ps, depth, &t->time))
112 /*******************************************************************
113 Reads or writes an NTTIME structure.
114 ********************************************************************/
116 BOOL smb_io_time(const char *desc, NTTIME *nttime, prs_struct *ps, int depth)
121 prs_debug(ps, depth, desc, "smb_io_time");
127 if(!prs_uint32("low ", ps, depth, &nttime->low)) /* low part */
129 if(!prs_uint32("high", ps, depth, &nttime->high)) /* high part */
135 /*******************************************************************
136 Reads or writes an NTTIME structure.
137 ********************************************************************/
139 BOOL smb_io_nttime(const char *desc, prs_struct *ps, int depth, NTTIME *nttime)
141 return smb_io_time( desc, nttime, ps, depth );
144 /*******************************************************************
145 Gets an enumeration handle from an ENUM_HND structure.
146 ********************************************************************/
148 uint32 get_enum_hnd(ENUM_HND *enh)
150 return (enh && enh->ptr_hnd != 0) ? enh->handle : 0;
153 /*******************************************************************
154 Inits an ENUM_HND structure.
155 ********************************************************************/
157 void init_enum_hnd(ENUM_HND *enh, uint32 hnd)
159 DEBUG(5,("smb_io_enum_hnd\n"));
161 enh->ptr_hnd = (hnd != 0) ? 1 : 0;
165 /*******************************************************************
166 Reads or writes an ENUM_HND structure.
167 ********************************************************************/
169 BOOL smb_io_enum_hnd(const char *desc, ENUM_HND *hnd, prs_struct *ps, int depth)
174 prs_debug(ps, depth, desc, "smb_io_enum_hnd");
180 if(!prs_uint32("ptr_hnd", ps, depth, &hnd->ptr_hnd)) /* pointer */
183 if (hnd->ptr_hnd != 0) {
184 if(!prs_uint32("handle ", ps, depth, &hnd->handle )) /* enum handle */
191 /*******************************************************************
192 Reads or writes a DOM_SID structure.
193 ********************************************************************/
195 BOOL smb_io_dom_sid(const char *desc, DOM_SID *sid, prs_struct *ps, int depth)
202 prs_debug(ps, depth, desc, "smb_io_dom_sid");
205 if(!prs_uint8 ("sid_rev_num", ps, depth, &sid->sid_rev_num))
208 if(!prs_uint8 ("num_auths ", ps, depth, &sid->num_auths))
211 for (i = 0; i < 6; i++)
214 slprintf(tmp, sizeof(tmp) - 1, "id_auth[%d] ", i);
215 if(!prs_uint8 (tmp, ps, depth, &sid->id_auth[i]))
219 /* oops! XXXX should really issue a warning here... */
220 if (sid->num_auths > MAXSUBAUTHS)
221 sid->num_auths = MAXSUBAUTHS;
223 if(!prs_uint32s(False, "sub_auths ", ps, depth, sid->sub_auths, sid->num_auths))
229 /*******************************************************************
230 Inits a DOM_SID2 structure.
231 ********************************************************************/
233 void init_dom_sid2(DOM_SID2 *sid2, const DOM_SID *sid)
236 sid2->num_auths = sid2->sid.num_auths;
239 /*******************************************************************
240 Reads or writes a DOM_SID2 structure.
241 ********************************************************************/
243 BOOL smb_io_dom_sid2_p(const char *desc, prs_struct *ps, int depth, DOM_SID2 **sid2)
247 /* caputure the pointer value to stream */
249 data_p = *sid2 ? 0xf000baaa : 0;
251 if ( !prs_uint32("dom_sid2_p", ps, depth, &data_p ))
254 /* we're done if there is no data */
259 if (UNMARSHALLING(ps)) {
260 if ( !(*sid2 = PRS_ALLOC_MEM(ps, DOM_SID2, 1)) )
266 /*******************************************************************
267 Reads or writes a DOM_SID2 structure.
268 ********************************************************************/
270 BOOL smb_io_dom_sid2(const char *desc, DOM_SID2 *sid, prs_struct *ps, int depth)
275 prs_debug(ps, depth, desc, "smb_io_dom_sid2");
281 if(!prs_uint32("num_auths", ps, depth, &sid->num_auths))
284 if(!smb_io_dom_sid("sid", &sid->sid, ps, depth))
290 /*******************************************************************
291 Reads or writes a struct uuid
292 ********************************************************************/
294 BOOL smb_io_uuid(const char *desc, struct uuid *uuid,
295 prs_struct *ps, int depth)
300 prs_debug(ps, depth, desc, "smb_io_uuid");
303 if(!prs_uint32 ("data ", ps, depth, &uuid->time_low))
305 if(!prs_uint16 ("data ", ps, depth, &uuid->time_mid))
307 if(!prs_uint16 ("data ", ps, depth, &uuid->time_hi_and_version))
310 if(!prs_uint8s (False, "data ", ps, depth, uuid->clock_seq, sizeof(uuid->clock_seq)))
312 if(!prs_uint8s (False, "data ", ps, depth, uuid->node, sizeof(uuid->node)))
318 /*******************************************************************
319 creates a STRHDR structure.
320 ********************************************************************/
322 void init_str_hdr(STRHDR *hdr, int max_len, int len, uint32 buffer)
324 hdr->str_max_len = max_len;
325 hdr->str_str_len = len;
326 hdr->buffer = buffer;
329 /*******************************************************************
330 Reads or writes a STRHDR structure.
331 ********************************************************************/
333 BOOL smb_io_strhdr(const char *desc, STRHDR *hdr, prs_struct *ps, int depth)
338 prs_debug(ps, depth, desc, "smb_io_strhdr");
343 if(!prs_uint16("str_str_len", ps, depth, &hdr->str_str_len))
345 if(!prs_uint16("str_max_len", ps, depth, &hdr->str_max_len))
347 if(!prs_uint32("buffer ", ps, depth, &hdr->buffer))
353 /*******************************************************************
354 Inits a UNIHDR structure.
355 ********************************************************************/
357 void init_uni_hdr(UNIHDR *hdr, UNISTR2 *str2)
359 hdr->uni_str_len = 2 * (str2->uni_str_len);
360 hdr->uni_max_len = 2 * (str2->uni_max_len);
361 hdr->buffer = (str2->uni_str_len != 0) ? 1 : 0;
364 /*******************************************************************
365 Reads or writes a UNIHDR structure.
366 ********************************************************************/
368 BOOL smb_io_unihdr(const char *desc, UNIHDR *hdr, prs_struct *ps, int depth)
373 prs_debug(ps, depth, desc, "smb_io_unihdr");
379 if(!prs_uint16("uni_str_len", ps, depth, &hdr->uni_str_len))
381 if(!prs_uint16("uni_max_len", ps, depth, &hdr->uni_max_len))
383 if(!prs_uint32("buffer ", ps, depth, &hdr->buffer))
389 /*******************************************************************
390 Inits a BUFHDR structure.
391 ********************************************************************/
393 void init_buf_hdr(BUFHDR *hdr, int max_len, int len)
395 hdr->buf_max_len = max_len;
399 /*******************************************************************
400 prs_uint16 wrapper. Call this and it sets up a pointer to where the
401 uint16 should be stored, or gets the size if reading.
402 ********************************************************************/
404 BOOL smb_io_hdrbuf_pre(const char *desc, BUFHDR *hdr, prs_struct *ps, int depth, uint32 *offset)
406 (*offset) = prs_offset(ps);
411 if(!smb_io_hdrbuf(desc, hdr, ps, depth))
418 if(!prs_set_offset(ps, prs_offset(ps) + (sizeof(uint32) * 2)))
425 /*******************************************************************
426 smb_io_hdrbuf wrapper. Call this and it retrospectively stores the size.
427 Does nothing on reading, as that is already handled by ...._pre()
428 ********************************************************************/
430 BOOL smb_io_hdrbuf_post(const char *desc, BUFHDR *hdr, prs_struct *ps, int depth,
431 uint32 ptr_hdrbuf, uint32 max_len, uint32 len)
434 /* writing: go back and do a retrospective job. i hate this */
436 uint32 old_offset = prs_offset(ps);
438 init_buf_hdr(hdr, max_len, len);
439 if(!prs_set_offset(ps, ptr_hdrbuf))
441 if(!smb_io_hdrbuf(desc, hdr, ps, depth))
444 if(!prs_set_offset(ps, old_offset))
451 /*******************************************************************
452 Reads or writes a BUFHDR structure.
453 ********************************************************************/
455 BOOL smb_io_hdrbuf(const char *desc, BUFHDR *hdr, prs_struct *ps, int depth)
460 prs_debug(ps, depth, desc, "smb_io_hdrbuf");
466 if(!prs_uint32("buf_max_len", ps, depth, &hdr->buf_max_len))
468 if(!prs_uint32("buf_len ", ps, depth, &hdr->buf_len))
474 /*******************************************************************
475 Inits a UNISTR structure.
476 ********************************************************************/
478 void init_unistr(UNISTR *str, const char *buf)
487 len = strlen(buf) + 1;
489 str->buffer = TALLOC_ZERO_ARRAY(get_talloc_ctx(), uint16, len);
490 if (str->buffer == NULL)
491 smb_panic("init_unistr: malloc fail\n");
493 rpcstr_push(str->buffer, buf, len*sizeof(uint16), STR_TERMINATE);
496 /*******************************************************************
497 reads or writes a UNISTR structure.
498 XXXX NOTE: UNISTR structures NEED to be null-terminated.
499 ********************************************************************/
501 BOOL smb_io_unistr(const char *desc, UNISTR *uni, prs_struct *ps, int depth)
506 prs_debug(ps, depth, desc, "smb_io_unistr");
509 if(!prs_unistr("unistr", ps, depth, uni))
515 /*******************************************************************
516 Allocate the RPC_DATA_BLOB memory.
517 ********************************************************************/
519 size_t create_rpc_blob(RPC_DATA_BLOB *str, size_t len)
521 str->buffer = (uint8 *)TALLOC_ZERO(get_talloc_ctx(), len);
522 if (str->buffer == NULL)
523 smb_panic("create_rpc_blob: talloc fail\n");
527 /*******************************************************************
528 Inits a RPC_DATA_BLOB structure from a uint32
529 ********************************************************************/
531 void init_rpc_blob_uint32(RPC_DATA_BLOB *str, uint32 val)
535 /* set up string lengths. */
536 str->buf_len = create_rpc_blob(str, sizeof(uint32));
537 SIVAL(str->buffer, 0, val);
540 /*******************************************************************
541 Inits a RPC_DATA_BLOB structure.
542 ********************************************************************/
544 void init_rpc_blob_str(RPC_DATA_BLOB *str, const char *buf, int len)
548 /* set up string lengths. */
549 str->buf_len = create_rpc_blob(str, len*2);
550 rpcstr_push(str->buffer, buf, (size_t)str->buf_len, STR_TERMINATE);
554 /*******************************************************************
555 Inits a RPC_DATA_BLOB structure from a hex string.
556 ********************************************************************/
558 void init_rpc_blob_hex(RPC_DATA_BLOB *str, const char *buf)
561 str->buf_len = create_rpc_blob(str, strlen(buf));
562 str->buf_len = strhex_to_str((char *)str->buffer, str->buf_len, buf);
565 /*******************************************************************
566 Inits a RPC_DATA_BLOB structure.
567 ********************************************************************/
569 void init_rpc_blob_bytes(RPC_DATA_BLOB *str, uint8 *buf, size_t len)
573 /* max buffer size (allocated size) */
575 len = create_rpc_blob(str, len);
576 memcpy(str->buffer, buf, len);
581 /*******************************************************************
582 reads or writes a BUFFER5 structure.
583 the buf_len member tells you how large the buffer is.
584 ********************************************************************/
585 BOOL smb_io_buffer5(const char *desc, BUFFER5 *buf5, prs_struct *ps, int depth)
587 prs_debug(ps, depth, desc, "smb_io_buffer5");
590 if (buf5 == NULL) return False;
594 if(!prs_uint32("buf_len", ps, depth, &buf5->buf_len))
598 if(!prs_buffer5(True, "buffer" , ps, depth, buf5))
605 /*******************************************************************
606 Inits a REGVAL_BUFFER structure.
607 ********************************************************************/
609 void init_regval_buffer(REGVAL_BUFFER *str, const uint8 *buf, size_t len)
613 /* max buffer size (allocated size) */
614 str->buf_max_len = len;
616 str->buf_len = buf != NULL ? len : 0;
619 SMB_ASSERT(str->buf_max_len >= str->buf_len);
620 str->buffer = (uint16 *)TALLOC_ZERO(get_talloc_ctx(),
622 if (str->buffer == NULL)
623 smb_panic("init_regval_buffer: talloc fail\n");
624 memcpy(str->buffer, buf, str->buf_len);
628 /*******************************************************************
629 Reads or writes a REGVAL_BUFFER structure.
630 the uni_max_len member tells you how large the buffer is.
631 the uni_str_len member tells you how much of the buffer is really used.
632 ********************************************************************/
634 BOOL smb_io_regval_buffer(const char *desc, prs_struct *ps, int depth, REGVAL_BUFFER *buf2)
637 prs_debug(ps, depth, desc, "smb_io_regval_buffer");
643 if(!prs_uint32("buf_max_len", ps, depth, &buf2->buf_max_len))
645 if(!prs_uint32("offset ", ps, depth, &buf2->offset))
647 if(!prs_uint32("buf_len ", ps, depth, &buf2->buf_len))
650 /* buffer advanced by indicated length of string
651 NOT by searching for null-termination */
653 if(!prs_regval_buffer(True, "buffer ", ps, depth, buf2))
659 /*******************************************************************
660 creates a UNISTR2 structure: sets up the buffer, too
661 ********************************************************************/
663 void init_buf_unistr2(UNISTR2 *str, uint32 *ptr, const char *buf)
667 init_unistr2(str, buf, UNI_STR_TERMINATE);
670 init_unistr2(str, NULL, UNI_FLAGS_NONE);
675 /*******************************************************************
676 Copies a UNISTR2 structure.
677 ********************************************************************/
679 void copy_unistr2(UNISTR2 *str, const UNISTR2 *from)
681 if (from->buffer == NULL) {
686 SMB_ASSERT(from->uni_max_len >= from->uni_str_len);
688 str->uni_max_len = from->uni_max_len;
689 str->offset = from->offset;
690 str->uni_str_len = from->uni_str_len;
692 /* the string buffer is allocated to the maximum size
693 (the the length of the source string) to prevent
694 reallocation of memory. */
695 if (str->buffer == NULL) {
696 str->buffer = (uint16 *)TALLOC_ZERO_ARRAY(get_talloc_ctx(), uint16, str->uni_max_len);
697 if ((str->buffer == NULL)) {
698 smb_panic("copy_unistr2: talloc fail\n");
703 /* copy the string */
704 memcpy(str->buffer, from->buffer, str->uni_max_len*sizeof(uint16));
707 /*******************************************************************
708 Creates a STRING2 structure.
709 ********************************************************************/
711 void init_string2(STRING2 *str, const char *buf, size_t max_len, size_t str_len)
713 /* set up string lengths. */
714 SMB_ASSERT(max_len >= str_len);
716 /* Ensure buf is valid if str_len was set. Coverity check. */
717 if (str_len && !buf) {
721 str->str_max_len = max_len;
723 str->str_str_len = str_len;
725 /* store the string */
727 str->buffer = (uint8 *)TALLOC_ZERO(get_talloc_ctx(),
729 if (str->buffer == NULL)
730 smb_panic("init_string2: malloc fail\n");
731 memcpy(str->buffer, buf, str_len);
735 /*******************************************************************
736 Reads or writes a STRING2 structure.
737 XXXX NOTE: STRING2 structures need NOT be null-terminated.
738 the str_str_len member tells you how long the string is;
739 the str_max_len member tells you how large the buffer is.
740 ********************************************************************/
742 BOOL smb_io_string2(const char *desc, STRING2 *str2, uint32 buffer, prs_struct *ps, int depth)
749 prs_debug(ps, depth, desc, "smb_io_string2");
755 if(!prs_uint32("str_max_len", ps, depth, &str2->str_max_len))
757 if(!prs_uint32("offset ", ps, depth, &str2->offset))
759 if(!prs_uint32("str_str_len", ps, depth, &str2->str_str_len))
762 /* buffer advanced by indicated length of string
763 NOT by searching for null-termination */
764 if(!prs_string2(True, "buffer ", ps, depth, str2))
769 prs_debug(ps, depth, desc, "smb_io_string2 - NULL");
771 memset((char *)str2, '\0', sizeof(*str2));
778 /*******************************************************************
779 Inits a UNISTR2 structure.
780 ********************************************************************/
782 void init_unistr2(UNISTR2 *str, const char *buf, enum unistr2_term_codes flags)
785 uint32 num_chars = 0;
788 /* We always null terminate the copy. */
789 len = strlen(buf) + 1;
790 if ( flags == UNI_STR_DBLTERMINATE )
793 /* no buffer -- nothing to do */
794 str->uni_max_len = 0;
796 str->uni_str_len = 0;
802 str->buffer = TALLOC_ZERO_ARRAY(get_talloc_ctx(), uint16, len);
803 if (str->buffer == NULL) {
804 smb_panic("init_unistr2: malloc fail\n");
808 /* Ensure len is the length in *bytes* */
809 len *= sizeof(uint16);
812 * The UNISTR2 must be initialized !!!
816 rpcstr_push((char *)str->buffer, buf, len, STR_TERMINATE);
817 num_chars = strlen_w(str->buffer);
818 if (flags == UNI_STR_TERMINATE || flags == UNI_MAXLEN_TERMINATE) {
821 if ( flags == UNI_STR_DBLTERMINATE )
825 str->uni_max_len = num_chars;
827 str->uni_str_len = num_chars;
828 if ( num_chars && ((flags == UNI_MAXLEN_TERMINATE) || (flags == UNI_BROKEN_NON_NULL)) )
832 /*******************************************************************
833 Inits a UNISTR4 structure.
834 ********************************************************************/
836 void init_unistr4(UNISTR4 *uni4, const char *buf, enum unistr2_term_codes flags)
838 uni4->string = TALLOC_P( get_talloc_ctx(), UNISTR2 );
840 smb_panic("init_unistr4: talloc fail\n");
843 init_unistr2( uni4->string, buf, flags );
845 uni4->length = 2 * (uni4->string->uni_str_len);
846 uni4->size = 2 * (uni4->string->uni_max_len);
849 void init_unistr4_w( TALLOC_CTX *ctx, UNISTR4 *uni4, const smb_ucs2_t *buf )
851 uni4->string = TALLOC_P( ctx, UNISTR2 );
853 smb_panic("init_unistr4_w: talloc fail\n");
856 init_unistr2_w( ctx, uni4->string, buf );
858 uni4->length = 2 * (uni4->string->uni_str_len);
859 uni4->size = 2 * (uni4->string->uni_max_len);
863 * Inits a UNISTR2 structure.
864 * @param ctx talloc context to allocate string on
865 * @param str pointer to string to create
866 * @param buf UCS2 null-terminated buffer to init from
869 void init_unistr2_w(TALLOC_CTX *ctx, UNISTR2 *str, const smb_ucs2_t *buf)
871 uint32 len = buf ? strlen_w(buf) : 0;
875 /* set up string lengths. */
876 str->uni_max_len = len;
878 str->uni_str_len = len;
880 str->buffer = TALLOC_ZERO_ARRAY(ctx, uint16, len + 1);
881 if (str->buffer == NULL) {
882 smb_panic("init_unistr2_w: talloc fail\n");
887 * don't move this test above ! The UNISTR2 must be initialized !!!
893 /* Yes, this is a strncpy( foo, bar, strlen(bar)) - but as
894 long as the buffer above is talloc()ed correctly then this
895 is the correct thing to do */
896 strncpy_w(str->buffer, buf, len + 1);
899 /*******************************************************************
900 Inits a UNISTR2 structure from a UNISTR
901 ********************************************************************/
903 void init_unistr2_from_unistr(UNISTR2 *to, const UNISTR *from)
907 /* the destination UNISTR2 should never be NULL.
908 if it is it is a programming error */
910 /* if the source UNISTR is NULL, then zero out
911 the destination string and return */
913 if ((from == NULL) || (from->buffer == NULL))
916 /* get the length; UNISTR must be NULL terminated */
918 while ((from->buffer)[i]!='\0')
920 i++; /* one more to catch the terminating NULL */
921 /* is this necessary -- jerry? I need to think */
923 /* set up string lengths; uni_max_len is set to i+1
924 because we need to account for the final NULL termination */
929 /* allocate the space and copy the string buffer */
930 to->buffer = TALLOC_ZERO_ARRAY(get_talloc_ctx(), uint16, i);
931 if (to->buffer == NULL)
932 smb_panic("init_unistr2_from_unistr: malloc fail\n");
933 memcpy(to->buffer, from->buffer, i*sizeof(uint16));
937 /*******************************************************************
938 Inits a UNISTR2 structure from a DATA_BLOB.
939 The length of the data_blob must count the bytes of the buffer.
940 Copies the blob data.
941 ********************************************************************/
943 void init_unistr2_from_datablob(UNISTR2 *str, DATA_BLOB *blob)
945 /* Allocs the unistring */
946 init_unistr2(str, NULL, UNI_FLAGS_NONE);
948 /* Sets the values */
949 str->uni_str_len = blob->length / sizeof(uint16);
950 str->uni_max_len = str->uni_str_len;
953 str->buffer = (uint16 *) memdup(blob->data, blob->length);
957 if ((str->buffer == NULL) && (blob->length > 0)) {
958 smb_panic("init_unistr2_from_datablob: malloc fail\n");
962 /*******************************************************************
963 UNISTR2* are a little different in that the pointer and the UNISTR2
964 are not necessarily read/written back to back. So we break it up
965 into 2 separate functions.
966 See SPOOL_USER_1 in include/rpc_spoolss.h for an example.
967 ********************************************************************/
969 BOOL prs_io_unistr2_p(const char *desc, prs_struct *ps, int depth, UNISTR2 **uni2)
973 /* caputure the pointer value to stream */
975 data_p = *uni2 ? 0xf000baaa : 0;
977 if ( !prs_uint32("ptr", ps, depth, &data_p ))
980 /* we're done if there is no data */
985 if (UNMARSHALLING(ps)) {
986 if ( !(*uni2 = PRS_ALLOC_MEM(ps, UNISTR2, 1)) )
993 /*******************************************************************
994 now read/write the actual UNISTR2. Memory for the UNISTR2 (but
995 not UNISTR2.buffer) has been allocated previously by prs_unistr2_p()
996 ********************************************************************/
998 BOOL prs_io_unistr2(const char *desc, prs_struct *ps, int depth, UNISTR2 *uni2 )
1000 /* just return true if there is no pointer to deal with.
1001 the memory must have been previously allocated on unmarshalling
1002 by prs_unistr2_p() */
1007 /* just pass off to smb_io_unstr2() passing the uni2 address as
1008 the pointer (like you would expect) */
1010 return smb_io_unistr2( desc, uni2, uni2 ? 1 : 0, ps, depth );
1013 /*******************************************************************
1014 Reads or writes a UNISTR2 structure.
1015 XXXX NOTE: UNISTR2 structures need NOT be null-terminated.
1016 the uni_str_len member tells you how long the string is;
1017 the uni_max_len member tells you how large the buffer is.
1018 ********************************************************************/
1020 BOOL smb_io_unistr2(const char *desc, UNISTR2 *uni2, uint32 buffer, prs_struct *ps, int depth)
1027 prs_debug(ps, depth, desc, "smb_io_unistr2");
1033 if(!prs_uint32("uni_max_len", ps, depth, &uni2->uni_max_len))
1035 if(!prs_uint32("offset ", ps, depth, &uni2->offset))
1037 if(!prs_uint32("uni_str_len", ps, depth, &uni2->uni_str_len))
1040 /* buffer advanced by indicated length of string
1041 NOT by searching for null-termination */
1042 if(!prs_unistr2(True, "buffer ", ps, depth, uni2))
1047 prs_debug(ps, depth, desc, "smb_io_unistr2 - NULL");
1049 memset((char *)uni2, '\0', sizeof(*uni2));
1056 /*******************************************************************
1057 now read/write UNISTR4
1058 ********************************************************************/
1060 BOOL prs_unistr4(const char *desc, prs_struct *ps, int depth, UNISTR4 *uni4)
1063 prs_debug(ps, depth, desc, "prs_unistr4");
1066 if ( !prs_uint16("length", ps, depth, &uni4->length ))
1068 if ( !prs_uint16("size", ps, depth, &uni4->size ))
1073 if ( !prs_pointer( desc, ps, depth, &ptr, sizeof(UNISTR2), (PRS_POINTER_CAST)prs_io_unistr2 ) )
1076 uni4->string = (UNISTR2 *)ptr;
1081 /*******************************************************************
1082 now read/write UNISTR4 header
1083 ********************************************************************/
1085 BOOL prs_unistr4_hdr(const char *desc, prs_struct *ps, int depth, UNISTR4 *uni4)
1087 prs_debug(ps, depth, desc, "prs_unistr4_hdr");
1090 if ( !prs_uint16("length", ps, depth, &uni4->length) )
1092 if ( !prs_uint16("size", ps, depth, &uni4->size) )
1094 if ( !prs_io_unistr2_p(desc, ps, depth, &uni4->string) )
1100 /*******************************************************************
1101 now read/write UNISTR4 string
1102 ********************************************************************/
1104 BOOL prs_unistr4_str(const char *desc, prs_struct *ps, int depth, UNISTR4 *uni4)
1106 prs_debug(ps, depth, desc, "prs_unistr4_str");
1109 if ( !prs_io_unistr2(desc, ps, depth, uni4->string) )
1115 /*******************************************************************
1116 Reads or writes a UNISTR4_ARRAY structure.
1117 ********************************************************************/
1119 BOOL prs_unistr4_array(const char *desc, prs_struct *ps, int depth, UNISTR4_ARRAY *array )
1123 prs_debug(ps, depth, desc, "prs_unistr4_array");
1126 if(!prs_uint32("count", ps, depth, &array->count))
1129 if ( array->count == 0 )
1132 if (UNMARSHALLING(ps)) {
1133 if ( !(array->strings = TALLOC_ZERO_ARRAY( get_talloc_ctx(), UNISTR4, array->count)) )
1137 /* write the headers and then the actual string buffer */
1139 for ( i=0; i<array->count; i++ ) {
1140 if ( !prs_unistr4_hdr( "string", ps, depth, &array->strings[i]) )
1144 for (i=0;i<array->count;i++) {
1145 if ( !prs_unistr4_str("string", ps, depth, &array->strings[i]) )
1152 /********************************************************************
1153 initialise a UNISTR_ARRAY from a char**
1154 ********************************************************************/
1156 BOOL init_unistr4_array( UNISTR4_ARRAY *array, uint32 count, const char **strings )
1160 array->count = count;
1162 if ( array->count == 0 )
1165 /* allocate memory for the array of UNISTR4 objects */
1167 if ( !(array->strings = TALLOC_ZERO_ARRAY(get_talloc_ctx(), UNISTR4, count )) )
1170 for ( i=0; i<count; i++ )
1171 init_unistr4( &array->strings[i], strings[i], UNI_STR_TERMINATE );
1176 BOOL smb_io_lockout_string_hdr(const char *desc, HDR_LOCKOUT_STRING *hdr_account_lockout, prs_struct *ps, int depth)
1178 prs_debug(ps, depth, desc, "smb_io_lockout_string_hdr");
1184 if(!prs_uint16("size", ps, depth, &hdr_account_lockout->size))
1186 if(!prs_uint16("length", ps, depth, &hdr_account_lockout->length))
1188 if(!prs_uint32("buffer", ps, depth, &hdr_account_lockout->buffer))
1194 BOOL smb_io_account_lockout_str(const char *desc, LOCKOUT_STRING *account_lockout, uint32 buffer, prs_struct *ps, int depth)
1196 prs_debug(ps, depth, desc, "smb_io_account_lockout_string");
1199 if(!prs_uint32("array_size", ps, depth, &account_lockout->array_size))
1202 if(!prs_uint32("offset", ps, depth, &account_lockout->offset))
1204 if(!prs_uint32("length", ps, depth, &account_lockout->length))
1207 if (!prs_uint64("lockout_duration", ps, depth, &account_lockout->lockout_duration))
1209 if (!prs_uint64("reset_count", ps, depth, &account_lockout->reset_count))
1211 if (!prs_uint32("bad_attempt_lockout", ps, depth, &account_lockout->bad_attempt_lockout))
1213 if (!prs_uint32("dummy", ps, depth, &account_lockout->dummy))
1216 if(!prs_uint16s (False, "bindata", ps, depth, &account_lockout->bindata, length))
1223 /*******************************************************************
1224 Inits a DOM_RID structure.
1225 ********************************************************************/
1227 void init_dom_rid(DOM_RID *prid, uint32 rid, uint16 type, uint32 idx)
1231 prid->rid_idx = idx;
1234 /*******************************************************************
1235 Reads or writes a DOM_RID structure.
1236 ********************************************************************/
1238 BOOL smb_io_dom_rid(const char *desc, DOM_RID *rid, prs_struct *ps, int depth)
1243 prs_debug(ps, depth, desc, "smb_io_dom_rid");
1249 if(!prs_uint16("type ", ps, depth, &rid->type))
1253 if(!prs_uint32("rid ", ps, depth, &rid->rid))
1255 if(!prs_uint32("rid_idx", ps, depth, &rid->rid_idx))
1261 /*******************************************************************
1262 Reads or writes a DOM_RID2 structure.
1263 ********************************************************************/
1265 BOOL smb_io_dom_rid2(const char *desc, DOM_RID2 *rid, prs_struct *ps, int depth)
1270 prs_debug(ps, depth, desc, "smb_io_dom_rid2");
1276 if(!prs_uint16("type ", ps, depth, &rid->type))
1280 if(!prs_uint32("rid ", ps, depth, &rid->rid))
1282 if(!prs_uint32("rid_idx", ps, depth, &rid->rid_idx))
1284 if(!prs_uint32("unknown", ps, depth, &rid->unknown))
1291 /*******************************************************************
1292 creates a DOM_RID3 structure.
1293 ********************************************************************/
1295 void init_dom_rid3(DOM_RID3 *rid3, uint32 rid, uint8 type)
1299 rid3->ptr_type = 0x1; /* non-zero, basically. */
1304 /*******************************************************************
1305 reads or writes a DOM_RID3 structure.
1306 ********************************************************************/
1308 BOOL smb_io_dom_rid3(const char *desc, DOM_RID3 *rid3, prs_struct *ps, int depth)
1313 prs_debug(ps, depth, desc, "smb_io_dom_rid3");
1319 if(!prs_uint32("rid ", ps, depth, &rid3->rid))
1321 if(!prs_uint32("type1 ", ps, depth, &rid3->type1))
1323 if(!prs_uint32("ptr_type", ps, depth, &rid3->ptr_type))
1325 if(!prs_uint32("type2 ", ps, depth, &rid3->type2))
1327 if(!prs_uint32("unk ", ps, depth, &rid3->unk))
1333 /*******************************************************************
1334 Inits a DOM_RID4 structure.
1335 ********************************************************************/
1337 void init_dom_rid4(DOM_RID4 *rid4, uint16 unknown, uint16 attr, uint32 rid)
1339 rid4->unknown = unknown;
1344 /*******************************************************************
1345 Inits a DOM_CLNT_SRV structure.
1346 ********************************************************************/
1348 static void init_clnt_srv(DOM_CLNT_SRV *logcln, const char *logon_srv, const char *comp_name)
1350 DEBUG(5,("init_clnt_srv: %d\n", __LINE__));
1352 if (logon_srv != NULL) {
1353 logcln->undoc_buffer = 1;
1354 init_unistr2(&logcln->uni_logon_srv, logon_srv, UNI_STR_TERMINATE);
1356 logcln->undoc_buffer = 0;
1359 if (comp_name != NULL) {
1360 logcln->undoc_buffer2 = 1;
1361 init_unistr2(&logcln->uni_comp_name, comp_name, UNI_STR_TERMINATE);
1363 logcln->undoc_buffer2 = 0;
1367 /*******************************************************************
1368 Inits or writes a DOM_CLNT_SRV structure.
1369 ********************************************************************/
1371 BOOL smb_io_clnt_srv(const char *desc, DOM_CLNT_SRV *logcln, prs_struct *ps, int depth)
1376 prs_debug(ps, depth, desc, "smb_io_clnt_srv");
1382 if(!prs_uint32("undoc_buffer ", ps, depth, &logcln->undoc_buffer))
1385 if (logcln->undoc_buffer != 0) {
1386 if(!smb_io_unistr2("unistr2", &logcln->uni_logon_srv, logcln->undoc_buffer, ps, depth))
1393 if(!prs_uint32("undoc_buffer2", ps, depth, &logcln->undoc_buffer2))
1396 if (logcln->undoc_buffer2 != 0) {
1397 if(!smb_io_unistr2("unistr2", &logcln->uni_comp_name, logcln->undoc_buffer2, ps, depth))
1404 /*******************************************************************
1405 Inits a DOM_LOG_INFO structure.
1406 ********************************************************************/
1408 void init_log_info(DOM_LOG_INFO *loginfo, const char *logon_srv, const char *acct_name,
1409 uint16 sec_chan, const char *comp_name)
1411 DEBUG(5,("make_log_info %d\n", __LINE__));
1413 loginfo->undoc_buffer = 1;
1415 init_unistr2(&loginfo->uni_logon_srv, logon_srv, UNI_STR_TERMINATE);
1416 init_unistr2(&loginfo->uni_acct_name, acct_name, UNI_STR_TERMINATE);
1418 loginfo->sec_chan = sec_chan;
1420 init_unistr2(&loginfo->uni_comp_name, comp_name, UNI_STR_TERMINATE);
1423 /*******************************************************************
1424 Reads or writes a DOM_LOG_INFO structure.
1425 ********************************************************************/
1427 BOOL smb_io_log_info(const char *desc, DOM_LOG_INFO *loginfo, prs_struct *ps, int depth)
1429 if (loginfo == NULL)
1432 prs_debug(ps, depth, desc, "smb_io_log_info");
1438 if(!prs_uint32("undoc_buffer", ps, depth, &loginfo->undoc_buffer))
1441 if(!smb_io_unistr2("unistr2", &loginfo->uni_logon_srv, True, ps, depth))
1443 if(!smb_io_unistr2("unistr2", &loginfo->uni_acct_name, True, ps, depth))
1446 if(!prs_uint16("sec_chan", ps, depth, &loginfo->sec_chan))
1449 if(!smb_io_unistr2("unistr2", &loginfo->uni_comp_name, True, ps, depth))
1455 /*******************************************************************
1456 Reads or writes a DOM_CHAL structure.
1457 ********************************************************************/
1459 BOOL smb_io_chal(const char *desc, DOM_CHAL *chal, prs_struct *ps, int depth)
1464 prs_debug(ps, depth, desc, "smb_io_chal");
1467 if(!prs_uint8s (False, "data", ps, depth, chal->data, 8))
1473 /*******************************************************************
1474 Reads or writes a DOM_CRED structure.
1475 ********************************************************************/
1477 BOOL smb_io_cred(const char *desc, DOM_CRED *cred, prs_struct *ps, int depth)
1482 prs_debug(ps, depth, desc, "smb_io_cred");
1488 if(!smb_io_chal ("", &cred->challenge, ps, depth))
1491 if(!smb_io_utime("", &cred->timestamp, ps, depth))
1497 /*******************************************************************
1498 Inits a DOM_CLNT_INFO2 structure.
1499 ********************************************************************/
1501 void init_clnt_info2(DOM_CLNT_INFO2 *clnt,
1502 const char *logon_srv, const char *comp_name,
1503 const DOM_CRED *clnt_cred)
1505 DEBUG(5,("make_clnt_info: %d\n", __LINE__));
1507 init_clnt_srv(&clnt->login, logon_srv, comp_name);
1509 if (clnt_cred != NULL) {
1511 memcpy(&clnt->cred, clnt_cred, sizeof(clnt->cred));
1517 /*******************************************************************
1518 Reads or writes a DOM_CLNT_INFO2 structure.
1519 ********************************************************************/
1521 BOOL smb_io_clnt_info2(const char *desc, DOM_CLNT_INFO2 *clnt, prs_struct *ps, int depth)
1526 prs_debug(ps, depth, desc, "smb_io_clnt_info2");
1532 if(!smb_io_clnt_srv("", &clnt->login, ps, depth))
1538 if(!prs_uint32("ptr_cred", ps, depth, &clnt->ptr_cred))
1540 if(!smb_io_cred("", &clnt->cred, ps, depth))
1546 /*******************************************************************
1547 Inits a DOM_CLNT_INFO structure.
1548 ********************************************************************/
1550 void init_clnt_info(DOM_CLNT_INFO *clnt,
1551 const char *logon_srv, const char *acct_name,
1552 uint16 sec_chan, const char *comp_name,
1553 const DOM_CRED *cred)
1555 DEBUG(5,("make_clnt_info\n"));
1557 init_log_info(&clnt->login, logon_srv, acct_name, sec_chan, comp_name);
1558 memcpy(&clnt->cred, cred, sizeof(clnt->cred));
1561 /*******************************************************************
1562 Reads or writes a DOM_CLNT_INFO structure.
1563 ********************************************************************/
1565 BOOL smb_io_clnt_info(const char *desc, DOM_CLNT_INFO *clnt, prs_struct *ps, int depth)
1570 prs_debug(ps, depth, desc, "smb_io_clnt_info");
1576 if(!smb_io_log_info("", &clnt->login, ps, depth))
1578 if(!smb_io_cred("", &clnt->cred, ps, depth))
1584 /*******************************************************************
1585 Inits a DOM_LOGON_ID structure.
1586 ********************************************************************/
1588 void init_logon_id(DOM_LOGON_ID *logonid, uint32 log_id_low, uint32 log_id_high)
1590 DEBUG(5,("make_logon_id: %d\n", __LINE__));
1592 logonid->low = log_id_low;
1593 logonid->high = log_id_high;
1596 /*******************************************************************
1597 Reads or writes a DOM_LOGON_ID structure.
1598 ********************************************************************/
1600 BOOL smb_io_logon_id(const char *desc, DOM_LOGON_ID *logonid, prs_struct *ps, int depth)
1602 if (logonid == NULL)
1605 prs_debug(ps, depth, desc, "smb_io_logon_id");
1611 if(!prs_uint32("low ", ps, depth, &logonid->low ))
1613 if(!prs_uint32("high", ps, depth, &logonid->high))
1619 /*******************************************************************
1620 Inits an OWF_INFO structure.
1621 ********************************************************************/
1623 void init_owf_info(OWF_INFO *hash, const uint8 data[16])
1625 DEBUG(5,("init_owf_info: %d\n", __LINE__));
1628 memcpy(hash->data, data, sizeof(hash->data));
1630 memset((char *)hash->data, '\0', sizeof(hash->data));
1633 /*******************************************************************
1634 Reads or writes an OWF_INFO structure.
1635 ********************************************************************/
1637 BOOL smb_io_owf_info(const char *desc, OWF_INFO *hash, prs_struct *ps, int depth)
1642 prs_debug(ps, depth, desc, "smb_io_owf_info");
1648 if(!prs_uint8s (False, "data", ps, depth, hash->data, 16))
1654 /*******************************************************************
1655 Reads or writes a DOM_GID structure.
1656 ********************************************************************/
1658 BOOL smb_io_gid(const char *desc, DOM_GID *gid, prs_struct *ps, int depth)
1663 prs_debug(ps, depth, desc, "smb_io_gid");
1669 if(!prs_uint32("g_rid", ps, depth, &gid->g_rid))
1671 if(!prs_uint32("attr ", ps, depth, &gid->attr))
1677 /*******************************************************************
1678 Reads or writes an POLICY_HND structure.
1679 ********************************************************************/
1681 BOOL smb_io_pol_hnd(const char *desc, POLICY_HND *pol, prs_struct *ps, int depth)
1686 prs_debug(ps, depth, desc, "smb_io_pol_hnd");
1692 if(UNMARSHALLING(ps))
1695 if (!prs_uint32("data1", ps, depth, &pol->data1))
1697 if (!prs_uint32("data2", ps, depth, &pol->data2))
1699 if (!prs_uint16("data3", ps, depth, &pol->data3))
1701 if (!prs_uint16("data4", ps, depth, &pol->data4))
1703 if(!prs_uint8s (False, "data5", ps, depth, pol->data5, sizeof(pol->data5)))
1709 /*******************************************************************
1711 ********************************************************************/
1713 void init_unistr3(UNISTR3 *str, const char *buf)
1717 str->str.buffer = NULL;
1721 str->uni_str_len = strlen(buf) + 1;
1723 str->str.buffer = TALLOC_ZERO_ARRAY(get_talloc_ctx(), uint16, str->uni_str_len);
1724 if (str->str.buffer == NULL)
1725 smb_panic("init_unistr3: malloc fail\n");
1727 rpcstr_push((char *)str->str.buffer, buf, str->uni_str_len * sizeof(uint16), STR_TERMINATE);
1730 /*******************************************************************
1731 Reads or writes a UNISTR3 structure.
1732 ********************************************************************/
1734 BOOL smb_io_unistr3(const char *desc, UNISTR3 *name, prs_struct *ps, int depth)
1739 prs_debug(ps, depth, desc, "smb_io_unistr3");
1745 if(!prs_uint32("uni_str_len", ps, depth, &name->uni_str_len))
1748 /* we're done if there is no string */
1750 if ( name->uni_str_len == 0 )
1753 /* don't know if len is specified by uni_str_len member... */
1754 /* assume unicode string is unicode-null-terminated, instead */
1756 if(!prs_unistr3(True, "unistr", name, ps, depth))
1762 /*******************************************************************
1763 Stream a uint64_struct
1764 ********************************************************************/
1765 BOOL prs_uint64(const char *name, prs_struct *ps, int depth, UINT64_S *data64)
1767 return prs_uint32(name, ps, depth+1, &data64->low) &&
1768 prs_uint32(name, ps, depth+1, &data64->high);
1771 /*******************************************************************
1772 reads or writes a BUFHDR2 structure.
1773 ********************************************************************/
1774 BOOL smb_io_bufhdr2(const char *desc, BUFHDR2 *hdr, prs_struct *ps, int depth)
1776 prs_debug(ps, depth, desc, "smb_io_bufhdr2");
1780 prs_uint32("info_level", ps, depth, &(hdr->info_level));
1781 prs_uint32("length ", ps, depth, &(hdr->length ));
1782 prs_uint32("buffer ", ps, depth, &(hdr->buffer ));
1787 /*******************************************************************
1788 reads or writes a BUFHDR4 structure.
1789 ********************************************************************/
1790 BOOL smb_io_bufhdr4(const char *desc, BUFHDR4 *hdr, prs_struct *ps, int depth)
1792 prs_debug(ps, depth, desc, "smb_io_bufhdr4");
1796 prs_uint32("size", ps, depth, &hdr->size);
1797 prs_uint32("buffer", ps, depth, &hdr->buffer);
1802 /*******************************************************************
1803 reads or writes a RPC_DATA_BLOB structure.
1804 ********************************************************************/
1806 BOOL smb_io_rpc_blob(const char *desc, RPC_DATA_BLOB *blob, prs_struct *ps, int depth)
1808 prs_debug(ps, depth, desc, "smb_io_rpc_blob");
1812 if ( !prs_uint32("buf_len", ps, depth, &blob->buf_len) )
1815 if ( blob->buf_len == 0 )
1818 if (UNMARSHALLING(ps)) {
1819 blob->buffer = PRS_ALLOC_MEM(ps, uint8, blob->buf_len);
1820 if (!blob->buffer) {
1825 if ( !prs_uint8s(True, "buffer", ps, depth, blob->buffer, blob->buf_len) )
1831 /*******************************************************************
1832 creates a UNIHDR structure.
1833 ********************************************************************/
1835 BOOL make_uni_hdr(UNIHDR *hdr, int len)
1841 hdr->uni_str_len = 2 * len;
1842 hdr->uni_max_len = 2 * len;
1843 hdr->buffer = len != 0 ? 1 : 0;
1848 /*******************************************************************
1849 creates a BUFHDR2 structure.
1850 ********************************************************************/
1851 BOOL make_bufhdr2(BUFHDR2 *hdr, uint32 info_level, uint32 length, uint32 buffer)
1853 hdr->info_level = info_level;
1854 hdr->length = length;
1855 hdr->buffer = buffer;
1860 /*******************************************************************
1861 return the length of a UNISTR string.
1862 ********************************************************************/
1864 uint32 str_len_uni(UNISTR *source)
1868 if (!source->buffer)
1871 while (source->buffer[i])