security descriptors
[samba.git] / source3 / rpc_client / cli_reg.c
index 3acd8281de6a6529ed8a17536da63fb0002932a8..61e38a8d96a845c24c7b6a6e27fa1270710fd703 100644 (file)
@@ -3,9 +3,9 @@
  *  Unix SMB/Netbios implementation.
  *  Version 1.9.
  *  RPC Pipe client / server routines
- *  Copyright (C) Andrew Tridgell              1992-1997,
- *  Copyright (C) Luke Kenneth Casson Leighton 1996-1997,
- *  Copyright (C) Paul Ashton                       1997.
+ *  Copyright (C) Andrew Tridgell              1992-1998,
+ *  Copyright (C) Luke Kenneth Casson Leighton 1996-1998,
+ *  Copyright (C) Paul Ashton                  1997-1998.
  *  
  *  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
 
 extern int DEBUGLEVEL;
 
+/****************************************************************************
+do a REG Open Policy
+****************************************************************************/
+BOOL do_reg_connect(struct cli_state *cli, char *full_keyname, char *key_name,
+                               POLICY_HND *reg_hnd)
+{
+       BOOL res = True;
+       uint32 reg_type = 0;
+
+       if (full_keyname == NULL)
+       {
+               return False;
+       }
+
+       ZERO_STRUCTP(reg_hnd);
+
+       /*
+        * open registry receive a policy handle
+        */
+
+       if (!reg_split_key(full_keyname, &reg_type, key_name))
+       {
+               DEBUG(0,("do_reg_connect: unrecognised key name %s\n", full_keyname));  
+               return False;
+       }
+
+       switch (reg_type)
+       {
+               case HKEY_LOCAL_MACHINE:
+               {
+                       res = res ? do_reg_open_hklm(cli,
+                                       0x84E0, 0x02000000,
+                                       reg_hnd) : False;
+                       break;
+               }
+       
+               case HKEY_USERS:
+               {
+                       res = res ? do_reg_open_hku(cli,
+                                       0x84E0, 0x02000000,
+                                       reg_hnd) : False;
+                       break;
+               }
+               default:
+               {
+                       DEBUG(0,("do_reg_connect: unrecognised hive key\n"));   
+                       return False;
+               }
+       }
+
+       return res;
+}
 
 /****************************************************************************
 do a REG Open Policy
 ****************************************************************************/
-BOOL do_reg_open_policy(struct cli_state *cli, uint16 unknown_0, uint32 level,
+BOOL do_reg_open_hklm(struct cli_state *cli, uint16 unknown_0, uint32 level,
                                POLICY_HND *hnd)
 {
        prs_struct rbuf;
        prs_struct buf; 
-       REG_Q_OPEN_POLICY q_o;
+       REG_Q_OPEN_HKLM q_o;
        BOOL valid_pol = False;
 
        if (hnd == NULL) return False;
@@ -48,30 +100,30 @@ BOOL do_reg_open_policy(struct cli_state *cli, uint16 unknown_0, uint32 level,
        prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
        prs_init(&rbuf, 0   , 4, SAFETY_MARGIN, True );
 
-       /* create and send a MSRPC command with api REG_OPEN_POLICY */
+       /* create and send a MSRPC command with api REG_OPEN_HKLM */
 
-       DEBUG(4,("REG Open Policy\n"));
+       DEBUG(4,("REG Open HKLM\n"));
 
-       make_reg_q_open_pol(&q_o, unknown_0, level);
+       make_reg_q_open_hklm(&q_o, unknown_0, level);
 
        /* turn parameters into data stream */
-       reg_io_q_open_policy("", &q_o, &buf, 0);
+       reg_io_q_open_hklm("", &q_o, &buf, 0);
 
        /* send the data on \PIPE\ */
-       if (rpc_api_pipe_req(cli, REG_OPEN_POLICY, &buf, &rbuf))
+       if (rpc_api_pipe_req(cli, REG_OPEN_HKLM, &buf, &rbuf))
        {
-               REG_R_OPEN_POLICY r_o;
+               REG_R_OPEN_HKLM r_o;
                BOOL p;
 
                ZERO_STRUCT(r_o);
 
-               reg_io_r_open_policy("", &r_o, &rbuf, 0);
+               reg_io_r_open_hklm("", &r_o, &rbuf, 0);
                p = rbuf.offset != 0;
 
                if (p && r_o.status != 0)
                {
                        /* report error code */
-                       DEBUG(0,("REG_OPEN_POLICY: %s\n", get_nt_error_msg(r_o.status)));
+                       DEBUG(0,("REG_OPEN_HKLM: %s\n", get_nt_error_msg(r_o.status)));
                        p = False;
                }
 
@@ -90,14 +142,14 @@ BOOL do_reg_open_policy(struct cli_state *cli, uint16 unknown_0, uint32 level,
 }
 
 /****************************************************************************
-do a REG Open Unknown 4
+do a REG Open HKU
 ****************************************************************************/
-BOOL do_reg_open_unk_4(struct cli_state *cli, uint16 unknown_0, uint32 level,
+BOOL do_reg_open_hku(struct cli_state *cli, uint16 unknown_0, uint32 level,
                                POLICY_HND *hnd)
 {
        prs_struct rbuf;
        prs_struct buf; 
-       REG_Q_OPEN_UNK_4 q_o;
+       REG_Q_OPEN_HKU q_o;
        BOOL valid_pol = False;
 
        if (hnd == NULL) return False;
@@ -105,30 +157,30 @@ BOOL do_reg_open_unk_4(struct cli_state *cli, uint16 unknown_0, uint32 level,
        prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
        prs_init(&rbuf, 0   , 4, SAFETY_MARGIN, True );
 
-       /* create and send a MSRPC command with api REG_OPEN_UNK_4 */
+       /* create and send a MSRPC command with api REG_OPEN_HKU */
 
-       DEBUG(4,("REG Open Unknown4\n"));
+       DEBUG(4,("REG Open HKU\n"));
 
-       make_reg_q_open_unk_4(&q_o, unknown_0, level);
+       make_reg_q_open_hku(&q_o, unknown_0, level);
 
        /* turn parameters into data stream */
-       reg_io_q_open_unk_4("", &q_o, &buf, 0);
+       reg_io_q_open_hku("", &q_o, &buf, 0);
 
        /* send the data on \PIPE\ */
-       if (rpc_api_pipe_req(cli, REG_OPEN_UNK_4, &buf, &rbuf))
+       if (rpc_api_pipe_req(cli, REG_OPEN_HKU, &buf, &rbuf))
        {
-               REG_R_OPEN_UNK_4 r_o;
+               REG_R_OPEN_HKU r_o;
                BOOL p;
 
                ZERO_STRUCT(r_o);
 
-               reg_io_r_open_unk_4("", &r_o, &rbuf, 0);
+               reg_io_r_open_hku("", &r_o, &rbuf, 0);
                p = rbuf.offset != 0;
 
                if (p && r_o.status != 0)
                {
                        /* report error code */
-                       DEBUG(0,("REG_OPEN_UNK_4: %s\n", get_nt_error_msg(r_o.status)));
+                       DEBUG(0,("REG_OPEN_HKU: %s\n", get_nt_error_msg(r_o.status)));
                        p = False;
                }
 
@@ -151,11 +203,11 @@ do a REG Unknown 0xB command.  sent after a create key or create value.
 this might be some sort of "sync" or "refresh" command, sent after
 modification of the registry...
 ****************************************************************************/
-BOOL do_reg_unk_b(struct cli_state *cli, POLICY_HND *hnd)
+BOOL do_reg_flush_key(struct cli_state *cli, POLICY_HND *hnd)
 {
        prs_struct rbuf;
        prs_struct buf; 
-       REG_Q_UNK_B q_o;
+       REG_Q_FLUSH_KEY q_o;
        BOOL valid_query = False;
 
        if (hnd == NULL) return False;
@@ -163,30 +215,30 @@ BOOL do_reg_unk_b(struct cli_state *cli, POLICY_HND *hnd)
        prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
        prs_init(&rbuf, 0   , 4, SAFETY_MARGIN, True );
 
-       /* create and send a MSRPC command with api REG_UNK_B */
+       /* create and send a MSRPC command with api REG_FLUSH_KEY */
 
        DEBUG(4,("REG Unknown 0xB\n"));
 
-       make_reg_q_unk_b(&q_o, hnd);
+       make_reg_q_flush_key(&q_o, hnd);
 
        /* turn parameters into data stream */
-       reg_io_q_unk_b("", &q_o, &buf, 0);
+       reg_io_q_flush_key("", &q_o, &buf, 0);
 
        /* send the data on \PIPE\ */
-       if (rpc_api_pipe_req(cli, REG_UNK_B, &buf, &rbuf))
+       if (rpc_api_pipe_req(cli, REG_FLUSH_KEY, &buf, &rbuf))
        {
-               REG_R_UNK_B r_o;
+               REG_R_FLUSH_KEY r_o;
                BOOL p;
 
                ZERO_STRUCT(r_o);
 
-               reg_io_r_unk_b("", &r_o, &rbuf, 0);
+               reg_io_r_flush_key("", &r_o, &rbuf, 0);
                p = rbuf.offset != 0;
 
                if (p && r_o.status != 0)
                {
                        /* report error code */
-                       DEBUG(0,("REG_UNK_B: %s\n", get_nt_error_msg(r_o.status)));
+                       DEBUG(0,("REG_FLUSH_KEY: %s\n", get_nt_error_msg(r_o.status)));
                        p = False;
                }
 
@@ -385,6 +437,54 @@ BOOL do_reg_query_info(struct cli_state *cli, POLICY_HND *hnd,
        return valid_query;
 }
 
+/****************************************************************************
+do a REG Set Key Security 
+****************************************************************************/
+BOOL do_reg_set_key_sec(struct cli_state *cli, POLICY_HND *hnd,
+                               uint32 sec_buf_size, SEC_DESC *sec_buf)
+{
+       prs_struct rbuf;
+       prs_struct buf; 
+       REG_Q_SET_KEY_SEC q_o;
+       BOOL valid_query = False;
+
+       if (hnd == NULL) return False;
+
+       prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
+       prs_init(&rbuf, 0   , 4, SAFETY_MARGIN, True );
+
+       /* create and send a MSRPC command with api REG_SET_KEY_SEC */
+
+       DEBUG(4,("REG Set Key security.\n"));
+
+       make_reg_q_set_key_sec(&q_o, hnd, sec_buf_size, sec_buf);
+
+       /* turn parameters into data stream */
+       reg_io_q_set_key_sec("", &q_o, &buf, 0);
+
+       /* send the data on \PIPE\ */
+       if (rpc_api_pipe_req(cli, REG_SET_KEY_SEC, &buf, &rbuf))
+       {
+               REG_R_SET_KEY_SEC r_o;
+               BOOL p;
+
+               ZERO_STRUCT(r_o);
+
+               reg_io_r_set_key_sec("", &r_o, &rbuf, 0);
+               p = rbuf.offset != 0;
+
+               if (p && r_o.status != 0)
+               {
+                       valid_query = True;
+               }
+       }
+
+       prs_mem_free(&rbuf);
+       prs_mem_free(&buf );
+
+       return valid_query;
+}
+
 /****************************************************************************
 do a REG Query Key Security 
 ****************************************************************************/
@@ -419,6 +519,10 @@ BOOL do_reg_get_key_sec(struct cli_state *cli, POLICY_HND *hnd,
                ZERO_STRUCT(r_o);
 
                r_o.data = sec_buf;
+               if (*sec_buf_size != 0)
+               {
+                       sec_buf->sec = (SEC_DESC*)malloc(*sec_buf_size);
+               }
                reg_io_r_get_key_sec("", &r_o, &rbuf, 0);
                p = rbuf.offset != 0;
 
@@ -450,6 +554,60 @@ BOOL do_reg_get_key_sec(struct cli_state *cli, POLICY_HND *hnd,
        return valid_query;
 }
 
+/****************************************************************************
+do a REG Delete Value
+****************************************************************************/
+BOOL do_reg_delete_val(struct cli_state *cli, POLICY_HND *hnd, char *val_name)
+{
+       prs_struct rbuf;
+       prs_struct buf; 
+       REG_Q_DELETE_VALUE q_o;
+       BOOL valid_delete = False;
+
+       if (hnd == NULL) return False;
+
+       prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
+       prs_init(&rbuf, 0   , 4, SAFETY_MARGIN, True );
+
+       /* create and send a MSRPC command with api REG_DELETE_VALUE */
+
+       DEBUG(4,("REG Delete Value: %s\n", val_name));
+
+       make_reg_q_delete_val(&q_o, hnd, val_name);
+
+       /* turn parameters into data stream */
+       reg_io_q_delete_val("", &q_o, &buf, 0);
+
+       /* send the data on \PIPE\ */
+       if (rpc_api_pipe_req(cli, REG_DELETE_VALUE, &buf, &rbuf))
+       {
+               REG_R_DELETE_VALUE r_o;
+               BOOL p;
+
+               ZERO_STRUCT(r_o);
+
+               reg_io_r_delete_val("", &r_o, &rbuf, 0);
+               p = rbuf.offset != 0;
+
+               if (p && r_o.status != 0)
+               {
+                       /* report error code */
+                       DEBUG(0,("REG_DELETE_VALUE: %s\n", get_nt_error_msg(r_o.status)));
+                       p = False;
+               }
+
+               if (p)
+               {
+                       valid_delete = True;
+               }
+       }
+
+       prs_mem_free(&rbuf);
+       prs_mem_free(&buf );
+
+       return valid_delete;
+}
+
 /****************************************************************************
 do a REG Delete Key
 ****************************************************************************/
@@ -509,13 +667,20 @@ do a REG Create Key
 ****************************************************************************/
 BOOL do_reg_create_key(struct cli_state *cli, POLICY_HND *hnd,
                                char *key_name, char *key_class,
-                               SEC_INFO *sam_access,
+                               SEC_ACCESS *sam_access,
                                POLICY_HND *key)
 {
        prs_struct rbuf;
        prs_struct buf; 
        REG_Q_CREATE_KEY q_o;
        BOOL valid_create = False;
+       SEC_DESC sec;
+       SEC_DESC_BUF sec_buf;
+       int sec_len;
+
+       ZERO_STRUCT(sec);
+       ZERO_STRUCT(sec_buf);
+       ZERO_STRUCT(q_o);
 
        if (hnd == NULL) return False;
 
@@ -525,9 +690,15 @@ BOOL do_reg_create_key(struct cli_state *cli, POLICY_HND *hnd,
        /* create and send a MSRPC command with api REG_CREATE_KEY */
 
        DEBUG(4,("REG Create Key: %s %s 0x%08x\n", key_name, key_class,
-               sam_access != NULL ? sam_access->perms : 0));
+               sam_access != NULL ? sam_access->mask : 0));
 
-       make_reg_q_create_key(&q_o, hnd, key_name, key_class, sam_access);
+       sec_len = make_sec_desc(&sec, 1, SEC_DESC_SELF_RELATIVE,
+                               NULL, NULL, NULL, NULL);
+
+       DEBUG(10,("make_sec_desc: len = %d\n", sec_len));
+
+       make_reg_q_create_key(&q_o, hnd, key_name, key_class, sam_access,
+                             &sec_buf, sec_len, &sec);
 
        /* turn parameters into data stream */
        reg_io_q_create_key("", &q_o, &buf, 0);
@@ -557,6 +728,8 @@ BOOL do_reg_create_key(struct cli_state *cli, POLICY_HND *hnd,
                }
        }
 
+       free_sec_desc(&sec);
+
        prs_mem_free(&rbuf);
        prs_mem_free(&buf );