[prev in list] [next in list] [prev in thread] [next in thread] 

List:       bash-bug
Subject:    [patch] store local port after successful connection via network redirections (/dev/{tcp,udp}/host/p
From:       Piotr Grzybowski <narsil.pl () gmail ! com>
Date:       2015-12-23 10:52:09
Message-ID: CAP=cN8=jWhYnbO8Q9b-ZCQo2wdhOBiXyNWkBemeQTRFWDaKbXw () mail ! gmail ! com
[Download RAW message or body]

Hi.

 I have had a need to know the number of the client port after successful

exec 4<>/dev/tcp/${host}/${port}
[..]

 The attached patch stores the local, client port in the variable
LOCALPORT. Would you accept this new feature?
 We need it due to specific on-site security considerations.
 There is another benefit: after last discussion about tempfile this
would give another source of natural randomness along with $$ and $!.
Employing the kernel for generating the random sequence is the most
portable way to get pseudo random numbers, since most kernels
randomize pids and ports to some extent.
 The variable can go by any other name, and I am not sure about the
need for extra #ifdefs concerning ipv6 (I assumed that if we have
networking we also have ipv6, and in both cases struct
sockaddr_storage).

cheers,
pg

["0001-store-local-port-in-network-redirections.patch" (application/octet-stream)]

From 7717a97dd539ce7b89b6ed7856a1afb7d94e163f Mon Sep 17 00:00:00 2001
From: Piotr Grzybowski <merlin@narsil.org.pl>
Date: Wed, 23 Dec 2015 10:43:37 +0100
Subject: [PATCH] insert local port to LOCALPORT variable after successfull
 connection via network redirections.

---
 redir.c | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)

diff --git a/redir.c b/redir.c
index 0f40bd0..645e16f 100644
--- a/redir.c
+++ b/redir.c
@@ -36,6 +36,11 @@
 #  include <unistd.h>
 #endif
 
+#if defined(NETWORK_REDIRECTIONS) && defined(HAVE_NETWORK)
+#  include <sys/socket.h>
+#  include <netinet/in.h>
+#endif
+
 #include <errno.h>
 
 #if !defined (errno)
@@ -549,6 +554,12 @@ redir_special_open (spec, filename, flags, mode, ri)
 #if !defined (HAVE_DEV_FD)
   intmax_t lfd;
 #endif
+#if defined(NETWORK_REDIRECTIONS) && defined(HAVE_NETWORK)
+  struct sockaddr_storage address;
+  socklen_t address_length;
+  int rc;
+  char *port;
+#endif
 
   fd = -1;
   switch (spec)
@@ -582,6 +593,22 @@ redir_special_open (spec, filename, flags, mode, ri)
     case RF_DEVUDP:
 #if defined (HAVE_NETWORK)
       fd = netopen (filename);
+      if (fd != -1)
+        {
+          rc = getsockname (fd, (struct sockaddr *)&address, &address_length);
+          if (rc == 0 && address.ss_family == AF_INET)
+            {
+              port = itos (ntohs (((struct sockaddr_in *)&address)->sin_port));
+              bind_variable ("LOCALPORT", port, 0);
+              free (port);
+            }
+          if (rc == 0 && address.ss_family == AF_INET6)
+            {
+              port = itos (ntohs (((struct sockaddr_in6 *)&address)->sin6_port));
+              bind_variable ("LOCALPORT", port, 0);
+              free (port);
+            }
+        }
 #else
       internal_warning (_("/dev/(tcp|udp)/host/port not supported without networking"));
       fd = open (filename, flags, mode);
-- 
2.4.3



[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic