stream_terminate_connection: Prevent use-after-free
[nivanova/samba-autobuild/.git] / source4 / smbd / service_stream.c
index 6f78643daed50d641b3ff0b6e5b9df714fc97832..917a1876e07970ed1c66fd010f9e8918173cc835 100644 (file)
@@ -8,7 +8,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 <tevent.h>
 #include "process_model.h"
-#include "lib/events/events.h"
-#include "lib/socket/socket.h"
-#include "smbd/service_stream.h"
+#include "lib/util/server_id.h"
 #include "lib/messaging/irpc.h"
-
-/* the range of ports to try for dcerpc over tcp endpoints */
-#define SERVER_TCP_LOW_PORT  1024
-#define SERVER_TCP_HIGH_PORT 1300
+#include "cluster/cluster.h"
+#include "param/param.h"
+#include "../lib/tsocket/tsocket.h"
+#include "lib/util/util_net.h"
 
 /* size of listen() backlog in smbd */
 #define SERVER_LISTEN_BACKLOG 10
 */
 struct stream_socket {
        const struct stream_server_ops *ops;
-       struct event_context *event_ctx;
+       struct loadparm_context *lp_ctx;
+       struct tevent_context *event_ctx;
        const struct model_ops *model_ops;
        struct socket_context *sock;
-       void *private;
+       void *private_data;
 };
 
 
@@ -53,41 +52,91 @@ struct stream_socket {
 */
 void stream_terminate_connection(struct stream_connection *srv_conn, const char *reason)
 {
-       struct event_context *event_ctx = srv_conn->event.ctx;
+       struct tevent_context *event_ctx = srv_conn->event.ctx;
        const struct model_ops *model_ops = srv_conn->model_ops;
+       struct loadparm_context *lp_ctx = srv_conn->lp_ctx;
+       TALLOC_CTX *frame = NULL;
+
+       if (!reason) reason = "unknown reason";
+
+       if (srv_conn->processing) {
+               DEBUG(3,("Terminating connection deferred - '%s'\n", reason));
+       } else {
+               DEBUG(3,("Terminating connection - '%s'\n", reason));
+       }
+
+       srv_conn->terminate = reason;
+
+       if (srv_conn->processing) {
+               /* 
+                * if we're currently inside the stream_io_handler(),
+                * defer the termination to the end of stream_io_hendler()
+                *
+                * and we don't want to read or write to the connection...
+                */
+               tevent_fd_set_flags(srv_conn->event.fde, 0);
+               return;
+       }
+
+       frame = talloc_stackframe();
+
+       reason = talloc_strdup(frame, reason);
+       if (reason == NULL) {
+               reason = "OOM - unknown reason";
+       }
+
        talloc_free(srv_conn->event.fde);
-       talloc_free(srv_conn);
-       model_ops->terminate(event_ctx, reason);
+       srv_conn->event.fde = NULL;
+       imessaging_cleanup(srv_conn->msg_ctx);
+       TALLOC_FREE(srv_conn);
+       model_ops->terminate(event_ctx, lp_ctx, reason);
+
+       TALLOC_FREE(frame);
 }
 
-/*
+/**
   the select loop has indicated that a stream is ready for IO
 */
-static void stream_io_handler(struct event_context *ev, struct fd_event *fde, 
-                             uint16_t flags, void *private)
+static void stream_io_handler(struct stream_connection *conn, uint16_t flags)
 {
-       struct stream_connection *conn = talloc_get_type(private, 
-                                                        struct stream_connection);
-       if (flags & EVENT_FD_WRITE) {
+       conn->processing++;
+       if (flags & TEVENT_FD_WRITE) {
                conn->ops->send_handler(conn, flags);
-               return;
+       } else if (flags & TEVENT_FD_READ) {
+               conn->ops->recv_handler(conn, flags);
        }
+       conn->processing--;
 
-       if (flags & EVENT_FD_READ) {
-               conn->ops->recv_handler(conn, flags);
+       if (conn->terminate) {
+               stream_terminate_connection(conn, conn->terminate);
        }
 }
 
+void stream_io_handler_fde(struct tevent_context *ev, struct tevent_fd *fde,
+                                 uint16_t flags, void *private_data)
+{
+       struct stream_connection *conn = talloc_get_type(private_data,
+                                                        struct stream_connection);
+       stream_io_handler(conn, flags);
+}
+
+void stream_io_handler_callback(void *private_data, uint16_t flags)
+{
+       struct stream_connection *conn = talloc_get_type(private_data,
+                                                        struct stream_connection);
+       stream_io_handler(conn, flags);
+}
+
 /*
   this creates a stream_connection from an already existing connection,
   used for protocols, where a client connection needs to switched into
   a server connection
 */
-NTSTATUS stream_new_connection_merge(struct event_context *ev,
+NTSTATUS stream_new_connection_merge(struct tevent_context *ev,
+                                    struct loadparm_context *lp_ctx,
                                     const struct model_ops *model_ops,
-                                    struct socket_context *sock,
                                     const struct stream_server_ops *stream_ops,
-                                    struct messaging_context *msg_ctx,
+                                    struct imessaging_context *msg_ctx,
                                     void *private_data,
                                     struct stream_connection **_srv_conn)
 {
@@ -96,18 +145,16 @@ NTSTATUS stream_new_connection_merge(struct event_context *ev,
        srv_conn = talloc_zero(ev, struct stream_connection);
        NT_STATUS_HAVE_NO_MEMORY(srv_conn);
 
-       talloc_steal(srv_conn, sock);
-
-       srv_conn->private       = private_data;
+       srv_conn->private_data  = private_data;
        srv_conn->model_ops     = model_ops;
-       srv_conn->socket        = sock;
-       srv_conn->server_id     = 0;
+       srv_conn->socket        = NULL;
+       srv_conn->server_id     = cluster_id(0, 0);
        srv_conn->ops           = stream_ops;
        srv_conn->msg_ctx       = msg_ctx;
        srv_conn->event.ctx     = ev;
-       srv_conn->event.fde     = event_add_fd(ev, srv_conn, socket_get_fd(sock),
-                                              EVENT_FD_READ, 
-                                              stream_io_handler, srv_conn);
+       srv_conn->lp_ctx        = lp_ctx;
+       srv_conn->event.fde     = NULL;
+
        *_srv_conn = srv_conn;
        return NT_STATUS_OK;
 }
@@ -116,11 +163,12 @@ NTSTATUS stream_new_connection_merge(struct event_context *ev,
   called when a new socket connection has been established. This is called in the process
   context of the new process (if appropriate)
 */
-static void stream_new_connection(struct event_context *ev,
+static void stream_new_connection(struct tevent_context *ev,
+                                 struct loadparm_context *lp_ctx,
                                  struct socket_context *sock, 
-                                 uint32_t server_id, void *private)
+                                 struct server_id server_id, void *private_data)
 {
-       struct stream_socket *stream_socket = talloc_get_type(private, struct stream_socket);
+       struct stream_socket *stream_socket = talloc_get_type(private_data, struct stream_socket);
        struct stream_connection *srv_conn;
 
        srv_conn = talloc_zero(ev, struct stream_connection);
@@ -131,28 +179,68 @@ static void stream_new_connection(struct event_context *ev,
 
        talloc_steal(srv_conn, sock);
 
-       srv_conn->private       = stream_socket->private;
+       srv_conn->private_data  = stream_socket->private_data;
        srv_conn->model_ops     = stream_socket->model_ops;
        srv_conn->socket        = sock;
        srv_conn->server_id     = server_id;
        srv_conn->ops           = stream_socket->ops;
        srv_conn->event.ctx     = ev;
-       srv_conn->event.fde     = event_add_fd(ev, srv_conn, socket_get_fd(sock),
-                                              EVENT_FD_READ, 
-                                              stream_io_handler, srv_conn);
+       srv_conn->lp_ctx        = lp_ctx;
 
-       if (!socket_check_access(sock, "smbd", lp_hostsallow(-1), lp_hostsdeny(-1))) {
+       if (!socket_check_access(sock, "smbd", lpcfg_hosts_allow(NULL, lpcfg_default_service(lp_ctx)), lpcfg_hosts_deny(NULL, lpcfg_default_service(lp_ctx)))) {
                stream_terminate_connection(srv_conn, "denied by access rules");
                return;
        }
 
+       srv_conn->event.fde     = tevent_add_fd(ev, srv_conn, socket_get_fd(sock),
+                                               0, stream_io_handler_fde, srv_conn);
+       if (!srv_conn->event.fde) {
+               stream_terminate_connection(srv_conn, "tevent_add_fd() failed");
+               return;
+       }
+
        /* setup to receive internal messages on this connection */
-       srv_conn->msg_ctx = messaging_init(srv_conn, srv_conn->server_id, ev);
+       srv_conn->msg_ctx = imessaging_init(srv_conn,
+                                           lp_ctx,
+                                           srv_conn->server_id, ev);
        if (!srv_conn->msg_ctx) {
-               stream_terminate_connection(srv_conn, "messaging_init() failed");
+               stream_terminate_connection(srv_conn, "imessaging_init() failed");
                return;
        }
 
+       srv_conn->remote_address = socket_get_remote_addr(srv_conn->socket, srv_conn);
+       if (!srv_conn->remote_address) {
+               stream_terminate_connection(srv_conn, "socket_get_remote_addr() failed");
+               return;
+       }
+
+       srv_conn->local_address = socket_get_local_addr(srv_conn->socket, srv_conn);
+       if (!srv_conn->local_address) {
+               stream_terminate_connection(srv_conn, "socket_get_local_addr() failed");
+               return;
+       }
+
+       {
+               TALLOC_CTX *tmp_ctx;
+               const char *title;
+               struct server_id_buf idbuf;
+
+               tmp_ctx = talloc_new(srv_conn);
+
+               title = talloc_asprintf(tmp_ctx, "conn[%s] c[%s] s[%s] server_id[%s]",
+                                       stream_socket->ops->name, 
+                                       tsocket_address_string(srv_conn->remote_address, tmp_ctx),
+                                       tsocket_address_string(srv_conn->local_address, tmp_ctx),
+                                       server_id_str_buf(server_id, &idbuf));
+               if (title) {
+                       stream_connection_set_title(srv_conn, title);
+               }
+               talloc_free(tmp_ctx);
+       }
+
+       /* we're now ready to start receiving events on this stream */
+       TEVENT_FD_READABLE(srv_conn->event.fde);
+
        /* call the server specific accept code */
        stream_socket->ops->accept_connection(srv_conn);
 }
@@ -161,56 +249,101 @@ static void stream_new_connection(struct event_context *ev,
 /*
   called when someone opens a connection to one of our listening ports
 */
-static void stream_accept_handler(struct event_context *ev, struct fd_event *fde, 
-                                 uint16_t flags, void *private)
+static void stream_accept_handler(struct tevent_context *ev, struct tevent_fd *fde, 
+                                 uint16_t flags, void *private_data)
 {
-       struct stream_socket *stream_socket = talloc_get_type(private, struct stream_socket);
+       struct stream_socket *stream_socket = talloc_get_type(private_data, struct stream_socket);
 
        /* ask the process model to create us a process for this new
           connection.  When done, it calls stream_new_connection()
           with the newly created socket */
-       stream_socket->model_ops->accept_connection(ev, stream_socket->sock, 
+       stream_socket->model_ops->accept_connection(ev, stream_socket->lp_ctx,
+                                                   stream_socket->sock, 
                                                    stream_new_connection, stream_socket);
 }
 
-
-
 /*
   setup a listen stream socket
   if you pass *port == 0, then a port > 1024 is used
+
+  FIXME: This function is TCP/IP specific - uses an int rather than 
+        a string for the port. Should leave allocating a port nr 
+         to the socket implementation - JRV20070903
  */
-NTSTATUS stream_setup_socket(struct event_context *event_context,
+NTSTATUS stream_setup_socket(TALLOC_CTX *mem_ctx,
+                            struct tevent_context *event_context,
+                            struct loadparm_context *lp_ctx,
                             const struct model_ops *model_ops,
                             const struct stream_server_ops *stream_ops,
                             const char *family,
                             const char *sock_addr,
                             uint16_t *port,
-                            void *private)
+                            const char *socket_options,
+                            void *private_data)
 {
        NTSTATUS status;
        struct stream_socket *stream_socket;
+       struct socket_address *socket_address;
+       struct tevent_fd *fde;
        int i;
+       struct sockaddr_storage ss;
 
-       stream_socket = talloc_zero(event_context, struct stream_socket);
+       stream_socket = talloc_zero(mem_ctx, struct stream_socket);
        NT_STATUS_HAVE_NO_MEMORY(stream_socket);
 
-       status = socket_create(family, SOCKET_TYPE_STREAM, &stream_socket->sock, 0);
-       NT_STATUS_NOT_OK_RETURN(status);
+       if (strcmp(family, "ip") == 0) {
+               /* we will get the real family from the address itself */
+               if (!interpret_string_addr(&ss, sock_addr, 0)) {
+                       talloc_free(stream_socket);
+                       return NT_STATUS_INVALID_ADDRESS;
+               }
+
+               socket_address = socket_address_from_sockaddr_storage(stream_socket, &ss, port?*port:0);
+               if (socket_address == NULL) {
+                       TALLOC_FREE(stream_socket);
+                       return NT_STATUS_NO_MEMORY;
+               }
+
+               status = socket_create(socket_address->family, SOCKET_TYPE_STREAM, &stream_socket->sock, 0);
+               NT_STATUS_NOT_OK_RETURN(status);
+       } else {
+               status = socket_create(family, SOCKET_TYPE_STREAM, &stream_socket->sock, 0);
+               NT_STATUS_NOT_OK_RETURN(status);
+
+               /* this is for non-IP sockets, eg. unix domain sockets */
+               socket_address = socket_address_from_strings(stream_socket,
+                                                            stream_socket->sock->backend_name,
+                                                            sock_addr, port?*port:0);
+               NT_STATUS_HAVE_NO_MEMORY(socket_address);
+       }
+
 
        talloc_steal(stream_socket, stream_socket->sock);
 
+       stream_socket->lp_ctx = talloc_reference(stream_socket, lp_ctx);
+
        /* ready to listen */
        status = socket_set_option(stream_socket->sock, "SO_KEEPALIVE", NULL);
        NT_STATUS_NOT_OK_RETURN(status);
 
-       status = socket_set_option(stream_socket->sock, lp_socket_options(), NULL);
-       NT_STATUS_NOT_OK_RETURN(status);
-
-       /* TODO: set socket ACL's here when they're implemented */
+       if (socket_options != NULL) {
+               status = socket_set_option(stream_socket->sock, socket_options, NULL);
+               NT_STATUS_NOT_OK_RETURN(status);
+       }
 
-       if (*port == 0) {
-               for (i=SERVER_TCP_LOW_PORT;i<= SERVER_TCP_HIGH_PORT;i++) {
-                       status = socket_listen(stream_socket->sock, sock_addr, i, 
+       /* TODO: set socket ACL's (host allow etc) here when they're
+        * implemented */
+
+       /* Some sockets don't have a port, or are just described from
+        * the string.  We are indicating this by having port == NULL */
+       if (!port) {
+               status = socket_listen(stream_socket->sock, socket_address, SERVER_LISTEN_BACKLOG, 0);
+       } else if (*port == 0) {
+               for (i = lpcfg_rpc_low_port(lp_ctx);
+                    i <= lpcfg_rpc_high_port(lp_ctx);
+                    i++) {
+                       socket_address->port = i;
+                       status = socket_listen(stream_socket->sock, socket_address, 
                                               SERVER_LISTEN_BACKLOG, 0);
                        if (NT_STATUS_IS_OK(status)) {
                                *port = i;
@@ -218,24 +351,49 @@ NTSTATUS stream_setup_socket(struct event_context *event_context,
                        }
                }
        } else {
-               status = socket_listen(stream_socket->sock, sock_addr, *port, SERVER_LISTEN_BACKLOG, 0);
+               status = socket_listen(stream_socket->sock, socket_address, SERVER_LISTEN_BACKLOG, 0);
        }
 
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(0,("Failed to listen on %s:%u - %s\n",
-                       sock_addr, *port, nt_errstr(status)));
+                        sock_addr, port ? (unsigned int)(*port) : 0,
+                        nt_errstr(status)));
                talloc_free(stream_socket);
                return status;
        }
 
-       event_add_fd(event_context, stream_socket->sock, 
-                    socket_get_fd(stream_socket->sock), 
-                    EVENT_FD_READ, stream_accept_handler, stream_socket);
+       /* Add the FD from the newly created socket into the event
+        * subsystem.  it will call the accept handler whenever we get
+        * new connections */
 
-       stream_socket->private          = talloc_reference(stream_socket, private);
+       fde = tevent_add_fd(event_context, stream_socket->sock,
+                           socket_get_fd(stream_socket->sock),
+                           TEVENT_FD_READ,
+                           stream_accept_handler, stream_socket);
+       if (!fde) {
+               DEBUG(0,("Failed to setup fd event\n"));
+               talloc_free(stream_socket);
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       /* we let events system to the close on the socket. This avoids
+        * nasty interactions with waiting for talloc to close the socket. */
+       tevent_fd_set_close_fn(fde, socket_tevent_fd_close_fn);
+       socket_set_flags(stream_socket->sock, SOCKET_FLAG_NOCLOSE);
+
+       stream_socket->private_data     = talloc_reference(stream_socket, private_data);
        stream_socket->ops              = stream_ops;
        stream_socket->event_ctx        = event_context;
        stream_socket->model_ops        = model_ops;
 
        return NT_STATUS_OK;
 }
+
+
+/*
+  setup a connection title 
+*/
+void stream_connection_set_title(struct stream_connection *conn, const char *title)
+{
+       conn->model_ops->set_title(conn->event.ctx, title);
+}