summaryrefslogtreecommitdiff
path: root/source4/librpc/rpc/dcerpc_tcp.c
blob: 35928cc1c4ce981042d4397a5a0ecf84d2e062b5 (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
/* 
   Unix SMB/CIFS implementation.

   dcerpc over TCP transport

   Copyright (C) Andrew Tridgell 2003
   
   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 2 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, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "includes.h"

#define MIN_HDR_SIZE 16

struct tcp_blob {
	struct tcp_blob *next, *prev;
	DATA_BLOB data;
};

/* transport private information used by TCP pipe transport */
struct tcp_private {
	struct event_context *event_ctx;
	struct fd_event *fde;
	int fd;
	char *server_name;
	uint32_t port;

	struct tcp_blob *pending_send;

	struct {
		size_t received;
		DATA_BLOB data;
		uint_t pending_count;
	} recv;
};


/*
  mark the socket dead
*/
static void tcp_sock_dead(struct dcerpc_pipe *p, NTSTATUS status)
{
	struct tcp_private *tcp = p->transport.private;

	if (tcp && tcp->fd != -1) {
		close(tcp->fd);
		tcp->fd = -1;
	}

	/* wipe any pending sends */
	while (tcp->pending_send) {
		struct tcp_blob *blob = tcp->pending_send;
		DLIST_REMOVE(tcp->pending_send, blob);
		talloc_free(blob);
	}

	if (!NT_STATUS_IS_OK(status)) {
		p->transport.recv_data(p, NULL, status);
	}
}

/*
  process send requests
*/
static void tcp_process_send(struct dcerpc_pipe *p)
{
	struct tcp_private *tcp = p->transport.private;

	while (tcp->pending_send) {
		struct tcp_blob *blob = tcp->pending_send;
		ssize_t ret = write(tcp->fd, blob->data.data, blob->data.length);
		if (ret == -1) {
			if (errno != EAGAIN && errno != EINTR) {
				tcp_sock_dead(p, NT_STATUS_NET_WRITE_FAULT);
			}
			break;
		}

		blob->data.data += ret;
		blob->data.length -= ret;

		if (blob->data.length != 0) {
			break;
		}

		DLIST_REMOVE(tcp->pending_send, blob);
		talloc_free(blob);
	}

	if (tcp->pending_send == NULL) {
		tcp->fde->flags &= ~EVENT_FD_WRITE;
	}
}


/*
  process recv requests
*/
static void tcp_process_recv(struct dcerpc_pipe *p)
{
	struct tcp_private *tcp = p->transport.private;
	ssize_t ret;

	/* read in the base header to get the fragment length */
	if (tcp->recv.received < MIN_HDR_SIZE) {
		uint32_t frag_length;

		ret = read(tcp->fd, tcp->recv.data.data, 
			   MIN_HDR_SIZE - tcp->recv.received);
		if (ret == -1) {
			if (errno != EAGAIN && errno != EINTR) {
				tcp_sock_dead(p, NT_STATUS_NET_WRITE_FAULT);
			}
			return;
		}
		if (ret == 0) {
			tcp_sock_dead(p, NT_STATUS_NET_WRITE_FAULT);
			return;
		}

		tcp->recv.received += ret;

		if (tcp->recv.received != MIN_HDR_SIZE) {
			return;
		}
		frag_length = dcerpc_get_frag_length(&tcp->recv.data);

		tcp->recv.data.data = talloc_realloc(tcp->recv.data.data,
						     frag_length);
		if (tcp->recv.data.data == NULL) {
			tcp_sock_dead(p, NT_STATUS_NO_MEMORY);
			return;
		}
		tcp->recv.data.length = frag_length;
	}

	/* read in the rest of the packet */
	ret = read(tcp->fd, tcp->recv.data.data + tcp->recv.received,
		   tcp->recv.data.length - tcp->recv.received);
	if (ret == -1) {
		if (errno != EAGAIN && errno != EINTR) {
			tcp_sock_dead(p, NT_STATUS_NET_WRITE_FAULT);
		}
		return;
	}
	if (ret == 0) {
		tcp_sock_dead(p, NT_STATUS_NET_WRITE_FAULT);
		return;
	}

	tcp->recv.received += ret;

	if (tcp->recv.received != tcp->recv.data.length) {
		return;
	}

	/* we have a full packet */
	p->transport.recv_data(p, &tcp->recv.data, NT_STATUS_OK);

	tcp->recv.received = 0;
	tcp->recv.pending_count--;
	if (tcp->recv.pending_count == 0) {
		tcp->fde->flags &= ~EVENT_FD_READ;
	}
}

/*
  called when a IO is triggered by the events system
*/
static void tcp_io_handler(struct event_context *ev, struct fd_event *fde, 
			   time_t t, uint16_t flags)
{
	struct dcerpc_pipe *p = fde->private;
	struct tcp_private *tcp = p->transport.private;

	if (flags & EVENT_FD_WRITE) {
		tcp_process_send(p);
	}

	if (tcp->fd == -1) {
		return;
	}

	if (flags & EVENT_FD_READ) {
		tcp_process_recv(p);
	}
}

/* 
   initiate a read request 
*/
static NTSTATUS tcp_send_read(struct dcerpc_pipe *p)
{
	struct tcp_private *tcp = p->transport.private;

	tcp->recv.pending_count++;
	if (tcp->recv.pending_count == 1) {
		tcp->fde->flags |= EVENT_FD_READ;
	}
	return NT_STATUS_OK;
}

/* 
   send an initial pdu in a multi-pdu sequence
*/
static NTSTATUS tcp_send_request(struct dcerpc_pipe *p, DATA_BLOB *data, BOOL trigger_read)
{
	struct tcp_private *tcp = p->transport.private;
	struct tcp_blob *blob;

	blob = talloc_p(tcp, struct tcp_blob);
	if (blob == NULL) {
		return NT_STATUS_NO_MEMORY;
	}

	blob->data = data_blob_talloc(blob, data->data, data->length);
	if (blob->data.data == NULL) {
		talloc_free(blob);
		return NT_STATUS_NO_MEMORY;
	}

	DLIST_ADD_END(tcp->pending_send, blob, struct tcp_blob *);

	tcp->fde->flags |= EVENT_FD_WRITE;

	if (trigger_read) {
		tcp_send_read(p);
	}

	return NT_STATUS_OK;
}

/* 
   return the event context so the caller can process asynchronously
*/
static struct event_context *tcp_event_context(struct dcerpc_pipe *p)
{
	struct tcp_private *tcp = p->transport.private;

	return tcp->event_ctx;
}

/* 
   shutdown TCP pipe connection
*/
static NTSTATUS tcp_shutdown_pipe(struct dcerpc_pipe *p)
{
	tcp_sock_dead(p, NT_STATUS_OK);

	return NT_STATUS_OK;
}

/*
  return TCP server name
*/
static const char *tcp_peer_name(struct dcerpc_pipe *p)
{
	struct tcp_private *tcp = p->transport.private;
	return tcp->server_name;
}


/* 
   open a rpc connection to a named pipe 
*/
NTSTATUS dcerpc_pipe_open_tcp(struct dcerpc_pipe **p, 
			      const char *server,
			      uint32_t port)
{
	struct tcp_private *tcp;
	int fd;
	struct in_addr addr;
	struct fd_event fde;

	if (port == 0) {
		port = EPMAPPER_PORT;
	}

	addr.s_addr = interpret_addr(server);
	if (addr.s_addr == 0) {
		return NT_STATUS_BAD_NETWORK_NAME;
	}

	fd = open_socket_out(SOCK_STREAM, &addr, port, 30000);
	if (fd == -1) {
		return NT_STATUS_PORT_CONNECTION_REFUSED;
	}

	set_blocking(fd, False);

        if (!(*p = dcerpc_pipe_init())) {
                return NT_STATUS_NO_MEMORY;
	}
 
	/*
	  fill in the transport methods
	*/
	(*p)->transport.transport = NCACN_IP_TCP;
	(*p)->transport.private = NULL;

	(*p)->transport.send_request = tcp_send_request;
	(*p)->transport.send_read = tcp_send_read;
	(*p)->transport.event_context = tcp_event_context;
	(*p)->transport.recv_data = NULL;

	(*p)->transport.shutdown_pipe = tcp_shutdown_pipe;
	(*p)->transport.peer_name = tcp_peer_name;
	
	tcp = talloc((*p), sizeof(*tcp));
	if (!tcp) {
		dcerpc_pipe_close(*p);
		return NT_STATUS_NO_MEMORY;
	}

	tcp->fd = fd;
	tcp->server_name = talloc_strdup((*p), server);
	tcp->event_ctx = event_context_init();
	tcp->pending_send = NULL;
	tcp->recv.received = 0;
	tcp->recv.data = data_blob_talloc(tcp, NULL, MIN_HDR_SIZE);
	tcp->recv.pending_count = 0;

	fde.fd = fd;
	fde.flags = 0;
	fde.handler = tcp_io_handler;
	fde.private = *p;

	tcp->fde = event_add_fd(tcp->event_ctx, &fde);

	(*p)->transport.private = tcp;

        return NT_STATUS_OK;
}