[GLUE] Rsync SAMBA_3_2_0 SVN r25598 in order to create the v3-2-test branch.
[samba.git] / source / rpc_parse / parse_misc.c
index 7a07ef5e7b2ccda60c4ab1a2e1ee5f89ab217788..7321e362b89f30642cec648c46c75ec77bee8ee7 100644 (file)
@@ -8,7 +8,7 @@
  *  
  *  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,
@@ -17,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;
-
-static 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.
 ********************************************************************/
@@ -124,7 +61,7 @@ BOOL smb_io_time(const char *desc, NTTIME *nttime, prs_struct *ps, int depth)
 
        if(!prs_align(ps))
                return False;
-
+       
        if (MARSHALLING(ps)) {
                low = *nttime & 0xFFFFFFFF;
                high = *nttime >> 32;
@@ -151,6 +88,53 @@ BOOL smb_io_nttime(const char *desc, prs_struct *ps, int depth, NTTIME *nttime)
        return smb_io_time( desc, nttime, ps, depth );
 }
 
+/*******************************************************************
+ Gets an enumeration handle from an ENUM_HND structure.
+********************************************************************/
+
+uint32 get_enum_hnd(ENUM_HND *enh)
+{
+       return (enh && enh->ptr_hnd != 0) ? enh->handle : 0;
+}
+
+/*******************************************************************
+ Inits an ENUM_HND structure.
+********************************************************************/
+
+void init_enum_hnd(ENUM_HND *enh, uint32 hnd)
+{
+       DEBUG(5,("smb_io_enum_hnd\n"));
+
+       enh->ptr_hnd = (hnd != 0) ? 1 : 0;
+       enh->handle = hnd;
+}
+
+/*******************************************************************
+ Reads or writes an ENUM_HND structure.
+********************************************************************/
+
+BOOL smb_io_enum_hnd(const char *desc, ENUM_HND *hnd, prs_struct *ps, int depth)
+{
+       if (hnd == NULL)
+               return False;
+
+       prs_debug(ps, depth, desc, "smb_io_enum_hnd");
+       depth++;
+
+       if(!prs_align(ps))
+               return False;
+       
+       if(!prs_uint32("ptr_hnd", ps, depth, &hnd->ptr_hnd)) /* pointer */
+               return False;
+
+       if (hnd->ptr_hnd != 0) {
+               if(!prs_uint32("handle ", ps, depth, &hnd->handle )) /* enum handle */
+                       return False;
+       }
+
+       return True;
+}
+
 /*******************************************************************
  Reads or writes a DOM_SID structure.
 ********************************************************************/
@@ -450,9 +434,9 @@ void init_unistr(UNISTR *str, const char *buf)
        len = strlen(buf) + 1;
 
        if (len) {
-               str->buffer = TALLOC_ZERO_ARRAY(get_talloc_ctx(), uint16, len);
+               str->buffer = TALLOC_ZERO_ARRAY(talloc_tos(), uint16, len);
                if (str->buffer == NULL)
-                       smb_panic("init_unistr: malloc fail\n");
+                       smb_panic("init_unistr: malloc fail");
 
                rpcstr_push(str->buffer, buf, len*sizeof(uint16), STR_TERMINATE);
        } else {
@@ -483,12 +467,17 @@ BOOL smb_io_unistr(const char *desc, UNISTR *uni, prs_struct *ps, int depth)
  Allocate the RPC_DATA_BLOB memory.
 ********************************************************************/
 
-size_t create_rpc_blob(RPC_DATA_BLOB *str, size_t len)
+static void create_rpc_blob(RPC_DATA_BLOB *str, size_t len)
 {
-       str->buffer = (uint8 *)TALLOC_ZERO(get_talloc_ctx(), len);
-       if (str->buffer == NULL)
-               smb_panic("create_rpc_blob: talloc fail\n");
-       return len;
+       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;
+       }
 }
 
 /*******************************************************************
@@ -500,7 +489,7 @@ void init_rpc_blob_uint32(RPC_DATA_BLOB *str, uint32 val)
        ZERO_STRUCTP(str);
 
        /* set up string lengths. */
-       str->buf_len = create_rpc_blob(str, sizeof(uint32));
+       create_rpc_blob(str, sizeof(uint32));
        SIVAL(str->buffer, 0, val);
 }
 
@@ -513,9 +502,10 @@ void init_rpc_blob_str(RPC_DATA_BLOB *str, const char *buf, int len)
        ZERO_STRUCTP(str);
 
        /* set up string lengths. */
-       str->buf_len = create_rpc_blob(str, len*2);
-       rpcstr_push(str->buffer, buf, (size_t)str->buf_len, STR_TERMINATE);
-       
+       if (len) {
+               create_rpc_blob(str, len*2);
+               rpcstr_push(str->buffer, buf, (size_t)str->buf_len, STR_TERMINATE);
+       }
 }
 
 /*******************************************************************
@@ -525,8 +515,10 @@ void init_rpc_blob_str(RPC_DATA_BLOB *str, const char *buf, int len)
 void init_rpc_blob_hex(RPC_DATA_BLOB *str, const char *buf)
 {
        ZERO_STRUCTP(str);
-       str->buf_len = create_rpc_blob(str, strlen(buf));
-       str->buf_len = strhex_to_str((char *)str->buffer, str->buf_len, buf);
+       if (buf && *buf) {
+               create_rpc_blob(str, strlen(buf));
+               str->buf_len = strhex_to_str((char *)str->buffer, str->buf_len, buf);
+       }
 }
 
 /*******************************************************************
@@ -538,8 +530,8 @@ void init_rpc_blob_bytes(RPC_DATA_BLOB *str, uint8 *buf, size_t len)
        ZERO_STRUCTP(str);
 
        /* max buffer size (allocated size) */
-       if (buf != NULL) {
-               len = create_rpc_blob(str, len);
+       if (buf != NULL && len) {
+               create_rpc_blob(str, len);
                memcpy(str->buffer, buf, len);
        }
        str->buf_len = len;
@@ -584,10 +576,10 @@ void init_regval_buffer(REGVAL_BUFFER *str, const uint8 *buf, size_t len)
 
        if (buf != NULL) {
                SMB_ASSERT(str->buf_max_len >= str->buf_len);
-               str->buffer = (uint16 *)TALLOC_ZERO(get_talloc_ctx(),
+               str->buffer = (uint16 *)TALLOC_ZERO(talloc_tos(),
                                                    str->buf_max_len);
                if (str->buffer == NULL)
-                       smb_panic("init_regval_buffer: talloc fail\n");
+                       smb_panic("init_regval_buffer: talloc fail");
                memcpy(str->buffer, buf, str->buf_len);
        }
 }
@@ -661,9 +653,9 @@ void copy_unistr2(UNISTR2 *str, const UNISTR2 *from)
           reallocation of memory. */
        if (str->buffer == NULL) {
                if (str->uni_max_len) {
-                       str->buffer = (uint16 *)TALLOC_ZERO_ARRAY(get_talloc_ctx(), uint16, 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\n");
+                               smb_panic("copy_unistr2: talloc fail");
                                return;
                        }
                        /* copy the string */
@@ -694,10 +686,10 @@ void init_string2(STRING2 *str, const char *buf, size_t max_len, size_t str_len)
 
        /* store the string */
        if(str_len != 0) {
-               str->buffer = (uint8 *)TALLOC_ZERO(get_talloc_ctx(),
+               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);
        }
 }
@@ -771,9 +763,9 @@ void init_unistr2(UNISTR2 *str, const char *buf, enum unistr2_term_codes flags)
        }
        
 
-       str->buffer = TALLOC_ZERO_ARRAY(get_talloc_ctx(), uint16, len);
+       str->buffer = TALLOC_ZERO_ARRAY(talloc_tos(), uint16, len);
        if (str->buffer == NULL) {
-               smb_panic("init_unistr2: malloc fail\n");
+               smb_panic("init_unistr2: malloc fail");
                return;
        }
 
@@ -807,9 +799,9 @@ void init_unistr2(UNISTR2 *str, const char *buf, enum unistr2_term_codes flags)
 
 void init_unistr4(UNISTR4 *uni4, const char *buf, enum unistr2_term_codes flags)
 {
-       uni4->string = TALLOC_P( get_talloc_ctx(), UNISTR2 );
+       uni4->string = TALLOC_P( talloc_tos(), UNISTR2 );
        if (!uni4->string) {
-               smb_panic("init_unistr4: talloc fail\n");
+               smb_panic("init_unistr4: talloc fail");
                return;
        }
        init_unistr2( uni4->string, buf, flags );
@@ -822,7 +814,7 @@ 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\n");
+               smb_panic("init_unistr4_w: talloc fail");
                return;
        }
        init_unistr2_w( ctx, uni4->string, buf );
@@ -852,7 +844,7 @@ void init_unistr2_w(TALLOC_CTX *ctx, UNISTR2 *str, const smb_ucs2_t *buf)
        if (len + 1) {
                str->buffer = TALLOC_ZERO_ARRAY(ctx, uint16, len + 1);
                if (str->buffer == NULL) {
-                       smb_panic("init_unistr2_w: talloc fail\n");
+                       smb_panic("init_unistr2_w: talloc fail");
                        return;
                }
        } else {
@@ -906,9 +898,9 @@ void init_unistr2_from_unistr(UNISTR2 *to, const UNISTR *from)
 
        /* allocate the space and copy the string buffer */
        if (i) {
-               to->buffer = TALLOC_ZERO_ARRAY(get_talloc_ctx(), uint16, i);
+               to->buffer = TALLOC_ZERO_ARRAY(talloc_tos(), uint16, i);
                if (to->buffer == NULL)
-                       smb_panic("init_unistr2_from_unistr: malloc fail\n");
+                       smb_panic("init_unistr2_from_unistr: malloc fail");
                memcpy(to->buffer, from->buffer, i*sizeof(uint16));
        } else {
                to->buffer = NULL;
@@ -937,7 +929,7 @@ void init_unistr2_from_datablob(UNISTR2 *str, DATA_BLOB *blob)
                str->buffer = NULL;
        }
        if ((str->buffer == NULL) && (blob->length > 0)) {
-               smb_panic("init_unistr2_from_datablob: malloc fail\n");
+               smb_panic("init_unistr2_from_datablob: malloc fail");
        }
 }
 
@@ -1110,7 +1102,7 @@ BOOL prs_unistr4_array(const char *desc, prs_struct *ps, int depth, UNISTR4_ARRA
 
        if (UNMARSHALLING(ps)) {
                if (array->count) {
-                       if ( !(array->strings = TALLOC_ZERO_ARRAY( get_talloc_ctx(), UNISTR4, array->count)) )
+                       if ( !(array->strings = TALLOC_ZERO_ARRAY( talloc_tos(), UNISTR4, array->count)) )
                                return False;
                } else {
                        array->strings = NULL;
@@ -1145,7 +1137,7 @@ BOOL init_unistr4_array( UNISTR4_ARRAY *array, uint32 count, const char **string
        /* allocate memory for the array of UNISTR4 objects */
 
        if (array->count) {
-               if ( !(array->strings = TALLOC_ZERO_ARRAY(get_talloc_ctx(), UNISTR4, count )) )
+               if ( !(array->strings = TALLOC_ZERO_ARRAY(talloc_tos(), UNISTR4, count )) )
                        return False;
        } else {
                array->strings = NULL;
@@ -1329,7 +1321,8 @@ 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 *logcln, 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__));
 
@@ -1699,9 +1692,9 @@ void init_unistr3(UNISTR3 *str, const char *buf)
        str->uni_str_len = strlen(buf) + 1;
 
        if (str->uni_str_len) {
-               str->str.buffer = TALLOC_ZERO_ARRAY(get_talloc_ctx(), uint16, 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\n");
+                       smb_panic("init_unistr3: malloc fail");
 
                rpcstr_push((char *)str->str.buffer, buf, str->uni_str_len * sizeof(uint16), STR_TERMINATE);
        } else {