summaryrefslogtreecommitdiff
path: root/source3/rpc_client/rpc_transport_np.c
blob: dceacf68157353fa6f7aaf83cde2daff36c7e948 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*
 *  Unix SMB/CIFS implementation.
 *  RPC client transport over named pipes
 *  Copyright (C) Volker Lendecke 2009
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

#include "includes.h"

#undef DBGC_CLASS
#define DBGC_CLASS DBGC_RPC_CLI

struct rpc_transport_np_state {
	struct cli_state *cli;
	const char *pipe_name;
	uint16_t fnum;
};

static int rpc_transport_np_state_destructor(struct rpc_transport_np_state *s)
{
	bool ret;

	if (s->cli->fd == -1) {
		DEBUG(10, ("socket was closed, no need to send close request.\n"));
		return 0;
	}
	
	ret = cli_close(s->cli, s->fnum);
	if (!ret) {
		DEBUG(1, ("rpc_transport_np_state_destructor: cli_close "
			  "failed on pipe %s. Error was %s\n", s->pipe_name,
			  cli_errstr(s->cli)));
	}
	DEBUG(10, ("rpc_pipe_destructor: closed %s\n", s->pipe_name));
	/*
	 * We can't do much on failure
	 */
	return 0;
}

struct rpc_np_write_state {
	size_t size;
	size_t written;
};

static void rpc_np_write_done(struct tevent_req *subreq);

static struct tevent_req *rpc_np_write_send(TALLOC_CTX *mem_ctx,
					    struct event_context *ev,
					    const uint8_t *data, size_t size,
					    void *priv)
{
	struct rpc_transport_np_state *np_transport = talloc_get_type_abort(
		priv, struct rpc_transport_np_state);
	struct tevent_req *req, *subreq;
	struct rpc_np_write_state *state;

	req = tevent_req_create(mem_ctx, &state, struct rpc_np_write_state);
	if (req == NULL) {
		return NULL;
	}
	state->size = size;

	subreq = cli_write_andx_send(mem_ctx, ev, np_transport->cli,
				     np_transport->fnum,
				     8, /* 8 means message mode. */
				     data, 0, size);
	if (tevent_req_nomem(subreq, req)) {
		return tevent_req_post(req, ev);
	}
	tevent_req_set_callback(subreq, rpc_np_write_done, req);
	return req;
}

static void rpc_np_write_done(struct tevent_req *subreq)
{
	struct tevent_req *req = tevent_req_callback_data(
		subreq, struct tevent_req);
	struct rpc_np_write_state *state = tevent_req_data(
		req, struct rpc_np_write_state);
	NTSTATUS status;

	status = cli_write_andx_recv(subreq, &state->written);
	TALLOC_FREE(subreq);
	if (!NT_STATUS_IS_OK(status)) {
		tevent_req_nterror(req, status);
		return;
	}
	tevent_req_done(req);
}

static NTSTATUS rpc_np_write_recv(struct tevent_req *req, ssize_t *pwritten)
{
	struct rpc_np_write_state *state = tevent_req_data(
		req, struct rpc_np_write_state);
	NTSTATUS status;

	if (tevent_req_is_nterror(req, &status)) {
		return status;
	}
	*pwritten = state->written;
	return NT_STATUS_OK;
}

struct rpc_np_read_state {
	uint8_t *data;
	size_t size;
	ssize_t received;
};

static void rpc_np_read_done(struct tevent_req *subreq);

static struct tevent_req *rpc_np_read_send(TALLOC_CTX *mem_ctx,
					   struct event_context *ev,
					   uint8_t *data, size_t size,
					   void *priv)
{
	struct rpc_transport_np_state *np_transport = talloc_get_type_abort(
		priv, struct rpc_transport_np_state);
	struct tevent_req *req, *subreq;
	struct rpc_np_read_state *state;

	req = tevent_req_create(mem_ctx, &state, struct rpc_np_read_state);
	if (req == NULL) {
		return NULL;
	}
	state->data = data;
	state->size = size;

	subreq = cli_read_andx_send(mem_ctx, ev, np_transport->cli,
				    np_transport->fnum, 0, size);
	if (subreq == NULL) {
		goto fail;
	}
	tevent_req_set_callback(subreq, rpc_np_read_done, req);
	return req;
 fail:
	TALLOC_FREE(req);
	return NULL;
}

static void rpc_np_read_done(struct tevent_req *subreq)
{
	struct tevent_req *req = tevent_req_callback_data(
		subreq, struct tevent_req);
	struct rpc_np_read_state *state = tevent_req_data(
		req, struct rpc_np_read_state);
	NTSTATUS status;
	uint8_t *rcvbuf;

	status = cli_read_andx_recv(subreq, &state->received, &rcvbuf);
	/*
	 * We can't TALLOC_FREE(subreq) as usual here, as rcvbuf still is a
	 * child of that.
	 */
	if (NT_STATUS_EQUAL(status, NT_STATUS_BUFFER_TOO_SMALL)) {
		status = NT_STATUS_OK;
	}
	if (!NT_STATUS_IS_OK(status)) {
		TALLOC_FREE(subreq);
		tevent_req_nterror(req, status);
		return;
	}

	if (state->received > state->size) {
		TALLOC_FREE(subreq);
		tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
		return;
	}

	memcpy(state->data, rcvbuf, state->received);
	tevent_req_done(req);
}

static NTSTATUS rpc_np_read_recv(struct tevent_req *req, ssize_t *preceived)
{
	struct rpc_np_read_state *state = tevent_req_data(
		req, struct rpc_np_read_state);
	NTSTATUS status;

	if (tevent_req_is_nterror(req, &status)) {
		return status;
	}
	*preceived = state->received;
	return NT_STATUS_OK;
}

struct rpc_np_trans_state {
	uint16_t setup[2];
	uint8_t *rdata;
	uint32_t rdata_len;
};

static void rpc_np_trans_done(struct tevent_req *subreq);

static struct tevent_req *rpc_np_trans_send(TALLOC_CTX *mem_ctx,
					    struct event_context *ev,
					    uint8_t *data, size_t data_len,
					    uint32_t max_rdata_len,
					    void *priv)
{
	struct rpc_transport_np_state *np_transport = talloc_get_type_abort(
		priv, struct rpc_transport_np_state);
	struct tevent_req *req, *subreq;
	struct rpc_np_trans_state *state;

	req = tevent_req_create(mem_ctx, &state, struct rpc_np_trans_state);
	if (req == NULL) {
		return NULL;
	}

	SSVAL(state->setup+0, 0, TRANSACT_DCERPCCMD);
	SSVAL(state->setup+1, 0, np_transport->fnum);

	subreq = cli_trans_send(
		state, ev, np_transport->cli, SMBtrans,
		"\\PIPE\\", 0, 0, 0, state->setup, 2, 0,
		NULL, 0, 0, data, data_len, max_rdata_len);
	if (subreq == NULL) {
		goto fail;
	}
	tevent_req_set_callback(subreq, rpc_np_trans_done, req);
	return req;

 fail:
	TALLOC_FREE(req);
	return NULL;
}

static void rpc_np_trans_done(struct tevent_req *subreq)
{
	struct tevent_req *req = tevent_req_callback_data(
		subreq, struct tevent_req);
	struct rpc_np_trans_state *state = tevent_req_data(
		req, struct rpc_np_trans_state);
	NTSTATUS status;

	status = cli_trans_recv(subreq, state, NULL, NULL, NULL, NULL,
				&state->rdata, &state->rdata_len);
	TALLOC_FREE(subreq);
	if (!NT_STATUS_IS_OK(status)) {
		tevent_req_nterror(req, status);
		return;
	}
	tevent_req_done(req);
}

static NTSTATUS rpc_np_trans_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
				  uint8_t **prdata, uint32_t *prdata_len)
{
	struct rpc_np_trans_state *state = tevent_req_data(
		req, struct rpc_np_trans_state);
	NTSTATUS status;

	if (tevent_req_is_nterror(req, &status)) {
		return status;
	}
	*prdata = talloc_move(mem_ctx, &state->rdata);
	*prdata_len = state->rdata_len;
	return NT_STATUS_OK;
}

struct rpc_transport_np_init_state {
	struct rpc_cli_transport *transport;
	struct rpc_transport_np_state *transport_np;
};

static void rpc_transport_np_init_pipe_open(struct tevent_req *subreq);

struct tevent_req *rpc_transport_np_init_send(TALLOC_CTX *mem_ctx,
					      struct event_context *ev,
					      struct cli_state *cli,
					      const struct ndr_syntax_id *abstract_syntax)
{
	struct tevent_req *req, *subreq;
	struct rpc_transport_np_init_state *state;

	req = tevent_req_create(mem_ctx, &state,
				struct rpc_transport_np_init_state);
	if (req == NULL) {
		return NULL;
	}

	state->transport = talloc(state, struct rpc_cli_transport);
	if (tevent_req_nomem(state->transport, req)) {
		return tevent_req_post(req, ev);
	}
	state->transport_np = talloc(state->transport,
				     struct rpc_transport_np_state);
	if (tevent_req_nomem(state->transport_np, req)) {
		return tevent_req_post(req, ev);
	}
	state->transport->priv = state->transport_np;

	state->transport_np->pipe_name = get_pipe_name_from_iface(
		abstract_syntax);
	state->transport_np->cli = cli;

	subreq = cli_ntcreate_send(
		state, ev, cli, state->transport_np->pipe_name,	0,
		DESIRED_ACCESS_PIPE, 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
		FILE_OPEN, 0, 0);
	if (tevent_req_nomem(subreq, req)) {
		return tevent_req_post(req, ev);
	}
	tevent_req_set_callback(subreq, rpc_transport_np_init_pipe_open,
				req);
	return req;
}

static void rpc_transport_np_init_pipe_open(struct tevent_req *subreq)
{
	struct tevent_req *req = tevent_req_callback_data(
		subreq, struct tevent_req);
	struct rpc_transport_np_init_state *state = tevent_req_data(
		req, struct rpc_transport_np_init_state);
	NTSTATUS status;

	status = cli_ntcreate_recv(subreq, &state->transport_np->fnum);
	TALLOC_FREE(subreq);
	if (!NT_STATUS_IS_OK(status)) {
		tevent_req_nterror(req, status);
		return;
	}

	talloc_set_destructor(state->transport_np,
			      rpc_transport_np_state_destructor);
	tevent_req_done(req);
}

NTSTATUS rpc_transport_np_init_recv(struct tevent_req *req,
				    TALLOC_CTX *mem_ctx,
				    struct rpc_cli_transport **presult)
{
	struct rpc_transport_np_init_state *state = tevent_req_data(
		req, struct rpc_transport_np_init_state);
	NTSTATUS status;

	if (tevent_req_is_nterror(req, &status)) {
		return status;
	}

	state->transport->write_send = rpc_np_write_send;
	state->transport->write_recv = rpc_np_write_recv;
	state->transport->read_send = rpc_np_read_send;
	state->transport->read_recv = rpc_np_read_recv;
	state->transport->trans_send = rpc_np_trans_send;
	state->transport->trans_recv = rpc_np_trans_recv;

	*presult = talloc_move(mem_ctx, &state->transport);
	return NT_STATUS_OK;
}

NTSTATUS rpc_transport_np_init(TALLOC_CTX *mem_ctx, struct cli_state *cli,
			       const struct ndr_syntax_id *abstract_syntax,
			       struct rpc_cli_transport **presult)
{
	TALLOC_CTX *frame = talloc_stackframe();
	struct event_context *ev;
	struct tevent_req *req;
	NTSTATUS status = NT_STATUS_OK;

	ev = event_context_init(frame);
	if (ev == NULL) {
		status = NT_STATUS_NO_MEMORY;
		goto fail;
	}

	req = rpc_transport_np_init_send(frame, ev, cli, abstract_syntax);
	if (req == NULL) {
		status = NT_STATUS_NO_MEMORY;
		goto fail;
	}

	if (!tevent_req_poll(req, ev)) {
		status = map_nt_error_from_unix(errno);
		goto fail;
	}

	status = rpc_transport_np_init_recv(req, mem_ctx, presult);
 fail:
	TALLOC_FREE(frame);
	return status;
}

struct cli_state *rpc_pipe_np_smb_conn(struct rpc_pipe_client *p)
{
	struct rpc_transport_np_state *state = talloc_get_type(
		p->transport->priv, struct rpc_transport_np_state);

	if (state == NULL) {
		return NULL;
	}
	return state->cli;
}