s3:net: registry: add new command enumerate_recursive
authorGregor Beck <gbeck@sernet.de>
Mon, 30 May 2011 06:58:34 +0000 (08:58 +0200)
committerKarolin Seeger <kseeger@samba.org>
Tue, 21 Jun 2011 17:57:49 +0000 (19:57 +0200)
Signed-off-by: Michael Adam <obnox@samba.org>
(cherry picked from commit 22011ddc9e72c8a201e3ca6e01745a68738d5916)

Fix bug: #8193

source3/utils/net_registry.c

index dcc88a95d06d32f342bda11626b1766c411e7528..e1267c37456c15b3acb570ba29f530b9ac3511be 100644 (file)
@@ -185,6 +185,105 @@ done:
        return ret;
 }
 
+static WERROR registry_enumkey(struct registry_key* parent, const char* keyname, bool recursive)
+{
+       WERROR werr;
+       TALLOC_CTX *ctx = talloc_stackframe();
+       char*  subkey_name;
+       NTTIME modtime;
+       uint32_t count;
+       char* valname = NULL;
+       struct registry_value *valvalue = NULL;
+       struct registry_key* key = NULL;
+
+       werr = reg_openkey(ctx, parent, keyname, REG_KEY_READ, &key);
+       if (!W_ERROR_IS_OK(werr)) {
+               goto done;
+       }
+
+       printf("[%s]\n", key->key->name);
+
+       for (count = 0;
+            werr = reg_enumkey(ctx, key, count, &subkey_name, &modtime),
+            W_ERROR_IS_OK(werr);
+            count++)
+       {
+               print_registry_key(subkey_name, &modtime);
+       }
+       if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
+               goto done;
+       }
+
+       for (count = 0;
+            werr = reg_enumvalue(ctx, key, count, &valname, &valvalue),
+            W_ERROR_IS_OK(werr);
+            count++)
+       {
+               print_registry_value_with_name(valname, valvalue);
+       }
+       if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
+               goto done;
+       }
+
+       if (!recursive) {
+               werr = WERR_OK;
+               goto done;
+       }
+
+       for (count = 0;
+            werr = reg_enumkey(ctx, key, count, &subkey_name, &modtime),
+            W_ERROR_IS_OK(werr);
+            count++)
+       {
+               werr = registry_enumkey(key, subkey_name, recursive);
+               if (!W_ERROR_IS_OK(werr)) {
+                       goto done;
+               }
+       }
+       if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
+               goto done;
+       }
+       werr = WERR_OK;
+done:
+       TALLOC_FREE(ctx);
+       return werr;
+}
+
+static int net_registry_enumerate_recursive(struct net_context *c, int argc,
+                                           const char **argv)
+{
+       WERROR werr;
+       struct registry_key *key = NULL;
+       char* name = NULL;
+       TALLOC_CTX *ctx = talloc_stackframe();
+       int ret = -1;
+
+       if (argc != 1 || c->display_usage) {
+               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;
+       }
+
+       werr = open_hive(ctx, argv[0], REG_KEY_READ, &key, &name);
+       if (!W_ERROR_IS_OK(werr)) {
+               d_fprintf(stderr, _("open_key failed: %s\n"), win_errstr(werr));
+               goto done;
+       }
+
+       werr = registry_enumkey(key, name, true);
+       if (W_ERROR_IS_OK(werr)) {
+               ret = 0;
+       }
+done:
+       TALLOC_FREE(ctx);
+       return ret;
+}
+
+
 static int net_registry_createkey(struct net_context *c, int argc,
                                  const char **argv)
 {
@@ -1175,6 +1274,14 @@ int net_registry(struct net_context *c, int argc, const char **argv)
                        N_("net registry enumerate\n"
                           "    Enumerate registry keys and values")
                },
+               {
+                       "enumerate_recursive",
+                       net_registry_enumerate_recursive,
+                       NET_TRANSPORT_LOCAL,
+                       N_("Enumerate registry keys and values"),
+                       N_("net registry enumerate_recursive\n"
+                          "    Enumerate registry keys and values")
+               },
                {
                        "createkey",
                        net_registry_createkey,