r10682: force the free of the fd event first when a stream terminates. That ensures
[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_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         talloc_free(srv_conn->event.fde);
59         talloc_free(srv_conn);
60         model_ops->terminate(event_ctx, reason);
61 }
62
63 /*
64   the select loop has indicated that a stream is ready for IO
65 */
66 static void stream_io_handler(struct event_context *ev, struct fd_event *fde, 
67                               uint16_t flags, void *private)
68 {
69         struct stream_connection *conn = talloc_get_type(private, 
70                                                          struct stream_connection);
71         if (flags & EVENT_FD_WRITE) {
72                 conn->ops->send_handler(conn, flags);
73                 return;
74         }
75
76         if (flags & EVENT_FD_READ) {
77                 conn->ops->recv_handler(conn, flags);
78         }
79 }
80
81
82 /*
83   called when a new socket connection has been established. This is called in the process
84   context of the new process (if appropriate)
85 */
86 static void stream_new_connection(struct event_context *ev,
87                                   struct socket_context *sock, 
88                                   uint32_t server_id, void *private)
89 {
90         struct stream_socket *stream_socket = talloc_get_type(private, struct stream_socket);
91         struct stream_connection *srv_conn;
92
93         srv_conn = talloc_zero(ev, struct stream_connection);
94         if (!srv_conn) {
95                 DEBUG(0,("talloc(mem_ctx, struct stream_connection) failed\n"));
96                 return;
97         }
98
99         talloc_steal(srv_conn, sock);
100
101         srv_conn->private       = stream_socket->private;
102         srv_conn->model_ops     = stream_socket->model_ops;
103         srv_conn->socket        = sock;
104         srv_conn->server_id     = server_id;
105         srv_conn->ops           = stream_socket->ops;
106         srv_conn->event.ctx     = ev;
107         srv_conn->event.fde     = event_add_fd(ev, srv_conn, socket_get_fd(sock),
108                                                EVENT_FD_READ, 
109                                                stream_io_handler, srv_conn);
110
111         if (!socket_check_access(sock, "smbd", lp_hostsallow(-1), lp_hostsdeny(-1))) {
112                 stream_terminate_connection(srv_conn, "denied by access rules");
113                 return;
114         }
115
116         /* setup to receive internal messages on this connection */
117         srv_conn->msg_ctx = messaging_init(srv_conn, srv_conn->server_id, ev);
118         if (!srv_conn->msg_ctx) {
119                 stream_terminate_connection(srv_conn, "messaging_init() failed");
120                 return;
121         }
122
123         /* call the server specific accept code */
124         stream_socket->ops->accept_connection(srv_conn);
125 }
126
127
128 /*
129   called when someone opens a connection to one of our listening ports
130 */
131 static void stream_accept_handler(struct event_context *ev, struct fd_event *fde, 
132                                   uint16_t flags, void *private)
133 {
134         struct stream_socket *stream_socket = talloc_get_type(private, struct stream_socket);
135
136         /* ask the process model to create us a process for this new
137            connection.  When done, it calls stream_new_connection()
138            with the newly created socket */
139         stream_socket->model_ops->accept_connection(ev, stream_socket->sock, 
140                                                     stream_new_connection, stream_socket);
141 }
142
143
144
145 /*
146   setup a listen stream socket
147   if you pass *port == 0, then a port > 1024 is used
148  */
149 NTSTATUS stream_setup_socket(struct event_context *event_context,
150                              const struct model_ops *model_ops,
151                              const struct stream_server_ops *stream_ops,
152                              const char *family,
153                              const char *sock_addr,
154                              uint16_t *port,
155                              void *private)
156 {
157         NTSTATUS status;
158         struct stream_socket *stream_socket;
159         int i;
160
161         stream_socket = talloc_zero(event_context, struct stream_socket);
162         NT_STATUS_HAVE_NO_MEMORY(stream_socket);
163
164         status = socket_create(family, SOCKET_TYPE_STREAM, &stream_socket->sock, 0);
165         NT_STATUS_NOT_OK_RETURN(status);
166
167         talloc_steal(stream_socket, stream_socket->sock);
168
169         /* ready to listen */
170         status = socket_set_option(stream_socket->sock, "SO_KEEPALIVE", NULL);
171         NT_STATUS_NOT_OK_RETURN(status);
172
173         status = socket_set_option(stream_socket->sock, lp_socket_options(), NULL);
174         NT_STATUS_NOT_OK_RETURN(status);
175
176         /* TODO: set socket ACL's here when they're implemented */
177
178         if (*port == 0) {
179                 for (i=SERVER_TCP_LOW_PORT;i<= SERVER_TCP_HIGH_PORT;i++) {
180                         status = socket_listen(stream_socket->sock, sock_addr, i, 
181                                                SERVER_LISTEN_BACKLOG, 0);
182                         if (NT_STATUS_IS_OK(status)) {
183                                 *port = i;
184                                 break;
185                         }
186                 }
187         } else {
188                 status = socket_listen(stream_socket->sock, sock_addr, *port, SERVER_LISTEN_BACKLOG, 0);
189         }
190
191         if (!NT_STATUS_IS_OK(status)) {
192                 DEBUG(0,("Failed to listen on %s:%u - %s\n",
193                         sock_addr, *port, nt_errstr(status)));
194                 talloc_free(stream_socket);
195                 return status;
196         }
197
198         event_add_fd(event_context, stream_socket->sock, 
199                      socket_get_fd(stream_socket->sock), 
200                      EVENT_FD_READ, stream_accept_handler, stream_socket);
201
202         stream_socket->private          = talloc_reference(stream_socket, private);
203         stream_socket->ops              = stream_ops;
204         stream_socket->event_ctx        = event_context;
205         stream_socket->model_ops        = model_ops;
206
207         return NT_STATUS_OK;
208 }