From: Stefan Metzmacher Date: Wed, 7 May 2025 15:11:49 +0000 (+0200) Subject: s3:libsmb: let smbsock_connect() return smbXcli_transport X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=e66db40555d76152c774f0d7fe66fb76ddeb63d2;p=thirdparty%2Fsamba.git s3:libsmb: let smbsock_connect() return smbXcli_transport Signed-off-by: Stefan Metzmacher Reviewed-by: Volker Lendecke --- diff --git a/source3/libsmb/smbsock_connect.c b/source3/libsmb/smbsock_connect.c index f962136a0fc..d7e67368584 100644 --- a/source3/libsmb/smbsock_connect.c +++ b/source3/libsmb/smbsock_connect.c @@ -758,12 +758,16 @@ NTSTATUS smbsock_connect(const struct sockaddr_storage *addr, const struct smb_transports *transports, const char *called_name, int called_type, const char *calling_name, int calling_type, - int *pfd, uint16_t *ret_port, int sec_timeout) + int sec_timeout, + TALLOC_CTX *mem_ctx, + struct smbXcli_transport **ptransport) { TALLOC_CTX *frame = talloc_stackframe(); struct tevent_context *ev; struct tevent_req *req; NTSTATUS status = NT_STATUS_NO_MEMORY; + int fd = -1; + uint16_t ret_port = 0; ev = samba_tevent_context_init(frame); if (ev == NULL) { @@ -783,7 +787,21 @@ NTSTATUS smbsock_connect(const struct sockaddr_storage *addr, if (!tevent_req_poll_ntstatus(req, ev, &status)) { goto fail; } - status = smbsock_connect_recv(req, pfd, ret_port); + status = smbsock_connect_recv(req, &fd, &ret_port); + if (NT_STATUS_IS_OK(status)) { + struct smb_transport tp = { + .type = SMB_TRANSPORT_TYPE_UNKNOWN, + .port = ret_port, + }; + + set_socket_options(fd, lp_socket_options()); + *ptransport = smbXcli_transport_bsd(mem_ctx, fd, &tp); + if (*ptransport == NULL) { + close(fd); + status = NT_STATUS_NO_MEMORY; + goto fail; + } + } fail: TALLOC_FREE(frame); return status; diff --git a/source3/libsmb/smbsock_connect.h b/source3/libsmb/smbsock_connect.h index 26c54ca0297..5e0b566aa01 100644 --- a/source3/libsmb/smbsock_connect.h +++ b/source3/libsmb/smbsock_connect.h @@ -43,8 +43,10 @@ NTSTATUS smbsock_connect(const struct sockaddr_storage *addr, const struct smb_transports *transports, const char *called_name, int called_type, const char *calling_name, int calling_type, - int *pfd, uint16_t *ret_port, int sec_timeout) - NONNULL(1) NONNULL(2) NONNULL(3) NONNULL(8); + int sec_timeout, + TALLOC_CTX *mem_ctx, + struct smbXcli_transport **ptransport) + NONNULL(1) NONNULL(2) NONNULL(3) NONNULL(10); struct tevent_req *smbsock_any_connect_send(TALLOC_CTX *mem_ctx, struct tevent_context *ev, diff --git a/source3/winbindd/winbindd_cm.c b/source3/winbindd/winbindd_cm.c index e8b28a0478b..cc0b47b0600 100644 --- a/source3/winbindd/winbindd_cm.c +++ b/source3/winbindd/winbindd_cm.c @@ -1427,7 +1427,7 @@ static bool get_dcs(TALLOC_CTX *mem_ctx, struct winbindd_domain *domain, static bool connect_preferred_dc(TALLOC_CTX *mem_ctx, struct winbindd_domain *domain, uint32_t request_flags, - int *fd) + struct smbXcli_transport **ptransport) { char *saf_servername = NULL; struct smb_transports ts = @@ -1509,13 +1509,14 @@ static bool connect_preferred_dc(TALLOC_CTX *mem_ctx, status = smbsock_connect(&domain->dcaddr, lp_ctx, &ts, domain->dcname, -1, NULL, -1, - fd, NULL, 10); + 10, NULL, ptransport); if (!NT_STATUS_IS_OK(status)) { winbind_add_failed_connection_entry(domain, domain->dcname, NT_STATUS_UNSUCCESSFUL); return false; } + talloc_reparent(NULL, mem_ctx, *ptransport); return true; fail: @@ -1553,9 +1554,7 @@ static bool find_dc(TALLOC_CTX *mem_ctx, smb_transports_parse("client smb transports", lp_client_smb_transports()); int i; - int fd = -1; size_t fd_index; - struct smb_transport tp = { .type = SMB_TRANSPORT_TYPE_UNKNOWN, }; struct smbXcli_transport *xtp = NULL; NTSTATUS status; @@ -1566,7 +1565,7 @@ static bool find_dc(TALLOC_CTX *mem_ctx, D_NOTICE("First try to connect to the closest DC (using server " "affinity cache). If this fails, try to lookup the DC using " "DNS afterwards.\n"); - ok = connect_preferred_dc(mem_ctx, domain, request_flags, &fd); + ok = connect_preferred_dc(mem_ctx, domain, request_flags, &xtp); if (ok) { goto return_transport; } @@ -1663,10 +1662,6 @@ static bool find_dc(TALLOC_CTX *mem_ctx, TALLOC_FREE(addrs); num_addrs = 0; - if (fd != -1) { - close(fd); - fd = -1; - } TALLOC_FREE(xtp); /* @@ -1677,15 +1672,6 @@ static bool find_dc(TALLOC_CTX *mem_ctx, goto again; return_transport: - if (fd != -1) { - set_socket_options(fd, lp_socket_options()); - xtp = smbXcli_transport_bsd(NULL, fd, &tp); - if (xtp == NULL) { - close(fd); - DBG_ERR("smbXcli_transport_bsd() failed\n"); - return false; - } - } *ptransport = talloc_move(mem_ctx, &xtp);