smbd: move homes_snum from struct user_struct to struct smbXsrv_session
[gd/samba-autobuild/.git] / source3 / smbd / smbXsrv_session.c
index 7eca968285338925e4e2eee6f2ded3a6a0beeb08..1bed4ab22e9e8a85117726e9f65f0a541e3dd0eb 100644 (file)
@@ -21,6 +21,7 @@
 #include "includes.h"
 #include "system/filesys.h"
 #include <tevent.h>
+#include "lib/util/server_id.h"
 #include "smbd/smbd.h"
 #include "smbd/globals.h"
 #include "dbwrap/dbwrap.h"
@@ -51,15 +52,12 @@ struct smbXsrv_session_table {
        } global;
 };
 
-static NTSTATUS smb2srv_session_lookup_raw(struct smbXsrv_session_table *table,
-                                          uint64_t session_id, NTTIME now,
-                                          struct smbXsrv_session **session);
-
 static struct db_context *smbXsrv_session_global_db_ctx = NULL;
 
-NTSTATUS smbXsrv_session_global_init(void)
+NTSTATUS smbXsrv_session_global_init(struct messaging_context *msg_ctx)
 {
-       const char *global_path = NULL;
+       char *global_path = NULL;
+       struct db_context *backend = NULL;
        struct db_context *db_ctx = NULL;
 
        if (smbXsrv_session_global_db_ctx != NULL) {
@@ -69,17 +67,21 @@ NTSTATUS smbXsrv_session_global_init(void)
        /*
         * This contains secret information like session keys!
         */
-       global_path = lock_path("smbXsrv_session_global.tdb");
-
-       db_ctx = db_open(NULL, global_path,
-                        0, /* hash_size */
-                        TDB_DEFAULT |
-                        TDB_CLEAR_IF_FIRST |
-                        TDB_INCOMPATIBLE_HASH,
-                        O_RDWR | O_CREAT, 0600,
-                        DBWRAP_LOCK_ORDER_1,
-                        DBWRAP_FLAG_NONE);
-       if (db_ctx == NULL) {
+       global_path = lock_path(talloc_tos(), "smbXsrv_session_global.tdb");
+       if (global_path == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       backend = db_open(NULL, global_path,
+                         0, /* hash_size */
+                         TDB_DEFAULT |
+                         TDB_CLEAR_IF_FIRST |
+                         TDB_INCOMPATIBLE_HASH,
+                         O_RDWR | O_CREAT, 0600,
+                         DBWRAP_LOCK_ORDER_1,
+                         DBWRAP_FLAG_NONE);
+       TALLOC_FREE(global_path);
+       if (backend == NULL) {
                NTSTATUS status;
 
                status = map_nt_error_from_unix_common(errno);
@@ -87,6 +89,12 @@ NTSTATUS smbXsrv_session_global_init(void)
                return status;
        }
 
+       db_ctx = db_open_watched(NULL, &backend, global_messaging_context());
+       if (db_ctx == NULL) {
+               TALLOC_FREE(backend);
+               return NT_STATUS_NO_MEMORY;
+       }
+
        smbXsrv_session_global_db_ctx = db_ctx;
 
        return NT_STATUS_OK;
@@ -161,6 +169,48 @@ static NTSTATUS smbXsrv_session_local_key_to_id(TDB_DATA key, uint32_t *id)
        return NT_STATUS_OK;
 }
 
+static struct db_record *smbXsrv_session_global_fetch_locked(
+                       struct db_context *db,
+                       uint32_t id,
+                       TALLOC_CTX *mem_ctx)
+{
+       TDB_DATA key;
+       uint8_t key_buf[SMBXSRV_SESSION_GLOBAL_TDB_KEY_SIZE];
+       struct db_record *rec = NULL;
+
+       key = smbXsrv_session_global_id_to_key(id, key_buf);
+
+       rec = dbwrap_fetch_locked(db, mem_ctx, key);
+
+       if (rec == NULL) {
+               DBG_DEBUG("Failed to lock global id 0x%08x, key '%s'\n", id,
+                         hex_encode_talloc(talloc_tos(), key.dptr, key.dsize));
+       }
+
+       return rec;
+}
+
+static struct db_record *smbXsrv_session_local_fetch_locked(
+                       struct db_context *db,
+                       uint32_t id,
+                       TALLOC_CTX *mem_ctx)
+{
+       TDB_DATA key;
+       uint8_t key_buf[SMBXSRV_SESSION_LOCAL_TDB_KEY_SIZE];
+       struct db_record *rec = NULL;
+
+       key = smbXsrv_session_local_id_to_key(id, key_buf);
+
+       rec = dbwrap_fetch_locked(db, mem_ctx, key);
+
+       if (rec == NULL) {
+               DBG_DEBUG("Failed to lock local id 0x%08x, key '%s'\n", id,
+                         hex_encode_talloc(talloc_tos(), key.dptr, key.dsize));
+       }
+
+       return rec;
+}
+
 static void smbXsrv_session_close_loop(struct tevent_req *subreq);
 
 static NTSTATUS smbXsrv_session_table_init(struct smbXsrv_connection *conn,
@@ -200,7 +250,7 @@ static NTSTATUS smbXsrv_session_table_init(struct smbXsrv_connection *conn,
        table->local.highest_id = highest_id;
        table->local.max_sessions = max_sessions;
 
-       status = smbXsrv_session_global_init();
+       status = smbXsrv_session_global_init(client->msg_ctx);
        if (!NT_STATUS_IS_OK(status)) {
                TALLOC_FREE(table);
                return status;
@@ -208,9 +258,9 @@ static NTSTATUS smbXsrv_session_table_init(struct smbXsrv_connection *conn,
 
        table->global.db_ctx = smbXsrv_session_global_db_ctx;
 
-       dbwrap_watch_db(table->global.db_ctx, client->msg_ctx);
-
-       subreq = messaging_read_send(table, client->ev_ctx, client->msg_ctx,
+       subreq = messaging_read_send(table,
+                                    client->raw_ev_ctx,
+                                    client->msg_ctx,
                                     MSG_SMBXSRV_SESSION_CLOSE);
        if (subreq == NULL) {
                TALLOC_FREE(table);
@@ -222,6 +272,8 @@ static NTSTATUS smbXsrv_session_table_init(struct smbXsrv_connection *conn,
        return NT_STATUS_OK;
 }
 
+static void smbXsrv_session_close_shutdown_done(struct tevent_req *subreq);
+
 static void smbXsrv_session_close_loop(struct tevent_req *subreq)
 {
        struct smbXsrv_client *client =
@@ -274,9 +326,9 @@ static void smbXsrv_session_close_loop(struct tevent_req *subreq)
                goto next;
        }
 
-       status = smb2srv_session_lookup_raw(client->session_table,
-                                           close_info0->old_session_wire_id,
-                                           now, &session);
+       status = smb2srv_session_lookup_client(client,
+                                              close_info0->old_session_wire_id,
+                                              now, &session);
        if (NT_STATUS_EQUAL(status, NT_STATUS_USER_SESSION_DELETED)) {
                DEBUG(4,("smbXsrv_session_close_loop: "
                         "old_session_wire_id %llu not found\n",
@@ -326,25 +378,29 @@ static void smbXsrv_session_close_loop(struct tevent_req *subreq)
                goto next;
        }
 
-       /*
-        * TODO: cancel all outstanding requests on the session
-        */
-       status = smbXsrv_session_logoff(session);
-       if (!NT_STATUS_IS_OK(status)) {
+       subreq = smb2srv_session_shutdown_send(session, client->raw_ev_ctx,
+                                              session, NULL);
+       if (subreq == NULL) {
+               status = NT_STATUS_NO_MEMORY;
                DEBUG(0, ("smbXsrv_session_close_loop: "
-                         "smbXsrv_session_logoff(%llu) failed: %s\n",
+                         "smb2srv_session_shutdown_send(%llu) failed: %s\n",
                          (unsigned long long)session->global->session_wire_id,
                          nt_errstr(status)));
                if (DEBUGLVL(1)) {
                        NDR_PRINT_DEBUG(smbXsrv_session_closeB, &close_blob);
                }
+               goto next;
        }
-       TALLOC_FREE(session);
+       tevent_req_set_callback(subreq,
+                               smbXsrv_session_close_shutdown_done,
+                               session);
 
 next:
        TALLOC_FREE(rec);
 
-       subreq = messaging_read_send(table, client->ev_ctx, client->msg_ctx,
+       subreq = messaging_read_send(table,
+                                    client->raw_ev_ctx,
+                                    client->msg_ctx,
                                     MSG_SMBXSRV_SESSION_CLOSE);
        if (subreq == NULL) {
                const char *r;
@@ -355,6 +411,33 @@ next:
        tevent_req_set_callback(subreq, smbXsrv_session_close_loop, client);
 }
 
+static void smbXsrv_session_close_shutdown_done(struct tevent_req *subreq)
+{
+       struct smbXsrv_session *session =
+               tevent_req_callback_data(subreq,
+               struct smbXsrv_session);
+       NTSTATUS status;
+
+       status = smb2srv_session_shutdown_recv(subreq);
+       TALLOC_FREE(subreq);
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(0, ("smbXsrv_session_close_loop: "
+                         "smb2srv_session_shutdown_recv(%llu) failed: %s\n",
+                         (unsigned long long)session->global->session_wire_id,
+                         nt_errstr(status)));
+       }
+
+       status = smbXsrv_session_logoff(session);
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(0, ("smbXsrv_session_close_loop: "
+                         "smbXsrv_session_logoff(%llu) failed: %s\n",
+                         (unsigned long long)session->global->session_wire_id,
+                         nt_errstr(status)));
+       }
+
+       TALLOC_FREE(session);
+}
+
 struct smb1srv_session_local_allocate_state {
        const uint32_t lowest_id;
        const uint32_t highest_id;
@@ -431,8 +514,6 @@ static NTSTATUS smb1srv_session_local_allocate_id(struct db_context *db,
 
        for (i = 0; i < (range / 2); i++) {
                uint32_t id;
-               uint8_t key_buf[SMBXSRV_SESSION_LOCAL_TDB_KEY_SIZE];
-               TDB_DATA key;
                TDB_DATA val;
                struct db_record *rec = NULL;
 
@@ -446,9 +527,7 @@ static NTSTATUS smb1srv_session_local_allocate_id(struct db_context *db,
                        id = highest_id;
                }
 
-               key = smbXsrv_session_local_id_to_key(id, key_buf);
-
-               rec = dbwrap_fetch_locked(db, mem_ctx, key);
+               rec = smbXsrv_session_local_fetch_locked(db, id, mem_ctx);
                if (rec == NULL) {
                        return NT_STATUS_INSUFFICIENT_RESOURCES;
                }
@@ -498,16 +577,12 @@ static NTSTATUS smb1srv_session_local_allocate_id(struct db_context *db,
 
        if (NT_STATUS_IS_OK(state.status)) {
                uint32_t id;
-               uint8_t key_buf[SMBXSRV_SESSION_LOCAL_TDB_KEY_SIZE];
-               TDB_DATA key;
                TDB_DATA val;
                struct db_record *rec = NULL;
 
                id = state.useable_id;
 
-               key = smbXsrv_session_local_id_to_key(id, key_buf);
-
-               rec = dbwrap_fetch_locked(db, mem_ctx, key);
+               rec = smbXsrv_session_local_fetch_locked(db, id, mem_ctx);
                if (rec == NULL) {
                        return NT_STATUS_INSUFFICIENT_RESOURCES;
                }
@@ -549,6 +624,8 @@ static void smbXsrv_session_local_fetch_parser(TDB_DATA key, TDB_DATA data,
 }
 
 static NTSTATUS smbXsrv_session_local_lookup(struct smbXsrv_session_table *table,
+                                            /* conn: optional */
+                                            struct smbXsrv_connection *conn,
                                             uint32_t session_local_id,
                                             NTTIME now,
                                             struct smbXsrv_session **_session)
@@ -594,6 +671,19 @@ static NTSTATUS smbXsrv_session_local_lookup(struct smbXsrv_session_table *table
                return NT_STATUS_USER_SESSION_DELETED;
        }
 
+       /*
+        * If a connection is specified check if the session is
+        * valid on the channel.
+        */
+       if (conn != NULL) {
+               struct smbXsrv_channel_global0 *c = NULL;
+
+               status = smbXsrv_session_find_channel(state.session, conn, &c);
+               if (!NT_STATUS_IS_OK(status)) {
+                       return status;
+               }
+       }
+
        state.session->idle_time = now;
 
        if (!NT_STATUS_IS_OK(state.session->status)) {
@@ -647,8 +737,6 @@ static NTSTATUS smbXsrv_session_global_allocate(struct db_context *db,
                bool is_free = false;
                bool was_free = false;
                uint32_t id;
-               uint8_t key_buf[SMBXSRV_SESSION_GLOBAL_TDB_KEY_SIZE];
-               TDB_DATA key;
 
                if (i >= min_tries && last_free != 0) {
                        id = last_free;
@@ -662,9 +750,8 @@ static NTSTATUS smbXsrv_session_global_allocate(struct db_context *db,
                        id--;
                }
 
-               key = smbXsrv_session_global_id_to_key(id, key_buf);
-
-               global->db_rec = dbwrap_fetch_locked(db, mem_ctx, key);
+               global->db_rec = smbXsrv_session_global_fetch_locked(db, id,
+                                                                    mem_ctx);
                if (global->db_rec == NULL) {
                        talloc_free(global);
                        return NT_STATUS_INSUFFICIENT_RESOURCES;
@@ -756,6 +843,10 @@ static void smbXsrv_session_global_verify_record(struct db_record *db_rec,
                         hex_encode_talloc(frame, key.dptr, key.dsize),
                         nt_errstr(status)));
                TALLOC_FREE(frame);
+               *is_free = true;
+               if (was_free) {
+                       *was_free = true;
+               }
                return;
        }
 
@@ -771,6 +862,10 @@ static void smbXsrv_session_global_verify_record(struct db_record *db_rec,
                         global_blob.version));
                NDR_PRINT_DEBUG(smbXsrv_session_globalB, &global_blob);
                TALLOC_FREE(frame);
+               *is_free = true;
+               if (was_free) {
+                       *was_free = true;
+               }
                return;
        }
 
@@ -778,10 +873,12 @@ static void smbXsrv_session_global_verify_record(struct db_record *db_rec,
 
        exists = serverid_exists(&global->channels[0].server_id);
        if (!exists) {
+               struct server_id_buf idbuf;
                DEBUG(2,("smbXsrv_session_global_verify_record: "
                         "key '%s' server_id %s does not exist.\n",
                         hex_encode_talloc(frame, key.dptr, key.dsize),
-                        server_id_str(frame, &global->channels[0].server_id)));
+                        server_id_str_buf(global->channels[0].server_id,
+                                          &idbuf)));
                if (DEBUGLVL(2)) {
                        NDR_PRINT_DEBUG(smbXsrv_session_globalB, &global_blob);
                }
@@ -863,6 +960,7 @@ struct smb2srv_session_close_previous_state {
        struct tevent_context *ev;
        struct smbXsrv_connection *connection;
        struct dom_sid *current_sid;
+       uint64_t previous_session_id;
        uint64_t current_session_id;
        struct db_record *db_rec;
 };
@@ -883,8 +981,6 @@ struct tevent_req *smb2srv_session_close_previous_send(TALLOC_CTX *mem_ctx,
        uint64_t global_zeros = previous_session_id & 0xFFFFFFFF00000000LLU;
        struct smbXsrv_session_table *table = conn->client->session_table;
        struct security_token *current_token = NULL;
-       uint8_t key_buf[SMBXSRV_SESSION_GLOBAL_TDB_KEY_SIZE];
-       TDB_DATA key;
 
        req = tevent_req_create(mem_ctx, &state,
                                struct smb2srv_session_close_previous_state);
@@ -893,6 +989,7 @@ struct tevent_req *smb2srv_session_close_previous_send(TALLOC_CTX *mem_ctx,
        }
        state->ev = ev;
        state->connection = conn;
+       state->previous_session_id = previous_session_id;
        state->current_session_id = current_session_id;
 
        if (global_zeros != 0) {
@@ -921,10 +1018,10 @@ struct tevent_req *smb2srv_session_close_previous_send(TALLOC_CTX *mem_ctx,
                return tevent_req_post(req, ev);
        }
 
-       key = smbXsrv_session_global_id_to_key(global_id, key_buf);
-
-       state->db_rec = dbwrap_fetch_locked(table->global.db_ctx,
-                                           state, key);
+       state->db_rec = smbXsrv_session_global_fetch_locked(
+                                                       table->global.db_ctx,
+                                                       global_id,
+                                                       state /* TALLOC_CTX */);
        if (state->db_rec == NULL) {
                tevent_req_nterror(req, NT_STATUS_UNSUCCESSFUL);
                return tevent_req_post(req, ev);
@@ -980,8 +1077,8 @@ static void smb2srv_session_close_previous_check(struct tevent_req *req)
                return;
        }
 
-       subreq = dbwrap_record_watch_send(state, state->ev,
-                                         state->db_rec, conn->msg_ctx);
+       subreq = dbwrap_watched_watch_send(state, state->ev, state->db_rec,
+                                          (struct server_id){0});
        if (tevent_req_nomem(subreq, req)) {
                TALLOC_FREE(state->db_rec);
                return;
@@ -1013,7 +1110,7 @@ static void smb2srv_session_close_previous_check(struct tevent_req *req)
                return;
        }
 
-       status = messaging_send(conn->msg_ctx,
+       status = messaging_send(conn->client->msg_ctx,
                                global->channels[0].server_id,
                                MSG_SMBXSRV_SESSION_CLOSE, &blob);
        TALLOC_FREE(state->db_rec);
@@ -1033,14 +1130,21 @@ static void smb2srv_session_close_previous_modified(struct tevent_req *subreq)
        struct smb2srv_session_close_previous_state *state =
                tevent_req_data(req,
                struct smb2srv_session_close_previous_state);
+       uint32_t global_id;
        NTSTATUS status;
 
-       status = dbwrap_record_watch_recv(subreq, state, &state->db_rec);
+       status = dbwrap_watched_watch_recv(subreq, NULL, NULL);
        TALLOC_FREE(subreq);
        if (tevent_req_nterror(req, status)) {
                return;
        }
 
+       global_id = state->previous_session_id & UINT32_MAX;
+
+       state->db_rec = smbXsrv_session_global_fetch_locked(
+               state->connection->client->session_table->global.db_ctx,
+               global_id, state /* TALLOC_CTX */);
+
        smb2srv_session_close_previous_check(req);
 }
 
@@ -1057,11 +1161,43 @@ NTSTATUS smb2srv_session_close_previous_recv(struct tevent_req *req)
        return NT_STATUS_OK;
 }
 
-static int smbXsrv_session_destructor(struct smbXsrv_session *session)
+static NTSTATUS smbXsrv_session_clear_and_logoff(struct smbXsrv_session *session)
 {
        NTSTATUS status;
+       struct smbXsrv_connection *xconn = NULL;
+
+       if (session->client != NULL) {
+               xconn = session->client->connections;
+       }
+
+       for (; xconn != NULL; xconn = xconn->next) {
+               struct smbd_smb2_request *preq;
+
+               for (preq = xconn->smb2.requests; preq != NULL; preq = preq->next) {
+                       if (preq->session != session) {
+                               continue;
+                       }
+
+                       preq->session = NULL;
+                       /*
+                        * If we no longer have a session we can't
+                        * sign or encrypt replies.
+                        */
+                       preq->do_signing = false;
+                       preq->do_encryption = false;
+                       preq->preauth = NULL;
+               }
+       }
 
        status = smbXsrv_session_logoff(session);
+       return status;
+}
+
+static int smbXsrv_session_destructor(struct smbXsrv_session *session)
+{
+       NTSTATUS status;
+
+       status = smbXsrv_session_clear_and_logoff(session);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(0, ("smbXsrv_session_destructor: "
                          "smbXsrv_session_logoff() failed: %s\n",
@@ -1083,7 +1219,7 @@ NTSTATUS smbXsrv_session_create(struct smbXsrv_connection *conn,
        void *ptr = NULL;
        TDB_DATA val;
        struct smbXsrv_session_global0 *global = NULL;
-       struct smbXsrv_channel_global0 *channels = NULL;
+       struct smbXsrv_channel_global0 *channel = NULL;
        NTSTATUS status;
 
        if (table->local.num_sessions >= table->local.max_sessions) {
@@ -1098,6 +1234,7 @@ NTSTATUS smbXsrv_session_create(struct smbXsrv_connection *conn,
        session->idle_time = now;
        session->status = NT_STATUS_MORE_PROCESSING_REQUIRED;
        session->client = conn->client;
+       session->homes_snum = -1;
 
        status = smbXsrv_session_global_allocate(table->global.db_ctx,
                                                 session,
@@ -1110,8 +1247,6 @@ NTSTATUS smbXsrv_session_create(struct smbXsrv_connection *conn,
 
        if (conn->protocol >= PROTOCOL_SMB2_02) {
                uint64_t id = global->session_global_id;
-               uint8_t key_buf[SMBXSRV_SESSION_LOCAL_TDB_KEY_SIZE];
-               TDB_DATA key;
 
                global->connection_dialect = conn->smb2.server.dialect;
 
@@ -1125,10 +1260,10 @@ NTSTATUS smbXsrv_session_create(struct smbXsrv_connection *conn,
 
                session->local_id = global->session_global_id;
 
-               key = smbXsrv_session_local_id_to_key(session->local_id, key_buf);
-
-               local_rec = dbwrap_fetch_locked(table->local.db_ctx,
-                                               session, key);
+               local_rec = smbXsrv_session_local_fetch_locked(
+                                               table->local.db_ctx,
+                                               session->local_id,
+                                               session /* TALLOC_CTX */);
                if (local_rec == NULL) {
                        TALLOC_FREE(session);
                        return NT_STATUS_NO_MEMORY;
@@ -1158,36 +1293,11 @@ NTSTATUS smbXsrv_session_create(struct smbXsrv_connection *conn,
        global->creation_time = now;
        global->expiration_time = GENSEC_EXPIRE_TIME_INFINITY;
 
-       global->num_channels = 1;
-       channels = talloc_zero_array(global,
-                                    struct smbXsrv_channel_global0,
-                                    global->num_channels);
-       if (channels == NULL) {
-               TALLOC_FREE(session);
-               return NT_STATUS_NO_MEMORY;
-       }
-       global->channels = channels;
-
-       channels[0].server_id = messaging_server_id(conn->msg_ctx);
-       channels[0].local_address = tsocket_address_string(conn->local_address,
-                                                          channels);
-       if (channels[0].local_address == NULL) {
-               TALLOC_FREE(session);
-               return NT_STATUS_NO_MEMORY;
-       }
-       channels[0].remote_address = tsocket_address_string(conn->remote_address,
-                                                           channels);
-       if (channels[0].remote_address == NULL) {
-               TALLOC_FREE(session);
-               return NT_STATUS_NO_MEMORY;
-       }
-       channels[0].remote_name = talloc_strdup(channels, conn->remote_hostname);
-       if (channels[0].remote_name == NULL) {
+       status = smbXsrv_session_add_channel(session, conn, &channel);
+       if (!NT_STATUS_IS_OK(status)) {
                TALLOC_FREE(session);
-               return NT_STATUS_NO_MEMORY;
+               return status;
        }
-       channels[0].signing_key = data_blob_null;
-       channels[0].connection = conn;
 
        ptr = session;
        val = make_tdb_data((uint8_t const *)&ptr, sizeof(ptr));
@@ -1212,11 +1322,10 @@ NTSTATUS smbXsrv_session_create(struct smbXsrv_connection *conn,
        }
 
        if (DEBUGLVL(10)) {
-               struct smbXsrv_sessionB session_blob;
-
-               ZERO_STRUCT(session_blob);
-               session_blob.version = SMBXSRV_VERSION_0;
-               session_blob.info.info0 = session;
+               struct smbXsrv_sessionB session_blob = {
+                       .version = SMBXSRV_VERSION_0,
+                       .info.info0 = session,
+               };
 
                DEBUG(10,("smbXsrv_session_create: global_id (0x%08x) stored\n",
                         session->global->session_global_id));
@@ -1227,12 +1336,60 @@ NTSTATUS smbXsrv_session_create(struct smbXsrv_connection *conn,
        return NT_STATUS_OK;
 }
 
+NTSTATUS smbXsrv_session_add_channel(struct smbXsrv_session *session,
+                                    struct smbXsrv_connection *conn,
+                                    struct smbXsrv_channel_global0 **_c)
+{
+       struct smbXsrv_session_global0 *global = session->global;
+       struct smbXsrv_channel_global0 *c = NULL;
+
+       if (global->num_channels > 31) {
+               /*
+                * Windows 2012 and 2012R2 allow up to 32 channels
+                */
+               return NT_STATUS_INSUFFICIENT_RESOURCES;
+       }
+
+       c = talloc_realloc(global,
+                          global->channels,
+                          struct smbXsrv_channel_global0,
+                          global->num_channels + 1);
+       if (c == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
+       global->channels = c;
+
+       c = &global->channels[global->num_channels];
+       ZERO_STRUCTP(c);
+
+       c->server_id = messaging_server_id(conn->client->msg_ctx);
+       c->local_address = tsocket_address_string(conn->local_address,
+                                                 global->channels);
+       if (c->local_address == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
+       c->remote_address = tsocket_address_string(conn->remote_address,
+                                                  global->channels);
+       if (c->remote_address == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
+       c->remote_name = talloc_strdup(global->channels,
+                                      conn->remote_hostname);
+       if (c->remote_name == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
+       c->connection = conn;
+
+       global->num_channels += 1;
+
+       *_c = c;
+       return NT_STATUS_OK;
+}
+
 NTSTATUS smbXsrv_session_update(struct smbXsrv_session *session)
 {
        struct smbXsrv_session_table *table = session->table;
        NTSTATUS status;
-       uint8_t key_buf[SMBXSRV_SESSION_GLOBAL_TDB_KEY_SIZE];
-       TDB_DATA key;
 
        if (session->global->db_rec != NULL) {
                DEBUG(0, ("smbXsrv_session_update(0x%08x): "
@@ -1241,18 +1398,11 @@ NTSTATUS smbXsrv_session_update(struct smbXsrv_session *session)
                return NT_STATUS_INTERNAL_ERROR;
        }
 
-       key = smbXsrv_session_global_id_to_key(
+       session->global->db_rec = smbXsrv_session_global_fetch_locked(
+                                       table->global.db_ctx,
                                        session->global->session_global_id,
-                                       key_buf);
-
-       session->global->db_rec = dbwrap_fetch_locked(table->global.db_ctx,
-                                                     session->global, key);
+                                       session->global /* TALLOC_CTX */);
        if (session->global->db_rec == NULL) {
-               DEBUG(0, ("smbXsrv_session_update(0x%08x): "
-                         "Failed to lock global key '%s'\n",
-                         session->global->session_global_id,
-                         hex_encode_talloc(talloc_tos(), key.dptr,
-                                           key.dsize)));
                return NT_STATUS_INTERNAL_DB_ERROR;
        }
 
@@ -1266,11 +1416,10 @@ NTSTATUS smbXsrv_session_update(struct smbXsrv_session *session)
        }
 
        if (DEBUGLVL(10)) {
-               struct smbXsrv_sessionB session_blob;
-
-               ZERO_STRUCT(session_blob);
-               session_blob.version = SMBXSRV_VERSION_0;
-               session_blob.info.info0 = session;
+               struct smbXsrv_sessionB session_blob = {
+                       .version = SMBXSRV_VERSION_0,
+                       .info.info0 = session,
+               };
 
                DEBUG(10,("smbXsrv_session_update: global_id (0x%08x) stored\n",
                          session->global->session_global_id));
@@ -1298,6 +1447,201 @@ NTSTATUS smbXsrv_session_find_channel(const struct smbXsrv_session *session,
        return NT_STATUS_USER_SESSION_DELETED;
 }
 
+NTSTATUS smbXsrv_session_find_auth(const struct smbXsrv_session *session,
+                                  const struct smbXsrv_connection *conn,
+                                  NTTIME now,
+                                  struct smbXsrv_session_auth0 **_a)
+{
+       struct smbXsrv_session_auth0 *a;
+
+       for (a = session->pending_auth; a != NULL; a = a->next) {
+               if (a->connection == conn) {
+                       if (now != 0) {
+                               a->idle_time = now;
+                       }
+                       *_a = a;
+                       return NT_STATUS_OK;
+               }
+       }
+
+       return NT_STATUS_USER_SESSION_DELETED;
+}
+
+static int smbXsrv_session_auth0_destructor(struct smbXsrv_session_auth0 *a)
+{
+       if (a->session == NULL) {
+               return 0;
+       }
+
+       DLIST_REMOVE(a->session->pending_auth, a);
+       a->session = NULL;
+       return 0;
+}
+
+NTSTATUS smbXsrv_session_create_auth(struct smbXsrv_session *session,
+                                    struct smbXsrv_connection *conn,
+                                    NTTIME now,
+                                    uint8_t in_flags,
+                                    uint8_t in_security_mode,
+                                    struct smbXsrv_session_auth0 **_a)
+{
+       struct smbXsrv_session_auth0 *a;
+       NTSTATUS status;
+
+       status = smbXsrv_session_find_auth(session, conn, 0, &a);
+       if (NT_STATUS_IS_OK(status)) {
+               return NT_STATUS_INTERNAL_ERROR;
+       }
+
+       a = talloc_zero(session, struct smbXsrv_session_auth0);
+       if (a == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
+       a->session = session;
+       a->connection = conn;
+       a->in_flags = in_flags;
+       a->in_security_mode = in_security_mode;
+       a->creation_time = now;
+       a->idle_time = now;
+
+       if (conn->protocol >= PROTOCOL_SMB3_10) {
+               a->preauth = talloc(a, struct smbXsrv_preauth);
+               if (a->preauth == NULL) {
+                       TALLOC_FREE(session);
+                       return NT_STATUS_NO_MEMORY;
+               }
+               *a->preauth = conn->smb2.preauth;
+       }
+
+       talloc_set_destructor(a, smbXsrv_session_auth0_destructor);
+       DLIST_ADD_END(session->pending_auth, a);
+
+       *_a = a;
+       return NT_STATUS_OK;
+}
+
+struct smb2srv_session_shutdown_state {
+       struct tevent_queue *wait_queue;
+};
+
+static void smb2srv_session_shutdown_wait_done(struct tevent_req *subreq);
+
+struct tevent_req *smb2srv_session_shutdown_send(TALLOC_CTX *mem_ctx,
+                                       struct tevent_context *ev,
+                                       struct smbXsrv_session *session,
+                                       struct smbd_smb2_request *current_req)
+{
+       struct tevent_req *req;
+       struct smb2srv_session_shutdown_state *state;
+       struct tevent_req *subreq;
+       struct smbXsrv_connection *xconn = NULL;
+       size_t len = 0;
+
+       /*
+        * Make sure that no new request will be able to use this session.
+        */
+       session->status = NT_STATUS_USER_SESSION_DELETED;
+
+       req = tevent_req_create(mem_ctx, &state,
+                               struct smb2srv_session_shutdown_state);
+       if (req == NULL) {
+               return NULL;
+       }
+
+       state->wait_queue = tevent_queue_create(state, "smb2srv_session_shutdown_queue");
+       if (tevent_req_nomem(state->wait_queue, req)) {
+               return tevent_req_post(req, ev);
+       }
+
+       for (xconn = session->client->connections; xconn != NULL; xconn = xconn->next) {
+               struct smbd_smb2_request *preq;
+
+               for (preq = xconn->smb2.requests; preq != NULL; preq = preq->next) {
+                       if (preq == current_req) {
+                               /* Can't cancel current request. */
+                               continue;
+                       }
+                       if (preq->session != session) {
+                               /* Request on different session. */
+                               continue;
+                       }
+
+                       if (!NT_STATUS_IS_OK(xconn->transport.status)) {
+                               preq->session = NULL;
+                               /*
+                                * If we no longer have a session we can't
+                                * sign or encrypt replies.
+                                */
+                               preq->do_signing = false;
+                               preq->do_encryption = false;
+                               preq->preauth = NULL;
+
+                               if (preq->subreq != NULL) {
+                                       tevent_req_cancel(preq->subreq);
+                               }
+                               continue;
+                       }
+
+                       /*
+                        * Never cancel anything in a compound
+                        * request. Way too hard to deal with
+                        * the result.
+                        */
+                       if (!preq->compound_related && preq->subreq != NULL) {
+                               tevent_req_cancel(preq->subreq);
+                       }
+
+                       /*
+                        * Now wait until the request is finished.
+                        *
+                        * We don't set a callback, as we just want to block the
+                        * wait queue and the talloc_free() of the request will
+                        * remove the item from the wait queue.
+                        */
+                       subreq = tevent_queue_wait_send(preq, ev, state->wait_queue);
+                       if (tevent_req_nomem(subreq, req)) {
+                               return tevent_req_post(req, ev);
+                       }
+               }
+       }
+
+       len = tevent_queue_length(state->wait_queue);
+       if (len == 0) {
+               tevent_req_done(req);
+               return tevent_req_post(req, ev);
+       }
+
+       /*
+        * Now we add our own waiter to the end of the queue,
+        * this way we get notified when all pending requests are finished
+        * and send to the socket.
+        */
+       subreq = tevent_queue_wait_send(state, ev, state->wait_queue);
+       if (tevent_req_nomem(subreq, req)) {
+               return tevent_req_post(req, ev);
+       }
+       tevent_req_set_callback(subreq, smb2srv_session_shutdown_wait_done, req);
+
+       return req;
+}
+
+static void smb2srv_session_shutdown_wait_done(struct tevent_req *subreq)
+{
+       struct tevent_req *req =
+               tevent_req_callback_data(subreq,
+               struct tevent_req);
+
+       tevent_queue_wait_recv(subreq);
+       TALLOC_FREE(subreq);
+
+       tevent_req_done(req);
+}
+
+NTSTATUS smb2srv_session_shutdown_recv(struct tevent_req *req)
+{
+       return tevent_req_simple_recv_ntstatus(req);
+}
+
 NTSTATUS smbXsrv_session_logoff(struct smbXsrv_session *session)
 {
        struct smbXsrv_session_table *table;
@@ -1318,24 +1662,43 @@ NTSTATUS smbXsrv_session_logoff(struct smbXsrv_session *session)
        session->client = NULL;
        session->status = NT_STATUS_USER_SESSION_DELETED;
 
+       if (session->compat) {
+               /*
+                * For SMB2 this is a bit redundant as files are also close
+                * below via smb2srv_tcon_disconnect_all() -> ... ->
+                * smbXsrv_tcon_disconnect() -> close_cnum() ->
+                * file_close_conn().
+                */
+               file_close_user(sconn, session->compat->vuid);
+       }
+
+       if (session->tcon_table != NULL) {
+               /*
+                * Note: We only have a tcon_table for SMB2.
+                */
+               status = smb2srv_tcon_disconnect_all(session);
+               if (!NT_STATUS_IS_OK(status)) {
+                       DEBUG(0, ("smbXsrv_session_logoff(0x%08x): "
+                                 "smb2srv_tcon_disconnect_all() failed: %s\n",
+                                 session->global->session_global_id,
+                                 nt_errstr(status)));
+                       error = status;
+               }
+       }
+
+       if (session->compat) {
+               invalidate_vuid(sconn, session->compat->vuid);
+               session->compat = NULL;
+       }
+
        global_rec = session->global->db_rec;
        session->global->db_rec = NULL;
        if (global_rec == NULL) {
-               uint8_t key_buf[SMBXSRV_SESSION_GLOBAL_TDB_KEY_SIZE];
-               TDB_DATA key;
-
-               key = smbXsrv_session_global_id_to_key(
+               global_rec = smbXsrv_session_global_fetch_locked(
+                                       table->global.db_ctx,
                                        session->global->session_global_id,
-                                       key_buf);
-
-               global_rec = dbwrap_fetch_locked(table->global.db_ctx,
-                                                session->global, key);
+                                       session->global /* TALLOC_CTX */);
                if (global_rec == NULL) {
-                       DEBUG(0, ("smbXsrv_session_logoff(0x%08x): "
-                                 "Failed to lock global key '%s'\n",
-                                 session->global->session_global_id,
-                                 hex_encode_talloc(global_rec, key.dptr,
-                                                   key.dsize)));
                        error = NT_STATUS_INTERNAL_ERROR;
                }
        }
@@ -1358,20 +1721,11 @@ NTSTATUS smbXsrv_session_logoff(struct smbXsrv_session *session)
 
        local_rec = session->db_rec;
        if (local_rec == NULL) {
-               uint8_t key_buf[SMBXSRV_SESSION_LOCAL_TDB_KEY_SIZE];
-               TDB_DATA key;
-
-               key = smbXsrv_session_local_id_to_key(session->local_id,
-                                                     key_buf);
-
-               local_rec = dbwrap_fetch_locked(table->local.db_ctx,
-                                               session, key);
+               local_rec = smbXsrv_session_local_fetch_locked(
+                                               table->local.db_ctx,
+                                               session->local_id,
+                                               session /* TALLOC_CTX */);
                if (local_rec == NULL) {
-                       DEBUG(0, ("smbXsrv_session_logoff(0x%08x): "
-                                 "Failed to lock local key '%s'\n",
-                                 session->global->session_global_id,
-                                 hex_encode_talloc(local_rec, key.dptr,
-                                                   key.dsize)));
                        error = NT_STATUS_INTERNAL_ERROR;
                }
        }
@@ -1396,29 +1750,6 @@ NTSTATUS smbXsrv_session_logoff(struct smbXsrv_session *session)
        }
        session->db_rec = NULL;
 
-       if (session->compat) {
-               file_close_user(sconn, session->compat->vuid);
-       }
-
-       if (session->tcon_table != NULL) {
-               /*
-                * Note: We only have a tcon_table for SMB2.
-                */
-               status = smb2srv_tcon_disconnect_all(session);
-               if (!NT_STATUS_IS_OK(status)) {
-                       DEBUG(0, ("smbXsrv_session_logoff(0x%08x): "
-                                 "smb2srv_tcon_disconnect_all() failed: %s\n",
-                                 session->global->session_global_id,
-                                 nt_errstr(status)));
-                       error = status;
-               }
-       }
-
-       if (session->compat) {
-               invalidate_vuid(sconn, session->compat->vuid);
-               session->compat = NULL;
-       }
-
        return error;
 }
 
@@ -1430,9 +1761,9 @@ struct smbXsrv_session_logoff_all_state {
 static int smbXsrv_session_logoff_all_callback(struct db_record *local_rec,
                                               void *private_data);
 
-NTSTATUS smbXsrv_session_logoff_all(struct smbXsrv_connection *conn)
+NTSTATUS smbXsrv_session_logoff_all(struct smbXsrv_client *client)
 {
-       struct smbXsrv_session_table *table = conn->client->session_table;
+       struct smbXsrv_session_table *table = client->session_table;
        struct smbXsrv_session_logoff_all_state state;
        NTSTATUS status;
        int count = 0;
@@ -1490,7 +1821,8 @@ static int smbXsrv_session_logoff_all_callback(struct db_record *local_rec,
        session = talloc_get_type_abort(ptr, struct smbXsrv_session);
 
        session->db_rec = local_rec;
-       status = smbXsrv_session_logoff(session);
+
+       status = smbXsrv_session_clear_and_logoff(session);
        if (!NT_STATUS_IS_OK(status)) {
                if (NT_STATUS_IS_OK(state->first_status)) {
                        state->first_status = status;
@@ -1518,7 +1850,8 @@ NTSTATUS smb1srv_session_lookup(struct smbXsrv_connection *conn,
        struct smbXsrv_session_table *table = conn->client->session_table;
        uint32_t local_id = vuid;
 
-       return smbXsrv_session_local_lookup(table, local_id, now, session);
+       return smbXsrv_session_local_lookup(table, conn, local_id, now,
+                                           session);
 }
 
 NTSTATUS smb2srv_session_table_init(struct smbXsrv_connection *conn)
@@ -1531,6 +1864,8 @@ NTSTATUS smb2srv_session_table_init(struct smbXsrv_connection *conn)
 }
 
 static NTSTATUS smb2srv_session_lookup_raw(struct smbXsrv_session_table *table,
+                                          /* conn: optional */
+                                          struct smbXsrv_connection *conn,
                                           uint64_t session_id, NTTIME now,
                                           struct smbXsrv_session **session)
 {
@@ -1541,15 +1876,26 @@ static NTSTATUS smb2srv_session_lookup_raw(struct smbXsrv_session_table *table,
                return NT_STATUS_USER_SESSION_DELETED;
        }
 
-       return smbXsrv_session_local_lookup(table, local_id, now, session);
+       return smbXsrv_session_local_lookup(table, conn, local_id, now,
+                                           session);
 }
 
-NTSTATUS smb2srv_session_lookup(struct smbXsrv_connection *conn,
-                               uint64_t session_id, NTTIME now,
-                               struct smbXsrv_session **session)
+NTSTATUS smb2srv_session_lookup_conn(struct smbXsrv_connection *conn,
+                                    uint64_t session_id, NTTIME now,
+                                    struct smbXsrv_session **session)
 {
        struct smbXsrv_session_table *table = conn->client->session_table;
-       return smb2srv_session_lookup_raw(table, session_id, now, session);
+       return smb2srv_session_lookup_raw(table, conn, session_id, now,
+                                         session);
+}
+
+NTSTATUS smb2srv_session_lookup_client(struct smbXsrv_client *client,
+                                      uint64_t session_id, NTTIME now,
+                                      struct smbXsrv_session **session)
+{
+       struct smbXsrv_session_table *table = client->session_table;
+       return smb2srv_session_lookup_raw(table, NULL, session_id, now,
+                                         session);
 }
 
 struct smbXsrv_session_global_traverse_state {
@@ -1581,7 +1927,7 @@ static int smbXsrv_session_global_traverse_fn(struct db_record *rec, void *data)
 
        if (global_blob.version != SMBXSRV_VERSION_0) {
                DEBUG(1,("Invalid record in smbXsrv_session_global.tdb:"
-                        "key '%s' unsuported version - %d\n",
+                        "key '%s' unsupported version - %d\n",
                         hex_encode_talloc(frame, key.dptr, key.dsize),
                         (int)global_blob.version));
                goto done;
@@ -1607,7 +1953,7 @@ NTSTATUS smbXsrv_session_global_traverse(
        };
 
        become_root();
-       status = smbXsrv_session_global_init();
+       status = smbXsrv_session_global_init(NULL);
        if (!NT_STATUS_IS_OK(status)) {
                unbecome_root();
                DEBUG(0, ("Failed to initialize session_global: %s\n",