1 | /*
|
---|
2 | Unix SMB/CIFS implementation.
|
---|
3 |
|
---|
4 | helper functions for stream based servers
|
---|
5 |
|
---|
6 | Copyright (C) Andrew Tridgell 2003-2005
|
---|
7 | Copyright (C) Stefan (metze) Metzmacher 2004
|
---|
8 |
|
---|
9 | This program is free software; you can redistribute it and/or modify
|
---|
10 | it under the terms of the GNU General Public License as published by
|
---|
11 | the Free Software Foundation; either version 3 of the License, or
|
---|
12 | (at your option) any later version.
|
---|
13 |
|
---|
14 | This program is distributed in the hope that it will be useful,
|
---|
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
17 | GNU General Public License for more details.
|
---|
18 |
|
---|
19 | You should have received a copy of the GNU General Public License
|
---|
20 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
21 | */
|
---|
22 |
|
---|
23 | #include "includes.h"
|
---|
24 | #include <tevent.h>
|
---|
25 | #include "process_model.h"
|
---|
26 | #include "lib/messaging/irpc.h"
|
---|
27 | #include "cluster/cluster.h"
|
---|
28 | #include "param/param.h"
|
---|
29 | #include "../lib/tsocket/tsocket.h"
|
---|
30 |
|
---|
31 | /* the range of ports to try for dcerpc over tcp endpoints */
|
---|
32 | #define SERVER_TCP_LOW_PORT 1024
|
---|
33 | #define SERVER_TCP_HIGH_PORT 1300
|
---|
34 |
|
---|
35 | /* size of listen() backlog in smbd */
|
---|
36 | #define SERVER_LISTEN_BACKLOG 10
|
---|
37 |
|
---|
38 |
|
---|
39 | /*
|
---|
40 | private structure for a single listening stream socket
|
---|
41 | */
|
---|
42 | struct stream_socket {
|
---|
43 | const struct stream_server_ops *ops;
|
---|
44 | struct loadparm_context *lp_ctx;
|
---|
45 | struct tevent_context *event_ctx;
|
---|
46 | const struct model_ops *model_ops;
|
---|
47 | struct socket_context *sock;
|
---|
48 | void *private_data;
|
---|
49 | };
|
---|
50 |
|
---|
51 |
|
---|
52 | /*
|
---|
53 | close the socket and shutdown a stream_connection
|
---|
54 | */
|
---|
55 | void stream_terminate_connection(struct stream_connection *srv_conn, const char *reason)
|
---|
56 | {
|
---|
57 | struct tevent_context *event_ctx = srv_conn->event.ctx;
|
---|
58 | const struct model_ops *model_ops = srv_conn->model_ops;
|
---|
59 |
|
---|
60 | if (!reason) reason = "unknown reason";
|
---|
61 |
|
---|
62 | DEBUG(3,("Terminating connection - '%s'\n", reason));
|
---|
63 |
|
---|
64 | srv_conn->terminate = reason;
|
---|
65 |
|
---|
66 | if (srv_conn->processing) {
|
---|
67 | /*
|
---|
68 | * if we're currently inside the stream_io_handler(),
|
---|
69 | * defer the termination to the end of stream_io_hendler()
|
---|
70 | *
|
---|
71 | * and we don't want to read or write to the connection...
|
---|
72 | */
|
---|
73 | tevent_fd_set_flags(srv_conn->event.fde, 0);
|
---|
74 | return;
|
---|
75 | }
|
---|
76 |
|
---|
77 | talloc_free(srv_conn->event.fde);
|
---|
78 | srv_conn->event.fde = NULL;
|
---|
79 | model_ops->terminate(event_ctx, srv_conn->lp_ctx, reason);
|
---|
80 | talloc_free(srv_conn);
|
---|
81 | }
|
---|
82 |
|
---|
83 | /**
|
---|
84 | the select loop has indicated that a stream is ready for IO
|
---|
85 | */
|
---|
86 | static void stream_io_handler(struct stream_connection *conn, uint16_t flags)
|
---|
87 | {
|
---|
88 | conn->processing++;
|
---|
89 | if (flags & TEVENT_FD_WRITE) {
|
---|
90 | conn->ops->send_handler(conn, flags);
|
---|
91 | } else if (flags & TEVENT_FD_READ) {
|
---|
92 | conn->ops->recv_handler(conn, flags);
|
---|
93 | }
|
---|
94 | conn->processing--;
|
---|
95 |
|
---|
96 | if (conn->terminate) {
|
---|
97 | stream_terminate_connection(conn, conn->terminate);
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | void stream_io_handler_fde(struct tevent_context *ev, struct tevent_fd *fde,
|
---|
102 | uint16_t flags, void *private_data)
|
---|
103 | {
|
---|
104 | struct stream_connection *conn = talloc_get_type(private_data,
|
---|
105 | struct stream_connection);
|
---|
106 | stream_io_handler(conn, flags);
|
---|
107 | }
|
---|
108 |
|
---|
109 | void stream_io_handler_callback(void *private_data, uint16_t flags)
|
---|
110 | {
|
---|
111 | struct stream_connection *conn = talloc_get_type(private_data,
|
---|
112 | struct stream_connection);
|
---|
113 | stream_io_handler(conn, flags);
|
---|
114 | }
|
---|
115 |
|
---|
116 | /*
|
---|
117 | this creates a stream_connection from an already existing connection,
|
---|
118 | used for protocols, where a client connection needs to switched into
|
---|
119 | a server connection
|
---|
120 | */
|
---|
121 | NTSTATUS stream_new_connection_merge(struct tevent_context *ev,
|
---|
122 | struct loadparm_context *lp_ctx,
|
---|
123 | const struct model_ops *model_ops,
|
---|
124 | const struct stream_server_ops *stream_ops,
|
---|
125 | struct messaging_context *msg_ctx,
|
---|
126 | void *private_data,
|
---|
127 | struct stream_connection **_srv_conn)
|
---|
128 | {
|
---|
129 | struct stream_connection *srv_conn;
|
---|
130 |
|
---|
131 | srv_conn = talloc_zero(ev, struct stream_connection);
|
---|
132 | NT_STATUS_HAVE_NO_MEMORY(srv_conn);
|
---|
133 |
|
---|
134 | srv_conn->private_data = private_data;
|
---|
135 | srv_conn->model_ops = model_ops;
|
---|
136 | srv_conn->socket = NULL;
|
---|
137 | srv_conn->server_id = cluster_id(0, 0);
|
---|
138 | srv_conn->ops = stream_ops;
|
---|
139 | srv_conn->msg_ctx = msg_ctx;
|
---|
140 | srv_conn->event.ctx = ev;
|
---|
141 | srv_conn->lp_ctx = lp_ctx;
|
---|
142 | srv_conn->event.fde = NULL;
|
---|
143 |
|
---|
144 | *_srv_conn = srv_conn;
|
---|
145 | return NT_STATUS_OK;
|
---|
146 | }
|
---|
147 |
|
---|
148 | /*
|
---|
149 | called when a new socket connection has been established. This is called in the process
|
---|
150 | context of the new process (if appropriate)
|
---|
151 | */
|
---|
152 | static void stream_new_connection(struct tevent_context *ev,
|
---|
153 | struct loadparm_context *lp_ctx,
|
---|
154 | struct socket_context *sock,
|
---|
155 | struct server_id server_id, void *private_data)
|
---|
156 | {
|
---|
157 | struct stream_socket *stream_socket = talloc_get_type(private_data, struct stream_socket);
|
---|
158 | struct stream_connection *srv_conn;
|
---|
159 |
|
---|
160 | srv_conn = talloc_zero(ev, struct stream_connection);
|
---|
161 | if (!srv_conn) {
|
---|
162 | DEBUG(0,("talloc(mem_ctx, struct stream_connection) failed\n"));
|
---|
163 | return;
|
---|
164 | }
|
---|
165 |
|
---|
166 | talloc_steal(srv_conn, sock);
|
---|
167 |
|
---|
168 | srv_conn->private_data = stream_socket->private_data;
|
---|
169 | srv_conn->model_ops = stream_socket->model_ops;
|
---|
170 | srv_conn->socket = sock;
|
---|
171 | srv_conn->server_id = server_id;
|
---|
172 | srv_conn->ops = stream_socket->ops;
|
---|
173 | srv_conn->event.ctx = ev;
|
---|
174 | srv_conn->lp_ctx = lp_ctx;
|
---|
175 |
|
---|
176 | if (!socket_check_access(sock, "smbd", lpcfg_hostsallow(NULL, lpcfg_default_service(lp_ctx)), lpcfg_hostsdeny(NULL, lpcfg_default_service(lp_ctx)))) {
|
---|
177 | stream_terminate_connection(srv_conn, "denied by access rules");
|
---|
178 | return;
|
---|
179 | }
|
---|
180 |
|
---|
181 | srv_conn->event.fde = tevent_add_fd(ev, srv_conn, socket_get_fd(sock),
|
---|
182 | 0, stream_io_handler_fde, srv_conn);
|
---|
183 | if (!srv_conn->event.fde) {
|
---|
184 | stream_terminate_connection(srv_conn, "tevent_add_fd() failed");
|
---|
185 | return;
|
---|
186 | }
|
---|
187 |
|
---|
188 | /* setup to receive internal messages on this connection */
|
---|
189 | srv_conn->msg_ctx = messaging_init(srv_conn,
|
---|
190 | lpcfg_messaging_path(srv_conn, lp_ctx),
|
---|
191 | srv_conn->server_id, ev);
|
---|
192 | if (!srv_conn->msg_ctx) {
|
---|
193 | stream_terminate_connection(srv_conn, "messaging_init() failed");
|
---|
194 | return;
|
---|
195 | }
|
---|
196 |
|
---|
197 | srv_conn->remote_address = socket_get_remote_addr(srv_conn->socket, srv_conn);
|
---|
198 | if (!srv_conn->remote_address) {
|
---|
199 | stream_terminate_connection(srv_conn, "socket_get_remote_addr() failed");
|
---|
200 | return;
|
---|
201 | }
|
---|
202 |
|
---|
203 | srv_conn->local_address = socket_get_local_addr(srv_conn->socket, srv_conn);
|
---|
204 | if (!srv_conn->local_address) {
|
---|
205 | stream_terminate_connection(srv_conn, "socket_get_local_addr() failed");
|
---|
206 | return;
|
---|
207 | }
|
---|
208 |
|
---|
209 | {
|
---|
210 | TALLOC_CTX *tmp_ctx;
|
---|
211 | const char *title;
|
---|
212 |
|
---|
213 | tmp_ctx = talloc_new(srv_conn);
|
---|
214 |
|
---|
215 | title = talloc_asprintf(tmp_ctx, "conn[%s] c[%s] s[%s] server_id[%s]",
|
---|
216 | stream_socket->ops->name,
|
---|
217 | tsocket_address_string(srv_conn->remote_address, tmp_ctx),
|
---|
218 | tsocket_address_string(srv_conn->local_address, tmp_ctx),
|
---|
219 | cluster_id_string(tmp_ctx, server_id));
|
---|
220 | if (title) {
|
---|
221 | stream_connection_set_title(srv_conn, title);
|
---|
222 | }
|
---|
223 | talloc_free(tmp_ctx);
|
---|
224 | }
|
---|
225 |
|
---|
226 | /* we're now ready to start receiving events on this stream */
|
---|
227 | TEVENT_FD_READABLE(srv_conn->event.fde);
|
---|
228 |
|
---|
229 | /* call the server specific accept code */
|
---|
230 | stream_socket->ops->accept_connection(srv_conn);
|
---|
231 | }
|
---|
232 |
|
---|
233 |
|
---|
234 | /*
|
---|
235 | called when someone opens a connection to one of our listening ports
|
---|
236 | */
|
---|
237 | static void stream_accept_handler(struct tevent_context *ev, struct tevent_fd *fde,
|
---|
238 | uint16_t flags, void *private_data)
|
---|
239 | {
|
---|
240 | struct stream_socket *stream_socket = talloc_get_type(private_data, struct stream_socket);
|
---|
241 |
|
---|
242 | /* ask the process model to create us a process for this new
|
---|
243 | connection. When done, it calls stream_new_connection()
|
---|
244 | with the newly created socket */
|
---|
245 | stream_socket->model_ops->accept_connection(ev, stream_socket->lp_ctx,
|
---|
246 | stream_socket->sock,
|
---|
247 | stream_new_connection, stream_socket);
|
---|
248 | }
|
---|
249 |
|
---|
250 | /*
|
---|
251 | setup a listen stream socket
|
---|
252 | if you pass *port == 0, then a port > 1024 is used
|
---|
253 |
|
---|
254 | FIXME: This function is TCP/IP specific - uses an int rather than
|
---|
255 | a string for the port. Should leave allocating a port nr
|
---|
256 | to the socket implementation - JRV20070903
|
---|
257 | */
|
---|
258 | NTSTATUS stream_setup_socket(TALLOC_CTX *mem_ctx,
|
---|
259 | struct tevent_context *event_context,
|
---|
260 | struct loadparm_context *lp_ctx,
|
---|
261 | const struct model_ops *model_ops,
|
---|
262 | const struct stream_server_ops *stream_ops,
|
---|
263 | const char *family,
|
---|
264 | const char *sock_addr,
|
---|
265 | uint16_t *port,
|
---|
266 | const char *socket_options,
|
---|
267 | void *private_data)
|
---|
268 | {
|
---|
269 | NTSTATUS status;
|
---|
270 | struct stream_socket *stream_socket;
|
---|
271 | struct socket_address *socket_address;
|
---|
272 | struct tevent_fd *fde;
|
---|
273 | int i;
|
---|
274 |
|
---|
275 | stream_socket = talloc_zero(mem_ctx, struct stream_socket);
|
---|
276 | NT_STATUS_HAVE_NO_MEMORY(stream_socket);
|
---|
277 |
|
---|
278 | status = socket_create(family, SOCKET_TYPE_STREAM, &stream_socket->sock, 0);
|
---|
279 | NT_STATUS_NOT_OK_RETURN(status);
|
---|
280 |
|
---|
281 | talloc_steal(stream_socket, stream_socket->sock);
|
---|
282 |
|
---|
283 | stream_socket->lp_ctx = talloc_reference(stream_socket, lp_ctx);
|
---|
284 |
|
---|
285 | /* ready to listen */
|
---|
286 | status = socket_set_option(stream_socket->sock, "SO_KEEPALIVE", NULL);
|
---|
287 | NT_STATUS_NOT_OK_RETURN(status);
|
---|
288 |
|
---|
289 | if (socket_options != NULL) {
|
---|
290 | status = socket_set_option(stream_socket->sock, socket_options, NULL);
|
---|
291 | NT_STATUS_NOT_OK_RETURN(status);
|
---|
292 | }
|
---|
293 |
|
---|
294 | /* TODO: set socket ACL's (host allow etc) here when they're
|
---|
295 | * implemented */
|
---|
296 |
|
---|
297 | /* Some sockets don't have a port, or are just described from
|
---|
298 | * the string. We are indicating this by having port == NULL */
|
---|
299 | if (!port) {
|
---|
300 | socket_address = socket_address_from_strings(stream_socket,
|
---|
301 | stream_socket->sock->backend_name,
|
---|
302 | sock_addr, 0);
|
---|
303 | NT_STATUS_HAVE_NO_MEMORY(socket_address);
|
---|
304 | status = socket_listen(stream_socket->sock, socket_address, SERVER_LISTEN_BACKLOG, 0);
|
---|
305 | talloc_free(socket_address);
|
---|
306 |
|
---|
307 | } else if (*port == 0) {
|
---|
308 | for (i=SERVER_TCP_LOW_PORT;i<= SERVER_TCP_HIGH_PORT;i++) {
|
---|
309 | socket_address = socket_address_from_strings(stream_socket,
|
---|
310 | stream_socket->sock->backend_name,
|
---|
311 | sock_addr, i);
|
---|
312 | NT_STATUS_HAVE_NO_MEMORY(socket_address);
|
---|
313 | status = socket_listen(stream_socket->sock, socket_address,
|
---|
314 | SERVER_LISTEN_BACKLOG, 0);
|
---|
315 | talloc_free(socket_address);
|
---|
316 | if (NT_STATUS_IS_OK(status)) {
|
---|
317 | *port = i;
|
---|
318 | break;
|
---|
319 | }
|
---|
320 | }
|
---|
321 | } else {
|
---|
322 | socket_address = socket_address_from_strings(stream_socket,
|
---|
323 | stream_socket->sock->backend_name,
|
---|
324 | sock_addr, *port);
|
---|
325 | NT_STATUS_HAVE_NO_MEMORY(socket_address);
|
---|
326 | status = socket_listen(stream_socket->sock, socket_address, SERVER_LISTEN_BACKLOG, 0);
|
---|
327 | talloc_free(socket_address);
|
---|
328 | }
|
---|
329 |
|
---|
330 | if (!NT_STATUS_IS_OK(status)) {
|
---|
331 | DEBUG(0,("Failed to listen on %s:%u - %s\n",
|
---|
332 | sock_addr, port ? (unsigned int)(*port) : 0,
|
---|
333 | nt_errstr(status)));
|
---|
334 | talloc_free(stream_socket);
|
---|
335 | return status;
|
---|
336 | }
|
---|
337 |
|
---|
338 | /* Add the FD from the newly created socket into the event
|
---|
339 | * subsystem. it will call the accept handler whenever we get
|
---|
340 | * new connections */
|
---|
341 |
|
---|
342 | fde = tevent_add_fd(event_context, stream_socket->sock,
|
---|
343 | socket_get_fd(stream_socket->sock),
|
---|
344 | TEVENT_FD_READ,
|
---|
345 | stream_accept_handler, stream_socket);
|
---|
346 | if (!fde) {
|
---|
347 | DEBUG(0,("Failed to setup fd event\n"));
|
---|
348 | talloc_free(stream_socket);
|
---|
349 | return NT_STATUS_NO_MEMORY;
|
---|
350 | }
|
---|
351 |
|
---|
352 | /* we let events system to the close on the socket. This avoids
|
---|
353 | * nasty interactions with waiting for talloc to close the socket. */
|
---|
354 | tevent_fd_set_close_fn(fde, socket_tevent_fd_close_fn);
|
---|
355 | socket_set_flags(stream_socket->sock, SOCKET_FLAG_NOCLOSE);
|
---|
356 |
|
---|
357 | stream_socket->private_data = talloc_reference(stream_socket, private_data);
|
---|
358 | stream_socket->ops = stream_ops;
|
---|
359 | stream_socket->event_ctx = event_context;
|
---|
360 | stream_socket->model_ops = model_ops;
|
---|
361 |
|
---|
362 | return NT_STATUS_OK;
|
---|
363 | }
|
---|
364 |
|
---|
365 | /*
|
---|
366 | setup a connection title
|
---|
367 | */
|
---|
368 | void stream_connection_set_title(struct stream_connection *conn, const char *title)
|
---|
369 | {
|
---|
370 | conn->model_ops->set_title(conn->event.ctx, title);
|
---|
371 | }
|
---|