Add two flags to allow for handling of Extended Signatures (Session Key Protection...
[samba.git] / source3 / smbd / process.c
index c3304c7114d27a46d0e4650eecd3d635a7f669f4..dd26a27ec391eae7ecff4fa57bcfd81c7184242c 100644 (file)
@@ -39,8 +39,7 @@
 #include "../libcli/security/dom_sid.h"
 #include "../libcli/security/security_token.h"
 #include "lib/id_cache.h"
-
-extern bool global_machine_password_needs_changing;
+#include "serverid.h"
 
 /* Internal message queue for deferred opens. */
 struct pending_message_list {
@@ -76,12 +75,12 @@ static bool smbd_lock_socket_internal(struct smbd_server_connection *sconn)
                return true;
        }
 
-       DEBUG(10,("pid[%d] wait for socket lock\n", (int)sys_getpid()));
+       DEBUG(10,("pid[%d] wait for socket lock\n", (int)getpid()));
 
        do {
                ok = fcntl_lock(
                        sconn->smb1.echo_handler.socket_lock_fd,
-                       SMB_F_SETLKW, 0, 0, F_WRLCK);
+                       F_SETLKW, 0, 0, F_WRLCK);
        } while (!ok && (errno == EINTR));
 
        if (!ok) {
@@ -89,7 +88,7 @@ static bool smbd_lock_socket_internal(struct smbd_server_connection *sconn)
                return false;
        }
 
-       DEBUG(10,("pid[%d] got for socket lock\n", (int)sys_getpid()));
+       DEBUG(10,("pid[%d] got for socket lock\n", (int)getpid()));
 
        return true;
 }
@@ -118,7 +117,7 @@ static bool smbd_unlock_socket_internal(struct smbd_server_connection *sconn)
        do {
                ok = fcntl_lock(
                        sconn->smb1.echo_handler.socket_lock_fd,
-                       SMB_F_SETLKW, 0, 0, F_UNLCK);
+                       F_SETLKW, 0, 0, F_UNLCK);
        } while (!ok && (errno == EINTR));
 
        if (!ok) {
@@ -126,7 +125,7 @@ static bool smbd_unlock_socket_internal(struct smbd_server_connection *sconn)
                return false;
        }
 
-       DEBUG(10,("pid[%d] unlocked socket\n", (int)sys_getpid()));
+       DEBUG(10,("pid[%d] unlocked socket\n", (int)getpid()));
 
        return true;
 }
@@ -182,7 +181,7 @@ bool srv_send_smb(struct smbd_server_connection *sconn, char *buffer,
                 * client failed.
                 */
                DEBUG(1,("pid[%d] Error writing %d bytes to client %s. %d. (%s)\n",
-                        (int)sys_getpid(), (int)len,
+                        (int)getpid(), (int)len,
                         get_peer_addr(sconn->sock, addr, sizeof(addr)),
                         (int)ret, strerror(errno) ));
 
@@ -515,13 +514,21 @@ static bool init_smb_request(struct smb_request *req,
                             size_t unread_bytes, bool encrypted,
                             uint32_t seqnum)
 {
+       struct smbXsrv_tcon *tcon;
+       NTSTATUS status;
+       NTTIME now;
        size_t req_size = smb_len(inbuf) + 4;
+
        /* Ensure we have at least smb_size bytes. */
        if (req_size < smb_size) {
                DEBUG(0,("init_smb_request: invalid request size %u\n",
                        (unsigned int)req_size ));
                return false;
        }
+
+       req->request_time = timeval_current();
+       now = timeval_to_nttime(&req->request_time);
+
        req->cmd    = CVAL(inbuf, smb_com);
        req->flags2 = SVAL(inbuf, smb_flg2);
        req->smbpid = SVAL(inbuf, smb_pid);
@@ -536,12 +543,16 @@ static bool init_smb_request(struct smb_request *req,
        req->unread_bytes = unread_bytes;
        req->encrypted = encrypted;
        req->sconn = sconn;
-       req->conn = conn_find(sconn,req->tid);
+       status = smb1srv_tcon_lookup(sconn->conn, req->tid, now, &tcon);
+       if (NT_STATUS_IS_OK(status)) {
+               req->conn = tcon->compat;
+       } else {
+               req->conn = NULL;
+       }
        req->chain_fsp = NULL;
-       req->chain_outbuf = NULL;
-       req->done = false;
        req->smb2req = NULL;
        req->priv_paths = NULL;
+       req->chain = NULL;
        smb_init_perfcount_data(&req->pcd);
 
        /* Ensure we have at least wct words and 2 bytes of bcc. */
@@ -705,15 +716,14 @@ void remove_deferred_open_message_smb(struct smbd_server_connection *sconn,
  schedule it for immediate processing.
 ****************************************************************************/
 
-void schedule_deferred_open_message_smb(struct smbd_server_connection *sconn,
+bool schedule_deferred_open_message_smb(struct smbd_server_connection *sconn,
                                        uint64_t mid)
 {
        struct pending_message_list *pml;
        int i = 0;
 
        if (sconn->using_smb2) {
-               schedule_deferred_open_message_smb2(sconn, mid);
-               return;
+               return schedule_deferred_open_message_smb2(sconn, mid);
        }
 
        for (pml = sconn->deferred_open_queue; pml; pml = pml->next) {
@@ -755,13 +765,15 @@ void schedule_deferred_open_message_smb(struct smbd_server_connection *sconn,
                        TALLOC_FREE(pml->te);
                        pml->te = te;
                        DLIST_PROMOTE(sconn->deferred_open_queue, pml);
-                       return;
+                       return true;
                }
        }
 
        DEBUG(10,("schedule_deferred_open_message_smb: failed to "
                "find message mid %llu\n",
                (unsigned long long)mid ));
+
+       return false;
 }
 
 /****************************************************************************
@@ -1316,12 +1328,17 @@ static void smb_dump(const char *name, int type, const char *data)
 
        len = smb_len_tcp(data)+4;
        for (i=1;i<100;i++) {
-               if (asprintf(&fname, "/tmp/%s.%d.%s", name, i,
-                            type ? "req" : "resp") == -1) {
+               fname = talloc_asprintf(talloc_tos(),
+                               "/tmp/%s.%d.%s",
+                               name,
+                               i,
+                               type ? "req" : "resp");
+               if (fname == NULL) {
                        return;
                }
                fd = open(fname, O_WRONLY|O_CREAT|O_EXCL, 0644);
                if (fd != -1 || errno != EEXIST) break;
+               TALLOC_FREE(fname);
        }
        if (fd != -1) {
                ssize_t ret = write(fd, data, len);
@@ -1330,7 +1347,7 @@ static void smb_dump(const char *name, int type, const char *data)
                close(fd);
                DEBUG(0,("created %s len %lu\n", fname, (unsigned long)len));
        }
-       SAFE_FREE(fname);
+       TALLOC_FREE(fname);
 }
 
 /****************************************************************************
@@ -1349,9 +1366,12 @@ static void smb_dump(const char *name, int type, const char *data)
 static connection_struct *switch_message(uint8 type, struct smb_request *req)
 {
        int flags;
-       uint16 session_tag;
+       uint64_t session_tag;
        connection_struct *conn = NULL;
        struct smbd_server_connection *sconn = req->sconn;
+       NTTIME now = timeval_to_nttime(&req->request_time);
+       struct smbXsrv_session *session = NULL;
+       NTSTATUS status;
 
        errno = 0;
 
@@ -1369,7 +1389,7 @@ static connection_struct *switch_message(uint8 type, struct smb_request *req)
        conn = req->conn;
 
        DEBUG(3,("switch message %s (pid %d) conn 0x%lx\n", smb_fn_name(type),
-                (int)sys_getpid(), (unsigned long)conn));
+                (int)getpid(), (unsigned long)conn));
 
        smb_dump(smb_fn_name(type), 1, (const char *)req->inbuf);
 
@@ -1385,18 +1405,42 @@ static connection_struct *switch_message(uint8 type, struct smb_request *req)
         * JRA.
         */
 
+       /*
+        * lookup an existing session
+        *
+        * Note: for now we only check for NT_STATUS_NETWORK_SESSION_EXPIRED
+        * here, the main check is still in change_to_user()
+        */
+       status = smb1srv_session_lookup(sconn->conn,
+                                       session_tag,
+                                       now,
+                                       &session);
+       if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_SESSION_EXPIRED)) {
+               switch (type) {
+               case SMBsesssetupX:
+                       status = NT_STATUS_OK;
+                       break;
+               default:
+                       DEBUG(1,("Error: session %llu is expired, mid=%llu.\n",
+                                (unsigned long long)session_tag,
+                                (unsigned long long)req->mid));
+                       reply_nterror(req, NT_STATUS_NETWORK_SESSION_EXPIRED);
+                       return conn;
+               }
+       }
+
        if (session_tag != sconn->smb1.sessions.last_session_tag) {
-               user_struct *vuser = NULL;
+               struct user_struct *vuser = NULL;
 
                sconn->smb1.sessions.last_session_tag = session_tag;
-               if(session_tag != UID_FIELD_INVALID) {
-                       vuser = get_valid_user_struct(sconn, session_tag);
-                       if (vuser) {
-                               set_current_user_info(
-                                       vuser->session_info->unix_info->sanitized_username,
-                                       vuser->session_info->unix_info->unix_name,
-                                       vuser->session_info->info->domain_name);
-                       }
+               if (session) {
+                       vuser = session->compat;
+               }
+               if (vuser) {
+                       set_current_user_info(
+                               vuser->session_info->unix_info->sanitized_username,
+                               vuser->session_info->unix_info->unix_name,
+                               vuser->session_info->info->domain_name);
                }
        }
 
@@ -1538,11 +1582,6 @@ static void construct_reply(struct smbd_server_connection *sconn,
 
        conn = switch_message(req->cmd, req);
 
-       if (req->done) {
-               TALLOC_FREE(req);
-               return;
-       }
-
        if (req->outbuf == NULL) {
                return;
        }
@@ -1569,79 +1608,118 @@ static void construct_reply_chain(struct smbd_server_connection *sconn,
                                  bool encrypted,
                                  struct smb_perfcount_data *deferred_pcd)
 {
-       struct connection_struct *conn = NULL;
        struct smb_request **reqs = NULL;
-       struct smb_request *req, *first_req, *last_req;
-       unsigned i, num_reqs;
-       NTSTATUS status;
+       struct smb_request *req;
+       unsigned num_reqs;
        bool ok;
 
        ok = smb1_parse_chain(talloc_tos(), (uint8_t *)inbuf, sconn, encrypted,
                              seqnum, &reqs, &num_reqs);
        if (!ok) {
-               status = NT_STATUS_INVALID_PARAMETER;
-               goto error;
+               char errbuf[smb_size];
+               error_packet(errbuf, 0, 0, NT_STATUS_INVALID_PARAMETER,
+                            __LINE__, __FILE__);
+               if (!srv_send_smb(sconn, errbuf, true, seqnum, encrypted,
+                                 NULL)) {
+                       exit_server_cleanly("construct_reply_chain: "
+                                           "srv_send_smb failed.");
+               }
+               return;
        }
 
+       req = reqs[0];
+       req->inbuf = (uint8_t *)talloc_move(reqs, &inbuf);
+
+       req->conn = switch_message(req->cmd, req);
+
+       if (req->outbuf == NULL) {
+               /*
+                * Request has suspended itself, will come
+                * back here.
+                */
+               return;
+       }
+       smb_request_done(req);
+}
+
+/*
+ * To be called from an async SMB handler that is potentially chained
+ * when it is finished for shipping.
+ */
+
+void smb_request_done(struct smb_request *req)
+{
+       struct smb_request **reqs = NULL;
+       struct smb_request *first_req;
+       size_t i, num_reqs, next_index;
+       NTSTATUS status;
+
+       if (req->chain == NULL) {
+               first_req = req;
+               goto shipit;
+       }
+
+       reqs = req->chain;
+       num_reqs = talloc_array_length(reqs);
+
        for (i=0; i<num_reqs; i++) {
-               req = reqs[i];
+               if (reqs[i] == req) {
+                       break;
+               }
+       }
+       if (i == num_reqs) {
+               /*
+                * Invalid chain, should not happen
+                */
+               status = NT_STATUS_INTERNAL_ERROR;
+               goto error;
+       }
+       next_index = i+1;
 
-               if (i > 0) {
-                       /*
-                        * Get the session and tree ids and chain fsp
-                        * from the previous request into the current
-                        * one
-                        */
-                       struct smb_request *prev_req = reqs[i-1];
-                       const uint8_t *prev_outbuf = prev_req->outbuf;
+       while ((next_index < num_reqs) && (IVAL(req->outbuf, smb_rcls) == 0)) {
+               struct smb_request *next = reqs[next_index];
+               struct smbXsrv_tcon *tcon;
+               NTTIME now = timeval_to_nttime(&req->request_time);
 
-                       req->vuid = SVAL(prev_outbuf, smb_uid);
-                       req->tid  = SVAL(prev_outbuf, smb_tid);
-                       req->conn = conn_find(req->sconn, req->tid);
-                       req->chain_fsp = prev_req->chain_fsp;
+               next->vuid = SVAL(req->outbuf, smb_uid);
+               next->tid  = SVAL(req->outbuf, smb_tid);
+               status = smb1srv_tcon_lookup(req->sconn->conn, req->tid,
+                                            now, &tcon);
+               if (NT_STATUS_IS_OK(status)) {
+                       req->conn = tcon->compat;
+               } else {
+                       req->conn = NULL;
                }
-               req->inbuf = (uint8_t *)inbuf;
-               conn = switch_message(req->cmd, req);
+               next->chain_fsp = req->chain_fsp;
+               next->inbuf = (uint8_t *)req->inbuf;
+
+               req = next;
+               req->conn = switch_message(req->cmd, req);
 
                if (req->outbuf == NULL) {
-                       if (open_was_deferred(req->sconn, req->mid)) {
-                               TALLOC_FREE(reqs);
-                               return;
-                       }
-                       status = NT_STATUS_INTERNAL_ERROR;
-                       goto error;
-               }
-               if (IVAL(req->outbuf, smb_rcls) != 0) {
-                       break;
+                       /*
+                        * Request has suspended itself, will come
+                        * back here.
+                        */
+                       return;
                }
+               next_index += 1;
        }
 
        first_req = reqs[0];
-       last_req = req;
-
-       if (i == 0) {
-               /*
-                * The first request already gave an error, no need to
-                * do any splicing
-                */
-               goto shipit;
-       }
 
        for (i=1; i<next_index; i++) {
-               req = reqs[i];
+               bool ok;
 
-               ok = smb_splice_chain(&first_req->outbuf, req->outbuf);
+               ok = smb_splice_chain(&first_req->outbuf, reqs[i]->outbuf);
                if (!ok) {
                        status = NT_STATUS_INTERNAL_ERROR;
                        goto error;
                }
-               if (req == last_req) {
-                       break;
-               }
        }
 
-       SSVAL(first_req->outbuf, smb_uid, SVAL(last_req->outbuf, smb_uid));
-       SSVAL(first_req->outbuf, smb_tid, SVAL(last_req->outbuf, smb_tid));
+       SSVAL(first_req->outbuf, smb_uid, SVAL(req->outbuf, smb_uid));
+       SSVAL(first_req->outbuf, smb_tid, SVAL(req->outbuf, smb_tid));
 
        /*
         * This scary statement intends to set the
@@ -1650,42 +1728,43 @@ static void construct_reply_chain(struct smbd_server_connection *sconn,
         */
        SSVAL(first_req->outbuf, smb_flg2,
              (SVAL(first_req->outbuf, smb_flg2) & ~FLAGS2_32_BIT_ERROR_CODES)
-             |(SVAL(last_req->outbuf, smb_flg2) & FLAGS2_32_BIT_ERROR_CODES));
+             |(SVAL(req->outbuf, smb_flg2) & FLAGS2_32_BIT_ERROR_CODES));
 
        /*
         * Transfer the error codes from the subrequest to the main one
         */
-       SSVAL(first_req->outbuf, smb_rcls, SVAL(last_req->outbuf, smb_rcls));
-       SSVAL(first_req->outbuf, smb_err,  SVAL(last_req->outbuf, smb_err));
+       SSVAL(first_req->outbuf, smb_rcls, SVAL(req->outbuf, smb_rcls));
+       SSVAL(first_req->outbuf, smb_err,  SVAL(req->outbuf, smb_err));
 
-       goto shipit;
+       _smb_setlen_large(
+               first_req->outbuf, talloc_get_size(first_req->outbuf) - 4);
 
 shipit:
-       _smb_setlen_large(first_req->outbuf,
-                         talloc_get_size(first_req->outbuf) - 4);
-
        if (!srv_send_smb(first_req->sconn,
                          (char *)first_req->outbuf,
                          true, first_req->seqnum+1,
-                         IS_CONN_ENCRYPTED(conn)||first_req->encrypted,
+                         IS_CONN_ENCRYPTED(req->conn)||first_req->encrypted,
                          &first_req->pcd)) {
                exit_server_cleanly("construct_reply_chain: srv_send_smb "
                                    "failed.");
        }
-       TALLOC_FREE(reqs);
+       TALLOC_FREE(req);       /* non-chained case */
+       TALLOC_FREE(reqs);      /* chained case */
        return;
 
 error:
        {
                char errbuf[smb_size];
                error_packet(errbuf, 0, 0, status, __LINE__, __FILE__);
-               if (!srv_send_smb(sconn, errbuf, true, seqnum, encrypted,
+               if (!srv_send_smb(req->sconn, errbuf, true,
+                                 req->seqnum+1, req->encrypted,
                                  NULL)) {
                        exit_server_cleanly("construct_reply_chain: "
                                            "srv_send_smb failed.");
                }
        }
-       TALLOC_FREE(reqs);
+       TALLOC_FREE(req);       /* non-chained case */
+       TALLOC_FREE(reqs);      /* chained case */
 }
 
 /****************************************************************************
@@ -2227,6 +2306,7 @@ bool smb1_parse_chain(TALLOC_CTX *mem_ctx, const uint8_t *buf,
                      struct smb_request ***reqs, unsigned *num_reqs)
 {
        struct smb1_parse_chain_state state;
+       unsigned i;
 
        state.mem_ctx = mem_ctx;
        state.buf = buf;
@@ -2240,6 +2320,9 @@ bool smb1_parse_chain(TALLOC_CTX *mem_ctx, const uint8_t *buf,
                TALLOC_FREE(state.reqs);
                return false;
        }
+       for (i=0; i<state.num_reqs; i++) {
+               state.reqs[i]->chain = state.reqs;
+       }
        *reqs = state.reqs;
        *num_reqs = state.num_reqs;
        return true;
@@ -2379,12 +2462,21 @@ static void smbd_server_echo_handler(struct event_context *ev,
 }
 
 #ifdef CLUSTER_SUPPORT
+
+struct smbd_release_ip_state {
+       struct smbd_server_connection *sconn;
+       char addr[INET6_ADDRSTRLEN];
+};
+
 /****************************************************************************
 received when we should release a specific IP
 ****************************************************************************/
 static void release_ip(const char *ip, void *priv)
 {
-       const char *addr = (const char *)priv;
+       struct smbd_release_ip_state *state =
+               talloc_get_type_abort(priv,
+               struct smbd_release_ip_state);
+       const char *addr = state->addr;
        const char *p = addr;
 
        if (strncmp("::ffff:", addr, 7) == 0) {
@@ -2395,19 +2487,55 @@ static void release_ip(const char *ip, void *priv)
                   "our address is %s\n", ip, p));
 
        if ((strcmp(p, ip) == 0) || ((p != addr) && strcmp(addr, ip) == 0)) {
-               /* we can't afford to do a clean exit - that involves
-                  database writes, which would potentially mean we
-                  are still running after the failover has finished -
-                  we have to get rid of this process ID straight
-                  away */
                DEBUG(0,("Got release IP message for our IP %s - exiting immediately\n",
                        ip));
-               /* note we must exit with non-zero status so the unclean handler gets
-                  called in the parent, so that the brl database is tickled */
-               _exit(1);
+               /*
+                * With SMB2 we should do a clean disconnect,
+                * the previous_session_id in the session setup
+                * will cleanup the old session, tcons and opens.
+                *
+                * A clean disconnect is needed in order to support
+                * durable handles.
+                *
+                * Note: typically this is never triggered
+                *       as we got a TCP RST (triggered by ctdb event scripts)
+                *       before we get CTDB_SRVID_RELEASE_IP.
+                *
+                * We used to call _exit(1) here, but as this was mostly never
+                * triggered and has implication on our process model,
+                * we can just use smbd_server_connection_terminate()
+                * (also for SMB1).
+                */
+               smbd_server_connection_terminate(state->sconn,
+                                                "CTDB_SRVID_RELEASE_IP");
+               return;
        }
 }
 
+static NTSTATUS smbd_register_ips(struct smbd_server_connection *sconn,
+                                 struct sockaddr_storage *srv,
+                                 struct sockaddr_storage *clnt)
+{
+       struct smbd_release_ip_state *state;
+       struct ctdbd_connection *cconn;
+
+       cconn = messaging_ctdbd_connection();
+       if (cconn == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       state = talloc_zero(sconn, struct smbd_release_ip_state);
+       if (state == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
+       state->sconn = sconn;
+       if (print_sockaddr(state->addr, sizeof(state->addr), srv) == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       return ctdbd_register_ips(cconn, srv, clnt, release_ip, state);
+}
+
 static int client_get_tcp_info(int sock, struct sockaddr_storage *server,
                               struct sockaddr_storage *client)
 {
@@ -2496,9 +2624,6 @@ static bool housekeeping_fn(const struct timeval *now, void *private_data)
        /* check if we need to reload services */
        check_reload(sconn, time_mono(NULL));
 
-       /* Change machine password if neccessary. */
-       attempt_machine_password_change();
-
         /*
         * Force a log file check.
         */
@@ -2603,7 +2728,7 @@ static void smbd_echo_read_waited(struct tevent_req *subreq)
 
        if (!fd_is_readable(sconn->sock)) {
                DEBUG(10,("echo_handler[%d] the parent smbd was faster\n",
-                         (int)sys_getpid()));
+                         (int)getpid()));
 
                ok = smbd_unlock_socket_internal(sconn);
                if (!ok) {
@@ -2632,7 +2757,7 @@ static void smbd_echo_read_waited(struct tevent_req *subreq)
        if (tevent_req_nterror(req, status)) {
                tevent_req_nterror(req, status);
                DEBUG(1, ("echo_handler[%d]: receive_smb_raw_talloc failed: %s\n",
-                         (int)sys_getpid(), nt_errstr(status)));
+                         (int)getpid(), nt_errstr(status)));
                return;
        }
 
@@ -2716,7 +2841,7 @@ static void smbd_echo_writer_done(struct tevent_req *req)
                DEBUG(1, ("writev to parent failed: %s\n", strerror(err)));
                exit(1);
        }
-       DEBUG(10,("echo_handler[%d]: forwarded pdu to main\n", (int)sys_getpid()));
+       DEBUG(10,("echo_handler[%d]: forwarded pdu to main\n", (int)getpid()));
        smbd_echo_activate_writer(state);
 }
 
@@ -2897,7 +3022,7 @@ static void smbd_echo_got_packet(struct tevent_req *req)
                iov->iov_len = buflen;
 
                DEBUG(10,("echo_handler[%d]: forward to main\n",
-                         (int)sys_getpid()));
+                         (int)getpid()));
                smbd_echo_activate_writer(state);
        }
 
@@ -2930,7 +3055,7 @@ bool fork_echo_handler(struct smbd_server_connection *sconn)
                goto fail;
        }
 
-       child = sys_fork();
+       child = fork();
        if (child == 0) {
                NTSTATUS status;
 
@@ -2952,7 +3077,7 @@ bool fork_echo_handler(struct smbd_server_connection *sconn)
        listener_pipe[1] = -1;
        sconn->smb1.echo_handler.trusted_fd = listener_pipe[0];
 
-       DEBUG(10,("fork_echo_handler: main[%d] echo_child[%d]\n", (int)sys_getpid(), child));
+       DEBUG(10,("fork_echo_handler: main[%d] echo_child[%d]\n", (int)getpid(), child));
 
        /*
         * Without smb signing this is the same as the normal smbd
@@ -2987,31 +3112,6 @@ fail:
        return false;
 }
 
-#if CLUSTER_SUPPORT
-
-static NTSTATUS smbd_register_ips(struct smbd_server_connection *sconn,
-                                 struct sockaddr_storage *srv,
-                                 struct sockaddr_storage *clnt)
-{
-       struct ctdbd_connection *cconn;
-       char tmp_addr[INET6_ADDRSTRLEN];
-       char *addr;
-
-       cconn = messaging_ctdbd_connection();
-       if (cconn == NULL) {
-               return NT_STATUS_NO_MEMORY;
-       }
-
-       client_socket_addr(sconn->sock, tmp_addr, sizeof(tmp_addr));
-       addr = talloc_strdup(cconn, tmp_addr);
-       if (addr == NULL) {
-               return NT_STATUS_NO_MEMORY;
-       }
-       return ctdbd_register_ips(cconn, srv, clnt, release_ip, addr);
-}
-
-#endif
-
 static bool uid_in_use(const struct user_struct *user, uid_t uid)
 {
        while (user) {
@@ -3112,14 +3212,78 @@ static void smbd_id_cache_kill(struct messaging_context *msg_ctx,
        id_cache_delete_from_cache(&id);
 }
 
+NTSTATUS smbXsrv_connection_init_tables(struct smbXsrv_connection *conn,
+                                       enum protocol_types protocol)
+{
+       NTSTATUS status;
+
+       set_Protocol(protocol);
+       conn->protocol = protocol;
+
+       if (protocol >= PROTOCOL_SMB2_02) {
+               status = smb2srv_session_table_init(conn);
+               if (!NT_STATUS_IS_OK(status)) {
+                       return status;
+               }
+
+               status = smb2srv_open_table_init(conn);
+               if (!NT_STATUS_IS_OK(status)) {
+                       return status;
+               }
+       } else {
+               status = smb1srv_session_table_init(conn);
+               if (!NT_STATUS_IS_OK(status)) {
+                       return status;
+               }
+
+               status = smb1srv_tcon_table_init(conn);
+               if (!NT_STATUS_IS_OK(status)) {
+                       return status;
+               }
+
+               status = smb1srv_open_table_init(conn);
+               if (!NT_STATUS_IS_OK(status)) {
+                       return status;
+               }
+       }
+
+       return NT_STATUS_OK;
+}
+
+static void smbd_tevent_trace_callback(enum tevent_trace_point point,
+                                      void *private_data)
+{
+       struct smbXsrv_connection *conn =
+               talloc_get_type_abort(private_data,
+               struct smbXsrv_connection);
+
+       switch (point) {
+       case TEVENT_TRACE_BEFORE_WAIT:
+               /*
+                * This just removes compiler warning
+                * without profile support
+                */
+               conn->smbd_idle_profstamp = 0;
+               START_PROFILE_STAMP(smbd_idle, conn->smbd_idle_profstamp);
+               break;
+       case TEVENT_TRACE_AFTER_WAIT:
+               END_PROFILE_STAMP(smbd_idle, conn->smbd_idle_profstamp);
+               break;
+       }
+}
+
 /****************************************************************************
  Process commands from the client
 ****************************************************************************/
 
 void smbd_process(struct tevent_context *ev_ctx,
-                 struct smbd_server_connection *sconn)
+                 struct messaging_context *msg_ctx,
+                 int sock_fd,
+                 bool interactive)
 {
        TALLOC_CTX *frame = talloc_stackframe();
+       struct smbXsrv_connection *conn;
+       struct smbd_server_connection *sconn;
        struct sockaddr_storage ss;
        struct sockaddr *sa = NULL;
        socklen_t sa_socklen;
@@ -3130,6 +3294,47 @@ void smbd_process(struct tevent_context *ev_ctx,
        char *rhost;
        int ret;
 
+       conn = talloc_zero(ev_ctx, struct smbXsrv_connection);
+       if (conn == NULL) {
+               DEBUG(0,("talloc_zero(struct smbXsrv_connection)\n"));
+               exit_server_cleanly("talloc_zero(struct smbXsrv_connection).\n");
+       }
+
+       conn->ev_ctx = ev_ctx;
+       conn->msg_ctx = msg_ctx;
+
+       sconn = talloc_zero(conn, struct smbd_server_connection);
+       if (!sconn) {
+               exit_server("failed to create smbd_server_connection");
+       }
+
+       conn->sconn = sconn;
+       sconn->conn = conn;
+
+       /*
+        * TODO: remove this...:-)
+        */
+       global_smbXsrv_connection = conn;
+
+       sconn->ev_ctx = ev_ctx;
+       sconn->msg_ctx = msg_ctx;
+       sconn->sock = sock_fd;
+       sconn->smb1.echo_handler.trusted_fd = -1;
+       sconn->smb1.echo_handler.socket_lock_fd = -1;
+
+       if (!interactive) {
+               smbd_setup_sig_term_handler(sconn);
+               smbd_setup_sig_hup_handler(sconn);
+
+               if (!serverid_register(messaging_server_id(msg_ctx),
+                                      FLAG_MSG_GENERAL|FLAG_MSG_SMBD
+                                      |FLAG_MSG_DBWRAP
+                                      |FLAG_MSG_PRINT_GENERAL)) {
+                       exit_server_cleanly("Could not register myself in "
+                                           "serverid.tdb");
+               }
+       }
+
        if (lp_srv_maxprotocol() >= PROTOCOL_SMB2_02) {
                /*
                 * We're not making the decision here,
@@ -3259,7 +3464,9 @@ void smbd_process(struct tevent_context *ev_ctx,
                   tsocket_address_string(remote_address, talloc_tos()),
                   tsocket_address_string(local_address, talloc_tos())));
 
-       init_modules();
+       if (lp_preload_modules()) {
+               smb_load_modules(lp_preload_modules());
+       }
 
        smb_perfcount_init();
 
@@ -3267,22 +3474,27 @@ void smbd_process(struct tevent_context *ev_ctx,
                exit_server("Could not open account policy tdb.\n");
        }
 
-       if (*lp_rootdir()) {
-               if (chroot(lp_rootdir()) != 0) {
-                       DEBUG(0,("Failed to change root to %s\n", lp_rootdir()));
+       if (*lp_rootdir(talloc_tos())) {
+               if (chroot(lp_rootdir(talloc_tos())) != 0) {
+                       DEBUG(0,("Failed to change root to %s\n",
+                                lp_rootdir(talloc_tos())));
                        exit_server("Failed to chroot()");
                }
                if (chdir("/") == -1) {
-                       DEBUG(0,("Failed to chdir to / on chroot to %s\n", lp_rootdir()));
+                       DEBUG(0,("Failed to chdir to / on chroot to %s\n", lp_rootdir(talloc_tos())));
                        exit_server("Failed to chroot()");
                }
-               DEBUG(0,("Changed root to %s\n", lp_rootdir()));
+               DEBUG(0,("Changed root to %s\n", lp_rootdir(talloc_tos())));
        }
 
        if (!srv_init_signing(sconn)) {
                exit_server("Failed to init smb_signing");
        }
 
+       if (!file_init(sconn)) {
+               exit_server("file_init() failed");
+       }
+
        /* Setup oplocks */
        if (!init_oplocks(sconn))
                exit_server("Failed to init oplocks");
@@ -3368,15 +3580,12 @@ void smbd_process(struct tevent_context *ev_ctx,
 
        sconn->nbt.got_session = false;
 
-       sconn->smb1.negprot.max_recv = MIN(lp_maxxmit(),BUFFER_SIZE);
+       sconn->smb1.negprot.max_recv = MIN(lp_max_xmit(),BUFFER_SIZE);
 
        sconn->smb1.sessions.done_sesssetup = false;
        sconn->smb1.sessions.max_send = BUFFER_SIZE;
        sconn->smb1.sessions.last_session_tag = UID_FIELD_INVALID;
-       /* this holds info on user ids that are already validated for this VC */
-       sconn->smb1.sessions.next_vuid = VUID_OFFSET;
 
-       conn_init(sconn);
        if (!init_dptrs(sconn)) {
                exit_server("init_dptrs() failed");
        }
@@ -3391,8 +3600,15 @@ void smbd_process(struct tevent_context *ev_ctx,
                exit_server("failed to create smbd_server_connection fde");
        }
 
+       sconn->conn->local_address = sconn->local_address;
+       sconn->conn->remote_address = sconn->remote_address;
+       sconn->conn->remote_hostname = sconn->remote_hostname;
+       sconn->conn->protocol = PROTOCOL_NONE;
+
        TALLOC_FREE(frame);
 
+       tevent_set_trace_callback(ev_ctx, smbd_tevent_trace_callback, conn);
+
        while (True) {
                frame = talloc_stackframe_pool(8192);