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