added the 'lsaenumacctwithright' command to rpcclient. This allows you
[ira/wip.git] / source3 / rpc_parse / parse_prs.c
index 94a6100aa1a0a873c9159784c25ae0b98aca5eb7..6f6117a9e2dc5c6fc356ef1c9425eae293e936c8 100644 (file)
@@ -1,6 +1,5 @@
 /* 
-   Unix SMB/Netbios implementation.
-   Version 1.9.
+   Unix SMB/CIFS implementation.
    Samba memory buffer functions
    Copyright (C) Andrew Tridgell              1992-1997
    Copyright (C) Luke Kenneth Casson Leighton 1996-1997
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
 
-extern int DEBUGLEVEL;
-
 #include "includes.h"
 
+#undef DBGC_CLASS
+#define DBGC_CLASS DBGC_RPC_PARSE
 
-/*******************************************************************
-dump a prs to a file
- ********************************************************************/
+/**
+ * Dump a prs to a file: from the current location through to the end.
+ **/
 void prs_dump(char *name, int v, prs_struct *ps)
+{
+       prs_dump_region(name, v, ps, ps->data_offset, ps->buffer_size);
+}
+
+
+/**
+ * Dump from the start of the prs to the current location.
+ **/
+void prs_dump_before(char *name, int v, prs_struct *ps)
+{
+       prs_dump_region(name, v, ps, 0, ps->data_offset);
+}
+
+
+/**
+ * Dump everything from the start of the prs up to the current location.
+ **/
+void prs_dump_region(char *name, int v, prs_struct *ps,
+                    int from_off, int to_off)
 {
        int fd, i;
        pstring fname;
        if (DEBUGLEVEL < 50) return;
        for (i=1;i<100;i++) {
                if (v != -1) {
-                       slprintf(fname,sizeof(fname), "/tmp/%s_%d.%d.prs", name, v, i);
+                       slprintf(fname,sizeof(fname)-1, "/tmp/%s_%d.%d.prs", name, v, i);
                } else {
-                       slprintf(fname,sizeof(fname), "/tmp/%s.%d.prs", name, i);
+                       slprintf(fname,sizeof(fname)-1, "/tmp/%s.%d.prs", name, i);
                }
                fd = open(fname, O_WRONLY|O_CREAT|O_EXCL, 0644);
                if (fd != -1 || errno != EEXIST) break;
        }
        if (fd != -1) {
-               write(fd, ps->data_p + ps->data_offset, ps->grow_size - ps->data_offset);
+               write(fd, ps->data_p + from_off, to_off - from_off);
                close(fd);
                DEBUG(0,("created %s\n", fname));
        }
@@ -58,25 +76,31 @@ void prs_dump(char *name, int v, prs_struct *ps)
  XXXX side-effect of this function is to increase the debug depth XXXX
 
  ********************************************************************/
-void prs_debug(prs_struct *ps, int depth, char *desc, char *fn_name)
+void prs_debug(prs_struct *ps, int depth, const char *desc, const char *fn_name)
 {
        DEBUG(5+depth, ("%s%06x %s %s\n", tab_depth(depth), ps->data_offset, fn_name, desc));
 }
 
-/*******************************************************************
- Initialise a parse structure - malloc the data if requested.
- ********************************************************************/
 
-BOOL prs_init(prs_struct *ps, uint32 size, uint8 align, BOOL io)
+/**
+ * Initialise an expandable parse structure.
+ *
+ * @param size Initial buffer size.  If >0, a new buffer will be
+ * created with malloc().
+ *
+ * @return False if allocation fails, otherwise True.
+ **/
+BOOL prs_init(prs_struct *ps, uint32 size, TALLOC_CTX *ctx, BOOL io)
 {
        ZERO_STRUCTP(ps);
        ps->io = io;
-       ps->bigendian_data = False;
-       ps->align = align;
+       ps->bigendian_data = RPC_LITTLE_ENDIAN;
+       ps->align = RPC_PARSE_ALIGN;
        ps->is_dynamic = False;
        ps->data_offset = 0;
        ps->buffer_size = 0;
        ps->data_p = NULL;
+       ps->mem_ctx = ctx;
 
        if (size != 0) {
                ps->buffer_size = size;
@@ -84,6 +108,7 @@ BOOL prs_init(prs_struct *ps, uint32 size, uint8 align, BOOL io)
                        DEBUG(0,("prs_init: malloc fail for %u bytes.\n", (unsigned int)size));
                        return False;
                }
+               memset(ps->data_p, '\0', (size_t)size);
                ps->is_dynamic = True; /* We own this memory. */
        }
 
@@ -91,41 +116,48 @@ BOOL prs_init(prs_struct *ps, uint32 size, uint8 align, BOOL io)
 }
 
 /*******************************************************************
read from a socket into memory.
Delete the memory in a parse structure - if we own it.
  ********************************************************************/
-BOOL prs_read(prs_struct *ps, int fd, size_t len, int timeout)
+
+void prs_mem_free(prs_struct *ps)
 {
-       BOOL ok;
-       size_t prev_size = ps->buffer_size;
-       if (!prs_grow(ps, len))
-       {
-               return False;
-       }
+       if(ps->is_dynamic)
+               SAFE_FREE(ps->data_p);
+       ps->is_dynamic = False;
+       ps->buffer_size = 0;
+       ps->data_offset = 0;
+}
 
-       if (timeout > 0)
-       {
-               ok = (read_with_timeout(fd, &ps->data_p[prev_size],
-                                           len, len,timeout) == len);
-       }
-       else 
-       {
-               ok = (read_data(fd, &ps->data_p[prev_size], len) == len);
-       }
-       return ok;
+/*******************************************************************
+ Clear the memory in a parse structure.
+ ********************************************************************/
+
+void prs_mem_clear(prs_struct *ps)
+{
+       memset(ps->data_p, '\0', (size_t)ps->buffer_size);
 }
 
 /*******************************************************************
Delete the memory in a parse structure - if we own it.
Allocate memory when unmarshalling... Always zero clears.
  ********************************************************************/
 
-void prs_mem_free(prs_struct *ps)
+char *prs_alloc_mem(prs_struct *ps, size_t size)
 {
-       if(ps->is_dynamic && (ps->data_p != NULL))
-               free(ps->data_p);
-       ps->is_dynamic = False;
-       ps->data_p = NULL;
-       ps->buffer_size = 0;
-       ps->data_offset = 0;
+       char *ret = talloc(ps->mem_ctx, size);
+
+       if (ret)
+               memset(ret, '\0', size);
+
+       return ret;
+}
+
+/*******************************************************************
+ Return the current talloc context we're using.
+ ********************************************************************/
+
+TALLOC_CTX *prs_get_mem_context(prs_struct *ps)
+{
+       return ps->mem_ctx;
 }
 
 /*******************************************************************
@@ -221,7 +253,7 @@ BOOL prs_grow(prs_struct *ps, uint32 extra_space)
                        DEBUG(0,("prs_grow: Malloc failure for size %u.\n", (unsigned int)new_size));
                        return False;
                }
-               memset(new_data, '\0', new_size );
+               memset(new_data, '\0', (size_t)new_size );
        } else {
                /*
                 * If the current buffer size is bigger than the space needed, just 
@@ -235,7 +267,7 @@ BOOL prs_grow(prs_struct *ps, uint32 extra_space)
                        return False;
                }
 
-               memset(&new_data[ps->buffer_size], '\0', new_size - ps->buffer_size);
+               memset(&new_data[ps->buffer_size], '\0', (size_t)(new_size - ps->buffer_size));
        }
        ps->buffer_size = new_size;
        ps->data_p = new_data;
@@ -266,7 +298,7 @@ BOOL prs_force_grow(prs_struct *ps, uint32 extra_space)
                return False;
        }
 
-       memset(&new_data[ps->buffer_size], '\0', new_size - ps->buffer_size);
+       memset(&new_data[ps->buffer_size], '\0', (size_t)(new_size - ps->buffer_size));
 
        ps->buffer_size = new_size;
        ps->data_p = new_data;
@@ -368,12 +400,12 @@ BOOL prs_append_data(prs_struct *dst, char *src, uint32 len)
 }
 
 /*******************************************************************
- Set the data as big-endian (external interface).
+ Set the data as X-endian (external interface).
  ********************************************************************/
 
-void prs_set_bigendian_data(prs_struct *ps)
+void prs_set_endian_data(prs_struct *ps, BOOL endian)
 {
-       ps->bigendian_data = True;
+       ps->bigendian_data = endian;
 }
 
 /*******************************************************************
@@ -396,6 +428,50 @@ BOOL prs_align(prs_struct *ps)
        return True;
 }
 
+/******************************************************************
+ Align on a 2 byte boundary
+ *****************************************************************/
+BOOL prs_align_uint16(prs_struct *ps)
+{
+       BOOL ret;
+       uint8 old_align = ps->align;
+
+       ps->align = 2;
+       ret = prs_align(ps);
+       ps->align = old_align;
+       
+       return ret;
+}
+
+/******************************************************************
+ Align on a 8 byte boundary
+ *****************************************************************/
+BOOL prs_align_uint64(prs_struct *ps)
+{
+       BOOL ret;
+       uint8 old_align = ps->align;
+
+       ps->align = 8;
+       ret = prs_align(ps);
+       ps->align = old_align;
+       
+       return ret;
+}
+
+/*******************************************************************
+ Align only if required (for the unistr2 string mainly)
+ ********************************************************************/
+
+BOOL prs_align_needed(prs_struct *ps, uint32 needed)
+{
+       if (needed==0)
+               return True;
+       else
+               return prs_align(ps);
+}
+
 /*******************************************************************
  Ensure we can read/write to a given offset.
  ********************************************************************/
@@ -416,7 +492,7 @@ char *prs_mem_get(prs_struct *ps, uint32 extra_size)
                 * Writing - grow the buffer if needed.
                 */
                if(!prs_grow(ps, extra_size))
-                       return False;
+                       return NULL;
        }
        return &ps->data_p[ps->data_offset];
 }
@@ -444,14 +520,20 @@ void prs_force_dynamic(prs_struct *ps)
  Stream a uint8.
  ********************************************************************/
 
-BOOL prs_uint8(char *name, prs_struct *ps, int depth, uint8 *data8)
+BOOL prs_uint8(const char *name, prs_struct *ps, int depth, uint8 *data8)
 {
-       char *q = prs_mem_get(ps, sizeof(uint8));
+       char *q = prs_mem_get(ps, 1);
        if (q == NULL)
                return False;
 
-       DBG_RW_CVAL(name, depth, ps->data_offset, ps->io, q, *data8)
-       ps->data_offset += sizeof(uint8);
+    if (UNMARSHALLING(ps))
+               *data8 = CVAL(q,0);
+       else
+               SCVAL(q,0,*data8);
+
+    DEBUG(5,("%s%04x %s: %02x\n", tab_depth(depth), ps->data_offset, name, *data8));
+
+       ps->data_offset += 1;
 
        return True;
 }
@@ -460,13 +542,26 @@ BOOL prs_uint8(char *name, prs_struct *ps, int depth, uint8 *data8)
  Stream a uint16.
  ********************************************************************/
 
-BOOL prs_uint16(char *name, prs_struct *ps, int depth, uint16 *data16)
+BOOL prs_uint16(const char *name, prs_struct *ps, int depth, uint16 *data16)
 {
        char *q = prs_mem_get(ps, sizeof(uint16));
        if (q == NULL)
                return False;
 
-       DBG_RW_SVAL(name, depth, ps->data_offset, ps->io, ps->bigendian_data, q, *data16)
+    if (UNMARSHALLING(ps)) {
+               if (ps->bigendian_data)
+                       *data16 = RSVAL(q,0);
+               else
+                       *data16 = SVAL(q,0);
+    } else {
+               if (ps->bigendian_data)
+                       RSSVAL(q,0,*data16);
+               else
+                       SSVAL(q,0,*data16);
+       }
+
+       DEBUG(5,("%s%04x %s: %04x\n", tab_depth(depth), ps->data_offset, name, *data16));
+
        ps->data_offset += sizeof(uint16);
 
        return True;
@@ -476,30 +571,121 @@ BOOL prs_uint16(char *name, prs_struct *ps, int depth, uint16 *data16)
  Stream a uint32.
  ********************************************************************/
 
-BOOL prs_uint32(char *name, prs_struct *ps, int depth, uint32 *data32)
+BOOL prs_uint32(const char *name, prs_struct *ps, int depth, uint32 *data32)
 {
        char *q = prs_mem_get(ps, sizeof(uint32));
        if (q == NULL)
                return False;
 
-       DBG_RW_IVAL(name, depth, ps->data_offset, ps->io, ps->bigendian_data, q, *data32)
+       if (UNMARSHALLING(ps)) {
+               if (ps->bigendian_data)
+                       *data32 = RIVAL(q,0);
+               else
+                       *data32 = IVAL(q,0);
+       } else {
+               if (ps->bigendian_data)
+                       RSIVAL(q,0,*data32);
+               else
+                       SIVAL(q,0,*data32);
+       }
+
+       DEBUG(5,("%s%04x %s: %08x\n", tab_depth(depth), ps->data_offset, name, *data32));
+
        ps->data_offset += sizeof(uint32);
 
        return True;
 }
 
+/*******************************************************************
+ Stream a NTSTATUS
+ ********************************************************************/
+
+BOOL prs_ntstatus(const char *name, prs_struct *ps, int depth, NTSTATUS *status)
+{
+       char *q = prs_mem_get(ps, sizeof(uint32));
+       if (q == NULL)
+               return False;
+
+       if (UNMARSHALLING(ps)) {
+               if (ps->bigendian_data)
+                       *status = NT_STATUS(RIVAL(q,0));
+               else
+                       *status = NT_STATUS(IVAL(q,0));
+       } else {
+               if (ps->bigendian_data)
+                       RSIVAL(q,0,NT_STATUS_V(*status));
+               else
+                       SIVAL(q,0,NT_STATUS_V(*status));
+       }
+
+       DEBUG(5,("%s%04x %s: %s\n", tab_depth(depth), ps->data_offset, name, 
+                nt_errstr(*status)));
+
+       ps->data_offset += sizeof(uint32);
+
+       return True;
+}
+
+/*******************************************************************
+ Stream a WERROR
+ ********************************************************************/
+
+BOOL prs_werror(const char *name, prs_struct *ps, int depth, WERROR *status)
+{
+       char *q = prs_mem_get(ps, sizeof(uint32));
+       if (q == NULL)
+               return False;
+
+       if (UNMARSHALLING(ps)) {
+               if (ps->bigendian_data)
+                       *status = W_ERROR(RIVAL(q,0));
+               else
+                       *status = W_ERROR(IVAL(q,0));
+       } else {
+               if (ps->bigendian_data)
+                       RSIVAL(q,0,W_ERROR_V(*status));
+               else
+                       SIVAL(q,0,W_ERROR_V(*status));
+       }
+
+       DEBUG(5,("%s%04x %s: %s\n", tab_depth(depth), ps->data_offset, name, 
+                dos_errstr(*status)));
+
+       ps->data_offset += sizeof(uint32);
+
+       return True;
+}
+
+
 /******************************************************************
  Stream an array of uint8s. Length is number of uint8s.
  ********************************************************************/
 
-BOOL prs_uint8s(BOOL charmode, char *name, prs_struct *ps, int depth, uint8 *data8s, int len)
+BOOL prs_uint8s(BOOL charmode, const char *name, prs_struct *ps, int depth, uint8 *data8s, int len)
 {
-       char *q = prs_mem_get(ps, len * sizeof(uint8));
+       int i;
+       char *q = prs_mem_get(ps, len);
        if (q == NULL)
                return False;
 
-       DBG_RW_PCVAL(charmode, name, depth, ps->data_offset, ps->io, q, data8s, len)
-       ps->data_offset += (len * sizeof(uint8));
+       if (UNMARSHALLING(ps)) {
+               for (i = 0; i < len; i++)
+                       data8s[i] = CVAL(q,i);
+       } else {
+               for (i = 0; i < len; i++)
+                       SCVAL(q, i, data8s[i]);
+       }
+
+    DEBUG(5,("%s%04x %s: ", tab_depth(depth), ps->data_offset ,name));
+    if (charmode)
+               print_asc(5, (unsigned char*)data8s, len);
+       else {
+       for (i = 0; i < len; i++)
+                       DEBUG(5,("%02x ", data8s[i]));
+       }
+    DEBUG(5,("\n"));
+
+       ps->data_offset += len;
 
        return True;
 }
@@ -508,13 +694,94 @@ BOOL prs_uint8s(BOOL charmode, char *name, prs_struct *ps, int depth, uint8 *dat
  Stream an array of uint16s. Length is number of uint16s.
  ********************************************************************/
 
-BOOL prs_uint16s(BOOL charmode, char *name, prs_struct *ps, int depth, uint16 *data16s, int len)
+BOOL prs_uint16s(BOOL charmode, const char *name, prs_struct *ps, int depth, uint16 *data16s, int len)
 {
+       int i;
        char *q = prs_mem_get(ps, len * sizeof(uint16));
        if (q == NULL)
                return False;
 
-       DBG_RW_PSVAL(charmode, name, depth, ps->data_offset, ps->io, ps->bigendian_data, q, data16s, len)
+       if (UNMARSHALLING(ps)) {
+               if (ps->bigendian_data) {
+                       for (i = 0; i < len; i++)
+                               data16s[i] = RSVAL(q, 2*i);
+               } else {
+                       for (i = 0; i < len; i++)
+                               data16s[i] = SVAL(q, 2*i);
+               }
+       } else {
+               if (ps->bigendian_data) {
+                       for (i = 0; i < len; i++)
+                               RSSVAL(q, 2*i, data16s[i]);
+               } else {
+                       for (i = 0; i < len; i++)
+                               SSVAL(q, 2*i, data16s[i]);
+               }
+       }
+
+       DEBUG(5,("%s%04x %s: ", tab_depth(depth), ps->data_offset, name));
+       if (charmode)
+               print_asc(5, (unsigned char*)data16s, 2*len);
+       else {
+               for (i = 0; i < len; i++)
+                       DEBUG(5,("%04x ", data16s[i]));
+       }
+    DEBUG(5,("\n"));
+
+       ps->data_offset += (len * sizeof(uint16));
+
+       return True;
+}
+
+/******************************************************************
+ Start using a function for streaming unicode chars. If unmarshalling,
+ output must be little-endian, if marshalling, input must be little-endian.
+ ********************************************************************/
+
+static void dbg_rw_punival(BOOL charmode, const char *name, int depth, prs_struct *ps,
+                                                       char *in_buf, char *out_buf, int len)
+{
+       int i;
+
+       if (UNMARSHALLING(ps)) {
+               if (ps->bigendian_data) {
+                       for (i = 0; i < len; i++)
+                               SSVAL(out_buf,2*i,RSVAL(in_buf, 2*i));
+               } else {
+                       for (i = 0; i < len; i++)
+                               SSVAL(out_buf, 2*i, SVAL(in_buf, 2*i));
+               }
+       } else {
+               if (ps->bigendian_data) {
+                       for (i = 0; i < len; i++)
+                               RSSVAL(in_buf, 2*i, SVAL(out_buf,2*i));
+               } else {
+                       for (i = 0; i < len; i++)
+                               SSVAL(in_buf, 2*i, SVAL(out_buf,2*i));
+               }
+       }
+
+       DEBUG(5,("%s%04x %s: ", tab_depth(depth), ps->data_offset, name));
+       if (charmode)
+               print_asc(5, (unsigned char*)out_buf, 2*len);
+       else {
+               for (i = 0; i < len; i++)
+                       DEBUG(5,("%04x ", out_buf[i]));
+       }
+    DEBUG(5,("\n"));
+}
+
+/******************************************************************
+ Stream a unistr. Always little endian.
+ ********************************************************************/
+
+BOOL prs_uint16uni(BOOL charmode, const char *name, prs_struct *ps, int depth, uint16 *data16s, int len)
+{
+       char *q = prs_mem_get(ps, len * sizeof(uint16));
+       if (q == NULL)
+               return False;
+
+       dbg_rw_punival(charmode, name, depth, ps, q, (char *)data16s, len);
        ps->data_offset += (len * sizeof(uint16));
 
        return True;
@@ -524,35 +791,99 @@ BOOL prs_uint16s(BOOL charmode, char *name, prs_struct *ps, int depth, uint16 *d
  Stream an array of uint32s. Length is number of uint32s.
  ********************************************************************/
 
-BOOL prs_uint32s(BOOL charmode, char *name, prs_struct *ps, int depth, uint32 *data32s, int len)
+BOOL prs_uint32s(BOOL charmode, const char *name, prs_struct *ps, int depth, uint32 *data32s, int len)
 {
+       int i;
        char *q = prs_mem_get(ps, len * sizeof(uint32));
        if (q == NULL)
                return False;
 
-       DBG_RW_PIVAL(charmode, name, depth, ps->data_offset, ps->io, ps->bigendian_data, q, data32s, len)
+       if (UNMARSHALLING(ps)) {
+               if (ps->bigendian_data) {
+                       for (i = 0; i < len; i++)
+                               data32s[i] = RIVAL(q, 4*i);
+               } else {
+                       for (i = 0; i < len; i++)
+                               data32s[i] = IVAL(q, 4*i);
+               }
+       } else {
+               if (ps->bigendian_data) {
+                       for (i = 0; i < len; i++)
+                               RSIVAL(q, 4*i, data32s[i]);
+               } else {
+                       for (i = 0; i < len; i++)
+                               SIVAL(q, 4*i, data32s[i]);
+               }
+       }
+
+       DEBUG(5,("%s%04x %s: ", tab_depth(depth), ps->data_offset, name));
+       if (charmode)
+               print_asc(5, (unsigned char*)data32s, 4*len);
+       else {
+               for (i = 0; i < len; i++)
+                       DEBUG(5,("%08x ", data32s[i]));
+       }
+    DEBUG(5,("\n"));
+
        ps->data_offset += (len * sizeof(uint32));
 
        return True;
 }
 
+/******************************************************************
+ Stream an array of unicode string, length/buffer specified separately,
+ in uint16 chars. The unicode string is already in little-endian format.
+ ********************************************************************/
+
+BOOL prs_buffer5(BOOL charmode, const char *name, prs_struct *ps, int depth, BUFFER5 *str)
+{
+       char *p;
+       char *q = prs_mem_get(ps, str->buf_len * sizeof(uint16));
+       if (q == NULL)
+               return False;
+
+       if (UNMARSHALLING(ps)) {
+               str->buffer = (uint16 *)prs_alloc_mem(ps,str->buf_len * sizeof(uint16));
+               if (str->buffer == NULL)
+                       return False;
+       }
+
+       /* If the string is empty, we don't have anything to stream */
+       if (str->buf_len==0)
+               return True;
+
+       p = (char *)str->buffer;
+
+       dbg_rw_punival(charmode, name, depth, ps, q, p, str->buf_len);
+       
+       ps->data_offset += (str->buf_len * sizeof(uint16));
+
+       return True;
+}
+
 /******************************************************************
  Stream a "not" unicode string, length/buffer specified separately,
  in byte chars. String is in little-endian format.
  ********************************************************************/
 
-BOOL prs_buffer2(BOOL charmode, char *name, prs_struct *ps, int depth, BUFFER2 *str)
+BOOL prs_buffer2(BOOL charmode, const char *name, prs_struct *ps, int depth, BUFFER2 *str)
 {
-       char *p = (char *)str->buffer;
+       char *p;
        char *q = prs_mem_get(ps, str->buf_len);
        if (q == NULL)
                return False;
 
-       /* If we're using big-endian, reverse to get little-endian. */
-       if(ps->bigendian_data)
-               DBG_RW_PSVAL(charmode, name, depth, ps->data_offset, ps->io, ps->bigendian_data, q, p, str->buf_len/2)
-       else
-               DBG_RW_PCVAL(charmode, name, depth, ps->data_offset, ps->io, q, p, str->buf_len)
+       if (UNMARSHALLING(ps)) {
+               if ( str->buf_len ) {
+                       str->buffer = (uint16 *)prs_alloc_mem(ps,str->buf_len);
+                       if ( str->buffer == NULL )
+                               return False;
+               }
+       }
+
+       p = (char *)str->buffer;
+
+       dbg_rw_punival(charmode, name, depth, ps, q, p, str->buf_len/2);
        ps->data_offset += str->buf_len;
 
        return True;
@@ -563,36 +894,67 @@ BOOL prs_buffer2(BOOL charmode, char *name, prs_struct *ps, int depth, BUFFER2 *
  in uint8 chars.
  ********************************************************************/
 
-BOOL prs_string2(BOOL charmode, char *name, prs_struct *ps, int depth, STRING2 *str)
+BOOL prs_string2(BOOL charmode, const char *name, prs_struct *ps, int depth, STRING2 *str)
 {
-       char *q = prs_mem_get(ps, str->str_str_len * sizeof(uint8));
+       int i;
+       char *q = prs_mem_get(ps, str->str_max_len);
        if (q == NULL)
                return False;
 
-       DBG_RW_PCVAL(charmode, name, depth, ps->data_offset, ps->io, q, str->buffer, str->str_max_len)
-       ps->data_offset += (str->str_str_len * sizeof(uint8));
+       if (UNMARSHALLING(ps)) {
+               str->buffer = (unsigned char *)prs_alloc_mem(ps,str->str_max_len);
+               if (str->buffer == NULL)
+                       return False;
+       }
+
+       if (UNMARSHALLING(ps)) {
+               for (i = 0; i < str->str_str_len; i++)
+                       str->buffer[i] = CVAL(q,i);
+       } else {
+               for (i = 0; i < str->str_str_len; i++)
+                       SCVAL(q, i, str->buffer[i]);
+       }
+
+    DEBUG(5,("%s%04x %s: ", tab_depth(depth), ps->data_offset, name));
+    if (charmode)
+               print_asc(5, (unsigned char*)str->buffer, str->str_str_len);
+       else {
+       for (i = 0; i < str->str_str_len; i++)
+                       DEBUG(5,("%02x ", str->buffer[i]));
+       }
+    DEBUG(5,("\n"));
+
+       ps->data_offset += str->str_str_len;
 
        return True;
 }
 
 /******************************************************************
  Stream a unicode string, length/buffer specified separately,
- in uint16 chars. We use DBG_RW_PCVAL, not DBG_RW_PSVAL here
- as the unicode string is already in little-endian format.
+ in uint16 chars. The unicode string is already in little-endian format.
  ********************************************************************/
 
-BOOL prs_unistr2(BOOL charmode, char *name, prs_struct *ps, int depth, UNISTR2 *str)
+BOOL prs_unistr2(BOOL charmode, const char *name, prs_struct *ps, int depth, UNISTR2 *str)
 {
-       char *p = (char *)str->buffer;
+       char *p;
        char *q = prs_mem_get(ps, str->uni_str_len * sizeof(uint16));
        if (q == NULL)
                return False;
 
-       /* If we're using big-endian, reverse to get little-endian. */
-       if(ps->bigendian_data)
-               DBG_RW_PSVAL(charmode, name, depth, ps->data_offset, ps->io, ps->bigendian_data, q, p, str->uni_str_len)
-       else
-               DBG_RW_PCVAL(charmode, name, depth, ps->data_offset, ps->io, q, p, str->uni_str_len * 2)
+       /* If the string is empty, we don't have anything to stream */
+       if (str->uni_str_len==0)
+               return True;
+
+       if (UNMARSHALLING(ps)) {
+               str->buffer = (uint16 *)prs_alloc_mem(ps,str->uni_max_len * sizeof(uint16));
+               if (str->buffer == NULL)
+                       return False;
+       }
+
+       p = (char *)str->buffer;
+
+       dbg_rw_punival(charmode, name, depth, ps, q, p, str->uni_str_len);
+       
        ps->data_offset += (str->uni_str_len * sizeof(uint16));
 
        return True;
@@ -600,22 +962,25 @@ BOOL prs_unistr2(BOOL charmode, char *name, prs_struct *ps, int depth, UNISTR2 *
 
 /******************************************************************
  Stream a unicode string, length/buffer specified separately,
- in uint16 chars. We use DBG_RW_PCVAL, not DBG_RW_PSVAL here
- as the unicode string is already in little-endian format.
+ in uint16 chars. The unicode string is already in little-endian format.
  ********************************************************************/
 
-BOOL prs_unistr3(BOOL charmode, char *name, UNISTR3 *str, prs_struct *ps, int depth)
+BOOL prs_unistr3(BOOL charmode, const char *name, UNISTR3 *str, prs_struct *ps, int depth)
 {
-       char *p = (char *)str->str.buffer;
+       char *p;
        char *q = prs_mem_get(ps, str->uni_str_len * sizeof(uint16));
        if (q == NULL)
                return False;
 
-       /* If we're using big-endian, reverse to get little-endian. */
-       if(ps->bigendian_data)
-               DBG_RW_PSVAL(charmode, name, depth, ps->data_offset, ps->io, ps->bigendian_data, q, p, str->uni_str_len)
-       else
-               DBG_RW_PCVAL(charmode, name, depth, ps->data_offset, ps->io, q, p, str->uni_str_len * 2)
+       if (UNMARSHALLING(ps)) {
+               str->str.buffer = (uint16 *)prs_alloc_mem(ps,str->uni_str_len * sizeof(uint16));
+               if (str->str.buffer == NULL)
+                       return False;
+       }
+
+       p = (char *)str->str.buffer;
+
+       dbg_rw_punival(charmode, name, depth, ps, q, p, str->uni_str_len);
        ps->data_offset += (str->uni_str_len * sizeof(uint16));
 
        return True;
@@ -626,69 +991,135 @@ BOOL prs_unistr3(BOOL charmode, char *name, UNISTR3 *str, prs_struct *ps, int de
  in little-endian format then do it as a stream of bytes.
  ********************************************************************/
 
-BOOL prs_unistr(char *name, prs_struct *ps, int depth, UNISTR *str)
+BOOL prs_unistr(const char *name, prs_struct *ps, int depth, UNISTR *str)
 {
        int len = 0;
        unsigned char *p = (unsigned char *)str->buffer;
        uint8 *start;
        char *q;
-       char zero=0;
+       uint32 max_len;
+       uint16* ptr;
 
-       for(len = 0; len < (sizeof(str->buffer) / sizeof(str->buffer[0])) &&
-                          str->buffer[len] != 0; len++)
-               ;
+       if (MARSHALLING(ps)) {
 
-       q = prs_mem_get(ps, (len+1)*2);
-       if (q == NULL)
-               return False;
+               for(len = 0; str->buffer[len] != 0; len++)
+                       ;
 
-       start = (uint8*)q;
+               q = prs_mem_get(ps, (len+1)*2);
+               if (q == NULL)
+                       return False;
 
-       for(len = 0; len < (sizeof(str->buffer) / sizeof(str->buffer[0])) &&
-                          str->buffer[len] != 0; len++) {
-               if(ps->bigendian_data) {
-                       RW_SVAL(ps->io, ps->bigendian_data, q, *p, 0);
-                       p += 2;
-                       q += 2;
-               } else {
-                       RW_CVAL(ps->io, q, *p, 0);
-                       p++;
-                       q++;
-                       RW_CVAL(ps->io, q, *p, 0);
-                       p++;
-                       q++;
+               start = (uint8*)q;
+
+               for(len = 0; str->buffer[len] != 0; len++) 
+               {
+                       if(ps->bigendian_data) 
+                       {
+                               /* swap bytes - p is little endian, q is big endian. */
+                               q[0] = (char)p[1];
+                               q[1] = (char)p[0];
+                               p += 2;
+                               q += 2;
+                       } 
+                       else 
+                       {
+                               q[0] = (char)p[0];
+                               q[1] = (char)p[1];
+                               p += 2;
+                               q += 2;
+                       }
                }
+
+               /*
+                * even if the string is 'empty' (only an \0 char)
+                * at this point the leading \0 hasn't been parsed.
+                * so parse it now
+                */
+
+               q[0] = 0;
+               q[1] = 0;
+               q += 2;
+
+               len++;
+
+               DEBUG(5,("%s%04x %s: ", tab_depth(depth), ps->data_offset, name));
+               print_asc(5, (unsigned char*)start, 2*len);     
+               DEBUG(5, ("\n"));
        }
+       else { /* unmarshalling */
        
-       /*
-        * even if the string is 'empty' (only an \0 char)
-        * at this point the leading \0 hasn't been parsed.
-        * so parse it now
-        */
+               uint32 alloc_len = 0;
+               q = prs_data_p(ps) + prs_offset(ps);
+
+               /*
+                * Work out how much space we need and talloc it.
+                */
+               max_len = (ps->buffer_size - ps->data_offset)/sizeof(uint16);
 
-       RW_CVAL(ps->io, q, zero, 0);
-       q++;
-       RW_CVAL(ps->io, q, zero, 0);
-       q++;
+               /* the test of the value of *ptr helps to catch the circumstance
+                  where we have an emtpty (non-existent) string in the buffer */
+               for ( ptr = (uint16 *)q; *ptr && (alloc_len <= max_len); alloc_len++)
+                       /* do nothing */ 
+                       ;
 
-       len++;
-               
-       ps->data_offset += len*2;
+               /* should we allocate anything at all? */
+               str->buffer = (uint16 *)prs_alloc_mem(ps,alloc_len * sizeof(uint16));
+               if ((str->buffer == NULL) && (alloc_len > 0))
+                       return False;
+
+               p = (unsigned char *)str->buffer;
+
+               len = 0;
+               /* the (len < alloc_len) test is to prevent us from overwriting
+                  memory that is not ours...if we get that far, we have a non-null
+                  terminated string in the buffer and have messed up somewhere */
+               while ((len < alloc_len) && (*(uint16 *)q != 0))
+               {
+                       if(ps->bigendian_data) 
+                       {
+                               /* swap bytes - q is big endian, p is little endian. */
+                               p[0] = (unsigned char)q[1];
+                               p[1] = (unsigned char)q[0];
+                               p += 2;
+                               q += 2;
+                       } else {
+
+                               p[0] = (unsigned char)q[0];
+                               p[1] = (unsigned char)q[1];
+                               p += 2;
+                               q += 2;
+                       }
+
+                       len++;
+               } 
+               if (len < alloc_len)
+               {
+                       /* NULL terminate the UNISTR */
+                       str->buffer[len++] = '\0';
+               }
 
-       dump_data(5+depth, (char *)start, len * 2);
+               DEBUG(5,("%s%04x %s: ", tab_depth(depth), ps->data_offset, name));
+               print_asc(5, (unsigned char*)str->buffer, 2*len);       
+               DEBUG(5, ("\n"));
+       }
 
+       /* set the offset in the prs_struct; 'len' points to the
+          terminiating NULL in the UNISTR so we need to go one more
+          uint16 */
+       ps->data_offset += (len)*2;
+       
        return True;
 }
 
+
 /*******************************************************************
  Stream a null-terminated string.  len is strlen, and therefore does
  not include the null-termination character.
  ********************************************************************/
 
-BOOL prs_string(char *name, prs_struct *ps, int depth, char *str, int len, int max_buf_size)
+BOOL prs_string(const char *name, prs_struct *ps, int depth, char *str, int len, int max_buf_size)
 {
        char *q;
-       uint8 *start;
        int i;
 
        len = MIN(len, (max_buf_size-1));
@@ -697,23 +1128,23 @@ BOOL prs_string(char *name, prs_struct *ps, int depth, char *str, int len, int m
        if (q == NULL)
                return False;
 
-       start = (uint8*)q;
-
        for(i = 0; i < len; i++) {
-               RW_CVAL(ps->io, q, str[i],0);
-               q++;
+               if (UNMARSHALLING(ps))
+                       str[i] = q[i];
+               else
+                       q[i] = str[i];
        }
 
        /* The terminating null. */
        str[i] = '\0';
 
        if (MARSHALLING(ps)) {
-               RW_CVAL(ps->io, q, str[i], 0);
+               q[i] = '\0';
        }
 
        ps->data_offset += len+1;
 
-       dump_data(5+depth, (char *)start, len);
+       dump_data(5+depth, q, len);
 
        return True;
 }
@@ -723,9 +1154,9 @@ BOOL prs_string(char *name, prs_struct *ps, int depth, char *str, int len, int m
  uint16 should be stored, or gets the size if reading.
  ********************************************************************/
 
-BOOL prs_uint16_pre(char *name, prs_struct *ps, int depth, uint16 *data16, uint32 *offset)
+BOOL prs_uint16_pre(const char *name, prs_struct *ps, int depth, uint16 *data16, uint32 *offset)
 {
-       (*offset) = ps->data_offset;
+       *offset = ps->data_offset;
        if (UNMARSHALLING(ps)) {
                /* reading. */
                return prs_uint16(name, ps, depth, data16);
@@ -743,7 +1174,7 @@ BOOL prs_uint16_pre(char *name, prs_struct *ps, int depth, uint16 *data16, uint3
  does nothing on reading, as that is already handled by ...._pre()
  ********************************************************************/
 
-BOOL prs_uint16_post(char *name, prs_struct *ps, int depth, uint16 *data16,
+BOOL prs_uint16_post(const char *name, prs_struct *ps, int depth, uint16 *data16,
                                uint32 ptr_uint16, uint32 start_offset)
 {
        if (MARSHALLING(ps)) {
@@ -770,10 +1201,10 @@ BOOL prs_uint16_post(char *name, prs_struct *ps, int depth, uint16 *data16,
  uint32 should be stored, or gets the size if reading.
  ********************************************************************/
 
-BOOL prs_uint32_pre(char *name, prs_struct *ps, int depth, uint32 *data32, uint32 *offset)
+BOOL prs_uint32_pre(const char *name, prs_struct *ps, int depth, uint32 *data32, uint32 *offset)
 {
-       (*offset) = ps->data_offset;
-       if (UNMARSHALLING(ps)) {
+       *offset = ps->data_offset;
+       if (UNMARSHALLING(ps) && (data32 != NULL)) {
                /* reading. */
                return prs_uint32(name, ps, depth, data32);
        } else {
@@ -787,7 +1218,7 @@ BOOL prs_uint32_pre(char *name, prs_struct *ps, int depth, uint32 *data32, uint3
  does nothing on reading, as that is already handled by ...._pre()
  ********************************************************************/
 
-BOOL prs_uint32_post(char *name, prs_struct *ps, int depth, uint32 *data32,
+BOOL prs_uint32_post(const char *name, prs_struct *ps, int depth, uint32 *data32,
                                uint32 ptr_uint32, uint32 data_size)
 {
        if (MARSHALLING(ps)) {
@@ -817,18 +1248,43 @@ int tdb_prs_store(TDB_CONTEXT *tdb, char *keystr, prs_struct *ps)
 }
 
 /* useful function to fetch a structure into rpc wire format */
-int tdb_prs_fetch(TDB_CONTEXT *tdb, char *keystr, prs_struct *ps)
+int tdb_prs_fetch(TDB_CONTEXT *tdb, char *keystr, prs_struct *ps, TALLOC_CTX *mem_ctx)
 {
     TDB_DATA kbuf, dbuf;
     kbuf.dptr = keystr;
     kbuf.dsize = strlen(keystr)+1;
 
     dbuf = tdb_fetch(tdb, kbuf);
-    if (!dbuf.dptr) return -1;
+    if (!dbuf.dptr)
+           return -1;
 
     ZERO_STRUCTP(ps);
-    prs_init(ps, 0, 4, UNMARSHALL);
+    prs_init(ps, 0, mem_ctx, UNMARSHALL);
     prs_give_memory(ps, dbuf.dptr, dbuf.dsize, True);
 
     return 0;
 } 
+
+/*******************************************************************
+ hash a stream.
+ ********************************************************************/
+BOOL prs_hash1(prs_struct *ps, uint32 offset, uint8 sess_key[16])
+{
+       char *q;
+
+       q = prs_data_p(ps);
+        q = &q[offset];
+
+#ifdef DEBUG_PASSWORD
+       DEBUG(100, ("prs_hash1\n"));
+       dump_data(100, sess_key, 16);
+       dump_data(100, q, 68);
+#endif
+       SamOEMhash((uchar *) q, sess_key, 68);
+
+#ifdef DEBUG_PASSWORD
+       dump_data(100, q, 68);
+#endif
+
+       return True;
+}