s3 net: i18n support for net rpc shell
[kai/samba.git] / source3 / utils / net_rpc_shell.c
index 2e1f65fe6c4104237c3e6a9beaa1fe604c8ac568..e78af142fa15b6f72606c8f37734925407276b93 100644 (file)
@@ -5,7 +5,7 @@
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
+ *  the Free Software Foundation; either version 3 of the License, or
  *  (at your option) any later version.
  *
  *  This program is distributed in the hope that it will be useful,
  *  GNU General Public License for more details.
  *
  *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *  along with this program; if not, see <http://www.gnu.org/licenses/>.
  */
 
 
 #include "includes.h"
 #include "utils/net.h"
 
-static struct rpc_sh_cmd sh_cmds[];
-
-static NTSTATUS rpc_sh_info(TALLOC_CTX *mem_ctx, struct rpc_sh_ctx *ctx,
+static NTSTATUS rpc_sh_info(struct net_context *c,
+                           TALLOC_CTX *mem_ctx, struct rpc_sh_ctx *ctx,
                            struct rpc_pipe_client *pipe_hnd,
                            int argc, const char **argv)
 {
-       return rpc_info_internals(ctx->domain_sid, ctx->domain_name,
+       return rpc_info_internals(c, ctx->domain_sid, ctx->domain_name,
                                  ctx->cli, pipe_hnd, mem_ctx,
                                  argc, argv);
 }
@@ -48,7 +46,7 @@ static char **completion_fn(const char *text, int start, int end)
        ADD_TO_ARRAY(NULL, char *, SMB_STRDUP(text), &cmds, &n_cmds);
 
        for (c = this_ctx->cmds; c->name != NULL; c++) {
-               BOOL match = (strncmp(text, c->name, strlen(text)) == 0);
+               bool match = (strncmp(text, c->name, strlen(text)) == 0);
 
                if (match) {
                        ADD_TO_ARRAY(NULL, char *, SMB_STRDUP(c->name),
@@ -66,7 +64,8 @@ static char **completion_fn(const char *text, int start, int end)
        return cmds;
 }
 
-static NTSTATUS net_sh_run(struct rpc_sh_ctx *ctx, struct rpc_sh_cmd *cmd,
+static NTSTATUS net_sh_run(struct net_context *c,
+                          struct rpc_sh_ctx *ctx, struct rpc_sh_cmd *cmd,
                           int argc, const char **argv)
 {
        TALLOC_CTX *mem_ctx;
@@ -75,35 +74,37 @@ static NTSTATUS net_sh_run(struct rpc_sh_ctx *ctx, struct rpc_sh_cmd *cmd,
 
        mem_ctx = talloc_new(ctx);
        if (mem_ctx == NULL) {
-               d_fprintf(stderr, "talloc_new failed\n");
+               d_fprintf(stderr, _("talloc_new failed\n"));
                return NT_STATUS_NO_MEMORY;
        }
 
-       pipe_hnd = cli_rpc_pipe_open_noauth(ctx->cli, cmd->pipe_idx, &status);
-       if (pipe_hnd == NULL) {
-               d_fprintf(stderr, "Could not open pipe: %s\n",
+       status = cli_rpc_pipe_open_noauth(ctx->cli, cmd->interface,
+                                         &pipe_hnd);
+       if (!NT_STATUS_IS_OK(status)) {
+               d_fprintf(stderr, _("Could not open pipe: %s\n"),
                          nt_errstr(status));
                return status;
        }
 
-       status = cmd->fn(mem_ctx, ctx, pipe_hnd, argc, argv);
+       status = cmd->fn(c, mem_ctx, ctx, pipe_hnd, argc, argv);
 
-       cli_rpc_pipe_close(pipe_hnd);
+       TALLOC_FREE(pipe_hnd);
 
        talloc_destroy(mem_ctx);
 
        return status;
 }
 
-static BOOL net_sh_process(struct rpc_sh_ctx *ctx,
+static bool net_sh_process(struct net_context *c,
+                          struct rpc_sh_ctx *ctx,
                           int argc, const char **argv)
 {
-       struct rpc_sh_cmd *c;
+       struct rpc_sh_cmd *cmd;
        struct rpc_sh_ctx *new_ctx;
        NTSTATUS status;
 
        if (argc == 0) {
-               return True;
+               return true;
        }
 
        if (ctx == this_ctx) {
@@ -112,46 +113,50 @@ static BOOL net_sh_process(struct rpc_sh_ctx *ctx,
                if (strequal(argv[0], "..") &&
                    (this_ctx->parent != NULL)) {
                        new_ctx = this_ctx->parent;
-                       talloc_free(this_ctx);
+                       TALLOC_FREE(this_ctx);
                        this_ctx = new_ctx;
-                       return True;
+                       return true;
                }
        }
 
+       if (strequal(argv[0], "exit") || strequal(argv[0], "quit")) {
+               return false;
+       }
+
        if (strequal(argv[0], "help") || strequal(argv[0], "?")) {
-               for (c = ctx->cmds; c->name != NULL; c++) {
+               for (cmd = ctx->cmds; cmd->name != NULL; cmd++) {
                        if (ctx != this_ctx) {
                                d_printf("%s ", ctx->whoami);
                        }
-                       d_printf("%-15s %s\n", c->name, c->help);
+                       d_printf("%-15s %s\n", cmd->name, cmd->help);
                }
-               return True;
+               return true;
        }
 
-       for (c = ctx->cmds; c->name != NULL; c++) {
-               if (strequal(c->name, argv[0])) {
+       for (cmd = ctx->cmds; cmd->name != NULL; cmd++) {
+               if (strequal(cmd->name, argv[0])) {
                        break;
                }
        }
 
-       if (c->name == NULL) {
+       if (cmd->name == NULL) {
                /* None found */
-               d_fprintf(stderr, "%s: unknown cmd\n", argv[0]);
-               return True;
+               d_fprintf(stderr,_( "%s: unknown cmd\n"), argv[0]);
+               return true;
        }
 
        new_ctx = TALLOC_P(ctx, struct rpc_sh_ctx);
        if (new_ctx == NULL) {
-               d_fprintf(stderr, "talloc failed\n");
-               return False;
+               d_fprintf(stderr, _("talloc failed\n"));
+               return false;
        }
        new_ctx->cli = ctx->cli;
        new_ctx->whoami = talloc_asprintf(new_ctx, "%s %s",
-                                         ctx->whoami, c->name);
-       new_ctx->thiscmd = talloc_strdup(new_ctx, c->name);
+                                         ctx->whoami, cmd->name);
+       new_ctx->thiscmd = talloc_strdup(new_ctx, cmd->name);
 
-       if (c->sub != NULL) {
-               new_ctx->cmds = c->sub(new_ctx, ctx);
+       if (cmd->sub != NULL) {
+               new_ctx->cmds = cmd->sub(c, new_ctx, ctx);
        } else {
                new_ctx->cmds = NULL;
        }
@@ -163,43 +168,74 @@ static BOOL net_sh_process(struct rpc_sh_ctx *ctx,
        argc -= 1;
        argv += 1;
 
-       if (c->sub != NULL) {
+       if (cmd->sub != NULL) {
                if (argc == 0) {
                        this_ctx = new_ctx;
-                       return True;
+                       return true;
                }
-               return net_sh_process(new_ctx, argc, argv);
+               return net_sh_process(c, new_ctx, argc, argv);
        }
 
-       status = net_sh_run(new_ctx, c, argc, argv);
+       status = net_sh_run(c, new_ctx, cmd, argc, argv);
 
        if (!NT_STATUS_IS_OK(status)) {
-               d_fprintf(stderr, "%s failed: %s\n", new_ctx->whoami,
+               d_fprintf(stderr, _("%s failed: %s\n"), new_ctx->whoami,
                          nt_errstr(status));
        }
 
-       return True;
+       return true;
 }
 
-int net_rpc_shell(int argc, const char **argv)
+static struct rpc_sh_cmd sh_cmds[6] = {
+
+       { "info", NULL, &ndr_table_samr.syntax_id, rpc_sh_info,
+         N_("Print information about the domain connected to") },
+
+       { "rights", net_rpc_rights_cmds, 0, NULL,
+         N_("List/Grant/Revoke user rights") },
+
+       { "share", net_rpc_share_cmds, 0, NULL,
+         N_("List/Add/Remove etc shares") },
+
+       { "user", net_rpc_user_cmds, 0, NULL,
+         N_("List/Add/Remove user info") },
+
+       { "account", net_rpc_acct_cmds, 0, NULL,
+         N_("Show/Change account policy settings") },
+
+       { NULL, NULL, 0, NULL, NULL }
+};
+
+int net_rpc_shell(struct net_context *c, int argc, const char **argv)
 {
        NTSTATUS status;
        struct rpc_sh_ctx *ctx;
 
-       if (argc != 0) {
-               d_fprintf(stderr, "usage: net rpc shell\n");
+       if (argc != 0 || c->display_usage) {
+               d_printf(_("Usage:\n"
+                          "net rpc shell\n"));
+               return -1;
+       }
+
+       if (libnetapi_init(&c->netapi_ctx) != 0) {
                return -1;
        }
+       libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
+       libnetapi_set_password(c->netapi_ctx, c->opt_password);
+       if (c->opt_kerberos) {
+               libnetapi_set_use_kerberos(c->netapi_ctx);
+       }
 
        ctx = TALLOC_P(NULL, struct rpc_sh_ctx);
        if (ctx == NULL) {
-               d_fprintf(stderr, "talloc failed\n");
+               d_fprintf(stderr, _("talloc failed\n"));
                return -1;
        }
 
-       ctx->cli = net_make_ipc_connection(0);
-       if (ctx->cli == NULL) {
-               d_fprintf(stderr, "Could not open connection\n");
+       status = net_make_ipc_connection(c, 0, &(ctx->cli));
+       if (!NT_STATUS_IS_OK(status)) {
+               d_fprintf(stderr, _("Could not open connection: %s\n"),
+                         nt_errstr(status));
                return -1;
        }
 
@@ -213,17 +249,19 @@ int net_rpc_shell(int argc, const char **argv)
                return -1;
        }
 
-       d_printf("Talking to domain %s (%s)\n", ctx->domain_name,
-                sid_string_static(ctx->domain_sid));
-       
+       d_printf(_("Talking to domain %s (%s)\n"), ctx->domain_name,
+                sid_string_tos(ctx->domain_sid));
+
        this_ctx = ctx;
 
        while(1) {
-               char *prompt;
-               char *line;
+               char *prompt = NULL;
+               char *line = NULL;
                int ret;
 
-               asprintf(&prompt, "%s> ", this_ctx->whoami);
+               if (asprintf(&prompt, "%s> ", this_ctx->whoami) < 0) {
+                       break;
+               }
 
                line = smb_readline(prompt, NULL, completion_fn);
                SAFE_FREE(prompt);
@@ -233,38 +271,28 @@ int net_rpc_shell(int argc, const char **argv)
                }
 
                ret = poptParseArgvString(line, &argc, &argv);
+               if (ret == POPT_ERROR_NOARG) {
+                       SAFE_FREE(line);
+                       continue;
+               }
                if (ret != 0) {
-                       d_fprintf(stderr, "cmdline invalid: %s\n",
+                       d_fprintf(stderr, _("cmdline invalid: %s\n"),
                                  poptStrerror(ret));
-                       return False;
+                       SAFE_FREE(line);
+                       return false;
                }
 
                if ((line[0] != '\n') &&
-                   (!net_sh_process(this_ctx, argc, argv))) {
+                   (!net_sh_process(c, this_ctx, argc, argv))) {
+                       SAFE_FREE(line);
                        break;
                }
+               SAFE_FREE(line);
        }
 
        cli_shutdown(ctx->cli);
 
-       talloc_free(ctx);
+       TALLOC_FREE(ctx);
 
        return 0;
 }
-
-static struct rpc_sh_cmd sh_cmds[5] = {
-
-       { "info", NULL, PI_SAMR, rpc_sh_info,
-         "Print information about the domain connected to" },
-
-       { "rights", net_rpc_rights_cmds, 0, NULL,
-         "List/Grant/Revoke user rights" },
-
-       { "share", net_rpc_share_cmds, 0, NULL,
-         "List/Add/Remove etc shares" },
-
-       { "user", net_rpc_user_cmds, 0, NULL,
-         "List/Add/Remove user info" },
-
-       { NULL, NULL, 0, NULL, NULL }
-};