2 Unix SMB/CIFS implementation.
4 Copyright (C) Stefan Metzmacher 2014
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "system/filesys.h"
23 #include "lib/util/server_id.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"
40 #include "lib/util/iov_buf.h"
41 #include "lib/global_contexts.h"
43 struct smbXsrv_client_table
{
49 struct db_context
*db_ctx
;
53 static struct db_context
*smbXsrv_client_global_db_ctx
= NULL
;
55 NTSTATUS
smbXsrv_client_global_init(void)
57 const char *global_path
= NULL
;
58 struct db_context
*backend
= NULL
;
59 struct db_context
*db_ctx
= NULL
;
61 if (smbXsrv_client_global_db_ctx
!= NULL
) {
66 * This contains secret information like client keys!
68 global_path
= lock_path(talloc_tos(), "smbXsrv_client_global.tdb");
69 if (global_path
== NULL
) {
70 return NT_STATUS_NO_MEMORY
;
73 backend
= db_open(NULL
, global_path
,
77 TDB_INCOMPATIBLE_HASH
,
78 O_RDWR
| O_CREAT
, 0600,
81 if (backend
== NULL
) {
84 status
= map_nt_error_from_unix_common(errno
);
89 db_ctx
= db_open_watched(NULL
, &backend
, global_messaging_context());
92 return NT_STATUS_NO_MEMORY
;
95 smbXsrv_client_global_db_ctx
= db_ctx
;
102 * We need to store the keys in big endian so that dbwrap_rbt's memcmp
103 * has the same result as integer comparison between the uint32_t
106 * TODO: implement string based key
109 #define SMBXSRV_CLIENT_GLOBAL_TDB_KEY_SIZE 16
111 static TDB_DATA
smbXsrv_client_global_id_to_key(const struct GUID
*client_guid
,
114 TDB_DATA key
= { .dsize
= 0, };
116 struct GUID_ndr_buf buf
= { .buf
= {0}, };
118 status
= GUID_to_ndr_buf(client_guid
, &buf
);
119 if (!NT_STATUS_IS_OK(status
)) {
122 memcpy(key_buf
, buf
.buf
, SMBXSRV_CLIENT_GLOBAL_TDB_KEY_SIZE
);
124 key
= make_tdb_data(key_buf
, SMBXSRV_CLIENT_GLOBAL_TDB_KEY_SIZE
);
129 static struct db_record
*smbXsrv_client_global_fetch_locked(
130 struct db_context
*db
,
131 const struct GUID
*client_guid
,
135 uint8_t key_buf
[SMBXSRV_CLIENT_GLOBAL_TDB_KEY_SIZE
];
136 struct db_record
*rec
= NULL
;
138 key
= smbXsrv_client_global_id_to_key(client_guid
, key_buf
);
140 rec
= dbwrap_fetch_locked(db
, mem_ctx
, key
);
143 struct GUID_txt_buf buf
;
144 DBG_DEBUG("Failed to lock guid [%s], key '%s'\n",
145 GUID_buf_string(client_guid
, &buf
),
146 hex_encode_talloc(talloc_tos(), key
.dptr
, key
.dsize
));
152 static NTSTATUS
smbXsrv_client_table_create(TALLOC_CTX
*mem_ctx
,
153 struct messaging_context
*msg_ctx
,
154 uint32_t max_clients
,
155 struct smbXsrv_client_table
**_table
)
157 struct smbXsrv_client_table
*table
;
160 if (max_clients
> 1) {
161 return NT_STATUS_INTERNAL_ERROR
;
164 table
= talloc_zero(mem_ctx
, struct smbXsrv_client_table
);
166 return NT_STATUS_NO_MEMORY
;
169 table
->local
.max_clients
= max_clients
;
171 status
= smbXsrv_client_global_init();
172 if (!NT_STATUS_IS_OK(status
)) {
177 table
->global
.db_ctx
= smbXsrv_client_global_db_ctx
;
183 static int smbXsrv_client_global_destructor(struct smbXsrv_client_global0
*global
)
188 static void smbXsrv_client_global_verify_record(struct db_record
*db_rec
,
192 struct smbXsrv_client_global0
**_g
,
198 struct smbXsrv_client_globalB global_blob
;
199 enum ndr_err_code ndr_err
;
200 struct smbXsrv_client_global0
*global
= NULL
;
202 TALLOC_CTX
*frame
= talloc_stackframe();
216 key
= dbwrap_record_get_key(db_rec
);
218 val
= dbwrap_record_get_value(db_rec
);
219 if (val
.dsize
== 0) {
228 blob
= data_blob_const(val
.dptr
, val
.dsize
);
230 ndr_err
= ndr_pull_struct_blob(&blob
, frame
, &global_blob
,
231 (ndr_pull_flags_fn_t
)ndr_pull_smbXsrv_client_globalB
);
232 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
233 NTSTATUS status
= ndr_map_error2ntstatus(ndr_err
);
234 DBG_WARNING("key '%s' ndr_pull_struct_blob - %s\n",
235 hex_encode_talloc(frame
, key
.dptr
, key
.dsize
),
241 DBG_DEBUG("client_global:\n");
242 if (DEBUGLVL(DBGLVL_DEBUG
)) {
243 NDR_PRINT_DEBUG(smbXsrv_client_globalB
, &global_blob
);
246 if (global_blob
.version
!= SMBXSRV_VERSION_0
) {
247 DBG_ERR("key '%s' use unsupported version %u\n",
248 hex_encode_talloc(frame
, key
.dptr
, key
.dsize
),
249 global_blob
.version
);
250 NDR_PRINT_DEBUG(smbXsrv_client_globalB
, &global_blob
);
255 global
= global_blob
.info
.info0
;
257 exists
= serverid_exists(&global
->server_id
);
259 struct server_id_buf tmp
;
261 DBG_NOTICE("key '%s' server_id %s does not exist.\n",
262 hex_encode_talloc(frame
, key
.dptr
, key
.dsize
),
263 server_id_str_buf(global
->server_id
, &tmp
));
264 if (DEBUGLVL(DBGLVL_NOTICE
)) {
265 NDR_PRINT_DEBUG(smbXsrv_client_globalB
, &global_blob
);
268 dbwrap_record_delete(db_rec
);
274 *_g
= talloc_move(mem_ctx
, &global
);
277 *pseqnum
= global_blob
.seqnum
;
282 static NTSTATUS
smb2srv_client_connection_pass(struct smbd_smb2_request
*smb2req
,
283 struct smbXsrv_client_global0
*global
)
286 enum ndr_err_code ndr_err
;
288 struct smbXsrv_connection_pass0 pass_info0
;
289 struct smbXsrv_connection_passB pass_blob
;
293 pass_info0
= (struct smbXsrv_connection_pass0
) {
294 .client_guid
= global
->client_guid
,
295 .src_server_id
= smb2req
->xconn
->client
->global
->server_id
,
296 .xconn_connect_time
= smb2req
->xconn
->client
->global
->initial_connect_time
,
297 .dst_server_id
= global
->server_id
,
298 .client_connect_time
= global
->initial_connect_time
,
301 reqlen
= iov_buflen(smb2req
->in
.vector
, smb2req
->in
.vector_count
);
303 return NT_STATUS_INVALID_BUFFER_SIZE
;
306 pass_info0
.negotiate_request
.length
= reqlen
;
307 pass_info0
.negotiate_request
.data
= talloc_array(talloc_tos(), uint8_t,
309 if (pass_info0
.negotiate_request
.data
== NULL
) {
310 return NT_STATUS_NO_MEMORY
;
312 iov_buf(smb2req
->in
.vector
, smb2req
->in
.vector_count
,
313 pass_info0
.negotiate_request
.data
,
314 pass_info0
.negotiate_request
.length
);
316 ZERO_STRUCT(pass_blob
);
317 pass_blob
.version
= smbXsrv_version_global_current();
318 pass_blob
.info
.info0
= &pass_info0
;
320 if (DEBUGLVL(DBGLVL_DEBUG
)) {
321 NDR_PRINT_DEBUG(smbXsrv_connection_passB
, &pass_blob
);
324 ndr_err
= ndr_push_struct_blob(&blob
, talloc_tos(), &pass_blob
,
325 (ndr_push_flags_fn_t
)ndr_push_smbXsrv_connection_passB
);
326 data_blob_free(&pass_info0
.negotiate_request
);
327 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
328 status
= ndr_map_error2ntstatus(ndr_err
);
332 iov
.iov_base
= blob
.data
;
333 iov
.iov_len
= blob
.length
;
335 status
= messaging_send_iov(smb2req
->xconn
->client
->msg_ctx
,
337 MSG_SMBXSRV_CONNECTION_PASS
,
339 &smb2req
->xconn
->transport
.sock
, 1);
340 data_blob_free(&blob
);
341 if (!NT_STATUS_IS_OK(status
)) {
348 static NTSTATUS
smb2srv_client_connection_drop(struct smbd_smb2_request
*smb2req
,
349 struct smbXsrv_client_global0
*global
)
352 enum ndr_err_code ndr_err
;
354 struct smbXsrv_connection_drop0 drop_info0
;
355 struct smbXsrv_connection_dropB drop_blob
;
358 drop_info0
= (struct smbXsrv_connection_drop0
) {
359 .client_guid
= global
->client_guid
,
360 .src_server_id
= smb2req
->xconn
->client
->global
->server_id
,
361 .xconn_connect_time
= smb2req
->xconn
->client
->global
->initial_connect_time
,
362 .dst_server_id
= global
->server_id
,
363 .client_connect_time
= global
->initial_connect_time
,
366 ZERO_STRUCT(drop_blob
);
367 drop_blob
.version
= smbXsrv_version_global_current();
368 drop_blob
.info
.info0
= &drop_info0
;
370 if (DEBUGLVL(DBGLVL_DEBUG
)) {
371 NDR_PRINT_DEBUG(smbXsrv_connection_dropB
, &drop_blob
);
374 ndr_err
= ndr_push_struct_blob(&blob
, talloc_tos(), &drop_blob
,
375 (ndr_push_flags_fn_t
)ndr_push_smbXsrv_connection_dropB
);
376 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
377 status
= ndr_map_error2ntstatus(ndr_err
);
381 iov
.iov_base
= blob
.data
;
382 iov
.iov_len
= blob
.length
;
384 status
= messaging_send_iov(smb2req
->xconn
->client
->msg_ctx
,
386 MSG_SMBXSRV_CONNECTION_DROP
,
389 data_blob_free(&blob
);
390 if (!NT_STATUS_IS_OK(status
)) {
397 static NTSTATUS
smbXsrv_client_global_store(struct smbXsrv_client_global0
*global
)
399 struct smbXsrv_client_globalB global_blob
;
400 DATA_BLOB blob
= data_blob_null
;
404 enum ndr_err_code ndr_err
;
405 bool saved_stored
= global
->stored
;
408 * TODO: if we use other versions than '0'
409 * we would add glue code here, that would be able to
410 * store the information in the old format.
413 SMB_ASSERT(global
->local_address
!= NULL
);
414 SMB_ASSERT(global
->remote_address
!= NULL
);
415 SMB_ASSERT(global
->remote_name
!= NULL
);
417 if (global
->db_rec
== NULL
) {
418 return NT_STATUS_INTERNAL_ERROR
;
421 key
= dbwrap_record_get_key(global
->db_rec
);
422 val
= dbwrap_record_get_value(global
->db_rec
);
424 ZERO_STRUCT(global_blob
);
425 global_blob
.version
= smbXsrv_version_global_current();
426 if (val
.dsize
>= 8) {
427 global_blob
.seqnum
= IVAL(val
.dptr
, 4);
429 global_blob
.seqnum
+= 1;
430 global_blob
.info
.info0
= global
;
432 global
->stored
= true;
433 ndr_err
= ndr_push_struct_blob(&blob
, global
->db_rec
, &global_blob
,
434 (ndr_push_flags_fn_t
)ndr_push_smbXsrv_client_globalB
);
435 global
->stored
= saved_stored
;
436 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
437 status
= ndr_map_error2ntstatus(ndr_err
);
438 DBG_WARNING("key '%s' ndr_push - %s\n",
439 hex_encode_talloc(global
->db_rec
, key
.dptr
, key
.dsize
),
441 TALLOC_FREE(global
->db_rec
);
445 val
= make_tdb_data(blob
.data
, blob
.length
);
446 status
= dbwrap_record_store(global
->db_rec
, val
, TDB_REPLACE
);
447 if (!NT_STATUS_IS_OK(status
)) {
448 DBG_WARNING("key '%s' store - %s\n",
449 hex_encode_talloc(global
->db_rec
, key
.dptr
, key
.dsize
),
451 TALLOC_FREE(global
->db_rec
);
455 global
->stored
= true;
457 if (DEBUGLVL(DBGLVL_DEBUG
)) {
458 DBG_DEBUG("key '%s' stored\n",
459 hex_encode_talloc(global
->db_rec
, key
.dptr
, key
.dsize
));
460 NDR_PRINT_DEBUG(smbXsrv_client_globalB
, &global_blob
);
463 TALLOC_FREE(global
->db_rec
);
468 struct smb2srv_client_mc_negprot_state
{
469 struct tevent_context
*ev
;
470 struct smbd_smb2_request
*smb2req
;
471 struct db_record
*db_rec
;
472 uint64_t watch_instance
;
473 uint32_t last_seqnum
;
474 struct tevent_req
*filter_subreq
;
477 static void smb2srv_client_mc_negprot_cleanup(struct tevent_req
*req
,
478 enum tevent_req_state req_state
)
480 struct smb2srv_client_mc_negprot_state
*state
=
482 struct smb2srv_client_mc_negprot_state
);
484 if (state
->db_rec
!= NULL
) {
485 dbwrap_watched_watch_remove_instance(state
->db_rec
,
486 state
->watch_instance
);
487 state
->watch_instance
= 0;
488 TALLOC_FREE(state
->db_rec
);
492 static void smb2srv_client_mc_negprot_next(struct tevent_req
*req
);
493 static bool smb2srv_client_mc_negprot_filter(struct messaging_rec
*rec
, void *private_data
);
494 static void smb2srv_client_mc_negprot_done(struct tevent_req
*subreq
);
495 static void smb2srv_client_mc_negprot_watched(struct tevent_req
*subreq
);
497 struct tevent_req
*smb2srv_client_mc_negprot_send(TALLOC_CTX
*mem_ctx
,
498 struct tevent_context
*ev
,
499 struct smbd_smb2_request
*smb2req
)
501 struct tevent_req
*req
= NULL
;
502 struct smb2srv_client_mc_negprot_state
*state
= NULL
;
504 req
= tevent_req_create(mem_ctx
, &state
,
505 struct smb2srv_client_mc_negprot_state
);
510 state
->smb2req
= smb2req
;
512 tevent_req_set_cleanup_fn(req
, smb2srv_client_mc_negprot_cleanup
);
514 smb2srv_client_mc_negprot_next(req
);
516 if (!tevent_req_is_in_progress(req
)) {
517 return tevent_req_post(req
, ev
);
523 static void smb2srv_client_mc_negprot_next(struct tevent_req
*req
)
525 struct smb2srv_client_mc_negprot_state
*state
=
527 struct smb2srv_client_mc_negprot_state
);
528 struct smbXsrv_connection
*xconn
= state
->smb2req
->xconn
;
529 struct smbXsrv_client
*client
= xconn
->client
;
530 struct smbXsrv_client_table
*table
= client
->table
;
531 struct GUID client_guid
= xconn
->smb2
.client
.guid
;
532 struct smbXsrv_client_global0
*global
= NULL
;
533 bool is_free
= false;
534 struct tevent_req
*subreq
= NULL
;
538 TALLOC_FREE(state
->filter_subreq
);
539 SMB_ASSERT(state
->db_rec
== NULL
);
540 state
->db_rec
= smbXsrv_client_global_fetch_locked(table
->global
.db_ctx
,
543 if (state
->db_rec
== NULL
) {
544 tevent_req_nterror(req
, NT_STATUS_INTERNAL_DB_ERROR
);
548 smbXsrv_client_global_verify_record(state
->db_rec
,
555 dbwrap_watched_watch_remove_instance(state
->db_rec
,
556 state
->watch_instance
);
557 state
->watch_instance
= 0;
560 * This stores the new client information in
561 * smbXsrv_client_global.tdb
563 client
->global
->client_guid
= xconn
->smb2
.client
.guid
;
565 client
->global
->db_rec
= state
->db_rec
;
566 state
->db_rec
= NULL
;
567 status
= smbXsrv_client_global_store(client
->global
);
568 SMB_ASSERT(client
->global
->db_rec
== NULL
);
569 if (!NT_STATUS_IS_OK(status
)) {
570 struct GUID_txt_buf buf
;
571 DBG_ERR("client_guid[%s] store failed - %s\n",
572 GUID_buf_string(&client
->global
->client_guid
,
575 tevent_req_nterror(req
, status
);
579 if (DEBUGLVL(DBGLVL_DEBUG
)) {
580 struct smbXsrv_clientB client_blob
= {
581 .version
= SMBXSRV_VERSION_0
,
582 .info
.info0
= client
,
584 struct GUID_txt_buf buf
;
586 DBG_DEBUG("client_guid[%s] stored\n",
587 GUID_buf_string(&client
->global
->client_guid
,
589 NDR_PRINT_DEBUG(smbXsrv_clientB
, &client_blob
);
592 xconn
->smb2
.client
.guid_verified
= true;
593 tevent_req_done(req
);
597 if (global
== NULL
) {
599 * most likely ndr_pull_struct_blob() failed
601 tevent_req_nterror(req
, NT_STATUS_INTERNAL_DB_CORRUPTION
);
605 if (procid_is_local(&global
->server_id
)) {
606 subreq
= messaging_filtered_read_send(state
,
609 smb2srv_client_mc_negprot_filter
,
611 if (tevent_req_nomem(subreq
, req
)) {
614 tevent_req_set_callback(subreq
, smb2srv_client_mc_negprot_done
, req
);
615 state
->filter_subreq
= subreq
;
618 if (procid_is_local(&global
->server_id
)) {
619 status
= smb2srv_client_connection_pass(state
->smb2req
,
621 if (tevent_req_nterror(req
, status
)) {
625 status
= smb2srv_client_connection_drop(state
->smb2req
,
627 if (tevent_req_nterror(req
, status
)) {
633 * If the record changed, but we are not happy with the change yet,
634 * we better remove ourself from the waiter list
635 * (most likely the first position)
636 * and re-add us at the end of the list.
638 * This gives other waiters a change
641 * Otherwise we'll keep our waiter instance alive,
642 * keep waiting (most likely at first position).
643 * It means the order of watchers stays fair.
645 if (state
->last_seqnum
!= seqnum
) {
646 state
->last_seqnum
= seqnum
;
647 dbwrap_watched_watch_remove_instance(state
->db_rec
,
648 state
->watch_instance
);
649 state
->watch_instance
=
650 dbwrap_watched_watch_add_instance(state
->db_rec
);
653 subreq
= dbwrap_watched_watch_send(state
,
656 state
->watch_instance
,
658 if (tevent_req_nomem(subreq
, req
)) {
661 tevent_req_set_callback(subreq
, smb2srv_client_mc_negprot_watched
, req
);
664 TALLOC_FREE(state
->db_rec
);
668 static bool smb2srv_client_mc_negprot_filter(struct messaging_rec
*rec
, void *private_data
)
670 if (rec
->msg_type
!= MSG_SMBXSRV_CONNECTION_PASSED
) {
674 if (rec
->num_fds
!= 0) {
681 static void smb2srv_client_mc_negprot_done(struct tevent_req
*subreq
)
683 struct tevent_req
*req
=
684 tevent_req_callback_data(subreq
,
686 struct smb2srv_client_mc_negprot_state
*state
=
688 struct smb2srv_client_mc_negprot_state
);
689 struct smbXsrv_connection
*xconn
= state
->smb2req
->xconn
;
690 struct smbXsrv_client
*client
= xconn
->client
;
691 struct messaging_rec
*rec
= NULL
;
692 struct smbXsrv_connection_passB passed_blob
;
693 enum ndr_err_code ndr_err
;
694 struct smbXsrv_connection_pass0
*passed_info0
= NULL
;
698 SMB_ASSERT(state
->filter_subreq
== subreq
);
699 state
->filter_subreq
= NULL
;
701 ret
= messaging_filtered_read_recv(subreq
, state
, &rec
);
704 status
= map_nt_error_from_unix_common(ret
);
705 DBG_ERR("messaging_filtered_read_recv() - %s\n",
707 tevent_req_nterror(req
, status
);
711 DBG_DEBUG("MSG_SMBXSRV_CONNECTION_PASSED: received...\n");
713 ndr_err
= ndr_pull_struct_blob(&rec
->buf
, rec
, &passed_blob
,
714 (ndr_pull_flags_fn_t
)ndr_pull_smbXsrv_connection_passB
);
715 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
716 status
= ndr_map_error2ntstatus(ndr_err
);
717 DBG_ERR("ndr_pull_struct_blob - %s\n", nt_errstr(status
));
718 tevent_req_nterror(req
, status
);
722 if (DEBUGLVL(DBGLVL_DEBUG
)) {
723 NDR_PRINT_DEBUG(smbXsrv_connection_passB
, &passed_blob
);
726 if (passed_blob
.version
!= SMBXSRV_VERSION_0
) {
727 DBG_ERR("ignore invalid version %u\n", passed_blob
.version
);
728 NDR_PRINT_DEBUG(smbXsrv_connection_passB
, &passed_blob
);
729 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
733 passed_info0
= passed_blob
.info
.info0
;
734 if (passed_info0
== NULL
) {
735 DBG_ERR("ignore NULL info %u\n", passed_blob
.version
);
736 NDR_PRINT_DEBUG(smbXsrv_connection_passB
, &passed_blob
);
737 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
741 if (!GUID_equal(&xconn
->smb2
.client
.guid
, &passed_info0
->client_guid
)) {
742 struct GUID_txt_buf buf1
, buf2
;
744 DBG_ERR("client's client_guid [%s] != passed guid [%s]\n",
745 GUID_buf_string(&xconn
->smb2
.client
.guid
,
747 GUID_buf_string(&passed_info0
->client_guid
,
749 NDR_PRINT_DEBUG(smbXsrv_connection_passB
, &passed_blob
);
750 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
754 if (client
->global
->initial_connect_time
!=
755 passed_info0
->xconn_connect_time
)
757 DBG_ERR("client's initial connect time [%s] (%llu) != "
758 "passed xconn connect time [%s] (%llu)\n",
759 nt_time_string(talloc_tos(),
760 client
->global
->initial_connect_time
),
761 (unsigned long long)client
->global
->initial_connect_time
,
762 nt_time_string(talloc_tos(),
763 passed_info0
->xconn_connect_time
),
764 (unsigned long long)passed_info0
->xconn_connect_time
);
765 NDR_PRINT_DEBUG(smbXsrv_connection_passB
, &passed_blob
);
766 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
770 if (passed_info0
->negotiate_request
.length
!= 0) {
771 DBG_ERR("negotiate_request.length[%zu]\n",
772 passed_info0
->negotiate_request
.length
);
773 NDR_PRINT_DEBUG(smbXsrv_connection_passB
, &passed_blob
);
774 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
778 tevent_req_nterror(req
, NT_STATUS_MESSAGE_RETRIEVED
);
781 static void smb2srv_client_mc_negprot_watched(struct tevent_req
*subreq
)
783 struct tevent_req
*req
=
784 tevent_req_callback_data(subreq
,
786 struct smb2srv_client_mc_negprot_state
*state
=
788 struct smb2srv_client_mc_negprot_state
);
790 uint64_t instance
= 0;
792 status
= dbwrap_watched_watch_recv(subreq
, &instance
, NULL
, NULL
);
794 if (tevent_req_nterror(req
, status
)) {
798 state
->watch_instance
= instance
;
800 smb2srv_client_mc_negprot_next(req
);
803 NTSTATUS
smb2srv_client_mc_negprot_recv(struct tevent_req
*req
)
805 return tevent_req_simple_recv_ntstatus(req
);
808 static NTSTATUS
smbXsrv_client_global_remove(struct smbXsrv_client_global0
*global
)
814 * TODO: if we use other versions than '0'
815 * we would add glue code here, that would be able to
816 * store the information in the old format.
819 if (global
->db_rec
== NULL
) {
820 return NT_STATUS_INTERNAL_ERROR
;
823 key
= dbwrap_record_get_key(global
->db_rec
);
825 status
= dbwrap_record_delete(global
->db_rec
);
826 if (!NT_STATUS_IS_OK(status
)) {
827 DBG_WARNING("key '%s' delete - %s\n",
828 hex_encode_talloc(global
->db_rec
, key
.dptr
, key
.dsize
),
830 TALLOC_FREE(global
->db_rec
);
833 global
->stored
= false;
834 DBG_DEBUG("key '%s' delete\n",
835 hex_encode_talloc(global
->db_rec
, key
.dptr
, key
.dsize
));
837 TALLOC_FREE(global
->db_rec
);
842 static int smbXsrv_client_destructor(struct smbXsrv_client
*client
)
846 status
= smbXsrv_client_remove(client
);
847 if (!NT_STATUS_IS_OK(status
)) {
848 DBG_ERR("smbXsrv_client_remove() failed: %s\n",
852 TALLOC_FREE(client
->global
);
857 static bool smbXsrv_client_connection_pass_filter(struct messaging_rec
*rec
, void *private_data
);
858 static void smbXsrv_client_connection_pass_loop(struct tevent_req
*subreq
);
859 static bool smbXsrv_client_connection_drop_filter(struct messaging_rec
*rec
, void *private_data
);
860 static void smbXsrv_client_connection_drop_loop(struct tevent_req
*subreq
);
862 NTSTATUS
smbXsrv_client_create(TALLOC_CTX
*mem_ctx
,
863 struct tevent_context
*ev_ctx
,
864 struct messaging_context
*msg_ctx
,
866 struct smbXsrv_client
**_client
)
868 struct smbXsrv_client_table
*table
;
869 struct smbXsrv_client
*client
= NULL
;
870 struct smbXsrv_client_global0
*global
= NULL
;
872 struct tevent_req
*subreq
= NULL
;
874 status
= smbXsrv_client_table_create(mem_ctx
,
878 if (!NT_STATUS_IS_OK(status
)) {
882 if (table
->local
.num_clients
>= table
->local
.max_clients
) {
884 return NT_STATUS_INSUFFICIENT_RESOURCES
;
887 client
= talloc_zero(mem_ctx
, struct smbXsrv_client
);
888 if (client
== NULL
) {
890 return NT_STATUS_NO_MEMORY
;
892 client
->raw_ev_ctx
= ev_ctx
;
893 client
->msg_ctx
= msg_ctx
;
895 client
->server_multi_channel_enabled
=
896 smbXsrv_server_multi_channel_enabled();
897 if (client
->server_multi_channel_enabled
) {
898 client
->next_channel_id
= 1;
900 client
->table
= talloc_move(client
, &table
);
901 table
= client
->table
;
903 global
= talloc_zero(client
, struct smbXsrv_client_global0
);
904 if (global
== NULL
) {
906 return NT_STATUS_NO_MEMORY
;
908 talloc_set_destructor(global
, smbXsrv_client_global_destructor
);
909 client
->global
= global
;
911 global
->initial_connect_time
= now
;
913 global
->server_id
= messaging_server_id(client
->msg_ctx
);
915 table
->local
.num_clients
+= 1;
917 talloc_set_destructor(client
, smbXsrv_client_destructor
);
919 if (DEBUGLVL(DBGLVL_DEBUG
)) {
920 struct smbXsrv_clientB client_blob
= {
921 .version
= SMBXSRV_VERSION_0
,
922 .info
.info0
= client
,
924 struct GUID_txt_buf buf
;
926 DBG_DEBUG("client_guid[%s] created\n",
927 GUID_buf_string(&global
->client_guid
, &buf
));
928 NDR_PRINT_DEBUG(smbXsrv_clientB
, &client_blob
);
931 subreq
= messaging_filtered_read_send(client
,
934 smbXsrv_client_connection_pass_filter
,
936 if (subreq
== NULL
) {
938 return NT_STATUS_NO_MEMORY
;
940 tevent_req_set_callback(subreq
, smbXsrv_client_connection_pass_loop
, client
);
941 client
->connection_pass_subreq
= subreq
;
943 subreq
= messaging_filtered_read_send(client
,
946 smbXsrv_client_connection_drop_filter
,
948 if (subreq
== NULL
) {
950 return NT_STATUS_NO_MEMORY
;
952 tevent_req_set_callback(subreq
, smbXsrv_client_connection_drop_loop
, client
);
953 client
->connection_drop_subreq
= subreq
;
959 static NTSTATUS
smb2srv_client_connection_passed(struct smbXsrv_client
*client
,
960 const struct smbXsrv_connection_pass0
*recv_info0
)
963 enum ndr_err_code ndr_err
;
965 struct smbXsrv_connection_pass0 passed_info0
;
966 struct smbXsrv_connection_passB passed_blob
;
970 * We echo back the message with a cleared negotiate_request
972 passed_info0
= *recv_info0
;
973 passed_info0
.negotiate_request
= data_blob_null
;
975 ZERO_STRUCT(passed_blob
);
976 passed_blob
.version
= smbXsrv_version_global_current();
977 passed_blob
.info
.info0
= &passed_info0
;
979 if (DEBUGLVL(DBGLVL_DEBUG
)) {
980 NDR_PRINT_DEBUG(smbXsrv_connection_passB
, &passed_blob
);
983 ndr_err
= ndr_push_struct_blob(&blob
, talloc_tos(), &passed_blob
,
984 (ndr_push_flags_fn_t
)ndr_push_smbXsrv_connection_passB
);
985 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
986 status
= ndr_map_error2ntstatus(ndr_err
);
990 iov
.iov_base
= blob
.data
;
991 iov
.iov_len
= blob
.length
;
993 status
= messaging_send_iov(client
->msg_ctx
,
994 recv_info0
->src_server_id
,
995 MSG_SMBXSRV_CONNECTION_PASSED
,
998 data_blob_free(&blob
);
999 if (!NT_STATUS_IS_OK(status
)) {
1003 return NT_STATUS_OK
;
1006 static bool smbXsrv_client_connection_pass_filter(struct messaging_rec
*rec
, void *private_data
)
1008 if (rec
->msg_type
!= MSG_SMBXSRV_CONNECTION_PASS
) {
1012 if (rec
->num_fds
!= 1) {
1019 static void smbXsrv_client_connection_pass_loop(struct tevent_req
*subreq
)
1021 struct smbXsrv_client
*client
=
1022 tevent_req_callback_data(subreq
,
1023 struct smbXsrv_client
);
1024 struct smbXsrv_connection
*xconn
= NULL
;
1026 struct messaging_rec
*rec
= NULL
;
1027 struct smbXsrv_connection_passB pass_blob
;
1028 enum ndr_err_code ndr_err
;
1029 struct smbXsrv_connection_pass0
*pass_info0
= NULL
;
1034 client
->connection_pass_subreq
= NULL
;
1036 ret
= messaging_filtered_read_recv(subreq
, talloc_tos(), &rec
);
1037 TALLOC_FREE(subreq
);
1042 if (rec
->num_fds
!= 1) {
1043 DBG_ERR("MSG_SMBXSRV_CONNECTION_PASS: num_fds[%u]\n",
1048 sock_fd
= rec
->fds
[0];
1049 DBG_DEBUG("MSG_SMBXSRV_CONNECTION_PASS: got sock_fd[%d]\n", sock_fd
);
1051 ndr_err
= ndr_pull_struct_blob(&rec
->buf
, rec
, &pass_blob
,
1052 (ndr_pull_flags_fn_t
)ndr_pull_smbXsrv_connection_passB
);
1053 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
1054 status
= ndr_map_error2ntstatus(ndr_err
);
1055 DBG_WARNING("ndr_pull_struct_blob - %s\n", nt_errstr(status
));
1059 if (DEBUGLVL(DBGLVL_DEBUG
)) {
1060 NDR_PRINT_DEBUG(smbXsrv_connection_passB
, &pass_blob
);
1063 if (pass_blob
.version
!= SMBXSRV_VERSION_0
) {
1064 DBG_ERR("ignore invalid version %u\n", pass_blob
.version
);
1065 NDR_PRINT_DEBUG(smbXsrv_connection_passB
, &pass_blob
);
1069 pass_info0
= pass_blob
.info
.info0
;
1070 if (pass_info0
== NULL
) {
1071 DBG_ERR("ignore NULL info %u\n", pass_blob
.version
);
1072 NDR_PRINT_DEBUG(smbXsrv_connection_passB
, &pass_blob
);
1076 if (!GUID_equal(&client
->global
->client_guid
, &pass_info0
->client_guid
))
1078 struct GUID_txt_buf buf1
, buf2
;
1080 DBG_WARNING("client's client_guid [%s] != passed guid [%s]\n",
1081 GUID_buf_string(&client
->global
->client_guid
,
1083 GUID_buf_string(&pass_info0
->client_guid
,
1085 if (DEBUGLVL(DBGLVL_WARNING
)) {
1086 NDR_PRINT_DEBUG(smbXsrv_connection_passB
, &pass_blob
);
1091 if (client
->global
->initial_connect_time
!=
1092 pass_info0
->client_connect_time
)
1094 DBG_WARNING("client's initial connect time [%s] (%llu) != "
1095 "passed initial connect time [%s] (%llu)\n",
1096 nt_time_string(talloc_tos(),
1097 client
->global
->initial_connect_time
),
1098 (unsigned long long)client
->global
->initial_connect_time
,
1099 nt_time_string(talloc_tos(),
1100 pass_info0
->client_connect_time
),
1101 (unsigned long long)pass_info0
->client_connect_time
);
1102 if (DEBUGLVL(DBGLVL_WARNING
)) {
1103 NDR_PRINT_DEBUG(smbXsrv_connection_passB
, &pass_blob
);
1108 if (pass_info0
->negotiate_request
.length
< SMB2_HDR_BODY
) {
1109 DBG_WARNING("negotiate_request.length[%zu]\n",
1110 pass_info0
->negotiate_request
.length
);
1111 if (DEBUGLVL(DBGLVL_WARNING
)) {
1112 NDR_PRINT_DEBUG(smbXsrv_connection_passB
, &pass_blob
);
1117 status
= smb2srv_client_connection_passed(client
, pass_info0
);
1118 if (NT_STATUS_EQUAL(status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
)) {
1120 * We hit a race where, the client dropped the connection
1121 * while the socket was passed to us and the origin
1122 * process already existed.
1124 DBG_DEBUG("smb2srv_client_connection_passed() ignore %s\n",
1126 status
= NT_STATUS_OK
;
1128 if (!NT_STATUS_IS_OK(status
)) {
1129 const char *r
= "smb2srv_client_connection_passed() failed";
1130 DBG_ERR("%s => %s\n", r
, nt_errstr(status
));
1131 NDR_PRINT_DEBUG(smbXsrv_connection_passB
, &pass_blob
);
1132 exit_server_cleanly(r
);
1136 status
= smbd_add_connection(client
,
1138 pass_info0
->xconn_connect_time
,
1140 if (NT_STATUS_EQUAL(status
, NT_STATUS_NETWORK_ACCESS_DENIED
)) {
1142 smbd_server_connection_terminate(xconn
, nt_errstr(status
));
1144 if (!NT_STATUS_IS_OK(status
)) {
1145 DBG_ERR("smbd_add_connection => %s\n", nt_errstr(status
));
1146 NDR_PRINT_DEBUG(smbXsrv_connection_passB
, &pass_blob
);
1152 * Set seq_low to mid received in negprot
1154 seq_low
= BVAL(pass_info0
->negotiate_request
.data
,
1155 SMB2_HDR_MESSAGE_ID
);
1157 xconn
->smb2
.client
.guid_verified
= true;
1158 smbd_smb2_process_negprot(xconn
, seq_low
,
1159 pass_info0
->negotiate_request
.data
,
1160 pass_info0
->negotiate_request
.length
);
1166 for (fd_idx
= 0; fd_idx
< rec
->num_fds
; fd_idx
++) {
1167 sock_fd
= rec
->fds
[fd_idx
];
1175 subreq
= messaging_filtered_read_send(client
,
1178 smbXsrv_client_connection_pass_filter
,
1180 if (subreq
== NULL
) {
1182 r
= "messaging_read_send(MSG_SMBXSRV_CONNECTION_PASS failed";
1183 exit_server_cleanly(r
);
1186 tevent_req_set_callback(subreq
, smbXsrv_client_connection_pass_loop
, client
);
1187 client
->connection_pass_subreq
= subreq
;
1190 static bool smbXsrv_client_connection_drop_filter(struct messaging_rec
*rec
, void *private_data
)
1192 if (rec
->msg_type
!= MSG_SMBXSRV_CONNECTION_DROP
) {
1196 if (rec
->num_fds
!= 0) {
1203 static void smbXsrv_client_connection_drop_loop(struct tevent_req
*subreq
)
1205 struct smbXsrv_client
*client
=
1206 tevent_req_callback_data(subreq
,
1207 struct smbXsrv_client
);
1209 struct messaging_rec
*rec
= NULL
;
1210 struct smbXsrv_connection_dropB drop_blob
;
1211 enum ndr_err_code ndr_err
;
1212 struct smbXsrv_connection_drop0
*drop_info0
= NULL
;
1213 struct server_id_buf src_server_id_buf
= {};
1216 client
->connection_drop_subreq
= NULL
;
1218 ret
= messaging_filtered_read_recv(subreq
, talloc_tos(), &rec
);
1219 TALLOC_FREE(subreq
);
1224 if (rec
->num_fds
!= 0) {
1225 DBG_ERR("MSG_SMBXSRV_CONNECTION_DROP: num_fds[%u]\n",
1230 ndr_err
= ndr_pull_struct_blob(&rec
->buf
, rec
, &drop_blob
,
1231 (ndr_pull_flags_fn_t
)ndr_pull_smbXsrv_connection_dropB
);
1232 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
1233 status
= ndr_map_error2ntstatus(ndr_err
);
1234 DBG_WARNING("ndr_pull_struct_blob - %s\n", nt_errstr(status
));
1238 if (DEBUGLVL(DBGLVL_DEBUG
)) {
1239 NDR_PRINT_DEBUG(smbXsrv_connection_dropB
, &drop_blob
);
1242 if (drop_blob
.version
!= SMBXSRV_VERSION_0
) {
1243 DBG_ERR("ignore invalid version %u\n", drop_blob
.version
);
1244 NDR_PRINT_DEBUG(smbXsrv_connection_dropB
, &drop_blob
);
1248 drop_info0
= drop_blob
.info
.info0
;
1249 if (drop_info0
== NULL
) {
1250 DBG_ERR("ignore NULL info %u\n", drop_blob
.version
);
1251 NDR_PRINT_DEBUG(smbXsrv_connection_dropB
, &drop_blob
);
1255 if (!GUID_equal(&client
->global
->client_guid
, &drop_info0
->client_guid
))
1257 struct GUID_txt_buf buf1
, buf2
;
1259 DBG_WARNING("client's client_guid [%s] != droped guid [%s]\n",
1260 GUID_buf_string(&client
->global
->client_guid
,
1262 GUID_buf_string(&drop_info0
->client_guid
,
1264 if (DEBUGLVL(DBGLVL_WARNING
)) {
1265 NDR_PRINT_DEBUG(smbXsrv_connection_dropB
, &drop_blob
);
1270 if (client
->global
->initial_connect_time
!=
1271 drop_info0
->client_connect_time
)
1273 DBG_WARNING("client's initial connect time [%s] (%llu) != "
1274 "droped initial connect time [%s] (%llu)\n",
1275 nt_time_string(talloc_tos(),
1276 client
->global
->initial_connect_time
),
1277 (unsigned long long)client
->global
->initial_connect_time
,
1278 nt_time_string(talloc_tos(),
1279 drop_info0
->client_connect_time
),
1280 (unsigned long long)drop_info0
->client_connect_time
);
1281 if (DEBUGLVL(DBGLVL_WARNING
)) {
1282 NDR_PRINT_DEBUG(smbXsrv_connection_dropB
, &drop_blob
);
1288 * Disconnect all client connections, which means we will tear down all
1289 * sessions, tcons and non-durable opens. At the end we will remove our
1290 * smbXsrv_client_global.tdb record, which will wake up the watcher on
1291 * the other node in order to let it take over the client.
1293 * The client will have to reopen all sessions, tcons and durable opens.
1295 smbd_server_disconnect_client(client
,
1296 server_id_str_buf(drop_info0
->src_server_id
, &src_server_id_buf
));
1304 for (fd_idx
= 0; fd_idx
< rec
->num_fds
; fd_idx
++) {
1305 sock_fd
= rec
->fds
[fd_idx
];
1313 subreq
= messaging_filtered_read_send(client
,
1316 smbXsrv_client_connection_drop_filter
,
1318 if (subreq
== NULL
) {
1320 r
= "messaging_read_send(MSG_SMBXSRV_CONNECTION_DROP failed";
1321 exit_server_cleanly(r
);
1324 tevent_req_set_callback(subreq
, smbXsrv_client_connection_drop_loop
, client
);
1325 client
->connection_drop_subreq
= subreq
;
1328 NTSTATUS
smbXsrv_client_remove(struct smbXsrv_client
*client
)
1330 struct smbXsrv_client_table
*table
= client
->table
;
1333 if (client
->global
->db_rec
!= NULL
) {
1334 struct GUID_txt_buf buf
;
1335 DBG_ERR("client_guid[%s]: Called with db_rec != NULL'\n",
1336 GUID_buf_string(&client
->global
->client_guid
,
1338 return NT_STATUS_INTERNAL_ERROR
;
1341 if (!client
->global
->stored
) {
1342 return NT_STATUS_OK
;
1345 TALLOC_FREE(client
->connection_pass_subreq
);
1346 TALLOC_FREE(client
->connection_drop_subreq
);
1348 client
->global
->db_rec
= smbXsrv_client_global_fetch_locked(
1349 table
->global
.db_ctx
,
1350 &client
->global
->client_guid
,
1351 client
->global
/* TALLOC_CTX */);
1352 if (client
->global
->db_rec
== NULL
) {
1353 return NT_STATUS_INTERNAL_DB_ERROR
;
1356 status
= smbXsrv_client_global_remove(client
->global
);
1357 if (!NT_STATUS_IS_OK(status
)) {
1358 struct GUID_txt_buf buf
;
1359 DBG_ERR("client_guid[%s] store failed - %s\n",
1360 GUID_buf_string(&client
->global
->client_guid
, &buf
),
1365 if (DEBUGLVL(DBGLVL_DEBUG
)) {
1366 struct smbXsrv_clientB client_blob
= {
1367 .version
= SMBXSRV_VERSION_0
,
1368 .info
.info0
= client
,
1370 struct GUID_txt_buf buf
;
1372 DBG_DEBUG("client_guid[%s] stored\n",
1373 GUID_buf_string(&client
->global
->client_guid
, &buf
));
1374 NDR_PRINT_DEBUG(smbXsrv_clientB
, &client_blob
);
1377 return NT_STATUS_OK
;