s3-net: add command registry import
[obnox/samba-ctdb.git] / source3 / utils / net_registry.c
index f21a1603cf3ee55ab975c0f53c04a654e618859d..7f97278c8e6953dc0b1ef09edb4b1ad52d86b123 100644 (file)
@@ -22,7 +22,8 @@
 #include "includes.h"
 #include "utils/net.h"
 #include "utils/net_registry_util.h"
-
+#include "include/g_lock.h"
+#include "registry/reg_import.h"
 
 /*
  *
@@ -41,7 +42,7 @@ static WERROR open_hive(TALLOC_CTX *ctx, const char *path,
        WERROR werr;
        NT_USER_TOKEN *token = NULL;
        char *hivename = NULL;
-       const char *tmp_subkeyname = NULL;
+       char *tmp_subkeyname = NULL;
        TALLOC_CTX *tmp_ctx = talloc_stackframe();
 
        if ((hive == NULL) || (subkeyname == NULL)) {
@@ -91,14 +92,14 @@ static WERROR open_key(TALLOC_CTX *ctx, const char *path,
 
        werr = open_hive(tmp_ctx, path, desired_access, &hive, &subkey_name);
        if (!W_ERROR_IS_OK(werr)) {
-               d_fprintf(stderr, "open_hive failed: %s\n", dos_errstr(werr));
+               d_fprintf(stderr, "open_hive failed: %s\n", win_errstr(werr));
                goto done;
        }
 
        werr = reg_openkey(ctx, hive, subkey_name, desired_access, key);
        if (!W_ERROR_IS_OK(werr)) {
                d_fprintf(stderr, "reg_openkey failed: %s\n",
-                         dos_errstr(werr));
+                         win_errstr(werr));
                goto done;
        }
 
@@ -115,7 +116,8 @@ done:
  *
  */
 
-static int net_registry_enumerate(int argc, const char **argv)
+static int net_registry_enumerate(struct net_context *c, int argc,
+                                 const char **argv)
 {
        WERROR werr;
        struct registry_key *key = NULL;
@@ -127,7 +129,7 @@ static int net_registry_enumerate(int argc, const char **argv)
        struct registry_value *valvalue = NULL;
        int ret = -1;
 
-       if (argc != 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");
@@ -136,7 +138,7 @@ static int net_registry_enumerate(int argc, const char **argv)
 
        werr = open_key(ctx, argv[0], REG_KEY_READ, &key);
        if (!W_ERROR_IS_OK(werr)) {
-               d_fprintf(stderr, "open_key failed: %s\n", dos_errstr(werr));
+               d_fprintf(stderr, "open_key failed: %s\n", win_errstr(werr));
                goto done;
        }
 
@@ -156,7 +158,7 @@ static int net_registry_enumerate(int argc, const char **argv)
             W_ERROR_IS_OK(werr);
             count++)
        {
-               print_registry_value(valname, valvalue);
+               print_registry_value_with_name(valname, valvalue);
        }
        if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
                goto done;
@@ -168,7 +170,8 @@ done:
        return ret;
 }
 
-static int net_registry_createkey(int argc, const char **argv)
+static int net_registry_createkey(struct net_context *c, int argc,
+                                 const char **argv)
 {
        WERROR werr;
        enum winreg_CreateAction action;
@@ -178,7 +181,7 @@ static int net_registry_createkey(int argc, const char **argv)
        TALLOC_CTX *ctx = talloc_stackframe();
        int ret = -1;
 
-       if (argc != 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");
@@ -191,7 +194,7 @@ static int net_registry_createkey(int argc, const char **argv)
 
        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", dos_errstr(werr));
+               d_fprintf(stderr, "open_hive failed: %s\n", win_errstr(werr));
                goto done;
        }
 
@@ -199,7 +202,7 @@ static int net_registry_createkey(int argc, const char **argv)
                             &subkey, &action);
        if (!W_ERROR_IS_OK(werr)) {
                d_fprintf(stderr, "reg_createkey failed: %s\n",
-                         dos_errstr(werr));
+                         win_errstr(werr));
                goto done;
        }
        switch (action) {
@@ -221,7 +224,8 @@ done:
        return ret;
 }
 
-static int net_registry_deletekey(int argc, const char **argv)
+static int net_registry_deletekey(struct net_context *c, int argc,
+                                 const char **argv)
 {
        WERROR werr;
        char *subkeyname;
@@ -229,7 +233,7 @@ static int net_registry_deletekey(int argc, const char **argv)
        TALLOC_CTX *ctx = talloc_stackframe();
        int ret = -1;
 
-       if (argc != 1) {
+       if (argc != 1 || c->display_usage) {
                d_printf("Usage:    net registry deletekey <path>\n");
                d_printf("Example:  net registry deletekey "
                         "'HKLM\\Software\\Samba\\smbconf.127.0.0.1'\n");
@@ -242,14 +246,14 @@ static int net_registry_deletekey(int argc, const char **argv)
 
        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", dos_errstr(werr));
+               d_fprintf(stderr, "open_hive failed: %s\n", win_errstr(werr));
                goto done;
        }
 
        werr = reg_deletekey(hivekey, subkeyname);
        if (!W_ERROR_IS_OK(werr)) {
                d_fprintf(stderr, "reg_deletekey failed: %s\n",
-                         dos_errstr(werr));
+                         win_errstr(werr));
                goto done;
        }
 
@@ -260,7 +264,100 @@ done:
        return ret;
 }
 
-static int net_registry_setvalue(int argc, const char **argv)
+static int net_registry_getvalue_internal(struct net_context *c, int argc,
+                                         const char **argv, bool raw)
+{
+       WERROR werr;
+       int ret = -1;
+       struct registry_key *key = NULL;
+       struct registry_value *value = NULL;
+       TALLOC_CTX *ctx = talloc_stackframe();
+
+       if (argc != 2 || c->display_usage) {
+               d_fprintf(stderr, "usage: net rpc registry getvalue <key> "
+                                 "<valuename>\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;
+       }
+
+       werr = reg_queryvalue(ctx, key, argv[1], &value);
+       if (!W_ERROR_IS_OK(werr)) {
+               d_fprintf(stderr, "reg_queryvalue failed: %s\n",
+                         win_errstr(werr));
+               goto done;
+       }
+
+       print_registry_value(value, raw);
+
+       ret = 0;
+
+done:
+       TALLOC_FREE(ctx);
+       return ret;
+}
+
+static int net_registry_getvalue(struct net_context *c, int argc,
+                                const char **argv)
+{
+       return net_registry_getvalue_internal(c, argc, argv, false);
+}
+
+static int net_registry_getvalueraw(struct net_context *c, int argc,
+                                   const char **argv)
+{
+       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)
 {
        WERROR werr;
        struct registry_value value;
@@ -268,7 +365,7 @@ static int net_registry_setvalue(int argc, const char **argv)
        int ret = -1;
        TALLOC_CTX *ctx = talloc_stackframe();
 
-       if (argc < 4) {
+       if (argc < 4 || c->display_usage) {
                d_fprintf(stderr, "usage: net rpc registry setvalue <key> "
                          "<valuename> <type> [<val>]+\n");
                goto done;
@@ -286,6 +383,10 @@ static int net_registry_setvalue(int argc, const char **argv)
                value.type = REG_SZ;
                value.v.sz.len = strlen(argv[3])+1;
                value.v.sz.str = CONST_DISCARD(char *, argv[3]);
+       } else if (strequal(argv[2], "multi_sz")) {
+               value.type = REG_MULTI_SZ;
+               value.v.multi_sz.num_strings = argc - 3;
+               value.v.multi_sz.strings = (char **)(argv + 3);
        } else {
                d_fprintf(stderr, "type \"%s\" not implemented\n", argv[2]);
                goto done;
@@ -293,14 +394,14 @@ static int net_registry_setvalue(int argc, const char **argv)
 
        werr = open_key(ctx, argv[0], REG_KEY_WRITE, &key);
        if (!W_ERROR_IS_OK(werr)) {
-               d_fprintf(stderr, "open_key failed: %s\n", dos_errstr(werr));
+               d_fprintf(stderr, "open_key failed: %s\n", win_errstr(werr));
                goto done;
        }
 
        werr = reg_setvalue(key, argv[1], &value);
        if (!W_ERROR_IS_OK(werr)) {
                d_fprintf(stderr, "reg_setvalue failed: %s\n",
-                         dos_errstr(werr));
+                         win_errstr(werr));
                goto done;
        }
 
@@ -311,14 +412,111 @@ done:
        return ret;
 }
 
-static int net_registry_deletevalue(int argc, const char **argv)
+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;
+
+       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",
+                         reg_type_lookup(value->type));
+               goto done;
+       }
+
+       value->v.dword += state->increment;
+       state->newvalue = value->v.dword;
+
+       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),
+                          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("new value: %u\n", (unsigned)state.newvalue);
+
+       ret = 0;
+
+done:
+       return ret;
+}
+
+static int net_registry_deletevalue(struct net_context *c, int argc,
+                                   const char **argv)
 {
        WERROR werr;
        struct registry_key *key = NULL;
        TALLOC_CTX *ctx = talloc_stackframe();
        int ret = -1;
 
-       if (argc != 2) {
+       if (argc != 2 || c->display_usage) {
                d_fprintf(stderr, "usage: net rpc registry deletevalue <key> "
                          "<valuename>\n");
                goto done;
@@ -326,14 +524,14 @@ static int net_registry_deletevalue(int argc, const char **argv)
 
        werr = open_key(ctx, argv[0], REG_KEY_WRITE, &key);
        if (!W_ERROR_IS_OK(werr)) {
-               d_fprintf(stderr, "open_key failed: %s\n", dos_errstr(werr));
+               d_fprintf(stderr, "open_key failed: %s\n", win_errstr(werr));
                goto done;
        }
 
        werr = reg_deletevalue(key, argv[1]);
        if (!W_ERROR_IS_OK(werr)) {
                d_fprintf(stderr, "reg_deletekey failed: %s\n",
-                         dos_errstr(werr));
+                         win_errstr(werr));
                goto done;
        }
 
@@ -344,48 +542,181 @@ done:
        return ret;
 }
 
-static int net_registry_getsd(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_RIGHT_MAXIMUM_ALLOWED |
-                              SEC_RIGHT_SYSTEM_SECURITY;
+                              SEC_FLAG_MAXIMUM_ALLOWED |
+                              SEC_FLAG_SYSTEM_SECURITY;
 
        /*
-        * net_rpc_regsitry uses SEC_RIGHT_SYSTEM_SECURITY, but access
+        * net_rpc_regsitry uses SEC_FLAG_SYSTEM_SECURITY, but access
         * is denied with these perms right now...
         */
        access_mask = REG_KEY_READ;
 
-       if (argc != 1) {
+       if (sd == NULL) {
+               d_fprintf(stderr, "internal error: invalid argument\n");
+               werr = WERR_INVALID_PARAM;
+               goto done;
+       }
+
+       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, "open_key failed: %s\n", win_errstr(werr));
+               goto done;
+       }
+
+       werr = reg_getkeysecurity(mem_ctx, key, sd);
+       if (!W_ERROR_IS_OK(werr)) {
+               d_fprintf(stderr, "reg_getkeysecurity failed: %s\n",
+                         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("Usage:    net registry getsd <path>\n");
                d_printf("Example:  net registry getsd "
                         "'HKLM\\Software\\Samba'\n");
                goto done;
        }
-       if (strlen(argv[0]) == 0) {
+
+       werr = net_registry_getsd_internal(c, ctx, argv[0], &secdesc);
+       if (!W_ERROR_IS_OK(werr)) {
+               goto done;
+       }
+
+       display_sec_desc(secdesc);
+
+       ret = 0;
+
+done:
+       TALLOC_FREE(ctx);
+       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("Usage: net registry getsd_sddl <path>\n");
+               d_printf("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, 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", dos_errstr(werr));
+               d_fprintf(stderr, "open_key failed: %s\n", win_errstr(werr));
                goto done;
        }
 
-       werr = reg_getkeysecurity(ctx, key, &secdesc);
+       werr = reg_setkeysecurity(key, sd);
        if (!W_ERROR_IS_OK(werr)) {
-               d_fprintf(stderr, "reg_getkeysecurity failed: %s\n",
-                         dos_errstr(werr));
+               d_fprintf(stderr, "reg_setkeysecurity failed: %s\n",
+                         win_errstr(werr));
                goto done;
        }
 
-       display_sec_desc(secdesc);
+       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("Usage: net registry setsd_sddl <path> <security_descriptor>\n");
+               d_printf("Example:\n"
+                        "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;
 
@@ -394,51 +725,315 @@ done:
        return ret;
 }
 
-int net_registry(int argc, const char **argv)
+/******************************************************************************/
+/**
+ * @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_FOOBAR;
+               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(mem_ctx, 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_deletekey 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;
+}
+/**@}*/
+
+/******************************************************************************/
+
+int net_registry(struct net_context *c, int argc, const char **argv)
 {
        int ret = -1;
 
-       struct functable2 func[] = {
+       struct functable func[] = {
                {
                        "enumerate",
                        net_registry_enumerate,
-                       "Enumerate registry keys and values"
+                       NET_TRANSPORT_LOCAL,
+                       "Enumerate registry keys and values",
+                       "net registry enumerate\n"
+                       "    Enumerate registry keys and values"
                },
                {
                        "createkey",
                        net_registry_createkey,
-                       "Create a new registry key"
+                       NET_TRANSPORT_LOCAL,
+                       "Create a new registry key",
+                       "net registry createkey\n"
+                       "    Create a new registry key"
                },
                {
                        "deletekey",
                        net_registry_deletekey,
-                       "Delete a registry key"
+                       NET_TRANSPORT_LOCAL,
+                       "Delete a registry key",
+                       "net registry deletekey\n"
+                       "    Delete a registry key"
+               },
+               {
+                       "getvalue",
+                       net_registry_getvalue,
+                       NET_TRANSPORT_LOCAL,
+                       "Print a registry value",
+                       "net registry getvalue\n"
+                       "    Print a registry value"
+               },
+               {
+                       "getvalueraw",
+                       net_registry_getvalueraw,
+                       NET_TRANSPORT_LOCAL,
+                       "Print a registry value (raw format)",
+                       "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,
-                       "Set a new registry value"
+                       NET_TRANSPORT_LOCAL,
+                       "Set a new registry value",
+                       "net registry setvalue\n"
+                       "    Set a new registry value"
+               },
+               {
+                       "increment",
+                       net_registry_increment,
+                       NET_TRANSPORT_LOCAL,
+                       "Increment a DWORD registry value under a lock",
+                       "net registry increment\n"
+                       "    Increment a DWORD registry value under a lock"
                },
                {
                        "deletevalue",
                        net_registry_deletevalue,
-                       "Delete a registry value"
+                       NET_TRANSPORT_LOCAL,
+                       "Delete a registry value",
+                       "net registry deletevalue\n"
+                       "    Delete a registry value"
                },
                {
                        "getsd",
                        net_registry_getsd,
-                       "Get security descriptor"
+                       NET_TRANSPORT_LOCAL,
+                       "Get security descriptor",
+                       "net registry getsd\n"
+                       "    Get security descriptor"
+               },
+               {
+                       "getsd_sddl",
+                       net_registry_getsd_sddl,
+                       NET_TRANSPORT_LOCAL,
+                       "Get security descriptor in sddl format",
+                       "net registry getsd_sddl\n"
+                       "    Get security descriptor in sddl format"
+               },
+               {
+                       "setsd_sddl",
+                       net_registry_setsd_sddl,
+                       NET_TRANSPORT_LOCAL,
+                       "Set security descriptor from sddl format string",
+                       "net registry setsd_sddl\n"
+                       "    Set security descriptor from sddl format string"
                },
-       { NULL, NULL, NULL }
+               {
+                       "import",
+                       net_registry_import,
+                       NET_TRANSPORT_LOCAL,
+                       "Import .reg file",
+                       "net registry import\n"
+                       "    Import .reg file"
+               },
+       { NULL, NULL, 0, NULL, NULL }
        };
 
-       if (!registry_init_basic()) {
+       if (!W_ERROR_IS_OK(registry_init_basic())) {
                return -1;
        }
 
-       ret = net_run_function2(argc, argv, "net registry", func);
-
-       regdb_close();
+       ret = net_run_function(c, argc, argv, "net registry", func);
 
        return ret;
 }