root/source3/utils/net_rpc_shell.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. rpc_sh_info
  2. completion_fn
  3. net_sh_run
  4. net_sh_process
  5. net_rpc_shell

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

/* [<][>][^][v][top][bottom][index][help] */