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/>.
24 #include "process_model.h"
25 #include "lib/events/events.h"
26 #include "lib/socket/socket.h"
27 #include "smbd/service.h"
28 #include "smbd/service_stream.h"
29 #include "lib/messaging/irpc.h"
30 #include "cluster/cluster.h"
32 /* the range of ports to try for dcerpc over tcp endpoints */
33 #define SERVER_TCP_LOW_PORT 1024
34 #define SERVER_TCP_HIGH_PORT 1300
36 /* size of listen() backlog in smbd */
37 #define SERVER_LISTEN_BACKLOG 10
41 private structure for a single listening stream socket
43 struct stream_socket {
44 const struct stream_server_ops *ops;
45 struct event_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 event_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 srv_conn->terminate = reason;
64 if (srv_conn->processing) {
66 * if we're currently inside the stream_io_handler(),
67 * defer the termination to the end of stream_io_hendler()
69 * and we don't want to read or write to the connection...
71 event_set_fd_flags(srv_conn->event.fde, 0);
75 talloc_free(srv_conn->event.fde);
76 srv_conn->event.fde = NULL;
77 talloc_free(srv_conn);
78 model_ops->terminate(event_ctx, reason);
82 the select loop has indicated that a stream is ready for IO
84 static void stream_io_handler(struct stream_connection *conn, uint16_t flags)
86 conn->processing = True;
87 if (flags & EVENT_FD_WRITE) {
88 conn->ops->send_handler(conn, flags);
89 } else if (flags & EVENT_FD_READ) {
90 conn->ops->recv_handler(conn, flags);
92 conn->processing = False;
94 if (conn->terminate) {
95 stream_terminate_connection(conn, conn->terminate);
99 static void stream_io_handler_fde(struct event_context *ev, struct fd_event *fde,
100 uint16_t flags, void *private)
102 struct stream_connection *conn = talloc_get_type(private,
103 struct stream_connection);
104 stream_io_handler(conn, flags);
107 void stream_io_handler_callback(void *private, uint16_t flags)
109 struct stream_connection *conn = talloc_get_type(private,
110 struct stream_connection);
111 stream_io_handler(conn, flags);
115 this creates a stream_connection from an already existing connection,
116 used for protocols, where a client connection needs to switched into
119 NTSTATUS stream_new_connection_merge(struct event_context *ev,
120 const struct model_ops *model_ops,
121 struct socket_context *sock,
122 const struct stream_server_ops *stream_ops,
123 struct messaging_context *msg_ctx,
125 struct stream_connection **_srv_conn)
127 struct stream_connection *srv_conn;
129 srv_conn = talloc_zero(ev, struct stream_connection);
130 NT_STATUS_HAVE_NO_MEMORY(srv_conn);
132 talloc_steal(srv_conn, sock);
134 srv_conn->private = private_data;
135 srv_conn->model_ops = model_ops;
136 srv_conn->socket = sock;
137 srv_conn->server_id = cluster_id(0);
138 srv_conn->ops = stream_ops;
139 srv_conn->msg_ctx = msg_ctx;
140 srv_conn->event.ctx = ev;
141 srv_conn->event.fde = event_add_fd(ev, srv_conn, socket_get_fd(sock),
143 stream_io_handler_fde, srv_conn);
144 *_srv_conn = srv_conn;
149 called when a new socket connection has been established. This is called in the process
150 context of the new process (if appropriate)
152 static void stream_new_connection(struct event_context *ev,
153 struct socket_context *sock,
154 struct server_id server_id, void *private)
156 struct stream_socket *stream_socket = talloc_get_type(private, struct stream_socket);
157 struct stream_connection *srv_conn;
158 struct socket_address *c, *s;
160 srv_conn = talloc_zero(ev, struct stream_connection);
162 DEBUG(0,("talloc(mem_ctx, struct stream_connection) failed\n"));
166 talloc_steal(srv_conn, sock);
168 srv_conn->private = stream_socket->private;
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->event.fde = event_add_fd(ev, srv_conn, socket_get_fd(sock),
176 stream_io_handler_fde, srv_conn);
178 if (!socket_check_access(sock, "smbd", lp_hostsallow(-1), lp_hostsdeny(-1))) {
179 stream_terminate_connection(srv_conn, "denied by access rules");
183 /* setup to receive internal messages on this connection */
184 srv_conn->msg_ctx = messaging_init(srv_conn, srv_conn->server_id, ev);
185 if (!srv_conn->msg_ctx) {
186 stream_terminate_connection(srv_conn, "messaging_init() failed");
190 c = socket_get_peer_addr(sock, ev);
191 s = socket_get_my_addr(sock, ev);
194 title = talloc_asprintf(s, "conn[%s] c[%s:%u] s[%s:%u] server_id[%s]",
195 stream_socket->ops->name,
196 c->addr, c->port, s->addr, s->port,
197 cluster_id_string(s, server_id));
199 stream_connection_set_title(srv_conn, title);
205 /* call the server specific accept code */
206 stream_socket->ops->accept_connection(srv_conn);
211 called when someone opens a connection to one of our listening ports
213 static void stream_accept_handler(struct event_context *ev, struct fd_event *fde,
214 uint16_t flags, void *private)
216 struct stream_socket *stream_socket = talloc_get_type(private, struct stream_socket);
218 /* ask the process model to create us a process for this new
219 connection. When done, it calls stream_new_connection()
220 with the newly created socket */
221 stream_socket->model_ops->accept_connection(ev, stream_socket->sock,
222 stream_new_connection, stream_socket);
228 setup a listen stream socket
229 if you pass *port == 0, then a port > 1024 is used
231 NTSTATUS stream_setup_socket(struct event_context *event_context,
232 const struct model_ops *model_ops,
233 const struct stream_server_ops *stream_ops,
235 const char *sock_addr,
240 struct stream_socket *stream_socket;
241 struct socket_address *socket_address;
244 stream_socket = talloc_zero(event_context, struct stream_socket);
245 NT_STATUS_HAVE_NO_MEMORY(stream_socket);
247 status = socket_create(family, SOCKET_TYPE_STREAM, &stream_socket->sock, 0);
248 NT_STATUS_NOT_OK_RETURN(status);
250 talloc_steal(stream_socket, stream_socket->sock);
252 /* ready to listen */
253 status = socket_set_option(stream_socket->sock, "SO_KEEPALIVE", NULL);
254 NT_STATUS_NOT_OK_RETURN(status);
256 status = socket_set_option(stream_socket->sock, lp_socket_options(), NULL);
257 NT_STATUS_NOT_OK_RETURN(status);
259 /* TODO: set socket ACL's here when they're implemented */
262 for (i=SERVER_TCP_LOW_PORT;i<= SERVER_TCP_HIGH_PORT;i++) {
263 socket_address = socket_address_from_strings(stream_socket,
264 stream_socket->sock->backend_name,
266 NT_STATUS_HAVE_NO_MEMORY(socket_address);
267 status = socket_listen(stream_socket->sock, socket_address,
268 SERVER_LISTEN_BACKLOG, 0);
269 talloc_free(socket_address);
270 if (NT_STATUS_IS_OK(status)) {
276 socket_address = socket_address_from_strings(stream_socket,
277 stream_socket->sock->backend_name,
279 NT_STATUS_HAVE_NO_MEMORY(socket_address);
280 status = socket_listen(stream_socket->sock, socket_address, SERVER_LISTEN_BACKLOG, 0);
281 talloc_free(socket_address);
284 if (!NT_STATUS_IS_OK(status)) {
285 DEBUG(0,("Failed to listen on %s:%u - %s\n",
286 sock_addr, *port, nt_errstr(status)));
287 talloc_free(stream_socket);
291 /* we will close the socket using the events system */
292 socket_set_flags(stream_socket->sock, SOCKET_FLAG_NOCLOSE);
294 event_add_fd(event_context, stream_socket->sock,
295 socket_get_fd(stream_socket->sock),
296 EVENT_FD_READ|EVENT_FD_AUTOCLOSE,
297 stream_accept_handler, stream_socket);
299 stream_socket->private = talloc_reference(stream_socket, private);
300 stream_socket->ops = stream_ops;
301 stream_socket->event_ctx = event_context;
302 stream_socket->model_ops = model_ops;
308 setup a connection title
310 void stream_connection_set_title(struct stream_connection *conn, const char *title)
312 conn->model_ops->set_title(conn->event.ctx, title);