samba-tool: Ensure modifying GPO increments GPT.INI vers
[Samba.git] / source3 / smbd / smbXsrv_open.c
blob3975a7e6cffe8843bd134b1f4965445a03bcc772
1 /*
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"
22 #include "includes.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"
31 #include "messages.h"
32 #include "lib/util/util_tdb.h"
33 #include "librpc/gen_ndr/ndr_smbXsrv.h"
34 #include "serverid.h"
35 #include "source3/include/util_tdb.h"
36 #include "lib/util/idtree_random.h"
37 #include "lib/util/time_basic.h"
39 struct smbXsrv_open_table {
40 struct {
41 struct idr_context *idr;
42 struct db_context *replay_cache_db_ctx;
43 uint32_t lowest_id;
44 uint32_t highest_id;
45 uint32_t max_opens;
46 uint32_t num_opens;
47 } local;
48 struct {
49 struct db_context *db_ctx;
50 } global;
53 static struct db_context *smbXsrv_open_global_db_ctx = NULL;
55 NTSTATUS smbXsrv_open_global_init(void)
57 char *global_path = NULL;
58 struct db_context *db_ctx = NULL;
60 if (smbXsrv_open_global_db_ctx != NULL) {
61 return NT_STATUS_OK;
64 global_path = lock_path(talloc_tos(), "smbXsrv_open_global.tdb");
65 if (global_path == NULL) {
66 return NT_STATUS_NO_MEMORY;
69 db_ctx = db_open(NULL, global_path,
70 SMBD_VOLATILE_TDB_HASH_SIZE,
71 SMBD_VOLATILE_TDB_FLAGS,
72 O_RDWR | O_CREAT, 0600,
73 DBWRAP_LOCK_ORDER_1,
74 DBWRAP_FLAG_NONE);
75 TALLOC_FREE(global_path);
76 if (db_ctx == NULL) {
77 NTSTATUS status;
79 status = map_nt_error_from_unix_common(errno);
81 return status;
84 smbXsrv_open_global_db_ctx = db_ctx;
86 return NT_STATUS_OK;
90 * NOTE:
91 * We need to store the keys in big endian so that dbwrap_rbt's memcmp
92 * has the same result as integer comparison between the uint32_t
93 * values.
95 * TODO: implement string based key
98 struct smbXsrv_open_global_key_buf { uint8_t buf[sizeof(uint32_t)]; };
100 static TDB_DATA smbXsrv_open_global_id_to_key(
101 uint32_t id, struct smbXsrv_open_global_key_buf *key_buf)
103 RSIVAL(key_buf->buf, 0, id);
105 return (TDB_DATA) {
106 .dptr = key_buf->buf,
107 .dsize = sizeof(key_buf->buf),
111 static NTSTATUS smbXsrv_open_table_init(struct smbXsrv_connection *conn,
112 uint32_t lowest_id,
113 uint32_t highest_id,
114 uint32_t max_opens)
116 struct smbXsrv_client *client = conn->client;
117 struct smbXsrv_open_table *table;
118 NTSTATUS status;
119 uint64_t max_range;
121 if (lowest_id > highest_id) {
122 return NT_STATUS_INTERNAL_ERROR;
125 max_range = highest_id;
126 max_range -= lowest_id;
127 max_range += 1;
129 if (max_opens > max_range) {
130 return NT_STATUS_INTERNAL_ERROR;
133 table = talloc_zero(client, struct smbXsrv_open_table);
134 if (table == NULL) {
135 return NT_STATUS_NO_MEMORY;
138 table->local.idr = idr_init(table);
139 if (table->local.idr == NULL) {
140 TALLOC_FREE(table);
141 return NT_STATUS_NO_MEMORY;
143 table->local.replay_cache_db_ctx = db_open_rbt(table);
144 if (table->local.replay_cache_db_ctx == NULL) {
145 TALLOC_FREE(table);
146 return NT_STATUS_NO_MEMORY;
148 table->local.lowest_id = lowest_id;
149 table->local.highest_id = highest_id;
150 table->local.max_opens = max_opens;
152 status = smbXsrv_open_global_init();
153 if (!NT_STATUS_IS_OK(status)) {
154 TALLOC_FREE(table);
155 return status;
158 table->global.db_ctx = smbXsrv_open_global_db_ctx;
160 client->open_table = table;
161 return NT_STATUS_OK;
164 static NTSTATUS smbXsrv_open_local_lookup(struct smbXsrv_open_table *table,
165 uint32_t open_local_id,
166 uint32_t open_global_id,
167 NTTIME now,
168 struct smbXsrv_open **_open)
170 struct smbXsrv_open *op = NULL;
172 *_open = NULL;
174 if (open_local_id == 0) {
175 return NT_STATUS_FILE_CLOSED;
178 if (table == NULL) {
179 /* this might happen before the end of negprot */
180 return NT_STATUS_FILE_CLOSED;
183 if (table->local.idr == NULL) {
184 return NT_STATUS_INTERNAL_ERROR;
187 op = idr_find(table->local.idr, open_local_id);
188 if (op == NULL) {
189 return NT_STATUS_FILE_CLOSED;
192 if (open_global_id == 0) {
193 /* make the global check a no-op for SMB1 */
194 open_global_id = op->global->open_global_id;
197 if (op->global->open_global_id != open_global_id) {
198 return NT_STATUS_FILE_CLOSED;
201 if (now != 0) {
202 op->idle_time = now;
205 *_open = op;
206 return NT_STATUS_OK;
209 static NTSTATUS smbXsrv_open_global_parse_record(
210 TALLOC_CTX *mem_ctx,
211 TDB_DATA key,
212 TDB_DATA val,
213 struct smbXsrv_open_global0 **global)
215 DATA_BLOB blob = data_blob_const(val.dptr, val.dsize);
216 struct smbXsrv_open_globalB global_blob;
217 enum ndr_err_code ndr_err;
218 NTSTATUS status;
219 TALLOC_CTX *frame = talloc_stackframe();
221 ndr_err = ndr_pull_struct_blob(&blob, frame, &global_blob,
222 (ndr_pull_flags_fn_t)ndr_pull_smbXsrv_open_globalB);
223 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
224 DEBUG(1,("Invalid record in smbXsrv_open_global.tdb:"
225 "key '%s' ndr_pull_struct_blob - %s\n",
226 tdb_data_dbg(key),
227 ndr_errstr(ndr_err)));
228 status = ndr_map_error2ntstatus(ndr_err);
229 goto done;
232 DBG_DEBUG("\n");
233 if (CHECK_DEBUGLVL(10)) {
234 NDR_PRINT_DEBUG(smbXsrv_open_globalB, &global_blob);
237 if (global_blob.version != SMBXSRV_VERSION_0) {
238 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
239 DEBUG(1,("Invalid record in smbXsrv_open_global.tdb:"
240 "key '%s' unsupported version - %d - %s\n",
241 tdb_data_dbg(key),
242 (int)global_blob.version,
243 nt_errstr(status)));
244 goto done;
247 if (global_blob.info.info0 == NULL) {
248 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
249 DEBUG(1,("Invalid record in smbXsrv_tcon_global.tdb:"
250 "key '%s' info0 NULL pointer - %s\n",
251 tdb_data_dbg(key),
252 nt_errstr(status)));
253 goto done;
256 *global = talloc_move(mem_ctx, &global_blob.info.info0);
257 status = NT_STATUS_OK;
258 done:
259 talloc_free(frame);
260 return status;
263 static NTSTATUS smbXsrv_open_global_verify_record(
264 TDB_DATA key,
265 TDB_DATA val,
266 TALLOC_CTX *mem_ctx,
267 struct smbXsrv_open_global0 **_global0)
269 struct smbXsrv_open_global0 *global0 = NULL;
270 struct server_id_buf buf;
271 NTSTATUS status;
273 if (val.dsize == 0) {
274 return NT_STATUS_NOT_FOUND;
277 status = smbXsrv_open_global_parse_record(mem_ctx, key, val, &global0);
278 if (!NT_STATUS_IS_OK(status)) {
279 DBG_WARNING("smbXsrv_open_global_parse_record for %s failed: "
280 "%s\n",
281 tdb_data_dbg(key),
282 nt_errstr(status));
283 return status;
285 *_global0 = global0;
287 if (server_id_is_disconnected(&global0->server_id)) {
288 return NT_STATUS_OK;
290 if (serverid_exists(&global0->server_id)) {
291 return NT_STATUS_OK;
294 DBG_WARNING("smbd %s did not clean up record %s\n",
295 server_id_str_buf(global0->server_id, &buf),
296 tdb_data_dbg(key));
298 return NT_STATUS_FATAL_APP_EXIT;
301 static NTSTATUS smbXsrv_open_global_store(
302 struct db_record *rec,
303 TDB_DATA key,
304 TDB_DATA oldval,
305 struct smbXsrv_open_global0 *global)
307 struct smbXsrv_open_globalB global_blob;
308 DATA_BLOB blob = data_blob_null;
309 TDB_DATA val = { .dptr = NULL, };
310 NTSTATUS status;
311 enum ndr_err_code ndr_err;
314 * TODO: if we use other versions than '0'
315 * we would add glue code here, that would be able to
316 * store the information in the old format.
319 global_blob = (struct smbXsrv_open_globalB) {
320 .version = smbXsrv_version_global_current(),
323 if (oldval.dsize >= 8) {
324 global_blob.seqnum = IVAL(oldval.dptr, 4);
326 global_blob.seqnum += 1;
327 global_blob.info.info0 = global;
329 ndr_err = ndr_push_struct_blob(&blob, talloc_tos(), &global_blob,
330 (ndr_push_flags_fn_t)ndr_push_smbXsrv_open_globalB);
331 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
332 DBG_WARNING("key '%s' ndr_push - %s\n",
333 tdb_data_dbg(key),
334 ndr_map_error2string(ndr_err));
335 return ndr_map_error2ntstatus(ndr_err);
338 val = make_tdb_data(blob.data, blob.length);
339 status = dbwrap_record_store(rec, val, TDB_REPLACE);
340 TALLOC_FREE(blob.data);
341 if (!NT_STATUS_IS_OK(status)) {
342 DBG_WARNING("key '%s' store - %s\n",
343 tdb_data_dbg(key),
344 nt_errstr(status));
345 return status;
348 if (CHECK_DEBUGLVL(10)) {
349 DBG_DEBUG("key '%s' stored\n", tdb_data_dbg(key));
350 NDR_PRINT_DEBUG(smbXsrv_open_globalB, &global_blob);
353 return NT_STATUS_OK;
356 struct smbXsrv_open_global_allocate_state {
357 uint32_t id;
358 struct smbXsrv_open_global0 *global;
359 NTSTATUS status;
362 static void smbXsrv_open_global_allocate_fn(
363 struct db_record *rec, TDB_DATA oldval, void *private_data)
365 struct smbXsrv_open_global_allocate_state *state = private_data;
366 struct smbXsrv_open_global0 *global = state->global;
367 struct smbXsrv_open_global0 *tmp_global0 = NULL;
368 TDB_DATA key = dbwrap_record_get_key(rec);
370 state->status = smbXsrv_open_global_verify_record(
371 key, oldval, talloc_tos(), &tmp_global0);
373 if (NT_STATUS_IS_OK(state->status)) {
375 * Found an existing record
377 TALLOC_FREE(tmp_global0);
378 state->status = NT_STATUS_RETRY;
379 return;
382 if (NT_STATUS_EQUAL(state->status, NT_STATUS_NOT_FOUND)) {
384 * Found an empty slot
386 global->open_global_id = state->id;
387 global->open_persistent_id = state->id;
389 state->status = smbXsrv_open_global_store(
390 rec, key, (TDB_DATA) { .dsize = 0, }, state->global);
391 if (!NT_STATUS_IS_OK(state->status)) {
392 DBG_WARNING("smbXsrv_open_global_store() for "
393 "id %"PRIu32" failed: %s\n",
394 state->id,
395 nt_errstr(state->status));
397 return;
400 if (NT_STATUS_EQUAL(state->status, NT_STATUS_FATAL_APP_EXIT)) {
401 NTSTATUS status;
403 TALLOC_FREE(tmp_global0);
406 * smbd crashed
408 status = dbwrap_record_delete(rec);
409 if (!NT_STATUS_IS_OK(status)) {
410 DBG_WARNING("dbwrap_record_delete() failed "
411 "for record %"PRIu32": %s\n",
412 state->id,
413 nt_errstr(status));
414 state->status = NT_STATUS_INTERNAL_DB_CORRUPTION;
415 return;
417 return;
421 static NTSTATUS smbXsrv_open_global_allocate(
422 struct db_context *db, struct smbXsrv_open_global0 *global)
424 struct smbXsrv_open_global_allocate_state state = {
425 .global = global,
427 uint32_t i;
428 uint32_t last_free = 0;
429 const uint32_t min_tries = 3;
432 * Here we just randomly try the whole 32-bit space
434 * We use just 32-bit, because we want to reuse the
435 * ID for SRVSVC.
437 for (i = 0; i < UINT32_MAX; i++) {
438 struct smbXsrv_open_global_key_buf key_buf;
439 TDB_DATA key;
440 NTSTATUS status;
442 if (i >= min_tries && last_free != 0) {
443 state.id = last_free;
444 } else {
445 generate_nonce_buffer(
446 (uint8_t *)&state.id, sizeof(state.id));
447 state.id = MAX(state.id, 1);
448 state.id = MIN(state.id, UINT32_MAX-1);
451 key = smbXsrv_open_global_id_to_key(state.id, &key_buf);
453 status = dbwrap_do_locked(
454 db, key, smbXsrv_open_global_allocate_fn, &state);
456 if (!NT_STATUS_IS_OK(status)) {
457 DBG_WARNING("dbwrap_do_locked() failed: %s\n",
458 nt_errstr(status));
459 return NT_STATUS_INTERNAL_DB_ERROR;
462 if (NT_STATUS_IS_OK(state.status)) {
464 * Found an empty slot, done.
466 DBG_DEBUG("Found slot %"PRIu32"\n", state.id);
467 return NT_STATUS_OK;
470 if (NT_STATUS_EQUAL(state.status, NT_STATUS_FATAL_APP_EXIT)) {
472 if ((i < min_tries) && (last_free == 0)) {
474 * Remember "id" as free but also try
475 * others to not recycle ids too
476 * quickly.
478 last_free = state.id;
480 continue;
483 if (NT_STATUS_EQUAL(state.status, NT_STATUS_RETRY)) {
485 * Normal collision, try next
487 DBG_DEBUG("Found record for id %"PRIu32"\n",
488 state.id);
489 continue;
492 DBG_WARNING("smbXsrv_open_global_allocate_fn() failed: %s\n",
493 nt_errstr(state.status));
494 return state.status;
497 /* should not be reached */
498 return NT_STATUS_INTERNAL_ERROR;
501 static int smbXsrv_open_destructor(struct smbXsrv_open *op)
503 NTSTATUS status;
505 status = smbXsrv_open_close(op, 0);
506 if (!NT_STATUS_IS_OK(status)) {
507 DEBUG(0, ("smbXsrv_open_destructor: "
508 "smbXsrv_open_close() failed - %s\n",
509 nt_errstr(status)));
512 TALLOC_FREE(op->global);
514 return 0;
517 NTSTATUS smbXsrv_open_create(struct smbXsrv_connection *conn,
518 struct auth_session_info *session_info,
519 NTTIME now,
520 struct smbXsrv_open **_open)
522 struct smbXsrv_open_table *table = conn->client->open_table;
523 struct smbXsrv_open *op = NULL;
524 struct smbXsrv_open_global0 *global = NULL;
525 NTSTATUS status;
526 struct dom_sid *current_sid = NULL;
527 struct security_token *current_token = NULL;
528 int local_id;
530 if (session_info == NULL) {
531 return NT_STATUS_INVALID_HANDLE;
533 current_token = session_info->security_token;
535 if (current_token == NULL) {
536 return NT_STATUS_INVALID_HANDLE;
539 if (current_token->num_sids > PRIMARY_USER_SID_INDEX) {
540 current_sid = &current_token->sids[PRIMARY_USER_SID_INDEX];
543 if (current_sid == NULL) {
544 return NT_STATUS_INVALID_HANDLE;
547 if (table->local.num_opens >= table->local.max_opens) {
548 return NT_STATUS_INSUFFICIENT_RESOURCES;
551 op = talloc_zero(table, struct smbXsrv_open);
552 if (op == NULL) {
553 return NT_STATUS_NO_MEMORY;
555 op->table = table;
556 op->status = NT_STATUS_OK; /* TODO: start with INTERNAL_ERROR */
557 op->idle_time = now;
559 global = talloc_zero(op, struct smbXsrv_open_global0);
560 if (global == NULL) {
561 TALLOC_FREE(op);
562 return NT_STATUS_NO_MEMORY;
564 op->global = global;
567 * We mark every slot as invalid using 0xFF.
568 * Valid values are masked with 0xF.
570 memset(global->lock_sequence_array, 0xFF,
571 sizeof(global->lock_sequence_array));
573 local_id = idr_get_new_random(
574 table->local.idr,
576 table->local.lowest_id,
577 table->local.highest_id);
578 if (local_id == -1) {
579 TALLOC_FREE(op);
580 return NT_STATUS_INSUFFICIENT_RESOURCES;
582 op->local_id = local_id;
584 global->open_volatile_id = op->local_id;
586 global->server_id = messaging_server_id(conn->client->msg_ctx);
587 global->open_time = now;
588 global->open_owner = *current_sid;
589 if (conn->protocol >= PROTOCOL_SMB2_10) {
590 global->client_guid = conn->smb2.client.guid;
593 status = smbXsrv_open_global_allocate(table->global.db_ctx,
594 global);
595 if (!NT_STATUS_IS_OK(status)) {
596 int ret = idr_remove(table->local.idr, local_id);
597 SMB_ASSERT(ret == 0);
599 DBG_WARNING("smbXsrv_open_global_allocate() failed: %s\n",
600 nt_errstr(status));
601 TALLOC_FREE(op);
602 return status;
605 table->local.num_opens += 1;
606 talloc_set_destructor(op, smbXsrv_open_destructor);
608 if (CHECK_DEBUGLVL(10)) {
609 struct smbXsrv_openB open_blob = {
610 .version = SMBXSRV_VERSION_0,
611 .info.info0 = op,
614 DEBUG(10,("smbXsrv_open_create: global_id (0x%08x) stored\n",
615 op->global->open_global_id));
616 NDR_PRINT_DEBUG(smbXsrv_openB, &open_blob);
619 *_open = op;
620 return NT_STATUS_OK;
623 static NTSTATUS smbXsrv_open_set_replay_cache(struct smbXsrv_open *op)
625 struct GUID *create_guid;
626 struct GUID_txt_buf buf;
627 char *guid_string;
628 struct db_context *db = op->table->local.replay_cache_db_ctx;
629 struct smbXsrv_open_replay_cache rc = {
630 .idle_time = op->idle_time,
631 .local_id = op->local_id,
633 uint8_t data[SMBXSRV_OPEN_REPLAY_CACHE_FIXED_SIZE] = { 0 };
634 DATA_BLOB blob = { .data = data, .length = sizeof(data), };
635 enum ndr_err_code ndr_err;
636 NTSTATUS status;
637 TDB_DATA val;
639 if (!(op->flags & SMBXSRV_OPEN_NEED_REPLAY_CACHE)) {
640 return NT_STATUS_OK;
643 if (op->flags & SMBXSRV_OPEN_HAVE_REPLAY_CACHE) {
644 return NT_STATUS_OK;
647 create_guid = &op->global->create_guid;
648 guid_string = GUID_buf_string(create_guid, &buf);
650 ndr_err = ndr_push_struct_into_fixed_blob(&blob, &rc,
651 (ndr_push_flags_fn_t)ndr_push_smbXsrv_open_replay_cache);
652 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
653 status = ndr_map_error2ntstatus(ndr_err);
654 return status;
656 val = make_tdb_data(blob.data, blob.length);
658 status = dbwrap_store_bystring(db, guid_string, val, TDB_REPLACE);
660 if (NT_STATUS_IS_OK(status)) {
661 op->flags |= SMBXSRV_OPEN_HAVE_REPLAY_CACHE;
662 op->flags &= ~SMBXSRV_OPEN_NEED_REPLAY_CACHE;
665 return status;
668 NTSTATUS smbXsrv_open_purge_replay_cache(struct smbXsrv_client *client,
669 const struct GUID *create_guid)
671 struct GUID_txt_buf buf;
672 char *guid_string;
673 struct db_context *db;
675 if (client->open_table == NULL) {
676 return NT_STATUS_OK;
679 db = client->open_table->local.replay_cache_db_ctx;
681 guid_string = GUID_buf_string(create_guid, &buf);
682 if (guid_string == NULL) {
683 return NT_STATUS_INVALID_PARAMETER;
686 return dbwrap_purge_bystring(db, guid_string);
689 static NTSTATUS smbXsrv_open_clear_replay_cache(struct smbXsrv_open *op)
691 struct GUID *create_guid;
692 struct GUID_txt_buf buf;
693 char *guid_string;
694 struct db_context *db;
695 NTSTATUS status;
697 if (op->table == NULL) {
698 return NT_STATUS_OK;
701 db = op->table->local.replay_cache_db_ctx;
703 if (!(op->flags & SMBXSRV_OPEN_HAVE_REPLAY_CACHE)) {
704 return NT_STATUS_OK;
707 create_guid = &op->global->create_guid;
708 if (GUID_all_zero(create_guid)) {
709 return NT_STATUS_OK;
712 guid_string = GUID_buf_string(create_guid, &buf);
713 if (guid_string == NULL) {
714 return NT_STATUS_INVALID_PARAMETER;
717 status = dbwrap_purge_bystring(db, guid_string);
719 if (NT_STATUS_IS_OK(status)) {
720 op->flags &= ~SMBXSRV_OPEN_HAVE_REPLAY_CACHE;
723 return status;
726 struct smbXsrv_open_update_state {
727 struct smbXsrv_open_global0 *global;
728 NTSTATUS status;
731 static void smbXsrv_open_update_fn(
732 struct db_record *rec, TDB_DATA oldval, void *private_data)
734 struct smbXsrv_open_update_state *state = private_data;
735 TDB_DATA key = dbwrap_record_get_key(rec);
737 state->status = smbXsrv_open_global_store(
738 rec, key, oldval, state->global);
741 NTSTATUS smbXsrv_open_update(struct smbXsrv_open *op)
743 struct smbXsrv_open_update_state state = { .global = op->global, };
744 struct smbXsrv_open_table *table = op->table;
745 struct smbXsrv_open_global_key_buf key_buf;
746 TDB_DATA key = smbXsrv_open_global_id_to_key(
747 op->global->open_global_id, &key_buf);
748 NTSTATUS status;
750 status = dbwrap_do_locked(
751 table->global.db_ctx, key, smbXsrv_open_update_fn, &state);
752 if (!NT_STATUS_IS_OK(status)) {
753 DBG_WARNING("global_id (0x%08x) dbwrap_do_locked failed: %s\n",
754 op->global->open_global_id,
755 nt_errstr(status));
756 return NT_STATUS_INTERNAL_DB_ERROR;
759 if (!NT_STATUS_IS_OK(state.status)) {
760 DBG_WARNING("global_id (0x%08x) smbXsrv_open_global_store "
761 "failed: %s\n",
762 op->global->open_global_id,
763 nt_errstr(state.status));
764 return state.status;
767 status = smbXsrv_open_set_replay_cache(op);
768 if (!NT_STATUS_IS_OK(status)) {
769 DBG_ERR("smbXsrv_open_set_replay_cache failed: %s\n",
770 nt_errstr(status));
771 return status;
774 if (CHECK_DEBUGLVL(10)) {
775 struct smbXsrv_openB open_blob = {
776 .version = SMBXSRV_VERSION_0,
777 .info.info0 = op,
780 DEBUG(10,("smbXsrv_open_update: global_id (0x%08x) stored\n",
781 op->global->open_global_id));
782 NDR_PRINT_DEBUG(smbXsrv_openB, &open_blob);
785 return NT_STATUS_OK;
788 struct smbXsrv_open_close_state {
789 struct smbXsrv_open *op;
790 NTSTATUS status;
793 static void smbXsrv_open_close_fn(
794 struct db_record *rec, TDB_DATA oldval, void *private_data)
796 struct smbXsrv_open_close_state *state = private_data;
797 struct smbXsrv_open_global0 *global = state->op->global;
798 TDB_DATA key = dbwrap_record_get_key(rec);
800 if (global->durable) {
802 * Durable open -- we need to update the global part
803 * instead of deleting it
805 state->status = smbXsrv_open_global_store(
806 rec, key, oldval, global);
807 if (!NT_STATUS_IS_OK(state->status)) {
808 DBG_WARNING("failed to store global key '%s': %s\n",
809 tdb_data_dbg(key),
810 nt_errstr(state->status));
811 return;
814 if (CHECK_DEBUGLVL(10)) {
815 struct smbXsrv_openB open_blob = {
816 .version = SMBXSRV_VERSION_0,
817 .info.info0 = state->op,
820 DBG_DEBUG("(0x%08x) stored disconnect\n",
821 global->open_global_id);
822 NDR_PRINT_DEBUG(smbXsrv_openB, &open_blob);
824 return;
827 state->status = dbwrap_record_delete(rec);
828 if (!NT_STATUS_IS_OK(state->status)) {
829 DBG_WARNING("failed to delete global key '%s': %s\n",
830 tdb_data_dbg(key),
831 nt_errstr(state->status));
835 NTSTATUS smbXsrv_open_close(struct smbXsrv_open *op, NTTIME now)
837 struct smbXsrv_open_close_state state = { .op = op, };
838 struct smbXsrv_open_global0 *global = op->global;
839 struct smbXsrv_open_table *table;
840 NTSTATUS status;
841 NTSTATUS error = NT_STATUS_OK;
842 struct smbXsrv_open_global_key_buf key_buf;
843 TDB_DATA key = smbXsrv_open_global_id_to_key(
844 global->open_global_id, &key_buf);
845 int ret;
847 error = smbXsrv_open_clear_replay_cache(op);
848 if (!NT_STATUS_IS_OK(error)) {
849 DBG_ERR("smbXsrv_open_clear_replay_cache failed: %s\n",
850 nt_errstr(error));
853 if (op->table == NULL) {
854 return error;
857 table = op->table;
858 op->table = NULL;
860 op->status = NT_STATUS_FILE_CLOSED;
861 global->disconnect_time = now;
862 server_id_set_disconnected(&global->server_id);
864 status = dbwrap_do_locked(
865 table->global.db_ctx, key, smbXsrv_open_close_fn, &state);
866 if (!NT_STATUS_IS_OK(status)) {
867 DBG_WARNING("dbwrap_do_locked() for %s failed: %s\n",
868 tdb_data_dbg(key),
869 nt_errstr(status));
870 error = status;
871 } else if (!NT_STATUS_IS_OK(state.status)) {
872 DBG_WARNING("smbXsrv_open_close_fn() for %s failed: %s\n",
873 tdb_data_dbg(key),
874 nt_errstr(state.status));
875 error = state.status;
878 ret = idr_remove(table->local.idr, op->local_id);
879 SMB_ASSERT(ret == 0);
881 table->local.num_opens -= 1;
883 if (op->compat) {
884 op->compat->op = NULL;
885 file_free(NULL, op->compat);
886 op->compat = NULL;
889 return error;
892 NTSTATUS smb1srv_open_table_init(struct smbXsrv_connection *conn)
894 uint32_t max_opens;
897 * Allow a range from 1..65534.
899 * With real_max_open_files possible ids,
900 * truncated to the SMB1 limit of 16-bit.
902 * 0 and 0xFFFF are no valid ids.
904 max_opens = conn->client->sconn->real_max_open_files;
905 max_opens = MIN(max_opens, UINT16_MAX - 1);
907 return smbXsrv_open_table_init(conn, 1, UINT16_MAX - 1, max_opens);
910 NTSTATUS smb1srv_open_lookup(struct smbXsrv_connection *conn,
911 uint16_t fnum, NTTIME now,
912 struct smbXsrv_open **_open)
914 struct smbXsrv_open_table *table = conn->client->open_table;
915 uint32_t local_id = fnum;
916 uint32_t global_id = 0;
918 return smbXsrv_open_local_lookup(table, local_id, global_id, now, _open);
921 NTSTATUS smb2srv_open_table_init(struct smbXsrv_connection *conn)
923 uint32_t max_opens;
924 uint32_t highest_id;
927 * Allow a range from 1..4294967294.
929 * With real_max_open_files possible ids,
930 * truncated to 16-bit (the same as SMB1 for now).
932 * 0 and 0xFFFFFFFF are no valid ids.
934 * The usage of conn->sconn->real_max_open_files
935 * is the reason that we use one open table per
936 * transport connection (as we still have a 1:1 mapping
937 * between process and transport connection).
939 max_opens = conn->client->sconn->real_max_open_files;
940 max_opens = MIN(max_opens, UINT16_MAX - 1);
943 * idtree uses "int" for local IDs. Limit the maximum ID to
944 * what "int" can hold.
946 highest_id = UINT32_MAX-1;
947 highest_id = MIN(highest_id, INT_MAX);
949 return smbXsrv_open_table_init(conn, 1, highest_id, max_opens);
952 NTSTATUS smb2srv_open_lookup(struct smbXsrv_connection *conn,
953 uint64_t persistent_id,
954 uint64_t volatile_id,
955 NTTIME now,
956 struct smbXsrv_open **_open)
958 struct smbXsrv_open_table *table = conn->client->open_table;
959 uint32_t local_id = volatile_id & UINT32_MAX;
960 uint64_t local_zeros = volatile_id & 0xFFFFFFFF00000000LLU;
961 uint32_t global_id = persistent_id & UINT32_MAX;
962 uint64_t global_zeros = persistent_id & 0xFFFFFFFF00000000LLU;
963 NTSTATUS status;
965 if (local_zeros != 0) {
966 return NT_STATUS_FILE_CLOSED;
969 if (global_zeros != 0) {
970 return NT_STATUS_FILE_CLOSED;
973 if (global_id == 0) {
974 return NT_STATUS_FILE_CLOSED;
977 status = smbXsrv_open_local_lookup(table, local_id, global_id, now,
978 _open);
979 if (!NT_STATUS_IS_OK(status)) {
980 return status;
984 * Clear the replay cache for this create_guid if it exists:
985 * This is based on the assumption that this lookup will be
986 * triggered by a client request using the file-id for lookup.
987 * Hence the client has proven that it has in fact seen the
988 * reply to its initial create call. So subsequent create replays
989 * should be treated as invalid. Hence the index for create_guid
990 * lookup needs to be removed.
992 status = smbXsrv_open_clear_replay_cache(*_open);
994 return status;
998 * This checks or marks the replay cache, we have the following
999 * cases:
1001 * 1. There is no record in the cache
1002 * => we add the passes caller_req_guid as holder_req_guid
1003 * together with local_id as 0.
1004 * => We return STATUS_FWP_RESERVED in order to indicate
1005 * that the caller holds the current reservation
1007 * 2. There is a record in the cache and holder_req_guid
1008 * is already the same as caller_req_guid and local_id is 0
1009 * => We return STATUS_FWP_RESERVED in order to indicate
1010 * that the caller holds the current reservation
1012 * 3. There is a record in the cache with a holder_req_guid
1013 * other than caller_req_guid (and local_id is 0):
1014 * => We return NT_STATUS_FILE_NOT_AVAILABLE to indicate
1015 * the original request is still pending
1017 * 4. There is a record in the cache with a zero holder_req_guid
1018 * and a valid local_id:
1019 * => We lookup the existing open by local_id
1020 * => We return NT_STATUS_OK together with the smbXsrv_open
1023 * With NT_STATUS_OK the caller can continue the replay processing.
1025 * With STATUS_FWP_RESERVED the caller should continue the normal
1026 * open processing:
1027 * - On success:
1028 * - smbXsrv_open_update()/smbXsrv_open_set_replay_cache()
1029 * will convert the record to a zero holder_req_guid
1030 * with a valid local_id.
1031 * - On failure:
1032 * - smbXsrv_open_purge_replay_cache() should cleanup
1033 * the reservation.
1035 * All other values should be returned to the client,
1036 * while NT_STATUS_FILE_NOT_AVAILABLE will trigger the
1037 * retry loop on the client.
1039 NTSTATUS smb2srv_open_lookup_replay_cache(struct smbXsrv_connection *conn,
1040 struct GUID caller_req_guid,
1041 struct GUID create_guid,
1042 const char *name,
1043 NTTIME now,
1044 struct smbXsrv_open **_open)
1046 TALLOC_CTX *frame = talloc_stackframe();
1047 NTSTATUS status;
1048 struct smbXsrv_open_table *table = conn->client->open_table;
1049 struct db_context *db = table->local.replay_cache_db_ctx;
1050 struct GUID_txt_buf tmp_guid_buf;
1051 struct GUID_txt_buf _create_guid_buf;
1052 const char *create_guid_str = GUID_buf_string(&create_guid, &_create_guid_buf);
1053 TDB_DATA create_guid_key = string_term_tdb_data(create_guid_str);
1054 struct db_record *db_rec = NULL;
1055 struct smbXsrv_open *op = NULL;
1056 struct smbXsrv_open_replay_cache rc = {
1057 .holder_req_guid = caller_req_guid,
1058 .idle_time = now,
1059 .local_id = 0,
1061 enum ndr_err_code ndr_err;
1062 DATA_BLOB blob = data_blob_null;
1063 TDB_DATA val;
1065 *_open = NULL;
1067 db_rec = dbwrap_fetch_locked(db, frame, create_guid_key);
1068 if (db_rec == NULL) {
1069 TALLOC_FREE(frame);
1070 return NT_STATUS_INTERNAL_DB_ERROR;
1073 val = dbwrap_record_get_value(db_rec);
1074 if (val.dsize == 0) {
1075 uint8_t data[SMBXSRV_OPEN_REPLAY_CACHE_FIXED_SIZE];
1077 blob = data_blob_const(data, ARRAY_SIZE(data));
1078 ndr_err = ndr_push_struct_into_fixed_blob(&blob, &rc,
1079 (ndr_push_flags_fn_t)ndr_push_smbXsrv_open_replay_cache);
1080 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1081 status = ndr_map_error2ntstatus(ndr_err);
1082 TALLOC_FREE(frame);
1083 return status;
1086 val = make_tdb_data(blob.data, blob.length);
1087 status = dbwrap_record_store(db_rec, val, TDB_REPLACE);
1088 if (!NT_STATUS_IS_OK(status)) {
1089 TALLOC_FREE(frame);
1090 return status;
1094 * We're the new holder
1096 *_open = NULL;
1097 TALLOC_FREE(frame);
1098 return NT_STATUS_FWP_RESERVED;
1101 if (val.dsize != SMBXSRV_OPEN_REPLAY_CACHE_FIXED_SIZE) {
1102 TALLOC_FREE(frame);
1103 return NT_STATUS_INTERNAL_DB_CORRUPTION;
1106 blob = data_blob_const(val.dptr, val.dsize);
1107 ndr_err = ndr_pull_struct_blob_all_noalloc(&blob, &rc,
1108 (ndr_pull_flags_fn_t)ndr_pull_smbXsrv_open_replay_cache);
1109 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1110 status = ndr_map_error2ntstatus(ndr_err);
1111 TALLOC_FREE(frame);
1112 return status;
1114 if (rc.local_id != 0) {
1115 if (GUID_equal(&rc.holder_req_guid, &caller_req_guid)) {
1117 * This should not happen
1119 status = NT_STATUS_INTERNAL_ERROR;
1120 DBG_ERR("caller %s already holds local_id %u for create %s [%s] - %s\n",
1121 GUID_buf_string(&caller_req_guid, &tmp_guid_buf),
1122 (unsigned)rc.local_id,
1123 create_guid_str,
1124 name,
1125 nt_errstr(status));
1127 TALLOC_FREE(frame);
1128 return status;
1131 status = smbXsrv_open_local_lookup(table,
1132 rc.local_id,
1133 0, /* global_id */
1134 now,
1135 &op);
1136 if (!NT_STATUS_IS_OK(status)) {
1137 DBG_ERR("holder %s stale for local_id %u for create %s [%s] - %s\n",
1138 GUID_buf_string(&rc.holder_req_guid, &tmp_guid_buf),
1139 (unsigned)rc.local_id,
1140 create_guid_str,
1141 name,
1142 nt_errstr(status));
1144 TALLOC_FREE(frame);
1145 return status;
1149 * We found an open the caller can reuse.
1151 SMB_ASSERT(op != NULL);
1152 *_open = op;
1153 TALLOC_FREE(frame);
1154 return NT_STATUS_OK;
1157 if (GUID_equal(&rc.holder_req_guid, &caller_req_guid)) {
1159 * We're still the holder
1161 *_open = NULL;
1162 TALLOC_FREE(frame);
1163 return NT_STATUS_FWP_RESERVED;
1167 * The original request (or a former replay) is still
1168 * pending, ask the client to retry by sending FILE_NOT_AVAILABLE.
1170 status = NT_STATUS_FILE_NOT_AVAILABLE;
1171 DBG_DEBUG("holder %s still pending for create %s [%s] - %s\n",
1172 GUID_buf_string(&rc.holder_req_guid, &tmp_guid_buf),
1173 create_guid_str,
1174 name,
1175 nt_errstr(status));
1176 TALLOC_FREE(frame);
1177 return status;
1180 struct smb2srv_open_recreate_state {
1181 struct smbXsrv_open *op;
1182 const struct GUID *create_guid;
1183 struct security_token *current_token;
1184 struct server_id me;
1186 NTSTATUS status;
1189 static void smb2srv_open_recreate_fn(
1190 struct db_record *rec, TDB_DATA oldval, void *private_data)
1192 struct smb2srv_open_recreate_state *state = private_data;
1193 TDB_DATA key = dbwrap_record_get_key(rec);
1194 struct smbXsrv_open_global0 *global = NULL;
1196 state->status = smbXsrv_open_global_verify_record(
1197 key, oldval, state->op, &state->op->global);
1198 if (!NT_STATUS_IS_OK(state->status)) {
1199 DBG_WARNING("smbXsrv_open_global_verify_record for %s "
1200 "failed: %s\n",
1201 tdb_data_dbg(key),
1202 nt_errstr(state->status));
1203 goto not_found;
1205 global = state->op->global;
1208 * If the provided create_guid is NULL, this means that
1209 * the reconnect request was a v1 request. In that case
1210 * we should skip the create GUID verification, since
1211 * it is valid to v1-reconnect a v2-opened handle.
1213 if ((state->create_guid != NULL) &&
1214 !GUID_equal(&global->create_guid, state->create_guid)) {
1215 struct GUID_txt_buf buf1, buf2;
1216 DBG_NOTICE("%s != %s in %s\n",
1217 GUID_buf_string(&global->create_guid, &buf1),
1218 GUID_buf_string(state->create_guid, &buf2),
1219 tdb_data_dbg(key));
1220 goto not_found;
1223 if (!security_token_is_sid(
1224 state->current_token, &global->open_owner)) {
1225 struct dom_sid_buf buf;
1226 DBG_NOTICE("global owner %s not in our token in %s\n",
1227 dom_sid_str_buf(&global->open_owner, &buf),
1228 tdb_data_dbg(key));
1229 goto not_found;
1232 if (!global->durable) {
1233 DBG_NOTICE("%"PRIu64"/%"PRIu64" not durable in %s\n",
1234 global->open_persistent_id,
1235 global->open_volatile_id,
1236 tdb_data_dbg(key));
1237 goto not_found;
1240 global->open_volatile_id = state->op->local_id;
1241 global->server_id = state->me;
1243 state->status = smbXsrv_open_global_store(rec, key, oldval, global);
1244 if (!NT_STATUS_IS_OK(state->status)) {
1245 DBG_WARNING("smbXsrv_open_global_store for %s failed: %s\n",
1246 tdb_data_dbg(key),
1247 nt_errstr(state->status));
1248 return;
1250 return;
1252 not_found:
1253 state->status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
1256 NTSTATUS smb2srv_open_recreate(struct smbXsrv_connection *conn,
1257 struct auth_session_info *session_info,
1258 uint64_t persistent_id,
1259 const struct GUID *create_guid,
1260 NTTIME now,
1261 struct smbXsrv_open **_open)
1263 struct smbXsrv_open_table *table = conn->client->open_table;
1264 struct smb2srv_open_recreate_state state = {
1265 .create_guid = create_guid,
1266 .me = messaging_server_id(conn->client->msg_ctx),
1268 struct smbXsrv_open_global_key_buf key_buf;
1269 TDB_DATA key = smbXsrv_open_global_id_to_key(
1270 persistent_id & UINT32_MAX, &key_buf);
1271 int ret, local_id;
1272 NTSTATUS status;
1274 if (session_info == NULL) {
1275 DEBUG(10, ("session_info=NULL\n"));
1276 return NT_STATUS_INVALID_HANDLE;
1278 state.current_token = session_info->security_token;
1280 if (state.current_token == NULL) {
1281 DEBUG(10, ("current_token=NULL\n"));
1282 return NT_STATUS_INVALID_HANDLE;
1285 if ((persistent_id & 0xFFFFFFFF00000000LLU) != 0) {
1287 * We only use 32 bit for the persistent ID
1289 DBG_DEBUG("persistent_id=%"PRIx64"\n", persistent_id);
1290 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1293 if (table->local.num_opens >= table->local.max_opens) {
1294 return NT_STATUS_INSUFFICIENT_RESOURCES;
1297 state.op = talloc_zero(table, struct smbXsrv_open);
1298 if (state.op == NULL) {
1299 return NT_STATUS_NO_MEMORY;
1301 state.op->table = table;
1303 local_id = idr_get_new_random(
1304 table->local.idr,
1305 state.op,
1306 table->local.lowest_id,
1307 table->local.highest_id);
1308 if (local_id == -1) {
1309 TALLOC_FREE(state.op);
1310 return NT_STATUS_INSUFFICIENT_RESOURCES;
1312 state.op->local_id = local_id;
1313 SMB_ASSERT(state.op->local_id == local_id); /* No coercion loss */
1315 table->local.num_opens += 1;
1317 state.op->idle_time = now;
1318 state.op->status = NT_STATUS_FILE_CLOSED;
1320 status = dbwrap_do_locked(
1321 table->global.db_ctx, key, smb2srv_open_recreate_fn, &state);
1322 if (!NT_STATUS_IS_OK(status)) {
1323 DBG_DEBUG("dbwrap_do_locked() for %s failed: %s\n",
1324 tdb_data_dbg(key),
1325 nt_errstr(status));
1326 goto fail;
1329 if (!NT_STATUS_IS_OK(state.status)) {
1330 status = state.status;
1331 DBG_DEBUG("smb2srv_open_recreate_fn for %s failed: %s\n",
1332 tdb_data_dbg(key),
1333 nt_errstr(status));
1334 goto fail;
1337 talloc_set_destructor(state.op, smbXsrv_open_destructor);
1339 if (CHECK_DEBUGLVL(10)) {
1340 struct smbXsrv_openB open_blob = {
1341 .info.info0 = state.op,
1343 DBG_DEBUG("global_id (0x%08x) stored\n",
1344 state.op->global->open_global_id);
1345 NDR_PRINT_DEBUG(smbXsrv_openB, &open_blob);
1348 *_open = state.op;
1350 return NT_STATUS_OK;
1351 fail:
1352 table->local.num_opens -= 1;
1354 ret = idr_remove(table->local.idr, state.op->local_id);
1355 SMB_ASSERT(ret == 0);
1356 TALLOC_FREE(state.op);
1357 return status;
1360 struct smbXsrv_open_global_traverse_state {
1361 int (*fn)(struct db_record *rec, struct smbXsrv_open_global0 *, void *);
1362 void *private_data;
1365 static int smbXsrv_open_global_traverse_fn(struct db_record *rec, void *data)
1367 struct smbXsrv_open_global_traverse_state *state =
1368 (struct smbXsrv_open_global_traverse_state*)data;
1369 struct smbXsrv_open_global0 *global = NULL;
1370 TDB_DATA key = dbwrap_record_get_key(rec);
1371 TDB_DATA val = dbwrap_record_get_value(rec);
1372 NTSTATUS status;
1373 int ret = -1;
1375 status = smbXsrv_open_global_parse_record(
1376 talloc_tos(), key, val, &global);
1377 if (!NT_STATUS_IS_OK(status)) {
1378 return -1;
1381 ret = state->fn(rec, global, state->private_data);
1382 talloc_free(global);
1383 return ret;
1386 NTSTATUS smbXsrv_open_global_traverse(
1387 int (*fn)(struct db_record *rec, struct smbXsrv_open_global0 *, void *),
1388 void *private_data)
1391 NTSTATUS status;
1392 int count = 0;
1393 struct smbXsrv_open_global_traverse_state state = {
1394 .fn = fn,
1395 .private_data = private_data,
1398 become_root();
1399 status = smbXsrv_open_global_init();
1400 if (!NT_STATUS_IS_OK(status)) {
1401 unbecome_root();
1402 DEBUG(0, ("Failed to initialize open_global: %s\n",
1403 nt_errstr(status)));
1404 return status;
1407 status = dbwrap_traverse_read(smbXsrv_open_global_db_ctx,
1408 smbXsrv_open_global_traverse_fn,
1409 &state,
1410 &count);
1411 unbecome_root();
1413 return status;
1416 struct smbXsrv_open_cleanup_state {
1417 uint32_t global_id;
1418 NTSTATUS status;
1421 static void smbXsrv_open_cleanup_fn(
1422 struct db_record *rec, TDB_DATA oldval, void *private_data)
1424 struct smbXsrv_open_cleanup_state *state = private_data;
1425 struct smbXsrv_open_global0 *global = NULL;
1426 TDB_DATA key = dbwrap_record_get_key(rec);
1427 bool delete_open = false;
1429 if (oldval.dsize == 0) {
1430 DBG_DEBUG("[global: 0x%08x] "
1431 "empty record in %s, skipping...\n",
1432 state->global_id,
1433 dbwrap_name(dbwrap_record_get_db(rec)));
1434 state->status = NT_STATUS_OK;
1435 return;
1438 state->status = smbXsrv_open_global_parse_record(
1439 talloc_tos(), key, oldval, &global);
1440 if (!NT_STATUS_IS_OK(state->status)) {
1441 DBG_WARNING("[global: %x08x] "
1442 "smbXsrv_open_global_parse_record() in %s "
1443 "failed: %s, deleting record\n",
1444 state->global_id,
1445 dbwrap_name(dbwrap_record_get_db(rec)),
1446 nt_errstr(state->status));
1447 delete_open = true;
1448 goto do_delete;
1451 if (server_id_is_disconnected(&global->server_id)) {
1452 struct timeval now = timeval_current();
1453 struct timeval disconnect_time;
1454 struct timeval_buf buf;
1455 int64_t tdiff;
1457 nttime_to_timeval(&disconnect_time, global->disconnect_time);
1458 tdiff = usec_time_diff(&now, &disconnect_time);
1459 delete_open = (tdiff >= 1000*global->durable_timeout_msec);
1461 DBG_DEBUG("[global: 0x%08x] "
1462 "disconnected at [%s] %"PRIi64"s ago with "
1463 "timeout of %"PRIu32"s -%s reached\n",
1464 state->global_id,
1465 timeval_str_buf(&disconnect_time,
1466 false,
1467 false,
1468 &buf),
1469 tdiff/1000000,
1470 global->durable_timeout_msec / 1000,
1471 delete_open ? "" : " not");
1472 } else if (!serverid_exists(&global->server_id)) {
1473 struct server_id_buf idbuf;
1474 DBG_DEBUG("[global: 0x%08x] "
1475 "server[%s] does not exist\n",
1476 state->global_id,
1477 server_id_str_buf(global->server_id, &idbuf));
1478 delete_open = true;
1481 if (!delete_open) {
1482 state->status = NT_STATUS_OK;
1483 return;
1485 do_delete:
1486 state->status = dbwrap_record_delete(rec);
1487 if (!NT_STATUS_IS_OK(state->status)) {
1488 DBG_WARNING("[global: 0x%08x] "
1489 "failed to delete record"
1490 "from %s: %s\n",
1491 state->global_id,
1492 dbwrap_name(dbwrap_record_get_db(rec)),
1493 nt_errstr(state->status));
1494 return;
1497 DBG_DEBUG("[global: 0x%08x] "
1498 "deleted record from %s\n",
1499 state->global_id,
1500 dbwrap_name(dbwrap_record_get_db(rec)));
1503 NTSTATUS smbXsrv_open_cleanup(uint64_t persistent_id)
1505 struct smbXsrv_open_cleanup_state state = {
1506 .global_id = persistent_id & UINT32_MAX,
1508 struct smbXsrv_open_global_key_buf key_buf;
1509 TDB_DATA key = smbXsrv_open_global_id_to_key(
1510 state.global_id, &key_buf);
1511 NTSTATUS status;
1513 status = dbwrap_do_locked(
1514 smbXsrv_open_global_db_ctx,
1515 key,
1516 smbXsrv_open_cleanup_fn,
1517 &state);
1518 if (!NT_STATUS_IS_OK(status)) {
1519 DBG_DEBUG("[global: 0x%08x] dbwrap_do_locked failed: %s\n",
1520 state.global_id,
1521 nt_errstr(status));
1522 return status;
1525 return state.status;