s3:net: refactor getting of secdesc out of net_registry_getsd()
[amitay/samba.git] / source3 / utils / net_registry.c
index 3d245007749f8292463917d368b482e476be552d..2a13ebd6bd3247a8aa39fadc321d0cf351680ed7 100644 (file)
@@ -21,6 +21,7 @@
 
 #include "includes.h"
 #include "utils/net.h"
+#include "utils/net_registry_util.h"
 
 
 /*
  *
  */
 
-static void print_registry_key(const char *keyname, NTTIME *modtime)
-{
-       d_printf("Keyname   = %s\n", keyname);
-       d_printf("Modtime   = %s\n",
-                modtime
-                ? http_timestring(nt_time_to_unix(*modtime))
-                : "None");
-       d_printf("\n");
-}
-
-static void print_registry_value(const char *valname,
-                                const struct registry_value *valvalue)
-{
-       d_printf("Valuename  = %s\n", valname);
-       d_printf("Type       = %s\n",
-                reg_type_lookup(valvalue->type));
-       switch(valvalue->type) {
-       case REG_DWORD:
-               d_printf("Value      = %d\n", valvalue->v.dword);
-               break;
-       case REG_SZ:
-       case REG_EXPAND_SZ:
-               d_printf("Value      = \"%s\"\n", valvalue->v.sz.str);
-               break;
-       case REG_MULTI_SZ: {
-               uint32 j;
-               for (j = 0; j < valvalue->v.multi_sz.num_strings; j++) {
-                       d_printf("Value[%3.3d] = \"%s\"\n", j,
-                                valvalue->v.multi_sz.strings[j]);
-               }
-               break;
-       }
-       case REG_BINARY:
-               d_printf("Value      = %d bytes\n",
-                        (int)valvalue->v.binary.length);
-               break;
-       default:
-               d_printf("Value      = <unprintable>\n");
-               break;
-       }
-       d_printf("\n");
-}
-
-/**
- * Split path into hive name and subkeyname
- * normalizations performed:
- *  - convert '/' to '\\'
- *  - strip trailing '\\' chars
- */
-static WERROR split_hive_key(TALLOC_CTX *ctx, const char *path,
-                            char **hivename, const char **subkeyname)
-{
-       char *p;
-
-       if ((path == NULL) || (hivename == NULL) || (subkeyname == NULL)) {
-               return WERR_INVALID_PARAM;
-       }
-
-       if (strlen(path) == 0) {
-               return WERR_INVALID_PARAM;
-       }
-
-       *hivename = talloc_string_sub(ctx, path, "/", "\\");
-       if (*hivename == NULL) {
-               return WERR_NOMEM;
-       }
-
-       /* strip trailing '\\' chars */
-       p = strrchr(*hivename, '\\');
-       while ((p != NULL) && (p[1] == '\0')) {
-               *p = '\0';
-               p = strrchr(*hivename, '\\');
-       }
-
-       p = strchr(*hivename, '\\');
-
-       if ((p == NULL) || (*p == '\0')) {
-               /* just the hive - no subkey given */
-               *subkeyname = "";
-       } else {
-               *p = '\0';
-               *subkeyname = p+1;
-       }
-
-       return WERR_OK;
-}
-
 /**
  * split given path into hive and remaining path and open the hive key
  */
@@ -127,7 +41,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)) {
@@ -177,14 +91,15 @@ 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));
+               d_fprintf(stderr, _("reg_openkey failed: %s\n"),
+                         win_errstr(werr));
                goto done;
        }
 
@@ -201,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;
@@ -213,16 +129,19 @@ static int net_registry_enumerate(int argc, const char **argv)
        struct registry_value *valvalue = NULL;
        int ret = -1;
 
-       if (argc != 1) {
-               d_printf("Usage:    net registry enumerate <path>\n");
-               d_printf("Example:  net registry enumerate "
-                        "'HKLM\\Software\\Samba'\n");
+       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_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;
        }
 
@@ -242,7 +161,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;
@@ -254,7 +173,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;
@@ -264,39 +184,44 @@ static int net_registry_createkey(int argc, const char **argv)
        TALLOC_CTX *ctx = talloc_stackframe();
        int ret = -1;
 
-       if (argc != 1) {
-               d_printf("Usage:    net registry createkey <path>\n");
-               d_printf("Example:  net registry createkey "
-                        "'HKLM\\Software\\Samba\\smbconf.127.0.0.1'\n");
+       if (argc != 1 || c->display_usage) {
+               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) {
-               d_fprintf(stderr, "error: zero length key name given\n");
+               d_fprintf(stderr, _("error: zero length key name given\n"));
                goto done;
        }
 
        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_createkey(ctx, hivekey, subkeyname, REG_KEY_WRITE,
                             &subkey, &action);
        if (!W_ERROR_IS_OK(werr)) {
-               d_fprintf(stderr, "reg_createkey failed: %s\n",
-                         dos_errstr(werr));
+               d_fprintf(stderr, _("reg_createkey failed: %s\n"),
+                         win_errstr(werr));
                goto done;
        }
        switch (action) {
                case REG_ACTION_NONE:
-                       d_printf("createkey did nothing -- huh?\n");
+                       d_printf(_("createkey did nothing -- huh?\n"));
                        break;
                case REG_CREATED_NEW_KEY:
-                       d_printf("createkey created %s\n", argv[0]);
+                       d_printf(_("createkey created %s\n"), argv[0]);
                        break;
                case REG_OPENED_EXISTING_KEY:
-                       d_printf("createkey opened existing %s\n", argv[0]);
+                       d_printf(_("createkey opened existing %s\n"), argv[0]);
                        break;
        }
 
@@ -307,7 +232,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;
@@ -315,27 +241,32 @@ static int net_registry_deletekey(int argc, const char **argv)
        TALLOC_CTX *ctx = talloc_stackframe();
        int ret = -1;
 
-       if (argc != 1) {
-               d_printf("Usage:    net registry deletekey <path>\n");
-               d_printf("Example:  net registry deletekey "
-                        "'HKLM\\Software\\Samba\\smbconf.127.0.0.1'\n");
+       if (argc != 1 || c->display_usage) {
+               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;
        }
        if (strlen(argv[0]) == 0) {
-               d_fprintf(stderr, "error: zero length key name given\n");
+               d_fprintf(stderr, _("error: zero length key name given\n"));
                goto done;
        }
 
        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 %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",
-                         dos_errstr(werr));
+               d_fprintf(stderr, "reg_deletekey %s: %s\n", _("failed"),
+                         win_errstr(werr));
                goto done;
        }
 
@@ -346,7 +277,58 @@ 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, "%s\n%s",
+                         _("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_setvalue(struct net_context *c, int argc,
+                                const char **argv)
 {
        WERROR werr;
        struct registry_value value;
@@ -354,14 +336,16 @@ static int net_registry_setvalue(int argc, const char **argv)
        int ret = -1;
        TALLOC_CTX *ctx = talloc_stackframe();
 
-       if (argc < 4) {
-               d_fprintf(stderr, "usage: net rpc registry setvalue <key> "
-                         "<valuename> <type> [<val>]+\n");
+       if (argc < 4 || c->display_usage) {
+               d_fprintf(stderr, "%s\n%s",
+                         _("Usage:"),
+                         _("net rpc registry setvalue <key> <valuename> "
+                           "<type> [<val>]+\n"));
                goto done;
        }
 
        if (!strequal(argv[2], "multi_sz") && (argc != 4)) {
-               d_fprintf(stderr, "Too many args for type %s\n", argv[2]);
+               d_fprintf(stderr, _("Too many args for type %s\n"), argv[2]);
                goto done;
        }
 
@@ -372,21 +356,25 @@ 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]);
+               d_fprintf(stderr, _("type \"%s\" not implemented\n"), argv[2]);
                goto done;
        }
 
        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));
+               d_fprintf(stderr, _("reg_setvalue failed: %s\n"),
+                         win_errstr(werr));
                goto done;
        }
 
@@ -397,29 +385,31 @@ done:
        return ret;
 }
 
-static int net_registry_deletevalue(int argc, const char **argv)
+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) {
-               d_fprintf(stderr, "usage: net rpc registry deletevalue <key> "
-                         "<valuename>\n");
+       if (argc != 2 || c->display_usage) {
+               d_fprintf(stderr, "%s\n%s",
+                         _("Usage:"),
+                         _("net rpc registry deletevalue <key> <valuename>\n"));
                goto done;
        }
 
        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));
+               d_fprintf(stderr, _("reg_deletekey failed: %s\n"),
+                         win_errstr(werr));
                goto done;
        }
 
@@ -430,44 +420,76 @@ 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) {
-               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) {
+
+       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_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("%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)) {
-               d_fprintf(stderr, "reg_getkeysecurity failed: %s\n",
-                         dos_errstr(werr));
                goto done;
        }
 
@@ -480,51 +502,83 @@ done:
        return ret;
 }
 
-int net_registry(int argc, const char **argv)
+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,
+                       N_("Enumerate registry keys and values"),
+                       N_("net registry enumerate\n"
+                          "    Enumerate registry keys and values")
                },
                {
                        "createkey",
                        net_registry_createkey,
-                       "Create a new registry key"
+                       NET_TRANSPORT_LOCAL,
+                       N_("Create a new registry key"),
+                       N_("net registry createkey\n"
+                          "    Create a new registry key")
                },
                {
                        "deletekey",
                        net_registry_deletekey,
-                       "Delete a registry key"
+                       NET_TRANSPORT_LOCAL,
+                       N_("Delete a registry key"),
+                       N_("net registry deletekey\n"
+                          "    Delete a registry key")
+               },
+               {
+                       "getvalue",
+                       net_registry_getvalue,
+                       NET_TRANSPORT_LOCAL,
+                       N_("Print a registry value"),
+                       N_("net registry getvalue\n"
+                          "    Print a registry value")
+               },
+               {
+                       "getvalueraw",
+                       net_registry_getvalueraw,
+                       NET_TRANSPORT_LOCAL,
+                       N_("Print a registry value (raw format)"),
+                       N_("net registry getvalueraw\n"
+                          "    Print a registry value (raw format)")
                },
                {
                        "setvalue",
                        net_registry_setvalue,
-                       "Set a new registry value"
+                       NET_TRANSPORT_LOCAL,
+                       N_("Set a new registry value"),
+                       N_("net registry setvalue\n"
+                          "    Set a new registry value")
                },
                {
                        "deletevalue",
                        net_registry_deletevalue,
-                       "Delete a registry value"
+                       NET_TRANSPORT_LOCAL,
+                       N_("Delete a registry value"),
+                       N_("net registry deletevalue\n"
+                          "    Delete a registry value")
                },
                {
                        "getsd",
                        net_registry_getsd,
-                       "Get security descriptor"
+                       NET_TRANSPORT_LOCAL,
+                       N_("Get security descriptor"),
+                       N_("net registry getsd\n"
+                          "    Get security descriptor")
                },
-       { NULL, NULL, NULL }
+       { 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;
 }