source4/smbd: Fix code formatting after refactoring.
[metze/samba/wip.git] / source4 / smbd / service_stream.c
index e19d0085d1d38778cef6ba238f650a2043a8c25e..63318c686b0534dcb973021e3df9fc996aa36d15 100644 (file)
 #include "includes.h"
 #include <tevent.h>
 #include "process_model.h"
+#include "lib/util/server_id.h"
 #include "lib/messaging/irpc.h"
 #include "cluster/cluster.h"
 #include "param/param.h"
 #include "../lib/tsocket/tsocket.h"
 #include "lib/util/util_net.h"
 
-/* the range of ports to try for dcerpc over tcp endpoints */
-#define SERVER_TCP_LOW_PORT  1024
-#define SERVER_TCP_HIGH_PORT 1300
-
 /* size of listen() backlog in smbd */
 #define SERVER_LISTEN_BACKLOG 10
 
@@ -47,6 +44,7 @@ struct stream_socket {
        const struct model_ops *model_ops;
        struct socket_context *sock;
        void *private_data;
+       void *process_context;
 };
 
 
@@ -57,6 +55,9 @@ void stream_terminate_connection(struct stream_connection *srv_conn, const char
 {
        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;
+       void *process_context = srv_conn->process_context;
+       TALLOC_CTX *frame = NULL;
 
        if (!reason) reason = "unknown reason";
 
@@ -79,11 +80,19 @@ void stream_terminate_connection(struct stream_connection *srv_conn, const char
                return;
        }
 
+       frame = talloc_stackframe();
+
+       reason = talloc_strdup(frame, reason);
+       if (reason == NULL) {
+               reason = "OOM - unknown reason";
+       }
+
        talloc_free(srv_conn->event.fde);
        srv_conn->event.fde = NULL;
        imessaging_cleanup(srv_conn->msg_ctx);
-       model_ops->terminate(event_ctx, srv_conn->lp_ctx, reason);
-       talloc_free(srv_conn);
+       TALLOC_FREE(srv_conn);
+       model_ops->terminate(event_ctx, lp_ctx, reason, process_context);
+       TALLOC_FREE(frame);
 }
 
 /**
@@ -130,22 +139,24 @@ NTSTATUS stream_new_connection_merge(struct tevent_context *ev,
                                     const struct stream_server_ops *stream_ops,
                                     struct imessaging_context *msg_ctx,
                                     void *private_data,
-                                    struct stream_connection **_srv_conn)
+                                    struct stream_connection **_srv_conn,
+                                    void *process_context)
 {
        struct stream_connection *srv_conn;
 
        srv_conn = talloc_zero(ev, struct stream_connection);
        NT_STATUS_HAVE_NO_MEMORY(srv_conn);
 
-       srv_conn->private_data  = private_data;
-       srv_conn->model_ops     = model_ops;
-       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->lp_ctx        = lp_ctx;
-       srv_conn->event.fde     = NULL;
+       srv_conn->private_data    = private_data;
+       srv_conn->model_ops       = model_ops;
+       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->lp_ctx          = lp_ctx;
+       srv_conn->event.fde       = NULL;
+       srv_conn->process_context = process_context;
 
        *_srv_conn = srv_conn;
        return NT_STATUS_OK;
@@ -158,7 +169,9 @@ NTSTATUS stream_new_connection_merge(struct tevent_context *ev,
 static void stream_new_connection(struct tevent_context *ev,
                                  struct loadparm_context *lp_ctx,
                                  struct socket_context *sock, 
-                                 struct server_id server_id, void *private_data)
+                                 struct server_id server_id,
+                                 void *private_data,
+                                 void *process_context)
 {
        struct stream_socket *stream_socket = talloc_get_type(private_data, struct stream_socket);
        struct stream_connection *srv_conn;
@@ -171,13 +184,14 @@ static void stream_new_connection(struct tevent_context *ev,
 
        talloc_steal(srv_conn, sock);
 
-       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->lp_ctx        = lp_ctx;
+       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->lp_ctx          = lp_ctx;
+       srv_conn->process_context = process_context;
 
        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");
@@ -194,7 +208,7 @@ static void stream_new_connection(struct tevent_context *ev,
        /* setup to receive internal messages on this connection */
        srv_conn->msg_ctx = imessaging_init(srv_conn,
                                            lp_ctx,
-                                           srv_conn->server_id, ev, false);
+                                           srv_conn->server_id, ev);
        if (!srv_conn->msg_ctx) {
                stream_terminate_connection(srv_conn, "imessaging_init() failed");
                return;
@@ -215,6 +229,7 @@ static void stream_new_connection(struct tevent_context *ev,
        {
                TALLOC_CTX *tmp_ctx;
                const char *title;
+               struct server_id_buf idbuf;
 
                tmp_ctx = talloc_new(srv_conn);
 
@@ -222,7 +237,7 @@ static void stream_new_connection(struct tevent_context *ev,
                                        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(tmp_ctx, &server_id));
+                                       server_id_str_buf(server_id, &idbuf));
                if (title) {
                        stream_connection_set_title(srv_conn, title);
                }
@@ -248,9 +263,13 @@ static void stream_accept_handler(struct tevent_context *ev, struct tevent_fd *f
        /* 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->lp_ctx,
-                                                   stream_socket->sock, 
-                                                   stream_new_connection, stream_socket);
+       stream_socket->model_ops->accept_connection(
+               ev,
+               stream_socket->lp_ctx,
+               stream_socket->sock,
+               stream_new_connection,
+               stream_socket,
+               stream_socket->process_context);
 }
 
 /*
@@ -270,7 +289,8 @@ NTSTATUS stream_setup_socket(TALLOC_CTX *mem_ctx,
                             const char *sock_addr,
                             uint16_t *port,
                             const char *socket_options,
-                            void *private_data)
+                            void *private_data,
+                            void *process_context)
 {
        NTSTATUS status;
        struct stream_socket *stream_socket;
@@ -290,7 +310,10 @@ NTSTATUS stream_setup_socket(TALLOC_CTX *mem_ctx,
                }
 
                socket_address = socket_address_from_sockaddr_storage(stream_socket, &ss, port?*port:0);
-               NT_STATUS_HAVE_NO_MEMORY_AND_FREE(socket_address, stream_socket);
+               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);
@@ -327,7 +350,9 @@ NTSTATUS stream_setup_socket(TALLOC_CTX *mem_ctx,
        if (!port) {
                status = socket_listen(stream_socket->sock, socket_address, SERVER_LISTEN_BACKLOG, 0);
        } else if (*port == 0) {
-               for (i=SERVER_TCP_LOW_PORT;i<= SERVER_TCP_HIGH_PORT;i++) {
+               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);
@@ -371,6 +396,7 @@ NTSTATUS stream_setup_socket(TALLOC_CTX *mem_ctx,
        stream_socket->ops              = stream_ops;
        stream_socket->event_ctx        = event_context;
        stream_socket->model_ops        = model_ops;
+       stream_socket->process_context  = process_context;
 
        return NT_STATUS_OK;
 }