2 Unix SMB/CIFS implementation.
4 Copyright (C) Stefan Metzmacher 2012
5 Copyright (C) Michael Adam 2012
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "smbXsrv_open.h"
23 #include "system/filesys.h"
24 #include "lib/util/server_id.h"
25 #include "smbd/smbd.h"
26 #include "smbd/globals.h"
27 #include "dbwrap/dbwrap.h"
28 #include "dbwrap/dbwrap_rbt.h"
29 #include "dbwrap/dbwrap_open.h"
30 #include "../libcli/security/security.h"
32 #include "lib/util/util_tdb.h"
33 #include "librpc/gen_ndr/ndr_smbXsrv.h"
35 #include "source3/include/util_tdb.h"
37 struct smbXsrv_open_table {
39 struct db_context *db_ctx;
40 struct db_context *replay_cache_db_ctx;
47 struct db_context *db_ctx;
51 static struct db_context *smbXsrv_open_global_db_ctx = NULL;
53 NTSTATUS smbXsrv_open_global_init(void)
55 char *global_path = NULL;
56 struct db_context *db_ctx = NULL;
58 if (smbXsrv_open_global_db_ctx != NULL) {
62 global_path = lock_path(talloc_tos(), "smbXsrv_open_global.tdb");
63 if (global_path == NULL) {
64 return NT_STATUS_NO_MEMORY;
67 db_ctx = db_open(NULL, global_path,
68 SMBD_VOLATILE_TDB_HASH_SIZE,
69 SMBD_VOLATILE_TDB_FLAGS,
70 O_RDWR | O_CREAT, 0600,
73 TALLOC_FREE(global_path);
77 status = map_nt_error_from_unix_common(errno);
82 smbXsrv_open_global_db_ctx = db_ctx;
89 * We need to store the keys in big endian so that dbwrap_rbt's memcmp
90 * has the same result as integer comparison between the uint32_t
93 * TODO: implement string based key
96 #define SMBXSRV_OPEN_GLOBAL_TDB_KEY_SIZE sizeof(uint32_t)
98 static TDB_DATA smbXsrv_open_global_id_to_key(uint32_t id,
103 RSIVAL(key_buf, 0, id);
105 key = make_tdb_data(key_buf, SMBXSRV_OPEN_GLOBAL_TDB_KEY_SIZE);
111 static NTSTATUS smbXsrv_open_global_key_to_id(TDB_DATA key, uint32_t *id)
114 return NT_STATUS_INVALID_PARAMETER;
117 if (key.dsize != SMBXSRV_OPEN_GLOBAL_TDB_KEY_SIZE) {
118 return NT_STATUS_INTERNAL_DB_CORRUPTION;
121 *id = RIVAL(key.dptr, 0);
127 #define SMBXSRV_OPEN_LOCAL_TDB_KEY_SIZE sizeof(uint32_t)
129 static TDB_DATA smbXsrv_open_local_id_to_key(uint32_t id,
134 RSIVAL(key_buf, 0, id);
136 key = make_tdb_data(key_buf, SMBXSRV_OPEN_LOCAL_TDB_KEY_SIZE);
141 static NTSTATUS smbXsrv_open_local_key_to_id(TDB_DATA key, uint32_t *id)
144 return NT_STATUS_INVALID_PARAMETER;
147 if (key.dsize != SMBXSRV_OPEN_LOCAL_TDB_KEY_SIZE) {
148 return NT_STATUS_INTERNAL_DB_CORRUPTION;
151 *id = RIVAL(key.dptr, 0);
156 static struct db_record *smbXsrv_open_global_fetch_locked(
157 struct db_context *db,
162 uint8_t key_buf[SMBXSRV_OPEN_GLOBAL_TDB_KEY_SIZE];
163 struct db_record *rec = NULL;
165 key = smbXsrv_open_global_id_to_key(id, key_buf);
167 rec = dbwrap_fetch_locked(db, mem_ctx, key);
170 DBG_DEBUG("Failed to lock global id 0x%08x, key '%s'\n", id,
177 static struct db_record *smbXsrv_open_local_fetch_locked(
178 struct db_context *db,
183 uint8_t key_buf[SMBXSRV_OPEN_LOCAL_TDB_KEY_SIZE];
184 struct db_record *rec = NULL;
186 key = smbXsrv_open_local_id_to_key(id, key_buf);
188 rec = dbwrap_fetch_locked(db, mem_ctx, key);
191 DBG_DEBUG("Failed to lock local id 0x%08x, key '%s'\n", id,
198 static NTSTATUS smbXsrv_open_table_init(struct smbXsrv_connection *conn,
203 struct smbXsrv_client *client = conn->client;
204 struct smbXsrv_open_table *table;
208 if (lowest_id > highest_id) {
209 return NT_STATUS_INTERNAL_ERROR;
212 max_range = highest_id;
213 max_range -= lowest_id;
216 if (max_opens > max_range) {
217 return NT_STATUS_INTERNAL_ERROR;
220 table = talloc_zero(client, struct smbXsrv_open_table);
222 return NT_STATUS_NO_MEMORY;
225 table->local.db_ctx = db_open_rbt(table);
226 if (table->local.db_ctx == NULL) {
228 return NT_STATUS_NO_MEMORY;
230 table->local.replay_cache_db_ctx = db_open_rbt(table);
231 if (table->local.replay_cache_db_ctx == NULL) {
233 return NT_STATUS_NO_MEMORY;
235 table->local.lowest_id = lowest_id;
236 table->local.highest_id = highest_id;
237 table->local.max_opens = max_opens;
239 status = smbXsrv_open_global_init();
240 if (!NT_STATUS_IS_OK(status)) {
245 table->global.db_ctx = smbXsrv_open_global_db_ctx;
247 client->open_table = table;
251 struct smbXsrv_open_local_allocate_state {
252 const uint32_t lowest_id;
253 const uint32_t highest_id;
259 static int smbXsrv_open_local_allocate_traverse(struct db_record *rec,
262 struct smbXsrv_open_local_allocate_state *state =
263 (struct smbXsrv_open_local_allocate_state *)private_data;
264 TDB_DATA key = dbwrap_record_get_key(rec);
268 status = smbXsrv_open_local_key_to_id(key, &id);
269 if (!NT_STATUS_IS_OK(status)) {
270 state->status = status;
274 if (id <= state->last_id) {
275 state->status = NT_STATUS_INTERNAL_DB_CORRUPTION;
280 if (id > state->useable_id) {
281 state->status = NT_STATUS_OK;
285 if (state->useable_id == state->highest_id) {
286 state->status = NT_STATUS_INSUFFICIENT_RESOURCES;
290 state->useable_id +=1;
294 static NTSTATUS smbXsrv_open_local_allocate_id(struct db_context *db,
298 struct db_record **_rec,
301 struct smbXsrv_open_local_allocate_state state = {
302 .lowest_id = lowest_id,
303 .highest_id = highest_id,
305 .useable_id = lowest_id,
306 .status = NT_STATUS_INTERNAL_ERROR,
316 if (lowest_id > highest_id) {
317 return NT_STATUS_INSUFFICIENT_RESOURCES;
321 * first we try randomly
323 range = (highest_id - lowest_id) + 1;
325 for (i = 0; i < (range / 2); i++) {
328 struct db_record *rec = NULL;
330 id = generate_random() % range;
333 if (id < lowest_id) {
336 if (id > highest_id) {
340 rec = smbXsrv_open_local_fetch_locked(db, id, mem_ctx);
342 return NT_STATUS_INSUFFICIENT_RESOURCES;
345 val = dbwrap_record_get_value(rec);
346 if (val.dsize != 0) {
357 * if the range is almost full,
358 * we traverse the whole table
359 * (this relies on sorted behavior of dbwrap_rbt)
361 status = dbwrap_traverse_read(db, smbXsrv_open_local_allocate_traverse,
363 if (NT_STATUS_IS_OK(status)) {
364 if (NT_STATUS_IS_OK(state.status)) {
365 return NT_STATUS_INTERNAL_ERROR;
368 if (!NT_STATUS_EQUAL(state.status, NT_STATUS_INTERNAL_ERROR)) {
372 if (state.useable_id <= state.highest_id) {
373 state.status = NT_STATUS_OK;
375 return NT_STATUS_INSUFFICIENT_RESOURCES;
377 } else if (!NT_STATUS_EQUAL(status, NT_STATUS_INTERNAL_DB_CORRUPTION)) {
379 * Here we really expect NT_STATUS_INTERNAL_DB_CORRUPTION!
381 * If we get anything else it is an error, because it
382 * means we did not manage to find a free slot in
385 return NT_STATUS_INSUFFICIENT_RESOURCES;
388 if (NT_STATUS_IS_OK(state.status)) {
391 struct db_record *rec = NULL;
393 id = state.useable_id;
395 rec = smbXsrv_open_local_fetch_locked(db, id, mem_ctx);
397 return NT_STATUS_INSUFFICIENT_RESOURCES;
400 val = dbwrap_record_get_value(rec);
401 if (val.dsize != 0) {
403 return NT_STATUS_INTERNAL_DB_CORRUPTION;
414 struct smbXsrv_open_local_fetch_state {
415 struct smbXsrv_open *op;
419 static void smbXsrv_open_local_fetch_parser(TDB_DATA key, TDB_DATA data,
422 struct smbXsrv_open_local_fetch_state *state =
423 (struct smbXsrv_open_local_fetch_state *)private_data;
426 if (data.dsize != sizeof(ptr)) {
427 state->status = NT_STATUS_INTERNAL_DB_ERROR;
431 memcpy(&ptr, data.dptr, data.dsize);
432 state->op = talloc_get_type_abort(ptr, struct smbXsrv_open);
433 state->status = NT_STATUS_OK;
436 static NTSTATUS smbXsrv_open_local_lookup(struct smbXsrv_open_table *table,
437 uint32_t open_local_id,
438 uint32_t open_global_id,
440 struct smbXsrv_open **_open)
442 struct smbXsrv_open_local_fetch_state state = {
444 .status = NT_STATUS_INTERNAL_ERROR,
446 uint8_t key_buf[SMBXSRV_OPEN_LOCAL_TDB_KEY_SIZE];
452 if (open_local_id == 0) {
453 return NT_STATUS_FILE_CLOSED;
457 /* this might happen before the end of negprot */
458 return NT_STATUS_FILE_CLOSED;
461 if (table->local.db_ctx == NULL) {
462 return NT_STATUS_INTERNAL_ERROR;
465 key = smbXsrv_open_local_id_to_key(open_local_id, key_buf);
467 status = dbwrap_parse_record(table->local.db_ctx, key,
468 smbXsrv_open_local_fetch_parser,
470 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
471 return NT_STATUS_FILE_CLOSED;
473 if (!NT_STATUS_IS_OK(status)) {
476 if (!NT_STATUS_IS_OK(state.status)) {
480 if (NT_STATUS_EQUAL(state.op->status, NT_STATUS_FILE_CLOSED)) {
481 return NT_STATUS_FILE_CLOSED;
484 if (open_global_id == 0) {
485 /* make the global check a no-op for SMB1 */
486 open_global_id = state.op->global->open_global_id;
489 if (state.op->global->open_global_id != open_global_id) {
490 return NT_STATUS_FILE_CLOSED;
494 state.op->idle_time = now;
498 return state.op->status;
501 static int smbXsrv_open_global_destructor(struct smbXsrv_open_global0 *global)
506 static void smbXsrv_open_global_verify_record(struct db_record *db_rec,
510 struct smbXsrv_open_global0 **_g);
512 static NTSTATUS smbXsrv_open_global_allocate(struct db_context *db,
514 struct smbXsrv_open_global0 **_global)
517 struct smbXsrv_open_global0 *global = NULL;
518 uint32_t last_free = 0;
519 const uint32_t min_tries = 3;
523 global = talloc_zero(mem_ctx, struct smbXsrv_open_global0);
524 if (global == NULL) {
525 return NT_STATUS_NO_MEMORY;
527 talloc_set_destructor(global, smbXsrv_open_global_destructor);
530 * We mark every slot as invalid using 0xFF.
531 * Valid values are masked with 0xF.
533 memset(global->lock_sequence_array, 0xFF,
534 sizeof(global->lock_sequence_array));
537 * Here we just randomly try the whole 32-bit space
539 * We use just 32-bit, because we want to reuse the
542 for (i = 0; i < UINT32_MAX; i++) {
543 bool is_free = false;
544 bool was_free = false;
547 if (i >= min_tries && last_free != 0) {
550 id = generate_random();
555 if (id == UINT32_MAX) {
559 global->db_rec = smbXsrv_open_global_fetch_locked(db, id, mem_ctx);
560 if (global->db_rec == NULL) {
562 return NT_STATUS_INSUFFICIENT_RESOURCES;
565 smbXsrv_open_global_verify_record(global->db_rec,
571 TALLOC_FREE(global->db_rec);
575 if (!was_free && i < min_tries) {
577 * The session_id is free now,
578 * but was not free before.
580 * This happens if a smbd crashed
581 * and did not cleanup the record.
583 * If this is one of our first tries,
584 * then we try to find a real free one.
586 if (last_free == 0) {
589 TALLOC_FREE(global->db_rec);
593 global->open_global_id = id;
599 /* should not be reached */
601 return NT_STATUS_INTERNAL_ERROR;
604 static void smbXsrv_open_global_verify_record(struct db_record *db_rec,
608 struct smbXsrv_open_global0 **_g)
613 struct smbXsrv_open_globalB global_blob;
614 enum ndr_err_code ndr_err;
615 struct smbXsrv_open_global0 *global = NULL;
617 TALLOC_CTX *frame = talloc_stackframe();
628 key = dbwrap_record_get_key(db_rec);
630 val = dbwrap_record_get_value(db_rec);
631 if (val.dsize == 0) {
632 DEBUG(10, ("%s: empty value\n", __func__));
641 blob = data_blob_const(val.dptr, val.dsize);
643 ndr_err = ndr_pull_struct_blob(&blob, frame, &global_blob,
644 (ndr_pull_flags_fn_t)ndr_pull_smbXsrv_open_globalB);
645 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
646 NTSTATUS status = ndr_map_error2ntstatus(ndr_err);
647 DEBUG(1,("smbXsrv_open_global_verify_record: "
648 "key '%s' ndr_pull_struct_blob - %s\n",
655 DEBUG(10,("smbXsrv_open_global_verify_record\n"));
656 if (CHECK_DEBUGLVL(10)) {
657 NDR_PRINT_DEBUG(smbXsrv_open_globalB, &global_blob);
660 if (global_blob.version != SMBXSRV_VERSION_0) {
661 DEBUG(0,("smbXsrv_open_global_verify_record: "
662 "key '%s' use unsupported version %u\n",
664 global_blob.version));
665 NDR_PRINT_DEBUG(smbXsrv_open_globalB, &global_blob);
670 global = global_blob.info.info0;
672 if (server_id_is_disconnected(&global->server_id)) {
675 exists = serverid_exists(&global->server_id);
678 struct server_id_buf idbuf;
679 DEBUG(2,("smbXsrv_open_global_verify_record: "
680 "key '%s' server_id %s does not exist.\n",
682 server_id_str_buf(global->server_id, &idbuf)));
683 if (CHECK_DEBUGLVL(2)) {
684 NDR_PRINT_DEBUG(smbXsrv_open_globalB, &global_blob);
687 dbwrap_record_delete(db_rec);
693 *_g = talloc_move(mem_ctx, &global);
698 static NTSTATUS smbXsrv_open_global_store(struct smbXsrv_open_global0 *global)
700 struct smbXsrv_open_globalB global_blob;
701 DATA_BLOB blob = data_blob_null;
705 enum ndr_err_code ndr_err;
708 * TODO: if we use other versions than '0'
709 * we would add glue code here, that would be able to
710 * store the information in the old format.
713 if (global->db_rec == NULL) {
714 return NT_STATUS_INTERNAL_ERROR;
717 key = dbwrap_record_get_key(global->db_rec);
718 val = dbwrap_record_get_value(global->db_rec);
720 global_blob = (struct smbXsrv_open_globalB) {
721 .version = smbXsrv_version_global_current(),
724 if (val.dsize >= 8) {
725 global_blob.seqnum = IVAL(val.dptr, 4);
727 global_blob.seqnum += 1;
728 global_blob.info.info0 = global;
730 ndr_err = ndr_push_struct_blob(&blob, global->db_rec, &global_blob,
731 (ndr_push_flags_fn_t)ndr_push_smbXsrv_open_globalB);
732 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
733 status = ndr_map_error2ntstatus(ndr_err);
734 DEBUG(1,("smbXsrv_open_global_store: key '%s' ndr_push - %s\n",
737 TALLOC_FREE(global->db_rec);
741 val = make_tdb_data(blob.data, blob.length);
742 status = dbwrap_record_store(global->db_rec, val, TDB_REPLACE);
743 if (!NT_STATUS_IS_OK(status)) {
744 DEBUG(1,("smbXsrv_open_global_store: key '%s' store - %s\n",
747 TALLOC_FREE(global->db_rec);
751 if (CHECK_DEBUGLVL(10)) {
752 DEBUG(10,("smbXsrv_open_global_store: key '%s' stored\n",
754 NDR_PRINT_DEBUG(smbXsrv_open_globalB, &global_blob);
757 TALLOC_FREE(global->db_rec);
762 static NTSTATUS smbXsrv_open_global_lookup(struct smbXsrv_open_table *table,
763 uint32_t open_global_id,
765 struct smbXsrv_open_global0 **_global)
767 struct db_record *global_rec = NULL;
768 bool is_free = false;
772 if (table->global.db_ctx == NULL) {
773 return NT_STATUS_INTERNAL_ERROR;
776 global_rec = smbXsrv_open_global_fetch_locked(table->global.db_ctx,
779 if (global_rec == NULL) {
780 return NT_STATUS_INTERNAL_DB_ERROR;
783 smbXsrv_open_global_verify_record(global_rec,
789 DEBUG(10, ("%s: is_free=true\n", __func__));
790 talloc_free(global_rec);
791 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
794 (*_global)->db_rec = talloc_move(*_global, &global_rec);
796 talloc_set_destructor(*_global, smbXsrv_open_global_destructor);
801 static int smbXsrv_open_destructor(struct smbXsrv_open *op)
805 status = smbXsrv_open_close(op, 0);
806 if (!NT_STATUS_IS_OK(status)) {
807 DEBUG(0, ("smbXsrv_open_destructor: "
808 "smbXsrv_open_close() failed - %s\n",
812 TALLOC_FREE(op->global);
817 NTSTATUS smbXsrv_open_create(struct smbXsrv_connection *conn,
818 struct auth_session_info *session_info,
820 struct smbXsrv_open **_open)
822 struct smbXsrv_open_table *table = conn->client->open_table;
823 struct db_record *local_rec = NULL;
824 struct smbXsrv_open *op = NULL;
827 struct smbXsrv_open_global0 *global = NULL;
829 struct dom_sid *current_sid = NULL;
830 struct security_token *current_token = NULL;
832 if (session_info == NULL) {
833 return NT_STATUS_INVALID_HANDLE;
835 current_token = session_info->security_token;
837 if (current_token == NULL) {
838 return NT_STATUS_INVALID_HANDLE;
841 if (current_token->num_sids > PRIMARY_USER_SID_INDEX) {
842 current_sid = ¤t_token->sids[PRIMARY_USER_SID_INDEX];
845 if (current_sid == NULL) {
846 return NT_STATUS_INVALID_HANDLE;
849 if (table->local.num_opens >= table->local.max_opens) {
850 return NT_STATUS_INSUFFICIENT_RESOURCES;
853 op = talloc_zero(table, struct smbXsrv_open);
855 return NT_STATUS_NO_MEMORY;
858 op->status = NT_STATUS_OK; /* TODO: start with INTERNAL_ERROR */
861 status = smbXsrv_open_global_allocate(table->global.db_ctx,
863 if (!NT_STATUS_IS_OK(status)) {
869 status = smbXsrv_open_local_allocate_id(table->local.db_ctx,
870 table->local.lowest_id,
871 table->local.highest_id,
875 if (!NT_STATUS_IS_OK(status)) {
880 global->open_persistent_id = global->open_global_id;
881 global->open_volatile_id = op->local_id;
883 global->server_id = messaging_server_id(conn->client->msg_ctx);
884 global->open_time = now;
885 global->open_owner = *current_sid;
886 if (conn->protocol >= PROTOCOL_SMB2_10) {
887 global->client_guid = conn->smb2.client.guid;
891 val = make_tdb_data((uint8_t const *)&ptr, sizeof(ptr));
892 status = dbwrap_record_store(local_rec, val, TDB_REPLACE);
893 TALLOC_FREE(local_rec);
894 if (!NT_STATUS_IS_OK(status)) {
898 table->local.num_opens += 1;
900 talloc_set_destructor(op, smbXsrv_open_destructor);
902 status = smbXsrv_open_global_store(global);
903 if (!NT_STATUS_IS_OK(status)) {
904 DEBUG(0,("smbXsrv_open_create: "
905 "global_id (0x%08x) store failed - %s\n",
906 op->global->open_global_id,
912 if (CHECK_DEBUGLVL(10)) {
913 struct smbXsrv_openB open_blob = {
914 .version = SMBXSRV_VERSION_0,
918 DEBUG(10,("smbXsrv_open_create: global_id (0x%08x) stored\n",
919 op->global->open_global_id));
920 NDR_PRINT_DEBUG(smbXsrv_openB, &open_blob);
927 static NTSTATUS smbXsrv_open_set_replay_cache(struct smbXsrv_open *op)
929 struct GUID *create_guid;
930 struct GUID_txt_buf buf;
932 struct db_context *db = op->table->local.replay_cache_db_ctx;
933 struct smbXsrv_open_replay_cache rc = {
934 .idle_time = op->idle_time,
935 .local_id = op->local_id,
937 uint8_t data[SMBXSRV_OPEN_REPLAY_CACHE_FIXED_SIZE] = { 0 };
938 DATA_BLOB blob = data_blob_const(data, ARRAY_SIZE(data));
939 enum ndr_err_code ndr_err;
944 if (!(op->flags & SMBXSRV_OPEN_NEED_REPLAY_CACHE)) {
948 if (op->flags & SMBXSRV_OPEN_HAVE_REPLAY_CACHE) {
952 create_guid = &op->global->create_guid;
953 guid_string = GUID_buf_string(create_guid, &buf);
954 key = string_term_tdb_data(guid_string);
956 ndr_err = ndr_push_struct_into_fixed_blob(&blob, &rc,
957 (ndr_push_flags_fn_t)ndr_push_smbXsrv_open_replay_cache);
958 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
959 status = ndr_map_error2ntstatus(ndr_err);
962 val = make_tdb_data(blob.data, blob.length);
964 status = dbwrap_store(db, key, val, TDB_REPLACE);
966 if (NT_STATUS_IS_OK(status)) {
967 op->flags |= SMBXSRV_OPEN_HAVE_REPLAY_CACHE;
968 op->flags &= ~SMBXSRV_OPEN_NEED_REPLAY_CACHE;
974 NTSTATUS smbXsrv_open_purge_replay_cache(struct smbXsrv_client *client,
975 const struct GUID *create_guid)
977 struct GUID_txt_buf buf;
979 struct db_context *db;
981 if (client->open_table == NULL) {
985 db = client->open_table->local.replay_cache_db_ctx;
987 guid_string = GUID_buf_string(create_guid, &buf);
988 if (guid_string == NULL) {
989 return NT_STATUS_INVALID_PARAMETER;
992 return dbwrap_purge_bystring(db, guid_string);
995 static NTSTATUS smbXsrv_open_clear_replay_cache(struct smbXsrv_open *op)
997 struct GUID *create_guid;
998 struct GUID_txt_buf buf;
1000 struct db_context *db;
1003 if (op->table == NULL) {
1004 return NT_STATUS_OK;
1007 db = op->table->local.replay_cache_db_ctx;
1009 if (!(op->flags & SMBXSRV_OPEN_HAVE_REPLAY_CACHE)) {
1010 return NT_STATUS_OK;
1013 create_guid = &op->global->create_guid;
1014 if (GUID_all_zero(create_guid)) {
1015 return NT_STATUS_OK;
1018 guid_string = GUID_buf_string(create_guid, &buf);
1019 if (guid_string == NULL) {
1020 return NT_STATUS_INVALID_PARAMETER;
1023 status = dbwrap_purge_bystring(db, guid_string);
1025 if (NT_STATUS_IS_OK(status)) {
1026 op->flags &= ~SMBXSRV_OPEN_HAVE_REPLAY_CACHE;
1032 NTSTATUS smbXsrv_open_update(struct smbXsrv_open *op)
1034 struct smbXsrv_open_table *table = op->table;
1037 if (op->global->db_rec != NULL) {
1038 DEBUG(0, ("smbXsrv_open_update(0x%08x): "
1039 "Called with db_rec != NULL'\n",
1040 op->global->open_global_id));
1041 return NT_STATUS_INTERNAL_ERROR;
1044 op->global->db_rec = smbXsrv_open_global_fetch_locked(
1045 table->global.db_ctx,
1046 op->global->open_global_id,
1047 op->global /* TALLOC_CTX */);
1048 if (op->global->db_rec == NULL) {
1049 return NT_STATUS_INTERNAL_DB_ERROR;
1052 status = smbXsrv_open_global_store(op->global);
1053 if (!NT_STATUS_IS_OK(status)) {
1054 DEBUG(0,("smbXsrv_open_update: "
1055 "global_id (0x%08x) store failed - %s\n",
1056 op->global->open_global_id,
1057 nt_errstr(status)));
1061 status = smbXsrv_open_set_replay_cache(op);
1062 if (!NT_STATUS_IS_OK(status)) {
1063 DBG_ERR("smbXsrv_open_set_replay_cache failed: %s\n",
1068 if (CHECK_DEBUGLVL(10)) {
1069 struct smbXsrv_openB open_blob = {
1070 .version = SMBXSRV_VERSION_0,
1074 DEBUG(10,("smbXsrv_open_update: global_id (0x%08x) stored\n",
1075 op->global->open_global_id));
1076 NDR_PRINT_DEBUG(smbXsrv_openB, &open_blob);
1079 return NT_STATUS_OK;
1082 NTSTATUS smbXsrv_open_close(struct smbXsrv_open *op, NTTIME now)
1084 struct smbXsrv_open_table *table;
1085 struct db_record *local_rec = NULL;
1086 struct db_record *global_rec = NULL;
1088 NTSTATUS error = NT_STATUS_OK;
1090 error = smbXsrv_open_clear_replay_cache(op);
1091 if (!NT_STATUS_IS_OK(error)) {
1092 DBG_ERR("smbXsrv_open_clear_replay_cache failed: %s\n",
1096 if (op->table == NULL) {
1103 op->status = NT_STATUS_FILE_CLOSED;
1104 op->global->disconnect_time = now;
1105 server_id_set_disconnected(&op->global->server_id);
1107 global_rec = op->global->db_rec;
1108 op->global->db_rec = NULL;
1109 if (global_rec == NULL) {
1110 global_rec = smbXsrv_open_global_fetch_locked(
1111 table->global.db_ctx,
1112 op->global->open_global_id,
1113 op->global /* TALLOC_CTX */);
1114 if (global_rec == NULL) {
1115 error = NT_STATUS_INTERNAL_ERROR;
1119 if (global_rec != NULL && op->global->durable) {
1121 * If it is a durable open we need to update the global part
1122 * instead of deleting it
1124 op->global->db_rec = global_rec;
1125 status = smbXsrv_open_global_store(op->global);
1126 if (NT_STATUS_IS_OK(status)) {
1128 * smbXsrv_open_global_store does the free
1129 * of op->global->db_rec
1133 if (!NT_STATUS_IS_OK(status)) {
1134 DEBUG(0,("smbXsrv_open_close(0x%08x)"
1135 "smbXsrv_open_global_store() failed - %s\n",
1136 op->global->open_global_id,
1137 nt_errstr(status)));
1141 if (NT_STATUS_IS_OK(status) && CHECK_DEBUGLVL(10)) {
1142 struct smbXsrv_openB open_blob = {
1143 .version = SMBXSRV_VERSION_0,
1147 DEBUG(10,("smbXsrv_open_close(0x%08x): "
1148 "stored disconnect\n",
1149 op->global->open_global_id));
1150 NDR_PRINT_DEBUG(smbXsrv_openB, &open_blob);
1154 if (global_rec != NULL) {
1155 status = dbwrap_record_delete(global_rec);
1156 if (!NT_STATUS_IS_OK(status)) {
1157 TDB_DATA key = dbwrap_record_get_key(global_rec);
1159 DEBUG(0, ("smbXsrv_open_close(0x%08x): "
1160 "failed to delete global key '%s': %s\n",
1161 op->global->open_global_id,
1163 nt_errstr(status)));
1167 TALLOC_FREE(global_rec);
1169 local_rec = op->db_rec;
1170 if (local_rec == NULL) {
1171 local_rec = smbXsrv_open_local_fetch_locked(table->local.db_ctx,
1173 op /* TALLOC_CTX*/);
1174 if (local_rec == NULL) {
1175 error = NT_STATUS_INTERNAL_ERROR;
1179 if (local_rec != NULL) {
1180 status = dbwrap_record_delete(local_rec);
1181 if (!NT_STATUS_IS_OK(status)) {
1182 TDB_DATA key = dbwrap_record_get_key(local_rec);
1184 DEBUG(0, ("smbXsrv_open_close(0x%08x): "
1185 "failed to delete local key '%s': %s\n",
1186 op->global->open_global_id,
1188 nt_errstr(status)));
1191 table->local.num_opens -= 1;
1193 if (op->db_rec == NULL) {
1194 TALLOC_FREE(local_rec);
1199 op->compat->op = NULL;
1200 file_free(NULL, op->compat);
1207 NTSTATUS smb1srv_open_table_init(struct smbXsrv_connection *conn)
1212 * Allow a range from 1..65534.
1214 * With real_max_open_files possible ids,
1215 * truncated to the SMB1 limit of 16-bit.
1217 * 0 and 0xFFFF are no valid ids.
1219 max_opens = conn->client->sconn->real_max_open_files;
1220 max_opens = MIN(max_opens, UINT16_MAX - 1);
1222 return smbXsrv_open_table_init(conn, 1, UINT16_MAX - 1, max_opens);
1225 NTSTATUS smb1srv_open_lookup(struct smbXsrv_connection *conn,
1226 uint16_t fnum, NTTIME now,
1227 struct smbXsrv_open **_open)
1229 struct smbXsrv_open_table *table = conn->client->open_table;
1230 uint32_t local_id = fnum;
1231 uint32_t global_id = 0;
1233 return smbXsrv_open_local_lookup(table, local_id, global_id, now, _open);
1236 NTSTATUS smb2srv_open_table_init(struct smbXsrv_connection *conn)
1241 * Allow a range from 1..4294967294.
1243 * With real_max_open_files possible ids,
1244 * truncated to 16-bit (the same as SMB1 for now).
1246 * 0 and 0xFFFFFFFF are no valid ids.
1248 * The usage of conn->sconn->real_max_open_files
1249 * is the reason that we use one open table per
1250 * transport connection (as we still have a 1:1 mapping
1251 * between process and transport connection).
1253 max_opens = conn->client->sconn->real_max_open_files;
1254 max_opens = MIN(max_opens, UINT16_MAX - 1);
1256 return smbXsrv_open_table_init(conn, 1, UINT32_MAX - 1, max_opens);
1259 NTSTATUS smb2srv_open_lookup(struct smbXsrv_connection *conn,
1260 uint64_t persistent_id,
1261 uint64_t volatile_id,
1263 struct smbXsrv_open **_open)
1265 struct smbXsrv_open_table *table = conn->client->open_table;
1266 uint32_t local_id = volatile_id & UINT32_MAX;
1267 uint64_t local_zeros = volatile_id & 0xFFFFFFFF00000000LLU;
1268 uint32_t global_id = persistent_id & UINT32_MAX;
1269 uint64_t global_zeros = persistent_id & 0xFFFFFFFF00000000LLU;
1272 if (local_zeros != 0) {
1273 return NT_STATUS_FILE_CLOSED;
1276 if (global_zeros != 0) {
1277 return NT_STATUS_FILE_CLOSED;
1280 if (global_id == 0) {
1281 return NT_STATUS_FILE_CLOSED;
1284 status = smbXsrv_open_local_lookup(table, local_id, global_id, now,
1286 if (!NT_STATUS_IS_OK(status)) {
1291 * Clear the replay cache for this create_guid if it exists:
1292 * This is based on the assumption that this lookup will be
1293 * triggered by a client request using the file-id for lookup.
1294 * Hence the client has proven that it has in fact seen the
1295 * reply to its initial create call. So subsequent create replays
1296 * should be treated as invalid. Hence the index for create_guid
1297 * lookup needs to be removed.
1299 status = smbXsrv_open_clear_replay_cache(*_open);
1305 * This checks or marks the replay cache, we have the following
1308 * 1. There is no record in the cache
1309 * => we add the passes caller_req_guid as holder_req_guid
1310 * together with local_id as 0.
1311 * => We return STATUS_FWP_RESERVED in order to indicate
1312 * that the caller holds the current reservation
1314 * 2. There is a record in the cache and holder_req_guid
1315 * is already the same as caller_req_guid and local_id is 0
1316 * => We return STATUS_FWP_RESERVED in order to indicate
1317 * that the caller holds the current reservation
1319 * 3. There is a record in the cache with a holder_req_guid
1320 * other than caller_req_guid (and local_id is 0):
1321 * => We return NT_STATUS_FILE_NOT_AVAILABLE to indicate
1322 * the original request is still pending
1324 * 4. There is a record in the cache with a zero holder_req_guid
1325 * and a valid local_id:
1326 * => We lookup the existing open by local_id
1327 * => We return NT_STATUS_OK together with the smbXsrv_open
1330 * With NT_STATUS_OK the caller can continue the replay processing.
1332 * With STATUS_FWP_RESERVED the caller should continue the normal
1335 * - smbXsrv_open_update()/smbXsrv_open_set_replay_cache()
1336 * will convert the record to a zero holder_req_guid
1337 * with a valid local_id.
1339 * - smbXsrv_open_purge_replay_cache() should cleanup
1342 * All other values should be returned to the client,
1343 * while NT_STATUS_FILE_NOT_AVAILABLE will trigger the
1344 * retry loop on the client.
1346 NTSTATUS smb2srv_open_lookup_replay_cache(struct smbXsrv_connection *conn,
1347 struct GUID caller_req_guid,
1348 struct GUID create_guid,
1351 struct smbXsrv_open **_open)
1353 TALLOC_CTX *frame = talloc_stackframe();
1355 struct smbXsrv_open_table *table = conn->client->open_table;
1356 struct db_context *db = table->local.replay_cache_db_ctx;
1357 struct GUID_txt_buf _create_guid_buf;
1358 struct GUID_txt_buf tmp_guid_buf;
1359 const char *create_guid_str = NULL;
1360 TDB_DATA create_guid_key;
1361 struct db_record *db_rec = NULL;
1362 struct smbXsrv_open *op = NULL;
1363 struct smbXsrv_open_replay_cache rc = {
1364 .holder_req_guid = caller_req_guid,
1368 enum ndr_err_code ndr_err;
1369 DATA_BLOB blob = data_blob_null;
1374 create_guid_str = GUID_buf_string(&create_guid, &_create_guid_buf);
1375 create_guid_key = string_term_tdb_data(create_guid_str);
1377 db_rec = dbwrap_fetch_locked(db, frame, create_guid_key);
1378 if (db_rec == NULL) {
1380 return NT_STATUS_INTERNAL_DB_ERROR;
1383 val = dbwrap_record_get_value(db_rec);
1384 if (val.dsize == 0) {
1385 uint8_t data[SMBXSRV_OPEN_REPLAY_CACHE_FIXED_SIZE];
1387 blob = data_blob_const(data, ARRAY_SIZE(data));
1388 ndr_err = ndr_push_struct_into_fixed_blob(&blob, &rc,
1389 (ndr_push_flags_fn_t)ndr_push_smbXsrv_open_replay_cache);
1390 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1391 status = ndr_map_error2ntstatus(ndr_err);
1396 val = make_tdb_data(blob.data, blob.length);
1397 status = dbwrap_record_store(db_rec, val, TDB_REPLACE);
1398 if (!NT_STATUS_IS_OK(status)) {
1404 * We're the new holder
1408 return NT_STATUS_FWP_RESERVED;
1411 if (val.dsize != SMBXSRV_OPEN_REPLAY_CACHE_FIXED_SIZE) {
1413 return NT_STATUS_INTERNAL_DB_CORRUPTION;
1416 blob = data_blob_const(val.dptr, val.dsize);
1417 ndr_err = ndr_pull_struct_blob_all_noalloc(&blob, &rc,
1418 (ndr_pull_flags_fn_t)ndr_pull_smbXsrv_open_replay_cache);
1419 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1420 status = ndr_map_error2ntstatus(ndr_err);
1424 if (rc.local_id != 0) {
1425 if (GUID_equal(&rc.holder_req_guid, &caller_req_guid)) {
1427 * This should not happen
1429 status = NT_STATUS_INTERNAL_ERROR;
1430 DBG_ERR("caller %s already holds local_id %u for create %s [%s] - %s\n",
1431 GUID_buf_string(&caller_req_guid, &tmp_guid_buf),
1432 (unsigned)rc.local_id,
1441 status = smbXsrv_open_local_lookup(table,
1446 if (!NT_STATUS_IS_OK(status)) {
1447 DBG_ERR("holder %s stale for local_id %u for create %s [%s] - %s\n",
1448 GUID_buf_string(&rc.holder_req_guid, &tmp_guid_buf),
1449 (unsigned)rc.local_id,
1459 * We found an open the caller can reuse.
1461 SMB_ASSERT(op != NULL);
1464 return NT_STATUS_OK;
1467 if (GUID_equal(&rc.holder_req_guid, &caller_req_guid)) {
1469 * We're still the holder
1473 return NT_STATUS_FWP_RESERVED;
1477 * The original request (or a former replay) is still
1478 * pending, ask the client to retry by sending FILE_NOT_AVAILABLE.
1480 status = NT_STATUS_FILE_NOT_AVAILABLE;
1481 DBG_DEBUG("holder %s still pending for create %s [%s] - %s\n",
1482 GUID_buf_string(&rc.holder_req_guid, &tmp_guid_buf),
1490 NTSTATUS smb2srv_open_recreate(struct smbXsrv_connection *conn,
1491 struct auth_session_info *session_info,
1492 uint64_t persistent_id,
1493 const struct GUID *create_guid,
1495 struct smbXsrv_open **_open)
1497 struct smbXsrv_open_table *table = conn->client->open_table;
1498 struct db_record *local_rec = NULL;
1499 struct smbXsrv_open *op = NULL;
1502 uint32_t global_id = persistent_id & UINT32_MAX;
1503 uint64_t global_zeros = persistent_id & 0xFFFFFFFF00000000LLU;
1505 struct security_token *current_token = NULL;
1507 if (session_info == NULL) {
1508 DEBUG(10, ("session_info=NULL\n"));
1509 return NT_STATUS_INVALID_HANDLE;
1511 current_token = session_info->security_token;
1513 if (current_token == NULL) {
1514 DEBUG(10, ("current_token=NULL\n"));
1515 return NT_STATUS_INVALID_HANDLE;
1518 if (global_zeros != 0) {
1519 DEBUG(10, ("global_zeros!=0\n"));
1520 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1523 op = talloc_zero(table, struct smbXsrv_open);
1525 return NT_STATUS_NO_MEMORY;
1529 status = smbXsrv_open_global_lookup(table, global_id, op, &op->global);
1530 if (!NT_STATUS_IS_OK(status)) {
1532 DEBUG(10, ("smbXsrv_open_global_lookup returned %s\n",
1533 nt_errstr(status)));
1538 * If the provided create_guid is NULL, this means that
1539 * the reconnect request was a v1 request. In that case
1540 * we should skipt the create GUID verification, since
1541 * it is valid to v1-reconnect a v2-opened handle.
1543 if ((create_guid != NULL) &&
1544 !GUID_equal(&op->global->create_guid, create_guid))
1547 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1550 if (!security_token_is_sid(current_token, &op->global->open_owner)) {
1552 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1555 if (!op->global->durable) {
1557 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1560 if (table->local.num_opens >= table->local.max_opens) {
1562 return NT_STATUS_INSUFFICIENT_RESOURCES;
1565 status = smbXsrv_open_local_allocate_id(table->local.db_ctx,
1566 table->local.lowest_id,
1567 table->local.highest_id,
1571 if (!NT_STATUS_IS_OK(status)) {
1576 op->idle_time = now;
1577 op->status = NT_STATUS_FILE_CLOSED;
1579 op->global->open_volatile_id = op->local_id;
1580 op->global->server_id = messaging_server_id(conn->client->msg_ctx);
1583 val = make_tdb_data((uint8_t const *)&ptr, sizeof(ptr));
1584 status = dbwrap_record_store(local_rec, val, TDB_REPLACE);
1585 TALLOC_FREE(local_rec);
1586 if (!NT_STATUS_IS_OK(status)) {
1590 table->local.num_opens += 1;
1592 talloc_set_destructor(op, smbXsrv_open_destructor);
1594 status = smbXsrv_open_global_store(op->global);
1595 if (!NT_STATUS_IS_OK(status)) {
1600 if (CHECK_DEBUGLVL(10)) {
1601 struct smbXsrv_openB open_blob = {
1605 DEBUG(10,("smbXsrv_open_recreate: global_id (0x%08x) stored\n",
1606 op->global->open_global_id));
1607 NDR_PRINT_DEBUG(smbXsrv_openB, &open_blob);
1611 return NT_STATUS_OK;
1615 static NTSTATUS smbXsrv_open_global_parse_record(TALLOC_CTX *mem_ctx,
1616 struct db_record *rec,
1617 struct smbXsrv_open_global0 **global)
1619 TDB_DATA key = dbwrap_record_get_key(rec);
1620 TDB_DATA val = dbwrap_record_get_value(rec);
1621 DATA_BLOB blob = data_blob_const(val.dptr, val.dsize);
1622 struct smbXsrv_open_globalB global_blob;
1623 enum ndr_err_code ndr_err;
1625 TALLOC_CTX *frame = talloc_stackframe();
1627 ndr_err = ndr_pull_struct_blob(&blob, frame, &global_blob,
1628 (ndr_pull_flags_fn_t)ndr_pull_smbXsrv_open_globalB);
1629 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1630 DEBUG(1,("Invalid record in smbXsrv_open_global.tdb:"
1631 "key '%s' ndr_pull_struct_blob - %s\n",
1633 ndr_errstr(ndr_err)));
1634 status = ndr_map_error2ntstatus(ndr_err);
1638 if (global_blob.version != SMBXSRV_VERSION_0) {
1639 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
1640 DEBUG(1,("Invalid record in smbXsrv_open_global.tdb:"
1641 "key '%s' unsupported version - %d - %s\n",
1643 (int)global_blob.version,
1644 nt_errstr(status)));
1648 if (global_blob.info.info0 == NULL) {
1649 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
1650 DEBUG(1,("Invalid record in smbXsrv_tcon_global.tdb:"
1651 "key '%s' info0 NULL pointer - %s\n",
1653 nt_errstr(status)));
1657 *global = talloc_move(mem_ctx, &global_blob.info.info0);
1658 status = NT_STATUS_OK;
1664 struct smbXsrv_open_global_traverse_state {
1665 int (*fn)(struct smbXsrv_open_global0 *, void *);
1669 static int smbXsrv_open_global_traverse_fn(struct db_record *rec, void *data)
1671 struct smbXsrv_open_global_traverse_state *state =
1672 (struct smbXsrv_open_global_traverse_state*)data;
1673 struct smbXsrv_open_global0 *global = NULL;
1677 status = smbXsrv_open_global_parse_record(talloc_tos(), rec, &global);
1678 if (!NT_STATUS_IS_OK(status)) {
1682 global->db_rec = rec;
1683 ret = state->fn(global, state->private_data);
1684 talloc_free(global);
1688 NTSTATUS smbXsrv_open_global_traverse(
1689 int (*fn)(struct smbXsrv_open_global0 *, void *),
1695 struct smbXsrv_open_global_traverse_state state = {
1697 .private_data = private_data,
1701 status = smbXsrv_open_global_init();
1702 if (!NT_STATUS_IS_OK(status)) {
1704 DEBUG(0, ("Failed to initialize open_global: %s\n",
1705 nt_errstr(status)));
1709 status = dbwrap_traverse_read(smbXsrv_open_global_db_ctx,
1710 smbXsrv_open_global_traverse_fn,
1718 NTSTATUS smbXsrv_open_cleanup(uint64_t persistent_id)
1720 NTSTATUS status = NT_STATUS_OK;
1721 TALLOC_CTX *frame = talloc_stackframe();
1722 struct smbXsrv_open_global0 *op = NULL;
1724 struct db_record *rec;
1725 bool delete_open = false;
1726 uint32_t global_id = persistent_id & UINT32_MAX;
1728 rec = smbXsrv_open_global_fetch_locked(smbXsrv_open_global_db_ctx,
1732 status = NT_STATUS_NOT_FOUND;
1736 val = dbwrap_record_get_value(rec);
1737 if (val.dsize == 0) {
1738 DEBUG(10, ("smbXsrv_open_cleanup[global: 0x%08x] "
1739 "empty record in %s, skipping...\n",
1740 global_id, dbwrap_name(smbXsrv_open_global_db_ctx)));
1744 status = smbXsrv_open_global_parse_record(talloc_tos(), rec, &op);
1745 if (!NT_STATUS_IS_OK(status)) {
1746 DEBUG(1, ("smbXsrv_open_cleanup[global: 0x%08x] "
1747 "failed to read record: %s\n",
1748 global_id, nt_errstr(status)));
1752 if (server_id_is_disconnected(&op->server_id)) {
1753 struct timeval now, disconnect_time;
1755 now = timeval_current();
1756 nttime_to_timeval(&disconnect_time, op->disconnect_time);
1757 tdiff = usec_time_diff(&now, &disconnect_time);
1758 delete_open = (tdiff >= 1000*op->durable_timeout_msec);
1760 DEBUG(10, ("smbXsrv_open_cleanup[global: 0x%08x] "
1761 "disconnected at [%s] %us ago with "
1762 "timeout of %us -%s reached\n",
1764 nt_time_string(frame, op->disconnect_time),
1765 (unsigned)(tdiff/1000000),
1766 op->durable_timeout_msec / 1000,
1767 delete_open ? "" : " not"));
1768 } else if (!serverid_exists(&op->server_id)) {
1769 struct server_id_buf idbuf;
1770 DEBUG(10, ("smbXsrv_open_cleanup[global: 0x%08x] "
1771 "server[%s] does not exist\n",
1773 server_id_str_buf(op->server_id, &idbuf)));
1781 status = dbwrap_record_delete(rec);
1782 if (!NT_STATUS_IS_OK(status)) {
1783 DEBUG(1, ("smbXsrv_open_cleanup[global: 0x%08x] "
1784 "failed to delete record"
1785 "from %s: %s\n", global_id,
1786 dbwrap_name(smbXsrv_open_global_db_ctx),
1787 nt_errstr(status)));
1791 DEBUG(10, ("smbXsrv_open_cleanup[global: 0x%08x] "
1792 "delete record from %s\n",
1794 dbwrap_name(smbXsrv_open_global_db_ctx)));