ctdb-packaging: Remove mkversion.sh script
[Samba.git] / source4 / smbd / service_stream.c
blobbda28ad26f8577d84bcb7475ea2d07fcaa4e6235
1 /*
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/>.
23 #include "includes.h"
24 #include <tevent.h>
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;
46 void *private_data;
51 close the socket and shutdown a stream_connection
53 void stream_terminate_connection(struct stream_connection *srv_conn, const char *reason)
55 struct tevent_context *event_ctx = srv_conn->event.ctx;
56 const struct model_ops *model_ops = srv_conn->model_ops;
57 struct loadparm_context *lp_ctx = srv_conn->lp_ctx;
59 if (!reason) reason = "unknown reason";
61 if (srv_conn->processing) {
62 DEBUG(3,("Terminating connection deferred - '%s'\n", reason));
63 } else {
64 DEBUG(3,("Terminating connection - '%s'\n", reason));
67 srv_conn->terminate = reason;
69 if (srv_conn->processing) {
70 /*
71 * if we're currently inside the stream_io_handler(),
72 * defer the termination to the end of stream_io_hendler()
74 * and we don't want to read or write to the connection...
76 tevent_fd_set_flags(srv_conn->event.fde, 0);
77 return;
80 talloc_free(srv_conn->event.fde);
81 srv_conn->event.fde = NULL;
82 imessaging_cleanup(srv_conn->msg_ctx);
83 TALLOC_FREE(srv_conn);
84 model_ops->terminate(event_ctx, lp_ctx, reason);
87 /**
88 the select loop has indicated that a stream is ready for IO
90 static void stream_io_handler(struct stream_connection *conn, uint16_t flags)
92 conn->processing++;
93 if (flags & TEVENT_FD_WRITE) {
94 conn->ops->send_handler(conn, flags);
95 } else if (flags & TEVENT_FD_READ) {
96 conn->ops->recv_handler(conn, flags);
98 conn->processing--;
100 if (conn->terminate) {
101 stream_terminate_connection(conn, conn->terminate);
105 void stream_io_handler_fde(struct tevent_context *ev, struct tevent_fd *fde,
106 uint16_t flags, void *private_data)
108 struct stream_connection *conn = talloc_get_type(private_data,
109 struct stream_connection);
110 stream_io_handler(conn, flags);
113 void stream_io_handler_callback(void *private_data, uint16_t flags)
115 struct stream_connection *conn = talloc_get_type(private_data,
116 struct stream_connection);
117 stream_io_handler(conn, flags);
121 this creates a stream_connection from an already existing connection,
122 used for protocols, where a client connection needs to switched into
123 a server connection
125 NTSTATUS stream_new_connection_merge(struct tevent_context *ev,
126 struct loadparm_context *lp_ctx,
127 const struct model_ops *model_ops,
128 const struct stream_server_ops *stream_ops,
129 struct imessaging_context *msg_ctx,
130 void *private_data,
131 struct stream_connection **_srv_conn)
133 struct stream_connection *srv_conn;
135 srv_conn = talloc_zero(ev, struct stream_connection);
136 NT_STATUS_HAVE_NO_MEMORY(srv_conn);
138 srv_conn->private_data = private_data;
139 srv_conn->model_ops = model_ops;
140 srv_conn->socket = NULL;
141 srv_conn->server_id = cluster_id(0, 0);
142 srv_conn->ops = stream_ops;
143 srv_conn->msg_ctx = msg_ctx;
144 srv_conn->event.ctx = ev;
145 srv_conn->lp_ctx = lp_ctx;
146 srv_conn->event.fde = NULL;
148 *_srv_conn = srv_conn;
149 return NT_STATUS_OK;
153 called when a new socket connection has been established. This is called in the process
154 context of the new process (if appropriate)
156 static void stream_new_connection(struct tevent_context *ev,
157 struct loadparm_context *lp_ctx,
158 struct socket_context *sock,
159 struct server_id server_id, void *private_data)
161 struct stream_socket *stream_socket = talloc_get_type(private_data, struct stream_socket);
162 struct stream_connection *srv_conn;
164 srv_conn = talloc_zero(ev, struct stream_connection);
165 if (!srv_conn) {
166 DEBUG(0,("talloc(mem_ctx, struct stream_connection) failed\n"));
167 return;
170 talloc_steal(srv_conn, sock);
172 srv_conn->private_data = stream_socket->private_data;
173 srv_conn->model_ops = stream_socket->model_ops;
174 srv_conn->socket = sock;
175 srv_conn->server_id = server_id;
176 srv_conn->ops = stream_socket->ops;
177 srv_conn->event.ctx = ev;
178 srv_conn->lp_ctx = lp_ctx;
180 if (!socket_check_access(sock, "smbd", lpcfg_hosts_allow(NULL, lpcfg_default_service(lp_ctx)), lpcfg_hosts_deny(NULL, lpcfg_default_service(lp_ctx)))) {
181 stream_terminate_connection(srv_conn, "denied by access rules");
182 return;
185 srv_conn->event.fde = tevent_add_fd(ev, srv_conn, socket_get_fd(sock),
186 0, stream_io_handler_fde, srv_conn);
187 if (!srv_conn->event.fde) {
188 stream_terminate_connection(srv_conn, "tevent_add_fd() failed");
189 return;
192 /* setup to receive internal messages on this connection */
193 srv_conn->msg_ctx = imessaging_init(srv_conn,
194 lp_ctx,
195 srv_conn->server_id, ev);
196 if (!srv_conn->msg_ctx) {
197 stream_terminate_connection(srv_conn, "imessaging_init() failed");
198 return;
201 srv_conn->remote_address = socket_get_remote_addr(srv_conn->socket, srv_conn);
202 if (!srv_conn->remote_address) {
203 stream_terminate_connection(srv_conn, "socket_get_remote_addr() failed");
204 return;
207 srv_conn->local_address = socket_get_local_addr(srv_conn->socket, srv_conn);
208 if (!srv_conn->local_address) {
209 stream_terminate_connection(srv_conn, "socket_get_local_addr() failed");
210 return;
214 TALLOC_CTX *tmp_ctx;
215 const char *title;
216 struct server_id_buf idbuf;
218 tmp_ctx = talloc_new(srv_conn);
220 title = talloc_asprintf(tmp_ctx, "conn[%s] c[%s] s[%s] server_id[%s]",
221 stream_socket->ops->name,
222 tsocket_address_string(srv_conn->remote_address, tmp_ctx),
223 tsocket_address_string(srv_conn->local_address, tmp_ctx),
224 server_id_str_buf(server_id, &idbuf));
225 if (title) {
226 stream_connection_set_title(srv_conn, title);
228 talloc_free(tmp_ctx);
231 /* we're now ready to start receiving events on this stream */
232 TEVENT_FD_READABLE(srv_conn->event.fde);
234 /* call the server specific accept code */
235 stream_socket->ops->accept_connection(srv_conn);
240 called when someone opens a connection to one of our listening ports
242 static void stream_accept_handler(struct tevent_context *ev, struct tevent_fd *fde,
243 uint16_t flags, void *private_data)
245 struct stream_socket *stream_socket = talloc_get_type(private_data, struct stream_socket);
247 /* ask the process model to create us a process for this new
248 connection. When done, it calls stream_new_connection()
249 with the newly created socket */
250 stream_socket->model_ops->accept_connection(ev, stream_socket->lp_ctx,
251 stream_socket->sock,
252 stream_new_connection, stream_socket);
256 setup a listen stream socket
257 if you pass *port == 0, then a port > 1024 is used
259 FIXME: This function is TCP/IP specific - uses an int rather than
260 a string for the port. Should leave allocating a port nr
261 to the socket implementation - JRV20070903
263 NTSTATUS stream_setup_socket(TALLOC_CTX *mem_ctx,
264 struct tevent_context *event_context,
265 struct loadparm_context *lp_ctx,
266 const struct model_ops *model_ops,
267 const struct stream_server_ops *stream_ops,
268 const char *family,
269 const char *sock_addr,
270 uint16_t *port,
271 const char *socket_options,
272 void *private_data)
274 NTSTATUS status;
275 struct stream_socket *stream_socket;
276 struct socket_address *socket_address;
277 struct tevent_fd *fde;
278 int i;
279 struct sockaddr_storage ss;
281 stream_socket = talloc_zero(mem_ctx, struct stream_socket);
282 NT_STATUS_HAVE_NO_MEMORY(stream_socket);
284 if (strcmp(family, "ip") == 0) {
285 /* we will get the real family from the address itself */
286 if (!interpret_string_addr(&ss, sock_addr, 0)) {
287 talloc_free(stream_socket);
288 return NT_STATUS_INVALID_ADDRESS;
291 socket_address = socket_address_from_sockaddr_storage(stream_socket, &ss, port?*port:0);
292 if (socket_address == NULL) {
293 TALLOC_FREE(stream_socket);
294 return NT_STATUS_NO_MEMORY;
297 status = socket_create(socket_address->family, SOCKET_TYPE_STREAM, &stream_socket->sock, 0);
298 NT_STATUS_NOT_OK_RETURN(status);
299 } else {
300 status = socket_create(family, SOCKET_TYPE_STREAM, &stream_socket->sock, 0);
301 NT_STATUS_NOT_OK_RETURN(status);
303 /* this is for non-IP sockets, eg. unix domain sockets */
304 socket_address = socket_address_from_strings(stream_socket,
305 stream_socket->sock->backend_name,
306 sock_addr, port?*port:0);
307 NT_STATUS_HAVE_NO_MEMORY(socket_address);
311 talloc_steal(stream_socket, stream_socket->sock);
313 stream_socket->lp_ctx = talloc_reference(stream_socket, lp_ctx);
315 /* ready to listen */
316 status = socket_set_option(stream_socket->sock, "SO_KEEPALIVE", NULL);
317 NT_STATUS_NOT_OK_RETURN(status);
319 if (socket_options != NULL) {
320 status = socket_set_option(stream_socket->sock, socket_options, NULL);
321 NT_STATUS_NOT_OK_RETURN(status);
324 /* TODO: set socket ACL's (host allow etc) here when they're
325 * implemented */
327 /* Some sockets don't have a port, or are just described from
328 * the string. We are indicating this by having port == NULL */
329 if (!port) {
330 status = socket_listen(stream_socket->sock, socket_address, SERVER_LISTEN_BACKLOG, 0);
331 } else if (*port == 0) {
332 for (i = lpcfg_rpc_low_port(lp_ctx);
333 i <= lpcfg_rpc_high_port(lp_ctx);
334 i++) {
335 socket_address->port = i;
336 status = socket_listen(stream_socket->sock, socket_address,
337 SERVER_LISTEN_BACKLOG, 0);
338 if (NT_STATUS_IS_OK(status)) {
339 *port = i;
340 break;
343 } else {
344 status = socket_listen(stream_socket->sock, socket_address, SERVER_LISTEN_BACKLOG, 0);
347 if (!NT_STATUS_IS_OK(status)) {
348 DEBUG(0,("Failed to listen on %s:%u - %s\n",
349 sock_addr, port ? (unsigned int)(*port) : 0,
350 nt_errstr(status)));
351 talloc_free(stream_socket);
352 return status;
355 /* Add the FD from the newly created socket into the event
356 * subsystem. it will call the accept handler whenever we get
357 * new connections */
359 fde = tevent_add_fd(event_context, stream_socket->sock,
360 socket_get_fd(stream_socket->sock),
361 TEVENT_FD_READ,
362 stream_accept_handler, stream_socket);
363 if (!fde) {
364 DEBUG(0,("Failed to setup fd event\n"));
365 talloc_free(stream_socket);
366 return NT_STATUS_NO_MEMORY;
369 /* we let events system to the close on the socket. This avoids
370 * nasty interactions with waiting for talloc to close the socket. */
371 tevent_fd_set_close_fn(fde, socket_tevent_fd_close_fn);
372 socket_set_flags(stream_socket->sock, SOCKET_FLAG_NOCLOSE);
374 stream_socket->private_data = talloc_reference(stream_socket, private_data);
375 stream_socket->ops = stream_ops;
376 stream_socket->event_ctx = event_context;
377 stream_socket->model_ops = model_ops;
379 return NT_STATUS_OK;
384 setup a connection title
386 void stream_connection_set_title(struct stream_connection *conn, const char *title)
388 conn->model_ops->set_title(conn->event.ctx, title);