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