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