LCOV - code coverage report
Current view: top level - source3/utils - net_rpc_shell.c (source / functions) Hit Total Coverage
Test: coverage report for master 2f515e9b Lines: 0 142 0.0 %
Date: 2024-04-21 15:09:00 Functions: 0 5 0.0 %

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

Generated by: LCOV version 1.14