s4:web_server/*.c - optimise includes
[sfrench/samba-autobuild/.git] / source4 / web_server / web_server.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    web server startup
5
6    Copyright (C) Andrew Tridgell 2005
7    Copyright (C) Jelmer Vernooij 2008
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 3 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, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "includes.h"
24 #include "web_server/web_server.h"
25 #include "../lib/util/dlinklist.h"
26 #include "lib/tls/tls.h"
27 #include "lib/events/events.h"
28 #include "lib/socket/netif.h"
29 #include "param/param.h"
30
31 /* don't allow connections to hang around forever */
32 #define HTTP_TIMEOUT 120
33
34 /*
35   destroy a web connection
36 */
37 static int websrv_destructor(struct websrv_context *web)
38 {
39         return 0;
40 }
41
42 /*
43   called when a connection times out. This prevents a stuck connection
44   from hanging around forever
45 */
46 static void websrv_timeout(struct tevent_context *event_context, 
47                            struct tevent_timer *te, 
48                            struct timeval t, void *private_data)
49 {
50         struct websrv_context *web = talloc_get_type(private_data, struct websrv_context);
51         struct stream_connection *conn = web->conn;
52         web->conn = NULL;
53         /* TODO: send a message to any running esp context on this connection
54            to stop running */
55         stream_terminate_connection(conn, "websrv_timeout: timed out"); 
56 }
57
58 /*
59   setup for a raw http level error
60 */
61 void http_error(struct websrv_context *web, const char *status, const char *info)
62 {
63         char *s;
64         s = talloc_asprintf(web,"<HTML><HEAD><TITLE>Error %s</TITLE></HEAD><BODY><H1>Error %s</H1><pre>%s</pre><p></BODY></HTML>\r\n\r\n", 
65                             status, status, info);
66         if (s == NULL) {
67                 stream_terminate_connection(web->conn, "http_error: out of memory");
68                 return;
69         }
70         websrv_output_headers(web, status, NULL);
71         websrv_output(web, s, strlen(s));
72 }
73
74 void websrv_output_headers(struct websrv_context *web, const char *status, struct http_header *headers)
75 {
76         char *s;
77         DATA_BLOB b;
78         struct http_header *hdr;
79
80         s = talloc_asprintf(web, "HTTP/1.0 %s\r\n", status);
81         if (s == NULL) return;
82         for (hdr = headers; hdr; hdr = hdr->next) {
83                 s = talloc_asprintf_append_buffer(s, "%s: %s\r\n", hdr->name, hdr->value);
84         }
85
86         s = talloc_asprintf_append_buffer(s, "\r\n");
87
88         b = web->output.content;
89         web->output.content = data_blob_string_const(s);
90         websrv_output(web, b.data, b.length);
91         data_blob_free(&b);
92 }
93
94 void websrv_output(struct websrv_context *web, void *data, size_t length)
95 {
96         data_blob_append(web, &web->output.content, data, length);
97         EVENT_FD_NOT_READABLE(web->conn->event.fde);
98         EVENT_FD_WRITEABLE(web->conn->event.fde);
99         web->output.output_pending = true;
100 }
101
102
103 /*
104   parse one line of header input
105 */
106 NTSTATUS http_parse_header(struct websrv_context *web, const char *line)
107 {
108         if (line[0] == 0) {
109                 web->input.end_of_headers = true;
110         } else if (strncasecmp(line,"GET ", 4)==0) {
111                 web->input.url = talloc_strndup(web, &line[4], strcspn(&line[4], " \t"));
112         } else if (strncasecmp(line,"POST ", 5)==0) {
113                 web->input.post_request = true;
114                 web->input.url = talloc_strndup(web, &line[5], strcspn(&line[5], " \t"));
115         } else if (strchr(line, ':') == NULL) {
116                 http_error(web, "400 Bad request", "This server only accepts GET and POST requests");
117                 return NT_STATUS_INVALID_PARAMETER;
118         } else if (strncasecmp(line, "Content-Length: ", 16)==0) {
119                 web->input.content_length = strtoul(&line[16], NULL, 10);
120         } else {
121                 struct http_header *hdr = talloc_zero(web, struct http_header);
122                 char *colon = strchr(line, ':');
123                 if (colon == NULL) {
124                         http_error(web, "500 Internal Server Error", "invalidly formatted header");
125                         return NT_STATUS_INVALID_PARAMETER;
126                 }
127
128                 hdr->name = talloc_strndup(hdr, line, colon-line);
129                 hdr->value = talloc_strdup(hdr, colon+1);
130                 DLIST_ADD(web->input.headers, hdr);
131         }
132
133         /* ignore all other headers for now */
134         return NT_STATUS_OK;
135 }
136
137 /*
138   called when a web connection becomes readable
139 */
140 static void websrv_recv(struct stream_connection *conn, uint16_t flags)
141 {
142         struct web_server_data *wdata;
143         struct websrv_context *web = talloc_get_type(conn->private_data,
144                                                      struct websrv_context);
145         NTSTATUS status;
146         uint8_t buf[1024];
147         size_t nread;
148         uint8_t *p;
149         DATA_BLOB b;
150
151         /* not the most efficient http parser ever, but good enough for us */
152         status = socket_recv(conn->socket, buf, sizeof(buf), &nread);
153         if (NT_STATUS_IS_ERR(status)) goto failed;
154         if (!NT_STATUS_IS_OK(status)) return;
155
156         if (!data_blob_append(web, &web->input.partial, buf, nread))
157                 goto failed;
158
159         /* parse any lines that are available */
160         b = web->input.partial;
161         while (!web->input.end_of_headers &&
162                (p=(uint8_t *)memchr(b.data, '\n', b.length))) {
163                 const char *line = (const char *)b.data;
164                 *p = 0;
165                 if (p != b.data && p[-1] == '\r') {
166                         p[-1] = 0;
167                 }
168                 status = http_parse_header(web, line);
169                 if (!NT_STATUS_IS_OK(status)) return;
170                 b.length -= (p - b.data) + 1;
171                 b.data = p+1;
172         }
173
174         /* keep any remaining bytes in web->input.partial */
175         if (b.length == 0) {
176                 b.data = NULL;
177         }
178         b = data_blob_talloc(web, b.data, b.length);
179         data_blob_free(&web->input.partial);
180         web->input.partial = b;
181
182         /* we finish when we have both the full headers (terminated by
183            a blank line) and any post data, as indicated by the
184            content_length */
185         if (web->input.end_of_headers &&
186             web->input.partial.length >= web->input.content_length) {
187                 if (web->input.partial.length > web->input.content_length) {
188                         web->input.partial.data[web->input.content_length] = 0;
189                 }
190                 EVENT_FD_NOT_READABLE(web->conn->event.fde);
191
192                 /* the reference/unlink code here is quite subtle. It
193                  is needed because the rendering of the web-pages, and
194                  in particular the esp/ejs backend, is semi-async.  So
195                  we could well end up in the connection timeout code
196                  while inside http_process_input(), but we must not
197                  destroy the stack variables being used by that
198                  rendering process when we handle the timeout. */
199                 if (!talloc_reference(web->task, web)) goto failed;
200                 wdata = talloc_get_type(web->task->private_data, struct web_server_data);
201                 if (wdata == NULL) goto failed;
202                 wdata->http_process_input(wdata, web);
203                 talloc_unlink(web->task, web);
204         }
205         return;
206
207 failed:
208         stream_terminate_connection(conn, "websrv_recv: failed");
209 }
210
211
212
213 /*
214   called when a web connection becomes writable
215 */
216 static void websrv_send(struct stream_connection *conn, uint16_t flags)
217 {
218         struct websrv_context *web = talloc_get_type(conn->private_data,
219                                                      struct websrv_context);
220         NTSTATUS status;
221         size_t nsent;
222         DATA_BLOB b;
223
224         b = web->output.content;
225         b.data += web->output.nsent;
226         b.length -= web->output.nsent;
227
228         status = socket_send(conn->socket, &b, &nsent);
229         if (NT_STATUS_IS_ERR(status)) {
230                 stream_terminate_connection(web->conn, "socket_send: failed");
231                 return;
232         }
233         if (!NT_STATUS_IS_OK(status)) {
234                 return;
235         }
236
237         web->output.nsent += nsent;
238
239         if (web->output.content.length == web->output.nsent) {
240                 stream_terminate_connection(web->conn, "websrv_send: finished sending");
241         }
242 }
243
244 /*
245   establish a new connection to the web server
246 */
247 static void websrv_accept(struct stream_connection *conn)
248 {
249         struct task_server *task = talloc_get_type(conn->private_data, struct task_server);
250         struct web_server_data *wdata = talloc_get_type(task->private_data, struct web_server_data);
251         struct websrv_context *web;
252         struct socket_context *tls_socket;
253
254         web = talloc_zero(conn, struct websrv_context);
255         if (web == NULL) goto failed;
256
257         web->task = task;
258         web->conn = conn;
259         conn->private_data = web;
260         talloc_set_destructor(web, websrv_destructor);
261
262         event_add_timed(conn->event.ctx, web, 
263                         timeval_current_ofs(HTTP_TIMEOUT, 0),
264                         websrv_timeout, web);
265
266         /* Overwrite the socket with a (possibly) TLS socket */
267         tls_socket = tls_init_server(wdata->tls_params, conn->socket, 
268                                      conn->event.fde, "GPHO");
269         /* We might not have TLS, or it might not have initilised */
270         if (tls_socket) {
271                 talloc_unlink(conn, conn->socket);
272                 talloc_steal(conn, tls_socket);
273                 conn->socket = tls_socket;
274         } else {
275                 DEBUG(3, ("TLS not available for web_server connections\n"));
276         }
277
278         return;
279
280 failed:
281         talloc_free(conn);
282 }
283
284
285 static const struct stream_server_ops web_stream_ops = {
286         .name                   = "web",
287         .accept_connection      = websrv_accept,
288         .recv_handler           = websrv_recv,
289         .send_handler           = websrv_send,
290 };
291
292 /*
293   startup the web server task
294 */
295 static void websrv_task_init(struct task_server *task)
296 {
297         NTSTATUS status;
298         uint16_t port = lpcfg_web_port(task->lp_ctx);
299         const struct model_ops *model_ops;
300         struct web_server_data *wdata;
301
302         task_server_set_title(task, "task[websrv]");
303
304         /* run the web server as a single process */
305         model_ops = process_model_startup("single");
306         if (!model_ops) goto failed;
307
308         if (lpcfg_interfaces(task->lp_ctx) && lpcfg_bind_interfaces_only(task->lp_ctx)) {
309                 int num_interfaces;
310                 int i;
311                 struct interface *ifaces;
312
313                 load_interfaces(NULL, lpcfg_interfaces(task->lp_ctx), &ifaces);
314
315                 num_interfaces = iface_count(ifaces);
316                 for(i = 0; i < num_interfaces; i++) {
317                         const char *address = iface_n_ip(ifaces, i);
318                         status = stream_setup_socket(task,
319                                                      task->event_ctx,
320                                                      task->lp_ctx, model_ops,
321                                                      &web_stream_ops, 
322                                                      "ipv4", address, 
323                                                      &port, lpcfg_socket_options(task->lp_ctx),
324                                                      task);
325                         if (!NT_STATUS_IS_OK(status)) goto failed;
326                 }
327
328                 talloc_free(ifaces);
329         } else {
330                 status = stream_setup_socket(task, task->event_ctx, task->lp_ctx,
331                                              model_ops, &web_stream_ops, 
332                                              "ipv4", lpcfg_socket_address(task->lp_ctx),
333                                              &port, lpcfg_socket_options(task->lp_ctx), task);
334                 if (!NT_STATUS_IS_OK(status)) goto failed;
335         }
336
337         /* startup the esp processor - unfortunately we can't do this
338            per connection as that wouldn't allow for session variables */
339         wdata = talloc_zero(task, struct web_server_data);
340         if (wdata == NULL)goto failed;
341
342         task->private_data = wdata;
343         
344         wdata->tls_params = tls_initialise(wdata, task->lp_ctx);
345         if (wdata->tls_params == NULL) goto failed;
346
347         if (!wsgi_initialize(wdata)) goto failed;
348
349         return;
350
351 failed:
352         task_server_terminate(task, "websrv_task_init: failed to startup web server task", true);
353 }
354
355
356 /* called at smbd startup - register ourselves as a server service */
357 NTSTATUS server_service_web_init(void)
358 {
359         return register_server_service("web", websrv_task_init);
360 }