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