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