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/>.
22 #include "utils/net.h"
24 static NTSTATUS
rpc_sh_info(struct net_context
*c
,
25 TALLOC_CTX
*mem_ctx
, struct rpc_sh_ctx
*ctx
,
26 struct rpc_pipe_client
*pipe_hnd
,
27 int argc
, const char **argv
)
29 return rpc_info_internals(c
, ctx
->domain_sid
, ctx
->domain_name
,
30 ctx
->cli
, pipe_hnd
, mem_ctx
,
34 static struct rpc_sh_ctx
*this_ctx
;
36 static char **completion_fn(const char *text
, int start
, int end
)
46 ADD_TO_ARRAY(NULL
, char *, SMB_STRDUP(text
), &cmds
, &n_cmds
);
48 for (c
= this_ctx
->cmds
; c
->name
!= NULL
; c
++) {
49 bool match
= (strncmp(text
, c
->name
, strlen(text
)) == 0);
52 ADD_TO_ARRAY(NULL
, char *, SMB_STRDUP(c
->name
),
63 ADD_TO_ARRAY(NULL
, char *, NULL
, &cmds
, &n_cmds
);
67 static NTSTATUS
net_sh_run(struct net_context
*c
,
68 struct rpc_sh_ctx
*ctx
, struct rpc_sh_cmd
*cmd
,
69 int argc
, const char **argv
)
72 struct rpc_pipe_client
*pipe_hnd
;
75 mem_ctx
= talloc_new(ctx
);
76 if (mem_ctx
== NULL
) {
77 d_fprintf(stderr
, "talloc_new failed\n");
78 return NT_STATUS_NO_MEMORY
;
81 pipe_hnd
= cli_rpc_pipe_open_noauth(ctx
->cli
, cmd
->pipe_idx
, &status
);
82 if (pipe_hnd
== NULL
) {
83 d_fprintf(stderr
, "Could not open pipe: %s\n",
88 status
= cmd
->fn(c
, mem_ctx
, ctx
, pipe_hnd
, argc
, argv
);
90 TALLOC_FREE(pipe_hnd
);
92 talloc_destroy(mem_ctx
);
97 static bool net_sh_process(struct net_context
*c
,
98 struct rpc_sh_ctx
*ctx
,
99 int argc
, const char **argv
)
101 struct rpc_sh_cmd
*cmd
;
102 struct rpc_sh_ctx
*new_ctx
;
109 if (ctx
== this_ctx
) {
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
);
121 if (strequal(argv
[0], "exit") || strequal(argv
[0], "quit")) {
125 if (strequal(argv
[0], "help") || strequal(argv
[0], "?")) {
126 for (cmd
= ctx
->cmds
; cmd
->name
!= NULL
; cmd
++) {
127 if (ctx
!= this_ctx
) {
128 d_printf("%s ", ctx
->whoami
);
130 d_printf("%-15s %s\n", cmd
->name
, cmd
->help
);
135 for (cmd
= ctx
->cmds
; cmd
->name
!= NULL
; cmd
++) {
136 if (strequal(cmd
->name
, argv
[0])) {
141 if (cmd
->name
== NULL
) {
143 d_fprintf(stderr
, "%s: unknown cmd\n", argv
[0]);
147 new_ctx
= TALLOC_P(ctx
, struct rpc_sh_ctx
);
148 if (new_ctx
== NULL
) {
149 d_fprintf(stderr
, "talloc failed\n");
152 new_ctx
->cli
= ctx
->cli
;
153 new_ctx
->whoami
= talloc_asprintf(new_ctx
, "%s %s",
154 ctx
->whoami
, cmd
->name
);
155 new_ctx
->thiscmd
= talloc_strdup(new_ctx
, cmd
->name
);
157 if (cmd
->sub
!= NULL
) {
158 new_ctx
->cmds
= cmd
->sub(c
, new_ctx
, ctx
);
160 new_ctx
->cmds
= NULL
;
163 new_ctx
->parent
= ctx
;
164 new_ctx
->domain_name
= ctx
->domain_name
;
165 new_ctx
->domain_sid
= ctx
->domain_sid
;
170 if (cmd
->sub
!= NULL
) {
175 return net_sh_process(c
, new_ctx
, argc
, argv
);
178 status
= net_sh_run(c
, new_ctx
, cmd
, argc
, argv
);
180 if (!NT_STATUS_IS_OK(status
)) {
181 d_fprintf(stderr
, "%s failed: %s\n", new_ctx
->whoami
,
188 static struct rpc_sh_cmd sh_cmds
[6] = {
190 { "info", NULL
, PI_SAMR
, rpc_sh_info
,
191 "Print information about the domain connected to" },
193 { "rights", net_rpc_rights_cmds
, 0, NULL
,
194 "List/Grant/Revoke user rights" },
196 { "share", net_rpc_share_cmds
, 0, NULL
,
197 "List/Add/Remove etc shares" },
199 { "user", net_rpc_user_cmds
, 0, NULL
,
200 "List/Add/Remove user info" },
202 { "account", net_rpc_acct_cmds
, 0, NULL
,
203 "Show/Change account policy settings" },
205 { NULL
, NULL
, 0, NULL
, NULL
}
208 int net_rpc_shell(struct net_context
*c
, int argc
, const char **argv
)
211 struct rpc_sh_ctx
*ctx
;
213 if (argc
!= 0 || c
->display_usage
) {
219 ctx
= TALLOC_P(NULL
, struct rpc_sh_ctx
);
221 d_fprintf(stderr
, "talloc failed\n");
225 status
= net_make_ipc_connection(c
, 0, &(ctx
->cli
));
226 if (!NT_STATUS_IS_OK(status
)) {
227 d_fprintf(stderr
, "Could not open connection: %s\n",
233 ctx
->whoami
= "net rpc";
236 status
= net_get_remote_domain_sid(ctx
->cli
, ctx
, &ctx
->domain_sid
,
238 if (!NT_STATUS_IS_OK(status
)) {
242 d_printf("Talking to domain %s (%s)\n", ctx
->domain_name
,
243 sid_string_tos(ctx
->domain_sid
));
252 if (asprintf(&prompt
, "%s> ", this_ctx
->whoami
) < 0) {
256 line
= smb_readline(prompt
, NULL
, completion_fn
);
263 ret
= poptParseArgvString(line
, &argc
, &argv
);
264 if (ret
== POPT_ERROR_NOARG
) {
269 d_fprintf(stderr
, "cmdline invalid: %s\n",
275 if ((line
[0] != '\n') &&
276 (!net_sh_process(c
, this_ctx
, argc
, argv
))) {
283 cli_shutdown(ctx
->cli
);