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 async_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;
62 struct async_req *subreq;
63 struct rpc_np_write_state *state;
65 req = tevent_req_create(mem_ctx, &state, struct rpc_np_write_state);
71 subreq = cli_write_andx_send(mem_ctx, ev, np_transport->cli,
73 8, /* 8 means message mode. */
78 subreq->async.fn = rpc_np_write_done;
79 subreq->async.priv = req;
86 static void rpc_np_write_done(struct async_req *subreq)
88 struct tevent_req *req = talloc_get_type_abort(
89 subreq->async.priv, struct tevent_req);
90 struct rpc_np_write_state *state = tevent_req_data(
91 req, struct rpc_np_write_state);
94 status = cli_write_andx_recv(subreq, &state->written);
96 if (!NT_STATUS_IS_OK(status)) {
97 tevent_req_nterror(req, status);
100 tevent_req_done(req);
103 static NTSTATUS rpc_np_write_recv(struct tevent_req *req, ssize_t *pwritten)
105 struct rpc_np_write_state *state = tevent_req_data(
106 req, struct rpc_np_write_state);
109 if (tevent_req_is_nterror(req, &status)) {
112 *pwritten = state->written;
116 struct rpc_np_read_state {
122 static void rpc_np_read_done(struct async_req *subreq);
124 static struct tevent_req *rpc_np_read_send(TALLOC_CTX *mem_ctx,
125 struct event_context *ev,
126 uint8_t *data, size_t size,
129 struct rpc_transport_np_state *np_transport = talloc_get_type_abort(
130 priv, struct rpc_transport_np_state);
131 struct tevent_req *req;
132 struct async_req *subreq;
133 struct rpc_np_read_state *state;
135 req = tevent_req_create(mem_ctx, &state, struct rpc_np_read_state);
142 subreq = cli_read_andx_send(mem_ctx, ev, np_transport->cli,
143 np_transport->fnum, 0, size);
144 if (subreq == NULL) {
147 subreq->async.fn = rpc_np_read_done;
148 subreq->async.priv = req;
155 static void rpc_np_read_done(struct async_req *subreq)
157 struct tevent_req *req = talloc_get_type_abort(
158 subreq->async.priv, struct tevent_req);
159 struct rpc_np_read_state *state = tevent_req_data(
160 req, struct rpc_np_read_state);
164 status = cli_read_andx_recv(subreq, &state->received, &rcvbuf);
166 * We can't TALLOC_FREE(subreq) as usual here, as rcvbuf still is a
169 if (NT_STATUS_EQUAL(status, NT_STATUS_BUFFER_TOO_SMALL)) {
170 status = NT_STATUS_OK;
172 if (!NT_STATUS_IS_OK(status)) {
174 tevent_req_nterror(req, status);
178 if (state->received > state->size) {
180 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
184 memcpy(state->data, rcvbuf, state->received);
185 tevent_req_done(req);
188 static NTSTATUS rpc_np_read_recv(struct tevent_req *req, ssize_t *preceived)
190 struct rpc_np_read_state *state = tevent_req_data(
191 req, struct rpc_np_read_state);
194 if (tevent_req_is_nterror(req, &status)) {
197 *preceived = state->received;
201 struct rpc_np_trans_state {
207 static void rpc_np_trans_done(struct tevent_req *subreq);
209 static struct tevent_req *rpc_np_trans_send(TALLOC_CTX *mem_ctx,
210 struct event_context *ev,
211 uint8_t *data, size_t data_len,
212 uint32_t max_rdata_len,
215 struct rpc_transport_np_state *np_transport = talloc_get_type_abort(
216 priv, struct rpc_transport_np_state);
217 struct tevent_req *req, *subreq;
218 struct rpc_np_trans_state *state;
220 req = tevent_req_create(mem_ctx, &state, struct rpc_np_trans_state);
225 SSVAL(state->setup+0, 0, TRANSACT_DCERPCCMD);
226 SSVAL(state->setup+1, 0, np_transport->fnum);
228 subreq = cli_trans_send(
229 state, ev, np_transport->cli, SMBtrans,
230 "\\PIPE\\", 0, 0, 0, state->setup, 2, 0,
231 NULL, 0, 0, data, data_len, max_rdata_len);
232 if (subreq == NULL) {
235 tevent_req_set_callback(subreq, rpc_np_trans_done, req);
243 static void rpc_np_trans_done(struct tevent_req *subreq)
245 struct tevent_req *req = tevent_req_callback_data(
246 subreq, struct tevent_req);
247 struct rpc_np_trans_state *state = tevent_req_data(
248 req, struct rpc_np_trans_state);
251 status = cli_trans_recv(subreq, state, NULL, NULL, NULL, NULL,
252 &state->rdata, &state->rdata_len);
254 if (!NT_STATUS_IS_OK(status)) {
255 tevent_req_nterror(req, status);
258 tevent_req_done(req);
261 static NTSTATUS rpc_np_trans_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
262 uint8_t **prdata, uint32_t *prdata_len)
264 struct rpc_np_trans_state *state = tevent_req_data(
265 req, struct rpc_np_trans_state);
268 if (tevent_req_is_nterror(req, &status)) {
271 *prdata = talloc_move(mem_ctx, &state->rdata);
272 *prdata_len = state->rdata_len;
276 struct rpc_transport_np_init_state {
277 struct rpc_cli_transport *transport;
278 struct rpc_transport_np_state *transport_np;
281 static void rpc_transport_np_init_pipe_open(struct tevent_req *subreq);
283 struct async_req *rpc_transport_np_init_send(TALLOC_CTX *mem_ctx,
284 struct event_context *ev,
285 struct cli_state *cli,
286 const struct ndr_syntax_id *abstract_syntax)
288 struct async_req *result;
289 struct tevent_req *subreq;
290 struct rpc_transport_np_init_state *state;
292 if (!async_req_setup(mem_ctx, &result, &state,
293 struct rpc_transport_np_init_state)) {
297 state->transport = talloc(state, struct rpc_cli_transport);
298 if (state->transport == NULL) {
301 state->transport_np = talloc(state->transport,
302 struct rpc_transport_np_state);
303 if (state->transport_np == NULL) {
306 state->transport->priv = state->transport_np;
308 state->transport_np->pipe_name = get_pipe_name_from_iface(
310 state->transport_np->cli = cli;
312 subreq = cli_ntcreate_send(
313 state, ev, cli, state->transport_np->pipe_name, 0,
314 DESIRED_ACCESS_PIPE, 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
316 if (subreq == NULL) {
319 tevent_req_set_callback(subreq, rpc_transport_np_init_pipe_open,
328 static void rpc_transport_np_init_pipe_open(struct tevent_req *subreq)
330 struct async_req *req = tevent_req_callback_data(
331 subreq, struct async_req);
332 struct rpc_transport_np_init_state *state = talloc_get_type_abort(
333 req->private_data, struct rpc_transport_np_init_state);
336 status = cli_ntcreate_recv(subreq, &state->transport_np->fnum);
338 if (!NT_STATUS_IS_OK(status)) {
339 async_req_nterror(req, status);
343 talloc_set_destructor(state->transport_np,
344 rpc_transport_np_state_destructor);
348 NTSTATUS rpc_transport_np_init_recv(struct async_req *req,
350 struct rpc_cli_transport **presult)
352 struct rpc_transport_np_init_state *state = talloc_get_type_abort(
353 req->private_data, struct rpc_transport_np_init_state);
356 if (async_req_is_nterror(req, &status)) {
360 state->transport->write_send = rpc_np_write_send;
361 state->transport->write_recv = rpc_np_write_recv;
362 state->transport->read_send = rpc_np_read_send;
363 state->transport->read_recv = rpc_np_read_recv;
364 state->transport->trans_send = rpc_np_trans_send;
365 state->transport->trans_recv = rpc_np_trans_recv;
367 *presult = talloc_move(mem_ctx, &state->transport);
371 NTSTATUS rpc_transport_np_init(TALLOC_CTX *mem_ctx, struct cli_state *cli,
372 const struct ndr_syntax_id *abstract_syntax,
373 struct rpc_cli_transport **presult)
375 TALLOC_CTX *frame = talloc_stackframe();
376 struct event_context *ev;
377 struct async_req *req;
380 ev = event_context_init(frame);
382 status = NT_STATUS_NO_MEMORY;
386 req = rpc_transport_np_init_send(frame, ev, cli, abstract_syntax);
388 status = NT_STATUS_NO_MEMORY;
392 while (req->state < ASYNC_REQ_DONE) {
396 status = rpc_transport_np_init_recv(req, mem_ctx, presult);
402 struct cli_state *rpc_pipe_np_smb_conn(struct rpc_pipe_client *p)
404 struct rpc_transport_np_state *state = talloc_get_type(
405 p->transport->priv, struct rpc_transport_np_state);