Rename pdb_ldap to pdb_ldapsam
[Samba/bjacke.git] / source4 / smbd / service_stream.c
blob22c4c040e403f167fdaf88367d84d2a211304fff
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/messaging/irpc.h"
27 #include "cluster/cluster.h"
28 #include "param/param.h"
29 #include "../lib/tsocket/tsocket.h"
30 #include "lib/util/util_net.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 loadparm_context *lp_ctx;
46 struct tevent_context *event_ctx;
47 const struct model_ops *model_ops;
48 struct socket_context *sock;
49 void *private_data;
54 close the socket and shutdown a stream_connection
56 void stream_terminate_connection(struct stream_connection *srv_conn, const char *reason)
58 struct tevent_context *event_ctx = srv_conn->event.ctx;
59 const struct model_ops *model_ops = srv_conn->model_ops;
61 if (!reason) reason = "unknown reason";
63 DEBUG(3,("Terminating connection - '%s'\n", reason));
65 srv_conn->terminate = reason;
67 if (srv_conn->processing) {
68 /*
69 * if we're currently inside the stream_io_handler(),
70 * defer the termination to the end of stream_io_hendler()
72 * and we don't want to read or write to the connection...
74 tevent_fd_set_flags(srv_conn->event.fde, 0);
75 return;
78 talloc_free(srv_conn->event.fde);
79 srv_conn->event.fde = NULL;
80 imessaging_cleanup(srv_conn->msg_ctx);
81 model_ops->terminate(event_ctx, srv_conn->lp_ctx, reason);
82 talloc_free(srv_conn);
85 /**
86 the select loop has indicated that a stream is ready for IO
88 static void stream_io_handler(struct stream_connection *conn, uint16_t flags)
90 conn->processing++;
91 if (flags & TEVENT_FD_WRITE) {
92 conn->ops->send_handler(conn, flags);
93 } else if (flags & TEVENT_FD_READ) {
94 conn->ops->recv_handler(conn, flags);
96 conn->processing--;
98 if (conn->terminate) {
99 stream_terminate_connection(conn, conn->terminate);
103 void stream_io_handler_fde(struct tevent_context *ev, struct tevent_fd *fde,
104 uint16_t flags, void *private_data)
106 struct stream_connection *conn = talloc_get_type(private_data,
107 struct stream_connection);
108 stream_io_handler(conn, flags);
111 void stream_io_handler_callback(void *private_data, uint16_t flags)
113 struct stream_connection *conn = talloc_get_type(private_data,
114 struct stream_connection);
115 stream_io_handler(conn, flags);
119 this creates a stream_connection from an already existing connection,
120 used for protocols, where a client connection needs to switched into
121 a server connection
123 NTSTATUS stream_new_connection_merge(struct tevent_context *ev,
124 struct loadparm_context *lp_ctx,
125 const struct model_ops *model_ops,
126 const struct stream_server_ops *stream_ops,
127 struct imessaging_context *msg_ctx,
128 void *private_data,
129 struct stream_connection **_srv_conn)
131 struct stream_connection *srv_conn;
133 srv_conn = talloc_zero(ev, struct stream_connection);
134 NT_STATUS_HAVE_NO_MEMORY(srv_conn);
136 srv_conn->private_data = private_data;
137 srv_conn->model_ops = model_ops;
138 srv_conn->socket = NULL;
139 srv_conn->server_id = cluster_id(0, 0);
140 srv_conn->ops = stream_ops;
141 srv_conn->msg_ctx = msg_ctx;
142 srv_conn->event.ctx = ev;
143 srv_conn->lp_ctx = lp_ctx;
144 srv_conn->event.fde = NULL;
146 *_srv_conn = srv_conn;
147 return NT_STATUS_OK;
151 called when a new socket connection has been established. This is called in the process
152 context of the new process (if appropriate)
154 static void stream_new_connection(struct tevent_context *ev,
155 struct loadparm_context *lp_ctx,
156 struct socket_context *sock,
157 struct server_id server_id, void *private_data)
159 struct stream_socket *stream_socket = talloc_get_type(private_data, struct stream_socket);
160 struct stream_connection *srv_conn;
162 srv_conn = talloc_zero(ev, struct stream_connection);
163 if (!srv_conn) {
164 DEBUG(0,("talloc(mem_ctx, struct stream_connection) failed\n"));
165 return;
168 talloc_steal(srv_conn, sock);
170 srv_conn->private_data = stream_socket->private_data;
171 srv_conn->model_ops = stream_socket->model_ops;
172 srv_conn->socket = sock;
173 srv_conn->server_id = server_id;
174 srv_conn->ops = stream_socket->ops;
175 srv_conn->event.ctx = ev;
176 srv_conn->lp_ctx = lp_ctx;
178 if (!socket_check_access(sock, "smbd", lpcfg_hostsallow(NULL, lpcfg_default_service(lp_ctx)), lpcfg_hostsdeny(NULL, lpcfg_default_service(lp_ctx)))) {
179 stream_terminate_connection(srv_conn, "denied by access rules");
180 return;
183 srv_conn->event.fde = tevent_add_fd(ev, srv_conn, socket_get_fd(sock),
184 0, stream_io_handler_fde, srv_conn);
185 if (!srv_conn->event.fde) {
186 stream_terminate_connection(srv_conn, "tevent_add_fd() failed");
187 return;
190 /* setup to receive internal messages on this connection */
191 srv_conn->msg_ctx = imessaging_init(srv_conn,
192 lp_ctx,
193 srv_conn->server_id, ev, false);
194 if (!srv_conn->msg_ctx) {
195 stream_terminate_connection(srv_conn, "imessaging_init() failed");
196 return;
199 srv_conn->remote_address = socket_get_remote_addr(srv_conn->socket, srv_conn);
200 if (!srv_conn->remote_address) {
201 stream_terminate_connection(srv_conn, "socket_get_remote_addr() failed");
202 return;
205 srv_conn->local_address = socket_get_local_addr(srv_conn->socket, srv_conn);
206 if (!srv_conn->local_address) {
207 stream_terminate_connection(srv_conn, "socket_get_local_addr() failed");
208 return;
212 TALLOC_CTX *tmp_ctx;
213 const char *title;
215 tmp_ctx = talloc_new(srv_conn);
217 title = talloc_asprintf(tmp_ctx, "conn[%s] c[%s] s[%s] server_id[%s]",
218 stream_socket->ops->name,
219 tsocket_address_string(srv_conn->remote_address, tmp_ctx),
220 tsocket_address_string(srv_conn->local_address, tmp_ctx),
221 server_id_str(tmp_ctx, &server_id));
222 if (title) {
223 stream_connection_set_title(srv_conn, title);
225 talloc_free(tmp_ctx);
228 /* we're now ready to start receiving events on this stream */
229 TEVENT_FD_READABLE(srv_conn->event.fde);
231 /* call the server specific accept code */
232 stream_socket->ops->accept_connection(srv_conn);
237 called when someone opens a connection to one of our listening ports
239 static void stream_accept_handler(struct tevent_context *ev, struct tevent_fd *fde,
240 uint16_t flags, void *private_data)
242 struct stream_socket *stream_socket = talloc_get_type(private_data, struct stream_socket);
244 /* ask the process model to create us a process for this new
245 connection. When done, it calls stream_new_connection()
246 with the newly created socket */
247 stream_socket->model_ops->accept_connection(ev, stream_socket->lp_ctx,
248 stream_socket->sock,
249 stream_new_connection, stream_socket);
253 setup a listen stream socket
254 if you pass *port == 0, then a port > 1024 is used
256 FIXME: This function is TCP/IP specific - uses an int rather than
257 a string for the port. Should leave allocating a port nr
258 to the socket implementation - JRV20070903
260 NTSTATUS stream_setup_socket(TALLOC_CTX *mem_ctx,
261 struct tevent_context *event_context,
262 struct loadparm_context *lp_ctx,
263 const struct model_ops *model_ops,
264 const struct stream_server_ops *stream_ops,
265 const char *family,
266 const char *sock_addr,
267 uint16_t *port,
268 const char *socket_options,
269 void *private_data)
271 NTSTATUS status;
272 struct stream_socket *stream_socket;
273 struct socket_address *socket_address;
274 struct tevent_fd *fde;
275 int i;
276 struct sockaddr_storage ss;
278 stream_socket = talloc_zero(mem_ctx, struct stream_socket);
279 NT_STATUS_HAVE_NO_MEMORY(stream_socket);
281 if (strcmp(family, "ip") == 0) {
282 /* we will get the real family from the address itself */
283 if (!interpret_string_addr(&ss, sock_addr, 0)) {
284 talloc_free(stream_socket);
285 return NT_STATUS_INVALID_ADDRESS;
288 socket_address = socket_address_from_sockaddr_storage(stream_socket, &ss, port?*port:0);
289 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(socket_address, stream_socket);
291 status = socket_create(socket_address->family, SOCKET_TYPE_STREAM, &stream_socket->sock, 0);
292 NT_STATUS_NOT_OK_RETURN(status);
293 } else {
294 status = socket_create(family, SOCKET_TYPE_STREAM, &stream_socket->sock, 0);
295 NT_STATUS_NOT_OK_RETURN(status);
297 /* this is for non-IP sockets, eg. unix domain sockets */
298 socket_address = socket_address_from_strings(stream_socket,
299 stream_socket->sock->backend_name,
300 sock_addr, port?*port:0);
301 NT_STATUS_HAVE_NO_MEMORY(socket_address);
305 talloc_steal(stream_socket, stream_socket->sock);
307 stream_socket->lp_ctx = talloc_reference(stream_socket, lp_ctx);
309 /* ready to listen */
310 status = socket_set_option(stream_socket->sock, "SO_KEEPALIVE", NULL);
311 NT_STATUS_NOT_OK_RETURN(status);
313 if (socket_options != NULL) {
314 status = socket_set_option(stream_socket->sock, socket_options, NULL);
315 NT_STATUS_NOT_OK_RETURN(status);
318 /* TODO: set socket ACL's (host allow etc) here when they're
319 * implemented */
321 /* Some sockets don't have a port, or are just described from
322 * the string. We are indicating this by having port == NULL */
323 if (!port) {
324 status = socket_listen(stream_socket->sock, socket_address, SERVER_LISTEN_BACKLOG, 0);
325 } else if (*port == 0) {
326 for (i=SERVER_TCP_LOW_PORT;i<= SERVER_TCP_HIGH_PORT;i++) {
327 socket_address->port = i;
328 status = socket_listen(stream_socket->sock, socket_address,
329 SERVER_LISTEN_BACKLOG, 0);
330 if (NT_STATUS_IS_OK(status)) {
331 *port = i;
332 break;
335 } else {
336 status = socket_listen(stream_socket->sock, socket_address, SERVER_LISTEN_BACKLOG, 0);
339 if (!NT_STATUS_IS_OK(status)) {
340 DEBUG(0,("Failed to listen on %s:%u - %s\n",
341 sock_addr, port ? (unsigned int)(*port) : 0,
342 nt_errstr(status)));
343 talloc_free(stream_socket);
344 return status;
347 /* Add the FD from the newly created socket into the event
348 * subsystem. it will call the accept handler whenever we get
349 * new connections */
351 fde = tevent_add_fd(event_context, stream_socket->sock,
352 socket_get_fd(stream_socket->sock),
353 TEVENT_FD_READ,
354 stream_accept_handler, stream_socket);
355 if (!fde) {
356 DEBUG(0,("Failed to setup fd event\n"));
357 talloc_free(stream_socket);
358 return NT_STATUS_NO_MEMORY;
361 /* we let events system to the close on the socket. This avoids
362 * nasty interactions with waiting for talloc to close the socket. */
363 tevent_fd_set_close_fn(fde, socket_tevent_fd_close_fn);
364 socket_set_flags(stream_socket->sock, SOCKET_FLAG_NOCLOSE);
366 stream_socket->private_data = talloc_reference(stream_socket, private_data);
367 stream_socket->ops = stream_ops;
368 stream_socket->event_ctx = event_context;
369 stream_socket->model_ops = model_ops;
371 return NT_STATUS_OK;
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);