]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
libcli/smb: pass smbXcli_transport to smbXcli_conn_create()
authorStefan Metzmacher <metze@samba.org>
Wed, 7 May 2025 12:30:08 +0000 (14:30 +0200)
committerStefan Metzmacher <metze@samba.org>
Wed, 18 Jun 2025 17:52:37 +0000 (17:52 +0000)
Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Volker Lendecke <vl@samba.org>
libcli/smb/smbXcli_base.c
libcli/smb/smbXcli_base.h
source3/libsmb/clientgen.c
source3/torture/torture.c
source4/libcli/raw/clitransport.c
source4/libcli/smb2/transport.c
source4/libcli/smb_composite/connect_nego.c
source4/torture/smb2/multichannel.c

index 16b71ae34d2efb0845cdf451bb5534475bce38ba..326e52c83e0224aa1f3a0751fb45bc1e0684636b 100644 (file)
@@ -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) {
index 226ab5a7d8ff32a9d5dd253bd84a526c16305d3a..fa351ee60bdacdb09bedba0ae26500378c2ffac4 100644 (file)
@@ -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,
index f9d756fcd5fbcc9561994c485952191c19e9c14c..727cb0ed9912b05660e8119f702ddb33e245112c 100644 (file)
@@ -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,
index 3c2effcfa824656145912a4b9fd5bf8cebe986f2..90a2054e87ad6d0301ff80283f9feaf241fecb63 100644 (file)
@@ -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");
index 99c7897650784d5a437960b76b8481f8e5185bd8..0388499004d09a2eb367d255db6f68670e3ae98b 100644 (file)
@@ -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);
 
index 15d173c6c4d53ec638c651bef024b8f020c4ac2f..47d309bda8e4d79a516036bab708f40146941c10 100644 (file)
@@ -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);
 
index 4e55dae703321d1281440c3e9e86c9dd3b3d9a69..ca1718db8ea326870d1b07ccc8d1cf756b8f49b0 100644 (file)
@@ -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,
index fc131cc17a902976ecbf07e458a406a0974f3c86..4c7d77642f5d6aa5cd6651cb7201b4e2abb388f0 100644 (file)
@@ -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);
        }
 
        /*