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"
42 struct smbXsrv_session_table
{
44 struct db_context
*db_ctx
;
47 uint32_t max_sessions
;
48 uint32_t num_sessions
;
51 struct db_context
*db_ctx
;
55 static struct db_context
*smbXsrv_session_global_db_ctx
= NULL
;
57 NTSTATUS
smbXsrv_session_global_init(struct messaging_context
*msg_ctx
)
59 char *global_path
= NULL
;
60 struct db_context
*backend
= NULL
;
61 struct db_context
*db_ctx
= NULL
;
63 if (smbXsrv_session_global_db_ctx
!= NULL
) {
68 * This contains secret information like session keys!
70 global_path
= lock_path(talloc_tos(), "smbXsrv_session_global.tdb");
71 if (global_path
== NULL
) {
72 return NT_STATUS_NO_MEMORY
;
75 backend
= db_open(NULL
, global_path
,
79 TDB_INCOMPATIBLE_HASH
,
80 O_RDWR
| O_CREAT
, 0600,
83 TALLOC_FREE(global_path
);
84 if (backend
== NULL
) {
87 status
= map_nt_error_from_unix_common(errno
);
92 db_ctx
= db_open_watched(NULL
, &backend
, global_messaging_context());
95 return NT_STATUS_NO_MEMORY
;
98 smbXsrv_session_global_db_ctx
= db_ctx
;
105 * We need to store the keys in big endian so that dbwrap_rbt's memcmp
106 * has the same result as integer comparison between the uint32_t
109 * TODO: implement string based key
112 #define SMBXSRV_SESSION_GLOBAL_TDB_KEY_SIZE sizeof(uint32_t)
114 static TDB_DATA
smbXsrv_session_global_id_to_key(uint32_t id
,
119 RSIVAL(key_buf
, 0, id
);
121 key
= make_tdb_data(key_buf
, SMBXSRV_SESSION_GLOBAL_TDB_KEY_SIZE
);
127 static NTSTATUS
smbXsrv_session_global_key_to_id(TDB_DATA key
, uint32_t *id
)
130 return NT_STATUS_INVALID_PARAMETER
;
133 if (key
.dsize
!= SMBXSRV_SESSION_GLOBAL_TDB_KEY_SIZE
) {
134 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
137 *id
= RIVAL(key
.dptr
, 0);
143 #define SMBXSRV_SESSION_LOCAL_TDB_KEY_SIZE sizeof(uint32_t)
145 static TDB_DATA
smbXsrv_session_local_id_to_key(uint32_t id
,
150 RSIVAL(key_buf
, 0, id
);
152 key
= make_tdb_data(key_buf
, SMBXSRV_SESSION_LOCAL_TDB_KEY_SIZE
);
157 static NTSTATUS
smbXsrv_session_local_key_to_id(TDB_DATA key
, uint32_t *id
)
160 return NT_STATUS_INVALID_PARAMETER
;
163 if (key
.dsize
!= SMBXSRV_SESSION_LOCAL_TDB_KEY_SIZE
) {
164 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
167 *id
= RIVAL(key
.dptr
, 0);
172 static struct db_record
*smbXsrv_session_global_fetch_locked(
173 struct db_context
*db
,
178 uint8_t key_buf
[SMBXSRV_SESSION_GLOBAL_TDB_KEY_SIZE
];
179 struct db_record
*rec
= NULL
;
181 key
= smbXsrv_session_global_id_to_key(id
, key_buf
);
183 rec
= dbwrap_fetch_locked(db
, mem_ctx
, key
);
186 DBG_DEBUG("Failed to lock global id 0x%08x, key '%s'\n", id
,
187 hex_encode_talloc(talloc_tos(), key
.dptr
, key
.dsize
));
193 static struct db_record
*smbXsrv_session_local_fetch_locked(
194 struct db_context
*db
,
199 uint8_t key_buf
[SMBXSRV_SESSION_LOCAL_TDB_KEY_SIZE
];
200 struct db_record
*rec
= NULL
;
202 key
= smbXsrv_session_local_id_to_key(id
, key_buf
);
204 rec
= dbwrap_fetch_locked(db
, mem_ctx
, key
);
207 DBG_DEBUG("Failed to lock local id 0x%08x, key '%s'\n", id
,
208 hex_encode_talloc(talloc_tos(), key
.dptr
, key
.dsize
));
214 static void smbXsrv_session_close_loop(struct tevent_req
*subreq
);
216 static NTSTATUS
smbXsrv_session_table_init(struct smbXsrv_connection
*conn
,
219 uint32_t max_sessions
)
221 struct smbXsrv_client
*client
= conn
->client
;
222 struct smbXsrv_session_table
*table
;
224 struct tevent_req
*subreq
;
227 if (lowest_id
> highest_id
) {
228 return NT_STATUS_INTERNAL_ERROR
;
231 max_range
= highest_id
;
232 max_range
-= lowest_id
;
235 if (max_sessions
> max_range
) {
236 return NT_STATUS_INTERNAL_ERROR
;
239 table
= talloc_zero(client
, struct smbXsrv_session_table
);
241 return NT_STATUS_NO_MEMORY
;
244 table
->local
.db_ctx
= db_open_rbt(table
);
245 if (table
->local
.db_ctx
== NULL
) {
247 return NT_STATUS_NO_MEMORY
;
249 table
->local
.lowest_id
= lowest_id
;
250 table
->local
.highest_id
= highest_id
;
251 table
->local
.max_sessions
= max_sessions
;
253 status
= smbXsrv_session_global_init(client
->msg_ctx
);
254 if (!NT_STATUS_IS_OK(status
)) {
259 table
->global
.db_ctx
= smbXsrv_session_global_db_ctx
;
261 subreq
= messaging_read_send(table
,
264 MSG_SMBXSRV_SESSION_CLOSE
);
265 if (subreq
== NULL
) {
267 return NT_STATUS_NO_MEMORY
;
269 tevent_req_set_callback(subreq
, smbXsrv_session_close_loop
, client
);
271 client
->session_table
= table
;
275 static void smbXsrv_session_close_shutdown_done(struct tevent_req
*subreq
);
277 static void smbXsrv_session_close_loop(struct tevent_req
*subreq
)
279 struct smbXsrv_client
*client
=
280 tevent_req_callback_data(subreq
,
281 struct smbXsrv_client
);
282 struct smbXsrv_session_table
*table
= client
->session_table
;
284 struct messaging_rec
*rec
= NULL
;
285 struct smbXsrv_session_closeB close_blob
;
286 enum ndr_err_code ndr_err
;
287 struct smbXsrv_session_close0
*close_info0
= NULL
;
288 struct smbXsrv_session
*session
= NULL
;
290 struct timeval tv
= timeval_current();
291 NTTIME now
= timeval_to_nttime(&tv
);
293 ret
= messaging_read_recv(subreq
, talloc_tos(), &rec
);
299 ndr_err
= ndr_pull_struct_blob(&rec
->buf
, rec
, &close_blob
,
300 (ndr_pull_flags_fn_t
)ndr_pull_smbXsrv_session_closeB
);
301 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
302 status
= ndr_map_error2ntstatus(ndr_err
);
303 DEBUG(1,("smbXsrv_session_close_loop: "
304 "ndr_pull_struct_blob - %s\n",
309 DEBUG(10,("smbXsrv_session_close_loop: MSG_SMBXSRV_SESSION_CLOSE\n"));
311 NDR_PRINT_DEBUG(smbXsrv_session_closeB
, &close_blob
);
314 if (close_blob
.version
!= SMBXSRV_VERSION_0
) {
315 DEBUG(0,("smbXsrv_session_close_loop: "
316 "ignore invalid version %u\n", close_blob
.version
));
317 NDR_PRINT_DEBUG(smbXsrv_session_closeB
, &close_blob
);
321 close_info0
= close_blob
.info
.info0
;
322 if (close_info0
== NULL
) {
323 DEBUG(0,("smbXsrv_session_close_loop: "
324 "ignore NULL info %u\n", close_blob
.version
));
325 NDR_PRINT_DEBUG(smbXsrv_session_closeB
, &close_blob
);
329 status
= smb2srv_session_lookup_client(client
,
330 close_info0
->old_session_wire_id
,
332 if (NT_STATUS_EQUAL(status
, NT_STATUS_USER_SESSION_DELETED
)) {
333 DEBUG(4,("smbXsrv_session_close_loop: "
334 "old_session_wire_id %llu not found\n",
335 (unsigned long long)close_info0
->old_session_wire_id
));
337 NDR_PRINT_DEBUG(smbXsrv_session_closeB
, &close_blob
);
341 if (!NT_STATUS_IS_OK(status
) &&
342 !NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
) &&
343 !NT_STATUS_EQUAL(status
, NT_STATUS_NETWORK_SESSION_EXPIRED
)) {
344 DEBUG(1,("smbXsrv_session_close_loop: "
345 "old_session_wire_id %llu - %s\n",
346 (unsigned long long)close_info0
->old_session_wire_id
,
349 NDR_PRINT_DEBUG(smbXsrv_session_closeB
, &close_blob
);
354 if (session
->global
->session_global_id
!= close_info0
->old_session_global_id
) {
355 DEBUG(1,("smbXsrv_session_close_loop: "
356 "old_session_wire_id %llu - global %u != %u\n",
357 (unsigned long long)close_info0
->old_session_wire_id
,
358 session
->global
->session_global_id
,
359 close_info0
->old_session_global_id
));
361 NDR_PRINT_DEBUG(smbXsrv_session_closeB
, &close_blob
);
366 if (session
->global
->creation_time
!= close_info0
->old_creation_time
) {
367 DEBUG(1,("smbXsrv_session_close_loop: "
368 "old_session_wire_id %llu - "
369 "creation %s (%llu) != %s (%llu)\n",
370 (unsigned long long)close_info0
->old_session_wire_id
,
371 nt_time_string(rec
, session
->global
->creation_time
),
372 (unsigned long long)session
->global
->creation_time
,
373 nt_time_string(rec
, close_info0
->old_creation_time
),
374 (unsigned long long)close_info0
->old_creation_time
));
376 NDR_PRINT_DEBUG(smbXsrv_session_closeB
, &close_blob
);
381 subreq
= smb2srv_session_shutdown_send(session
, client
->raw_ev_ctx
,
383 if (subreq
== NULL
) {
384 status
= NT_STATUS_NO_MEMORY
;
385 DEBUG(0, ("smbXsrv_session_close_loop: "
386 "smb2srv_session_shutdown_send(%llu) failed: %s\n",
387 (unsigned long long)session
->global
->session_wire_id
,
390 NDR_PRINT_DEBUG(smbXsrv_session_closeB
, &close_blob
);
394 tevent_req_set_callback(subreq
,
395 smbXsrv_session_close_shutdown_done
,
401 subreq
= messaging_read_send(table
,
404 MSG_SMBXSRV_SESSION_CLOSE
);
405 if (subreq
== NULL
) {
407 r
= "messaging_read_send(MSG_SMBXSRV_SESSION_CLOSE) failed";
408 exit_server_cleanly(r
);
411 tevent_req_set_callback(subreq
, smbXsrv_session_close_loop
, client
);
414 static void smbXsrv_session_close_shutdown_done(struct tevent_req
*subreq
)
416 struct smbXsrv_session
*session
=
417 tevent_req_callback_data(subreq
,
418 struct smbXsrv_session
);
421 status
= smb2srv_session_shutdown_recv(subreq
);
423 if (!NT_STATUS_IS_OK(status
)) {
424 DEBUG(0, ("smbXsrv_session_close_loop: "
425 "smb2srv_session_shutdown_recv(%llu) failed: %s\n",
426 (unsigned long long)session
->global
->session_wire_id
,
430 status
= smbXsrv_session_logoff(session
);
431 if (!NT_STATUS_IS_OK(status
)) {
432 DEBUG(0, ("smbXsrv_session_close_loop: "
433 "smbXsrv_session_logoff(%llu) failed: %s\n",
434 (unsigned long long)session
->global
->session_wire_id
,
438 TALLOC_FREE(session
);
441 struct smb1srv_session_local_allocate_state
{
442 const uint32_t lowest_id
;
443 const uint32_t highest_id
;
449 static int smb1srv_session_local_allocate_traverse(struct db_record
*rec
,
452 struct smb1srv_session_local_allocate_state
*state
=
453 (struct smb1srv_session_local_allocate_state
*)private_data
;
454 TDB_DATA key
= dbwrap_record_get_key(rec
);
458 status
= smbXsrv_session_local_key_to_id(key
, &id
);
459 if (!NT_STATUS_IS_OK(status
)) {
460 state
->status
= status
;
464 if (id
<= state
->last_id
) {
465 state
->status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
470 if (id
> state
->useable_id
) {
471 state
->status
= NT_STATUS_OK
;
475 if (state
->useable_id
== state
->highest_id
) {
476 state
->status
= NT_STATUS_INSUFFICIENT_RESOURCES
;
480 state
->useable_id
+=1;
484 static NTSTATUS
smb1srv_session_local_allocate_id(struct db_context
*db
,
488 struct db_record
**_rec
,
491 struct smb1srv_session_local_allocate_state state
= {
492 .lowest_id
= lowest_id
,
493 .highest_id
= highest_id
,
495 .useable_id
= lowest_id
,
496 .status
= NT_STATUS_INTERNAL_ERROR
,
506 if (lowest_id
> highest_id
) {
507 return NT_STATUS_INSUFFICIENT_RESOURCES
;
511 * first we try randomly
513 range
= (highest_id
- lowest_id
) + 1;
515 for (i
= 0; i
< (range
/ 2); i
++) {
518 struct db_record
*rec
= NULL
;
520 id
= generate_random() % range
;
523 if (id
< lowest_id
) {
526 if (id
> highest_id
) {
530 rec
= smbXsrv_session_local_fetch_locked(db
, id
, mem_ctx
);
532 return NT_STATUS_INSUFFICIENT_RESOURCES
;
535 val
= dbwrap_record_get_value(rec
);
536 if (val
.dsize
!= 0) {
547 * if the range is almost full,
548 * we traverse the whole table
549 * (this relies on sorted behavior of dbwrap_rbt)
551 status
= dbwrap_traverse_read(db
, smb1srv_session_local_allocate_traverse
,
553 if (NT_STATUS_IS_OK(status
)) {
554 if (NT_STATUS_IS_OK(state
.status
)) {
555 return NT_STATUS_INTERNAL_ERROR
;
558 if (!NT_STATUS_EQUAL(state
.status
, NT_STATUS_INTERNAL_ERROR
)) {
562 if (state
.useable_id
<= state
.highest_id
) {
563 state
.status
= NT_STATUS_OK
;
565 return NT_STATUS_INSUFFICIENT_RESOURCES
;
567 } else if (!NT_STATUS_EQUAL(status
, NT_STATUS_INTERNAL_DB_CORRUPTION
)) {
569 * Here we really expect NT_STATUS_INTERNAL_DB_CORRUPTION!
571 * If we get anything else it is an error, because it
572 * means we did not manage to find a free slot in
575 return NT_STATUS_INSUFFICIENT_RESOURCES
;
578 if (NT_STATUS_IS_OK(state
.status
)) {
581 struct db_record
*rec
= NULL
;
583 id
= state
.useable_id
;
585 rec
= smbXsrv_session_local_fetch_locked(db
, id
, mem_ctx
);
587 return NT_STATUS_INSUFFICIENT_RESOURCES
;
590 val
= dbwrap_record_get_value(rec
);
591 if (val
.dsize
!= 0) {
593 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
604 struct smbXsrv_session_local_fetch_state
{
605 struct smbXsrv_session
*session
;
609 static void smbXsrv_session_local_fetch_parser(TDB_DATA key
, TDB_DATA data
,
612 struct smbXsrv_session_local_fetch_state
*state
=
613 (struct smbXsrv_session_local_fetch_state
*)private_data
;
616 if (data
.dsize
!= sizeof(ptr
)) {
617 state
->status
= NT_STATUS_INTERNAL_DB_ERROR
;
621 memcpy(&ptr
, data
.dptr
, data
.dsize
);
622 state
->session
= talloc_get_type_abort(ptr
, struct smbXsrv_session
);
623 state
->status
= NT_STATUS_OK
;
626 static NTSTATUS
smbXsrv_session_local_lookup(struct smbXsrv_session_table
*table
,
628 struct smbXsrv_connection
*conn
,
629 uint32_t session_local_id
,
631 struct smbXsrv_session
**_session
)
633 struct smbXsrv_session_local_fetch_state state
= {
635 .status
= NT_STATUS_INTERNAL_ERROR
,
637 uint8_t key_buf
[SMBXSRV_SESSION_LOCAL_TDB_KEY_SIZE
];
643 if (session_local_id
== 0) {
644 return NT_STATUS_USER_SESSION_DELETED
;
648 /* this might happen before the end of negprot */
649 return NT_STATUS_USER_SESSION_DELETED
;
652 if (table
->local
.db_ctx
== NULL
) {
653 return NT_STATUS_INTERNAL_ERROR
;
656 key
= smbXsrv_session_local_id_to_key(session_local_id
, key_buf
);
658 status
= dbwrap_parse_record(table
->local
.db_ctx
, key
,
659 smbXsrv_session_local_fetch_parser
,
661 if (NT_STATUS_EQUAL(status
, NT_STATUS_NOT_FOUND
)) {
662 return NT_STATUS_USER_SESSION_DELETED
;
663 } else if (!NT_STATUS_IS_OK(status
)) {
666 if (!NT_STATUS_IS_OK(state
.status
)) {
670 if (NT_STATUS_EQUAL(state
.session
->status
, NT_STATUS_USER_SESSION_DELETED
)) {
671 return NT_STATUS_USER_SESSION_DELETED
;
675 * If a connection is specified check if the session is
676 * valid on the channel.
679 struct smbXsrv_channel_global0
*c
= NULL
;
681 status
= smbXsrv_session_find_channel(state
.session
, conn
, &c
);
682 if (!NT_STATUS_IS_OK(status
)) {
687 state
.session
->idle_time
= now
;
689 if (!NT_STATUS_IS_OK(state
.session
->status
)) {
690 *_session
= state
.session
;
691 return state
.session
->status
;
694 if (now
> state
.session
->global
->expiration_time
) {
695 state
.session
->status
= NT_STATUS_NETWORK_SESSION_EXPIRED
;
698 *_session
= state
.session
;
699 return state
.session
->status
;
702 static int smbXsrv_session_global_destructor(struct smbXsrv_session_global0
*global
)
707 static void smbXsrv_session_global_verify_record(struct db_record
*db_rec
,
711 struct smbXsrv_session_global0
**_g
);
713 static NTSTATUS
smbXsrv_session_global_allocate(struct db_context
*db
,
715 struct smbXsrv_session_global0
**_global
)
718 struct smbXsrv_session_global0
*global
= NULL
;
719 uint32_t last_free
= 0;
720 const uint32_t min_tries
= 3;
724 global
= talloc_zero(mem_ctx
, struct smbXsrv_session_global0
);
725 if (global
== NULL
) {
726 return NT_STATUS_NO_MEMORY
;
728 talloc_set_destructor(global
, smbXsrv_session_global_destructor
);
731 * Here we just randomly try the whole 32-bit space
733 * We use just 32-bit, because we want to reuse the
736 for (i
= 0; i
< UINT32_MAX
; i
++) {
737 bool is_free
= false;
738 bool was_free
= false;
741 if (i
>= min_tries
&& last_free
!= 0) {
744 id
= generate_random();
749 if (id
== UINT32_MAX
) {
753 global
->db_rec
= smbXsrv_session_global_fetch_locked(db
, id
,
755 if (global
->db_rec
== NULL
) {
757 return NT_STATUS_INSUFFICIENT_RESOURCES
;
760 smbXsrv_session_global_verify_record(global
->db_rec
,
766 TALLOC_FREE(global
->db_rec
);
770 if (!was_free
&& i
< min_tries
) {
772 * The session_id is free now,
773 * but was not free before.
775 * This happens if a smbd crashed
776 * and did not cleanup the record.
778 * If this is one of our first tries,
779 * then we try to find a real free one.
781 if (last_free
== 0) {
784 TALLOC_FREE(global
->db_rec
);
788 global
->session_global_id
= id
;
794 /* should not be reached */
796 return NT_STATUS_INTERNAL_ERROR
;
799 static void smbXsrv_session_global_verify_record(struct db_record
*db_rec
,
803 struct smbXsrv_session_global0
**_g
)
808 struct smbXsrv_session_globalB global_blob
;
809 enum ndr_err_code ndr_err
;
810 struct smbXsrv_session_global0
*global
= NULL
;
812 TALLOC_CTX
*frame
= talloc_stackframe();
823 key
= dbwrap_record_get_key(db_rec
);
825 val
= dbwrap_record_get_value(db_rec
);
826 if (val
.dsize
== 0) {
835 blob
= data_blob_const(val
.dptr
, val
.dsize
);
837 ndr_err
= ndr_pull_struct_blob(&blob
, frame
, &global_blob
,
838 (ndr_pull_flags_fn_t
)ndr_pull_smbXsrv_session_globalB
);
839 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
840 NTSTATUS status
= ndr_map_error2ntstatus(ndr_err
);
841 DEBUG(1,("smbXsrv_session_global_verify_record: "
842 "key '%s' ndr_pull_struct_blob - %s\n",
843 hex_encode_talloc(frame
, key
.dptr
, key
.dsize
),
853 DEBUG(10,("smbXsrv_session_global_verify_record\n"));
855 NDR_PRINT_DEBUG(smbXsrv_session_globalB
, &global_blob
);
858 if (global_blob
.version
!= SMBXSRV_VERSION_0
) {
859 DEBUG(0,("smbXsrv_session_global_verify_record: "
860 "key '%s' use unsupported version %u\n",
861 hex_encode_talloc(frame
, key
.dptr
, key
.dsize
),
862 global_blob
.version
));
863 NDR_PRINT_DEBUG(smbXsrv_session_globalB
, &global_blob
);
872 global
= global_blob
.info
.info0
;
874 exists
= serverid_exists(&global
->channels
[0].server_id
);
876 struct server_id_buf idbuf
;
877 DEBUG(2,("smbXsrv_session_global_verify_record: "
878 "key '%s' server_id %s does not exist.\n",
879 hex_encode_talloc(frame
, key
.dptr
, key
.dsize
),
880 server_id_str_buf(global
->channels
[0].server_id
,
883 NDR_PRINT_DEBUG(smbXsrv_session_globalB
, &global_blob
);
886 dbwrap_record_delete(db_rec
);
892 *_g
= talloc_move(mem_ctx
, &global
);
897 static NTSTATUS
smbXsrv_session_global_store(struct smbXsrv_session_global0
*global
)
899 struct smbXsrv_session_globalB global_blob
;
900 DATA_BLOB blob
= data_blob_null
;
904 enum ndr_err_code ndr_err
;
907 * TODO: if we use other versions than '0'
908 * we would add glue code here, that would be able to
909 * store the information in the old format.
912 if (global
->db_rec
== NULL
) {
913 return NT_STATUS_INTERNAL_ERROR
;
916 key
= dbwrap_record_get_key(global
->db_rec
);
917 val
= dbwrap_record_get_value(global
->db_rec
);
919 ZERO_STRUCT(global_blob
);
920 global_blob
.version
= smbXsrv_version_global_current();
921 if (val
.dsize
>= 8) {
922 global_blob
.seqnum
= IVAL(val
.dptr
, 4);
924 global_blob
.seqnum
+= 1;
925 global_blob
.info
.info0
= global
;
927 ndr_err
= ndr_push_struct_blob(&blob
, global
->db_rec
, &global_blob
,
928 (ndr_push_flags_fn_t
)ndr_push_smbXsrv_session_globalB
);
929 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
930 status
= ndr_map_error2ntstatus(ndr_err
);
931 DEBUG(1,("smbXsrv_session_global_store: key '%s' ndr_push - %s\n",
932 hex_encode_talloc(global
->db_rec
, key
.dptr
, key
.dsize
),
934 TALLOC_FREE(global
->db_rec
);
938 val
= make_tdb_data(blob
.data
, blob
.length
);
939 status
= dbwrap_record_store(global
->db_rec
, val
, TDB_REPLACE
);
940 if (!NT_STATUS_IS_OK(status
)) {
941 DEBUG(1,("smbXsrv_session_global_store: key '%s' store - %s\n",
942 hex_encode_talloc(global
->db_rec
, key
.dptr
, key
.dsize
),
944 TALLOC_FREE(global
->db_rec
);
949 DEBUG(10,("smbXsrv_session_global_store: key '%s' stored\n",
950 hex_encode_talloc(global
->db_rec
, key
.dptr
, key
.dsize
)));
951 NDR_PRINT_DEBUG(smbXsrv_session_globalB
, &global_blob
);
954 TALLOC_FREE(global
->db_rec
);
959 struct smb2srv_session_close_previous_state
{
960 struct tevent_context
*ev
;
961 struct smbXsrv_connection
*connection
;
962 struct dom_sid
*current_sid
;
963 uint64_t previous_session_id
;
964 uint64_t current_session_id
;
965 struct db_record
*db_rec
;
968 static void smb2srv_session_close_previous_check(struct tevent_req
*req
);
969 static void smb2srv_session_close_previous_modified(struct tevent_req
*subreq
);
971 struct tevent_req
*smb2srv_session_close_previous_send(TALLOC_CTX
*mem_ctx
,
972 struct tevent_context
*ev
,
973 struct smbXsrv_connection
*conn
,
974 struct auth_session_info
*session_info
,
975 uint64_t previous_session_id
,
976 uint64_t current_session_id
)
978 struct tevent_req
*req
;
979 struct smb2srv_session_close_previous_state
*state
;
980 uint32_t global_id
= previous_session_id
& UINT32_MAX
;
981 uint64_t global_zeros
= previous_session_id
& 0xFFFFFFFF00000000LLU
;
982 struct smbXsrv_session_table
*table
= conn
->client
->session_table
;
983 struct security_token
*current_token
= NULL
;
985 req
= tevent_req_create(mem_ctx
, &state
,
986 struct smb2srv_session_close_previous_state
);
991 state
->connection
= conn
;
992 state
->previous_session_id
= previous_session_id
;
993 state
->current_session_id
= current_session_id
;
995 if (global_zeros
!= 0) {
996 tevent_req_done(req
);
997 return tevent_req_post(req
, ev
);
1000 if (session_info
== NULL
) {
1001 tevent_req_done(req
);
1002 return tevent_req_post(req
, ev
);
1004 current_token
= session_info
->security_token
;
1006 if (current_token
->num_sids
> PRIMARY_USER_SID_INDEX
) {
1007 state
->current_sid
= ¤t_token
->sids
[PRIMARY_USER_SID_INDEX
];
1010 if (state
->current_sid
== NULL
) {
1011 tevent_req_done(req
);
1012 return tevent_req_post(req
, ev
);
1015 if (!security_token_has_nt_authenticated_users(current_token
)) {
1017 tevent_req_done(req
);
1018 return tevent_req_post(req
, ev
);
1021 state
->db_rec
= smbXsrv_session_global_fetch_locked(
1022 table
->global
.db_ctx
,
1024 state
/* TALLOC_CTX */);
1025 if (state
->db_rec
== NULL
) {
1026 tevent_req_nterror(req
, NT_STATUS_UNSUCCESSFUL
);
1027 return tevent_req_post(req
, ev
);
1030 smb2srv_session_close_previous_check(req
);
1031 if (!tevent_req_is_in_progress(req
)) {
1032 return tevent_req_post(req
, ev
);
1038 static void smb2srv_session_close_previous_check(struct tevent_req
*req
)
1040 struct smb2srv_session_close_previous_state
*state
=
1041 tevent_req_data(req
,
1042 struct smb2srv_session_close_previous_state
);
1043 struct smbXsrv_connection
*conn
= state
->connection
;
1045 struct security_token
*previous_token
= NULL
;
1046 struct smbXsrv_session_global0
*global
= NULL
;
1047 enum ndr_err_code ndr_err
;
1048 struct smbXsrv_session_close0 close_info0
;
1049 struct smbXsrv_session_closeB close_blob
;
1050 struct tevent_req
*subreq
= NULL
;
1052 bool is_free
= false;
1054 smbXsrv_session_global_verify_record(state
->db_rec
,
1061 TALLOC_FREE(state
->db_rec
);
1062 tevent_req_done(req
);
1066 if (global
->auth_session_info
== NULL
) {
1067 TALLOC_FREE(state
->db_rec
);
1068 tevent_req_done(req
);
1072 previous_token
= global
->auth_session_info
->security_token
;
1074 if (!security_token_is_sid(previous_token
, state
->current_sid
)) {
1075 TALLOC_FREE(state
->db_rec
);
1076 tevent_req_done(req
);
1080 subreq
= dbwrap_watched_watch_send(state
, state
->ev
, state
->db_rec
,
1081 (struct server_id
){0});
1082 if (tevent_req_nomem(subreq
, req
)) {
1083 TALLOC_FREE(state
->db_rec
);
1086 tevent_req_set_callback(subreq
,
1087 smb2srv_session_close_previous_modified
,
1090 close_info0
.old_session_global_id
= global
->session_global_id
;
1091 close_info0
.old_session_wire_id
= global
->session_wire_id
;
1092 close_info0
.old_creation_time
= global
->creation_time
;
1093 close_info0
.new_session_wire_id
= state
->current_session_id
;
1095 ZERO_STRUCT(close_blob
);
1096 close_blob
.version
= smbXsrv_version_global_current();
1097 close_blob
.info
.info0
= &close_info0
;
1099 ndr_err
= ndr_push_struct_blob(&blob
, state
, &close_blob
,
1100 (ndr_push_flags_fn_t
)ndr_push_smbXsrv_session_closeB
);
1101 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
1102 TALLOC_FREE(state
->db_rec
);
1103 status
= ndr_map_error2ntstatus(ndr_err
);
1104 DEBUG(1,("smb2srv_session_close_previous_check: "
1105 "old_session[%llu] new_session[%llu] ndr_push - %s\n",
1106 (unsigned long long)close_info0
.old_session_wire_id
,
1107 (unsigned long long)close_info0
.new_session_wire_id
,
1108 nt_errstr(status
)));
1109 tevent_req_nterror(req
, status
);
1113 status
= messaging_send(conn
->client
->msg_ctx
,
1114 global
->channels
[0].server_id
,
1115 MSG_SMBXSRV_SESSION_CLOSE
, &blob
);
1116 TALLOC_FREE(state
->db_rec
);
1117 if (tevent_req_nterror(req
, status
)) {
1121 TALLOC_FREE(global
);
1125 static void smb2srv_session_close_previous_modified(struct tevent_req
*subreq
)
1127 struct tevent_req
*req
=
1128 tevent_req_callback_data(subreq
,
1130 struct smb2srv_session_close_previous_state
*state
=
1131 tevent_req_data(req
,
1132 struct smb2srv_session_close_previous_state
);
1136 status
= dbwrap_watched_watch_recv(subreq
, NULL
, NULL
);
1137 TALLOC_FREE(subreq
);
1138 if (tevent_req_nterror(req
, status
)) {
1142 global_id
= state
->previous_session_id
& UINT32_MAX
;
1144 state
->db_rec
= smbXsrv_session_global_fetch_locked(
1145 state
->connection
->client
->session_table
->global
.db_ctx
,
1146 global_id
, state
/* TALLOC_CTX */);
1148 smb2srv_session_close_previous_check(req
);
1151 NTSTATUS
smb2srv_session_close_previous_recv(struct tevent_req
*req
)
1155 if (tevent_req_is_nterror(req
, &status
)) {
1156 tevent_req_received(req
);
1160 tevent_req_received(req
);
1161 return NT_STATUS_OK
;
1164 static NTSTATUS
smbXsrv_session_clear_and_logoff(struct smbXsrv_session
*session
)
1167 struct smbXsrv_connection
*xconn
= NULL
;
1169 if (session
->client
!= NULL
) {
1170 xconn
= session
->client
->connections
;
1173 for (; xconn
!= NULL
; xconn
= xconn
->next
) {
1174 struct smbd_smb2_request
*preq
;
1176 for (preq
= xconn
->smb2
.requests
; preq
!= NULL
; preq
= preq
->next
) {
1177 if (preq
->session
!= session
) {
1181 preq
->session
= NULL
;
1183 * If we no longer have a session we can't
1184 * sign or encrypt replies.
1186 preq
->do_signing
= false;
1187 preq
->do_encryption
= false;
1188 preq
->preauth
= NULL
;
1192 status
= smbXsrv_session_logoff(session
);
1196 static int smbXsrv_session_destructor(struct smbXsrv_session
*session
)
1200 status
= smbXsrv_session_clear_and_logoff(session
);
1201 if (!NT_STATUS_IS_OK(status
)) {
1202 DEBUG(0, ("smbXsrv_session_destructor: "
1203 "smbXsrv_session_logoff() failed: %s\n",
1204 nt_errstr(status
)));
1207 TALLOC_FREE(session
->global
);
1212 NTSTATUS
smbXsrv_session_create(struct smbXsrv_connection
*conn
,
1214 struct smbXsrv_session
**_session
)
1216 struct smbXsrv_session_table
*table
= conn
->client
->session_table
;
1217 struct db_record
*local_rec
= NULL
;
1218 struct smbXsrv_session
*session
= NULL
;
1221 struct smbXsrv_session_global0
*global
= NULL
;
1222 struct smbXsrv_channel_global0
*channel
= NULL
;
1225 if (table
->local
.num_sessions
>= table
->local
.max_sessions
) {
1226 return NT_STATUS_INSUFFICIENT_RESOURCES
;
1229 session
= talloc_zero(table
, struct smbXsrv_session
);
1230 if (session
== NULL
) {
1231 return NT_STATUS_NO_MEMORY
;
1233 session
->table
= table
;
1234 session
->idle_time
= now
;
1235 session
->status
= NT_STATUS_MORE_PROCESSING_REQUIRED
;
1236 session
->client
= conn
->client
;
1237 session
->homes_snum
= -1;
1239 status
= smbXsrv_session_global_allocate(table
->global
.db_ctx
,
1242 if (!NT_STATUS_IS_OK(status
)) {
1243 TALLOC_FREE(session
);
1246 session
->global
= global
;
1248 if (conn
->protocol
>= PROTOCOL_SMB2_02
) {
1249 uint64_t id
= global
->session_global_id
;
1251 global
->connection_dialect
= conn
->smb2
.server
.dialect
;
1253 global
->session_wire_id
= id
;
1255 status
= smb2srv_tcon_table_init(session
);
1256 if (!NT_STATUS_IS_OK(status
)) {
1257 TALLOC_FREE(session
);
1261 session
->local_id
= global
->session_global_id
;
1263 local_rec
= smbXsrv_session_local_fetch_locked(
1264 table
->local
.db_ctx
,
1266 session
/* TALLOC_CTX */);
1267 if (local_rec
== NULL
) {
1268 TALLOC_FREE(session
);
1269 return NT_STATUS_NO_MEMORY
;
1272 val
= dbwrap_record_get_value(local_rec
);
1273 if (val
.dsize
!= 0) {
1274 TALLOC_FREE(session
);
1275 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
1279 status
= smb1srv_session_local_allocate_id(table
->local
.db_ctx
,
1280 table
->local
.lowest_id
,
1281 table
->local
.highest_id
,
1284 &session
->local_id
);
1285 if (!NT_STATUS_IS_OK(status
)) {
1286 TALLOC_FREE(session
);
1290 global
->session_wire_id
= session
->local_id
;
1293 global
->creation_time
= now
;
1294 global
->expiration_time
= GENSEC_EXPIRE_TIME_INFINITY
;
1296 status
= smbXsrv_session_add_channel(session
, conn
, now
, &channel
);
1297 if (!NT_STATUS_IS_OK(status
)) {
1298 TALLOC_FREE(session
);
1303 val
= make_tdb_data((uint8_t const *)&ptr
, sizeof(ptr
));
1304 status
= dbwrap_record_store(local_rec
, val
, TDB_REPLACE
);
1305 TALLOC_FREE(local_rec
);
1306 if (!NT_STATUS_IS_OK(status
)) {
1307 TALLOC_FREE(session
);
1310 table
->local
.num_sessions
+= 1;
1312 talloc_set_destructor(session
, smbXsrv_session_destructor
);
1314 status
= smbXsrv_session_global_store(global
);
1315 if (!NT_STATUS_IS_OK(status
)) {
1316 DEBUG(0,("smbXsrv_session_create: "
1317 "global_id (0x%08x) store failed - %s\n",
1318 session
->global
->session_global_id
,
1319 nt_errstr(status
)));
1320 TALLOC_FREE(session
);
1325 struct smbXsrv_sessionB session_blob
= {
1326 .version
= SMBXSRV_VERSION_0
,
1327 .info
.info0
= session
,
1330 DEBUG(10,("smbXsrv_session_create: global_id (0x%08x) stored\n",
1331 session
->global
->session_global_id
));
1332 NDR_PRINT_DEBUG(smbXsrv_sessionB
, &session_blob
);
1335 *_session
= session
;
1336 return NT_STATUS_OK
;
1339 NTSTATUS
smbXsrv_session_add_channel(struct smbXsrv_session
*session
,
1340 struct smbXsrv_connection
*conn
,
1342 struct smbXsrv_channel_global0
**_c
)
1344 struct smbXsrv_session_global0
*global
= session
->global
;
1345 struct smbXsrv_channel_global0
*c
= NULL
;
1347 if (global
->num_channels
> 31) {
1349 * Windows allow up to 32 channels
1351 return NT_STATUS_INSUFFICIENT_RESOURCES
;
1354 c
= talloc_realloc(global
,
1356 struct smbXsrv_channel_global0
,
1357 global
->num_channels
+ 1);
1359 return NT_STATUS_NO_MEMORY
;
1361 global
->channels
= c
;
1363 c
= &global
->channels
[global
->num_channels
];
1366 c
->server_id
= messaging_server_id(conn
->client
->msg_ctx
);
1367 c
->channel_id
= conn
->channel_id
;
1368 c
->creation_time
= now
;
1369 c
->local_address
= tsocket_address_string(conn
->local_address
,
1371 if (c
->local_address
== NULL
) {
1372 return NT_STATUS_NO_MEMORY
;
1374 c
->remote_address
= tsocket_address_string(conn
->remote_address
,
1376 if (c
->remote_address
== NULL
) {
1377 return NT_STATUS_NO_MEMORY
;
1379 c
->remote_name
= talloc_strdup(global
->channels
,
1380 conn
->remote_hostname
);
1381 if (c
->remote_name
== NULL
) {
1382 return NT_STATUS_NO_MEMORY
;
1384 c
->connection
= conn
;
1386 global
->num_channels
+= 1;
1389 return NT_STATUS_OK
;
1392 NTSTATUS
smbXsrv_session_update(struct smbXsrv_session
*session
)
1394 struct smbXsrv_session_table
*table
= session
->table
;
1397 if (session
->global
->db_rec
!= NULL
) {
1398 DEBUG(0, ("smbXsrv_session_update(0x%08x): "
1399 "Called with db_rec != NULL'\n",
1400 session
->global
->session_global_id
));
1401 return NT_STATUS_INTERNAL_ERROR
;
1404 session
->global
->db_rec
= smbXsrv_session_global_fetch_locked(
1405 table
->global
.db_ctx
,
1406 session
->global
->session_global_id
,
1407 session
->global
/* TALLOC_CTX */);
1408 if (session
->global
->db_rec
== NULL
) {
1409 return NT_STATUS_INTERNAL_DB_ERROR
;
1412 status
= smbXsrv_session_global_store(session
->global
);
1413 if (!NT_STATUS_IS_OK(status
)) {
1414 DEBUG(0,("smbXsrv_session_update: "
1415 "global_id (0x%08x) store failed - %s\n",
1416 session
->global
->session_global_id
,
1417 nt_errstr(status
)));
1422 struct smbXsrv_sessionB session_blob
= {
1423 .version
= SMBXSRV_VERSION_0
,
1424 .info
.info0
= session
,
1427 DEBUG(10,("smbXsrv_session_update: global_id (0x%08x) stored\n",
1428 session
->global
->session_global_id
));
1429 NDR_PRINT_DEBUG(smbXsrv_sessionB
, &session_blob
);
1432 return NT_STATUS_OK
;
1435 NTSTATUS
smbXsrv_session_find_channel(const struct smbXsrv_session
*session
,
1436 const struct smbXsrv_connection
*conn
,
1437 struct smbXsrv_channel_global0
**_c
)
1441 for (i
=0; i
< session
->global
->num_channels
; i
++) {
1442 struct smbXsrv_channel_global0
*c
= &session
->global
->channels
[i
];
1444 if (c
->channel_id
!= conn
->channel_id
) {
1448 if (c
->connection
!= conn
) {
1453 return NT_STATUS_OK
;
1456 return NT_STATUS_USER_SESSION_DELETED
;
1459 NTSTATUS
smbXsrv_session_find_auth(const struct smbXsrv_session
*session
,
1460 const struct smbXsrv_connection
*conn
,
1462 struct smbXsrv_session_auth0
**_a
)
1464 struct smbXsrv_session_auth0
*a
;
1466 for (a
= session
->pending_auth
; a
!= NULL
; a
= a
->next
) {
1467 if (a
->channel_id
!= conn
->channel_id
) {
1471 if (a
->connection
== conn
) {
1476 return NT_STATUS_OK
;
1480 return NT_STATUS_USER_SESSION_DELETED
;
1483 static int smbXsrv_session_auth0_destructor(struct smbXsrv_session_auth0
*a
)
1485 if (a
->session
== NULL
) {
1489 DLIST_REMOVE(a
->session
->pending_auth
, a
);
1494 NTSTATUS
smbXsrv_session_create_auth(struct smbXsrv_session
*session
,
1495 struct smbXsrv_connection
*conn
,
1498 uint8_t in_security_mode
,
1499 struct smbXsrv_session_auth0
**_a
)
1501 struct smbXsrv_session_auth0
*a
;
1504 status
= smbXsrv_session_find_auth(session
, conn
, 0, &a
);
1505 if (NT_STATUS_IS_OK(status
)) {
1506 return NT_STATUS_INTERNAL_ERROR
;
1509 a
= talloc_zero(session
, struct smbXsrv_session_auth0
);
1511 return NT_STATUS_NO_MEMORY
;
1513 a
->session
= session
;
1514 a
->connection
= conn
;
1515 a
->in_flags
= in_flags
;
1516 a
->in_security_mode
= in_security_mode
;
1517 a
->creation_time
= now
;
1519 a
->channel_id
= conn
->channel_id
;
1521 if (conn
->protocol
>= PROTOCOL_SMB3_10
) {
1522 a
->preauth
= talloc(a
, struct smbXsrv_preauth
);
1523 if (a
->preauth
== NULL
) {
1524 TALLOC_FREE(session
);
1525 return NT_STATUS_NO_MEMORY
;
1527 *a
->preauth
= conn
->smb2
.preauth
;
1530 talloc_set_destructor(a
, smbXsrv_session_auth0_destructor
);
1531 DLIST_ADD_END(session
->pending_auth
, a
);
1534 return NT_STATUS_OK
;
1537 struct smb2srv_session_shutdown_state
{
1538 struct tevent_queue
*wait_queue
;
1541 static void smb2srv_session_shutdown_wait_done(struct tevent_req
*subreq
);
1543 struct tevent_req
*smb2srv_session_shutdown_send(TALLOC_CTX
*mem_ctx
,
1544 struct tevent_context
*ev
,
1545 struct smbXsrv_session
*session
,
1546 struct smbd_smb2_request
*current_req
)
1548 struct tevent_req
*req
;
1549 struct smb2srv_session_shutdown_state
*state
;
1550 struct tevent_req
*subreq
;
1551 struct smbXsrv_connection
*xconn
= NULL
;
1555 * Make sure that no new request will be able to use this session.
1557 session
->status
= NT_STATUS_USER_SESSION_DELETED
;
1559 req
= tevent_req_create(mem_ctx
, &state
,
1560 struct smb2srv_session_shutdown_state
);
1565 state
->wait_queue
= tevent_queue_create(state
, "smb2srv_session_shutdown_queue");
1566 if (tevent_req_nomem(state
->wait_queue
, req
)) {
1567 return tevent_req_post(req
, ev
);
1570 for (xconn
= session
->client
->connections
; xconn
!= NULL
; xconn
= xconn
->next
) {
1571 struct smbd_smb2_request
*preq
;
1573 for (preq
= xconn
->smb2
.requests
; preq
!= NULL
; preq
= preq
->next
) {
1574 if (preq
== current_req
) {
1575 /* Can't cancel current request. */
1578 if (preq
->session
!= session
) {
1579 /* Request on different session. */
1583 if (!NT_STATUS_IS_OK(xconn
->transport
.status
)) {
1584 preq
->session
= NULL
;
1586 * If we no longer have a session we can't
1587 * sign or encrypt replies.
1589 preq
->do_signing
= false;
1590 preq
->do_encryption
= false;
1591 preq
->preauth
= NULL
;
1593 if (preq
->subreq
!= NULL
) {
1594 tevent_req_cancel(preq
->subreq
);
1600 * Never cancel anything in a compound
1601 * request. Way too hard to deal with
1604 if (!preq
->compound_related
&& preq
->subreq
!= NULL
) {
1605 tevent_req_cancel(preq
->subreq
);
1609 * Now wait until the request is finished.
1611 * We don't set a callback, as we just want to block the
1612 * wait queue and the talloc_free() of the request will
1613 * remove the item from the wait queue.
1615 subreq
= tevent_queue_wait_send(preq
, ev
, state
->wait_queue
);
1616 if (tevent_req_nomem(subreq
, req
)) {
1617 return tevent_req_post(req
, ev
);
1622 len
= tevent_queue_length(state
->wait_queue
);
1624 tevent_req_done(req
);
1625 return tevent_req_post(req
, ev
);
1629 * Now we add our own waiter to the end of the queue,
1630 * this way we get notified when all pending requests are finished
1631 * and send to the socket.
1633 subreq
= tevent_queue_wait_send(state
, ev
, state
->wait_queue
);
1634 if (tevent_req_nomem(subreq
, req
)) {
1635 return tevent_req_post(req
, ev
);
1637 tevent_req_set_callback(subreq
, smb2srv_session_shutdown_wait_done
, req
);
1642 static void smb2srv_session_shutdown_wait_done(struct tevent_req
*subreq
)
1644 struct tevent_req
*req
=
1645 tevent_req_callback_data(subreq
,
1648 tevent_queue_wait_recv(subreq
);
1649 TALLOC_FREE(subreq
);
1651 tevent_req_done(req
);
1654 NTSTATUS
smb2srv_session_shutdown_recv(struct tevent_req
*req
)
1656 return tevent_req_simple_recv_ntstatus(req
);
1659 NTSTATUS
smbXsrv_session_logoff(struct smbXsrv_session
*session
)
1661 struct smbXsrv_session_table
*table
;
1662 struct db_record
*local_rec
= NULL
;
1663 struct db_record
*global_rec
= NULL
;
1664 struct smbd_server_connection
*sconn
= NULL
;
1666 NTSTATUS error
= NT_STATUS_OK
;
1668 if (session
->table
== NULL
) {
1669 return NT_STATUS_OK
;
1672 table
= session
->table
;
1673 session
->table
= NULL
;
1675 sconn
= session
->client
->sconn
;
1676 session
->client
= NULL
;
1677 session
->status
= NT_STATUS_USER_SESSION_DELETED
;
1680 * For SMB2 this is a bit redundant as files are also close
1681 * below via smb2srv_tcon_disconnect_all() -> ... ->
1682 * smbXsrv_tcon_disconnect() -> close_cnum() ->
1683 * file_close_conn().
1685 file_close_user(sconn
, session
->global
->session_wire_id
);
1687 if (session
->tcon_table
!= NULL
) {
1689 * Note: We only have a tcon_table for SMB2.
1691 status
= smb2srv_tcon_disconnect_all(session
);
1692 if (!NT_STATUS_IS_OK(status
)) {
1693 DEBUG(0, ("smbXsrv_session_logoff(0x%08x): "
1694 "smb2srv_tcon_disconnect_all() failed: %s\n",
1695 session
->global
->session_global_id
,
1696 nt_errstr(status
)));
1701 invalidate_vuid(sconn
, session
->global
->session_wire_id
);
1703 global_rec
= session
->global
->db_rec
;
1704 session
->global
->db_rec
= NULL
;
1705 if (global_rec
== NULL
) {
1706 global_rec
= smbXsrv_session_global_fetch_locked(
1707 table
->global
.db_ctx
,
1708 session
->global
->session_global_id
,
1709 session
->global
/* TALLOC_CTX */);
1710 if (global_rec
== NULL
) {
1711 error
= NT_STATUS_INTERNAL_ERROR
;
1715 if (global_rec
!= NULL
) {
1716 status
= dbwrap_record_delete(global_rec
);
1717 if (!NT_STATUS_IS_OK(status
)) {
1718 TDB_DATA key
= dbwrap_record_get_key(global_rec
);
1720 DEBUG(0, ("smbXsrv_session_logoff(0x%08x): "
1721 "failed to delete global key '%s': %s\n",
1722 session
->global
->session_global_id
,
1723 hex_encode_talloc(global_rec
, key
.dptr
,
1725 nt_errstr(status
)));
1729 TALLOC_FREE(global_rec
);
1731 local_rec
= session
->db_rec
;
1732 if (local_rec
== NULL
) {
1733 local_rec
= smbXsrv_session_local_fetch_locked(
1734 table
->local
.db_ctx
,
1736 session
/* TALLOC_CTX */);
1737 if (local_rec
== NULL
) {
1738 error
= NT_STATUS_INTERNAL_ERROR
;
1742 if (local_rec
!= NULL
) {
1743 status
= dbwrap_record_delete(local_rec
);
1744 if (!NT_STATUS_IS_OK(status
)) {
1745 TDB_DATA key
= dbwrap_record_get_key(local_rec
);
1747 DEBUG(0, ("smbXsrv_session_logoff(0x%08x): "
1748 "failed to delete local key '%s': %s\n",
1749 session
->global
->session_global_id
,
1750 hex_encode_talloc(local_rec
, key
.dptr
,
1752 nt_errstr(status
)));
1755 table
->local
.num_sessions
-= 1;
1757 if (session
->db_rec
== NULL
) {
1758 TALLOC_FREE(local_rec
);
1760 session
->db_rec
= NULL
;
1765 struct smbXsrv_session_logoff_all_state
{
1766 NTSTATUS first_status
;
1770 static int smbXsrv_session_logoff_all_callback(struct db_record
*local_rec
,
1771 void *private_data
);
1773 NTSTATUS
smbXsrv_session_logoff_all(struct smbXsrv_client
*client
)
1775 struct smbXsrv_session_table
*table
= client
->session_table
;
1776 struct smbXsrv_session_logoff_all_state state
;
1780 if (table
== NULL
) {
1781 DEBUG(10, ("smbXsrv_session_logoff_all: "
1782 "empty session_table, nothing to do.\n"));
1783 return NT_STATUS_OK
;
1788 status
= dbwrap_traverse(table
->local
.db_ctx
,
1789 smbXsrv_session_logoff_all_callback
,
1791 if (!NT_STATUS_IS_OK(status
)) {
1792 DEBUG(0, ("smbXsrv_session_logoff_all: "
1793 "dbwrap_traverse() failed: %s\n",
1794 nt_errstr(status
)));
1798 if (!NT_STATUS_IS_OK(state
.first_status
)) {
1799 DEBUG(0, ("smbXsrv_session_logoff_all: "
1800 "count[%d] errors[%d] first[%s]\n",
1801 count
, state
.errors
,
1802 nt_errstr(state
.first_status
)));
1803 return state
.first_status
;
1806 return NT_STATUS_OK
;
1809 static int smbXsrv_session_logoff_all_callback(struct db_record
*local_rec
,
1812 struct smbXsrv_session_logoff_all_state
*state
=
1813 (struct smbXsrv_session_logoff_all_state
*)private_data
;
1816 struct smbXsrv_session
*session
= NULL
;
1819 val
= dbwrap_record_get_value(local_rec
);
1820 if (val
.dsize
!= sizeof(ptr
)) {
1821 status
= NT_STATUS_INTERNAL_ERROR
;
1822 if (NT_STATUS_IS_OK(state
->first_status
)) {
1823 state
->first_status
= status
;
1829 memcpy(&ptr
, val
.dptr
, val
.dsize
);
1830 session
= talloc_get_type_abort(ptr
, struct smbXsrv_session
);
1832 session
->db_rec
= local_rec
;
1834 status
= smbXsrv_session_clear_and_logoff(session
);
1835 if (!NT_STATUS_IS_OK(status
)) {
1836 if (NT_STATUS_IS_OK(state
->first_status
)) {
1837 state
->first_status
= status
;
1846 struct smbXsrv_session_local_trav_state
{
1848 int (*caller_cb
)(struct smbXsrv_session
*session
,
1853 static int smbXsrv_session_local_traverse_cb(struct db_record
*local_rec
,
1854 void *private_data
);
1856 NTSTATUS
smbXsrv_session_local_traverse(
1857 struct smbXsrv_client
*client
,
1858 int (*caller_cb
)(struct smbXsrv_session
*session
,
1862 struct smbXsrv_session_table
*table
= client
->session_table
;
1863 struct smbXsrv_session_local_trav_state state
;
1867 state
= (struct smbXsrv_session_local_trav_state
) {
1868 .status
= NT_STATUS_OK
,
1869 .caller_cb
= caller_cb
,
1870 .caller_data
= caller_data
,
1873 if (table
== NULL
) {
1874 DBG_DEBUG("empty session_table, nothing to do.\n");
1875 return NT_STATUS_OK
;
1878 status
= dbwrap_traverse(table
->local
.db_ctx
,
1879 smbXsrv_session_local_traverse_cb
,
1882 if (!NT_STATUS_IS_OK(status
)) {
1883 DBG_ERR("dbwrap_traverse() failed: %s\n", nt_errstr(status
));
1886 if (!NT_STATUS_IS_OK(state
.status
)) {
1887 DBG_ERR("count[%d] status[%s]\n",
1888 count
, nt_errstr(state
.status
));
1889 return state
.status
;
1892 return NT_STATUS_OK
;
1895 static int smbXsrv_session_local_traverse_cb(struct db_record
*local_rec
,
1898 struct smbXsrv_session_local_trav_state
*state
=
1899 (struct smbXsrv_session_local_trav_state
*)private_data
;
1902 struct smbXsrv_session
*session
= NULL
;
1904 val
= dbwrap_record_get_value(local_rec
);
1905 if (val
.dsize
!= sizeof(ptr
)) {
1906 state
->status
= NT_STATUS_INTERNAL_ERROR
;
1910 memcpy(&ptr
, val
.dptr
, val
.dsize
);
1911 session
= talloc_get_type_abort(ptr
, struct smbXsrv_session
);
1912 session
->db_rec
= local_rec
;
1914 return state
->caller_cb(session
, state
->caller_data
);
1917 struct smbXsrv_session_disconnect_xconn_state
{
1918 struct smbXsrv_connection
*xconn
;
1919 NTSTATUS first_status
;
1923 static int smbXsrv_session_disconnect_xconn_callback(struct db_record
*local_rec
,
1924 void *private_data
);
1926 NTSTATUS
smbXsrv_session_disconnect_xconn(struct smbXsrv_connection
*xconn
)
1928 struct smbXsrv_client
*client
= xconn
->client
;
1929 struct smbXsrv_session_table
*table
= client
->session_table
;
1930 struct smbXsrv_session_disconnect_xconn_state state
;
1934 if (table
== NULL
) {
1935 DBG_ERR("empty session_table, nothing to do.\n");
1936 return NT_STATUS_OK
;
1940 state
.xconn
= xconn
;
1942 status
= dbwrap_traverse(table
->local
.db_ctx
,
1943 smbXsrv_session_disconnect_xconn_callback
,
1945 if (!NT_STATUS_IS_OK(status
)) {
1946 DBG_ERR("dbwrap_traverse() failed: %s\n",
1951 if (!NT_STATUS_IS_OK(state
.first_status
)) {
1952 DBG_ERR("count[%d] errors[%d] first[%s]\n",
1953 count
, state
.errors
,
1954 nt_errstr(state
.first_status
));
1955 return state
.first_status
;
1958 return NT_STATUS_OK
;
1961 static int smbXsrv_session_disconnect_xconn_callback(struct db_record
*local_rec
,
1964 struct smbXsrv_session_disconnect_xconn_state
*state
=
1965 (struct smbXsrv_session_disconnect_xconn_state
*)private_data
;
1968 struct smbXsrv_session
*session
= NULL
;
1969 struct smbXsrv_session_auth0
*a
= NULL
;
1970 struct smbXsrv_channel_global0
*c
= NULL
;
1972 bool need_update
= false;
1974 val
= dbwrap_record_get_value(local_rec
);
1975 if (val
.dsize
!= sizeof(ptr
)) {
1976 status
= NT_STATUS_INTERNAL_ERROR
;
1977 if (NT_STATUS_IS_OK(state
->first_status
)) {
1978 state
->first_status
= status
;
1984 memcpy(&ptr
, val
.dptr
, val
.dsize
);
1985 session
= talloc_get_type_abort(ptr
, struct smbXsrv_session
);
1987 session
->db_rec
= local_rec
;
1989 status
= smbXsrv_session_find_auth(session
, state
->xconn
, 0, &a
);
1990 if (!NT_STATUS_IS_OK(status
)) {
1993 status
= smbXsrv_session_find_channel(session
, state
->xconn
, &c
);
1994 if (!NT_STATUS_IS_OK(status
)) {
1997 if (session
->global
->num_channels
<= 1) {
1999 * The last channel is treated different
2005 smbXsrv_session_auth0_destructor(a
);
2006 a
->connection
= NULL
;
2011 struct smbXsrv_session_global0
*global
= session
->global
;
2014 n
= (c
- global
->channels
);
2015 if (n
>= global
->num_channels
|| n
< 0) {
2016 status
= NT_STATUS_INTERNAL_ERROR
;
2017 if (NT_STATUS_IS_OK(state
->first_status
)) {
2018 state
->first_status
= status
;
2023 ARRAY_DEL_ELEMENT(global
->channels
, n
, global
->num_channels
);
2024 global
->num_channels
--;
2029 status
= smbXsrv_session_update(session
);
2030 if (NT_STATUS_IS_OK(state
->first_status
)) {
2031 state
->first_status
= status
;
2039 NTSTATUS
smb1srv_session_table_init(struct smbXsrv_connection
*conn
)
2042 * Allow a range from 1..65534 with 65534 values.
2044 return smbXsrv_session_table_init(conn
, 1, UINT16_MAX
- 1,
2048 NTSTATUS
smb1srv_session_lookup(struct smbXsrv_connection
*conn
,
2049 uint16_t vuid
, NTTIME now
,
2050 struct smbXsrv_session
**session
)
2052 struct smbXsrv_session_table
*table
= conn
->client
->session_table
;
2053 uint32_t local_id
= vuid
;
2055 return smbXsrv_session_local_lookup(table
, conn
, local_id
, now
,
2059 NTSTATUS
smbXsrv_session_info_lookup(struct smbXsrv_client
*client
,
2060 uint64_t session_wire_id
,
2061 struct auth_session_info
**si
)
2063 struct smbXsrv_session_table
*table
= client
->session_table
;
2064 uint8_t key_buf
[SMBXSRV_SESSION_LOCAL_TDB_KEY_SIZE
];
2065 struct smbXsrv_session_local_fetch_state state
= {
2067 .status
= NT_STATUS_INTERNAL_ERROR
,
2072 if (session_wire_id
== 0) {
2073 return NT_STATUS_USER_SESSION_DELETED
;
2076 if (table
== NULL
) {
2077 /* this might happen before the end of negprot */
2078 return NT_STATUS_USER_SESSION_DELETED
;
2081 if (table
->local
.db_ctx
== NULL
) {
2082 return NT_STATUS_INTERNAL_ERROR
;
2085 key
= smbXsrv_session_local_id_to_key(session_wire_id
, key_buf
);
2087 status
= dbwrap_parse_record(table
->local
.db_ctx
, key
,
2088 smbXsrv_session_local_fetch_parser
,
2090 if (!NT_STATUS_IS_OK(status
)) {
2093 if (!NT_STATUS_IS_OK(state
.status
)) {
2094 return state
.status
;
2096 if (state
.session
->global
->auth_session_info
== NULL
) {
2097 return NT_STATUS_USER_SESSION_DELETED
;
2100 *si
= state
.session
->global
->auth_session_info
;
2101 return NT_STATUS_OK
;
2105 * In memory of get_valid_user_struct()
2107 * This function is similar to smbXsrv_session_local_lookup() and it's wrappers,
2108 * but it doesn't implement the state checks of
2109 * those. get_valid_smbXsrv_session() is NOT meant to be called to validate the
2110 * session wire-id of incoming SMB requests, it MUST only be used in later
2111 * internal processing where the session wire-id has already been validated.
2113 NTSTATUS
get_valid_smbXsrv_session(struct smbXsrv_client
*client
,
2114 uint64_t session_wire_id
,
2115 struct smbXsrv_session
**session
)
2117 struct smbXsrv_session_table
*table
= client
->session_table
;
2118 uint8_t key_buf
[SMBXSRV_SESSION_LOCAL_TDB_KEY_SIZE
];
2119 struct smbXsrv_session_local_fetch_state state
= {
2121 .status
= NT_STATUS_INTERNAL_ERROR
,
2126 if (session_wire_id
== 0) {
2127 return NT_STATUS_USER_SESSION_DELETED
;
2130 if (table
== NULL
) {
2131 /* this might happen before the end of negprot */
2132 return NT_STATUS_USER_SESSION_DELETED
;
2135 if (table
->local
.db_ctx
== NULL
) {
2136 return NT_STATUS_INTERNAL_ERROR
;
2139 key
= smbXsrv_session_local_id_to_key(session_wire_id
, key_buf
);
2141 status
= dbwrap_parse_record(table
->local
.db_ctx
, key
,
2142 smbXsrv_session_local_fetch_parser
,
2144 if (!NT_STATUS_IS_OK(status
)) {
2147 if (!NT_STATUS_IS_OK(state
.status
)) {
2148 return state
.status
;
2150 if (state
.session
->global
->auth_session_info
== NULL
) {
2151 return NT_STATUS_USER_SESSION_DELETED
;
2154 *session
= state
.session
;
2155 return NT_STATUS_OK
;
2158 NTSTATUS
smb2srv_session_table_init(struct smbXsrv_connection
*conn
)
2161 * Allow a range from 1..4294967294 with 65534 (same as SMB1) values.
2163 return smbXsrv_session_table_init(conn
, 1, UINT32_MAX
- 1,
2167 static NTSTATUS
smb2srv_session_lookup_raw(struct smbXsrv_session_table
*table
,
2168 /* conn: optional */
2169 struct smbXsrv_connection
*conn
,
2170 uint64_t session_id
, NTTIME now
,
2171 struct smbXsrv_session
**session
)
2173 uint32_t local_id
= session_id
& UINT32_MAX
;
2174 uint64_t local_zeros
= session_id
& 0xFFFFFFFF00000000LLU
;
2176 if (local_zeros
!= 0) {
2177 return NT_STATUS_USER_SESSION_DELETED
;
2180 return smbXsrv_session_local_lookup(table
, conn
, local_id
, now
,
2184 NTSTATUS
smb2srv_session_lookup_conn(struct smbXsrv_connection
*conn
,
2185 uint64_t session_id
, NTTIME now
,
2186 struct smbXsrv_session
**session
)
2188 struct smbXsrv_session_table
*table
= conn
->client
->session_table
;
2189 return smb2srv_session_lookup_raw(table
, conn
, session_id
, now
,
2193 NTSTATUS
smb2srv_session_lookup_client(struct smbXsrv_client
*client
,
2194 uint64_t session_id
, NTTIME now
,
2195 struct smbXsrv_session
**session
)
2197 struct smbXsrv_session_table
*table
= client
->session_table
;
2198 return smb2srv_session_lookup_raw(table
, NULL
, session_id
, now
,
2202 struct smbXsrv_session_global_traverse_state
{
2203 int (*fn
)(struct smbXsrv_session_global0
*, void *);
2207 static int smbXsrv_session_global_traverse_fn(struct db_record
*rec
, void *data
)
2210 struct smbXsrv_session_global_traverse_state
*state
=
2211 (struct smbXsrv_session_global_traverse_state
*)data
;
2212 TDB_DATA key
= dbwrap_record_get_key(rec
);
2213 TDB_DATA val
= dbwrap_record_get_value(rec
);
2214 DATA_BLOB blob
= data_blob_const(val
.dptr
, val
.dsize
);
2215 struct smbXsrv_session_globalB global_blob
;
2216 enum ndr_err_code ndr_err
;
2217 TALLOC_CTX
*frame
= talloc_stackframe();
2219 ndr_err
= ndr_pull_struct_blob(&blob
, frame
, &global_blob
,
2220 (ndr_pull_flags_fn_t
)ndr_pull_smbXsrv_session_globalB
);
2221 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
2222 DEBUG(1,("Invalid record in smbXsrv_session_global.tdb:"
2223 "key '%s' ndr_pull_struct_blob - %s\n",
2224 hex_encode_talloc(frame
, key
.dptr
, key
.dsize
),
2225 ndr_errstr(ndr_err
)));
2229 if (global_blob
.version
!= SMBXSRV_VERSION_0
) {
2230 DEBUG(1,("Invalid record in smbXsrv_session_global.tdb:"
2231 "key '%s' unsupported version - %d\n",
2232 hex_encode_talloc(frame
, key
.dptr
, key
.dsize
),
2233 (int)global_blob
.version
));
2237 if (global_blob
.info
.info0
== NULL
) {
2238 DEBUG(1,("Invalid record in smbXsrv_tcon_global.tdb:"
2239 "key '%s' info0 NULL pointer\n",
2240 hex_encode_talloc(frame
, key
.dptr
, key
.dsize
)));
2244 global_blob
.info
.info0
->db_rec
= rec
;
2245 ret
= state
->fn(global_blob
.info
.info0
, state
->private_data
);
2251 NTSTATUS
smbXsrv_session_global_traverse(
2252 int (*fn
)(struct smbXsrv_session_global0
*, void *),
2258 struct smbXsrv_session_global_traverse_state state
= {
2260 .private_data
= private_data
,
2264 status
= smbXsrv_session_global_init(NULL
);
2265 if (!NT_STATUS_IS_OK(status
)) {
2267 DEBUG(0, ("Failed to initialize session_global: %s\n",
2268 nt_errstr(status
)));
2272 status
= dbwrap_traverse_read(smbXsrv_session_global_db_ctx
,
2273 smbXsrv_session_global_traverse_fn
,