f9031433d38a5542d09fb68bd2cc48c5af0ee7b1
[jelmer/samba4-debian.git] / source / web_server / web_server.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    web server startup
5
6    Copyright (C) Andrew Tridgell 2005
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "smbd/service_task.h"
24 #include "smbd/service_stream.h"
25 #include "smbd/service.h"
26 #include "web_server/web_server.h"
27 #include "lib/events/events.h"
28 #include "system/filesys.h"
29 #include "system/network.h"
30 #include "lib/socket/netif.h"
31 #include "lib/tls/tls.h"
32
33 /* don't allow connections to hang around forever */
34 #define HTTP_TIMEOUT 120
35
36 /*
37   destroy a web connection
38 */
39 static int websrv_destructor(struct websrv_context *web)
40 {
41         if (web->output.fd != -1) {
42                 close(web->output.fd);
43         }
44         return 0;
45 }
46
47 /*
48   called when a connection times out. This prevents a stuck connection
49   from hanging around forever
50 */
51 static void websrv_timeout(struct event_context *event_context, 
52                            struct timed_event *te, 
53                            struct timeval t, void *private)
54 {
55         struct websrv_context *web = talloc_get_type(private, struct websrv_context);
56         struct stream_connection *conn = web->conn;
57         web->conn = NULL;
58         /* TODO: send a message to any running esp context on this connection
59            to stop running */
60         stream_terminate_connection(conn, "websrv_timeout: timed out"); 
61 }
62
63 /*
64   called when a web connection becomes readable
65 */
66 static void websrv_recv(struct stream_connection *conn, uint16_t flags)
67 {
68         struct websrv_context *web = talloc_get_type(conn->private, 
69                                                      struct websrv_context);
70         NTSTATUS status;
71         uint8_t buf[1024];
72         size_t nread;
73         uint8_t *p;
74         DATA_BLOB b;
75
76         /* not the most efficient http parser ever, but good enough for us */
77         status = socket_recv(conn->socket, buf, sizeof(buf), &nread);
78         if (NT_STATUS_IS_ERR(status)) goto failed;
79         if (!NT_STATUS_IS_OK(status)) return;
80
81         if (!data_blob_append(web, &web->input.partial, buf, nread))
82                 goto failed;
83
84         /* parse any lines that are available */
85         b = web->input.partial;
86         while (!web->input.end_of_headers &&
87                (p=memchr(b.data, '\n', b.length))) {
88                 const char *line = (const char *)b.data;
89                 *p = 0;
90                 if (p != b.data && p[-1] == '\r') {
91                         p[-1] = 0;
92                 }
93                 status = http_parse_header(web, line);
94                 if (!NT_STATUS_IS_OK(status)) return;
95                 b.length -= (p - b.data) + 1;
96                 b.data = p+1;
97         }
98
99         /* keep any remaining bytes in web->input.partial */
100         if (b.length == 0) {
101                 b.data = NULL;
102         }
103         b = data_blob_talloc(web, b.data, b.length);
104         data_blob_free(&web->input.partial);
105         web->input.partial = b;
106
107         /* we finish when we have both the full headers (terminated by
108            a blank line) and any post data, as indicated by the
109            content_length */
110         if (web->input.end_of_headers &&
111             web->input.partial.length >= web->input.content_length) {
112                 if (web->input.partial.length > web->input.content_length) {
113                         web->input.partial.data[web->input.content_length] = 0;
114                 }
115                 EVENT_FD_NOT_READABLE(web->conn->event.fde);
116
117                 /* the reference/unlink code here is quite subtle. It
118                  is needed because the rendering of the web-pages, and
119                  in particular the esp/ejs backend, is semi-async.  So
120                  we could well end up in the connection timeout code
121                  while inside http_process_input(), but we must not
122                  destroy the stack variables being used by that
123                  rendering process when we handle the timeout. */
124                 if (!talloc_reference(web->task, web)) goto failed;
125                 http_process_input(web);
126                 talloc_unlink(web->task, web);
127         }
128         return;
129
130 failed:
131         stream_terminate_connection(conn, "websrv_recv: failed");
132 }
133
134
135 /*
136   called when a web connection becomes writable
137 */
138 static void websrv_send(struct stream_connection *conn, uint16_t flags)
139 {
140         struct websrv_context *web = talloc_get_type(conn->private, 
141                                                      struct websrv_context);
142         NTSTATUS status;
143         size_t nsent;
144         DATA_BLOB b;
145
146         b = web->output.content;
147         b.data += web->output.nsent;
148         b.length -= web->output.nsent;
149
150         status = socket_send(conn->socket, &b, &nsent);
151         if (NT_STATUS_IS_ERR(status)) {
152                 stream_terminate_connection(web->conn, "socket_send: failed");
153                 return;
154         }
155         if (!NT_STATUS_IS_OK(status)) {
156                 return;
157         }
158
159         web->output.nsent += nsent;
160
161         /* possibly read some more raw data from a file */
162         if (web->output.content.length == web->output.nsent && 
163             web->output.fd != -1) {
164                 uint8_t buf[2048];
165                 ssize_t nread;
166
167                 data_blob_free(&web->output.content);
168                 web->output.nsent = 0;
169
170                 nread = read(web->output.fd, buf, sizeof(buf));
171                 if (nread == -1 && errno == EINTR) {
172                         return;
173                 }
174                 if (nread <= 0) {
175                         close(web->output.fd);
176                         web->output.fd = -1;
177                         nread = 0;
178                 }
179                 web->output.content = data_blob_talloc(web, buf, nread);
180         }
181
182         if (web->output.content.length == web->output.nsent && 
183             web->output.fd == -1) {
184                 stream_terminate_connection(web->conn, "websrv_send: finished sending");
185         }
186 }
187
188 /*
189   establish a new connection to the web server
190 */
191 static void websrv_accept(struct stream_connection *conn)
192 {
193         struct task_server *task = talloc_get_type(conn->private, struct task_server);
194         struct esp_data *edata = talloc_get_type(task->private, struct esp_data);
195         struct websrv_context *web;
196         struct socket_context *tls_socket;
197
198         web = talloc_zero(conn, struct websrv_context);
199         if (web == NULL) goto failed;
200
201         web->task = task;
202         web->conn = conn;
203         conn->private = web;
204         web->output.fd = -1;
205         talloc_set_destructor(web, websrv_destructor);
206
207         event_add_timed(conn->event.ctx, web, 
208                         timeval_current_ofs(HTTP_TIMEOUT, 0),
209                         websrv_timeout, web);
210
211         /* Overwrite the socket with a (possibly) TLS socket */
212         tls_socket = tls_init_server(edata->tls_params, conn->socket, 
213                                      conn->event.fde, "GPHO");
214         /* We might not have TLS, or it might not have initilised */
215         if (tls_socket) {
216                 talloc_unlink(conn, conn->socket);
217                 talloc_steal(conn, tls_socket);
218                 conn->socket = tls_socket;
219         } else {
220                 DEBUG(3, ("TLS not available for web_server connections\n"));
221         }
222
223         return;
224
225 failed:
226         talloc_free(conn);
227 }
228
229
230 static const struct stream_server_ops web_stream_ops = {
231         .name                   = "web",
232         .accept_connection      = websrv_accept,
233         .recv_handler           = websrv_recv,
234         .send_handler           = websrv_send,
235 };
236
237 /*
238   startup the web server task
239 */
240 static void websrv_task_init(struct task_server *task)
241 {
242         NTSTATUS status;
243         uint16_t port = lp_web_port();
244         const struct model_ops *model_ops;
245
246         task_server_set_title(task, "task[websrv]");
247
248         /* run the web server as a single process */
249         model_ops = process_model_byname("single");
250         if (!model_ops) goto failed;
251
252         if (lp_interfaces() && lp_bind_interfaces_only()) {
253                 int num_interfaces = iface_count();
254                 int i;
255                 for(i = 0; i < num_interfaces; i++) {
256                         const char *address = iface_n_ip(i);
257                         status = stream_setup_socket(task->event_ctx, model_ops, 
258                                                      &web_stream_ops, 
259                                                      "ipv4", address, 
260                                                      &port, task);
261                         if (!NT_STATUS_IS_OK(status)) goto failed;
262                 }
263         } else {
264                 status = stream_setup_socket(task->event_ctx, model_ops, 
265                                              &web_stream_ops, 
266                                              "ipv4", lp_socket_address(), 
267                                              &port, task);
268                 if (!NT_STATUS_IS_OK(status)) goto failed;
269         }
270
271         /* startup the esp processor - unfortunately we can't do this
272            per connection as that wouldn't allow for session variables */
273         status = http_setup_esp(task);
274         if (!NT_STATUS_IS_OK(status)) goto failed;
275
276         return;
277
278 failed:
279         task_server_terminate(task, "websrv_task_init: failed to startup web server task");
280 }
281
282
283 /*
284   called on startup of the web server service It's job is to start
285   listening on all configured sockets
286 */
287 static NTSTATUS websrv_init(struct event_context *event_context, 
288                             const struct model_ops *model_ops)
289 {       
290         return task_server_startup(event_context, model_ops, websrv_task_init);
291 }
292
293 /* called at smbd startup - register ourselves as a server service */
294 NTSTATUS server_service_web_init(void)
295 {
296         return register_server_service("web", websrv_init);
297 }