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/util/server_id.h"
27 #include "lib/messaging/irpc.h"
28 #include "cluster/cluster.h"
29 #include "param/param.h"
30 #include "../lib/tsocket/tsocket.h"
31 #include "lib/util/util_net.h"
33 /* the range of ports to try for dcerpc over tcp endpoints */
34 #define SERVER_TCP_LOW_PORT 49152
35 #define SERVER_TCP_HIGH_PORT 65535
37 /* size of listen() backlog in smbd */
38 #define SERVER_LISTEN_BACKLOG 10
42 private structure for a single listening stream socket
44 struct stream_socket
{
45 const struct stream_server_ops
*ops
;
46 struct loadparm_context
*lp_ctx
;
47 struct tevent_context
*event_ctx
;
48 const struct model_ops
*model_ops
;
49 struct socket_context
*sock
;
55 close the socket and shutdown a stream_connection
57 void stream_terminate_connection(struct stream_connection
*srv_conn
, const char *reason
)
59 struct tevent_context
*event_ctx
= srv_conn
->event
.ctx
;
60 const struct model_ops
*model_ops
= srv_conn
->model_ops
;
62 if (!reason
) reason
= "unknown reason";
64 if (srv_conn
->processing
) {
65 DEBUG(3,("Terminating connection deferred - '%s'\n", reason
));
67 DEBUG(3,("Terminating connection - '%s'\n", reason
));
70 srv_conn
->terminate
= reason
;
72 if (srv_conn
->processing
) {
74 * if we're currently inside the stream_io_handler(),
75 * defer the termination to the end of stream_io_hendler()
77 * and we don't want to read or write to the connection...
79 tevent_fd_set_flags(srv_conn
->event
.fde
, 0);
83 talloc_free(srv_conn
->event
.fde
);
84 srv_conn
->event
.fde
= NULL
;
85 imessaging_cleanup(srv_conn
->msg_ctx
);
86 model_ops
->terminate(event_ctx
, srv_conn
->lp_ctx
, reason
);
87 talloc_free(srv_conn
);
91 the select loop has indicated that a stream is ready for IO
93 static void stream_io_handler(struct stream_connection
*conn
, uint16_t flags
)
96 if (flags
& TEVENT_FD_WRITE
) {
97 conn
->ops
->send_handler(conn
, flags
);
98 } else if (flags
& TEVENT_FD_READ
) {
99 conn
->ops
->recv_handler(conn
, flags
);
103 if (conn
->terminate
) {
104 stream_terminate_connection(conn
, conn
->terminate
);
108 void stream_io_handler_fde(struct tevent_context
*ev
, struct tevent_fd
*fde
,
109 uint16_t flags
, void *private_data
)
111 struct stream_connection
*conn
= talloc_get_type(private_data
,
112 struct stream_connection
);
113 stream_io_handler(conn
, flags
);
116 void stream_io_handler_callback(void *private_data
, uint16_t flags
)
118 struct stream_connection
*conn
= talloc_get_type(private_data
,
119 struct stream_connection
);
120 stream_io_handler(conn
, flags
);
124 this creates a stream_connection from an already existing connection,
125 used for protocols, where a client connection needs to switched into
128 NTSTATUS
stream_new_connection_merge(struct tevent_context
*ev
,
129 struct loadparm_context
*lp_ctx
,
130 const struct model_ops
*model_ops
,
131 const struct stream_server_ops
*stream_ops
,
132 struct imessaging_context
*msg_ctx
,
134 struct stream_connection
**_srv_conn
)
136 struct stream_connection
*srv_conn
;
138 srv_conn
= talloc_zero(ev
, struct stream_connection
);
139 NT_STATUS_HAVE_NO_MEMORY(srv_conn
);
141 srv_conn
->private_data
= private_data
;
142 srv_conn
->model_ops
= model_ops
;
143 srv_conn
->socket
= NULL
;
144 srv_conn
->server_id
= cluster_id(0, 0);
145 srv_conn
->ops
= stream_ops
;
146 srv_conn
->msg_ctx
= msg_ctx
;
147 srv_conn
->event
.ctx
= ev
;
148 srv_conn
->lp_ctx
= lp_ctx
;
149 srv_conn
->event
.fde
= NULL
;
151 *_srv_conn
= srv_conn
;
156 called when a new socket connection has been established. This is called in the process
157 context of the new process (if appropriate)
159 static void stream_new_connection(struct tevent_context
*ev
,
160 struct loadparm_context
*lp_ctx
,
161 struct socket_context
*sock
,
162 struct server_id server_id
, void *private_data
)
164 struct stream_socket
*stream_socket
= talloc_get_type(private_data
, struct stream_socket
);
165 struct stream_connection
*srv_conn
;
167 srv_conn
= talloc_zero(ev
, struct stream_connection
);
169 DEBUG(0,("talloc(mem_ctx, struct stream_connection) failed\n"));
173 talloc_steal(srv_conn
, sock
);
175 srv_conn
->private_data
= stream_socket
->private_data
;
176 srv_conn
->model_ops
= stream_socket
->model_ops
;
177 srv_conn
->socket
= sock
;
178 srv_conn
->server_id
= server_id
;
179 srv_conn
->ops
= stream_socket
->ops
;
180 srv_conn
->event
.ctx
= ev
;
181 srv_conn
->lp_ctx
= lp_ctx
;
183 if (!socket_check_access(sock
, "smbd", lpcfg_hosts_allow(NULL
, lpcfg_default_service(lp_ctx
)), lpcfg_hosts_deny(NULL
, lpcfg_default_service(lp_ctx
)))) {
184 stream_terminate_connection(srv_conn
, "denied by access rules");
188 srv_conn
->event
.fde
= tevent_add_fd(ev
, srv_conn
, socket_get_fd(sock
),
189 0, stream_io_handler_fde
, srv_conn
);
190 if (!srv_conn
->event
.fde
) {
191 stream_terminate_connection(srv_conn
, "tevent_add_fd() failed");
195 /* setup to receive internal messages on this connection */
196 srv_conn
->msg_ctx
= imessaging_init(srv_conn
,
198 srv_conn
->server_id
, ev
);
199 if (!srv_conn
->msg_ctx
) {
200 stream_terminate_connection(srv_conn
, "imessaging_init() failed");
204 srv_conn
->remote_address
= socket_get_remote_addr(srv_conn
->socket
, srv_conn
);
205 if (!srv_conn
->remote_address
) {
206 stream_terminate_connection(srv_conn
, "socket_get_remote_addr() failed");
210 srv_conn
->local_address
= socket_get_local_addr(srv_conn
->socket
, srv_conn
);
211 if (!srv_conn
->local_address
) {
212 stream_terminate_connection(srv_conn
, "socket_get_local_addr() failed");
219 struct server_id_buf idbuf
;
221 tmp_ctx
= talloc_new(srv_conn
);
223 title
= talloc_asprintf(tmp_ctx
, "conn[%s] c[%s] s[%s] server_id[%s]",
224 stream_socket
->ops
->name
,
225 tsocket_address_string(srv_conn
->remote_address
, tmp_ctx
),
226 tsocket_address_string(srv_conn
->local_address
, tmp_ctx
),
227 server_id_str_buf(server_id
, &idbuf
));
229 stream_connection_set_title(srv_conn
, title
);
231 talloc_free(tmp_ctx
);
234 /* we're now ready to start receiving events on this stream */
235 TEVENT_FD_READABLE(srv_conn
->event
.fde
);
237 /* call the server specific accept code */
238 stream_socket
->ops
->accept_connection(srv_conn
);
243 called when someone opens a connection to one of our listening ports
245 static void stream_accept_handler(struct tevent_context
*ev
, struct tevent_fd
*fde
,
246 uint16_t flags
, void *private_data
)
248 struct stream_socket
*stream_socket
= talloc_get_type(private_data
, struct stream_socket
);
250 /* ask the process model to create us a process for this new
251 connection. When done, it calls stream_new_connection()
252 with the newly created socket */
253 stream_socket
->model_ops
->accept_connection(ev
, stream_socket
->lp_ctx
,
255 stream_new_connection
, stream_socket
);
259 setup a listen stream socket
260 if you pass *port == 0, then a port > 1024 is used
262 FIXME: This function is TCP/IP specific - uses an int rather than
263 a string for the port. Should leave allocating a port nr
264 to the socket implementation - JRV20070903
266 NTSTATUS
stream_setup_socket(TALLOC_CTX
*mem_ctx
,
267 struct tevent_context
*event_context
,
268 struct loadparm_context
*lp_ctx
,
269 const struct model_ops
*model_ops
,
270 const struct stream_server_ops
*stream_ops
,
272 const char *sock_addr
,
274 const char *socket_options
,
278 struct stream_socket
*stream_socket
;
279 struct socket_address
*socket_address
;
280 struct tevent_fd
*fde
;
282 struct sockaddr_storage ss
;
284 stream_socket
= talloc_zero(mem_ctx
, struct stream_socket
);
285 NT_STATUS_HAVE_NO_MEMORY(stream_socket
);
287 if (strcmp(family
, "ip") == 0) {
288 /* we will get the real family from the address itself */
289 if (!interpret_string_addr(&ss
, sock_addr
, 0)) {
290 talloc_free(stream_socket
);
291 return NT_STATUS_INVALID_ADDRESS
;
294 socket_address
= socket_address_from_sockaddr_storage(stream_socket
, &ss
, port
?*port
:0);
295 if (socket_address
== NULL
) {
296 TALLOC_FREE(stream_socket
);
297 return NT_STATUS_NO_MEMORY
;
300 status
= socket_create(socket_address
->family
, SOCKET_TYPE_STREAM
, &stream_socket
->sock
, 0);
301 NT_STATUS_NOT_OK_RETURN(status
);
303 status
= socket_create(family
, SOCKET_TYPE_STREAM
, &stream_socket
->sock
, 0);
304 NT_STATUS_NOT_OK_RETURN(status
);
306 /* this is for non-IP sockets, eg. unix domain sockets */
307 socket_address
= socket_address_from_strings(stream_socket
,
308 stream_socket
->sock
->backend_name
,
309 sock_addr
, port
?*port
:0);
310 NT_STATUS_HAVE_NO_MEMORY(socket_address
);
314 talloc_steal(stream_socket
, stream_socket
->sock
);
316 stream_socket
->lp_ctx
= talloc_reference(stream_socket
, lp_ctx
);
318 /* ready to listen */
319 status
= socket_set_option(stream_socket
->sock
, "SO_KEEPALIVE", NULL
);
320 NT_STATUS_NOT_OK_RETURN(status
);
322 if (socket_options
!= NULL
) {
323 status
= socket_set_option(stream_socket
->sock
, socket_options
, NULL
);
324 NT_STATUS_NOT_OK_RETURN(status
);
327 /* TODO: set socket ACL's (host allow etc) here when they're
330 /* Some sockets don't have a port, or are just described from
331 * the string. We are indicating this by having port == NULL */
333 status
= socket_listen(stream_socket
->sock
, socket_address
, SERVER_LISTEN_BACKLOG
, 0);
334 } else if (*port
== 0) {
335 for (i
=SERVER_TCP_LOW_PORT
;i
<= SERVER_TCP_HIGH_PORT
;i
++) {
336 socket_address
->port
= i
;
337 status
= socket_listen(stream_socket
->sock
, socket_address
,
338 SERVER_LISTEN_BACKLOG
, 0);
339 if (NT_STATUS_IS_OK(status
)) {
345 status
= socket_listen(stream_socket
->sock
, socket_address
, SERVER_LISTEN_BACKLOG
, 0);
348 if (!NT_STATUS_IS_OK(status
)) {
349 DEBUG(0,("Failed to listen on %s:%u - %s\n",
350 sock_addr
, port
? (unsigned int)(*port
) : 0,
352 talloc_free(stream_socket
);
356 /* Add the FD from the newly created socket into the event
357 * subsystem. it will call the accept handler whenever we get
360 fde
= tevent_add_fd(event_context
, stream_socket
->sock
,
361 socket_get_fd(stream_socket
->sock
),
363 stream_accept_handler
, stream_socket
);
365 DEBUG(0,("Failed to setup fd event\n"));
366 talloc_free(stream_socket
);
367 return NT_STATUS_NO_MEMORY
;
370 /* we let events system to the close on the socket. This avoids
371 * nasty interactions with waiting for talloc to close the socket. */
372 tevent_fd_set_close_fn(fde
, socket_tevent_fd_close_fn
);
373 socket_set_flags(stream_socket
->sock
, SOCKET_FLAG_NOCLOSE
);
375 stream_socket
->private_data
= talloc_reference(stream_socket
, private_data
);
376 stream_socket
->ops
= stream_ops
;
377 stream_socket
->event_ctx
= event_context
;
378 stream_socket
->model_ops
= model_ops
;
385 setup a connection title
387 void stream_connection_set_title(struct stream_connection
*conn
, const char *title
)
389 conn
->model_ops
->set_title(conn
->event
.ctx
, title
);