2 Unix SMB/CIFS implementation.
4 helper functions for stream based servers
6 Copyright (C) Andrew Tridgell 2003-2005
7 Copyright (C) Stefan (metze) Metzmacher 2004
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.
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.
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/>.
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"
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
35 /* size of listen() backlog in smbd */
36 #define SERVER_LISTEN_BACKLOG 10
40 private structure for a single listening stream socket
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
;
53 close the socket and shutdown a stream_connection
55 void stream_terminate_connection(struct stream_connection
*srv_conn
, const char *reason
)
57 struct tevent_context
*event_ctx
= srv_conn
->event
.ctx
;
58 const struct model_ops
*model_ops
= srv_conn
->model_ops
;
60 if (!reason
) reason
= "unknown reason";
62 DEBUG(3,("Terminating connection - '%s'\n", reason
));
64 srv_conn
->terminate
= reason
;
66 if (srv_conn
->processing
) {
68 * if we're currently inside the stream_io_handler(),
69 * defer the termination to the end of stream_io_hendler()
71 * and we don't want to read or write to the connection...
73 tevent_fd_set_flags(srv_conn
->event
.fde
, 0);
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
);
84 the select loop has indicated that a stream is ready for IO
86 static void stream_io_handler(struct stream_connection
*conn
, uint16_t flags
)
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
);
96 if (conn
->terminate
) {
97 stream_terminate_connection(conn
, conn
->terminate
);
101 static void stream_io_handler_fde(struct tevent_context
*ev
, struct tevent_fd
*fde
,
102 uint16_t flags
, void *private_data
)
104 struct stream_connection
*conn
= talloc_get_type(private_data
,
105 struct stream_connection
);
106 stream_io_handler(conn
, flags
);
109 void stream_io_handler_callback(void *private_data
, uint16_t flags
)
111 struct stream_connection
*conn
= talloc_get_type(private_data
,
112 struct stream_connection
);
113 stream_io_handler(conn
, flags
);
117 this creates a stream_connection from an already existing connection,
118 used for protocols, where a client connection needs to switched into
121 NTSTATUS
stream_new_connection_merge(struct tevent_context
*ev
,
122 struct loadparm_context
*lp_ctx
,
123 const struct model_ops
*model_ops
,
124 struct socket_context
*sock
,
125 const struct stream_server_ops
*stream_ops
,
126 struct messaging_context
*msg_ctx
,
128 struct stream_connection
**_srv_conn
)
130 struct stream_connection
*srv_conn
;
132 srv_conn
= talloc_zero(ev
, struct stream_connection
);
133 NT_STATUS_HAVE_NO_MEMORY(srv_conn
);
135 talloc_steal(srv_conn
, sock
);
137 srv_conn
->private_data
= private_data
;
138 srv_conn
->model_ops
= model_ops
;
139 srv_conn
->socket
= sock
;
140 srv_conn
->server_id
= cluster_id(0, 0);
141 srv_conn
->ops
= stream_ops
;
142 srv_conn
->msg_ctx
= msg_ctx
;
143 srv_conn
->event
.ctx
= ev
;
144 srv_conn
->lp_ctx
= lp_ctx
;
145 srv_conn
->event
.fde
= tevent_add_fd(ev
, srv_conn
, socket_get_fd(sock
),
147 stream_io_handler_fde
, srv_conn
);
148 if (!srv_conn
->event
.fde
) {
149 talloc_free(srv_conn
);
150 return NT_STATUS_NO_MEMORY
;
153 *_srv_conn
= srv_conn
;
158 called when a new socket connection has been established. This is called in the process
159 context of the new process (if appropriate)
161 static void stream_new_connection(struct tevent_context
*ev
,
162 struct loadparm_context
*lp_ctx
,
163 struct socket_context
*sock
,
164 struct server_id server_id
, void *private_data
)
166 struct stream_socket
*stream_socket
= talloc_get_type(private_data
, struct stream_socket
);
167 struct stream_connection
*srv_conn
;
169 srv_conn
= talloc_zero(ev
, struct stream_connection
);
171 DEBUG(0,("talloc(mem_ctx, struct stream_connection) failed\n"));
175 talloc_steal(srv_conn
, sock
);
177 srv_conn
->private_data
= stream_socket
->private_data
;
178 srv_conn
->model_ops
= stream_socket
->model_ops
;
179 srv_conn
->socket
= sock
;
180 srv_conn
->server_id
= server_id
;
181 srv_conn
->ops
= stream_socket
->ops
;
182 srv_conn
->event
.ctx
= ev
;
183 srv_conn
->lp_ctx
= lp_ctx
;
185 if (!socket_check_access(sock
, "smbd", lp_hostsallow(NULL
, lp_default_service(lp_ctx
)), lp_hostsdeny(NULL
, lp_default_service(lp_ctx
)))) {
186 stream_terminate_connection(srv_conn
, "denied by access rules");
190 srv_conn
->event
.fde
= tevent_add_fd(ev
, srv_conn
, socket_get_fd(sock
),
191 0, stream_io_handler_fde
, srv_conn
);
192 if (!srv_conn
->event
.fde
) {
193 stream_terminate_connection(srv_conn
, "tevent_add_fd() failed");
197 /* setup to receive internal messages on this connection */
198 srv_conn
->msg_ctx
= messaging_init(srv_conn
,
199 lp_messaging_path(srv_conn
, lp_ctx
),
201 lp_iconv_convenience(lp_ctx
),
203 if (!srv_conn
->msg_ctx
) {
204 stream_terminate_connection(srv_conn
, "messaging_init() failed");
208 srv_conn
->remote_address
= socket_get_remote_addr(srv_conn
->socket
, srv_conn
);
209 if (!srv_conn
->remote_address
) {
210 stream_terminate_connection(srv_conn
, "socket_get_remote_addr() failed");
214 srv_conn
->local_address
= socket_get_local_addr(srv_conn
->socket
, srv_conn
);
215 if (!srv_conn
->local_address
) {
216 stream_terminate_connection(srv_conn
, "socket_get_local_addr() failed");
224 tmp_ctx
= talloc_new(srv_conn
);
226 title
= talloc_asprintf(tmp_ctx
, "conn[%s] c[%s] s[%s] server_id[%s]",
227 stream_socket
->ops
->name
,
228 tsocket_address_string(srv_conn
->remote_address
, tmp_ctx
),
229 tsocket_address_string(srv_conn
->local_address
, tmp_ctx
),
230 cluster_id_string(tmp_ctx
, server_id
));
232 stream_connection_set_title(srv_conn
, title
);
234 talloc_free(tmp_ctx
);
237 /* we're now ready to start receiving events on this stream */
238 TEVENT_FD_READABLE(srv_conn
->event
.fde
);
240 /* call the server specific accept code */
241 stream_socket
->ops
->accept_connection(srv_conn
);
246 called when someone opens a connection to one of our listening ports
248 static void stream_accept_handler(struct tevent_context
*ev
, struct tevent_fd
*fde
,
249 uint16_t flags
, void *private_data
)
251 struct stream_socket
*stream_socket
= talloc_get_type(private_data
, struct stream_socket
);
253 /* ask the process model to create us a process for this new
254 connection. When done, it calls stream_new_connection()
255 with the newly created socket */
256 stream_socket
->model_ops
->accept_connection(ev
, stream_socket
->lp_ctx
,
258 stream_new_connection
, stream_socket
);
262 setup a listen stream socket
263 if you pass *port == 0, then a port > 1024 is used
265 FIXME: This function is TCP/IP specific - uses an int rather than
266 a string for the port. Should leave allocating a port nr
267 to the socket implementation - JRV20070903
269 NTSTATUS
stream_setup_socket(struct tevent_context
*event_context
,
270 struct loadparm_context
*lp_ctx
,
271 const struct model_ops
*model_ops
,
272 const struct stream_server_ops
*stream_ops
,
274 const char *sock_addr
,
276 const char *socket_options
,
280 struct stream_socket
*stream_socket
;
281 struct socket_address
*socket_address
;
282 struct tevent_fd
*fde
;
285 stream_socket
= talloc_zero(event_context
, struct stream_socket
);
286 NT_STATUS_HAVE_NO_MEMORY(stream_socket
);
288 status
= socket_create(family
, SOCKET_TYPE_STREAM
, &stream_socket
->sock
, 0);
289 NT_STATUS_NOT_OK_RETURN(status
);
291 talloc_steal(stream_socket
, stream_socket
->sock
);
293 stream_socket
->lp_ctx
= talloc_reference(stream_socket
, lp_ctx
);
295 /* ready to listen */
296 status
= socket_set_option(stream_socket
->sock
, "SO_KEEPALIVE", NULL
);
297 NT_STATUS_NOT_OK_RETURN(status
);
299 if (socket_options
!= NULL
) {
300 status
= socket_set_option(stream_socket
->sock
, socket_options
, NULL
);
301 NT_STATUS_NOT_OK_RETURN(status
);
304 /* TODO: set socket ACL's (host allow etc) here when they're
307 /* Some sockets don't have a port, or are just described from
308 * the string. We are indicating this by having port == NULL */
310 socket_address
= socket_address_from_strings(stream_socket
,
311 stream_socket
->sock
->backend_name
,
313 NT_STATUS_HAVE_NO_MEMORY(socket_address
);
314 status
= socket_listen(stream_socket
->sock
, socket_address
, SERVER_LISTEN_BACKLOG
, 0);
315 talloc_free(socket_address
);
317 } else if (*port
== 0) {
318 for (i
=SERVER_TCP_LOW_PORT
;i
<= SERVER_TCP_HIGH_PORT
;i
++) {
319 socket_address
= socket_address_from_strings(stream_socket
,
320 stream_socket
->sock
->backend_name
,
322 NT_STATUS_HAVE_NO_MEMORY(socket_address
);
323 status
= socket_listen(stream_socket
->sock
, socket_address
,
324 SERVER_LISTEN_BACKLOG
, 0);
325 talloc_free(socket_address
);
326 if (NT_STATUS_IS_OK(status
)) {
332 socket_address
= socket_address_from_strings(stream_socket
,
333 stream_socket
->sock
->backend_name
,
335 NT_STATUS_HAVE_NO_MEMORY(socket_address
);
336 status
= socket_listen(stream_socket
->sock
, socket_address
, SERVER_LISTEN_BACKLOG
, 0);
337 talloc_free(socket_address
);
340 if (!NT_STATUS_IS_OK(status
)) {
341 DEBUG(0,("Failed to listen on %s:%u - %s\n",
342 sock_addr
, port
? (unsigned int)(*port
) : 0,
344 talloc_free(stream_socket
);
348 /* Add the FD from the newly created socket into the event
349 * subsystem. it will call the accept handler whenever we get
352 fde
= tevent_add_fd(event_context
, stream_socket
->sock
,
353 socket_get_fd(stream_socket
->sock
),
355 stream_accept_handler
, stream_socket
);
357 DEBUG(0,("Failed to setup fd event\n"));
358 talloc_free(stream_socket
);
359 return NT_STATUS_NO_MEMORY
;
362 /* we let events system to the close on the socket. This avoids
363 * nasty interactions with waiting for talloc to close the socket. */
364 tevent_fd_set_close_fn(fde
, socket_tevent_fd_close_fn
);
365 socket_set_flags(stream_socket
->sock
, SOCKET_FLAG_NOCLOSE
);
367 stream_socket
->private_data
= talloc_reference(stream_socket
, private_data
);
368 stream_socket
->ops
= stream_ops
;
369 stream_socket
->event_ctx
= event_context
;
370 stream_socket
->model_ops
= model_ops
;
376 setup a connection title
378 void stream_connection_set_title(struct stream_connection
*conn
, const char *title
)
380 conn
->model_ops
->set_title(conn
->event
.ctx
, title
);