summaryrefslogtreecommitdiffstats
path: root/source4/librpc/rpc/dcerpc_roh_channel_out.c
blob: 476033e67ab58f6b7d0b4c89084c9c4dda8a9881 (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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
/*
   Unix SMB/CIFS implementation.

   [MS-RPCH] - RPC over HTTP client

   Copyright (C) 2013 Samuel Cabrero <samuelcabrero@kernevil.me>
   Copyright (C) Julien Kerihuel <j.kerihuel@openchange.org> 2013

   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"
#include <tevent.h>
#include <talloc.h>
#include "lib/tsocket/tsocket.h"
#include "lib/tls/tls.h"
#include "lib/util/tevent_ntstatus.h"
#include "lib/util/util_net.h"
#include "libcli/resolve/resolve.h"
#include "libcli/composite/composite.h"
#include "auth/credentials/credentials.h"
#include "auth/credentials/credentials_internal.h"
#include <gen_ndr/dcerpc.h>
#include <gen_ndr/ndr_dcerpc.h>

#include "librpc/rpc/dcerpc.h"
#include "librpc/rpc/dcerpc_roh.h"
#include "librpc/rpc/dcerpc_proto.h"
#include "librpc/rpc/dcerpc_util.h"
#include "libcli/http/http.h"

struct roh_request_state {
	struct http_request	*request;
	struct http_request	*response;
};

static void roh_send_RPC_DATA_OUT_done(struct tevent_req *subreq);
struct tevent_req *roh_send_RPC_DATA_OUT_send(TALLOC_CTX *mem_ctx,
					      struct loadparm_context *lp_ctx,
					      struct tevent_context *ev,
					      struct cli_credentials *credentials,
					      struct roh_connection *roh,
					      const char *rpc_server,
					      uint32_t rpc_server_port,
					      const char *rpc_proxy,
					      uint8_t http_auth)
{
	struct tevent_req		*req;
	struct tevent_req		*subreq;
	struct roh_request_state	*state;
	const char			*path;
	char				*query;
	char				*uri;

	DEBUG(8, ("%s: Sending RPC_OUT_DATA request\n", __func__));

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

	state->request = talloc_zero(state, struct http_request);
	if (tevent_req_nomem(state->request, req)) {
		return tevent_req_post(req, ev);
	}

	/* Build URI, as specified in section 2.2.2 */
	query = talloc_asprintf(state, "%s:%d", rpc_server, rpc_server_port);
	if (tevent_req_nomem(query, req)) {
		return tevent_req_post(req, ev);
	}

	/*
	 * TODO This path changes to "/rpcwithcert/rpcproxy.dll" when using
	 * certificates
	 */
	path = "/rpc/rpcproxy.dll";
	uri = talloc_asprintf(state, "%s?%s", path, query);
	if (tevent_req_nomem(uri, req)) {
		tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
		return tevent_req_post(req, ev);
	}
	TALLOC_FREE(query);

	/*
	 * Create the HTTP channel OUT request as specified in the
	 * section 2.1.2.1.2
	 */
	state->request->type = HTTP_REQ_RPC_OUT_DATA;
	state->request->uri = uri;
	state->request->body.length = 0;
	state->request->body.data = NULL;
	state->request->major = '1';
	state->request->minor = '0';

	http_add_header(state, &state->request->headers,
			"Accept", "application/rpc");
	http_add_header(state, &state->request->headers,
			"User-Agent", "MSRPC");
	http_add_header(state, &state->request->headers,
			"Host", rpc_proxy);
	http_add_header(state, &state->request->headers,
			"Connection", "keep-alive");
	http_add_header(state, &state->request->headers,
			"Content-Length", "76");
	http_add_header(state, &state->request->headers,
			"Cache-Control", "no-cache");
	http_add_header(state, &state->request->headers,
			"Pragma", "no-cache");

	subreq = http_send_auth_request_send(state,
					ev,
					roh->default_channel_out->http_conn,
					state->request,
					credentials,
					lp_ctx,
					http_auth);
	if (tevent_req_nomem(subreq, req)) {
		return tevent_req_post(req, ev);
	}
	tevent_req_set_callback(subreq, roh_send_RPC_DATA_OUT_done, req);

	return req;
}

static void roh_send_RPC_DATA_OUT_done(struct tevent_req *subreq)
{
	NTSTATUS		status;
	struct tevent_req	*req;

	req = tevent_req_callback_data(subreq, struct tevent_req);

	/* Receive the sent bytes to check if request has been properly sent */
	status = http_send_auth_request_recv(subreq);
	TALLOC_FREE(subreq);
	if (tevent_req_nterror(req, status)) {
		return;
	}

	DEBUG(8, ("%s: RPC_OUT_DATA sent\n", __func__));

	tevent_req_done(req);
}

NTSTATUS roh_send_RPC_DATA_OUT_recv(struct tevent_req *req)
{
	NTSTATUS status;

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

	tevent_req_received(req);
	return NT_STATUS_OK;
}

struct roh_send_pdu_state {
	DATA_BLOB	buffer;
	struct iovec	iov;
	int		bytes_written;
	int		sys_errno;
};

static void roh_send_CONN_A1_done(struct tevent_req *subreq);
struct tevent_req *roh_send_CONN_A1_send(TALLOC_CTX *mem_ctx,
					 struct tevent_context *ev,
					 struct roh_connection *roh)
{
	struct tevent_req		*req;
	struct tevent_req		*subreq;
	struct roh_send_pdu_state	*state;
	struct dcerpc_rts		rts;
	struct ncacn_packet		pkt;
	struct ndr_push			*ndr;
	struct tstream_context          *stream = NULL;
	struct tevent_queue             *send_queue = NULL;

	DEBUG(8, ("%s: Sending CONN/A1 request\n", __func__));

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

	rts.Flags = RTS_FLAG_NONE;
	rts.NumberOfCommands = 4;
	rts.Commands = talloc_array(state, struct dcerpc_rts_cmd, 4);

	/* CONN/A1: Version RTS command */
	rts.Commands[0].CommandType = 0x00000006;
	rts.Commands[0].Command.Version.Version = 0x00000001;

	/* CONN/A1: VirtualConnectionCookie RTS command */
	rts.Commands[1].CommandType = 0x00000003;
	rts.Commands[1].Command.Cookie.Cookie.Cookie = roh->connection_cookie;

	/* CONN/A1: OutChannelCookie RTS command */
	rts.Commands[2].CommandType = 0x00000003;
	rts.Commands[2].Command.Cookie.Cookie.Cookie =
			roh->default_channel_out->channel_cookie;

	/* CONN/A1: ReceiveWindowSize */
	rts.Commands[3].CommandType = 0x00000000;
	rts.Commands[3].Command.ReceiveWindowSize.ReceiveWindowSize = 0x40000;

	pkt.rpc_vers = 5;
	pkt.rpc_vers_minor = 0;
	pkt.ptype = DCERPC_PKT_RTS;
	pkt.pfc_flags = DCERPC_PFC_FLAG_LAST | DCERPC_PFC_FLAG_FIRST;
	pkt.drep[0] = DCERPC_DREP_LE;
	pkt.drep[1] = 0;
	pkt.drep[2] = 0;
	pkt.drep[3] = 0;
	pkt.frag_length = 76;
	pkt.auth_length = 0;
	pkt.call_id = 0;
	pkt.u.rts = rts;

	ndr = ndr_push_init_ctx(state);
	if (ndr == NULL) {
		return NULL;
	}
	ndr->offset = 0;
	ndr_push_ncacn_packet(ndr, NDR_SCALARS, &pkt);

	state->buffer = ndr_push_blob(ndr);
	state->iov.iov_base = (char *) state->buffer.data;
	state->iov.iov_len = state->buffer.length;

	stream = http_conn_tstream(roh->default_channel_out->http_conn);
	send_queue = http_conn_send_queue(roh->default_channel_out->http_conn);

	subreq = tstream_writev_queue_send(mem_ctx,
					   ev,
					   stream,
					   send_queue,
					   &state->iov,
					   1);
	if (tevent_req_nomem(subreq, req)) {
		return tevent_req_post(req, ev);
	}
	tevent_req_set_callback(subreq, roh_send_CONN_A1_done, req);

	return req;
}

static void roh_send_CONN_A1_done(struct tevent_req *subreq)
{
	NTSTATUS			status;
	struct tevent_req		*req;
	struct roh_send_pdu_state	*state;
	int				sys_errno;

	req = tevent_req_callback_data(subreq, struct tevent_req);
	state = tevent_req_data(req, struct roh_send_pdu_state);

	state->bytes_written = tstream_writev_queue_recv(subreq, &sys_errno);
	state->sys_errno = sys_errno;
	TALLOC_FREE(subreq);
	if (state->bytes_written <= 0 && sys_errno != 0) {
		status = map_nt_error_from_unix_common(sys_errno);
		tevent_req_nterror(req, status);
		return;
	}
	DEBUG(8, ("%s: CONN/A1 sent (%d bytes written)\n",
		  __func__, state->bytes_written));

	tevent_req_done(req);
}

NTSTATUS roh_send_CONN_A1_recv(struct tevent_req *req)
{
	NTSTATUS status;

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

	tevent_req_received(req);
	return NT_STATUS_OK;
}

struct roh_recv_response_state
{
	struct http_request	*response;
};

static void roh_recv_out_channel_response_done(struct tevent_req *);
struct tevent_req *roh_recv_out_channel_response_send(TALLOC_CTX *mem_ctx,
						      struct tevent_context *ev,
						      struct roh_connection *roh)
{
	struct tevent_req		*req;
	struct tevent_req		*subreq;
	struct roh_recv_response_state	*state;

	DEBUG(8, ("%s: Waiting for RPC_OUT_DATA response\n", __func__));

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

	subreq = http_read_response_send(state, ev,
					 roh->default_channel_out->http_conn,
					 0); /* we'll get the content later */
	if (tevent_req_nomem(subreq, req)) {
		return tevent_req_post(req, ev);
	}
	tevent_req_set_callback(subreq, roh_recv_out_channel_response_done, req);

	return req;
}

static void roh_recv_out_channel_response_done(struct tevent_req *subreq)
{
	NTSTATUS			status;
	struct tevent_req		*req;
	struct roh_recv_response_state	*state;

	req = tevent_req_callback_data(subreq, struct tevent_req);
	state = tevent_req_data(req, struct roh_recv_response_state);
	status = http_read_response_recv(subreq, state, &state->response);
	TALLOC_FREE(subreq);
	if (tevent_req_nterror(req, status)) {
		return;
	}

	DEBUG(8, ("%s: RCP_OUT_DATA response received\n", __func__));

	/* TODO Map response code to nt error */
	switch (state->response->response_code) {
	case 200:
		break;
	case 401:
		DEBUG(0, ("%s: Server response: Access denied\n", __func__));
		tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
		return;
	case 503:
		/* TODO Decode error info as specified in section 2.1.2.1.3 */
		DEBUG(0, ("%s: Server response: RPC error\n", __func__));
		tevent_req_nterror(req, NT_STATUS_GENERIC_NOT_MAPPED);
		return;
	default:
		DEBUG(0, ("%s: Server response: Unknown error\n", __func__));
		tevent_req_nterror(req, NT_STATUS_GENERIC_NOT_MAPPED);
		return;
	}

	tevent_req_done(req);
}

NTSTATUS roh_recv_out_channel_response_recv(struct tevent_req *req,
		TALLOC_CTX *mem_ctx,
		char **response_msg)
{
	NTSTATUS status;

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

	tevent_req_received(req);
	return NT_STATUS_OK;
}

struct roh_recv_pdu_state {
	struct roh_connection	*roh;
	uint32_t		connection_timeout;
	uint32_t		version;
	uint32_t		recv_window_size;
};

static void roh_recv_CONN_A3_done(struct tevent_req *subreq);
struct tevent_req *roh_recv_CONN_A3_send(TALLOC_CTX *mem_ctx,
					 struct tevent_context *ev,
					 struct roh_connection *roh)
{
	struct tevent_req		*req;
	struct tevent_req		*subreq;
	struct roh_recv_pdu_state	*state;
	struct tstream_context          *stream = NULL;

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

	DEBUG(8, ("%s: Waiting for CONN/A3\n", __func__));

	stream = http_conn_tstream(roh->default_channel_out->http_conn);

	subreq = dcerpc_read_ncacn_packet_send(state, ev, stream);
	if (tevent_req_nomem(subreq, req)) {
		return tevent_req_post(req, ev);
	}
	tevent_req_set_callback(subreq, roh_recv_CONN_A3_done, req);

	return req;
}

static void roh_recv_CONN_A3_done(struct tevent_req *subreq)
{
	NTSTATUS			status;
	struct tevent_req		*req;
	struct roh_recv_pdu_state	*state;
	struct ncacn_packet		*pkt;
	DATA_BLOB			buffer;
	struct dcerpc_rts		rts;

	req = tevent_req_callback_data(subreq, struct tevent_req);
	state = tevent_req_data(req, struct roh_recv_pdu_state);
	status = dcerpc_read_ncacn_packet_recv(subreq, state, &pkt, &buffer);
	TALLOC_FREE(subreq);

	if (tevent_req_nterror(req, status)) {
		DEBUG(0, ("%s: Error receiving PDU\n", __func__));
		return;
	}

	/*
	 * Check if it is a CONN/A3 (2.2.4.4) packet and get the connection
	 * timeout
	 */
	rts = pkt->u.rts;
	if (rts.NumberOfCommands != 1) {
		DEBUG(0, ("%s: Invalid number of commands received\n", __func__));
		tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
		return;
	}

	if (rts.Commands[0].CommandType != ROH_CMD_TYPE_CONNECTION_TIMEOUT) {
		DEBUG(0, ("%s: Invalid command type received\n", __func__));
		tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
		return;
	}

	/* Extract connection timeout */
	state->connection_timeout = rts.Commands[0].Command.ConnectionTimeout.ConnectionTimeout;

	DEBUG(8, ("%s: CONN/A3 received, connection timeout is %u\n",
		  __func__, state->connection_timeout));
	tevent_req_done(req);
}

NTSTATUS roh_recv_CONN_A3_recv(struct tevent_req *req,
			       unsigned int *connection_timeout)
{
	NTSTATUS			status;
	struct roh_recv_pdu_state	*state;

	state = tevent_req_data(req, struct roh_recv_pdu_state);
	if (tevent_req_is_nterror(req, &status)) {
		tevent_req_received(req);
		return status;
	}

	*connection_timeout = state->connection_timeout;

	tevent_req_received(req);
	return NT_STATUS_OK;
}

static void roh_recv_CONN_C2_done(struct tevent_req *subreq);
struct tevent_req *roh_recv_CONN_C2_send(TALLOC_CTX *mem_ctx,
					 struct tevent_context *ev,
					 struct roh_connection *roh)
{
	struct tevent_req		*req;
	struct tevent_req		*subreq;
	struct roh_recv_pdu_state	*state;
	struct tstream_context		*stream = NULL;

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

	DEBUG(8, ("%s: Waiting for CONN/C2\n", __func__));
	stream = http_conn_tstream(roh->default_channel_out->http_conn);

	subreq = dcerpc_read_ncacn_packet_send(state, ev, stream);
	if (tevent_req_nomem(subreq, req)) {
		return tevent_req_post(req, ev);
	}
	tevent_req_set_callback(subreq, roh_recv_CONN_C2_done, req);

	return req;
}

static void roh_recv_CONN_C2_done(struct tevent_req *subreq)
{
	NTSTATUS			status;
	struct tevent_req		*req;
	struct roh_recv_pdu_state	*state;
	struct ncacn_packet		*pkt;
	DATA_BLOB			buffer;
	struct dcerpc_rts		rts;

	req = tevent_req_callback_data(subreq, struct tevent_req);
	state = tevent_req_data(req, struct roh_recv_pdu_state);

	status = dcerpc_read_ncacn_packet_recv(subreq, state, &pkt, &buffer);
	TALLOC_FREE(subreq);
	if (tevent_req_nterror(req, status)) {
		DEBUG(0, ("%s: Error receiving PDU\n", __func__));
		return;
	}

	/*
	 * Check if it is a CONN/C2 packet (2.2.4.9), and get the version, the
	 * receive windows size and the connection timeout for the IN channel
	 */
	rts = pkt->u.rts;
	if (rts.NumberOfCommands != 3) {
		DEBUG(0, ("%s: Invalid number of commands received\n",
			  __func__));
		tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
		return;
	}
	if (rts.Commands[0].CommandType != ROH_CMD_TYPE_VERSION) {
		DEBUG(0, ("%s: Invalid command type received\n", __func__));
		tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
		return;
	}
	if (rts.Commands[1].CommandType != ROH_CMD_TYPE_RECV_WINDOWS_SIZE) {
		DEBUG(0, ("%s: Invalid command type received\n", __func__));
		tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
		return;
	}
	if (rts.Commands[2].CommandType != ROH_CMD_TYPE_CONNECTION_TIMEOUT) {
		DEBUG(0, ("%s: Invalid command type received\n", __func__));
		tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
		return;
	}

	/* Extract data */
	state->version = rts.Commands[0].Command.Version.Version;
	state->recv_window_size = rts.Commands[1].Command.ReceiveWindowSize.ReceiveWindowSize;
	state->connection_timeout = rts.Commands[2].Command.ConnectionTimeout.ConnectionTimeout;

	DEBUG(8, ("%s: CONN/C2 received, version is %u, receive windows size is %u, connection timeout is %u\n",
		  __func__, state->version, state->recv_window_size,
		  state->connection_timeout));
	tevent_req_done(req);
}

NTSTATUS roh_recv_CONN_C2_recv(struct tevent_req *req,
			       unsigned int *version,
			       unsigned int *recv_window_size,
			       unsigned int *connection_timeout)
{
	NTSTATUS			status;
	struct roh_recv_pdu_state	*state;

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

	state =  tevent_req_data(req, struct roh_recv_pdu_state);
	*version = state->version;
	*recv_window_size = state->recv_window_size;
	*connection_timeout = state->connection_timeout;

	tevent_req_received(req);
	return NT_STATUS_OK;
}