WHATSNEW: Add release notes for Samba 4.3.4.
[Samba.git] / source3 / rpc_client / rpc_transport_np.c
blobb928b7c3474279c83b26e3cd4b8d5d057e89c75d
1 /*
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/>.
20 #include "includes.h"
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"
26 #include "client.h"
28 #undef DBGC_CLASS
29 #define DBGC_CLASS DBGC_RPC_CLI
31 struct rpc_transport_np_init_state {
32 struct rpc_cli_transport *transport;
33 int retries;
34 struct tevent_context *ev;
35 struct smbXcli_conn *conn;
36 int timeout;
37 struct timeval abs_timeout;
38 const char *pipe_name;
39 struct smbXcli_session *session;
40 struct smbXcli_tcon *tcon;
41 uint16_t pid;
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);
57 if (req == NULL) {
58 return NULL;
61 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
62 state->tcon = cli->smb2.tcon;
63 state->session = cli->smb2.session;
64 } else {
65 state->tcon = cli->smb1.tcon;
66 state->session = cli->smb1.session;
67 state->pid = cli->smb1.pid;
70 state->ev = ev;
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,
75 table);
76 if (tevent_req_nomem(state->pipe_name, req)) {
77 return tevent_req_post(req, ev);
80 while (state->pipe_name[0] == '\\') {
81 state->pipe_name++;
84 subreq = tstream_smbXcli_np_open_send(state, ev, state->conn,
85 state->session, state->tcon,
86 state->pid, state->timeout,
87 state->pipe_name);
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);
93 return req;
96 static void rpc_transport_np_init_pipe_open_retry(struct tevent_context *ev,
97 struct tevent_timer *te,
98 struct timeval t,
99 void *priv_data)
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,
107 state->conn,
108 state->session,
109 state->tcon,
110 state->pid,
111 state->timeout,
112 state->pipe_name);
113 if (tevent_req_nomem(subreq, req)) {
114 return;
116 tevent_req_set_callback(subreq, rpc_transport_np_init_pipe_open, req);
117 state->retries++;
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);
126 NTSTATUS status;
127 struct tstream_context *stream;
129 status = tstream_smbXcli_np_open_recv(subreq, state, &stream);
130 TALLOC_FREE(subreq);
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)) {
144 return;
146 return;
147 } else if (!NT_STATUS_IS_OK(status)) {
148 tevent_req_nterror(req, status);
149 return;
152 status = rpc_transport_tstream_init(state,
153 &stream,
154 &state->transport);
155 if (!NT_STATUS_IS_OK(status)) {
156 tevent_req_nterror(req, status);
157 return;
160 tevent_req_done(req);
163 NTSTATUS rpc_transport_np_init_recv(struct tevent_req *req,
164 TALLOC_CTX *mem_ctx,
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);
169 NTSTATUS status;
171 if (tevent_req_is_nterror(req, &status)) {
172 return status;
175 *presult = talloc_move(mem_ctx, &state->transport);
176 return NT_STATUS_OK;
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);
189 if (ev == NULL) {
190 status = NT_STATUS_NO_MEMORY;
191 goto fail;
194 req = rpc_transport_np_init_send(frame, ev, cli, table);
195 if (req == NULL) {
196 status = NT_STATUS_NO_MEMORY;
197 goto fail;
200 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
201 goto fail;
204 status = rpc_transport_np_init_recv(req, mem_ctx, presult);
205 fail:
206 TALLOC_FREE(frame);
207 return status;