fe07dc5a38ffc0c24fbce8ed07e47cb4ec59a547
[kai/samba.git] / source4 / 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 <tevent.h>
25 #include "process_model.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 tevent_context *event_ctx;
48         const struct model_ops *model_ops;
49         struct socket_context *sock;
50         void *private_data;
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 tevent_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         DEBUG(3,("Terminating connection - '%s'\n", reason));
65
66         srv_conn->terminate = reason;
67
68         if (srv_conn->processing) {
69                 /* 
70                  * if we're currently inside the stream_io_handler(),
71                  * defer the termination to the end of stream_io_hendler()
72                  *
73                  * and we don't want to read or write to the connection...
74                  */
75                 tevent_fd_set_flags(srv_conn->event.fde, 0);
76                 return;
77         }
78
79         talloc_free(srv_conn->event.fde);
80         srv_conn->event.fde = NULL;
81         model_ops->terminate(event_ctx, srv_conn->lp_ctx, reason);
82         talloc_free(srv_conn);
83 }
84
85 /**
86   the select loop has indicated that a stream is ready for IO
87 */
88 static void stream_io_handler(struct stream_connection *conn, uint16_t flags)
89 {
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);
95         }
96         conn->processing--;
97
98         if (conn->terminate) {
99                 stream_terminate_connection(conn, conn->terminate);
100         }
101 }
102
103 static void stream_io_handler_fde(struct tevent_context *ev, struct tevent_fd *fde, 
104                                   uint16_t flags, void *private_data)
105 {
106         struct stream_connection *conn = talloc_get_type(private_data,
107                                                          struct stream_connection);
108         stream_io_handler(conn, flags);
109 }
110
111 void stream_io_handler_callback(void *private_data, uint16_t flags)
112 {
113         struct stream_connection *conn = talloc_get_type(private_data,
114                                                          struct stream_connection);
115         stream_io_handler(conn, flags);
116 }
117
118 /*
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
122 */
123 NTSTATUS stream_new_connection_merge(struct tevent_context *ev,
124                                      struct loadparm_context *lp_ctx,
125                                      const struct model_ops *model_ops,
126                                      struct socket_context *sock,
127                                      const struct stream_server_ops *stream_ops,
128                                      struct messaging_context *msg_ctx,
129                                      void *private_data,
130                                      struct stream_connection **_srv_conn)
131 {
132         struct stream_connection *srv_conn;
133
134         srv_conn = talloc_zero(ev, struct stream_connection);
135         NT_STATUS_HAVE_NO_MEMORY(srv_conn);
136
137         talloc_steal(srv_conn, sock);
138
139         srv_conn->private       = private_data;
140         srv_conn->model_ops     = model_ops;
141         srv_conn->socket        = sock;
142         srv_conn->server_id     = cluster_id(0, 0);
143         srv_conn->ops           = stream_ops;
144         srv_conn->msg_ctx       = msg_ctx;
145         srv_conn->event.ctx     = ev;
146         srv_conn->lp_ctx        = lp_ctx;
147         srv_conn->event.fde     = tevent_add_fd(ev, srv_conn, socket_get_fd(sock),
148                                                 TEVENT_FD_READ,
149                                                 stream_io_handler_fde, srv_conn);
150         if (!srv_conn->event.fde) {
151                 talloc_free(srv_conn);
152                 return NT_STATUS_NO_MEMORY;
153         }
154
155         *_srv_conn = srv_conn;
156         return NT_STATUS_OK;
157 }
158
159 /*
160   called when a new socket connection has been established. This is called in the process
161   context of the new process (if appropriate)
162 */
163 static void stream_new_connection(struct tevent_context *ev,
164                                   struct loadparm_context *lp_ctx,
165                                   struct socket_context *sock, 
166                                   struct server_id server_id, void *private_data)
167 {
168         struct stream_socket *stream_socket = talloc_get_type(private_data, struct stream_socket);
169         struct stream_connection *srv_conn;
170         struct socket_address *c, *s;
171
172         srv_conn = talloc_zero(ev, struct stream_connection);
173         if (!srv_conn) {
174                 DEBUG(0,("talloc(mem_ctx, struct stream_connection) failed\n"));
175                 return;
176         }
177
178         talloc_steal(srv_conn, sock);
179
180         srv_conn->private       = stream_socket->private_data;
181         srv_conn->model_ops     = stream_socket->model_ops;
182         srv_conn->socket        = sock;
183         srv_conn->server_id     = server_id;
184         srv_conn->ops           = stream_socket->ops;
185         srv_conn->event.ctx     = ev;
186         srv_conn->lp_ctx        = lp_ctx;
187
188         if (!socket_check_access(sock, "smbd", lp_hostsallow(NULL, lp_default_service(lp_ctx)), lp_hostsdeny(NULL, lp_default_service(lp_ctx)))) {
189                 stream_terminate_connection(srv_conn, "denied by access rules");
190                 return;
191         }
192
193         srv_conn->event.fde     = tevent_add_fd(ev, srv_conn, socket_get_fd(sock),
194                                                 0, stream_io_handler_fde, srv_conn);
195         if (!srv_conn->event.fde) {
196                 stream_terminate_connection(srv_conn, "tevent_add_fd() failed");
197                 return;
198         }
199
200         /* setup to receive internal messages on this connection */
201         srv_conn->msg_ctx = messaging_init(srv_conn, 
202                                            lp_messaging_path(srv_conn, lp_ctx),
203                                            srv_conn->server_id, 
204                                            lp_iconv_convenience(lp_ctx),
205                                            ev);
206         if (!srv_conn->msg_ctx) {
207                 stream_terminate_connection(srv_conn, "messaging_init() failed");
208                 return;
209         }
210
211         c = socket_get_peer_addr(sock, ev);
212         s = socket_get_my_addr(sock, ev);
213         if (s && c) {
214                 const char *title;
215                 title = talloc_asprintf(s, "conn[%s] c[%s:%u] s[%s:%u] server_id[%s]",
216                                         stream_socket->ops->name, 
217                                         c->addr, c->port, s->addr, s->port,
218                                         cluster_id_string(s, server_id));
219                 if (title) {
220                         stream_connection_set_title(srv_conn, title);
221                 }
222         }
223         talloc_free(c);
224         talloc_free(s);
225
226         /* we're now ready to start receiving events on this stream */
227         TEVENT_FD_READABLE(srv_conn->event.fde);
228
229         /* call the server specific accept code */
230         stream_socket->ops->accept_connection(srv_conn);
231 }
232
233
234 /*
235   called when someone opens a connection to one of our listening ports
236 */
237 static void stream_accept_handler(struct tevent_context *ev, struct tevent_fd *fde, 
238                                   uint16_t flags, void *private_data)
239 {
240         struct stream_socket *stream_socket = talloc_get_type(private_data, struct stream_socket);
241
242         /* ask the process model to create us a process for this new
243            connection.  When done, it calls stream_new_connection()
244            with the newly created socket */
245         stream_socket->model_ops->accept_connection(ev, stream_socket->lp_ctx, 
246                                                     stream_socket->sock, 
247                                                     stream_new_connection, stream_socket);
248 }
249
250 /*
251   setup a listen stream socket
252   if you pass *port == 0, then a port > 1024 is used
253
254   FIXME: This function is TCP/IP specific - uses an int rather than 
255          a string for the port. Should leave allocating a port nr 
256          to the socket implementation - JRV20070903
257  */
258 NTSTATUS stream_setup_socket(struct tevent_context *event_context,
259                              struct loadparm_context *lp_ctx,
260                              const struct model_ops *model_ops,
261                              const struct stream_server_ops *stream_ops,
262                              const char *family,
263                              const char *sock_addr,
264                              uint16_t *port,
265                              const char *socket_options,
266                              void *private_data)
267 {
268         NTSTATUS status;
269         struct stream_socket *stream_socket;
270         struct socket_address *socket_address;
271         struct tevent_fd *fde;
272         int i;
273
274         stream_socket = talloc_zero(event_context, struct stream_socket);
275         NT_STATUS_HAVE_NO_MEMORY(stream_socket);
276
277         status = socket_create(family, SOCKET_TYPE_STREAM, &stream_socket->sock, 0);
278         NT_STATUS_NOT_OK_RETURN(status);
279
280         talloc_steal(stream_socket, stream_socket->sock);
281
282         stream_socket->lp_ctx = talloc_reference(stream_socket, lp_ctx);
283
284         /* ready to listen */
285         status = socket_set_option(stream_socket->sock, "SO_KEEPALIVE", NULL);
286         NT_STATUS_NOT_OK_RETURN(status);
287
288         if (socket_options != NULL) {
289                 status = socket_set_option(stream_socket->sock, socket_options, NULL);
290                 NT_STATUS_NOT_OK_RETURN(status);
291         }
292
293         /* TODO: set socket ACL's (host allow etc) here when they're
294          * implemented */
295
296         /* Some sockets don't have a port, or are just described from
297          * the string.  We are indicating this by having port == NULL */
298         if (!port) {
299                 socket_address = socket_address_from_strings(stream_socket, 
300                                                              stream_socket->sock->backend_name,
301                                                              sock_addr, 0);
302                 NT_STATUS_HAVE_NO_MEMORY(socket_address);
303                 status = socket_listen(stream_socket->sock, socket_address, SERVER_LISTEN_BACKLOG, 0);
304                 talloc_free(socket_address);
305
306         } else if (*port == 0) {
307                 for (i=SERVER_TCP_LOW_PORT;i<= SERVER_TCP_HIGH_PORT;i++) {
308                         socket_address = socket_address_from_strings(stream_socket, 
309                                                                      stream_socket->sock->backend_name,
310                                                                      sock_addr, i);
311                         NT_STATUS_HAVE_NO_MEMORY(socket_address);
312                         status = socket_listen(stream_socket->sock, socket_address, 
313                                                SERVER_LISTEN_BACKLOG, 0);
314                         talloc_free(socket_address);
315                         if (NT_STATUS_IS_OK(status)) {
316                                 *port = i;
317                                 break;
318                         }
319                 }
320         } else {
321                 socket_address = socket_address_from_strings(stream_socket, 
322                                                              stream_socket->sock->backend_name,
323                                                              sock_addr, *port);
324                 NT_STATUS_HAVE_NO_MEMORY(socket_address);
325                 status = socket_listen(stream_socket->sock, socket_address, SERVER_LISTEN_BACKLOG, 0);
326                 talloc_free(socket_address);
327         }
328
329         if (!NT_STATUS_IS_OK(status)) {
330                 DEBUG(0,("Failed to listen on %s:%u - %s\n",
331                         sock_addr, *port, nt_errstr(status)));
332                 talloc_free(stream_socket);
333                 return status;
334         }
335
336         /* Add the FD from the newly created socket into the event
337          * subsystem.  it will call the accept handler whenever we get
338          * new connections */
339
340         fde = tevent_add_fd(event_context, stream_socket->sock,
341                             socket_get_fd(stream_socket->sock),
342                             TEVENT_FD_READ,
343                             stream_accept_handler, stream_socket);
344         if (!fde) {
345                 DEBUG(0,("Failed to setup fd event\n"));
346                 talloc_free(stream_socket);
347                 return NT_STATUS_NO_MEMORY;
348         }
349
350         /* we let events system to the close on the socket. This avoids
351          * nasty interactions with waiting for talloc to close the socket. */
352         tevent_fd_set_close_fn(fde, socket_tevent_fd_close_fn);
353         socket_set_flags(stream_socket->sock, SOCKET_FLAG_NOCLOSE);
354
355         stream_socket->private_data     = talloc_reference(stream_socket, private_data);
356         stream_socket->ops              = stream_ops;
357         stream_socket->event_ctx        = event_context;
358         stream_socket->model_ops        = model_ops;
359
360         return NT_STATUS_OK;
361 }
362
363 /*
364   setup a connection title 
365 */
366 void stream_connection_set_title(struct stream_connection *conn, const char *title)
367 {
368         conn->model_ops->set_title(conn->event.ctx, title);
369 }