summaryrefslogtreecommitdiff
path: root/source3/utils/net_rpc_shell.c
blob: dee7a9f9c7ed5e0a61e793f074c0e065b936dd45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
 *  Unix SMB/CIFS implementation.
 *  Shell around net rpc subcommands
 *  Copyright (C) Volker Lendecke 2006
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, see <http://www.gnu.org/licenses/>.
 */


#include "includes.h"
#include "utils/net.h"

static NTSTATUS rpc_sh_info(struct net_context *c,
			    TALLOC_CTX *mem_ctx, struct rpc_sh_ctx *ctx,
			    struct rpc_pipe_client *pipe_hnd,
			    int argc, const char **argv)
{
	return rpc_info_internals(c, ctx->domain_sid, ctx->domain_name,
				  ctx->cli, pipe_hnd, mem_ctx,
				  argc, argv);
}

static struct rpc_sh_ctx *this_ctx;

static char **completion_fn(const char *text, int start, int end)
{
	char **cmds = NULL;
	int n_cmds = 0;
	struct rpc_sh_cmd *c;

	if (start != 0) {
		return NULL;
	}

	ADD_TO_ARRAY(NULL, char *, SMB_STRDUP(text), &cmds, &n_cmds);

	for (c = this_ctx->cmds; c->name != NULL; c++) {
		bool match = (strncmp(text, c->name, strlen(text)) == 0);

		if (match) {
			ADD_TO_ARRAY(NULL, char *, SMB_STRDUP(c->name),
				     &cmds, &n_cmds);
		}
	}

	if (n_cmds == 2) {
		SAFE_FREE(cmds[0]);
		cmds[0] = cmds[1];
		n_cmds -= 1;
	}

	ADD_TO_ARRAY(NULL, char *, NULL, &cmds, &n_cmds);
	return cmds;
}

static NTSTATUS net_sh_run(struct net_context *c,
			   struct rpc_sh_ctx *ctx, struct rpc_sh_cmd *cmd,
			   int argc, const char **argv)
{
	TALLOC_CTX *mem_ctx;
	struct rpc_pipe_client *pipe_hnd;
	NTSTATUS status;

	mem_ctx = talloc_new(ctx);
	if (mem_ctx == NULL) {
		d_fprintf(stderr, "talloc_new failed\n");
		return NT_STATUS_NO_MEMORY;
	}

	pipe_hnd = cli_rpc_pipe_open_noauth(ctx->cli, cmd->pipe_idx, &status);
	if (pipe_hnd == NULL) {
		d_fprintf(stderr, "Could not open pipe: %s\n",
			  nt_errstr(status));
		return status;
	}

	status = cmd->fn(c, mem_ctx, ctx, pipe_hnd, argc, argv);

	TALLOC_FREE(pipe_hnd);

	talloc_destroy(mem_ctx);

	return status;
}

static bool net_sh_process(struct net_context *c,
			   struct rpc_sh_ctx *ctx,
			   int argc, const char **argv)
{
	struct rpc_sh_cmd *cmd;
	struct rpc_sh_ctx *new_ctx;
	NTSTATUS status;

	if (argc == 0) {
		return true;
	}

	if (ctx == this_ctx) {

		/* We've been called from the cmd line */
		if (strequal(argv[0], "..") &&
		    (this_ctx->parent != NULL)) {
			new_ctx = this_ctx->parent;
			TALLOC_FREE(this_ctx);
			this_ctx = new_ctx;
			return true;
		}
	}

	if (strequal(argv[0], "exit") || strequal(argv[0], "quit")) {
		return false;
	}

	if (strequal(argv[0], "help") || strequal(argv[0], "?")) {
		for (cmd = ctx->cmds; cmd->name != NULL; cmd++) {
			if (ctx != this_ctx) {
				d_printf("%s ", ctx->whoami);
			}
			d_printf("%-15s %s\n", cmd->name, cmd->help);
		}
		return true;
	}

	for (cmd = ctx->cmds; cmd->name != NULL; cmd++) {
		if (strequal(cmd->name, argv[0])) {
			break;
		}
	}

	if (cmd->name == NULL) {
		/* None found */
		d_fprintf(stderr, "%s: unknown cmd\n", argv[0]);
		return true;
	}

	new_ctx = TALLOC_P(ctx, struct rpc_sh_ctx);
	if (new_ctx == NULL) {
		d_fprintf(stderr, "talloc failed\n");
		return false;
	}
	new_ctx->cli = ctx->cli;
	new_ctx->whoami = talloc_asprintf(new_ctx, "%s %s",
					  ctx->whoami, cmd->name);
	new_ctx->thiscmd = talloc_strdup(new_ctx, cmd->name);

	if (cmd->sub != NULL) {
		new_ctx->cmds = cmd->sub(c, new_ctx, ctx);
	} else {
		new_ctx->cmds = NULL;
	}

	new_ctx->parent = ctx;
	new_ctx->domain_name = ctx->domain_name;
	new_ctx->domain_sid = ctx->domain_sid;

	argc -= 1;
	argv += 1;

	if (cmd->sub != NULL) {
		if (argc == 0) {
			this_ctx = new_ctx;
			return true;
		}
		return net_sh_process(c, new_ctx, argc, argv);
	}

	status = net_sh_run(c, new_ctx, cmd, argc, argv);

	if (!NT_STATUS_IS_OK(status)) {
		d_fprintf(stderr, "%s failed: %s\n", new_ctx->whoami,
			  nt_errstr(status));
	}

	return true;
}

static struct rpc_sh_cmd sh_cmds[6] = {

	{ "info", NULL, PI_SAMR, rpc_sh_info,
	  "Print information about the domain connected to" },

	{ "rights", net_rpc_rights_cmds, 0, NULL,
	  "List/Grant/Revoke user rights" },

	{ "share", net_rpc_share_cmds, 0, NULL,
	  "List/Add/Remove etc shares" },

	{ "user", net_rpc_user_cmds, 0, NULL,
	  "List/Add/Remove user info" },

	{ "account", net_rpc_acct_cmds, 0, NULL,
	  "Show/Change account policy settings" },

	{ NULL, NULL, 0, NULL, NULL }
};

int net_rpc_shell(struct net_context *c, int argc, const char **argv)
{
	NTSTATUS status;
	struct rpc_sh_ctx *ctx;

	if (argc != 0 || c->display_usage) {
		d_printf("Usage:\n"
			 "net rpc shell\n");
		return -1;
	}

	ctx = TALLOC_P(NULL, struct rpc_sh_ctx);
	if (ctx == NULL) {
		d_fprintf(stderr, "talloc failed\n");
		return -1;
	}

	status = net_make_ipc_connection(c, 0, &(ctx->cli));
	if (!NT_STATUS_IS_OK(status)) {
		d_fprintf(stderr, "Could not open connection: %s\n",
			  nt_errstr(status));
		return -1;
	}

	ctx->cmds = sh_cmds;
	ctx->whoami = "net rpc";
	ctx->parent = NULL;

	status = net_get_remote_domain_sid(ctx->cli, ctx, &ctx->domain_sid,
					   &ctx->domain_name);
	if (!NT_STATUS_IS_OK(status)) {
		return -1;
	}

	d_printf("Talking to domain %s (%s)\n", ctx->domain_name,
		 sid_string_tos(ctx->domain_sid));

	this_ctx = ctx;

	while(1) {
		char *prompt = NULL;
		char *line = NULL;
		int ret;

		if (asprintf(&prompt, "%s> ", this_ctx->whoami) < 0) {
			break;
		}

		line = smb_readline(prompt, NULL, completion_fn);
		SAFE_FREE(prompt);

		if (line == NULL) {
			break;
		}

		ret = poptParseArgvString(line, &argc, &argv);
		if (ret == POPT_ERROR_NOARG) {
			SAFE_FREE(line);
			continue;
		}
		if (ret != 0) {
			d_fprintf(stderr, "cmdline invalid: %s\n",
				  poptStrerror(ret));
			SAFE_FREE(line);
			return false;
		}

		if ((line[0] != '\n') &&
		    (!net_sh_process(c, this_ctx, argc, argv))) {
			SAFE_FREE(line);
			break;
		}
		SAFE_FREE(line);
	}

	cli_shutdown(ctx->cli);

	TALLOC_FREE(ctx);

	return 0;
}