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 "smbd/smbd.h"
25 #include "smbd/globals.h"
26 #include "dbwrap/dbwrap.h"
27 #include "dbwrap/dbwrap_rbt.h"
28 #include "dbwrap/dbwrap_open.h"
29 #include "dbwrap/dbwrap_watch.h"
32 #include "auth/gensec/gensec.h"
33 #include "../lib/tsocket/tsocket.h"
34 #include "../libcli/security/security.h"
36 #include "lib/util/util_tdb.h"
37 #include "librpc/gen_ndr/ndr_smbXsrv.h"
39 #include "lib/util/tevent_ntstatus.h"
41 struct smbXsrv_session_table
{
43 struct db_context
*db_ctx
;
46 uint32_t max_sessions
;
47 uint32_t num_sessions
;
50 struct db_context
*db_ctx
;
54 static struct db_context
*smbXsrv_session_global_db_ctx
= NULL
;
56 NTSTATUS
smbXsrv_session_global_init(void)
58 char *global_path
= NULL
;
59 struct db_context
*db_ctx
= NULL
;
61 if (smbXsrv_session_global_db_ctx
!= NULL
) {
66 * This contains secret information like session keys!
68 global_path
= lock_path("smbXsrv_session_global.tdb");
69 if (global_path
== NULL
) {
70 return NT_STATUS_NO_MEMORY
;
73 db_ctx
= db_open(NULL
, global_path
,
77 TDB_INCOMPATIBLE_HASH
,
78 O_RDWR
| O_CREAT
, 0600,
81 TALLOC_FREE(global_path
);
85 status
= map_nt_error_from_unix_common(errno
);
90 smbXsrv_session_global_db_ctx
= db_ctx
;
97 * We need to store the keys in big endian so that dbwrap_rbt's memcmp
98 * has the same result as integer comparison between the uint32_t
101 * TODO: implement string based key
104 #define SMBXSRV_SESSION_GLOBAL_TDB_KEY_SIZE sizeof(uint32_t)
106 static TDB_DATA
smbXsrv_session_global_id_to_key(uint32_t id
,
111 RSIVAL(key_buf
, 0, id
);
113 key
= make_tdb_data(key_buf
, SMBXSRV_SESSION_GLOBAL_TDB_KEY_SIZE
);
119 static NTSTATUS
smbXsrv_session_global_key_to_id(TDB_DATA key
, uint32_t *id
)
122 return NT_STATUS_INVALID_PARAMETER
;
125 if (key
.dsize
!= SMBXSRV_SESSION_GLOBAL_TDB_KEY_SIZE
) {
126 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
129 *id
= RIVAL(key
.dptr
, 0);
135 #define SMBXSRV_SESSION_LOCAL_TDB_KEY_SIZE sizeof(uint32_t)
137 static TDB_DATA
smbXsrv_session_local_id_to_key(uint32_t id
,
142 RSIVAL(key_buf
, 0, id
);
144 key
= make_tdb_data(key_buf
, SMBXSRV_SESSION_LOCAL_TDB_KEY_SIZE
);
149 static NTSTATUS
smbXsrv_session_local_key_to_id(TDB_DATA key
, uint32_t *id
)
152 return NT_STATUS_INVALID_PARAMETER
;
155 if (key
.dsize
!= SMBXSRV_SESSION_LOCAL_TDB_KEY_SIZE
) {
156 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
159 *id
= RIVAL(key
.dptr
, 0);
164 static void smbXsrv_session_close_loop(struct tevent_req
*subreq
);
166 static NTSTATUS
smbXsrv_session_table_init(struct smbXsrv_connection
*conn
,
169 uint32_t max_sessions
)
171 struct smbXsrv_client
*client
= conn
->client
;
172 struct smbXsrv_session_table
*table
;
174 struct tevent_req
*subreq
;
177 if (lowest_id
> highest_id
) {
178 return NT_STATUS_INTERNAL_ERROR
;
181 max_range
= highest_id
;
182 max_range
-= lowest_id
;
185 if (max_sessions
> max_range
) {
186 return NT_STATUS_INTERNAL_ERROR
;
189 table
= talloc_zero(client
, struct smbXsrv_session_table
);
191 return NT_STATUS_NO_MEMORY
;
194 table
->local
.db_ctx
= db_open_rbt(table
);
195 if (table
->local
.db_ctx
== NULL
) {
197 return NT_STATUS_NO_MEMORY
;
199 table
->local
.lowest_id
= lowest_id
;
200 table
->local
.highest_id
= highest_id
;
201 table
->local
.max_sessions
= max_sessions
;
203 status
= smbXsrv_session_global_init();
204 if (!NT_STATUS_IS_OK(status
)) {
209 table
->global
.db_ctx
= smbXsrv_session_global_db_ctx
;
211 dbwrap_watch_db(table
->global
.db_ctx
, client
->msg_ctx
);
213 subreq
= messaging_read_send(table
, client
->ev_ctx
, client
->msg_ctx
,
214 MSG_SMBXSRV_SESSION_CLOSE
);
215 if (subreq
== NULL
) {
217 return NT_STATUS_NO_MEMORY
;
219 tevent_req_set_callback(subreq
, smbXsrv_session_close_loop
, client
);
221 client
->session_table
= table
;
225 static void smbXsrv_session_close_shutdown_done(struct tevent_req
*subreq
);
227 static void smbXsrv_session_close_loop(struct tevent_req
*subreq
)
229 struct smbXsrv_client
*client
=
230 tevent_req_callback_data(subreq
,
231 struct smbXsrv_client
);
232 struct smbXsrv_session_table
*table
= client
->session_table
;
234 struct messaging_rec
*rec
= NULL
;
235 struct smbXsrv_session_closeB close_blob
;
236 enum ndr_err_code ndr_err
;
237 struct smbXsrv_session_close0
*close_info0
= NULL
;
238 struct smbXsrv_session
*session
= NULL
;
240 struct timeval tv
= timeval_current();
241 NTTIME now
= timeval_to_nttime(&tv
);
243 ret
= messaging_read_recv(subreq
, talloc_tos(), &rec
);
249 ndr_err
= ndr_pull_struct_blob(&rec
->buf
, rec
, &close_blob
,
250 (ndr_pull_flags_fn_t
)ndr_pull_smbXsrv_session_closeB
);
251 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
252 status
= ndr_map_error2ntstatus(ndr_err
);
253 DEBUG(1,("smbXsrv_session_close_loop: "
254 "ndr_pull_struct_blob - %s\n",
259 DEBUG(10,("smbXsrv_session_close_loop: MSG_SMBXSRV_SESSION_CLOSE\n"));
261 NDR_PRINT_DEBUG(smbXsrv_session_closeB
, &close_blob
);
264 if (close_blob
.version
!= SMBXSRV_VERSION_0
) {
265 DEBUG(0,("smbXsrv_session_close_loop: "
266 "ignore invalid version %u\n", close_blob
.version
));
267 NDR_PRINT_DEBUG(smbXsrv_session_closeB
, &close_blob
);
271 close_info0
= close_blob
.info
.info0
;
272 if (close_info0
== NULL
) {
273 DEBUG(0,("smbXsrv_session_close_loop: "
274 "ignore NULL info %u\n", close_blob
.version
));
275 NDR_PRINT_DEBUG(smbXsrv_session_closeB
, &close_blob
);
279 status
= smb2srv_session_lookup_client(client
,
280 close_info0
->old_session_wire_id
,
282 if (NT_STATUS_EQUAL(status
, NT_STATUS_USER_SESSION_DELETED
)) {
283 DEBUG(4,("smbXsrv_session_close_loop: "
284 "old_session_wire_id %llu not found\n",
285 (unsigned long long)close_info0
->old_session_wire_id
));
287 NDR_PRINT_DEBUG(smbXsrv_session_closeB
, &close_blob
);
291 if (!NT_STATUS_IS_OK(status
) &&
292 !NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
) &&
293 !NT_STATUS_EQUAL(status
, NT_STATUS_NETWORK_SESSION_EXPIRED
)) {
294 DEBUG(1,("smbXsrv_session_close_loop: "
295 "old_session_wire_id %llu - %s\n",
296 (unsigned long long)close_info0
->old_session_wire_id
,
299 NDR_PRINT_DEBUG(smbXsrv_session_closeB
, &close_blob
);
304 if (session
->global
->session_global_id
!= close_info0
->old_session_global_id
) {
305 DEBUG(1,("smbXsrv_session_close_loop: "
306 "old_session_wire_id %llu - global %u != %u\n",
307 (unsigned long long)close_info0
->old_session_wire_id
,
308 session
->global
->session_global_id
,
309 close_info0
->old_session_global_id
));
311 NDR_PRINT_DEBUG(smbXsrv_session_closeB
, &close_blob
);
316 if (session
->global
->creation_time
!= close_info0
->old_creation_time
) {
317 DEBUG(1,("smbXsrv_session_close_loop: "
318 "old_session_wire_id %llu - "
319 "creation %s (%llu) != %s (%llu)\n",
320 (unsigned long long)close_info0
->old_session_wire_id
,
321 nt_time_string(rec
, session
->global
->creation_time
),
322 (unsigned long long)session
->global
->creation_time
,
323 nt_time_string(rec
, close_info0
->old_creation_time
),
324 (unsigned long long)close_info0
->old_creation_time
));
326 NDR_PRINT_DEBUG(smbXsrv_session_closeB
, &close_blob
);
331 subreq
= smb2srv_session_shutdown_send(session
, client
->ev_ctx
,
333 if (subreq
== NULL
) {
334 status
= NT_STATUS_NO_MEMORY
;
335 DEBUG(0, ("smbXsrv_session_close_loop: "
336 "smb2srv_session_shutdown_send(%llu) failed: %s\n",
337 (unsigned long long)session
->global
->session_wire_id
,
340 NDR_PRINT_DEBUG(smbXsrv_session_closeB
, &close_blob
);
344 tevent_req_set_callback(subreq
,
345 smbXsrv_session_close_shutdown_done
,
351 subreq
= messaging_read_send(table
, client
->ev_ctx
, client
->msg_ctx
,
352 MSG_SMBXSRV_SESSION_CLOSE
);
353 if (subreq
== NULL
) {
355 r
= "messaging_read_send(MSG_SMBXSRV_SESSION_CLOSE) failed";
356 exit_server_cleanly(r
);
359 tevent_req_set_callback(subreq
, smbXsrv_session_close_loop
, client
);
362 static void smbXsrv_session_close_shutdown_done(struct tevent_req
*subreq
)
364 struct smbXsrv_session
*session
=
365 tevent_req_callback_data(subreq
,
366 struct smbXsrv_session
);
369 status
= smb2srv_session_shutdown_recv(subreq
);
371 if (!NT_STATUS_IS_OK(status
)) {
372 DEBUG(0, ("smbXsrv_session_close_loop: "
373 "smb2srv_session_shutdown_recv(%llu) failed: %s\n",
374 (unsigned long long)session
->global
->session_wire_id
,
378 status
= smbXsrv_session_logoff(session
);
379 if (!NT_STATUS_IS_OK(status
)) {
380 DEBUG(0, ("smbXsrv_session_close_loop: "
381 "smbXsrv_session_logoff(%llu) failed: %s\n",
382 (unsigned long long)session
->global
->session_wire_id
,
386 TALLOC_FREE(session
);
389 struct smb1srv_session_local_allocate_state
{
390 const uint32_t lowest_id
;
391 const uint32_t highest_id
;
397 static int smb1srv_session_local_allocate_traverse(struct db_record
*rec
,
400 struct smb1srv_session_local_allocate_state
*state
=
401 (struct smb1srv_session_local_allocate_state
*)private_data
;
402 TDB_DATA key
= dbwrap_record_get_key(rec
);
406 status
= smbXsrv_session_local_key_to_id(key
, &id
);
407 if (!NT_STATUS_IS_OK(status
)) {
408 state
->status
= status
;
412 if (id
<= state
->last_id
) {
413 state
->status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
418 if (id
> state
->useable_id
) {
419 state
->status
= NT_STATUS_OK
;
423 if (state
->useable_id
== state
->highest_id
) {
424 state
->status
= NT_STATUS_INSUFFICIENT_RESOURCES
;
428 state
->useable_id
+=1;
432 static NTSTATUS
smb1srv_session_local_allocate_id(struct db_context
*db
,
436 struct db_record
**_rec
,
439 struct smb1srv_session_local_allocate_state state
= {
440 .lowest_id
= lowest_id
,
441 .highest_id
= highest_id
,
443 .useable_id
= lowest_id
,
444 .status
= NT_STATUS_INTERNAL_ERROR
,
454 if (lowest_id
> highest_id
) {
455 return NT_STATUS_INSUFFICIENT_RESOURCES
;
459 * first we try randomly
461 range
= (highest_id
- lowest_id
) + 1;
463 for (i
= 0; i
< (range
/ 2); i
++) {
465 uint8_t key_buf
[SMBXSRV_SESSION_LOCAL_TDB_KEY_SIZE
];
468 struct db_record
*rec
= NULL
;
470 id
= generate_random() % range
;
473 if (id
< lowest_id
) {
476 if (id
> highest_id
) {
480 key
= smbXsrv_session_local_id_to_key(id
, key_buf
);
482 rec
= dbwrap_fetch_locked(db
, mem_ctx
, key
);
484 return NT_STATUS_INSUFFICIENT_RESOURCES
;
487 val
= dbwrap_record_get_value(rec
);
488 if (val
.dsize
!= 0) {
499 * if the range is almost full,
500 * we traverse the whole table
501 * (this relies on sorted behavior of dbwrap_rbt)
503 status
= dbwrap_traverse_read(db
, smb1srv_session_local_allocate_traverse
,
505 if (NT_STATUS_IS_OK(status
)) {
506 if (NT_STATUS_IS_OK(state
.status
)) {
507 return NT_STATUS_INTERNAL_ERROR
;
510 if (!NT_STATUS_EQUAL(state
.status
, NT_STATUS_INTERNAL_ERROR
)) {
514 if (state
.useable_id
<= state
.highest_id
) {
515 state
.status
= NT_STATUS_OK
;
517 return NT_STATUS_INSUFFICIENT_RESOURCES
;
519 } else if (!NT_STATUS_EQUAL(status
, NT_STATUS_INTERNAL_DB_CORRUPTION
)) {
521 * Here we really expect NT_STATUS_INTERNAL_DB_CORRUPTION!
523 * If we get anything else it is an error, because it
524 * means we did not manage to find a free slot in
527 return NT_STATUS_INSUFFICIENT_RESOURCES
;
530 if (NT_STATUS_IS_OK(state
.status
)) {
532 uint8_t key_buf
[SMBXSRV_SESSION_LOCAL_TDB_KEY_SIZE
];
535 struct db_record
*rec
= NULL
;
537 id
= state
.useable_id
;
539 key
= smbXsrv_session_local_id_to_key(id
, key_buf
);
541 rec
= dbwrap_fetch_locked(db
, mem_ctx
, key
);
543 return NT_STATUS_INSUFFICIENT_RESOURCES
;
546 val
= dbwrap_record_get_value(rec
);
547 if (val
.dsize
!= 0) {
549 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
560 struct smbXsrv_session_local_fetch_state
{
561 struct smbXsrv_session
*session
;
565 static void smbXsrv_session_local_fetch_parser(TDB_DATA key
, TDB_DATA data
,
568 struct smbXsrv_session_local_fetch_state
*state
=
569 (struct smbXsrv_session_local_fetch_state
*)private_data
;
572 if (data
.dsize
!= sizeof(ptr
)) {
573 state
->status
= NT_STATUS_INTERNAL_DB_ERROR
;
577 memcpy(&ptr
, data
.dptr
, data
.dsize
);
578 state
->session
= talloc_get_type_abort(ptr
, struct smbXsrv_session
);
579 state
->status
= NT_STATUS_OK
;
582 static NTSTATUS
smbXsrv_session_local_lookup(struct smbXsrv_session_table
*table
,
584 struct smbXsrv_connection
*conn
,
585 uint32_t session_local_id
,
587 struct smbXsrv_session
**_session
)
589 struct smbXsrv_session_local_fetch_state state
= {
591 .status
= NT_STATUS_INTERNAL_ERROR
,
593 uint8_t key_buf
[SMBXSRV_SESSION_LOCAL_TDB_KEY_SIZE
];
599 if (session_local_id
== 0) {
600 return NT_STATUS_USER_SESSION_DELETED
;
604 /* this might happen before the end of negprot */
605 return NT_STATUS_USER_SESSION_DELETED
;
608 if (table
->local
.db_ctx
== NULL
) {
609 return NT_STATUS_INTERNAL_ERROR
;
612 key
= smbXsrv_session_local_id_to_key(session_local_id
, key_buf
);
614 status
= dbwrap_parse_record(table
->local
.db_ctx
, key
,
615 smbXsrv_session_local_fetch_parser
,
617 if (NT_STATUS_EQUAL(status
, NT_STATUS_NOT_FOUND
)) {
618 return NT_STATUS_USER_SESSION_DELETED
;
619 } else if (!NT_STATUS_IS_OK(status
)) {
622 if (!NT_STATUS_IS_OK(state
.status
)) {
626 if (NT_STATUS_EQUAL(state
.session
->status
, NT_STATUS_USER_SESSION_DELETED
)) {
627 return NT_STATUS_USER_SESSION_DELETED
;
631 * If a connection is specified check if the session is
632 * valid on the channel.
635 struct smbXsrv_channel_global0
*c
= NULL
;
637 status
= smbXsrv_session_find_channel(state
.session
, conn
, &c
);
638 if (!NT_STATUS_IS_OK(status
)) {
643 state
.session
->idle_time
= now
;
645 if (!NT_STATUS_IS_OK(state
.session
->status
)) {
646 *_session
= state
.session
;
647 return state
.session
->status
;
650 if (now
> state
.session
->global
->expiration_time
) {
651 state
.session
->status
= NT_STATUS_NETWORK_SESSION_EXPIRED
;
654 *_session
= state
.session
;
655 return state
.session
->status
;
658 static int smbXsrv_session_global_destructor(struct smbXsrv_session_global0
*global
)
663 static void smbXsrv_session_global_verify_record(struct db_record
*db_rec
,
667 struct smbXsrv_session_global0
**_g
);
669 static NTSTATUS
smbXsrv_session_global_allocate(struct db_context
*db
,
671 struct smbXsrv_session_global0
**_global
)
674 struct smbXsrv_session_global0
*global
= NULL
;
675 uint32_t last_free
= 0;
676 const uint32_t min_tries
= 3;
680 global
= talloc_zero(mem_ctx
, struct smbXsrv_session_global0
);
681 if (global
== NULL
) {
682 return NT_STATUS_NO_MEMORY
;
684 talloc_set_destructor(global
, smbXsrv_session_global_destructor
);
687 * Here we just randomly try the whole 32-bit space
689 * We use just 32-bit, because we want to reuse the
692 for (i
= 0; i
< UINT32_MAX
; i
++) {
693 bool is_free
= false;
694 bool was_free
= false;
696 uint8_t key_buf
[SMBXSRV_SESSION_GLOBAL_TDB_KEY_SIZE
];
699 if (i
>= min_tries
&& last_free
!= 0) {
702 id
= generate_random();
707 if (id
== UINT32_MAX
) {
711 key
= smbXsrv_session_global_id_to_key(id
, key_buf
);
713 global
->db_rec
= dbwrap_fetch_locked(db
, mem_ctx
, key
);
714 if (global
->db_rec
== NULL
) {
716 return NT_STATUS_INSUFFICIENT_RESOURCES
;
719 smbXsrv_session_global_verify_record(global
->db_rec
,
725 TALLOC_FREE(global
->db_rec
);
729 if (!was_free
&& i
< min_tries
) {
731 * The session_id is free now,
732 * but was not free before.
734 * This happens if a smbd crashed
735 * and did not cleanup the record.
737 * If this is one of our first tries,
738 * then we try to find a real free one.
740 if (last_free
== 0) {
743 TALLOC_FREE(global
->db_rec
);
747 global
->session_global_id
= id
;
753 /* should not be reached */
755 return NT_STATUS_INTERNAL_ERROR
;
758 static void smbXsrv_session_global_verify_record(struct db_record
*db_rec
,
762 struct smbXsrv_session_global0
**_g
)
767 struct smbXsrv_session_globalB global_blob
;
768 enum ndr_err_code ndr_err
;
769 struct smbXsrv_session_global0
*global
= NULL
;
771 TALLOC_CTX
*frame
= talloc_stackframe();
782 key
= dbwrap_record_get_key(db_rec
);
784 val
= dbwrap_record_get_value(db_rec
);
785 if (val
.dsize
== 0) {
794 blob
= data_blob_const(val
.dptr
, val
.dsize
);
796 ndr_err
= ndr_pull_struct_blob(&blob
, frame
, &global_blob
,
797 (ndr_pull_flags_fn_t
)ndr_pull_smbXsrv_session_globalB
);
798 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
799 NTSTATUS status
= ndr_map_error2ntstatus(ndr_err
);
800 DEBUG(1,("smbXsrv_session_global_verify_record: "
801 "key '%s' ndr_pull_struct_blob - %s\n",
802 hex_encode_talloc(frame
, key
.dptr
, key
.dsize
),
808 DEBUG(10,("smbXsrv_session_global_verify_record\n"));
810 NDR_PRINT_DEBUG(smbXsrv_session_globalB
, &global_blob
);
813 if (global_blob
.version
!= SMBXSRV_VERSION_0
) {
814 DEBUG(0,("smbXsrv_session_global_verify_record: "
815 "key '%s' use unsupported version %u\n",
816 hex_encode_talloc(frame
, key
.dptr
, key
.dsize
),
817 global_blob
.version
));
818 NDR_PRINT_DEBUG(smbXsrv_session_globalB
, &global_blob
);
823 global
= global_blob
.info
.info0
;
825 exists
= serverid_exists(&global
->channels
[0].server_id
);
827 struct server_id_buf idbuf
;
828 DEBUG(2,("smbXsrv_session_global_verify_record: "
829 "key '%s' server_id %s does not exist.\n",
830 hex_encode_talloc(frame
, key
.dptr
, key
.dsize
),
831 server_id_str_buf(global
->channels
[0].server_id
,
834 NDR_PRINT_DEBUG(smbXsrv_session_globalB
, &global_blob
);
837 dbwrap_record_delete(db_rec
);
843 *_g
= talloc_move(mem_ctx
, &global
);
848 static NTSTATUS
smbXsrv_session_global_store(struct smbXsrv_session_global0
*global
)
850 struct smbXsrv_session_globalB global_blob
;
851 DATA_BLOB blob
= data_blob_null
;
855 enum ndr_err_code ndr_err
;
858 * TODO: if we use other versions than '0'
859 * we would add glue code here, that would be able to
860 * store the information in the old format.
863 if (global
->db_rec
== NULL
) {
864 return NT_STATUS_INTERNAL_ERROR
;
867 key
= dbwrap_record_get_key(global
->db_rec
);
868 val
= dbwrap_record_get_value(global
->db_rec
);
870 ZERO_STRUCT(global_blob
);
871 global_blob
.version
= smbXsrv_version_global_current();
872 if (val
.dsize
>= 8) {
873 global_blob
.seqnum
= IVAL(val
.dptr
, 4);
875 global_blob
.seqnum
+= 1;
876 global_blob
.info
.info0
= global
;
878 ndr_err
= ndr_push_struct_blob(&blob
, global
->db_rec
, &global_blob
,
879 (ndr_push_flags_fn_t
)ndr_push_smbXsrv_session_globalB
);
880 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
881 status
= ndr_map_error2ntstatus(ndr_err
);
882 DEBUG(1,("smbXsrv_session_global_store: key '%s' ndr_push - %s\n",
883 hex_encode_talloc(global
->db_rec
, key
.dptr
, key
.dsize
),
885 TALLOC_FREE(global
->db_rec
);
889 val
= make_tdb_data(blob
.data
, blob
.length
);
890 status
= dbwrap_record_store(global
->db_rec
, val
, TDB_REPLACE
);
891 if (!NT_STATUS_IS_OK(status
)) {
892 DEBUG(1,("smbXsrv_session_global_store: key '%s' store - %s\n",
893 hex_encode_talloc(global
->db_rec
, key
.dptr
, key
.dsize
),
895 TALLOC_FREE(global
->db_rec
);
900 DEBUG(10,("smbXsrv_session_global_store: key '%s' stored\n",
901 hex_encode_talloc(global
->db_rec
, key
.dptr
, key
.dsize
)));
902 NDR_PRINT_DEBUG(smbXsrv_session_globalB
, &global_blob
);
905 TALLOC_FREE(global
->db_rec
);
910 struct smb2srv_session_close_previous_state
{
911 struct tevent_context
*ev
;
912 struct smbXsrv_connection
*connection
;
913 struct dom_sid
*current_sid
;
914 uint64_t current_session_id
;
915 struct db_record
*db_rec
;
918 static void smb2srv_session_close_previous_check(struct tevent_req
*req
);
919 static void smb2srv_session_close_previous_modified(struct tevent_req
*subreq
);
921 struct tevent_req
*smb2srv_session_close_previous_send(TALLOC_CTX
*mem_ctx
,
922 struct tevent_context
*ev
,
923 struct smbXsrv_connection
*conn
,
924 struct auth_session_info
*session_info
,
925 uint64_t previous_session_id
,
926 uint64_t current_session_id
)
928 struct tevent_req
*req
;
929 struct smb2srv_session_close_previous_state
*state
;
930 uint32_t global_id
= previous_session_id
& UINT32_MAX
;
931 uint64_t global_zeros
= previous_session_id
& 0xFFFFFFFF00000000LLU
;
932 struct smbXsrv_session_table
*table
= conn
->client
->session_table
;
933 struct security_token
*current_token
= NULL
;
934 uint8_t key_buf
[SMBXSRV_SESSION_GLOBAL_TDB_KEY_SIZE
];
937 req
= tevent_req_create(mem_ctx
, &state
,
938 struct smb2srv_session_close_previous_state
);
943 state
->connection
= conn
;
944 state
->current_session_id
= current_session_id
;
946 if (global_zeros
!= 0) {
947 tevent_req_done(req
);
948 return tevent_req_post(req
, ev
);
951 if (session_info
== NULL
) {
952 tevent_req_done(req
);
953 return tevent_req_post(req
, ev
);
955 current_token
= session_info
->security_token
;
957 if (current_token
->num_sids
> PRIMARY_USER_SID_INDEX
) {
958 state
->current_sid
= ¤t_token
->sids
[PRIMARY_USER_SID_INDEX
];
961 if (state
->current_sid
== NULL
) {
962 tevent_req_done(req
);
963 return tevent_req_post(req
, ev
);
966 if (!security_token_has_nt_authenticated_users(current_token
)) {
968 tevent_req_done(req
);
969 return tevent_req_post(req
, ev
);
972 key
= smbXsrv_session_global_id_to_key(global_id
, key_buf
);
974 state
->db_rec
= dbwrap_fetch_locked(table
->global
.db_ctx
,
976 if (state
->db_rec
== NULL
) {
977 tevent_req_nterror(req
, NT_STATUS_UNSUCCESSFUL
);
978 return tevent_req_post(req
, ev
);
981 smb2srv_session_close_previous_check(req
);
982 if (!tevent_req_is_in_progress(req
)) {
983 return tevent_req_post(req
, ev
);
989 static void smb2srv_session_close_previous_check(struct tevent_req
*req
)
991 struct smb2srv_session_close_previous_state
*state
=
993 struct smb2srv_session_close_previous_state
);
994 struct smbXsrv_connection
*conn
= state
->connection
;
996 struct security_token
*previous_token
= NULL
;
997 struct smbXsrv_session_global0
*global
= NULL
;
998 enum ndr_err_code ndr_err
;
999 struct smbXsrv_session_close0 close_info0
;
1000 struct smbXsrv_session_closeB close_blob
;
1001 struct tevent_req
*subreq
= NULL
;
1003 bool is_free
= false;
1005 smbXsrv_session_global_verify_record(state
->db_rec
,
1012 TALLOC_FREE(state
->db_rec
);
1013 tevent_req_done(req
);
1017 if (global
->auth_session_info
== NULL
) {
1018 TALLOC_FREE(state
->db_rec
);
1019 tevent_req_done(req
);
1023 previous_token
= global
->auth_session_info
->security_token
;
1025 if (!security_token_is_sid(previous_token
, state
->current_sid
)) {
1026 TALLOC_FREE(state
->db_rec
);
1027 tevent_req_done(req
);
1031 subreq
= dbwrap_record_watch_send(state
, state
->ev
,
1032 state
->db_rec
, conn
->msg_ctx
);
1033 if (tevent_req_nomem(subreq
, req
)) {
1034 TALLOC_FREE(state
->db_rec
);
1037 tevent_req_set_callback(subreq
,
1038 smb2srv_session_close_previous_modified
,
1041 close_info0
.old_session_global_id
= global
->session_global_id
;
1042 close_info0
.old_session_wire_id
= global
->session_wire_id
;
1043 close_info0
.old_creation_time
= global
->creation_time
;
1044 close_info0
.new_session_wire_id
= state
->current_session_id
;
1046 ZERO_STRUCT(close_blob
);
1047 close_blob
.version
= smbXsrv_version_global_current();
1048 close_blob
.info
.info0
= &close_info0
;
1050 ndr_err
= ndr_push_struct_blob(&blob
, state
, &close_blob
,
1051 (ndr_push_flags_fn_t
)ndr_push_smbXsrv_session_closeB
);
1052 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
1053 TALLOC_FREE(state
->db_rec
);
1054 status
= ndr_map_error2ntstatus(ndr_err
);
1055 DEBUG(1,("smb2srv_session_close_previous_check: "
1056 "old_session[%llu] new_session[%llu] ndr_push - %s\n",
1057 (unsigned long long)close_info0
.old_session_wire_id
,
1058 (unsigned long long)close_info0
.new_session_wire_id
,
1059 nt_errstr(status
)));
1060 tevent_req_nterror(req
, status
);
1064 status
= messaging_send(conn
->msg_ctx
,
1065 global
->channels
[0].server_id
,
1066 MSG_SMBXSRV_SESSION_CLOSE
, &blob
);
1067 TALLOC_FREE(state
->db_rec
);
1068 if (tevent_req_nterror(req
, status
)) {
1072 TALLOC_FREE(global
);
1076 static void smb2srv_session_close_previous_modified(struct tevent_req
*subreq
)
1078 struct tevent_req
*req
=
1079 tevent_req_callback_data(subreq
,
1081 struct smb2srv_session_close_previous_state
*state
=
1082 tevent_req_data(req
,
1083 struct smb2srv_session_close_previous_state
);
1086 status
= dbwrap_record_watch_recv(subreq
, state
, &state
->db_rec
);
1087 TALLOC_FREE(subreq
);
1088 if (tevent_req_nterror(req
, status
)) {
1092 smb2srv_session_close_previous_check(req
);
1095 NTSTATUS
smb2srv_session_close_previous_recv(struct tevent_req
*req
)
1099 if (tevent_req_is_nterror(req
, &status
)) {
1100 tevent_req_received(req
);
1104 tevent_req_received(req
);
1105 return NT_STATUS_OK
;
1108 static NTSTATUS
smbXsrv_session_clear_and_logoff(struct smbXsrv_session
*session
)
1111 struct smbXsrv_connection
*xconn
= NULL
;
1113 if (session
->client
!= NULL
) {
1114 xconn
= session
->client
->connections
;
1117 for (; xconn
!= NULL
; xconn
= xconn
->next
) {
1118 struct smbd_smb2_request
*preq
;
1120 for (preq
= xconn
->smb2
.requests
; preq
!= NULL
; preq
= preq
->next
) {
1121 if (preq
->session
!= session
) {
1125 preq
->session
= NULL
;
1127 * If we no longer have a session we can't
1128 * sign or encrypt replies.
1130 preq
->do_signing
= false;
1131 preq
->do_encryption
= false;
1132 preq
->preauth
= NULL
;
1136 status
= smbXsrv_session_logoff(session
);
1140 static int smbXsrv_session_destructor(struct smbXsrv_session
*session
)
1144 status
= smbXsrv_session_clear_and_logoff(session
);
1145 if (!NT_STATUS_IS_OK(status
)) {
1146 DEBUG(0, ("smbXsrv_session_destructor: "
1147 "smbXsrv_session_logoff() failed: %s\n",
1148 nt_errstr(status
)));
1151 TALLOC_FREE(session
->global
);
1156 NTSTATUS
smbXsrv_session_create(struct smbXsrv_connection
*conn
,
1158 struct smbXsrv_session
**_session
)
1160 struct smbXsrv_session_table
*table
= conn
->client
->session_table
;
1161 struct db_record
*local_rec
= NULL
;
1162 struct smbXsrv_session
*session
= NULL
;
1165 struct smbXsrv_session_global0
*global
= NULL
;
1166 struct smbXsrv_channel_global0
*channels
= NULL
;
1169 if (table
->local
.num_sessions
>= table
->local
.max_sessions
) {
1170 return NT_STATUS_INSUFFICIENT_RESOURCES
;
1173 session
= talloc_zero(table
, struct smbXsrv_session
);
1174 if (session
== NULL
) {
1175 return NT_STATUS_NO_MEMORY
;
1177 session
->table
= table
;
1178 session
->idle_time
= now
;
1179 session
->status
= NT_STATUS_MORE_PROCESSING_REQUIRED
;
1180 session
->client
= conn
->client
;
1182 status
= smbXsrv_session_global_allocate(table
->global
.db_ctx
,
1185 if (!NT_STATUS_IS_OK(status
)) {
1186 TALLOC_FREE(session
);
1189 session
->global
= global
;
1191 if (conn
->protocol
>= PROTOCOL_SMB2_02
) {
1192 uint64_t id
= global
->session_global_id
;
1193 uint8_t key_buf
[SMBXSRV_SESSION_LOCAL_TDB_KEY_SIZE
];
1196 global
->connection_dialect
= conn
->smb2
.server
.dialect
;
1198 global
->session_wire_id
= id
;
1200 status
= smb2srv_tcon_table_init(session
);
1201 if (!NT_STATUS_IS_OK(status
)) {
1202 TALLOC_FREE(session
);
1206 session
->local_id
= global
->session_global_id
;
1208 key
= smbXsrv_session_local_id_to_key(session
->local_id
, key_buf
);
1210 local_rec
= dbwrap_fetch_locked(table
->local
.db_ctx
,
1212 if (local_rec
== NULL
) {
1213 TALLOC_FREE(session
);
1214 return NT_STATUS_NO_MEMORY
;
1217 val
= dbwrap_record_get_value(local_rec
);
1218 if (val
.dsize
!= 0) {
1219 TALLOC_FREE(session
);
1220 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
1224 status
= smb1srv_session_local_allocate_id(table
->local
.db_ctx
,
1225 table
->local
.lowest_id
,
1226 table
->local
.highest_id
,
1229 &session
->local_id
);
1230 if (!NT_STATUS_IS_OK(status
)) {
1231 TALLOC_FREE(session
);
1235 global
->session_wire_id
= session
->local_id
;
1238 global
->creation_time
= now
;
1239 global
->expiration_time
= GENSEC_EXPIRE_TIME_INFINITY
;
1241 global
->num_channels
= 1;
1242 channels
= talloc_zero_array(global
,
1243 struct smbXsrv_channel_global0
,
1244 global
->num_channels
);
1245 if (channels
== NULL
) {
1246 TALLOC_FREE(session
);
1247 return NT_STATUS_NO_MEMORY
;
1249 global
->channels
= channels
;
1251 channels
[0].server_id
= messaging_server_id(conn
->msg_ctx
);
1252 channels
[0].local_address
= tsocket_address_string(conn
->local_address
,
1254 if (channels
[0].local_address
== NULL
) {
1255 TALLOC_FREE(session
);
1256 return NT_STATUS_NO_MEMORY
;
1258 channels
[0].remote_address
= tsocket_address_string(conn
->remote_address
,
1260 if (channels
[0].remote_address
== NULL
) {
1261 TALLOC_FREE(session
);
1262 return NT_STATUS_NO_MEMORY
;
1264 channels
[0].remote_name
= talloc_strdup(channels
, conn
->remote_hostname
);
1265 if (channels
[0].remote_name
== NULL
) {
1266 TALLOC_FREE(session
);
1267 return NT_STATUS_NO_MEMORY
;
1269 channels
[0].signing_key
= data_blob_null
;
1270 channels
[0].connection
= conn
;
1273 val
= make_tdb_data((uint8_t const *)&ptr
, sizeof(ptr
));
1274 status
= dbwrap_record_store(local_rec
, val
, TDB_REPLACE
);
1275 TALLOC_FREE(local_rec
);
1276 if (!NT_STATUS_IS_OK(status
)) {
1277 TALLOC_FREE(session
);
1280 table
->local
.num_sessions
+= 1;
1282 talloc_set_destructor(session
, smbXsrv_session_destructor
);
1284 status
= smbXsrv_session_global_store(global
);
1285 if (!NT_STATUS_IS_OK(status
)) {
1286 DEBUG(0,("smbXsrv_session_create: "
1287 "global_id (0x%08x) store failed - %s\n",
1288 session
->global
->session_global_id
,
1289 nt_errstr(status
)));
1290 TALLOC_FREE(session
);
1295 struct smbXsrv_sessionB session_blob
;
1297 ZERO_STRUCT(session_blob
);
1298 session_blob
.version
= SMBXSRV_VERSION_0
;
1299 session_blob
.info
.info0
= session
;
1301 DEBUG(10,("smbXsrv_session_create: global_id (0x%08x) stored\n",
1302 session
->global
->session_global_id
));
1303 NDR_PRINT_DEBUG(smbXsrv_sessionB
, &session_blob
);
1306 *_session
= session
;
1307 return NT_STATUS_OK
;
1310 NTSTATUS
smbXsrv_session_update(struct smbXsrv_session
*session
)
1312 struct smbXsrv_session_table
*table
= session
->table
;
1314 uint8_t key_buf
[SMBXSRV_SESSION_GLOBAL_TDB_KEY_SIZE
];
1317 if (session
->global
->db_rec
!= NULL
) {
1318 DEBUG(0, ("smbXsrv_session_update(0x%08x): "
1319 "Called with db_rec != NULL'\n",
1320 session
->global
->session_global_id
));
1321 return NT_STATUS_INTERNAL_ERROR
;
1324 key
= smbXsrv_session_global_id_to_key(
1325 session
->global
->session_global_id
,
1328 session
->global
->db_rec
= dbwrap_fetch_locked(table
->global
.db_ctx
,
1329 session
->global
, key
);
1330 if (session
->global
->db_rec
== NULL
) {
1331 DEBUG(0, ("smbXsrv_session_update(0x%08x): "
1332 "Failed to lock global key '%s'\n",
1333 session
->global
->session_global_id
,
1334 hex_encode_talloc(talloc_tos(), key
.dptr
,
1336 return NT_STATUS_INTERNAL_DB_ERROR
;
1339 status
= smbXsrv_session_global_store(session
->global
);
1340 if (!NT_STATUS_IS_OK(status
)) {
1341 DEBUG(0,("smbXsrv_session_update: "
1342 "global_id (0x%08x) store failed - %s\n",
1343 session
->global
->session_global_id
,
1344 nt_errstr(status
)));
1349 struct smbXsrv_sessionB session_blob
;
1351 ZERO_STRUCT(session_blob
);
1352 session_blob
.version
= SMBXSRV_VERSION_0
;
1353 session_blob
.info
.info0
= session
;
1355 DEBUG(10,("smbXsrv_session_update: global_id (0x%08x) stored\n",
1356 session
->global
->session_global_id
));
1357 NDR_PRINT_DEBUG(smbXsrv_sessionB
, &session_blob
);
1360 return NT_STATUS_OK
;
1363 NTSTATUS
smbXsrv_session_find_channel(const struct smbXsrv_session
*session
,
1364 const struct smbXsrv_connection
*conn
,
1365 struct smbXsrv_channel_global0
**_c
)
1369 for (i
=0; i
< session
->global
->num_channels
; i
++) {
1370 struct smbXsrv_channel_global0
*c
= &session
->global
->channels
[i
];
1372 if (c
->connection
== conn
) {
1374 return NT_STATUS_OK
;
1378 return NT_STATUS_USER_SESSION_DELETED
;
1381 NTSTATUS
smbXsrv_session_find_auth(const struct smbXsrv_session
*session
,
1382 const struct smbXsrv_connection
*conn
,
1384 struct smbXsrv_session_auth0
**_a
)
1386 struct smbXsrv_session_auth0
*a
;
1388 for (a
= session
->pending_auth
; a
!= NULL
; a
= a
->next
) {
1389 if (a
->connection
== conn
) {
1394 return NT_STATUS_OK
;
1398 return NT_STATUS_USER_SESSION_DELETED
;
1401 static int smbXsrv_session_auth0_destructor(struct smbXsrv_session_auth0
*a
)
1403 if (a
->session
== NULL
) {
1407 DLIST_REMOVE(a
->session
->pending_auth
, a
);
1412 NTSTATUS
smbXsrv_session_create_auth(struct smbXsrv_session
*session
,
1413 struct smbXsrv_connection
*conn
,
1416 uint8_t in_security_mode
,
1417 struct smbXsrv_session_auth0
**_a
)
1419 struct smbXsrv_session_auth0
*a
;
1422 status
= smbXsrv_session_find_auth(session
, conn
, 0, &a
);
1423 if (NT_STATUS_IS_OK(status
)) {
1424 return NT_STATUS_INTERNAL_ERROR
;
1427 a
= talloc_zero(session
, struct smbXsrv_session_auth0
);
1429 return NT_STATUS_NO_MEMORY
;
1431 a
->session
= session
;
1432 a
->connection
= conn
;
1433 a
->in_flags
= in_flags
;
1434 a
->in_security_mode
= in_security_mode
;
1435 a
->creation_time
= now
;
1438 if (conn
->protocol
>= PROTOCOL_SMB3_10
) {
1439 a
->preauth
= talloc(a
, struct smbXsrv_preauth
);
1440 if (a
->preauth
== NULL
) {
1441 TALLOC_FREE(session
);
1442 return NT_STATUS_NO_MEMORY
;
1444 *a
->preauth
= conn
->smb2
.preauth
;
1447 talloc_set_destructor(a
, smbXsrv_session_auth0_destructor
);
1448 DLIST_ADD_END(session
->pending_auth
, a
, NULL
);
1451 return NT_STATUS_OK
;
1454 struct smb2srv_session_shutdown_state
{
1455 struct tevent_queue
*wait_queue
;
1458 static void smb2srv_session_shutdown_wait_done(struct tevent_req
*subreq
);
1460 struct tevent_req
*smb2srv_session_shutdown_send(TALLOC_CTX
*mem_ctx
,
1461 struct tevent_context
*ev
,
1462 struct smbXsrv_session
*session
,
1463 struct smbd_smb2_request
*current_req
)
1465 struct tevent_req
*req
;
1466 struct smb2srv_session_shutdown_state
*state
;
1467 struct tevent_req
*subreq
;
1468 struct smbXsrv_connection
*xconn
= NULL
;
1472 * Make sure that no new request will be able to use this session.
1474 session
->status
= NT_STATUS_USER_SESSION_DELETED
;
1476 req
= tevent_req_create(mem_ctx
, &state
,
1477 struct smb2srv_session_shutdown_state
);
1482 state
->wait_queue
= tevent_queue_create(state
, "smb2srv_session_shutdown_queue");
1483 if (tevent_req_nomem(state
->wait_queue
, req
)) {
1484 return tevent_req_post(req
, ev
);
1487 for (xconn
= session
->client
->connections
; xconn
!= NULL
; xconn
= xconn
->next
) {
1488 struct smbd_smb2_request
*preq
;
1490 for (preq
= xconn
->smb2
.requests
; preq
!= NULL
; preq
= preq
->next
) {
1491 if (preq
== current_req
) {
1492 /* Can't cancel current request. */
1495 if (preq
->session
!= session
) {
1496 /* Request on different session. */
1500 if (!NT_STATUS_IS_OK(xconn
->transport
.status
)) {
1501 preq
->session
= NULL
;
1503 * If we no longer have a session we can't
1504 * sign or encrypt replies.
1506 preq
->do_signing
= false;
1507 preq
->do_encryption
= false;
1508 preq
->preauth
= NULL
;
1510 if (preq
->subreq
!= NULL
) {
1511 tevent_req_cancel(preq
->subreq
);
1517 * Never cancel anything in a compound
1518 * request. Way too hard to deal with
1521 if (!preq
->compound_related
&& preq
->subreq
!= NULL
) {
1522 tevent_req_cancel(preq
->subreq
);
1526 * Now wait until the request is finished.
1528 * We don't set a callback, as we just want to block the
1529 * wait queue and the talloc_free() of the request will
1530 * remove the item from the wait queue.
1532 subreq
= tevent_queue_wait_send(preq
, ev
, state
->wait_queue
);
1533 if (tevent_req_nomem(subreq
, req
)) {
1534 return tevent_req_post(req
, ev
);
1539 len
= tevent_queue_length(state
->wait_queue
);
1541 tevent_req_done(req
);
1542 return tevent_req_post(req
, ev
);
1546 * Now we add our own waiter to the end of the queue,
1547 * this way we get notified when all pending requests are finished
1548 * and send to the socket.
1550 subreq
= tevent_queue_wait_send(state
, ev
, state
->wait_queue
);
1551 if (tevent_req_nomem(subreq
, req
)) {
1552 return tevent_req_post(req
, ev
);
1554 tevent_req_set_callback(subreq
, smb2srv_session_shutdown_wait_done
, req
);
1559 static void smb2srv_session_shutdown_wait_done(struct tevent_req
*subreq
)
1561 struct tevent_req
*req
=
1562 tevent_req_callback_data(subreq
,
1565 tevent_queue_wait_recv(subreq
);
1566 TALLOC_FREE(subreq
);
1568 tevent_req_done(req
);
1571 NTSTATUS
smb2srv_session_shutdown_recv(struct tevent_req
*req
)
1573 return tevent_req_simple_recv_ntstatus(req
);
1576 NTSTATUS
smbXsrv_session_logoff(struct smbXsrv_session
*session
)
1578 struct smbXsrv_session_table
*table
;
1579 struct db_record
*local_rec
= NULL
;
1580 struct db_record
*global_rec
= NULL
;
1581 struct smbd_server_connection
*sconn
= NULL
;
1583 NTSTATUS error
= NT_STATUS_OK
;
1585 if (session
->table
== NULL
) {
1586 return NT_STATUS_OK
;
1589 table
= session
->table
;
1590 session
->table
= NULL
;
1592 sconn
= session
->client
->sconn
;
1593 session
->client
= NULL
;
1594 session
->status
= NT_STATUS_USER_SESSION_DELETED
;
1596 global_rec
= session
->global
->db_rec
;
1597 session
->global
->db_rec
= NULL
;
1598 if (global_rec
== NULL
) {
1599 uint8_t key_buf
[SMBXSRV_SESSION_GLOBAL_TDB_KEY_SIZE
];
1602 key
= smbXsrv_session_global_id_to_key(
1603 session
->global
->session_global_id
,
1606 global_rec
= dbwrap_fetch_locked(table
->global
.db_ctx
,
1607 session
->global
, key
);
1608 if (global_rec
== NULL
) {
1609 DEBUG(0, ("smbXsrv_session_logoff(0x%08x): "
1610 "Failed to lock global key '%s'\n",
1611 session
->global
->session_global_id
,
1612 hex_encode_talloc(global_rec
, key
.dptr
,
1614 error
= NT_STATUS_INTERNAL_ERROR
;
1618 if (global_rec
!= NULL
) {
1619 status
= dbwrap_record_delete(global_rec
);
1620 if (!NT_STATUS_IS_OK(status
)) {
1621 TDB_DATA key
= dbwrap_record_get_key(global_rec
);
1623 DEBUG(0, ("smbXsrv_session_logoff(0x%08x): "
1624 "failed to delete global key '%s': %s\n",
1625 session
->global
->session_global_id
,
1626 hex_encode_talloc(global_rec
, key
.dptr
,
1628 nt_errstr(status
)));
1632 TALLOC_FREE(global_rec
);
1634 local_rec
= session
->db_rec
;
1635 if (local_rec
== NULL
) {
1636 uint8_t key_buf
[SMBXSRV_SESSION_LOCAL_TDB_KEY_SIZE
];
1639 key
= smbXsrv_session_local_id_to_key(session
->local_id
,
1642 local_rec
= dbwrap_fetch_locked(table
->local
.db_ctx
,
1644 if (local_rec
== NULL
) {
1645 DEBUG(0, ("smbXsrv_session_logoff(0x%08x): "
1646 "Failed to lock local key '%s'\n",
1647 session
->global
->session_global_id
,
1648 hex_encode_talloc(local_rec
, key
.dptr
,
1650 error
= NT_STATUS_INTERNAL_ERROR
;
1654 if (local_rec
!= NULL
) {
1655 status
= dbwrap_record_delete(local_rec
);
1656 if (!NT_STATUS_IS_OK(status
)) {
1657 TDB_DATA key
= dbwrap_record_get_key(local_rec
);
1659 DEBUG(0, ("smbXsrv_session_logoff(0x%08x): "
1660 "failed to delete local key '%s': %s\n",
1661 session
->global
->session_global_id
,
1662 hex_encode_talloc(local_rec
, key
.dptr
,
1664 nt_errstr(status
)));
1667 table
->local
.num_sessions
-= 1;
1669 if (session
->db_rec
== NULL
) {
1670 TALLOC_FREE(local_rec
);
1672 session
->db_rec
= NULL
;
1674 if (session
->compat
) {
1675 file_close_user(sconn
, session
->compat
->vuid
);
1678 if (session
->tcon_table
!= NULL
) {
1680 * Note: We only have a tcon_table for SMB2.
1682 status
= smb2srv_tcon_disconnect_all(session
);
1683 if (!NT_STATUS_IS_OK(status
)) {
1684 DEBUG(0, ("smbXsrv_session_logoff(0x%08x): "
1685 "smb2srv_tcon_disconnect_all() failed: %s\n",
1686 session
->global
->session_global_id
,
1687 nt_errstr(status
)));
1692 if (session
->compat
) {
1693 invalidate_vuid(sconn
, session
->compat
->vuid
);
1694 session
->compat
= NULL
;
1700 struct smbXsrv_session_logoff_all_state
{
1701 NTSTATUS first_status
;
1705 static int smbXsrv_session_logoff_all_callback(struct db_record
*local_rec
,
1706 void *private_data
);
1708 NTSTATUS
smbXsrv_session_logoff_all(struct smbXsrv_connection
*conn
)
1710 struct smbXsrv_session_table
*table
= conn
->client
->session_table
;
1711 struct smbXsrv_session_logoff_all_state state
;
1715 if (table
== NULL
) {
1716 DEBUG(10, ("smbXsrv_session_logoff_all: "
1717 "empty session_table, nothing to do.\n"));
1718 return NT_STATUS_OK
;
1723 status
= dbwrap_traverse(table
->local
.db_ctx
,
1724 smbXsrv_session_logoff_all_callback
,
1726 if (!NT_STATUS_IS_OK(status
)) {
1727 DEBUG(0, ("smbXsrv_session_logoff_all: "
1728 "dbwrap_traverse() failed: %s\n",
1729 nt_errstr(status
)));
1733 if (!NT_STATUS_IS_OK(state
.first_status
)) {
1734 DEBUG(0, ("smbXsrv_session_logoff_all: "
1735 "count[%d] errors[%d] first[%s]\n",
1736 count
, state
.errors
,
1737 nt_errstr(state
.first_status
)));
1738 return state
.first_status
;
1741 return NT_STATUS_OK
;
1744 static int smbXsrv_session_logoff_all_callback(struct db_record
*local_rec
,
1747 struct smbXsrv_session_logoff_all_state
*state
=
1748 (struct smbXsrv_session_logoff_all_state
*)private_data
;
1751 struct smbXsrv_session
*session
= NULL
;
1754 val
= dbwrap_record_get_value(local_rec
);
1755 if (val
.dsize
!= sizeof(ptr
)) {
1756 status
= NT_STATUS_INTERNAL_ERROR
;
1757 if (NT_STATUS_IS_OK(state
->first_status
)) {
1758 state
->first_status
= status
;
1764 memcpy(&ptr
, val
.dptr
, val
.dsize
);
1765 session
= talloc_get_type_abort(ptr
, struct smbXsrv_session
);
1767 session
->db_rec
= local_rec
;
1769 status
= smbXsrv_session_clear_and_logoff(session
);
1770 if (!NT_STATUS_IS_OK(status
)) {
1771 if (NT_STATUS_IS_OK(state
->first_status
)) {
1772 state
->first_status
= status
;
1781 NTSTATUS
smb1srv_session_table_init(struct smbXsrv_connection
*conn
)
1784 * Allow a range from 1..65534 with 65534 values.
1786 return smbXsrv_session_table_init(conn
, 1, UINT16_MAX
- 1,
1790 NTSTATUS
smb1srv_session_lookup(struct smbXsrv_connection
*conn
,
1791 uint16_t vuid
, NTTIME now
,
1792 struct smbXsrv_session
**session
)
1794 struct smbXsrv_session_table
*table
= conn
->client
->session_table
;
1795 uint32_t local_id
= vuid
;
1797 return smbXsrv_session_local_lookup(table
, conn
, local_id
, now
,
1801 NTSTATUS
smb2srv_session_table_init(struct smbXsrv_connection
*conn
)
1804 * Allow a range from 1..4294967294 with 65534 (same as SMB1) values.
1806 return smbXsrv_session_table_init(conn
, 1, UINT32_MAX
- 1,
1810 static NTSTATUS
smb2srv_session_lookup_raw(struct smbXsrv_session_table
*table
,
1811 /* conn: optional */
1812 struct smbXsrv_connection
*conn
,
1813 uint64_t session_id
, NTTIME now
,
1814 struct smbXsrv_session
**session
)
1816 uint32_t local_id
= session_id
& UINT32_MAX
;
1817 uint64_t local_zeros
= session_id
& 0xFFFFFFFF00000000LLU
;
1819 if (local_zeros
!= 0) {
1820 return NT_STATUS_USER_SESSION_DELETED
;
1823 return smbXsrv_session_local_lookup(table
, conn
, local_id
, now
,
1827 NTSTATUS
smb2srv_session_lookup_conn(struct smbXsrv_connection
*conn
,
1828 uint64_t session_id
, NTTIME now
,
1829 struct smbXsrv_session
**session
)
1831 struct smbXsrv_session_table
*table
= conn
->client
->session_table
;
1832 return smb2srv_session_lookup_raw(table
, conn
, session_id
, now
,
1836 NTSTATUS
smb2srv_session_lookup_client(struct smbXsrv_client
*client
,
1837 uint64_t session_id
, NTTIME now
,
1838 struct smbXsrv_session
**session
)
1840 struct smbXsrv_session_table
*table
= client
->session_table
;
1841 return smb2srv_session_lookup_raw(table
, NULL
, session_id
, now
,
1845 struct smbXsrv_session_global_traverse_state
{
1846 int (*fn
)(struct smbXsrv_session_global0
*, void *);
1850 static int smbXsrv_session_global_traverse_fn(struct db_record
*rec
, void *data
)
1853 struct smbXsrv_session_global_traverse_state
*state
=
1854 (struct smbXsrv_session_global_traverse_state
*)data
;
1855 TDB_DATA key
= dbwrap_record_get_key(rec
);
1856 TDB_DATA val
= dbwrap_record_get_value(rec
);
1857 DATA_BLOB blob
= data_blob_const(val
.dptr
, val
.dsize
);
1858 struct smbXsrv_session_globalB global_blob
;
1859 enum ndr_err_code ndr_err
;
1860 TALLOC_CTX
*frame
= talloc_stackframe();
1862 ndr_err
= ndr_pull_struct_blob(&blob
, frame
, &global_blob
,
1863 (ndr_pull_flags_fn_t
)ndr_pull_smbXsrv_session_globalB
);
1864 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
1865 DEBUG(1,("Invalid record in smbXsrv_session_global.tdb:"
1866 "key '%s' ndr_pull_struct_blob - %s\n",
1867 hex_encode_talloc(frame
, key
.dptr
, key
.dsize
),
1868 ndr_errstr(ndr_err
)));
1872 if (global_blob
.version
!= SMBXSRV_VERSION_0
) {
1873 DEBUG(1,("Invalid record in smbXsrv_session_global.tdb:"
1874 "key '%s' unsuported version - %d\n",
1875 hex_encode_talloc(frame
, key
.dptr
, key
.dsize
),
1876 (int)global_blob
.version
));
1880 global_blob
.info
.info0
->db_rec
= rec
;
1881 ret
= state
->fn(global_blob
.info
.info0
, state
->private_data
);
1887 NTSTATUS
smbXsrv_session_global_traverse(
1888 int (*fn
)(struct smbXsrv_session_global0
*, void *),
1894 struct smbXsrv_session_global_traverse_state state
= {
1896 .private_data
= private_data
,
1900 status
= smbXsrv_session_global_init();
1901 if (!NT_STATUS_IS_OK(status
)) {
1903 DEBUG(0, ("Failed to initialize session_global: %s\n",
1904 nt_errstr(status
)));
1908 status
= dbwrap_traverse_read(smbXsrv_session_global_db_ctx
,
1909 smbXsrv_session_global_traverse_fn
,