Allow SWAT to be run outside of smbd.
[amitay/samba.git] / source4 / web_server / web_server.c
index 93d59cc47a19dff9de830fd427f1d1e1fa51c699..1ed37f657b2a37aea530d730ccea2f88a6516e27 100644 (file)
@@ -4,10 +4,11 @@
    web server startup
 
    Copyright (C) Andrew Tridgell 2005
+   Copyright (C) Jelmer Vernooij 2008
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
@@ -16,8 +17,7 @@
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include "includes.h"
 #include "lib/events/events.h"
 #include "system/filesys.h"
 #include "system/network.h"
-#include "netif/netif.h"
+#include "lib/socket/netif.h"
 #include "lib/tls/tls.h"
+#include "lib/util/dlinklist.h"
+#include "param/param.h"
 
 /* don't allow connections to hang around forever */
-#define HTTP_TIMEOUT 30
+#define HTTP_TIMEOUT 120
 
 /*
   destroy a web connection
 */
-static int websrv_destructor(void *ptr)
+static int websrv_destructor(struct websrv_context *web)
 {
-       struct websrv_context *web = talloc_get_type(ptr, struct websrv_context);
-       if (web->output.fd != -1) {
-               close(web->output.fd);
-       }
        return 0;
 }
 
@@ -62,11 +60,91 @@ static void websrv_timeout(struct event_context *event_context,
        stream_terminate_connection(conn, "websrv_timeout: timed out"); 
 }
 
+/*
+  setup for a raw http level error
+*/
+void http_error(struct websrv_context *web, const char *status, const char *info)
+{
+       char *s;
+       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", 
+                           status, status, info);
+       if (s == NULL) {
+               stream_terminate_connection(web->conn, "http_error: out of memory");
+               return;
+       }
+       websrv_output_headers(web, status, NULL);
+       websrv_output(web, s, strlen(s));
+}
+
+void websrv_output_headers(struct websrv_context *web, const char *status, struct http_header *headers)
+{
+       char *s;
+       DATA_BLOB b;
+       struct http_header *hdr;
+
+       s = talloc_asprintf(web, "HTTP/1.0 %s\r\n", status);
+       if (s == NULL) return;
+       for (hdr = headers; hdr; hdr = hdr->next) {
+               s = talloc_asprintf_append_buffer(s, "%s: %s\r\n", hdr->name, hdr->value);
+       }
+
+       s = talloc_asprintf_append_buffer(s, "\r\n");
+
+       b = web->output.content;
+       web->output.content = data_blob_string_const(s);
+       websrv_output(web, b.data, b.length);
+       data_blob_free(&b);
+}
+
+void websrv_output(struct websrv_context *web, void *data, size_t length)
+{
+       data_blob_append(web, &web->output.content, data, length);
+       EVENT_FD_NOT_READABLE(web->conn->event.fde);
+       EVENT_FD_WRITEABLE(web->conn->event.fde);
+       web->output.output_pending = true;
+}
+
+
+/*
+  parse one line of header input
+*/
+NTSTATUS http_parse_header(struct websrv_context *web, const char *line)
+{
+       if (line[0] == 0) {
+               web->input.end_of_headers = true;
+       } else if (strncasecmp(line,"GET ", 4)==0) {
+               web->input.url = talloc_strndup(web, &line[4], strcspn(&line[4], " \t"));
+       } else if (strncasecmp(line,"POST ", 5)==0) {
+               web->input.post_request = true;
+               web->input.url = talloc_strndup(web, &line[5], strcspn(&line[5], " \t"));
+       } else if (strchr(line, ':') == NULL) {
+               http_error(web, "400 Bad request", "This server only accepts GET and POST requests");
+               return NT_STATUS_INVALID_PARAMETER;
+       } else if (strncasecmp(line, "Content-Length: ", 16)==0) {
+               web->input.content_length = strtoul(&line[16], NULL, 10);
+       } else {
+               struct http_header *hdr = talloc_zero(web, struct http_header);
+               char *colon = strchr(line, ':');
+               if (colon == NULL) {
+                       http_error(web, "500 Internal Server Error", "invalidly formatted header");
+                       return NT_STATUS_INVALID_PARAMETER;
+               }
+
+               hdr->name = talloc_strndup(hdr, line, colon-line);
+               hdr->value = talloc_strdup(hdr, colon+1);
+               DLIST_ADD(web->input.headers, hdr);
+       }
+
+       /* ignore all other headers for now */
+       return NT_STATUS_OK;
+}
+
 /*
   called when a web connection becomes readable
 */
 static void websrv_recv(struct stream_connection *conn, uint16_t flags)
 {
+       struct web_server_data *wdata;
        struct websrv_context *web = talloc_get_type(conn->private, 
                                                     struct websrv_context);
        NTSTATUS status;
@@ -80,13 +158,13 @@ static void websrv_recv(struct stream_connection *conn, uint16_t flags)
        if (NT_STATUS_IS_ERR(status)) goto failed;
        if (!NT_STATUS_IS_OK(status)) return;
 
-       status = data_blob_append(web, &web->input.partial, buf, nread);
-       if (!NT_STATUS_IS_OK(status)) goto failed;
+       if (!data_blob_append(web, &web->input.partial, buf, nread))
+               goto failed;
 
        /* parse any lines that are available */
        b = web->input.partial;
        while (!web->input.end_of_headers &&
-              (p=memchr(b.data, '\n', b.length))) {
+              (p=(uint8_t *)memchr(b.data, '\n', b.length))) {
                const char *line = (const char *)b.data;
                *p = 0;
                if (p != b.data && p[-1] == '\r') {
@@ -123,8 +201,10 @@ static void websrv_recv(struct stream_connection *conn, uint16_t flags)
                 while inside http_process_input(), but we must not
                 destroy the stack variables being used by that
                 rendering process when we handle the timeout. */
-               talloc_reference(web->task, web);
-               http_process_input(web);
+               if (!talloc_reference(web->task, web)) goto failed;
+               wdata = talloc_get_type(web->task->private, struct web_server_data);
+               if (wdata == NULL) goto failed;
+               wdata->http_process_input(wdata, web);
                talloc_unlink(web->task, web);
        }
        return;
@@ -134,6 +214,7 @@ failed:
 }
 
 
+
 /*
   called when a web connection becomes writable
 */
@@ -160,29 +241,7 @@ static void websrv_send(struct stream_connection *conn, uint16_t flags)
 
        web->output.nsent += nsent;
 
-       /* possibly read some more raw data from a file */
-       if (web->output.content.length == web->output.nsent && 
-           web->output.fd != -1) {
-               uint8_t buf[2048];
-               ssize_t nread;
-
-               data_blob_free(&web->output.content);
-               web->output.nsent = 0;
-
-               nread = read(web->output.fd, buf, sizeof(buf));
-               if (nread == -1 && errno == EINTR) {
-                       return;
-               }
-               if (nread <= 0) {
-                       close(web->output.fd);
-                       web->output.fd = -1;
-                       nread = 0;
-               }
-               web->output.content = data_blob_talloc(web, buf, nread);
-       }
-
-       if (web->output.content.length == web->output.nsent && 
-           web->output.fd == -1) {
+       if (web->output.content.length == web->output.nsent) {
                stream_terminate_connection(web->conn, "websrv_send: finished sending");
        }
 }
@@ -193,8 +252,9 @@ static void websrv_send(struct stream_connection *conn, uint16_t flags)
 static void websrv_accept(struct stream_connection *conn)
 {
        struct task_server *task = talloc_get_type(conn->private, struct task_server);
-       struct esp_data *edata = talloc_get_type(task->private, struct esp_data);
+       struct web_server_data *wdata = talloc_get_type(task->private, struct web_server_data);
        struct websrv_context *web;
+       struct socket_context *tls_socket;
 
        web = talloc_zero(conn, struct websrv_context);
        if (web == NULL) goto failed;
@@ -202,7 +262,6 @@ static void websrv_accept(struct stream_connection *conn)
        web->task = task;
        web->conn = conn;
        conn->private = web;
-       web->output.fd = -1;
        talloc_set_destructor(web, websrv_destructor);
 
        event_add_timed(conn->event.ctx, web, 
@@ -210,9 +269,16 @@ static void websrv_accept(struct stream_connection *conn)
                        websrv_timeout, web);
 
        /* Overwrite the socket with a (possibly) TLS socket */
-       conn->socket = tls_init_server(edata->tls_params, conn->socket, 
-                                      conn->event.fde, "GPHO");
-       if (conn->socket == NULL) goto failed;
+       tls_socket = tls_init_server(wdata->tls_params, conn->socket, 
+                                    conn->event.fde, "GPHO");
+       /* We might not have TLS, or it might not have initilised */
+       if (tls_socket) {
+               talloc_unlink(conn, conn->socket);
+               talloc_steal(conn, tls_socket);
+               conn->socket = tls_socket;
+       } else {
+               DEBUG(3, ("TLS not available for web_server connections\n"));
+       }
 
        return;
 
@@ -234,8 +300,9 @@ static const struct stream_server_ops web_stream_ops = {
 static void websrv_task_init(struct task_server *task)
 {
        NTSTATUS status;
-       uint16_t port = lp_web_port();
+       uint16_t port = lp_web_port(task->lp_ctx);
        const struct model_ops *model_ops;
+       struct web_server_data *wdata;
 
        task_server_set_title(task, "task[websrv]");
 
@@ -243,29 +310,45 @@ static void websrv_task_init(struct task_server *task)
        model_ops = process_model_byname("single");
        if (!model_ops) goto failed;
 
-       if (lp_interfaces() && lp_bind_interfaces_only()) {
-               int num_interfaces = iface_count();
+       if (lp_interfaces(task->lp_ctx) && lp_bind_interfaces_only(task->lp_ctx)) {
+               int num_interfaces;
                int i;
+               struct interface *ifaces;
+
+               load_interfaces(NULL, lp_interfaces(task->lp_ctx), &ifaces);
+
+               num_interfaces = iface_count(ifaces);
                for(i = 0; i < num_interfaces; i++) {
-                       const char *address = iface_n_ip(i);
-                       status = stream_setup_socket(task->event_ctx, model_ops, 
+                       const char *address = iface_n_ip(ifaces, i);
+                       status = stream_setup_socket(task->event_ctx, 
+                                                    task->lp_ctx, model_ops, 
                                                     &web_stream_ops, 
                                                     "ipv4", address, 
-                                                    &port, task);
+                                                    &port, lp_socket_options(task->lp_ctx), 
+                                                    task);
                        if (!NT_STATUS_IS_OK(status)) goto failed;
                }
+
+               talloc_free(ifaces);
        } else {
-               status = stream_setup_socket(task->event_ctx, model_ops, 
-                                            &web_stream_ops, 
-                                            "ipv4", lp_socket_address(), 
-                                            &port, task);
+               status = stream_setup_socket(task->event_ctx, task->lp_ctx,
+                                            model_ops, &web_stream_ops, 
+                                            "ipv4", lp_socket_address(task->lp_ctx), 
+                                            &port, lp_socket_options(task->lp_ctx), task);
                if (!NT_STATUS_IS_OK(status)) goto failed;
        }
 
        /* startup the esp processor - unfortunately we can't do this
           per connection as that wouldn't allow for session variables */
-       status = http_setup_esp(task);
-       if (!NT_STATUS_IS_OK(status)) goto failed;
+       wdata = talloc_zero(task, struct web_server_data);
+       if (wdata == NULL)goto failed;
+
+       task->private = wdata;
+       
+       wdata->tls_params = tls_initialise(wdata, task->lp_ctx);
+       if (wdata->tls_params == NULL) goto failed;
+
+       if (!wsgi_initialize(wdata)) goto failed;
 
        return;
 
@@ -274,18 +357,8 @@ failed:
 }
 
 
-/*
-  called on startup of the web server service It's job is to start
-  listening on all configured sockets
-*/
-static NTSTATUS websrv_init(struct event_context *event_context, 
-                           const struct model_ops *model_ops)
-{      
-       return task_server_startup(event_context, model_ops, websrv_task_init);
-}
-
 /* called at smbd startup - register ourselves as a server service */
 NTSTATUS server_service_web_init(void)
 {
-       return register_server_service("web", websrv_init);
+       return register_server_service("web", websrv_task_init);
 }