LCOV - code coverage report
Current view: top level - source3/rpc_client - rpc_transport_np.c (source / functions) Hit Total Coverage
Test: coverage report for abartlet/fix-coverage dd10fb34 Lines: 50 78 64.1 %
Date: 2021-09-23 10:06:22 Functions: 4 5 80.0 %

          Line data    Source code
       1             : /*
       2             :  *  Unix SMB/CIFS implementation.
       3             :  *  RPC client transport over named pipes
       4             :  *  Copyright (C) Volker Lendecke 2009
       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             : #include "includes.h"
      21             : #include "../lib/util/tevent_ntstatus.h"
      22             : #include "librpc/rpc/dcerpc_util.h"
      23             : #include "rpc_client/rpc_transport.h"
      24             : #include "librpc/ndr/ndr_table.h"
      25             : #include "libcli/smb/smbXcli_base.h"
      26             : #include "libcli/smb/tstream_smbXcli_np.h"
      27             : #include "client.h"
      28             : 
      29             : #undef DBGC_CLASS
      30             : #define DBGC_CLASS DBGC_RPC_CLI
      31             : 
      32             : struct rpc_transport_np_init_state {
      33             :         struct rpc_cli_transport *transport;
      34             :         int retries;
      35             :         struct tevent_context *ev;
      36             :         struct smbXcli_conn *conn;
      37             :         int timeout;
      38             :         struct timeval abs_timeout;
      39             :         const char *pipe_name;
      40             :         struct smbXcli_session *session;
      41             :         struct smbXcli_tcon *tcon;
      42             :         uint16_t pid;
      43             : };
      44             : 
      45             : static void rpc_transport_np_init_pipe_open(struct tevent_req *subreq);
      46             : 
      47        9679 : struct tevent_req *rpc_transport_np_init_send(TALLOC_CTX *mem_ctx,
      48             :                                               struct tevent_context *ev,
      49             :                                               struct cli_state *cli,
      50             :                                               const struct ndr_interface_table *table)
      51             : {
      52             :         struct tevent_req *req;
      53             :         struct rpc_transport_np_init_state *state;
      54             :         struct tevent_req *subreq;
      55             : 
      56        9679 :         req = tevent_req_create(mem_ctx, &state,
      57             :                                 struct rpc_transport_np_init_state);
      58        9679 :         if (req == NULL) {
      59           0 :                 return NULL;
      60             :         }
      61             : 
      62        9679 :         if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
      63        9541 :                 state->tcon = cli->smb2.tcon;
      64        9541 :                 state->session = cli->smb2.session;
      65             :         } else {
      66         138 :                 state->tcon = cli->smb1.tcon;
      67         138 :                 state->session = cli->smb1.session;
      68         138 :                 state->pid = cli->smb1.pid;
      69             :         }
      70             : 
      71        9679 :         state->ev = ev;
      72        9679 :         state->conn = cli->conn;
      73        9679 :         state->timeout = cli->timeout;
      74        9679 :         state->abs_timeout = timeval_current_ofs_msec(cli->timeout);
      75        9679 :         state->pipe_name = dcerpc_default_transport_endpoint(state, NCACN_NP,
      76             :                                                              table);
      77        9679 :         if (tevent_req_nomem(state->pipe_name, req)) {
      78           0 :                 return tevent_req_post(req, ev);
      79             :         }
      80             : 
      81       18980 :         while (state->pipe_name[0] == '\\') {
      82           0 :                 state->pipe_name++;
      83             :         }
      84             : 
      85       46883 :         subreq = tstream_smbXcli_np_open_send(state, ev, state->conn,
      86       18980 :                                               state->session, state->tcon,
      87       18980 :                                               state->pid, state->timeout,
      88        9679 :                                               state->pipe_name);
      89        9679 :         if (tevent_req_nomem(subreq, req)) {
      90           0 :                 return tevent_req_post(req, ev);
      91             :         }
      92        9679 :         tevent_req_set_callback(subreq, rpc_transport_np_init_pipe_open, req);
      93             : 
      94        9679 :         return req;
      95             : }
      96             : 
      97           0 : static void rpc_transport_np_init_pipe_open_retry(struct tevent_context *ev,
      98             :                                                   struct tevent_timer *te,
      99             :                                                   struct timeval t,
     100             :                                                   void *priv_data)
     101             : {
     102             :         struct tevent_req *subreq;
     103           0 :         struct tevent_req *req = talloc_get_type(priv_data, struct tevent_req);
     104           0 :         struct rpc_transport_np_init_state *state = tevent_req_data(
     105             :                 req, struct rpc_transport_np_init_state);
     106             : 
     107           0 :         subreq = tstream_smbXcli_np_open_send(state, ev,
     108             :                                               state->conn,
     109             :                                               state->session,
     110             :                                               state->tcon,
     111           0 :                                               state->pid,
     112           0 :                                               state->timeout,
     113             :                                               state->pipe_name);
     114           0 :         if (tevent_req_nomem(subreq, req)) {
     115           0 :                 return;
     116             :         }
     117           0 :         tevent_req_set_callback(subreq, rpc_transport_np_init_pipe_open, req);
     118           0 :         state->retries++;
     119             : }
     120             : 
     121        9679 : static void rpc_transport_np_init_pipe_open(struct tevent_req *subreq)
     122             : {
     123        9679 :         struct tevent_req *req = tevent_req_callback_data(
     124             :                 subreq, struct tevent_req);
     125        9679 :         struct rpc_transport_np_init_state *state = tevent_req_data(
     126             :                 req, struct rpc_transport_np_init_state);
     127             :         NTSTATUS status;
     128             :         struct tstream_context *stream;
     129             : 
     130        9679 :         status = tstream_smbXcli_np_open_recv(subreq, state, &stream);
     131        9679 :         TALLOC_FREE(subreq);
     132        9679 :         if (NT_STATUS_EQUAL(status, NT_STATUS_PIPE_NOT_AVAILABLE)
     133           0 :                                 && (!timeval_expired(&state->abs_timeout))) {
     134             :                 struct tevent_timer *te;
     135             :                 /*
     136             :                  * Retry on STATUS_PIPE_NOT_AVAILABLE, Windows starts some
     137             :                  * servers (FssagentRpc) on demand.
     138             :                  */
     139           0 :                 DEBUG(2, ("RPC pipe %s not available, retry %d\n",
     140             :                           state->pipe_name, state->retries));
     141           0 :                 te = tevent_add_timer(state->ev, state,
     142             :                                  timeval_current_ofs_msec(100 * state->retries),
     143             :                                  rpc_transport_np_init_pipe_open_retry, req);
     144           0 :                 if (tevent_req_nomem(te, req)) {
     145           0 :                         DEBUG(2, ("Failed to create asynchronous "
     146             :                                         "tevent_timer"));
     147             :                 }
     148           0 :                 return;
     149             :         }
     150             : 
     151        9679 :         if (tevent_req_nterror(req, status)) {
     152           0 :                 return;
     153             :         }
     154             : 
     155        9679 :         status = rpc_transport_tstream_init(state,
     156             :                                             &stream,
     157             :                                             &state->transport);
     158        9679 :         if (tevent_req_nterror(req, status)) {
     159           0 :                 return;
     160             :         }
     161             : 
     162        9679 :         tevent_req_done(req);
     163             : }
     164             : 
     165        9679 : NTSTATUS rpc_transport_np_init_recv(struct tevent_req *req,
     166             :                                     TALLOC_CTX *mem_ctx,
     167             :                                     struct rpc_cli_transport **presult)
     168             : {
     169        9679 :         struct rpc_transport_np_init_state *state = tevent_req_data(
     170             :                 req, struct rpc_transport_np_init_state);
     171             :         NTSTATUS status;
     172             : 
     173        9679 :         if (tevent_req_is_nterror(req, &status)) {
     174           0 :                 return status;
     175             :         }
     176             : 
     177        9679 :         *presult = talloc_move(mem_ctx, &state->transport);
     178        9679 :         return NT_STATUS_OK;
     179             : }
     180             : 
     181        9679 : NTSTATUS rpc_transport_np_init(TALLOC_CTX *mem_ctx, struct cli_state *cli,
     182             :                                const struct ndr_interface_table *table,
     183             :                                struct rpc_cli_transport **presult)
     184             : {
     185        9679 :         TALLOC_CTX *frame = talloc_stackframe();
     186             :         struct tevent_context *ev;
     187             :         struct tevent_req *req;
     188        9679 :         NTSTATUS status = NT_STATUS_OK;
     189             : 
     190        9679 :         ev = samba_tevent_context_init(frame);
     191        9679 :         if (ev == NULL) {
     192           0 :                 status = NT_STATUS_NO_MEMORY;
     193           0 :                 goto fail;
     194             :         }
     195             : 
     196        9679 :         req = rpc_transport_np_init_send(frame, ev, cli, table);
     197        9679 :         if (req == NULL) {
     198           0 :                 status = NT_STATUS_NO_MEMORY;
     199           0 :                 goto fail;
     200             :         }
     201             : 
     202        9679 :         if (!tevent_req_poll_ntstatus(req, ev, &status)) {
     203           0 :                 goto fail;
     204             :         }
     205             : 
     206        9679 :         status = rpc_transport_np_init_recv(req, mem_ctx, presult);
     207        9679 :  fail:
     208        9679 :         TALLOC_FREE(frame);
     209        9679 :         return status;
     210             : }

Generated by: LCOV version 1.13