39c7dda227e958e2817f8c3d68f98a4f12665c47
[metze/samba/wip.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 2 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, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.h"
25 #include "process_model.h"
26 #include "lib/events/events.h"
27 #include "lib/socket/socket.h"
28 #include "smbd/service.h"
29 #include "smbd/service_stream.h"
30 #include "lib/messaging/irpc.h"
31 #include "cluster/cluster.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 event_context *event_ctx;
47         const struct model_ops *model_ops;
48         struct socket_context *sock;
49         void *private;
50 };
51
52
53 /*
54   close the socket and shutdown a stream_connection
55 */
56 void stream_terminate_connection(struct stream_connection *srv_conn, const char *reason)
57 {
58         struct event_context *event_ctx = srv_conn->event.ctx;
59         const struct model_ops *model_ops = srv_conn->model_ops;
60
61         if (!reason) reason = "unknown reason";
62
63         srv_conn->terminate = reason;
64
65         if (srv_conn->processing) {
66                 /* 
67                  * if we're currently inside the stream_io_handler(),
68                  * defer the termination to the end of stream_io_hendler()
69                  *
70                  * and we don't want to read or write to the connection...
71                  */
72                 event_set_fd_flags(srv_conn->event.fde, 0);
73                 return;
74         }
75
76         talloc_free(srv_conn->event.fde);
77         srv_conn->event.fde = NULL;
78         talloc_free(srv_conn);
79         model_ops->terminate(event_ctx, reason);
80 }
81
82 /*
83   the select loop has indicated that a stream is ready for IO
84 */
85 static void stream_io_handler(struct stream_connection *conn, uint16_t flags)
86 {
87         conn->processing = True;
88         if (flags & EVENT_FD_WRITE) {
89                 conn->ops->send_handler(conn, flags);
90         } else if (flags & EVENT_FD_READ) {
91                 conn->ops->recv_handler(conn, flags);
92         }
93         conn->processing = False;
94
95         if (conn->terminate) {
96                 stream_terminate_connection(conn, conn->terminate);
97         }
98 }
99
100 static void stream_io_handler_fde(struct event_context *ev, struct fd_event *fde, 
101                                   uint16_t flags, void *private)
102 {
103         struct stream_connection *conn = talloc_get_type(private, 
104                                                          struct stream_connection);
105         stream_io_handler(conn, flags);
106 }
107
108 void stream_io_handler_callback(void *private, uint16_t flags) 
109 {
110         struct stream_connection *conn = talloc_get_type(private, 
111                                                          struct stream_connection);
112         stream_io_handler(conn, flags);
113 }
114
115 /*
116   this creates a stream_connection from an already existing connection,
117   used for protocols, where a client connection needs to switched into
118   a server connection
119 */
120 NTSTATUS stream_new_connection_merge(struct event_context *ev,
121                                      const struct model_ops *model_ops,
122                                      struct socket_context *sock,
123                                      const struct stream_server_ops *stream_ops,
124                                      struct messaging_context *msg_ctx,
125                                      void *private_data,
126                                      struct stream_connection **_srv_conn)
127 {
128         struct stream_connection *srv_conn;
129
130         srv_conn = talloc_zero(ev, struct stream_connection);
131         NT_STATUS_HAVE_NO_MEMORY(srv_conn);
132
133         talloc_steal(srv_conn, sock);
134
135         srv_conn->private       = private_data;
136         srv_conn->model_ops     = model_ops;
137         srv_conn->socket        = sock;
138         srv_conn->server_id     = cluster_id(0);
139         srv_conn->ops           = stream_ops;
140         srv_conn->msg_ctx       = msg_ctx;
141         srv_conn->event.ctx     = ev;
142         srv_conn->event.fde     = event_add_fd(ev, srv_conn, socket_get_fd(sock),
143                                                EVENT_FD_READ, 
144                                                stream_io_handler_fde, srv_conn);
145         *_srv_conn = srv_conn;
146         return NT_STATUS_OK;
147 }
148
149 /*
150   called when a new socket connection has been established. This is called in the process
151   context of the new process (if appropriate)
152 */
153 static void stream_new_connection(struct event_context *ev,
154                                   struct socket_context *sock, 
155                                   struct server_id server_id, void *private)
156 {
157         struct stream_socket *stream_socket = talloc_get_type(private, struct stream_socket);
158         struct stream_connection *srv_conn;
159         struct socket_address *c, *s;
160
161         srv_conn = talloc_zero(ev, struct stream_connection);
162         if (!srv_conn) {
163                 DEBUG(0,("talloc(mem_ctx, struct stream_connection) failed\n"));
164                 return;
165         }
166
167         talloc_steal(srv_conn, sock);
168
169         srv_conn->private       = stream_socket->private;
170         srv_conn->model_ops     = stream_socket->model_ops;
171         srv_conn->socket        = sock;
172         srv_conn->server_id     = server_id;
173         srv_conn->ops           = stream_socket->ops;
174         srv_conn->event.ctx     = ev;
175         srv_conn->event.fde     = event_add_fd(ev, srv_conn, socket_get_fd(sock),
176                                                EVENT_FD_READ, 
177                                                stream_io_handler_fde, srv_conn);
178
179         if (!socket_check_access(sock, "smbd", lp_hostsallow(-1), lp_hostsdeny(-1))) {
180                 stream_terminate_connection(srv_conn, "denied by access rules");
181                 return;
182         }
183
184         /* setup to receive internal messages on this connection */
185         srv_conn->msg_ctx = messaging_init(srv_conn, srv_conn->server_id, ev);
186         if (!srv_conn->msg_ctx) {
187                 stream_terminate_connection(srv_conn, "messaging_init() failed");
188                 return;
189         }
190
191         c = socket_get_peer_addr(sock, ev);
192         s = socket_get_my_addr(sock, ev);
193         if (s && c) {
194                 const char *title;
195                 title = talloc_asprintf(s, "conn[%s] c[%s:%u] s[%s:%u] server_id[%s]",
196                                         stream_socket->ops->name, 
197                                         c->addr, c->port, s->addr, s->port,
198                                         cluster_id_string(s, server_id));
199                 if (title) {
200                         stream_connection_set_title(srv_conn, title);
201                 }
202         }
203         talloc_free(c);
204         talloc_free(s);
205
206         /* call the server specific accept code */
207         stream_socket->ops->accept_connection(srv_conn);
208 }
209
210
211 /*
212   called when someone opens a connection to one of our listening ports
213 */
214 static void stream_accept_handler(struct event_context *ev, struct fd_event *fde, 
215                                   uint16_t flags, void *private)
216 {
217         struct stream_socket *stream_socket = talloc_get_type(private, struct stream_socket);
218
219         /* ask the process model to create us a process for this new
220            connection.  When done, it calls stream_new_connection()
221            with the newly created socket */
222         stream_socket->model_ops->accept_connection(ev, stream_socket->sock, 
223                                                     stream_new_connection, stream_socket);
224 }
225
226
227
228 /*
229   setup a listen stream socket
230   if you pass *port == 0, then a port > 1024 is used
231  */
232 NTSTATUS stream_setup_socket(struct event_context *event_context,
233                              const struct model_ops *model_ops,
234                              const struct stream_server_ops *stream_ops,
235                              const char *family,
236                              const char *sock_addr,
237                              uint16_t *port,
238                              void *private)
239 {
240         NTSTATUS status;
241         struct stream_socket *stream_socket;
242         struct socket_address *socket_address;
243         int i;
244
245         stream_socket = talloc_zero(event_context, struct stream_socket);
246         NT_STATUS_HAVE_NO_MEMORY(stream_socket);
247
248         status = socket_create(family, SOCKET_TYPE_STREAM, &stream_socket->sock, 0);
249         NT_STATUS_NOT_OK_RETURN(status);
250
251         talloc_steal(stream_socket, stream_socket->sock);
252
253         /* ready to listen */
254         status = socket_set_option(stream_socket->sock, "SO_KEEPALIVE", NULL);
255         NT_STATUS_NOT_OK_RETURN(status);
256
257         status = socket_set_option(stream_socket->sock, lp_socket_options(), NULL);
258         NT_STATUS_NOT_OK_RETURN(status);
259
260         /* TODO: set socket ACL's here when they're implemented */
261
262         if (*port == 0) {
263                 for (i=SERVER_TCP_LOW_PORT;i<= SERVER_TCP_HIGH_PORT;i++) {
264                         socket_address = socket_address_from_strings(stream_socket, 
265                                                                      stream_socket->sock->backend_name,
266                                                                      sock_addr, i);
267                         NT_STATUS_HAVE_NO_MEMORY(socket_address);
268                         status = socket_listen(stream_socket->sock, socket_address, 
269                                                SERVER_LISTEN_BACKLOG, 0);
270                         talloc_free(socket_address);
271                         if (NT_STATUS_IS_OK(status)) {
272                                 *port = i;
273                                 break;
274                         }
275                 }
276         } else {
277                 socket_address = socket_address_from_strings(stream_socket, 
278                                                              stream_socket->sock->backend_name,
279                                                              sock_addr, *port);
280                 NT_STATUS_HAVE_NO_MEMORY(socket_address);
281                 status = socket_listen(stream_socket->sock, socket_address, SERVER_LISTEN_BACKLOG, 0);
282                 talloc_free(socket_address);
283         }
284
285         if (!NT_STATUS_IS_OK(status)) {
286                 DEBUG(0,("Failed to listen on %s:%u - %s\n",
287                         sock_addr, *port, nt_errstr(status)));
288                 talloc_free(stream_socket);
289                 return status;
290         }
291
292         /* we will close the socket using the events system */
293         socket_set_flags(stream_socket->sock, SOCKET_FLAG_NOCLOSE);
294
295         event_add_fd(event_context, stream_socket->sock, 
296                      socket_get_fd(stream_socket->sock), 
297                      EVENT_FD_READ|EVENT_FD_AUTOCLOSE, 
298                      stream_accept_handler, stream_socket);
299
300         stream_socket->private          = talloc_reference(stream_socket, private);
301         stream_socket->ops              = stream_ops;
302         stream_socket->event_ctx        = event_context;
303         stream_socket->model_ops        = model_ops;
304
305         return NT_STATUS_OK;
306 }
307
308 /*
309   setup a connection title 
310 */
311 void stream_connection_set_title(struct stream_connection *conn, const char *title)
312 {
313         conn->model_ops->set_title(conn->event.ctx, title);
314 }