From: Stefan Metzmacher Date: Wed, 7 May 2025 14:46:55 +0000 (+0200) Subject: s4:libcli: let struct smbcli_socket hold struct smbXcli_transport instead of sockfd X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=7376dabd1558b9d40770e68ef51bba417f91ba3f;p=thirdparty%2Fsamba.git s4:libcli: let struct smbcli_socket hold struct smbXcli_transport instead of sockfd Signed-off-by: Stefan Metzmacher Reviewed-by: Volker Lendecke --- diff --git a/source4/libcli/raw/clisocket.c b/source4/libcli/raw/clisocket.c index a7779e4e0b5..27814617d2e 100644 --- a/source4/libcli/raw/clisocket.c +++ b/source4/libcli/raw/clisocket.c @@ -30,6 +30,7 @@ #include "lib/socket/socket.h" #include "libcli/resolve/resolve.h" #include "param/param.h" +#include "../libcli/smb/smbXcli_base.h" #include "libcli/raw/raw_proto.h" #include "../libcli/smb/read_smb.h" #include "lib/util/util_net.h" @@ -141,10 +142,7 @@ failed: static int smbcli_socket_destructor(struct smbcli_socket *sock) { - if (sock->sockfd != -1) { - close(sock->sockfd); - sock->sockfd = -1; - } + TALLOC_FREE(sock->transport); return 0; } @@ -237,6 +235,7 @@ static void smbcli_sock_connect_recv_conn(struct tevent_req *subreq) struct sock_connect_state *state = tevent_req_callback_data(subreq, struct sock_connect_state); + struct smb_transport tp = { .type = SMB_TRANSPORT_TYPE_UNKNOWN, }; int sockfd = -1; state->ctx->status = smbsock_any_connect_recv(subreq, @@ -253,7 +252,14 @@ static void smbcli_sock_connect_recv_conn(struct tevent_req *subreq) return; } - state->result->sockfd = sockfd; + state->result->transport = smbXcli_transport_bsd(state->result, + sockfd, + &tp); + if (composite_nomem(state->result->transport, state->ctx)) { + close(sockfd); + return; + } + state->result->hostname = talloc_steal(state->result, state->host_name); talloc_set_destructor(state->result, smbcli_socket_destructor); diff --git a/source4/libcli/raw/clitransport.c b/source4/libcli/raw/clitransport.c index 0388499004d..b774cce3f5b 100644 --- a/source4/libcli/raw/clitransport.c +++ b/source4/libcli/raw/clitransport.c @@ -50,8 +50,6 @@ 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); @@ -92,25 +90,16 @@ 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, - &xtp, + &sock->transport, sock->hostname, options->signing, smb1_capabilities, NULL, /* client_guid */ 0, /* smb2_capabilities */ NULL); /* smb3_ciphers */ + TALLOC_FREE(sock); if (transport->conn == NULL) { - TALLOC_FREE(xtp); TALLOC_FREE(transport); return NULL; } diff --git a/source4/libcli/raw/libcliraw.h b/source4/libcli/raw/libcliraw.h index a8c2891b08c..417939cd8ab 100644 --- a/source4/libcli/raw/libcliraw.h +++ b/source4/libcli/raw/libcliraw.h @@ -70,7 +70,7 @@ struct smbcli_negotiate { /* this is the context for a SMB socket associated with the socket itself */ struct smbcli_socket { - int sockfd; + struct smbXcli_transport *transport; /* the hostname we connected to */ const char *hostname; diff --git a/source4/libcli/smb2/transport.c b/source4/libcli/smb2/transport.c index 47d309bda8e..402b894babe 100644 --- a/source4/libcli/smb2/transport.c +++ b/source4/libcli/smb2/transport.c @@ -48,8 +48,6 @@ 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; @@ -65,25 +63,16 @@ 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, - &xtp, + &sock->transport, sock->hostname, options->signing, 0, /* smb1_capabilities */ &options->client_guid, options->smb2_capabilities, &options->smb3_capabilities); + TALLOC_FREE(sock); if (transport->conn == NULL) { - TALLOC_FREE(xtp); TALLOC_FREE(transport); return NULL; } diff --git a/source4/libcli/smb_composite/connect_nego.c b/source4/libcli/smb_composite/connect_nego.c index ca1718db8ea..1cb97eccb4f 100644 --- a/source4/libcli/smb_composite/connect_nego.c +++ b/source4/libcli/smb_composite/connect_nego.c @@ -109,8 +109,6 @@ 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; @@ -145,21 +143,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, - &xtp, + &sock->transport, state->target_hostname, state->options.signing, smb1_capabilities, &state->options.client_guid, state->options.smb2_capabilities, &state->options.smb3_capabilities); + TALLOC_FREE(sock); if (tevent_req_nomem(state->conn, req)) { return; }