build: set shared libraries flags correctly on mac os X
[Samba/gebeck_regimport.git] / source3 / utils / net_rpc_shell.c
blob82f9f29cedf699778498a0707b81c6848140db6c
1 /*
2 * Unix SMB/CIFS implementation.
3 * Shell around net rpc subcommands
4 * Copyright (C) Volker Lendecke 2006
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.
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.
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/>.
21 #include "includes.h"
22 #include "popt_common.h"
23 #include "utils/net.h"
24 #include "../librpc/gen_ndr/ndr_samr.h"
25 #include "lib/netapi/netapi.h"
26 #include "../libcli/smbreadline/smbreadline.h"
28 static NTSTATUS rpc_sh_info(struct net_context *c,
29 TALLOC_CTX *mem_ctx, struct rpc_sh_ctx *ctx,
30 struct rpc_pipe_client *pipe_hnd,
31 int argc, const char **argv)
33 return rpc_info_internals(c, ctx->domain_sid, ctx->domain_name,
34 ctx->cli, pipe_hnd, mem_ctx,
35 argc, argv);
38 static struct rpc_sh_ctx *this_ctx;
40 static char **completion_fn(const char *text, int start, int end)
42 char **cmds = NULL;
43 int n_cmds = 0;
44 struct rpc_sh_cmd *c;
46 if (start != 0) {
47 return NULL;
50 ADD_TO_ARRAY(NULL, char *, SMB_STRDUP(text), &cmds, &n_cmds);
52 for (c = this_ctx->cmds; c->name != NULL; c++) {
53 bool match = (strncmp(text, c->name, strlen(text)) == 0);
55 if (match) {
56 ADD_TO_ARRAY(NULL, char *, SMB_STRDUP(c->name),
57 &cmds, &n_cmds);
61 if (n_cmds == 2) {
62 SAFE_FREE(cmds[0]);
63 cmds[0] = cmds[1];
64 n_cmds -= 1;
67 ADD_TO_ARRAY(NULL, char *, NULL, &cmds, &n_cmds);
68 return cmds;
71 static NTSTATUS net_sh_run(struct net_context *c,
72 struct rpc_sh_ctx *ctx, struct rpc_sh_cmd *cmd,
73 int argc, const char **argv)
75 TALLOC_CTX *mem_ctx;
76 struct rpc_pipe_client *pipe_hnd = NULL;
77 NTSTATUS status;
79 mem_ctx = talloc_new(ctx);
80 if (mem_ctx == NULL) {
81 d_fprintf(stderr, _("talloc_new failed\n"));
82 return NT_STATUS_NO_MEMORY;
85 status = cli_rpc_pipe_open_noauth(ctx->cli, cmd->interface,
86 &pipe_hnd);
87 if (!NT_STATUS_IS_OK(status)) {
88 d_fprintf(stderr, _("Could not open pipe: %s\n"),
89 nt_errstr(status));
90 return status;
93 status = cmd->fn(c, mem_ctx, ctx, pipe_hnd, argc, argv);
95 TALLOC_FREE(pipe_hnd);
97 talloc_destroy(mem_ctx);
99 return status;
102 static bool net_sh_process(struct net_context *c,
103 struct rpc_sh_ctx *ctx,
104 int argc, const char **argv)
106 struct rpc_sh_cmd *cmd;
107 struct rpc_sh_ctx *new_ctx;
108 NTSTATUS status;
110 if (argc == 0) {
111 return true;
114 if (ctx == this_ctx) {
116 /* We've been called from the cmd line */
117 if (strequal(argv[0], "..") &&
118 (this_ctx->parent != NULL)) {
119 new_ctx = this_ctx->parent;
120 TALLOC_FREE(this_ctx);
121 this_ctx = new_ctx;
122 return true;
126 if (strequal(argv[0], "exit") ||
127 strequal(argv[0], "quit") ||
128 strequal(argv[0], "q")) {
129 return false;
132 if (strequal(argv[0], "help") || strequal(argv[0], "?")) {
133 for (cmd = ctx->cmds; cmd->name != NULL; cmd++) {
134 if (ctx != this_ctx) {
135 d_printf("%s ", ctx->whoami);
137 d_printf("%-15s %s\n", cmd->name, cmd->help);
139 return true;
142 for (cmd = ctx->cmds; cmd->name != NULL; cmd++) {
143 if (strequal(cmd->name, argv[0])) {
144 break;
148 if (cmd->name == NULL) {
149 /* None found */
150 d_fprintf(stderr,_( "%s: unknown cmd\n"), argv[0]);
151 return true;
154 new_ctx = TALLOC_P(ctx, struct rpc_sh_ctx);
155 if (new_ctx == NULL) {
156 d_fprintf(stderr, _("talloc failed\n"));
157 return false;
159 new_ctx->cli = ctx->cli;
160 new_ctx->whoami = talloc_asprintf(new_ctx, "%s %s",
161 ctx->whoami, cmd->name);
162 new_ctx->thiscmd = talloc_strdup(new_ctx, cmd->name);
164 if (cmd->sub != NULL) {
165 new_ctx->cmds = cmd->sub(c, new_ctx, ctx);
166 } else {
167 new_ctx->cmds = NULL;
170 new_ctx->parent = ctx;
171 new_ctx->domain_name = ctx->domain_name;
172 new_ctx->domain_sid = ctx->domain_sid;
174 argc -= 1;
175 argv += 1;
177 if (cmd->sub != NULL) {
178 if (argc == 0) {
179 this_ctx = new_ctx;
180 return true;
182 return net_sh_process(c, new_ctx, argc, argv);
185 status = net_sh_run(c, new_ctx, cmd, argc, argv);
187 if (!NT_STATUS_IS_OK(status)) {
188 d_fprintf(stderr, _("%s failed: %s\n"), new_ctx->whoami,
189 nt_errstr(status));
192 return true;
195 static struct rpc_sh_cmd sh_cmds[6] = {
197 { "info", NULL, &ndr_table_samr.syntax_id, rpc_sh_info,
198 N_("Print information about the domain connected to") },
200 { "rights", net_rpc_rights_cmds, 0, NULL,
201 N_("List/Grant/Revoke user rights") },
203 { "share", net_rpc_share_cmds, 0, NULL,
204 N_("List/Add/Remove etc shares") },
206 { "user", net_rpc_user_cmds, 0, NULL,
207 N_("List/Add/Remove user info") },
209 { "account", net_rpc_acct_cmds, 0, NULL,
210 N_("Show/Change account policy settings") },
212 { NULL, NULL, 0, NULL, NULL }
215 int net_rpc_shell(struct net_context *c, int argc, const char **argv)
217 NTSTATUS status;
218 struct rpc_sh_ctx *ctx;
220 if (argc != 0 || c->display_usage) {
221 d_printf("%s\nnet rpc shell\n", _("Usage:"));
222 return -1;
225 if (libnetapi_init(&c->netapi_ctx) != 0) {
226 return -1;
228 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
229 libnetapi_set_password(c->netapi_ctx, c->opt_password);
230 if (c->opt_kerberos) {
231 libnetapi_set_use_kerberos(c->netapi_ctx);
234 ctx = TALLOC_P(NULL, struct rpc_sh_ctx);
235 if (ctx == NULL) {
236 d_fprintf(stderr, _("talloc failed\n"));
237 return -1;
240 status = net_make_ipc_connection(c, 0, &(ctx->cli));
241 if (!NT_STATUS_IS_OK(status)) {
242 d_fprintf(stderr, _("Could not open connection: %s\n"),
243 nt_errstr(status));
244 return -1;
247 ctx->cmds = sh_cmds;
248 ctx->whoami = "net rpc";
249 ctx->parent = NULL;
251 status = net_get_remote_domain_sid(ctx->cli, ctx, &ctx->domain_sid,
252 &ctx->domain_name);
253 if (!NT_STATUS_IS_OK(status)) {
254 return -1;
257 d_printf(_("Talking to domain %s (%s)\n"), ctx->domain_name,
258 sid_string_tos(ctx->domain_sid));
260 this_ctx = ctx;
262 while(1) {
263 char *prompt = NULL;
264 char *line = NULL;
265 int ret;
267 if (asprintf(&prompt, "%s> ", this_ctx->whoami) < 0) {
268 break;
271 line = smb_readline(prompt, NULL, completion_fn);
272 SAFE_FREE(prompt);
274 if (line == NULL) {
275 break;
278 ret = poptParseArgvString(line, &argc, &argv);
279 if (ret == POPT_ERROR_NOARG) {
280 SAFE_FREE(line);
281 continue;
283 if (ret != 0) {
284 d_fprintf(stderr, _("cmdline invalid: %s\n"),
285 poptStrerror(ret));
286 SAFE_FREE(line);
287 return false;
290 if ((line[0] != '\n') &&
291 (!net_sh_process(c, this_ctx, argc, argv))) {
292 SAFE_FREE(line);
293 break;
295 SAFE_FREE(line);
298 cli_shutdown(ctx->cli);
300 TALLOC_FREE(ctx);
302 return 0;