updated the 3.0 branch from the head branch - ready for alpha18
[nivanova/samba-autobuild/.git] / source3 / rpc_client / cli_reg.c
index b5e9cbb2ac2e7627844a40c2a6c1b36af7285dd3..07001f13bd7ccdd4857caec9fb1194feb49d89f6 100644 (file)
@@ -1,7 +1,5 @@
-
 /* 
- *  Unix SMB/Netbios implementation.
- *  Version 1.9.
+ *  Unix SMB/CIFS implementation.
  *  RPC Pipe client / server routines
  *  Copyright (C) Andrew Tridgell              1992-1998,
  *  Copyright (C) Luke Kenneth Casson Leighton 1996-1998,
  */
 
 
-#ifdef SYSLOG
-#undef SYSLOG
-#endif
-
 #include "includes.h"
 
-extern int DEBUGLEVEL;
+#undef DBGC_CLASS
+#define DBGC_CLASS DBGC_RPC_CLI
 
 /****************************************************************************
 do a REG Open Policy
@@ -86,8 +81,8 @@ BOOL do_reg_open_hklm(struct cli_state *cli, uint16 unknown_0, uint32 level,
        if (hnd == NULL)
                return False;
 
-       prs_init(&buf , MAX_PDU_FRAG_LEN, 4, MARSHALL);
-       prs_init(&rbuf, 0, 4, UNMARSHALL);
+       prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
+       prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
 
        /* create and send a MSRPC command with api REG_OPEN_HKLM */
 
@@ -120,13 +115,13 @@ BOOL do_reg_open_hklm(struct cli_state *cli, uint16 unknown_0, uint32 level,
 
        if (r_o.status != 0) {
                /* report error code */
-               DEBUG(0,("REG_OPEN_HKLM: %s\n", get_nt_error_msg(r_o.status)));
+               DEBUG(0,("REG_OPEN_HKLM: %s\n", nt_errstr(r_o.status)));
                prs_mem_free(&rbuf);
                return False;
        }
 
        /* ok, at last: we're happy. return the policy handle */
-       memcpy(hnd, r_o.pol.data, sizeof(hnd->data));
+       *hnd = r_o.pol;
 
        prs_mem_free(&rbuf);
 
@@ -147,8 +142,8 @@ BOOL do_reg_open_hku(struct cli_state *cli, uint16 unknown_0, uint32 level,
        if (hnd == NULL)
                return False;
 
-       prs_init(&buf , MAX_PDU_FRAG_LEN, 4, MARSHALL);
-       prs_init(&rbuf, 0, 4, UNMARSHALL);
+       prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
+       prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
 
        /* create and send a MSRPC command with api REG_OPEN_HKU */
 
@@ -181,13 +176,13 @@ BOOL do_reg_open_hku(struct cli_state *cli, uint16 unknown_0, uint32 level,
 
        if (r_o.status != 0) {
                /* report error code */
-               DEBUG(0,("REG_OPEN_HKU: %s\n", get_nt_error_msg(r_o.status)));
+               DEBUG(0,("REG_OPEN_HKU: %s\n", nt_errstr(r_o.status)));
                prs_mem_free(&rbuf);
                return False;
        }
 
        /* ok, at last: we're happy. return the policy handle */
-       memcpy(hnd, r_o.pol.data, sizeof(hnd->data));
+       *hnd = r_o.pol;
 
        prs_mem_free(&rbuf);
 
@@ -209,8 +204,8 @@ BOOL do_reg_flush_key(struct cli_state *cli, POLICY_HND *hnd)
        if (hnd == NULL)
                return False;
 
-       prs_init(&buf , MAX_PDU_FRAG_LEN, 4, MARSHALL);
-       prs_init(&rbuf, 0, 4, UNMARSHALL);
+       prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
+       prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
 
        /* create and send a MSRPC command with api REG_FLUSH_KEY */
 
@@ -243,7 +238,7 @@ BOOL do_reg_flush_key(struct cli_state *cli, POLICY_HND *hnd)
 
        if (r_o.status != 0) {
                /* report error code */
-               DEBUG(0,("REG_FLUSH_KEY: %s\n", get_nt_error_msg(r_o.status)));
+               DEBUG(0,("REG_FLUSH_KEY: %s\n", nt_errstr(r_o.status)));
                prs_mem_free(&rbuf);
                return False;
        }
@@ -271,8 +266,8 @@ BOOL do_reg_query_key(struct cli_state *cli, POLICY_HND *hnd,
        if (hnd == NULL)
                return False;
 
-       prs_init(&buf , MAX_PDU_FRAG_LEN, 4, MARSHALL);
-       prs_init(&rbuf, 0, 4, UNMARSHALL);
+       prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
+       prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
 
        /* create and send a MSRPC command with api REG_QUERY_KEY */
 
@@ -305,13 +300,13 @@ BOOL do_reg_query_key(struct cli_state *cli, POLICY_HND *hnd,
 
        if (r_o.status != 0) {
                /* report error code */
-               DEBUG(0,("REG_QUERY_KEY: %s\n", get_nt_error_msg(r_o.status)));
+               DEBUG(0,("REG_QUERY_KEY: %s\n", nt_errstr(r_o.status)));
                prs_mem_free(&rbuf);
                return False;
        }
 
        *class_len      = r_o.hdr_class.uni_max_len;
-       fstrcpy(class, dos_unistr2_to_str(&r_o.uni_class));
+       rpcstr_pull(class, &r_o.uni_class, -1, -1, 0);
        *num_subkeys    = r_o.num_subkeys   ;
        *max_subkeylen  = r_o.max_subkeylen ;
        *max_subkeysize = r_o.max_subkeysize;
@@ -339,8 +334,8 @@ BOOL do_reg_unknown_1a(struct cli_state *cli, POLICY_HND *hnd, uint32 *unk)
        if (hnd == NULL)
                return False;
 
-       prs_init(&buf , MAX_PDU_FRAG_LEN, 4, MARSHALL);
-       prs_init(&rbuf, 0, 4, UNMARSHALL);
+       prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
+       prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
 
        /* create and send a MSRPC command with api REG_UNKNOWN_1A */
 
@@ -373,7 +368,7 @@ BOOL do_reg_unknown_1a(struct cli_state *cli, POLICY_HND *hnd, uint32 *unk)
 
        if (r_o.status != 0) {
                /* report error code */
-               DEBUG(0,("REG_UNK_1A: %s\n", get_nt_error_msg(r_o.status)));
+               DEBUG(0,("REG_UNK_1A: %s\n", nt_errstr(r_o.status)));
                prs_mem_free(&rbuf);
                return False;
        }
@@ -389,7 +384,7 @@ BOOL do_reg_unknown_1a(struct cli_state *cli, POLICY_HND *hnd, uint32 *unk)
 do a REG Query Info
 ****************************************************************************/
 BOOL do_reg_query_info(struct cli_state *cli, POLICY_HND *hnd,
-                               char *type, uint32 *unk_0, uint32 *unk_1)
+                               char *key_value, uint32* key_type)
 {
        prs_struct rbuf;
        prs_struct buf; 
@@ -399,14 +394,14 @@ BOOL do_reg_query_info(struct cli_state *cli, POLICY_HND *hnd,
        if (hnd == NULL)
                return False;
 
-       prs_init(&buf , MAX_PDU_FRAG_LEN, 4, MARSHALL);
-       prs_init(&rbuf, 0, 4, UNMARSHALL);
+       prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
+       prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
 
        /* create and send a MSRPC command with api REG_INFO */
 
        DEBUG(4,("REG Query Info\n"));
 
-       init_reg_q_info(&q_o, hnd, "ProductType", time(NULL), 4, 1);
+       init_reg_q_info(&q_o, hnd, "ProductType");
 
        /* turn parameters into data stream */
        if(!reg_io_q_info("", &q_o, &buf, 0)) {
@@ -433,14 +428,14 @@ BOOL do_reg_query_info(struct cli_state *cli, POLICY_HND *hnd,
 
        if ( r_o.status != 0) {
                /* report error code */
-               DEBUG(0,("REG_INFO: %s\n", get_nt_error_msg(r_o.status)));
+               DEBUG(0,("REG_INFO: %s\n", nt_errstr(r_o.status)));
                prs_mem_free(&rbuf);
                return False;
        }
 
-       fstrcpy(type, dos_buffer2_to_str(&r_o.uni_type));
-       (*unk_0) = r_o.unknown_0;
-       (*unk_1) = r_o.unknown_1;
+       /*fstrcpy(key_value, dos_buffer2_to_str(r_o.uni_val));*/
+       rpcstr_pull(key_value, r_o.uni_val->buffer, sizeof(fstring), r_o.uni_val->buf_len, 0);
+       *key_type = r_o.type;
 
        prs_mem_free(&rbuf);
 
@@ -460,8 +455,8 @@ BOOL do_reg_set_key_sec(struct cli_state *cli, POLICY_HND *hnd, SEC_DESC_BUF *se
        if (hnd == NULL)
                return False;
 
-       prs_init(&buf , MAX_PDU_FRAG_LEN, 4, MARSHALL);
-       prs_init(&rbuf, 0, 4, UNMARSHALL);
+       prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
+       prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
 
        /* create and send a MSRPC command with api REG_SET_KEY_SEC */
 
@@ -516,8 +511,8 @@ BOOL do_reg_get_key_sec(struct cli_state *cli, POLICY_HND *hnd, uint32 *sec_buf_
        if (hnd == NULL)
                return False;
 
-       prs_init(&buf , MAX_PDU_FRAG_LEN, 4, MARSHALL);
-       prs_init(&rbuf, 0, 4, UNMARSHALL);
+       prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
+       prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
 
        /* create and send a MSRPC command with api REG_GET_KEY_SEC */
 
@@ -556,7 +551,7 @@ BOOL do_reg_get_key_sec(struct cli_state *cli, POLICY_HND *hnd, uint32 *sec_buf_
                DEBUG(5,("sec_buf_size too small.  use %d\n", *sec_buf_size));
        } else if (r_o.status != 0) {
                /* report error code */
-               DEBUG(0,("REG_GET_KEY_SEC: %s\n", get_nt_error_msg(r_o.status)));
+               DEBUG(0,("REG_GET_KEY_SEC: %s\n", nt_errstr(r_o.status)));
                prs_mem_free(&rbuf);
                return False;
        } else {
@@ -582,8 +577,8 @@ BOOL do_reg_delete_val(struct cli_state *cli, POLICY_HND *hnd, char *val_name)
        if (hnd == NULL)
                return False;
 
-       prs_init(&buf , MAX_PDU_FRAG_LEN, 4, MARSHALL);
-       prs_init(&rbuf, 0, 4, UNMARSHALL);
+       prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
+       prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
 
        /* create and send a MSRPC command with api REG_DELETE_VALUE */
 
@@ -616,7 +611,7 @@ BOOL do_reg_delete_val(struct cli_state *cli, POLICY_HND *hnd, char *val_name)
 
        if (r_o.status != 0) {
                /* report error code */
-               DEBUG(0,("REG_DELETE_VALUE: %s\n", get_nt_error_msg(r_o.status)));
+               DEBUG(0,("REG_DELETE_VALUE: %s\n", nt_errstr(r_o.status)));
                prs_mem_free(&rbuf);
                return False;
        }
@@ -639,8 +634,8 @@ BOOL do_reg_delete_key(struct cli_state *cli, POLICY_HND *hnd, char *key_name)
        if (hnd == NULL)
                return False;
 
-       prs_init(&buf , MAX_PDU_FRAG_LEN, 4, MARSHALL);
-       prs_init(&rbuf, 0, 4, UNMARSHALL);
+       prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
+       prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
 
        /* create and send a MSRPC command with api REG_DELETE_KEY */
 
@@ -673,7 +668,7 @@ BOOL do_reg_delete_key(struct cli_state *cli, POLICY_HND *hnd, char *key_name)
 
        if (r_o.status != 0) {
                /* report error code */
-               DEBUG(0,("REG_DELETE_KEY: %s\n", get_nt_error_msg(r_o.status)));
+               DEBUG(0,("REG_DELETE_KEY: %s\n", nt_errstr(r_o.status)));
                prs_mem_free(&rbuf);
                return False;
        }
@@ -709,28 +704,25 @@ BOOL do_reg_create_key(struct cli_state *cli, POLICY_HND *hnd,
        DEBUG(4,("REG Create Key: %s %s 0x%08x\n", key_name, key_class,
                sam_access != NULL ? sam_access->mask : 0));
 
-       if((sec = make_sec_desc( 1, SEC_DESC_SELF_RELATIVE, NULL, NULL, NULL, NULL, &sec_len)) == NULL) {
+       if((sec = make_sec_desc( cli->mem_ctx, 1, NULL, NULL, NULL, NULL, &sec_len)) == NULL) {
                DEBUG(0,("make_sec_desc : malloc fail.\n"));
                return False;
        }
 
        DEBUG(10,("make_sec_desc: len = %d\n", (int)sec_len));
 
-       if((sec_buf = make_sec_desc_buf( (int)sec_len, sec)) == NULL) {
+       if((sec_buf = make_sec_desc_buf( cli->mem_ctx, (int)sec_len, sec)) == NULL) {
                DEBUG(0,("make_sec_desc : malloc fail (1)\n"));
-               free_sec_desc(&sec);
                return False;
        }
-       free_sec_desc(&sec);
 
-       prs_init(&buf, MAX_PDU_FRAG_LEN, 4, MARSHALL);
-       prs_init(&rbuf, 0, 4, UNMARSHALL);
+       prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
+       prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
 
        init_reg_q_create_key(&q_o, hnd, key_name, key_class, sam_access, sec_buf);
 
        /* turn parameters into data stream */
        if(!reg_io_q_create_key("", &q_o, &buf, 0)) {
-               free_sec_desc_buf(&sec_buf);
                prs_mem_free(&buf);
                prs_mem_free(&rbuf);
                return False;
@@ -738,13 +730,11 @@ BOOL do_reg_create_key(struct cli_state *cli, POLICY_HND *hnd,
 
        /* send the data on \PIPE\ */
        if (rpc_api_pipe_req(cli, REG_CREATE_KEY, &buf, &rbuf)) {
-               free_sec_desc_buf(&sec_buf);
                prs_mem_free(&buf);
                prs_mem_free(&rbuf);
                return False;
        }
 
-       free_sec_desc_buf(&sec_buf);
        prs_mem_free(&buf);
 
        ZERO_STRUCT(r_o);
@@ -756,12 +746,12 @@ BOOL do_reg_create_key(struct cli_state *cli, POLICY_HND *hnd,
 
        if (r_o.status != 0) {
                /* report error code */
-               DEBUG(0,("REG_CREATE_KEY: %s\n", get_nt_error_msg(r_o.status)));
+               DEBUG(0,("REG_CREATE_KEY: %s\n", nt_errstr(r_o.status)));
                prs_mem_free(&rbuf);
                return False;
        }
 
-       memcpy(key, r_o.key_pol.data, sizeof(key->data));
+       *key = r_o.key_pol;
 
        prs_mem_free(&rbuf);
 
@@ -784,8 +774,8 @@ BOOL do_reg_enum_key(struct cli_state *cli, POLICY_HND *hnd,
        if (hnd == NULL)
                return False;
 
-       prs_init(&buf, MAX_PDU_FRAG_LEN, 4, MARSHALL);
-       prs_init(&rbuf, 0, 4, UNMARSHALL);
+       prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
+       prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
 
        /* create and send a MSRPC command with api REG_ENUM_KEY */
 
@@ -818,14 +808,14 @@ BOOL do_reg_enum_key(struct cli_state *cli, POLICY_HND *hnd,
 
        if (r_o.status != 0) {
                /* report error code */
-               DEBUG(0,("REG_ENUM_KEY: %s\n", get_nt_error_msg(r_o.status)));
+               DEBUG(0,("REG_ENUM_KEY: %s\n", nt_errstr(r_o.status)));
                prs_mem_free(&rbuf);
                return False;
        }
 
        (*unk_1) = r_o.unknown_1;
        (*unk_2) = r_o.unknown_2;
-       fstrcpy(key_name, dos_unistr2(r_o.key_name.str.buffer));
+       rpcstr_pull(key_name, r_o.key_name.str.buffer, -1, -1, 0);
        (*mod_time) = nt_time_to_unix(&r_o.time);
 
        prs_mem_free(&rbuf);
@@ -847,8 +837,8 @@ BOOL do_reg_create_val(struct cli_state *cli, POLICY_HND *hnd,
        if (hnd == NULL)
                return False;
 
-       prs_init(&buf, MAX_PDU_FRAG_LEN, 4, MARSHALL);
-       prs_init(&rbuf, 0, 4, UNMARSHALL);
+       prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
+       prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
 
        /* create and send a MSRPC command with api REG_CREATE_VALUE */
 
@@ -881,7 +871,7 @@ BOOL do_reg_create_val(struct cli_state *cli, POLICY_HND *hnd,
 
        if (r_o.status != 0) {
                /* report error code */
-               DEBUG(0,("REG_CREATE_VALUE: %s\n", get_nt_error_msg(r_o.status)));
+               DEBUG(0,("REG_CREATE_VALUE: %s\n", nt_errstr(r_o.status)));
                prs_mem_free(&rbuf);
                return False;
        }
@@ -907,8 +897,8 @@ BOOL do_reg_enum_val(struct cli_state *cli, POLICY_HND *hnd,
        if (hnd == NULL)
                return False;
 
-       prs_init(&buf, MAX_PDU_FRAG_LEN, 4, MARSHALL);
-       prs_init(&rbuf, 0, 4, UNMARSHALL);
+       prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
+       prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
 
        /* create and send a MSRPC command with api REG_ENUM_VALUE */
 
@@ -942,13 +932,13 @@ BOOL do_reg_enum_val(struct cli_state *cli, POLICY_HND *hnd,
 
        if (r_o.status != 0) {
                /* report error code */
-               DEBUG(0,("REG_ENUM_VALUE: %s\n", get_nt_error_msg(r_o.status)));
+               DEBUG(0,("REG_ENUM_VALUE: %s\n", nt_errstr(r_o.status)));
                prs_mem_free(&rbuf);
                return False;
        }
 
        (*val_type) = r_o.type;
-       fstrcpy(val_name, dos_unistr2_to_str(&r_o.uni_name));
+       rpcstr_pull(val_name, &r_o.uni_name, -1, -1, 0);
 
        prs_mem_free(&rbuf);
 
@@ -970,8 +960,8 @@ BOOL do_reg_open_entry(struct cli_state *cli, POLICY_HND *hnd,
        if (hnd == NULL)
                return False;
 
-       prs_init(&buf, MAX_PDU_FRAG_LEN, 4, MARSHALL);
-       prs_init(&rbuf, 0, 4, UNMARSHALL);
+       prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
+       prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
 
        /* create and send a MSRPC command with api REG_OPEN_ENTRY */
 
@@ -1004,12 +994,12 @@ BOOL do_reg_open_entry(struct cli_state *cli, POLICY_HND *hnd,
 
        if (r_o.status != 0) {
                /* report error code */
-               DEBUG(0,("REG_OPEN_ENTRY: %s\n", get_nt_error_msg(r_o.status)));
+               DEBUG(0,("REG_OPEN_ENTRY: %s\n", nt_errstr(r_o.status)));
                prs_mem_free(&rbuf);
                return False;
        }
 
-       memcpy(key_hnd, r_o.pol.data, sizeof(key_hnd->data));
+       *key_hnd = r_o.pol;
 
        prs_mem_free(&rbuf);
 
@@ -1025,15 +1015,14 @@ BOOL do_reg_close(struct cli_state *cli, POLICY_HND *hnd)
        prs_struct buf; 
        REG_Q_CLOSE q_c;
        REG_R_CLOSE r_c;
-       int i;
 
        if (hnd == NULL)
                return False;
 
        /* create and send a MSRPC command with api REG_CLOSE */
 
-       prs_init(&buf, MAX_PDU_FRAG_LEN, 4, MARSHALL);
-       prs_init(&rbuf, 0, 4, UNMARSHALL);
+       prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
+       prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
 
        DEBUG(4,("REG Close\n"));
 
@@ -1065,19 +1054,18 @@ BOOL do_reg_close(struct cli_state *cli, POLICY_HND *hnd)
 
        if (r_c.status != 0) {
                /* report error code */
-               DEBUG(0,("REG_CLOSE: %s\n", get_nt_error_msg(r_c.status)));
+               DEBUG(0,("REG_CLOSE: %s\n", nt_errstr(r_c.status)));
                prs_mem_free(&rbuf);
                return False;
        }
 
        /* check that the returned policy handle is all zeros */
 
-       for (i = 0; i < sizeof(r_c.pol.data); i++) {
-               if (r_c.pol.data[i] != 0) {
+       if (IVAL(&r_c.pol.data1,0) || IVAL(&r_c.pol.data2,0) || SVAL(&r_c.pol.data3,0) ||
+               SVAL(&r_c.pol.data4,0) || IVAL(r_c.pol.data5,0) || IVAL(r_c.pol.data5,4) ) {
                        prs_mem_free(&rbuf);
                        DEBUG(0,("REG_CLOSE: non-zero handle returned\n"));
                        return False;
-               }
        }       
 
        prs_mem_free(&rbuf);