* changed structure of REG_R_ENUM_VALUE structure since the BUFFER2
[bbaumbach/samba-autobuild/.git] / source / rpc_parse / parse_misc.c
index 12501a5e57b6e8049d4071453b83e322e122059d..01d76981736f5bb9e7c610b3b35b2a21de99c29a 100644 (file)
@@ -1,7 +1,5 @@
-
 /* 
- *  Unix SMB/Netbios implementation.
- *  Version 1.9.
+ *  Unix SMB/CIFS implementation.
  *  RPC Pipe client / server routines
  *  Copyright (C) Andrew Tridgell              1992-1997,
  *  Copyright (C) Luke Kenneth Casson Leighton 1996-1997,
  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
 
-
 #include "includes.h"
 
-extern int DEBUGLEVEL;
+#undef DBGC_CLASS
+#define DBGC_CLASS DBGC_RPC_PARSE
+
+/****************************************************************************
+ A temporary TALLOC context for things like unistrs, that is valid for
+ the life of a complete RPC call.
+****************************************************************************/
+
+static TALLOC_CTX *current_rpc_talloc = NULL;
+
+TALLOC_CTX *get_current_rpc_talloc(void)
+{
+    return current_rpc_talloc;
+}
+
+void set_current_rpc_talloc( TALLOC_CTX *ctx)
+{
+       current_rpc_talloc = ctx;
+}
+
+static TALLOC_CTX *main_loop_talloc = NULL;
+
+/*******************************************************************
+free up temporary memory - called from the main loop
+********************************************************************/
+
+void main_loop_talloc_free(void)
+{
+    if (!main_loop_talloc)
+        return;
+    talloc_destroy(main_loop_talloc);
+    main_loop_talloc = NULL;
+}
+
+/*******************************************************************
+ Get a talloc context that is freed in the main loop...
+********************************************************************/
+
+TALLOC_CTX *main_loop_talloc_get(void)
+{
+    if (!main_loop_talloc) {
+        main_loop_talloc = talloc_init_named("main loop talloc (mainly parse_misc)");
+        if (!main_loop_talloc)
+            smb_panic("main_loop_talloc: malloc fail\n");
+    }
+
+    return main_loop_talloc;
+}
+
+/*******************************************************************
+ Try and get a talloc context. Get the rpc one if possible, else
+ get the main loop one. The main loop one is more dangerous as it
+ goes away between packets, the rpc one will stay around for as long
+ as a current RPC lasts.
+********************************************************************/ 
 
+TALLOC_CTX *get_talloc_ctx(void)
+{
+       TALLOC_CTX *tc = get_current_rpc_talloc();
 
+       if (tc)
+               return tc;
+       return main_loop_talloc_get();
+}
 
 /*******************************************************************
-reads or writes a UTIME type.
+ Reads or writes a UTIME type.
 ********************************************************************/
-static void smb_io_utime(char *desc,  UTIME *t, prs_struct *ps, int depth)
+
+static BOOL smb_io_utime(char *desc, UTIME *t, prs_struct *ps, int depth)
 {
-       if (t == NULL) return;
+       if (t == NULL)
+               return False;
 
        prs_debug(ps, depth, desc, "smb_io_utime");
        depth++;
 
-       prs_align(ps);
+       if(!prs_align(ps))
+               return False;
        
-       prs_uint32 ("time", ps, depth, &(t->time));
+       if(!prs_uint32 ("time", ps, depth, &t->time))
+               return False;
+
+       return True;
 }
 
 /*******************************************************************
-reads or writes an NTTIME structure.
+ Reads or writes an NTTIME structure.
 ********************************************************************/
-void smb_io_time(char *desc,  NTTIME *nttime, prs_struct *ps, int depth)
+
+BOOL smb_io_time(char *desc, NTTIME *nttime, prs_struct *ps, int depth)
 {
-       if (nttime == NULL) return;
+       if (nttime == NULL)
+               return False;
 
        prs_debug(ps, depth, desc, "smb_io_time");
        depth++;
 
-       prs_align(ps);
+       if(!prs_align(ps))
+               return False;
        
-       prs_uint32("low ", ps, depth, &(nttime->low )); /* low part */
-       prs_uint32("high", ps, depth, &(nttime->high)); /* high part */
+       if(!prs_uint32("low ", ps, depth, &nttime->low)) /* low part */
+               return False;
+       if(!prs_uint32("high", ps, depth, &nttime->high)) /* high part */
+               return False;
+
+       return True;
 }
 
 /*******************************************************************
-reads or writes a LOOKUP_LEVEL structure.
+ Reads or writes a LOOKUP_LEVEL structure.
 ********************************************************************/
-void smb_io_lookup_level(char *desc, LOOKUP_LEVEL *level, prs_struct *ps, int depth)
+
+BOOL smb_io_lookup_level(char *desc, LOOKUP_LEVEL *level, prs_struct *ps, int depth)
 {
-       if (level == NULL) return;
+       if (level == NULL)
+               return False;
 
        prs_debug(ps, depth, desc, "smb_io_lookup_level");
        depth++;
 
-       prs_align(ps);
-       prs_uint16("value", ps, depth, &(level->value));
-       prs_align(ps);
+       if(!prs_align(ps))
+               return False;
+       if(!prs_uint16("value", ps, depth, &level->value))
+               return False;
+       if(!prs_align(ps))
+               return False;
+
+       return True;
 }
 
 /*******************************************************************
-gets an enumeration handle from an ENUM_HND structure.
+ Gets an enumeration handle from an ENUM_HND structure.
 ********************************************************************/
+
 uint32 get_enum_hnd(ENUM_HND *enh)
 {
        return (enh && enh->ptr_hnd != 0) ? enh->handle : 0;
 }
 
 /*******************************************************************
-makes an ENUM_HND structure.
+ Inits an ENUM_HND structure.
 ********************************************************************/
-void make_enum_hnd(ENUM_HND *enh, uint32 hnd)
-{
-       if (enh == NULL) return;
 
+void init_enum_hnd(ENUM_HND *enh, uint32 hnd)
+{
        DEBUG(5,("smb_io_enum_hnd\n"));
 
        enh->ptr_hnd = (hnd != 0) ? 1 : 0;
@@ -97,84 +175,158 @@ void make_enum_hnd(ENUM_HND *enh, uint32 hnd)
 }
 
 /*******************************************************************
-reads or writes an ENUM_HND structure.
+ Reads or writes an ENUM_HND structure.
 ********************************************************************/
-void smb_io_enum_hnd(char *desc,  ENUM_HND *hnd, prs_struct *ps, int depth)
+
+BOOL smb_io_enum_hnd(char *desc, ENUM_HND *hnd, prs_struct *ps, int depth)
 {
-       if (hnd == NULL) return;
+       if (hnd == NULL)
+               return False;
 
        prs_debug(ps, depth, desc, "smb_io_enum_hnd");
        depth++;
 
-       prs_align(ps);
+       if(!prs_align(ps))
+               return False;
        
-       prs_uint32("ptr_hnd", ps, depth, &(hnd->ptr_hnd)); /* pointer */
-       if (hnd->ptr_hnd != 0)
-       {
-               prs_uint32("handle ", ps, depth, &(hnd->handle )); /* enum handle */
+       if(!prs_uint32("ptr_hnd", ps, depth, &hnd->ptr_hnd)) /* pointer */
+               return False;
+
+       if (hnd->ptr_hnd != 0) {
+               if(!prs_uint32("handle ", ps, depth, &hnd->handle )) /* enum handle */
+                       return False;
        }
+
+       return True;
 }
 
 /*******************************************************************
-reads or writes a DOM_SID structure.
+ Reads or writes a DOM_SID structure.
 ********************************************************************/
-void smb_io_dom_sid(char *desc,  DOM_SID *sid, prs_struct *ps, int depth)
+
+BOOL smb_io_dom_sid(char *desc, DOM_SID *sid, prs_struct *ps, int depth)
 {
        int i;
 
-       if (sid == NULL) return;
+       if (sid == NULL)
+               return False;
 
        prs_debug(ps, depth, desc, "smb_io_dom_sid");
        depth++;
 
-       prs_align(ps);
-       
-       prs_uint8 ("sid_rev_num", ps, depth, &(sid->sid_rev_num)); 
-       prs_uint8 ("num_auths  ", ps, depth, &(sid->num_auths));
+       if(!prs_uint8 ("sid_rev_num", ps, depth, &sid->sid_rev_num))
+               return False;
+       if(!prs_uint8 ("num_auths  ", ps, depth, &sid->num_auths))
+               return False;
 
        for (i = 0; i < 6; i++)
        {
                fstring tmp;
                slprintf(tmp, sizeof(tmp) - 1, "id_auth[%d] ", i);
-               prs_uint8 (tmp, ps, depth, &(sid->id_auth[i]));
+               if(!prs_uint8 (tmp, ps, depth, &sid->id_auth[i]))
+                       return False;
        }
 
        /* oops! XXXX should really issue a warning here... */
-       if (sid->num_auths > MAXSUBAUTHS) sid->num_auths = MAXSUBAUTHS;
+       if (sid->num_auths > MAXSUBAUTHS)
+               sid->num_auths = MAXSUBAUTHS;
 
-       prs_uint32s(False, "sub_auths ", ps, depth, sid->sub_auths, sid->num_auths);
+       if(!prs_uint32s(False, "sub_auths ", ps, depth, sid->sub_auths, sid->num_auths))
+               return False;
+
+       return True;
 }
 
 /*******************************************************************
-creates a DOM_SID2 structure.
+ Inits a DOM_SID structure.
+
+ BIG NOTE: this function only does SIDS where the identauth is not >= 2^32 
+ identauth >= 2^32 can be detected because it will be specified in hex
 ********************************************************************/
-void make_dom_sid2(DOM_SID2 *sid2, DOM_SID *sid)
+
+void init_dom_sid(DOM_SID *sid, char *str_sid)
 {
-        sid_copy(&sid2->sid, sid);
+       pstring domsid;
+       int identauth;
+       char *p;
+
+       if (str_sid == NULL)
+       {
+               DEBUG(4,("netlogon domain SID: none\n"));
+               sid->sid_rev_num = 0;
+               sid->num_auths = 0;
+               return;
+       }
+               
+       pstrcpy(domsid, str_sid);
+
+       DEBUG(4,("init_dom_sid %d SID:  %s\n", __LINE__, domsid));
+
+       /* assume, but should check, that domsid starts "S-" */
+       p = strtok(domsid+2,"-");
+       sid->sid_rev_num = atoi(p);
+
+       /* identauth in decimal should be <  2^32 */
+       /* identauth in hex     should be >= 2^32 */
+       identauth = atoi(strtok(0,"-"));
+
+       DEBUG(4,("netlogon rev %d\n", sid->sid_rev_num));
+       DEBUG(4,("netlogon %s ia %d\n", p, identauth));
+
+       sid->id_auth[0] = 0;
+       sid->id_auth[1] = 0;
+       sid->id_auth[2] = (identauth & 0xff000000) >> 24;
+       sid->id_auth[3] = (identauth & 0x00ff0000) >> 16;
+       sid->id_auth[4] = (identauth & 0x0000ff00) >> 8;
+       sid->id_auth[5] = (identauth & 0x000000ff);
+
+       sid->num_auths = 0;
+
+       while ((p = strtok(0, "-")) != NULL && sid->num_auths < MAXSUBAUTHS)
+               sid->sub_auths[sid->num_auths++] = atoi(p);
+
+       DEBUG(4,("init_dom_sid: %d SID:  %s\n", __LINE__, domsid));
+}
+
+/*******************************************************************
+ Inits a DOM_SID2 structure.
+********************************************************************/
+
+void init_dom_sid2(DOM_SID2 *sid2, const DOM_SID *sid)
+{
+       sid2->sid = *sid;
        sid2->num_auths = sid2->sid.num_auths;
 }
 
 /*******************************************************************
-reads or writes a DOM_SID2 structure.
+ Reads or writes a DOM_SID2 structure.
 ********************************************************************/
-void smb_io_dom_sid2(char *desc,  DOM_SID2 *sid, prs_struct *ps, int depth)
+
+BOOL smb_io_dom_sid2(char *desc, DOM_SID2 *sid, prs_struct *ps, int depth)
 {
-       if (sid == NULL) return;
+       if (sid == NULL)
+               return False;
 
        prs_debug(ps, depth, desc, "smb_io_dom_sid2");
        depth++;
 
-       prs_align(ps);
+       if(!prs_align(ps))
+               return False;
        
-       prs_uint32("num_auths", ps, depth, &(sid->num_auths));
+       if(!prs_uint32("num_auths", ps, depth, &sid->num_auths))
+               return False;
 
-       smb_io_dom_sid("sid", &(sid->sid), ps, depth);
+       if(!smb_io_dom_sid("sid", &sid->sid, ps, depth))
+               return False;
+
+       return True;
 }
 
 /*******************************************************************
 creates a STRHDR structure.
 ********************************************************************/
-void make_str_hdr(STRHDR *hdr, int max_len, int len, uint32 buffer)
+
+void init_str_hdr(STRHDR *hdr, int max_len, int len, uint32 buffer)
 {
        hdr->str_max_len = max_len;
        hdr->str_str_len = len;
@@ -182,173 +334,249 @@ void make_str_hdr(STRHDR *hdr, int max_len, int len, uint32 buffer)
 }
 
 /*******************************************************************
-reads or writes a STRHDR structure.
+ Reads or writes a STRHDR structure.
 ********************************************************************/
-void smb_io_strhdr(char *desc,  STRHDR *hdr, prs_struct *ps, int depth)
+
+BOOL smb_io_strhdr(char *desc,  STRHDR *hdr, prs_struct *ps, int depth)
 {
-       if (hdr == NULL) return;
+       if (hdr == NULL)
+               return False;
 
        prs_debug(ps, depth, desc, "smb_io_strhdr");
        depth++;
 
        prs_align(ps);
        
-       prs_uint16("str_str_len", ps, depth, &(hdr->str_str_len));
-       prs_uint16("str_max_len", ps, depth, &(hdr->str_max_len));
-       prs_uint32("buffer     ", ps, depth, &(hdr->buffer     ));
-
-       /* oops! XXXX maybe issue a warning that this is happening... */
-       if (hdr->str_max_len > MAX_STRINGLEN) hdr->str_max_len = MAX_STRINGLEN;
-       if (hdr->str_str_len > MAX_STRINGLEN) hdr->str_str_len = MAX_STRINGLEN;
+       if(!prs_uint16("str_str_len", ps, depth, &hdr->str_str_len))
+               return False;
+       if(!prs_uint16("str_max_len", ps, depth, &hdr->str_max_len))
+               return False;
+       if(!prs_uint32("buffer     ", ps, depth, &hdr->buffer))
+               return False;
+
+       return True;
 }
 
 /*******************************************************************
-creates a UNIHDR structure.
+ Inits a UNIHDR structure.
 ********************************************************************/
-void make_uni_hdr(UNIHDR *hdr, int max_len, int len, uint32 buffer)
+
+void init_uni_hdr(UNIHDR *hdr, int len)
 {
        hdr->uni_str_len = 2 * len;
-       hdr->uni_max_len = 2 * max_len;
-       hdr->buffer      = buffer;
+       hdr->uni_max_len = 2 * len;
+       hdr->buffer      = len != 0 ? 1 : 0;
 }
 
 /*******************************************************************
-reads or writes a UNIHDR structure.
+ Reads or writes a UNIHDR structure.
 ********************************************************************/
-void smb_io_unihdr(char *desc,  UNIHDR *hdr, prs_struct *ps, int depth)
+
+BOOL smb_io_unihdr(char *desc, UNIHDR *hdr, prs_struct *ps, int depth)
 {
-       if (hdr == NULL) return;
+       if (hdr == NULL)
+               return False;
 
        prs_debug(ps, depth, desc, "smb_io_unihdr");
        depth++;
 
-       prs_align(ps);
+       if(!prs_align(ps))
+               return False;
        
-       prs_uint16("uni_str_len", ps, depth, &(hdr->uni_str_len));
-       prs_uint16("uni_max_len", ps, depth, &(hdr->uni_max_len));
-       prs_uint32("buffer     ", ps, depth, &(hdr->buffer     ));
-
-       /* oops! XXXX maybe issue a warning that this is happening... */
-       if (hdr->uni_max_len > MAX_UNISTRLEN) hdr->uni_max_len = MAX_UNISTRLEN;
-       if (hdr->uni_str_len > MAX_UNISTRLEN) hdr->uni_str_len = MAX_UNISTRLEN;
+       if(!prs_uint16("uni_str_len", ps, depth, &hdr->uni_str_len))
+               return False;
+       if(!prs_uint16("uni_max_len", ps, depth, &hdr->uni_max_len))
+               return False;
+       if(!prs_uint32("buffer     ", ps, depth, &hdr->buffer))
+               return False;
+
+       return True;
 }
 
 /*******************************************************************
-creates a BUFHDR structure.
+ Inits a BUFHDR structure.
 ********************************************************************/
-void make_buf_hdr(BUFHDR *hdr, int max_len, int len)
+
+void init_buf_hdr(BUFHDR *hdr, int max_len, int len)
 {
        hdr->buf_max_len = max_len;
        hdr->buf_len     = len;
 }
 
 /*******************************************************************
- prs_uint16 wrapper.  call this and it sets up a pointer to where the
- uint16 should be stored, or gets the size if reading
+ prs_uint16 wrapper. Call this and it sets up a pointer to where the
+ uint16 should be stored, or gets the size if reading.
  ********************************************************************/
-void smb_io_hdrbuf_pre(char *desc,  BUFHDR *hdr, prs_struct *ps, int depth, uint32 *offset)
+
+BOOL smb_io_hdrbuf_pre(char *desc, BUFHDR *hdr, prs_struct *ps, int depth, uint32 *offset)
 {
-       (*offset) = ps->offset;
-       if (ps->io)
-       {
+       (*offset) = prs_offset(ps);
+       if (ps->io) {
+
                /* reading. */
-               smb_io_hdrbuf(desc, hdr, ps, depth);
-       }
-       else
-       {
-               ps->offset += sizeof(uint32) * 2;
+
+               if(!smb_io_hdrbuf(desc, hdr, ps, depth))
+                       return False;
+
+       } else {
+
+               /* writing. */
+
+               if(!prs_set_offset(ps, prs_offset(ps) + (sizeof(uint32) * 2)))
+                       return False;
        }
+
+       return True;
 }
 
 /*******************************************************************
- smb_io_hdrbuf wrapper.  call this and it retrospectively stores the size.
does nothing on reading, as that is already handled by ...._pre()
+ smb_io_hdrbuf wrapper. Call this and it retrospectively stores the size.
Does nothing on reading, as that is already handled by ...._pre()
  ********************************************************************/
-void smb_io_hdrbuf_post(char *desc,  BUFHDR *hdr, prs_struct *ps, int depth, 
+
+BOOL smb_io_hdrbuf_post(char *desc, BUFHDR *hdr, prs_struct *ps, int depth, 
                                uint32 ptr_hdrbuf, uint32 max_len, uint32 len)
 {
-       if (!ps->io)
-       {
-               /* storing: go back and do a retrospective job.  i hate this */
-               uint32 old_offset = ps->offset;
+       if (!ps->io) {
+               /* writing: go back and do a retrospective job.  i hate this */
+
+               uint32 old_offset = prs_offset(ps);
 
-               make_buf_hdr(hdr, max_len, len);
-               ps->offset = ptr_hdrbuf;
-               smb_io_hdrbuf(desc, hdr, ps, depth);
-               ps->offset = old_offset;
+               init_buf_hdr(hdr, max_len, len);
+               if(!prs_set_offset(ps, ptr_hdrbuf))
+                       return False;
+               if(!smb_io_hdrbuf(desc, hdr, ps, depth))
+                       return False;
+
+               if(!prs_set_offset(ps, old_offset))
+                       return False;
        }
+
+       return True;
 }
+
 /*******************************************************************
-reads or writes a BUFHDR structure.
+ Reads or writes a BUFHDR structure.
 ********************************************************************/
-void smb_io_hdrbuf(char *desc,  BUFHDR *hdr, prs_struct *ps, int depth)
+
+BOOL smb_io_hdrbuf(char *desc, BUFHDR *hdr, prs_struct *ps, int depth)
 {
-       if (hdr == NULL) return;
+       if (hdr == NULL)
+               return False;
 
        prs_debug(ps, depth, desc, "smb_io_hdrbuf");
        depth++;
 
-       prs_align(ps);
+       if(!prs_align(ps))
+               return False;
        
-       prs_uint32("buf_max_len", ps, depth, &(hdr->buf_max_len));
-       prs_uint32("buf_len    ", ps, depth, &(hdr->buf_len    ));
+       if(!prs_uint32("buf_max_len", ps, depth, &hdr->buf_max_len))
+               return False;
+       if(!prs_uint32("buf_len    ", ps, depth, &hdr->buf_len))
+               return False;
 
-       /* oops! XXXX maybe issue a warning that this is happening... */
-       if (hdr->buf_max_len > MAX_BUFFERLEN) hdr->buf_max_len = MAX_BUFFERLEN;
-       if (hdr->buf_len     > MAX_BUFFERLEN) hdr->buf_len     = MAX_BUFFERLEN;
+       return True;
 }
 
 /*******************************************************************
 creates a UNIHDR2 structure.
 ********************************************************************/
-void make_uni_hdr2(UNIHDR2 *hdr, int max_len, int len, uint16 terminate)
+
+void init_uni_hdr2(UNIHDR2 *hdr, int len)
 {
-       make_uni_hdr(&(hdr->unihdr), max_len, len, terminate);
-       hdr->buffer = len > 0 ? 1 : 0;
+       init_uni_hdr(&hdr->unihdr, len);
+       hdr->buffer = (len > 0) ? 1 : 0;
 }
 
 /*******************************************************************
-reads or writes a UNIHDR2 structure.
+ Reads or writes a UNIHDR2 structure.
 ********************************************************************/
-void smb_io_unihdr2(char *desc,  UNIHDR2 *hdr2, prs_struct *ps, int depth)
+
+BOOL smb_io_unihdr2(char *desc, UNIHDR2 *hdr2, prs_struct *ps, int depth)
 {
-       if (hdr2 == NULL) return;
+       if (hdr2 == NULL)
+               return False;
 
        prs_debug(ps, depth, desc, "smb_io_unihdr2");
        depth++;
 
-       prs_align(ps);
+       if(!prs_align(ps))
+               return False;
+
+       if(!smb_io_unihdr("hdr", &hdr2->unihdr, ps, depth))
+               return False;
+       if(!prs_uint32("buffer", ps, depth, &hdr2->buffer))
+               return False;
 
-       smb_io_unihdr("hdr", &(hdr2->unihdr), ps, depth);
-       prs_uint32("buffer", ps, depth, &(hdr2->buffer));
+       return True;
 }
 
 /*******************************************************************
-creates a UNISTR structure.
+ Inits a UNISTR structure.
 ********************************************************************/
-void make_unistr(UNISTR *str, char *buf)
+
+void init_unistr(UNISTR *str, const char *buf)
 {
-       ascii_to_unistr(str->buffer, buf, sizeof(str->buffer)-1);
+       size_t len;
+
+       if (buf == NULL) {
+               str->buffer = NULL;
+               return;
+       }
+               
+
+       len = strlen(buf) + 1;
+
+       if (len < MAX_UNISTRLEN)
+               len = MAX_UNISTRLEN;
+       len *= sizeof(uint16);
+
+       str->buffer = (uint16 *)talloc_zero(get_talloc_ctx(), len);
+       if (str->buffer == NULL)
+               smb_panic("init_unistr: malloc fail\n");
+
+       rpcstr_push(str->buffer, buf, len, STR_TERMINATE);
 }
 
 /*******************************************************************
 reads or writes a UNISTR structure.
 XXXX NOTE: UNISTR structures NEED to be null-terminated.
 ********************************************************************/
-void smb_io_unistr(char *desc,  UNISTR *uni, prs_struct *ps, int depth)
+
+BOOL smb_io_unistr(char *desc, UNISTR *uni, prs_struct *ps, int depth)
 {
-       if (uni == NULL) return;
+       if (uni == NULL)
+               return False;
 
        prs_debug(ps, depth, desc, "smb_io_unistr");
        depth++;
 
-       prs_unistr("unistr", ps, depth, uni);
+       if(!prs_unistr("unistr", ps, depth, uni))
+               return False;
+
+       return True;
 }
 
 /*******************************************************************
-creates a BUFFER3 structure from a uint32
+ Allocate the BUFFER3 memory.
 ********************************************************************/
-void make_buffer3_uint32(BUFFER3 *str, uint32 val)
+
+static void create_buffer3(BUFFER3 *str, size_t len)
+{
+       if (len < MAX_BUFFERLEN)
+               len = MAX_BUFFERLEN;
+
+    str->buffer = talloc_zero(get_talloc_ctx(), len);
+       if (str->buffer == NULL)
+               smb_panic("create_buffer3: talloc fail\n");
+
+}
+
+/*******************************************************************
+ Inits a BUFFER3 structure from a uint32
+********************************************************************/
+
+void init_buffer3_uint32(BUFFER3 *str, uint32 val)
 {
        ZERO_STRUCTP(str);
 
@@ -356,76 +584,119 @@ void make_buffer3_uint32(BUFFER3 *str, uint32 val)
        str->buf_max_len = sizeof(uint32);
        str->buf_len     = sizeof(uint32);
 
+       create_buffer3(str, sizeof(uint32));
        SIVAL(str->buffer, 0, val);
 }
 
 /*******************************************************************
-creates a BUFFER3 structure.
+ Inits a BUFFER3 structure.
 ********************************************************************/
-void make_buffer3_str(BUFFER3 *str, char *buf, int len)
+
+void init_buffer3_str(BUFFER3 *str, char *buf, int len)
 {
        ZERO_STRUCTP(str);
 
        /* set up string lengths. */
        str->buf_max_len = len * 2;
-       str->buf_len     = len * 2;
+       str->buf_len = len * 2;
+
+       create_buffer3(str, str->buf_max_len);
 
-       /* store the string (little endian buffer) */
-       ascii_to_unibuf(str->buffer, buf, str->buf_len);
+       rpcstr_push(str->buffer, buf, str->buf_max_len, STR_TERMINATE);
+       
 }
 
 /*******************************************************************
-creates a BUFFER3 structure from a hex string.
+ Inits a BUFFER3 structure from a hex string.
 ********************************************************************/
-void make_buffer3_hex(BUFFER3 *str, char *buf)
+
+void init_buffer3_hex(BUFFER3 *str, char *buf)
 {
        ZERO_STRUCTP(str);
+       create_buffer3(str, strlen(buf));
        str->buf_max_len = str->buf_len = strhex_to_str((char *)str->buffer, sizeof(str->buffer), buf);
 }
 
 /*******************************************************************
-creates a BUFFER3 structure.
+ Inits a BUFFER3 structure.
 ********************************************************************/
-void make_buffer3_bytes(BUFFER3 *str, uint8 *buf, int len)
+
+void init_buffer3_bytes(BUFFER3 *str, uint8 *buf, int len)
 {
        ZERO_STRUCTP(str);
 
        /* max buffer size (allocated size) */
        str->buf_max_len = len;
-       if (buf != NULL)
-       {
-               memcpy(str->buffer, buf, MIN(str->buf_len, sizeof(str->buffer)));
+       if (buf != NULL) {
+               create_buffer3(str, len);
+               memcpy(str->buffer, buf, len);
        }
        str->buf_len = buf != NULL ? len : 0;
 }
 
 /*******************************************************************
-reads or writes a BUFFER3 structure.
-     the uni_max_len member tells you how large the buffer is.
-     the uni_str_len member tells you how much of the buffer is really used.
+ Reads or writes a BUFFER3 structure.
+   the uni_max_len member tells you how large the buffer is.
+   the uni_str_len member tells you how much of the buffer is really used.
 ********************************************************************/
-void smb_io_buffer3(char *desc,  BUFFER3 *buf3, prs_struct *ps, int depth)
+
+BOOL smb_io_buffer3(char *desc, BUFFER3 *buf3, prs_struct *ps, int depth)
 {
-       if (buf3 == NULL) return;
+       if (buf3 == NULL)
+               return False;
 
        prs_debug(ps, depth, desc, "smb_io_buffer3");
        depth++;
 
-       prs_align(ps);
+       if(!prs_align(ps))
+               return False;
        
-       prs_uint32("uni_max_len", ps, depth, &(buf3->buf_max_len));
-       if (buf3->buf_max_len > MAX_UNISTRLEN) buf3->buf_max_len = MAX_UNISTRLEN;
+       if(!prs_uint32("uni_max_len", ps, depth, &buf3->buf_max_len))
+               return False;
+
+       if (UNMARSHALLING(ps)) {
+               buf3->buffer = (unsigned char *)prs_alloc_mem(ps, buf3->buf_max_len);
+               if (buf3->buffer == NULL)
+                       return False;
+       }
+
+       if(!prs_uint8s(True, "buffer     ", ps, depth, buf3->buffer, buf3->buf_max_len))
+               return False;
 
-       prs_uint8s(True, "buffer     ", ps, depth, buf3->buffer, buf3->buf_max_len);
+       if(!prs_uint32("buf_len    ", ps, depth, &buf3->buf_len))
+               return False;
 
-       prs_uint32("buf_len    ", ps, depth, &(buf3->buf_len));
-       if (buf3->buf_len     > MAX_UNISTRLEN) buf3->buf_len     = MAX_UNISTRLEN;
+       return True;
 }
 
 /*******************************************************************
-creates a BUFFER2 structure.
+reads or writes a BUFFER5 structure.
+the buf_len member tells you how large the buffer is.
 ********************************************************************/
-void make_buffer2(BUFFER2 *str, uint8 *buf, int len)
+BOOL smb_io_buffer5(char *desc, BUFFER5 *buf5, prs_struct *ps, int depth)
+{
+       prs_debug(ps, depth, desc, "smb_io_buffer5");
+       depth++;
+
+       if (buf5 == NULL) return False;
+
+       if(!prs_align(ps))
+               return False;
+       if(!prs_uint32("buf_len", ps, depth, &buf5->buf_len))
+               return False;
+
+
+       if(!prs_buffer5(True, "buffer" , ps, depth, buf5))
+               return False;
+
+       return True;
+}
+
+/*******************************************************************
+ Inits a BUFFER2 structure.
+********************************************************************/
+
+void init_buffer2(BUFFER2 *str, uint8 *buf, int len)
 {
        ZERO_STRUCTP(str);
 
@@ -434,188 +705,348 @@ void make_buffer2(BUFFER2 *str, uint8 *buf, int len)
        str->undoc       = 0;
        str->buf_len = buf != NULL ? len : 0;
 
-       if (buf != NULL)
-       {
-               memcpy(str->buffer, buf, MIN(str->buf_len, sizeof(str->buffer)));
+       if (buf != NULL) {
+               if (len < MAX_BUFFERLEN)
+                       len = MAX_BUFFERLEN;
+               str->buffer = talloc_zero(get_talloc_ctx(), len);
+               if (str->buffer == NULL)
+                       smb_panic("init_buffer2: talloc fail\n");
+               memcpy(str->buffer, buf, MIN(str->buf_len, len));
        }
 }
 
 /*******************************************************************
-reads or writes a BUFFER2 structure.
-     the uni_max_len member tells you how large the buffer is.
-     the uni_str_len member tells you how much of the buffer is really used.
+ Reads or writes a BUFFER2 structure.
+   the uni_max_len member tells you how large the buffer is.
+   the uni_str_len member tells you how much of the buffer is really used.
 ********************************************************************/
-void smb_io_buffer2(char *desc,  BUFFER2 *buf2, uint32 buffer, prs_struct *ps, int depth)
+
+BOOL smb_io_buffer2(char *desc, BUFFER2 *buf2, uint32 buffer, prs_struct *ps, int depth)
 {
-       if (buf2 == NULL) return;
+       if (buf2 == NULL)
+               return False;
+
+       if (buffer) {
 
-       if (buffer)
-       {
                prs_debug(ps, depth, desc, "smb_io_buffer2");
                depth++;
 
-               prs_align(ps);
+               if(!prs_align(ps))
+                       return False;
                
-               prs_uint32("buf_max_len", ps, depth, &(buf2->buf_max_len));
-               prs_uint32("undoc      ", ps, depth, &(buf2->undoc      ));
-               prs_uint32("buf_len    ", ps, depth, &(buf2->buf_len));
-
-               /* oops! XXXX maybe issue a warning that this is happening... */
-               if (buf2->buf_max_len > MAX_UNISTRLEN) buf2->buf_max_len = MAX_UNISTRLEN;
-               if (buf2->buf_len     > MAX_UNISTRLEN) buf2->buf_len     = MAX_UNISTRLEN;
+               if(!prs_uint32("uni_max_len", ps, depth, &buf2->buf_max_len))
+                       return False;
+               if(!prs_uint32("undoc      ", ps, depth, &buf2->undoc))
+                       return False;
+               if(!prs_uint32("buf_len    ", ps, depth, &buf2->buf_len))
+                       return False;
 
                /* buffer advanced by indicated length of string
                   NOT by searching for null-termination */
-               prs_buffer2(True, "buffer     ", ps, depth, buf2);
-       }
-       else
-       {
+
+               if(!prs_buffer2(True, "buffer     ", ps, depth, buf2))
+                       return False;
+
+       } else {
+
                prs_debug(ps, depth, desc, "smb_io_buffer2 - NULL");
                depth++;
-               bzero(buf2, sizeof(*buf2));
+               memset((char *)buf2, '\0', sizeof(*buf2));
+
        }
+       return True;
 }
 
 /*******************************************************************
 creates a UNISTR2 structure: sets up the buffer, too
 ********************************************************************/
-void make_buf_unistr2(UNISTR2 *str, uint32 *ptr, char *buf)
+
+void init_buf_unistr2(UNISTR2 *str, uint32 *ptr, const char *buf)
 {
-       if (buf != NULL)
-       {
+       if (buf != NULL) {
+
                *ptr = 1;
-               make_unistr2(str, buf, strlen(buf)+1);
-       }
-       else
-       {
+               init_unistr2(str, buf, strlen(buf)+1);
+
+       } else {
+
                *ptr = 0;
-               make_unistr2(str, "", 0);
+               init_unistr2(str, "", 0);
+
        }
 }
 
 /*******************************************************************
-copies a UNISTR2 structure.
+ Copies a UNISTR2 structure.
 ********************************************************************/
-void copy_unistr2(UNISTR2 *str, UNISTR2 *from)
+
+void copy_unistr2(UNISTR2 *str, const UNISTR2 *from)
 {
+
        /* set up string lengths. add one if string is not null-terminated */
        str->uni_max_len = from->uni_max_len;
        str->undoc       = from->undoc;
        str->uni_str_len = from->uni_str_len;
 
+       if (from->buffer == NULL)
+               return;
+               
+       /* the string buffer is allocated to the maximum size
+          (the the length of the source string) to prevent
+          reallocation of memory. */
+       if (str->buffer == NULL) {
+               size_t len = from->uni_max_len * sizeof(uint16);
+
+               if (len < MAX_UNISTRLEN)
+                       len = MAX_UNISTRLEN;
+               len *= sizeof(uint16);
+
+               str->buffer = (uint16 *)talloc_zero(get_talloc_ctx(), len);
+               if ((str->buffer == NULL) && (len > 0 ))
+               {
+                       smb_panic("copy_unistr2: talloc fail\n");
+                       return;
+               }
+       }
+
        /* copy the string */
-       memcpy(str->buffer, from->buffer, sizeof(from->buffer));
+       memcpy(str->buffer, from->buffer, from->uni_max_len*sizeof(uint16));
 }
 
 /*******************************************************************
-creates a STRING2 structure.
+ Creates a STRING2 structure.
 ********************************************************************/
-void make_string2(STRING2 *str, char *buf, int len)
+
+void init_string2(STRING2 *str, const char *buf, int max_len, int str_len)
 {
-  /* set up string lengths. */
-  str->str_max_len = len;
-  str->undoc       = 0;
-  str->str_str_len = len;
+       int alloc_len = 0;
 
-  /* store the string */
-  if(len != 0)
-    memcpy(str->buffer, buf, len);
+       /* set up string lengths. */
+       str->str_max_len = max_len;
+       str->undoc       = 0;
+       str->str_str_len = str_len;
+
+       /* store the string */
+       if(str_len != 0) {
+               if (str_len < MAX_STRINGLEN)
+                       alloc_len = MAX_STRINGLEN;
+               str->buffer = talloc_zero(get_talloc_ctx(), alloc_len);
+               if (str->buffer == NULL)
+                       smb_panic("init_string2: malloc fail\n");
+               memcpy(str->buffer, buf, str_len);
+  }
 }
 
 /*******************************************************************
-reads or writes a STRING2 structure.
-XXXX NOTE: STRING2 structures need NOT be null-terminated.
-     the str_str_len member tells you how long the string is;
-     the str_max_len member tells you how large the buffer is.
+ Reads or writes a STRING2 structure.
+ XXXX NOTE: STRING2 structures need NOT be null-terminated.
+   the str_str_len member tells you how long the string is;
+   the str_max_len member tells you how large the buffer is.
 ********************************************************************/
-void smb_io_string2(char *desc,  STRING2 *str2, uint32 buffer, prs_struct *ps, int depth)
+
+BOOL smb_io_string2(char *desc, STRING2 *str2, uint32 buffer, prs_struct *ps, int depth)
 {
-       if (str2 == NULL) return;
+       if (str2 == NULL)
+               return False;
+
+       if (buffer) {
 
-       if (buffer)
-       {
                prs_debug(ps, depth, desc, "smb_io_string2");
                depth++;
 
-               prs_align(ps);
+               if(!prs_align(ps))
+                       return False;
                
-               prs_uint32("str_max_len", ps, depth, &(str2->str_max_len));
-               prs_uint32("undoc      ", ps, depth, &(str2->undoc      ));
-               prs_uint32("str_str_len", ps, depth, &(str2->str_str_len));
-
-               /* oops! XXXX maybe issue a warning that this is happening... */
-               if (str2->str_max_len > MAX_STRINGLEN) str2->str_max_len = MAX_STRINGLEN;
-               if (str2->str_str_len > MAX_STRINGLEN) str2->str_str_len = MAX_STRINGLEN;
+               if(!prs_uint32("str_max_len", ps, depth, &str2->str_max_len))
+                       return False;
+               if(!prs_uint32("undoc      ", ps, depth, &str2->undoc))
+                       return False;
+               if(!prs_uint32("str_str_len", ps, depth, &str2->str_str_len))
+                       return False;
 
                /* buffer advanced by indicated length of string
                   NOT by searching for null-termination */
-               prs_string2(True, "buffer     ", ps, depth, str2);
-       }
-       else
-       {
+               if(!prs_string2(True, "buffer     ", ps, depth, str2))
+                       return False;
+
+       } else {
+
                prs_debug(ps, depth, desc, "smb_io_string2 - NULL");
                depth++;
-               bzero(str2, sizeof(*str2));
+               memset((char *)str2, '\0', sizeof(*str2));
+
        }
+
+       return True;
 }
 
 /*******************************************************************
-creates a UNISTR2 structure.
+ Inits a UNISTR2 structure.
 ********************************************************************/
-void make_unistr2(UNISTR2 *str, const char *buf, int len)
+
+void init_unistr2(UNISTR2 *str, const char *buf, size_t len)
 {
        ZERO_STRUCTP(str);
 
+       /* set up string lengths. */
+       str->uni_max_len = (uint32)len;
+       str->undoc       = 0;
+       str->uni_str_len = (uint32)len;
+
+       if (len < MAX_UNISTRLEN)
+               len = MAX_UNISTRLEN;
+       len *= sizeof(uint16);
+
+       str->buffer = (uint16 *)talloc_zero(get_talloc_ctx(), len);
+       if ((str->buffer == NULL) && (len > 0))
+       {
+               smb_panic("init_unistr2: malloc fail\n");
+               return;
+       }
+
+       /*
+        * don't move this test above ! The UNISTR2 must be initialized !!!
+        * jfm, 7/7/2001.
+        */
+       if (buf==NULL)
+               return;
+
+       rpcstr_push((char *)str->buffer, buf, len, STR_TERMINATE);
+}
+
+/** 
+ *  Inits a UNISTR2 structure.
+ *  @param  ctx talloc context to allocate string on
+ *  @param  str pointer to string to create
+ *  @param  buf UCS2 null-terminated buffer to init from
+*/
+
+void init_unistr2_w(TALLOC_CTX *ctx, UNISTR2 *str, const smb_ucs2_t *buf)
+{
+       uint32 len = strlen_w(buf);
+       uint32 max_len = len;
+       uint32 alloc_len;
+
+       ZERO_STRUCTP(str);
+
        /* set up string lengths. */
        str->uni_max_len = len;
        str->undoc       = 0;
        str->uni_str_len = len;
 
-       /* store the string (wide chars) */
-       ascii_to_unistr(str->buffer, buf, len);
+       if (max_len < MAX_UNISTRLEN)
+               max_len = MAX_UNISTRLEN;
+
+       alloc_len = (max_len + 1) * sizeof(uint16);
+
+       str->buffer = (uint16 *)talloc_zero(ctx, alloc_len);
+       if ((str->buffer == NULL) && (alloc_len > 0))
+       {
+               smb_panic("init_unistr2_w: malloc fail\n");
+               return;
+       }
+       
+       /*
+        * don't move this test above ! The UNISTR2 must be initialized !!!
+        * jfm, 7/7/2001.
+        */
+       if (buf==NULL)
+               return;
+       
+       /* Yes, this is a strncpy( foo, bar, strlen(bar)) - but as
+           long as the buffer above is talloc()ed correctly then this
+           is the correct thing to do */
+       strncpy_w(str->buffer, buf, len + 1);
 }
 
 /*******************************************************************
-reads or writes a UNISTR2 structure.
-XXXX NOTE: UNISTR2 structures need NOT be null-terminated.
-     the uni_str_len member tells you how long the string is;
-     the uni_max_len member tells you how large the buffer is.
+ Inits a UNISTR2 structure from a UNISTR
 ********************************************************************/
-void smb_io_unistr2(char *desc,  UNISTR2 *uni2, uint32 buffer, prs_struct *ps, int depth)
+void init_unistr2_from_unistr (UNISTR2 *to, UNISTR *from)
 {
-       if (uni2 == NULL) return;
 
-       if (buffer)
-       {
+       uint32 i;
+
+       /* the destination UNISTR2 should never be NULL.
+          if it is it is a programming error */
+
+       /* if the source UNISTR is NULL, then zero out
+          the destination string and return */
+       ZERO_STRUCTP (to);
+       if ((from == NULL) || (from->buffer == NULL))
+               return;
+
+       /* get the length; UNISTR must be NULL terminated */
+       i = 0;
+       while ((from->buffer)[i]!='\0')
+               i++;
+       i++;    /* one more to catch the terminating NULL */
+               /* is this necessary -- jerry?  I need to think */
+
+       /* set up string lengths; uni_max_len is set to i+1
+           because we need to account for the final NULL termination */
+       to->uni_max_len = i;
+       to->undoc       = 0;
+       to->uni_str_len = i;
+
+       /* allocate the space and copy the string buffer */
+       to->buffer = (uint16 *)talloc_zero(get_talloc_ctx(), sizeof(uint16)*(to->uni_str_len));
+       if (to->buffer == NULL)
+               smb_panic("init_unistr2_from_unistr: malloc fail\n");
+       memcpy(to->buffer, from->buffer, to->uni_max_len*sizeof(uint16));
+               
+       return;
+}
+
+
+/*******************************************************************
+ Reads or writes a UNISTR2 structure.
+ XXXX NOTE: UNISTR2 structures need NOT be null-terminated.
+   the uni_str_len member tells you how long the string is;
+   the uni_max_len member tells you how large the buffer is.
+********************************************************************/
+
+BOOL smb_io_unistr2(char *desc, UNISTR2 *uni2, uint32 buffer, prs_struct *ps, int depth)
+{
+       if (uni2 == NULL)
+               return False;
+
+       if (buffer) {
+
                prs_debug(ps, depth, desc, "smb_io_unistr2");
                depth++;
 
-               prs_align(ps);
+               if(!prs_align(ps))
+                       return False;
                
-               prs_uint32("uni_max_len", ps, depth, &(uni2->uni_max_len));
-               prs_uint32("undoc      ", ps, depth, &(uni2->undoc      ));
-               prs_uint32("uni_str_len", ps, depth, &(uni2->uni_str_len));
-
-               /* oops! XXXX maybe issue a warning that this is happening... */
-               if (uni2->uni_max_len > MAX_UNISTRLEN) uni2->uni_max_len = MAX_UNISTRLEN;
-               if (uni2->uni_str_len > MAX_UNISTRLEN) uni2->uni_str_len = MAX_UNISTRLEN;
+               if(!prs_uint32("uni_max_len", ps, depth, &uni2->uni_max_len))
+                       return False;
+               if(!prs_uint32("undoc      ", ps, depth, &uni2->undoc))
+                       return False;
+               if(!prs_uint32("uni_str_len", ps, depth, &uni2->uni_str_len))
+                       return False;
 
                /* buffer advanced by indicated length of string
                   NOT by searching for null-termination */
-               prs_unistr2(True, "buffer     ", ps, depth, uni2);
-       }
-       else
-       {
+               if(!prs_unistr2(True, "buffer     ", ps, depth, uni2))
+                       return False;
+
+       } else {
+
                prs_debug(ps, depth, desc, "smb_io_unistr2 - NULL");
                depth++;
-               bzero(uni2, sizeof(*uni2));
+               memset((char *)uni2, '\0', sizeof(*uni2));
+
        }
+
+       return True;
 }
 
 /*******************************************************************
-creates a DOM_RID2 structure.
+ Inits a DOM_RID2 structure.
 ********************************************************************/
-void make_dom_rid2(DOM_RID2 *rid2, uint32 rid, uint8 type, uint32 idx)
+
+void init_dom_rid2(DOM_RID2 *rid2, uint32 rid, uint8 type, uint32 idx)
 {
        rid2->type    = type;
        rid2->rid     = rid;
@@ -623,277 +1054,334 @@ void make_dom_rid2(DOM_RID2 *rid2, uint32 rid, uint8 type, uint32 idx)
 }
 
 /*******************************************************************
-reads or writes a DOM_RID2 structure.
+ Reads or writes a DOM_RID2 structure.
 ********************************************************************/
-void smb_io_dom_rid2(char *desc,  DOM_RID2 *rid2, prs_struct *ps, int depth)
+
+BOOL smb_io_dom_rid2(char *desc, DOM_RID2 *rid2, prs_struct *ps, int depth)
 {
-       if (rid2 == NULL) return;
+       if (rid2 == NULL)
+               return False;
 
        prs_debug(ps, depth, desc, "smb_io_dom_rid2");
        depth++;
 
-       prs_align(ps);
-       
-       prs_uint8("type   ", ps, depth, &(rid2->type));
-       prs_align(ps);
-       prs_uint32("rid    ", ps, depth, &(rid2->rid     ));
-       prs_uint32("rid_idx", ps, depth, &(rid2->rid_idx ));
+       if(!prs_align(ps))
+               return False;
+   
+       if(!prs_uint8("type   ", ps, depth, &rid2->type))
+               return False;
+       if(!prs_align(ps))
+               return False;
+       if(!prs_uint32("rid    ", ps, depth, &rid2->rid))
+               return False;
+       if(!prs_uint32("rid_idx", ps, depth, &rid2->rid_idx))
+               return False;
+
+       return True;
 }
 
 /*******************************************************************
 creates a DOM_RID3 structure.
 ********************************************************************/
-void make_dom_rid3(DOM_RID3 *rid3, uint32 rid, uint8 type)
+
+void init_dom_rid3(DOM_RID3 *rid3, uint32 rid, uint8 type)
 {
-       rid3->rid      = rid;
-       rid3->type1    = type;
-       rid3->ptr_type = 0x1; /* non-zero, basically. */
-       rid3->type2    = 0x1;
-       rid3->unk      = type;
+    rid3->rid      = rid;
+    rid3->type1    = type;
+    rid3->ptr_type = 0x1; /* non-zero, basically. */
+    rid3->type2    = 0x1;
+    rid3->unk      = type;
 }
 
 /*******************************************************************
 reads or writes a DOM_RID3 structure.
 ********************************************************************/
-void smb_io_dom_rid3(char *desc,  DOM_RID3 *rid3, prs_struct *ps, int depth)
+
+BOOL smb_io_dom_rid3(char *desc, DOM_RID3 *rid3, prs_struct *ps, int depth)
 {
-       if (rid3 == NULL) return;
+       if (rid3 == NULL)
+               return False;
 
        prs_debug(ps, depth, desc, "smb_io_dom_rid3");
        depth++;
 
-       prs_align(ps);
-       
-       prs_uint32("rid     ", ps, depth, &(rid3->rid     ));
-       prs_uint32("type1   ", ps, depth, &(rid3->type1   ));
-       prs_uint32("ptr_type", ps, depth, &(rid3->ptr_type));
-       prs_uint32("type2   ", ps, depth, &(rid3->type2   ));
-       prs_uint32("unk     ", ps, depth, &(rid3->unk     ));
+       if(!prs_align(ps))
+               return False;
+
+       if(!prs_uint32("rid     ", ps, depth, &rid3->rid))
+               return False;
+       if(!prs_uint32("type1   ", ps, depth, &rid3->type1))
+               return False;
+       if(!prs_uint32("ptr_type", ps, depth, &rid3->ptr_type))
+               return False;
+       if(!prs_uint32("type2   ", ps, depth, &rid3->type2))
+               return False;
+       if(!prs_uint32("unk     ", ps, depth, &rid3->unk))
+               return False;
+
+       return True;
 }
 
 /*******************************************************************
-creates a DOM_RID4 structure.
+ Inits a DOM_RID4 structure.
 ********************************************************************/
-void make_dom_rid4(DOM_RID4 *rid4, uint16 unknown, uint16 attr, uint32 rid)
+
+void init_dom_rid4(DOM_RID4 *rid4, uint16 unknown, uint16 attr, uint32 rid)
 {
-       rid4->unknown = unknown;
-       rid4->attr    = attr;
-       rid4->rid     = rid;
+    rid4->unknown = unknown;
+    rid4->attr    = attr;
+    rid4->rid     = rid;
 }
 
 /*******************************************************************
-makes a DOM_CLNT_SRV structure.
+ Inits a DOM_CLNT_SRV structure.
 ********************************************************************/
-static void make_clnt_srv(DOM_CLNT_SRV *log, char *logon_srv, char *comp_name)
-{
-       if (log == NULL) return;
 
-       DEBUG(5,("make_clnt_srv: %d\n", __LINE__));
+static void init_clnt_srv(DOM_CLNT_SRV *log, char *logon_srv, char *comp_name)
+{
+       DEBUG(5,("init_clnt_srv: %d\n", __LINE__));
 
-       if (logon_srv != NULL)
-       {
+       if (logon_srv != NULL) {
                log->undoc_buffer = 1;
-               make_unistr2(&(log->uni_logon_srv), logon_srv, strlen(logon_srv)+1);
-       }
-       else
-       {
+               init_unistr2(&log->uni_logon_srv, logon_srv, strlen(logon_srv)+1);
+       } else {
                log->undoc_buffer = 0;
        }
 
-       if (comp_name != NULL)
-       {
+       if (comp_name != NULL) {
                log->undoc_buffer2 = 1;
-               make_unistr2(&(log->uni_comp_name), comp_name, strlen(comp_name)+1);
-       }
-       else
-       {
+               init_unistr2(&log->uni_comp_name, comp_name, strlen(comp_name)+1);
+       } else {
                log->undoc_buffer2 = 0;
        }
 }
 
 /*******************************************************************
-reads or writes a DOM_CLNT_SRV structure.
+ Inits or writes a DOM_CLNT_SRV structure.
 ********************************************************************/
-static void smb_io_clnt_srv(char *desc,  DOM_CLNT_SRV *log, prs_struct *ps, int depth)
+
+static BOOL smb_io_clnt_srv(char *desc, DOM_CLNT_SRV *log, prs_struct *ps, int depth)
 {
-       if (log == NULL) return;
+       if (log == NULL)
+               return False;
 
        prs_debug(ps, depth, desc, "smb_io_clnt_srv");
        depth++;
 
-       prs_align(ps);
+       if(!prs_align(ps))
+               return False;
        
-       prs_uint32("undoc_buffer ", ps, depth, &(log->undoc_buffer ));
-       if (log->undoc_buffer != 0)
-       {
-               smb_io_unistr2("unistr2", &(log->uni_logon_srv), log->undoc_buffer, ps, depth);
+       if(!prs_uint32("undoc_buffer ", ps, depth, &log->undoc_buffer))
+               return False;
+
+       if (log->undoc_buffer != 0) {
+               if(!smb_io_unistr2("unistr2", &log->uni_logon_srv, log->undoc_buffer, ps, depth))
+                       return False;
        }
 
-       prs_align(ps);
+       if(!prs_align(ps))
+               return False;
 
-       prs_uint32("undoc_buffer2", ps, depth, &(log->undoc_buffer2));
-       if (log->undoc_buffer2 != 0)
-       {
-               smb_io_unistr2("unistr2", &(log->uni_comp_name), log->undoc_buffer2, ps, depth);
+       if(!prs_uint32("undoc_buffer2", ps, depth, &log->undoc_buffer2))
+               return False;
+
+       if (log->undoc_buffer2 != 0) {
+               if(!smb_io_unistr2("unistr2", &log->uni_comp_name, log->undoc_buffer2, ps, depth))
+                       return False;
        }
+
+       return True;
 }
 
 /*******************************************************************
-makes a DOM_LOG_INFO structure.
+ Inits a DOM_LOG_INFO structure.
 ********************************************************************/
-void make_log_info(DOM_LOG_INFO *log, char *logon_srv, char *acct_name,
-               uint16 sec_chan, char *comp_name)
-{
-       if (log == NULL) return;
 
+void init_log_info(DOM_LOG_INFO *log, const char *logon_srv, const char *acct_name,
+               uint16 sec_chan, const char *comp_name)
+{
        DEBUG(5,("make_log_info %d\n", __LINE__));
 
        log->undoc_buffer = 1;
 
-       make_unistr2(&(log->uni_logon_srv), logon_srv, strlen(logon_srv)+1);
-       make_unistr2(&(log->uni_acct_name), acct_name, strlen(acct_name)+1);
+       init_unistr2(&log->uni_logon_srv, logon_srv, strlen(logon_srv)+1);
+       init_unistr2(&log->uni_acct_name, acct_name, strlen(acct_name)+1);
 
        log->sec_chan = sec_chan;
 
-       make_unistr2(&(log->uni_comp_name), comp_name, strlen(comp_name)+1);
+       init_unistr2(&log->uni_comp_name, comp_name, strlen(comp_name)+1);
 }
 
 /*******************************************************************
-reads or writes a DOM_LOG_INFO structure.
+ Reads or writes a DOM_LOG_INFO structure.
 ********************************************************************/
-void smb_io_log_info(char *desc,  DOM_LOG_INFO *log, prs_struct *ps, int depth)
+
+BOOL smb_io_log_info(char *desc, DOM_LOG_INFO *log, prs_struct *ps, int depth)
 {
-       if (log == NULL) return;
+       if (log == NULL)
+               return False;
 
        prs_debug(ps, depth, desc, "smb_io_log_info");
        depth++;
 
-       prs_align(ps);
+       if(!prs_align(ps))
+               return False;
        
-       prs_uint32("undoc_buffer", ps, depth, &(log->undoc_buffer));
+       if(!prs_uint32("undoc_buffer", ps, depth, &log->undoc_buffer))
+               return False;
+
+       if(!smb_io_unistr2("unistr2", &log->uni_logon_srv, True, ps, depth))
+               return False;
+       if(!smb_io_unistr2("unistr2", &log->uni_acct_name, True, ps, depth))
+               return False;
 
-       smb_io_unistr2("unistr2", &(log->uni_logon_srv), True, ps, depth);
-       smb_io_unistr2("unistr2", &(log->uni_acct_name), True, ps, depth);
+       if(!prs_uint16("sec_chan", ps, depth, &log->sec_chan))
+               return False;
 
-       prs_uint16("sec_chan", ps, depth, &(log->sec_chan));
+       if(!smb_io_unistr2("unistr2", &log->uni_comp_name, True, ps, depth))
+               return False;
 
-       smb_io_unistr2("unistr2", &(log->uni_comp_name), True, ps, depth);
+       return True;
 }
 
 /*******************************************************************
-reads or writes a DOM_CHAL structure.
+ Reads or writes a DOM_CHAL structure.
 ********************************************************************/
-void smb_io_chal(char *desc,  DOM_CHAL *chal, prs_struct *ps, int depth)
+
+BOOL smb_io_chal(char *desc, DOM_CHAL *chal, prs_struct *ps, int depth)
 {
-       if (chal == NULL) return;
+       if (chal == NULL)
+               return False;
 
        prs_debug(ps, depth, desc, "smb_io_chal");
        depth++;
 
-       prs_align(ps);
+       if(!prs_align(ps))
+               return False;
        
-       prs_uint8s (False, "data", ps, depth, chal->data, 8);
+       if(!prs_uint8s (False, "data", ps, depth, chal->data, 8))
+               return False;
+
+       return True;
 }
 
 /*******************************************************************
-reads or writes a DOM_CRED structure.
+ Reads or writes a DOM_CRED structure.
 ********************************************************************/
-void smb_io_cred(char *desc,  DOM_CRED *cred, prs_struct *ps, int depth)
+
+BOOL smb_io_cred(char *desc,  DOM_CRED *cred, prs_struct *ps, int depth)
 {
-       if (cred == NULL) return;
+       if (cred == NULL)
+               return False;
 
        prs_debug(ps, depth, desc, "smb_io_cred");
        depth++;
 
-       prs_align(ps);
-       
-       smb_io_chal ("", &(cred->challenge), ps, depth);
-       smb_io_utime("", &(cred->timestamp), ps, depth);
+       if(!prs_align(ps))
+               return False;
+
+       if(!smb_io_chal ("", &cred->challenge, ps, depth))
+               return False;
+
+       if(!smb_io_utime("", &cred->timestamp, ps, depth))
+               return False;
+
+       return True;
 }
 
 /*******************************************************************
-makes a DOM_CLNT_INFO2 structure.
+ Inits a DOM_CLNT_INFO2 structure.
 ********************************************************************/
-void make_clnt_info2(DOM_CLNT_INFO2 *clnt,
+
+void init_clnt_info2(DOM_CLNT_INFO2 *clnt,
                                char *logon_srv, char *comp_name,
                                DOM_CRED *clnt_cred)
 {
-       if (clnt == NULL) return;
-
        DEBUG(5,("make_clnt_info: %d\n", __LINE__));
 
-       make_clnt_srv(&(clnt->login), logon_srv, comp_name);
+       init_clnt_srv(&(clnt->login), logon_srv, comp_name);
 
-       if (clnt_cred != NULL)
-       {
+       if (clnt_cred != NULL) {
                clnt->ptr_cred = 1;
                memcpy(&(clnt->cred), clnt_cred, sizeof(clnt->cred));
-       }
-       else
-       {
+       } else {
                clnt->ptr_cred = 0;
        }
 }
 
 /*******************************************************************
-reads or writes a DOM_CLNT_INFO2 structure.
+ Reads or writes a DOM_CLNT_INFO2 structure.
 ********************************************************************/
-void smb_io_clnt_info2(char *desc,  DOM_CLNT_INFO2 *clnt, prs_struct *ps, int depth)
+
+BOOL smb_io_clnt_info2(char *desc, DOM_CLNT_INFO2 *clnt, prs_struct *ps, int depth)
 {
-       if (clnt == NULL) return;
+       if (clnt == NULL)
+               return False;
 
        prs_debug(ps, depth, desc, "smb_io_clnt_info2");
        depth++;
 
-       prs_align(ps);
+       if(!prs_align(ps))
+               return False;
        
-       smb_io_clnt_srv("", &(clnt->login), ps, depth);
+       if(!smb_io_clnt_srv("", &clnt->login, ps, depth))
+               return False;
 
-       prs_align(ps);
+       if(!prs_align(ps))
+               return False;
        
-       prs_uint32("ptr_cred", ps, depth, &(clnt->ptr_cred));
-       smb_io_cred    ("", &(clnt->cred ), ps, depth);
+       if(!prs_uint32("ptr_cred", ps, depth, &clnt->ptr_cred))
+               return False;
+       if(!smb_io_cred("", &clnt->cred, ps, depth))
+               return False;
+
+       return True;
 }
 
 /*******************************************************************
-makes a DOM_CLNT_INFO structure.
+ Inits a DOM_CLNT_INFO structure.
 ********************************************************************/
-void make_clnt_info(DOM_CLNT_INFO *clnt,
+
+void init_clnt_info(DOM_CLNT_INFO *clnt,
                char *logon_srv, char *acct_name,
                uint16 sec_chan, char *comp_name,
                                DOM_CRED *cred)
 {
-       if (clnt == NULL || cred == NULL) return;
-
        DEBUG(5,("make_clnt_info\n"));
 
-       make_log_info(&(clnt->login), logon_srv, acct_name, sec_chan, comp_name);
-       memcpy(&(clnt->cred), cred, sizeof(clnt->cred));
+       init_log_info(&clnt->login, logon_srv, acct_name, sec_chan, comp_name);
+       memcpy(&clnt->cred, cred, sizeof(clnt->cred));
 }
 
 /*******************************************************************
-reads or writes a DOM_CLNT_INFO structure.
+ Reads or writes a DOM_CLNT_INFO structure.
 ********************************************************************/
-void smb_io_clnt_info(char *desc,  DOM_CLNT_INFO *clnt, prs_struct *ps, int depth)
+
+BOOL smb_io_clnt_info(char *desc,  DOM_CLNT_INFO *clnt, prs_struct *ps, int depth)
 {
-       if (clnt == NULL) return;
+       if (clnt == NULL)
+               return False;
 
        prs_debug(ps, depth, desc, "smb_io_clnt_info");
        depth++;
 
-       prs_align(ps);
+       if(!prs_align(ps))
+               return False;
        
-       smb_io_log_info("", &(clnt->login), ps, depth);
-       smb_io_cred    ("", &(clnt->cred ), ps, depth);
+       if(!smb_io_log_info("", &clnt->login, ps, depth))
+               return False;
+       if(!smb_io_cred("", &clnt->cred, ps, depth))
+               return False;
+
+       return True;
 }
 
 /*******************************************************************
-makes a DOM_LOGON_ID structure.
+ Inits a DOM_LOGON_ID structure.
 ********************************************************************/
-void make_logon_id(DOM_LOGON_ID *log, uint32 log_id_low, uint32 log_id_high)
-{
-       if (log == NULL) return;
 
+void init_logon_id(DOM_LOGON_ID *log, uint32 log_id_low, uint32 log_id_high)
+{
        DEBUG(5,("make_logon_id: %d\n", __LINE__));
 
        log->low  = log_id_low;
@@ -901,151 +1389,247 @@ void make_logon_id(DOM_LOGON_ID *log, uint32 log_id_low, uint32 log_id_high)
 }
 
 /*******************************************************************
-reads or writes a DOM_LOGON_ID structure.
+ Reads or writes a DOM_LOGON_ID structure.
 ********************************************************************/
-void smb_io_logon_id(char *desc,  DOM_LOGON_ID *log, prs_struct *ps, int depth)
+
+BOOL smb_io_logon_id(char *desc, DOM_LOGON_ID *log, prs_struct *ps, int depth)
 {
-       if (log == NULL) return;
+       if (log == NULL)
+               return False;
 
        prs_debug(ps, depth, desc, "smb_io_logon_id");
        depth++;
 
-       prs_align(ps);
+       if(!prs_align(ps))
+               return False;
        
-       prs_uint32("low ", ps, depth, &(log->low ));
-       prs_uint32("high", ps, depth, &(log->high));
+       if(!prs_uint32("low ", ps, depth, &log->low ))
+               return False;
+       if(!prs_uint32("high", ps, depth, &log->high))
+               return False;
+
+       return True;
 }
 
 /*******************************************************************
-makes an OWF_INFO structure.
+ Inits an OWF_INFO structure.
 ********************************************************************/
-void make_owf_info(OWF_INFO *hash, uint8 data[16])
-{
-       if (hash == NULL) return;
 
-       DEBUG(5,("make_owf_info: %d\n", __LINE__));
+void init_owf_info(OWF_INFO *hash, uint8 data[16])
+{
+       DEBUG(5,("init_owf_info: %d\n", __LINE__));
        
        if (data != NULL)
-       {
                memcpy(hash->data, data, sizeof(hash->data));
-       }
        else
-       {
-               bzero(hash->data, sizeof(hash->data));
-       }
+               memset((char *)hash->data, '\0', sizeof(hash->data));
 }
 
 /*******************************************************************
-reads or writes an OWF_INFO structure.
+ Reads or writes an OWF_INFO structure.
 ********************************************************************/
-void smb_io_owf_info(char *desc, OWF_INFO *hash, prs_struct *ps, int depth)
+
+BOOL smb_io_owf_info(char *desc, OWF_INFO *hash, prs_struct *ps, int depth)
 {
-       if (hash == NULL) return;
+       if (hash == NULL)
+               return False;
 
        prs_debug(ps, depth, desc, "smb_io_owf_info");
        depth++;
 
-       prs_align(ps);
+       if(!prs_align(ps))
+               return False;
        
-       prs_uint8s (False, "data", ps, depth, hash->data, 16);
+       if(!prs_uint8s (False, "data", ps, depth, hash->data, 16))
+               return False;
+
+       return True;
 }
 
 /*******************************************************************
-reads or writes a DOM_GID structure.
+ Reads or writes a DOM_GID structure.
 ********************************************************************/
-void smb_io_gid(char *desc,  DOM_GID *gid, prs_struct *ps, int depth)
+
+BOOL smb_io_gid(char *desc,  DOM_GID *gid, prs_struct *ps, int depth)
 {
-       if (gid == NULL) return;
+       if (gid == NULL)
+               return False;
 
        prs_debug(ps, depth, desc, "smb_io_gid");
        depth++;
 
-       prs_align(ps);
+       if(!prs_align(ps))
+               return False;
        
-       prs_uint32("g_rid", ps, depth, &(gid->g_rid));
-       prs_uint32("attr ", ps, depth, &(gid->attr ));
+       if(!prs_uint32("g_rid", ps, depth, &gid->g_rid))
+               return False;
+       if(!prs_uint32("attr ", ps, depth, &gid->attr))
+               return False;
+
+       return True;
 }
 
 /*******************************************************************
-reads or writes an POLICY_HND structure.
+ Reads or writes an POLICY_HND structure.
 ********************************************************************/
-void smb_io_pol_hnd(char *desc,  POLICY_HND *pol, prs_struct *ps, int depth)
+
+BOOL smb_io_pol_hnd(char *desc, POLICY_HND *pol, prs_struct *ps, int depth)
 {
-       if (pol == NULL) return;
+       if (pol == NULL)
+               return False;
 
        prs_debug(ps, depth, desc, "smb_io_pol_hnd");
        depth++;
 
-       prs_align(ps);
+       if(!prs_align(ps))
+               return False;
+
+       if(UNMARSHALLING(ps))
+               ZERO_STRUCTP(pol);
        
-       prs_uint8s (False, "data", ps, depth, pol->data, POL_HND_SIZE);
+       if (!prs_uint32("data1", ps, depth, &pol->data1))
+               return False;
+       if (!prs_uint32("data2", ps, depth, &pol->data2))
+               return False;
+       if (!prs_uint16("data3", ps, depth, &pol->data3))
+               return False;
+       if (!prs_uint16("data4", ps, depth, &pol->data4))
+               return False;
+       if(!prs_uint8s (False, "data5", ps, depth, pol->data5, sizeof(pol->data5)))
+               return False;
+
+       return True;
+}
+
+/*******************************************************************
+ Create a UNISTR3.
+********************************************************************/
+
+void init_unistr3(UNISTR3 *str, const char *buf)
+{
+       size_t len;
+
+       if (buf == NULL) {
+               str->uni_str_len=0;
+               str->str.buffer = NULL;
+               return;
+       }
+
+       len = strlen(buf) + 1;
+
+       str->uni_str_len=len;
+
+       if (len < MAX_UNISTRLEN)
+               len = MAX_UNISTRLEN;
+
+       len *= sizeof(uint16);
+
+       str->str.buffer = (uint16 *)talloc_zero(get_talloc_ctx(), len);
+       if (str->str.buffer == NULL)
+               smb_panic("init_unistr3: malloc fail\n");
+
+       rpcstr_push((char *)str->str.buffer, buf, len, STR_TERMINATE);
 }
 
 /*******************************************************************
-reads or writes a dom query structure.
+ Reads or writes a UNISTR3 structure.
 ********************************************************************/
-static void smb_io_dom_query(char *desc,  DOM_QUERY *d_q, prs_struct *ps, int depth)
+
+BOOL smb_io_unistr3(char *desc, UNISTR3 *name, prs_struct *ps, int depth)
 {
-       if (d_q == NULL) return;
+       if (name == NULL)
+               return False;
 
-       prs_debug(ps, depth, desc, "smb_io_dom_query");
+       prs_debug(ps, depth, desc, "smb_io_unistr3");
        depth++;
 
-       prs_align(ps);
+       if(!prs_align(ps))
+               return False;
        
-       prs_uint16("uni_dom_max_len", ps, depth, &(d_q->uni_dom_max_len)); /* domain name string length * 2 */
-       prs_uint16("uni_dom_str_len", ps, depth, &(d_q->uni_dom_str_len)); /* domain name string length * 2 */
+       if(!prs_uint32("uni_str_len", ps, depth, &name->uni_str_len))
+               return False;
 
-       prs_uint32("buffer_dom_name", ps, depth, &(d_q->buffer_dom_name)); /* undocumented domain name string buffer pointer */
-       prs_uint32("buffer_dom_sid ", ps, depth, &(d_q->buffer_dom_sid )); /* undocumented domain SID string buffer pointer */
+       /* don't know if len is specified by uni_str_len member... */
+       /* assume unicode string is unicode-null-terminated, instead */
 
-       smb_io_unistr2("unistr2", &(d_q->uni_domain_name), d_q->buffer_dom_name, ps, depth); /* domain name (unicode string) */
+       if(!prs_unistr3(True, "unistr", name, ps, depth))
+               return False;
 
-       if (d_q->buffer_dom_sid != 0)
-       {
-               smb_io_dom_sid2("", &(d_q->dom_sid), ps, depth); /* domain SID */
-       }
-       else
-       {
-               bzero(&(d_q->dom_sid), sizeof(d_q->dom_sid));
-       }
+       return True;
 }
 
+
 /*******************************************************************
-reads or writes a dom query structure.
-********************************************************************/
-void smb_io_dom_query_3(char *desc,  DOM_QUERY_3 *d_q, prs_struct *ps, int depth)
+ Stream a uint64_struct
+ ********************************************************************/
+BOOL prs_uint64(char *name, prs_struct *ps, int depth, UINT64_S *data64)
 {
-       smb_io_dom_query("", d_q, ps, depth);
+       return prs_uint32(name, ps, depth+1, &data64->low) &&
+               prs_uint32(name, ps, depth+1, &data64->high);
 }
 
 /*******************************************************************
-reads or writes a dom query structure.
+reads or writes a BUFHDR2 structure.
 ********************************************************************/
-void smb_io_dom_query_5(char *desc,  DOM_QUERY_3 *d_q, prs_struct *ps, int depth)
+BOOL smb_io_bufhdr2(char *desc, BUFHDR2 *hdr, prs_struct *ps, int depth)
 {
-       smb_io_dom_query("", d_q, ps, depth);
-}
+       prs_debug(ps, depth, desc, "smb_io_bufhdr2");
+       depth++;
 
+       prs_align(ps);
+       prs_uint32("info_level", ps, depth, &(hdr->info_level));
+       prs_uint32("length    ", ps, depth, &(hdr->length    ));
+       prs_uint32("buffer    ", ps, depth, &(hdr->buffer    ));
+
+       return True;
+}
 
 /*******************************************************************
-reads or writes a UNISTR3 structure.
+reads or writes a BUFFER4 structure.
 ********************************************************************/
-void smb_io_unistr3(char *desc,  UNISTR3 *name, prs_struct *ps, int depth)
+BOOL smb_io_buffer4(char *desc, BUFFER4 *buf4, uint32 buffer, prs_struct *ps, int depth)
 {
-       if (name == NULL) return;
-
-       prs_debug(ps, depth, desc, "smb_io_unistr3");
+       prs_debug(ps, depth, desc, "smb_io_buffer4");
        depth++;
 
        prs_align(ps);
-       
-       prs_uint32("uni_str_len", ps, depth, &(name->uni_str_len));
+       prs_uint32("buf_len", ps, depth, &(buf4->buf_len));
 
-       /* don't know if len is specified by uni_str_len member... */
-       /* assume unicode string is unicode-null-terminated, instead */
+       if (buf4->buf_len > MAX_BUFFERLEN)
+       {
+               buf4->buf_len = MAX_BUFFERLEN;
+       }
+
+       prs_uint8s(True, "buffer", ps, depth, buf4->buffer, buf4->buf_len);
+
+       return True;
+}
+
+/*******************************************************************
+creates a UNIHDR structure.
+********************************************************************/
 
-       prs_unistr3(True, "unistr", name, ps, depth);
+BOOL make_uni_hdr(UNIHDR *hdr, int len)
+{
+       if (hdr == NULL)
+       {
+               return False;
+       }
+       hdr->uni_str_len = 2 * len;
+       hdr->uni_max_len = 2 * len;
+       hdr->buffer      = len != 0 ? 1 : 0;
+
+       return True;
 }
 
+/*******************************************************************
+creates a BUFHDR2 structure.
+********************************************************************/
+BOOL make_bufhdr2(BUFHDR2 *hdr, uint32 info_level, uint32 length, uint32 buffer)
+{
+       hdr->info_level = info_level;
+       hdr->length     = length;
+       hdr->buffer     = buffer;
 
+       return True;
+}