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