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.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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\n");
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)
120 prs_debug(ps, depth, desc, "smb_io_time");
126 if(!prs_uint32("low ", ps, depth, &nttime->low)) /* low part */
128 if(!prs_uint32("high", ps, depth, &nttime->high)) /* high part */
134 /*******************************************************************
135 Reads or writes a LOOKUP_LEVEL structure.
136 ********************************************************************/
138 BOOL smb_io_lookup_level(const char *desc, LOOKUP_LEVEL *level, prs_struct *ps, int depth)
143 prs_debug(ps, depth, desc, "smb_io_lookup_level");
148 if(!prs_uint16("value", ps, depth, &level->value))
156 /*******************************************************************
157 Gets an enumeration handle from an ENUM_HND structure.
158 ********************************************************************/
160 uint32 get_enum_hnd(ENUM_HND *enh)
162 return (enh && enh->ptr_hnd != 0) ? enh->handle : 0;
165 /*******************************************************************
166 Inits an ENUM_HND structure.
167 ********************************************************************/
169 void init_enum_hnd(ENUM_HND *enh, uint32 hnd)
171 DEBUG(5,("smb_io_enum_hnd\n"));
173 enh->ptr_hnd = (hnd != 0) ? 1 : 0;
177 /*******************************************************************
178 Reads or writes an ENUM_HND structure.
179 ********************************************************************/
181 BOOL smb_io_enum_hnd(const char *desc, ENUM_HND *hnd, prs_struct *ps, int depth)
186 prs_debug(ps, depth, desc, "smb_io_enum_hnd");
192 if(!prs_uint32("ptr_hnd", ps, depth, &hnd->ptr_hnd)) /* pointer */
195 if (hnd->ptr_hnd != 0) {
196 if(!prs_uint32("handle ", ps, depth, &hnd->handle )) /* enum handle */
203 /*******************************************************************
204 Reads or writes a DOM_SID structure.
205 ********************************************************************/
207 BOOL smb_io_dom_sid(const char *desc, DOM_SID *sid, prs_struct *ps, int depth)
214 prs_debug(ps, depth, desc, "smb_io_dom_sid");
217 if(!prs_uint8 ("sid_rev_num", ps, depth, &sid->sid_rev_num))
220 if(!prs_uint8 ("num_auths ", ps, depth, &sid->num_auths))
223 for (i = 0; i < 6; i++)
226 slprintf(tmp, sizeof(tmp) - 1, "id_auth[%d] ", i);
227 if(!prs_uint8 (tmp, ps, depth, &sid->id_auth[i]))
231 /* oops! XXXX should really issue a warning here... */
232 if (sid->num_auths > MAXSUBAUTHS)
233 sid->num_auths = MAXSUBAUTHS;
235 if(!prs_uint32s(False, "sub_auths ", ps, depth, sid->sub_auths, sid->num_auths))
241 /*******************************************************************
242 Inits a DOM_SID structure.
244 BIG NOTE: this function only does SIDS where the identauth is not >= 2^32
245 identauth >= 2^32 can be detected because it will be specified in hex
246 ********************************************************************/
248 void init_dom_sid(DOM_SID *sid, const char *str_sid)
254 if (str_sid == NULL) {
255 DEBUG(4,("netlogon domain SID: none\n"));
256 sid->sid_rev_num = 0;
261 pstrcpy(domsid, str_sid);
263 DEBUG(4,("init_dom_sid %d SID: %s\n", __LINE__, domsid));
265 /* assume, but should check, that domsid starts "S-" */
266 p = strtok(domsid+2,"-");
267 sid->sid_rev_num = atoi(p);
269 /* identauth in decimal should be < 2^32 */
270 /* identauth in hex should be >= 2^32 */
271 identauth = atoi(strtok(0,"-"));
273 DEBUG(4,("netlogon rev %d\n", sid->sid_rev_num));
274 DEBUG(4,("netlogon %s ia %d\n", p, identauth));
278 sid->id_auth[2] = (identauth & 0xff000000) >> 24;
279 sid->id_auth[3] = (identauth & 0x00ff0000) >> 16;
280 sid->id_auth[4] = (identauth & 0x0000ff00) >> 8;
281 sid->id_auth[5] = (identauth & 0x000000ff);
285 while ((p = strtok(0, "-")) != NULL && sid->num_auths < MAXSUBAUTHS)
286 sid->sub_auths[sid->num_auths++] = atoi(p);
288 DEBUG(4,("init_dom_sid: %d SID: %s\n", __LINE__, domsid));
291 /*******************************************************************
292 Inits a DOM_SID2 structure.
293 ********************************************************************/
295 void init_dom_sid2(DOM_SID2 *sid2, const DOM_SID *sid)
298 sid2->num_auths = sid2->sid.num_auths;
301 /*******************************************************************
302 Reads or writes a DOM_SID2 structure.
303 ********************************************************************/
305 BOOL smb_io_dom_sid2(const char *desc, DOM_SID2 *sid, prs_struct *ps, int depth)
310 prs_debug(ps, depth, desc, "smb_io_dom_sid2");
316 if(!prs_uint32("num_auths", ps, depth, &sid->num_auths))
319 if(!smb_io_dom_sid("sid", &sid->sid, ps, depth))
325 /*******************************************************************
326 Reads or writes a struct uuid
327 ********************************************************************/
329 BOOL smb_io_uuid(const char *desc, struct uuid *uuid,
330 prs_struct *ps, int depth)
335 prs_debug(ps, depth, desc, "smb_io_uuid");
338 if(!prs_uint32 ("data ", ps, depth, &uuid->time_low))
340 if(!prs_uint16 ("data ", ps, depth, &uuid->time_mid))
342 if(!prs_uint16 ("data ", ps, depth, &uuid->time_hi_and_version))
345 if(!prs_uint8s (False, "data ", ps, depth, uuid->clock_seq, sizeof(uuid->clock_seq)))
347 if(!prs_uint8s (False, "data ", ps, depth, uuid->node, sizeof(uuid->node)))
353 /*******************************************************************
354 creates a STRHDR structure.
355 ********************************************************************/
357 void init_str_hdr(STRHDR *hdr, int max_len, int len, uint32 buffer)
359 hdr->str_max_len = max_len;
360 hdr->str_str_len = len;
361 hdr->buffer = buffer;
364 /*******************************************************************
365 Reads or writes a STRHDR structure.
366 ********************************************************************/
368 BOOL smb_io_strhdr(const char *desc, STRHDR *hdr, prs_struct *ps, int depth)
373 prs_debug(ps, depth, desc, "smb_io_strhdr");
378 if(!prs_uint16("str_str_len", ps, depth, &hdr->str_str_len))
380 if(!prs_uint16("str_max_len", ps, depth, &hdr->str_max_len))
382 if(!prs_uint32("buffer ", ps, depth, &hdr->buffer))
388 /*******************************************************************
389 Inits a UNIHDR structure.
390 ********************************************************************/
392 void init_uni_hdr(UNIHDR *hdr, UNISTR2 *str2)
394 hdr->uni_str_len = 2 * (str2->uni_str_len);
395 hdr->uni_max_len = 2 * (str2->uni_max_len);
396 hdr->buffer = (str2->uni_str_len != 0) ? 1 : 0;
399 /*******************************************************************
400 Reads or writes a UNIHDR structure.
401 ********************************************************************/
403 BOOL smb_io_unihdr(const char *desc, UNIHDR *hdr, prs_struct *ps, int depth)
408 prs_debug(ps, depth, desc, "smb_io_unihdr");
414 if(!prs_uint16("uni_str_len", ps, depth, &hdr->uni_str_len))
416 if(!prs_uint16("uni_max_len", ps, depth, &hdr->uni_max_len))
418 if(!prs_uint32("buffer ", ps, depth, &hdr->buffer))
424 /*******************************************************************
425 Inits a BUFHDR structure.
426 ********************************************************************/
428 void init_buf_hdr(BUFHDR *hdr, int max_len, int len)
430 hdr->buf_max_len = max_len;
434 /*******************************************************************
435 prs_uint16 wrapper. Call this and it sets up a pointer to where the
436 uint16 should be stored, or gets the size if reading.
437 ********************************************************************/
439 BOOL smb_io_hdrbuf_pre(const char *desc, BUFHDR *hdr, prs_struct *ps, int depth, uint32 *offset)
441 (*offset) = prs_offset(ps);
446 if(!smb_io_hdrbuf(desc, hdr, ps, depth))
453 if(!prs_set_offset(ps, prs_offset(ps) + (sizeof(uint32) * 2)))
460 /*******************************************************************
461 smb_io_hdrbuf wrapper. Call this and it retrospectively stores the size.
462 Does nothing on reading, as that is already handled by ...._pre()
463 ********************************************************************/
465 BOOL smb_io_hdrbuf_post(const char *desc, BUFHDR *hdr, prs_struct *ps, int depth,
466 uint32 ptr_hdrbuf, uint32 max_len, uint32 len)
469 /* writing: go back and do a retrospective job. i hate this */
471 uint32 old_offset = prs_offset(ps);
473 init_buf_hdr(hdr, max_len, len);
474 if(!prs_set_offset(ps, ptr_hdrbuf))
476 if(!smb_io_hdrbuf(desc, hdr, ps, depth))
479 if(!prs_set_offset(ps, old_offset))
486 /*******************************************************************
487 Reads or writes a BUFHDR structure.
488 ********************************************************************/
490 BOOL smb_io_hdrbuf(const char *desc, BUFHDR *hdr, prs_struct *ps, int depth)
495 prs_debug(ps, depth, desc, "smb_io_hdrbuf");
501 if(!prs_uint32("buf_max_len", ps, depth, &hdr->buf_max_len))
503 if(!prs_uint32("buf_len ", ps, depth, &hdr->buf_len))
509 /*******************************************************************
510 creates a UNIHDR2 structure.
511 ********************************************************************/
513 void init_uni_hdr2(UNIHDR2 *hdr, UNISTR2 *str2)
515 init_uni_hdr(&hdr->unihdr, str2);
516 hdr->buffer = (str2->uni_str_len > 0) ? 1 : 0;
519 /*******************************************************************
520 Reads or writes a UNIHDR2 structure.
521 ********************************************************************/
523 BOOL smb_io_unihdr2(const char *desc, UNIHDR2 *hdr2, prs_struct *ps, int depth)
528 prs_debug(ps, depth, desc, "smb_io_unihdr2");
534 if(!smb_io_unihdr("hdr", &hdr2->unihdr, ps, depth))
536 if(!prs_uint32("buffer", ps, depth, &hdr2->buffer))
542 /*******************************************************************
543 Inits a UNISTR structure.
544 ********************************************************************/
546 void init_unistr(UNISTR *str, const char *buf)
555 len = strlen(buf) + 1;
557 str->buffer = TALLOC_ZERO_ARRAY(get_talloc_ctx(), uint16, len);
558 if (str->buffer == NULL)
559 smb_panic("init_unistr: malloc fail\n");
561 rpcstr_push(str->buffer, buf, len*sizeof(uint16), STR_TERMINATE);
564 /*******************************************************************
565 reads or writes a UNISTR structure.
566 XXXX NOTE: UNISTR structures NEED to be null-terminated.
567 ********************************************************************/
569 BOOL smb_io_unistr(const char *desc, UNISTR *uni, prs_struct *ps, int depth)
574 prs_debug(ps, depth, desc, "smb_io_unistr");
577 if(!prs_unistr("unistr", ps, depth, uni))
583 /*******************************************************************
584 Allocate the BUFFER3 memory.
585 ********************************************************************/
587 static size_t create_buffer3(BUFFER3 *str, size_t len)
589 str->buffer = TALLOC_ZERO(get_talloc_ctx(), len);
590 if (str->buffer == NULL)
591 smb_panic("create_buffer3: talloc fail\n");
595 /*******************************************************************
596 Inits a BUFFER3 structure from a uint32
597 ********************************************************************/
599 void init_buffer3_uint32(BUFFER3 *str, uint32 val)
603 /* set up string lengths. */
604 str->buf_max_len = str->buf_len = create_buffer3(str, sizeof(uint32));
605 SIVAL(str->buffer, 0, val);
608 /*******************************************************************
609 Inits a BUFFER3 structure.
610 ********************************************************************/
612 void init_buffer3_str(BUFFER3 *str, const char *buf, int len)
616 /* set up string lengths. */
617 str->buf_max_len = str->buf_len = create_buffer3(str, len*2);
618 rpcstr_push(str->buffer, buf, str->buf_max_len, STR_TERMINATE);
622 /*******************************************************************
623 Inits a BUFFER3 structure from a hex string.
624 ********************************************************************/
626 void init_buffer3_hex(BUFFER3 *str, const char *buf)
629 str->buf_max_len = str->buf_len = create_buffer3(str, strlen(buf));
630 str->buf_max_len = str->buf_len = strhex_to_str((char *)str->buffer, str->buf_len, buf);
633 /*******************************************************************
634 Inits a BUFFER3 structure.
635 ********************************************************************/
637 void init_buffer3_bytes(BUFFER3 *str, uint8 *buf, size_t len)
641 /* max buffer size (allocated size) */
643 len = create_buffer3(str, len);
644 memcpy(str->buffer, buf, len);
646 str->buf_max_len = len;
647 str->buf_len = buf != NULL ? len : 0;
650 /*******************************************************************
651 Reads or writes a BUFFER3 structure.
652 the uni_max_len member tells you how large the buffer is.
653 the uni_str_len member tells you how much of the buffer is really used.
654 ********************************************************************/
656 BOOL smb_io_buffer3(const char *desc, BUFFER3 *buf3, prs_struct *ps, int depth)
661 prs_debug(ps, depth, desc, "smb_io_buffer3");
667 if(!prs_uint32("uni_max_len", ps, depth, &buf3->buf_max_len))
670 if (UNMARSHALLING(ps)) {
671 buf3->buffer = PRS_ALLOC_MEM(ps, unsigned char, buf3->buf_max_len);
672 if (buf3->buffer == NULL)
676 if(!prs_uint8s(True, "buffer ", ps, depth, buf3->buffer, buf3->buf_max_len))
679 if(!prs_uint32("buf_len ", ps, depth, &buf3->buf_len))
685 /*******************************************************************
686 reads or writes a BUFFER5 structure.
687 the buf_len member tells you how large the buffer is.
688 ********************************************************************/
689 BOOL smb_io_buffer5(const char *desc, BUFFER5 *buf5, prs_struct *ps, int depth)
691 prs_debug(ps, depth, desc, "smb_io_buffer5");
694 if (buf5 == NULL) return False;
698 if(!prs_uint32("buf_len", ps, depth, &buf5->buf_len))
702 if(!prs_buffer5(True, "buffer" , ps, depth, buf5))
709 /*******************************************************************
710 Inits a BUFFER2 structure.
711 ********************************************************************/
713 void init_buffer2(BUFFER2 *str, const uint8 *buf, size_t len)
717 /* max buffer size (allocated size) */
718 str->buf_max_len = len;
720 str->buf_len = buf != NULL ? len : 0;
723 SMB_ASSERT(str->buf_max_len >= str->buf_len);
724 str->buffer = TALLOC_ZERO(get_talloc_ctx(), str->buf_max_len);
725 if (str->buffer == NULL)
726 smb_panic("init_buffer2: talloc fail\n");
727 memcpy(str->buffer, buf, str->buf_len);
731 /*******************************************************************
732 Reads or writes a BUFFER2 structure.
733 the uni_max_len member tells you how large the buffer is.
734 the uni_str_len member tells you how much of the buffer is really used.
735 ********************************************************************/
737 BOOL smb_io_buffer2(const char *desc, BUFFER2 *buf2, uint32 buffer, prs_struct *ps, int depth)
744 prs_debug(ps, depth, desc, "smb_io_buffer2");
750 if(!prs_uint32("uni_max_len", ps, depth, &buf2->buf_max_len))
752 if(!prs_uint32("offset ", ps, depth, &buf2->offset))
754 if(!prs_uint32("buf_len ", ps, depth, &buf2->buf_len))
757 /* buffer advanced by indicated length of string
758 NOT by searching for null-termination */
760 if(!prs_buffer2(True, "buffer ", ps, depth, buf2))
765 prs_debug(ps, depth, desc, "smb_io_buffer2 - NULL");
767 memset((char *)buf2, '\0', sizeof(*buf2));
773 /*******************************************************************
774 creates a UNISTR2 structure: sets up the buffer, too
775 ********************************************************************/
777 void init_buf_unistr2(UNISTR2 *str, uint32 *ptr, const char *buf)
781 init_unistr2(str, buf, UNI_STR_TERMINATE);
784 init_unistr2(str, NULL, UNI_FLAGS_NONE);
789 /*******************************************************************
790 Copies a UNISTR2 structure.
791 ********************************************************************/
793 void copy_unistr2(UNISTR2 *str, const UNISTR2 *from)
795 if (from->buffer == NULL) {
800 SMB_ASSERT(from->uni_max_len >= from->uni_str_len);
802 str->uni_max_len = from->uni_max_len;
803 str->offset = from->offset;
804 str->uni_str_len = from->uni_str_len;
806 /* the string buffer is allocated to the maximum size
807 (the the length of the source string) to prevent
808 reallocation of memory. */
809 if (str->buffer == NULL) {
810 str->buffer = (uint16 *)TALLOC_ZERO_ARRAY(get_talloc_ctx(), uint16, str->uni_max_len);
811 if ((str->buffer == NULL)) {
812 smb_panic("copy_unistr2: talloc fail\n");
817 /* copy the string */
818 memcpy(str->buffer, from->buffer, str->uni_max_len*sizeof(uint16));
821 /*******************************************************************
822 Creates a STRING2 structure.
823 ********************************************************************/
825 void init_string2(STRING2 *str, const char *buf, size_t max_len, size_t str_len)
827 /* set up string lengths. */
828 SMB_ASSERT(max_len >= str_len);
830 str->str_max_len = max_len;
832 str->str_str_len = str_len;
834 /* store the string */
836 str->buffer = TALLOC_ZERO(get_talloc_ctx(), str->str_max_len);
837 if (str->buffer == NULL)
838 smb_panic("init_string2: malloc fail\n");
839 memcpy(str->buffer, buf, str_len);
843 /*******************************************************************
844 Reads or writes a STRING2 structure.
845 XXXX NOTE: STRING2 structures need NOT be null-terminated.
846 the str_str_len member tells you how long the string is;
847 the str_max_len member tells you how large the buffer is.
848 ********************************************************************/
850 BOOL smb_io_string2(const char *desc, STRING2 *str2, uint32 buffer, prs_struct *ps, int depth)
857 prs_debug(ps, depth, desc, "smb_io_string2");
863 if(!prs_uint32("str_max_len", ps, depth, &str2->str_max_len))
865 if(!prs_uint32("offset ", ps, depth, &str2->offset))
867 if(!prs_uint32("str_str_len", ps, depth, &str2->str_str_len))
870 /* buffer advanced by indicated length of string
871 NOT by searching for null-termination */
872 if(!prs_string2(True, "buffer ", ps, depth, str2))
877 prs_debug(ps, depth, desc, "smb_io_string2 - NULL");
879 memset((char *)str2, '\0', sizeof(*str2));
886 /*******************************************************************
887 Inits a UNISTR2 structure.
888 ********************************************************************/
890 void init_unistr2(UNISTR2 *str, const char *buf, enum unistr2_term_codes flags)
893 uint32 num_chars = 0;
896 /* We always null terminate the copy. */
897 len = strlen(buf) + 1;
899 /* no buffer -- nothing to do */
900 str->uni_max_len = 0;
902 str->uni_str_len = 0;
908 str->buffer = TALLOC_ZERO_ARRAY(get_talloc_ctx(), uint16, len);
909 if (str->buffer == NULL) {
910 smb_panic("init_unistr2: malloc fail\n");
914 /* Ensure len is the length in *bytes* */
915 len *= sizeof(uint16);
918 * The UNISTR2 must be initialized !!!
922 rpcstr_push((char *)str->buffer, buf, len, STR_TERMINATE);
923 num_chars = strlen_w(str->buffer);
924 if (flags == UNI_STR_TERMINATE || flags == UNI_MAXLEN_TERMINATE) {
929 str->uni_max_len = num_chars;
931 str->uni_str_len = num_chars;
932 if ( num_chars && ((flags == UNI_MAXLEN_TERMINATE) || (flags == UNI_BROKEN_NON_NULL)) )
937 * Inits a UNISTR2 structure.
938 * @param ctx talloc context to allocate string on
939 * @param str pointer to string to create
940 * @param buf UCS2 null-terminated buffer to init from
943 void init_unistr2_w(TALLOC_CTX *ctx, UNISTR2 *str, const smb_ucs2_t *buf)
945 uint32 len = strlen_w(buf);
949 /* set up string lengths. */
950 str->uni_max_len = len;
952 str->uni_str_len = len;
954 str->buffer = TALLOC_ZERO_ARRAY(ctx, uint16, len + 1);
955 if (str->buffer == NULL) {
956 smb_panic("init_unistr2_w: malloc fail\n");
961 * don't move this test above ! The UNISTR2 must be initialized !!!
967 /* Yes, this is a strncpy( foo, bar, strlen(bar)) - but as
968 long as the buffer above is talloc()ed correctly then this
969 is the correct thing to do */
970 strncpy_w(str->buffer, buf, len + 1);
973 /*******************************************************************
974 Inits a UNISTR2 structure from a UNISTR
975 ********************************************************************/
977 void init_unistr2_from_unistr(UNISTR2 *to, const UNISTR *from)
981 /* the destination UNISTR2 should never be NULL.
982 if it is it is a programming error */
984 /* if the source UNISTR is NULL, then zero out
985 the destination string and return */
987 if ((from == NULL) || (from->buffer == NULL))
990 /* get the length; UNISTR must be NULL terminated */
992 while ((from->buffer)[i]!='\0')
994 i++; /* one more to catch the terminating NULL */
995 /* is this necessary -- jerry? I need to think */
997 /* set up string lengths; uni_max_len is set to i+1
998 because we need to account for the final NULL termination */
1001 to->uni_str_len = i;
1003 /* allocate the space and copy the string buffer */
1004 to->buffer = TALLOC_ZERO_ARRAY(get_talloc_ctx(), uint16, i);
1005 if (to->buffer == NULL)
1006 smb_panic("init_unistr2_from_unistr: malloc fail\n");
1007 memcpy(to->buffer, from->buffer, i*sizeof(uint16));
1011 /*******************************************************************
1012 Inits a UNISTR2 structure from a DATA_BLOB.
1013 The length of the data_blob must count the bytes of the buffer.
1014 Copies the blob data.
1015 ********************************************************************/
1017 void init_unistr2_from_datablob(UNISTR2 *str, DATA_BLOB *blob)
1019 /* Allocs the unistring */
1020 init_unistr2(str, NULL, UNI_FLAGS_NONE);
1022 /* Sets the values */
1023 str->uni_str_len = blob->length / sizeof(uint16);
1024 str->uni_max_len = str->uni_str_len;
1027 str->buffer = (uint16 *) memdup(blob->data, blob->length);
1031 if ((str->buffer == NULL) && (blob->length > 0)) {
1032 smb_panic("init_unistr2_from_datablob: malloc fail\n");
1036 /*******************************************************************
1037 Reads or writes a UNISTR2 structure.
1038 XXXX NOTE: UNISTR2 structures need NOT be null-terminated.
1039 the uni_str_len member tells you how long the string is;
1040 the uni_max_len member tells you how large the buffer is.
1041 ********************************************************************/
1043 BOOL smb_io_unistr2(const char *desc, UNISTR2 *uni2, uint32 buffer, prs_struct *ps, int depth)
1050 prs_debug(ps, depth, desc, "smb_io_unistr2");
1056 if(!prs_uint32("uni_max_len", ps, depth, &uni2->uni_max_len))
1058 if(!prs_uint32("offset ", ps, depth, &uni2->offset))
1060 if(!prs_uint32("uni_str_len", ps, depth, &uni2->uni_str_len))
1063 /* buffer advanced by indicated length of string
1064 NOT by searching for null-termination */
1065 if(!prs_unistr2(True, "buffer ", ps, depth, uni2))
1070 prs_debug(ps, depth, desc, "smb_io_unistr2 - NULL");
1072 memset((char *)uni2, '\0', sizeof(*uni2));
1081 initialise a UNISTR_ARRAY from a char**
1083 BOOL init_unistr2_array(UNISTR2_ARRAY *array,
1084 uint32 count, const char **strings)
1088 array->count = count;
1089 array->ref_id = count?1:0;
1090 if (array->count == 0) {
1094 array->strings = TALLOC_ZERO_ARRAY(get_talloc_ctx(), UNISTR2_ARRAY_EL, count );
1095 if (!array->strings) {
1099 for (i=0;i<count;i++) {
1100 init_unistr2(&array->strings[i].string, strings[i], UNI_FLAGS_NONE);
1101 array->strings[i].size = array->strings[i].string.uni_max_len*2;
1102 array->strings[i].length = array->strings[i].size;
1103 array->strings[i].ref_id = 1;
1109 BOOL smb_io_lockout_string_hdr(const char *desc, HDR_LOCKOUT_STRING *hdr_account_lockout, prs_struct *ps, int depth)
1111 prs_debug(ps, depth, desc, "smb_io_lockout_string_hdr");
1117 if(!prs_uint16("size", ps, depth, &hdr_account_lockout->size))
1119 if(!prs_uint16("length", ps, depth, &hdr_account_lockout->length))
1121 if(!prs_uint32("buffer", ps, depth, &hdr_account_lockout->buffer))
1127 BOOL smb_io_account_lockout_str(const char *desc, LOCKOUT_STRING *account_lockout, uint32 buffer, prs_struct *ps, int depth)
1129 prs_debug(ps, depth, desc, "smb_io_account_lockout_string");
1132 if(!prs_uint32("array_size", ps, depth, &account_lockout->array_size))
1135 if(!prs_uint32("offset", ps, depth, &account_lockout->offset))
1137 if(!prs_uint32("length", ps, depth, &account_lockout->length))
1140 if (!prs_uint64("lockout_duration", ps, depth, &account_lockout->lockout_duration))
1142 if (!prs_uint64("reset_count", ps, depth, &account_lockout->reset_count))
1144 if (!prs_uint32("bad_attempt_lockout", ps, depth, &account_lockout->bad_attempt_lockout))
1146 if (!prs_uint32("dummy", ps, depth, &account_lockout->dummy))
1149 if(!prs_uint16s (False, "bindata", ps, depth, &account_lockout->bindata, length))
1156 /*******************************************************************
1157 Reads or writes a UNISTR2_ARRAY structure.
1158 ********************************************************************/
1159 BOOL smb_io_unistr2_array(const char *desc, UNISTR2_ARRAY *array, prs_struct *ps, int depth)
1163 prs_debug(ps, depth, desc, "smb_io_unistr2_array");
1166 if(!prs_uint32("ref_id", ps, depth, &array->ref_id))
1169 if (! array->ref_id) {
1173 if(!prs_uint32("count", ps, depth, &array->count))
1176 if (array->count == 0) {
1180 if (UNMARSHALLING(ps)) {
1181 array->strings = TALLOC_ZERO_ARRAY(get_talloc_ctx(), UNISTR2_ARRAY_EL, array->count );
1183 if (! array->strings) {
1187 for (i=0;i<array->count;i++) {
1188 if(!prs_uint16("length", ps, depth, &array->strings[i].length))
1190 if(!prs_uint16("size", ps, depth, &array->strings[i].size))
1192 if(!prs_uint32("ref_id", ps, depth, &array->strings[i].ref_id))
1196 for (i=0;i<array->count;i++) {
1197 if (! smb_io_unistr2("string", &array->strings[i].string, array->strings[i].ref_id, ps, depth))
1205 /*******************************************************************
1206 Inits a DOM_RID2 structure.
1207 ********************************************************************/
1209 void init_dom_rid2(DOM_RID2 *rid2, uint32 rid, uint8 type, uint32 idx)
1213 rid2->rid_idx = idx;
1216 /*******************************************************************
1217 Reads or writes a DOM_RID2 structure.
1218 ********************************************************************/
1220 BOOL smb_io_dom_rid2(const char *desc, DOM_RID2 *rid2, prs_struct *ps, int depth)
1225 prs_debug(ps, depth, desc, "smb_io_dom_rid2");
1231 if(!prs_uint8("type ", ps, depth, &rid2->type))
1235 if(!prs_uint32("rid ", ps, depth, &rid2->rid))
1237 if(!prs_uint32("rid_idx", ps, depth, &rid2->rid_idx))
1243 /*******************************************************************
1244 creates a DOM_RID3 structure.
1245 ********************************************************************/
1247 void init_dom_rid3(DOM_RID3 *rid3, uint32 rid, uint8 type)
1251 rid3->ptr_type = 0x1; /* non-zero, basically. */
1256 /*******************************************************************
1257 reads or writes a DOM_RID3 structure.
1258 ********************************************************************/
1260 BOOL smb_io_dom_rid3(const char *desc, DOM_RID3 *rid3, prs_struct *ps, int depth)
1265 prs_debug(ps, depth, desc, "smb_io_dom_rid3");
1271 if(!prs_uint32("rid ", ps, depth, &rid3->rid))
1273 if(!prs_uint32("type1 ", ps, depth, &rid3->type1))
1275 if(!prs_uint32("ptr_type", ps, depth, &rid3->ptr_type))
1277 if(!prs_uint32("type2 ", ps, depth, &rid3->type2))
1279 if(!prs_uint32("unk ", ps, depth, &rid3->unk))
1285 /*******************************************************************
1286 Inits a DOM_RID4 structure.
1287 ********************************************************************/
1289 void init_dom_rid4(DOM_RID4 *rid4, uint16 unknown, uint16 attr, uint32 rid)
1291 rid4->unknown = unknown;
1296 /*******************************************************************
1297 Inits a DOM_CLNT_SRV structure.
1298 ********************************************************************/
1300 static void init_clnt_srv(DOM_CLNT_SRV *logcln, const char *logon_srv, const char *comp_name)
1302 DEBUG(5,("init_clnt_srv: %d\n", __LINE__));
1304 if (logon_srv != NULL) {
1305 logcln->undoc_buffer = 1;
1306 init_unistr2(&logcln->uni_logon_srv, logon_srv, UNI_STR_TERMINATE);
1308 logcln->undoc_buffer = 0;
1311 if (comp_name != NULL) {
1312 logcln->undoc_buffer2 = 1;
1313 init_unistr2(&logcln->uni_comp_name, comp_name, UNI_STR_TERMINATE);
1315 logcln->undoc_buffer2 = 0;
1319 /*******************************************************************
1320 Inits or writes a DOM_CLNT_SRV structure.
1321 ********************************************************************/
1323 static BOOL smb_io_clnt_srv(const char *desc, DOM_CLNT_SRV *logcln, prs_struct *ps, int depth)
1328 prs_debug(ps, depth, desc, "smb_io_clnt_srv");
1334 if(!prs_uint32("undoc_buffer ", ps, depth, &logcln->undoc_buffer))
1337 if (logcln->undoc_buffer != 0) {
1338 if(!smb_io_unistr2("unistr2", &logcln->uni_logon_srv, logcln->undoc_buffer, ps, depth))
1345 if(!prs_uint32("undoc_buffer2", ps, depth, &logcln->undoc_buffer2))
1348 if (logcln->undoc_buffer2 != 0) {
1349 if(!smb_io_unistr2("unistr2", &logcln->uni_comp_name, logcln->undoc_buffer2, ps, depth))
1356 /*******************************************************************
1357 Inits a DOM_LOG_INFO structure.
1358 ********************************************************************/
1360 void init_log_info(DOM_LOG_INFO *loginfo, const char *logon_srv, const char *acct_name,
1361 uint16 sec_chan, const char *comp_name)
1363 DEBUG(5,("make_log_info %d\n", __LINE__));
1365 loginfo->undoc_buffer = 1;
1367 init_unistr2(&loginfo->uni_logon_srv, logon_srv, UNI_STR_TERMINATE);
1368 init_unistr2(&loginfo->uni_acct_name, acct_name, UNI_STR_TERMINATE);
1370 loginfo->sec_chan = sec_chan;
1372 init_unistr2(&loginfo->uni_comp_name, comp_name, UNI_STR_TERMINATE);
1375 /*******************************************************************
1376 Reads or writes a DOM_LOG_INFO structure.
1377 ********************************************************************/
1379 BOOL smb_io_log_info(const char *desc, DOM_LOG_INFO *loginfo, prs_struct *ps, int depth)
1381 if (loginfo == NULL)
1384 prs_debug(ps, depth, desc, "smb_io_log_info");
1390 if(!prs_uint32("undoc_buffer", ps, depth, &loginfo->undoc_buffer))
1393 if(!smb_io_unistr2("unistr2", &loginfo->uni_logon_srv, True, ps, depth))
1395 if(!smb_io_unistr2("unistr2", &loginfo->uni_acct_name, True, ps, depth))
1398 if(!prs_uint16("sec_chan", ps, depth, &loginfo->sec_chan))
1401 if(!smb_io_unistr2("unistr2", &loginfo->uni_comp_name, True, ps, depth))
1407 /*******************************************************************
1408 Reads or writes a DOM_CHAL structure.
1409 ********************************************************************/
1411 BOOL smb_io_chal(const char *desc, DOM_CHAL *chal, prs_struct *ps, int depth)
1416 prs_debug(ps, depth, desc, "smb_io_chal");
1419 if(!prs_uint8s (False, "data", ps, depth, chal->data, 8))
1425 /*******************************************************************
1426 Reads or writes a DOM_CRED structure.
1427 ********************************************************************/
1429 BOOL smb_io_cred(const char *desc, DOM_CRED *cred, prs_struct *ps, int depth)
1434 prs_debug(ps, depth, desc, "smb_io_cred");
1440 if(!smb_io_chal ("", &cred->challenge, ps, depth))
1443 if(!smb_io_utime("", &cred->timestamp, ps, depth))
1449 /*******************************************************************
1450 Inits a DOM_CLNT_INFO2 structure.
1451 ********************************************************************/
1453 void init_clnt_info2(DOM_CLNT_INFO2 *clnt,
1454 const char *logon_srv, const char *comp_name,
1455 const DOM_CRED *clnt_cred)
1457 DEBUG(5,("make_clnt_info: %d\n", __LINE__));
1459 init_clnt_srv(&clnt->login, logon_srv, comp_name);
1461 if (clnt_cred != NULL) {
1463 memcpy(&clnt->cred, clnt_cred, sizeof(clnt->cred));
1469 /*******************************************************************
1470 Reads or writes a DOM_CLNT_INFO2 structure.
1471 ********************************************************************/
1473 BOOL smb_io_clnt_info2(const char *desc, DOM_CLNT_INFO2 *clnt, prs_struct *ps, int depth)
1478 prs_debug(ps, depth, desc, "smb_io_clnt_info2");
1484 if(!smb_io_clnt_srv("", &clnt->login, ps, depth))
1490 if(!prs_uint32("ptr_cred", ps, depth, &clnt->ptr_cred))
1492 if(!smb_io_cred("", &clnt->cred, ps, depth))
1498 /*******************************************************************
1499 Inits a DOM_CLNT_INFO structure.
1500 ********************************************************************/
1502 void init_clnt_info(DOM_CLNT_INFO *clnt,
1503 const char *logon_srv, const char *acct_name,
1504 uint16 sec_chan, const char *comp_name,
1505 const DOM_CRED *cred)
1507 DEBUG(5,("make_clnt_info\n"));
1509 init_log_info(&clnt->login, logon_srv, acct_name, sec_chan, comp_name);
1510 memcpy(&clnt->cred, cred, sizeof(clnt->cred));
1513 /*******************************************************************
1514 Reads or writes a DOM_CLNT_INFO structure.
1515 ********************************************************************/
1517 BOOL smb_io_clnt_info(const char *desc, DOM_CLNT_INFO *clnt, prs_struct *ps, int depth)
1522 prs_debug(ps, depth, desc, "smb_io_clnt_info");
1528 if(!smb_io_log_info("", &clnt->login, ps, depth))
1530 if(!smb_io_cred("", &clnt->cred, ps, depth))
1536 /*******************************************************************
1537 Inits a DOM_LOGON_ID structure.
1538 ********************************************************************/
1540 void init_logon_id(DOM_LOGON_ID *logonid, uint32 log_id_low, uint32 log_id_high)
1542 DEBUG(5,("make_logon_id: %d\n", __LINE__));
1544 logonid->low = log_id_low;
1545 logonid->high = log_id_high;
1548 /*******************************************************************
1549 Reads or writes a DOM_LOGON_ID structure.
1550 ********************************************************************/
1552 BOOL smb_io_logon_id(const char *desc, DOM_LOGON_ID *logonid, prs_struct *ps, int depth)
1554 if (logonid == NULL)
1557 prs_debug(ps, depth, desc, "smb_io_logon_id");
1563 if(!prs_uint32("low ", ps, depth, &logonid->low ))
1565 if(!prs_uint32("high", ps, depth, &logonid->high))
1571 /*******************************************************************
1572 Inits an OWF_INFO structure.
1573 ********************************************************************/
1575 void init_owf_info(OWF_INFO *hash, const uint8 data[16])
1577 DEBUG(5,("init_owf_info: %d\n", __LINE__));
1580 memcpy(hash->data, data, sizeof(hash->data));
1582 memset((char *)hash->data, '\0', sizeof(hash->data));
1585 /*******************************************************************
1586 Reads or writes an OWF_INFO structure.
1587 ********************************************************************/
1589 BOOL smb_io_owf_info(const char *desc, OWF_INFO *hash, prs_struct *ps, int depth)
1594 prs_debug(ps, depth, desc, "smb_io_owf_info");
1600 if(!prs_uint8s (False, "data", ps, depth, hash->data, 16))
1606 /*******************************************************************
1607 Reads or writes a DOM_GID structure.
1608 ********************************************************************/
1610 BOOL smb_io_gid(const char *desc, DOM_GID *gid, prs_struct *ps, int depth)
1615 prs_debug(ps, depth, desc, "smb_io_gid");
1621 if(!prs_uint32("g_rid", ps, depth, &gid->g_rid))
1623 if(!prs_uint32("attr ", ps, depth, &gid->attr))
1629 /*******************************************************************
1630 Reads or writes an POLICY_HND structure.
1631 ********************************************************************/
1633 BOOL smb_io_pol_hnd(const char *desc, POLICY_HND *pol, prs_struct *ps, int depth)
1638 prs_debug(ps, depth, desc, "smb_io_pol_hnd");
1644 if(UNMARSHALLING(ps))
1647 if (!prs_uint32("data1", ps, depth, &pol->data1))
1649 if (!prs_uint32("data2", ps, depth, &pol->data2))
1651 if (!prs_uint16("data3", ps, depth, &pol->data3))
1653 if (!prs_uint16("data4", ps, depth, &pol->data4))
1655 if(!prs_uint8s (False, "data5", ps, depth, pol->data5, sizeof(pol->data5)))
1661 /*******************************************************************
1663 ********************************************************************/
1665 void init_unistr3(UNISTR3 *str, const char *buf)
1669 str->str.buffer = NULL;
1673 str->uni_str_len = strlen(buf) + 1;
1675 str->str.buffer = TALLOC_ZERO_ARRAY(get_talloc_ctx(), uint16, str->uni_str_len);
1676 if (str->str.buffer == NULL)
1677 smb_panic("init_unistr3: malloc fail\n");
1679 rpcstr_push((char *)str->str.buffer, buf, str->uni_str_len * sizeof(uint16), STR_TERMINATE);
1682 /*******************************************************************
1683 Reads or writes a UNISTR3 structure.
1684 ********************************************************************/
1686 BOOL smb_io_unistr3(const char *desc, UNISTR3 *name, prs_struct *ps, int depth)
1691 prs_debug(ps, depth, desc, "smb_io_unistr3");
1697 if(!prs_uint32("uni_str_len", ps, depth, &name->uni_str_len))
1700 /* don't know if len is specified by uni_str_len member... */
1701 /* assume unicode string is unicode-null-terminated, instead */
1703 if(!prs_unistr3(True, "unistr", name, ps, depth))
1710 /*******************************************************************
1711 Stream a uint64_struct
1712 ********************************************************************/
1713 BOOL prs_uint64(const char *name, prs_struct *ps, int depth, UINT64_S *data64)
1715 return prs_uint32(name, ps, depth+1, &data64->low) &&
1716 prs_uint32(name, ps, depth+1, &data64->high);
1719 /*******************************************************************
1720 reads or writes a BUFHDR2 structure.
1721 ********************************************************************/
1722 BOOL smb_io_bufhdr2(const char *desc, BUFHDR2 *hdr, prs_struct *ps, int depth)
1724 prs_debug(ps, depth, desc, "smb_io_bufhdr2");
1728 prs_uint32("info_level", ps, depth, &(hdr->info_level));
1729 prs_uint32("length ", ps, depth, &(hdr->length ));
1730 prs_uint32("buffer ", ps, depth, &(hdr->buffer ));
1735 /*******************************************************************
1736 reads or writes a BUFHDR4 structure.
1737 ********************************************************************/
1738 BOOL smb_io_bufhdr4(const char *desc, BUFHDR4 *hdr, prs_struct *ps, int depth)
1740 prs_debug(ps, depth, desc, "smb_io_bufhdr4");
1744 prs_uint32("size", ps, depth, &hdr->size);
1745 prs_uint32("buffer", ps, depth, &hdr->buffer);
1750 /*******************************************************************
1751 reads or writes a BUFFER4 structure.
1752 ********************************************************************/
1754 BOOL smb_io_buffer4(const char *desc, BUFFER4 *buf4, uint32 buffer, prs_struct *ps, int depth)
1756 prs_debug(ps, depth, desc, "smb_io_buffer4");
1760 prs_uint32("buf_len", ps, depth, &buf4->buf_len);
1761 if (UNMARSHALLING(ps)) {
1762 buf4->buffer = PRS_ALLOC_MEM(ps, uint8, buf4->buf_len);
1763 if (!buf4->buffer) {
1767 prs_uint8s(True, "buffer", ps, depth, buf4->buffer, buf4->buf_len);
1772 /*******************************************************************
1773 creates a UNIHDR structure.
1774 ********************************************************************/
1776 BOOL make_uni_hdr(UNIHDR *hdr, int len)
1782 hdr->uni_str_len = 2 * len;
1783 hdr->uni_max_len = 2 * len;
1784 hdr->buffer = len != 0 ? 1 : 0;
1789 /*******************************************************************
1790 creates a BUFHDR2 structure.
1791 ********************************************************************/
1792 BOOL make_bufhdr2(BUFHDR2 *hdr, uint32 info_level, uint32 length, uint32 buffer)
1794 hdr->info_level = info_level;
1795 hdr->length = length;
1796 hdr->buffer = buffer;