LCOV - code coverage report
Current view: top level - source4/torture/rpc - mgmt.c (source / functions) Hit Total Coverage
Test: coverage report for v4-17-test 1498b464 Lines: 80 123 65.0 %
Date: 2024-06-13 04:01:37 Functions: 6 7 85.7 %

          Line data    Source code
       1             : /*
       2             :    Unix SMB/CIFS implementation.
       3             :    test suite for mgmt rpc operations
       4             : 
       5             :    Copyright (C) Andrew Tridgell 2003
       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 "librpc/gen_ndr/ndr_mgmt_c.h"
      23             : #include "auth/gensec/gensec.h"
      24             : #include "librpc/ndr/ndr_table.h"
      25             : #include "torture/rpc/torture_rpc.h"
      26             : #include "param/param.h"
      27             : 
      28             : 
      29             : /*
      30             :   ask the server what interface IDs are available on this endpoint
      31             : */
      32          54 : bool test_inq_if_ids(struct torture_context *tctx,
      33             :                      struct dcerpc_binding_handle *b,
      34             :                      TALLOC_CTX *mem_ctx,
      35             :                      bool (*per_id_test)(struct torture_context *,
      36             :                                          const struct ndr_interface_table *iface,
      37             :                                          TALLOC_CTX *mem_ctx,
      38             :                                          struct ndr_syntax_id *id),
      39             :                      const void *priv)
      40             : {
      41             :         struct mgmt_inq_if_ids r;
      42             :         struct rpc_if_id_vector_t *vector;
      43             :         int i;
      44             : 
      45          54 :         vector = talloc(mem_ctx, struct rpc_if_id_vector_t);
      46          54 :         r.out.if_id_vector = &vector;
      47             : 
      48          54 :         torture_assert_ntstatus_ok(tctx,
      49             :                 dcerpc_mgmt_inq_if_ids_r(b, mem_ctx, &r),
      50             :                 "inq_if_ids failed");
      51             : 
      52          54 :         torture_assert_werr_ok(tctx,
      53             :                 r.out.result,
      54             :                 "inq_if_ids gave unexpected error code");
      55             : 
      56          54 :         if (!vector) {
      57           0 :                 torture_comment(tctx, "inq_if_ids gave NULL if_id_vector\n");
      58           0 :                 return false;
      59             :         }
      60             : 
      61         180 :         for (i=0;i<vector->count;i++) {
      62         126 :                 struct ndr_syntax_id *id = vector->if_id[i].id;
      63         126 :                 if (!id) continue;
      64             : 
      65         378 :                 torture_comment(tctx, "\tuuid %s  version 0x%08x  '%s'\n",
      66         126 :                        GUID_string(mem_ctx, &id->uuid),
      67             :                        id->if_version,
      68         126 :                        ndr_interface_name(&id->uuid, id->if_version));
      69             : 
      70         126 :                 if (per_id_test) {
      71           0 :                         per_id_test(tctx, priv, mem_ctx, id);
      72             :                 }
      73             :         }
      74             : 
      75          54 :         return true;
      76             : }
      77             : 
      78          54 : static bool test_inq_stats(struct torture_context *tctx,
      79             :                            struct dcerpc_binding_handle *b,
      80             :                            TALLOC_CTX *mem_ctx)
      81             : {
      82             :         struct mgmt_inq_stats r;
      83             :         struct mgmt_statistics statistics;
      84             : 
      85          54 :         r.in.max_count = MGMT_STATS_ARRAY_MAX_SIZE;
      86          54 :         r.in.unknown = 0;
      87          54 :         r.out.statistics = &statistics;
      88             : 
      89          54 :         torture_assert_ntstatus_ok(tctx,
      90             :                 dcerpc_mgmt_inq_stats_r(b, mem_ctx, &r),
      91             :                 "inq_stats failed");
      92             : 
      93          54 :         if (statistics.count != MGMT_STATS_ARRAY_MAX_SIZE) {
      94           0 :                 torture_comment(tctx, "Unexpected array size %d\n", statistics.count);
      95           0 :                 return false;
      96             :         }
      97             : 
      98         216 :         torture_comment(tctx, "\tcalls_in %6d  calls_out %6d\n\tpkts_in  %6d  pkts_out  %6d\n",
      99          54 :                statistics.statistics[MGMT_STATS_CALLS_IN],
     100          54 :                statistics.statistics[MGMT_STATS_CALLS_OUT],
     101          54 :                statistics.statistics[MGMT_STATS_PKTS_IN],
     102          54 :                statistics.statistics[MGMT_STATS_PKTS_OUT]);
     103             : 
     104          54 :         return true;
     105             : }
     106             : 
     107           0 : static bool test_inq_princ_name_size(struct torture_context *tctx,
     108             :                                      struct dcerpc_binding_handle *b,
     109             :                                      uint32_t authn_proto,
     110             :                                      const char *expected_princ_name)
     111             : {
     112             :         struct mgmt_inq_princ_name r;
     113             :         uint32_t len, i;
     114             : 
     115           0 :         len = strlen(expected_princ_name);
     116             : 
     117           0 :         r.in.authn_proto = authn_proto;
     118             : 
     119             :         /*
     120             :          * 0 gives NT_STATUS_RPC_BAD_STUB_DATA
     121             :          */
     122             : 
     123           0 :         for (i=1; i <= len; i++) {
     124           0 :                 r.in.princ_name_size = i;
     125             : 
     126           0 :                 torture_assert_ntstatus_ok(tctx,
     127             :                         dcerpc_mgmt_inq_princ_name_r(b, tctx, &r),
     128             :                         "mgmt_inq_princ_name failed");
     129           0 :                 torture_assert_werr_equal(tctx,
     130             :                         r.out.result,
     131             :                         WERR_INSUFFICIENT_BUFFER,
     132             :                         "mgmt_inq_princ_name failed");
     133             :         }
     134             : 
     135           0 :         r.in.princ_name_size = len + 1;
     136             : 
     137           0 :         torture_assert_ntstatus_ok(tctx,
     138             :                 dcerpc_mgmt_inq_princ_name_r(b, tctx, &r),
     139             :                 "mgmt_inq_princ_name failed");
     140           0 :         torture_assert_werr_ok(tctx,
     141             :                 r.out.result,
     142             :                 "mgmt_inq_princ_name failed");
     143             : 
     144           0 :         return true;
     145             : }
     146             : 
     147          54 : static bool test_inq_princ_name(struct torture_context *tctx,
     148             :                                 struct dcerpc_binding_handle *b,
     149             :                                 TALLOC_CTX *mem_ctx)
     150             : {
     151             :         NTSTATUS status;
     152             :         struct mgmt_inq_princ_name r;
     153             :         int i;
     154          54 :         bool ret = false;
     155             : 
     156       13878 :         for (i=0;i<256;i++) {
     157       13824 :                 r.in.authn_proto = i;  /* DCERPC_AUTH_TYPE_* */
     158       13824 :                 r.in.princ_name_size = 100;
     159             : 
     160       13824 :                 status = dcerpc_mgmt_inq_princ_name_r(b, mem_ctx, &r);
     161       13824 :                 if (!NT_STATUS_IS_OK(status)) {
     162       13824 :                         continue;
     163             :                 }
     164           0 :                 if (W_ERROR_IS_OK(r.out.result)) {
     165           0 :                         const char *name = gensec_get_name_by_authtype(NULL, i);
     166           0 :                         ret = true;
     167           0 :                         if (name) {
     168           0 :                                 torture_comment(tctx, "\tprinciple name for proto %u (%s) is '%s'\n",
     169             :                                        i, name, r.out.princ_name);
     170             :                         } else {
     171           0 :                                 torture_comment(tctx, "\tprinciple name for proto %u is '%s'\n",
     172             :                                        i, r.out.princ_name);
     173             :                         }
     174             : 
     175           0 :                         switch (i) {
     176           0 :                         case DCERPC_AUTH_TYPE_KRB5:
     177             :                         case DCERPC_AUTH_TYPE_NTLMSSP:
     178             :                         case DCERPC_AUTH_TYPE_SPNEGO:
     179           0 :                                 torture_assert(tctx,
     180             :                                         test_inq_princ_name_size(tctx, b, i, r.out.princ_name),
     181             :                                         "failed");
     182           0 :                                 break;
     183           0 :                         case DCERPC_AUTH_TYPE_SCHANNEL:
     184             :                                 /*
     185             :                                  * for some reason schannel behaves differently
     186             :                                  *
     187             :                                  */
     188             :                         default:
     189           0 :                                 break;
     190             :                         }
     191           0 :                 }
     192             :         }
     193             : 
     194          54 :         if (!ret) {
     195          54 :                 torture_comment(tctx, "\tno principle names?\n");
     196             :         }
     197             : 
     198          54 :         return true;
     199             : }
     200             : 
     201          54 : static bool test_is_server_listening(struct torture_context *tctx,
     202             :                                      struct dcerpc_binding_handle *b,
     203             :                                      TALLOC_CTX *mem_ctx)
     204             : {
     205             :         struct mgmt_is_server_listening r;
     206          54 :         r.out.status = talloc(mem_ctx, uint32_t);
     207             : 
     208          54 :         torture_assert_ntstatus_ok(tctx,
     209             :                 dcerpc_mgmt_is_server_listening_r(b, mem_ctx, &r),
     210             :                 "is_server_listening failed");
     211             : 
     212          54 :         if (*r.out.status != 0 || r.out.result == 0) {
     213           0 :                 torture_comment(tctx, "\tserver is NOT listening\n");
     214             :         } else {
     215          54 :                 torture_comment(tctx, "\tserver is listening\n");
     216             :         }
     217             : 
     218          54 :         return true;
     219             : }
     220             : 
     221          54 : static bool test_stop_server_listening(struct torture_context *tctx,
     222             :                                        struct dcerpc_binding_handle *b,
     223             :                                        TALLOC_CTX *mem_ctx)
     224             : {
     225             :         struct mgmt_stop_server_listening r;
     226             : 
     227          54 :         torture_assert_ntstatus_ok(tctx,
     228             :                 dcerpc_mgmt_stop_server_listening_r(b, mem_ctx, &r),
     229             :                 "stop_server_listening failed");
     230             : 
     231          54 :         if (!W_ERROR_IS_OK(r.out.result)) {
     232          54 :                 torture_comment(tctx, "\tserver refused to stop listening - %s\n", win_errstr(r.out.result));
     233             :         } else {
     234           0 :                 torture_comment(tctx, "\tserver allowed a stop_server_listening request\n");
     235           0 :                 return false;
     236             :         }
     237             : 
     238          54 :         return true;
     239             : }
     240             : 
     241             : 
     242           3 : bool torture_rpc_mgmt(struct torture_context *tctx)
     243             : {
     244             :         NTSTATUS status;
     245             :         struct dcerpc_pipe *p;
     246             :         TALLOC_CTX *mem_ctx, *loop_ctx;
     247           3 :         bool ret = true;
     248             :         const struct ndr_interface_list *l;
     249             :         struct dcerpc_binding *b;
     250             : 
     251           3 :         mem_ctx = talloc_init("torture_rpc_mgmt");
     252             : 
     253           3 :         status = torture_rpc_binding(tctx, &b);
     254           3 :         if (!NT_STATUS_IS_OK(status)) {
     255           0 :                 talloc_free(mem_ctx);
     256           0 :                 return false;
     257             :         }
     258             : 
     259         381 :         for (l=ndr_table_list();l;l=l->next) {
     260             :                 struct dcerpc_binding_handle *bh;
     261             : 
     262         378 :                 loop_ctx = talloc_named(mem_ctx, 0, "torture_rpc_mgmt loop context");
     263             : 
     264             :                 /* some interfaces are not mappable */
     265         612 :                 if (l->table->num_calls == 0 ||
     266         234 :                     strcmp(l->table->name, "mgmt") == 0) {
     267         147 :                         talloc_free(loop_ctx);
     268         147 :                         continue;
     269             :                 }
     270             : 
     271         231 :                 torture_comment(tctx, "\nTesting pipe '%s'\n", l->table->name);
     272             : 
     273         231 :                 status = dcerpc_epm_map_binding(loop_ctx, b, l->table,
     274             :                                                 tctx->ev, tctx->lp_ctx);
     275         231 :                 if (!NT_STATUS_IS_OK(status)) {
     276          24 :                         torture_comment(tctx, "Failed to map port for uuid %s\n",
     277          24 :                                    GUID_string(loop_ctx, &l->table->syntax_id.uuid));
     278          24 :                         talloc_free(loop_ctx);
     279          24 :                         continue;
     280             :                 }
     281             : 
     282         207 :                 lpcfg_set_cmdline(tctx->lp_ctx, "torture:binding", dcerpc_binding_string(loop_ctx, b));
     283             : 
     284         207 :                 status = torture_rpc_connection(tctx, &p, &ndr_table_mgmt);
     285         207 :                 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
     286         153 :                         torture_comment(tctx, "Interface not available - skipping\n");
     287         153 :                         talloc_free(loop_ctx);
     288         153 :                         continue;
     289             :                 }
     290             : 
     291          54 :                 if (!NT_STATUS_IS_OK(status)) {
     292           0 :                         talloc_free(loop_ctx);
     293           0 :                         torture_comment(tctx, "Interface not available (%s) - skipping\n", nt_errstr(status));
     294           0 :                         ret = false;
     295           0 :                         continue;
     296             :                 }
     297          54 :                 bh = p->binding_handle;
     298             : 
     299          54 :                 if (!test_is_server_listening(tctx, bh, loop_ctx)) {
     300           0 :                         ret = false;
     301             :                 }
     302             : 
     303          54 :                 if (!test_stop_server_listening(tctx, bh, loop_ctx)) {
     304           0 :                         ret = false;
     305             :                 }
     306             : 
     307          54 :                 if (!test_inq_stats(tctx, bh, loop_ctx)) {
     308           0 :                         ret = false;
     309             :                 }
     310             : 
     311          54 :                 if (!test_inq_princ_name(tctx, bh, loop_ctx)) {
     312           0 :                         ret = false;
     313             :                 }
     314             : 
     315          54 :                 if (!test_inq_if_ids(tctx, bh, loop_ctx, NULL, NULL)) {
     316           0 :                         ret = false;
     317             :                 }
     318             : 
     319             :         }
     320             : 
     321           3 :         return ret;
     322             : }

Generated by: LCOV version 1.13