2 Unix SMB/CIFS implementation.
4 Copyright (C) Stefan Metzmacher 2011-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"
29 #include "lib/util/util_tdb.h"
30 #include "librpc/gen_ndr/ndr_smbXsrv.h"
33 struct smbXsrv_tcon_table
{
35 struct db_context
*db_ctx
;
42 struct db_context
*db_ctx
;
46 static struct db_context
*smbXsrv_tcon_global_db_ctx
= NULL
;
48 NTSTATUS
smbXsrv_tcon_global_init(void)
50 char *global_path
= NULL
;
51 struct db_context
*db_ctx
= NULL
;
53 if (smbXsrv_tcon_global_db_ctx
!= NULL
) {
57 global_path
= lock_path("smbXsrv_tcon_global.tdb");
58 if (global_path
== NULL
) {
59 return NT_STATUS_NO_MEMORY
;
62 db_ctx
= db_open(NULL
, global_path
,
66 TDB_INCOMPATIBLE_HASH
,
67 O_RDWR
| O_CREAT
, 0600,
70 TALLOC_FREE(global_path
);
74 status
= map_nt_error_from_unix_common(errno
);
79 smbXsrv_tcon_global_db_ctx
= db_ctx
;
86 * We need to store the keys in big endian so that dbwrap_rbt's memcmp
87 * has the same result as integer comparison between the uint32_t
90 * TODO: implement string based key
93 #define SMBXSRV_TCON_GLOBAL_TDB_KEY_SIZE sizeof(uint32_t)
95 static TDB_DATA
smbXsrv_tcon_global_id_to_key(uint32_t id
,
100 RSIVAL(key_buf
, 0, id
);
102 key
= make_tdb_data(key_buf
, SMBXSRV_TCON_GLOBAL_TDB_KEY_SIZE
);
108 static NTSTATUS
smbXsrv_tcon_global_key_to_id(TDB_DATA key
, uint32_t *id
)
111 return NT_STATUS_INVALID_PARAMETER
;
114 if (key
.dsize
!= SMBXSRV_TCON_GLOBAL_TDB_KEY_SIZE
) {
115 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
118 *id
= RIVAL(key
.dptr
, 0);
124 #define SMBXSRV_TCON_LOCAL_TDB_KEY_SIZE sizeof(uint32_t)
126 static TDB_DATA
smbXsrv_tcon_local_id_to_key(uint32_t id
,
131 RSIVAL(key_buf
, 0, id
);
133 key
= make_tdb_data(key_buf
, SMBXSRV_TCON_LOCAL_TDB_KEY_SIZE
);
138 static NTSTATUS
smbXsrv_tcon_local_key_to_id(TDB_DATA key
, uint32_t *id
)
141 return NT_STATUS_INVALID_PARAMETER
;
144 if (key
.dsize
!= SMBXSRV_TCON_LOCAL_TDB_KEY_SIZE
) {
145 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
148 *id
= RIVAL(key
.dptr
, 0);
153 static NTSTATUS
smbXsrv_tcon_table_init(TALLOC_CTX
*mem_ctx
,
154 struct smbXsrv_tcon_table
*table
,
162 if (lowest_id
> highest_id
) {
163 return NT_STATUS_INTERNAL_ERROR
;
166 max_range
= highest_id
;
167 max_range
-= lowest_id
;
170 if (max_tcons
> max_range
) {
171 return NT_STATUS_INTERNAL_ERROR
;
175 table
->local
.db_ctx
= db_open_rbt(table
);
176 if (table
->local
.db_ctx
== NULL
) {
177 return NT_STATUS_NO_MEMORY
;
179 table
->local
.lowest_id
= lowest_id
;
180 table
->local
.highest_id
= highest_id
;
181 table
->local
.max_tcons
= max_tcons
;
183 status
= smbXsrv_tcon_global_init();
184 if (!NT_STATUS_IS_OK(status
)) {
188 table
->global
.db_ctx
= smbXsrv_tcon_global_db_ctx
;
193 struct smb1srv_tcon_local_allocate_state
{
194 const uint32_t lowest_id
;
195 const uint32_t highest_id
;
201 static int smb1srv_tcon_local_allocate_traverse(struct db_record
*rec
,
204 struct smb1srv_tcon_local_allocate_state
*state
=
205 (struct smb1srv_tcon_local_allocate_state
*)private_data
;
206 TDB_DATA key
= dbwrap_record_get_key(rec
);
210 status
= smbXsrv_tcon_local_key_to_id(key
, &id
);
211 if (!NT_STATUS_IS_OK(status
)) {
212 state
->status
= status
;
216 if (id
<= state
->last_id
) {
217 state
->status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
222 if (id
> state
->useable_id
) {
223 state
->status
= NT_STATUS_OK
;
227 if (state
->useable_id
== state
->highest_id
) {
228 state
->status
= NT_STATUS_INSUFFICIENT_RESOURCES
;
232 state
->useable_id
+=1;
236 static NTSTATUS
smb1srv_tcon_local_allocate_id(struct db_context
*db
,
240 struct db_record
**_rec
,
243 struct smb1srv_tcon_local_allocate_state state
= {
244 .lowest_id
= lowest_id
,
245 .highest_id
= highest_id
,
247 .useable_id
= lowest_id
,
248 .status
= NT_STATUS_INTERNAL_ERROR
,
258 if (lowest_id
> highest_id
) {
259 return NT_STATUS_INSUFFICIENT_RESOURCES
;
263 * first we try randomly
265 range
= (highest_id
- lowest_id
) + 1;
267 for (i
= 0; i
< (range
/ 2); i
++) {
269 uint8_t key_buf
[SMBXSRV_TCON_LOCAL_TDB_KEY_SIZE
];
272 struct db_record
*rec
= NULL
;
274 id
= generate_random() % range
;
277 if (id
< lowest_id
) {
280 if (id
> highest_id
) {
284 key
= smbXsrv_tcon_local_id_to_key(id
, key_buf
);
286 rec
= dbwrap_fetch_locked(db
, mem_ctx
, key
);
288 return NT_STATUS_INSUFFICIENT_RESOURCES
;
291 val
= dbwrap_record_get_value(rec
);
292 if (val
.dsize
!= 0) {
303 * if the range is almost full,
304 * we traverse the whole table
305 * (this relies on sorted behavior of dbwrap_rbt)
307 status
= dbwrap_traverse_read(db
, smb1srv_tcon_local_allocate_traverse
,
309 if (NT_STATUS_IS_OK(status
)) {
310 if (NT_STATUS_IS_OK(state
.status
)) {
311 return NT_STATUS_INTERNAL_ERROR
;
314 if (!NT_STATUS_EQUAL(state
.status
, NT_STATUS_INTERNAL_ERROR
)) {
318 if (state
.useable_id
<= state
.highest_id
) {
319 state
.status
= NT_STATUS_OK
;
321 return NT_STATUS_INSUFFICIENT_RESOURCES
;
323 } else if (!NT_STATUS_EQUAL(status
, NT_STATUS_INTERNAL_DB_CORRUPTION
)) {
325 * Here we really expect NT_STATUS_INTERNAL_DB_CORRUPTION!
327 * If we get anything else it is an error, because it
328 * means we did not manage to find a free slot in
331 return NT_STATUS_INSUFFICIENT_RESOURCES
;
334 if (NT_STATUS_IS_OK(state
.status
)) {
336 uint8_t key_buf
[SMBXSRV_TCON_LOCAL_TDB_KEY_SIZE
];
339 struct db_record
*rec
= NULL
;
341 id
= state
.useable_id
;
343 key
= smbXsrv_tcon_local_id_to_key(id
, key_buf
);
345 rec
= dbwrap_fetch_locked(db
, mem_ctx
, key
);
347 return NT_STATUS_INSUFFICIENT_RESOURCES
;
350 val
= dbwrap_record_get_value(rec
);
351 if (val
.dsize
!= 0) {
353 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
364 struct smbXsrv_tcon_local_fetch_state
{
365 struct smbXsrv_tcon
*tcon
;
369 static void smbXsrv_tcon_local_fetch_parser(TDB_DATA key
, TDB_DATA data
,
372 struct smbXsrv_tcon_local_fetch_state
*state
=
373 (struct smbXsrv_tcon_local_fetch_state
*)private_data
;
376 if (data
.dsize
!= sizeof(ptr
)) {
377 state
->status
= NT_STATUS_INTERNAL_DB_ERROR
;
381 memcpy(&ptr
, data
.dptr
, data
.dsize
);
382 state
->tcon
= talloc_get_type_abort(ptr
, struct smbXsrv_tcon
);
383 state
->status
= NT_STATUS_OK
;
386 static NTSTATUS
smbXsrv_tcon_local_lookup(struct smbXsrv_tcon_table
*table
,
387 uint32_t tcon_local_id
,
389 struct smbXsrv_tcon
**_tcon
)
391 struct smbXsrv_tcon_local_fetch_state state
= {
393 .status
= NT_STATUS_INTERNAL_ERROR
,
395 uint8_t key_buf
[SMBXSRV_TCON_LOCAL_TDB_KEY_SIZE
];
401 if (tcon_local_id
== 0) {
402 return NT_STATUS_NETWORK_NAME_DELETED
;
406 /* this might happen before the end of negprot */
407 return NT_STATUS_NETWORK_NAME_DELETED
;
410 if (table
->local
.db_ctx
== NULL
) {
411 return NT_STATUS_INTERNAL_ERROR
;
414 key
= smbXsrv_tcon_local_id_to_key(tcon_local_id
, key_buf
);
416 status
= dbwrap_parse_record(table
->local
.db_ctx
, key
,
417 smbXsrv_tcon_local_fetch_parser
,
419 if (NT_STATUS_EQUAL(status
, NT_STATUS_NOT_FOUND
)) {
420 return NT_STATUS_NETWORK_NAME_DELETED
;
421 } else if (!NT_STATUS_IS_OK(status
)) {
424 if (!NT_STATUS_IS_OK(state
.status
)) {
428 if (NT_STATUS_EQUAL(state
.tcon
->status
, NT_STATUS_NETWORK_NAME_DELETED
)) {
429 return NT_STATUS_NETWORK_NAME_DELETED
;
432 state
.tcon
->idle_time
= now
;
435 return state
.tcon
->status
;
438 static int smbXsrv_tcon_global_destructor(struct smbXsrv_tcon_global0
*global
)
443 static void smbXsrv_tcon_global_verify_record(struct db_record
*db_rec
,
447 struct smbXsrv_tcon_global0
**_g
);
449 static NTSTATUS
smbXsrv_tcon_global_allocate(struct db_context
*db
,
451 struct smbXsrv_tcon_global0
**_global
)
454 struct smbXsrv_tcon_global0
*global
= NULL
;
455 uint32_t last_free
= 0;
456 const uint32_t min_tries
= 3;
460 global
= talloc_zero(mem_ctx
, struct smbXsrv_tcon_global0
);
461 if (global
== NULL
) {
462 return NT_STATUS_NO_MEMORY
;
464 talloc_set_destructor(global
, smbXsrv_tcon_global_destructor
);
467 * Here we just randomly try the whole 32-bit space
469 * We use just 32-bit, because we want to reuse the
472 for (i
= 0; i
< UINT32_MAX
; i
++) {
473 bool is_free
= false;
474 bool was_free
= false;
476 uint8_t key_buf
[SMBXSRV_TCON_GLOBAL_TDB_KEY_SIZE
];
479 if (i
>= min_tries
&& last_free
!= 0) {
482 id
= generate_random();
487 if (id
== UINT32_MAX
) {
491 key
= smbXsrv_tcon_global_id_to_key(id
, key_buf
);
493 global
->db_rec
= dbwrap_fetch_locked(db
, mem_ctx
, key
);
494 if (global
->db_rec
== NULL
) {
496 return NT_STATUS_INSUFFICIENT_RESOURCES
;
499 smbXsrv_tcon_global_verify_record(global
->db_rec
,
505 TALLOC_FREE(global
->db_rec
);
509 if (!was_free
&& i
< min_tries
) {
511 * The session_id is free now,
512 * but was not free before.
514 * This happens if a smbd crashed
515 * and did not cleanup the record.
517 * If this is one of our first tries,
518 * then we try to find a real free one.
520 if (last_free
== 0) {
523 TALLOC_FREE(global
->db_rec
);
527 global
->tcon_global_id
= id
;
533 /* should not be reached */
535 return NT_STATUS_INTERNAL_ERROR
;
538 static void smbXsrv_tcon_global_verify_record(struct db_record
*db_rec
,
542 struct smbXsrv_tcon_global0
**_g
)
547 struct smbXsrv_tcon_globalB global_blob
;
548 enum ndr_err_code ndr_err
;
549 struct smbXsrv_tcon_global0
*global
= NULL
;
551 TALLOC_CTX
*frame
= talloc_stackframe();
562 key
= dbwrap_record_get_key(db_rec
);
564 val
= dbwrap_record_get_value(db_rec
);
565 if (val
.dsize
== 0) {
574 blob
= data_blob_const(val
.dptr
, val
.dsize
);
576 ndr_err
= ndr_pull_struct_blob(&blob
, frame
, &global_blob
,
577 (ndr_pull_flags_fn_t
)ndr_pull_smbXsrv_tcon_globalB
);
578 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
579 NTSTATUS status
= ndr_map_error2ntstatus(ndr_err
);
580 DEBUG(1,("smbXsrv_tcon_global_verify_record: "
581 "key '%s' ndr_pull_struct_blob - %s\n",
582 hex_encode_talloc(frame
, key
.dptr
, key
.dsize
),
588 DEBUG(10,("smbXsrv_tcon_global_verify_record\n"));
590 NDR_PRINT_DEBUG(smbXsrv_tcon_globalB
, &global_blob
);
593 if (global_blob
.version
!= SMBXSRV_VERSION_0
) {
594 DEBUG(0,("smbXsrv_tcon_global_verify_record: "
595 "key '%s' use unsupported version %u\n",
596 hex_encode_talloc(frame
, key
.dptr
, key
.dsize
),
597 global_blob
.version
));
598 NDR_PRINT_DEBUG(smbXsrv_tcon_globalB
, &global_blob
);
603 global
= global_blob
.info
.info0
;
605 exists
= serverid_exists(&global
->server_id
);
607 DEBUG(2,("smbXsrv_tcon_global_verify_record: "
608 "key '%s' server_id %s does not exist.\n",
609 hex_encode_talloc(frame
, key
.dptr
, key
.dsize
),
610 server_id_str(frame
, &global
->server_id
)));
612 NDR_PRINT_DEBUG(smbXsrv_tcon_globalB
, &global_blob
);
615 dbwrap_record_delete(db_rec
);
621 *_g
= talloc_move(mem_ctx
, &global
);
626 static NTSTATUS
smbXsrv_tcon_global_store(struct smbXsrv_tcon_global0
*global
)
628 struct smbXsrv_tcon_globalB global_blob
;
629 DATA_BLOB blob
= data_blob_null
;
633 enum ndr_err_code ndr_err
;
636 * TODO: if we use other versions than '0'
637 * we would add glue code here, that would be able to
638 * store the information in the old format.
641 if (global
->db_rec
== NULL
) {
642 return NT_STATUS_INTERNAL_ERROR
;
645 key
= dbwrap_record_get_key(global
->db_rec
);
646 val
= dbwrap_record_get_value(global
->db_rec
);
648 ZERO_STRUCT(global_blob
);
649 global_blob
.version
= smbXsrv_version_global_current();
650 if (val
.dsize
>= 8) {
651 global_blob
.seqnum
= IVAL(val
.dptr
, 4);
653 global_blob
.seqnum
+= 1;
654 global_blob
.info
.info0
= global
;
656 ndr_err
= ndr_push_struct_blob(&blob
, global
->db_rec
, &global_blob
,
657 (ndr_push_flags_fn_t
)ndr_push_smbXsrv_tcon_globalB
);
658 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
659 status
= ndr_map_error2ntstatus(ndr_err
);
660 DEBUG(1,("smbXsrv_tcon_global_store: key '%s' ndr_push - %s\n",
661 hex_encode_talloc(global
->db_rec
, key
.dptr
, key
.dsize
),
663 TALLOC_FREE(global
->db_rec
);
667 val
= make_tdb_data(blob
.data
, blob
.length
);
668 status
= dbwrap_record_store(global
->db_rec
, val
, TDB_REPLACE
);
669 if (!NT_STATUS_IS_OK(status
)) {
670 DEBUG(1,("smbXsrv_tcon_global_store: key '%s' store - %s\n",
671 hex_encode_talloc(global
->db_rec
, key
.dptr
, key
.dsize
),
673 TALLOC_FREE(global
->db_rec
);
678 DEBUG(10,("smbXsrv_tcon_global_store: key '%s' stored\n",
679 hex_encode_talloc(global
->db_rec
, key
.dptr
, key
.dsize
)));
680 NDR_PRINT_DEBUG(smbXsrv_tcon_globalB
, &global_blob
);
683 TALLOC_FREE(global
->db_rec
);
688 static int smbXsrv_tcon_destructor(struct smbXsrv_tcon
*tcon
)
692 status
= smbXsrv_tcon_disconnect(tcon
, 0);
693 if (!NT_STATUS_IS_OK(status
)) {
694 DEBUG(0, ("smbXsrv_tcon_destructor: "
695 "smbXsrv_tcon_disconnect() failed - %s\n",
699 TALLOC_FREE(tcon
->global
);
704 static NTSTATUS
smbXsrv_tcon_create(struct smbXsrv_tcon_table
*table
,
705 enum protocol_types protocol
,
706 struct server_id server_id
,
708 struct smbXsrv_tcon
**_tcon
)
710 struct db_record
*local_rec
= NULL
;
711 struct smbXsrv_tcon
*tcon
= NULL
;
714 struct smbXsrv_tcon_global0
*global
= NULL
;
717 if (table
->local
.num_tcons
>= table
->local
.max_tcons
) {
718 return NT_STATUS_INSUFFICIENT_RESOURCES
;
721 tcon
= talloc_zero(table
, struct smbXsrv_tcon
);
723 return NT_STATUS_NO_MEMORY
;
726 tcon
->status
= NT_STATUS_INTERNAL_ERROR
;
727 tcon
->idle_time
= now
;
729 status
= smbXsrv_tcon_global_allocate(table
->global
.db_ctx
,
731 if (!NT_STATUS_IS_OK(status
)) {
735 tcon
->global
= global
;
737 if (protocol
>= PROTOCOL_SMB2_02
) {
738 uint64_t id
= global
->tcon_global_id
;
739 uint8_t key_buf
[SMBXSRV_TCON_LOCAL_TDB_KEY_SIZE
];
742 global
->tcon_wire_id
= id
;
744 tcon
->local_id
= global
->tcon_global_id
;
746 key
= smbXsrv_tcon_local_id_to_key(tcon
->local_id
, key_buf
);
748 local_rec
= dbwrap_fetch_locked(table
->local
.db_ctx
,
750 if (local_rec
== NULL
) {
752 return NT_STATUS_NO_MEMORY
;
755 val
= dbwrap_record_get_value(local_rec
);
756 if (val
.dsize
!= 0) {
758 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
762 status
= smb1srv_tcon_local_allocate_id(table
->local
.db_ctx
,
763 table
->local
.lowest_id
,
764 table
->local
.highest_id
,
768 if (!NT_STATUS_IS_OK(status
)) {
773 global
->tcon_wire_id
= tcon
->local_id
;
776 global
->creation_time
= now
;
778 global
->server_id
= server_id
;
781 val
= make_tdb_data((uint8_t const *)&ptr
, sizeof(ptr
));
782 status
= dbwrap_record_store(local_rec
, val
, TDB_REPLACE
);
783 TALLOC_FREE(local_rec
);
784 if (!NT_STATUS_IS_OK(status
)) {
788 table
->local
.num_tcons
+= 1;
790 talloc_set_destructor(tcon
, smbXsrv_tcon_destructor
);
792 status
= smbXsrv_tcon_global_store(global
);
793 if (!NT_STATUS_IS_OK(status
)) {
794 DEBUG(0,("smbXsrv_tcon_create: "
795 "global_id (0x%08x) store failed - %s\n",
796 tcon
->global
->tcon_global_id
,
803 struct smbXsrv_tconB tcon_blob
;
805 ZERO_STRUCT(tcon_blob
);
806 tcon_blob
.version
= SMBXSRV_VERSION_0
;
807 tcon_blob
.info
.info0
= tcon
;
809 DEBUG(10,("smbXsrv_tcon_create: global_id (0x%08x) stored\n",
810 tcon
->global
->tcon_global_id
));
811 NDR_PRINT_DEBUG(smbXsrv_tconB
, &tcon_blob
);
818 NTSTATUS
smbXsrv_tcon_update(struct smbXsrv_tcon
*tcon
)
820 struct smbXsrv_tcon_table
*table
= tcon
->table
;
822 uint8_t key_buf
[SMBXSRV_TCON_GLOBAL_TDB_KEY_SIZE
];
825 if (tcon
->global
->db_rec
!= NULL
) {
826 DEBUG(0, ("smbXsrv_tcon_update(0x%08x): "
827 "Called with db_rec != NULL'\n",
828 tcon
->global
->tcon_global_id
));
829 return NT_STATUS_INTERNAL_ERROR
;
832 key
= smbXsrv_tcon_global_id_to_key(tcon
->global
->tcon_global_id
,
835 tcon
->global
->db_rec
= dbwrap_fetch_locked(table
->global
.db_ctx
,
837 if (tcon
->global
->db_rec
== NULL
) {
838 DEBUG(0, ("smbXsrv_tcon_update(0x%08x): "
839 "Failed to lock global key '%s'\n",
840 tcon
->global
->tcon_global_id
,
841 hex_encode_talloc(talloc_tos(), key
.dptr
,
843 return NT_STATUS_INTERNAL_DB_ERROR
;
846 status
= smbXsrv_tcon_global_store(tcon
->global
);
847 if (!NT_STATUS_IS_OK(status
)) {
848 DEBUG(0,("smbXsrv_tcon_update: "
849 "global_id (0x%08x) store failed - %s\n",
850 tcon
->global
->tcon_global_id
,
856 struct smbXsrv_tconB tcon_blob
;
858 ZERO_STRUCT(tcon_blob
);
859 tcon_blob
.version
= SMBXSRV_VERSION_0
;
860 tcon_blob
.info
.info0
= tcon
;
862 DEBUG(10,("smbXsrv_tcon_update: global_id (0x%08x) stored\n",
863 tcon
->global
->tcon_global_id
));
864 NDR_PRINT_DEBUG(smbXsrv_tconB
, &tcon_blob
);
870 NTSTATUS
smbXsrv_tcon_disconnect(struct smbXsrv_tcon
*tcon
, uint64_t vuid
)
872 struct smbXsrv_tcon_table
*table
;
873 struct db_record
*local_rec
= NULL
;
874 struct db_record
*global_rec
= NULL
;
876 NTSTATUS error
= NT_STATUS_OK
;
878 if (tcon
->table
== NULL
) {
885 tcon
->status
= NT_STATUS_NETWORK_NAME_DELETED
;
887 global_rec
= tcon
->global
->db_rec
;
888 tcon
->global
->db_rec
= NULL
;
889 if (global_rec
== NULL
) {
890 uint8_t key_buf
[SMBXSRV_TCON_GLOBAL_TDB_KEY_SIZE
];
893 key
= smbXsrv_tcon_global_id_to_key(
894 tcon
->global
->tcon_global_id
,
897 global_rec
= dbwrap_fetch_locked(table
->global
.db_ctx
,
899 if (global_rec
== NULL
) {
900 DEBUG(0, ("smbXsrv_tcon_disconnect(0x%08x, '%s'): "
901 "Failed to lock global key '%s'\n",
902 tcon
->global
->tcon_global_id
,
903 tcon
->global
->share_name
,
904 hex_encode_talloc(global_rec
, key
.dptr
,
906 error
= NT_STATUS_INTERNAL_ERROR
;
910 if (global_rec
!= NULL
) {
911 status
= dbwrap_record_delete(global_rec
);
912 if (!NT_STATUS_IS_OK(status
)) {
913 TDB_DATA key
= dbwrap_record_get_key(global_rec
);
915 DEBUG(0, ("smbXsrv_tcon_disconnect(0x%08x, '%s'): "
916 "failed to delete global key '%s': %s\n",
917 tcon
->global
->tcon_global_id
,
918 tcon
->global
->share_name
,
919 hex_encode_talloc(global_rec
, key
.dptr
,
925 TALLOC_FREE(global_rec
);
927 local_rec
= tcon
->db_rec
;
928 if (local_rec
== NULL
) {
929 uint8_t key_buf
[SMBXSRV_TCON_LOCAL_TDB_KEY_SIZE
];
932 key
= smbXsrv_tcon_local_id_to_key(tcon
->local_id
, key_buf
);
934 local_rec
= dbwrap_fetch_locked(table
->local
.db_ctx
,
936 if (local_rec
== NULL
) {
937 DEBUG(0, ("smbXsrv_tcon_disconnect(0x%08x, '%s'): "
938 "Failed to lock local key '%s'\n",
939 tcon
->global
->tcon_global_id
,
940 tcon
->global
->share_name
,
941 hex_encode_talloc(local_rec
, key
.dptr
,
943 error
= NT_STATUS_INTERNAL_ERROR
;
947 if (local_rec
!= NULL
) {
948 status
= dbwrap_record_delete(local_rec
);
949 if (!NT_STATUS_IS_OK(status
)) {
950 TDB_DATA key
= dbwrap_record_get_key(local_rec
);
952 DEBUG(0, ("smbXsrv_tcon_disconnect(0x%08x, '%s'): "
953 "failed to delete local key '%s': %s\n",
954 tcon
->global
->tcon_global_id
,
955 tcon
->global
->share_name
,
956 hex_encode_talloc(local_rec
, key
.dptr
,
961 table
->local
.num_tcons
-= 1;
963 if (tcon
->db_rec
== NULL
) {
964 TALLOC_FREE(local_rec
);
971 ok
= set_current_service(tcon
->compat
, 0, true);
973 status
= NT_STATUS_INTERNAL_ERROR
;
974 DEBUG(0, ("smbXsrv_tcon_disconnect(0x%08x, '%s'): "
975 "set_current_service() failed: %s\n",
976 tcon
->global
->tcon_global_id
,
977 tcon
->global
->share_name
,
983 close_cnum(tcon
->compat
, vuid
);
990 struct smbXsrv_tcon_disconnect_all_state
{
992 NTSTATUS first_status
;
996 static int smbXsrv_tcon_disconnect_all_callback(struct db_record
*local_rec
,
999 static NTSTATUS
smbXsrv_tcon_disconnect_all(struct smbXsrv_tcon_table
*table
,
1002 struct smbXsrv_tcon_disconnect_all_state state
;
1006 if (table
== NULL
) {
1007 return NT_STATUS_OK
;
1013 status
= dbwrap_traverse(table
->local
.db_ctx
,
1014 smbXsrv_tcon_disconnect_all_callback
,
1016 if (!NT_STATUS_IS_OK(status
)) {
1017 DEBUG(0, ("smbXsrv_tcon_disconnect_all: "
1018 "dbwrap_traverse() failed: %s\n",
1019 nt_errstr(status
)));
1023 if (!NT_STATUS_IS_OK(state
.first_status
)) {
1024 DEBUG(0, ("smbXsrv_tcon_disconnect_all: "
1025 "count[%d] errors[%d] first[%s]\n",
1026 count
, state
.errors
,
1027 nt_errstr(state
.first_status
)));
1028 return state
.first_status
;
1031 return NT_STATUS_OK
;
1034 static int smbXsrv_tcon_disconnect_all_callback(struct db_record
*local_rec
,
1037 struct smbXsrv_tcon_disconnect_all_state
*state
=
1038 (struct smbXsrv_tcon_disconnect_all_state
*)private_data
;
1041 struct smbXsrv_tcon
*tcon
= NULL
;
1045 val
= dbwrap_record_get_value(local_rec
);
1046 if (val
.dsize
!= sizeof(ptr
)) {
1047 status
= NT_STATUS_INTERNAL_ERROR
;
1048 if (NT_STATUS_IS_OK(state
->first_status
)) {
1049 state
->first_status
= status
;
1055 memcpy(&ptr
, val
.dptr
, val
.dsize
);
1056 tcon
= talloc_get_type_abort(ptr
, struct smbXsrv_tcon
);
1059 if (vuid
== 0 && tcon
->compat
) {
1060 vuid
= tcon
->compat
->vuid
;
1063 tcon
->db_rec
= local_rec
;
1064 status
= smbXsrv_tcon_disconnect(tcon
, vuid
);
1065 if (!NT_STATUS_IS_OK(status
)) {
1066 if (NT_STATUS_IS_OK(state
->first_status
)) {
1067 state
->first_status
= status
;
1076 NTSTATUS
smb1srv_tcon_table_init(struct smbXsrv_connection
*conn
)
1078 struct smbXsrv_client
*client
= conn
->client
;
1081 * Allow a range from 1..65534 with 65534 values.
1083 client
->tcon_table
= talloc_zero(client
, struct smbXsrv_tcon_table
);
1084 if (client
->tcon_table
== NULL
) {
1085 return NT_STATUS_NO_MEMORY
;
1088 return smbXsrv_tcon_table_init(client
, client
->tcon_table
,
1093 NTSTATUS
smb1srv_tcon_create(struct smbXsrv_connection
*conn
,
1095 struct smbXsrv_tcon
**_tcon
)
1097 struct server_id id
= messaging_server_id(conn
->msg_ctx
);
1099 return smbXsrv_tcon_create(conn
->client
->tcon_table
,
1104 NTSTATUS
smb1srv_tcon_lookup(struct smbXsrv_connection
*conn
,
1105 uint16_t tree_id
, NTTIME now
,
1106 struct smbXsrv_tcon
**tcon
)
1108 uint32_t local_id
= tree_id
;
1110 return smbXsrv_tcon_local_lookup(conn
->client
->tcon_table
,
1111 local_id
, now
, tcon
);
1114 NTSTATUS
smb1srv_tcon_disconnect_all(struct smbXsrv_connection
*conn
)
1116 struct smbXsrv_client
*client
= conn
->client
;
1119 * We do not pass a vuid here,
1120 * which means the vuid is taken from
1121 * the tcon->compat->vuid.
1123 * NOTE: that tcon->compat->vuid may point to
1124 * a none existing vuid (or the wrong one)
1125 * as the tcon can exist without a session
1128 * This matches the old behavior of
1129 * conn_close_all(), but we should think
1130 * about how to fix this in future.
1132 return smbXsrv_tcon_disconnect_all(client
->tcon_table
, 0);
1135 NTSTATUS
smb2srv_tcon_table_init(struct smbXsrv_session
*session
)
1138 * Allow a range from 1..4294967294 with 65534 (same as SMB1) values.
1140 session
->tcon_table
= talloc_zero(session
, struct smbXsrv_tcon_table
);
1141 if (session
->tcon_table
== NULL
) {
1142 return NT_STATUS_NO_MEMORY
;
1145 return smbXsrv_tcon_table_init(session
, session
->tcon_table
,
1150 NTSTATUS
smb2srv_tcon_create(struct smbXsrv_session
*session
,
1152 struct smbXsrv_tcon
**_tcon
)
1154 struct server_id id
= messaging_server_id(session
->client
->msg_ctx
);
1156 return smbXsrv_tcon_create(session
->tcon_table
,
1161 NTSTATUS
smb2srv_tcon_lookup(struct smbXsrv_session
*session
,
1162 uint32_t tree_id
, NTTIME now
,
1163 struct smbXsrv_tcon
**tcon
)
1165 uint32_t local_id
= tree_id
;
1167 return smbXsrv_tcon_local_lookup(session
->tcon_table
,
1168 local_id
, now
, tcon
);
1171 NTSTATUS
smb2srv_tcon_disconnect_all(struct smbXsrv_session
*session
)
1175 if (session
->compat
) {
1176 vuid
= session
->compat
->vuid
;
1181 return smbXsrv_tcon_disconnect_all(session
->tcon_table
, vuid
);
1184 struct smbXsrv_tcon_global_traverse_state
{
1185 int (*fn
)(struct smbXsrv_tcon_global0
*, void *);
1189 static int smbXsrv_tcon_global_traverse_fn(struct db_record
*rec
, void *data
)
1192 struct smbXsrv_tcon_global_traverse_state
*state
=
1193 (struct smbXsrv_tcon_global_traverse_state
*)data
;
1194 TDB_DATA key
= dbwrap_record_get_key(rec
);
1195 TDB_DATA val
= dbwrap_record_get_value(rec
);
1196 DATA_BLOB blob
= data_blob_const(val
.dptr
, val
.dsize
);
1197 struct smbXsrv_tcon_globalB global_blob
;
1198 enum ndr_err_code ndr_err
;
1199 TALLOC_CTX
*frame
= talloc_stackframe();
1201 ndr_err
= ndr_pull_struct_blob(&blob
, frame
, &global_blob
,
1202 (ndr_pull_flags_fn_t
)ndr_pull_smbXsrv_tcon_globalB
);
1203 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
1204 DEBUG(1,("Invalid record in smbXsrv_tcon_global.tdb:"
1205 "key '%s' ndr_pull_struct_blob - %s\n",
1206 hex_encode_talloc(frame
, key
.dptr
, key
.dsize
),
1207 ndr_errstr(ndr_err
)));
1211 if (global_blob
.version
!= SMBXSRV_VERSION_0
) {
1212 DEBUG(1,("Invalid record in smbXsrv_tcon_global.tdb:"
1213 "key '%s' unsuported version - %d\n",
1214 hex_encode_talloc(frame
, key
.dptr
, key
.dsize
),
1215 (int)global_blob
.version
));
1219 global_blob
.info
.info0
->db_rec
= rec
;
1220 ret
= state
->fn(global_blob
.info
.info0
, state
->private_data
);
1226 NTSTATUS
smbXsrv_tcon_global_traverse(
1227 int (*fn
)(struct smbXsrv_tcon_global0
*, void *),
1232 struct smbXsrv_tcon_global_traverse_state state
= {
1234 .private_data
= private_data
,
1238 status
= smbXsrv_tcon_global_init();
1239 if (!NT_STATUS_IS_OK(status
)) {
1241 DEBUG(0, ("Failed to initialize tcon_global: %s\n",
1242 nt_errstr(status
)));
1246 status
= dbwrap_traverse_read(smbXsrv_tcon_global_db_ctx
,
1247 smbXsrv_tcon_global_traverse_fn
,