Rework service init functions to pass down service name. This is
[ira/wip.git] / source / web_server / web_server.c
index 9bbf423d29fcf802e72d5f5a7def6b14aa6e64b1..bd212cff7d28df7fcaa37c7703a2f0b60fad60c4 100644 (file)
@@ -7,7 +7,7 @@
    
    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,
    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 "smbd/service_task.h"
 #include "smbd/service_stream.h"
+#include "smbd/service.h"
 #include "web_server/web_server.h"
 #include "lib/events/events.h"
 #include "system/filesys.h"
+#include "system/network.h"
+#include "lib/socket/netif.h"
+#include "lib/tls/tls.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);
        }
@@ -51,7 +54,11 @@ static void websrv_timeout(struct event_context *event_context,
                           struct timeval t, void *private)
 {
        struct websrv_context *web = talloc_get_type(private, struct websrv_context);
-       stream_terminate_connection(web->conn, "websrv_context: timeout");
+       struct stream_connection *conn = web->conn;
+       web->conn = NULL;
+       /* TODO: send a message to any running esp context on this connection
+          to stop running */
+       stream_terminate_connection(conn, "websrv_timeout: timed out"); 
 }
 
 /*
@@ -68,18 +75,18 @@ static void websrv_recv(struct stream_connection *conn, uint16_t flags)
        DATA_BLOB b;
 
        /* not the most efficient http parser ever, but good enough for us */
-       status = socket_recv(conn->socket, buf, sizeof(buf), &nread, 0);
+       status = socket_recv(conn->socket, buf, sizeof(buf), &nread);
        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))) {
-               const char *line = b.data;
+              (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') {
                        p[-1] = 0;
@@ -102,14 +109,27 @@ static void websrv_recv(struct stream_connection *conn, uint16_t flags)
           a blank line) and any post data, as indicated by the
           content_length */
        if (web->input.end_of_headers &&
-           web->input.partial.length == web->input.content_length) {
+           web->input.partial.length >= web->input.content_length) {
+               if (web->input.partial.length > web->input.content_length) {
+                       web->input.partial.data[web->input.content_length] = 0;
+               }
                EVENT_FD_NOT_READABLE(web->conn->event.fde);
+
+               /* the reference/unlink code here is quite subtle. It
+                is needed because the rendering of the web-pages, and
+                in particular the esp/ejs backend, is semi-async.  So
+                we could well end up in the connection timeout code
+                while inside http_process_input(), but we must not
+                destroy the stack variables being used by that
+                rendering process when we handle the timeout. */
+               if (!talloc_reference(web->task, web)) goto failed;
                http_process_input(web);
+               talloc_unlink(web->task, web);
        }
        return;
 
 failed:
-       stream_terminate_connection(conn, "websrv_recv: failed\n");
+       stream_terminate_connection(conn, "websrv_recv: failed");
 }
 
 
@@ -128,7 +148,7 @@ static void websrv_send(struct stream_connection *conn, uint16_t flags)
        b.data += web->output.nsent;
        b.length -= web->output.nsent;
 
-       status = socket_send(conn->socket, &b, &nsent, 0);
+       status = socket_send(conn->socket, &b, &nsent);
        if (NT_STATUS_IS_ERR(status)) {
                stream_terminate_connection(web->conn, "socket_send: failed");
                return;
@@ -149,18 +169,20 @@ static void websrv_send(struct stream_connection *conn, uint16_t flags)
                web->output.nsent = 0;
 
                nread = read(web->output.fd, buf, sizeof(buf));
-               if (nread == 0) {
-                       close(web->output.fd);
-                       web->output.fd = -1;
-               }
                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) {
-               stream_terminate_connection(web->conn, NULL);
+       if (web->output.content.length == web->output.nsent && 
+           web->output.fd == -1) {
+               stream_terminate_connection(web->conn, "websrv_send: finished sending");
        }
 }
 
@@ -170,7 +192,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 websrv_context *web;
+       struct socket_context *tls_socket;
 
        web = talloc_zero(conn, struct websrv_context);
        if (web == NULL) goto failed;
@@ -184,6 +208,19 @@ static void websrv_accept(struct stream_connection *conn)
        event_add_timed(conn->event.ctx, web, 
                        timeval_current_ofs(HTTP_TIMEOUT, 0),
                        websrv_timeout, web);
+
+       /* Overwrite the socket with a (possibly) TLS socket */
+       tls_socket = tls_init_server(edata->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;
 
 failed:
@@ -204,29 +241,40 @@ 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;
 
+       task_server_set_title(task, "task[websrv]");
+
        /* run the web server as a single process */
        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;
        }
 
@@ -238,7 +286,7 @@ static void websrv_task_init(struct task_server *task)
        return;
 
 failed:
-       task_terminate(task, "Failed to startup web server task");
+       task_server_terminate(task, "websrv_task_init: failed to startup web server task");
 }
 
 
@@ -247,9 +295,11 @@ failed:
   listening on all configured sockets
 */
 static NTSTATUS websrv_init(struct event_context *event_context, 
+                           struct loadparm_context *lp_ctx,
                            const struct model_ops *model_ops)
 {      
-       return task_server_startup(event_context, model_ops, websrv_task_init);
+       return task_server_startup(event_context, lp_ctx, "web", 
+                                  model_ops, websrv_task_init);
 }
 
 /* called at smbd startup - register ourselves as a server service */