s3: Make run_rpc_command take strings instead of a ndr_interface_table
[ira/wip.git] / source3 / utils / net_rpc_shell.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  Shell around net rpc subcommands
4  *  Copyright (C) Volker Lendecke 2006
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20
21 #include "includes.h"
22 #include "utils/net.h"
23
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)
28 {
29         return rpc_info_internals(c, ctx->domain_sid, ctx->domain_name,
30                                   ctx->cli, pipe_hnd, mem_ctx,
31                                   argc, argv);
32 }
33
34 static struct rpc_sh_ctx *this_ctx;
35
36 static char **completion_fn(const char *text, int start, int end)
37 {
38         char **cmds = NULL;
39         int n_cmds = 0;
40         struct rpc_sh_cmd *c;
41
42         if (start != 0) {
43                 return NULL;
44         }
45
46         ADD_TO_ARRAY(NULL, char *, SMB_STRDUP(text), &cmds, &n_cmds);
47
48         for (c = this_ctx->cmds; c->name != NULL; c++) {
49                 bool match = (strncmp(text, c->name, strlen(text)) == 0);
50
51                 if (match) {
52                         ADD_TO_ARRAY(NULL, char *, SMB_STRDUP(c->name),
53                                      &cmds, &n_cmds);
54                 }
55         }
56
57         if (n_cmds == 2) {
58                 SAFE_FREE(cmds[0]);
59                 cmds[0] = cmds[1];
60                 n_cmds -= 1;
61         }
62
63         ADD_TO_ARRAY(NULL, char *, NULL, &cmds, &n_cmds);
64         return cmds;
65 }
66
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)
70 {
71         TALLOC_CTX *mem_ctx;
72         struct rpc_pipe_client *pipe_hnd;
73         struct ndr_syntax_id syntax;
74         NTSTATUS status;
75
76         if (!ndr_syntax_from_string(cmd->interface, cmd->interface_version,
77                                     &syntax)) {
78                 return NT_STATUS_INVALID_PARAMETER;
79         }
80
81         mem_ctx = talloc_new(ctx);
82         if (mem_ctx == NULL) {
83                 d_fprintf(stderr, _("talloc_new failed\n"));
84                 return NT_STATUS_NO_MEMORY;
85         }
86
87         status = cli_rpc_pipe_open_noauth(ctx->cli, &syntax, &pipe_hnd);
88         if (!NT_STATUS_IS_OK(status)) {
89                 d_fprintf(stderr, _("Could not open pipe: %s\n"),
90                           nt_errstr(status));
91                 return status;
92         }
93
94         status = cmd->fn(c, mem_ctx, ctx, pipe_hnd, argc, argv);
95
96         TALLOC_FREE(pipe_hnd);
97
98         talloc_destroy(mem_ctx);
99
100         return status;
101 }
102
103 static bool net_sh_process(struct net_context *c,
104                            struct rpc_sh_ctx *ctx,
105                            int argc, const char **argv)
106 {
107         struct rpc_sh_cmd *cmd;
108         struct rpc_sh_ctx *new_ctx;
109         NTSTATUS status;
110
111         if (argc == 0) {
112                 return true;
113         }
114
115         if (ctx == this_ctx) {
116
117                 /* We've been called from the cmd line */
118                 if (strequal(argv[0], "..") &&
119                     (this_ctx->parent != NULL)) {
120                         new_ctx = this_ctx->parent;
121                         TALLOC_FREE(this_ctx);
122                         this_ctx = new_ctx;
123                         return true;
124                 }
125         }
126
127         if (strequal(argv[0], "exit") ||
128             strequal(argv[0], "quit") ||
129             strequal(argv[0], "q")) {
130                 return false;
131         }
132
133         if (strequal(argv[0], "help") || strequal(argv[0], "?")) {
134                 for (cmd = ctx->cmds; cmd->name != NULL; cmd++) {
135                         if (ctx != this_ctx) {
136                                 d_printf("%s ", ctx->whoami);
137                         }
138                         d_printf("%-15s %s\n", cmd->name, cmd->help);
139                 }
140                 return true;
141         }
142
143         for (cmd = ctx->cmds; cmd->name != NULL; cmd++) {
144                 if (strequal(cmd->name, argv[0])) {
145                         break;
146                 }
147         }
148
149         if (cmd->name == NULL) {
150                 /* None found */
151                 d_fprintf(stderr,_( "%s: unknown cmd\n"), argv[0]);
152                 return true;
153         }
154
155         new_ctx = TALLOC_P(ctx, struct rpc_sh_ctx);
156         if (new_ctx == NULL) {
157                 d_fprintf(stderr, _("talloc failed\n"));
158                 return false;
159         }
160         new_ctx->cli = ctx->cli;
161         new_ctx->whoami = talloc_asprintf(new_ctx, "%s %s",
162                                           ctx->whoami, cmd->name);
163         new_ctx->thiscmd = talloc_strdup(new_ctx, cmd->name);
164
165         if (cmd->sub != NULL) {
166                 new_ctx->cmds = cmd->sub(c, new_ctx, ctx);
167         } else {
168                 new_ctx->cmds = NULL;
169         }
170
171         new_ctx->parent = ctx;
172         new_ctx->domain_name = ctx->domain_name;
173         new_ctx->domain_sid = ctx->domain_sid;
174
175         argc -= 1;
176         argv += 1;
177
178         if (cmd->sub != NULL) {
179                 if (argc == 0) {
180                         this_ctx = new_ctx;
181                         return true;
182                 }
183                 return net_sh_process(c, new_ctx, argc, argv);
184         }
185
186         status = net_sh_run(c, new_ctx, cmd, argc, argv);
187
188         if (!NT_STATUS_IS_OK(status)) {
189                 d_fprintf(stderr, _("%s failed: %s\n"), new_ctx->whoami,
190                           nt_errstr(status));
191         }
192
193         return true;
194 }
195
196 static struct rpc_sh_cmd sh_cmds[6] = {
197
198         { "info", NULL, NDR_SAMR_UUID, NDR_SAMR_VERSION, rpc_sh_info,
199           N_("Print information about the domain connected to") },
200
201         { "rights", net_rpc_rights_cmds, NULL, 0, NULL,
202           N_("List/Grant/Revoke user rights") },
203
204         { "share", net_rpc_share_cmds, NULL, 0, NULL,
205           N_("List/Add/Remove etc shares") },
206
207         { "user", net_rpc_user_cmds, NULL, 0, NULL,
208           N_("List/Add/Remove user info") },
209
210         { "account", net_rpc_acct_cmds, NULL, 0, NULL,
211           N_("Show/Change account policy settings") },
212
213         { NULL, NULL, NULL, 0, NULL, NULL }
214 };
215
216 int net_rpc_shell(struct net_context *c, int argc, const char **argv)
217 {
218         NTSTATUS status;
219         struct rpc_sh_ctx *ctx;
220
221         if (argc != 0 || c->display_usage) {
222                 d_printf(_("Usage:\n"
223                            "net rpc shell\n"));
224                 return -1;
225         }
226
227         if (libnetapi_init(&c->netapi_ctx) != 0) {
228                 return -1;
229         }
230         libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
231         libnetapi_set_password(c->netapi_ctx, c->opt_password);
232         if (c->opt_kerberos) {
233                 libnetapi_set_use_kerberos(c->netapi_ctx);
234         }
235
236         ctx = TALLOC_P(NULL, struct rpc_sh_ctx);
237         if (ctx == NULL) {
238                 d_fprintf(stderr, _("talloc failed\n"));
239                 return -1;
240         }
241
242         status = net_make_ipc_connection(c, 0, &(ctx->cli));
243         if (!NT_STATUS_IS_OK(status)) {
244                 d_fprintf(stderr, _("Could not open connection: %s\n"),
245                           nt_errstr(status));
246                 return -1;
247         }
248
249         ctx->cmds = sh_cmds;
250         ctx->whoami = "net rpc";
251         ctx->parent = NULL;
252
253         status = net_get_remote_domain_sid(ctx->cli, ctx, &ctx->domain_sid,
254                                            &ctx->domain_name);
255         if (!NT_STATUS_IS_OK(status)) {
256                 return -1;
257         }
258
259         d_printf(_("Talking to domain %s (%s)\n"), ctx->domain_name,
260                  sid_string_tos(ctx->domain_sid));
261
262         this_ctx = ctx;
263
264         while(1) {
265                 char *prompt = NULL;
266                 char *line = NULL;
267                 int ret;
268
269                 if (asprintf(&prompt, "%s> ", this_ctx->whoami) < 0) {
270                         break;
271                 }
272
273                 line = smb_readline(prompt, NULL, completion_fn);
274                 SAFE_FREE(prompt);
275
276                 if (line == NULL) {
277                         break;
278                 }
279
280                 ret = poptParseArgvString(line, &argc, &argv);
281                 if (ret == POPT_ERROR_NOARG) {
282                         SAFE_FREE(line);
283                         continue;
284                 }
285                 if (ret != 0) {
286                         d_fprintf(stderr, _("cmdline invalid: %s\n"),
287                                   poptStrerror(ret));
288                         SAFE_FREE(line);
289                         return false;
290                 }
291
292                 if ((line[0] != '\n') &&
293                     (!net_sh_process(c, this_ctx, argc, argv))) {
294                         SAFE_FREE(line);
295                         break;
296                 }
297                 SAFE_FREE(line);
298         }
299
300         cli_shutdown(ctx->cli);
301
302         TALLOC_FREE(ctx);
303
304         return 0;
305 }