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/>.
23 #define DBGC_CLASS DBGC_RPC_CLI
25 struct rpc_transport_np_state {
26 struct cli_state *cli;
27 const char *pipe_name;
31 static int rpc_transport_np_state_destructor(struct rpc_transport_np_state *s)
34 ret = cli_close(s->cli, s->fnum);
36 DEBUG(1, ("rpc_transport_np_state_destructor: cli_close "
37 "failed on pipe %s. Error was %s\n", s->pipe_name,
40 DEBUG(10, ("rpc_pipe_destructor: closed %s\n", s->pipe_name));
42 * We can't do much on failure
47 struct rpc_np_write_state {
52 static void rpc_np_write_done(struct tevent_req *subreq);
54 static struct tevent_req *rpc_np_write_send(TALLOC_CTX *mem_ctx,
55 struct event_context *ev,
56 const uint8_t *data, size_t size,
59 struct rpc_transport_np_state *np_transport = talloc_get_type_abort(
60 priv, struct rpc_transport_np_state);
61 struct tevent_req *req, *subreq;
62 struct rpc_np_write_state *state;
64 req = tevent_req_create(mem_ctx, &state, struct rpc_np_write_state);
70 subreq = cli_write_andx_send(mem_ctx, ev, np_transport->cli,
72 8, /* 8 means message mode. */
74 if (tevent_req_nomem(subreq, req)) {
75 return tevent_req_post(req, ev);
77 tevent_req_set_callback(subreq, rpc_np_write_done, req);
81 static void rpc_np_write_done(struct tevent_req *subreq)
83 struct tevent_req *req = tevent_req_callback_data(
84 subreq, struct tevent_req);
85 struct rpc_np_write_state *state = tevent_req_data(
86 req, struct rpc_np_write_state);
89 status = cli_write_andx_recv(subreq, &state->written);
91 if (!NT_STATUS_IS_OK(status)) {
92 tevent_req_nterror(req, status);
98 static NTSTATUS rpc_np_write_recv(struct tevent_req *req, ssize_t *pwritten)
100 struct rpc_np_write_state *state = tevent_req_data(
101 req, struct rpc_np_write_state);
104 if (tevent_req_is_nterror(req, &status)) {
107 *pwritten = state->written;
111 struct rpc_np_read_state {
117 static void rpc_np_read_done(struct tevent_req *subreq);
119 static struct tevent_req *rpc_np_read_send(TALLOC_CTX *mem_ctx,
120 struct event_context *ev,
121 uint8_t *data, size_t size,
124 struct rpc_transport_np_state *np_transport = talloc_get_type_abort(
125 priv, struct rpc_transport_np_state);
126 struct tevent_req *req, *subreq;
127 struct rpc_np_read_state *state;
129 req = tevent_req_create(mem_ctx, &state, struct rpc_np_read_state);
136 subreq = cli_read_andx_send(mem_ctx, ev, np_transport->cli,
137 np_transport->fnum, 0, size);
138 if (subreq == NULL) {
141 tevent_req_set_callback(subreq, rpc_np_read_done, req);
148 static void rpc_np_read_done(struct tevent_req *subreq)
150 struct tevent_req *req = tevent_req_callback_data(
151 subreq, struct tevent_req);
152 struct rpc_np_read_state *state = tevent_req_data(
153 req, struct rpc_np_read_state);
157 status = cli_read_andx_recv(subreq, &state->received, &rcvbuf);
159 * We can't TALLOC_FREE(subreq) as usual here, as rcvbuf still is a
162 if (NT_STATUS_EQUAL(status, NT_STATUS_BUFFER_TOO_SMALL)) {
163 status = NT_STATUS_OK;
165 if (!NT_STATUS_IS_OK(status)) {
167 tevent_req_nterror(req, status);
171 if (state->received > state->size) {
173 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
177 memcpy(state->data, rcvbuf, state->received);
178 tevent_req_done(req);
181 static NTSTATUS rpc_np_read_recv(struct tevent_req *req, ssize_t *preceived)
183 struct rpc_np_read_state *state = tevent_req_data(
184 req, struct rpc_np_read_state);
187 if (tevent_req_is_nterror(req, &status)) {
190 *preceived = state->received;
194 struct rpc_np_trans_state {
200 static void rpc_np_trans_done(struct tevent_req *subreq);
202 static struct tevent_req *rpc_np_trans_send(TALLOC_CTX *mem_ctx,
203 struct event_context *ev,
204 uint8_t *data, size_t data_len,
205 uint32_t max_rdata_len,
208 struct rpc_transport_np_state *np_transport = talloc_get_type_abort(
209 priv, struct rpc_transport_np_state);
210 struct tevent_req *req, *subreq;
211 struct rpc_np_trans_state *state;
213 req = tevent_req_create(mem_ctx, &state, struct rpc_np_trans_state);
218 SSVAL(state->setup+0, 0, TRANSACT_DCERPCCMD);
219 SSVAL(state->setup+1, 0, np_transport->fnum);
221 subreq = cli_trans_send(
222 state, ev, np_transport->cli, SMBtrans,
223 "\\PIPE\\", 0, 0, 0, state->setup, 2, 0,
224 NULL, 0, 0, data, data_len, max_rdata_len);
225 if (subreq == NULL) {
228 tevent_req_set_callback(subreq, rpc_np_trans_done, req);
236 static void rpc_np_trans_done(struct tevent_req *subreq)
238 struct tevent_req *req = tevent_req_callback_data(
239 subreq, struct tevent_req);
240 struct rpc_np_trans_state *state = tevent_req_data(
241 req, struct rpc_np_trans_state);
244 status = cli_trans_recv(subreq, state, NULL, NULL, NULL, NULL,
245 &state->rdata, &state->rdata_len);
247 if (!NT_STATUS_IS_OK(status)) {
248 tevent_req_nterror(req, status);
251 tevent_req_done(req);
254 static NTSTATUS rpc_np_trans_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
255 uint8_t **prdata, uint32_t *prdata_len)
257 struct rpc_np_trans_state *state = tevent_req_data(
258 req, struct rpc_np_trans_state);
261 if (tevent_req_is_nterror(req, &status)) {
264 *prdata = talloc_move(mem_ctx, &state->rdata);
265 *prdata_len = state->rdata_len;
269 struct rpc_transport_np_init_state {
270 struct rpc_cli_transport *transport;
271 struct rpc_transport_np_state *transport_np;
274 static void rpc_transport_np_init_pipe_open(struct tevent_req *subreq);
276 struct tevent_req *rpc_transport_np_init_send(TALLOC_CTX *mem_ctx,
277 struct event_context *ev,
278 struct cli_state *cli,
279 const struct ndr_syntax_id *abstract_syntax)
281 struct tevent_req *req, *subreq;
282 struct rpc_transport_np_init_state *state;
284 req = tevent_req_create(mem_ctx, &state,
285 struct rpc_transport_np_init_state);
290 state->transport = talloc(state, struct rpc_cli_transport);
291 if (tevent_req_nomem(state->transport, req)) {
292 return tevent_req_post(req, ev);
294 state->transport_np = talloc(state->transport,
295 struct rpc_transport_np_state);
296 if (tevent_req_nomem(state->transport_np, req)) {
297 return tevent_req_post(req, ev);
299 state->transport->priv = state->transport_np;
301 state->transport_np->pipe_name = get_pipe_name_from_iface(
303 state->transport_np->cli = cli;
305 subreq = cli_ntcreate_send(
306 state, ev, cli, state->transport_np->pipe_name, 0,
307 DESIRED_ACCESS_PIPE, 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
309 if (tevent_req_nomem(subreq, req)) {
310 return tevent_req_post(req, ev);
312 tevent_req_set_callback(subreq, rpc_transport_np_init_pipe_open,
317 static void rpc_transport_np_init_pipe_open(struct tevent_req *subreq)
319 struct tevent_req *req = tevent_req_callback_data(
320 subreq, struct tevent_req);
321 struct rpc_transport_np_init_state *state = tevent_req_data(
322 req, struct rpc_transport_np_init_state);
325 status = cli_ntcreate_recv(subreq, &state->transport_np->fnum);
327 if (!NT_STATUS_IS_OK(status)) {
328 tevent_req_nterror(req, status);
332 talloc_set_destructor(state->transport_np,
333 rpc_transport_np_state_destructor);
334 tevent_req_done(req);
337 NTSTATUS rpc_transport_np_init_recv(struct tevent_req *req,
339 struct rpc_cli_transport **presult)
341 struct rpc_transport_np_init_state *state = tevent_req_data(
342 req, struct rpc_transport_np_init_state);
345 if (tevent_req_is_nterror(req, &status)) {
349 state->transport->write_send = rpc_np_write_send;
350 state->transport->write_recv = rpc_np_write_recv;
351 state->transport->read_send = rpc_np_read_send;
352 state->transport->read_recv = rpc_np_read_recv;
353 state->transport->trans_send = rpc_np_trans_send;
354 state->transport->trans_recv = rpc_np_trans_recv;
356 *presult = talloc_move(mem_ctx, &state->transport);
360 NTSTATUS rpc_transport_np_init(TALLOC_CTX *mem_ctx, struct cli_state *cli,
361 const struct ndr_syntax_id *abstract_syntax,
362 struct rpc_cli_transport **presult)
364 TALLOC_CTX *frame = talloc_stackframe();
365 struct event_context *ev;
366 struct tevent_req *req;
367 NTSTATUS status = NT_STATUS_OK;
369 ev = event_context_init(frame);
371 status = NT_STATUS_NO_MEMORY;
375 req = rpc_transport_np_init_send(frame, ev, cli, abstract_syntax);
377 status = NT_STATUS_NO_MEMORY;
381 if (!tevent_req_poll(req, ev)) {
382 status = map_nt_error_from_unix(errno);
386 status = rpc_transport_np_init_recv(req, mem_ctx, presult);
392 struct cli_state *rpc_pipe_np_smb_conn(struct rpc_pipe_client *p)
394 struct rpc_transport_np_state *state = talloc_get_type(
395 p->transport->priv, struct rpc_transport_np_state);