Refactoring: Change calling conventions for cli_rpc_pipe_open_noauth
[Samba/gbeck.git] / source / utils / net_rpc_shell.c
blob6abdb03c48d02ac882bcd21bdedccd78542c5de0
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"
24 static NTSTATUS rpc_sh_info(struct net_context *c,
25 TALLOC_CTX *mem_ctx, struct rpc_sh_ctx *ctx,
26 struct rpc_pipe_client *pipe_hnd,
27 int argc, const char **argv)
29 return rpc_info_internals(c, ctx->domain_sid, ctx->domain_name,
30 ctx->cli, pipe_hnd, mem_ctx,
31 argc, argv);
34 static struct rpc_sh_ctx *this_ctx;
36 static char **completion_fn(const char *text, int start, int end)
38 char **cmds = NULL;
39 int n_cmds = 0;
40 struct rpc_sh_cmd *c;
42 if (start != 0) {
43 return NULL;
46 ADD_TO_ARRAY(NULL, char *, SMB_STRDUP(text), &cmds, &n_cmds);
48 for (c = this_ctx->cmds; c->name != NULL; c++) {
49 bool match = (strncmp(text, c->name, strlen(text)) == 0);
51 if (match) {
52 ADD_TO_ARRAY(NULL, char *, SMB_STRDUP(c->name),
53 &cmds, &n_cmds);
57 if (n_cmds == 2) {
58 SAFE_FREE(cmds[0]);
59 cmds[0] = cmds[1];
60 n_cmds -= 1;
63 ADD_TO_ARRAY(NULL, char *, NULL, &cmds, &n_cmds);
64 return cmds;
67 static NTSTATUS net_sh_run(struct net_context *c,
68 struct rpc_sh_ctx *ctx, struct rpc_sh_cmd *cmd,
69 int argc, const char **argv)
71 TALLOC_CTX *mem_ctx;
72 struct rpc_pipe_client *pipe_hnd;
73 NTSTATUS status;
75 mem_ctx = talloc_new(ctx);
76 if (mem_ctx == NULL) {
77 d_fprintf(stderr, "talloc_new failed\n");
78 return NT_STATUS_NO_MEMORY;
81 status = cli_rpc_pipe_open_noauth(ctx->cli,
82 cli_get_iface(cmd->pipe_idx),
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") || strequal(argv[0], "quit")) {
124 return false;
127 if (strequal(argv[0], "help") || strequal(argv[0], "?")) {
128 for (cmd = ctx->cmds; cmd->name != NULL; cmd++) {
129 if (ctx != this_ctx) {
130 d_printf("%s ", ctx->whoami);
132 d_printf("%-15s %s\n", cmd->name, cmd->help);
134 return true;
137 for (cmd = ctx->cmds; cmd->name != NULL; cmd++) {
138 if (strequal(cmd->name, argv[0])) {
139 break;
143 if (cmd->name == NULL) {
144 /* None found */
145 d_fprintf(stderr, "%s: unknown cmd\n", argv[0]);
146 return true;
149 new_ctx = TALLOC_P(ctx, struct rpc_sh_ctx);
150 if (new_ctx == NULL) {
151 d_fprintf(stderr, "talloc failed\n");
152 return false;
154 new_ctx->cli = ctx->cli;
155 new_ctx->whoami = talloc_asprintf(new_ctx, "%s %s",
156 ctx->whoami, cmd->name);
157 new_ctx->thiscmd = talloc_strdup(new_ctx, cmd->name);
159 if (cmd->sub != NULL) {
160 new_ctx->cmds = cmd->sub(c, new_ctx, ctx);
161 } else {
162 new_ctx->cmds = NULL;
165 new_ctx->parent = ctx;
166 new_ctx->domain_name = ctx->domain_name;
167 new_ctx->domain_sid = ctx->domain_sid;
169 argc -= 1;
170 argv += 1;
172 if (cmd->sub != NULL) {
173 if (argc == 0) {
174 this_ctx = new_ctx;
175 return true;
177 return net_sh_process(c, new_ctx, argc, argv);
180 status = net_sh_run(c, new_ctx, cmd, argc, argv);
182 if (!NT_STATUS_IS_OK(status)) {
183 d_fprintf(stderr, "%s failed: %s\n", new_ctx->whoami,
184 nt_errstr(status));
187 return true;
190 static struct rpc_sh_cmd sh_cmds[6] = {
192 { "info", NULL, PI_SAMR, rpc_sh_info,
193 "Print information about the domain connected to" },
195 { "rights", net_rpc_rights_cmds, 0, NULL,
196 "List/Grant/Revoke user rights" },
198 { "share", net_rpc_share_cmds, 0, NULL,
199 "List/Add/Remove etc shares" },
201 { "user", net_rpc_user_cmds, 0, NULL,
202 "List/Add/Remove user info" },
204 { "account", net_rpc_acct_cmds, 0, NULL,
205 "Show/Change account policy settings" },
207 { NULL, NULL, 0, NULL, NULL }
210 int net_rpc_shell(struct net_context *c, int argc, const char **argv)
212 NTSTATUS status;
213 struct rpc_sh_ctx *ctx;
215 if (argc != 0 || c->display_usage) {
216 d_printf("Usage:\n"
217 "net rpc shell\n");
218 return -1;
221 ctx = TALLOC_P(NULL, struct rpc_sh_ctx);
222 if (ctx == NULL) {
223 d_fprintf(stderr, "talloc failed\n");
224 return -1;
227 status = net_make_ipc_connection(c, 0, &(ctx->cli));
228 if (!NT_STATUS_IS_OK(status)) {
229 d_fprintf(stderr, "Could not open connection: %s\n",
230 nt_errstr(status));
231 return -1;
234 ctx->cmds = sh_cmds;
235 ctx->whoami = "net rpc";
236 ctx->parent = NULL;
238 status = net_get_remote_domain_sid(ctx->cli, ctx, &ctx->domain_sid,
239 &ctx->domain_name);
240 if (!NT_STATUS_IS_OK(status)) {
241 return -1;
244 d_printf("Talking to domain %s (%s)\n", ctx->domain_name,
245 sid_string_tos(ctx->domain_sid));
247 this_ctx = ctx;
249 while(1) {
250 char *prompt = NULL;
251 char *line = NULL;
252 int ret;
254 if (asprintf(&prompt, "%s> ", this_ctx->whoami) < 0) {
255 break;
258 line = smb_readline(prompt, NULL, completion_fn);
259 SAFE_FREE(prompt);
261 if (line == NULL) {
262 break;
265 ret = poptParseArgvString(line, &argc, &argv);
266 if (ret == POPT_ERROR_NOARG) {
267 SAFE_FREE(line);
268 continue;
270 if (ret != 0) {
271 d_fprintf(stderr, "cmdline invalid: %s\n",
272 poptStrerror(ret));
273 SAFE_FREE(line);
274 return false;
277 if ((line[0] != '\n') &&
278 (!net_sh_process(c, this_ctx, argc, argv))) {
279 SAFE_FREE(line);
280 break;
282 SAFE_FREE(line);
285 cli_shutdown(ctx->cli);
287 TALLOC_FREE(ctx);
289 return 0;