LCOV - code coverage report
Current view: top level - source4/torture/libnet - libnet_rpc.c (source / functions) Hit Total Coverage
Test: coverage report for master 2f515e9b Lines: 71 107 66.4 %
Date: 2024-04-21 15:09:00 Functions: 7 7 100.0 %

          Line data    Source code
       1             : /*
       2             :    Unix SMB/CIFS implementation.
       3             :    Test suite for libnet calls.
       4             : 
       5             :    Copyright (C) Rafal Szczesniak 2005
       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 "lib/cmdline/cmdline.h"
      23             : #include "libnet/libnet.h"
      24             : #include "libcli/security/security.h"
      25             : #include "librpc/gen_ndr/ndr_lsa.h"
      26             : #include "librpc/gen_ndr/ndr_samr.h"
      27             : #include "librpc/gen_ndr/ndr_srvsvc.h"
      28             : #include "torture/rpc/torture_rpc.h"
      29             : #include "torture/libnet/proto.h"
      30             : #include "param/param.h"
      31             : 
      32             : 
      33          25 : static bool test_connect_service(struct torture_context *tctx,
      34             :                                  struct libnet_context *ctx,
      35             :                                  const struct ndr_interface_table *iface,
      36             :                                  const char *binding_string,
      37             :                                  const char *hostname,
      38             :                                  const enum libnet_RpcConnect_level level,
      39             :                                  bool badcreds, NTSTATUS expected_status)
      40             : {
      41           0 :         NTSTATUS status;
      42           0 :         struct libnet_RpcConnect connect_r;
      43          25 :         ZERO_STRUCT(connect_r);
      44             : 
      45          25 :         connect_r.level            = level;
      46          25 :         connect_r.in.binding       = binding_string;
      47          25 :         connect_r.in.name          = hostname;
      48          25 :         connect_r.in.dcerpc_iface  = iface;
      49             : 
      50             :         /* if bad credentials are needed, set baduser%badpassword instead
      51             :            of default commandline-passed credentials */
      52          25 :         if (badcreds) {
      53          10 :                 cli_credentials_set_username(ctx->cred, "baduser", CRED_SPECIFIED);
      54          10 :                 cli_credentials_set_password(ctx->cred, "badpassword", CRED_SPECIFIED);
      55             :         }
      56             : 
      57          25 :         status = libnet_RpcConnect(ctx, ctx, &connect_r);
      58             : 
      59          25 :         if (!NT_STATUS_EQUAL(status, expected_status)) {
      60           0 :                 torture_comment(tctx, "Connecting to rpc service %s on %s.\n\tFAILED. Expected: %s."
      61             :                        "Received: %s\n",
      62           0 :                        connect_r.in.dcerpc_iface->name, connect_r.in.binding, nt_errstr(expected_status),
      63             :                        nt_errstr(status));
      64             : 
      65           0 :                 return false;
      66             :         }
      67             : 
      68          25 :         torture_comment(tctx, "PASSED. Expected: %s, received: %s\n", nt_errstr(expected_status),
      69             :                nt_errstr(status));
      70             : 
      71          25 :         if (connect_r.level == LIBNET_RPC_CONNECT_DC_INFO && NT_STATUS_IS_OK(status)) {
      72           3 :                 torture_comment(tctx, "Domain Controller Info:\n");
      73           3 :                 torture_comment(tctx, "\tDomain Name:\t %s\n", connect_r.out.domain_name);
      74           3 :                 torture_comment(tctx, "\tDomain SID:\t %s\n", dom_sid_string(ctx, connect_r.out.domain_sid));
      75           3 :                 torture_comment(tctx, "\tRealm:\t\t %s\n", connect_r.out.realm);
      76           3 :                 torture_comment(tctx, "\tGUID:\t\t %s\n", GUID_string(ctx, connect_r.out.guid));
      77             : 
      78          22 :         } else if (!NT_STATUS_IS_OK(status)) {
      79          10 :                 torture_comment(tctx, "Error string: %s\n", connect_r.out.error_string);
      80             :         }
      81             : 
      82          25 :         return true;
      83             : }
      84             : 
      85             : 
      86           5 : static bool torture_rpc_connect(struct torture_context *torture,
      87             :                                 const enum libnet_RpcConnect_level level,
      88             :                                 const char *bindstr, const char *hostname)
      89             : {
      90           0 :         struct libnet_context *ctx;
      91             : 
      92           5 :         ctx = libnet_context_init(torture->ev, torture->lp_ctx);
      93           5 :         ctx->cred = samba_cmdline_get_creds();
      94             : 
      95           5 :         torture_comment(torture, "Testing connection to LSA interface\n");
      96             : 
      97           5 :         if (!test_connect_service(torture, ctx, &ndr_table_lsarpc, bindstr,
      98           5 :                                   hostname, level, false, NT_STATUS_OK)) {
      99           0 :                 torture_comment(torture, "failed to connect LSA interface\n");
     100           0 :                 return false;
     101             :         }
     102             : 
     103           5 :         torture_comment(torture, "Testing connection to SAMR interface\n");
     104           5 :         if (!test_connect_service(torture, ctx, &ndr_table_samr, bindstr,
     105           5 :                                   hostname, level, false, NT_STATUS_OK)) {
     106           0 :                 torture_comment(torture, "failed to connect SAMR interface\n");
     107           0 :                 return false;
     108             :         }
     109             : 
     110           5 :         torture_comment(torture, "Testing connection to SRVSVC interface\n");
     111           5 :         if (!test_connect_service(torture, ctx, &ndr_table_srvsvc, bindstr,
     112           5 :                                   hostname, level, false, NT_STATUS_OK)) {
     113           0 :                 torture_comment(torture, "failed to connect SRVSVC interface\n");
     114           0 :                 return false;
     115             :         }
     116             : 
     117           5 :         torture_comment(torture, "Testing connection to LSA interface with wrong credentials\n");
     118           5 :         if (!test_connect_service(torture, ctx, &ndr_table_lsarpc, bindstr,
     119           5 :                                   hostname, level, true, NT_STATUS_LOGON_FAILURE)) {
     120           0 :                 torture_comment(torture, "failed to test wrong credentials on LSA interface\n");
     121           0 :                 return false;
     122             :         }
     123             : 
     124           5 :         torture_comment(torture, "Testing connection to SAMR interface with wrong credentials\n");
     125           5 :         if (!test_connect_service(torture, ctx, &ndr_table_samr, bindstr,
     126           5 :                                   hostname, level, true, NT_STATUS_LOGON_FAILURE)) {
     127           0 :                 torture_comment(torture, "failed to test wrong credentials on SAMR interface\n");
     128           0 :                 return false;
     129             :         }
     130             : 
     131           5 :         talloc_free(ctx);
     132             : 
     133           5 :         return true;
     134             : }
     135             : 
     136             : 
     137           1 : bool torture_rpc_connect_srv(struct torture_context *torture)
     138             : {
     139           1 :         const enum libnet_RpcConnect_level level = LIBNET_RPC_CONNECT_SERVER;
     140           0 :         NTSTATUS status;
     141           0 :         struct dcerpc_binding *binding;
     142           0 :         const char *host;
     143             : 
     144           1 :         status = torture_rpc_binding(torture, &binding);
     145           1 :         if (!NT_STATUS_IS_OK(status)) {
     146           0 :                 return false;
     147             :         }
     148             : 
     149           1 :         host = dcerpc_binding_get_string_option(binding, "host");
     150             : 
     151           1 :         return torture_rpc_connect(torture, level, NULL, host);
     152             : }
     153             : 
     154             : 
     155           1 : bool torture_rpc_connect_pdc(struct torture_context *torture)
     156             : {
     157           1 :         const enum libnet_RpcConnect_level level = LIBNET_RPC_CONNECT_PDC;
     158           0 :         NTSTATUS status;
     159           0 :         struct dcerpc_binding *binding;
     160           0 :         const char *domain_name;
     161             : 
     162           1 :         status = torture_rpc_binding(torture, &binding);
     163           1 :         if (!NT_STATUS_IS_OK(status)) {
     164           0 :                 return false;
     165             :         }
     166             : 
     167             :         /* we're accessing domain controller so the domain name should be
     168             :            passed (it's going to be resolved to dc name and address) instead
     169             :            of specific server name. */
     170           1 :         domain_name = lpcfg_workgroup(torture->lp_ctx);
     171           1 :         return torture_rpc_connect(torture, level, NULL, domain_name);
     172             : }
     173             : 
     174             : 
     175           1 : bool torture_rpc_connect_dc(struct torture_context *torture)
     176             : {
     177           1 :         const enum libnet_RpcConnect_level level = LIBNET_RPC_CONNECT_DC;
     178           0 :         NTSTATUS status;
     179           0 :         struct dcerpc_binding *binding;
     180           0 :         const char *domain_name;
     181             : 
     182           1 :         status = torture_rpc_binding(torture, &binding);
     183           1 :         if (!NT_STATUS_IS_OK(status)) {
     184           0 :                 return false;
     185             :         }
     186             : 
     187             :         /* we're accessing domain controller so the domain name should be
     188             :            passed (it's going to be resolved to dc name and address) instead
     189             :            of specific server name. */
     190           1 :         domain_name = lpcfg_workgroup(torture->lp_ctx);
     191           1 :         return torture_rpc_connect(torture, level, NULL, domain_name);
     192             : }
     193             : 
     194             : 
     195           1 : bool torture_rpc_connect_dc_info(struct torture_context *torture)
     196             : {
     197           1 :         const enum libnet_RpcConnect_level level = LIBNET_RPC_CONNECT_DC_INFO;
     198           0 :         NTSTATUS status;
     199           0 :         struct dcerpc_binding *binding;
     200           0 :         const char *domain_name;
     201             : 
     202           1 :         status = torture_rpc_binding(torture, &binding);
     203           1 :         if (!NT_STATUS_IS_OK(status)) {
     204           0 :                 return false;
     205             :         }
     206             : 
     207             :         /* we're accessing domain controller so the domain name should be
     208             :            passed (it's going to be resolved to dc name and address) instead
     209             :            of specific server name. */
     210           1 :         domain_name = lpcfg_workgroup(torture->lp_ctx);
     211           1 :         return torture_rpc_connect(torture, level, NULL, domain_name);
     212             : }
     213             : 
     214             : 
     215           1 : bool torture_rpc_connect_binding(struct torture_context *torture)
     216             : {
     217           1 :         const enum libnet_RpcConnect_level level = LIBNET_RPC_CONNECT_BINDING;
     218           0 :         NTSTATUS status;
     219           0 :         struct dcerpc_binding *binding;
     220           0 :         const char *bindstr;
     221             : 
     222           1 :         status = torture_rpc_binding(torture, &binding);
     223           1 :         if (!NT_STATUS_IS_OK(status)) {
     224           0 :                 return false;
     225             :         }
     226             : 
     227           1 :         bindstr = dcerpc_binding_string(torture, binding);
     228             : 
     229           1 :         return torture_rpc_connect(torture, level, bindstr, NULL);
     230             : }

Generated by: LCOV version 1.14