1985bf8b0b6eaeb7c490252617d88d162eb6f83e
[vlendec/samba-autobuild/.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 "popt_common.h"
23 #include "utils/net.h"
24 #include "rpc_client/cli_pipe.h"
25 #include "../librpc/gen_ndr/ndr_samr.h"
26 #include "lib/netapi/netapi.h"
27 #include "lib/netapi/netapi_net.h"
28 #include "../libcli/smbreadline/smbreadline.h"
29
30 static NTSTATUS rpc_sh_info(struct net_context *c,
31                             TALLOC_CTX *mem_ctx, struct rpc_sh_ctx *ctx,
32                             struct rpc_pipe_client *pipe_hnd,
33                             int argc, const char **argv)
34 {
35         return rpc_info_internals(c, ctx->domain_sid, ctx->domain_name,
36                                   ctx->cli, pipe_hnd, mem_ctx,
37                                   argc, argv);
38 }
39
40 static struct rpc_sh_ctx *this_ctx;
41
42 static char **completion_fn(const char *text, int start, int end)
43 {
44         char **cmds = NULL;
45         int n_cmds = 0;
46         struct rpc_sh_cmd *c;
47
48         if (start != 0) {
49                 return NULL;
50         }
51
52         ADD_TO_ARRAY(NULL, char *, SMB_STRDUP(text), &cmds, &n_cmds);
53
54         for (c = this_ctx->cmds; c->name != NULL; c++) {
55                 bool match = (strncmp(text, c->name, strlen(text)) == 0);
56
57                 if (match) {
58                         ADD_TO_ARRAY(NULL, char *, SMB_STRDUP(c->name),
59                                      &cmds, &n_cmds);
60                 }
61         }
62
63         if (n_cmds == 2) {
64                 SAFE_FREE(cmds[0]);
65                 cmds[0] = cmds[1];
66                 n_cmds -= 1;
67         }
68
69         ADD_TO_ARRAY(NULL, char *, NULL, &cmds, &n_cmds);
70         return cmds;
71 }
72
73 static NTSTATUS net_sh_run(struct net_context *c,
74                            struct rpc_sh_ctx *ctx, struct rpc_sh_cmd *cmd,
75                            int argc, const char **argv)
76 {
77         TALLOC_CTX *mem_ctx;
78         struct rpc_pipe_client *pipe_hnd = NULL;
79         NTSTATUS status;
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, cmd->interface,
88                                           &pipe_hnd);
89         if (!NT_STATUS_IS_OK(status)) {
90                 d_fprintf(stderr, _("Could not open pipe: %s\n"),
91                           nt_errstr(status));
92                 return status;
93         }
94
95         status = cmd->fn(c, mem_ctx, ctx, pipe_hnd, argc, argv);
96
97         TALLOC_FREE(pipe_hnd);
98
99         talloc_destroy(mem_ctx);
100
101         return status;
102 }
103
104 static bool net_sh_process(struct net_context *c,
105                            struct rpc_sh_ctx *ctx,
106                            int argc, const char **argv)
107 {
108         struct rpc_sh_cmd *cmd;
109         struct rpc_sh_ctx *new_ctx;
110         NTSTATUS status;
111
112         if (argc == 0) {
113                 return true;
114         }
115
116         if (ctx == this_ctx) {
117
118                 /* We've been called from the cmd line */
119                 if (strequal(argv[0], "..") &&
120                     (this_ctx->parent != NULL)) {
121                         new_ctx = this_ctx->parent;
122                         TALLOC_FREE(this_ctx);
123                         this_ctx = new_ctx;
124                         return true;
125                 }
126         }
127
128         if (strequal(argv[0], "exit") ||
129             strequal(argv[0], "quit") ||
130             strequal(argv[0], "q")) {
131                 return false;
132         }
133
134         if (strequal(argv[0], "help") || strequal(argv[0], "?")) {
135                 for (cmd = ctx->cmds; cmd->name != NULL; cmd++) {
136                         if (ctx != this_ctx) {
137                                 d_printf("%s ", ctx->whoami);
138                         }
139                         d_printf("%-15s %s\n", cmd->name, cmd->help);
140                 }
141                 return true;
142         }
143
144         for (cmd = ctx->cmds; cmd->name != NULL; cmd++) {
145                 if (strequal(cmd->name, argv[0])) {
146                         break;
147                 }
148         }
149
150         if (cmd->name == NULL) {
151                 /* None found */
152                 d_fprintf(stderr,_( "%s: unknown cmd\n"), argv[0]);
153                 return true;
154         }
155
156         new_ctx = TALLOC_P(ctx, struct rpc_sh_ctx);
157         if (new_ctx == NULL) {
158                 d_fprintf(stderr, _("talloc failed\n"));
159                 return false;
160         }
161         new_ctx->cli = ctx->cli;
162         new_ctx->whoami = talloc_asprintf(new_ctx, "%s %s",
163                                           ctx->whoami, cmd->name);
164         new_ctx->thiscmd = talloc_strdup(new_ctx, cmd->name);
165
166         if (cmd->sub != NULL) {
167                 new_ctx->cmds = cmd->sub(c, new_ctx, ctx);
168         } else {
169                 new_ctx->cmds = NULL;
170         }
171
172         new_ctx->parent = ctx;
173         new_ctx->domain_name = ctx->domain_name;
174         new_ctx->domain_sid = ctx->domain_sid;
175
176         argc -= 1;
177         argv += 1;
178
179         if (cmd->sub != NULL) {
180                 if (argc == 0) {
181                         this_ctx = new_ctx;
182                         return true;
183                 }
184                 return net_sh_process(c, new_ctx, argc, argv);
185         }
186
187         status = net_sh_run(c, new_ctx, cmd, argc, argv);
188
189         if (!NT_STATUS_IS_OK(status)) {
190                 d_fprintf(stderr, _("%s failed: %s\n"), new_ctx->whoami,
191                           nt_errstr(status));
192         }
193
194         return true;
195 }
196
197 static struct rpc_sh_cmd sh_cmds[6] = {
198
199         { "info", NULL, &ndr_table_samr.syntax_id, rpc_sh_info,
200           N_("Print information about the domain connected to") },
201
202         { "rights", net_rpc_rights_cmds, 0, NULL,
203           N_("List/Grant/Revoke user rights") },
204
205         { "share", net_rpc_share_cmds, 0, NULL,
206           N_("List/Add/Remove etc shares") },
207
208         { "user", net_rpc_user_cmds, 0, NULL,
209           N_("List/Add/Remove user info") },
210
211         { "account", net_rpc_acct_cmds, 0, NULL,
212           N_("Show/Change account policy settings") },
213
214         { NULL, NULL, 0, NULL, NULL }
215 };
216
217 int net_rpc_shell(struct net_context *c, int argc, const char **argv)
218 {
219         NTSTATUS status;
220         struct rpc_sh_ctx *ctx;
221
222         if (argc != 0 || c->display_usage) {
223                 d_printf("%s\nnet rpc shell\n", _("Usage:"));
224                 return -1;
225         }
226
227         if (libnetapi_net_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 }