s3-rpc_server: run minimal_includes.pl.
[samba.git] / source3 / utils / net_registry.c
index 711c111275348469ece13a04cb462980350a5621..19405e2c085b10fbbcc163c45214605721361aaf 100644 (file)
  */
 
 #include "includes.h"
+#include "registry.h"
+#include "registry/reg_api.h"
+#include "registry/reg_util_token.h"
+#include "registry/reg_init_basic.h"
 #include "utils/net.h"
 #include "utils/net_registry_util.h"
-
+#include "include/g_lock.h"
+#include "registry/reg_backend_db.h"
+#include "registry/reg_import.h"
+#include "registry/reg_format.h"
+#include <assert.h>
+#include "../libcli/security/display_sec.h"
+#include "../libcli/security/sddl.h"
+#include "../libcli/registry/util_reg.h"
+#include "passdb/machine_sid.h"
 
 /*
  *
@@ -39,7 +51,7 @@ static WERROR open_hive(TALLOC_CTX *ctx, const char *path,
                        char **subkeyname)
 {
        WERROR werr;
-       NT_USER_TOKEN *token = NULL;
+       struct security_token *token = NULL;
        char *hivename = NULL;
        char *tmp_subkeyname = NULL;
        TALLOC_CTX *tmp_ctx = talloc_stackframe();
@@ -130,9 +142,12 @@ static int net_registry_enumerate(struct net_context *c, int argc,
        int ret = -1;
 
        if (argc != 1 || c->display_usage) {
-               d_printf(_("Usage:    net registry enumerate <path>\n"));
-               d_printf(_("Example:  net registry enumerate "
-                        "'HKLM\\Software\\Samba'\n"));
+               d_printf("%s\n%s",
+                        _("Usage:"),
+                        _("net registry enumerate <path>\n"));
+               d_printf("%s\n%s",
+                        _("Example:"),
+                        _("net registry enumerate 'HKLM\\Software\\Samba'\n"));
                goto done;
        }
 
@@ -182,9 +197,13 @@ static int net_registry_createkey(struct net_context *c, int argc,
        int ret = -1;
 
        if (argc != 1 || c->display_usage) {
-               d_printf(_("Usage:    net registry createkey <path>\n"));
-               d_printf(_("Example:  net registry createkey "
-                        "'HKLM\\Software\\Samba\\smbconf.127.0.0.1'\n"));
+               d_printf("%s\n%s",
+                        _("Usage:"),
+                        _("net registry createkey <path>\n"));
+               d_printf("%s\n%s",
+                        _("Example:"),
+                        _("net registry createkey "
+                          "'HKLM\\Software\\Samba\\smbconf.127.0.0.1'\n"));
                goto done;
        }
        if (strlen(argv[0]) == 0) {
@@ -225,8 +244,9 @@ done:
        return ret;
 }
 
-static int net_registry_deletekey(struct net_context *c, int argc,
-                                 const char **argv)
+static int net_registry_deletekey_internal(struct net_context *c, int argc,
+                                          const char **argv,
+                                          bool recursive)
 {
        WERROR werr;
        char *subkeyname;
@@ -235,8 +255,12 @@ static int net_registry_deletekey(struct net_context *c, int argc,
        int ret = -1;
 
        if (argc != 1 || c->display_usage) {
-               d_printf(_("Usage:    net registry deletekey <path>\n"));
-               d_printf(_("Example:  net registry deletekey "
+               d_printf("%s\n%s",
+                        _("Usage:"),
+                        _("net registry deletekey <path>\n"));
+               d_printf("%s\n%s",
+                        _("Example:"),
+                        _("net registry deletekey "
                           "'HKLM\\Software\\Samba\\smbconf.127.0.0.1'\n"));
                goto done;
        }
@@ -247,14 +271,20 @@ static int net_registry_deletekey(struct net_context *c, int argc,
 
        werr = open_hive(ctx, argv[0], REG_KEY_WRITE, &hivekey, &subkeyname);
        if (!W_ERROR_IS_OK(werr)) {
-               d_fprintf(stderr, _("open_hive failed: %s\n"),
+               d_fprintf(stderr, "open_hive %s: %s\n", _("failed"),
                          win_errstr(werr));
                goto done;
        }
 
-       werr = reg_deletekey(hivekey, subkeyname);
-       if (!W_ERROR_IS_OK(werr)) {
-               d_fprintf(stderr, _("reg_deletekey failed: %s\n"),
+       if (recursive) {
+               werr = reg_deletekey_recursive(hivekey, subkeyname);
+       } else {
+               werr = reg_deletekey(hivekey, subkeyname);
+       }
+       if (!W_ERROR_IS_OK(werr) &&
+           !(c->opt_force && W_ERROR_EQUAL(werr, WERR_BADFILE)))
+       {
+               d_fprintf(stderr, "reg_deletekey %s: %s\n", _("failed"),
                          win_errstr(werr));
                goto done;
        }
@@ -266,6 +296,18 @@ done:
        return ret;
 }
 
+static int net_registry_deletekey(struct net_context *c, int argc,
+                                 const char **argv)
+{
+       return net_registry_deletekey_internal(c, argc, argv, false);
+}
+
+static int net_registry_deletekey_recursive(struct net_context *c, int argc,
+                                           const char **argv)
+{
+       return net_registry_deletekey_internal(c, argc, argv, true);
+}
+
 static int net_registry_getvalue_internal(struct net_context *c, int argc,
                                          const char **argv, bool raw)
 {
@@ -276,8 +318,9 @@ static int net_registry_getvalue_internal(struct net_context *c, int argc,
        TALLOC_CTX *ctx = talloc_stackframe();
 
        if (argc != 2 || c->display_usage) {
-               d_fprintf(stderr, _("usage: net rpc registry getvalue <key> "
-                                   "<valuename>\n"));
+               d_fprintf(stderr, "%s\n%s",
+                         _("Usage:"),
+                         _("net registry getvalue <key> <valuename>\n"));
                goto done;
        }
 
@@ -315,6 +358,49 @@ static int net_registry_getvalueraw(struct net_context *c, int argc,
        return net_registry_getvalue_internal(c, argc, argv, true);
 }
 
+static int net_registry_getvaluesraw(struct net_context *c, int argc,
+                                    const char **argv)
+{
+       WERROR werr;
+       int ret = -1;
+       struct registry_key *key = NULL;
+       TALLOC_CTX *ctx = talloc_stackframe();
+       uint32_t idx;
+
+       if (argc != 1 || c->display_usage) {
+               d_fprintf(stderr, "usage: net rpc registry getvaluesraw "
+                         "<key>\n");
+               goto done;
+       }
+
+       werr = open_key(ctx, argv[0], REG_KEY_READ, &key);
+       if (!W_ERROR_IS_OK(werr)) {
+               d_fprintf(stderr, "open_key failed: %s\n", win_errstr(werr));
+               goto done;
+       }
+
+       idx = 0;
+       while (true) {
+               struct registry_value *val;
+
+               werr = reg_enumvalue(talloc_tos(), key, idx, NULL, &val);
+
+               if (W_ERROR_EQUAL(werr, WERR_NO_MORE_ITEMS)) {
+                       ret = 0;
+                       break;
+               }
+               if (!W_ERROR_IS_OK(werr)) {
+                       break;
+               }
+               print_registry_value(val, true);
+               TALLOC_FREE(val);
+               idx += 1;
+       }
+done:
+       TALLOC_FREE(ctx);
+       return ret;
+}
+
 static int net_registry_setvalue(struct net_context *c, int argc,
                                 const char **argv)
 {
@@ -325,8 +411,10 @@ static int net_registry_setvalue(struct net_context *c, int argc,
        TALLOC_CTX *ctx = talloc_stackframe();
 
        if (argc < 4 || c->display_usage) {
-               d_fprintf(stderr, _("usage: net rpc registry setvalue <key> "
-                         "<valuename> <type> [<val>]+\n"));
+               d_fprintf(stderr, "%s\n%s",
+                         _("Usage:"),
+                         _("net registry setvalue <key> <valuename> "
+                           "<type> [<val>]+\n"));
                goto done;
        }
 
@@ -336,16 +424,33 @@ static int net_registry_setvalue(struct net_context *c, int argc,
        }
 
        if (strequal(argv[2], "dword")) {
+               uint32_t v = strtoul(argv[3], NULL, 10);
                value.type = REG_DWORD;
-               value.v.dword = strtoul(argv[3], NULL, 10);
+               value.data = data_blob_talloc(ctx, NULL, 4);
+               SIVAL(value.data.data, 0, v);
        } else if (strequal(argv[2], "sz")) {
                value.type = REG_SZ;
-               value.v.sz.len = strlen(argv[3])+1;
-               value.v.sz.str = CONST_DISCARD(char *, argv[3]);
+               if (!push_reg_sz(ctx, &value.data, argv[3])) {
+                       goto done;
+               }
        } else if (strequal(argv[2], "multi_sz")) {
+               const char **array;
+               int count = argc - 3;
+               int i;
                value.type = REG_MULTI_SZ;
-               value.v.multi_sz.num_strings = argc - 3;
-               value.v.multi_sz.strings = (char **)(argv + 3);
+               array = talloc_zero_array(ctx, const char *, count + 1);
+               if (array == NULL) {
+                       goto done;
+               }
+               for (i=0; i < count; i++) {
+                       array[i] = talloc_strdup(array, argv[count+i]);
+                       if (array[i] == NULL) {
+                               goto done;
+                       }
+               }
+               if (!push_reg_multi_sz(ctx, &value.data, array)) {
+                       goto done;
+               }
        } else {
                d_fprintf(stderr, _("type \"%s\" not implemented\n"), argv[2]);
                goto done;
@@ -371,6 +476,111 @@ done:
        return ret;
 }
 
+struct net_registry_increment_state {
+       const char *keyname;
+       const char *valuename;
+       uint32_t increment;
+       uint32_t newvalue;
+       WERROR werr;
+};
+
+static void net_registry_increment_fn(void *private_data)
+{
+       struct net_registry_increment_state *state =
+               (struct net_registry_increment_state *)private_data;
+       struct registry_value *value;
+       struct registry_key *key = NULL;
+       uint32_t v;
+
+       state->werr = open_key(talloc_tos(), state->keyname,
+                              REG_KEY_READ|REG_KEY_WRITE, &key);
+       if (!W_ERROR_IS_OK(state->werr)) {
+               d_fprintf(stderr, _("open_key failed: %s\n"),
+                         win_errstr(state->werr));
+               goto done;
+       }
+
+       state->werr = reg_queryvalue(key, key, state->valuename, &value);
+       if (!W_ERROR_IS_OK(state->werr)) {
+               d_fprintf(stderr, _("reg_queryvalue failed: %s\n"),
+                         win_errstr(state->werr));
+               goto done;
+       }
+
+       if (value->type != REG_DWORD) {
+               d_fprintf(stderr, _("value not a DWORD: %s\n"),
+                         str_regtype(value->type));
+               goto done;
+       }
+
+       if (value->data.length < 4) {
+               d_fprintf(stderr, _("value too short for regular DWORD\n"));
+               goto done;
+       }
+
+       v = IVAL(value->data.data, 0);
+       v += state->increment;
+       state->newvalue = v;
+
+       SIVAL(value->data.data, 0, v);
+
+       state->werr = reg_setvalue(key, state->valuename, value);
+       if (!W_ERROR_IS_OK(state->werr)) {
+               d_fprintf(stderr, _("reg_setvalue failed: %s\n"),
+                         win_errstr(state->werr));
+               goto done;
+       }
+
+done:
+       TALLOC_FREE(key);
+       return;
+}
+
+static int net_registry_increment(struct net_context *c, int argc,
+                                 const char **argv)
+{
+       struct net_registry_increment_state state;
+       NTSTATUS status;
+       int ret = -1;
+
+       if (argc < 2 || c->display_usage) {
+               d_fprintf(stderr, "%s\n%s",
+                         _("Usage:"),
+                         _("net registry increment <key> <valuename> "
+                           "[<increment>]\n"));
+               goto done;
+       }
+
+       state.keyname = argv[0];
+       state.valuename = argv[1];
+
+       state.increment = 1;
+       if (argc == 3) {
+               state.increment = strtoul(argv[2], NULL, 10);
+       }
+
+       status = g_lock_do("registry_increment_lock", G_LOCK_WRITE,
+                          timeval_set(600, 0), procid_self(),
+                          net_registry_increment_fn, &state);
+       if (!NT_STATUS_IS_OK(status)) {
+               d_fprintf(stderr, _("g_lock_do failed: %s\n"),
+                         nt_errstr(status));
+               goto done;
+       }
+       if (!W_ERROR_IS_OK(state.werr)) {
+               d_fprintf(stderr, _("increment failed: %s\n"),
+                         win_errstr(state.werr));
+               goto done;
+       }
+
+       d_printf(_("%u\n"), (unsigned)state.newvalue);
+
+       ret = 0;
+
+done:
+       return ret;
+}
+
 static int net_registry_deletevalue(struct net_context *c, int argc,
                                    const char **argv)
 {
@@ -380,8 +590,9 @@ static int net_registry_deletevalue(struct net_context *c, int argc,
        int ret = -1;
 
        if (argc != 2 || c->display_usage) {
-               d_fprintf(stderr, _("usage: net rpc registry deletevalue <key> "
-                         "<valuename>\n"));
+               d_fprintf(stderr, "%s\n%s",
+                         _("Usage:"),
+                         _("net registry deletevalue <key> <valuename>\n"));
                goto done;
        }
 
@@ -393,7 +604,7 @@ static int net_registry_deletevalue(struct net_context *c, int argc,
 
        werr = reg_deletevalue(key, argv[1]);
        if (!W_ERROR_IS_OK(werr)) {
-               d_fprintf(stderr, _("reg_deletekey failed: %s\n"),
+               d_fprintf(stderr, _("reg_deletevalue failed: %s\n"),
                          win_errstr(werr));
                goto done;
        }
@@ -405,13 +616,13 @@ done:
        return ret;
 }
 
-static int net_registry_getsd(struct net_context *c, int argc,
-                             const char **argv)
+static WERROR net_registry_getsd_internal(struct net_context *c,
+                                         TALLOC_CTX *mem_ctx,
+                                         const char *keyname,
+                                         struct security_descriptor **sd)
 {
        WERROR werr;
-       int ret = -1;
        struct registry_key *key = NULL;
-       struct security_descriptor *secdesc = NULL;
        TALLOC_CTX *ctx = talloc_stackframe();
        uint32_t access_mask = REG_KEY_READ |
                               SEC_FLAG_MAXIMUM_ALLOWED |
@@ -423,30 +634,62 @@ static int net_registry_getsd(struct net_context *c, int argc,
         */
        access_mask = REG_KEY_READ;
 
-       if (argc != 1 || c->display_usage) {
-               d_printf(_("Usage:    net registry getsd <path>\n"));
-               d_printf(_("Example:  net registry getsd "
-                          "'HKLM\\Software\\Samba'\n"));
+       if (sd == NULL) {
+               d_fprintf(stderr, _("internal error: invalid argument\n"));
+               werr = WERR_INVALID_PARAM;
                goto done;
        }
-       if (strlen(argv[0]) == 0) {
-               d_fprintf(stderr, "error: zero length key name given\n");
+
+       if (strlen(keyname) == 0) {
+               d_fprintf(stderr, _("error: zero length key name given\n"));
+               werr = WERR_INVALID_PARAM;
                goto done;
        }
 
-       werr = open_key(ctx, argv[0], access_mask, &key);
+       werr = open_key(ctx, keyname, access_mask, &key);
        if (!W_ERROR_IS_OK(werr)) {
-               d_fprintf(stderr, _("open_key failed: %s\n"), win_errstr(werr));
+               d_fprintf(stderr, "%s%s\n", _("open_key failed: "),
+                         win_errstr(werr));
                goto done;
        }
 
-       werr = reg_getkeysecurity(ctx, key, &secdesc);
+       werr = reg_getkeysecurity(mem_ctx, key, sd);
        if (!W_ERROR_IS_OK(werr)) {
-               d_fprintf(stderr, _("reg_getkeysecurity failed: %s\n"),
+               d_fprintf(stderr, "%s%s\n", _("reg_getkeysecurity failed: "),
                          win_errstr(werr));
                goto done;
        }
 
+       werr = WERR_OK;
+
+done:
+       TALLOC_FREE(ctx);
+       return werr;
+}
+
+static int net_registry_getsd(struct net_context *c, int argc,
+                             const char **argv)
+{
+       WERROR werr;
+       int ret = -1;
+       struct security_descriptor *secdesc = NULL;
+       TALLOC_CTX *ctx = talloc_stackframe();
+
+       if (argc != 1 || c->display_usage) {
+               d_printf("%s\n%s",
+                        _("Usage:"),
+                        _("net registry getsd <path>\n"));
+               d_printf("%s\n%s",
+                        _("Example:"),
+                        _("net registry getsd 'HKLM\\Software\\Samba'\n"));
+               goto done;
+       }
+
+       werr = net_registry_getsd_internal(c, ctx, argv[0], &secdesc);
+       if (!W_ERROR_IS_OK(werr)) {
+               goto done;
+       }
+
        display_sec_desc(secdesc);
 
        ret = 0;
@@ -456,6 +699,467 @@ done:
        return ret;
 }
 
+static int net_registry_getsd_sddl(struct net_context *c,
+                                  int argc, const char **argv)
+{
+       WERROR werr;
+       int ret = -1;
+       struct security_descriptor *secdesc = NULL;
+       TALLOC_CTX *ctx = talloc_stackframe();
+
+       if (argc != 1 || c->display_usage) {
+               d_printf("%s\n%s",
+                        _("Usage:"),
+                        _("net registry getsd_sddl <path>\n"));
+               d_printf("%s\n%s",
+                        _("Example:"),
+                        _("net registry getsd_sddl 'HKLM\\Software\\Samba'\n"));
+               goto done;
+       }
+
+       werr = net_registry_getsd_internal(c, ctx, argv[0], &secdesc);
+       if (!W_ERROR_IS_OK(werr)) {
+               goto done;
+       }
+
+       d_printf("%s\n", sddl_encode(ctx, secdesc, get_global_sam_sid()));
+
+       ret = 0;
+
+done:
+       TALLOC_FREE(ctx);
+       return ret;
+}
+
+static WERROR net_registry_setsd_internal(struct net_context *c,
+                                         TALLOC_CTX *mem_ctx,
+                                         const char *keyname,
+                                         struct security_descriptor *sd)
+{
+       WERROR werr;
+       struct registry_key *key = NULL;
+       TALLOC_CTX *ctx = talloc_stackframe();
+       uint32_t access_mask = REG_KEY_WRITE |
+                              SEC_FLAG_MAXIMUM_ALLOWED |
+                              SEC_FLAG_SYSTEM_SECURITY;
+
+       /*
+        * net_rpc_regsitry uses SEC_FLAG_SYSTEM_SECURITY, but access
+        * is denied with these perms right now...
+        */
+       access_mask = REG_KEY_WRITE;
+
+       if (strlen(keyname) == 0) {
+               d_fprintf(stderr, _("error: zero length key name given\n"));
+               werr = WERR_INVALID_PARAM;
+               goto done;
+       }
+
+       werr = open_key(ctx, keyname, access_mask, &key);
+       if (!W_ERROR_IS_OK(werr)) {
+               d_fprintf(stderr, "%s%s\n", _("open_key failed: "),
+                         win_errstr(werr));
+               goto done;
+       }
+
+       werr = reg_setkeysecurity(key, sd);
+       if (!W_ERROR_IS_OK(werr)) {
+               d_fprintf(stderr, "%s%s\n", _("reg_setkeysecurity failed: "),
+                         win_errstr(werr));
+               goto done;
+       }
+
+       werr = WERR_OK;
+
+done:
+       TALLOC_FREE(ctx);
+       return werr;
+}
+
+static int net_registry_setsd_sddl(struct net_context *c,
+                                  int argc, const char **argv)
+{
+       WERROR werr;
+       int ret = -1;
+       struct security_descriptor *secdesc = NULL;
+       TALLOC_CTX *ctx = talloc_stackframe();
+
+       if (argc != 2 || c->display_usage) {
+               d_printf("%s\n%s",
+                        _("Usage:"),
+                        _("net registry setsd_sddl <path> <security_descriptor>\n"));
+               d_printf("%s\n%s",
+                        _("Example:"),
+                        _("net registry setsd_sddl 'HKLM\\Software\\Samba'\n"));
+               goto done;
+       }
+
+       secdesc = sddl_decode(ctx, argv[1], get_global_sam_sid());
+       if (secdesc == NULL) {
+               goto done;
+       }
+
+       werr = net_registry_setsd_internal(c, ctx, argv[0], secdesc);
+       if (!W_ERROR_IS_OK(werr)) {
+               goto done;
+       }
+
+       ret = 0;
+
+done:
+       TALLOC_FREE(ctx);
+       return ret;
+}
+
+/******************************************************************************/
+/**
+ * @defgroup net_registry net registry
+ */
+
+/**
+ * @defgroup net_registry_import Import
+ * @ingroup net_registry
+ * @{
+ */
+
+struct import_ctx {
+       TALLOC_CTX *mem_ctx;
+};
+
+
+static WERROR import_create_key(struct import_ctx* ctx,
+                               struct registry_key* parent,
+                               const char* name, void** pkey, bool* existing)
+{
+       WERROR werr;
+       void* mem_ctx = talloc_new(ctx->mem_ctx);
+
+       struct registry_key* key = NULL;
+       enum winreg_CreateAction action;
+
+       if (parent == NULL) {
+               char* subkeyname = NULL;
+               werr = open_hive(mem_ctx, name, REG_KEY_WRITE,
+                        &parent, &subkeyname);
+               if (!W_ERROR_IS_OK(werr)) {
+                       d_fprintf(stderr, _("open_hive failed: %s\n"),
+                                 win_errstr(werr));
+                       goto done;
+               }
+               name = subkeyname;
+       }
+
+       action = REG_ACTION_NONE;
+       werr = reg_createkey(mem_ctx, parent, name, REG_KEY_WRITE,
+                            &key, &action);
+       if (!W_ERROR_IS_OK(werr)) {
+               d_fprintf(stderr, _("reg_createkey failed: %s\n"),
+                         win_errstr(werr));
+               goto done;
+       }
+
+       if (action == REG_ACTION_NONE) {
+               d_fprintf(stderr, _("createkey did nothing -- huh?\n"));
+               werr = WERR_CREATE_FAILED;
+               goto done;
+       }
+
+       if (existing != NULL) {
+               *existing = (action == REG_OPENED_EXISTING_KEY);
+       }
+
+       if (pkey!=NULL) {
+               *pkey = talloc_steal(ctx->mem_ctx, key);
+       }
+
+done:
+       talloc_free(mem_ctx);
+       return werr;
+}
+
+static WERROR import_close_key(struct import_ctx* ctx,
+                              struct registry_key* key)
+{
+       return WERR_OK;
+}
+
+static WERROR import_delete_key(struct import_ctx* ctx,
+                               struct registry_key* parent, const char* name)
+{
+       WERROR werr;
+       void* mem_ctx = talloc_new(talloc_tos());
+
+       if (parent == NULL) {
+               char* subkeyname = NULL;
+               werr = open_hive(mem_ctx, name, REG_KEY_WRITE,
+                        &parent, &subkeyname);
+               if (!W_ERROR_IS_OK(werr)) {
+                       d_fprintf(stderr, _("open_hive failed: %s\n"),
+                                 win_errstr(werr));
+                       goto done;
+               }
+               name = subkeyname;
+       }
+
+       werr = reg_deletekey_recursive(parent, name);
+       if (!W_ERROR_IS_OK(werr)) {
+               d_fprintf(stderr, "reg_deletekey_recursive %s: %s\n", _("failed"),
+                         win_errstr(werr));
+               goto done;
+       }
+
+done:
+       talloc_free(mem_ctx);
+       return werr;
+}
+
+static WERROR import_create_val (struct import_ctx* ctx,
+                                struct registry_key* parent, const char* name,
+                                const struct registry_value* value)
+{
+       WERROR werr;
+
+       if (parent == NULL) {
+               return WERR_INVALID_PARAM;
+       }
+
+       werr = reg_setvalue(parent, name, value);
+       if (!W_ERROR_IS_OK(werr)) {
+               d_fprintf(stderr, _("reg_setvalue failed: %s\n"),
+                         win_errstr(werr));
+       }
+       return werr;
+}
+
+static WERROR import_delete_val (struct import_ctx* ctx, struct registry_key* parent, const char* name) {
+       WERROR werr;
+
+       if (parent == NULL) {
+               return WERR_INVALID_PARAM;
+       }
+
+       werr = reg_deletevalue(parent, name);
+       if (!W_ERROR_IS_OK(werr)) {
+               d_fprintf(stderr, _("reg_deletevalue failed: %s\n"),
+                         win_errstr(werr));
+       }
+
+       return werr;
+}
+
+
+static int net_registry_import(struct net_context *c, int argc,
+                              const char **argv)
+{
+       struct import_ctx import_ctx;
+       struct reg_import_callback import_callback = {
+               .openkey     = NULL,
+               .closekey    = (reg_import_callback_closekey_t)&import_close_key,
+               .createkey   = (reg_import_callback_createkey_t)&import_create_key,
+               .deletekey   = (reg_import_callback_deletekey_t)&import_delete_key,
+               .deleteval   = (reg_import_callback_deleteval_t)&import_delete_val,
+               .setval.registry_value = (reg_import_callback_setval_registry_value_t)
+               &import_create_val,
+               .setval_type           = REGISTRY_VALUE,
+               .data        = &import_ctx
+       };
+
+       int ret;
+
+       if (argc < 1 || argc > 2 || c->display_usage) {
+               d_printf("%s\n%s",
+                        _("Usage:"),
+                        _("net registry import <reg> [options]\n"));
+               d_printf("%s\n%s",
+                        _("Example:"),
+                        _("net registry import file.reg enc=CP1252\n"));
+               return -1;
+       }
+
+       ZERO_STRUCT(import_ctx);
+       import_ctx.mem_ctx = talloc_stackframe();
+
+       regdb_open();
+       regdb_transaction_start();
+
+       ret = reg_parse_file(argv[0],
+                            reg_import_adapter(import_ctx.mem_ctx,
+                                               import_callback),
+                            (argc > 1) ? argv[1] : NULL
+               );
+       if (ret < 0) {
+               d_printf("reg_parse_file failed: transaction canceled\n");
+               regdb_transaction_cancel();
+       } else{
+               regdb_transaction_commit();
+       }
+
+       regdb_close();
+       talloc_free(import_ctx.mem_ctx);
+
+       return ret;
+}
+/**@}*/
+
+/******************************************************************************/
+
+/**
+ * @defgroup net_registry_export Export
+ * @ingroup net_registry
+ * @{
+ */
+
+static int registry_export(TALLOC_CTX *ctx, /*const*/ struct registry_key* key,
+                          struct reg_format* f)
+{
+       int ret=-1;
+       WERROR werr;
+       uint32_t count;
+
+       struct registry_value *valvalue = NULL;
+       char *valname = NULL;
+
+       struct registry_key* subkey = NULL;
+       char *subkey_name = NULL;
+       NTTIME modtime = 0;
+
+       reg_format_registry_key(f, key, false);
+
+       /* print values */
+       for (count = 0;
+            werr = reg_enumvalue(ctx, key, count, &valname, &valvalue),
+                    W_ERROR_IS_OK(werr);
+            count++)
+       {
+               reg_format_registry_value(f, valname, valvalue);
+       }
+       if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
+               d_fprintf(stderr, _("reg_enumvalue failed: %s\n"),
+                         win_errstr(werr));
+               goto done;
+       }
+
+       /* recurse on subkeys */
+       for (count = 0;
+            werr = reg_enumkey(ctx, key, count, &subkey_name, &modtime),
+                    W_ERROR_IS_OK(werr);
+            count++)
+       {
+               werr = reg_openkey(ctx, key, subkey_name, REG_KEY_READ,
+                                  &subkey);
+               if (!W_ERROR_IS_OK(werr)) {
+                       d_fprintf(stderr, _("reg_openkey failed: %s\n"),
+                                 win_errstr(werr));
+                       goto done;
+               }
+
+               registry_export(ctx, subkey, f);
+       }
+       if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
+               d_fprintf(stderr, _("reg_enumkey failed: %s\n"),
+                         win_errstr(werr));
+               goto done;
+       }
+       ret = 0;
+done:
+       return ret;
+}
+
+static int net_registry_export(struct net_context *c, int argc,
+                              const char **argv)
+{
+       int ret=-1;
+       WERROR werr;
+       struct registry_key *key = NULL;
+       TALLOC_CTX *ctx = talloc_stackframe();
+       struct reg_format* f=NULL;
+
+       if (argc < 2 || argc > 3 || c->display_usage) {
+               d_printf("%s\n%s",
+                        _("Usage:"),
+                        _("net registry export <path> <file> [opt]\n"));
+               d_printf("%s\n%s",
+                        _("Example:"),
+                        _("net registry export 'HKLM\\Software\\Samba' "
+                          "samba.reg regedit5\n"));
+               goto done;
+       }
+
+       werr = open_key(ctx, argv[0], REG_KEY_READ, &key);
+       if (!W_ERROR_IS_OK(werr)) {
+               d_fprintf(stderr, _("open_key failed: %s\n"), win_errstr(werr));
+               goto done;
+       }
+
+       f = reg_format_file(ctx, argv[1], (argc > 2) ? argv[2] : NULL);
+       if (f == NULL) {
+               d_fprintf(stderr, _("open file failed: %s\n"), strerror(errno));
+               goto done;
+       }
+
+       ret = registry_export(ctx, key, f);
+
+done:
+       TALLOC_FREE(ctx);
+       return ret;
+}
+/**@}*/
+
+/******************************************************************************/
+/**
+ * @defgroup net_registry_convert Convert
+ * @ingroup net_registry
+ * @{
+ */
+
+static int net_registry_convert(struct net_context *c, int argc,
+                              const char **argv)
+{
+       int ret;
+       void* mem_ctx;
+       const char* in_opt  = NULL;
+       const char* out_opt = NULL;
+
+       if (argc < 2 || argc > 4|| c->display_usage) {
+               d_printf("%s\n%s",
+                        _("Usage:"),
+                        _("net registry convert <in> <out> [in_opt] [out_opt]\n"
+                          "net registry convert <in> <out> [out_opt]\n"));
+               d_printf("%s\n%s",
+                        _("Example:"),
+                        _("net registry convert in.reg out.reg regedit4,enc=CP1252\n"));
+               return -1;
+       }
+
+       mem_ctx = talloc_stackframe();
+
+       switch (argc ) {
+       case 2:
+               break;
+       case 3:
+               out_opt = argv[2];
+               break;
+       case 4:
+               out_opt = argv[3];
+               in_opt  = argv[2];
+               break;
+       default:
+               assert(false);
+       }
+
+
+       ret = reg_parse_file(argv[0], (struct reg_parse_callback*)
+                            reg_format_file(mem_ctx, argv[1], out_opt),
+                            in_opt);
+
+       talloc_free(mem_ctx);
+
+       return ret;
+}
+/**@}*/
+
+/******************************************************************************/
+
 int net_registry(struct net_context *c, int argc, const char **argv)
 {
        int ret = -1;
@@ -485,6 +1189,14 @@ int net_registry(struct net_context *c, int argc, const char **argv)
                        N_("net registry deletekey\n"
                           "    Delete a registry key")
                },
+               {
+                       "deletekey_recursive",
+                       net_registry_deletekey_recursive,
+                       NET_TRANSPORT_LOCAL,
+                       N_("Delete a registry key with subkeys"),
+                       N_("net registry deletekey_recursive\n"
+                          "    Delete a registry key with subkeys")
+               },
                {
                        "getvalue",
                        net_registry_getvalue,
@@ -501,6 +1213,14 @@ int net_registry(struct net_context *c, int argc, const char **argv)
                        N_("net registry getvalueraw\n"
                           "    Print a registry value (raw format)")
                },
+               {
+                       "getvaluesraw",
+                       net_registry_getvaluesraw,
+                       NET_TRANSPORT_LOCAL,
+                       "Print all values of a key in raw format",
+                       "net registry getvaluesraw <key>\n"
+                       "    Print a registry value (raw format)"
+               },
                {
                        "setvalue",
                        net_registry_setvalue,
@@ -509,6 +1229,14 @@ int net_registry(struct net_context *c, int argc, const char **argv)
                        N_("net registry setvalue\n"
                           "    Set a new registry value")
                },
+               {
+                       "increment",
+                       net_registry_increment,
+                       NET_TRANSPORT_LOCAL,
+                       N_("Increment a DWORD registry value under a lock"),
+                       N_("net registry increment\n"
+                          "    Increment a DWORD registry value under a lock")
+               },
                {
                        "deletevalue",
                        net_registry_deletevalue,
@@ -525,6 +1253,46 @@ int net_registry(struct net_context *c, int argc, const char **argv)
                        N_("net registry getsd\n"
                           "    Get security descriptor")
                },
+               {
+                       "getsd_sddl",
+                       net_registry_getsd_sddl,
+                       NET_TRANSPORT_LOCAL,
+                       N_("Get security descriptor in sddl format"),
+                       N_("net registry getsd_sddl\n"
+                          "    Get security descriptor in sddl format")
+               },
+               {
+                       "setsd_sddl",
+                       net_registry_setsd_sddl,
+                       NET_TRANSPORT_LOCAL,
+                       N_("Set security descriptor from sddl format string"),
+                       N_("net registry setsd_sddl\n"
+                          "    Set security descriptor from sddl format string")
+               },
+               {
+                       "import",
+                       net_registry_import,
+                       NET_TRANSPORT_LOCAL,
+                       N_("Import .reg file"),
+                       N_("net registry import\n"
+                          "    Import .reg file")
+               },
+               {
+                       "export",
+                       net_registry_export,
+                       NET_TRANSPORT_LOCAL,
+                       N_("Export .reg file"),
+                       N_("net registry export\n"
+                          "    Export .reg file")
+               },
+               {
+                       "convert",
+                       net_registry_convert,
+                       NET_TRANSPORT_LOCAL,
+                       N_("Convert .reg file"),
+                       N_("net registry convert\n"
+                          "    Convert .reg file")
+               },
        { NULL, NULL, 0, NULL, NULL }
        };