tests/krb5: Clarify checksum type assertion message
[Samba.git] / source3 / smbd / smbXsrv_open.c
blob871820ac876448f2222b43ac0701c4bb9fc91b9c
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 "includes.h"
22 #include "system/filesys.h"
23 #include "lib/util/server_id.h"
24 #include "smbd/smbd.h"
25 #include "smbd/globals.h"
26 #include "dbwrap/dbwrap.h"
27 #include "dbwrap/dbwrap_rbt.h"
28 #include "dbwrap/dbwrap_open.h"
29 #include "../libcli/security/security.h"
30 #include "messages.h"
31 #include "lib/util/util_tdb.h"
32 #include "librpc/gen_ndr/ndr_smbXsrv.h"
33 #include "serverid.h"
35 struct smbXsrv_open_table {
36 struct {
37 struct db_context *db_ctx;
38 struct db_context *replay_cache_db_ctx;
39 uint32_t lowest_id;
40 uint32_t highest_id;
41 uint32_t max_opens;
42 uint32_t num_opens;
43 } local;
44 struct {
45 struct db_context *db_ctx;
46 } global;
49 static struct db_context *smbXsrv_open_global_db_ctx = NULL;
51 NTSTATUS smbXsrv_open_global_init(void)
53 char *global_path = NULL;
54 struct db_context *db_ctx = NULL;
56 if (smbXsrv_open_global_db_ctx != NULL) {
57 return NT_STATUS_OK;
60 global_path = lock_path(talloc_tos(), "smbXsrv_open_global.tdb");
61 if (global_path == NULL) {
62 return NT_STATUS_NO_MEMORY;
65 db_ctx = db_open(NULL, global_path,
66 0, /* hash_size */
67 TDB_DEFAULT |
68 TDB_CLEAR_IF_FIRST |
69 TDB_INCOMPATIBLE_HASH,
70 O_RDWR | O_CREAT, 0600,
71 DBWRAP_LOCK_ORDER_1,
72 DBWRAP_FLAG_NONE);
73 TALLOC_FREE(global_path);
74 if (db_ctx == NULL) {
75 NTSTATUS status;
77 status = map_nt_error_from_unix_common(errno);
79 return status;
82 smbXsrv_open_global_db_ctx = db_ctx;
84 return NT_STATUS_OK;
88 * NOTE:
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
91 * values.
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,
99 uint8_t *key_buf)
101 TDB_DATA key;
103 RSIVAL(key_buf, 0, id);
105 key = make_tdb_data(key_buf, SMBXSRV_OPEN_GLOBAL_TDB_KEY_SIZE);
107 return key;
110 #if 0
111 static NTSTATUS smbXsrv_open_global_key_to_id(TDB_DATA key, uint32_t *id)
113 if (id == NULL) {
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);
123 return NT_STATUS_OK;
125 #endif
127 #define SMBXSRV_OPEN_LOCAL_TDB_KEY_SIZE sizeof(uint32_t)
129 static TDB_DATA smbXsrv_open_local_id_to_key(uint32_t id,
130 uint8_t *key_buf)
132 TDB_DATA key;
134 RSIVAL(key_buf, 0, id);
136 key = make_tdb_data(key_buf, SMBXSRV_OPEN_LOCAL_TDB_KEY_SIZE);
138 return key;
141 static NTSTATUS smbXsrv_open_local_key_to_id(TDB_DATA key, uint32_t *id)
143 if (id == NULL) {
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);
153 return NT_STATUS_OK;
156 static struct db_record *smbXsrv_open_global_fetch_locked(
157 struct db_context *db,
158 uint32_t id,
159 TALLOC_CTX *mem_ctx)
161 TDB_DATA key;
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);
169 if (rec == NULL) {
170 DBG_DEBUG("Failed to lock global id 0x%08x, key '%s'\n", id,
171 hex_encode_talloc(talloc_tos(), key.dptr, key.dsize));
174 return rec;
177 static struct db_record *smbXsrv_open_local_fetch_locked(
178 struct db_context *db,
179 uint32_t id,
180 TALLOC_CTX *mem_ctx)
182 TDB_DATA key;
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);
190 if (rec == NULL) {
191 DBG_DEBUG("Failed to lock local id 0x%08x, key '%s'\n", id,
192 hex_encode_talloc(talloc_tos(), key.dptr, key.dsize));
195 return rec;
198 static NTSTATUS smbXsrv_open_table_init(struct smbXsrv_connection *conn,
199 uint32_t lowest_id,
200 uint32_t highest_id,
201 uint32_t max_opens)
203 struct smbXsrv_client *client = conn->client;
204 struct smbXsrv_open_table *table;
205 NTSTATUS status;
206 uint64_t max_range;
208 if (lowest_id > highest_id) {
209 return NT_STATUS_INTERNAL_ERROR;
212 max_range = highest_id;
213 max_range -= lowest_id;
214 max_range += 1;
216 if (max_opens > max_range) {
217 return NT_STATUS_INTERNAL_ERROR;
220 table = talloc_zero(client, struct smbXsrv_open_table);
221 if (table == NULL) {
222 return NT_STATUS_NO_MEMORY;
225 table->local.db_ctx = db_open_rbt(table);
226 if (table->local.db_ctx == NULL) {
227 TALLOC_FREE(table);
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) {
232 TALLOC_FREE(table);
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)) {
241 TALLOC_FREE(table);
242 return status;
245 table->global.db_ctx = smbXsrv_open_global_db_ctx;
247 client->open_table = table;
248 return NT_STATUS_OK;
251 struct smbXsrv_open_local_allocate_state {
252 const uint32_t lowest_id;
253 const uint32_t highest_id;
254 uint32_t last_id;
255 uint32_t useable_id;
256 NTSTATUS status;
259 static int smbXsrv_open_local_allocate_traverse(struct db_record *rec,
260 void *private_data)
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);
265 uint32_t id = 0;
266 NTSTATUS status;
268 status = smbXsrv_open_local_key_to_id(key, &id);
269 if (!NT_STATUS_IS_OK(status)) {
270 state->status = status;
271 return -1;
274 if (id <= state->last_id) {
275 state->status = NT_STATUS_INTERNAL_DB_CORRUPTION;
276 return -1;
278 state->last_id = id;
280 if (id > state->useable_id) {
281 state->status = NT_STATUS_OK;
282 return -1;
285 if (state->useable_id == state->highest_id) {
286 state->status = NT_STATUS_INSUFFICIENT_RESOURCES;
287 return -1;
290 state->useable_id +=1;
291 return 0;
294 static NTSTATUS smbXsrv_open_local_allocate_id(struct db_context *db,
295 uint32_t lowest_id,
296 uint32_t highest_id,
297 TALLOC_CTX *mem_ctx,
298 struct db_record **_rec,
299 uint32_t *_id)
301 struct smbXsrv_open_local_allocate_state state = {
302 .lowest_id = lowest_id,
303 .highest_id = highest_id,
304 .last_id = 0,
305 .useable_id = lowest_id,
306 .status = NT_STATUS_INTERNAL_ERROR,
308 uint32_t i;
309 uint32_t range;
310 NTSTATUS status;
311 int count = 0;
313 *_rec = NULL;
314 *_id = 0;
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++) {
326 uint32_t id;
327 TDB_DATA val;
328 struct db_record *rec = NULL;
330 id = generate_random() % range;
331 id += lowest_id;
333 if (id < lowest_id) {
334 id = lowest_id;
336 if (id > highest_id) {
337 id = highest_id;
340 rec = smbXsrv_open_local_fetch_locked(db, id, mem_ctx);
341 if (rec == NULL) {
342 return NT_STATUS_INSUFFICIENT_RESOURCES;
345 val = dbwrap_record_get_value(rec);
346 if (val.dsize != 0) {
347 TALLOC_FREE(rec);
348 continue;
351 *_rec = rec;
352 *_id = id;
353 return NT_STATUS_OK;
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,
362 &state, &count);
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)) {
369 return state.status;
372 if (state.useable_id <= state.highest_id) {
373 state.status = NT_STATUS_OK;
374 } else {
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
383 * the db.
385 return NT_STATUS_INSUFFICIENT_RESOURCES;
388 if (NT_STATUS_IS_OK(state.status)) {
389 uint32_t id;
390 TDB_DATA val;
391 struct db_record *rec = NULL;
393 id = state.useable_id;
395 rec = smbXsrv_open_local_fetch_locked(db, id, mem_ctx);
396 if (rec == NULL) {
397 return NT_STATUS_INSUFFICIENT_RESOURCES;
400 val = dbwrap_record_get_value(rec);
401 if (val.dsize != 0) {
402 TALLOC_FREE(rec);
403 return NT_STATUS_INTERNAL_DB_CORRUPTION;
406 *_rec = rec;
407 *_id = id;
408 return NT_STATUS_OK;
411 return state.status;
414 struct smbXsrv_open_local_fetch_state {
415 struct smbXsrv_open *op;
416 NTSTATUS status;
419 static void smbXsrv_open_local_fetch_parser(TDB_DATA key, TDB_DATA data,
420 void *private_data)
422 struct smbXsrv_open_local_fetch_state *state =
423 (struct smbXsrv_open_local_fetch_state *)private_data;
424 void *ptr;
426 if (data.dsize != sizeof(ptr)) {
427 state->status = NT_STATUS_INTERNAL_DB_ERROR;
428 return;
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,
439 NTTIME now,
440 struct smbXsrv_open **_open)
442 struct smbXsrv_open_local_fetch_state state = {
443 .op = NULL,
444 .status = NT_STATUS_INTERNAL_ERROR,
446 uint8_t key_buf[SMBXSRV_OPEN_LOCAL_TDB_KEY_SIZE];
447 TDB_DATA key;
448 NTSTATUS status;
450 *_open = NULL;
452 if (open_local_id == 0) {
453 return NT_STATUS_FILE_CLOSED;
456 if (table == NULL) {
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,
469 &state);
470 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
471 return NT_STATUS_FILE_CLOSED;
473 if (!NT_STATUS_IS_OK(status)) {
474 return status;
476 if (!NT_STATUS_IS_OK(state.status)) {
477 return 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;
493 if (now != 0) {
494 state.op->idle_time = now;
497 *_open = state.op;
498 return state.op->status;
501 static int smbXsrv_open_global_destructor(struct smbXsrv_open_global0 *global)
503 return 0;
506 static void smbXsrv_open_global_verify_record(struct db_record *db_rec,
507 bool *is_free,
508 bool *was_free,
509 TALLOC_CTX *mem_ctx,
510 struct smbXsrv_open_global0 **_g);
512 static NTSTATUS smbXsrv_open_global_allocate(struct db_context *db,
513 TALLOC_CTX *mem_ctx,
514 struct smbXsrv_open_global0 **_global)
516 uint32_t i;
517 struct smbXsrv_open_global0 *global = NULL;
518 uint32_t last_free = 0;
519 const uint32_t min_tries = 3;
521 *_global = NULL;
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
540 * ID for SRVSVC.
542 for (i = 0; i < UINT32_MAX; i++) {
543 bool is_free = false;
544 bool was_free = false;
545 uint32_t id;
547 if (i >= min_tries && last_free != 0) {
548 id = last_free;
549 } else {
550 id = generate_random();
552 if (id == 0) {
553 id++;
555 if (id == UINT32_MAX) {
556 id--;
559 global->db_rec = smbXsrv_open_global_fetch_locked(db, id, mem_ctx);
560 if (global->db_rec == NULL) {
561 talloc_free(global);
562 return NT_STATUS_INSUFFICIENT_RESOURCES;
565 smbXsrv_open_global_verify_record(global->db_rec,
566 &is_free,
567 &was_free,
568 NULL, NULL);
570 if (!is_free) {
571 TALLOC_FREE(global->db_rec);
572 continue;
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) {
587 last_free = id;
589 TALLOC_FREE(global->db_rec);
590 continue;
593 global->open_global_id = id;
595 *_global = global;
596 return NT_STATUS_OK;
599 /* should not be reached */
600 talloc_free(global);
601 return NT_STATUS_INTERNAL_ERROR;
604 static void smbXsrv_open_global_verify_record(struct db_record *db_rec,
605 bool *is_free,
606 bool *was_free,
607 TALLOC_CTX *mem_ctx,
608 struct smbXsrv_open_global0 **_g)
610 TDB_DATA key;
611 TDB_DATA val;
612 DATA_BLOB blob;
613 struct smbXsrv_open_globalB global_blob;
614 enum ndr_err_code ndr_err;
615 struct smbXsrv_open_global0 *global = NULL;
616 bool exists;
617 TALLOC_CTX *frame = talloc_stackframe();
619 *is_free = false;
621 if (was_free) {
622 *was_free = false;
624 if (_g) {
625 *_g = NULL;
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__));
633 TALLOC_FREE(frame);
634 *is_free = true;
635 if (was_free) {
636 *was_free = true;
638 return;
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",
649 hex_encode_talloc(frame, key.dptr, key.dsize),
650 nt_errstr(status)));
651 TALLOC_FREE(frame);
652 return;
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",
663 hex_encode_talloc(frame, key.dptr, key.dsize),
664 global_blob.version));
665 NDR_PRINT_DEBUG(smbXsrv_open_globalB, &global_blob);
666 TALLOC_FREE(frame);
667 return;
670 global = global_blob.info.info0;
672 if (server_id_is_disconnected(&global->server_id)) {
673 exists = true;
674 } else {
675 exists = serverid_exists(&global->server_id);
677 if (!exists) {
678 struct server_id_buf idbuf;
679 DEBUG(2,("smbXsrv_open_global_verify_record: "
680 "key '%s' server_id %s does not exist.\n",
681 hex_encode_talloc(frame, key.dptr, key.dsize),
682 server_id_str_buf(global->server_id, &idbuf)));
683 if (CHECK_DEBUGLVL(2)) {
684 NDR_PRINT_DEBUG(smbXsrv_open_globalB, &global_blob);
686 TALLOC_FREE(frame);
687 dbwrap_record_delete(db_rec);
688 *is_free = true;
689 return;
692 if (_g) {
693 *_g = talloc_move(mem_ctx, &global);
695 TALLOC_FREE(frame);
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;
702 TDB_DATA key;
703 TDB_DATA val;
704 NTSTATUS status;
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 ZERO_STRUCT(global_blob);
721 global_blob.version = smbXsrv_version_global_current();
722 if (val.dsize >= 8) {
723 global_blob.seqnum = IVAL(val.dptr, 4);
725 global_blob.seqnum += 1;
726 global_blob.info.info0 = global;
728 ndr_err = ndr_push_struct_blob(&blob, global->db_rec, &global_blob,
729 (ndr_push_flags_fn_t)ndr_push_smbXsrv_open_globalB);
730 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
731 status = ndr_map_error2ntstatus(ndr_err);
732 DEBUG(1,("smbXsrv_open_global_store: key '%s' ndr_push - %s\n",
733 hex_encode_talloc(global->db_rec, key.dptr, key.dsize),
734 nt_errstr(status)));
735 TALLOC_FREE(global->db_rec);
736 return status;
739 val = make_tdb_data(blob.data, blob.length);
740 status = dbwrap_record_store(global->db_rec, val, TDB_REPLACE);
741 if (!NT_STATUS_IS_OK(status)) {
742 DEBUG(1,("smbXsrv_open_global_store: key '%s' store - %s\n",
743 hex_encode_talloc(global->db_rec, key.dptr, key.dsize),
744 nt_errstr(status)));
745 TALLOC_FREE(global->db_rec);
746 return status;
749 if (CHECK_DEBUGLVL(10)) {
750 DEBUG(10,("smbXsrv_open_global_store: key '%s' stored\n",
751 hex_encode_talloc(global->db_rec, key.dptr, key.dsize)));
752 NDR_PRINT_DEBUG(smbXsrv_open_globalB, &global_blob);
755 TALLOC_FREE(global->db_rec);
757 return NT_STATUS_OK;
760 static NTSTATUS smbXsrv_open_global_lookup(struct smbXsrv_open_table *table,
761 uint32_t open_global_id,
762 TALLOC_CTX *mem_ctx,
763 struct smbXsrv_open_global0 **_global)
765 struct db_record *global_rec = NULL;
766 bool is_free = false;
768 *_global = NULL;
770 if (table->global.db_ctx == NULL) {
771 return NT_STATUS_INTERNAL_ERROR;
774 global_rec = smbXsrv_open_global_fetch_locked(table->global.db_ctx,
775 open_global_id,
776 mem_ctx);
777 if (global_rec == NULL) {
778 return NT_STATUS_INTERNAL_DB_ERROR;
781 smbXsrv_open_global_verify_record(global_rec,
782 &is_free,
783 NULL,
784 mem_ctx,
785 _global);
786 if (is_free) {
787 DEBUG(10, ("%s: is_free=true\n", __func__));
788 talloc_free(global_rec);
789 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
792 (*_global)->db_rec = talloc_move(*_global, &global_rec);
794 talloc_set_destructor(*_global, smbXsrv_open_global_destructor);
796 return NT_STATUS_OK;
799 static int smbXsrv_open_destructor(struct smbXsrv_open *op)
801 NTSTATUS status;
803 status = smbXsrv_open_close(op, 0);
804 if (!NT_STATUS_IS_OK(status)) {
805 DEBUG(0, ("smbXsrv_open_destructor: "
806 "smbXsrv_open_close() failed - %s\n",
807 nt_errstr(status)));
810 TALLOC_FREE(op->global);
812 return 0;
815 NTSTATUS smbXsrv_open_create(struct smbXsrv_connection *conn,
816 struct auth_session_info *session_info,
817 NTTIME now,
818 struct smbXsrv_open **_open)
820 struct smbXsrv_open_table *table = conn->client->open_table;
821 struct db_record *local_rec = NULL;
822 struct smbXsrv_open *op = NULL;
823 void *ptr = NULL;
824 TDB_DATA val;
825 struct smbXsrv_open_global0 *global = NULL;
826 NTSTATUS status;
827 struct dom_sid *current_sid = NULL;
828 struct security_token *current_token = NULL;
830 if (session_info == NULL) {
831 return NT_STATUS_INVALID_HANDLE;
833 current_token = session_info->security_token;
835 if (current_token == NULL) {
836 return NT_STATUS_INVALID_HANDLE;
839 if (current_token->num_sids > PRIMARY_USER_SID_INDEX) {
840 current_sid = &current_token->sids[PRIMARY_USER_SID_INDEX];
843 if (current_sid == NULL) {
844 return NT_STATUS_INVALID_HANDLE;
847 if (table->local.num_opens >= table->local.max_opens) {
848 return NT_STATUS_INSUFFICIENT_RESOURCES;
851 op = talloc_zero(table, struct smbXsrv_open);
852 if (op == NULL) {
853 return NT_STATUS_NO_MEMORY;
855 op->table = table;
856 op->status = NT_STATUS_OK; /* TODO: start with INTERNAL_ERROR */
857 op->idle_time = now;
859 status = smbXsrv_open_global_allocate(table->global.db_ctx,
860 op, &global);
861 if (!NT_STATUS_IS_OK(status)) {
862 TALLOC_FREE(op);
863 return status;
865 op->global = global;
867 status = smbXsrv_open_local_allocate_id(table->local.db_ctx,
868 table->local.lowest_id,
869 table->local.highest_id,
871 &local_rec,
872 &op->local_id);
873 if (!NT_STATUS_IS_OK(status)) {
874 TALLOC_FREE(op);
875 return status;
878 global->open_persistent_id = global->open_global_id;
879 global->open_volatile_id = op->local_id;
881 global->server_id = messaging_server_id(conn->client->msg_ctx);
882 global->open_time = now;
883 global->open_owner = *current_sid;
884 if (conn->protocol >= PROTOCOL_SMB2_10) {
885 global->client_guid = conn->smb2.client.guid;
888 ptr = op;
889 val = make_tdb_data((uint8_t const *)&ptr, sizeof(ptr));
890 status = dbwrap_record_store(local_rec, val, TDB_REPLACE);
891 TALLOC_FREE(local_rec);
892 if (!NT_STATUS_IS_OK(status)) {
893 TALLOC_FREE(op);
894 return status;
896 table->local.num_opens += 1;
898 talloc_set_destructor(op, smbXsrv_open_destructor);
900 status = smbXsrv_open_global_store(global);
901 if (!NT_STATUS_IS_OK(status)) {
902 DEBUG(0,("smbXsrv_open_create: "
903 "global_id (0x%08x) store failed - %s\n",
904 op->global->open_global_id,
905 nt_errstr(status)));
906 TALLOC_FREE(op);
907 return status;
910 if (CHECK_DEBUGLVL(10)) {
911 struct smbXsrv_openB open_blob = {
912 .version = SMBXSRV_VERSION_0,
913 .info.info0 = op,
916 DEBUG(10,("smbXsrv_open_create: global_id (0x%08x) stored\n",
917 op->global->open_global_id));
918 NDR_PRINT_DEBUG(smbXsrv_openB, &open_blob);
921 *_open = op;
922 return NT_STATUS_OK;
925 static NTSTATUS smbXsrv_open_set_replay_cache(struct smbXsrv_open *op)
927 struct GUID *create_guid;
928 struct GUID_txt_buf buf;
929 char *guid_string;
930 struct db_context *db = op->table->local.replay_cache_db_ctx;
931 NTSTATUS status;
933 if (!(op->flags & SMBXSRV_OPEN_NEED_REPLAY_CACHE)) {
934 return NT_STATUS_OK;
937 if (op->flags & SMBXSRV_OPEN_HAVE_REPLAY_CACHE) {
938 return NT_STATUS_OK;
941 create_guid = &op->global->create_guid;
942 if (GUID_all_zero(create_guid)) {
943 return NT_STATUS_OK;
946 guid_string = GUID_buf_string(create_guid, &buf);
947 if (guid_string == NULL) {
948 return NT_STATUS_INVALID_PARAMETER;
951 status = dbwrap_store_uint32_bystring(db, guid_string, op->local_id);
953 if (NT_STATUS_IS_OK(status)) {
954 op->flags |= SMBXSRV_OPEN_HAVE_REPLAY_CACHE;
955 op->flags &= ~SMBXSRV_OPEN_NEED_REPLAY_CACHE;
958 return status;
961 static NTSTATUS smbXsrv_open_clear_replay_cache(struct smbXsrv_open *op)
963 struct GUID *create_guid;
964 struct GUID_txt_buf buf;
965 char *guid_string;
966 struct db_context *db;
967 NTSTATUS status;
969 if (op->table == NULL) {
970 return NT_STATUS_OK;
973 db = op->table->local.replay_cache_db_ctx;
975 if (!(op->flags & SMBXSRV_OPEN_HAVE_REPLAY_CACHE)) {
976 return NT_STATUS_OK;
979 create_guid = &op->global->create_guid;
980 if (GUID_all_zero(create_guid)) {
981 return NT_STATUS_OK;
984 guid_string = GUID_buf_string(create_guid, &buf);
985 if (guid_string == NULL) {
986 return NT_STATUS_INVALID_PARAMETER;
989 status = dbwrap_purge_bystring(db, guid_string);
991 if (NT_STATUS_IS_OK(status)) {
992 op->flags &= ~SMBXSRV_OPEN_HAVE_REPLAY_CACHE;
995 return status;
998 NTSTATUS smbXsrv_open_update(struct smbXsrv_open *op)
1000 struct smbXsrv_open_table *table = op->table;
1001 NTSTATUS status;
1003 if (op->global->db_rec != NULL) {
1004 DEBUG(0, ("smbXsrv_open_update(0x%08x): "
1005 "Called with db_rec != NULL'\n",
1006 op->global->open_global_id));
1007 return NT_STATUS_INTERNAL_ERROR;
1010 op->global->db_rec = smbXsrv_open_global_fetch_locked(
1011 table->global.db_ctx,
1012 op->global->open_global_id,
1013 op->global /* TALLOC_CTX */);
1014 if (op->global->db_rec == NULL) {
1015 return NT_STATUS_INTERNAL_DB_ERROR;
1018 status = smbXsrv_open_global_store(op->global);
1019 if (!NT_STATUS_IS_OK(status)) {
1020 DEBUG(0,("smbXsrv_open_update: "
1021 "global_id (0x%08x) store failed - %s\n",
1022 op->global->open_global_id,
1023 nt_errstr(status)));
1024 return status;
1027 status = smbXsrv_open_set_replay_cache(op);
1028 if (!NT_STATUS_IS_OK(status)) {
1029 DBG_ERR("smbXsrv_open_set_replay_cache failed: %s\n",
1030 nt_errstr(status));
1031 return status;
1034 if (CHECK_DEBUGLVL(10)) {
1035 struct smbXsrv_openB open_blob = {
1036 .version = SMBXSRV_VERSION_0,
1037 .info.info0 = op,
1040 DEBUG(10,("smbXsrv_open_update: global_id (0x%08x) stored\n",
1041 op->global->open_global_id));
1042 NDR_PRINT_DEBUG(smbXsrv_openB, &open_blob);
1045 return NT_STATUS_OK;
1048 NTSTATUS smbXsrv_open_close(struct smbXsrv_open *op, NTTIME now)
1050 struct smbXsrv_open_table *table;
1051 struct db_record *local_rec = NULL;
1052 struct db_record *global_rec = NULL;
1053 NTSTATUS status;
1054 NTSTATUS error = NT_STATUS_OK;
1056 error = smbXsrv_open_clear_replay_cache(op);
1057 if (!NT_STATUS_IS_OK(error)) {
1058 DBG_ERR("smbXsrv_open_clear_replay_cache failed: %s\n",
1059 nt_errstr(error));
1062 if (op->table == NULL) {
1063 return error;
1066 table = op->table;
1067 op->table = NULL;
1069 op->status = NT_STATUS_FILE_CLOSED;
1070 op->global->disconnect_time = now;
1071 server_id_set_disconnected(&op->global->server_id);
1073 global_rec = op->global->db_rec;
1074 op->global->db_rec = NULL;
1075 if (global_rec == NULL) {
1076 global_rec = smbXsrv_open_global_fetch_locked(
1077 table->global.db_ctx,
1078 op->global->open_global_id,
1079 op->global /* TALLOC_CTX */);
1080 if (global_rec == NULL) {
1081 error = NT_STATUS_INTERNAL_ERROR;
1085 if (global_rec != NULL && op->global->durable) {
1087 * If it is a durable open we need to update the global part
1088 * instead of deleting it
1090 op->global->db_rec = global_rec;
1091 status = smbXsrv_open_global_store(op->global);
1092 if (NT_STATUS_IS_OK(status)) {
1094 * smbXsrv_open_global_store does the free
1095 * of op->global->db_rec
1097 global_rec = NULL;
1099 if (!NT_STATUS_IS_OK(status)) {
1100 DEBUG(0,("smbXsrv_open_close(0x%08x)"
1101 "smbXsrv_open_global_store() failed - %s\n",
1102 op->global->open_global_id,
1103 nt_errstr(status)));
1104 error = status;
1107 if (NT_STATUS_IS_OK(status) && CHECK_DEBUGLVL(10)) {
1108 struct smbXsrv_openB open_blob = {
1109 .version = SMBXSRV_VERSION_0,
1110 .info.info0 = op,
1113 DEBUG(10,("smbXsrv_open_close(0x%08x): "
1114 "stored disconnect\n",
1115 op->global->open_global_id));
1116 NDR_PRINT_DEBUG(smbXsrv_openB, &open_blob);
1120 if (global_rec != NULL) {
1121 status = dbwrap_record_delete(global_rec);
1122 if (!NT_STATUS_IS_OK(status)) {
1123 TDB_DATA key = dbwrap_record_get_key(global_rec);
1125 DEBUG(0, ("smbXsrv_open_close(0x%08x): "
1126 "failed to delete global key '%s': %s\n",
1127 op->global->open_global_id,
1128 hex_encode_talloc(global_rec, key.dptr,
1129 key.dsize),
1130 nt_errstr(status)));
1131 error = status;
1134 TALLOC_FREE(global_rec);
1136 local_rec = op->db_rec;
1137 if (local_rec == NULL) {
1138 local_rec = smbXsrv_open_local_fetch_locked(table->local.db_ctx,
1139 op->local_id,
1140 op /* TALLOC_CTX*/);
1141 if (local_rec == NULL) {
1142 error = NT_STATUS_INTERNAL_ERROR;
1146 if (local_rec != NULL) {
1147 status = dbwrap_record_delete(local_rec);
1148 if (!NT_STATUS_IS_OK(status)) {
1149 TDB_DATA key = dbwrap_record_get_key(local_rec);
1151 DEBUG(0, ("smbXsrv_open_close(0x%08x): "
1152 "failed to delete local key '%s': %s\n",
1153 op->global->open_global_id,
1154 hex_encode_talloc(local_rec, key.dptr,
1155 key.dsize),
1156 nt_errstr(status)));
1157 error = status;
1159 table->local.num_opens -= 1;
1161 if (op->db_rec == NULL) {
1162 TALLOC_FREE(local_rec);
1164 op->db_rec = NULL;
1166 if (op->compat) {
1167 op->compat->op = NULL;
1168 file_free(NULL, op->compat);
1169 op->compat = NULL;
1172 return error;
1175 NTSTATUS smb1srv_open_table_init(struct smbXsrv_connection *conn)
1177 uint32_t max_opens;
1180 * Allow a range from 1..65534.
1182 * With real_max_open_files possible ids,
1183 * truncated to the SMB1 limit of 16-bit.
1185 * 0 and 0xFFFF are no valid ids.
1187 max_opens = conn->client->sconn->real_max_open_files;
1188 max_opens = MIN(max_opens, UINT16_MAX - 1);
1190 return smbXsrv_open_table_init(conn, 1, UINT16_MAX - 1, max_opens);
1193 NTSTATUS smb1srv_open_lookup(struct smbXsrv_connection *conn,
1194 uint16_t fnum, NTTIME now,
1195 struct smbXsrv_open **_open)
1197 struct smbXsrv_open_table *table = conn->client->open_table;
1198 uint32_t local_id = fnum;
1199 uint32_t global_id = 0;
1201 return smbXsrv_open_local_lookup(table, local_id, global_id, now, _open);
1204 NTSTATUS smb2srv_open_table_init(struct smbXsrv_connection *conn)
1206 uint32_t max_opens;
1209 * Allow a range from 1..4294967294.
1211 * With real_max_open_files possible ids,
1212 * truncated to 16-bit (the same as SMB1 for now).
1214 * 0 and 0xFFFFFFFF are no valid ids.
1216 * The usage of conn->sconn->real_max_open_files
1217 * is the reason that we use one open table per
1218 * transport connection (as we still have a 1:1 mapping
1219 * between process and transport connection).
1221 max_opens = conn->client->sconn->real_max_open_files;
1222 max_opens = MIN(max_opens, UINT16_MAX - 1);
1224 return smbXsrv_open_table_init(conn, 1, UINT32_MAX - 1, max_opens);
1227 NTSTATUS smb2srv_open_lookup(struct smbXsrv_connection *conn,
1228 uint64_t persistent_id,
1229 uint64_t volatile_id,
1230 NTTIME now,
1231 struct smbXsrv_open **_open)
1233 struct smbXsrv_open_table *table = conn->client->open_table;
1234 uint32_t local_id = volatile_id & UINT32_MAX;
1235 uint64_t local_zeros = volatile_id & 0xFFFFFFFF00000000LLU;
1236 uint32_t global_id = persistent_id & UINT32_MAX;
1237 uint64_t global_zeros = persistent_id & 0xFFFFFFFF00000000LLU;
1238 NTSTATUS status;
1240 if (local_zeros != 0) {
1241 return NT_STATUS_FILE_CLOSED;
1244 if (global_zeros != 0) {
1245 return NT_STATUS_FILE_CLOSED;
1248 if (global_id == 0) {
1249 return NT_STATUS_FILE_CLOSED;
1252 status = smbXsrv_open_local_lookup(table, local_id, global_id, now,
1253 _open);
1254 if (!NT_STATUS_IS_OK(status)) {
1255 return status;
1259 * Clear the replay cache for this create_guid if it exists:
1260 * This is based on the assumption that this lookup will be
1261 * triggered by a client request using the file-id for lookup.
1262 * Hence the client has proven that it has in fact seen the
1263 * reply to its initial create call. So subsequent create replays
1264 * should be treated as invalid. Hence the index for create_guid
1265 * lookup needs to be removed.
1267 status = smbXsrv_open_clear_replay_cache(*_open);
1269 return status;
1272 NTSTATUS smb2srv_open_lookup_replay_cache(struct smbXsrv_connection *conn,
1273 const struct GUID *create_guid,
1274 NTTIME now, /* TODO: needed ? */
1275 struct smbXsrv_open **_open)
1277 NTSTATUS status;
1278 char *guid_string;
1279 struct GUID_txt_buf buf;
1280 uint32_t local_id = 0;
1281 struct smbXsrv_open_table *table = conn->client->open_table;
1282 struct db_context *db = table->local.replay_cache_db_ctx;
1284 if (GUID_all_zero(create_guid)) {
1285 return NT_STATUS_NOT_FOUND;
1288 guid_string = GUID_buf_string(create_guid, &buf);
1289 if (guid_string == NULL) {
1290 return NT_STATUS_INVALID_PARAMETER;
1293 status = dbwrap_fetch_uint32_bystring(db, guid_string, &local_id);
1294 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
1295 return status;
1297 if (!NT_STATUS_IS_OK(status)) {
1298 DBG_ERR("failed to fetch local_id from replay cache: %s\n",
1299 nt_errstr(status));
1300 return status;
1303 status = smbXsrv_open_local_lookup(table, local_id, 0, /* global_id */
1304 now, _open);
1305 if (!NT_STATUS_IS_OK(status)) {
1306 DBG_ERR("smbXsrv_open_local_lookup failed for local_id %u\n",
1307 (unsigned)local_id);
1310 return status;
1313 NTSTATUS smb2srv_open_recreate(struct smbXsrv_connection *conn,
1314 struct auth_session_info *session_info,
1315 uint64_t persistent_id,
1316 const struct GUID *create_guid,
1317 NTTIME now,
1318 struct smbXsrv_open **_open)
1320 struct smbXsrv_open_table *table = conn->client->open_table;
1321 struct db_record *local_rec = NULL;
1322 struct smbXsrv_open *op = NULL;
1323 void *ptr = NULL;
1324 TDB_DATA val;
1325 uint32_t global_id = persistent_id & UINT32_MAX;
1326 uint64_t global_zeros = persistent_id & 0xFFFFFFFF00000000LLU;
1327 NTSTATUS status;
1328 struct security_token *current_token = NULL;
1330 if (session_info == NULL) {
1331 DEBUG(10, ("session_info=NULL\n"));
1332 return NT_STATUS_INVALID_HANDLE;
1334 current_token = session_info->security_token;
1336 if (current_token == NULL) {
1337 DEBUG(10, ("current_token=NULL\n"));
1338 return NT_STATUS_INVALID_HANDLE;
1341 if (global_zeros != 0) {
1342 DEBUG(10, ("global_zeros!=0\n"));
1343 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1346 op = talloc_zero(table, struct smbXsrv_open);
1347 if (op == NULL) {
1348 return NT_STATUS_NO_MEMORY;
1350 op->table = table;
1352 status = smbXsrv_open_global_lookup(table, global_id, op, &op->global);
1353 if (!NT_STATUS_IS_OK(status)) {
1354 TALLOC_FREE(op);
1355 DEBUG(10, ("smbXsrv_open_global_lookup returned %s\n",
1356 nt_errstr(status)));
1357 return status;
1361 * If the provided create_guid is NULL, this means that
1362 * the reconnect request was a v1 request. In that case
1363 * we should skipt the create GUID verification, since
1364 * it is valid to v1-reconnect a v2-opened handle.
1366 if ((create_guid != NULL) &&
1367 !GUID_equal(&op->global->create_guid, create_guid))
1369 TALLOC_FREE(op);
1370 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1373 if (!security_token_is_sid(current_token, &op->global->open_owner)) {
1374 TALLOC_FREE(op);
1375 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1378 if (!op->global->durable) {
1379 TALLOC_FREE(op);
1380 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1383 if (table->local.num_opens >= table->local.max_opens) {
1384 TALLOC_FREE(op);
1385 return NT_STATUS_INSUFFICIENT_RESOURCES;
1388 status = smbXsrv_open_local_allocate_id(table->local.db_ctx,
1389 table->local.lowest_id,
1390 table->local.highest_id,
1392 &local_rec,
1393 &op->local_id);
1394 if (!NT_STATUS_IS_OK(status)) {
1395 TALLOC_FREE(op);
1396 return status;
1399 op->idle_time = now;
1400 op->status = NT_STATUS_FILE_CLOSED;
1402 op->global->open_volatile_id = op->local_id;
1403 op->global->server_id = messaging_server_id(conn->client->msg_ctx);
1405 ptr = op;
1406 val = make_tdb_data((uint8_t const *)&ptr, sizeof(ptr));
1407 status = dbwrap_record_store(local_rec, val, TDB_REPLACE);
1408 TALLOC_FREE(local_rec);
1409 if (!NT_STATUS_IS_OK(status)) {
1410 TALLOC_FREE(op);
1411 return status;
1413 table->local.num_opens += 1;
1415 talloc_set_destructor(op, smbXsrv_open_destructor);
1417 status = smbXsrv_open_global_store(op->global);
1418 if (!NT_STATUS_IS_OK(status)) {
1419 TALLOC_FREE(op);
1420 return status;
1423 if (CHECK_DEBUGLVL(10)) {
1424 struct smbXsrv_openB open_blob = {
1425 .info.info0 = op,
1428 DEBUG(10,("smbXsrv_open_recreate: global_id (0x%08x) stored\n",
1429 op->global->open_global_id));
1430 NDR_PRINT_DEBUG(smbXsrv_openB, &open_blob);
1433 *_open = op;
1434 return NT_STATUS_OK;
1438 static NTSTATUS smbXsrv_open_global_parse_record(TALLOC_CTX *mem_ctx,
1439 struct db_record *rec,
1440 struct smbXsrv_open_global0 **global)
1442 TDB_DATA key = dbwrap_record_get_key(rec);
1443 TDB_DATA val = dbwrap_record_get_value(rec);
1444 DATA_BLOB blob = data_blob_const(val.dptr, val.dsize);
1445 struct smbXsrv_open_globalB global_blob;
1446 enum ndr_err_code ndr_err;
1447 NTSTATUS status;
1448 TALLOC_CTX *frame = talloc_stackframe();
1450 ndr_err = ndr_pull_struct_blob(&blob, frame, &global_blob,
1451 (ndr_pull_flags_fn_t)ndr_pull_smbXsrv_open_globalB);
1452 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1453 DEBUG(1,("Invalid record in smbXsrv_open_global.tdb:"
1454 "key '%s' ndr_pull_struct_blob - %s\n",
1455 hex_encode_talloc(frame, key.dptr, key.dsize),
1456 ndr_errstr(ndr_err)));
1457 status = ndr_map_error2ntstatus(ndr_err);
1458 goto done;
1461 if (global_blob.version != SMBXSRV_VERSION_0) {
1462 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
1463 DEBUG(1,("Invalid record in smbXsrv_open_global.tdb:"
1464 "key '%s' unsupported version - %d - %s\n",
1465 hex_encode_talloc(frame, key.dptr, key.dsize),
1466 (int)global_blob.version,
1467 nt_errstr(status)));
1468 goto done;
1471 if (global_blob.info.info0 == NULL) {
1472 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
1473 DEBUG(1,("Invalid record in smbXsrv_tcon_global.tdb:"
1474 "key '%s' info0 NULL pointer - %s\n",
1475 hex_encode_talloc(frame, key.dptr, key.dsize),
1476 nt_errstr(status)));
1477 goto done;
1480 *global = talloc_move(mem_ctx, &global_blob.info.info0);
1481 status = NT_STATUS_OK;
1482 done:
1483 talloc_free(frame);
1484 return status;
1487 struct smbXsrv_open_global_traverse_state {
1488 int (*fn)(struct smbXsrv_open_global0 *, void *);
1489 void *private_data;
1492 static int smbXsrv_open_global_traverse_fn(struct db_record *rec, void *data)
1494 struct smbXsrv_open_global_traverse_state *state =
1495 (struct smbXsrv_open_global_traverse_state*)data;
1496 struct smbXsrv_open_global0 *global = NULL;
1497 NTSTATUS status;
1498 int ret = -1;
1500 status = smbXsrv_open_global_parse_record(talloc_tos(), rec, &global);
1501 if (!NT_STATUS_IS_OK(status)) {
1502 return -1;
1505 global->db_rec = rec;
1506 ret = state->fn(global, state->private_data);
1507 talloc_free(global);
1508 return ret;
1511 NTSTATUS smbXsrv_open_global_traverse(
1512 int (*fn)(struct smbXsrv_open_global0 *, void *),
1513 void *private_data)
1516 NTSTATUS status;
1517 int count = 0;
1518 struct smbXsrv_open_global_traverse_state state = {
1519 .fn = fn,
1520 .private_data = private_data,
1523 become_root();
1524 status = smbXsrv_open_global_init();
1525 if (!NT_STATUS_IS_OK(status)) {
1526 unbecome_root();
1527 DEBUG(0, ("Failed to initialize open_global: %s\n",
1528 nt_errstr(status)));
1529 return status;
1532 status = dbwrap_traverse_read(smbXsrv_open_global_db_ctx,
1533 smbXsrv_open_global_traverse_fn,
1534 &state,
1535 &count);
1536 unbecome_root();
1538 return status;
1541 NTSTATUS smbXsrv_open_cleanup(uint64_t persistent_id)
1543 NTSTATUS status = NT_STATUS_OK;
1544 TALLOC_CTX *frame = talloc_stackframe();
1545 struct smbXsrv_open_global0 *op = NULL;
1546 TDB_DATA val;
1547 struct db_record *rec;
1548 bool delete_open = false;
1549 uint32_t global_id = persistent_id & UINT32_MAX;
1551 rec = smbXsrv_open_global_fetch_locked(smbXsrv_open_global_db_ctx,
1552 global_id,
1553 frame);
1554 if (rec == NULL) {
1555 status = NT_STATUS_NOT_FOUND;
1556 goto done;
1559 val = dbwrap_record_get_value(rec);
1560 if (val.dsize == 0) {
1561 DEBUG(10, ("smbXsrv_open_cleanup[global: 0x%08x] "
1562 "empty record in %s, skipping...\n",
1563 global_id, dbwrap_name(smbXsrv_open_global_db_ctx)));
1564 goto done;
1567 status = smbXsrv_open_global_parse_record(talloc_tos(), rec, &op);
1568 if (!NT_STATUS_IS_OK(status)) {
1569 DEBUG(1, ("smbXsrv_open_cleanup[global: 0x%08x] "
1570 "failed to read record: %s\n",
1571 global_id, nt_errstr(status)));
1572 goto done;
1575 if (server_id_is_disconnected(&op->server_id)) {
1576 struct timeval now, disconnect_time;
1577 int64_t tdiff;
1578 now = timeval_current();
1579 nttime_to_timeval(&disconnect_time, op->disconnect_time);
1580 tdiff = usec_time_diff(&now, &disconnect_time);
1581 delete_open = (tdiff >= 1000*op->durable_timeout_msec);
1583 DEBUG(10, ("smbXsrv_open_cleanup[global: 0x%08x] "
1584 "disconnected at [%s] %us ago with "
1585 "timeout of %us -%s reached\n",
1586 global_id,
1587 nt_time_string(frame, op->disconnect_time),
1588 (unsigned)(tdiff/1000000),
1589 op->durable_timeout_msec / 1000,
1590 delete_open ? "" : " not"));
1591 } else if (!serverid_exists(&op->server_id)) {
1592 struct server_id_buf idbuf;
1593 DEBUG(10, ("smbXsrv_open_cleanup[global: 0x%08x] "
1594 "server[%s] does not exist\n",
1595 global_id,
1596 server_id_str_buf(op->server_id, &idbuf)));
1597 delete_open = true;
1600 if (!delete_open) {
1601 goto done;
1604 status = dbwrap_record_delete(rec);
1605 if (!NT_STATUS_IS_OK(status)) {
1606 DEBUG(1, ("smbXsrv_open_cleanup[global: 0x%08x] "
1607 "failed to delete record"
1608 "from %s: %s\n", global_id,
1609 dbwrap_name(smbXsrv_open_global_db_ctx),
1610 nt_errstr(status)));
1611 goto done;
1614 DEBUG(10, ("smbXsrv_open_cleanup[global: 0x%08x] "
1615 "delete record from %s\n",
1616 global_id,
1617 dbwrap_name(smbXsrv_open_global_db_ctx)));
1619 done:
1620 talloc_free(frame);
1621 return status;