LCOV - code coverage report
Current view: top level - source3/rpcclient - cmd_epmapper.c (source / functions) Hit Total Coverage
Test: coverage report for master 2f515e9b Lines: 39 97 40.2 %
Date: 2024-04-21 15:09:00 Functions: 1 2 50.0 %

          Line data    Source code
       1             : /*
       2             :    Unix SMB/CIFS implementation.
       3             :    RPC pipe client
       4             : 
       5             :    Copyright (C) Volker Lendecke 2009
       6             : 
       7             :    This program is free software; you can redistribute it and/or modify
       8             :    it under the terms of the GNU General Public License as published by
       9             :    the Free Software Foundation; either version 3 of the License, or
      10             :    (at your option) any later version.
      11             : 
      12             :    This program is distributed in the hope that it will be useful,
      13             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      15             :    GNU General Public License for more details.
      16             : 
      17             :    You should have received a copy of the GNU General Public License
      18             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      19             : */
      20             : 
      21             : #include "includes.h"
      22             : #include "rpcclient.h"
      23             : #include "../librpc/gen_ndr/ndr_epmapper_c.h"
      24             : #include "librpc/ndr/ndr_table.h"
      25             : 
      26         232 : static NTSTATUS cmd_epmapper_map(struct rpc_pipe_client *p,
      27             :                                  TALLOC_CTX *mem_ctx,
      28             :                                  int argc, const char **argv)
      29             : {
      30         232 :         struct dcerpc_binding_handle *b = p->binding_handle;
      31             :         struct dcerpc_binding *map_binding;
      32             :         struct epm_twr_t map_tower;
      33             :         struct epm_twr_p_t towers[500];
      34             :         struct policy_handle entry_handle;
      35             :         struct ndr_syntax_id abstract_syntax;
      36             :         uint32_t num_towers;
      37         232 :         TALLOC_CTX *tmp_ctx = talloc_stackframe();
      38             :         NTSTATUS status;
      39             :         uint32_t result;
      40             :         uint32_t i;
      41             :         const struct ndr_interface_list *l;
      42         232 :         const char *interface_name = "lsarpc";
      43         232 :         enum dcerpc_transport_t transport = NCACN_NP;
      44         232 :         bool ok = false;
      45         232 :         struct GUID object_uuid = GUID_zero();
      46             : 
      47         232 :         if (argc > 4) {
      48           0 :                 d_fprintf(stderr,
      49             :                           "Usage: %s [interface_name] [transport] "
      50             :                           "[object_uuid]\n",
      51             :                           argv[0]);
      52           0 :                 return NT_STATUS_OK;
      53             :         }
      54             : 
      55         232 :         if (argc >= 2) {
      56           0 :                 interface_name = argv[1];
      57             :         }
      58             : 
      59       19952 :         for (l = ndr_table_list(); l != NULL; l = l->next) {
      60             : 
      61       19952 :                 ok = strequal(interface_name, l->table->name);
      62       19952 :                 if (ok) {
      63         232 :                         abstract_syntax = l->table->syntax_id;
      64         232 :                         break;
      65             :                 }
      66             :         }
      67             : 
      68         232 :         if (!ok) {
      69           0 :                 d_fprintf(stderr, "unknown interface: %s\n",
      70             :                         interface_name);
      71           0 :                 status = NT_STATUS_UNSUCCESSFUL;
      72           0 :                 goto done;
      73             :         }
      74             : 
      75         232 :         if (argc >= 3) {
      76           0 :                 transport = dcerpc_transport_by_name(argv[2]);
      77           0 :                 if (transport == NCA_UNKNOWN) {
      78           0 :                         d_fprintf(stderr, "unknown transport: %s\n",
      79           0 :                                 argv[2]);
      80           0 :                         status = NT_STATUS_UNSUCCESSFUL;
      81           0 :                         goto done;
      82             :                 }
      83             :         }
      84             : 
      85         232 :         if (argc >= 4) {
      86           0 :                 status = GUID_from_string(argv[3], &object_uuid);
      87           0 :                 if (!NT_STATUS_IS_OK(status)) {
      88           0 :                         goto done;
      89             :                 }
      90             :         }
      91             : 
      92             :         /* 127.0.0.1[0] => correct? needed? */
      93         232 :         status = dcerpc_parse_binding(tmp_ctx, "ncacn_np:127.0.0.1[0]",
      94             :                                       &map_binding);
      95         232 :         if (!NT_STATUS_IS_OK(status)) {
      96           0 :                 d_fprintf(stderr, "dcerpc_parse_binding returned %s\n",
      97             :                           nt_errstr(status));
      98           0 :                 goto done;
      99             :         }
     100             : 
     101         232 :         status = dcerpc_binding_set_transport(map_binding, transport);
     102         232 :         if (!NT_STATUS_IS_OK(status)) {
     103           0 :                 d_fprintf(stderr, "dcerpc_binding_set_transport returned %s\n",
     104             :                           nt_errstr(status));
     105           0 :                 goto done;
     106             :         }
     107             : 
     108         232 :         status = dcerpc_binding_set_abstract_syntax(map_binding,
     109             :                                                     &abstract_syntax);
     110         232 :         if (!NT_STATUS_IS_OK(status)) {
     111           0 :                 d_fprintf(stderr, "dcerpc_binding_set_abstract_syntax returned %s\n",
     112             :                           nt_errstr(status));
     113           0 :                 goto done;
     114             :         }
     115             : 
     116         232 :         status = dcerpc_binding_build_tower(tmp_ctx, map_binding,
     117             :                                             &map_tower.tower);
     118         232 :         if (!NT_STATUS_IS_OK(status)) {
     119           0 :                 d_fprintf(stderr, "dcerpc_binding_build_tower returned %s\n",
     120             :                           nt_errstr(status));
     121           0 :                 goto done;
     122             :         }
     123             : 
     124         232 :         ZERO_STRUCT(towers);
     125         232 :         ZERO_STRUCT(entry_handle);
     126             : 
     127         232 :         status = dcerpc_epm_Map(
     128             :                 b, tmp_ctx, &object_uuid,
     129             :                 &map_tower, &entry_handle, ARRAY_SIZE(towers),
     130             :                 &num_towers, towers, &result);
     131         232 :         if (!NT_STATUS_IS_OK(status)) {
     132           0 :                 d_fprintf(stderr, "dcerpc_epm_Map returned %s\n",
     133             :                           nt_errstr(status));
     134           0 :                 goto done;
     135             :         }
     136             : 
     137         232 :         if (result != EPMAPPER_STATUS_OK) {
     138           0 :                 d_fprintf(stderr, "epm_Map returned %u (0x%08X)\n",
     139             :                           result, result);
     140           0 :                 status = NT_STATUS_UNSUCCESSFUL;
     141           0 :                 goto done;
     142             :         }
     143             : 
     144         232 :         d_printf("num_tower[%u]\n", num_towers);
     145             : 
     146         696 :         for (i=0; i < num_towers; i++) {
     147             :                 struct dcerpc_binding *binding;
     148             : 
     149         464 :                 if (towers[i].twr == NULL) {
     150           0 :                         d_fprintf(stderr, "tower[%u] NULL\n", i);
     151           0 :                         break;
     152             :                 }
     153             : 
     154         464 :                 status = dcerpc_binding_from_tower(tmp_ctx, &towers[i].twr->tower,
     155             :                                                    &binding);
     156         464 :                 if (!NT_STATUS_IS_OK(status)) {
     157           0 :                         break;
     158             :                 }
     159             : 
     160         464 :                 d_printf("tower[%u] %s\n", i, dcerpc_binding_string(tmp_ctx, binding));
     161             :         }
     162         232 : done:
     163         232 :         TALLOC_FREE(tmp_ctx);
     164         232 :         return status;
     165             : }
     166             : 
     167           0 : static NTSTATUS cmd_epmapper_lookup(struct rpc_pipe_client *p,
     168             :                                     TALLOC_CTX *mem_ctx,
     169             :                                     int argc, const char **argv)
     170             : {
     171           0 :         struct dcerpc_binding_handle *b = p->binding_handle;
     172             :         struct policy_handle entry_handle;
     173             : 
     174           0 :         ZERO_STRUCT(entry_handle);
     175             : 
     176           0 :         while (true) {
     177           0 :                 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
     178             :                 uint32_t num_entries;
     179             :                 struct epm_entry_t entry;
     180             :                 NTSTATUS status;
     181             :                 char *guid_string;
     182             :                 struct dcerpc_binding *binding;
     183             :                 uint32_t result;
     184             : 
     185           0 :                 status = dcerpc_epm_Lookup(b, tmp_ctx,
     186             :                                    0, /* rpc_c_ep_all */
     187             :                                    NULL,
     188             :                                    NULL,
     189             :                                    0, /* rpc_c_vers_all */
     190             :                                    &entry_handle,
     191             :                                    1, /* max_ents */
     192             :                                    &num_entries, &entry,
     193             :                                    &result);
     194           0 :                 if (!NT_STATUS_IS_OK(status)) {
     195           0 :                         d_fprintf(stderr, "dcerpc_epm_Lookup returned %s\n",
     196             :                                   nt_errstr(status));
     197           0 :                         break;
     198             :                 }
     199             : 
     200           0 :                 if (result == EPMAPPER_STATUS_NO_MORE_ENTRIES) {
     201           0 :                         d_fprintf(stderr, "epm_Lookup no more entries\n");
     202           0 :                         break;
     203             :                 }
     204             : 
     205           0 :                 if (result != EPMAPPER_STATUS_OK) {
     206           0 :                         d_fprintf(stderr, "epm_Lookup returned %u (0x%08X)\n",
     207             :                                   result, result);
     208           0 :                         break;
     209             :                 }
     210             : 
     211           0 :                 if (num_entries != 1) {
     212           0 :                         d_fprintf(stderr, "epm_Lookup returned %d "
     213             :                                   "entries, expected one\n", (int)num_entries);
     214           0 :                         break;
     215             :                 }
     216             : 
     217           0 :                 guid_string = GUID_string(tmp_ctx, &entry.object);
     218           0 :                 if (guid_string == NULL) {
     219           0 :                         break;
     220             :                 }
     221             : 
     222           0 :                 status = dcerpc_binding_from_tower(tmp_ctx, &entry.tower->tower,
     223             :                                                    &binding);
     224           0 :                 if (!NT_STATUS_IS_OK(status)) {
     225           0 :                         break;
     226             :                 }
     227             : 
     228           0 :                 d_printf("%s %s: %s\n", guid_string,
     229             :                          dcerpc_binding_string(tmp_ctx, binding),
     230             :                          entry.annotation);
     231             : 
     232           0 :                 TALLOC_FREE(tmp_ctx);
     233             :         }
     234             : 
     235           0 :         return NT_STATUS_OK;
     236             : }
     237             : 
     238             : 
     239             : /* List of commands exported by this module */
     240             : 
     241             : struct cmd_set epmapper_commands[] = {
     242             : 
     243             :         {
     244             :                 .name = "EPMAPPER",
     245             :         },
     246             : 
     247             :         {
     248             :                 .name               = "epmmap",
     249             :                 .returntype         = RPC_RTYPE_NTSTATUS,
     250             :                 .ntfn               = cmd_epmapper_map,
     251             :                 .wfn                = NULL,
     252             :                 .table              = &ndr_table_epmapper,
     253             :                 .rpc_pipe           = NULL,
     254             :                 .description        = "Map a binding",
     255             :                 .usage              = "",
     256             :         },
     257             :         {
     258             :                 .name               = "epmlookup",
     259             :                 .returntype         = RPC_RTYPE_NTSTATUS,
     260             :                 .ntfn               = cmd_epmapper_lookup,
     261             :                 .wfn                = NULL,
     262             :                 .table              = &ndr_table_epmapper,
     263             :                 .rpc_pipe           = NULL,
     264             :                 .description        = "Lookup bindings",
     265             :                 .usage              = "",
     266             :         },
     267             :         {
     268             :                 .name = NULL,
     269             :         },
     270             : };

Generated by: LCOV version 1.14