Remove more redundant lsa parsing functions.
[bbaumbach/samba-autobuild/.git] / source / rpc_parse / parse_misc.c
index 9d3bd6f28a2823e1eabe4c3e5d743d6ba2c43ca8..9e1937ea328a18329ddcd8cf118143c67e9c2088 100644 (file)
@@ -4,10 +4,11 @@
  *  Copyright (C) Andrew Tridgell              1992-1997,
  *  Copyright (C) Luke Kenneth Casson Leighton 1996-1997,
  *  Copyright (C) Paul Ashton                       1997.
+ *  Copyright (C) Gerald (Jerry) Carter             2005
  *  
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
+ *  the Free Software Foundation; either version 3 of the License, or
  *  (at your option) any later version.
  *  
  *  This program is distributed in the hope that it will be useful,
@@ -16,8 +17,7 @@
  *  GNU General Public License for more details.
  *  
  *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *  along with this program; if not, see <http://www.gnu.org/licenses/>.
  */
 
 #include "includes.h"
 #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("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.
 ********************************************************************/
 
-static BOOL smb_io_utime(const char *desc, UTIME *t, prs_struct *ps, int depth)
+static bool smb_io_utime(const char *desc, UTIME *t, prs_struct *ps, int depth)
 {
        if (t == NULL)
                return False;
@@ -112,8 +50,9 @@ static BOOL smb_io_utime(const char *desc, UTIME *t, prs_struct *ps, int depth)
  Reads or writes an NTTIME structure.
 ********************************************************************/
 
-BOOL smb_io_time(const char *desc, NTTIME *nttime, prs_struct *ps, int depth)
+bool smb_io_time(const char *desc, NTTIME *nttime, prs_struct *ps, int depth)
 {
+       uint32 low, high;
        if (nttime == NULL)
                return False;
 
@@ -123,34 +62,30 @@ BOOL smb_io_time(const char *desc, NTTIME *nttime, prs_struct *ps, int depth)
        if(!prs_align(ps))
                return False;
        
-       if(!prs_uint32("low ", ps, depth, &nttime->low)) /* low part */
+       if (MARSHALLING(ps)) {
+               low = *nttime & 0xFFFFFFFF;
+               high = *nttime >> 32;
+       }
+       
+       if(!prs_uint32("low ", ps, depth, &low)) /* low part */
                return False;
-       if(!prs_uint32("high", ps, depth, &nttime->high)) /* high part */
+       if(!prs_uint32("high", ps, depth, &high)) /* high part */
                return False;
 
+       if (UNMARSHALLING(ps)) {
+               *nttime = (((uint64_t)high << 32) + low);
+       }
+
        return True;
 }
 
 /*******************************************************************
- Reads or writes a LOOKUP_LEVEL structure.
+ Reads or writes an NTTIME structure.
 ********************************************************************/
 
-BOOL smb_io_lookup_level(const char *desc, LOOKUP_LEVEL *level, prs_struct *ps, int depth)
+bool smb_io_nttime(const char *desc, prs_struct *ps, int depth, NTTIME *nttime)
 {
-       if (level == NULL)
-               return False;
-
-       prs_debug(ps, depth, desc, "smb_io_lookup_level");
-       depth++;
-
-       if(!prs_align(ps))
-               return False;
-       if(!prs_uint16("value", ps, depth, &level->value))
-               return False;
-       if(!prs_align(ps))
-               return False;
-
-       return True;
+       return smb_io_time( desc, nttime, ps, depth );
 }
 
 /*******************************************************************
@@ -178,7 +113,7 @@ void init_enum_hnd(ENUM_HND *enh, uint32 hnd)
  Reads or writes an ENUM_HND structure.
 ********************************************************************/
 
-BOOL smb_io_enum_hnd(const char *desc, ENUM_HND *hnd, prs_struct *ps, int depth)
+bool smb_io_enum_hnd(const char *desc, ENUM_HND *hnd, prs_struct *ps, int depth)
 {
        if (hnd == NULL)
                return False;
@@ -204,7 +139,7 @@ BOOL smb_io_enum_hnd(const char *desc, ENUM_HND *hnd, prs_struct *ps, int depth)
  Reads or writes a DOM_SID structure.
 ********************************************************************/
 
-BOOL smb_io_dom_sid(const char *desc, DOM_SID *sid, prs_struct *ps, int depth)
+bool smb_io_dom_sid(const char *desc, DOM_SID *sid, prs_struct *ps, int depth)
 {
        int i;
 
@@ -216,6 +151,7 @@ BOOL smb_io_dom_sid(const char *desc, DOM_SID *sid, prs_struct *ps, int depth)
 
        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;
 
@@ -238,70 +174,47 @@ BOOL smb_io_dom_sid(const char *desc, DOM_SID *sid, prs_struct *ps, int depth)
 }
 
 /*******************************************************************
- 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
+ Inits a DOM_SID2 structure.
 ********************************************************************/
 
-void init_dom_sid(DOM_SID *sid, const char *str_sid)
+void init_dom_sid2(DOM_SID2 *sid2, const DOM_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));
+       sid2->sid = *sid;
+       sid2->num_auths = sid2->sid.num_auths;
+}
 
-       /* assume, but should check, that domsid starts "S-" */
-       p = strtok(domsid+2,"-");
-       sid->sid_rev_num = atoi(p);
+/*******************************************************************
+ Reads or writes a DOM_SID2 structure.
+********************************************************************/
 
-       /* identauth in decimal should be <  2^32 */
-       /* identauth in hex     should be >= 2^32 */
-       identauth = atoi(strtok(0,"-"));
+bool smb_io_dom_sid2_p(const char *desc, prs_struct *ps, int depth, DOM_SID2 **sid2)
+{
+       uint32 data_p;
 
-       DEBUG(4,("netlogon rev %d\n", sid->sid_rev_num));
-       DEBUG(4,("netlogon %s ia %d\n", p, identauth));
+       /* caputure the pointer value to stream */
 
-       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);
+       data_p = *sid2 ? 0xf000baaa : 0;
 
-       sid->num_auths = 0;
+       if ( !prs_uint32("dom_sid2_p", ps, depth, &data_p ))
+               return False;
 
-       while ((p = strtok(0, "-")) != NULL && sid->num_auths < MAXSUBAUTHS)
-               sid->sub_auths[sid->num_auths++] = atoi(p);
+       /* we're done if there is no data */
 
-       DEBUG(4,("init_dom_sid: %d SID:  %s\n", __LINE__, domsid));
-}
+       if ( !data_p )
+               return True;
 
-/*******************************************************************
- Inits a DOM_SID2 structure.
-********************************************************************/
+       if (UNMARSHALLING(ps)) {
+               if ( !(*sid2 = PRS_ALLOC_MEM(ps, DOM_SID2, 1)) )
+                       return False;
+       }
 
-void init_dom_sid2(DOM_SID2 *sid2, const DOM_SID *sid)
-{
-       sid2->sid = *sid;
-       sid2->num_auths = sid2->sid.num_auths;
+       return True;
 }
-
 /*******************************************************************
  Reads or writes a DOM_SID2 structure.
 ********************************************************************/
 
-BOOL smb_io_dom_sid2(const char *desc, DOM_SID2 *sid, prs_struct *ps, int depth)
+bool smb_io_dom_sid2(const char *desc, DOM_SID2 *sid, prs_struct *ps, int depth)
 {
        if (sid == NULL)
                return False;
@@ -321,6 +234,34 @@ BOOL smb_io_dom_sid2(const char *desc, DOM_SID2 *sid, prs_struct *ps, int depth)
        return True;
 }
 
+/*******************************************************************
+ Reads or writes a struct GUID
+********************************************************************/
+
+bool smb_io_uuid(const char *desc, struct GUID *uuid, 
+                prs_struct *ps, int depth)
+{
+       if (uuid == NULL)
+               return False;
+
+       prs_debug(ps, depth, desc, "smb_io_uuid");
+       depth++;
+
+       if(!prs_uint32 ("data   ", ps, depth, &uuid->time_low))
+               return False;
+       if(!prs_uint16 ("data   ", ps, depth, &uuid->time_mid))
+               return False;
+       if(!prs_uint16 ("data   ", ps, depth, &uuid->time_hi_and_version))
+               return False;
+
+       if(!prs_uint8s (False, "data   ", ps, depth, uuid->clock_seq, sizeof(uuid->clock_seq)))
+               return False;
+       if(!prs_uint8s (False, "data   ", ps, depth, uuid->node, sizeof(uuid->node)))
+               return False;
+
+       return True;
+}
+
 /*******************************************************************
 creates a STRHDR structure.
 ********************************************************************/
@@ -336,7 +277,7 @@ void init_str_hdr(STRHDR *hdr, int max_len, int len, uint32 buffer)
  Reads or writes a STRHDR structure.
 ********************************************************************/
 
-BOOL smb_io_strhdr(const char *desc,  STRHDR *hdr, prs_struct *ps, int depth)
+bool smb_io_strhdr(const char *desc,  STRHDR *hdr, prs_struct *ps, int depth)
 {
        if (hdr == NULL)
                return False;
@@ -360,18 +301,18 @@ BOOL smb_io_strhdr(const char *desc,  STRHDR *hdr, prs_struct *ps, int depth)
  Inits a UNIHDR structure.
 ********************************************************************/
 
-void init_uni_hdr(UNIHDR *hdr, int len)
+void init_uni_hdr(UNIHDR *hdr, UNISTR2 *str2)
 {
-       hdr->uni_str_len = 2 * len;
-       hdr->uni_max_len = 2 * len;
-       hdr->buffer      = len != 0 ? 1 : 0;
+       hdr->uni_str_len = 2 * (str2->uni_str_len);
+       hdr->uni_max_len = 2 * (str2->uni_max_len);
+       hdr->buffer = (str2->uni_str_len != 0) ? 1 : 0;
 }
 
 /*******************************************************************
  Reads or writes a UNIHDR structure.
 ********************************************************************/
 
-BOOL smb_io_unihdr(const char *desc, UNIHDR *hdr, prs_struct *ps, int depth)
+bool smb_io_unihdr(const char *desc, UNIHDR *hdr, prs_struct *ps, int depth)
 {
        if (hdr == NULL)
                return False;
@@ -407,7 +348,7 @@ void init_buf_hdr(BUFHDR *hdr, int max_len, int len)
  uint16 should be stored, or gets the size if reading.
  ********************************************************************/
 
-BOOL smb_io_hdrbuf_pre(const char *desc, BUFHDR *hdr, prs_struct *ps, int depth, uint32 *offset)
+bool smb_io_hdrbuf_pre(const char *desc, BUFHDR *hdr, prs_struct *ps, int depth, uint32 *offset)
 {
        (*offset) = prs_offset(ps);
        if (ps->io) {
@@ -433,7 +374,7 @@ BOOL smb_io_hdrbuf_pre(const char *desc, BUFHDR *hdr, prs_struct *ps, int depth,
  Does nothing on reading, as that is already handled by ...._pre()
  ********************************************************************/
 
-BOOL smb_io_hdrbuf_post(const char *desc, BUFHDR *hdr, prs_struct *ps, int depth, 
+bool smb_io_hdrbuf_post(const char *desc, BUFHDR *hdr, prs_struct *ps, int depth, 
                                uint32 ptr_hdrbuf, uint32 max_len, uint32 len)
 {
        if (!ps->io) {
@@ -458,7 +399,7 @@ BOOL smb_io_hdrbuf_post(const char *desc, BUFHDR *hdr, prs_struct *ps, int depth
  Reads or writes a BUFHDR structure.
 ********************************************************************/
 
-BOOL smb_io_hdrbuf(const char *desc, BUFHDR *hdr, prs_struct *ps, int depth)
+bool smb_io_hdrbuf(const char *desc, BUFHDR *hdr, prs_struct *ps, int depth)
 {
        if (hdr == NULL)
                return False;
@@ -477,39 +418,6 @@ BOOL smb_io_hdrbuf(const char *desc, BUFHDR *hdr, prs_struct *ps, int depth)
        return True;
 }
 
-/*******************************************************************
-creates a UNIHDR2 structure.
-********************************************************************/
-
-void init_uni_hdr2(UNIHDR2 *hdr, int len)
-{
-       init_uni_hdr(&hdr->unihdr, len);
-       hdr->buffer = (len > 0) ? 1 : 0;
-}
-
-/*******************************************************************
- Reads or writes a UNIHDR2 structure.
-********************************************************************/
-
-BOOL smb_io_unihdr2(const char *desc, UNIHDR2 *hdr2, prs_struct *ps, int depth)
-{
-       if (hdr2 == NULL)
-               return False;
-
-       prs_debug(ps, depth, desc, "smb_io_unihdr2");
-       depth++;
-
-       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;
-
-       return True;
-}
-
 /*******************************************************************
  Inits a UNISTR structure.
 ********************************************************************/
@@ -522,19 +430,11 @@ void init_unistr(UNISTR *str, const char *buf)
                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);
+       len = rpcstr_push_talloc(talloc_tos(), &str->buffer, buf);
+       if (len == (size_t)-1) {
+               str->buffer = NULL;
+       }
 }
 
 /*******************************************************************
@@ -542,7 +442,7 @@ reads or writes a UNISTR structure.
 XXXX NOTE: UNISTR structures NEED to be null-terminated.
 ********************************************************************/
 
-BOOL smb_io_unistr(const char *desc, UNISTR *uni, prs_struct *ps, int depth)
+bool smb_io_unistr(const char *desc, UNISTR *uni, prs_struct *ps, int depth)
 {
        if (uni == NULL)
                return False;
@@ -557,122 +457,86 @@ BOOL smb_io_unistr(const char *desc, UNISTR *uni, prs_struct *ps, int depth)
 }
 
 /*******************************************************************
- Allocate the BUFFER3 memory.
+ Allocate the RPC_DATA_BLOB memory.
 ********************************************************************/
 
-static void create_buffer3(BUFFER3 *str, size_t len)
+static void create_rpc_blob(RPC_DATA_BLOB *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");
-
+       if (len) {
+               str->buffer = (uint8 *)TALLOC_ZERO(talloc_tos(), len);
+               if (str->buffer == NULL)
+                       smb_panic("create_rpc_blob: talloc fail");
+               str->buf_len = len;
+       } else {
+               str->buffer = NULL;
+               str->buf_len = 0;
+       }
 }
 
 /*******************************************************************
- Inits a BUFFER3 structure from a uint32
+ Inits a RPC_DATA_BLOB structure from a uint32
 ********************************************************************/
 
-void init_buffer3_uint32(BUFFER3 *str, uint32 val)
+void init_rpc_blob_uint32(RPC_DATA_BLOB *str, uint32 val)
 {
        ZERO_STRUCTP(str);
 
        /* set up string lengths. */
-       str->buf_max_len = sizeof(uint32);
-       str->buf_len     = sizeof(uint32);
-
-       create_buffer3(str, sizeof(uint32));
+       create_rpc_blob(str, sizeof(uint32));
        SIVAL(str->buffer, 0, val);
 }
 
 /*******************************************************************
- Inits a BUFFER3 structure.
+ Inits a RPC_DATA_BLOB structure.
 ********************************************************************/
 
-void init_buffer3_str(BUFFER3 *str, const char *buf, int len)
+void init_rpc_blob_str(RPC_DATA_BLOB *str, const char *buf, int len)
 {
        ZERO_STRUCTP(str);
 
        /* set up string lengths. */
-       str->buf_max_len = len * 2;
-       str->buf_len = len * 2;
-
-       create_buffer3(str, str->buf_max_len);
-
-       rpcstr_push(str->buffer, buf, str->buf_max_len, STR_TERMINATE);
-       
+       if (len) {
+               create_rpc_blob(str, len*2);
+               rpcstr_push(str->buffer, buf, (size_t)str->buf_len, STR_TERMINATE);
+       }
 }
 
 /*******************************************************************
- Inits a BUFFER3 structure from a hex string.
+ Inits a RPC_DATA_BLOB structure from a hex string.
 ********************************************************************/
 
-void init_buffer3_hex(BUFFER3 *str, const char *buf)
+void init_rpc_blob_hex(RPC_DATA_BLOB *str, const 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);
+       if (buf && *buf) {
+               size_t len = strlen(buf);
+               create_rpc_blob(str, len);
+               str->buf_len = strhex_to_str((char *)str->buffer, str->buf_len,
+                               buf, len);
+       }
 }
 
 /*******************************************************************
- Inits a BUFFER3 structure.
+ Inits a RPC_DATA_BLOB structure.
 ********************************************************************/
 
-void init_buffer3_bytes(BUFFER3 *str, uint8 *buf, int len)
+void init_rpc_blob_bytes(RPC_DATA_BLOB *str, uint8 *buf, size_t len)
 {
        ZERO_STRUCTP(str);
 
        /* max buffer size (allocated size) */
-       str->buf_max_len = len;
-       if (buf != NULL) {
-               create_buffer3(str, len);
+       if (buf != NULL && len) {
+               create_rpc_blob(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.
-********************************************************************/
-
-BOOL smb_io_buffer3(const char *desc, BUFFER3 *buf3, prs_struct *ps, int depth)
-{
-       if (buf3 == NULL)
-               return False;
-
-       prs_debug(ps, depth, desc, "smb_io_buffer3");
-       depth++;
-
-       if(!prs_align(ps))
-               return False;
-       
-       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;
-
-       if(!prs_uint32("buf_len    ", ps, depth, &buf3->buf_len))
-               return False;
-
-       return True;
+       str->buf_len = len;
 }
 
 /*******************************************************************
 reads or writes a BUFFER5 structure.
 the buf_len member tells you how large the buffer is.
 ********************************************************************/
-BOOL smb_io_buffer5(const char *desc, BUFFER5 *buf5, prs_struct *ps, int depth)
+bool smb_io_buffer5(const char *desc, BUFFER5 *buf5, prs_struct *ps, int depth)
 {
        prs_debug(ps, depth, desc, "smb_io_buffer5");
        depth++;
@@ -693,67 +557,56 @@ BOOL smb_io_buffer5(const char *desc, BUFFER5 *buf5, prs_struct *ps, int depth)
 }
 
 /*******************************************************************
- Inits a BUFFER2 structure.
+ Inits a REGVAL_BUFFER structure.
 ********************************************************************/
 
-void init_buffer2(BUFFER2 *str, const uint8 *buf, int len)
+void init_regval_buffer(REGVAL_BUFFER *str, const uint8 *buf, size_t len)
 {
        ZERO_STRUCTP(str);
 
        /* max buffer size (allocated size) */
        str->buf_max_len = len;
-       str->undoc       = 0;
+       str->offset = 0;
        str->buf_len = buf != NULL ? len : 0;
 
        if (buf != NULL) {
-               if (len < MAX_BUFFERLEN)
-                       len = MAX_BUFFERLEN;
-               str->buffer = talloc_zero(get_talloc_ctx(), len);
+               SMB_ASSERT(str->buf_max_len >= str->buf_len);
+               str->buffer = (uint16 *)TALLOC_ZERO(talloc_tos(),
+                                                   str->buf_max_len);
                if (str->buffer == NULL)
-                       smb_panic("init_buffer2: talloc fail\n");
-               memcpy(str->buffer, buf, MIN(str->buf_len, len));
+                       smb_panic("init_regval_buffer: talloc fail");
+               memcpy(str->buffer, buf, str->buf_len);
        }
 }
 
 /*******************************************************************
- Reads or writes a BUFFER2 structure.
+ Reads or writes a REGVAL_BUFFER 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.
 ********************************************************************/
 
-BOOL smb_io_buffer2(const char *desc, BUFFER2 *buf2, uint32 buffer, prs_struct *ps, int depth)
+bool smb_io_regval_buffer(const char *desc, prs_struct *ps, int depth, REGVAL_BUFFER *buf2)
 {
-       if (buf2 == NULL)
-               return False;
-
-       if (buffer) {
 
-               prs_debug(ps, depth, desc, "smb_io_buffer2");
-               depth++;
+       prs_debug(ps, depth, desc, "smb_io_regval_buffer");
+       depth++;
 
-               if(!prs_align(ps))
-                       return False;
+       if(!prs_align(ps))
+               return False;
                
-               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 */
-
-               if(!prs_buffer2(True, "buffer     ", ps, depth, buf2))
-                       return False;
+       if(!prs_uint32("buf_max_len", ps, depth, &buf2->buf_max_len))
+               return False;
+       if(!prs_uint32("offset     ", ps, depth, &buf2->offset))
+               return False;
+       if(!prs_uint32("buf_len    ", ps, depth, &buf2->buf_len))
+               return False;
 
-       } else {
+       /* buffer advanced by indicated length of string
+          NOT by searching for null-termination */
 
-               prs_debug(ps, depth, desc, "smb_io_buffer2 - NULL");
-               depth++;
-               memset((char *)buf2, '\0', sizeof(*buf2));
+       if(!prs_regval_buffer(True, "buffer     ", ps, depth, buf2))
+               return False;
 
-       }
        return True;
 }
 
@@ -764,14 +617,11 @@ creates a UNISTR2 structure: sets up the buffer, too
 void init_buf_unistr2(UNISTR2 *str, uint32 *ptr, const char *buf)
 {
        if (buf != NULL) {
-
                *ptr = 1;
-               init_unistr2(str, buf, strlen(buf)+1);
-
+               init_unistr2(str, buf, UNI_STR_TERMINATE);
        } else {
-
                *ptr = 0;
-               init_unistr2(str, "", 0);
+               init_unistr2(str, NULL, UNI_FLAGS_NONE);
 
        }
 }
@@ -782,59 +632,61 @@ void init_buf_unistr2(UNISTR2 *str, uint32 *ptr, const char *buf)
 
 void copy_unistr2(UNISTR2 *str, const UNISTR2 *from)
 {
+       if (from->buffer == NULL) {
+               ZERO_STRUCTP(str);
+               return;
+       }
+
+       SMB_ASSERT(from->uni_max_len >= from->uni_str_len);
 
-       /* 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->offset      = from->offset;
        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;
+               if (str->uni_max_len) {
+                       str->buffer = (uint16 *)TALLOC_ZERO_ARRAY(talloc_tos(), uint16, str->uni_max_len);
+                       if ((str->buffer == NULL)) {
+                               smb_panic("copy_unistr2: talloc fail");
+                               return;
+                       }
+                       /* copy the string */
+                       memcpy(str->buffer, from->buffer, str->uni_max_len*sizeof(uint16));
+               } else {
+                       str->buffer = NULL;
                }
        }
-
-       /* copy the string */
-       memcpy(str->buffer, from->buffer, from->uni_max_len*sizeof(uint16));
 }
 
 /*******************************************************************
  Creates a STRING2 structure.
 ********************************************************************/
 
-void init_string2(STRING2 *str, const char *buf, int max_len, int str_len)
+void init_string2(STRING2 *str, const char *buf, size_t max_len, size_t str_len)
 {
-       int alloc_len = 0;
-
        /* set up string lengths. */
+       SMB_ASSERT(max_len >= str_len);
+
+       /* Ensure buf is valid if str_len was set. Coverity check. */
+       if (str_len && !buf) {
+               return;
+       }
+
        str->str_max_len = max_len;
-       str->undoc       = 0;
+       str->offset = 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);
+               str->buffer = (uint8 *)TALLOC_ZERO(talloc_tos(),
+                                                  str->str_max_len);
                if (str->buffer == NULL)
-                       smb_panic("init_string2: malloc fail\n");
+                       smb_panic("init_string2: malloc fail");
                memcpy(str->buffer, buf, str_len);
-  }
+       }
 }
 
 /*******************************************************************
@@ -844,7 +696,7 @@ void init_string2(STRING2 *str, const char *buf, int max_len, int str_len)
    the str_max_len member tells you how large the buffer is.
 ********************************************************************/
 
-BOOL smb_io_string2(const char *desc, STRING2 *str2, uint32 buffer, prs_struct *ps, int depth)
+bool smb_io_string2(const char *desc, STRING2 *str2, uint32 buffer, prs_struct *ps, int depth)
 {
        if (str2 == NULL)
                return False;
@@ -859,7 +711,7 @@ BOOL smb_io_string2(const char *desc, STRING2 *str2, uint32 buffer, prs_struct *
                
                if(!prs_uint32("str_max_len", ps, depth, &str2->str_max_len))
                        return False;
-               if(!prs_uint32("undoc      ", ps, depth, &str2->undoc))
+               if(!prs_uint32("offset     ", ps, depth, &str2->offset))
                        return False;
                if(!prs_uint32("str_str_len", ps, depth, &str2->str_str_len))
                        return False;
@@ -884,34 +736,86 @@ BOOL smb_io_string2(const char *desc, STRING2 *str2, uint32 buffer, prs_struct *
  Inits a UNISTR2 structure.
 ********************************************************************/
 
-void init_unistr2(UNISTR2 *str, const char *buf, size_t len)
+void init_unistr2(UNISTR2 *str, const char *buf, enum unistr2_term_codes flags)
 {
-       ZERO_STRUCTP(str);
+       size_t len = 0;
+       uint32 num_chars = 0;
 
-       /* set up string lengths. */
-       str->uni_max_len = (uint32)len;
-       str->undoc       = 0;
-       str->uni_str_len = (uint32)len;
+       if (buf) {
+               /* We always null terminate the copy. */
+               len = strlen(buf) + 1;
+               if ( flags == UNI_STR_DBLTERMINATE )
+                       len++;
+       }
 
-       if (len < MAX_UNISTRLEN)
-               len = MAX_UNISTRLEN;
-       len *= sizeof(uint16);
+       if (buf == NULL || len == 0) {
+               /* no buffer -- nothing to do */
+               str->uni_max_len = 0;
+               str->offset = 0;
+               str->uni_str_len = 0;
 
-       str->buffer = (uint16 *)talloc_zero(get_talloc_ctx(), len);
-       if ((str->buffer == NULL) && (len > 0))
-       {
-               smb_panic("init_unistr2: malloc fail\n");
                return;
        }
+       
+
+       str->buffer = TALLOC_ZERO_ARRAY(talloc_tos(), uint16, len);
+       if (str->buffer == NULL) {
+               smb_panic("init_unistr2: malloc fail");
+               return;
+       }
+
+       /* Ensure len is the length in *bytes* */
+       len *= sizeof(uint16);
 
        /*
-        * don't move this test above ! The UNISTR2 must be initialized !!!
+        * The UNISTR2 must be initialized !!!
         * jfm, 7/7/2001.
         */
-       if (buf==NULL)
+       if (buf) {
+               rpcstr_push((char *)str->buffer, buf, len, STR_TERMINATE);
+               num_chars = strlen_w(str->buffer);
+               if (flags == UNI_STR_TERMINATE || flags == UNI_MAXLEN_TERMINATE) {
+                       num_chars++;
+               }
+               if ( flags == UNI_STR_DBLTERMINATE )
+                       num_chars += 2;
+       }
+
+       str->uni_max_len = num_chars;
+       str->offset = 0;
+       str->uni_str_len = num_chars;
+       if ( num_chars && ((flags == UNI_MAXLEN_TERMINATE) || (flags == UNI_BROKEN_NON_NULL)) )
+               str->uni_max_len++;
+}
+
+/*******************************************************************
+ Inits a UNISTR4 structure.
+********************************************************************/
+
+void init_unistr4(UNISTR4 *uni4, const char *buf, enum unistr2_term_codes flags)
+{
+       uni4->string = TALLOC_P( talloc_tos(), UNISTR2 );
+       if (!uni4->string) {
+               smb_panic("init_unistr4: talloc fail");
                return;
+       }
+       init_unistr2( uni4->string, buf, flags );
+
+       uni4->length = 2 * (uni4->string->uni_str_len);
+       uni4->size   = 2 * (uni4->string->uni_max_len);
+}
+
+void init_unistr4_w( TALLOC_CTX *ctx, UNISTR4 *uni4, const smb_ucs2_t *buf )
+{
+       uni4->string = TALLOC_P( ctx, UNISTR2 );
+       if (!uni4->string) {
+               smb_panic("init_unistr4_w: talloc fail");
+               return;
+       }
+       init_unistr2_w( ctx, uni4->string, buf );
 
-       rpcstr_push((char *)str->buffer, buf, len, STR_TERMINATE);
+       uni4->length = 2 * (uni4->string->uni_str_len);
+       uni4->size   = 2 * (uni4->string->uni_max_len);
 }
 
 /** 
@@ -923,27 +827,23 @@ void init_unistr2(UNISTR2 *str, const char *buf, size_t len)
 
 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;
+       uint32 len = buf ? strlen_w(buf) : 0;
 
        ZERO_STRUCTP(str);
 
        /* set up string lengths. */
        str->uni_max_len = len;
-       str->undoc       = 0;
+       str->offset = 0;
        str->uni_str_len = 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;
+       if (len + 1) {
+               str->buffer = TALLOC_ZERO_ARRAY(ctx, uint16, len + 1);
+               if (str->buffer == NULL) {
+                       smb_panic("init_unistr2_w: talloc fail");
+                       return;
+               }
+       } else {
+               str->buffer = NULL;
        }
        
        /*
@@ -956,15 +856,17 @@ void init_unistr2_w(TALLOC_CTX *ctx, UNISTR2 *str, const smb_ucs2_t *buf)
        /* 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);
+       if (len+1) {
+               strncpy_w(str->buffer, buf, len + 1);
+       }
 }
 
 /*******************************************************************
  Inits a UNISTR2 structure from a UNISTR
 ********************************************************************/
-void init_unistr2_from_unistr (UNISTR2 *to, const UNISTR *from)
-{
 
+void init_unistr2_from_unistr(TALLOC_CTX *ctx, UNISTR2 *to, const UNISTR *from)
+{
        uint32 i;
 
        /* the destination UNISTR2 should never be NULL.
@@ -986,18 +888,96 @@ void init_unistr2_from_unistr (UNISTR2 *to, const UNISTR *from)
        /* 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->offset = 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));
-               
+       if (i) {
+               to->buffer = TALLOC_ZERO_ARRAY(ctx, uint16, i);
+               if (to->buffer == NULL)
+                       smb_panic("init_unistr2_from_unistr: talloc fail");
+               memcpy(to->buffer, from->buffer, i*sizeof(uint16));
+       } else {
+               to->buffer = NULL;
+       }
        return;
 }
 
+/*******************************************************************
+  Inits a UNISTR2 structure from a DATA_BLOB.
+  The length of the data_blob must count the bytes of the buffer.
+  Copies the blob data.
+********************************************************************/
+
+void init_unistr2_from_datablob(UNISTR2 *str, DATA_BLOB *blob) 
+{
+       /* Allocs the unistring */
+       init_unistr2(str, NULL, UNI_FLAGS_NONE);
+       
+       /* Sets the values */
+       str->uni_str_len = blob->length / sizeof(uint16);
+       str->uni_max_len = str->uni_str_len;
+       str->offset = 0;
+       if (blob->length) {
+               str->buffer = (uint16 *) memdup(blob->data, blob->length);
+       } else {
+               str->buffer = NULL;
+       }
+       if ((str->buffer == NULL) && (blob->length > 0)) {
+               smb_panic("init_unistr2_from_datablob: malloc fail");
+       }
+}
+
+/*******************************************************************
+ UNISTR2* are a little different in that the pointer and the UNISTR2
+ are not necessarily read/written back to back.  So we break it up 
+ into 2 separate functions.
+ See SPOOL_USER_1 in include/rpc_spoolss.h for an example.
+********************************************************************/
+
+bool prs_io_unistr2_p(const char *desc, prs_struct *ps, int depth, UNISTR2 **uni2)
+{
+       uint32 data_p;
+
+       /* caputure the pointer value to stream */
+
+       data_p = *uni2 ? 0xf000baaa : 0;
+
+       if ( !prs_uint32("ptr", ps, depth, &data_p ))
+               return False;
+
+       /* we're done if there is no data */
+
+       if ( !data_p )
+               return True;
+
+       if (UNMARSHALLING(ps)) {
+               if ( !(*uni2 = PRS_ALLOC_MEM(ps, UNISTR2, 1)) )
+                       return False;
+       }
+
+       return True;
+}
+
+/*******************************************************************
+ now read/write the actual UNISTR2.  Memory for the UNISTR2 (but
+ not UNISTR2.buffer) has been allocated previously by prs_unistr2_p()
+********************************************************************/
+
+bool prs_io_unistr2(const char *desc, prs_struct *ps, int depth, UNISTR2 *uni2 )
+{
+       /* just return true if there is no pointer to deal with.
+          the memory must have been previously allocated on unmarshalling
+          by prs_unistr2_p() */
+
+       if ( !uni2 )
+               return True;
+
+       /* just pass off to smb_io_unstr2() passing the uni2 address as 
+          the pointer (like you would expect) */
+
+       return smb_io_unistr2( desc, uni2, uni2 ? 1 : 0, ps, depth );
+}
 
 /*******************************************************************
  Reads or writes a UNISTR2 structure.
@@ -1006,7 +986,7 @@ void init_unistr2_from_unistr (UNISTR2 *to, const UNISTR *from)
    the uni_max_len member tells you how large the buffer is.
 ********************************************************************/
 
-BOOL smb_io_unistr2(const char *desc, UNISTR2 *uni2, uint32 buffer, prs_struct *ps, int depth)
+bool smb_io_unistr2(const char *desc, UNISTR2 *uni2, uint32 buffer, prs_struct *ps, int depth)
 {
        if (uni2 == NULL)
                return False;
@@ -1021,7 +1001,7 @@ BOOL smb_io_unistr2(const char *desc, UNISTR2 *uni2, uint32 buffer, prs_struct *
                
                if(!prs_uint32("uni_max_len", ps, depth, &uni2->uni_max_len))
                        return False;
-               if(!prs_uint32("undoc      ", ps, depth, &uni2->undoc))
+               if(!prs_uint32("offset     ", ps, depth, &uni2->offset))
                        return False;
                if(!prs_uint32("uni_str_len", ps, depth, &uni2->uni_str_len))
                        return False;
@@ -1042,73 +1022,220 @@ BOOL smb_io_unistr2(const char *desc, UNISTR2 *uni2, uint32 buffer, prs_struct *
        return True;
 }
 
-
 /*******************************************************************
- Reads or writes a UNISTR_ARRAY structure.
+ now read/write UNISTR4
 ********************************************************************/
-BOOL smb_io_unistr_array(const char *desc, UNISTR_ARRAY *array, prs_struct *ps, int depth)
+
+bool prs_unistr4(const char *desc, prs_struct *ps, int depth, UNISTR4 *uni4)
 {
-       int i;
+       void *ptr;
+       prs_debug(ps, depth, desc, "prs_unistr4");
+       depth++;
 
+       if ( !prs_uint16("length", ps, depth, &uni4->length ))
+               return False;
+       if ( !prs_uint16("size", ps, depth, &uni4->size ))
+               return False;
+               
+       ptr = uni4->string;
+
+       if ( !prs_pointer( desc, ps, depth, &ptr, sizeof(UNISTR2), (PRS_POINTER_CAST)prs_io_unistr2 ) )
+               return False;
+
+       uni4->string = (UNISTR2 *)ptr;
+       
+       return True;
+}
+
+/*******************************************************************
+ now read/write UNISTR4 header
+********************************************************************/
+
+bool prs_unistr4_hdr(const char *desc, prs_struct *ps, int depth, UNISTR4 *uni4)
+{
+       prs_debug(ps, depth, desc, "prs_unistr4_hdr");
        depth++;
 
-       array->count = 0;
+       if ( !prs_uint16("length", ps, depth, &uni4->length) )
+               return False;
+       if ( !prs_uint16("size", ps, depth, &uni4->size) )
+               return False;
+       if ( !prs_io_unistr2_p(desc, ps, depth, &uni4->string) )
+               return False;
+               
+       return True;
+}
+
+/*******************************************************************
+ now read/write UNISTR4 string
+********************************************************************/
+
+bool prs_unistr4_str(const char *desc, prs_struct *ps, int depth, UNISTR4 *uni4)
+{
+       prs_debug(ps, depth, desc, "prs_unistr4_str");
+       depth++;
 
-       if(!prs_uint32("ref_id", ps, depth, &array->ref_id))
+       if ( !prs_io_unistr2(desc, ps, depth, uni4->string) )
                return False;
+               
+       return True;
+}
 
-       if (! array->ref_id) {
-               return True;
-       }
+/*******************************************************************
+ Reads or writes a UNISTR4_ARRAY structure.
+********************************************************************/
+
+bool prs_unistr4_array(const char *desc, prs_struct *ps, int depth, UNISTR4_ARRAY *array )
+{
+       unsigned int i;
+
+       prs_debug(ps, depth, desc, "prs_unistr4_array");
+       depth++;
 
        if(!prs_uint32("count", ps, depth, &array->count))
                return False;
 
-       if (array->count == 0) {
-               return True;
+       if (UNMARSHALLING(ps)) {
+               if (array->count) {
+                       if ( !(array->strings = TALLOC_ZERO_ARRAY( talloc_tos(), UNISTR4, array->count)) )
+                               return False;
+               } else {
+                       array->strings = NULL;
+               }
        }
-
-       array->strings = talloc_zero(get_talloc_ctx(), array->count * sizeof(array->strings[0]));
-       if (! array->strings) {
-               return False;
+       
+       /* write the headers and then the actual string buffer */
+       
+       for ( i=0; i<array->count; i++ ) {
+               if ( !prs_unistr4_hdr( "string", ps, depth, &array->strings[i]) )
+                       return False;
        }
 
        for (i=0;i<array->count;i++) {
-               if(!prs_uint16("length", ps, depth, &array->strings[i].length))
-                       return False;
-               if(!prs_uint16("size", ps, depth, &array->strings[i].size))
-                       return False;
-               if(!prs_uint32("ref_id", ps, depth, &array->strings[i].ref_id))
+               if ( !prs_unistr4_str("string", ps, depth, &array->strings[i]) ) 
                        return False;
        }
+       
+       return True;
+}
 
-       for (i=0;i<array->count;i++) {
-               if (! smb_io_unistr2("string", &array->strings[i].string, array->strings[i].ref_id, ps, depth)) 
+/********************************************************************
+  initialise a UNISTR_ARRAY from a char**
+********************************************************************/
+
+bool init_unistr4_array( UNISTR4_ARRAY *array, uint32 count, const char **strings )
+{
+       unsigned int i;
+
+       array->count = count;
+
+       /* allocate memory for the array of UNISTR4 objects */
+
+       if (array->count) {
+               if ( !(array->strings = TALLOC_ZERO_ARRAY(talloc_tos(), UNISTR4, count )) )
                        return False;
+       } else {
+               array->strings = NULL;
        }
-       
+
+       for ( i=0; i<count; i++ ) 
+               init_unistr4( &array->strings[i], strings[i], UNI_STR_TERMINATE );
+
+       return True;
+}
+
+bool smb_io_lockout_string_hdr(const char *desc, HDR_LOCKOUT_STRING *hdr_account_lockout, prs_struct *ps, int depth)
+{
+       prs_debug(ps, depth, desc, "smb_io_lockout_string_hdr");
+       depth++;
+
+       if(!prs_align(ps))
+               return False;
+
+       if(!prs_uint16("size", ps, depth, &hdr_account_lockout->size))
+               return False;
+       if(!prs_uint16("length", ps, depth, &hdr_account_lockout->length))
+               return False;
+       if(!prs_uint32("buffer", ps, depth, &hdr_account_lockout->buffer))
+               return False;
+
+       return True;
+}
+
+bool smb_io_account_lockout_str(const char *desc, LOCKOUT_STRING *account_lockout, uint32 buffer, prs_struct *ps, int depth)
+{
+       prs_debug(ps, depth, desc, "smb_io_account_lockout_string");
+       depth++;
+
+       if(!prs_uint32("array_size", ps, depth, &account_lockout->array_size))
+               return False;
+
+       if(!prs_uint32("offset", ps, depth, &account_lockout->offset))
+               return False;
+       if(!prs_uint32("length", ps, depth, &account_lockout->length))
+               return False;
+
+       if (!prs_uint64("lockout_duration", ps, depth, &account_lockout->lockout_duration))
+               return False;
+       if (!prs_uint64("reset_count", ps, depth, &account_lockout->reset_count))
+               return False;
+       if (!prs_uint32("bad_attempt_lockout", ps, depth, &account_lockout->bad_attempt_lockout))
+               return False;
+       if (!prs_uint32("dummy", ps, depth, &account_lockout->dummy))
+               return False;
+#if 0
+       if(!prs_uint16s (False, "bindata", ps, depth, &account_lockout->bindata, length))
+               return False;
+#endif
+
        return True;
 }
 
+/*******************************************************************
+ Inits a DOM_RID structure.
+********************************************************************/
+
+void init_dom_rid(DOM_RID *prid, uint32 rid, uint16 type, uint32 idx)
+{
+       prid->type    = type;
+       prid->rid     = rid;
+       prid->rid_idx = idx;
+}
 
 /*******************************************************************
Inits a DOM_RID2 structure.
Reads or writes a DOM_RID structure.
 ********************************************************************/
 
-void init_dom_rid2(DOM_RID2 *rid2, uint32 rid, uint8 type, uint32 idx)
+bool smb_io_dom_rid(const char *desc, DOM_RID *rid, prs_struct *ps, int depth)
 {
-       rid2->type    = type;
-       rid2->rid     = rid;
-       rid2->rid_idx = idx;
+       if (rid == NULL)
+               return False;
+
+       prs_debug(ps, depth, desc, "smb_io_dom_rid");
+       depth++;
+
+       if(!prs_align(ps))
+               return False;
+   
+       if(!prs_uint16("type   ", ps, depth, &rid->type))
+               return False;
+       if(!prs_align(ps))
+               return False;
+       if(!prs_uint32("rid    ", ps, depth, &rid->rid))
+               return False;
+       if(!prs_uint32("rid_idx", ps, depth, &rid->rid_idx))
+               return False;
+
+       return True;
 }
 
 /*******************************************************************
  Reads or writes a DOM_RID2 structure.
 ********************************************************************/
 
-BOOL smb_io_dom_rid2(const char *desc, DOM_RID2 *rid2, prs_struct *ps, int depth)
+bool smb_io_dom_rid2(const char *desc, DOM_RID2 *rid, prs_struct *ps, int depth)
 {
-       if (rid2 == NULL)
+       if (rid == NULL)
                return False;
 
        prs_debug(ps, depth, desc, "smb_io_dom_rid2");
@@ -1117,18 +1244,21 @@ BOOL smb_io_dom_rid2(const char *desc, DOM_RID2 *rid2, prs_struct *ps, int depth
        if(!prs_align(ps))
                return False;
    
-       if(!prs_uint8("type   ", ps, depth, &rid2->type))
+       if(!prs_uint16("type   ", ps, depth, &rid->type))
                return False;
        if(!prs_align(ps))
                return False;
-       if(!prs_uint32("rid    ", ps, depth, &rid2->rid))
+       if(!prs_uint32("rid    ", ps, depth, &rid->rid))
                return False;
-       if(!prs_uint32("rid_idx", ps, depth, &rid2->rid_idx))
+       if(!prs_uint32("rid_idx", ps, depth, &rid->rid_idx))
+               return False;
+       if(!prs_uint32("unknown", ps, depth, &rid->unknown))
                return False;
 
        return True;
 }
 
+
 /*******************************************************************
 creates a DOM_RID3 structure.
 ********************************************************************/
@@ -1146,7 +1276,7 @@ void init_dom_rid3(DOM_RID3 *rid3, uint32 rid, uint8 type)
 reads or writes a DOM_RID3 structure.
 ********************************************************************/
 
-BOOL smb_io_dom_rid3(const char *desc, DOM_RID3 *rid3, prs_struct *ps, int depth)
+bool smb_io_dom_rid3(const char *desc, DOM_RID3 *rid3, prs_struct *ps, int depth)
 {
        if (rid3 == NULL)
                return False;
@@ -1186,22 +1316,23 @@ void init_dom_rid4(DOM_RID4 *rid4, uint16 unknown, uint16 attr, uint32 rid)
  Inits a DOM_CLNT_SRV structure.
 ********************************************************************/
 
-static void init_clnt_srv(DOM_CLNT_SRV *log, const char *logon_srv, const char *comp_name)
+void init_clnt_srv(DOM_CLNT_SRV *logcln, const char *logon_srv,
+                  const char *comp_name)
 {
        DEBUG(5,("init_clnt_srv: %d\n", __LINE__));
 
        if (logon_srv != NULL) {
-               log->undoc_buffer = 1;
-               init_unistr2(&log->uni_logon_srv, logon_srv, strlen(logon_srv)+1);
+               logcln->undoc_buffer = 1;
+               init_unistr2(&logcln->uni_logon_srv, logon_srv, UNI_STR_TERMINATE);
        } else {
-               log->undoc_buffer = 0;
+               logcln->undoc_buffer = 0;
        }
 
        if (comp_name != NULL) {
-               log->undoc_buffer2 = 1;
-               init_unistr2(&log->uni_comp_name, comp_name, strlen(comp_name)+1);
+               logcln->undoc_buffer2 = 1;
+               init_unistr2(&logcln->uni_comp_name, comp_name, UNI_STR_TERMINATE);
        } else {
-               log->undoc_buffer2 = 0;
+               logcln->undoc_buffer2 = 0;
        }
 }
 
@@ -1209,9 +1340,9 @@ static void init_clnt_srv(DOM_CLNT_SRV *log, const char *logon_srv, const char *
  Inits or writes a DOM_CLNT_SRV structure.
 ********************************************************************/
 
-static BOOL smb_io_clnt_srv(const char *desc, DOM_CLNT_SRV *log, prs_struct *ps, int depth)
+bool smb_io_clnt_srv(const char *desc, DOM_CLNT_SRV *logcln, prs_struct *ps, int depth)
 {
-       if (log == NULL)
+       if (logcln == NULL)
                return False;
 
        prs_debug(ps, depth, desc, "smb_io_clnt_srv");
@@ -1220,22 +1351,22 @@ static BOOL smb_io_clnt_srv(const char *desc, DOM_CLNT_SRV *log, prs_struct *ps,
        if(!prs_align(ps))
                return False;
        
-       if(!prs_uint32("undoc_buffer ", ps, depth, &log->undoc_buffer))
+       if(!prs_uint32("undoc_buffer ", ps, depth, &logcln->undoc_buffer))
                return False;
 
-       if (log->undoc_buffer != 0) {
-               if(!smb_io_unistr2("unistr2", &log->uni_logon_srv, log->undoc_buffer, ps, depth))
+       if (logcln->undoc_buffer != 0) {
+               if(!smb_io_unistr2("unistr2", &logcln->uni_logon_srv, logcln->undoc_buffer, ps, depth))
                        return False;
        }
 
        if(!prs_align(ps))
                return False;
 
-       if(!prs_uint32("undoc_buffer2", ps, depth, &log->undoc_buffer2))
+       if(!prs_uint32("undoc_buffer2", ps, depth, &logcln->undoc_buffer2))
                return False;
 
-       if (log->undoc_buffer2 != 0) {
-               if(!smb_io_unistr2("unistr2", &log->uni_comp_name, log->undoc_buffer2, ps, depth))
+       if (logcln->undoc_buffer2 != 0) {
+               if(!smb_io_unistr2("unistr2", &logcln->uni_comp_name, logcln->undoc_buffer2, ps, depth))
                        return False;
        }
 
@@ -1246,28 +1377,28 @@ static BOOL smb_io_clnt_srv(const char *desc, DOM_CLNT_SRV *log, prs_struct *ps,
  Inits a DOM_LOG_INFO structure.
 ********************************************************************/
 
-void init_log_info(DOM_LOG_INFO *log, const char *logon_srv, const char *acct_name,
+void init_log_info(DOM_LOG_INFO *loginfo, 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;
+       loginfo->undoc_buffer = 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);
+       init_unistr2(&loginfo->uni_logon_srv, logon_srv, UNI_STR_TERMINATE);
+       init_unistr2(&loginfo->uni_acct_name, acct_name, UNI_STR_TERMINATE);
 
-       log->sec_chan = sec_chan;
+       loginfo->sec_chan = sec_chan;
 
-       init_unistr2(&log->uni_comp_name, comp_name, strlen(comp_name)+1);
+       init_unistr2(&loginfo->uni_comp_name, comp_name, UNI_STR_TERMINATE);
 }
 
 /*******************************************************************
  Reads or writes a DOM_LOG_INFO structure.
 ********************************************************************/
 
-BOOL smb_io_log_info(const char *desc, DOM_LOG_INFO *log, prs_struct *ps, int depth)
+bool smb_io_log_info(const char *desc, DOM_LOG_INFO *loginfo, prs_struct *ps, int depth)
 {
-       if (log == NULL)
+       if (loginfo == NULL)
                return False;
 
        prs_debug(ps, depth, desc, "smb_io_log_info");
@@ -1276,18 +1407,18 @@ BOOL smb_io_log_info(const char *desc, DOM_LOG_INFO *log, prs_struct *ps, int de
        if(!prs_align(ps))
                return False;
        
-       if(!prs_uint32("undoc_buffer", ps, depth, &log->undoc_buffer))
+       if(!prs_uint32("undoc_buffer", ps, depth, &loginfo->undoc_buffer))
                return False;
 
-       if(!smb_io_unistr2("unistr2", &log->uni_logon_srv, True, ps, depth))
+       if(!smb_io_unistr2("unistr2", &loginfo->uni_logon_srv, True, ps, depth))
                return False;
-       if(!smb_io_unistr2("unistr2", &log->uni_acct_name, True, ps, depth))
+       if(!smb_io_unistr2("unistr2", &loginfo->uni_acct_name, True, ps, depth))
                return False;
 
-       if(!prs_uint16("sec_chan", ps, depth, &log->sec_chan))
+       if(!prs_uint16("sec_chan", ps, depth, &loginfo->sec_chan))
                return False;
 
-       if(!smb_io_unistr2("unistr2", &log->uni_comp_name, True, ps, depth))
+       if(!smb_io_unistr2("unistr2", &loginfo->uni_comp_name, True, ps, depth))
                return False;
 
        return True;
@@ -1297,7 +1428,7 @@ BOOL smb_io_log_info(const char *desc, DOM_LOG_INFO *log, prs_struct *ps, int de
  Reads or writes a DOM_CHAL structure.
 ********************************************************************/
 
-BOOL smb_io_chal(const char *desc, DOM_CHAL *chal, prs_struct *ps, int depth)
+bool smb_io_chal(const char *desc, DOM_CHAL *chal, prs_struct *ps, int depth)
 {
        if (chal == NULL)
                return False;
@@ -1315,7 +1446,7 @@ BOOL smb_io_chal(const char *desc, DOM_CHAL *chal, prs_struct *ps, int depth)
  Reads or writes a DOM_CRED structure.
 ********************************************************************/
 
-BOOL smb_io_cred(const char *desc,  DOM_CRED *cred, prs_struct *ps, int depth)
+bool smb_io_cred(const char *desc,  DOM_CRED *cred, prs_struct *ps, int depth)
 {
        if (cred == NULL)
                return False;
@@ -1359,7 +1490,7 @@ void init_clnt_info2(DOM_CLNT_INFO2 *clnt,
  Reads or writes a DOM_CLNT_INFO2 structure.
 ********************************************************************/
 
-BOOL smb_io_clnt_info2(const char *desc, DOM_CLNT_INFO2 *clnt, prs_struct *ps, int depth)
+bool smb_io_clnt_info2(const char *desc, DOM_CLNT_INFO2 *clnt, prs_struct *ps, int depth)
 {
        if (clnt == NULL)
                return False;
@@ -1403,7 +1534,7 @@ void init_clnt_info(DOM_CLNT_INFO *clnt,
  Reads or writes a DOM_CLNT_INFO structure.
 ********************************************************************/
 
-BOOL smb_io_clnt_info(const char *desc,  DOM_CLNT_INFO *clnt, prs_struct *ps, int depth)
+bool smb_io_clnt_info(const char *desc,  DOM_CLNT_INFO *clnt, prs_struct *ps, int depth)
 {
        if (clnt == NULL)
                return False;
@@ -1426,21 +1557,21 @@ BOOL smb_io_clnt_info(const char *desc,  DOM_CLNT_INFO *clnt, prs_struct *ps, in
  Inits a DOM_LOGON_ID structure.
 ********************************************************************/
 
-void init_logon_id(DOM_LOGON_ID *log, uint32 log_id_low, uint32 log_id_high)
+void init_logon_id(DOM_LOGON_ID *logonid, uint32 log_id_low, uint32 log_id_high)
 {
        DEBUG(5,("make_logon_id: %d\n", __LINE__));
 
-       log->low  = log_id_low;
-       log->high = log_id_high;
+       logonid->low  = log_id_low;
+       logonid->high = log_id_high;
 }
 
 /*******************************************************************
  Reads or writes a DOM_LOGON_ID structure.
 ********************************************************************/
 
-BOOL smb_io_logon_id(const char *desc, DOM_LOGON_ID *log, prs_struct *ps, int depth)
+bool smb_io_logon_id(const char *desc, DOM_LOGON_ID *logonid, prs_struct *ps, int depth)
 {
-       if (log == NULL)
+       if (logonid == NULL)
                return False;
 
        prs_debug(ps, depth, desc, "smb_io_logon_id");
@@ -1449,9 +1580,9 @@ BOOL smb_io_logon_id(const char *desc, DOM_LOGON_ID *log, prs_struct *ps, int de
        if(!prs_align(ps))
                return False;
        
-       if(!prs_uint32("low ", ps, depth, &log->low ))
+       if(!prs_uint32("low ", ps, depth, &logonid->low ))
                return False;
-       if(!prs_uint32("high", ps, depth, &log->high))
+       if(!prs_uint32("high", ps, depth, &logonid->high))
                return False;
 
        return True;
@@ -1475,7 +1606,7 @@ void init_owf_info(OWF_INFO *hash, const uint8 data[16])
  Reads or writes an OWF_INFO structure.
 ********************************************************************/
 
-BOOL smb_io_owf_info(const char *desc, OWF_INFO *hash, prs_struct *ps, int depth)
+bool smb_io_owf_info(const char *desc, OWF_INFO *hash, prs_struct *ps, int depth)
 {
        if (hash == NULL)
                return False;
@@ -1496,7 +1627,7 @@ BOOL smb_io_owf_info(const char *desc, OWF_INFO *hash, prs_struct *ps, int depth
  Reads or writes a DOM_GID structure.
 ********************************************************************/
 
-BOOL smb_io_gid(const char *desc,  DOM_GID *gid, prs_struct *ps, int depth)
+bool smb_io_gid(const char *desc,  DOM_GID *gid, prs_struct *ps, int depth)
 {
        if (gid == NULL)
                return False;
@@ -1519,7 +1650,7 @@ BOOL smb_io_gid(const char *desc,  DOM_GID *gid, prs_struct *ps, int depth)
  Reads or writes an POLICY_HND structure.
 ********************************************************************/
 
-BOOL smb_io_pol_hnd(const char *desc, POLICY_HND *pol, prs_struct *ps, int depth)
+bool smb_io_pol_hnd(const char *desc, POLICY_HND *pol, prs_struct *ps, int depth)
 {
        if (pol == NULL)
                return False;
@@ -1533,15 +1664,9 @@ BOOL smb_io_pol_hnd(const char *desc, POLICY_HND *pol, prs_struct *ps, int depth
        if(UNMARSHALLING(ps))
                ZERO_STRUCTP(pol);
        
-       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))
+       if (!prs_uint32("handle_type", ps, depth, &pol->handle_type))
                return False;
-       if (!prs_uint16("data4", ps, depth, &pol->data4))
-               return False;
-       if(!prs_uint8s (False, "data5", ps, depth, pol->data5, sizeof(pol->data5)))
+       if (!smb_io_uuid("uuid", (struct GUID*)&pol->uuid, ps, depth))
                return False;
 
        return True;
@@ -1553,35 +1678,30 @@ BOOL smb_io_pol_hnd(const char *desc, POLICY_HND *pol, prs_struct *ps, int depth
 
 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;
+       str->uni_str_len = strlen(buf) + 1;
 
-       if (len < MAX_UNISTRLEN)
-               len = MAX_UNISTRLEN;
+       if (str->uni_str_len) {
+               str->str.buffer = TALLOC_ZERO_ARRAY(talloc_tos(), uint16, str->uni_str_len);
+               if (str->str.buffer == NULL)
+                       smb_panic("init_unistr3: malloc fail");
 
-       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);
+               rpcstr_push((char *)str->str.buffer, buf, str->uni_str_len * sizeof(uint16), STR_TERMINATE);
+       } else {
+               str->str.buffer = NULL;
+       }
 }
 
 /*******************************************************************
  Reads or writes a UNISTR3 structure.
 ********************************************************************/
 
-BOOL smb_io_unistr3(const char *desc, UNISTR3 *name, prs_struct *ps, int depth)
+bool smb_io_unistr3(const char *desc, UNISTR3 *name, prs_struct *ps, int depth)
 {
        if (name == NULL)
                return False;
@@ -1594,6 +1714,11 @@ BOOL smb_io_unistr3(const char *desc, UNISTR3 *name, prs_struct *ps, int depth)
        
        if(!prs_uint32("uni_str_len", ps, depth, &name->uni_str_len))
                return False;
+               
+       /* we're done if there is no string */
+       
+       if ( name->uni_str_len == 0 )
+               return True;
 
        /* don't know if len is specified by uni_str_len member... */
        /* assume unicode string is unicode-null-terminated, instead */
@@ -1604,20 +1729,34 @@ BOOL smb_io_unistr3(const char *desc, UNISTR3 *name, prs_struct *ps, int depth)
        return True;
 }
 
-
 /*******************************************************************
  Stream a uint64_struct
  ********************************************************************/
-BOOL prs_uint64(const char *name, prs_struct *ps, int depth, UINT64_S *data64)
+bool prs_uint64(const char *name, prs_struct *ps, int depth, uint64 *data64)
 {
-       return prs_uint32(name, ps, depth+1, &data64->low) &&
-               prs_uint32(name, ps, depth+1, &data64->high);
+       if (UNMARSHALLING(ps)) {
+               uint32 high, low;
+
+               if (!prs_uint32(name, ps, depth+1, &low))
+                       return False;
+
+               if (!prs_uint32(name, ps, depth+1, &high))
+                       return False;
+
+               *data64 = ((uint64_t)high << 32) + low;
+
+               return True;
+       } else {
+               uint32 high = (*data64) >> 32, low = (*data64) & 0xFFFFFFFF;
+               return prs_uint32(name, ps, depth+1, &low) && 
+                          prs_uint32(name, ps, depth+1, &high);
+       }
 }
 
 /*******************************************************************
 reads or writes a BUFHDR2 structure.
 ********************************************************************/
-BOOL smb_io_bufhdr2(const char *desc, BUFHDR2 *hdr, prs_struct *ps, int depth)
+bool smb_io_bufhdr2(const char *desc, BUFHDR2 *hdr, prs_struct *ps, int depth)
 {
        prs_debug(ps, depth, desc, "smb_io_bufhdr2");
        depth++;
@@ -1631,22 +1770,45 @@ BOOL smb_io_bufhdr2(const char *desc, BUFHDR2 *hdr, prs_struct *ps, int depth)
 }
 
 /*******************************************************************
-reads or writes a BUFFER4 structure.
+reads or writes a BUFHDR4 structure.
 ********************************************************************/
-BOOL smb_io_buffer4(const char *desc, BUFFER4 *buf4, uint32 buffer, prs_struct *ps, int depth)
+bool smb_io_bufhdr4(const char *desc, BUFHDR4 *hdr, prs_struct *ps, int depth)
 {
-       prs_debug(ps, depth, desc, "smb_io_buffer4");
+       prs_debug(ps, depth, desc, "smb_io_bufhdr4");
        depth++;
 
        prs_align(ps);
-       prs_uint32("buf_len", ps, depth, &(buf4->buf_len));
+       prs_uint32("size", ps, depth, &hdr->size);
+       prs_uint32("buffer", ps, depth, &hdr->buffer);
 
-       if (buf4->buf_len > MAX_BUFFERLEN)
-       {
-               buf4->buf_len = MAX_BUFFERLEN;
+       return True;
+}
+
+/*******************************************************************
+reads or writes a RPC_DATA_BLOB structure.
+********************************************************************/
+
+bool smb_io_rpc_blob(const char *desc, RPC_DATA_BLOB *blob, prs_struct *ps, int depth)
+{
+       prs_debug(ps, depth, desc, "smb_io_rpc_blob");
+       depth++;
+
+       prs_align(ps);
+       if ( !prs_uint32("buf_len", ps, depth, &blob->buf_len) )
+               return False;
+
+       if ( blob->buf_len == 0 )
+               return True;
+
+       if (UNMARSHALLING(ps)) {
+               blob->buffer = PRS_ALLOC_MEM(ps, uint8, blob->buf_len);
+               if (!blob->buffer) {
+                       return False;
+               }
        }
 
-       prs_uint8s(True, "buffer", ps, depth, buf4->buffer, buf4->buf_len);
+       if ( !prs_uint8s(True, "buffer", ps, depth, blob->buffer, blob->buf_len) )
+               return False;
 
        return True;
 }
@@ -1655,7 +1817,7 @@ BOOL smb_io_buffer4(const char *desc, BUFFER4 *buf4, uint32 buffer, prs_struct *
 creates a UNIHDR structure.
 ********************************************************************/
 
-BOOL make_uni_hdr(UNIHDR *hdr, int len)
+bool make_uni_hdr(UNIHDR *hdr, int len)
 {
        if (hdr == NULL)
        {
@@ -1671,7 +1833,7 @@ BOOL make_uni_hdr(UNIHDR *hdr, int len)
 /*******************************************************************
 creates a BUFHDR2 structure.
 ********************************************************************/
-BOOL make_bufhdr2(BUFHDR2 *hdr, uint32 info_level, uint32 length, uint32 buffer)
+bool make_bufhdr2(BUFHDR2 *hdr, uint32 info_level, uint32 length, uint32 buffer)
 {
        hdr->info_level = info_level;
        hdr->length     = length;
@@ -1679,3 +1841,22 @@ BOOL make_bufhdr2(BUFHDR2 *hdr, uint32 info_level, uint32 length, uint32 buffer)
 
        return True;
 }
+
+/*******************************************************************
+return the length of a UNISTR string.
+********************************************************************/  
+
+uint32 str_len_uni(UNISTR *source)
+{
+       uint32 i=0;
+
+       if (!source->buffer)
+               return 0;
+
+       while (source->buffer[i])
+               i++;
+
+       return i;
+}
+
+