]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
lib: Add lp_allow_local_address()
authorVolker Lendecke <vl@samba.org>
Fri, 21 Oct 2022 14:45:35 +0000 (16:45 +0200)
committerVolker Lendecke <vl@samba.org>
Thu, 10 Nov 2022 07:27:31 +0000 (07:27 +0000)
Helper function for listing and accessing shares

Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Stefan Metzmacher <metze@samba.org>
source3/include/proto.h
source3/lib/util_sock.c
source3/param/service.c

index f632cf37c087e2e6dc5ba07bdbf67d695e8c0e41..8582830d558c58f65a36a19cfa241a60f3397e07 100644 (file)
@@ -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);
index 85dc3efbe15e5be9404976ff9da7fe6e4b351e08..391b46f801e4f91130115ffd77bd8372409de757 100644 (file)
@@ -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.
index 880e79fb28471da1ff0d0d8079229b163dc1788c..b06a2fbc734fbb50c1ce447c1e787cd76c9bb1d1 100644 (file)
@@ -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;
+}