From: Stefan Metzmacher Date: Wed, 23 Apr 2025 08:58:55 +0000 (+0200) Subject: s3:rpc_server: make sure we can bind to the same port on all ip addresses X-Git-Tag: samba-4.22.2~24 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6f21f9527d6f1c6046bfc8bb5495eef8edbcfb08;p=thirdparty%2Fsamba.git s3:rpc_server: make sure we can bind to the same port on all ip addresses BUG: https://bugzilla.samba.org/show_bug.cgi?id=15851 Signed-off-by: Stefan Metzmacher Reviewed-by: Andreas Schneider (cherry picked from commit 783ca9dc434bd1d18b762185ee936fcbcf292124) Autobuild-User(v4-22-test): Jule Anger Autobuild-Date(v4-22-test): Thu May 8 14:04:08 UTC 2025 on atb-devel-224 --- diff --git a/source3/rpc_server/rpc_sock_helper.c b/source3/rpc_server/rpc_sock_helper.c index 364b889d9b7..cc11e1221db 100644 --- a/source3/rpc_server/rpc_sock_helper.c +++ b/source3/rpc_server/rpc_sock_helper.c @@ -110,32 +110,16 @@ out: ********************************************************************/ static NTSTATUS dcesrv_create_ncacn_ip_tcp_socket( - const struct sockaddr_storage *ifss, uint16_t *port, int *out_fd) + const struct sockaddr_storage *ifss, + uint16_t port, + bool rebind, + int *out_fd) { int fd = -1; - if (*port == 0) { - static uint16_t low = 0; - uint16_t i; - - if (low == 0) { - low = lp_rpc_low_port(); - } - - for (i = low; i <= lp_rpc_high_port(); i++) { - fd = open_socket_in(SOCK_STREAM, ifss, i, false); - if (fd >= 0) { - *port = i; - low = i+1; - break; - } - } - } else { - fd = open_socket_in(SOCK_STREAM, ifss, *port, true); - } - + fd = open_socket_in(SOCK_STREAM, ifss, port, rebind); if (fd < 0) { - DBG_ERR("Failed to create socket on port %u!\n", *port); + DBG_ERR("Failed to create socket on port %u!\n", port); return map_nt_error_from_unix(-fd); } @@ -143,7 +127,7 @@ static NTSTATUS dcesrv_create_ncacn_ip_tcp_socket( set_socket_options(fd, "SO_KEEPALIVE"); set_socket_options(fd, lp_socket_options()); - DBG_DEBUG("Opened ncacn_ip_tcp socket fd %d for port %u\n", fd, *port); + DBG_DEBUG("Opened ncacn_ip_tcp socket fd %d for port %u\n", fd, port); *out_fd = fd; @@ -156,15 +140,24 @@ static NTSTATUS dcesrv_create_ncacn_ip_tcp_sockets( size_t *pnum_fds, int **pfds) { + static uint16_t next_low_port; + static uint16_t conf_high_port; uint16_t port = 0; + uint16_t highest_port = 0; char port_str[11]; const char *endpoint = NULL; size_t i = 0, num_fds; int *fds = NULL; struct samba_sockaddr *addrs = NULL; NTSTATUS status = NT_STATUS_INVALID_PARAMETER; + bool rebind = false; bool ok; + if (next_low_port == 0) { + next_low_port = lp_rpc_low_port(); + conf_high_port = lp_rpc_high_port(); + } + endpoint = dcerpc_binding_get_string_option(b, "endpoint"); if (endpoint != NULL) { port = atoi(endpoint); @@ -181,15 +174,21 @@ static NTSTATUS dcesrv_create_ncacn_ip_tcp_sockets( addrs = talloc_array(mem_ctx, struct samba_sockaddr, num_fds); if (addrs == NULL) { + num_fds = 0; /* nothing to close */ status = NT_STATUS_NO_MEMORY; goto fail; } fds = talloc_array(mem_ctx, int, num_fds); if (fds == NULL) { + num_fds = 0; /* nothing to close */ status = NT_STATUS_NO_MEMORY; goto fail; } + for (i=0; i 0) { - close(fds[i-1]); - i -= 1; + for (i=0; i