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/>.
22 #include "system/filesys.h"
23 #include "smbd/smbd.h"
24 #include "smbd/globals.h"
25 #include "dbwrap/dbwrap.h"
26 #include "dbwrap/dbwrap_rbt.h"
27 #include "dbwrap/dbwrap_open.h"
28 #include "../libcli/security/security.h"
30 #include "lib/util/util_tdb.h"
31 #include "librpc/gen_ndr/ndr_smbXsrv.h"
32 #include <ccan/hash/hash.h>
35 struct smbXsrv_open_table
{
37 struct db_context
*db_ctx
;
44 struct db_context
*db_ctx
;
48 static struct db_context
*smbXsrv_open_global_db_ctx
= NULL
;
50 NTSTATUS
smbXsrv_open_global_init(void)
52 const char *global_path
= NULL
;
53 struct db_context
*db_ctx
= NULL
;
55 if (smbXsrv_open_global_db_ctx
!= NULL
) {
59 global_path
= lock_path("smbXsrv_open_global.tdb");
61 db_ctx
= db_open(NULL
, global_path
,
65 TDB_INCOMPATIBLE_HASH
,
66 O_RDWR
| O_CREAT
, 0600,
71 status
= map_nt_error_from_unix_common(errno
);
76 smbXsrv_open_global_db_ctx
= db_ctx
;
83 * We need to store the keys in big endian so that dbwrap_rbt's memcmp
84 * has the same result as integer comparison between the uint32_t
87 * TODO: implement string based key
90 #define SMBXSRV_OPEN_GLOBAL_TDB_KEY_SIZE sizeof(uint32_t)
92 static TDB_DATA
smbXsrv_open_global_id_to_key(uint32_t id
,
97 RSIVAL(key_buf
, 0, id
);
99 key
= make_tdb_data(key_buf
, SMBXSRV_OPEN_GLOBAL_TDB_KEY_SIZE
);
105 static NTSTATUS
smbXsrv_open_global_key_to_id(TDB_DATA key
, uint32_t *id
)
108 return NT_STATUS_INVALID_PARAMETER
;
111 if (key
.dsize
!= SMBXSRV_OPEN_GLOBAL_TDB_KEY_SIZE
) {
112 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
115 *id
= RIVAL(key
.dptr
, 0);
121 #define SMBXSRV_OPEN_LOCAL_TDB_KEY_SIZE sizeof(uint32_t)
123 static TDB_DATA
smbXsrv_open_local_id_to_key(uint32_t id
,
128 RSIVAL(key_buf
, 0, id
);
130 key
= make_tdb_data(key_buf
, SMBXSRV_OPEN_LOCAL_TDB_KEY_SIZE
);
135 static NTSTATUS
smbXsrv_open_local_key_to_id(TDB_DATA key
, uint32_t *id
)
138 return NT_STATUS_INVALID_PARAMETER
;
141 if (key
.dsize
!= SMBXSRV_OPEN_LOCAL_TDB_KEY_SIZE
) {
142 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
145 *id
= RIVAL(key
.dptr
, 0);
150 static NTSTATUS
smbXsrv_open_table_init(struct smbXsrv_connection
*conn
,
155 struct smbXsrv_open_table
*table
;
159 if (lowest_id
> highest_id
) {
160 return NT_STATUS_INTERNAL_ERROR
;
163 max_range
= highest_id
;
164 max_range
-= lowest_id
;
167 if (max_opens
> max_range
) {
168 return NT_STATUS_INTERNAL_ERROR
;
171 table
= talloc_zero(conn
, struct smbXsrv_open_table
);
173 return NT_STATUS_NO_MEMORY
;
176 table
->local
.db_ctx
= db_open_rbt(table
);
177 if (table
->local
.db_ctx
== NULL
) {
179 return NT_STATUS_NO_MEMORY
;
181 table
->local
.lowest_id
= lowest_id
;
182 table
->local
.highest_id
= highest_id
;
183 table
->local
.max_opens
= max_opens
;
185 status
= smbXsrv_open_global_init();
186 if (!NT_STATUS_IS_OK(status
)) {
191 table
->global
.db_ctx
= smbXsrv_open_global_db_ctx
;
193 conn
->open_table
= table
;
197 struct smbXsrv_open_local_allocate_state
{
198 const uint32_t lowest_id
;
199 const uint32_t highest_id
;
205 static int smbXsrv_open_local_allocate_traverse(struct db_record
*rec
,
208 struct smbXsrv_open_local_allocate_state
*state
=
209 (struct smbXsrv_open_local_allocate_state
*)private_data
;
210 TDB_DATA key
= dbwrap_record_get_key(rec
);
214 status
= smbXsrv_open_local_key_to_id(key
, &id
);
215 if (!NT_STATUS_IS_OK(status
)) {
216 state
->status
= status
;
220 if (id
<= state
->last_id
) {
221 state
->status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
226 if (id
> state
->useable_id
) {
227 state
->status
= NT_STATUS_OK
;
231 if (state
->useable_id
== state
->highest_id
) {
232 state
->status
= NT_STATUS_INSUFFICIENT_RESOURCES
;
236 state
->useable_id
+=1;
240 static NTSTATUS
smbXsrv_open_local_allocate_id(struct db_context
*db
,
244 struct db_record
**_rec
,
247 struct smbXsrv_open_local_allocate_state state
= {
248 .lowest_id
= lowest_id
,
249 .highest_id
= highest_id
,
251 .useable_id
= lowest_id
,
252 .status
= NT_STATUS_INTERNAL_ERROR
,
262 if (lowest_id
> highest_id
) {
263 return NT_STATUS_INSUFFICIENT_RESOURCES
;
267 * first we try randomly
269 range
= (highest_id
- lowest_id
) + 1;
271 for (i
= 0; i
< (range
/ 2); i
++) {
273 uint8_t key_buf
[SMBXSRV_OPEN_LOCAL_TDB_KEY_SIZE
];
276 struct db_record
*rec
= NULL
;
278 id
= generate_random() % range
;
281 if (id
< lowest_id
) {
284 if (id
> highest_id
) {
288 key
= smbXsrv_open_local_id_to_key(id
, key_buf
);
290 rec
= dbwrap_fetch_locked(db
, mem_ctx
, key
);
292 return NT_STATUS_INSUFFICIENT_RESOURCES
;
295 val
= dbwrap_record_get_value(rec
);
296 if (val
.dsize
!= 0) {
307 * if the range is almost full,
308 * we traverse the whole table
309 * (this relies on sorted behavior of dbwrap_rbt)
311 status
= dbwrap_traverse_read(db
, smbXsrv_open_local_allocate_traverse
,
313 if (NT_STATUS_IS_OK(status
)) {
314 if (NT_STATUS_IS_OK(state
.status
)) {
315 return NT_STATUS_INTERNAL_ERROR
;
318 if (!NT_STATUS_EQUAL(state
.status
, NT_STATUS_INTERNAL_ERROR
)) {
322 if (state
.useable_id
<= state
.highest_id
) {
323 state
.status
= NT_STATUS_OK
;
325 return NT_STATUS_INSUFFICIENT_RESOURCES
;
327 } else if (!NT_STATUS_EQUAL(status
, NT_STATUS_INTERNAL_DB_CORRUPTION
)) {
329 * Here we really expect NT_STATUS_INTERNAL_DB_CORRUPTION!
331 * If we get anything else it is an error, because it
332 * means we did not manage to find a free slot in
335 return NT_STATUS_INSUFFICIENT_RESOURCES
;
338 if (NT_STATUS_IS_OK(state
.status
)) {
340 uint8_t key_buf
[SMBXSRV_OPEN_LOCAL_TDB_KEY_SIZE
];
343 struct db_record
*rec
= NULL
;
345 id
= state
.useable_id
;
347 key
= smbXsrv_open_local_id_to_key(id
, key_buf
);
349 rec
= dbwrap_fetch_locked(db
, mem_ctx
, key
);
351 return NT_STATUS_INSUFFICIENT_RESOURCES
;
354 val
= dbwrap_record_get_value(rec
);
355 if (val
.dsize
!= 0) {
357 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
368 struct smbXsrv_open_local_fetch_state
{
369 struct smbXsrv_open
*op
;
373 static void smbXsrv_open_local_fetch_parser(TDB_DATA key
, TDB_DATA data
,
376 struct smbXsrv_open_local_fetch_state
*state
=
377 (struct smbXsrv_open_local_fetch_state
*)private_data
;
380 if (data
.dsize
!= sizeof(ptr
)) {
381 state
->status
= NT_STATUS_INTERNAL_DB_ERROR
;
385 memcpy(&ptr
, data
.dptr
, data
.dsize
);
386 state
->op
= talloc_get_type_abort(ptr
, struct smbXsrv_open
);
387 state
->status
= NT_STATUS_OK
;
390 static NTSTATUS
smbXsrv_open_local_lookup(struct smbXsrv_open_table
*table
,
391 uint32_t open_local_id
,
392 uint32_t open_global_id
,
394 struct smbXsrv_open
**_open
)
396 struct smbXsrv_open_local_fetch_state state
= {
398 .status
= NT_STATUS_INTERNAL_ERROR
,
400 uint8_t key_buf
[SMBXSRV_OPEN_LOCAL_TDB_KEY_SIZE
];
406 if (open_local_id
== 0) {
407 return NT_STATUS_FILE_CLOSED
;
411 /* this might happen before the end of negprot */
412 return NT_STATUS_FILE_CLOSED
;
415 if (table
->local
.db_ctx
== NULL
) {
416 return NT_STATUS_INTERNAL_ERROR
;
419 key
= smbXsrv_open_local_id_to_key(open_local_id
, key_buf
);
421 status
= dbwrap_parse_record(table
->local
.db_ctx
, key
,
422 smbXsrv_open_local_fetch_parser
,
424 if (NT_STATUS_EQUAL(status
, NT_STATUS_NOT_FOUND
)) {
425 return NT_STATUS_FILE_CLOSED
;
426 } else if (!NT_STATUS_IS_OK(status
)) {
429 if (!NT_STATUS_IS_OK(state
.status
)) {
433 if (NT_STATUS_EQUAL(state
.op
->status
, NT_STATUS_FILE_CLOSED
)) {
434 return NT_STATUS_FILE_CLOSED
;
437 if (open_global_id
== 0) {
438 /* make the global check a no-op for SMB1 */
439 open_global_id
= state
.op
->global
->open_global_id
;
442 if (state
.op
->global
->open_global_id
!= open_global_id
) {
443 return NT_STATUS_FILE_CLOSED
;
446 state
.op
->idle_time
= now
;
449 return state
.op
->status
;
452 static int smbXsrv_open_global_destructor(struct smbXsrv_open_global0
*global
)
457 static void smbXsrv_open_global_verify_record(struct db_record
*db_rec
,
461 struct smbXsrv_open_global0
**_g
);
463 static NTSTATUS
smbXsrv_open_global_allocate(struct db_context
*db
,
465 struct smbXsrv_open_global0
**_global
)
468 struct smbXsrv_open_global0
*global
= NULL
;
469 uint32_t last_free
= 0;
470 const uint32_t min_tries
= 3;
474 global
= talloc_zero(mem_ctx
, struct smbXsrv_open_global0
);
475 if (global
== NULL
) {
476 return NT_STATUS_NO_MEMORY
;
478 talloc_set_destructor(global
, smbXsrv_open_global_destructor
);
481 * Here we just randomly try the whole 32-bit space
483 * We use just 32-bit, because we want to reuse the
486 for (i
= 0; i
< UINT32_MAX
; i
++) {
487 bool is_free
= false;
488 bool was_free
= false;
490 uint8_t key_buf
[SMBXSRV_OPEN_GLOBAL_TDB_KEY_SIZE
];
493 if (i
>= min_tries
&& last_free
!= 0) {
496 id
= generate_random();
501 if (id
== UINT32_MAX
) {
505 key
= smbXsrv_open_global_id_to_key(id
, key_buf
);
507 global
->db_rec
= dbwrap_fetch_locked(db
, mem_ctx
, key
);
508 if (global
->db_rec
== NULL
) {
510 return NT_STATUS_INSUFFICIENT_RESOURCES
;
513 smbXsrv_open_global_verify_record(global
->db_rec
,
519 TALLOC_FREE(global
->db_rec
);
523 if (!was_free
&& i
< min_tries
) {
525 * The session_id is free now,
526 * but was not free before.
528 * This happens if a smbd crashed
529 * and did not cleanup the record.
531 * If this is one of our first tries,
532 * then we try to find a real free one.
534 if (last_free
== 0) {
537 TALLOC_FREE(global
->db_rec
);
541 global
->open_global_id
= id
;
547 /* should not be reached */
549 return NT_STATUS_INTERNAL_ERROR
;
552 static void smbXsrv_open_global_verify_record(struct db_record
*db_rec
,
556 struct smbXsrv_open_global0
**_g
)
561 struct smbXsrv_open_globalB global_blob
;
562 enum ndr_err_code ndr_err
;
563 struct smbXsrv_open_global0
*global
= NULL
;
565 TALLOC_CTX
*frame
= talloc_stackframe();
576 key
= dbwrap_record_get_key(db_rec
);
578 val
= dbwrap_record_get_value(db_rec
);
579 if (val
.dsize
== 0) {
588 blob
= data_blob_const(val
.dptr
, val
.dsize
);
590 ndr_err
= ndr_pull_struct_blob(&blob
, frame
, &global_blob
,
591 (ndr_pull_flags_fn_t
)ndr_pull_smbXsrv_open_globalB
);
592 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
593 NTSTATUS status
= ndr_map_error2ntstatus(ndr_err
);
594 DEBUG(1,("smbXsrv_open_global_verify_record: "
595 "key '%s' ndr_pull_struct_blob - %s\n",
596 hex_encode_talloc(frame
, key
.dptr
, key
.dsize
),
602 DEBUG(10,("smbXsrv_open_global_verify_record\n"));
604 NDR_PRINT_DEBUG(smbXsrv_open_globalB
, &global_blob
);
607 if (global_blob
.version
!= SMBXSRV_VERSION_0
) {
608 DEBUG(0,("smbXsrv_open_global_verify_record: "
609 "key '%s' use unsupported version %u\n",
610 hex_encode_talloc(frame
, key
.dptr
, key
.dsize
),
611 global_blob
.version
));
612 NDR_PRINT_DEBUG(smbXsrv_open_globalB
, &global_blob
);
617 global
= global_blob
.info
.info0
;
619 exists
= serverid_exists(&global
->server_id
);
621 DEBUG(2,("smbXsrv_open_global_verify_record: "
622 "key '%s' server_id %s does not exist.\n",
623 hex_encode_talloc(frame
, key
.dptr
, key
.dsize
),
624 server_id_str(frame
, &global
->server_id
)));
626 NDR_PRINT_DEBUG(smbXsrv_open_globalB
, &global_blob
);
629 dbwrap_record_delete(db_rec
);
635 *_g
= talloc_move(mem_ctx
, &global
);
640 static NTSTATUS
smbXsrv_open_global_store(struct smbXsrv_open_global0
*global
)
642 struct smbXsrv_open_globalB global_blob
;
643 DATA_BLOB blob
= data_blob_null
;
647 enum ndr_err_code ndr_err
;
650 * TODO: if we use other versions than '0'
651 * we would add glue code here, that would be able to
652 * store the information in the old format.
655 if (global
->db_rec
== NULL
) {
656 return NT_STATUS_INTERNAL_ERROR
;
659 key
= dbwrap_record_get_key(global
->db_rec
);
660 val
= dbwrap_record_get_value(global
->db_rec
);
662 ZERO_STRUCT(global_blob
);
663 global_blob
.version
= smbXsrv_version_global_current();
664 if (val
.dsize
>= 8) {
665 global_blob
.seqnum
= IVAL(val
.dptr
, 4);
667 global_blob
.seqnum
+= 1;
668 global_blob
.info
.info0
= global
;
670 ndr_err
= ndr_push_struct_blob(&blob
, global
->db_rec
, &global_blob
,
671 (ndr_push_flags_fn_t
)ndr_push_smbXsrv_open_globalB
);
672 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
673 status
= ndr_map_error2ntstatus(ndr_err
);
674 DEBUG(1,("smbXsrv_open_global_store: key '%s' ndr_push - %s\n",
675 hex_encode_talloc(global
->db_rec
, key
.dptr
, key
.dsize
),
677 TALLOC_FREE(global
->db_rec
);
681 val
= make_tdb_data(blob
.data
, blob
.length
);
682 status
= dbwrap_record_store(global
->db_rec
, val
, TDB_REPLACE
);
683 if (!NT_STATUS_IS_OK(status
)) {
684 DEBUG(1,("smbXsrv_open_global_store: key '%s' store - %s\n",
685 hex_encode_talloc(global
->db_rec
, key
.dptr
, key
.dsize
),
687 TALLOC_FREE(global
->db_rec
);
692 DEBUG(10,("smbXsrv_open_global_store: key '%s' stored\n",
693 hex_encode_talloc(global
->db_rec
, key
.dptr
, key
.dsize
)));
694 NDR_PRINT_DEBUG(smbXsrv_open_globalB
, &global_blob
);
697 TALLOC_FREE(global
->db_rec
);
702 static int smbXsrv_open_destructor(struct smbXsrv_open
*op
)
706 status
= smbXsrv_open_close(op
, 0);
707 if (!NT_STATUS_IS_OK(status
)) {
708 DEBUG(0, ("smbXsrv_open_destructor: "
709 "smbXsrv_open_close() failed - %s\n",
713 TALLOC_FREE(op
->global
);
718 NTSTATUS
smbXsrv_open_create(struct smbXsrv_connection
*conn
,
719 struct auth_session_info
*session_info
,
721 struct smbXsrv_open
**_open
)
723 struct smbXsrv_open_table
*table
= conn
->open_table
;
724 struct db_record
*local_rec
= NULL
;
725 struct smbXsrv_open
*op
= NULL
;
728 struct smbXsrv_open_global0
*global
= NULL
;
730 struct dom_sid
*current_sid
= NULL
;
731 struct security_token
*current_token
= NULL
;
733 if (session_info
== NULL
) {
734 return NT_STATUS_INVALID_HANDLE
;
736 current_token
= session_info
->security_token
;
738 if (current_token
== NULL
) {
739 return NT_STATUS_INVALID_HANDLE
;
742 if (current_token
->num_sids
> PRIMARY_USER_SID_INDEX
) {
743 current_sid
= ¤t_token
->sids
[PRIMARY_USER_SID_INDEX
];
746 if (current_sid
== NULL
) {
747 return NT_STATUS_INVALID_HANDLE
;
750 if (table
->local
.num_opens
>= table
->local
.max_opens
) {
751 return NT_STATUS_INSUFFICIENT_RESOURCES
;
754 op
= talloc_zero(table
, struct smbXsrv_open
);
756 return NT_STATUS_NO_MEMORY
;
759 op
->status
= NT_STATUS_OK
; /* TODO: start with INTERNAL_ERROR */
762 status
= smbXsrv_open_global_allocate(table
->global
.db_ctx
,
764 if (!NT_STATUS_IS_OK(status
)) {
770 status
= smbXsrv_open_local_allocate_id(table
->local
.db_ctx
,
771 table
->local
.lowest_id
,
772 table
->local
.highest_id
,
776 if (!NT_STATUS_IS_OK(status
)) {
781 global
->open_persistent_id
= global
->open_global_id
;
782 global
->open_volatile_id
= op
->local_id
;
784 global
->server_id
= messaging_server_id(conn
->msg_ctx
);
785 global
->open_time
= now
;
786 global
->open_owner
= *current_sid
;
789 val
= make_tdb_data((uint8_t const *)&ptr
, sizeof(ptr
));
790 status
= dbwrap_record_store(local_rec
, val
, TDB_REPLACE
);
791 TALLOC_FREE(local_rec
);
792 if (!NT_STATUS_IS_OK(status
)) {
796 table
->local
.num_opens
+= 1;
798 talloc_set_destructor(op
, smbXsrv_open_destructor
);
800 status
= smbXsrv_open_global_store(global
);
801 if (!NT_STATUS_IS_OK(status
)) {
802 DEBUG(0,("smbXsrv_open_create: "
803 "global_id (0x%08x) store failed - %s\n",
804 op
->global
->open_global_id
,
811 struct smbXsrv_openB open_blob
;
813 ZERO_STRUCT(open_blob
);
814 open_blob
.version
= SMBXSRV_VERSION_0
;
815 open_blob
.info
.info0
= op
;
817 DEBUG(10,("smbXsrv_open_create: global_id (0x%08x) stored\n",
818 op
->global
->open_global_id
));
819 NDR_PRINT_DEBUG(smbXsrv_openB
, &open_blob
);
826 uint32_t smbXsrv_open_hash(struct smbXsrv_open
*_open
)
831 SBVAL(buf
, 0, _open
->global
->open_persistent_id
);
832 SBVAL(buf
, 8, _open
->global
->open_volatile_id
);
833 SBVAL(buf
, 16, _open
->global
->open_time
);
835 ret
= hash(buf
, sizeof(buf
), 0);
844 NTSTATUS
smbXsrv_open_update(struct smbXsrv_open
*op
)
846 struct smbXsrv_open_table
*table
= op
->table
;
848 uint8_t key_buf
[SMBXSRV_OPEN_GLOBAL_TDB_KEY_SIZE
];
851 if (op
->global
->db_rec
!= NULL
) {
852 DEBUG(0, ("smbXsrv_open_update(0x%08x): "
853 "Called with db_rec != NULL'\n",
854 op
->global
->open_global_id
));
855 return NT_STATUS_INTERNAL_ERROR
;
858 key
= smbXsrv_open_global_id_to_key(op
->global
->open_global_id
,
861 op
->global
->db_rec
= dbwrap_fetch_locked(table
->global
.db_ctx
,
863 if (op
->global
->db_rec
== NULL
) {
864 DEBUG(0, ("smbXsrv_open_update(0x%08x): "
865 "Failed to lock global key '%s'\n",
866 op
->global
->open_global_id
,
867 hex_encode_talloc(talloc_tos(), key
.dptr
,
869 return NT_STATUS_INTERNAL_DB_ERROR
;
872 status
= smbXsrv_open_global_store(op
->global
);
873 if (!NT_STATUS_IS_OK(status
)) {
874 DEBUG(0,("smbXsrv_open_update: "
875 "global_id (0x%08x) store failed - %s\n",
876 op
->global
->open_global_id
,
882 struct smbXsrv_openB open_blob
;
884 ZERO_STRUCT(open_blob
);
885 open_blob
.version
= SMBXSRV_VERSION_0
;
886 open_blob
.info
.info0
= op
;
888 DEBUG(10,("smbXsrv_open_update: global_id (0x%08x) stored\n",
889 op
->global
->open_global_id
));
890 NDR_PRINT_DEBUG(smbXsrv_openB
, &open_blob
);
896 NTSTATUS
smbXsrv_open_close(struct smbXsrv_open
*op
, NTTIME now
)
898 struct smbXsrv_open_table
*table
;
899 struct db_record
*local_rec
= NULL
;
900 struct db_record
*global_rec
= NULL
;
902 NTSTATUS error
= NT_STATUS_OK
;
904 if (op
->table
== NULL
) {
911 op
->status
= NT_STATUS_FILE_CLOSED
;
913 global_rec
= op
->global
->db_rec
;
914 op
->global
->db_rec
= NULL
;
915 if (global_rec
== NULL
) {
916 uint8_t key_buf
[SMBXSRV_OPEN_GLOBAL_TDB_KEY_SIZE
];
919 key
= smbXsrv_open_global_id_to_key(
920 op
->global
->open_global_id
,
923 global_rec
= dbwrap_fetch_locked(table
->global
.db_ctx
,
925 if (global_rec
== NULL
) {
926 DEBUG(0, ("smbXsrv_open_close(0x%08x): "
927 "Failed to lock global key '%s'\n",
928 op
->global
->open_global_id
,
929 hex_encode_talloc(global_rec
, key
.dptr
,
931 error
= NT_STATUS_INTERNAL_ERROR
;
935 if (global_rec
!= NULL
) {
936 status
= dbwrap_record_delete(global_rec
);
937 if (!NT_STATUS_IS_OK(status
)) {
938 TDB_DATA key
= dbwrap_record_get_key(global_rec
);
940 DEBUG(0, ("smbXsrv_open_close(0x%08x): "
941 "failed to delete global key '%s': %s\n",
942 op
->global
->open_global_id
,
943 hex_encode_talloc(global_rec
, key
.dptr
,
949 TALLOC_FREE(global_rec
);
951 local_rec
= op
->db_rec
;
952 if (local_rec
== NULL
) {
953 uint8_t key_buf
[SMBXSRV_OPEN_LOCAL_TDB_KEY_SIZE
];
956 key
= smbXsrv_open_local_id_to_key(op
->local_id
, key_buf
);
958 local_rec
= dbwrap_fetch_locked(table
->local
.db_ctx
,
960 if (local_rec
== NULL
) {
961 DEBUG(0, ("smbXsrv_open_close(0x%08x): "
962 "Failed to lock local key '%s'\n",
963 op
->global
->open_global_id
,
964 hex_encode_talloc(local_rec
, key
.dptr
,
966 error
= NT_STATUS_INTERNAL_ERROR
;
970 if (local_rec
!= NULL
) {
971 status
= dbwrap_record_delete(local_rec
);
972 if (!NT_STATUS_IS_OK(status
)) {
973 TDB_DATA key
= dbwrap_record_get_key(local_rec
);
975 DEBUG(0, ("smbXsrv_open_close(0x%08x): "
976 "failed to delete local key '%s': %s\n",
977 op
->global
->open_global_id
,
978 hex_encode_talloc(local_rec
, key
.dptr
,
983 table
->local
.num_opens
-= 1;
985 if (op
->db_rec
== NULL
) {
986 TALLOC_FREE(local_rec
);
991 file_free(NULL
, op
->compat
);
998 NTSTATUS
smb1srv_open_table_init(struct smbXsrv_connection
*conn
)
1003 * Allow a range from 1..65534.
1005 * With real_max_open_files possible ids,
1006 * truncated to the SMB1 limit of 16-bit.
1008 * 0 and 0xFFFF are no valid ids.
1010 max_opens
= conn
->sconn
->real_max_open_files
;
1011 max_opens
= MIN(max_opens
, UINT16_MAX
- 1);
1013 return smbXsrv_open_table_init(conn
, 1, UINT16_MAX
- 1, max_opens
);
1016 NTSTATUS
smb1srv_open_lookup(struct smbXsrv_connection
*conn
,
1017 uint16_t fnum
, NTTIME now
,
1018 struct smbXsrv_open
**_open
)
1020 struct smbXsrv_open_table
*table
= conn
->open_table
;
1021 uint32_t local_id
= fnum
;
1022 uint32_t global_id
= 0;
1024 return smbXsrv_open_local_lookup(table
, local_id
, global_id
, now
, _open
);
1027 NTSTATUS
smb2srv_open_table_init(struct smbXsrv_connection
*conn
)
1032 * Allow a range from 1..4294967294.
1034 * With real_max_open_files possible ids,
1035 * truncated to 16-bit (the same as SMB1 for now).
1037 * 0 and 0xFFFFFFFF are no valid ids.
1039 * The usage of conn->sconn->real_max_open_files
1040 * is the reason that we use one open table per
1041 * transport connection (as we still have a 1:1 mapping
1042 * between process and transport connection).
1044 max_opens
= conn
->sconn
->real_max_open_files
;
1045 max_opens
= MIN(max_opens
, UINT16_MAX
- 1);
1047 return smbXsrv_open_table_init(conn
, 1, UINT32_MAX
- 1, max_opens
);
1050 NTSTATUS
smb2srv_open_lookup(struct smbXsrv_connection
*conn
,
1051 uint64_t persistent_id
,
1052 uint64_t volatile_id
,
1054 struct smbXsrv_open
**_open
)
1056 struct smbXsrv_open_table
*table
= conn
->open_table
;
1057 uint32_t local_id
= volatile_id
& UINT32_MAX
;
1058 uint64_t local_zeros
= volatile_id
& 0xFFFFFFFF00000000LLU
;
1059 uint32_t global_id
= persistent_id
& UINT32_MAX
;
1060 uint64_t global_zeros
= persistent_id
& 0xFFFFFFFF00000000LLU
;
1062 if (local_zeros
!= 0) {
1063 return NT_STATUS_FILE_CLOSED
;
1066 if (global_zeros
!= 0) {
1067 return NT_STATUS_FILE_CLOSED
;
1070 if (global_id
== 0) {
1071 return NT_STATUS_FILE_CLOSED
;
1074 return smbXsrv_open_local_lookup(table
, local_id
, global_id
, now
, _open
);