r13316: Let the carnage begin....
[kai/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 2 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, write to the Free Software
18  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20
21
22 #include "includes.h"
23 #include "utils/net.h"
24
25 static struct rpc_sh_cmd sh_cmds[];
26
27 static NTSTATUS rpc_sh_info(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(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 rpc_sh_ctx *ctx, struct rpc_sh_cmd *cmd,
70                            int argc, const char **argv)
71 {
72         TALLOC_CTX *mem_ctx;
73         struct rpc_pipe_client *pipe_hnd;
74         NTSTATUS status;
75
76         mem_ctx = talloc_new(ctx);
77         if (mem_ctx == NULL) {
78                 d_fprintf(stderr, "talloc_new failed\n");
79                 return NT_STATUS_NO_MEMORY;
80         }
81
82         pipe_hnd = cli_rpc_pipe_open_noauth(ctx->cli, cmd->pipe_idx, &status);
83         if (pipe_hnd == NULL) {
84                 d_fprintf(stderr, "Could not open pipe: %s\n",
85                           nt_errstr(status));
86                 return status;
87         }
88
89         status = cmd->fn(mem_ctx, ctx, pipe_hnd, argc, argv);
90
91         cli_rpc_pipe_close(pipe_hnd);
92
93         talloc_destroy(mem_ctx);
94
95         return status;
96 }
97
98 static BOOL net_sh_process(struct rpc_sh_ctx *ctx,
99                            int argc, const char **argv)
100 {
101         struct rpc_sh_cmd *c;
102         struct rpc_sh_ctx *new_ctx;
103         NTSTATUS status;
104
105         if (argc == 0) {
106                 return True;
107         }
108
109         if (ctx == this_ctx) {
110
111                 /* We've been called from the cmd line */
112                 if (strequal(argv[0], "..") &&
113                     (this_ctx->parent != NULL)) {
114                         new_ctx = this_ctx->parent;
115                         talloc_free(this_ctx);
116                         this_ctx = new_ctx;
117                         return True;
118                 }
119         }
120
121         if (strequal(argv[0], "help") || strequal(argv[0], "?")) {
122                 for (c = ctx->cmds; c->name != NULL; c++) {
123                         if (ctx != this_ctx) {
124                                 d_printf("%s ", ctx->whoami);
125                         }
126                         d_printf("%-15s %s\n", c->name, c->help);
127                 }
128                 return True;
129         }
130
131         for (c = ctx->cmds; c->name != NULL; c++) {
132                 if (strequal(c->name, argv[0])) {
133                         break;
134                 }
135         }
136
137         if (c->name == NULL) {
138                 /* None found */
139                 d_fprintf(stderr, "%s: unknown cmd\n", argv[0]);
140                 return True;
141         }
142
143         new_ctx = TALLOC_P(ctx, struct rpc_sh_ctx);
144         if (new_ctx == NULL) {
145                 d_fprintf(stderr, "talloc failed\n");
146                 return False;
147         }
148         new_ctx->cli = ctx->cli;
149         new_ctx->whoami = talloc_asprintf(new_ctx, "%s %s",
150                                           ctx->whoami, c->name);
151         new_ctx->thiscmd = talloc_strdup(new_ctx, c->name);
152
153         if (c->sub != NULL) {
154                 new_ctx->cmds = c->sub(new_ctx, ctx);
155         } else {
156                 new_ctx->cmds = NULL;
157         }
158
159         new_ctx->parent = ctx;
160         new_ctx->domain_name = ctx->domain_name;
161         new_ctx->domain_sid = ctx->domain_sid;
162
163         argc -= 1;
164         argv += 1;
165
166         if (c->sub != NULL) {
167                 if (argc == 0) {
168                         this_ctx = new_ctx;
169                         return True;
170                 }
171                 return net_sh_process(new_ctx, argc, argv);
172         }
173
174         status = net_sh_run(new_ctx, c, argc, argv);
175
176         if (!NT_STATUS_IS_OK(status)) {
177                 d_fprintf(stderr, "%s failed: %s\n", new_ctx->whoami,
178                           nt_errstr(status));
179         }
180
181         return True;
182 }
183
184 int net_rpc_shell(int argc, const char **argv)
185 {
186         NTSTATUS status;
187         struct rpc_sh_ctx *ctx;
188
189         if (argc != 0) {
190                 d_fprintf(stderr, "usage: net rpc shell\n");
191                 return -1;
192         }
193
194         ctx = TALLOC_P(NULL, struct rpc_sh_ctx);
195         if (ctx == NULL) {
196                 d_fprintf(stderr, "talloc failed\n");
197                 return -1;
198         }
199
200         ctx->cli = net_make_ipc_connection(0);
201         if (ctx->cli == NULL) {
202                 d_fprintf(stderr, "Could not open connection\n");
203                 return -1;
204         }
205
206         ctx->cmds = sh_cmds;
207         ctx->whoami = "net rpc";
208         ctx->parent = NULL;
209
210         status = net_get_remote_domain_sid(ctx->cli, ctx, &ctx->domain_sid,
211                                            &ctx->domain_name);
212         if (!NT_STATUS_IS_OK(status)) {
213                 return -1;
214         }
215
216         d_printf("Talking to domain %s (%s)\n", ctx->domain_name,
217                  sid_string_static(ctx->domain_sid));
218         
219         this_ctx = ctx;
220
221         while(1) {
222                 char *prompt;
223                 char *line;
224                 int ret;
225
226                 asprintf(&prompt, "%s> ", this_ctx->whoami);
227
228                 line = smb_readline(prompt, NULL, completion_fn);
229                 SAFE_FREE(prompt);
230
231                 if (line == NULL) {
232                         break;
233                 }
234
235                 ret = poptParseArgvString(line, &argc, &argv);
236                 if (ret != 0) {
237                         d_fprintf(stderr, "cmdline invalid: %s\n",
238                                   poptStrerror(ret));
239                         return False;
240                 }
241
242                 if ((line[0] != '\n') &&
243                     (!net_sh_process(this_ctx, argc, argv))) {
244                         break;
245                 }
246         }
247
248         cli_shutdown(ctx->cli);
249
250         talloc_free(ctx);
251
252         return 0;
253 }
254
255 static struct rpc_sh_cmd sh_cmds[] = {
256
257         { "info", NULL, PI_SAMR, rpc_sh_info,
258           "Print information about the domain connected to" },
259
260         { "rights", net_rpc_rights_cmds, 0, NULL,
261           "List/Grant/Revoke user rights" },
262
263         { "share", net_rpc_share_cmds, 0, NULL,
264           "List/Add/Remove etc shares" },
265
266         { "user", net_rpc_user_cmds, 0, NULL,
267           "List/Add/Remove user info" },
268
269         { NULL, NULL, 0, NULL, NULL }
270 };