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 3 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, see <http://www.gnu.org/licenses/>.
26 #define DBGC_CLASS DBGC_RPC_PARSE
28 /****************************************************************************
29 A temporary TALLOC context for things like unistrs, that is valid for
30 the life of a complete RPC call.
31 ****************************************************************************/
33 static TALLOC_CTX *current_rpc_talloc = NULL;
35 static TALLOC_CTX *get_current_rpc_talloc(void)
37 return current_rpc_talloc;
40 void set_current_rpc_talloc( TALLOC_CTX *ctx)
42 current_rpc_talloc = ctx;
45 static TALLOC_CTX *main_loop_talloc = NULL;
47 /*******************************************************************
48 free up temporary memory - called from the main loop
49 ********************************************************************/
51 void main_loop_TALLOC_FREE(void)
53 if (!main_loop_talloc)
55 talloc_destroy(main_loop_talloc);
56 main_loop_talloc = NULL;
59 /*******************************************************************
60 Get a talloc context that is freed in the main loop...
61 ********************************************************************/
63 TALLOC_CTX *main_loop_talloc_get(void)
65 if (!main_loop_talloc) {
66 main_loop_talloc = talloc_init("main loop talloc (mainly parse_misc)");
67 if (!main_loop_talloc)
68 smb_panic("main_loop_talloc: malloc fail");
71 return main_loop_talloc;
74 /*******************************************************************
75 Try and get a talloc context. Get the rpc one if possible, else
76 get the main loop one. The main loop one is more dangerous as it
77 goes away between packets, the rpc one will stay around for as long
78 as a current RPC lasts.
79 ********************************************************************/
81 TALLOC_CTX *get_talloc_ctx(void)
83 TALLOC_CTX *tc = get_current_rpc_talloc();
87 return main_loop_talloc_get();
90 /*******************************************************************
91 Reads or writes a UTIME type.
92 ********************************************************************/
94 static BOOL smb_io_utime(const char *desc, UTIME *t, prs_struct *ps, int depth)
99 prs_debug(ps, depth, desc, "smb_io_utime");
105 if(!prs_uint32 ("time", ps, depth, &t->time))
111 /*******************************************************************
112 Reads or writes an NTTIME structure.
113 ********************************************************************/
115 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 (MARSHALLING(ps)) {
128 low = *nttime & 0xFFFFFFFF;
129 high = *nttime >> 32;
132 if(!prs_uint32("low ", ps, depth, &low)) /* low part */
134 if(!prs_uint32("high", ps, depth, &high)) /* high part */
137 if (UNMARSHALLING(ps)) {
138 *nttime = (((uint64_t)high << 32) + low);
144 /*******************************************************************
145 Reads or writes an NTTIME structure.
146 ********************************************************************/
148 BOOL smb_io_nttime(const char *desc, prs_struct *ps, int depth, NTTIME *nttime)
150 return smb_io_time( desc, nttime, ps, depth );
153 /*******************************************************************
154 Reads or writes a DOM_SID structure.
155 ********************************************************************/
157 BOOL smb_io_dom_sid(const char *desc, DOM_SID *sid, prs_struct *ps, int depth)
164 prs_debug(ps, depth, desc, "smb_io_dom_sid");
167 if(!prs_uint8 ("sid_rev_num", ps, depth, &sid->sid_rev_num))
170 if(!prs_uint8 ("num_auths ", ps, depth, &sid->num_auths))
173 for (i = 0; i < 6; i++)
176 slprintf(tmp, sizeof(tmp) - 1, "id_auth[%d] ", i);
177 if(!prs_uint8 (tmp, ps, depth, &sid->id_auth[i]))
181 /* oops! XXXX should really issue a warning here... */
182 if (sid->num_auths > MAXSUBAUTHS)
183 sid->num_auths = MAXSUBAUTHS;
185 if(!prs_uint32s(False, "sub_auths ", ps, depth, sid->sub_auths, sid->num_auths))
191 /*******************************************************************
192 Inits a DOM_SID2 structure.
193 ********************************************************************/
195 void init_dom_sid2(DOM_SID2 *sid2, const DOM_SID *sid)
198 sid2->num_auths = sid2->sid.num_auths;
201 /*******************************************************************
202 Reads or writes a DOM_SID2 structure.
203 ********************************************************************/
205 BOOL smb_io_dom_sid2_p(const char *desc, prs_struct *ps, int depth, DOM_SID2 **sid2)
209 /* caputure the pointer value to stream */
211 data_p = *sid2 ? 0xf000baaa : 0;
213 if ( !prs_uint32("dom_sid2_p", ps, depth, &data_p ))
216 /* we're done if there is no data */
221 if (UNMARSHALLING(ps)) {
222 if ( !(*sid2 = PRS_ALLOC_MEM(ps, DOM_SID2, 1)) )
228 /*******************************************************************
229 Reads or writes a DOM_SID2 structure.
230 ********************************************************************/
232 BOOL smb_io_dom_sid2(const char *desc, DOM_SID2 *sid, prs_struct *ps, int depth)
237 prs_debug(ps, depth, desc, "smb_io_dom_sid2");
243 if(!prs_uint32("num_auths", ps, depth, &sid->num_auths))
246 if(!smb_io_dom_sid("sid", &sid->sid, ps, depth))
252 /*******************************************************************
253 Reads or writes a struct GUID
254 ********************************************************************/
256 BOOL smb_io_uuid(const char *desc, struct GUID *uuid,
257 prs_struct *ps, int depth)
262 prs_debug(ps, depth, desc, "smb_io_uuid");
265 if(!prs_uint32 ("data ", ps, depth, &uuid->time_low))
267 if(!prs_uint16 ("data ", ps, depth, &uuid->time_mid))
269 if(!prs_uint16 ("data ", ps, depth, &uuid->time_hi_and_version))
272 if(!prs_uint8s (False, "data ", ps, depth, uuid->clock_seq, sizeof(uuid->clock_seq)))
274 if(!prs_uint8s (False, "data ", ps, depth, uuid->node, sizeof(uuid->node)))
280 /*******************************************************************
281 creates a STRHDR structure.
282 ********************************************************************/
284 void init_str_hdr(STRHDR *hdr, int max_len, int len, uint32 buffer)
286 hdr->str_max_len = max_len;
287 hdr->str_str_len = len;
288 hdr->buffer = buffer;
291 /*******************************************************************
292 Reads or writes a STRHDR structure.
293 ********************************************************************/
295 BOOL smb_io_strhdr(const char *desc, STRHDR *hdr, prs_struct *ps, int depth)
300 prs_debug(ps, depth, desc, "smb_io_strhdr");
305 if(!prs_uint16("str_str_len", ps, depth, &hdr->str_str_len))
307 if(!prs_uint16("str_max_len", ps, depth, &hdr->str_max_len))
309 if(!prs_uint32("buffer ", ps, depth, &hdr->buffer))
315 /*******************************************************************
316 Inits a UNIHDR structure.
317 ********************************************************************/
319 void init_uni_hdr(UNIHDR *hdr, UNISTR2 *str2)
321 hdr->uni_str_len = 2 * (str2->uni_str_len);
322 hdr->uni_max_len = 2 * (str2->uni_max_len);
323 hdr->buffer = (str2->uni_str_len != 0) ? 1 : 0;
326 /*******************************************************************
327 Reads or writes a UNIHDR structure.
328 ********************************************************************/
330 BOOL smb_io_unihdr(const char *desc, UNIHDR *hdr, prs_struct *ps, int depth)
335 prs_debug(ps, depth, desc, "smb_io_unihdr");
341 if(!prs_uint16("uni_str_len", ps, depth, &hdr->uni_str_len))
343 if(!prs_uint16("uni_max_len", ps, depth, &hdr->uni_max_len))
345 if(!prs_uint32("buffer ", ps, depth, &hdr->buffer))
351 /*******************************************************************
352 Inits a BUFHDR structure.
353 ********************************************************************/
355 void init_buf_hdr(BUFHDR *hdr, int max_len, int len)
357 hdr->buf_max_len = max_len;
361 /*******************************************************************
362 prs_uint16 wrapper. Call this and it sets up a pointer to where the
363 uint16 should be stored, or gets the size if reading.
364 ********************************************************************/
366 BOOL smb_io_hdrbuf_pre(const char *desc, BUFHDR *hdr, prs_struct *ps, int depth, uint32 *offset)
368 (*offset) = prs_offset(ps);
373 if(!smb_io_hdrbuf(desc, hdr, ps, depth))
380 if(!prs_set_offset(ps, prs_offset(ps) + (sizeof(uint32) * 2)))
387 /*******************************************************************
388 smb_io_hdrbuf wrapper. Call this and it retrospectively stores the size.
389 Does nothing on reading, as that is already handled by ...._pre()
390 ********************************************************************/
392 BOOL smb_io_hdrbuf_post(const char *desc, BUFHDR *hdr, prs_struct *ps, int depth,
393 uint32 ptr_hdrbuf, uint32 max_len, uint32 len)
396 /* writing: go back and do a retrospective job. i hate this */
398 uint32 old_offset = prs_offset(ps);
400 init_buf_hdr(hdr, max_len, len);
401 if(!prs_set_offset(ps, ptr_hdrbuf))
403 if(!smb_io_hdrbuf(desc, hdr, ps, depth))
406 if(!prs_set_offset(ps, old_offset))
413 /*******************************************************************
414 Reads or writes a BUFHDR structure.
415 ********************************************************************/
417 BOOL smb_io_hdrbuf(const char *desc, BUFHDR *hdr, prs_struct *ps, int depth)
422 prs_debug(ps, depth, desc, "smb_io_hdrbuf");
428 if(!prs_uint32("buf_max_len", ps, depth, &hdr->buf_max_len))
430 if(!prs_uint32("buf_len ", ps, depth, &hdr->buf_len))
436 /*******************************************************************
437 Inits a UNISTR structure.
438 ********************************************************************/
440 void init_unistr(UNISTR *str, const char *buf)
449 len = strlen(buf) + 1;
452 str->buffer = TALLOC_ZERO_ARRAY(get_talloc_ctx(), uint16, len);
453 if (str->buffer == NULL)
454 smb_panic("init_unistr: malloc fail");
456 rpcstr_push(str->buffer, buf, len*sizeof(uint16), STR_TERMINATE);
462 /*******************************************************************
463 reads or writes a UNISTR structure.
464 XXXX NOTE: UNISTR structures NEED to be null-terminated.
465 ********************************************************************/
467 BOOL smb_io_unistr(const char *desc, UNISTR *uni, prs_struct *ps, int depth)
472 prs_debug(ps, depth, desc, "smb_io_unistr");
475 if(!prs_unistr("unistr", ps, depth, uni))
481 /*******************************************************************
482 Allocate the RPC_DATA_BLOB memory.
483 ********************************************************************/
485 static void create_rpc_blob(RPC_DATA_BLOB *str, size_t len)
488 str->buffer = (uint8 *)TALLOC_ZERO(get_talloc_ctx(), len);
489 if (str->buffer == NULL)
490 smb_panic("create_rpc_blob: talloc fail");
498 /*******************************************************************
499 Inits a RPC_DATA_BLOB structure from a uint32
500 ********************************************************************/
502 void init_rpc_blob_uint32(RPC_DATA_BLOB *str, uint32 val)
506 /* set up string lengths. */
507 create_rpc_blob(str, sizeof(uint32));
508 SIVAL(str->buffer, 0, val);
511 /*******************************************************************
512 Inits a RPC_DATA_BLOB structure.
513 ********************************************************************/
515 void init_rpc_blob_str(RPC_DATA_BLOB *str, const char *buf, int len)
519 /* set up string lengths. */
521 create_rpc_blob(str, len*2);
522 rpcstr_push(str->buffer, buf, (size_t)str->buf_len, STR_TERMINATE);
526 /*******************************************************************
527 Inits a RPC_DATA_BLOB structure from a hex string.
528 ********************************************************************/
530 void init_rpc_blob_hex(RPC_DATA_BLOB *str, const char *buf)
534 create_rpc_blob(str, strlen(buf));
535 str->buf_len = strhex_to_str((char *)str->buffer, str->buf_len, buf);
539 /*******************************************************************
540 Inits a RPC_DATA_BLOB structure.
541 ********************************************************************/
543 void init_rpc_blob_bytes(RPC_DATA_BLOB *str, uint8 *buf, size_t len)
547 /* max buffer size (allocated size) */
548 if (buf != NULL && len) {
549 create_rpc_blob(str, len);
550 memcpy(str->buffer, buf, len);
555 /*******************************************************************
556 reads or writes a BUFFER5 structure.
557 the buf_len member tells you how large the buffer is.
558 ********************************************************************/
559 BOOL smb_io_buffer5(const char *desc, BUFFER5 *buf5, prs_struct *ps, int depth)
561 prs_debug(ps, depth, desc, "smb_io_buffer5");
564 if (buf5 == NULL) return False;
568 if(!prs_uint32("buf_len", ps, depth, &buf5->buf_len))
572 if(!prs_buffer5(True, "buffer" , ps, depth, buf5))
579 /*******************************************************************
580 Inits a REGVAL_BUFFER structure.
581 ********************************************************************/
583 void init_regval_buffer(REGVAL_BUFFER *str, const uint8 *buf, size_t len)
587 /* max buffer size (allocated size) */
588 str->buf_max_len = len;
590 str->buf_len = buf != NULL ? len : 0;
593 SMB_ASSERT(str->buf_max_len >= str->buf_len);
594 str->buffer = (uint16 *)TALLOC_ZERO(get_talloc_ctx(),
596 if (str->buffer == NULL)
597 smb_panic("init_regval_buffer: talloc fail");
598 memcpy(str->buffer, buf, str->buf_len);
602 /*******************************************************************
603 Reads or writes a REGVAL_BUFFER structure.
604 the uni_max_len member tells you how large the buffer is.
605 the uni_str_len member tells you how much of the buffer is really used.
606 ********************************************************************/
608 BOOL smb_io_regval_buffer(const char *desc, prs_struct *ps, int depth, REGVAL_BUFFER *buf2)
611 prs_debug(ps, depth, desc, "smb_io_regval_buffer");
617 if(!prs_uint32("buf_max_len", ps, depth, &buf2->buf_max_len))
619 if(!prs_uint32("offset ", ps, depth, &buf2->offset))
621 if(!prs_uint32("buf_len ", ps, depth, &buf2->buf_len))
624 /* buffer advanced by indicated length of string
625 NOT by searching for null-termination */
627 if(!prs_regval_buffer(True, "buffer ", ps, depth, buf2))
633 /*******************************************************************
634 creates a UNISTR2 structure: sets up the buffer, too
635 ********************************************************************/
637 void init_buf_unistr2(UNISTR2 *str, uint32 *ptr, const char *buf)
641 init_unistr2(str, buf, UNI_STR_TERMINATE);
644 init_unistr2(str, NULL, UNI_FLAGS_NONE);
649 /*******************************************************************
650 Copies a UNISTR2 structure.
651 ********************************************************************/
653 void copy_unistr2(UNISTR2 *str, const UNISTR2 *from)
655 if (from->buffer == NULL) {
660 SMB_ASSERT(from->uni_max_len >= from->uni_str_len);
662 str->uni_max_len = from->uni_max_len;
663 str->offset = from->offset;
664 str->uni_str_len = from->uni_str_len;
666 /* the string buffer is allocated to the maximum size
667 (the the length of the source string) to prevent
668 reallocation of memory. */
669 if (str->buffer == NULL) {
670 if (str->uni_max_len) {
671 str->buffer = (uint16 *)TALLOC_ZERO_ARRAY(get_talloc_ctx(), uint16, str->uni_max_len);
672 if ((str->buffer == NULL)) {
673 smb_panic("copy_unistr2: talloc fail");
676 /* copy the string */
677 memcpy(str->buffer, from->buffer, str->uni_max_len*sizeof(uint16));
684 /*******************************************************************
685 Creates a STRING2 structure.
686 ********************************************************************/
688 void init_string2(STRING2 *str, const char *buf, size_t max_len, size_t str_len)
690 /* set up string lengths. */
691 SMB_ASSERT(max_len >= str_len);
693 /* Ensure buf is valid if str_len was set. Coverity check. */
694 if (str_len && !buf) {
698 str->str_max_len = max_len;
700 str->str_str_len = str_len;
702 /* store the string */
704 str->buffer = (uint8 *)TALLOC_ZERO(get_talloc_ctx(),
706 if (str->buffer == NULL)
707 smb_panic("init_string2: malloc fail");
708 memcpy(str->buffer, buf, str_len);
712 /*******************************************************************
713 Reads or writes a STRING2 structure.
714 XXXX NOTE: STRING2 structures need NOT be null-terminated.
715 the str_str_len member tells you how long the string is;
716 the str_max_len member tells you how large the buffer is.
717 ********************************************************************/
719 BOOL smb_io_string2(const char *desc, STRING2 *str2, uint32 buffer, prs_struct *ps, int depth)
726 prs_debug(ps, depth, desc, "smb_io_string2");
732 if(!prs_uint32("str_max_len", ps, depth, &str2->str_max_len))
734 if(!prs_uint32("offset ", ps, depth, &str2->offset))
736 if(!prs_uint32("str_str_len", ps, depth, &str2->str_str_len))
739 /* buffer advanced by indicated length of string
740 NOT by searching for null-termination */
741 if(!prs_string2(True, "buffer ", ps, depth, str2))
746 prs_debug(ps, depth, desc, "smb_io_string2 - NULL");
748 memset((char *)str2, '\0', sizeof(*str2));
755 /*******************************************************************
756 Inits a UNISTR2 structure.
757 ********************************************************************/
759 void init_unistr2(UNISTR2 *str, const char *buf, enum unistr2_term_codes flags)
762 uint32 num_chars = 0;
765 /* We always null terminate the copy. */
766 len = strlen(buf) + 1;
767 if ( flags == UNI_STR_DBLTERMINATE )
771 if (buf == NULL || len == 0) {
772 /* no buffer -- nothing to do */
773 str->uni_max_len = 0;
775 str->uni_str_len = 0;
781 str->buffer = TALLOC_ZERO_ARRAY(get_talloc_ctx(), uint16, len);
782 if (str->buffer == NULL) {
783 smb_panic("init_unistr2: malloc fail");
787 /* Ensure len is the length in *bytes* */
788 len *= sizeof(uint16);
791 * The UNISTR2 must be initialized !!!
795 rpcstr_push((char *)str->buffer, buf, len, STR_TERMINATE);
796 num_chars = strlen_w(str->buffer);
797 if (flags == UNI_STR_TERMINATE || flags == UNI_MAXLEN_TERMINATE) {
800 if ( flags == UNI_STR_DBLTERMINATE )
804 str->uni_max_len = num_chars;
806 str->uni_str_len = num_chars;
807 if ( num_chars && ((flags == UNI_MAXLEN_TERMINATE) || (flags == UNI_BROKEN_NON_NULL)) )
811 /*******************************************************************
812 Inits a UNISTR4 structure.
813 ********************************************************************/
815 void init_unistr4(UNISTR4 *uni4, const char *buf, enum unistr2_term_codes flags)
817 uni4->string = TALLOC_P( get_talloc_ctx(), UNISTR2 );
819 smb_panic("init_unistr4: talloc fail");
822 init_unistr2( uni4->string, buf, flags );
824 uni4->length = 2 * (uni4->string->uni_str_len);
825 uni4->size = 2 * (uni4->string->uni_max_len);
828 void init_unistr4_w( TALLOC_CTX *ctx, UNISTR4 *uni4, const smb_ucs2_t *buf )
830 uni4->string = TALLOC_P( ctx, UNISTR2 );
832 smb_panic("init_unistr4_w: talloc fail");
835 init_unistr2_w( ctx, uni4->string, buf );
837 uni4->length = 2 * (uni4->string->uni_str_len);
838 uni4->size = 2 * (uni4->string->uni_max_len);
842 * Inits a UNISTR2 structure.
843 * @param ctx talloc context to allocate string on
844 * @param str pointer to string to create
845 * @param buf UCS2 null-terminated buffer to init from
848 void init_unistr2_w(TALLOC_CTX *ctx, UNISTR2 *str, const smb_ucs2_t *buf)
850 uint32 len = buf ? strlen_w(buf) : 0;
854 /* set up string lengths. */
855 str->uni_max_len = len;
857 str->uni_str_len = len;
860 str->buffer = TALLOC_ZERO_ARRAY(ctx, uint16, len + 1);
861 if (str->buffer == NULL) {
862 smb_panic("init_unistr2_w: talloc fail");
870 * don't move this test above ! The UNISTR2 must be initialized !!!
876 /* Yes, this is a strncpy( foo, bar, strlen(bar)) - but as
877 long as the buffer above is talloc()ed correctly then this
878 is the correct thing to do */
880 strncpy_w(str->buffer, buf, len + 1);
884 /*******************************************************************
885 Inits a UNISTR2 structure from a UNISTR
886 ********************************************************************/
888 void init_unistr2_from_unistr(UNISTR2 *to, const UNISTR *from)
892 /* the destination UNISTR2 should never be NULL.
893 if it is it is a programming error */
895 /* if the source UNISTR is NULL, then zero out
896 the destination string and return */
898 if ((from == NULL) || (from->buffer == NULL))
901 /* get the length; UNISTR must be NULL terminated */
903 while ((from->buffer)[i]!='\0')
905 i++; /* one more to catch the terminating NULL */
906 /* is this necessary -- jerry? I need to think */
908 /* set up string lengths; uni_max_len is set to i+1
909 because we need to account for the final NULL termination */
914 /* allocate the space and copy the string buffer */
916 to->buffer = TALLOC_ZERO_ARRAY(get_talloc_ctx(), uint16, i);
917 if (to->buffer == NULL)
918 smb_panic("init_unistr2_from_unistr: malloc fail");
919 memcpy(to->buffer, from->buffer, i*sizeof(uint16));
926 /*******************************************************************
927 Inits a UNISTR2 structure from a DATA_BLOB.
928 The length of the data_blob must count the bytes of the buffer.
929 Copies the blob data.
930 ********************************************************************/
932 void init_unistr2_from_datablob(UNISTR2 *str, DATA_BLOB *blob)
934 /* Allocs the unistring */
935 init_unistr2(str, NULL, UNI_FLAGS_NONE);
937 /* Sets the values */
938 str->uni_str_len = blob->length / sizeof(uint16);
939 str->uni_max_len = str->uni_str_len;
942 str->buffer = (uint16 *) memdup(blob->data, blob->length);
946 if ((str->buffer == NULL) && (blob->length > 0)) {
947 smb_panic("init_unistr2_from_datablob: malloc fail");
951 /*******************************************************************
952 UNISTR2* are a little different in that the pointer and the UNISTR2
953 are not necessarily read/written back to back. So we break it up
954 into 2 separate functions.
955 See SPOOL_USER_1 in include/rpc_spoolss.h for an example.
956 ********************************************************************/
958 BOOL prs_io_unistr2_p(const char *desc, prs_struct *ps, int depth, UNISTR2 **uni2)
962 /* caputure the pointer value to stream */
964 data_p = *uni2 ? 0xf000baaa : 0;
966 if ( !prs_uint32("ptr", ps, depth, &data_p ))
969 /* we're done if there is no data */
974 if (UNMARSHALLING(ps)) {
975 if ( !(*uni2 = PRS_ALLOC_MEM(ps, UNISTR2, 1)) )
982 /*******************************************************************
983 now read/write the actual UNISTR2. Memory for the UNISTR2 (but
984 not UNISTR2.buffer) has been allocated previously by prs_unistr2_p()
985 ********************************************************************/
987 BOOL prs_io_unistr2(const char *desc, prs_struct *ps, int depth, UNISTR2 *uni2 )
989 /* just return true if there is no pointer to deal with.
990 the memory must have been previously allocated on unmarshalling
991 by prs_unistr2_p() */
996 /* just pass off to smb_io_unstr2() passing the uni2 address as
997 the pointer (like you would expect) */
999 return smb_io_unistr2( desc, uni2, uni2 ? 1 : 0, ps, depth );
1002 /*******************************************************************
1003 Reads or writes a UNISTR2 structure.
1004 XXXX NOTE: UNISTR2 structures need NOT be null-terminated.
1005 the uni_str_len member tells you how long the string is;
1006 the uni_max_len member tells you how large the buffer is.
1007 ********************************************************************/
1009 BOOL smb_io_unistr2(const char *desc, UNISTR2 *uni2, uint32 buffer, prs_struct *ps, int depth)
1016 prs_debug(ps, depth, desc, "smb_io_unistr2");
1022 if(!prs_uint32("uni_max_len", ps, depth, &uni2->uni_max_len))
1024 if(!prs_uint32("offset ", ps, depth, &uni2->offset))
1026 if(!prs_uint32("uni_str_len", ps, depth, &uni2->uni_str_len))
1029 /* buffer advanced by indicated length of string
1030 NOT by searching for null-termination */
1031 if(!prs_unistr2(True, "buffer ", ps, depth, uni2))
1036 prs_debug(ps, depth, desc, "smb_io_unistr2 - NULL");
1038 memset((char *)uni2, '\0', sizeof(*uni2));
1045 /*******************************************************************
1046 now read/write UNISTR4
1047 ********************************************************************/
1049 BOOL prs_unistr4(const char *desc, prs_struct *ps, int depth, UNISTR4 *uni4)
1052 prs_debug(ps, depth, desc, "prs_unistr4");
1055 if ( !prs_uint16("length", ps, depth, &uni4->length ))
1057 if ( !prs_uint16("size", ps, depth, &uni4->size ))
1062 if ( !prs_pointer( desc, ps, depth, &ptr, sizeof(UNISTR2), (PRS_POINTER_CAST)prs_io_unistr2 ) )
1065 uni4->string = (UNISTR2 *)ptr;
1070 /*******************************************************************
1071 now read/write UNISTR4 header
1072 ********************************************************************/
1074 BOOL prs_unistr4_hdr(const char *desc, prs_struct *ps, int depth, UNISTR4 *uni4)
1076 prs_debug(ps, depth, desc, "prs_unistr4_hdr");
1079 if ( !prs_uint16("length", ps, depth, &uni4->length) )
1081 if ( !prs_uint16("size", ps, depth, &uni4->size) )
1083 if ( !prs_io_unistr2_p(desc, ps, depth, &uni4->string) )
1089 /*******************************************************************
1090 now read/write UNISTR4 string
1091 ********************************************************************/
1093 BOOL prs_unistr4_str(const char *desc, prs_struct *ps, int depth, UNISTR4 *uni4)
1095 prs_debug(ps, depth, desc, "prs_unistr4_str");
1098 if ( !prs_io_unistr2(desc, ps, depth, uni4->string) )
1104 /*******************************************************************
1105 Reads or writes a UNISTR4_ARRAY structure.
1106 ********************************************************************/
1108 BOOL prs_unistr4_array(const char *desc, prs_struct *ps, int depth, UNISTR4_ARRAY *array )
1112 prs_debug(ps, depth, desc, "prs_unistr4_array");
1115 if(!prs_uint32("count", ps, depth, &array->count))
1118 if (UNMARSHALLING(ps)) {
1120 if ( !(array->strings = TALLOC_ZERO_ARRAY( get_talloc_ctx(), UNISTR4, array->count)) )
1123 array->strings = NULL;
1127 /* write the headers and then the actual string buffer */
1129 for ( i=0; i<array->count; i++ ) {
1130 if ( !prs_unistr4_hdr( "string", ps, depth, &array->strings[i]) )
1134 for (i=0;i<array->count;i++) {
1135 if ( !prs_unistr4_str("string", ps, depth, &array->strings[i]) )
1142 /********************************************************************
1143 initialise a UNISTR_ARRAY from a char**
1144 ********************************************************************/
1146 BOOL init_unistr4_array( UNISTR4_ARRAY *array, uint32 count, const char **strings )
1150 array->count = count;
1152 /* allocate memory for the array of UNISTR4 objects */
1155 if ( !(array->strings = TALLOC_ZERO_ARRAY(get_talloc_ctx(), UNISTR4, count )) )
1158 array->strings = NULL;
1161 for ( i=0; i<count; i++ )
1162 init_unistr4( &array->strings[i], strings[i], UNI_STR_TERMINATE );
1167 BOOL smb_io_lockout_string_hdr(const char *desc, HDR_LOCKOUT_STRING *hdr_account_lockout, prs_struct *ps, int depth)
1169 prs_debug(ps, depth, desc, "smb_io_lockout_string_hdr");
1175 if(!prs_uint16("size", ps, depth, &hdr_account_lockout->size))
1177 if(!prs_uint16("length", ps, depth, &hdr_account_lockout->length))
1179 if(!prs_uint32("buffer", ps, depth, &hdr_account_lockout->buffer))
1185 BOOL smb_io_account_lockout_str(const char *desc, LOCKOUT_STRING *account_lockout, uint32 buffer, prs_struct *ps, int depth)
1187 prs_debug(ps, depth, desc, "smb_io_account_lockout_string");
1190 if(!prs_uint32("array_size", ps, depth, &account_lockout->array_size))
1193 if(!prs_uint32("offset", ps, depth, &account_lockout->offset))
1195 if(!prs_uint32("length", ps, depth, &account_lockout->length))
1198 if (!prs_uint64("lockout_duration", ps, depth, &account_lockout->lockout_duration))
1200 if (!prs_uint64("reset_count", ps, depth, &account_lockout->reset_count))
1202 if (!prs_uint32("bad_attempt_lockout", ps, depth, &account_lockout->bad_attempt_lockout))
1204 if (!prs_uint32("dummy", ps, depth, &account_lockout->dummy))
1207 if(!prs_uint16s (False, "bindata", ps, depth, &account_lockout->bindata, length))
1214 /*******************************************************************
1215 Inits a DOM_RID structure.
1216 ********************************************************************/
1218 void init_dom_rid(DOM_RID *prid, uint32 rid, uint16 type, uint32 idx)
1222 prid->rid_idx = idx;
1225 /*******************************************************************
1226 Reads or writes a DOM_RID structure.
1227 ********************************************************************/
1229 BOOL smb_io_dom_rid(const char *desc, DOM_RID *rid, prs_struct *ps, int depth)
1234 prs_debug(ps, depth, desc, "smb_io_dom_rid");
1240 if(!prs_uint16("type ", ps, depth, &rid->type))
1244 if(!prs_uint32("rid ", ps, depth, &rid->rid))
1246 if(!prs_uint32("rid_idx", ps, depth, &rid->rid_idx))
1252 /*******************************************************************
1253 Reads or writes a DOM_RID2 structure.
1254 ********************************************************************/
1256 BOOL smb_io_dom_rid2(const char *desc, DOM_RID2 *rid, prs_struct *ps, int depth)
1261 prs_debug(ps, depth, desc, "smb_io_dom_rid2");
1267 if(!prs_uint16("type ", ps, depth, &rid->type))
1271 if(!prs_uint32("rid ", ps, depth, &rid->rid))
1273 if(!prs_uint32("rid_idx", ps, depth, &rid->rid_idx))
1275 if(!prs_uint32("unknown", ps, depth, &rid->unknown))
1282 /*******************************************************************
1283 creates a DOM_RID3 structure.
1284 ********************************************************************/
1286 void init_dom_rid3(DOM_RID3 *rid3, uint32 rid, uint8 type)
1290 rid3->ptr_type = 0x1; /* non-zero, basically. */
1295 /*******************************************************************
1296 reads or writes a DOM_RID3 structure.
1297 ********************************************************************/
1299 BOOL smb_io_dom_rid3(const char *desc, DOM_RID3 *rid3, prs_struct *ps, int depth)
1304 prs_debug(ps, depth, desc, "smb_io_dom_rid3");
1310 if(!prs_uint32("rid ", ps, depth, &rid3->rid))
1312 if(!prs_uint32("type1 ", ps, depth, &rid3->type1))
1314 if(!prs_uint32("ptr_type", ps, depth, &rid3->ptr_type))
1316 if(!prs_uint32("type2 ", ps, depth, &rid3->type2))
1318 if(!prs_uint32("unk ", ps, depth, &rid3->unk))
1324 /*******************************************************************
1325 Inits a DOM_RID4 structure.
1326 ********************************************************************/
1328 void init_dom_rid4(DOM_RID4 *rid4, uint16 unknown, uint16 attr, uint32 rid)
1330 rid4->unknown = unknown;
1335 /*******************************************************************
1336 Inits a DOM_CLNT_SRV structure.
1337 ********************************************************************/
1339 void init_clnt_srv(DOM_CLNT_SRV *logcln, const char *logon_srv,
1340 const char *comp_name)
1342 DEBUG(5,("init_clnt_srv: %d\n", __LINE__));
1344 if (logon_srv != NULL) {
1345 logcln->undoc_buffer = 1;
1346 init_unistr2(&logcln->uni_logon_srv, logon_srv, UNI_STR_TERMINATE);
1348 logcln->undoc_buffer = 0;
1351 if (comp_name != NULL) {
1352 logcln->undoc_buffer2 = 1;
1353 init_unistr2(&logcln->uni_comp_name, comp_name, UNI_STR_TERMINATE);
1355 logcln->undoc_buffer2 = 0;
1359 /*******************************************************************
1360 Inits or writes a DOM_CLNT_SRV structure.
1361 ********************************************************************/
1363 BOOL smb_io_clnt_srv(const char *desc, DOM_CLNT_SRV *logcln, prs_struct *ps, int depth)
1368 prs_debug(ps, depth, desc, "smb_io_clnt_srv");
1374 if(!prs_uint32("undoc_buffer ", ps, depth, &logcln->undoc_buffer))
1377 if (logcln->undoc_buffer != 0) {
1378 if(!smb_io_unistr2("unistr2", &logcln->uni_logon_srv, logcln->undoc_buffer, ps, depth))
1385 if(!prs_uint32("undoc_buffer2", ps, depth, &logcln->undoc_buffer2))
1388 if (logcln->undoc_buffer2 != 0) {
1389 if(!smb_io_unistr2("unistr2", &logcln->uni_comp_name, logcln->undoc_buffer2, ps, depth))
1396 /*******************************************************************
1397 Inits a DOM_LOG_INFO structure.
1398 ********************************************************************/
1400 void init_log_info(DOM_LOG_INFO *loginfo, const char *logon_srv, const char *acct_name,
1401 uint16 sec_chan, const char *comp_name)
1403 DEBUG(5,("make_log_info %d\n", __LINE__));
1405 loginfo->undoc_buffer = 1;
1407 init_unistr2(&loginfo->uni_logon_srv, logon_srv, UNI_STR_TERMINATE);
1408 init_unistr2(&loginfo->uni_acct_name, acct_name, UNI_STR_TERMINATE);
1410 loginfo->sec_chan = sec_chan;
1412 init_unistr2(&loginfo->uni_comp_name, comp_name, UNI_STR_TERMINATE);
1415 /*******************************************************************
1416 Reads or writes a DOM_LOG_INFO structure.
1417 ********************************************************************/
1419 BOOL smb_io_log_info(const char *desc, DOM_LOG_INFO *loginfo, prs_struct *ps, int depth)
1421 if (loginfo == NULL)
1424 prs_debug(ps, depth, desc, "smb_io_log_info");
1430 if(!prs_uint32("undoc_buffer", ps, depth, &loginfo->undoc_buffer))
1433 if(!smb_io_unistr2("unistr2", &loginfo->uni_logon_srv, True, ps, depth))
1435 if(!smb_io_unistr2("unistr2", &loginfo->uni_acct_name, True, ps, depth))
1438 if(!prs_uint16("sec_chan", ps, depth, &loginfo->sec_chan))
1441 if(!smb_io_unistr2("unistr2", &loginfo->uni_comp_name, True, ps, depth))
1447 /*******************************************************************
1448 Reads or writes a DOM_CHAL structure.
1449 ********************************************************************/
1451 BOOL smb_io_chal(const char *desc, DOM_CHAL *chal, prs_struct *ps, int depth)
1456 prs_debug(ps, depth, desc, "smb_io_chal");
1459 if(!prs_uint8s (False, "data", ps, depth, chal->data, 8))
1465 /*******************************************************************
1466 Reads or writes a DOM_CRED structure.
1467 ********************************************************************/
1469 BOOL smb_io_cred(const char *desc, DOM_CRED *cred, prs_struct *ps, int depth)
1474 prs_debug(ps, depth, desc, "smb_io_cred");
1480 if(!smb_io_chal ("", &cred->challenge, ps, depth))
1483 if(!smb_io_utime("", &cred->timestamp, ps, depth))
1489 /*******************************************************************
1490 Inits a DOM_CLNT_INFO2 structure.
1491 ********************************************************************/
1493 void init_clnt_info2(DOM_CLNT_INFO2 *clnt,
1494 const char *logon_srv, const char *comp_name,
1495 const DOM_CRED *clnt_cred)
1497 DEBUG(5,("make_clnt_info: %d\n", __LINE__));
1499 init_clnt_srv(&clnt->login, logon_srv, comp_name);
1501 if (clnt_cred != NULL) {
1503 memcpy(&clnt->cred, clnt_cred, sizeof(clnt->cred));
1509 /*******************************************************************
1510 Reads or writes a DOM_CLNT_INFO2 structure.
1511 ********************************************************************/
1513 BOOL smb_io_clnt_info2(const char *desc, DOM_CLNT_INFO2 *clnt, prs_struct *ps, int depth)
1518 prs_debug(ps, depth, desc, "smb_io_clnt_info2");
1524 if(!smb_io_clnt_srv("", &clnt->login, ps, depth))
1530 if(!prs_uint32("ptr_cred", ps, depth, &clnt->ptr_cred))
1532 if(!smb_io_cred("", &clnt->cred, ps, depth))
1538 /*******************************************************************
1539 Inits a DOM_CLNT_INFO structure.
1540 ********************************************************************/
1542 void init_clnt_info(DOM_CLNT_INFO *clnt,
1543 const char *logon_srv, const char *acct_name,
1544 uint16 sec_chan, const char *comp_name,
1545 const DOM_CRED *cred)
1547 DEBUG(5,("make_clnt_info\n"));
1549 init_log_info(&clnt->login, logon_srv, acct_name, sec_chan, comp_name);
1550 memcpy(&clnt->cred, cred, sizeof(clnt->cred));
1553 /*******************************************************************
1554 Reads or writes a DOM_CLNT_INFO structure.
1555 ********************************************************************/
1557 BOOL smb_io_clnt_info(const char *desc, DOM_CLNT_INFO *clnt, prs_struct *ps, int depth)
1562 prs_debug(ps, depth, desc, "smb_io_clnt_info");
1568 if(!smb_io_log_info("", &clnt->login, ps, depth))
1570 if(!smb_io_cred("", &clnt->cred, ps, depth))
1576 /*******************************************************************
1577 Inits a DOM_LOGON_ID structure.
1578 ********************************************************************/
1580 void init_logon_id(DOM_LOGON_ID *logonid, uint32 log_id_low, uint32 log_id_high)
1582 DEBUG(5,("make_logon_id: %d\n", __LINE__));
1584 logonid->low = log_id_low;
1585 logonid->high = log_id_high;
1588 /*******************************************************************
1589 Reads or writes a DOM_LOGON_ID structure.
1590 ********************************************************************/
1592 BOOL smb_io_logon_id(const char *desc, DOM_LOGON_ID *logonid, prs_struct *ps, int depth)
1594 if (logonid == NULL)
1597 prs_debug(ps, depth, desc, "smb_io_logon_id");
1603 if(!prs_uint32("low ", ps, depth, &logonid->low ))
1605 if(!prs_uint32("high", ps, depth, &logonid->high))
1611 /*******************************************************************
1612 Inits an OWF_INFO structure.
1613 ********************************************************************/
1615 void init_owf_info(OWF_INFO *hash, const uint8 data[16])
1617 DEBUG(5,("init_owf_info: %d\n", __LINE__));
1620 memcpy(hash->data, data, sizeof(hash->data));
1622 memset((char *)hash->data, '\0', sizeof(hash->data));
1625 /*******************************************************************
1626 Reads or writes an OWF_INFO structure.
1627 ********************************************************************/
1629 BOOL smb_io_owf_info(const char *desc, OWF_INFO *hash, prs_struct *ps, int depth)
1634 prs_debug(ps, depth, desc, "smb_io_owf_info");
1640 if(!prs_uint8s (False, "data", ps, depth, hash->data, 16))
1646 /*******************************************************************
1647 Reads or writes a DOM_GID structure.
1648 ********************************************************************/
1650 BOOL smb_io_gid(const char *desc, DOM_GID *gid, prs_struct *ps, int depth)
1655 prs_debug(ps, depth, desc, "smb_io_gid");
1661 if(!prs_uint32("g_rid", ps, depth, &gid->g_rid))
1663 if(!prs_uint32("attr ", ps, depth, &gid->attr))
1669 /*******************************************************************
1670 Reads or writes an POLICY_HND structure.
1671 ********************************************************************/
1673 BOOL smb_io_pol_hnd(const char *desc, POLICY_HND *pol, prs_struct *ps, int depth)
1678 prs_debug(ps, depth, desc, "smb_io_pol_hnd");
1684 if(UNMARSHALLING(ps))
1687 if (!prs_uint32("handle_type", ps, depth, &pol->handle_type))
1689 if (!smb_io_uuid("uuid", (struct GUID*)&pol->uuid, ps, depth))
1695 /*******************************************************************
1697 ********************************************************************/
1699 void init_unistr3(UNISTR3 *str, const char *buf)
1703 str->str.buffer = NULL;
1707 str->uni_str_len = strlen(buf) + 1;
1709 if (str->uni_str_len) {
1710 str->str.buffer = TALLOC_ZERO_ARRAY(get_talloc_ctx(), uint16, str->uni_str_len);
1711 if (str->str.buffer == NULL)
1712 smb_panic("init_unistr3: malloc fail");
1714 rpcstr_push((char *)str->str.buffer, buf, str->uni_str_len * sizeof(uint16), STR_TERMINATE);
1716 str->str.buffer = NULL;
1720 /*******************************************************************
1721 Reads or writes a UNISTR3 structure.
1722 ********************************************************************/
1724 BOOL smb_io_unistr3(const char *desc, UNISTR3 *name, prs_struct *ps, int depth)
1729 prs_debug(ps, depth, desc, "smb_io_unistr3");
1735 if(!prs_uint32("uni_str_len", ps, depth, &name->uni_str_len))
1738 /* we're done if there is no string */
1740 if ( name->uni_str_len == 0 )
1743 /* don't know if len is specified by uni_str_len member... */
1744 /* assume unicode string is unicode-null-terminated, instead */
1746 if(!prs_unistr3(True, "unistr", name, ps, depth))
1752 /*******************************************************************
1753 Stream a uint64_struct
1754 ********************************************************************/
1755 BOOL prs_uint64(const char *name, prs_struct *ps, int depth, uint64 *data64)
1757 if (UNMARSHALLING(ps)) {
1760 if (!prs_uint32(name, ps, depth+1, &low))
1763 if (!prs_uint32(name, ps, depth+1, &high))
1766 *data64 = ((uint64_t)high << 32) + low;
1770 uint32 high = (*data64) >> 32, low = (*data64) & 0xFFFFFFFF;
1771 return prs_uint32(name, ps, depth+1, &low) &&
1772 prs_uint32(name, ps, depth+1, &high);
1776 /*******************************************************************
1777 reads or writes a BUFHDR2 structure.
1778 ********************************************************************/
1779 BOOL smb_io_bufhdr2(const char *desc, BUFHDR2 *hdr, prs_struct *ps, int depth)
1781 prs_debug(ps, depth, desc, "smb_io_bufhdr2");
1785 prs_uint32("info_level", ps, depth, &(hdr->info_level));
1786 prs_uint32("length ", ps, depth, &(hdr->length ));
1787 prs_uint32("buffer ", ps, depth, &(hdr->buffer ));
1792 /*******************************************************************
1793 reads or writes a BUFHDR4 structure.
1794 ********************************************************************/
1795 BOOL smb_io_bufhdr4(const char *desc, BUFHDR4 *hdr, prs_struct *ps, int depth)
1797 prs_debug(ps, depth, desc, "smb_io_bufhdr4");
1801 prs_uint32("size", ps, depth, &hdr->size);
1802 prs_uint32("buffer", ps, depth, &hdr->buffer);
1807 /*******************************************************************
1808 reads or writes a RPC_DATA_BLOB structure.
1809 ********************************************************************/
1811 BOOL smb_io_rpc_blob(const char *desc, RPC_DATA_BLOB *blob, prs_struct *ps, int depth)
1813 prs_debug(ps, depth, desc, "smb_io_rpc_blob");
1817 if ( !prs_uint32("buf_len", ps, depth, &blob->buf_len) )
1820 if ( blob->buf_len == 0 )
1823 if (UNMARSHALLING(ps)) {
1824 blob->buffer = PRS_ALLOC_MEM(ps, uint8, blob->buf_len);
1825 if (!blob->buffer) {
1830 if ( !prs_uint8s(True, "buffer", ps, depth, blob->buffer, blob->buf_len) )
1836 /*******************************************************************
1837 creates a UNIHDR structure.
1838 ********************************************************************/
1840 BOOL make_uni_hdr(UNIHDR *hdr, int len)
1846 hdr->uni_str_len = 2 * len;
1847 hdr->uni_max_len = 2 * len;
1848 hdr->buffer = len != 0 ? 1 : 0;
1853 /*******************************************************************
1854 creates a BUFHDR2 structure.
1855 ********************************************************************/
1856 BOOL make_bufhdr2(BUFHDR2 *hdr, uint32 info_level, uint32 length, uint32 buffer)
1858 hdr->info_level = info_level;
1859 hdr->length = length;
1860 hdr->buffer = buffer;
1865 /*******************************************************************
1866 return the length of a UNISTR string.
1867 ********************************************************************/
1869 uint32 str_len_uni(UNISTR *source)
1873 if (!source->buffer)
1876 while (source->buffer[i])