summaryrefslogtreecommitdiff
path: root/source4/rpc_server/dcerpc_tcp.c
blob: 869ee1cb644b0033aaadb7474d3481a456915974 (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
/* 
   Unix SMB/CIFS implementation.

   server side dcerpc over tcp code

   Copyright (C) Andrew Tridgell 2003
   Copyright (C) Stefan (metze) Metzmacher 2004   

   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"

struct rpc_server_context {
	struct dcesrv_ep_description *ep_description;
	const struct dcesrv_endpoint *endpoint;
	const struct model_ops *model_ops;
	struct dcesrv_connection *dce_conn;
	struct dcesrv_context dcesrv_context;
	int socket_fd;
	struct event_context *events;	
};

/*
  a callback from the process model termination routine 
*/
void rpc_server_terminate(void *rr)
{
	struct rpc_server_context *r = rr;

	dcesrv_endpoint_disconnect(r->dce_conn);
	close(r->socket_fd);
	event_remove_fd_all(r->events, r->socket_fd);
	free(r);
}

/*
  called when a rpc session needs to be shutdown
*/
static void terminate_rpc_session(struct rpc_server_context *r, const char *reason)
{
	r->model_ops->terminate_rpc_connection(r, reason);
}


/*
  write_fn callback for dcesrv_output()
*/
static ssize_t dcerpc_write_fn(void *private, const void *buf, size_t count)
{
	struct fd_event *fde = private;
	ssize_t ret;
	ret = write(fde->fd, buf, count);
	if (ret == -1 && errno == EINTR) {
		return 0;
	}
	return ret;
}

/*
  called when a RPC socket becomes writable
*/
static void dcerpc_write_handler(struct event_context *ev, struct fd_event *fde, 
				 time_t t, uint16 flags)
{
	struct rpc_server_context *r = fde->private;
	NTSTATUS status;

	status = dcesrv_output(r->dce_conn, fde, dcerpc_write_fn);
	if (!NT_STATUS_IS_OK(status)) {
		/* TODO: destroy fd_event? */
	}

	if (!r->dce_conn->call_list || !r->dce_conn->call_list->replies) {
		fde->flags &= ~EVENT_FD_WRITE;
	}
}

/*
  called when a RPC socket becomes readable
*/
static void dcerpc_read_handler(struct event_context *ev, struct fd_event *fde, 
				time_t t, uint16 flags)
{
	struct rpc_server_context *r = fde->private;
	DATA_BLOB blob;
	ssize_t ret;

	blob = data_blob(NULL, 0x4000);
	if (!blob.data) {
		terminate_rpc_session(r, "out of memory");
		return;
	}

	ret = read(fde->fd, blob.data, blob.length);
	if (ret == 0 || (ret == -1 && errno != EINTR)) {
		data_blob_free(&blob);
		terminate_rpc_session(r, "eof on socket");
		return;
	}
	if (ret == -1) {
		data_blob_free(&blob);
		return;
	}

	blob.length = ret;

	dcesrv_input(r->dce_conn, &blob);

	data_blob_free(&blob);

	if (r->dce_conn->call_list && r->dce_conn->call_list->replies) {
		fde->flags |= EVENT_FD_WRITE;
	}
}




/*
  called when a RPC socket becomes readable
*/
static void dcerpc_io_handler(struct event_context *ev, struct fd_event *fde, 
			      time_t t, uint16 flags)
{
	if (flags & EVENT_FD_WRITE) {
		dcerpc_write_handler(ev, fde, t, flags);
	}

	if (flags & EVENT_FD_READ) {
		dcerpc_read_handler(ev, fde, t, flags);
	}
}
	
/*
  initialise a server_context from a open socket and register a event handler
  for reading from that socket
*/
void init_rpc_session(struct event_context *ev, void *private, int fd)
{
	struct fd_event fde;
	struct rpc_server_context *r = private;
	NTSTATUS status;

	r = memdup(r, sizeof(struct rpc_server_context));

	r->events = ev;
	r->socket_fd = fd;

	set_socket_options(fd,"SO_KEEPALIVE");
	set_socket_options(fd, lp_socket_options());

	status = dcesrv_endpoint_connect(&r->dcesrv_context, r->endpoint, &r->dce_conn);
	if (!NT_STATUS_IS_OK(status)) {
		close(fd);
		free(r);
		DEBUG(0,("init_rpc_session: connection to endpoint failed: %s\n", 
			nt_errstr(status)));
		return;
	}

	r->dce_conn->dce_ctx = &r->dcesrv_context;

	set_blocking(fd, False);

	/* setup a event handler for this socket. We are initially
	   only interested in reading from the socket */
	fde.fd = fd;
	fde.handler = dcerpc_io_handler;
	fde.private = r;
	fde.flags = EVENT_FD_READ;

	event_add_fd(ev, &fde);
}


/*
  setup a single rpc listener
 */
static void setup_listen_rpc(struct event_context *events,
			     const struct model_ops *model_ops, 
			     struct in_addr *ifip, uint32 *port,
			     struct rpc_server_context *r,
			     const struct dcesrv_endpoint *endpoint)
{
	struct fd_event fde;
	int i;

	if (*port == 0) {
		fde.fd = -1;
		for (i=DCERPC_TCP_LOW_PORT;i<= DCERPC_TCP_HIGH_PORT;i++) {
			fde.fd = open_socket_in(SOCK_STREAM, i, 0, ifip->s_addr, True);			
			if (fde.fd != -1) break;
		}
		if (fde.fd != -1) {
			*port = i;
		}
	} else {
		fde.fd = open_socket_in(SOCK_STREAM, *port, 0, ifip->s_addr, True);
	}

	if (fde.fd == -1) {
		DEBUG(0,("Failed to open socket on %s:%u - %s\n",
			 inet_ntoa(*ifip), *port, strerror(errno)));
		return;
	}

	/* each listening socket has separate state, so must use a different context */
	r = memdup(r, sizeof(struct rpc_server_context));
	if (!r) {
		smb_panic("out of memory");
	}

	r->ep_description = malloc(sizeof(struct dcesrv_ep_description));
	if (!r->ep_description) {
		smb_panic("out of memory");
	}
	r->ep_description->type = ENDPOINT_TCP;
	r->ep_description->info.tcp_port = *port;

	r->endpoint = endpoint;

	/* ready to listen */
	set_socket_options(fde.fd, "SO_KEEPALIVE"); 
	set_socket_options(fde.fd, lp_socket_options());
      
	if (listen(fde.fd, SMBD_LISTEN_BACKLOG) == -1) {
		DEBUG(0,("Failed to listen on %s:%d - %s\n",
			 inet_ntoa(*ifip), *port, strerror(errno)));
		close(fde.fd);
		return;
	}

	/* we are only interested in read events on the listen socket */
	fde.flags = EVENT_FD_READ;
	fde.private = r;
	fde.handler = model_ops->accept_rpc_connection;
	
	event_add_fd(events, &fde);
}

/*
  add a socket address to the list of events, one event per dcerpc endpoint
*/
static void add_socket_rpc(struct event_context *events, 
			   const struct model_ops *model_ops, 
			   struct in_addr *ifip)
{
	struct dcesrv_endpoint *e;
	struct rpc_server_context *r;

	r = malloc(sizeof(struct rpc_server_context));
	if (!r) {
		smb_panic("out of memory");
	}

	r->dcesrv_context.endpoint_list = NULL;
	dcesrv_init_context(&r->dcesrv_context);
	r->ep_description = NULL;
	r->model_ops = model_ops;
	r->dce_conn = NULL;
	r->socket_fd = -1;
	r->events = NULL;
	
	for (e=r->dcesrv_context.endpoint_list;e;e=e->next) {
		if (e->ep_description.type == ENDPOINT_TCP) {
			setup_listen_rpc(events, model_ops, ifip, 
					 &e->ep_description.info.tcp_port, 
					 r, e);
		}
	}

	free(r);
}

/****************************************************************************
 Open the listening sockets for RPC over TCP
****************************************************************************/
void open_sockets_rpc(struct event_context *events,
		      const struct model_ops *model_ops)
{
	if (lp_interfaces() && lp_bind_interfaces_only()) {
		int num_interfaces = iface_count();
		int i;
		for(i = 0; i < num_interfaces; i++) {
			struct in_addr *ifip = iface_n_ip(i);
			if (ifip == NULL) {
				continue;
			}
			add_socket_rpc(events, model_ops, ifip);
		}
	} else {
		TALLOC_CTX *mem_ctx = talloc_init("open_sockets_smbd");		
		struct in_addr *ifip = interpret_addr2(mem_ctx, lp_socket_address());
		if (!mem_ctx) {
			smb_panic("No memory");
		}
		add_socket_rpc(events, model_ops, ifip);
		talloc_destroy(mem_ctx);
	} 
}