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