smbd: Simplify smbXsrv_open_set_replay_cache() with a struct assignment
[Samba.git] / source3 / smbd / smbXsrv_open.c
blobd2628c218eaddbf1cacf57012f1f3a1aa936cbcb
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"
37 struct smbXsrv_open_table {
38 struct {
39 struct db_context *db_ctx;
40 struct db_context *replay_cache_db_ctx;
41 uint32_t lowest_id;
42 uint32_t highest_id;
43 uint32_t max_opens;
44 uint32_t num_opens;
45 } local;
46 struct {
47 struct db_context *db_ctx;
48 } global;
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) {
59 return NT_STATUS_OK;
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,
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 tdb_data_dbg(key));
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 tdb_data_dbg(key));
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 tdb_data_dbg(key),
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 tdb_data_dbg(key),
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 tdb_data_dbg(key),
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 key = dbwrap_record_get_key(global->db_rec);
714 val = dbwrap_record_get_value(global->db_rec);
716 global_blob = (struct smbXsrv_open_globalB) {
717 .version = smbXsrv_version_global_current(),
720 if (val.dsize >= 8) {
721 global_blob.seqnum = IVAL(val.dptr, 4);
723 global_blob.seqnum += 1;
724 global_blob.info.info0 = global;
726 ndr_err = ndr_push_struct_blob(&blob, talloc_tos(), &global_blob,
727 (ndr_push_flags_fn_t)ndr_push_smbXsrv_open_globalB);
728 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
729 status = ndr_map_error2ntstatus(ndr_err);
730 DEBUG(1,("smbXsrv_open_global_store: key '%s' ndr_push - %s\n",
731 tdb_data_dbg(key),
732 nt_errstr(status)));
733 TALLOC_FREE(global->db_rec);
734 return status;
737 val = make_tdb_data(blob.data, blob.length);
738 status = dbwrap_record_store(global->db_rec, val, TDB_REPLACE);
739 TALLOC_FREE(blob.data);
740 if (!NT_STATUS_IS_OK(status)) {
741 DEBUG(1,("smbXsrv_open_global_store: key '%s' store - %s\n",
742 tdb_data_dbg(key),
743 nt_errstr(status)));
744 TALLOC_FREE(global->db_rec);
745 return status;
748 if (CHECK_DEBUGLVL(10)) {
749 DEBUG(10,("smbXsrv_open_global_store: key '%s' stored\n",
750 tdb_data_dbg(key)));
751 NDR_PRINT_DEBUG(smbXsrv_open_globalB, &global_blob);
754 TALLOC_FREE(global->db_rec);
756 return NT_STATUS_OK;
759 static NTSTATUS smbXsrv_open_global_lookup(struct smbXsrv_open_table *table,
760 uint32_t open_global_id,
761 TALLOC_CTX *mem_ctx,
762 struct smbXsrv_open_global0 **_global)
764 struct db_record *global_rec = NULL;
765 bool is_free = false;
767 *_global = NULL;
769 if (table->global.db_ctx == NULL) {
770 return NT_STATUS_INTERNAL_ERROR;
773 global_rec = smbXsrv_open_global_fetch_locked(table->global.db_ctx,
774 open_global_id,
775 mem_ctx);
776 if (global_rec == NULL) {
777 return NT_STATUS_INTERNAL_DB_ERROR;
780 smbXsrv_open_global_verify_record(global_rec,
781 &is_free,
782 NULL,
783 mem_ctx,
784 _global);
785 if (is_free) {
786 DEBUG(10, ("%s: is_free=true\n", __func__));
787 talloc_free(global_rec);
788 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
791 (*_global)->db_rec = talloc_move(*_global, &global_rec);
793 talloc_set_destructor(*_global, smbXsrv_open_global_destructor);
795 return NT_STATUS_OK;
798 static int smbXsrv_open_destructor(struct smbXsrv_open *op)
800 NTSTATUS status;
802 status = smbXsrv_open_close(op, 0);
803 if (!NT_STATUS_IS_OK(status)) {
804 DEBUG(0, ("smbXsrv_open_destructor: "
805 "smbXsrv_open_close() failed - %s\n",
806 nt_errstr(status)));
809 TALLOC_FREE(op->global);
811 return 0;
814 NTSTATUS smbXsrv_open_create(struct smbXsrv_connection *conn,
815 struct auth_session_info *session_info,
816 NTTIME now,
817 struct smbXsrv_open **_open)
819 struct smbXsrv_open_table *table = conn->client->open_table;
820 struct db_record *local_rec = NULL;
821 struct smbXsrv_open *op = NULL;
822 void *ptr = NULL;
823 TDB_DATA val;
824 struct smbXsrv_open_global0 *global = NULL;
825 NTSTATUS status;
826 struct dom_sid *current_sid = NULL;
827 struct security_token *current_token = NULL;
829 if (session_info == NULL) {
830 return NT_STATUS_INVALID_HANDLE;
832 current_token = session_info->security_token;
834 if (current_token == NULL) {
835 return NT_STATUS_INVALID_HANDLE;
838 if (current_token->num_sids > PRIMARY_USER_SID_INDEX) {
839 current_sid = &current_token->sids[PRIMARY_USER_SID_INDEX];
842 if (current_sid == NULL) {
843 return NT_STATUS_INVALID_HANDLE;
846 if (table->local.num_opens >= table->local.max_opens) {
847 return NT_STATUS_INSUFFICIENT_RESOURCES;
850 op = talloc_zero(table, struct smbXsrv_open);
851 if (op == NULL) {
852 return NT_STATUS_NO_MEMORY;
854 op->table = table;
855 op->status = NT_STATUS_OK; /* TODO: start with INTERNAL_ERROR */
856 op->idle_time = now;
858 status = smbXsrv_open_global_allocate(table->global.db_ctx,
859 op, &global);
860 if (!NT_STATUS_IS_OK(status)) {
861 TALLOC_FREE(op);
862 return status;
864 op->global = global;
866 status = smbXsrv_open_local_allocate_id(table->local.db_ctx,
867 table->local.lowest_id,
868 table->local.highest_id,
870 &local_rec,
871 &op->local_id);
872 if (!NT_STATUS_IS_OK(status)) {
873 TALLOC_FREE(op);
874 return status;
877 global->open_persistent_id = global->open_global_id;
878 global->open_volatile_id = op->local_id;
880 global->server_id = messaging_server_id(conn->client->msg_ctx);
881 global->open_time = now;
882 global->open_owner = *current_sid;
883 if (conn->protocol >= PROTOCOL_SMB2_10) {
884 global->client_guid = conn->smb2.client.guid;
887 ptr = op;
888 val = make_tdb_data((uint8_t const *)&ptr, sizeof(ptr));
889 status = dbwrap_record_store(local_rec, val, TDB_REPLACE);
890 TALLOC_FREE(local_rec);
891 if (!NT_STATUS_IS_OK(status)) {
892 TALLOC_FREE(op);
893 return status;
895 table->local.num_opens += 1;
897 talloc_set_destructor(op, smbXsrv_open_destructor);
899 status = smbXsrv_open_global_store(global);
900 if (!NT_STATUS_IS_OK(status)) {
901 DEBUG(0,("smbXsrv_open_create: "
902 "global_id (0x%08x) store failed - %s\n",
903 op->global->open_global_id,
904 nt_errstr(status)));
905 TALLOC_FREE(op);
906 return status;
909 if (CHECK_DEBUGLVL(10)) {
910 struct smbXsrv_openB open_blob = {
911 .version = SMBXSRV_VERSION_0,
912 .info.info0 = op,
915 DEBUG(10,("smbXsrv_open_create: global_id (0x%08x) stored\n",
916 op->global->open_global_id));
917 NDR_PRINT_DEBUG(smbXsrv_openB, &open_blob);
920 *_open = op;
921 return NT_STATUS_OK;
924 static NTSTATUS smbXsrv_open_set_replay_cache(struct smbXsrv_open *op)
926 struct GUID *create_guid;
927 struct GUID_txt_buf buf;
928 char *guid_string;
929 struct db_context *db = op->table->local.replay_cache_db_ctx;
930 struct smbXsrv_open_replay_cache rc = {
931 .idle_time = op->idle_time,
932 .local_id = op->local_id,
934 uint8_t data[SMBXSRV_OPEN_REPLAY_CACHE_FIXED_SIZE] = { 0 };
935 DATA_BLOB blob = { .data = data, .length = sizeof(data), };
936 enum ndr_err_code ndr_err;
937 NTSTATUS status;
938 TDB_DATA key;
939 TDB_DATA val;
941 if (!(op->flags & SMBXSRV_OPEN_NEED_REPLAY_CACHE)) {
942 return NT_STATUS_OK;
945 if (op->flags & SMBXSRV_OPEN_HAVE_REPLAY_CACHE) {
946 return NT_STATUS_OK;
949 create_guid = &op->global->create_guid;
950 guid_string = GUID_buf_string(create_guid, &buf);
951 key = string_term_tdb_data(guid_string);
953 ndr_err = ndr_push_struct_into_fixed_blob(&blob, &rc,
954 (ndr_push_flags_fn_t)ndr_push_smbXsrv_open_replay_cache);
955 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
956 status = ndr_map_error2ntstatus(ndr_err);
957 return status;
959 val = make_tdb_data(blob.data, blob.length);
961 status = dbwrap_store(db, key, val, TDB_REPLACE);
963 if (NT_STATUS_IS_OK(status)) {
964 op->flags |= SMBXSRV_OPEN_HAVE_REPLAY_CACHE;
965 op->flags &= ~SMBXSRV_OPEN_NEED_REPLAY_CACHE;
968 return status;
971 NTSTATUS smbXsrv_open_purge_replay_cache(struct smbXsrv_client *client,
972 const struct GUID *create_guid)
974 struct GUID_txt_buf buf;
975 char *guid_string;
976 struct db_context *db;
978 if (client->open_table == NULL) {
979 return NT_STATUS_OK;
982 db = client->open_table->local.replay_cache_db_ctx;
984 guid_string = GUID_buf_string(create_guid, &buf);
985 if (guid_string == NULL) {
986 return NT_STATUS_INVALID_PARAMETER;
989 return dbwrap_purge_bystring(db, guid_string);
992 static NTSTATUS smbXsrv_open_clear_replay_cache(struct smbXsrv_open *op)
994 struct GUID *create_guid;
995 struct GUID_txt_buf buf;
996 char *guid_string;
997 struct db_context *db;
998 NTSTATUS status;
1000 if (op->table == NULL) {
1001 return NT_STATUS_OK;
1004 db = op->table->local.replay_cache_db_ctx;
1006 if (!(op->flags & SMBXSRV_OPEN_HAVE_REPLAY_CACHE)) {
1007 return NT_STATUS_OK;
1010 create_guid = &op->global->create_guid;
1011 if (GUID_all_zero(create_guid)) {
1012 return NT_STATUS_OK;
1015 guid_string = GUID_buf_string(create_guid, &buf);
1016 if (guid_string == NULL) {
1017 return NT_STATUS_INVALID_PARAMETER;
1020 status = dbwrap_purge_bystring(db, guid_string);
1022 if (NT_STATUS_IS_OK(status)) {
1023 op->flags &= ~SMBXSRV_OPEN_HAVE_REPLAY_CACHE;
1026 return status;
1029 NTSTATUS smbXsrv_open_update(struct smbXsrv_open *op)
1031 struct smbXsrv_open_table *table = op->table;
1032 NTSTATUS status;
1034 if (op->global->db_rec != NULL) {
1035 DEBUG(0, ("smbXsrv_open_update(0x%08x): "
1036 "Called with db_rec != NULL'\n",
1037 op->global->open_global_id));
1038 return NT_STATUS_INTERNAL_ERROR;
1041 op->global->db_rec = smbXsrv_open_global_fetch_locked(
1042 table->global.db_ctx,
1043 op->global->open_global_id,
1044 op->global /* TALLOC_CTX */);
1045 if (op->global->db_rec == NULL) {
1046 return NT_STATUS_INTERNAL_DB_ERROR;
1049 status = smbXsrv_open_global_store(op->global);
1050 if (!NT_STATUS_IS_OK(status)) {
1051 DEBUG(0,("smbXsrv_open_update: "
1052 "global_id (0x%08x) store failed - %s\n",
1053 op->global->open_global_id,
1054 nt_errstr(status)));
1055 return status;
1058 status = smbXsrv_open_set_replay_cache(op);
1059 if (!NT_STATUS_IS_OK(status)) {
1060 DBG_ERR("smbXsrv_open_set_replay_cache failed: %s\n",
1061 nt_errstr(status));
1062 return status;
1065 if (CHECK_DEBUGLVL(10)) {
1066 struct smbXsrv_openB open_blob = {
1067 .version = SMBXSRV_VERSION_0,
1068 .info.info0 = op,
1071 DEBUG(10,("smbXsrv_open_update: global_id (0x%08x) stored\n",
1072 op->global->open_global_id));
1073 NDR_PRINT_DEBUG(smbXsrv_openB, &open_blob);
1076 return NT_STATUS_OK;
1079 NTSTATUS smbXsrv_open_close(struct smbXsrv_open *op, NTTIME now)
1081 struct smbXsrv_open_table *table;
1082 struct db_record *local_rec = NULL;
1083 struct db_record *global_rec = NULL;
1084 NTSTATUS status;
1085 NTSTATUS error = NT_STATUS_OK;
1087 error = smbXsrv_open_clear_replay_cache(op);
1088 if (!NT_STATUS_IS_OK(error)) {
1089 DBG_ERR("smbXsrv_open_clear_replay_cache failed: %s\n",
1090 nt_errstr(error));
1093 if (op->table == NULL) {
1094 return error;
1097 table = op->table;
1098 op->table = NULL;
1100 op->status = NT_STATUS_FILE_CLOSED;
1101 op->global->disconnect_time = now;
1102 server_id_set_disconnected(&op->global->server_id);
1104 global_rec = op->global->db_rec;
1105 op->global->db_rec = NULL;
1106 if (global_rec == NULL) {
1107 global_rec = smbXsrv_open_global_fetch_locked(
1108 table->global.db_ctx,
1109 op->global->open_global_id,
1110 op->global /* TALLOC_CTX */);
1111 if (global_rec == NULL) {
1112 error = NT_STATUS_INTERNAL_ERROR;
1116 if (global_rec != NULL && op->global->durable) {
1118 * If it is a durable open we need to update the global part
1119 * instead of deleting it
1121 op->global->db_rec = global_rec;
1122 status = smbXsrv_open_global_store(op->global);
1123 if (NT_STATUS_IS_OK(status)) {
1125 * smbXsrv_open_global_store does the free
1126 * of op->global->db_rec
1128 global_rec = NULL;
1130 if (!NT_STATUS_IS_OK(status)) {
1131 DEBUG(0,("smbXsrv_open_close(0x%08x)"
1132 "smbXsrv_open_global_store() failed - %s\n",
1133 op->global->open_global_id,
1134 nt_errstr(status)));
1135 error = status;
1138 if (NT_STATUS_IS_OK(status) && CHECK_DEBUGLVL(10)) {
1139 struct smbXsrv_openB open_blob = {
1140 .version = SMBXSRV_VERSION_0,
1141 .info.info0 = op,
1144 DEBUG(10,("smbXsrv_open_close(0x%08x): "
1145 "stored disconnect\n",
1146 op->global->open_global_id));
1147 NDR_PRINT_DEBUG(smbXsrv_openB, &open_blob);
1151 if (global_rec != NULL) {
1152 status = dbwrap_record_delete(global_rec);
1153 if (!NT_STATUS_IS_OK(status)) {
1154 TDB_DATA key = dbwrap_record_get_key(global_rec);
1156 DEBUG(0, ("smbXsrv_open_close(0x%08x): "
1157 "failed to delete global key '%s': %s\n",
1158 op->global->open_global_id,
1159 tdb_data_dbg(key),
1160 nt_errstr(status)));
1161 error = status;
1164 TALLOC_FREE(global_rec);
1166 local_rec = smbXsrv_open_local_fetch_locked(table->local.db_ctx,
1167 op->local_id,
1168 op /* TALLOC_CTX*/);
1169 if (local_rec == NULL) {
1170 error = NT_STATUS_INTERNAL_ERROR;
1173 status = dbwrap_record_delete(local_rec);
1174 if (!NT_STATUS_IS_OK(status)) {
1175 TDB_DATA key = dbwrap_record_get_key(local_rec);
1177 DEBUG(0, ("smbXsrv_open_close(0x%08x): "
1178 "failed to delete local key '%s': %s\n",
1179 op->global->open_global_id,
1180 tdb_data_dbg(key),
1181 nt_errstr(status)));
1182 error = status;
1184 table->local.num_opens -= 1;
1186 TALLOC_FREE(local_rec);
1188 if (op->compat) {
1189 op->compat->op = NULL;
1190 file_free(NULL, op->compat);
1191 op->compat = NULL;
1194 return error;
1197 NTSTATUS smb1srv_open_table_init(struct smbXsrv_connection *conn)
1199 uint32_t max_opens;
1202 * Allow a range from 1..65534.
1204 * With real_max_open_files possible ids,
1205 * truncated to the SMB1 limit of 16-bit.
1207 * 0 and 0xFFFF are no valid ids.
1209 max_opens = conn->client->sconn->real_max_open_files;
1210 max_opens = MIN(max_opens, UINT16_MAX - 1);
1212 return smbXsrv_open_table_init(conn, 1, UINT16_MAX - 1, max_opens);
1215 NTSTATUS smb1srv_open_lookup(struct smbXsrv_connection *conn,
1216 uint16_t fnum, NTTIME now,
1217 struct smbXsrv_open **_open)
1219 struct smbXsrv_open_table *table = conn->client->open_table;
1220 uint32_t local_id = fnum;
1221 uint32_t global_id = 0;
1223 return smbXsrv_open_local_lookup(table, local_id, global_id, now, _open);
1226 NTSTATUS smb2srv_open_table_init(struct smbXsrv_connection *conn)
1228 uint32_t max_opens;
1231 * Allow a range from 1..4294967294.
1233 * With real_max_open_files possible ids,
1234 * truncated to 16-bit (the same as SMB1 for now).
1236 * 0 and 0xFFFFFFFF are no valid ids.
1238 * The usage of conn->sconn->real_max_open_files
1239 * is the reason that we use one open table per
1240 * transport connection (as we still have a 1:1 mapping
1241 * between process and transport connection).
1243 max_opens = conn->client->sconn->real_max_open_files;
1244 max_opens = MIN(max_opens, UINT16_MAX - 1);
1246 return smbXsrv_open_table_init(conn, 1, UINT32_MAX - 1, max_opens);
1249 NTSTATUS smb2srv_open_lookup(struct smbXsrv_connection *conn,
1250 uint64_t persistent_id,
1251 uint64_t volatile_id,
1252 NTTIME now,
1253 struct smbXsrv_open **_open)
1255 struct smbXsrv_open_table *table = conn->client->open_table;
1256 uint32_t local_id = volatile_id & UINT32_MAX;
1257 uint64_t local_zeros = volatile_id & 0xFFFFFFFF00000000LLU;
1258 uint32_t global_id = persistent_id & UINT32_MAX;
1259 uint64_t global_zeros = persistent_id & 0xFFFFFFFF00000000LLU;
1260 NTSTATUS status;
1262 if (local_zeros != 0) {
1263 return NT_STATUS_FILE_CLOSED;
1266 if (global_zeros != 0) {
1267 return NT_STATUS_FILE_CLOSED;
1270 if (global_id == 0) {
1271 return NT_STATUS_FILE_CLOSED;
1274 status = smbXsrv_open_local_lookup(table, local_id, global_id, now,
1275 _open);
1276 if (!NT_STATUS_IS_OK(status)) {
1277 return status;
1281 * Clear the replay cache for this create_guid if it exists:
1282 * This is based on the assumption that this lookup will be
1283 * triggered by a client request using the file-id for lookup.
1284 * Hence the client has proven that it has in fact seen the
1285 * reply to its initial create call. So subsequent create replays
1286 * should be treated as invalid. Hence the index for create_guid
1287 * lookup needs to be removed.
1289 status = smbXsrv_open_clear_replay_cache(*_open);
1291 return status;
1295 * This checks or marks the replay cache, we have the following
1296 * cases:
1298 * 1. There is no record in the cache
1299 * => we add the passes caller_req_guid as holder_req_guid
1300 * together with local_id as 0.
1301 * => We return STATUS_FWP_RESERVED in order to indicate
1302 * that the caller holds the current reservation
1304 * 2. There is a record in the cache and holder_req_guid
1305 * is already the same as caller_req_guid and local_id is 0
1306 * => We return STATUS_FWP_RESERVED in order to indicate
1307 * that the caller holds the current reservation
1309 * 3. There is a record in the cache with a holder_req_guid
1310 * other than caller_req_guid (and local_id is 0):
1311 * => We return NT_STATUS_FILE_NOT_AVAILABLE to indicate
1312 * the original request is still pending
1314 * 4. There is a record in the cache with a zero holder_req_guid
1315 * and a valid local_id:
1316 * => We lookup the existing open by local_id
1317 * => We return NT_STATUS_OK together with the smbXsrv_open
1320 * With NT_STATUS_OK the caller can continue the replay processing.
1322 * With STATUS_FWP_RESERVED the caller should continue the normal
1323 * open processing:
1324 * - On success:
1325 * - smbXsrv_open_update()/smbXsrv_open_set_replay_cache()
1326 * will convert the record to a zero holder_req_guid
1327 * with a valid local_id.
1328 * - On failure:
1329 * - smbXsrv_open_purge_replay_cache() should cleanup
1330 * the reservation.
1332 * All other values should be returned to the client,
1333 * while NT_STATUS_FILE_NOT_AVAILABLE will trigger the
1334 * retry loop on the client.
1336 NTSTATUS smb2srv_open_lookup_replay_cache(struct smbXsrv_connection *conn,
1337 struct GUID caller_req_guid,
1338 struct GUID create_guid,
1339 const char *name,
1340 NTTIME now,
1341 struct smbXsrv_open **_open)
1343 TALLOC_CTX *frame = talloc_stackframe();
1344 NTSTATUS status;
1345 struct smbXsrv_open_table *table = conn->client->open_table;
1346 struct db_context *db = table->local.replay_cache_db_ctx;
1347 struct GUID_txt_buf _create_guid_buf;
1348 struct GUID_txt_buf tmp_guid_buf;
1349 const char *create_guid_str = NULL;
1350 TDB_DATA create_guid_key;
1351 struct db_record *db_rec = NULL;
1352 struct smbXsrv_open *op = NULL;
1353 struct smbXsrv_open_replay_cache rc = {
1354 .holder_req_guid = caller_req_guid,
1355 .idle_time = now,
1356 .local_id = 0,
1358 enum ndr_err_code ndr_err;
1359 DATA_BLOB blob = data_blob_null;
1360 TDB_DATA val;
1362 *_open = NULL;
1364 create_guid_str = GUID_buf_string(&create_guid, &_create_guid_buf);
1365 create_guid_key = string_term_tdb_data(create_guid_str);
1367 db_rec = dbwrap_fetch_locked(db, frame, create_guid_key);
1368 if (db_rec == NULL) {
1369 TALLOC_FREE(frame);
1370 return NT_STATUS_INTERNAL_DB_ERROR;
1373 val = dbwrap_record_get_value(db_rec);
1374 if (val.dsize == 0) {
1375 uint8_t data[SMBXSRV_OPEN_REPLAY_CACHE_FIXED_SIZE];
1377 blob = data_blob_const(data, ARRAY_SIZE(data));
1378 ndr_err = ndr_push_struct_into_fixed_blob(&blob, &rc,
1379 (ndr_push_flags_fn_t)ndr_push_smbXsrv_open_replay_cache);
1380 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1381 status = ndr_map_error2ntstatus(ndr_err);
1382 TALLOC_FREE(frame);
1383 return status;
1386 val = make_tdb_data(blob.data, blob.length);
1387 status = dbwrap_record_store(db_rec, val, TDB_REPLACE);
1388 if (!NT_STATUS_IS_OK(status)) {
1389 TALLOC_FREE(frame);
1390 return status;
1394 * We're the new holder
1396 *_open = NULL;
1397 TALLOC_FREE(frame);
1398 return NT_STATUS_FWP_RESERVED;
1401 if (val.dsize != SMBXSRV_OPEN_REPLAY_CACHE_FIXED_SIZE) {
1402 TALLOC_FREE(frame);
1403 return NT_STATUS_INTERNAL_DB_CORRUPTION;
1406 blob = data_blob_const(val.dptr, val.dsize);
1407 ndr_err = ndr_pull_struct_blob_all_noalloc(&blob, &rc,
1408 (ndr_pull_flags_fn_t)ndr_pull_smbXsrv_open_replay_cache);
1409 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1410 status = ndr_map_error2ntstatus(ndr_err);
1411 TALLOC_FREE(frame);
1412 return status;
1414 if (rc.local_id != 0) {
1415 if (GUID_equal(&rc.holder_req_guid, &caller_req_guid)) {
1417 * This should not happen
1419 status = NT_STATUS_INTERNAL_ERROR;
1420 DBG_ERR("caller %s already holds local_id %u for create %s [%s] - %s\n",
1421 GUID_buf_string(&caller_req_guid, &tmp_guid_buf),
1422 (unsigned)rc.local_id,
1423 create_guid_str,
1424 name,
1425 nt_errstr(status));
1427 TALLOC_FREE(frame);
1428 return status;
1431 status = smbXsrv_open_local_lookup(table,
1432 rc.local_id,
1433 0, /* global_id */
1434 now,
1435 &op);
1436 if (!NT_STATUS_IS_OK(status)) {
1437 DBG_ERR("holder %s stale for local_id %u for create %s [%s] - %s\n",
1438 GUID_buf_string(&rc.holder_req_guid, &tmp_guid_buf),
1439 (unsigned)rc.local_id,
1440 create_guid_str,
1441 name,
1442 nt_errstr(status));
1444 TALLOC_FREE(frame);
1445 return status;
1449 * We found an open the caller can reuse.
1451 SMB_ASSERT(op != NULL);
1452 *_open = op;
1453 TALLOC_FREE(frame);
1454 return NT_STATUS_OK;
1457 if (GUID_equal(&rc.holder_req_guid, &caller_req_guid)) {
1459 * We're still the holder
1461 *_open = NULL;
1462 TALLOC_FREE(frame);
1463 return NT_STATUS_FWP_RESERVED;
1467 * The original request (or a former replay) is still
1468 * pending, ask the client to retry by sending FILE_NOT_AVAILABLE.
1470 status = NT_STATUS_FILE_NOT_AVAILABLE;
1471 DBG_DEBUG("holder %s still pending for create %s [%s] - %s\n",
1472 GUID_buf_string(&rc.holder_req_guid, &tmp_guid_buf),
1473 create_guid_str,
1474 name,
1475 nt_errstr(status));
1476 TALLOC_FREE(frame);
1477 return status;
1480 NTSTATUS smb2srv_open_recreate(struct smbXsrv_connection *conn,
1481 struct auth_session_info *session_info,
1482 uint64_t persistent_id,
1483 const struct GUID *create_guid,
1484 NTTIME now,
1485 struct smbXsrv_open **_open)
1487 struct smbXsrv_open_table *table = conn->client->open_table;
1488 struct db_record *local_rec = NULL;
1489 struct smbXsrv_open *op = NULL;
1490 void *ptr = NULL;
1491 TDB_DATA val;
1492 uint32_t global_id = persistent_id & UINT32_MAX;
1493 uint64_t global_zeros = persistent_id & 0xFFFFFFFF00000000LLU;
1494 NTSTATUS status;
1495 struct security_token *current_token = NULL;
1497 if (session_info == NULL) {
1498 DEBUG(10, ("session_info=NULL\n"));
1499 return NT_STATUS_INVALID_HANDLE;
1501 current_token = session_info->security_token;
1503 if (current_token == NULL) {
1504 DEBUG(10, ("current_token=NULL\n"));
1505 return NT_STATUS_INVALID_HANDLE;
1508 if (global_zeros != 0) {
1509 DEBUG(10, ("global_zeros!=0\n"));
1510 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1513 op = talloc_zero(table, struct smbXsrv_open);
1514 if (op == NULL) {
1515 return NT_STATUS_NO_MEMORY;
1517 op->table = table;
1519 status = smbXsrv_open_global_lookup(table, global_id, op, &op->global);
1520 if (!NT_STATUS_IS_OK(status)) {
1521 TALLOC_FREE(op);
1522 DEBUG(10, ("smbXsrv_open_global_lookup returned %s\n",
1523 nt_errstr(status)));
1524 return status;
1528 * If the provided create_guid is NULL, this means that
1529 * the reconnect request was a v1 request. In that case
1530 * we should skipt the create GUID verification, since
1531 * it is valid to v1-reconnect a v2-opened handle.
1533 if ((create_guid != NULL) &&
1534 !GUID_equal(&op->global->create_guid, create_guid))
1536 TALLOC_FREE(op);
1537 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1540 if (!security_token_is_sid(current_token, &op->global->open_owner)) {
1541 TALLOC_FREE(op);
1542 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1545 if (!op->global->durable) {
1546 TALLOC_FREE(op);
1547 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1550 if (table->local.num_opens >= table->local.max_opens) {
1551 TALLOC_FREE(op);
1552 return NT_STATUS_INSUFFICIENT_RESOURCES;
1555 status = smbXsrv_open_local_allocate_id(table->local.db_ctx,
1556 table->local.lowest_id,
1557 table->local.highest_id,
1559 &local_rec,
1560 &op->local_id);
1561 if (!NT_STATUS_IS_OK(status)) {
1562 TALLOC_FREE(op);
1563 return status;
1566 op->idle_time = now;
1567 op->status = NT_STATUS_FILE_CLOSED;
1569 op->global->open_volatile_id = op->local_id;
1570 op->global->server_id = messaging_server_id(conn->client->msg_ctx);
1572 ptr = op;
1573 val = make_tdb_data((uint8_t const *)&ptr, sizeof(ptr));
1574 status = dbwrap_record_store(local_rec, val, TDB_REPLACE);
1575 TALLOC_FREE(local_rec);
1576 if (!NT_STATUS_IS_OK(status)) {
1577 TALLOC_FREE(op);
1578 return status;
1580 table->local.num_opens += 1;
1582 talloc_set_destructor(op, smbXsrv_open_destructor);
1584 status = smbXsrv_open_global_store(op->global);
1585 if (!NT_STATUS_IS_OK(status)) {
1586 TALLOC_FREE(op);
1587 return status;
1590 if (CHECK_DEBUGLVL(10)) {
1591 struct smbXsrv_openB open_blob = {
1592 .info.info0 = op,
1595 DEBUG(10,("smbXsrv_open_recreate: global_id (0x%08x) stored\n",
1596 op->global->open_global_id));
1597 NDR_PRINT_DEBUG(smbXsrv_openB, &open_blob);
1600 *_open = op;
1601 return NT_STATUS_OK;
1605 static NTSTATUS smbXsrv_open_global_parse_record(TALLOC_CTX *mem_ctx,
1606 struct db_record *rec,
1607 struct smbXsrv_open_global0 **global)
1609 TDB_DATA key = dbwrap_record_get_key(rec);
1610 TDB_DATA val = dbwrap_record_get_value(rec);
1611 DATA_BLOB blob = data_blob_const(val.dptr, val.dsize);
1612 struct smbXsrv_open_globalB global_blob;
1613 enum ndr_err_code ndr_err;
1614 NTSTATUS status;
1615 TALLOC_CTX *frame = talloc_stackframe();
1617 ndr_err = ndr_pull_struct_blob(&blob, frame, &global_blob,
1618 (ndr_pull_flags_fn_t)ndr_pull_smbXsrv_open_globalB);
1619 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1620 DEBUG(1,("Invalid record in smbXsrv_open_global.tdb:"
1621 "key '%s' ndr_pull_struct_blob - %s\n",
1622 tdb_data_dbg(key),
1623 ndr_errstr(ndr_err)));
1624 status = ndr_map_error2ntstatus(ndr_err);
1625 goto done;
1628 if (global_blob.version != SMBXSRV_VERSION_0) {
1629 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
1630 DEBUG(1,("Invalid record in smbXsrv_open_global.tdb:"
1631 "key '%s' unsupported version - %d - %s\n",
1632 tdb_data_dbg(key),
1633 (int)global_blob.version,
1634 nt_errstr(status)));
1635 goto done;
1638 if (global_blob.info.info0 == NULL) {
1639 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
1640 DEBUG(1,("Invalid record in smbXsrv_tcon_global.tdb:"
1641 "key '%s' info0 NULL pointer - %s\n",
1642 tdb_data_dbg(key),
1643 nt_errstr(status)));
1644 goto done;
1647 *global = talloc_move(mem_ctx, &global_blob.info.info0);
1648 status = NT_STATUS_OK;
1649 done:
1650 talloc_free(frame);
1651 return status;
1654 struct smbXsrv_open_global_traverse_state {
1655 int (*fn)(struct smbXsrv_open_global0 *, void *);
1656 void *private_data;
1659 static int smbXsrv_open_global_traverse_fn(struct db_record *rec, void *data)
1661 struct smbXsrv_open_global_traverse_state *state =
1662 (struct smbXsrv_open_global_traverse_state*)data;
1663 struct smbXsrv_open_global0 *global = NULL;
1664 NTSTATUS status;
1665 int ret = -1;
1667 status = smbXsrv_open_global_parse_record(talloc_tos(), rec, &global);
1668 if (!NT_STATUS_IS_OK(status)) {
1669 return -1;
1672 global->db_rec = rec;
1673 ret = state->fn(global, state->private_data);
1674 talloc_free(global);
1675 return ret;
1678 NTSTATUS smbXsrv_open_global_traverse(
1679 int (*fn)(struct smbXsrv_open_global0 *, void *),
1680 void *private_data)
1683 NTSTATUS status;
1684 int count = 0;
1685 struct smbXsrv_open_global_traverse_state state = {
1686 .fn = fn,
1687 .private_data = private_data,
1690 become_root();
1691 status = smbXsrv_open_global_init();
1692 if (!NT_STATUS_IS_OK(status)) {
1693 unbecome_root();
1694 DEBUG(0, ("Failed to initialize open_global: %s\n",
1695 nt_errstr(status)));
1696 return status;
1699 status = dbwrap_traverse_read(smbXsrv_open_global_db_ctx,
1700 smbXsrv_open_global_traverse_fn,
1701 &state,
1702 &count);
1703 unbecome_root();
1705 return status;
1708 NTSTATUS smbXsrv_open_cleanup(uint64_t persistent_id)
1710 NTSTATUS status = NT_STATUS_OK;
1711 TALLOC_CTX *frame = talloc_stackframe();
1712 struct smbXsrv_open_global0 *op = NULL;
1713 TDB_DATA val;
1714 struct db_record *rec;
1715 bool delete_open = false;
1716 uint32_t global_id = persistent_id & UINT32_MAX;
1718 rec = smbXsrv_open_global_fetch_locked(smbXsrv_open_global_db_ctx,
1719 global_id,
1720 frame);
1721 if (rec == NULL) {
1722 status = NT_STATUS_NOT_FOUND;
1723 goto done;
1726 val = dbwrap_record_get_value(rec);
1727 if (val.dsize == 0) {
1728 DEBUG(10, ("smbXsrv_open_cleanup[global: 0x%08x] "
1729 "empty record in %s, skipping...\n",
1730 global_id, dbwrap_name(smbXsrv_open_global_db_ctx)));
1731 goto done;
1734 status = smbXsrv_open_global_parse_record(talloc_tos(), rec, &op);
1735 if (!NT_STATUS_IS_OK(status)) {
1736 DEBUG(1, ("smbXsrv_open_cleanup[global: 0x%08x] "
1737 "failed to read record: %s\n",
1738 global_id, nt_errstr(status)));
1739 goto done;
1742 if (server_id_is_disconnected(&op->server_id)) {
1743 struct timeval now, disconnect_time;
1744 int64_t tdiff;
1745 now = timeval_current();
1746 nttime_to_timeval(&disconnect_time, op->disconnect_time);
1747 tdiff = usec_time_diff(&now, &disconnect_time);
1748 delete_open = (tdiff >= 1000*op->durable_timeout_msec);
1750 DEBUG(10, ("smbXsrv_open_cleanup[global: 0x%08x] "
1751 "disconnected at [%s] %us ago with "
1752 "timeout of %us -%s reached\n",
1753 global_id,
1754 nt_time_string(frame, op->disconnect_time),
1755 (unsigned)(tdiff/1000000),
1756 op->durable_timeout_msec / 1000,
1757 delete_open ? "" : " not"));
1758 } else if (!serverid_exists(&op->server_id)) {
1759 struct server_id_buf idbuf;
1760 DEBUG(10, ("smbXsrv_open_cleanup[global: 0x%08x] "
1761 "server[%s] does not exist\n",
1762 global_id,
1763 server_id_str_buf(op->server_id, &idbuf)));
1764 delete_open = true;
1767 if (!delete_open) {
1768 goto done;
1771 status = dbwrap_record_delete(rec);
1772 if (!NT_STATUS_IS_OK(status)) {
1773 DEBUG(1, ("smbXsrv_open_cleanup[global: 0x%08x] "
1774 "failed to delete record"
1775 "from %s: %s\n", global_id,
1776 dbwrap_name(smbXsrv_open_global_db_ctx),
1777 nt_errstr(status)));
1778 goto done;
1781 DEBUG(10, ("smbXsrv_open_cleanup[global: 0x%08x] "
1782 "delete record from %s\n",
1783 global_id,
1784 dbwrap_name(smbXsrv_open_global_db_ctx)));
1786 done:
1787 talloc_free(frame);
1788 return status;