2 * Unix SMB/CIFS implementation.
3 * RPC client transport over named pipes
4 * Copyright (C) Volker Lendecke 2009
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.
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.
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/>.
21 #include "../lib/util/tevent_ntstatus.h"
22 #include "rpc_client/rpc_transport.h"
23 #include "librpc/ndr/ndr_table.h"
24 #include "libcli/smb/smbXcli_base.h"
25 #include "libcli/smb/tstream_smbXcli_np.h"
29 #define DBGC_CLASS DBGC_RPC_CLI
31 struct rpc_transport_np_init_state
{
32 struct rpc_cli_transport
*transport
;
34 struct tevent_context
*ev
;
35 struct smbXcli_conn
*conn
;
37 struct timeval abs_timeout
;
38 const char *pipe_name
;
39 struct smbXcli_session
*session
;
40 struct smbXcli_tcon
*tcon
;
44 static void rpc_transport_np_init_pipe_open(struct tevent_req
*subreq
);
46 struct tevent_req
*rpc_transport_np_init_send(TALLOC_CTX
*mem_ctx
,
47 struct tevent_context
*ev
,
48 struct cli_state
*cli
,
49 const struct ndr_interface_table
*table
)
51 struct tevent_req
*req
;
52 struct rpc_transport_np_init_state
*state
;
53 struct tevent_req
*subreq
;
55 req
= tevent_req_create(mem_ctx
, &state
,
56 struct rpc_transport_np_init_state
);
61 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
62 state
->tcon
= cli
->smb2
.tcon
;
63 state
->session
= cli
->smb2
.session
;
65 state
->tcon
= cli
->smb1
.tcon
;
66 state
->session
= cli
->smb1
.session
;
67 state
->pid
= cli
->smb1
.pid
;
71 state
->conn
= cli
->conn
;
72 state
->timeout
= cli
->timeout
;
73 state
->abs_timeout
= timeval_current_ofs_msec(cli
->timeout
);
74 state
->pipe_name
= dcerpc_default_transport_endpoint(state
, NCACN_NP
,
76 if (tevent_req_nomem(state
->pipe_name
, req
)) {
77 return tevent_req_post(req
, ev
);
80 while (state
->pipe_name
[0] == '\\') {
84 subreq
= tstream_smbXcli_np_open_send(state
, ev
, state
->conn
,
85 state
->session
, state
->tcon
,
86 state
->pid
, state
->timeout
,
88 if (tevent_req_nomem(subreq
, req
)) {
89 return tevent_req_post(req
, ev
);
91 tevent_req_set_callback(subreq
, rpc_transport_np_init_pipe_open
, req
);
96 static void rpc_transport_np_init_pipe_open_retry(struct tevent_context
*ev
,
97 struct tevent_timer
*te
,
101 struct tevent_req
*subreq
;
102 struct tevent_req
*req
= talloc_get_type(priv_data
, struct tevent_req
);
103 struct rpc_transport_np_init_state
*state
= tevent_req_data(
104 req
, struct rpc_transport_np_init_state
);
106 subreq
= tstream_smbXcli_np_open_send(state
, ev
,
113 if (tevent_req_nomem(subreq
, req
)) {
116 tevent_req_set_callback(subreq
, rpc_transport_np_init_pipe_open
, req
);
120 static void rpc_transport_np_init_pipe_open(struct tevent_req
*subreq
)
122 struct tevent_req
*req
= tevent_req_callback_data(
123 subreq
, struct tevent_req
);
124 struct rpc_transport_np_init_state
*state
= tevent_req_data(
125 req
, struct rpc_transport_np_init_state
);
127 struct tstream_context
*stream
;
129 status
= tstream_smbXcli_np_open_recv(subreq
, state
, &stream
);
131 if (NT_STATUS_EQUAL(status
, NT_STATUS_PIPE_NOT_AVAILABLE
)
132 && (!timeval_expired(&state
->abs_timeout
))) {
133 struct tevent_timer
*te
;
135 * Retry on STATUS_PIPE_NOT_AVAILABLE, Windows starts some
136 * servers (FssagentRpc) on demand.
138 DEBUG(2, ("RPC pipe %s not available, retry %d\n",
139 state
->pipe_name
, state
->retries
));
140 te
= tevent_add_timer(state
->ev
, state
,
141 timeval_current_ofs_msec(100 * state
->retries
),
142 rpc_transport_np_init_pipe_open_retry
, req
);
143 if (tevent_req_nomem(te
, req
)) {
147 } else if (!NT_STATUS_IS_OK(status
)) {
148 tevent_req_nterror(req
, status
);
152 status
= rpc_transport_tstream_init(state
,
155 if (!NT_STATUS_IS_OK(status
)) {
156 tevent_req_nterror(req
, status
);
160 tevent_req_done(req
);
163 NTSTATUS
rpc_transport_np_init_recv(struct tevent_req
*req
,
165 struct rpc_cli_transport
**presult
)
167 struct rpc_transport_np_init_state
*state
= tevent_req_data(
168 req
, struct rpc_transport_np_init_state
);
171 if (tevent_req_is_nterror(req
, &status
)) {
175 *presult
= talloc_move(mem_ctx
, &state
->transport
);
179 NTSTATUS
rpc_transport_np_init(TALLOC_CTX
*mem_ctx
, struct cli_state
*cli
,
180 const struct ndr_interface_table
*table
,
181 struct rpc_cli_transport
**presult
)
183 TALLOC_CTX
*frame
= talloc_stackframe();
184 struct tevent_context
*ev
;
185 struct tevent_req
*req
;
186 NTSTATUS status
= NT_STATUS_OK
;
188 ev
= samba_tevent_context_init(frame
);
190 status
= NT_STATUS_NO_MEMORY
;
194 req
= rpc_transport_np_init_send(frame
, ev
, cli
, table
);
196 status
= NT_STATUS_NO_MEMORY
;
200 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
204 status
= rpc_transport_np_init_recv(req
, mem_ctx
, presult
);