TODO s3:smbd: add smbXsrv_open* infrastructure
authorStefan Metzmacher <metze@samba.org>
Mon, 6 Feb 2012 22:06:41 +0000 (23:06 +0100)
committerStefan Metzmacher <metze@samba.org>
Fri, 1 Jun 2012 09:53:52 +0000 (11:53 +0200)
Pair-Programmed-With: Michael Adam <obnox@samba.org>

metze

was 2f3a019148b2bbcee7273a281cc4f4cafb7baf46

source3/Makefile.in
source3/smbd/globals.h
source3/smbd/server.c
source3/smbd/smbXsrv_open.c [new file with mode: 0644]
source3/wscript_build

index f901afb1146dc13175f3f2545c799c9c84a0fcdc..190ab2c6de26d2d5ee0e03dfcea2c41a24630eff 100644 (file)
@@ -969,6 +969,7 @@ SMBD_OBJ_SRV = smbd/server_reload.o \
               smbd/smbXsrv_version.o \
               smbd/smbXsrv_session.o \
               smbd/smbXsrv_tcon.o \
+              smbd/smbXsrv_open.o \
               $(MANGLE_OBJ) @VFS_STATIC@
 
 SMBD_OBJ_BASE = $(PARAM_WITHOUT_REG_OBJ) $(SMBD_OBJ_SRV) $(LIBSMB_OBJ) \
index fb2158e82d1829c056231e034a201324e3cac898..2003c6c8df793d05637433ddca01e23aa6f81df6 100644 (file)
@@ -354,6 +354,7 @@ struct smbXsrv_connection {
 
        struct smbXsrv_session_table *session_table;
        struct smbXsrv_tcon_table *tcon_table;
+       struct smbXsrv_open_table *open_table;
 };
 
 NTSTATUS smbXsrv_version_global_init(const struct server_id *server_id);
@@ -404,6 +405,27 @@ NTSTATUS smb2srv_tcon_lookup(struct smbXsrv_session *session,
                             struct smbXsrv_tcon **tcon);
 NTSTATUS smb2srv_tcon_disconnect_all(struct smbXsrv_session *session);
 
+NTSTATUS smbXsrv_open_global_init(void);
+NTSTATUS smbXsrv_open_create(struct smbXsrv_connection *conn,
+                            struct auth_session_info *session_info,
+                            NTTIME now,
+                            struct smbXsrv_open **_open);
+NTSTATUS smbXsrv_open_update(struct smbXsrv_open *_open);
+NTSTATUS smb1srv_open_table_init(struct smbXsrv_connection *conn);
+NTSTATUS smb1srv_open_lookup(struct smbXsrv_connection *conn,
+                            uint16_t fnum, NTTIME now,
+                            struct smbXsrv_open **_open);
+NTSTATUS smb2srv_open_table_init(struct smbXsrv_connection *conn);
+NTSTATUS smb2srv_open_lookup(struct smbXsrv_connection *conn,
+                            uint64_t volatile_id,
+                            uint64_t persistent_id,
+                            NTTIME now,
+                            struct smbXsrv_open **_open);
+NTSTATUS smb2srv_open_recreate(struct smbXsrv_connection *conn,
+                              struct auth_session_info *session_info,
+                              uint64_t persistent_id,
+                              struct smbXsrv_open **_open);
+
 struct smbd_smb2_request {
        struct smbd_smb2_request *prev, *next;
 
index 0f353b5f09d6046e6cb7cedd111272ebed68bb00..4bf2ff0a03f1136a1addde952477fd7e92b5878f 100644 (file)
@@ -1442,6 +1442,10 @@ extern void build_options(bool screen);
                DEBUG(0, ("ERROR: file_init_global() failed\n"));
                return -1;
        }
+       status = smbXsrv_open_global_init();
+       if (!NT_STATUS_IS_OK(status)) {
+               exit(1);
+       }
 
        /* This MUST be done before start_epmd() because otherwise
         * start_epmd() forks and races against dcesrv_ep_setup() to
diff --git a/source3/smbd/smbXsrv_open.c b/source3/smbd/smbXsrv_open.c
new file mode 100644 (file)
index 0000000..4690c6c
--- /dev/null
@@ -0,0 +1,921 @@
+/*
+   Unix SMB/CIFS implementation.
+
+   Copyright (C) Stefan Metzmacher 2012
+   Copyright (C) Michael Adam 2012
+
+   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 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   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, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "includes.h"
+#include "system/filesys.h"
+#include "smbd/smbd.h"
+#include "smbd/globals.h"
+#include "dbwrap/dbwrap.h"
+#include "dbwrap/dbwrap_rbt.h"
+#include "dbwrap/dbwrap_open.h"
+#include "session.h"
+#include "auth.h"
+#include "../lib/tsocket/tsocket.h"
+#include "../libcli/security/security.h"
+#include "messages.h"
+#include "lib/util/util_tdb.h"
+#include "librpc/gen_ndr/ndr_smbXsrv.h"
+
+struct smbXsrv_open_table {
+       struct {
+               struct db_context *db_ctx;
+               uint32_t lowest_id;
+               uint32_t highest_id;
+               uint32_t num_opens;
+       } local;
+       struct {
+               struct db_context *db_ctx;
+       } global;
+};
+
+static struct db_context *smbXsrv_open_global_db_ctx = NULL;
+
+/*
+ * NOTE:
+ * We need to store the keys in big endian so that dbwrap_rbt's memcmp
+ * has the same result as integer comparison between the uint32_t
+ * values.
+ *
+ * TODO: implement string based key
+ */
+
+#define SMBXSRV_OPEN_GLOBAL_TDB_KEY_SIZE sizeof(uint32_t)
+
+static TDB_DATA smbXsrv_open_global_id_to_key(uint32_t id,
+                                             uint8_t *key_buf)
+{
+       TDB_DATA key;
+
+       RSIVAL(key_buf, 0, id);
+
+       key = make_tdb_data(key_buf, SMBXSRV_OPEN_GLOBAL_TDB_KEY_SIZE);
+
+       return key;
+}
+
+#if 0
+static NTSTATUS smbXsrv_open_global_key_to_id(TDB_DATA key, uint32_t *id)
+{
+       if (id == NULL) {
+               return NT_STATUS_INVALID_PARAMETER;
+       }
+
+       if (key.dsize != SMBXSRV_OPEN_GLOBAL_TDB_KEY_SIZE){
+               return NT_STATUS_INTERNAL_DB_CORRUPTION;
+       }
+
+       *id = RIVAL(key.dptr, 0);
+
+       return NT_STATUS_OK;
+}
+#endif
+
+#define SMBXSRV_OPEN_LOCAL_TDB_KEY_SIZE sizeof(uint32_t)
+
+static TDB_DATA smbXsrv_open_local_id_to_key(uint32_t id,
+                                            uint8_t *key_buf)
+{
+       TDB_DATA key;
+
+       RSIVAL(key_buf, 0, id);
+
+       key = make_tdb_data(key_buf, SMBXSRV_OPEN_LOCAL_TDB_KEY_SIZE);
+
+       return key;
+}
+
+static NTSTATUS smbXsrv_open_local_key_to_id(TDB_DATA key, uint32_t *id)
+{
+       if (id == NULL) {
+               return NT_STATUS_INVALID_PARAMETER;
+       }
+
+       if (key.dsize != SMBXSRV_OPEN_LOCAL_TDB_KEY_SIZE){
+               return NT_STATUS_INTERNAL_DB_CORRUPTION;
+       }
+
+       *id = RIVAL(key.dptr, 0);
+
+       return NT_STATUS_OK;
+}
+
+NTSTATUS smbXsrv_open_global_init(void)
+{
+       const char *global_path = NULL;
+       struct db_context *db_ctx = NULL;
+
+       if (smbXsrv_open_global_db_ctx != NULL) {
+               return NT_STATUS_OK;
+       }
+
+       global_path = lock_path("smbXsrv_open_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 /* TODO - correct lock order */);
+       if (db_ctx == NULL) {
+               NTSTATUS status;
+
+               status = map_nt_error_from_unix_common(errno);
+
+               return status;
+       }
+
+       smbXsrv_open_global_db_ctx = db_ctx;
+
+       return NT_STATUS_OK;
+}
+
+static NTSTATUS smbXsrv_open_table_init(struct smbXsrv_connection *conn,
+                                       uint32_t lowest_id,
+                                       uint32_t highest_id)
+{
+       struct smbXsrv_open_table *table;
+       NTSTATUS status;
+
+       table = talloc_zero(conn, struct smbXsrv_open_table);
+       if (table == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       table->local.db_ctx = db_open_rbt(table);
+       if (table->local.db_ctx == NULL) {
+               TALLOC_FREE(table);
+               return NT_STATUS_NO_MEMORY;
+       }
+       table->local.lowest_id = lowest_id;
+       table->local.highest_id = highest_id;
+
+       status = smbXsrv_open_global_init();
+       if (!NT_STATUS_IS_OK(status)) {
+               TALLOC_FREE(table);
+               return status;
+       }
+
+       table->global.db_ctx = smbXsrv_open_global_db_ctx;
+
+       conn->open_table = table;
+       return NT_STATUS_OK;
+}
+
+struct smbXsrv_open_local_allocate_state {
+       const uint32_t lowest_id;
+       const uint32_t highest_id;
+       uint32_t last_id;
+       uint32_t useable_id;
+       NTSTATUS status;
+};
+
+static int smbXsrv_open_local_allocate_traverse(struct db_record *rec,
+                                                  void *private_data)
+{
+       struct smbXsrv_open_local_allocate_state *state =
+               (struct smbXsrv_open_local_allocate_state *)private_data;
+       TDB_DATA key = dbwrap_record_get_key(rec);
+       uint32_t id = 0;
+       NTSTATUS status;
+
+       status = smbXsrv_open_local_key_to_id(key, &id);
+       if (!NT_STATUS_IS_OK(status)) {
+               //TODO errno?
+               state->status = status;
+               return -1;
+       }
+
+       if (id <= state->last_id) {
+               //TODO errno?
+               state->status = NT_STATUS_INTERNAL_DB_CORRUPTION;
+               return -1;
+       }
+       state->last_id = id;
+
+       if (id > state->useable_id) {
+               state->status = NT_STATUS_OK;
+               return -1;
+       }
+
+       state->useable_id +=1;
+       return 0;
+}
+
+static NTSTATUS smbXsrv_open_local_allocate_id(struct db_context *db,
+                                              uint32_t lowest_id,
+                                              uint32_t highest_id,
+                                              TALLOC_CTX *mem_ctx,
+                                              struct db_record **_rec,
+                                              uint32_t *_id)
+{
+       struct smbXsrv_open_local_allocate_state state = {
+               .lowest_id = lowest_id,
+               .highest_id = highest_id,
+               .last_id = 0,
+               .useable_id = lowest_id,
+               .status = NT_STATUS_INTERNAL_ERROR,
+       };
+       uint32_t i;
+       uint32_t range;
+       NTSTATUS status;
+       int count = 0;
+
+       *_rec = NULL;
+       *_id = 0;
+
+       if (lowest_id > highest_id) {
+               return NT_STATUS_INSUFFICIENT_RESOURCES;
+       }
+
+       range = (highest_id - lowest_id) + 1;
+
+       for (i = 0; i < range; i++) {
+               uint32_t id;
+               uint8_t key_buf[SMBXSRV_OPEN_LOCAL_TDB_KEY_SIZE];
+               TDB_DATA key;
+               TDB_DATA val;
+               struct db_record *rec = NULL;
+
+               id = generate_random() % range;
+               id += lowest_id;
+
+               if (id < lowest_id) {
+                       id = lowest_id;
+               }
+               if (id > highest_id) {
+                       id = highest_id;
+               }
+
+               key = smbXsrv_open_local_id_to_key(id, key_buf);
+
+               rec = dbwrap_fetch_locked(db, mem_ctx, key);
+               if (rec == NULL) {
+                       return NT_STATUS_INSUFFICIENT_RESOURCES;
+               }
+
+               val = dbwrap_record_get_value(rec);
+               if (val.dsize != 0) {
+                       TALLOC_FREE(rec);
+                       continue;
+               }
+
+               *_rec = rec;
+               *_id = id;
+               return NT_STATUS_OK;
+       }
+
+       status = dbwrap_traverse_read(db, smbXsrv_open_local_allocate_traverse,
+                                     &state, &count);
+       if (!NT_STATUS_EQUAL(status, NT_STATUS_INTERNAL_DB_CORRUPTION)) {
+               /*
+                * Here we really expect NT_STATUS_INTERNAL_DB_CORRUPTION!
+                *
+                * If we get anything else it is an error, because it
+                * means we did not manage to find a free slot in
+                * the db.
+                */
+               return NT_STATUS_INSUFFICIENT_RESOURCES;
+       }
+
+       if (NT_STATUS_IS_OK(state.status)) {
+               uint32_t id;
+               uint8_t key_buf[SMBXSRV_OPEN_LOCAL_TDB_KEY_SIZE];
+               TDB_DATA key;
+               TDB_DATA val;
+               struct db_record *rec = NULL;
+
+               id = state.useable_id;
+
+               key = smbXsrv_open_local_id_to_key(id, key_buf);
+
+               rec = dbwrap_fetch_locked(db, mem_ctx, key);
+               if (rec == NULL) {
+                       return NT_STATUS_INSUFFICIENT_RESOURCES;
+               }
+
+               val = dbwrap_record_get_value(rec);
+               if (val.dsize != 0) {
+                       TALLOC_FREE(rec);
+                       return NT_STATUS_INTERNAL_DB_CORRUPTION;
+               }
+
+               *_rec = rec;
+               *_id = id;
+               return NT_STATUS_OK;
+       }
+
+       return state.status;
+}
+
+struct smbXsrv_open_local_fetch_state {
+       struct smbXsrv_open *op;
+       NTSTATUS status;
+};
+
+static void smbXsrv_open_local_fetch_parser(TDB_DATA key, TDB_DATA data,
+                                           void *private_data)
+{
+       struct smbXsrv_open_local_fetch_state *state =
+               (struct smbXsrv_open_local_fetch_state *)private_data;
+       void *ptr;
+
+       if (data.dsize != sizeof(ptr)) {
+               state->status = NT_STATUS_INTERNAL_DB_ERROR;
+               return;
+       }
+
+       memcpy(&ptr, data.dptr, data.dsize);
+       state->op = talloc_get_type_abort(ptr, struct smbXsrv_open);
+       state->status = NT_STATUS_OK;
+}
+
+static NTSTATUS smbXsrv_open_local_lookup(struct smbXsrv_open_table *table,
+                                         uint32_t open_local_id,
+                                         uint32_t open_global_id,
+                                         NTTIME now,
+                                         struct smbXsrv_open **_open)
+{
+       struct smbXsrv_open_local_fetch_state state = {
+               .op = NULL,
+               .status = NT_STATUS_INTERNAL_ERROR,
+       };
+       uint8_t key_buf[SMBXSRV_OPEN_LOCAL_TDB_KEY_SIZE];
+       TDB_DATA key;
+       NTSTATUS status;
+
+       *_open = NULL;
+
+       if (open_local_id == 0) {
+               return NT_STATUS_USER_SESSION_DELETED;
+       }
+
+       if (table == NULL) {
+               /* this might happen before the end of negprot */
+               return NT_STATUS_FILE_CLOSED;
+       }
+
+       if (table->local.db_ctx == NULL) {
+               return NT_STATUS_INTERNAL_ERROR;
+       }
+
+       key = smbXsrv_open_local_id_to_key(open_local_id, key_buf);
+
+       status = dbwrap_parse_record(table->local.db_ctx, key,
+                                    smbXsrv_open_local_fetch_parser,
+                                    &state);
+       if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
+               return NT_STATUS_FILE_CLOSED;
+       } else if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+       if (!NT_STATUS_IS_OK(state.status)) {
+               return state.status;
+       }
+
+       if (open_global_id == 0) {
+               /* make the global check a no-op for SMB1 */
+               open_global_id = state.op->global->open_global_id;
+       }
+
+       if (state.op->global->open_global_id != open_global_id) {
+               return NT_STATUS_FILE_CLOSED;
+       }
+
+       *_open = state.op;
+       return NT_STATUS_OK;
+}
+
+static int smbXsrv_open_global_destructor(struct smbXsrv_open_global0 *global)
+{
+       return 0;
+}
+
+static NTSTATUS smbXsrv_open_global_allocate(struct db_context *db,
+                                       TALLOC_CTX *mem_ctx,
+                                       struct smbXsrv_open_global0 **_global)
+{
+       uint32_t i;
+       struct smbXsrv_open_global0 *global = NULL;
+
+       *_global = NULL;
+
+       global = talloc_zero(mem_ctx, struct smbXsrv_open_global0);
+       if (global == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
+       talloc_set_destructor(global, smbXsrv_open_global_destructor);
+
+       for (i = 0; i < UINT32_MAX; i++) {
+               uint32_t id;
+               uint8_t key_buf[SMBXSRV_OPEN_GLOBAL_TDB_KEY_SIZE];
+               TDB_DATA key;
+               TDB_DATA val;
+
+               id = generate_random();
+               if (id == 0) {
+                       id++;
+               }
+               if (id == UINT32_MAX) {
+                       id--;
+               }
+
+               key = smbXsrv_open_global_id_to_key(id, key_buf);
+
+               global->db_rec = dbwrap_fetch_locked(db, mem_ctx, key);
+               if (global->db_rec == NULL) {
+                       talloc_free(global);
+                       return NT_STATUS_INSUFFICIENT_RESOURCES;
+               }
+
+               val = dbwrap_record_get_value(global->db_rec);
+               if (val.dsize != 0) {
+                       // TODO verify record or cleanup
+                       TALLOC_FREE(global->db_rec);
+                       continue;
+               }
+
+               global->open_global_id = id;
+
+               *_global = global;
+               return NT_STATUS_OK;
+       }
+
+       /* should not be reached */
+       talloc_free(global);
+       return NT_STATUS_INTERNAL_ERROR;
+}
+
+static NTSTATUS smbXsrv_open_global_store(struct smbXsrv_open_global0 *global)
+{
+       struct smbXsrv_open_globalB global_blob;
+       DATA_BLOB blob = data_blob_null;
+       TDB_DATA val;
+       NTSTATUS status;
+       enum ndr_err_code ndr_err;
+
+       /*
+        * TODO: if we use other versions than '0'
+        * we would add glue code here, that would be able to
+        * store the information in the old format.
+        */
+
+       if (global->db_rec == NULL) {
+               return NT_STATUS_INTERNAL_ERROR;
+       }
+
+       val = dbwrap_record_get_value(global->db_rec);
+
+       ZERO_STRUCT(global_blob);
+       global_blob.version = smbXsrv_version_global_current();
+       if (val.dsize >= 8) {
+               global_blob.seqnum = IVAL(val.dptr, 4);
+       }
+       global_blob.seqnum += 1;
+       global_blob.info.info0 = global;
+
+       DEBUG(0,("%s:%s:\n", __location__, __FUNCTION__));
+       NDR_PRINT_DEBUG(smbXsrv_open_globalB, &global_blob);
+
+       ndr_err = ndr_push_struct_blob(&blob, global->db_rec, &global_blob,
+                       (ndr_push_flags_fn_t)ndr_push_smbXsrv_open_globalB);
+       if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
+               //status = ndr_err_code;
+               TALLOC_FREE(global->db_rec);
+               //status =  map...
+               return status;
+       }
+
+       val = make_tdb_data(blob.data, blob.length);
+       status = dbwrap_record_store(global->db_rec, val, TDB_REPLACE);
+       TALLOC_FREE(global->db_rec);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+
+       return NT_STATUS_OK;
+}
+
+
+static NTSTATUS smbXsrv_open_global_lookup(struct smbXsrv_open_table *table,
+                                          uint32_t open_global_id,
+                                          TALLOC_CTX *mem_ctx,
+                                          struct smbXsrv_open_global0 **_global)
+{
+       TDB_DATA key, val;
+       uint8_t key_buf[SMBXSRV_OPEN_GLOBAL_TDB_KEY_SIZE];
+       NTSTATUS status;
+       DATA_BLOB blob;
+       struct smbXsrv_open_globalB global_blob;
+       enum ndr_err_code ndr_err;
+       struct db_record *global_rec = NULL;
+
+       *_global = NULL;
+
+       DEBUG(0,("%s:%s:\n", __location__, __FUNCTION__));
+
+       if (table->global.db_ctx == NULL) {
+       DEBUG(0,("%s:%s:\n", __location__, __FUNCTION__));
+               return NT_STATUS_INTERNAL_ERROR;
+       }
+
+       key = smbXsrv_open_global_id_to_key(open_global_id, key_buf);
+
+       global_rec = dbwrap_fetch_locked(table->global.db_ctx, mem_ctx, key);
+       if (global_rec == NULL) {
+               return NT_STATUS_INSUFFICIENT_RESOURCES;
+       }
+
+       val = dbwrap_record_get_value(global_rec);
+       if (val.dsize == 0) {
+               talloc_free(global_rec);
+               return NT_STATUS_OBJECT_NAME_NOT_FOUND;
+       }
+
+       blob = data_blob_const(val.dptr, val.dsize);
+
+       ndr_err = ndr_pull_struct_blob(&blob, global_rec, &global_blob,
+                       (ndr_pull_flags_fn_t)ndr_pull_smbXsrv_open_globalB);
+       if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
+               talloc_free(global_rec);
+               // status = map;
+               return status;
+       }
+
+       DEBUG(0,("%s:%s:\n", __location__, __FUNCTION__));
+       NDR_PRINT_DEBUG(smbXsrv_open_globalB, &global_blob);
+
+       // TODO: verify server_id
+
+       *_global = talloc_move(mem_ctx, &global_blob.info.info0);
+       (*_global)->db_rec = talloc_move(*_global, &global_rec);
+
+       talloc_set_destructor(*_global, smbXsrv_open_global_destructor);
+
+       DEBUG(0,("%s:%s:\n", __location__, __FUNCTION__));
+       return NT_STATUS_OK;
+}
+
+static int smbXsrv_open_destructor(struct smbXsrv_open *op)
+{
+       struct smbXsrv_open_table *table;
+       struct db_record *local_rec = NULL;
+       struct db_record *global_rec = NULL;
+       NTSTATUS status;
+
+       if (op->table == NULL) {
+               return 0;
+       }
+
+       table = op->table;
+       op->table = NULL;
+
+       local_rec = op->db_rec;
+       op->db_rec = NULL;
+       if (local_rec == NULL) {
+               uint8_t key_buf[SMBXSRV_OPEN_LOCAL_TDB_KEY_SIZE];
+               TDB_DATA key;
+
+               key = smbXsrv_open_local_id_to_key(op->local_id, key_buf);
+
+               local_rec = dbwrap_fetch_locked(table->local.db_ctx,
+                                               op, key);
+       }
+
+       if (local_rec != NULL) {
+               status = dbwrap_record_delete(local_rec);
+       }
+
+       global_rec = op->global->db_rec;
+       op->global->db_rec = NULL;
+       if (global_rec == NULL && !op->global->durable) {
+               uint8_t key_buf[SMBXSRV_OPEN_GLOBAL_TDB_KEY_SIZE];
+               TDB_DATA key;
+
+               key = smbXsrv_open_global_id_to_key(op->global->open_global_id,
+                                                   key_buf);
+
+               global_rec = dbwrap_fetch_locked(table->global.db_ctx,
+                                                op->global, key);
+       }
+
+       if (global_rec != NULL) {
+               status = dbwrap_record_delete(global_rec);
+       }
+       TALLOC_FREE(op->global);
+
+       return 0;
+}
+
+NTSTATUS smbXsrv_open_create(struct smbXsrv_connection *conn,
+                            struct auth_session_info *session_info,
+                            NTTIME now,
+                            struct smbXsrv_open **_open)
+{
+       struct smbXsrv_open_table *table = conn->open_table;
+       uint32_t max_opens = table->local.highest_id - table->local.lowest_id;
+       struct db_record *local_rec = NULL;
+       struct smbXsrv_open *op = NULL;
+       void *ptr = NULL;
+       TDB_DATA val;
+       struct smbXsrv_open_global0 *global = NULL;
+       NTSTATUS status;
+       struct dom_sid *current_sid = NULL;
+       struct security_token *current_token = NULL;
+
+       if (session_info == NULL) {
+               return NT_STATUS_INVALID_HANDLE;
+       }
+       current_token = session_info->security_token;
+
+       if (current_token == NULL) {
+               return NT_STATUS_INVALID_HANDLE;
+       }
+
+       if (current_token->num_sids > PRIMARY_USER_SID_INDEX) {
+               current_sid = &current_token->sids[PRIMARY_USER_SID_INDEX];
+       }
+
+       if (current_sid == NULL) {
+               return NT_STATUS_INVALID_HANDLE;
+       }
+
+       if (table->local.num_opens >= max_opens) {
+               return NT_STATUS_INSUFFICIENT_RESOURCES;
+       }
+
+       op = talloc_zero(table, struct smbXsrv_open);
+       if (op == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
+       op->table = table;
+
+       status = smbXsrv_open_global_allocate(table->global.db_ctx,
+                                             op, &global);
+       if (!NT_STATUS_IS_OK(status)) {
+               talloc_free(op);
+               return status;
+       }
+       op->global = global;
+
+       talloc_set_destructor(op, smbXsrv_open_destructor);
+
+       status = smbXsrv_open_local_allocate_id(table->local.db_ctx,
+                                               table->local.lowest_id,
+                                               table->local.highest_id,
+                                               op,
+                                               &local_rec,
+                                               &op->local_id);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+
+       global->open_persistent_id = global->open_global_id;
+       global->open_volatile_id = op->local_id;
+
+       ptr = op;
+       val = make_tdb_data((uint8_t const *)&ptr, sizeof(ptr));
+       status = dbwrap_record_store(local_rec, val, TDB_REPLACE);
+       TALLOC_FREE(local_rec);
+       if (!NT_STATUS_IS_OK(status)) {
+               talloc_free(op);
+               return status;
+       }
+
+       global->server_id = messaging_server_id(conn->msg_ctx);
+       global->open_time = now;
+       global->open_path = "TODO...";
+       global->open_owner = *current_sid;
+
+       status = smbXsrv_open_global_store(global);
+       if (!NT_STATUS_IS_OK(status)) {
+               talloc_free(op);
+               return status;
+       }
+
+       {
+               struct smbXsrv_openB open_blob;
+
+               ZERO_STRUCT(open_blob);
+               open_blob.version = SMBXSRV_VERSION_0;
+               open_blob.info.info0 = op;
+
+               DEBUG(0,("%s:%s:\n", __location__, __FUNCTION__));
+               NDR_PRINT_DEBUG(smbXsrv_openB, &open_blob);
+       }
+
+       *_open = op;
+       return NT_STATUS_OK;
+}
+
+NTSTATUS smbXsrv_open_update(struct smbXsrv_open *op)
+{
+       struct smbXsrv_open_table *table = op->table;
+       NTSTATUS status;
+       uint8_t key_buf[SMBXSRV_OPEN_GLOBAL_TDB_KEY_SIZE];
+       TDB_DATA key;
+
+       if (op->global->db_rec != NULL) {
+               return NT_STATUS_INTERNAL_ERROR;
+       }
+
+       key = smbXsrv_open_global_id_to_key(op->global->open_global_id,
+                                           key_buf);
+
+       op->global->db_rec = dbwrap_fetch_locked(table->global.db_ctx,
+                                                op->global, key);
+       if (op->global->db_rec == NULL) {
+               // TODO proper return code?
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       status = smbXsrv_open_global_store(op->global);
+       if (!NT_STATUS_IS_OK(status)) {
+               /* debug */
+               return status;
+       }
+
+       {
+               struct smbXsrv_openB open_blob;
+
+               ZERO_STRUCT(open_blob);
+               open_blob.version = SMBXSRV_VERSION_0;
+               open_blob.info.info0 = op;
+
+               DEBUG(0,("%s:%s:\n", __location__, __FUNCTION__));
+               NDR_PRINT_DEBUG(smbXsrv_openB, &open_blob);
+       }
+
+       return NT_STATUS_OK;
+}
+
+NTSTATUS smb1srv_open_table_init(struct smbXsrv_connection *conn)
+{
+       return smbXsrv_open_table_init(conn, 1,
+                                      conn->sconn->real_max_open_files);
+}
+
+NTSTATUS smb1srv_open_lookup(struct smbXsrv_connection *conn,
+                            uint16_t fnum, NTTIME now,
+                            struct smbXsrv_open **_open)
+{
+       struct smbXsrv_open_table *table = conn->open_table;
+       uint32_t local_id = fnum;
+       uint32_t global_id = 0;
+
+       return smbXsrv_open_local_lookup(table, local_id, global_id, now, _open);
+}
+
+NTSTATUS smb2srv_open_table_init(struct smbXsrv_connection *conn)
+{
+       return smbXsrv_open_table_init(conn, 1,
+                                      conn->sconn->real_max_open_files);
+}
+
+NTSTATUS smb2srv_open_lookup(struct smbXsrv_connection *conn,
+                            uint64_t volatile_id,
+                            uint64_t persistent_id,
+                            NTTIME now,
+                            struct smbXsrv_open **_open)
+{
+       struct smbXsrv_open_table *table = conn->open_table;
+       uint32_t local_id = volatile_id & UINT32_MAX;
+       uint64_t local_zeros = volatile_id & 0xFFFFFFFF00000000LLU;
+       uint32_t global_id = persistent_id & UINT32_MAX;
+       uint64_t global_zeros = persistent_id & 0xFFFFFFFF00000000LLU;
+
+       if (local_zeros != 0) {
+               return NT_STATUS_FILE_CLOSED;
+       }
+
+       if (global_zeros != 0) {
+               return NT_STATUS_FILE_CLOSED;
+       }
+
+       if (global_id == 0) {
+               return NT_STATUS_FILE_CLOSED;
+       }
+
+       return smbXsrv_open_local_lookup(table, local_id, global_id, now, _open);
+}
+
+NTSTATUS smb2srv_open_recreate(struct smbXsrv_connection *conn,
+                              struct auth_session_info *session_info,
+                              uint64_t persistent_id,
+                              struct smbXsrv_open **_open)
+{
+       struct smbXsrv_open_table *table = conn->open_table;
+       uint32_t max_opens = table->local.highest_id - table->local.lowest_id;
+       struct db_record *local_rec = NULL;
+       struct smbXsrv_open *op = NULL;
+       void *ptr = NULL;
+       TDB_DATA val;
+       uint32_t global_id = persistent_id & UINT32_MAX;
+       uint64_t global_zeros = persistent_id & 0xFFFFFFFF00000000LLU;
+       NTSTATUS status;
+       struct security_token *current_token = NULL;
+
+       if (session_info == NULL) {
+               return NT_STATUS_INVALID_HANDLE;
+       }
+       current_token = session_info->security_token;
+
+       if (current_token == NULL) {
+               return NT_STATUS_INVALID_HANDLE;
+       }
+
+       if (global_zeros != 0) {
+               return NT_STATUS_OBJECT_NAME_NOT_FOUND;
+       }
+
+       op = talloc_zero(table, struct smbXsrv_open);
+       if (op == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
+       op->table = table;
+
+       status = smbXsrv_open_global_lookup(table, global_id, op, &op->global);
+       if (!NT_STATUS_IS_OK(status)) {
+               talloc_free(op);
+               return status;
+       }
+
+       if (!security_token_is_sid(current_token, &op->global->open_owner)) {
+               talloc_free(op);
+               return NT_STATUS_OBJECT_NAME_NOT_FOUND;
+       }
+
+       if (!op->global->durable) {
+               talloc_free(op);
+               return NT_STATUS_OBJECT_NAME_NOT_FOUND;
+       }
+
+       // TODO: smbXsrv_open_destructor will delete the global record...
+       // at what stage should that happen?
+       talloc_set_destructor(op, smbXsrv_open_destructor);
+
+       if (table->local.num_opens >= max_opens) {
+               talloc_free(op);
+               return NT_STATUS_INSUFFICIENT_RESOURCES;
+       }
+
+       status = smbXsrv_open_local_allocate_id(table->local.db_ctx,
+                                               table->local.lowest_id,
+                                               table->local.highest_id,
+                                               op,
+                                               &local_rec,
+                                               &op->local_id);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+
+       op->global->open_volatile_id = op->local_id;
+
+       ptr = op;
+       val = make_tdb_data((uint8_t const *)&ptr, sizeof(ptr));
+       status = dbwrap_record_store(local_rec, val, TDB_REPLACE);
+       TALLOC_FREE(local_rec);
+       if (!NT_STATUS_IS_OK(status)) {
+               talloc_free(op);
+               return status;
+       }
+
+       op->global->server_id = messaging_server_id(conn->msg_ctx);
+
+       status = smbXsrv_open_global_store(op->global);
+       if (!NT_STATUS_IS_OK(status)) {
+               talloc_free(op);
+               return status;
+       }
+
+       {
+               struct smbXsrv_openB open_blob;
+
+               ZERO_STRUCT(open_blob);
+               open_blob.version = 0;
+               open_blob.info.info0 = op;
+
+               DEBUG(0,("%s:%s:\n", __location__, __FUNCTION__));
+               NDR_PRINT_DEBUG(smbXsrv_openB, &open_blob);
+       }
+
+       *_open = op;
+       return NT_STATUS_OK;
+}
index 11e2fba12601f5e9c14c3c04905bb8ffd1f9bcb0..a949f4d55009dbe34cc2203bd119c94db670183e 100755 (executable)
@@ -378,6 +378,7 @@ SMBD_SRC_SRV = '''smbd/server_reload.c smbd/files.c smbd/connection.c
                smbd/smbXsrv_version.c
                smbd/smbXsrv_session.c
                smbd/smbXsrv_tcon.c
+               smbd/smbXsrv_open.c
                smbd/server_exit.c
                ${MANGLE_SRC}'''