Rework cluster_id() to take an additional argument, as we need
[jelmer/samba4-debian.git] / source / smbd / service_stream.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    helper functions for stream based servers
5
6    Copyright (C) Andrew Tridgell 2003-2005
7    Copyright (C) Stefan (metze) Metzmacher      2004
8    
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.
13    
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.
18    
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/>.
21 */
22
23 #include "includes.h"
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"
31 #include "param/param.h"
32
33 /* the range of ports to try for dcerpc over tcp endpoints */
34 #define SERVER_TCP_LOW_PORT  1024
35 #define SERVER_TCP_HIGH_PORT 1300
36
37 /* size of listen() backlog in smbd */
38 #define SERVER_LISTEN_BACKLOG 10
39
40
41 /*
42   private structure for a single listening stream socket
43 */
44 struct stream_socket {
45         const struct stream_server_ops *ops;
46         struct loadparm_context *lp_ctx;
47         struct event_context *event_ctx;
48         const struct model_ops *model_ops;
49         struct socket_context *sock;
50         void *private;
51 };
52
53
54 /*
55   close the socket and shutdown a stream_connection
56 */
57 void stream_terminate_connection(struct stream_connection *srv_conn, const char *reason)
58 {
59         struct event_context *event_ctx = srv_conn->event.ctx;
60         const struct model_ops *model_ops = srv_conn->model_ops;
61
62         if (!reason) reason = "unknown reason";
63
64         srv_conn->terminate = reason;
65
66         if (srv_conn->processing) {
67                 /* 
68                  * if we're currently inside the stream_io_handler(),
69                  * defer the termination to the end of stream_io_hendler()
70                  *
71                  * and we don't want to read or write to the connection...
72                  */
73                 event_set_fd_flags(srv_conn->event.fde, 0);
74                 return;
75         }
76
77         talloc_free(srv_conn->event.fde);
78         srv_conn->event.fde = NULL;
79         talloc_free(srv_conn);
80         model_ops->terminate(event_ctx, reason);
81 }
82
83 /**
84   the select loop has indicated that a stream is ready for IO
85 */
86 static void stream_io_handler(struct stream_connection *conn, uint16_t flags)
87 {
88         conn->processing = true;
89         if (flags & EVENT_FD_WRITE) {
90                 conn->ops->send_handler(conn, flags);
91         } else if (flags & EVENT_FD_READ) {
92                 conn->ops->recv_handler(conn, flags);
93         }
94         conn->processing = false;
95
96         if (conn->terminate) {
97                 stream_terminate_connection(conn, conn->terminate);
98         }
99 }
100
101 static void stream_io_handler_fde(struct event_context *ev, struct fd_event *fde, 
102                                   uint16_t flags, void *private)
103 {
104         struct stream_connection *conn = talloc_get_type(private, 
105                                                          struct stream_connection);
106         stream_io_handler(conn, flags);
107 }
108
109 void stream_io_handler_callback(void *private, uint16_t flags) 
110 {
111         struct stream_connection *conn = talloc_get_type(private, 
112                                                          struct stream_connection);
113         stream_io_handler(conn, flags);
114 }
115
116 /*
117   this creates a stream_connection from an already existing connection,
118   used for protocols, where a client connection needs to switched into
119   a server connection
120 */
121 NTSTATUS stream_new_connection_merge(struct event_context *ev,
122                                      const struct model_ops *model_ops,
123                                      struct socket_context *sock,
124                                      const struct stream_server_ops *stream_ops,
125                                      struct messaging_context *msg_ctx,
126                                      void *private_data,
127                                      struct stream_connection **_srv_conn)
128 {
129         struct stream_connection *srv_conn;
130
131         srv_conn = talloc_zero(ev, struct stream_connection);
132         NT_STATUS_HAVE_NO_MEMORY(srv_conn);
133
134         talloc_steal(srv_conn, sock);
135
136         srv_conn->private       = private_data;
137         srv_conn->model_ops     = model_ops;
138         srv_conn->socket        = sock;
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->event.fde     = event_add_fd(ev, srv_conn, socket_get_fd(sock),
144                                                EVENT_FD_READ, 
145                                                stream_io_handler_fde, srv_conn);
146         *_srv_conn = srv_conn;
147         return NT_STATUS_OK;
148 }
149
150 /*
151   called when a new socket connection has been established. This is called in the process
152   context of the new process (if appropriate)
153 */
154 static void stream_new_connection(struct event_context *ev,
155                                   struct loadparm_context *lp_ctx,
156                                   struct socket_context *sock, 
157                                   struct server_id server_id, void *private)
158 {
159         struct stream_socket *stream_socket = talloc_get_type(private, struct stream_socket);
160         struct stream_connection *srv_conn;
161         struct socket_address *c, *s;
162
163         srv_conn = talloc_zero(ev, struct stream_connection);
164         if (!srv_conn) {
165                 DEBUG(0,("talloc(mem_ctx, struct stream_connection) failed\n"));
166                 return;
167         }
168
169         talloc_steal(srv_conn, sock);
170
171         srv_conn->private       = stream_socket->private;
172         srv_conn->model_ops     = stream_socket->model_ops;
173         srv_conn->socket        = sock;
174         srv_conn->server_id     = server_id;
175         srv_conn->ops           = stream_socket->ops;
176         srv_conn->event.ctx     = ev;
177         srv_conn->event.fde     = event_add_fd(ev, srv_conn, socket_get_fd(sock),
178                                                0, stream_io_handler_fde, srv_conn);
179
180         if (!socket_check_access(sock, "smbd", lp_hostsallow(NULL), lp_hostsdeny(NULL))) {
181                 stream_terminate_connection(srv_conn, "denied by access rules");
182                 return;
183         }
184
185         /* setup to receive internal messages on this connection */
186         srv_conn->msg_ctx = messaging_init(srv_conn, 
187                                            lp_messaging_path(srv_conn, lp_ctx),
188                                            srv_conn->server_id, 
189                                            lp_iconv_convenience(lp_ctx),
190                                            ev);
191         if (!srv_conn->msg_ctx) {
192                 stream_terminate_connection(srv_conn, "messaging_init() failed");
193                 return;
194         }
195
196         c = socket_get_peer_addr(sock, ev);
197         s = socket_get_my_addr(sock, ev);
198         if (s && c) {
199                 const char *title;
200                 title = talloc_asprintf(s, "conn[%s] c[%s:%u] s[%s:%u] server_id[%s]",
201                                         stream_socket->ops->name, 
202                                         c->addr, c->port, s->addr, s->port,
203                                         cluster_id_string(s, server_id));
204                 if (title) {
205                         stream_connection_set_title(srv_conn, title);
206                 }
207         }
208         talloc_free(c);
209         talloc_free(s);
210
211         /* we're now ready to start receiving events on this stream */
212         EVENT_FD_READABLE(srv_conn->event.fde);
213
214         /* call the server specific accept code */
215         stream_socket->ops->accept_connection(srv_conn);
216 }
217
218
219 /*
220   called when someone opens a connection to one of our listening ports
221 */
222 static void stream_accept_handler(struct event_context *ev, struct fd_event *fde, 
223                                   uint16_t flags, void *private)
224 {
225         struct stream_socket *stream_socket = talloc_get_type(private, struct stream_socket);
226
227         /* ask the process model to create us a process for this new
228            connection.  When done, it calls stream_new_connection()
229            with the newly created socket */
230         stream_socket->model_ops->accept_connection(ev, stream_socket->lp_ctx, 
231                                                     stream_socket->sock, 
232                                                     stream_new_connection, stream_socket);
233 }
234
235 /*
236   setup a listen stream socket
237   if you pass *port == 0, then a port > 1024 is used
238
239   FIXME: This function is TCP/IP specific - uses an int rather than 
240          a string for the port. Should leave allocating a port nr 
241          to the socket implementation - JRV20070903
242  */
243 NTSTATUS stream_setup_socket(struct event_context *event_context,
244                              struct loadparm_context *lp_ctx,
245                              const struct model_ops *model_ops,
246                              const struct stream_server_ops *stream_ops,
247                              const char *family,
248                              const char *sock_addr,
249                              uint16_t *port,
250                              const char *socket_options,
251                              void *private)
252 {
253         NTSTATUS status;
254         struct stream_socket *stream_socket;
255         struct socket_address *socket_address;
256         int i;
257
258         stream_socket = talloc_zero(event_context, struct stream_socket);
259         NT_STATUS_HAVE_NO_MEMORY(stream_socket);
260
261         status = socket_create(family, SOCKET_TYPE_STREAM, &stream_socket->sock, 0);
262         NT_STATUS_NOT_OK_RETURN(status);
263
264         talloc_steal(stream_socket, stream_socket->sock);
265
266         stream_socket->lp_ctx = talloc_reference(stream_socket, lp_ctx);
267
268         /* ready to listen */
269         status = socket_set_option(stream_socket->sock, "SO_KEEPALIVE", NULL);
270         NT_STATUS_NOT_OK_RETURN(status);
271
272         if (socket_options != NULL) {
273                 status = socket_set_option(stream_socket->sock, socket_options, NULL);
274                 NT_STATUS_NOT_OK_RETURN(status);
275         }
276
277         /* TODO: set socket ACL's (host allow etc) here when they're
278          * implemented */
279
280         /* Some sockets don't have a port, or are just described from
281          * the string.  We are indicating this by having port == NULL */
282         if (!port) {
283                 socket_address = socket_address_from_strings(stream_socket, 
284                                                              stream_socket->sock->backend_name,
285                                                              sock_addr, 0);
286                 NT_STATUS_HAVE_NO_MEMORY(socket_address);
287                 status = socket_listen(stream_socket->sock, socket_address, SERVER_LISTEN_BACKLOG, 0);
288                 talloc_free(socket_address);
289
290         } else if (*port == 0) {
291                 for (i=SERVER_TCP_LOW_PORT;i<= SERVER_TCP_HIGH_PORT;i++) {
292                         socket_address = socket_address_from_strings(stream_socket, 
293                                                                      stream_socket->sock->backend_name,
294                                                                      sock_addr, i);
295                         NT_STATUS_HAVE_NO_MEMORY(socket_address);
296                         status = socket_listen(stream_socket->sock, socket_address, 
297                                                SERVER_LISTEN_BACKLOG, 0);
298                         talloc_free(socket_address);
299                         if (NT_STATUS_IS_OK(status)) {
300                                 *port = i;
301                                 break;
302                         }
303                 }
304         } else {
305                 socket_address = socket_address_from_strings(stream_socket, 
306                                                              stream_socket->sock->backend_name,
307                                                              sock_addr, *port);
308                 NT_STATUS_HAVE_NO_MEMORY(socket_address);
309                 status = socket_listen(stream_socket->sock, socket_address, SERVER_LISTEN_BACKLOG, 0);
310                 talloc_free(socket_address);
311         }
312
313         if (!NT_STATUS_IS_OK(status)) {
314                 DEBUG(0,("Failed to listen on %s:%u - %s\n",
315                         sock_addr, *port, nt_errstr(status)));
316                 talloc_free(stream_socket);
317                 return status;
318         }
319
320         /* By specifying EVENT_FD_AUTOCLOSE below, we indicate that we
321          * will close the socket using the events system.  This avoids
322          * nasty interactions with waiting for talloc to close the socket. */
323
324         socket_set_flags(stream_socket->sock, SOCKET_FLAG_NOCLOSE);
325
326         /* Add the FD from the newly created socket into the event
327          * subsystem.  it will call the accept handler whenever we get
328          * new connections */
329
330         event_add_fd(event_context, stream_socket->sock, 
331                      socket_get_fd(stream_socket->sock), 
332                      EVENT_FD_READ|EVENT_FD_AUTOCLOSE, 
333                      stream_accept_handler, stream_socket);
334
335         stream_socket->private          = talloc_reference(stream_socket, private);
336         stream_socket->ops              = stream_ops;
337         stream_socket->event_ctx        = event_context;
338         stream_socket->model_ops        = model_ops;
339
340         return NT_STATUS_OK;
341 }
342
343 /*
344   setup a connection title 
345 */
346 void stream_connection_set_title(struct stream_connection *conn, const char *title)
347 {
348         conn->model_ops->set_title(conn->event.ctx, title);
349 }