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"
34 struct smbXsrv_open_table
{
36 struct db_context
*db_ctx
;
43 struct db_context
*db_ctx
;
47 static struct db_context
*smbXsrv_open_global_db_ctx
= NULL
;
49 NTSTATUS
smbXsrv_open_global_init(void)
51 char *global_path
= NULL
;
52 struct db_context
*db_ctx
= NULL
;
54 if (smbXsrv_open_global_db_ctx
!= NULL
) {
58 global_path
= lock_path("smbXsrv_open_global.tdb");
59 if (global_path
== NULL
) {
60 return NT_STATUS_NO_MEMORY
;
63 db_ctx
= db_open(NULL
, global_path
,
67 TDB_INCOMPATIBLE_HASH
,
68 O_RDWR
| O_CREAT
, 0600,
71 TALLOC_FREE(global_path
);
75 status
= map_nt_error_from_unix_common(errno
);
80 smbXsrv_open_global_db_ctx
= db_ctx
;
87 * We need to store the keys in big endian so that dbwrap_rbt's memcmp
88 * has the same result as integer comparison between the uint32_t
91 * TODO: implement string based key
94 #define SMBXSRV_OPEN_GLOBAL_TDB_KEY_SIZE sizeof(uint32_t)
96 static TDB_DATA
smbXsrv_open_global_id_to_key(uint32_t id
,
101 RSIVAL(key_buf
, 0, id
);
103 key
= make_tdb_data(key_buf
, SMBXSRV_OPEN_GLOBAL_TDB_KEY_SIZE
);
109 static NTSTATUS
smbXsrv_open_global_key_to_id(TDB_DATA key
, uint32_t *id
)
112 return NT_STATUS_INVALID_PARAMETER
;
115 if (key
.dsize
!= SMBXSRV_OPEN_GLOBAL_TDB_KEY_SIZE
) {
116 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
119 *id
= RIVAL(key
.dptr
, 0);
125 #define SMBXSRV_OPEN_LOCAL_TDB_KEY_SIZE sizeof(uint32_t)
127 static TDB_DATA
smbXsrv_open_local_id_to_key(uint32_t id
,
132 RSIVAL(key_buf
, 0, id
);
134 key
= make_tdb_data(key_buf
, SMBXSRV_OPEN_LOCAL_TDB_KEY_SIZE
);
139 static NTSTATUS
smbXsrv_open_local_key_to_id(TDB_DATA key
, uint32_t *id
)
142 return NT_STATUS_INVALID_PARAMETER
;
145 if (key
.dsize
!= SMBXSRV_OPEN_LOCAL_TDB_KEY_SIZE
) {
146 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
149 *id
= RIVAL(key
.dptr
, 0);
154 static NTSTATUS
smbXsrv_open_table_init(struct smbXsrv_connection
*conn
,
159 struct smbXsrv_client
*client
= conn
->client
;
160 struct smbXsrv_open_table
*table
;
164 if (lowest_id
> highest_id
) {
165 return NT_STATUS_INTERNAL_ERROR
;
168 max_range
= highest_id
;
169 max_range
-= lowest_id
;
172 if (max_opens
> max_range
) {
173 return NT_STATUS_INTERNAL_ERROR
;
176 table
= talloc_zero(client
, struct smbXsrv_open_table
);
178 return NT_STATUS_NO_MEMORY
;
181 table
->local
.db_ctx
= db_open_rbt(table
);
182 if (table
->local
.db_ctx
== NULL
) {
184 return NT_STATUS_NO_MEMORY
;
186 table
->local
.lowest_id
= lowest_id
;
187 table
->local
.highest_id
= highest_id
;
188 table
->local
.max_opens
= max_opens
;
190 status
= smbXsrv_open_global_init();
191 if (!NT_STATUS_IS_OK(status
)) {
196 table
->global
.db_ctx
= smbXsrv_open_global_db_ctx
;
198 client
->open_table
= table
;
202 struct smbXsrv_open_local_allocate_state
{
203 const uint32_t lowest_id
;
204 const uint32_t highest_id
;
210 static int smbXsrv_open_local_allocate_traverse(struct db_record
*rec
,
213 struct smbXsrv_open_local_allocate_state
*state
=
214 (struct smbXsrv_open_local_allocate_state
*)private_data
;
215 TDB_DATA key
= dbwrap_record_get_key(rec
);
219 status
= smbXsrv_open_local_key_to_id(key
, &id
);
220 if (!NT_STATUS_IS_OK(status
)) {
221 state
->status
= status
;
225 if (id
<= state
->last_id
) {
226 state
->status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
231 if (id
> state
->useable_id
) {
232 state
->status
= NT_STATUS_OK
;
236 if (state
->useable_id
== state
->highest_id
) {
237 state
->status
= NT_STATUS_INSUFFICIENT_RESOURCES
;
241 state
->useable_id
+=1;
245 static NTSTATUS
smbXsrv_open_local_allocate_id(struct db_context
*db
,
249 struct db_record
**_rec
,
252 struct smbXsrv_open_local_allocate_state state
= {
253 .lowest_id
= lowest_id
,
254 .highest_id
= highest_id
,
256 .useable_id
= lowest_id
,
257 .status
= NT_STATUS_INTERNAL_ERROR
,
267 if (lowest_id
> highest_id
) {
268 return NT_STATUS_INSUFFICIENT_RESOURCES
;
272 * first we try randomly
274 range
= (highest_id
- lowest_id
) + 1;
276 for (i
= 0; i
< (range
/ 2); i
++) {
278 uint8_t key_buf
[SMBXSRV_OPEN_LOCAL_TDB_KEY_SIZE
];
281 struct db_record
*rec
= NULL
;
283 id
= generate_random() % range
;
286 if (id
< lowest_id
) {
289 if (id
> highest_id
) {
293 key
= smbXsrv_open_local_id_to_key(id
, key_buf
);
295 rec
= dbwrap_fetch_locked(db
, mem_ctx
, key
);
297 return NT_STATUS_INSUFFICIENT_RESOURCES
;
300 val
= dbwrap_record_get_value(rec
);
301 if (val
.dsize
!= 0) {
312 * if the range is almost full,
313 * we traverse the whole table
314 * (this relies on sorted behavior of dbwrap_rbt)
316 status
= dbwrap_traverse_read(db
, smbXsrv_open_local_allocate_traverse
,
318 if (NT_STATUS_IS_OK(status
)) {
319 if (NT_STATUS_IS_OK(state
.status
)) {
320 return NT_STATUS_INTERNAL_ERROR
;
323 if (!NT_STATUS_EQUAL(state
.status
, NT_STATUS_INTERNAL_ERROR
)) {
327 if (state
.useable_id
<= state
.highest_id
) {
328 state
.status
= NT_STATUS_OK
;
330 return NT_STATUS_INSUFFICIENT_RESOURCES
;
332 } else if (!NT_STATUS_EQUAL(status
, NT_STATUS_INTERNAL_DB_CORRUPTION
)) {
334 * Here we really expect NT_STATUS_INTERNAL_DB_CORRUPTION!
336 * If we get anything else it is an error, because it
337 * means we did not manage to find a free slot in
340 return NT_STATUS_INSUFFICIENT_RESOURCES
;
343 if (NT_STATUS_IS_OK(state
.status
)) {
345 uint8_t key_buf
[SMBXSRV_OPEN_LOCAL_TDB_KEY_SIZE
];
348 struct db_record
*rec
= NULL
;
350 id
= state
.useable_id
;
352 key
= smbXsrv_open_local_id_to_key(id
, key_buf
);
354 rec
= dbwrap_fetch_locked(db
, mem_ctx
, key
);
356 return NT_STATUS_INSUFFICIENT_RESOURCES
;
359 val
= dbwrap_record_get_value(rec
);
360 if (val
.dsize
!= 0) {
362 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
373 struct smbXsrv_open_local_fetch_state
{
374 struct smbXsrv_open
*op
;
378 static void smbXsrv_open_local_fetch_parser(TDB_DATA key
, TDB_DATA data
,
381 struct smbXsrv_open_local_fetch_state
*state
=
382 (struct smbXsrv_open_local_fetch_state
*)private_data
;
385 if (data
.dsize
!= sizeof(ptr
)) {
386 state
->status
= NT_STATUS_INTERNAL_DB_ERROR
;
390 memcpy(&ptr
, data
.dptr
, data
.dsize
);
391 state
->op
= talloc_get_type_abort(ptr
, struct smbXsrv_open
);
392 state
->status
= NT_STATUS_OK
;
395 static NTSTATUS
smbXsrv_open_local_lookup(struct smbXsrv_open_table
*table
,
396 uint32_t open_local_id
,
397 uint32_t open_global_id
,
399 struct smbXsrv_open
**_open
)
401 struct smbXsrv_open_local_fetch_state state
= {
403 .status
= NT_STATUS_INTERNAL_ERROR
,
405 uint8_t key_buf
[SMBXSRV_OPEN_LOCAL_TDB_KEY_SIZE
];
411 if (open_local_id
== 0) {
412 return NT_STATUS_FILE_CLOSED
;
416 /* this might happen before the end of negprot */
417 return NT_STATUS_FILE_CLOSED
;
420 if (table
->local
.db_ctx
== NULL
) {
421 return NT_STATUS_INTERNAL_ERROR
;
424 key
= smbXsrv_open_local_id_to_key(open_local_id
, key_buf
);
426 status
= dbwrap_parse_record(table
->local
.db_ctx
, key
,
427 smbXsrv_open_local_fetch_parser
,
429 if (NT_STATUS_EQUAL(status
, NT_STATUS_NOT_FOUND
)) {
430 return NT_STATUS_FILE_CLOSED
;
431 } else if (!NT_STATUS_IS_OK(status
)) {
434 if (!NT_STATUS_IS_OK(state
.status
)) {
438 if (NT_STATUS_EQUAL(state
.op
->status
, NT_STATUS_FILE_CLOSED
)) {
439 return NT_STATUS_FILE_CLOSED
;
442 if (open_global_id
== 0) {
443 /* make the global check a no-op for SMB1 */
444 open_global_id
= state
.op
->global
->open_global_id
;
447 if (state
.op
->global
->open_global_id
!= open_global_id
) {
448 return NT_STATUS_FILE_CLOSED
;
452 state
.op
->idle_time
= now
;
456 return state
.op
->status
;
459 static int smbXsrv_open_global_destructor(struct smbXsrv_open_global0
*global
)
464 static void smbXsrv_open_global_verify_record(struct db_record
*db_rec
,
468 struct smbXsrv_open_global0
**_g
);
470 static NTSTATUS
smbXsrv_open_global_allocate(struct db_context
*db
,
472 struct smbXsrv_open_global0
**_global
)
475 struct smbXsrv_open_global0
*global
= NULL
;
476 uint32_t last_free
= 0;
477 const uint32_t min_tries
= 3;
481 global
= talloc_zero(mem_ctx
, struct smbXsrv_open_global0
);
482 if (global
== NULL
) {
483 return NT_STATUS_NO_MEMORY
;
485 talloc_set_destructor(global
, smbXsrv_open_global_destructor
);
488 * Here we just randomly try the whole 32-bit space
490 * We use just 32-bit, because we want to reuse the
493 for (i
= 0; i
< UINT32_MAX
; i
++) {
494 bool is_free
= false;
495 bool was_free
= false;
497 uint8_t key_buf
[SMBXSRV_OPEN_GLOBAL_TDB_KEY_SIZE
];
500 if (i
>= min_tries
&& last_free
!= 0) {
503 id
= generate_random();
508 if (id
== UINT32_MAX
) {
512 key
= smbXsrv_open_global_id_to_key(id
, key_buf
);
514 global
->db_rec
= dbwrap_fetch_locked(db
, mem_ctx
, key
);
515 if (global
->db_rec
== NULL
) {
517 return NT_STATUS_INSUFFICIENT_RESOURCES
;
520 smbXsrv_open_global_verify_record(global
->db_rec
,
526 TALLOC_FREE(global
->db_rec
);
530 if (!was_free
&& i
< min_tries
) {
532 * The session_id is free now,
533 * but was not free before.
535 * This happens if a smbd crashed
536 * and did not cleanup the record.
538 * If this is one of our first tries,
539 * then we try to find a real free one.
541 if (last_free
== 0) {
544 TALLOC_FREE(global
->db_rec
);
548 global
->open_global_id
= id
;
554 /* should not be reached */
556 return NT_STATUS_INTERNAL_ERROR
;
559 static void smbXsrv_open_global_verify_record(struct db_record
*db_rec
,
563 struct smbXsrv_open_global0
**_g
)
568 struct smbXsrv_open_globalB global_blob
;
569 enum ndr_err_code ndr_err
;
570 struct smbXsrv_open_global0
*global
= NULL
;
572 TALLOC_CTX
*frame
= talloc_stackframe();
583 key
= dbwrap_record_get_key(db_rec
);
585 val
= dbwrap_record_get_value(db_rec
);
586 if (val
.dsize
== 0) {
587 DEBUG(10, ("%s: empty value\n", __func__
));
596 blob
= data_blob_const(val
.dptr
, val
.dsize
);
598 ndr_err
= ndr_pull_struct_blob(&blob
, frame
, &global_blob
,
599 (ndr_pull_flags_fn_t
)ndr_pull_smbXsrv_open_globalB
);
600 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
601 NTSTATUS status
= ndr_map_error2ntstatus(ndr_err
);
602 DEBUG(1,("smbXsrv_open_global_verify_record: "
603 "key '%s' ndr_pull_struct_blob - %s\n",
604 hex_encode_talloc(frame
, key
.dptr
, key
.dsize
),
610 DEBUG(10,("smbXsrv_open_global_verify_record\n"));
611 if (CHECK_DEBUGLVL(10)) {
612 NDR_PRINT_DEBUG(smbXsrv_open_globalB
, &global_blob
);
615 if (global_blob
.version
!= SMBXSRV_VERSION_0
) {
616 DEBUG(0,("smbXsrv_open_global_verify_record: "
617 "key '%s' use unsupported version %u\n",
618 hex_encode_talloc(frame
, key
.dptr
, key
.dsize
),
619 global_blob
.version
));
620 NDR_PRINT_DEBUG(smbXsrv_open_globalB
, &global_blob
);
625 global
= global_blob
.info
.info0
;
627 if (server_id_is_disconnected(&global
->server_id
)) {
630 exists
= serverid_exists(&global
->server_id
);
633 DEBUG(2,("smbXsrv_open_global_verify_record: "
634 "key '%s' server_id %s does not exist.\n",
635 hex_encode_talloc(frame
, key
.dptr
, key
.dsize
),
636 server_id_str(frame
, &global
->server_id
)));
637 if (CHECK_DEBUGLVL(2)) {
638 NDR_PRINT_DEBUG(smbXsrv_open_globalB
, &global_blob
);
641 dbwrap_record_delete(db_rec
);
647 *_g
= talloc_move(mem_ctx
, &global
);
652 static NTSTATUS
smbXsrv_open_global_store(struct smbXsrv_open_global0
*global
)
654 struct smbXsrv_open_globalB global_blob
;
655 DATA_BLOB blob
= data_blob_null
;
659 enum ndr_err_code ndr_err
;
662 * TODO: if we use other versions than '0'
663 * we would add glue code here, that would be able to
664 * store the information in the old format.
667 if (global
->db_rec
== NULL
) {
668 return NT_STATUS_INTERNAL_ERROR
;
671 key
= dbwrap_record_get_key(global
->db_rec
);
672 val
= dbwrap_record_get_value(global
->db_rec
);
674 ZERO_STRUCT(global_blob
);
675 global_blob
.version
= smbXsrv_version_global_current();
676 if (val
.dsize
>= 8) {
677 global_blob
.seqnum
= IVAL(val
.dptr
, 4);
679 global_blob
.seqnum
+= 1;
680 global_blob
.info
.info0
= global
;
682 ndr_err
= ndr_push_struct_blob(&blob
, global
->db_rec
, &global_blob
,
683 (ndr_push_flags_fn_t
)ndr_push_smbXsrv_open_globalB
);
684 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
685 status
= ndr_map_error2ntstatus(ndr_err
);
686 DEBUG(1,("smbXsrv_open_global_store: key '%s' ndr_push - %s\n",
687 hex_encode_talloc(global
->db_rec
, key
.dptr
, key
.dsize
),
689 TALLOC_FREE(global
->db_rec
);
693 val
= make_tdb_data(blob
.data
, blob
.length
);
694 status
= dbwrap_record_store(global
->db_rec
, val
, TDB_REPLACE
);
695 if (!NT_STATUS_IS_OK(status
)) {
696 DEBUG(1,("smbXsrv_open_global_store: key '%s' store - %s\n",
697 hex_encode_talloc(global
->db_rec
, key
.dptr
, key
.dsize
),
699 TALLOC_FREE(global
->db_rec
);
703 if (CHECK_DEBUGLVL(10)) {
704 DEBUG(10,("smbXsrv_open_global_store: key '%s' stored\n",
705 hex_encode_talloc(global
->db_rec
, key
.dptr
, key
.dsize
)));
706 NDR_PRINT_DEBUG(smbXsrv_open_globalB
, &global_blob
);
709 TALLOC_FREE(global
->db_rec
);
714 static NTSTATUS
smbXsrv_open_global_lookup(struct smbXsrv_open_table
*table
,
715 uint32_t open_global_id
,
717 struct smbXsrv_open_global0
**_global
)
720 uint8_t key_buf
[SMBXSRV_OPEN_GLOBAL_TDB_KEY_SIZE
];
721 struct db_record
*global_rec
= NULL
;
722 bool is_free
= false;
726 if (table
->global
.db_ctx
== NULL
) {
727 return NT_STATUS_INTERNAL_ERROR
;
730 key
= smbXsrv_open_global_id_to_key(open_global_id
, key_buf
);
732 global_rec
= dbwrap_fetch_locked(table
->global
.db_ctx
, mem_ctx
, key
);
733 if (global_rec
== NULL
) {
734 DEBUG(0, ("smbXsrv_open_global_lookup(0x%08x): "
735 "Failed to lock global key '%s'\n",
737 hex_encode_talloc(talloc_tos(), key
.dptr
,
739 return NT_STATUS_INTERNAL_DB_ERROR
;
742 smbXsrv_open_global_verify_record(global_rec
,
748 DEBUG(10, ("%s: is_free=true\n", __func__
));
749 talloc_free(global_rec
);
750 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
753 (*_global
)->db_rec
= talloc_move(*_global
, &global_rec
);
755 talloc_set_destructor(*_global
, smbXsrv_open_global_destructor
);
760 static int smbXsrv_open_destructor(struct smbXsrv_open
*op
)
764 status
= smbXsrv_open_close(op
, 0);
765 if (!NT_STATUS_IS_OK(status
)) {
766 DEBUG(0, ("smbXsrv_open_destructor: "
767 "smbXsrv_open_close() failed - %s\n",
771 TALLOC_FREE(op
->global
);
776 NTSTATUS
smbXsrv_open_create(struct smbXsrv_connection
*conn
,
777 struct auth_session_info
*session_info
,
779 struct smbXsrv_open
**_open
)
781 struct smbXsrv_open_table
*table
= conn
->client
->open_table
;
782 struct db_record
*local_rec
= NULL
;
783 struct smbXsrv_open
*op
= NULL
;
786 struct smbXsrv_open_global0
*global
= NULL
;
788 struct dom_sid
*current_sid
= NULL
;
789 struct security_token
*current_token
= NULL
;
791 if (session_info
== NULL
) {
792 return NT_STATUS_INVALID_HANDLE
;
794 current_token
= session_info
->security_token
;
796 if (current_token
== NULL
) {
797 return NT_STATUS_INVALID_HANDLE
;
800 if (current_token
->num_sids
> PRIMARY_USER_SID_INDEX
) {
801 current_sid
= ¤t_token
->sids
[PRIMARY_USER_SID_INDEX
];
804 if (current_sid
== NULL
) {
805 return NT_STATUS_INVALID_HANDLE
;
808 if (table
->local
.num_opens
>= table
->local
.max_opens
) {
809 return NT_STATUS_INSUFFICIENT_RESOURCES
;
812 op
= talloc_zero(table
, struct smbXsrv_open
);
814 return NT_STATUS_NO_MEMORY
;
817 op
->status
= NT_STATUS_OK
; /* TODO: start with INTERNAL_ERROR */
820 status
= smbXsrv_open_global_allocate(table
->global
.db_ctx
,
822 if (!NT_STATUS_IS_OK(status
)) {
828 status
= smbXsrv_open_local_allocate_id(table
->local
.db_ctx
,
829 table
->local
.lowest_id
,
830 table
->local
.highest_id
,
834 if (!NT_STATUS_IS_OK(status
)) {
839 global
->open_persistent_id
= global
->open_global_id
;
840 global
->open_volatile_id
= op
->local_id
;
842 global
->server_id
= messaging_server_id(conn
->msg_ctx
);
843 global
->open_time
= now
;
844 global
->open_owner
= *current_sid
;
845 if (conn
->protocol
>= PROTOCOL_SMB2_10
) {
846 global
->client_guid
= conn
->smb2
.client
.guid
;
850 val
= make_tdb_data((uint8_t const *)&ptr
, sizeof(ptr
));
851 status
= dbwrap_record_store(local_rec
, val
, TDB_REPLACE
);
852 TALLOC_FREE(local_rec
);
853 if (!NT_STATUS_IS_OK(status
)) {
857 table
->local
.num_opens
+= 1;
859 talloc_set_destructor(op
, smbXsrv_open_destructor
);
861 status
= smbXsrv_open_global_store(global
);
862 if (!NT_STATUS_IS_OK(status
)) {
863 DEBUG(0,("smbXsrv_open_create: "
864 "global_id (0x%08x) store failed - %s\n",
865 op
->global
->open_global_id
,
871 if (CHECK_DEBUGLVL(10)) {
872 struct smbXsrv_openB open_blob
;
874 ZERO_STRUCT(open_blob
);
875 open_blob
.version
= SMBXSRV_VERSION_0
;
876 open_blob
.info
.info0
= op
;
878 DEBUG(10,("smbXsrv_open_create: global_id (0x%08x) stored\n",
879 op
->global
->open_global_id
));
880 NDR_PRINT_DEBUG(smbXsrv_openB
, &open_blob
);
887 uint32_t smbXsrv_open_hash(struct smbXsrv_open
*_open
)
893 SBVAL(buf
, 0, _open
->global
->open_persistent_id
);
894 SBVAL(buf
, 8, _open
->global
->open_volatile_id
);
895 SBVAL(buf
, 16, _open
->global
->open_time
);
897 key
= (TDB_DATA
) { .dptr
= buf
, .dsize
= sizeof(buf
) };
898 ret
= tdb_jenkins_hash(&key
);
907 NTSTATUS
smbXsrv_open_update(struct smbXsrv_open
*op
)
909 struct smbXsrv_open_table
*table
= op
->table
;
911 uint8_t key_buf
[SMBXSRV_OPEN_GLOBAL_TDB_KEY_SIZE
];
914 if (op
->global
->db_rec
!= NULL
) {
915 DEBUG(0, ("smbXsrv_open_update(0x%08x): "
916 "Called with db_rec != NULL'\n",
917 op
->global
->open_global_id
));
918 return NT_STATUS_INTERNAL_ERROR
;
921 key
= smbXsrv_open_global_id_to_key(op
->global
->open_global_id
,
924 op
->global
->db_rec
= dbwrap_fetch_locked(table
->global
.db_ctx
,
926 if (op
->global
->db_rec
== NULL
) {
927 DEBUG(0, ("smbXsrv_open_update(0x%08x): "
928 "Failed to lock global key '%s'\n",
929 op
->global
->open_global_id
,
930 hex_encode_talloc(talloc_tos(), key
.dptr
,
932 return NT_STATUS_INTERNAL_DB_ERROR
;
935 status
= smbXsrv_open_global_store(op
->global
);
936 if (!NT_STATUS_IS_OK(status
)) {
937 DEBUG(0,("smbXsrv_open_update: "
938 "global_id (0x%08x) store failed - %s\n",
939 op
->global
->open_global_id
,
944 if (CHECK_DEBUGLVL(10)) {
945 struct smbXsrv_openB open_blob
;
947 ZERO_STRUCT(open_blob
);
948 open_blob
.version
= SMBXSRV_VERSION_0
;
949 open_blob
.info
.info0
= op
;
951 DEBUG(10,("smbXsrv_open_update: global_id (0x%08x) stored\n",
952 op
->global
->open_global_id
));
953 NDR_PRINT_DEBUG(smbXsrv_openB
, &open_blob
);
959 NTSTATUS
smbXsrv_open_close(struct smbXsrv_open
*op
, NTTIME now
)
961 struct smbXsrv_open_table
*table
;
962 struct db_record
*local_rec
= NULL
;
963 struct db_record
*global_rec
= NULL
;
965 NTSTATUS error
= NT_STATUS_OK
;
967 if (op
->table
== NULL
) {
974 op
->status
= NT_STATUS_FILE_CLOSED
;
975 op
->global
->disconnect_time
= now
;
976 server_id_set_disconnected(&op
->global
->server_id
);
978 global_rec
= op
->global
->db_rec
;
979 op
->global
->db_rec
= NULL
;
980 if (global_rec
== NULL
) {
981 uint8_t key_buf
[SMBXSRV_OPEN_GLOBAL_TDB_KEY_SIZE
];
984 key
= smbXsrv_open_global_id_to_key(
985 op
->global
->open_global_id
,
988 global_rec
= dbwrap_fetch_locked(table
->global
.db_ctx
,
990 if (global_rec
== NULL
) {
991 DEBUG(0, ("smbXsrv_open_close(0x%08x): "
992 "Failed to lock global key '%s'\n",
993 op
->global
->open_global_id
,
994 hex_encode_talloc(global_rec
, key
.dptr
,
996 error
= NT_STATUS_INTERNAL_ERROR
;
1000 if (global_rec
!= NULL
&& op
->global
->durable
) {
1002 * If it is a durable open we need to update the global part
1003 * instead of deleting it
1005 op
->global
->db_rec
= global_rec
;
1006 status
= smbXsrv_open_global_store(op
->global
);
1007 if (NT_STATUS_IS_OK(status
)) {
1009 * smbXsrv_open_global_store does the free
1010 * of op->global->db_rec
1014 if (!NT_STATUS_IS_OK(status
)) {
1015 DEBUG(0,("smbXsrv_open_close(0x%08x)"
1016 "smbXsrv_open_global_store() failed - %s\n",
1017 op
->global
->open_global_id
,
1018 nt_errstr(status
)));
1022 if (NT_STATUS_IS_OK(status
) && CHECK_DEBUGLVL(10)) {
1023 struct smbXsrv_openB open_blob
;
1025 ZERO_STRUCT(open_blob
);
1026 open_blob
.version
= SMBXSRV_VERSION_0
;
1027 open_blob
.info
.info0
= op
;
1029 DEBUG(10,("smbXsrv_open_close(0x%08x): "
1030 "stored disconnect\n",
1031 op
->global
->open_global_id
));
1032 NDR_PRINT_DEBUG(smbXsrv_openB
, &open_blob
);
1036 if (global_rec
!= NULL
) {
1037 status
= dbwrap_record_delete(global_rec
);
1038 if (!NT_STATUS_IS_OK(status
)) {
1039 TDB_DATA key
= dbwrap_record_get_key(global_rec
);
1041 DEBUG(0, ("smbXsrv_open_close(0x%08x): "
1042 "failed to delete global key '%s': %s\n",
1043 op
->global
->open_global_id
,
1044 hex_encode_talloc(global_rec
, key
.dptr
,
1046 nt_errstr(status
)));
1050 TALLOC_FREE(global_rec
);
1052 local_rec
= op
->db_rec
;
1053 if (local_rec
== NULL
) {
1054 uint8_t key_buf
[SMBXSRV_OPEN_LOCAL_TDB_KEY_SIZE
];
1057 key
= smbXsrv_open_local_id_to_key(op
->local_id
, key_buf
);
1059 local_rec
= dbwrap_fetch_locked(table
->local
.db_ctx
,
1061 if (local_rec
== NULL
) {
1062 DEBUG(0, ("smbXsrv_open_close(0x%08x): "
1063 "Failed to lock local key '%s'\n",
1064 op
->global
->open_global_id
,
1065 hex_encode_talloc(local_rec
, key
.dptr
,
1067 error
= NT_STATUS_INTERNAL_ERROR
;
1071 if (local_rec
!= NULL
) {
1072 status
= dbwrap_record_delete(local_rec
);
1073 if (!NT_STATUS_IS_OK(status
)) {
1074 TDB_DATA key
= dbwrap_record_get_key(local_rec
);
1076 DEBUG(0, ("smbXsrv_open_close(0x%08x): "
1077 "failed to delete local key '%s': %s\n",
1078 op
->global
->open_global_id
,
1079 hex_encode_talloc(local_rec
, key
.dptr
,
1081 nt_errstr(status
)));
1084 table
->local
.num_opens
-= 1;
1086 if (op
->db_rec
== NULL
) {
1087 TALLOC_FREE(local_rec
);
1092 op
->compat
->op
= NULL
;
1093 file_free(NULL
, op
->compat
);
1100 NTSTATUS
smb1srv_open_table_init(struct smbXsrv_connection
*conn
)
1105 * Allow a range from 1..65534.
1107 * With real_max_open_files possible ids,
1108 * truncated to the SMB1 limit of 16-bit.
1110 * 0 and 0xFFFF are no valid ids.
1112 max_opens
= conn
->client
->sconn
->real_max_open_files
;
1113 max_opens
= MIN(max_opens
, UINT16_MAX
- 1);
1115 return smbXsrv_open_table_init(conn
, 1, UINT16_MAX
- 1, max_opens
);
1118 NTSTATUS
smb1srv_open_lookup(struct smbXsrv_connection
*conn
,
1119 uint16_t fnum
, NTTIME now
,
1120 struct smbXsrv_open
**_open
)
1122 struct smbXsrv_open_table
*table
= conn
->client
->open_table
;
1123 uint32_t local_id
= fnum
;
1124 uint32_t global_id
= 0;
1126 return smbXsrv_open_local_lookup(table
, local_id
, global_id
, now
, _open
);
1129 NTSTATUS
smb2srv_open_table_init(struct smbXsrv_connection
*conn
)
1134 * Allow a range from 1..4294967294.
1136 * With real_max_open_files possible ids,
1137 * truncated to 16-bit (the same as SMB1 for now).
1139 * 0 and 0xFFFFFFFF are no valid ids.
1141 * The usage of conn->sconn->real_max_open_files
1142 * is the reason that we use one open table per
1143 * transport connection (as we still have a 1:1 mapping
1144 * between process and transport connection).
1146 max_opens
= conn
->client
->sconn
->real_max_open_files
;
1147 max_opens
= MIN(max_opens
, UINT16_MAX
- 1);
1149 return smbXsrv_open_table_init(conn
, 1, UINT32_MAX
- 1, max_opens
);
1152 NTSTATUS
smb2srv_open_lookup(struct smbXsrv_connection
*conn
,
1153 uint64_t persistent_id
,
1154 uint64_t volatile_id
,
1156 struct smbXsrv_open
**_open
)
1158 struct smbXsrv_open_table
*table
= conn
->client
->open_table
;
1159 uint32_t local_id
= volatile_id
& UINT32_MAX
;
1160 uint64_t local_zeros
= volatile_id
& 0xFFFFFFFF00000000LLU
;
1161 uint32_t global_id
= persistent_id
& UINT32_MAX
;
1162 uint64_t global_zeros
= persistent_id
& 0xFFFFFFFF00000000LLU
;
1164 if (local_zeros
!= 0) {
1165 return NT_STATUS_FILE_CLOSED
;
1168 if (global_zeros
!= 0) {
1169 return NT_STATUS_FILE_CLOSED
;
1172 if (global_id
== 0) {
1173 return NT_STATUS_FILE_CLOSED
;
1176 return smbXsrv_open_local_lookup(table
, local_id
, global_id
, now
, _open
);
1179 NTSTATUS
smb2srv_open_recreate(struct smbXsrv_connection
*conn
,
1180 struct auth_session_info
*session_info
,
1181 uint64_t persistent_id
,
1182 const struct GUID
*create_guid
,
1184 struct smbXsrv_open
**_open
)
1186 struct smbXsrv_open_table
*table
= conn
->client
->open_table
;
1187 struct db_record
*local_rec
= NULL
;
1188 struct smbXsrv_open
*op
= NULL
;
1191 uint32_t global_id
= persistent_id
& UINT32_MAX
;
1192 uint64_t global_zeros
= persistent_id
& 0xFFFFFFFF00000000LLU
;
1194 struct security_token
*current_token
= NULL
;
1196 if (session_info
== NULL
) {
1197 DEBUG(10, ("session_info=NULL\n"));
1198 return NT_STATUS_INVALID_HANDLE
;
1200 current_token
= session_info
->security_token
;
1202 if (current_token
== NULL
) {
1203 DEBUG(10, ("current_token=NULL\n"));
1204 return NT_STATUS_INVALID_HANDLE
;
1207 if (global_zeros
!= 0) {
1208 DEBUG(10, ("global_zeros!=0\n"));
1209 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
1212 op
= talloc_zero(table
, struct smbXsrv_open
);
1214 return NT_STATUS_NO_MEMORY
;
1218 status
= smbXsrv_open_global_lookup(table
, global_id
, op
, &op
->global
);
1219 if (!NT_STATUS_IS_OK(status
)) {
1221 DEBUG(10, ("smbXsrv_open_global_lookup returned %s\n",
1222 nt_errstr(status
)));
1227 * If the provided create_guid is NULL, this means that
1228 * the reconnect request was a v1 request. In that case
1229 * we should skipt the create GUID verification, since
1230 * it is valid to v1-reconnect a v2-opened handle.
1232 if ((create_guid
!= NULL
) &&
1233 !GUID_equal(&op
->global
->create_guid
, create_guid
))
1236 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
1239 if (!security_token_is_sid(current_token
, &op
->global
->open_owner
)) {
1241 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
1244 if (!op
->global
->durable
) {
1246 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
1249 if (table
->local
.num_opens
>= table
->local
.max_opens
) {
1251 return NT_STATUS_INSUFFICIENT_RESOURCES
;
1254 status
= smbXsrv_open_local_allocate_id(table
->local
.db_ctx
,
1255 table
->local
.lowest_id
,
1256 table
->local
.highest_id
,
1260 if (!NT_STATUS_IS_OK(status
)) {
1265 op
->idle_time
= now
;
1266 op
->status
= NT_STATUS_FILE_CLOSED
;
1268 op
->global
->open_volatile_id
= op
->local_id
;
1269 op
->global
->server_id
= messaging_server_id(conn
->msg_ctx
);
1272 val
= make_tdb_data((uint8_t const *)&ptr
, sizeof(ptr
));
1273 status
= dbwrap_record_store(local_rec
, val
, TDB_REPLACE
);
1274 TALLOC_FREE(local_rec
);
1275 if (!NT_STATUS_IS_OK(status
)) {
1279 table
->local
.num_opens
+= 1;
1281 talloc_set_destructor(op
, smbXsrv_open_destructor
);
1283 status
= smbXsrv_open_global_store(op
->global
);
1284 if (!NT_STATUS_IS_OK(status
)) {
1289 if (CHECK_DEBUGLVL(10)) {
1290 struct smbXsrv_openB open_blob
;
1292 ZERO_STRUCT(open_blob
);
1293 open_blob
.version
= 0;
1294 open_blob
.info
.info0
= op
;
1296 DEBUG(10,("smbXsrv_open_recreate: global_id (0x%08x) stored\n",
1297 op
->global
->open_global_id
));
1298 NDR_PRINT_DEBUG(smbXsrv_openB
, &open_blob
);
1302 return NT_STATUS_OK
;
1306 static NTSTATUS
smbXsrv_open_global_parse_record(TALLOC_CTX
*mem_ctx
,
1307 struct db_record
*rec
,
1308 struct smbXsrv_open_global0
**global
)
1310 TDB_DATA key
= dbwrap_record_get_key(rec
);
1311 TDB_DATA val
= dbwrap_record_get_value(rec
);
1312 DATA_BLOB blob
= data_blob_const(val
.dptr
, val
.dsize
);
1313 struct smbXsrv_open_globalB global_blob
;
1314 enum ndr_err_code ndr_err
;
1316 TALLOC_CTX
*frame
= talloc_stackframe();
1318 ndr_err
= ndr_pull_struct_blob(&blob
, frame
, &global_blob
,
1319 (ndr_pull_flags_fn_t
)ndr_pull_smbXsrv_open_globalB
);
1320 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
1321 DEBUG(1,("Invalid record in smbXsrv_open_global.tdb:"
1322 "key '%s' ndr_pull_struct_blob - %s\n",
1323 hex_encode_talloc(frame
, key
.dptr
, key
.dsize
),
1324 ndr_errstr(ndr_err
)));
1325 status
= ndr_map_error2ntstatus(ndr_err
);
1329 if (global_blob
.version
!= SMBXSRV_VERSION_0
) {
1330 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
1331 DEBUG(1,("Invalid record in smbXsrv_open_global.tdb:"
1332 "key '%s' unsuported version - %d - %s\n",
1333 hex_encode_talloc(frame
, key
.dptr
, key
.dsize
),
1334 (int)global_blob
.version
,
1335 nt_errstr(status
)));
1339 *global
= talloc_move(mem_ctx
, &global_blob
.info
.info0
);
1340 status
= NT_STATUS_OK
;
1346 struct smbXsrv_open_global_traverse_state
{
1347 int (*fn
)(struct smbXsrv_open_global0
*, void *);
1351 static int smbXsrv_open_global_traverse_fn(struct db_record
*rec
, void *data
)
1353 struct smbXsrv_open_global_traverse_state
*state
=
1354 (struct smbXsrv_open_global_traverse_state
*)data
;
1355 struct smbXsrv_open_global0
*global
= NULL
;
1359 status
= smbXsrv_open_global_parse_record(talloc_tos(), rec
, &global
);
1360 if (!NT_STATUS_IS_OK(status
)) {
1364 global
->db_rec
= rec
;
1365 ret
= state
->fn(global
, state
->private_data
);
1366 talloc_free(global
);
1370 NTSTATUS
smbXsrv_open_global_traverse(
1371 int (*fn
)(struct smbXsrv_open_global0
*, void *),
1377 struct smbXsrv_open_global_traverse_state state
= {
1379 .private_data
= private_data
,
1383 status
= smbXsrv_open_global_init();
1384 if (!NT_STATUS_IS_OK(status
)) {
1386 DEBUG(0, ("Failed to initialize open_global: %s\n",
1387 nt_errstr(status
)));
1391 status
= dbwrap_traverse_read(smbXsrv_open_global_db_ctx
,
1392 smbXsrv_open_global_traverse_fn
,
1400 NTSTATUS
smbXsrv_open_cleanup(uint64_t persistent_id
)
1402 NTSTATUS status
= NT_STATUS_OK
;
1403 TALLOC_CTX
*frame
= talloc_stackframe();
1404 struct smbXsrv_open_global0
*op
= NULL
;
1405 uint8_t key_buf
[SMBXSRV_OPEN_GLOBAL_TDB_KEY_SIZE
];
1408 struct db_record
*rec
;
1409 bool delete_open
= false;
1410 uint32_t global_id
= persistent_id
& UINT32_MAX
;
1412 key
= smbXsrv_open_global_id_to_key(global_id
, key_buf
);
1413 rec
= dbwrap_fetch_locked(smbXsrv_open_global_db_ctx
, frame
, key
);
1415 status
= NT_STATUS_NOT_FOUND
;
1416 DEBUG(1, ("smbXsrv_open_cleanup[global: 0x%08x] "
1417 "failed to fetch record from %s - %s\n",
1418 global_id
, dbwrap_name(smbXsrv_open_global_db_ctx
),
1419 nt_errstr(status
)));
1423 val
= dbwrap_record_get_value(rec
);
1424 if (val
.dsize
== 0) {
1425 DEBUG(10, ("smbXsrv_open_cleanup[global: 0x%08x] "
1426 "empty record in %s, skipping...\n",
1427 global_id
, dbwrap_name(smbXsrv_open_global_db_ctx
)));
1431 status
= smbXsrv_open_global_parse_record(talloc_tos(), rec
, &op
);
1432 if (!NT_STATUS_IS_OK(status
)) {
1433 DEBUG(1, ("smbXsrv_open_cleanup[global: 0x%08x] "
1434 "failed to read record: %s\n",
1435 global_id
, nt_errstr(status
)));
1439 if (server_id_is_disconnected(&op
->server_id
)) {
1440 struct timeval now
, disconnect_time
;
1442 now
= timeval_current();
1443 nttime_to_timeval(&disconnect_time
, op
->disconnect_time
);
1444 tdiff
= usec_time_diff(&now
, &disconnect_time
);
1445 delete_open
= (tdiff
>= 1000*op
->durable_timeout_msec
);
1447 DEBUG(10, ("smbXsrv_open_cleanup[global: 0x%08x] "
1448 "disconnected at [%s] %us ago with "
1449 "timeout of %us -%s reached\n",
1451 nt_time_string(frame
, op
->disconnect_time
),
1452 (unsigned)(tdiff
/1000000),
1453 op
->durable_timeout_msec
/ 1000,
1454 delete_open
? "" : " not"));
1455 } else if (!serverid_exists(&op
->server_id
)) {
1456 DEBUG(10, ("smbXsrv_open_cleanup[global: 0x%08x] "
1457 "server[%s] does not exist\n",
1458 global_id
, server_id_str(frame
, &op
->server_id
)));
1466 status
= dbwrap_record_delete(rec
);
1467 if (!NT_STATUS_IS_OK(status
)) {
1468 DEBUG(1, ("smbXsrv_open_cleanup[global: 0x%08x] "
1469 "failed to delete record"
1470 "from %s: %s\n", global_id
,
1471 dbwrap_name(smbXsrv_open_global_db_ctx
),
1472 nt_errstr(status
)));
1476 DEBUG(10, ("smbXsrv_open_cleanup[global: 0x%08x] "
1477 "delete record from %s\n",
1479 dbwrap_name(smbXsrv_open_global_db_ctx
)));