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