From: Volker Lendecke Date: Fri, 21 Oct 2022 14:45:35 +0000 (+0200) Subject: lib: Add lp_allow_local_address() X-Git-Tag: talloc-2.4.0~559 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9321a533cdc3cbb81afa03bcf3cd5030b8b317ea;p=thirdparty%2Fsamba.git lib: Add lp_allow_local_address() Helper function for listing and accessing shares Signed-off-by: Volker Lendecke Reviewed-by: Stefan Metzmacher --- diff --git a/source3/include/proto.h b/source3/include/proto.h index f632cf37c08..8582830d558 100644 --- a/source3/include/proto.h +++ b/source3/include/proto.h @@ -765,6 +765,8 @@ void unbecome_root(void); /* The following definitions come from lib/smbd_shim.c */ int find_service(TALLOC_CTX *ctx, const char *service_in, char **p_service_out); +bool lp_allow_local_address( + int snum, const struct tsocket_address *local_address); void send_stat_cache_delete_message(struct messaging_context *msg_ctx, const char *name); NTSTATUS can_delete_directory_fsp(files_struct *fsp); diff --git a/source3/lib/util_sock.c b/source3/lib/util_sock.c index 85dc3efbe15..391b46f801e 100644 --- a/source3/lib/util_sock.c +++ b/source3/lib/util_sock.c @@ -30,6 +30,7 @@ #include "../lib/tsocket/tsocket.h" #include "lib/util/sys_rw.h" #include "lib/util/sys_rw_data.h" +#include "source3/lib/util_tsock.h" /**************************************************************************** Determine if a file descriptor is in fact a socket. diff --git a/source3/param/service.c b/source3/param/service.c index 880e79fb284..b06a2fbc734 100644 --- a/source3/param/service.c +++ b/source3/param/service.c @@ -264,3 +264,72 @@ int find_service(TALLOC_CTX *ctx, const char *service_in, char **p_service_out) return (iService); } + +bool lp_allow_local_address( + int snum, const struct tsocket_address *local_address) +{ + bool is_inet = tsocket_address_is_inet(local_address, "ip"); + const char **server_addresses = lp_server_addresses(snum); + char *local = NULL; + ssize_t i; + + if (!is_inet) { + return false; + } + + if (server_addresses == NULL) { + return true; + } + + local = tsocket_address_inet_addr_string(local_address, talloc_tos()); + if (local == NULL) { + return false; + } + + for (i=0; server_addresses[i] != NULL; i++) { + struct tsocket_address *server_addr = NULL; + char *server_addr_string = NULL; + bool equal; + int ret; + + /* + * Go through struct tsocket_address to normalize the + * string representation + */ + + ret = tsocket_address_inet_from_strings( + talloc_tos(), + "ip", + server_addresses[i], + 0, + &server_addr); + if (ret == -1) { + DBG_WARNING("tsocket_address_inet_from_strings " + "failed for %s: %s, ignoring\n", + server_addresses[i], + strerror(errno)); + continue; + } + + server_addr_string = tsocket_address_inet_addr_string( + server_addr, talloc_tos()); + TALLOC_FREE(server_addr); + if (server_addr_string == NULL) { + DBG_ERR("tsocket_address_inet_addr_string failed " + "for %s, ignoring\n", + server_addresses[i]); + continue; + } + + equal = strequal(local, server_addr_string); + TALLOC_FREE(server_addr_string); + + if (equal) { + TALLOC_FREE(local); + return true; + } + } + + TALLOC_FREE(local); + return false; +}