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 /* size of listen() backlog in smbd */
34 #define SERVER_LISTEN_BACKLOG 10
38 private structure for a single listening stream socket
40 struct stream_socket
{
41 const struct stream_server_ops
*ops
;
42 struct loadparm_context
*lp_ctx
;
43 struct tevent_context
*event_ctx
;
44 const struct model_ops
*model_ops
;
45 struct socket_context
*sock
;
47 void *process_context
;
52 close the socket and shutdown a stream_connection
54 void stream_terminate_connection(struct stream_connection
*srv_conn
, const char *reason
)
56 struct tevent_context
*event_ctx
= srv_conn
->event
.ctx
;
57 const struct model_ops
*model_ops
= srv_conn
->model_ops
;
58 struct loadparm_context
*lp_ctx
= srv_conn
->lp_ctx
;
59 void *process_context
= srv_conn
->process_context
;
60 TALLOC_CTX
*frame
= NULL
;
62 if (!reason
) reason
= "unknown reason";
64 if (srv_conn
->processing
) {
65 DBG_NOTICE("Terminating connection deferred - '%s'\n", reason
);
67 DBG_NOTICE("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 frame
= talloc_stackframe();
85 reason
= talloc_strdup(frame
, reason
);
87 reason
= "OOM - unknown reason";
90 talloc_free(srv_conn
->event
.fde
);
91 srv_conn
->event
.fde
= NULL
;
92 imessaging_cleanup(srv_conn
->msg_ctx
);
93 TALLOC_FREE(srv_conn
);
94 model_ops
->terminate_connection(
95 event_ctx
, lp_ctx
, reason
, process_context
);
100 the select loop has indicated that a stream is ready for IO
102 static void stream_io_handler(struct stream_connection
*conn
, uint16_t flags
)
105 if (flags
& TEVENT_FD_WRITE
) {
106 conn
->ops
->send_handler(conn
, flags
);
107 } else if (flags
& TEVENT_FD_READ
) {
108 conn
->ops
->recv_handler(conn
, flags
);
112 if (conn
->terminate
) {
113 stream_terminate_connection(conn
, conn
->terminate
);
117 void stream_io_handler_fde(struct tevent_context
*ev
, struct tevent_fd
*fde
,
118 uint16_t flags
, void *private_data
)
120 struct stream_connection
*conn
= talloc_get_type(private_data
,
121 struct stream_connection
);
122 stream_io_handler(conn
, flags
);
125 void stream_io_handler_callback(void *private_data
, uint16_t flags
)
127 struct stream_connection
*conn
= talloc_get_type(private_data
,
128 struct stream_connection
);
129 stream_io_handler(conn
, flags
);
133 this creates a stream_connection from an already existing connection,
134 used for protocols, where a client connection needs to switched into
137 NTSTATUS
stream_new_connection_merge(struct tevent_context
*ev
,
138 struct loadparm_context
*lp_ctx
,
139 const struct model_ops
*model_ops
,
140 const struct stream_server_ops
*stream_ops
,
141 struct imessaging_context
*msg_ctx
,
143 struct stream_connection
**_srv_conn
,
144 void *process_context
)
146 struct stream_connection
*srv_conn
;
148 srv_conn
= talloc_zero(ev
, struct stream_connection
);
149 NT_STATUS_HAVE_NO_MEMORY(srv_conn
);
151 srv_conn
->private_data
= private_data
;
152 srv_conn
->model_ops
= model_ops
;
153 srv_conn
->socket
= NULL
;
154 srv_conn
->server_id
= cluster_id(0, 0);
155 srv_conn
->ops
= stream_ops
;
156 srv_conn
->msg_ctx
= msg_ctx
;
157 srv_conn
->event
.ctx
= ev
;
158 srv_conn
->lp_ctx
= lp_ctx
;
159 srv_conn
->event
.fde
= NULL
;
160 srv_conn
->process_context
= process_context
;
162 *_srv_conn
= srv_conn
;
167 called when a new socket connection has been established. This is called in the process
168 context of the new process (if appropriate)
170 static void stream_new_connection(struct tevent_context
*ev
,
171 struct loadparm_context
*lp_ctx
,
172 struct socket_context
*sock
,
173 struct server_id server_id
,
175 void *process_context
)
177 struct stream_socket
*stream_socket
= talloc_get_type(private_data
, struct stream_socket
);
178 struct stream_connection
*srv_conn
;
180 srv_conn
= talloc_zero(ev
, struct stream_connection
);
182 DBG_ERR("talloc(mem_ctx, struct stream_connection) failed\n");
186 talloc_steal(srv_conn
, sock
);
188 srv_conn
->private_data
= stream_socket
->private_data
;
189 srv_conn
->model_ops
= stream_socket
->model_ops
;
190 srv_conn
->socket
= sock
;
191 srv_conn
->server_id
= server_id
;
192 srv_conn
->ops
= stream_socket
->ops
;
193 srv_conn
->event
.ctx
= ev
;
194 srv_conn
->lp_ctx
= lp_ctx
;
195 srv_conn
->process_context
= process_context
;
197 if (!socket_check_access(sock
, "smbd", lpcfg_hosts_allow(NULL
, lpcfg_default_service(lp_ctx
)), lpcfg_hosts_deny(NULL
, lpcfg_default_service(lp_ctx
)))) {
198 stream_terminate_connection(srv_conn
, "denied by access rules");
202 srv_conn
->event
.fde
= tevent_add_fd(ev
, srv_conn
, socket_get_fd(sock
),
203 0, stream_io_handler_fde
, srv_conn
);
204 if (!srv_conn
->event
.fde
) {
205 stream_terminate_connection(srv_conn
, "tevent_add_fd() failed");
209 /* setup to receive internal messages on this connection */
210 srv_conn
->msg_ctx
= imessaging_init(srv_conn
,
212 srv_conn
->server_id
, ev
);
213 if (!srv_conn
->msg_ctx
) {
214 stream_terminate_connection(srv_conn
, "imessaging_init() failed");
218 srv_conn
->remote_address
= socket_get_remote_addr(srv_conn
->socket
, srv_conn
);
219 if (!srv_conn
->remote_address
) {
220 stream_terminate_connection(srv_conn
, "socket_get_remote_addr() failed");
224 srv_conn
->local_address
= socket_get_local_addr(srv_conn
->socket
, srv_conn
);
225 if (!srv_conn
->local_address
) {
226 stream_terminate_connection(srv_conn
, "socket_get_local_addr() failed");
233 struct server_id_buf idbuf
;
235 tmp_ctx
= talloc_new(srv_conn
);
237 title
= talloc_asprintf(tmp_ctx
, "conn[%s] c[%s] s[%s] server_id[%s]",
238 stream_socket
->ops
->name
,
239 tsocket_address_string(srv_conn
->remote_address
, tmp_ctx
),
240 tsocket_address_string(srv_conn
->local_address
, tmp_ctx
),
241 server_id_str_buf(server_id
, &idbuf
));
243 stream_connection_set_title(srv_conn
, title
);
245 talloc_free(tmp_ctx
);
248 /* we're now ready to start receiving events on this stream */
249 TEVENT_FD_READABLE(srv_conn
->event
.fde
);
251 /* call the server specific accept code */
252 stream_socket
->ops
->accept_connection(srv_conn
);
257 called when someone opens a connection to one of our listening ports
259 static void stream_accept_handler(struct tevent_context
*ev
, struct tevent_fd
*fde
,
260 uint16_t flags
, void *private_data
)
262 struct stream_socket
*stream_socket
= talloc_get_type(private_data
, struct stream_socket
);
264 /* ask the process model to create us a process for this new
265 connection. When done, it calls stream_new_connection()
266 with the newly created socket */
267 stream_socket
->model_ops
->accept_connection(
269 stream_socket
->lp_ctx
,
271 stream_new_connection
,
273 stream_socket
->process_context
);
277 setup a listen stream socket
278 if you pass *port == 0, then a port > 1024 is used
280 FIXME: This function is TCP/IP specific - uses an int rather than
281 a string for the port. Should leave allocating a port nr
282 to the socket implementation - JRV20070903
284 NTSTATUS
stream_setup_socket(TALLOC_CTX
*mem_ctx
,
285 struct tevent_context
*event_context
,
286 struct loadparm_context
*lp_ctx
,
287 const struct model_ops
*model_ops
,
288 const struct stream_server_ops
*stream_ops
,
290 const char *sock_addr
,
292 const char *socket_options
,
294 void *process_context
)
297 struct stream_socket
*stream_socket
;
298 struct socket_address
*socket_address
;
299 struct tevent_fd
*fde
;
301 struct sockaddr_storage ss
;
303 stream_socket
= talloc_zero(mem_ctx
, struct stream_socket
);
304 NT_STATUS_HAVE_NO_MEMORY(stream_socket
);
306 if (strcmp(family
, "ip") == 0) {
307 /* we will get the real family from the address itself */
308 if (!interpret_string_addr(&ss
, sock_addr
, 0)) {
309 talloc_free(stream_socket
);
310 return NT_STATUS_INVALID_ADDRESS
;
313 socket_address
= socket_address_from_sockaddr_storage(stream_socket
, &ss
, port
?*port
:0);
314 if (socket_address
== NULL
) {
315 TALLOC_FREE(stream_socket
);
316 return NT_STATUS_NO_MEMORY
;
319 status
= socket_create(stream_socket
, socket_address
->family
,
321 &stream_socket
->sock
, 0);
322 NT_STATUS_NOT_OK_RETURN(status
);
324 status
= socket_create(stream_socket
, family
,
326 &stream_socket
->sock
, 0);
327 NT_STATUS_NOT_OK_RETURN(status
);
329 /* this is for non-IP sockets, eg. unix domain sockets */
330 socket_address
= socket_address_from_strings(stream_socket
,
331 stream_socket
->sock
->backend_name
,
332 sock_addr
, port
?*port
:0);
333 NT_STATUS_HAVE_NO_MEMORY(socket_address
);
337 stream_socket
->lp_ctx
= talloc_reference(stream_socket
, lp_ctx
);
339 /* ready to listen */
340 status
= socket_set_option(stream_socket
->sock
, "SO_KEEPALIVE", NULL
);
341 NT_STATUS_NOT_OK_RETURN(status
);
343 if (socket_options
!= NULL
) {
344 status
= socket_set_option(stream_socket
->sock
, socket_options
, NULL
);
345 NT_STATUS_NOT_OK_RETURN(status
);
348 /* TODO: set socket ACL's (host allow etc) here when they're
351 /* Some sockets don't have a port, or are just described from
352 * the string. We are indicating this by having port == NULL */
354 status
= socket_listen(stream_socket
->sock
, socket_address
, SERVER_LISTEN_BACKLOG
, 0);
355 } else if (*port
== 0) {
356 for (i
= lpcfg_rpc_low_port(lp_ctx
);
357 i
<= lpcfg_rpc_high_port(lp_ctx
);
359 socket_address
->port
= i
;
360 status
= socket_listen(stream_socket
->sock
, socket_address
,
361 SERVER_LISTEN_BACKLOG
, 0);
362 if (NT_STATUS_IS_OK(status
)) {
368 status
= socket_listen(stream_socket
->sock
, socket_address
, SERVER_LISTEN_BACKLOG
, 0);
371 if (!NT_STATUS_IS_OK(status
)) {
372 DBG_ERR("Failed to listen on %s:%u - %s\n",
373 sock_addr
, port
? (unsigned int)(*port
) : 0,
375 talloc_free(stream_socket
);
379 /* Add the FD from the newly created socket into the event
380 * subsystem. it will call the accept handler whenever we get
383 fde
= tevent_add_fd(event_context
, stream_socket
->sock
,
384 socket_get_fd(stream_socket
->sock
),
386 stream_accept_handler
, stream_socket
);
388 DBG_ERR("Failed to setup fd event\n");
389 talloc_free(stream_socket
);
390 return NT_STATUS_NO_MEMORY
;
393 /* we let events system to the close on the socket. This avoids
394 * nasty interactions with waiting for talloc to close the socket. */
395 tevent_fd_set_close_fn(fde
, socket_tevent_fd_close_fn
);
396 socket_set_flags(stream_socket
->sock
, SOCKET_FLAG_NOCLOSE
);
398 stream_socket
->private_data
= talloc_reference(stream_socket
, private_data
);
399 stream_socket
->ops
= stream_ops
;
400 stream_socket
->event_ctx
= event_context
;
401 stream_socket
->model_ops
= model_ops
;
402 stream_socket
->process_context
= process_context
;
409 setup a connection title
411 void stream_connection_set_title(struct stream_connection
*conn
, const char *title
)
413 conn
->model_ops
->set_title(conn
->event
.ctx
, title
);