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)
122 prs_debug(ps, depth, desc, "smb_io_time");
128 if (MARSHALLING(ps)) {
129 low = *nttime & 0xFFFFFFFF;
130 high = *nttime >> 32;
133 if(!prs_uint32("low ", ps, depth, &low)) /* low part */
135 if(!prs_uint32("high", ps, depth, &high)) /* high part */
138 if (UNMARSHALLING(ps)) {
139 *nttime = (((uint64_t)high << 32) + low);
145 /*******************************************************************
146 Reads or writes an NTTIME structure.
147 ********************************************************************/
149 BOOL smb_io_nttime(const char *desc, prs_struct *ps, int depth, NTTIME *nttime)
151 return smb_io_time( desc, nttime, ps, depth );
154 /*******************************************************************
155 Reads or writes a DOM_SID structure.
156 ********************************************************************/
158 BOOL smb_io_dom_sid(const char *desc, DOM_SID *sid, prs_struct *ps, int depth)
165 prs_debug(ps, depth, desc, "smb_io_dom_sid");
168 if(!prs_uint8 ("sid_rev_num", ps, depth, &sid->sid_rev_num))
171 if(!prs_uint8 ("num_auths ", ps, depth, &sid->num_auths))
174 for (i = 0; i < 6; i++)
177 slprintf(tmp, sizeof(tmp) - 1, "id_auth[%d] ", i);
178 if(!prs_uint8 (tmp, ps, depth, &sid->id_auth[i]))
182 /* oops! XXXX should really issue a warning here... */
183 if (sid->num_auths > MAXSUBAUTHS)
184 sid->num_auths = MAXSUBAUTHS;
186 if(!prs_uint32s(False, "sub_auths ", ps, depth, sid->sub_auths, sid->num_auths))
192 /*******************************************************************
193 Inits a DOM_SID2 structure.
194 ********************************************************************/
196 void init_dom_sid2(DOM_SID2 *sid2, const DOM_SID *sid)
199 sid2->num_auths = sid2->sid.num_auths;
202 /*******************************************************************
203 Reads or writes a DOM_SID2 structure.
204 ********************************************************************/
206 BOOL smb_io_dom_sid2_p(const char *desc, prs_struct *ps, int depth, DOM_SID2 **sid2)
210 /* caputure the pointer value to stream */
212 data_p = *sid2 ? 0xf000baaa : 0;
214 if ( !prs_uint32("dom_sid2_p", ps, depth, &data_p ))
217 /* we're done if there is no data */
222 if (UNMARSHALLING(ps)) {
223 if ( !(*sid2 = PRS_ALLOC_MEM(ps, DOM_SID2, 1)) )
229 /*******************************************************************
230 Reads or writes a DOM_SID2 structure.
231 ********************************************************************/
233 BOOL smb_io_dom_sid2(const char *desc, DOM_SID2 *sid, prs_struct *ps, int depth)
238 prs_debug(ps, depth, desc, "smb_io_dom_sid2");
244 if(!prs_uint32("num_auths", ps, depth, &sid->num_auths))
247 if(!smb_io_dom_sid("sid", &sid->sid, ps, depth))
253 /*******************************************************************
254 Reads or writes a struct GUID
255 ********************************************************************/
257 BOOL smb_io_uuid(const char *desc, struct GUID *uuid,
258 prs_struct *ps, int depth)
263 prs_debug(ps, depth, desc, "smb_io_uuid");
266 if(!prs_uint32 ("data ", ps, depth, &uuid->time_low))
268 if(!prs_uint16 ("data ", ps, depth, &uuid->time_mid))
270 if(!prs_uint16 ("data ", ps, depth, &uuid->time_hi_and_version))
273 if(!prs_uint8s (False, "data ", ps, depth, uuid->clock_seq, sizeof(uuid->clock_seq)))
275 if(!prs_uint8s (False, "data ", ps, depth, uuid->node, sizeof(uuid->node)))
281 /*******************************************************************
282 creates a STRHDR structure.
283 ********************************************************************/
285 void init_str_hdr(STRHDR *hdr, int max_len, int len, uint32 buffer)
287 hdr->str_max_len = max_len;
288 hdr->str_str_len = len;
289 hdr->buffer = buffer;
292 /*******************************************************************
293 Reads or writes a STRHDR structure.
294 ********************************************************************/
296 BOOL smb_io_strhdr(const char *desc, STRHDR *hdr, prs_struct *ps, int depth)
301 prs_debug(ps, depth, desc, "smb_io_strhdr");
306 if(!prs_uint16("str_str_len", ps, depth, &hdr->str_str_len))
308 if(!prs_uint16("str_max_len", ps, depth, &hdr->str_max_len))
310 if(!prs_uint32("buffer ", ps, depth, &hdr->buffer))
316 /*******************************************************************
317 Inits a UNIHDR structure.
318 ********************************************************************/
320 void init_uni_hdr(UNIHDR *hdr, UNISTR2 *str2)
322 hdr->uni_str_len = 2 * (str2->uni_str_len);
323 hdr->uni_max_len = 2 * (str2->uni_max_len);
324 hdr->buffer = (str2->uni_str_len != 0) ? 1 : 0;
327 /*******************************************************************
328 Reads or writes a UNIHDR structure.
329 ********************************************************************/
331 BOOL smb_io_unihdr(const char *desc, UNIHDR *hdr, prs_struct *ps, int depth)
336 prs_debug(ps, depth, desc, "smb_io_unihdr");
342 if(!prs_uint16("uni_str_len", ps, depth, &hdr->uni_str_len))
344 if(!prs_uint16("uni_max_len", ps, depth, &hdr->uni_max_len))
346 if(!prs_uint32("buffer ", ps, depth, &hdr->buffer))
352 /*******************************************************************
353 Inits a BUFHDR structure.
354 ********************************************************************/
356 void init_buf_hdr(BUFHDR *hdr, int max_len, int len)
358 hdr->buf_max_len = max_len;
362 /*******************************************************************
363 prs_uint16 wrapper. Call this and it sets up a pointer to where the
364 uint16 should be stored, or gets the size if reading.
365 ********************************************************************/
367 BOOL smb_io_hdrbuf_pre(const char *desc, BUFHDR *hdr, prs_struct *ps, int depth, uint32 *offset)
369 (*offset) = prs_offset(ps);
374 if(!smb_io_hdrbuf(desc, hdr, ps, depth))
381 if(!prs_set_offset(ps, prs_offset(ps) + (sizeof(uint32) * 2)))
388 /*******************************************************************
389 smb_io_hdrbuf wrapper. Call this and it retrospectively stores the size.
390 Does nothing on reading, as that is already handled by ...._pre()
391 ********************************************************************/
393 BOOL smb_io_hdrbuf_post(const char *desc, BUFHDR *hdr, prs_struct *ps, int depth,
394 uint32 ptr_hdrbuf, uint32 max_len, uint32 len)
397 /* writing: go back and do a retrospective job. i hate this */
399 uint32 old_offset = prs_offset(ps);
401 init_buf_hdr(hdr, max_len, len);
402 if(!prs_set_offset(ps, ptr_hdrbuf))
404 if(!smb_io_hdrbuf(desc, hdr, ps, depth))
407 if(!prs_set_offset(ps, old_offset))
414 /*******************************************************************
415 Reads or writes a BUFHDR structure.
416 ********************************************************************/
418 BOOL smb_io_hdrbuf(const char *desc, BUFHDR *hdr, prs_struct *ps, int depth)
423 prs_debug(ps, depth, desc, "smb_io_hdrbuf");
429 if(!prs_uint32("buf_max_len", ps, depth, &hdr->buf_max_len))
431 if(!prs_uint32("buf_len ", ps, depth, &hdr->buf_len))
437 /*******************************************************************
438 Inits a UNISTR structure.
439 ********************************************************************/
441 void init_unistr(UNISTR *str, const char *buf)
450 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\n");
456 rpcstr_push(str->buffer, buf, len*sizeof(uint16), STR_TERMINATE);
459 /*******************************************************************
460 reads or writes a UNISTR structure.
461 XXXX NOTE: UNISTR structures NEED to be null-terminated.
462 ********************************************************************/
464 BOOL smb_io_unistr(const char *desc, UNISTR *uni, prs_struct *ps, int depth)
469 prs_debug(ps, depth, desc, "smb_io_unistr");
472 if(!prs_unistr("unistr", ps, depth, uni))
478 /*******************************************************************
479 Allocate the RPC_DATA_BLOB memory.
480 ********************************************************************/
482 size_t create_rpc_blob(RPC_DATA_BLOB *str, size_t len)
484 str->buffer = (uint8 *)TALLOC_ZERO(get_talloc_ctx(), len);
485 if (str->buffer == NULL)
486 smb_panic("create_rpc_blob: talloc fail\n");
490 /*******************************************************************
491 Inits a RPC_DATA_BLOB structure from a uint32
492 ********************************************************************/
494 void init_rpc_blob_uint32(RPC_DATA_BLOB *str, uint32 val)
498 /* set up string lengths. */
499 str->buf_len = create_rpc_blob(str, sizeof(uint32));
500 SIVAL(str->buffer, 0, val);
503 /*******************************************************************
504 Inits a RPC_DATA_BLOB structure.
505 ********************************************************************/
507 void init_rpc_blob_str(RPC_DATA_BLOB *str, const char *buf, int len)
511 /* set up string lengths. */
512 str->buf_len = create_rpc_blob(str, len*2);
513 rpcstr_push(str->buffer, buf, (size_t)str->buf_len, STR_TERMINATE);
517 /*******************************************************************
518 Inits a RPC_DATA_BLOB structure from a hex string.
519 ********************************************************************/
521 void init_rpc_blob_hex(RPC_DATA_BLOB *str, const char *buf)
524 str->buf_len = create_rpc_blob(str, strlen(buf));
525 str->buf_len = strhex_to_str((char *)str->buffer, str->buf_len, buf);
528 /*******************************************************************
529 Inits a RPC_DATA_BLOB structure.
530 ********************************************************************/
532 void init_rpc_blob_bytes(RPC_DATA_BLOB *str, uint8 *buf, size_t len)
536 /* max buffer size (allocated size) */
538 len = create_rpc_blob(str, len);
539 memcpy(str->buffer, buf, len);
544 /*******************************************************************
545 reads or writes a BUFFER5 structure.
546 the buf_len member tells you how large the buffer is.
547 ********************************************************************/
548 BOOL smb_io_buffer5(const char *desc, BUFFER5 *buf5, prs_struct *ps, int depth)
550 prs_debug(ps, depth, desc, "smb_io_buffer5");
553 if (buf5 == NULL) return False;
557 if(!prs_uint32("buf_len", ps, depth, &buf5->buf_len))
561 if(!prs_buffer5(True, "buffer" , ps, depth, buf5))
568 /*******************************************************************
569 Inits a REGVAL_BUFFER structure.
570 ********************************************************************/
572 void init_regval_buffer(REGVAL_BUFFER *str, const uint8 *buf, size_t len)
576 /* max buffer size (allocated size) */
577 str->buf_max_len = len;
579 str->buf_len = buf != NULL ? len : 0;
582 SMB_ASSERT(str->buf_max_len >= str->buf_len);
583 str->buffer = (uint16 *)TALLOC_ZERO(get_talloc_ctx(),
585 if (str->buffer == NULL)
586 smb_panic("init_regval_buffer: talloc fail\n");
587 memcpy(str->buffer, buf, str->buf_len);
591 /*******************************************************************
592 Reads or writes a REGVAL_BUFFER structure.
593 the uni_max_len member tells you how large the buffer is.
594 the uni_str_len member tells you how much of the buffer is really used.
595 ********************************************************************/
597 BOOL smb_io_regval_buffer(const char *desc, prs_struct *ps, int depth, REGVAL_BUFFER *buf2)
600 prs_debug(ps, depth, desc, "smb_io_regval_buffer");
606 if(!prs_uint32("buf_max_len", ps, depth, &buf2->buf_max_len))
608 if(!prs_uint32("offset ", ps, depth, &buf2->offset))
610 if(!prs_uint32("buf_len ", ps, depth, &buf2->buf_len))
613 /* buffer advanced by indicated length of string
614 NOT by searching for null-termination */
616 if(!prs_regval_buffer(True, "buffer ", ps, depth, buf2))
622 /*******************************************************************
623 creates a UNISTR2 structure: sets up the buffer, too
624 ********************************************************************/
626 void init_buf_unistr2(UNISTR2 *str, uint32 *ptr, const char *buf)
630 init_unistr2(str, buf, UNI_STR_TERMINATE);
633 init_unistr2(str, NULL, UNI_FLAGS_NONE);
638 /*******************************************************************
639 Copies a UNISTR2 structure.
640 ********************************************************************/
642 void copy_unistr2(UNISTR2 *str, const UNISTR2 *from)
644 if (from->buffer == NULL) {
649 SMB_ASSERT(from->uni_max_len >= from->uni_str_len);
651 str->uni_max_len = from->uni_max_len;
652 str->offset = from->offset;
653 str->uni_str_len = from->uni_str_len;
655 /* the string buffer is allocated to the maximum size
656 (the the length of the source string) to prevent
657 reallocation of memory. */
658 if (str->buffer == NULL) {
659 str->buffer = (uint16 *)TALLOC_ZERO_ARRAY(get_talloc_ctx(), uint16, str->uni_max_len);
660 if ((str->buffer == NULL)) {
661 smb_panic("copy_unistr2: talloc fail\n");
666 /* copy the string */
667 memcpy(str->buffer, from->buffer, str->uni_max_len*sizeof(uint16));
670 /*******************************************************************
671 Creates a STRING2 structure.
672 ********************************************************************/
674 void init_string2(STRING2 *str, const char *buf, size_t max_len, size_t str_len)
676 /* set up string lengths. */
677 SMB_ASSERT(max_len >= str_len);
679 /* Ensure buf is valid if str_len was set. Coverity check. */
680 if (str_len && !buf) {
684 str->str_max_len = max_len;
686 str->str_str_len = str_len;
688 /* store the string */
690 str->buffer = (uint8 *)TALLOC_ZERO(get_talloc_ctx(),
692 if (str->buffer == NULL)
693 smb_panic("init_string2: malloc fail\n");
694 memcpy(str->buffer, buf, str_len);
698 /*******************************************************************
699 Reads or writes a STRING2 structure.
700 XXXX NOTE: STRING2 structures need NOT be null-terminated.
701 the str_str_len member tells you how long the string is;
702 the str_max_len member tells you how large the buffer is.
703 ********************************************************************/
705 BOOL smb_io_string2(const char *desc, STRING2 *str2, uint32 buffer, prs_struct *ps, int depth)
712 prs_debug(ps, depth, desc, "smb_io_string2");
718 if(!prs_uint32("str_max_len", ps, depth, &str2->str_max_len))
720 if(!prs_uint32("offset ", ps, depth, &str2->offset))
722 if(!prs_uint32("str_str_len", ps, depth, &str2->str_str_len))
725 /* buffer advanced by indicated length of string
726 NOT by searching for null-termination */
727 if(!prs_string2(True, "buffer ", ps, depth, str2))
732 prs_debug(ps, depth, desc, "smb_io_string2 - NULL");
734 memset((char *)str2, '\0', sizeof(*str2));
741 /*******************************************************************
742 Inits a UNISTR2 structure.
743 ********************************************************************/
745 void init_unistr2(UNISTR2 *str, const char *buf, enum unistr2_term_codes flags)
748 uint32 num_chars = 0;
751 /* We always null terminate the copy. */
752 len = strlen(buf) + 1;
753 if ( flags == UNI_STR_DBLTERMINATE )
756 /* no buffer -- nothing to do */
757 str->uni_max_len = 0;
759 str->uni_str_len = 0;
765 str->buffer = TALLOC_ZERO_ARRAY(get_talloc_ctx(), uint16, len);
766 if (str->buffer == NULL) {
767 smb_panic("init_unistr2: malloc fail\n");
771 /* Ensure len is the length in *bytes* */
772 len *= sizeof(uint16);
775 * The UNISTR2 must be initialized !!!
779 rpcstr_push((char *)str->buffer, buf, len, STR_TERMINATE);
780 num_chars = strlen_w(str->buffer);
781 if (flags == UNI_STR_TERMINATE || flags == UNI_MAXLEN_TERMINATE) {
784 if ( flags == UNI_STR_DBLTERMINATE )
788 str->uni_max_len = num_chars;
790 str->uni_str_len = num_chars;
791 if ( num_chars && ((flags == UNI_MAXLEN_TERMINATE) || (flags == UNI_BROKEN_NON_NULL)) )
795 /*******************************************************************
796 Inits a UNISTR4 structure.
797 ********************************************************************/
799 void init_unistr4(UNISTR4 *uni4, const char *buf, enum unistr2_term_codes flags)
801 uni4->string = TALLOC_P( get_talloc_ctx(), UNISTR2 );
803 smb_panic("init_unistr4: talloc fail\n");
806 init_unistr2( uni4->string, buf, flags );
808 uni4->length = 2 * (uni4->string->uni_str_len);
809 uni4->size = 2 * (uni4->string->uni_max_len);
812 void init_unistr4_w( TALLOC_CTX *ctx, UNISTR4 *uni4, const smb_ucs2_t *buf )
814 uni4->string = TALLOC_P( ctx, UNISTR2 );
816 smb_panic("init_unistr4_w: talloc fail\n");
819 init_unistr2_w( ctx, uni4->string, buf );
821 uni4->length = 2 * (uni4->string->uni_str_len);
822 uni4->size = 2 * (uni4->string->uni_max_len);
826 * Inits a UNISTR2 structure.
827 * @param ctx talloc context to allocate string on
828 * @param str pointer to string to create
829 * @param buf UCS2 null-terminated buffer to init from
832 void init_unistr2_w(TALLOC_CTX *ctx, UNISTR2 *str, const smb_ucs2_t *buf)
834 uint32 len = buf ? strlen_w(buf) : 0;
838 /* set up string lengths. */
839 str->uni_max_len = len;
841 str->uni_str_len = len;
843 str->buffer = TALLOC_ZERO_ARRAY(ctx, uint16, len + 1);
844 if (str->buffer == NULL) {
845 smb_panic("init_unistr2_w: talloc fail\n");
850 * don't move this test above ! The UNISTR2 must be initialized !!!
856 /* Yes, this is a strncpy( foo, bar, strlen(bar)) - but as
857 long as the buffer above is talloc()ed correctly then this
858 is the correct thing to do */
859 strncpy_w(str->buffer, buf, len + 1);
862 /*******************************************************************
863 Inits a UNISTR2 structure from a UNISTR
864 ********************************************************************/
866 void init_unistr2_from_unistr(UNISTR2 *to, const UNISTR *from)
870 /* the destination UNISTR2 should never be NULL.
871 if it is it is a programming error */
873 /* if the source UNISTR is NULL, then zero out
874 the destination string and return */
876 if ((from == NULL) || (from->buffer == NULL))
879 /* get the length; UNISTR must be NULL terminated */
881 while ((from->buffer)[i]!='\0')
883 i++; /* one more to catch the terminating NULL */
884 /* is this necessary -- jerry? I need to think */
886 /* set up string lengths; uni_max_len is set to i+1
887 because we need to account for the final NULL termination */
892 /* allocate the space and copy the string buffer */
893 to->buffer = TALLOC_ZERO_ARRAY(get_talloc_ctx(), uint16, i);
894 if (to->buffer == NULL)
895 smb_panic("init_unistr2_from_unistr: malloc fail\n");
896 memcpy(to->buffer, from->buffer, i*sizeof(uint16));
900 /*******************************************************************
901 Inits a UNISTR2 structure from a DATA_BLOB.
902 The length of the data_blob must count the bytes of the buffer.
903 Copies the blob data.
904 ********************************************************************/
906 void init_unistr2_from_datablob(UNISTR2 *str, DATA_BLOB *blob)
908 /* Allocs the unistring */
909 init_unistr2(str, NULL, UNI_FLAGS_NONE);
911 /* Sets the values */
912 str->uni_str_len = blob->length / sizeof(uint16);
913 str->uni_max_len = str->uni_str_len;
916 str->buffer = (uint16 *) memdup(blob->data, blob->length);
920 if ((str->buffer == NULL) && (blob->length > 0)) {
921 smb_panic("init_unistr2_from_datablob: malloc fail\n");
925 /*******************************************************************
926 UNISTR2* are a little different in that the pointer and the UNISTR2
927 are not necessarily read/written back to back. So we break it up
928 into 2 separate functions.
929 See SPOOL_USER_1 in include/rpc_spoolss.h for an example.
930 ********************************************************************/
932 BOOL prs_io_unistr2_p(const char *desc, prs_struct *ps, int depth, UNISTR2 **uni2)
936 /* caputure the pointer value to stream */
938 data_p = *uni2 ? 0xf000baaa : 0;
940 if ( !prs_uint32("ptr", ps, depth, &data_p ))
943 /* we're done if there is no data */
948 if (UNMARSHALLING(ps)) {
949 if ( !(*uni2 = PRS_ALLOC_MEM(ps, UNISTR2, 1)) )
956 /*******************************************************************
957 now read/write the actual UNISTR2. Memory for the UNISTR2 (but
958 not UNISTR2.buffer) has been allocated previously by prs_unistr2_p()
959 ********************************************************************/
961 BOOL prs_io_unistr2(const char *desc, prs_struct *ps, int depth, UNISTR2 *uni2 )
963 /* just return true if there is no pointer to deal with.
964 the memory must have been previously allocated on unmarshalling
965 by prs_unistr2_p() */
970 /* just pass off to smb_io_unstr2() passing the uni2 address as
971 the pointer (like you would expect) */
973 return smb_io_unistr2( desc, uni2, uni2 ? 1 : 0, ps, depth );
976 /*******************************************************************
977 Reads or writes a UNISTR2 structure.
978 XXXX NOTE: UNISTR2 structures need NOT be null-terminated.
979 the uni_str_len member tells you how long the string is;
980 the uni_max_len member tells you how large the buffer is.
981 ********************************************************************/
983 BOOL smb_io_unistr2(const char *desc, UNISTR2 *uni2, uint32 buffer, prs_struct *ps, int depth)
990 prs_debug(ps, depth, desc, "smb_io_unistr2");
996 if(!prs_uint32("uni_max_len", ps, depth, &uni2->uni_max_len))
998 if(!prs_uint32("offset ", ps, depth, &uni2->offset))
1000 if(!prs_uint32("uni_str_len", ps, depth, &uni2->uni_str_len))
1003 /* buffer advanced by indicated length of string
1004 NOT by searching for null-termination */
1005 if(!prs_unistr2(True, "buffer ", ps, depth, uni2))
1010 prs_debug(ps, depth, desc, "smb_io_unistr2 - NULL");
1012 memset((char *)uni2, '\0', sizeof(*uni2));
1019 /*******************************************************************
1020 now read/write UNISTR4
1021 ********************************************************************/
1023 BOOL prs_unistr4(const char *desc, prs_struct *ps, int depth, UNISTR4 *uni4)
1026 prs_debug(ps, depth, desc, "prs_unistr4");
1029 if ( !prs_uint16("length", ps, depth, &uni4->length ))
1031 if ( !prs_uint16("size", ps, depth, &uni4->size ))
1036 if ( !prs_pointer( desc, ps, depth, &ptr, sizeof(UNISTR2), (PRS_POINTER_CAST)prs_io_unistr2 ) )
1039 uni4->string = (UNISTR2 *)ptr;
1044 /*******************************************************************
1045 now read/write UNISTR4 header
1046 ********************************************************************/
1048 BOOL prs_unistr4_hdr(const char *desc, prs_struct *ps, int depth, UNISTR4 *uni4)
1050 prs_debug(ps, depth, desc, "prs_unistr4_hdr");
1053 if ( !prs_uint16("length", ps, depth, &uni4->length) )
1055 if ( !prs_uint16("size", ps, depth, &uni4->size) )
1057 if ( !prs_io_unistr2_p(desc, ps, depth, &uni4->string) )
1063 /*******************************************************************
1064 now read/write UNISTR4 string
1065 ********************************************************************/
1067 BOOL prs_unistr4_str(const char *desc, prs_struct *ps, int depth, UNISTR4 *uni4)
1069 prs_debug(ps, depth, desc, "prs_unistr4_str");
1072 if ( !prs_io_unistr2(desc, ps, depth, uni4->string) )
1078 /*******************************************************************
1079 Reads or writes a UNISTR4_ARRAY structure.
1080 ********************************************************************/
1082 BOOL prs_unistr4_array(const char *desc, prs_struct *ps, int depth, UNISTR4_ARRAY *array )
1086 prs_debug(ps, depth, desc, "prs_unistr4_array");
1089 if(!prs_uint32("count", ps, depth, &array->count))
1092 if ( array->count == 0 )
1095 if (UNMARSHALLING(ps)) {
1096 if ( !(array->strings = TALLOC_ZERO_ARRAY( get_talloc_ctx(), UNISTR4, array->count)) )
1100 /* write the headers and then the actual string buffer */
1102 for ( i=0; i<array->count; i++ ) {
1103 if ( !prs_unistr4_hdr( "string", ps, depth, &array->strings[i]) )
1107 for (i=0;i<array->count;i++) {
1108 if ( !prs_unistr4_str("string", ps, depth, &array->strings[i]) )
1115 /********************************************************************
1116 initialise a UNISTR_ARRAY from a char**
1117 ********************************************************************/
1119 BOOL init_unistr4_array( UNISTR4_ARRAY *array, uint32 count, const char **strings )
1123 array->count = count;
1125 if ( array->count == 0 )
1128 /* allocate memory for the array of UNISTR4 objects */
1130 if ( !(array->strings = TALLOC_ZERO_ARRAY(get_talloc_ctx(), UNISTR4, count )) )
1133 for ( i=0; i<count; i++ )
1134 init_unistr4( &array->strings[i], strings[i], UNI_STR_TERMINATE );
1139 BOOL smb_io_lockout_string_hdr(const char *desc, HDR_LOCKOUT_STRING *hdr_account_lockout, prs_struct *ps, int depth)
1141 prs_debug(ps, depth, desc, "smb_io_lockout_string_hdr");
1147 if(!prs_uint16("size", ps, depth, &hdr_account_lockout->size))
1149 if(!prs_uint16("length", ps, depth, &hdr_account_lockout->length))
1151 if(!prs_uint32("buffer", ps, depth, &hdr_account_lockout->buffer))
1157 BOOL smb_io_account_lockout_str(const char *desc, LOCKOUT_STRING *account_lockout, uint32 buffer, prs_struct *ps, int depth)
1159 prs_debug(ps, depth, desc, "smb_io_account_lockout_string");
1162 if(!prs_uint32("array_size", ps, depth, &account_lockout->array_size))
1165 if(!prs_uint32("offset", ps, depth, &account_lockout->offset))
1167 if(!prs_uint32("length", ps, depth, &account_lockout->length))
1170 if (!prs_uint64("lockout_duration", ps, depth, &account_lockout->lockout_duration))
1172 if (!prs_uint64("reset_count", ps, depth, &account_lockout->reset_count))
1174 if (!prs_uint32("bad_attempt_lockout", ps, depth, &account_lockout->bad_attempt_lockout))
1176 if (!prs_uint32("dummy", ps, depth, &account_lockout->dummy))
1179 if(!prs_uint16s (False, "bindata", ps, depth, &account_lockout->bindata, length))
1186 /*******************************************************************
1187 Inits a DOM_RID structure.
1188 ********************************************************************/
1190 void init_dom_rid(DOM_RID *prid, uint32 rid, uint16 type, uint32 idx)
1194 prid->rid_idx = idx;
1197 /*******************************************************************
1198 Reads or writes a DOM_RID structure.
1199 ********************************************************************/
1201 BOOL smb_io_dom_rid(const char *desc, DOM_RID *rid, prs_struct *ps, int depth)
1206 prs_debug(ps, depth, desc, "smb_io_dom_rid");
1212 if(!prs_uint16("type ", ps, depth, &rid->type))
1216 if(!prs_uint32("rid ", ps, depth, &rid->rid))
1218 if(!prs_uint32("rid_idx", ps, depth, &rid->rid_idx))
1224 /*******************************************************************
1225 Reads or writes a DOM_RID2 structure.
1226 ********************************************************************/
1228 BOOL smb_io_dom_rid2(const char *desc, DOM_RID2 *rid, prs_struct *ps, int depth)
1233 prs_debug(ps, depth, desc, "smb_io_dom_rid2");
1239 if(!prs_uint16("type ", ps, depth, &rid->type))
1243 if(!prs_uint32("rid ", ps, depth, &rid->rid))
1245 if(!prs_uint32("rid_idx", ps, depth, &rid->rid_idx))
1247 if(!prs_uint32("unknown", ps, depth, &rid->unknown))
1254 /*******************************************************************
1255 creates a DOM_RID3 structure.
1256 ********************************************************************/
1258 void init_dom_rid3(DOM_RID3 *rid3, uint32 rid, uint8 type)
1262 rid3->ptr_type = 0x1; /* non-zero, basically. */
1267 /*******************************************************************
1268 reads or writes a DOM_RID3 structure.
1269 ********************************************************************/
1271 BOOL smb_io_dom_rid3(const char *desc, DOM_RID3 *rid3, prs_struct *ps, int depth)
1276 prs_debug(ps, depth, desc, "smb_io_dom_rid3");
1282 if(!prs_uint32("rid ", ps, depth, &rid3->rid))
1284 if(!prs_uint32("type1 ", ps, depth, &rid3->type1))
1286 if(!prs_uint32("ptr_type", ps, depth, &rid3->ptr_type))
1288 if(!prs_uint32("type2 ", ps, depth, &rid3->type2))
1290 if(!prs_uint32("unk ", ps, depth, &rid3->unk))
1296 /*******************************************************************
1297 Inits a DOM_RID4 structure.
1298 ********************************************************************/
1300 void init_dom_rid4(DOM_RID4 *rid4, uint16 unknown, uint16 attr, uint32 rid)
1302 rid4->unknown = unknown;
1307 /*******************************************************************
1308 Inits a DOM_CLNT_SRV structure.
1309 ********************************************************************/
1311 static void init_clnt_srv(DOM_CLNT_SRV *logcln, const char *logon_srv, const char *comp_name)
1313 DEBUG(5,("init_clnt_srv: %d\n", __LINE__));
1315 if (logon_srv != NULL) {
1316 logcln->undoc_buffer = 1;
1317 init_unistr2(&logcln->uni_logon_srv, logon_srv, UNI_STR_TERMINATE);
1319 logcln->undoc_buffer = 0;
1322 if (comp_name != NULL) {
1323 logcln->undoc_buffer2 = 1;
1324 init_unistr2(&logcln->uni_comp_name, comp_name, UNI_STR_TERMINATE);
1326 logcln->undoc_buffer2 = 0;
1330 /*******************************************************************
1331 Inits or writes a DOM_CLNT_SRV structure.
1332 ********************************************************************/
1334 BOOL smb_io_clnt_srv(const char *desc, DOM_CLNT_SRV *logcln, prs_struct *ps, int depth)
1339 prs_debug(ps, depth, desc, "smb_io_clnt_srv");
1345 if(!prs_uint32("undoc_buffer ", ps, depth, &logcln->undoc_buffer))
1348 if (logcln->undoc_buffer != 0) {
1349 if(!smb_io_unistr2("unistr2", &logcln->uni_logon_srv, logcln->undoc_buffer, ps, depth))
1356 if(!prs_uint32("undoc_buffer2", ps, depth, &logcln->undoc_buffer2))
1359 if (logcln->undoc_buffer2 != 0) {
1360 if(!smb_io_unistr2("unistr2", &logcln->uni_comp_name, logcln->undoc_buffer2, ps, depth))
1367 /*******************************************************************
1368 Inits a DOM_LOG_INFO structure.
1369 ********************************************************************/
1371 void init_log_info(DOM_LOG_INFO *loginfo, const char *logon_srv, const char *acct_name,
1372 uint16 sec_chan, const char *comp_name)
1374 DEBUG(5,("make_log_info %d\n", __LINE__));
1376 loginfo->undoc_buffer = 1;
1378 init_unistr2(&loginfo->uni_logon_srv, logon_srv, UNI_STR_TERMINATE);
1379 init_unistr2(&loginfo->uni_acct_name, acct_name, UNI_STR_TERMINATE);
1381 loginfo->sec_chan = sec_chan;
1383 init_unistr2(&loginfo->uni_comp_name, comp_name, UNI_STR_TERMINATE);
1386 /*******************************************************************
1387 Reads or writes a DOM_LOG_INFO structure.
1388 ********************************************************************/
1390 BOOL smb_io_log_info(const char *desc, DOM_LOG_INFO *loginfo, prs_struct *ps, int depth)
1392 if (loginfo == NULL)
1395 prs_debug(ps, depth, desc, "smb_io_log_info");
1401 if(!prs_uint32("undoc_buffer", ps, depth, &loginfo->undoc_buffer))
1404 if(!smb_io_unistr2("unistr2", &loginfo->uni_logon_srv, True, ps, depth))
1406 if(!smb_io_unistr2("unistr2", &loginfo->uni_acct_name, True, ps, depth))
1409 if(!prs_uint16("sec_chan", ps, depth, &loginfo->sec_chan))
1412 if(!smb_io_unistr2("unistr2", &loginfo->uni_comp_name, True, ps, depth))
1418 /*******************************************************************
1419 Reads or writes a DOM_CHAL structure.
1420 ********************************************************************/
1422 BOOL smb_io_chal(const char *desc, DOM_CHAL *chal, prs_struct *ps, int depth)
1427 prs_debug(ps, depth, desc, "smb_io_chal");
1430 if(!prs_uint8s (False, "data", ps, depth, chal->data, 8))
1436 /*******************************************************************
1437 Reads or writes a DOM_CRED structure.
1438 ********************************************************************/
1440 BOOL smb_io_cred(const char *desc, DOM_CRED *cred, prs_struct *ps, int depth)
1445 prs_debug(ps, depth, desc, "smb_io_cred");
1451 if(!smb_io_chal ("", &cred->challenge, ps, depth))
1454 if(!smb_io_utime("", &cred->timestamp, ps, depth))
1460 /*******************************************************************
1461 Inits a DOM_CLNT_INFO2 structure.
1462 ********************************************************************/
1464 void init_clnt_info2(DOM_CLNT_INFO2 *clnt,
1465 const char *logon_srv, const char *comp_name,
1466 const DOM_CRED *clnt_cred)
1468 DEBUG(5,("make_clnt_info: %d\n", __LINE__));
1470 init_clnt_srv(&clnt->login, logon_srv, comp_name);
1472 if (clnt_cred != NULL) {
1474 memcpy(&clnt->cred, clnt_cred, sizeof(clnt->cred));
1480 /*******************************************************************
1481 Reads or writes a DOM_CLNT_INFO2 structure.
1482 ********************************************************************/
1484 BOOL smb_io_clnt_info2(const char *desc, DOM_CLNT_INFO2 *clnt, prs_struct *ps, int depth)
1489 prs_debug(ps, depth, desc, "smb_io_clnt_info2");
1495 if(!smb_io_clnt_srv("", &clnt->login, ps, depth))
1501 if(!prs_uint32("ptr_cred", ps, depth, &clnt->ptr_cred))
1503 if(!smb_io_cred("", &clnt->cred, ps, depth))
1509 /*******************************************************************
1510 Inits a DOM_CLNT_INFO structure.
1511 ********************************************************************/
1513 void init_clnt_info(DOM_CLNT_INFO *clnt,
1514 const char *logon_srv, const char *acct_name,
1515 uint16 sec_chan, const char *comp_name,
1516 const DOM_CRED *cred)
1518 DEBUG(5,("make_clnt_info\n"));
1520 init_log_info(&clnt->login, logon_srv, acct_name, sec_chan, comp_name);
1521 memcpy(&clnt->cred, cred, sizeof(clnt->cred));
1524 /*******************************************************************
1525 Reads or writes a DOM_CLNT_INFO structure.
1526 ********************************************************************/
1528 BOOL smb_io_clnt_info(const char *desc, DOM_CLNT_INFO *clnt, prs_struct *ps, int depth)
1533 prs_debug(ps, depth, desc, "smb_io_clnt_info");
1539 if(!smb_io_log_info("", &clnt->login, ps, depth))
1541 if(!smb_io_cred("", &clnt->cred, ps, depth))
1547 /*******************************************************************
1548 Inits a DOM_LOGON_ID structure.
1549 ********************************************************************/
1551 void init_logon_id(DOM_LOGON_ID *logonid, uint32 log_id_low, uint32 log_id_high)
1553 DEBUG(5,("make_logon_id: %d\n", __LINE__));
1555 logonid->low = log_id_low;
1556 logonid->high = log_id_high;
1559 /*******************************************************************
1560 Reads or writes a DOM_LOGON_ID structure.
1561 ********************************************************************/
1563 BOOL smb_io_logon_id(const char *desc, DOM_LOGON_ID *logonid, prs_struct *ps, int depth)
1565 if (logonid == NULL)
1568 prs_debug(ps, depth, desc, "smb_io_logon_id");
1574 if(!prs_uint32("low ", ps, depth, &logonid->low ))
1576 if(!prs_uint32("high", ps, depth, &logonid->high))
1582 /*******************************************************************
1583 Inits an OWF_INFO structure.
1584 ********************************************************************/
1586 void init_owf_info(OWF_INFO *hash, const uint8 data[16])
1588 DEBUG(5,("init_owf_info: %d\n", __LINE__));
1591 memcpy(hash->data, data, sizeof(hash->data));
1593 memset((char *)hash->data, '\0', sizeof(hash->data));
1596 /*******************************************************************
1597 Reads or writes an OWF_INFO structure.
1598 ********************************************************************/
1600 BOOL smb_io_owf_info(const char *desc, OWF_INFO *hash, prs_struct *ps, int depth)
1605 prs_debug(ps, depth, desc, "smb_io_owf_info");
1611 if(!prs_uint8s (False, "data", ps, depth, hash->data, 16))
1617 /*******************************************************************
1618 Reads or writes a DOM_GID structure.
1619 ********************************************************************/
1621 BOOL smb_io_gid(const char *desc, DOM_GID *gid, prs_struct *ps, int depth)
1626 prs_debug(ps, depth, desc, "smb_io_gid");
1632 if(!prs_uint32("g_rid", ps, depth, &gid->g_rid))
1634 if(!prs_uint32("attr ", ps, depth, &gid->attr))
1640 /*******************************************************************
1641 Reads or writes an POLICY_HND structure.
1642 ********************************************************************/
1644 BOOL smb_io_pol_hnd(const char *desc, POLICY_HND *pol, prs_struct *ps, int depth)
1649 prs_debug(ps, depth, desc, "smb_io_pol_hnd");
1655 if(UNMARSHALLING(ps))
1658 if (!prs_uint32("handle_type", ps, depth, &pol->handle_type))
1660 if (!smb_io_uuid("uuid", (struct GUID*)&pol->uuid, ps, depth))
1666 /*******************************************************************
1668 ********************************************************************/
1670 void init_unistr3(UNISTR3 *str, const char *buf)
1674 str->str.buffer = NULL;
1678 str->uni_str_len = strlen(buf) + 1;
1680 str->str.buffer = TALLOC_ZERO_ARRAY(get_talloc_ctx(), uint16, str->uni_str_len);
1681 if (str->str.buffer == NULL)
1682 smb_panic("init_unistr3: malloc fail\n");
1684 rpcstr_push((char *)str->str.buffer, buf, str->uni_str_len * sizeof(uint16), STR_TERMINATE);
1687 /*******************************************************************
1688 Reads or writes a UNISTR3 structure.
1689 ********************************************************************/
1691 BOOL smb_io_unistr3(const char *desc, UNISTR3 *name, prs_struct *ps, int depth)
1696 prs_debug(ps, depth, desc, "smb_io_unistr3");
1702 if(!prs_uint32("uni_str_len", ps, depth, &name->uni_str_len))
1705 /* we're done if there is no string */
1707 if ( name->uni_str_len == 0 )
1710 /* don't know if len is specified by uni_str_len member... */
1711 /* assume unicode string is unicode-null-terminated, instead */
1713 if(!prs_unistr3(True, "unistr", name, ps, depth))
1719 /*******************************************************************
1720 Stream a uint64_struct
1721 ********************************************************************/
1722 BOOL prs_uint64(const char *name, prs_struct *ps, int depth, uint64 *data64)
1724 if (UNMARSHALLING(ps)) {
1727 if (!prs_uint32(name, ps, depth+1, &low))
1730 if (!prs_uint32(name, ps, depth+1, &high))
1733 *data64 = ((uint64_t)high << 32) + low;
1737 uint32 high = (*data64) >> 32, low = (*data64) & 0xFFFFFFFF;
1738 return prs_uint32(name, ps, depth+1, &low) &&
1739 prs_uint32(name, ps, depth+1, &high);
1743 /*******************************************************************
1744 reads or writes a BUFHDR2 structure.
1745 ********************************************************************/
1746 BOOL smb_io_bufhdr2(const char *desc, BUFHDR2 *hdr, prs_struct *ps, int depth)
1748 prs_debug(ps, depth, desc, "smb_io_bufhdr2");
1752 prs_uint32("info_level", ps, depth, &(hdr->info_level));
1753 prs_uint32("length ", ps, depth, &(hdr->length ));
1754 prs_uint32("buffer ", ps, depth, &(hdr->buffer ));
1759 /*******************************************************************
1760 reads or writes a BUFHDR4 structure.
1761 ********************************************************************/
1762 BOOL smb_io_bufhdr4(const char *desc, BUFHDR4 *hdr, prs_struct *ps, int depth)
1764 prs_debug(ps, depth, desc, "smb_io_bufhdr4");
1768 prs_uint32("size", ps, depth, &hdr->size);
1769 prs_uint32("buffer", ps, depth, &hdr->buffer);
1774 /*******************************************************************
1775 reads or writes a RPC_DATA_BLOB structure.
1776 ********************************************************************/
1778 BOOL smb_io_rpc_blob(const char *desc, RPC_DATA_BLOB *blob, prs_struct *ps, int depth)
1780 prs_debug(ps, depth, desc, "smb_io_rpc_blob");
1784 if ( !prs_uint32("buf_len", ps, depth, &blob->buf_len) )
1787 if ( blob->buf_len == 0 )
1790 if (UNMARSHALLING(ps)) {
1791 blob->buffer = PRS_ALLOC_MEM(ps, uint8, blob->buf_len);
1792 if (!blob->buffer) {
1797 if ( !prs_uint8s(True, "buffer", ps, depth, blob->buffer, blob->buf_len) )
1803 /*******************************************************************
1804 creates a UNIHDR structure.
1805 ********************************************************************/
1807 BOOL make_uni_hdr(UNIHDR *hdr, int len)
1813 hdr->uni_str_len = 2 * len;
1814 hdr->uni_max_len = 2 * len;
1815 hdr->buffer = len != 0 ? 1 : 0;
1820 /*******************************************************************
1821 creates a BUFHDR2 structure.
1822 ********************************************************************/
1823 BOOL make_bufhdr2(BUFHDR2 *hdr, uint32 info_level, uint32 length, uint32 buffer)
1825 hdr->info_level = info_level;
1826 hdr->length = length;
1827 hdr->buffer = buffer;
1832 /*******************************************************************
1833 return the length of a UNISTR string.
1834 ********************************************************************/
1836 uint32 str_len_uni(UNISTR *source)
1840 if (!source->buffer)
1843 while (source->buffer[i])