r24780: More work allowing libutil to be used by external users.
[kai/samba.git] / source4 / web_server / web_server.c
index 57bb5c62b177891ea7118f984784fa0f75d3799e..f9031433d38a5542d09fb68bd2cc48c5af0ee7b1 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"
 
 /* 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);
        }
@@ -73,12 +74,12 @@ 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 = tls_socket_recv(web->tls, buf, sizeof(buf), &nread);
+       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;
@@ -120,7 +121,7 @@ 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);
+               if (!talloc_reference(web->task, web)) goto failed;
                http_process_input(web);
                talloc_unlink(web->task, web);
        }
@@ -146,7 +147,7 @@ static void websrv_send(struct stream_connection *conn, uint16_t flags)
        b.data += web->output.nsent;
        b.length -= web->output.nsent;
 
-       status = tls_socket_send(web->tls, &b, &nsent);
+       status = socket_send(conn->socket, &b, &nsent);
        if (NT_STATUS_IS_ERR(status)) {
                stream_terminate_connection(web->conn, "socket_send: failed");
                return;
@@ -180,8 +181,6 @@ static void websrv_send(struct stream_connection *conn, uint16_t flags)
 
        if (web->output.content.length == web->output.nsent && 
            web->output.fd == -1) {
-               talloc_free(web->tls);
-               web->tls = NULL;
                stream_terminate_connection(web->conn, "websrv_send: finished sending");
        }
 }
@@ -194,6 +193,7 @@ 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;
@@ -208,9 +208,17 @@ static void websrv_accept(struct stream_connection *conn)
                        timeval_current_ofs(HTTP_TIMEOUT, 0),
                        websrv_timeout, web);
 
-       web->tls = tls_init_server(edata->tls_params, conn->socket, 
-                                  conn->event.fde, "GPHO", True);
-       if (web->tls == NULL) goto failed;
+       /* 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;
 
@@ -235,6 +243,8 @@ static void websrv_task_init(struct task_server *task)
        uint16_t port = lp_web_port();
        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;