From: Stefan Metzmacher Date: Wed, 7 May 2025 12:30:08 +0000 (+0200) Subject: libcli/smb: pass smbXcli_transport to smbXcli_conn_create() X-Git-Tag: tdb-1.4.14~203 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=efd4089ef52efc16baed82e5b63581a177146402;p=thirdparty%2Fsamba.git libcli/smb: pass smbXcli_transport to smbXcli_conn_create() Signed-off-by: Stefan Metzmacher Reviewed-by: Volker Lendecke --- diff --git a/libcli/smb/smbXcli_base.c b/libcli/smb/smbXcli_base.c index 16b71ae34d2..326e52c83e0 100644 --- a/libcli/smb/smbXcli_base.c +++ b/libcli/smb/smbXcli_base.c @@ -386,7 +386,7 @@ static int smbXcli_conn_destructor(struct smbXcli_conn *conn) } struct smbXcli_conn *smbXcli_conn_create(TALLOC_CTX *mem_ctx, - int fd, + struct smbXcli_transport **ptransport, const char *remote_name, enum smb_signing_setting signing_state, uint32_t smb1_capabilities, @@ -394,7 +394,6 @@ struct smbXcli_conn *smbXcli_conn_create(TALLOC_CTX *mem_ctx, uint32_t smb2_capabilities, const struct smb311_capabilities *smb3_capabilities) { - struct smb_transport tp = { .type = SMB_TRANSPORT_TYPE_UNKNOWN, }; struct smbXcli_conn *conn = NULL; if (smb3_capabilities != NULL) { @@ -412,10 +411,7 @@ struct smbXcli_conn *smbXcli_conn_create(TALLOC_CTX *mem_ctx, return NULL; } - conn->transport = smbXcli_transport_bsd(conn, fd, &tp); - if (conn->transport == NULL) { - goto error; - } + conn->transport = talloc_move(conn, ptransport); conn->remote_name = talloc_strdup(conn, remote_name); if (conn->remote_name == NULL) { diff --git a/libcli/smb/smbXcli_base.h b/libcli/smb/smbXcli_base.h index 226ab5a7d8f..fa351ee60bd 100644 --- a/libcli/smb/smbXcli_base.h +++ b/libcli/smb/smbXcli_base.h @@ -47,7 +47,7 @@ struct smbXcli_transport *smbXcli_transport_bsd(TALLOC_CTX *mem_ctx, const struct smb_transport *tp); struct smbXcli_conn *smbXcli_conn_create(TALLOC_CTX *mem_ctx, - int fd, + struct smbXcli_transport **ptransport, const char *remote_name, enum smb_signing_setting signing_state, uint32_t smb1_capabilities, diff --git a/source3/libsmb/clientgen.c b/source3/libsmb/clientgen.c index f9d756fcd5f..727cb0ed991 100644 --- a/source3/libsmb/clientgen.c +++ b/source3/libsmb/clientgen.c @@ -70,6 +70,8 @@ struct cli_state *cli_state_create(TALLOC_CTX *mem_ctx, enum smb_signing_setting signing_state, int flags) { + struct smb_transport tp = { .type = SMB_TRANSPORT_TYPE_UNKNOWN, }; + struct smbXcli_transport *transport = NULL; struct cli_state *cli = NULL; bool use_spnego = lp_client_use_spnego(); bool force_dos_errors = false; @@ -193,7 +195,14 @@ struct cli_state *cli_state_create(TALLOC_CTX *mem_ctx, smb2_capabilities = SMB2_CAP_ALL; - cli->conn = smbXcli_conn_create(cli, fd, remote_name, + transport = smbXcli_transport_bsd(cli, fd, &tp); + if (transport == NULL) { + goto error; + } + + cli->conn = smbXcli_conn_create(cli, + &transport, + remote_name, signing_state, smb1_capabilities, &client_guid, diff --git a/source3/torture/torture.c b/source3/torture/torture.c index 3c2effcfa82..90a2054e87a 100644 --- a/source3/torture/torture.c +++ b/source3/torture/torture.c @@ -15004,6 +15004,11 @@ static bool run_smb1_truncated_sesssetup(int dummy) { struct tevent_context *ev; struct tevent_req *req; + struct smb_transport tp = { + .type = SMB_TRANSPORT_TYPE_TCP, + .port = 445, + }; + struct smbXcli_transport *xtp = NULL; struct smbXcli_conn *conn; struct sockaddr_storage ss; NTSTATUS status; @@ -15025,7 +15030,13 @@ static bool run_smb1_truncated_sesssetup(int dummy) return false; } - conn = smbXcli_conn_create(talloc_tos(), fd, host, SMB_SIGNING_OFF, 0, + xtp = smbXcli_transport_bsd(talloc_tos(), fd, &tp); + if (xtp == NULL) { + d_fprintf(stderr, "smbXcli_transport_bsd failed\n"); + return false; + } + + conn = smbXcli_conn_create(talloc_tos(), &xtp, host, SMB_SIGNING_OFF, 0, NULL, 0, NULL); if (conn == NULL) { d_fprintf(stderr, "smbXcli_conn_create failed\n"); @@ -15193,6 +15204,11 @@ static bool do_smb1_exit(TALLOC_CTX *mem_ctx, static bool run_smb1_negotiate_exit(int dummy) { struct tevent_context *ev; + struct smb_transport tp = { + .type = SMB_TRANSPORT_TYPE_TCP, + .port = 445, + }; + struct smbXcli_transport *xtp = NULL; struct smbXcli_conn *conn; struct sockaddr_storage ss; NTSTATUS status; @@ -15214,7 +15230,13 @@ static bool run_smb1_negotiate_exit(int dummy) return false; } - conn = smbXcli_conn_create(talloc_tos(), fd, host, SMB_SIGNING_OFF, 0, + xtp = smbXcli_transport_bsd(talloc_tos(), fd, &tp); + if (xtp == NULL) { + d_fprintf(stderr, "smbXcli_transport_bsd failed\n"); + return false; + } + + conn = smbXcli_conn_create(talloc_tos(), &xtp, host, SMB_SIGNING_OFF, 0, NULL, 0, NULL); if (conn == NULL) { d_fprintf(stderr, "smbXcli_conn_create failed\n"); @@ -15304,6 +15326,11 @@ static bool run_ign_bad_negprot(int dummy) { struct tevent_context *ev; struct tevent_req *req; + struct smb_transport tp = { + .type = SMB_TRANSPORT_TYPE_TCP, + .port = 445, + }; + struct smbXcli_transport *xtp = NULL; struct smbXcli_conn *conn; struct sockaddr_storage ss; NTSTATUS status; @@ -15325,7 +15352,13 @@ static bool run_ign_bad_negprot(int dummy) return false; } - conn = smbXcli_conn_create(talloc_tos(), fd, host, SMB_SIGNING_OFF, 0, + xtp = smbXcli_transport_bsd(talloc_tos(), fd, &tp); + if (xtp == NULL) { + d_fprintf(stderr, "smbXcli_transport_bsd failed\n"); + return false; + } + + conn = smbXcli_conn_create(talloc_tos(), &xtp, host, SMB_SIGNING_OFF, 0, NULL, 0, NULL); if (conn == NULL) { d_fprintf(stderr, "smbXcli_conn_create failed\n"); diff --git a/source4/libcli/raw/clitransport.c b/source4/libcli/raw/clitransport.c index 99c78976507..0388499004d 100644 --- a/source4/libcli/raw/clitransport.c +++ b/source4/libcli/raw/clitransport.c @@ -50,6 +50,8 @@ struct smbcli_transport *smbcli_transport_init(struct smbcli_socket *sock, struct smbcli_options *options) { struct smbcli_transport *transport; + struct smb_transport tp = { .type = SMB_TRANSPORT_TYPE_UNKNOWN, }; + struct smbXcli_transport *xtp = NULL; uint32_t smb1_capabilities; transport = talloc_zero(parent_ctx, struct smbcli_transport); @@ -90,8 +92,17 @@ struct smbcli_transport *smbcli_transport_init(struct smbcli_socket *sock, smb1_capabilities |= CAP_LEVEL_II_OPLOCKS; } + xtp = smbXcli_transport_bsd(transport, sock->sockfd, &tp); + if (xtp == NULL) { + TALLOC_FREE(sock); + TALLOC_FREE(transport); + return NULL; + } + sock->sockfd = -1; + TALLOC_FREE(sock); + transport->conn = smbXcli_conn_create(transport, - sock->sockfd, + &xtp, sock->hostname, options->signing, smb1_capabilities, @@ -99,12 +110,10 @@ struct smbcli_transport *smbcli_transport_init(struct smbcli_socket *sock, 0, /* smb2_capabilities */ NULL); /* smb3_ciphers */ if (transport->conn == NULL) { - TALLOC_FREE(sock); + TALLOC_FREE(xtp); TALLOC_FREE(transport); return NULL; } - sock->sockfd = -1; - TALLOC_FREE(sock); talloc_set_destructor(transport, transport_destructor); diff --git a/source4/libcli/smb2/transport.c b/source4/libcli/smb2/transport.c index 15d173c6c4d..47d309bda8e 100644 --- a/source4/libcli/smb2/transport.c +++ b/source4/libcli/smb2/transport.c @@ -48,6 +48,8 @@ struct smb2_transport *smb2_transport_init(struct smbcli_socket *sock, struct smbcli_options *options) { struct smb2_transport *transport; + struct smb_transport tp = { .type = SMB_TRANSPORT_TYPE_UNKNOWN, }; + struct smbXcli_transport *xtp = NULL; transport = talloc_zero(parent_ctx, struct smb2_transport); if (!transport) return NULL; @@ -63,8 +65,17 @@ struct smb2_transport *smb2_transport_init(struct smbcli_socket *sock, transport->options.max_protocol = PROTOCOL_LATEST; } + xtp = smbXcli_transport_bsd(transport, sock->sockfd, &tp); + if (xtp == NULL) { + TALLOC_FREE(sock); + TALLOC_FREE(transport); + return NULL; + } + sock->sockfd = -1; + TALLOC_FREE(sock); + transport->conn = smbXcli_conn_create(transport, - sock->sockfd, + &xtp, sock->hostname, options->signing, 0, /* smb1_capabilities */ @@ -72,11 +83,10 @@ struct smb2_transport *smb2_transport_init(struct smbcli_socket *sock, options->smb2_capabilities, &options->smb3_capabilities); if (transport->conn == NULL) { - talloc_free(transport); + TALLOC_FREE(xtp); + TALLOC_FREE(transport); return NULL; } - sock->sockfd = -1; - TALLOC_FREE(sock); talloc_set_destructor(transport, transport_destructor); diff --git a/source4/libcli/smb_composite/connect_nego.c b/source4/libcli/smb_composite/connect_nego.c index 4e55dae7033..ca1718db8ea 100644 --- a/source4/libcli/smb_composite/connect_nego.c +++ b/source4/libcli/smb_composite/connect_nego.c @@ -109,6 +109,8 @@ static void smb_connect_nego_connect_done(struct composite_context *creq) struct smb_connect_nego_state); struct tevent_req *subreq = NULL; struct smbcli_socket *sock = NULL; + struct smb_transport tp = { .type = SMB_TRANSPORT_TYPE_UNKNOWN, }; + struct smbXcli_transport *xtp = NULL; uint32_t smb1_capabilities; uint32_t timeout_msec = state->options.request_timeout * 1000; NTSTATUS status; @@ -143,8 +145,15 @@ static void smb_connect_nego_connect_done(struct composite_context *creq) smb1_capabilities |= CAP_LEVEL_II_OPLOCKS; } + xtp = smbXcli_transport_bsd(state, sock->sockfd, &tp); + if (tevent_req_nomem(xtp, req)) { + return; + } + sock->sockfd = -1; + TALLOC_FREE(sock); + state->conn = smbXcli_conn_create(state, - sock->sockfd, + &xtp, state->target_hostname, state->options.signing, smb1_capabilities, @@ -154,8 +163,6 @@ static void smb_connect_nego_connect_done(struct composite_context *creq) if (tevent_req_nomem(state->conn, req)) { return; } - sock->sockfd = -1; - TALLOC_FREE(sock); subreq = smbXcli_negprot_send(state, state->ev, diff --git a/source4/torture/smb2/multichannel.c b/source4/torture/smb2/multichannel.c index fc131cc17a9..4c7d77642f5 100644 --- a/source4/torture/smb2/multichannel.c +++ b/source4/torture/smb2/multichannel.c @@ -2549,6 +2549,11 @@ static bool test_multichannel_bug_15346(struct torture_context *tctx, struct socket_context *sock = NULL; uint16_t port = 445; struct smbcli_options options = transport1->options; + struct smb_transport tp = { + .type = SMB_TRANSPORT_TYPE_TCP, + .port = port, + }; + struct smbXcli_transport *xtp = NULL; conn->state = state; conn->idx = i; @@ -2563,8 +2568,14 @@ static bool test_multichannel_bug_15346(struct torture_context *tctx, torture_assert_ntstatus_ok_goto(tctx, status, ret, done, "socket_connect_multi failed"); + xtp = smbXcli_transport_bsd(state->conns, sock->fd, &tp); + torture_assert_goto(tctx, xtp != NULL, ret, done, + "smbXcli_transport_bsd failed"); + sock->fd = -1; + TALLOC_FREE(sock); + conn->smbXcli = smbXcli_conn_create(state->conns, - sock->fd, + &xtp, host, SMB_SIGNING_OFF, 0, @@ -2573,8 +2584,6 @@ static bool test_multichannel_bug_15346(struct torture_context *tctx, &options.smb3_capabilities); torture_assert_goto(tctx, conn->smbXcli != NULL, ret, done, "smbXcli_conn_create failed"); - sock->fd = -1; - TALLOC_FREE(sock); } /*