s3: Fix an uninitialized variable
[Samba/gebeck_regimport.git] / source3 / utils / net_rpc_shell.c
blobb1de4d24c50d8a426d5fc0dc2ae68435113c8018
1 /*
2 * Unix SMB/CIFS implementation.
3 * Shell around net rpc subcommands
4 * Copyright (C) Volker Lendecke 2006
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
22 #include "utils/net.h"
23 #include "../librpc/gen_ndr/ndr_samr.h"
25 static NTSTATUS rpc_sh_info(struct net_context *c,
26 TALLOC_CTX *mem_ctx, struct rpc_sh_ctx *ctx,
27 struct rpc_pipe_client *pipe_hnd,
28 int argc, const char **argv)
30 return rpc_info_internals(c, ctx->domain_sid, ctx->domain_name,
31 ctx->cli, pipe_hnd, mem_ctx,
32 argc, argv);
35 static struct rpc_sh_ctx *this_ctx;
37 static char **completion_fn(const char *text, int start, int end)
39 char **cmds = NULL;
40 int n_cmds = 0;
41 struct rpc_sh_cmd *c;
43 if (start != 0) {
44 return NULL;
47 ADD_TO_ARRAY(NULL, char *, SMB_STRDUP(text), &cmds, &n_cmds);
49 for (c = this_ctx->cmds; c->name != NULL; c++) {
50 bool match = (strncmp(text, c->name, strlen(text)) == 0);
52 if (match) {
53 ADD_TO_ARRAY(NULL, char *, SMB_STRDUP(c->name),
54 &cmds, &n_cmds);
58 if (n_cmds == 2) {
59 SAFE_FREE(cmds[0]);
60 cmds[0] = cmds[1];
61 n_cmds -= 1;
64 ADD_TO_ARRAY(NULL, char *, NULL, &cmds, &n_cmds);
65 return cmds;
68 static NTSTATUS net_sh_run(struct net_context *c,
69 struct rpc_sh_ctx *ctx, struct rpc_sh_cmd *cmd,
70 int argc, const char **argv)
72 TALLOC_CTX *mem_ctx;
73 struct rpc_pipe_client *pipe_hnd = NULL;
74 NTSTATUS status;
76 mem_ctx = talloc_new(ctx);
77 if (mem_ctx == NULL) {
78 d_fprintf(stderr, _("talloc_new failed\n"));
79 return NT_STATUS_NO_MEMORY;
82 status = cli_rpc_pipe_open_noauth(ctx->cli, cmd->interface,
83 &pipe_hnd);
84 if (!NT_STATUS_IS_OK(status)) {
85 d_fprintf(stderr, _("Could not open pipe: %s\n"),
86 nt_errstr(status));
87 return status;
90 status = cmd->fn(c, mem_ctx, ctx, pipe_hnd, argc, argv);
92 TALLOC_FREE(pipe_hnd);
94 talloc_destroy(mem_ctx);
96 return status;
99 static bool net_sh_process(struct net_context *c,
100 struct rpc_sh_ctx *ctx,
101 int argc, const char **argv)
103 struct rpc_sh_cmd *cmd;
104 struct rpc_sh_ctx *new_ctx;
105 NTSTATUS status;
107 if (argc == 0) {
108 return true;
111 if (ctx == this_ctx) {
113 /* We've been called from the cmd line */
114 if (strequal(argv[0], "..") &&
115 (this_ctx->parent != NULL)) {
116 new_ctx = this_ctx->parent;
117 TALLOC_FREE(this_ctx);
118 this_ctx = new_ctx;
119 return true;
123 if (strequal(argv[0], "exit") ||
124 strequal(argv[0], "quit") ||
125 strequal(argv[0], "q")) {
126 return false;
129 if (strequal(argv[0], "help") || strequal(argv[0], "?")) {
130 for (cmd = ctx->cmds; cmd->name != NULL; cmd++) {
131 if (ctx != this_ctx) {
132 d_printf("%s ", ctx->whoami);
134 d_printf("%-15s %s\n", cmd->name, cmd->help);
136 return true;
139 for (cmd = ctx->cmds; cmd->name != NULL; cmd++) {
140 if (strequal(cmd->name, argv[0])) {
141 break;
145 if (cmd->name == NULL) {
146 /* None found */
147 d_fprintf(stderr,_( "%s: unknown cmd\n"), argv[0]);
148 return true;
151 new_ctx = TALLOC_P(ctx, struct rpc_sh_ctx);
152 if (new_ctx == NULL) {
153 d_fprintf(stderr, _("talloc failed\n"));
154 return false;
156 new_ctx->cli = ctx->cli;
157 new_ctx->whoami = talloc_asprintf(new_ctx, "%s %s",
158 ctx->whoami, cmd->name);
159 new_ctx->thiscmd = talloc_strdup(new_ctx, cmd->name);
161 if (cmd->sub != NULL) {
162 new_ctx->cmds = cmd->sub(c, new_ctx, ctx);
163 } else {
164 new_ctx->cmds = NULL;
167 new_ctx->parent = ctx;
168 new_ctx->domain_name = ctx->domain_name;
169 new_ctx->domain_sid = ctx->domain_sid;
171 argc -= 1;
172 argv += 1;
174 if (cmd->sub != NULL) {
175 if (argc == 0) {
176 this_ctx = new_ctx;
177 return true;
179 return net_sh_process(c, new_ctx, argc, argv);
182 status = net_sh_run(c, new_ctx, cmd, argc, argv);
184 if (!NT_STATUS_IS_OK(status)) {
185 d_fprintf(stderr, _("%s failed: %s\n"), new_ctx->whoami,
186 nt_errstr(status));
189 return true;
192 static struct rpc_sh_cmd sh_cmds[6] = {
194 { "info", NULL, &ndr_table_samr.syntax_id, rpc_sh_info,
195 N_("Print information about the domain connected to") },
197 { "rights", net_rpc_rights_cmds, 0, NULL,
198 N_("List/Grant/Revoke user rights") },
200 { "share", net_rpc_share_cmds, 0, NULL,
201 N_("List/Add/Remove etc shares") },
203 { "user", net_rpc_user_cmds, 0, NULL,
204 N_("List/Add/Remove user info") },
206 { "account", net_rpc_acct_cmds, 0, NULL,
207 N_("Show/Change account policy settings") },
209 { NULL, NULL, 0, NULL, NULL }
212 int net_rpc_shell(struct net_context *c, int argc, const char **argv)
214 NTSTATUS status;
215 struct rpc_sh_ctx *ctx;
217 if (argc != 0 || c->display_usage) {
218 d_printf("%s\nnet rpc shell\n", _("Usage:"));
219 return -1;
222 if (libnetapi_init(&c->netapi_ctx) != 0) {
223 return -1;
225 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
226 libnetapi_set_password(c->netapi_ctx, c->opt_password);
227 if (c->opt_kerberos) {
228 libnetapi_set_use_kerberos(c->netapi_ctx);
231 ctx = TALLOC_P(NULL, struct rpc_sh_ctx);
232 if (ctx == NULL) {
233 d_fprintf(stderr, _("talloc failed\n"));
234 return -1;
237 status = net_make_ipc_connection(c, 0, &(ctx->cli));
238 if (!NT_STATUS_IS_OK(status)) {
239 d_fprintf(stderr, _("Could not open connection: %s\n"),
240 nt_errstr(status));
241 return -1;
244 ctx->cmds = sh_cmds;
245 ctx->whoami = "net rpc";
246 ctx->parent = NULL;
248 status = net_get_remote_domain_sid(ctx->cli, ctx, &ctx->domain_sid,
249 &ctx->domain_name);
250 if (!NT_STATUS_IS_OK(status)) {
251 return -1;
254 d_printf(_("Talking to domain %s (%s)\n"), ctx->domain_name,
255 sid_string_tos(ctx->domain_sid));
257 this_ctx = ctx;
259 while(1) {
260 char *prompt = NULL;
261 char *line = NULL;
262 int ret;
264 if (asprintf(&prompt, "%s> ", this_ctx->whoami) < 0) {
265 break;
268 line = smb_readline(prompt, NULL, completion_fn);
269 SAFE_FREE(prompt);
271 if (line == NULL) {
272 break;
275 ret = poptParseArgvString(line, &argc, &argv);
276 if (ret == POPT_ERROR_NOARG) {
277 SAFE_FREE(line);
278 continue;
280 if (ret != 0) {
281 d_fprintf(stderr, _("cmdline invalid: %s\n"),
282 poptStrerror(ret));
283 SAFE_FREE(line);
284 return false;
287 if ((line[0] != '\n') &&
288 (!net_sh_process(c, this_ctx, argc, argv))) {
289 SAFE_FREE(line);
290 break;
292 SAFE_FREE(line);
295 cli_shutdown(ctx->cli);
297 TALLOC_FREE(ctx);
299 return 0;