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"
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 "dbwrap/dbwrap_watch.h"
33 #include "auth/gensec/gensec.h"
34 #include "../lib/tsocket/tsocket.h"
35 #include "../libcli/security/security.h"
37 #include "lib/util/util_tdb.h"
38 #include "librpc/gen_ndr/ndr_smbXsrv.h"
40 #include "lib/util/tevent_ntstatus.h"
41 #include "lib/global_contexts.h"
43 struct smbXsrv_session_table
{
45 struct db_context
*db_ctx
;
48 uint32_t max_sessions
;
49 uint32_t num_sessions
;
52 struct db_context
*db_ctx
;
56 static struct db_context
*smbXsrv_session_global_db_ctx
= NULL
;
58 NTSTATUS
smbXsrv_session_global_init(struct messaging_context
*msg_ctx
)
60 char *global_path
= NULL
;
61 struct db_context
*backend
= NULL
;
62 struct db_context
*db_ctx
= NULL
;
64 if (smbXsrv_session_global_db_ctx
!= NULL
) {
69 * This contains secret information like session keys!
71 global_path
= lock_path(talloc_tos(), "smbXsrv_session_global.tdb");
72 if (global_path
== NULL
) {
73 return NT_STATUS_NO_MEMORY
;
76 backend
= db_open(NULL
, global_path
,
81 TDB_INCOMPATIBLE_HASH
,
82 O_RDWR
| O_CREAT
, 0600,
85 TALLOC_FREE(global_path
);
86 if (backend
== NULL
) {
89 status
= map_nt_error_from_unix_common(errno
);
94 db_ctx
= db_open_watched(NULL
, &backend
, global_messaging_context());
97 return NT_STATUS_NO_MEMORY
;
100 smbXsrv_session_global_db_ctx
= db_ctx
;
107 * We need to store the keys in big endian so that dbwrap_rbt's memcmp
108 * has the same result as integer comparison between the uint32_t
111 * TODO: implement string based key
114 #define SMBXSRV_SESSION_GLOBAL_TDB_KEY_SIZE sizeof(uint32_t)
116 static TDB_DATA
smbXsrv_session_global_id_to_key(uint32_t id
,
121 RSIVAL(key_buf
, 0, id
);
123 key
= make_tdb_data(key_buf
, SMBXSRV_SESSION_GLOBAL_TDB_KEY_SIZE
);
129 static NTSTATUS
smbXsrv_session_global_key_to_id(TDB_DATA key
, uint32_t *id
)
132 return NT_STATUS_INVALID_PARAMETER
;
135 if (key
.dsize
!= SMBXSRV_SESSION_GLOBAL_TDB_KEY_SIZE
) {
136 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
139 *id
= RIVAL(key
.dptr
, 0);
145 #define SMBXSRV_SESSION_LOCAL_TDB_KEY_SIZE sizeof(uint32_t)
147 static TDB_DATA
smbXsrv_session_local_id_to_key(uint32_t id
,
152 RSIVAL(key_buf
, 0, id
);
154 key
= make_tdb_data(key_buf
, SMBXSRV_SESSION_LOCAL_TDB_KEY_SIZE
);
159 static NTSTATUS
smbXsrv_session_local_key_to_id(TDB_DATA key
, uint32_t *id
)
162 return NT_STATUS_INVALID_PARAMETER
;
165 if (key
.dsize
!= SMBXSRV_SESSION_LOCAL_TDB_KEY_SIZE
) {
166 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
169 *id
= RIVAL(key
.dptr
, 0);
174 static struct db_record
*smbXsrv_session_global_fetch_locked(
175 struct db_context
*db
,
180 uint8_t key_buf
[SMBXSRV_SESSION_GLOBAL_TDB_KEY_SIZE
];
181 struct db_record
*rec
= NULL
;
183 key
= smbXsrv_session_global_id_to_key(id
, key_buf
);
185 rec
= dbwrap_fetch_locked(db
, mem_ctx
, key
);
188 DBG_DEBUG("Failed to lock global id 0x%08x, key '%s'\n", id
,
189 hex_encode_talloc(talloc_tos(), key
.dptr
, key
.dsize
));
195 static struct db_record
*smbXsrv_session_local_fetch_locked(
196 struct db_context
*db
,
201 uint8_t key_buf
[SMBXSRV_SESSION_LOCAL_TDB_KEY_SIZE
];
202 struct db_record
*rec
= NULL
;
204 key
= smbXsrv_session_local_id_to_key(id
, key_buf
);
206 rec
= dbwrap_fetch_locked(db
, mem_ctx
, key
);
209 DBG_DEBUG("Failed to lock local id 0x%08x, key '%s'\n", id
,
210 hex_encode_talloc(talloc_tos(), key
.dptr
, key
.dsize
));
216 static void smbXsrv_session_close_loop(struct tevent_req
*subreq
);
218 static NTSTATUS
smbXsrv_session_table_init(struct smbXsrv_connection
*conn
,
221 uint32_t max_sessions
)
223 struct smbXsrv_client
*client
= conn
->client
;
224 struct smbXsrv_session_table
*table
;
226 struct tevent_req
*subreq
;
229 if (lowest_id
> highest_id
) {
230 return NT_STATUS_INTERNAL_ERROR
;
233 max_range
= highest_id
;
234 max_range
-= lowest_id
;
237 if (max_sessions
> max_range
) {
238 return NT_STATUS_INTERNAL_ERROR
;
241 table
= talloc_zero(client
, struct smbXsrv_session_table
);
243 return NT_STATUS_NO_MEMORY
;
246 table
->local
.db_ctx
= db_open_rbt(table
);
247 if (table
->local
.db_ctx
== NULL
) {
249 return NT_STATUS_NO_MEMORY
;
251 table
->local
.lowest_id
= lowest_id
;
252 table
->local
.highest_id
= highest_id
;
253 table
->local
.max_sessions
= max_sessions
;
255 status
= smbXsrv_session_global_init(client
->msg_ctx
);
256 if (!NT_STATUS_IS_OK(status
)) {
261 table
->global
.db_ctx
= smbXsrv_session_global_db_ctx
;
263 subreq
= messaging_read_send(table
,
266 MSG_SMBXSRV_SESSION_CLOSE
);
267 if (subreq
== NULL
) {
269 return NT_STATUS_NO_MEMORY
;
271 tevent_req_set_callback(subreq
, smbXsrv_session_close_loop
, client
);
273 client
->session_table
= table
;
277 static void smbXsrv_session_close_shutdown_done(struct tevent_req
*subreq
);
279 static void smbXsrv_session_close_loop(struct tevent_req
*subreq
)
281 struct smbXsrv_client
*client
=
282 tevent_req_callback_data(subreq
,
283 struct smbXsrv_client
);
284 struct smbXsrv_session_table
*table
= client
->session_table
;
286 struct messaging_rec
*rec
= NULL
;
287 struct smbXsrv_session_closeB close_blob
;
288 enum ndr_err_code ndr_err
;
289 struct smbXsrv_session_close0
*close_info0
= NULL
;
290 struct smbXsrv_session
*session
= NULL
;
292 struct timeval tv
= timeval_current();
293 NTTIME now
= timeval_to_nttime(&tv
);
295 ret
= messaging_read_recv(subreq
, talloc_tos(), &rec
);
301 ndr_err
= ndr_pull_struct_blob(&rec
->buf
, rec
, &close_blob
,
302 (ndr_pull_flags_fn_t
)ndr_pull_smbXsrv_session_closeB
);
303 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
304 status
= ndr_map_error2ntstatus(ndr_err
);
305 DEBUG(1,("smbXsrv_session_close_loop: "
306 "ndr_pull_struct_blob - %s\n",
311 DEBUG(10,("smbXsrv_session_close_loop: MSG_SMBXSRV_SESSION_CLOSE\n"));
313 NDR_PRINT_DEBUG(smbXsrv_session_closeB
, &close_blob
);
316 if (close_blob
.version
!= SMBXSRV_VERSION_0
) {
317 DEBUG(0,("smbXsrv_session_close_loop: "
318 "ignore invalid version %u\n", close_blob
.version
));
319 NDR_PRINT_DEBUG(smbXsrv_session_closeB
, &close_blob
);
323 close_info0
= close_blob
.info
.info0
;
324 if (close_info0
== NULL
) {
325 DEBUG(0,("smbXsrv_session_close_loop: "
326 "ignore NULL info %u\n", close_blob
.version
));
327 NDR_PRINT_DEBUG(smbXsrv_session_closeB
, &close_blob
);
331 status
= smb2srv_session_lookup_client(client
,
332 close_info0
->old_session_wire_id
,
334 if (NT_STATUS_EQUAL(status
, NT_STATUS_USER_SESSION_DELETED
)) {
335 DEBUG(4,("smbXsrv_session_close_loop: "
336 "old_session_wire_id %llu not found\n",
337 (unsigned long long)close_info0
->old_session_wire_id
));
339 NDR_PRINT_DEBUG(smbXsrv_session_closeB
, &close_blob
);
343 if (!NT_STATUS_IS_OK(status
) &&
344 !NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
) &&
345 !NT_STATUS_EQUAL(status
, NT_STATUS_NETWORK_SESSION_EXPIRED
)) {
346 DEBUG(1,("smbXsrv_session_close_loop: "
347 "old_session_wire_id %llu - %s\n",
348 (unsigned long long)close_info0
->old_session_wire_id
,
351 NDR_PRINT_DEBUG(smbXsrv_session_closeB
, &close_blob
);
356 if (session
->global
->session_global_id
!= close_info0
->old_session_global_id
) {
357 DEBUG(1,("smbXsrv_session_close_loop: "
358 "old_session_wire_id %llu - global %u != %u\n",
359 (unsigned long long)close_info0
->old_session_wire_id
,
360 session
->global
->session_global_id
,
361 close_info0
->old_session_global_id
));
363 NDR_PRINT_DEBUG(smbXsrv_session_closeB
, &close_blob
);
368 if (session
->global
->creation_time
!= close_info0
->old_creation_time
) {
369 DEBUG(1,("smbXsrv_session_close_loop: "
370 "old_session_wire_id %llu - "
371 "creation %s (%llu) != %s (%llu)\n",
372 (unsigned long long)close_info0
->old_session_wire_id
,
373 nt_time_string(rec
, session
->global
->creation_time
),
374 (unsigned long long)session
->global
->creation_time
,
375 nt_time_string(rec
, close_info0
->old_creation_time
),
376 (unsigned long long)close_info0
->old_creation_time
));
378 NDR_PRINT_DEBUG(smbXsrv_session_closeB
, &close_blob
);
383 subreq
= smb2srv_session_shutdown_send(session
, client
->raw_ev_ctx
,
385 if (subreq
== NULL
) {
386 status
= NT_STATUS_NO_MEMORY
;
387 DEBUG(0, ("smbXsrv_session_close_loop: "
388 "smb2srv_session_shutdown_send(%llu) failed: %s\n",
389 (unsigned long long)session
->global
->session_wire_id
,
392 NDR_PRINT_DEBUG(smbXsrv_session_closeB
, &close_blob
);
396 tevent_req_set_callback(subreq
,
397 smbXsrv_session_close_shutdown_done
,
403 subreq
= messaging_read_send(table
,
406 MSG_SMBXSRV_SESSION_CLOSE
);
407 if (subreq
== NULL
) {
409 r
= "messaging_read_send(MSG_SMBXSRV_SESSION_CLOSE) failed";
410 exit_server_cleanly(r
);
413 tevent_req_set_callback(subreq
, smbXsrv_session_close_loop
, client
);
416 static void smbXsrv_session_close_shutdown_done(struct tevent_req
*subreq
)
418 struct smbXsrv_session
*session
=
419 tevent_req_callback_data(subreq
,
420 struct smbXsrv_session
);
423 status
= smb2srv_session_shutdown_recv(subreq
);
425 if (!NT_STATUS_IS_OK(status
)) {
426 DEBUG(0, ("smbXsrv_session_close_loop: "
427 "smb2srv_session_shutdown_recv(%llu) failed: %s\n",
428 (unsigned long long)session
->global
->session_wire_id
,
432 status
= smbXsrv_session_logoff(session
);
433 if (!NT_STATUS_IS_OK(status
)) {
434 DEBUG(0, ("smbXsrv_session_close_loop: "
435 "smbXsrv_session_logoff(%llu) failed: %s\n",
436 (unsigned long long)session
->global
->session_wire_id
,
440 TALLOC_FREE(session
);
443 struct smb1srv_session_local_allocate_state
{
444 const uint32_t lowest_id
;
445 const uint32_t highest_id
;
451 static int smb1srv_session_local_allocate_traverse(struct db_record
*rec
,
454 struct smb1srv_session_local_allocate_state
*state
=
455 (struct smb1srv_session_local_allocate_state
*)private_data
;
456 TDB_DATA key
= dbwrap_record_get_key(rec
);
460 status
= smbXsrv_session_local_key_to_id(key
, &id
);
461 if (!NT_STATUS_IS_OK(status
)) {
462 state
->status
= status
;
466 if (id
<= state
->last_id
) {
467 state
->status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
472 if (id
> state
->useable_id
) {
473 state
->status
= NT_STATUS_OK
;
477 if (state
->useable_id
== state
->highest_id
) {
478 state
->status
= NT_STATUS_INSUFFICIENT_RESOURCES
;
482 state
->useable_id
+=1;
486 static NTSTATUS
smb1srv_session_local_allocate_id(struct db_context
*db
,
490 struct db_record
**_rec
,
493 struct smb1srv_session_local_allocate_state state
= {
494 .lowest_id
= lowest_id
,
495 .highest_id
= highest_id
,
497 .useable_id
= lowest_id
,
498 .status
= NT_STATUS_INTERNAL_ERROR
,
508 if (lowest_id
> highest_id
) {
509 return NT_STATUS_INSUFFICIENT_RESOURCES
;
513 * first we try randomly
515 range
= (highest_id
- lowest_id
) + 1;
517 for (i
= 0; i
< (range
/ 2); i
++) {
520 struct db_record
*rec
= NULL
;
522 id
= generate_random() % range
;
525 if (id
< lowest_id
) {
528 if (id
> highest_id
) {
532 rec
= smbXsrv_session_local_fetch_locked(db
, id
, mem_ctx
);
534 return NT_STATUS_INSUFFICIENT_RESOURCES
;
537 val
= dbwrap_record_get_value(rec
);
538 if (val
.dsize
!= 0) {
549 * if the range is almost full,
550 * we traverse the whole table
551 * (this relies on sorted behavior of dbwrap_rbt)
553 status
= dbwrap_traverse_read(db
, smb1srv_session_local_allocate_traverse
,
555 if (NT_STATUS_IS_OK(status
)) {
556 if (NT_STATUS_IS_OK(state
.status
)) {
557 return NT_STATUS_INTERNAL_ERROR
;
560 if (!NT_STATUS_EQUAL(state
.status
, NT_STATUS_INTERNAL_ERROR
)) {
564 if (state
.useable_id
<= state
.highest_id
) {
565 state
.status
= NT_STATUS_OK
;
567 return NT_STATUS_INSUFFICIENT_RESOURCES
;
569 } else if (!NT_STATUS_EQUAL(status
, NT_STATUS_INTERNAL_DB_CORRUPTION
)) {
571 * Here we really expect NT_STATUS_INTERNAL_DB_CORRUPTION!
573 * If we get anything else it is an error, because it
574 * means we did not manage to find a free slot in
577 return NT_STATUS_INSUFFICIENT_RESOURCES
;
580 if (NT_STATUS_IS_OK(state
.status
)) {
583 struct db_record
*rec
= NULL
;
585 id
= state
.useable_id
;
587 rec
= smbXsrv_session_local_fetch_locked(db
, id
, mem_ctx
);
589 return NT_STATUS_INSUFFICIENT_RESOURCES
;
592 val
= dbwrap_record_get_value(rec
);
593 if (val
.dsize
!= 0) {
595 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
606 struct smbXsrv_session_local_fetch_state
{
607 struct smbXsrv_session
*session
;
611 static void smbXsrv_session_local_fetch_parser(TDB_DATA key
, TDB_DATA data
,
614 struct smbXsrv_session_local_fetch_state
*state
=
615 (struct smbXsrv_session_local_fetch_state
*)private_data
;
618 if (data
.dsize
!= sizeof(ptr
)) {
619 state
->status
= NT_STATUS_INTERNAL_DB_ERROR
;
623 memcpy(&ptr
, data
.dptr
, data
.dsize
);
624 state
->session
= talloc_get_type_abort(ptr
, struct smbXsrv_session
);
625 state
->status
= NT_STATUS_OK
;
628 static NTSTATUS
smbXsrv_session_local_lookup(struct smbXsrv_session_table
*table
,
630 struct smbXsrv_connection
*conn
,
631 uint32_t session_local_id
,
633 struct smbXsrv_session
**_session
)
635 struct smbXsrv_session_local_fetch_state state
= {
637 .status
= NT_STATUS_INTERNAL_ERROR
,
639 uint8_t key_buf
[SMBXSRV_SESSION_LOCAL_TDB_KEY_SIZE
];
645 if (session_local_id
== 0) {
646 return NT_STATUS_USER_SESSION_DELETED
;
650 /* this might happen before the end of negprot */
651 return NT_STATUS_USER_SESSION_DELETED
;
654 if (table
->local
.db_ctx
== NULL
) {
655 return NT_STATUS_INTERNAL_ERROR
;
658 key
= smbXsrv_session_local_id_to_key(session_local_id
, key_buf
);
660 status
= dbwrap_parse_record(table
->local
.db_ctx
, key
,
661 smbXsrv_session_local_fetch_parser
,
663 if (NT_STATUS_EQUAL(status
, NT_STATUS_NOT_FOUND
)) {
664 return NT_STATUS_USER_SESSION_DELETED
;
665 } else if (!NT_STATUS_IS_OK(status
)) {
668 if (!NT_STATUS_IS_OK(state
.status
)) {
672 if (NT_STATUS_EQUAL(state
.session
->status
, NT_STATUS_USER_SESSION_DELETED
)) {
673 return NT_STATUS_USER_SESSION_DELETED
;
677 * If a connection is specified check if the session is
678 * valid on the channel.
681 struct smbXsrv_channel_global0
*c
= NULL
;
683 status
= smbXsrv_session_find_channel(state
.session
, conn
, &c
);
684 if (!NT_STATUS_IS_OK(status
)) {
689 state
.session
->idle_time
= now
;
691 if (!NT_STATUS_IS_OK(state
.session
->status
)) {
692 *_session
= state
.session
;
693 return state
.session
->status
;
696 if (now
> state
.session
->global
->expiration_time
) {
697 state
.session
->status
= NT_STATUS_NETWORK_SESSION_EXPIRED
;
700 *_session
= state
.session
;
701 return state
.session
->status
;
704 static int smbXsrv_session_global_destructor(struct smbXsrv_session_global0
*global
)
709 static void smbXsrv_session_global_verify_record(struct db_record
*db_rec
,
713 struct smbXsrv_session_global0
**_g
,
716 static NTSTATUS
smbXsrv_session_global_allocate(struct db_context
*db
,
718 struct smbXsrv_session_global0
**_global
)
721 struct smbXsrv_session_global0
*global
= NULL
;
722 uint32_t last_free
= 0;
723 const uint32_t min_tries
= 3;
727 global
= talloc_zero(mem_ctx
, struct smbXsrv_session_global0
);
728 if (global
== NULL
) {
729 return NT_STATUS_NO_MEMORY
;
731 talloc_set_destructor(global
, smbXsrv_session_global_destructor
);
734 * Here we just randomly try the whole 32-bit space
736 * We use just 32-bit, because we want to reuse the
739 for (i
= 0; i
< UINT32_MAX
; i
++) {
740 bool is_free
= false;
741 bool was_free
= false;
744 if (i
>= min_tries
&& last_free
!= 0) {
747 id
= generate_random();
752 if (id
== UINT32_MAX
) {
756 global
->db_rec
= smbXsrv_session_global_fetch_locked(db
, id
,
758 if (global
->db_rec
== NULL
) {
760 return NT_STATUS_INSUFFICIENT_RESOURCES
;
763 smbXsrv_session_global_verify_record(global
->db_rec
,
769 TALLOC_FREE(global
->db_rec
);
773 if (!was_free
&& i
< min_tries
) {
775 * The session_id is free now,
776 * but was not free before.
778 * This happens if a smbd crashed
779 * and did not cleanup the record.
781 * If this is one of our first tries,
782 * then we try to find a real free one.
784 if (last_free
== 0) {
787 TALLOC_FREE(global
->db_rec
);
791 global
->session_global_id
= id
;
797 /* should not be reached */
799 return NT_STATUS_INTERNAL_ERROR
;
802 static void smbXsrv_session_global_verify_record(struct db_record
*db_rec
,
806 struct smbXsrv_session_global0
**_g
,
812 struct smbXsrv_session_globalB global_blob
;
813 enum ndr_err_code ndr_err
;
814 struct smbXsrv_session_global0
*global
= NULL
;
816 TALLOC_CTX
*frame
= talloc_stackframe();
830 key
= dbwrap_record_get_key(db_rec
);
832 val
= dbwrap_record_get_value(db_rec
);
833 if (val
.dsize
== 0) {
842 blob
= data_blob_const(val
.dptr
, val
.dsize
);
844 ndr_err
= ndr_pull_struct_blob(&blob
, frame
, &global_blob
,
845 (ndr_pull_flags_fn_t
)ndr_pull_smbXsrv_session_globalB
);
846 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
847 NTSTATUS status
= ndr_map_error2ntstatus(ndr_err
);
848 DEBUG(1,("smbXsrv_session_global_verify_record: "
849 "key '%s' ndr_pull_struct_blob - %s\n",
850 hex_encode_talloc(frame
, key
.dptr
, key
.dsize
),
860 DEBUG(10,("smbXsrv_session_global_verify_record\n"));
862 NDR_PRINT_DEBUG(smbXsrv_session_globalB
, &global_blob
);
865 if (global_blob
.version
!= SMBXSRV_VERSION_0
) {
866 DEBUG(0,("smbXsrv_session_global_verify_record: "
867 "key '%s' use unsupported version %u\n",
868 hex_encode_talloc(frame
, key
.dptr
, key
.dsize
),
869 global_blob
.version
));
870 NDR_PRINT_DEBUG(smbXsrv_session_globalB
, &global_blob
);
879 global
= global_blob
.info
.info0
;
881 #define __BLOB_KEEP_SECRET(__blob) do { \
882 if ((__blob).length != 0) { \
883 talloc_keep_secret((__blob).data); \
888 __BLOB_KEEP_SECRET(global
->application_key_blob
);
889 __BLOB_KEEP_SECRET(global
->signing_key_blob
);
890 __BLOB_KEEP_SECRET(global
->encryption_key_blob
);
891 __BLOB_KEEP_SECRET(global
->decryption_key_blob
);
892 for (i
= 0; i
< global
->num_channels
; i
++) {
893 __BLOB_KEEP_SECRET(global
->channels
[i
].signing_key_blob
);
896 #undef __BLOB_KEEP_SECRET
898 exists
= serverid_exists(&global
->channels
[0].server_id
);
900 struct server_id_buf idbuf
;
901 DEBUG(2,("smbXsrv_session_global_verify_record: "
902 "key '%s' server_id %s does not exist.\n",
903 hex_encode_talloc(frame
, key
.dptr
, key
.dsize
),
904 server_id_str_buf(global
->channels
[0].server_id
,
907 NDR_PRINT_DEBUG(smbXsrv_session_globalB
, &global_blob
);
910 dbwrap_record_delete(db_rec
);
916 *_g
= talloc_move(mem_ctx
, &global
);
919 *pseqnum
= global_blob
.seqnum
;
924 static NTSTATUS
smbXsrv_session_global_store(struct smbXsrv_session_global0
*global
)
926 struct smbXsrv_session_globalB global_blob
;
927 DATA_BLOB blob
= data_blob_null
;
931 enum ndr_err_code ndr_err
;
934 * TODO: if we use other versions than '0'
935 * we would add glue code here, that would be able to
936 * store the information in the old format.
939 if (global
->db_rec
== NULL
) {
940 return NT_STATUS_INTERNAL_ERROR
;
943 key
= dbwrap_record_get_key(global
->db_rec
);
944 val
= dbwrap_record_get_value(global
->db_rec
);
946 ZERO_STRUCT(global_blob
);
947 global_blob
.version
= smbXsrv_version_global_current();
948 if (val
.dsize
>= 8) {
949 global_blob
.seqnum
= IVAL(val
.dptr
, 4);
951 global_blob
.seqnum
+= 1;
952 global_blob
.info
.info0
= global
;
954 ndr_err
= ndr_push_struct_blob(&blob
, global
->db_rec
, &global_blob
,
955 (ndr_push_flags_fn_t
)ndr_push_smbXsrv_session_globalB
);
956 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
957 status
= ndr_map_error2ntstatus(ndr_err
);
958 DEBUG(1,("smbXsrv_session_global_store: key '%s' ndr_push - %s\n",
959 hex_encode_talloc(global
->db_rec
, key
.dptr
, key
.dsize
),
961 TALLOC_FREE(global
->db_rec
);
965 val
= make_tdb_data(blob
.data
, blob
.length
);
966 status
= dbwrap_record_store(global
->db_rec
, val
, TDB_REPLACE
);
967 if (!NT_STATUS_IS_OK(status
)) {
968 DEBUG(1,("smbXsrv_session_global_store: key '%s' store - %s\n",
969 hex_encode_talloc(global
->db_rec
, key
.dptr
, key
.dsize
),
971 TALLOC_FREE(global
->db_rec
);
976 DEBUG(10,("smbXsrv_session_global_store: key '%s' stored\n",
977 hex_encode_talloc(global
->db_rec
, key
.dptr
, key
.dsize
)));
978 NDR_PRINT_DEBUG(smbXsrv_session_globalB
, &global_blob
);
981 TALLOC_FREE(global
->db_rec
);
986 struct smb2srv_session_close_previous_state
{
987 struct tevent_context
*ev
;
988 struct smbXsrv_connection
*connection
;
989 struct dom_sid
*current_sid
;
990 uint64_t previous_session_id
;
991 uint64_t current_session_id
;
992 struct db_record
*db_rec
;
993 uint64_t watch_instance
;
994 uint32_t last_seqnum
;
997 static void smb2srv_session_close_previous_cleanup(struct tevent_req
*req
,
998 enum tevent_req_state req_state
)
1000 struct smb2srv_session_close_previous_state
*state
=
1001 tevent_req_data(req
,
1002 struct smb2srv_session_close_previous_state
);
1004 if (state
->db_rec
!= NULL
) {
1005 dbwrap_watched_watch_remove_instance(state
->db_rec
,
1006 state
->watch_instance
);
1007 state
->watch_instance
= 0;
1008 TALLOC_FREE(state
->db_rec
);
1012 static void smb2srv_session_close_previous_check(struct tevent_req
*req
);
1013 static void smb2srv_session_close_previous_modified(struct tevent_req
*subreq
);
1015 struct tevent_req
*smb2srv_session_close_previous_send(TALLOC_CTX
*mem_ctx
,
1016 struct tevent_context
*ev
,
1017 struct smbXsrv_connection
*conn
,
1018 struct auth_session_info
*session_info
,
1019 uint64_t previous_session_id
,
1020 uint64_t current_session_id
)
1022 struct tevent_req
*req
;
1023 struct smb2srv_session_close_previous_state
*state
;
1024 uint32_t global_id
= previous_session_id
& UINT32_MAX
;
1025 uint64_t global_zeros
= previous_session_id
& 0xFFFFFFFF00000000LLU
;
1026 struct smbXsrv_session_table
*table
= conn
->client
->session_table
;
1027 struct security_token
*current_token
= NULL
;
1029 req
= tevent_req_create(mem_ctx
, &state
,
1030 struct smb2srv_session_close_previous_state
);
1035 state
->connection
= conn
;
1036 state
->previous_session_id
= previous_session_id
;
1037 state
->current_session_id
= current_session_id
;
1039 tevent_req_set_cleanup_fn(req
, smb2srv_session_close_previous_cleanup
);
1041 if (global_zeros
!= 0) {
1042 tevent_req_done(req
);
1043 return tevent_req_post(req
, ev
);
1046 if (session_info
== NULL
) {
1047 tevent_req_done(req
);
1048 return tevent_req_post(req
, ev
);
1050 current_token
= session_info
->security_token
;
1052 if (current_token
->num_sids
> PRIMARY_USER_SID_INDEX
) {
1053 state
->current_sid
= ¤t_token
->sids
[PRIMARY_USER_SID_INDEX
];
1056 if (state
->current_sid
== NULL
) {
1057 tevent_req_done(req
);
1058 return tevent_req_post(req
, ev
);
1061 if (!security_token_has_nt_authenticated_users(current_token
)) {
1063 tevent_req_done(req
);
1064 return tevent_req_post(req
, ev
);
1067 state
->db_rec
= smbXsrv_session_global_fetch_locked(
1068 table
->global
.db_ctx
,
1070 state
/* TALLOC_CTX */);
1071 if (state
->db_rec
== NULL
) {
1072 tevent_req_nterror(req
, NT_STATUS_UNSUCCESSFUL
);
1073 return tevent_req_post(req
, ev
);
1076 smb2srv_session_close_previous_check(req
);
1077 if (!tevent_req_is_in_progress(req
)) {
1078 return tevent_req_post(req
, ev
);
1084 static void smb2srv_session_close_previous_check(struct tevent_req
*req
)
1086 struct smb2srv_session_close_previous_state
*state
=
1087 tevent_req_data(req
,
1088 struct smb2srv_session_close_previous_state
);
1089 struct smbXsrv_connection
*conn
= state
->connection
;
1091 struct security_token
*previous_token
= NULL
;
1092 struct smbXsrv_session_global0
*global
= NULL
;
1093 enum ndr_err_code ndr_err
;
1094 struct smbXsrv_session_close0 close_info0
;
1095 struct smbXsrv_session_closeB close_blob
;
1096 struct tevent_req
*subreq
= NULL
;
1098 bool is_free
= false;
1099 uint32_t seqnum
= 0;
1101 smbXsrv_session_global_verify_record(state
->db_rec
,
1109 tevent_req_done(req
);
1113 if (global
->auth_session_info
== NULL
) {
1114 tevent_req_done(req
);
1118 previous_token
= global
->auth_session_info
->security_token
;
1120 if (!security_token_is_sid(previous_token
, state
->current_sid
)) {
1121 tevent_req_done(req
);
1126 * If the record changed, but we are not happy with the change yet,
1127 * we better remove ourself from the waiter list
1128 * (most likely the first position)
1129 * and re-add us at the end of the list.
1131 * This gives other waiters a change
1134 * Otherwise we'll keep our waiter instance alive,
1135 * keep waiting (most likely at first position).
1136 * It means the order of watchers stays fair.
1138 if (state
->last_seqnum
!= seqnum
) {
1139 state
->last_seqnum
= seqnum
;
1140 dbwrap_watched_watch_remove_instance(state
->db_rec
,
1141 state
->watch_instance
);
1142 state
->watch_instance
=
1143 dbwrap_watched_watch_add_instance(state
->db_rec
);
1146 subreq
= dbwrap_watched_watch_send(state
, state
->ev
, state
->db_rec
,
1147 state
->watch_instance
,
1148 (struct server_id
){0});
1149 if (tevent_req_nomem(subreq
, req
)) {
1152 tevent_req_set_callback(subreq
,
1153 smb2srv_session_close_previous_modified
,
1156 close_info0
.old_session_global_id
= global
->session_global_id
;
1157 close_info0
.old_session_wire_id
= global
->session_wire_id
;
1158 close_info0
.old_creation_time
= global
->creation_time
;
1159 close_info0
.new_session_wire_id
= state
->current_session_id
;
1161 ZERO_STRUCT(close_blob
);
1162 close_blob
.version
= smbXsrv_version_global_current();
1163 close_blob
.info
.info0
= &close_info0
;
1165 ndr_err
= ndr_push_struct_blob(&blob
, state
, &close_blob
,
1166 (ndr_push_flags_fn_t
)ndr_push_smbXsrv_session_closeB
);
1167 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
1168 status
= ndr_map_error2ntstatus(ndr_err
);
1169 DEBUG(1,("smb2srv_session_close_previous_check: "
1170 "old_session[%llu] new_session[%llu] ndr_push - %s\n",
1171 (unsigned long long)close_info0
.old_session_wire_id
,
1172 (unsigned long long)close_info0
.new_session_wire_id
,
1173 nt_errstr(status
)));
1174 tevent_req_nterror(req
, status
);
1178 status
= messaging_send(conn
->client
->msg_ctx
,
1179 global
->channels
[0].server_id
,
1180 MSG_SMBXSRV_SESSION_CLOSE
, &blob
);
1181 TALLOC_FREE(global
);
1182 if (tevent_req_nterror(req
, status
)) {
1186 TALLOC_FREE(state
->db_rec
);
1190 static void smb2srv_session_close_previous_modified(struct tevent_req
*subreq
)
1192 struct tevent_req
*req
=
1193 tevent_req_callback_data(subreq
,
1195 struct smb2srv_session_close_previous_state
*state
=
1196 tevent_req_data(req
,
1197 struct smb2srv_session_close_previous_state
);
1200 uint64_t instance
= 0;
1202 status
= dbwrap_watched_watch_recv(subreq
, &instance
, NULL
, NULL
);
1203 TALLOC_FREE(subreq
);
1204 if (tevent_req_nterror(req
, status
)) {
1208 state
->watch_instance
= instance
;
1210 global_id
= state
->previous_session_id
& UINT32_MAX
;
1212 state
->db_rec
= smbXsrv_session_global_fetch_locked(
1213 state
->connection
->client
->session_table
->global
.db_ctx
,
1214 global_id
, state
/* TALLOC_CTX */);
1215 if (state
->db_rec
== NULL
) {
1216 tevent_req_nterror(req
, NT_STATUS_UNSUCCESSFUL
);
1220 smb2srv_session_close_previous_check(req
);
1223 NTSTATUS
smb2srv_session_close_previous_recv(struct tevent_req
*req
)
1227 if (tevent_req_is_nterror(req
, &status
)) {
1228 tevent_req_received(req
);
1232 tevent_req_received(req
);
1233 return NT_STATUS_OK
;
1236 static NTSTATUS
smbXsrv_session_clear_and_logoff(struct smbXsrv_session
*session
)
1239 struct smbXsrv_connection
*xconn
= NULL
;
1241 if (session
->client
!= NULL
) {
1242 xconn
= session
->client
->connections
;
1245 for (; xconn
!= NULL
; xconn
= xconn
->next
) {
1246 struct smbd_smb2_request
*preq
;
1248 for (preq
= xconn
->smb2
.requests
; preq
!= NULL
; preq
= preq
->next
) {
1249 if (preq
->session
!= session
) {
1253 preq
->session
= NULL
;
1255 * If we no longer have a session we can't
1256 * sign or encrypt replies.
1258 preq
->do_signing
= false;
1259 preq
->do_encryption
= false;
1260 preq
->preauth
= NULL
;
1264 status
= smbXsrv_session_logoff(session
);
1268 static int smbXsrv_session_destructor(struct smbXsrv_session
*session
)
1272 DBG_DEBUG("destructing session(%llu)\n",
1273 (unsigned long long)session
->global
->session_wire_id
);
1275 status
= smbXsrv_session_clear_and_logoff(session
);
1276 if (!NT_STATUS_IS_OK(status
)) {
1277 DEBUG(0, ("smbXsrv_session_destructor: "
1278 "smbXsrv_session_logoff() failed: %s\n",
1279 nt_errstr(status
)));
1282 TALLOC_FREE(session
->global
);
1287 NTSTATUS
smbXsrv_session_create(struct smbXsrv_connection
*conn
,
1289 struct smbXsrv_session
**_session
)
1291 struct smbXsrv_session_table
*table
= conn
->client
->session_table
;
1292 struct db_record
*local_rec
= NULL
;
1293 struct smbXsrv_session
*session
= NULL
;
1296 struct smbXsrv_session_global0
*global
= NULL
;
1297 struct smbXsrv_channel_global0
*channel
= NULL
;
1300 if (table
->local
.num_sessions
>= table
->local
.max_sessions
) {
1301 return NT_STATUS_INSUFFICIENT_RESOURCES
;
1304 session
= talloc_zero(table
, struct smbXsrv_session
);
1305 if (session
== NULL
) {
1306 return NT_STATUS_NO_MEMORY
;
1308 session
->table
= table
;
1309 session
->idle_time
= now
;
1310 session
->status
= NT_STATUS_MORE_PROCESSING_REQUIRED
;
1311 session
->client
= conn
->client
;
1312 session
->homes_snum
= -1;
1314 status
= smbXsrv_session_global_allocate(table
->global
.db_ctx
,
1317 if (!NT_STATUS_IS_OK(status
)) {
1318 TALLOC_FREE(session
);
1321 session
->global
= global
;
1323 if (conn
->protocol
>= PROTOCOL_SMB2_02
) {
1324 uint64_t id
= global
->session_global_id
;
1326 global
->connection_dialect
= conn
->smb2
.server
.dialect
;
1328 global
->session_wire_id
= id
;
1330 status
= smb2srv_tcon_table_init(session
);
1331 if (!NT_STATUS_IS_OK(status
)) {
1332 TALLOC_FREE(session
);
1336 session
->local_id
= global
->session_global_id
;
1338 local_rec
= smbXsrv_session_local_fetch_locked(
1339 table
->local
.db_ctx
,
1341 session
/* TALLOC_CTX */);
1342 if (local_rec
== NULL
) {
1343 TALLOC_FREE(session
);
1344 return NT_STATUS_NO_MEMORY
;
1347 val
= dbwrap_record_get_value(local_rec
);
1348 if (val
.dsize
!= 0) {
1349 TALLOC_FREE(session
);
1350 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
1354 status
= smb1srv_session_local_allocate_id(table
->local
.db_ctx
,
1355 table
->local
.lowest_id
,
1356 table
->local
.highest_id
,
1359 &session
->local_id
);
1360 if (!NT_STATUS_IS_OK(status
)) {
1361 TALLOC_FREE(session
);
1365 global
->session_wire_id
= session
->local_id
;
1368 global
->creation_time
= now
;
1369 global
->expiration_time
= GENSEC_EXPIRE_TIME_INFINITY
;
1371 status
= smbXsrv_session_add_channel(session
, conn
, now
, &channel
);
1372 if (!NT_STATUS_IS_OK(status
)) {
1373 TALLOC_FREE(session
);
1378 val
= make_tdb_data((uint8_t const *)&ptr
, sizeof(ptr
));
1379 status
= dbwrap_record_store(local_rec
, val
, TDB_REPLACE
);
1380 TALLOC_FREE(local_rec
);
1381 if (!NT_STATUS_IS_OK(status
)) {
1382 TALLOC_FREE(session
);
1385 table
->local
.num_sessions
+= 1;
1387 talloc_set_destructor(session
, smbXsrv_session_destructor
);
1389 status
= smbXsrv_session_global_store(global
);
1390 if (!NT_STATUS_IS_OK(status
)) {
1391 DEBUG(0,("smbXsrv_session_create: "
1392 "global_id (0x%08x) store failed - %s\n",
1393 session
->global
->session_global_id
,
1394 nt_errstr(status
)));
1395 TALLOC_FREE(session
);
1400 struct smbXsrv_sessionB session_blob
= {
1401 .version
= SMBXSRV_VERSION_0
,
1402 .info
.info0
= session
,
1405 DEBUG(10,("smbXsrv_session_create: global_id (0x%08x) stored\n",
1406 session
->global
->session_global_id
));
1407 NDR_PRINT_DEBUG(smbXsrv_sessionB
, &session_blob
);
1410 *_session
= session
;
1411 return NT_STATUS_OK
;
1414 NTSTATUS
smbXsrv_session_add_channel(struct smbXsrv_session
*session
,
1415 struct smbXsrv_connection
*conn
,
1417 struct smbXsrv_channel_global0
**_c
)
1419 struct smbXsrv_session_global0
*global
= session
->global
;
1420 struct smbXsrv_channel_global0
*c
= NULL
;
1422 if (global
->num_channels
> 31) {
1424 * Windows allow up to 32 channels
1426 return NT_STATUS_INSUFFICIENT_RESOURCES
;
1429 c
= talloc_realloc(global
,
1431 struct smbXsrv_channel_global0
,
1432 global
->num_channels
+ 1);
1434 return NT_STATUS_NO_MEMORY
;
1436 global
->channels
= c
;
1438 c
= &global
->channels
[global
->num_channels
];
1441 c
->server_id
= messaging_server_id(conn
->client
->msg_ctx
);
1442 c
->channel_id
= conn
->channel_id
;
1443 c
->creation_time
= now
;
1444 c
->local_address
= tsocket_address_string(conn
->local_address
,
1446 if (c
->local_address
== NULL
) {
1447 return NT_STATUS_NO_MEMORY
;
1449 c
->remote_address
= tsocket_address_string(conn
->remote_address
,
1451 if (c
->remote_address
== NULL
) {
1452 return NT_STATUS_NO_MEMORY
;
1454 c
->remote_name
= talloc_strdup(global
->channels
,
1455 conn
->remote_hostname
);
1456 if (c
->remote_name
== NULL
) {
1457 return NT_STATUS_NO_MEMORY
;
1459 c
->connection
= conn
;
1461 global
->num_channels
+= 1;
1464 return NT_STATUS_OK
;
1467 NTSTATUS
smbXsrv_session_update(struct smbXsrv_session
*session
)
1469 struct smbXsrv_session_table
*table
= session
->table
;
1472 if (session
->global
->db_rec
!= NULL
) {
1473 DEBUG(0, ("smbXsrv_session_update(0x%08x): "
1474 "Called with db_rec != NULL'\n",
1475 session
->global
->session_global_id
));
1476 return NT_STATUS_INTERNAL_ERROR
;
1479 if (table
== NULL
) {
1480 DEBUG(0, ("smbXsrv_session_update(0x%08x): "
1481 "Called with table == NULL'\n",
1482 session
->global
->session_global_id
));
1483 return NT_STATUS_INTERNAL_ERROR
;
1486 session
->global
->db_rec
= smbXsrv_session_global_fetch_locked(
1487 table
->global
.db_ctx
,
1488 session
->global
->session_global_id
,
1489 session
->global
/* TALLOC_CTX */);
1490 if (session
->global
->db_rec
== NULL
) {
1491 return NT_STATUS_INTERNAL_DB_ERROR
;
1494 status
= smbXsrv_session_global_store(session
->global
);
1495 if (!NT_STATUS_IS_OK(status
)) {
1496 DEBUG(0,("smbXsrv_session_update: "
1497 "global_id (0x%08x) store failed - %s\n",
1498 session
->global
->session_global_id
,
1499 nt_errstr(status
)));
1504 struct smbXsrv_sessionB session_blob
= {
1505 .version
= SMBXSRV_VERSION_0
,
1506 .info
.info0
= session
,
1509 DEBUG(10,("smbXsrv_session_update: global_id (0x%08x) stored\n",
1510 session
->global
->session_global_id
));
1511 NDR_PRINT_DEBUG(smbXsrv_sessionB
, &session_blob
);
1514 return NT_STATUS_OK
;
1517 NTSTATUS
smbXsrv_session_find_channel(const struct smbXsrv_session
*session
,
1518 const struct smbXsrv_connection
*conn
,
1519 struct smbXsrv_channel_global0
**_c
)
1523 for (i
=0; i
< session
->global
->num_channels
; i
++) {
1524 struct smbXsrv_channel_global0
*c
= &session
->global
->channels
[i
];
1526 if (c
->channel_id
!= conn
->channel_id
) {
1530 if (c
->connection
!= conn
) {
1535 return NT_STATUS_OK
;
1538 return NT_STATUS_USER_SESSION_DELETED
;
1541 NTSTATUS
smbXsrv_session_find_auth(const struct smbXsrv_session
*session
,
1542 const struct smbXsrv_connection
*conn
,
1544 struct smbXsrv_session_auth0
**_a
)
1546 struct smbXsrv_session_auth0
*a
;
1548 for (a
= session
->pending_auth
; a
!= NULL
; a
= a
->next
) {
1549 if (a
->channel_id
!= conn
->channel_id
) {
1553 if (a
->connection
== conn
) {
1558 return NT_STATUS_OK
;
1562 return NT_STATUS_USER_SESSION_DELETED
;
1565 static int smbXsrv_session_auth0_destructor(struct smbXsrv_session_auth0
*a
)
1567 if (a
->session
== NULL
) {
1571 DLIST_REMOVE(a
->session
->pending_auth
, a
);
1576 NTSTATUS
smbXsrv_session_create_auth(struct smbXsrv_session
*session
,
1577 struct smbXsrv_connection
*conn
,
1580 uint8_t in_security_mode
,
1581 struct smbXsrv_session_auth0
**_a
)
1583 struct smbXsrv_session_auth0
*a
;
1586 status
= smbXsrv_session_find_auth(session
, conn
, 0, &a
);
1587 if (NT_STATUS_IS_OK(status
)) {
1588 return NT_STATUS_INTERNAL_ERROR
;
1591 a
= talloc_zero(session
, struct smbXsrv_session_auth0
);
1593 return NT_STATUS_NO_MEMORY
;
1595 a
->session
= session
;
1596 a
->connection
= conn
;
1597 a
->in_flags
= in_flags
;
1598 a
->in_security_mode
= in_security_mode
;
1599 a
->creation_time
= now
;
1601 a
->channel_id
= conn
->channel_id
;
1603 if (conn
->protocol
>= PROTOCOL_SMB3_11
) {
1604 a
->preauth
= talloc(a
, struct smbXsrv_preauth
);
1605 if (a
->preauth
== NULL
) {
1606 TALLOC_FREE(session
);
1607 return NT_STATUS_NO_MEMORY
;
1609 *a
->preauth
= conn
->smb2
.preauth
;
1612 talloc_set_destructor(a
, smbXsrv_session_auth0_destructor
);
1613 DLIST_ADD_END(session
->pending_auth
, a
);
1616 return NT_STATUS_OK
;
1619 static void smbXsrv_session_remove_channel_done(struct tevent_req
*subreq
);
1621 NTSTATUS
smbXsrv_session_remove_channel(struct smbXsrv_session
*session
,
1622 struct smbXsrv_connection
*xconn
)
1624 struct smbXsrv_session_auth0
*a
= NULL
;
1625 struct smbXsrv_channel_global0
*c
= NULL
;
1627 bool need_update
= false;
1629 status
= smbXsrv_session_find_auth(session
, xconn
, 0, &a
);
1630 if (!NT_STATUS_IS_OK(status
)) {
1633 status
= smbXsrv_session_find_channel(session
, xconn
, &c
);
1634 if (!NT_STATUS_IS_OK(status
)) {
1639 smbXsrv_session_auth0_destructor(a
);
1640 a
->connection
= NULL
;
1645 struct smbXsrv_session_global0
*global
= session
->global
;
1648 n
= (c
- global
->channels
);
1649 if (n
>= global
->num_channels
|| n
< 0) {
1650 return NT_STATUS_INTERNAL_ERROR
;
1652 ARRAY_DEL_ELEMENT(global
->channels
, n
, global
->num_channels
);
1653 global
->num_channels
--;
1654 if (global
->num_channels
== 0) {
1655 struct smbXsrv_client
*client
= session
->client
;
1656 struct tevent_queue
*xconn_wait_queue
=
1657 xconn
->transport
.shutdown_wait_queue
;
1658 struct tevent_req
*subreq
= NULL
;
1661 * Let the connection wait until the session is
1664 * We don't set a callback, as we just want to block the
1665 * wait queue and the talloc_free() of the session will
1666 * remove the item from the wait queue in order
1667 * to remove allow the connection to disapear.
1669 if (xconn_wait_queue
!= NULL
) {
1670 subreq
= tevent_queue_wait_send(session
,
1673 if (subreq
== NULL
) {
1674 status
= NT_STATUS_NO_MEMORY
;
1675 DBG_ERR("tevent_queue_wait_send() session(%llu) failed: %s\n",
1676 (unsigned long long)session
->global
->session_wire_id
,
1683 * This is garanteed to set
1684 * session->status = NT_STATUS_USER_SESSION_DELETED
1685 * even if NULL is returned.
1687 subreq
= smb2srv_session_shutdown_send(session
,
1691 if (subreq
== NULL
) {
1692 status
= NT_STATUS_NO_MEMORY
;
1693 DBG_ERR("smb2srv_session_shutdown_send(%llu) failed: %s\n",
1694 (unsigned long long)session
->global
->session_wire_id
,
1698 tevent_req_set_callback(subreq
,
1699 smbXsrv_session_remove_channel_done
,
1706 return NT_STATUS_OK
;
1709 return smbXsrv_session_update(session
);
1712 static void smbXsrv_session_remove_channel_done(struct tevent_req
*subreq
)
1714 struct smbXsrv_session
*session
=
1715 tevent_req_callback_data(subreq
,
1716 struct smbXsrv_session
);
1719 status
= smb2srv_session_shutdown_recv(subreq
);
1720 TALLOC_FREE(subreq
);
1721 if (!NT_STATUS_IS_OK(status
)) {
1722 DBG_ERR("smb2srv_session_shutdown_recv(%llu) failed: %s\n",
1723 (unsigned long long)session
->global
->session_wire_id
,
1727 status
= smbXsrv_session_logoff(session
);
1728 if (!NT_STATUS_IS_OK(status
)) {
1729 DBG_ERR("smbXsrv_session_logoff(%llu) failed: %s\n",
1730 (unsigned long long)session
->global
->session_wire_id
,
1734 TALLOC_FREE(session
);
1737 struct smb2srv_session_shutdown_state
{
1738 struct tevent_queue
*wait_queue
;
1741 static void smb2srv_session_shutdown_wait_done(struct tevent_req
*subreq
);
1743 struct tevent_req
*smb2srv_session_shutdown_send(TALLOC_CTX
*mem_ctx
,
1744 struct tevent_context
*ev
,
1745 struct smbXsrv_session
*session
,
1746 struct smbd_smb2_request
*current_req
)
1748 struct tevent_req
*req
;
1749 struct smb2srv_session_shutdown_state
*state
;
1750 struct tevent_req
*subreq
;
1751 struct smbXsrv_connection
*xconn
= NULL
;
1755 * Make sure that no new request will be able to use this session.
1757 session
->status
= NT_STATUS_USER_SESSION_DELETED
;
1759 req
= tevent_req_create(mem_ctx
, &state
,
1760 struct smb2srv_session_shutdown_state
);
1765 state
->wait_queue
= tevent_queue_create(state
, "smb2srv_session_shutdown_queue");
1766 if (tevent_req_nomem(state
->wait_queue
, req
)) {
1767 return tevent_req_post(req
, ev
);
1770 for (xconn
= session
->client
->connections
; xconn
!= NULL
; xconn
= xconn
->next
) {
1771 struct smbd_smb2_request
*preq
;
1773 for (preq
= xconn
->smb2
.requests
; preq
!= NULL
; preq
= preq
->next
) {
1774 if (preq
== current_req
) {
1775 /* Can't cancel current request. */
1778 if (preq
->session
!= session
) {
1779 /* Request on different session. */
1783 if (preq
->subreq
!= NULL
) {
1784 tevent_req_cancel(preq
->subreq
);
1788 * Now wait until the request is finished.
1790 * We don't set a callback, as we just want to block the
1791 * wait queue and the talloc_free() of the request will
1792 * remove the item from the wait queue.
1794 subreq
= tevent_queue_wait_send(preq
, ev
, state
->wait_queue
);
1795 if (tevent_req_nomem(subreq
, req
)) {
1796 return tevent_req_post(req
, ev
);
1801 len
= tevent_queue_length(state
->wait_queue
);
1803 tevent_req_done(req
);
1804 return tevent_req_post(req
, ev
);
1808 * Now we add our own waiter to the end of the queue,
1809 * this way we get notified when all pending requests are finished
1810 * and send to the socket.
1812 subreq
= tevent_queue_wait_send(state
, ev
, state
->wait_queue
);
1813 if (tevent_req_nomem(subreq
, req
)) {
1814 return tevent_req_post(req
, ev
);
1816 tevent_req_set_callback(subreq
, smb2srv_session_shutdown_wait_done
, req
);
1821 static void smb2srv_session_shutdown_wait_done(struct tevent_req
*subreq
)
1823 struct tevent_req
*req
=
1824 tevent_req_callback_data(subreq
,
1827 tevent_queue_wait_recv(subreq
);
1828 TALLOC_FREE(subreq
);
1830 tevent_req_done(req
);
1833 NTSTATUS
smb2srv_session_shutdown_recv(struct tevent_req
*req
)
1835 return tevent_req_simple_recv_ntstatus(req
);
1838 NTSTATUS
smbXsrv_session_logoff(struct smbXsrv_session
*session
)
1840 struct smbXsrv_session_table
*table
;
1841 struct db_record
*local_rec
= NULL
;
1842 struct db_record
*global_rec
= NULL
;
1843 struct smbd_server_connection
*sconn
= NULL
;
1845 NTSTATUS error
= NT_STATUS_OK
;
1847 if (session
->table
== NULL
) {
1848 return NT_STATUS_OK
;
1851 table
= session
->table
;
1852 session
->table
= NULL
;
1854 sconn
= session
->client
->sconn
;
1855 session
->client
= NULL
;
1856 session
->status
= NT_STATUS_USER_SESSION_DELETED
;
1859 * For SMB2 this is a bit redundant as files are also close
1860 * below via smb2srv_tcon_disconnect_all() -> ... ->
1861 * smbXsrv_tcon_disconnect() -> close_cnum() ->
1862 * file_close_conn().
1864 file_close_user(sconn
, session
->global
->session_wire_id
);
1866 if (session
->tcon_table
!= NULL
) {
1868 * Note: We only have a tcon_table for SMB2.
1870 status
= smb2srv_tcon_disconnect_all(session
);
1871 if (!NT_STATUS_IS_OK(status
)) {
1872 DEBUG(0, ("smbXsrv_session_logoff(0x%08x): "
1873 "smb2srv_tcon_disconnect_all() failed: %s\n",
1874 session
->global
->session_global_id
,
1875 nt_errstr(status
)));
1880 invalidate_vuid(sconn
, session
->global
->session_wire_id
);
1882 global_rec
= session
->global
->db_rec
;
1883 session
->global
->db_rec
= NULL
;
1884 if (global_rec
== NULL
) {
1885 global_rec
= smbXsrv_session_global_fetch_locked(
1886 table
->global
.db_ctx
,
1887 session
->global
->session_global_id
,
1888 session
->global
/* TALLOC_CTX */);
1889 if (global_rec
== NULL
) {
1890 error
= NT_STATUS_INTERNAL_ERROR
;
1894 if (global_rec
!= NULL
) {
1895 status
= dbwrap_record_delete(global_rec
);
1896 if (!NT_STATUS_IS_OK(status
)) {
1897 TDB_DATA key
= dbwrap_record_get_key(global_rec
);
1899 DEBUG(0, ("smbXsrv_session_logoff(0x%08x): "
1900 "failed to delete global key '%s': %s\n",
1901 session
->global
->session_global_id
,
1902 hex_encode_talloc(global_rec
, key
.dptr
,
1904 nt_errstr(status
)));
1908 TALLOC_FREE(global_rec
);
1910 local_rec
= session
->db_rec
;
1911 if (local_rec
== NULL
) {
1912 local_rec
= smbXsrv_session_local_fetch_locked(
1913 table
->local
.db_ctx
,
1915 session
/* TALLOC_CTX */);
1916 if (local_rec
== NULL
) {
1917 error
= NT_STATUS_INTERNAL_ERROR
;
1921 if (local_rec
!= NULL
) {
1922 status
= dbwrap_record_delete(local_rec
);
1923 if (!NT_STATUS_IS_OK(status
)) {
1924 TDB_DATA key
= dbwrap_record_get_key(local_rec
);
1926 DEBUG(0, ("smbXsrv_session_logoff(0x%08x): "
1927 "failed to delete local key '%s': %s\n",
1928 session
->global
->session_global_id
,
1929 hex_encode_talloc(local_rec
, key
.dptr
,
1931 nt_errstr(status
)));
1934 table
->local
.num_sessions
-= 1;
1936 if (session
->db_rec
== NULL
) {
1937 TALLOC_FREE(local_rec
);
1939 session
->db_rec
= NULL
;
1944 struct smbXsrv_session_logoff_all_state
{
1945 NTSTATUS first_status
;
1949 static int smbXsrv_session_logoff_all_callback(struct db_record
*local_rec
,
1950 void *private_data
);
1952 NTSTATUS
smbXsrv_session_logoff_all(struct smbXsrv_client
*client
)
1954 struct smbXsrv_session_table
*table
= client
->session_table
;
1955 struct smbXsrv_session_logoff_all_state state
;
1959 if (table
== NULL
) {
1960 DEBUG(10, ("smbXsrv_session_logoff_all: "
1961 "empty session_table, nothing to do.\n"));
1962 return NT_STATUS_OK
;
1967 status
= dbwrap_traverse(table
->local
.db_ctx
,
1968 smbXsrv_session_logoff_all_callback
,
1970 if (!NT_STATUS_IS_OK(status
)) {
1971 DEBUG(0, ("smbXsrv_session_logoff_all: "
1972 "dbwrap_traverse() failed: %s\n",
1973 nt_errstr(status
)));
1977 if (!NT_STATUS_IS_OK(state
.first_status
)) {
1978 DEBUG(0, ("smbXsrv_session_logoff_all: "
1979 "count[%d] errors[%d] first[%s]\n",
1980 count
, state
.errors
,
1981 nt_errstr(state
.first_status
)));
1982 return state
.first_status
;
1985 return NT_STATUS_OK
;
1988 static int smbXsrv_session_logoff_all_callback(struct db_record
*local_rec
,
1991 struct smbXsrv_session_logoff_all_state
*state
=
1992 (struct smbXsrv_session_logoff_all_state
*)private_data
;
1995 struct smbXsrv_session
*session
= NULL
;
1998 val
= dbwrap_record_get_value(local_rec
);
1999 if (val
.dsize
!= sizeof(ptr
)) {
2000 status
= NT_STATUS_INTERNAL_ERROR
;
2001 if (NT_STATUS_IS_OK(state
->first_status
)) {
2002 state
->first_status
= status
;
2008 memcpy(&ptr
, val
.dptr
, val
.dsize
);
2009 session
= talloc_get_type_abort(ptr
, struct smbXsrv_session
);
2011 session
->db_rec
= local_rec
;
2012 status
= smbXsrv_session_clear_and_logoff(session
);
2013 session
->db_rec
= NULL
;
2014 if (!NT_STATUS_IS_OK(status
)) {
2015 if (NT_STATUS_IS_OK(state
->first_status
)) {
2016 state
->first_status
= status
;
2025 struct smbXsrv_session_local_trav_state
{
2027 int (*caller_cb
)(struct smbXsrv_session
*session
,
2032 static int smbXsrv_session_local_traverse_cb(struct db_record
*local_rec
,
2033 void *private_data
);
2035 NTSTATUS
smbXsrv_session_local_traverse(
2036 struct smbXsrv_client
*client
,
2037 int (*caller_cb
)(struct smbXsrv_session
*session
,
2041 struct smbXsrv_session_table
*table
= client
->session_table
;
2042 struct smbXsrv_session_local_trav_state state
;
2046 state
= (struct smbXsrv_session_local_trav_state
) {
2047 .status
= NT_STATUS_OK
,
2048 .caller_cb
= caller_cb
,
2049 .caller_data
= caller_data
,
2052 if (table
== NULL
) {
2053 DBG_DEBUG("empty session_table, nothing to do.\n");
2054 return NT_STATUS_OK
;
2057 status
= dbwrap_traverse(table
->local
.db_ctx
,
2058 smbXsrv_session_local_traverse_cb
,
2061 if (!NT_STATUS_IS_OK(status
)) {
2062 DBG_ERR("dbwrap_traverse() failed: %s\n", nt_errstr(status
));
2065 if (!NT_STATUS_IS_OK(state
.status
)) {
2066 DBG_ERR("count[%d] status[%s]\n",
2067 count
, nt_errstr(state
.status
));
2068 return state
.status
;
2071 return NT_STATUS_OK
;
2074 static int smbXsrv_session_local_traverse_cb(struct db_record
*local_rec
,
2077 struct smbXsrv_session_local_trav_state
*state
=
2078 (struct smbXsrv_session_local_trav_state
*)private_data
;
2081 struct smbXsrv_session
*session
= NULL
;
2084 val
= dbwrap_record_get_value(local_rec
);
2085 if (val
.dsize
!= sizeof(ptr
)) {
2086 state
->status
= NT_STATUS_INTERNAL_ERROR
;
2090 memcpy(&ptr
, val
.dptr
, val
.dsize
);
2091 session
= talloc_get_type_abort(ptr
, struct smbXsrv_session
);
2093 session
->db_rec
= local_rec
;
2094 ret
= state
->caller_cb(session
, state
->caller_data
);
2095 session
->db_rec
= NULL
;
2100 struct smbXsrv_session_disconnect_xconn_state
{
2101 struct smbXsrv_connection
*xconn
;
2102 NTSTATUS first_status
;
2106 static int smbXsrv_session_disconnect_xconn_callback(struct db_record
*local_rec
,
2107 void *private_data
);
2109 NTSTATUS
smbXsrv_session_disconnect_xconn(struct smbXsrv_connection
*xconn
)
2111 struct smbXsrv_client
*client
= xconn
->client
;
2112 struct smbXsrv_session_table
*table
= client
->session_table
;
2113 struct smbXsrv_session_disconnect_xconn_state state
;
2117 if (table
== NULL
) {
2118 DBG_ERR("empty session_table, nothing to do.\n");
2119 return NT_STATUS_OK
;
2123 state
.xconn
= xconn
;
2125 status
= dbwrap_traverse(table
->local
.db_ctx
,
2126 smbXsrv_session_disconnect_xconn_callback
,
2128 if (!NT_STATUS_IS_OK(status
)) {
2129 DBG_ERR("dbwrap_traverse() failed: %s\n",
2134 if (!NT_STATUS_IS_OK(state
.first_status
)) {
2135 DBG_ERR("count[%d] errors[%d] first[%s]\n",
2136 count
, state
.errors
,
2137 nt_errstr(state
.first_status
));
2138 return state
.first_status
;
2141 return NT_STATUS_OK
;
2144 static int smbXsrv_session_disconnect_xconn_callback(struct db_record
*local_rec
,
2147 struct smbXsrv_session_disconnect_xconn_state
*state
=
2148 (struct smbXsrv_session_disconnect_xconn_state
*)private_data
;
2151 struct smbXsrv_session
*session
= NULL
;
2154 val
= dbwrap_record_get_value(local_rec
);
2155 if (val
.dsize
!= sizeof(ptr
)) {
2156 status
= NT_STATUS_INTERNAL_ERROR
;
2157 if (NT_STATUS_IS_OK(state
->first_status
)) {
2158 state
->first_status
= status
;
2164 memcpy(&ptr
, val
.dptr
, val
.dsize
);
2165 session
= talloc_get_type_abort(ptr
, struct smbXsrv_session
);
2167 session
->db_rec
= local_rec
;
2168 status
= smbXsrv_session_remove_channel(session
, state
->xconn
);
2169 session
->db_rec
= NULL
;
2170 if (!NT_STATUS_IS_OK(status
)) {
2171 if (NT_STATUS_IS_OK(state
->first_status
)) {
2172 state
->first_status
= status
;
2180 NTSTATUS
smb1srv_session_table_init(struct smbXsrv_connection
*conn
)
2183 * Allow a range from 1..65534 with 65534 values.
2185 return smbXsrv_session_table_init(conn
, 1, UINT16_MAX
- 1,
2189 NTSTATUS
smb1srv_session_lookup(struct smbXsrv_connection
*conn
,
2190 uint16_t vuid
, NTTIME now
,
2191 struct smbXsrv_session
**session
)
2193 struct smbXsrv_session_table
*table
= conn
->client
->session_table
;
2194 uint32_t local_id
= vuid
;
2196 return smbXsrv_session_local_lookup(table
, conn
, local_id
, now
,
2200 NTSTATUS
smbXsrv_session_info_lookup(struct smbXsrv_client
*client
,
2201 uint64_t session_wire_id
,
2202 struct auth_session_info
**si
)
2204 struct smbXsrv_session_table
*table
= client
->session_table
;
2205 uint8_t key_buf
[SMBXSRV_SESSION_LOCAL_TDB_KEY_SIZE
];
2206 struct smbXsrv_session_local_fetch_state state
= {
2208 .status
= NT_STATUS_INTERNAL_ERROR
,
2213 if (session_wire_id
== 0) {
2214 return NT_STATUS_USER_SESSION_DELETED
;
2217 if (table
== NULL
) {
2218 /* this might happen before the end of negprot */
2219 return NT_STATUS_USER_SESSION_DELETED
;
2222 if (table
->local
.db_ctx
== NULL
) {
2223 return NT_STATUS_INTERNAL_ERROR
;
2226 key
= smbXsrv_session_local_id_to_key(session_wire_id
, key_buf
);
2228 status
= dbwrap_parse_record(table
->local
.db_ctx
, key
,
2229 smbXsrv_session_local_fetch_parser
,
2231 if (!NT_STATUS_IS_OK(status
)) {
2234 if (!NT_STATUS_IS_OK(state
.status
)) {
2235 return state
.status
;
2237 if (state
.session
->global
->auth_session_info
== NULL
) {
2238 return NT_STATUS_USER_SESSION_DELETED
;
2241 *si
= state
.session
->global
->auth_session_info
;
2242 return NT_STATUS_OK
;
2246 * In memory of get_valid_user_struct()
2248 * This function is similar to smbXsrv_session_local_lookup() and it's wrappers,
2249 * but it doesn't implement the state checks of
2250 * those. get_valid_smbXsrv_session() is NOT meant to be called to validate the
2251 * session wire-id of incoming SMB requests, it MUST only be used in later
2252 * internal processing where the session wire-id has already been validated.
2254 NTSTATUS
get_valid_smbXsrv_session(struct smbXsrv_client
*client
,
2255 uint64_t session_wire_id
,
2256 struct smbXsrv_session
**session
)
2258 struct smbXsrv_session_table
*table
= client
->session_table
;
2259 uint8_t key_buf
[SMBXSRV_SESSION_LOCAL_TDB_KEY_SIZE
];
2260 struct smbXsrv_session_local_fetch_state state
= {
2262 .status
= NT_STATUS_INTERNAL_ERROR
,
2267 if (session_wire_id
== 0) {
2268 return NT_STATUS_USER_SESSION_DELETED
;
2271 if (table
== NULL
) {
2272 /* this might happen before the end of negprot */
2273 return NT_STATUS_USER_SESSION_DELETED
;
2276 if (table
->local
.db_ctx
== NULL
) {
2277 return NT_STATUS_INTERNAL_ERROR
;
2280 key
= smbXsrv_session_local_id_to_key(session_wire_id
, key_buf
);
2282 status
= dbwrap_parse_record(table
->local
.db_ctx
, key
,
2283 smbXsrv_session_local_fetch_parser
,
2285 if (!NT_STATUS_IS_OK(status
)) {
2288 if (!NT_STATUS_IS_OK(state
.status
)) {
2289 return state
.status
;
2291 if (state
.session
->global
->auth_session_info
== NULL
) {
2292 return NT_STATUS_USER_SESSION_DELETED
;
2295 *session
= state
.session
;
2296 return NT_STATUS_OK
;
2299 NTSTATUS
smb2srv_session_lookup_global(struct smbXsrv_client
*client
,
2300 uint64_t session_wire_id
,
2301 TALLOC_CTX
*mem_ctx
,
2302 struct smbXsrv_session
**_session
)
2304 TALLOC_CTX
*frame
= talloc_stackframe();
2305 struct smbXsrv_session_table
*table
= client
->session_table
;
2306 uint32_t global_id
= session_wire_id
& UINT32_MAX
;
2307 uint64_t global_zeros
= session_wire_id
& 0xFFFFFFFF00000000LLU
;
2308 struct smbXsrv_session
*session
= NULL
;
2309 struct db_record
*global_rec
= NULL
;
2310 bool is_free
= false;
2313 if (global_id
== 0) {
2315 return NT_STATUS_USER_SESSION_DELETED
;
2317 if (global_zeros
!= 0) {
2319 return NT_STATUS_USER_SESSION_DELETED
;
2322 if (table
== NULL
) {
2323 /* this might happen before the end of negprot */
2325 return NT_STATUS_USER_SESSION_DELETED
;
2328 if (table
->global
.db_ctx
== NULL
) {
2330 return NT_STATUS_INTERNAL_ERROR
;
2333 session
= talloc_zero(mem_ctx
, struct smbXsrv_session
);
2334 if (session
== NULL
) {
2336 return NT_STATUS_NO_MEMORY
;
2338 talloc_steal(frame
, session
);
2340 session
->client
= client
;
2341 session
->status
= NT_STATUS_BAD_LOGON_SESSION_STATE
;
2342 session
->local_id
= global_id
;
2345 * This means smb2_get_new_nonce() will return
2346 * NT_STATUS_ENCRYPTION_FAILED.
2348 * But we intialize some random parts just in case...
2350 session
->nonce_high_max
= session
->nonce_high
= 0;
2351 generate_nonce_buffer((uint8_t *)&session
->nonce_high_random
,
2352 sizeof(session
->nonce_high_random
));
2353 generate_nonce_buffer((uint8_t *)&session
->nonce_low
,
2354 sizeof(session
->nonce_low
));
2356 global_rec
= smbXsrv_session_global_fetch_locked(table
->global
.db_ctx
,
2359 if (global_rec
== NULL
) {
2361 return NT_STATUS_INTERNAL_DB_ERROR
;
2364 smbXsrv_session_global_verify_record(global_rec
,
2372 return NT_STATUS_USER_SESSION_DELETED
;
2376 * We don't have channels on this session
2377 * and only the main signing key
2379 session
->global
->num_channels
= 0;
2380 status
= smb2_signing_key_sign_create(session
->global
,
2381 session
->global
->signing_algo
,
2382 NULL
, /* no master key */
2383 NULL
, /* derivations */
2384 &session
->global
->signing_key
);
2385 if (!NT_STATUS_IS_OK(status
)) {
2387 return NT_STATUS_NO_MEMORY
;
2389 session
->global
->signing_key
->blob
= session
->global
->signing_key_blob
;
2390 session
->global
->signing_flags
= 0;
2392 status
= smb2_signing_key_cipher_create(session
->global
,
2393 session
->global
->encryption_cipher
,
2394 NULL
, /* no master key */
2395 NULL
, /* derivations */
2396 &session
->global
->decryption_key
);
2397 if (!NT_STATUS_IS_OK(status
)) {
2399 return NT_STATUS_NO_MEMORY
;
2401 session
->global
->decryption_key
->blob
= session
->global
->decryption_key_blob
;
2402 session
->global
->encryption_flags
= 0;
2404 *_session
= talloc_move(mem_ctx
, &session
);
2406 return NT_STATUS_OK
;
2409 NTSTATUS
smb2srv_session_table_init(struct smbXsrv_connection
*conn
)
2412 * Allow a range from 1..4294967294 with 65534 (same as SMB1) values.
2414 return smbXsrv_session_table_init(conn
, 1, UINT32_MAX
- 1,
2418 static NTSTATUS
smb2srv_session_lookup_raw(struct smbXsrv_session_table
*table
,
2419 /* conn: optional */
2420 struct smbXsrv_connection
*conn
,
2421 uint64_t session_id
, NTTIME now
,
2422 struct smbXsrv_session
**session
)
2424 uint32_t local_id
= session_id
& UINT32_MAX
;
2425 uint64_t local_zeros
= session_id
& 0xFFFFFFFF00000000LLU
;
2427 if (local_zeros
!= 0) {
2428 return NT_STATUS_USER_SESSION_DELETED
;
2431 return smbXsrv_session_local_lookup(table
, conn
, local_id
, now
,
2435 NTSTATUS
smb2srv_session_lookup_conn(struct smbXsrv_connection
*conn
,
2436 uint64_t session_id
, NTTIME now
,
2437 struct smbXsrv_session
**session
)
2439 struct smbXsrv_session_table
*table
= conn
->client
->session_table
;
2440 return smb2srv_session_lookup_raw(table
, conn
, session_id
, now
,
2444 NTSTATUS
smb2srv_session_lookup_client(struct smbXsrv_client
*client
,
2445 uint64_t session_id
, NTTIME now
,
2446 struct smbXsrv_session
**session
)
2448 struct smbXsrv_session_table
*table
= client
->session_table
;
2449 return smb2srv_session_lookup_raw(table
, NULL
, session_id
, now
,
2453 struct smbXsrv_session_global_traverse_state
{
2454 int (*fn
)(struct smbXsrv_session_global0
*, void *);
2458 static int smbXsrv_session_global_traverse_fn(struct db_record
*rec
, void *data
)
2461 struct smbXsrv_session_global_traverse_state
*state
=
2462 (struct smbXsrv_session_global_traverse_state
*)data
;
2463 TDB_DATA key
= dbwrap_record_get_key(rec
);
2464 TDB_DATA val
= dbwrap_record_get_value(rec
);
2465 DATA_BLOB blob
= data_blob_const(val
.dptr
, val
.dsize
);
2466 struct smbXsrv_session_globalB global_blob
;
2467 enum ndr_err_code ndr_err
;
2468 TALLOC_CTX
*frame
= talloc_stackframe();
2470 ndr_err
= ndr_pull_struct_blob(&blob
, frame
, &global_blob
,
2471 (ndr_pull_flags_fn_t
)ndr_pull_smbXsrv_session_globalB
);
2472 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
2473 DEBUG(1,("Invalid record in smbXsrv_session_global.tdb:"
2474 "key '%s' ndr_pull_struct_blob - %s\n",
2475 hex_encode_talloc(frame
, key
.dptr
, key
.dsize
),
2476 ndr_errstr(ndr_err
)));
2480 if (global_blob
.version
!= SMBXSRV_VERSION_0
) {
2481 DEBUG(1,("Invalid record in smbXsrv_session_global.tdb:"
2482 "key '%s' unsupported version - %d\n",
2483 hex_encode_talloc(frame
, key
.dptr
, key
.dsize
),
2484 (int)global_blob
.version
));
2488 if (global_blob
.info
.info0
== NULL
) {
2489 DEBUG(1,("Invalid record in smbXsrv_tcon_global.tdb:"
2490 "key '%s' info0 NULL pointer\n",
2491 hex_encode_talloc(frame
, key
.dptr
, key
.dsize
)));
2495 global_blob
.info
.info0
->db_rec
= rec
;
2496 ret
= state
->fn(global_blob
.info
.info0
, state
->private_data
);
2502 NTSTATUS
smbXsrv_session_global_traverse(
2503 int (*fn
)(struct smbXsrv_session_global0
*, void *),
2509 struct smbXsrv_session_global_traverse_state state
= {
2511 .private_data
= private_data
,
2515 status
= smbXsrv_session_global_init(NULL
);
2516 if (!NT_STATUS_IS_OK(status
)) {
2518 DEBUG(0, ("Failed to initialize session_global: %s\n",
2519 nt_errstr(status
)));
2523 status
= dbwrap_traverse_read(smbXsrv_session_global_db_ctx
,
2524 smbXsrv_session_global_traverse_fn
,