Skip to contentSkip to conversation

Log the actual management interface port in use

PATCH In review [email protected]... 3 participants 6 messages Jun 30, 2022 – Jul 14, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 Log the actual management interface port in use

From: Selva Nair <[email protected]...>

When the port is specified as zero, log the actual port bound to, instead of 0.

---
 src/openvpn/manage.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
src/openvpn/manage.c +16 -2
index 50f162a3..21c7ccdd 100644
@@ -1830,8 +1830,22 @@ man_listen(struct management *man)
         }
         else
 #endif
-        msg(D_MANAGEMENT, "MANAGEMENT: TCP Socket listening on %s",
-            print_sockaddr(man->settings.local->ai_addr, &gc));
+        {
+            const struct sockaddr *man_addr = man->settings.local->ai_addr;
+            struct sockaddr_storage addr;
+            socklen_t addrlen = sizeof(addr);
+            if (!getsockname(man->connection.sd_top, (struct sockaddr *) &addr, &addrlen))
+            {
+                man_addr = (struct sockaddr *) &addr;
+            }
+            else
+            {
+                msg(M_WARN|M_ERRNO,
+                    "Failed to get the management socket address");
+            }
+            msg(D_MANAGEMENT, "MANAGEMENT: TCP Socket listening on %s",
+                print_sockaddr(man_addr, &gc));
+        }
     }
 
 #ifdef _WIN32
Signature · 1 line
2.30.2
#1 lore raw

PATCH 2/3 Reactivate record_peer_info in manage.c

From: Selva Nair <[email protected]...>

--management-client has an obscure and undocumented feature to take a file argument where the peer's address and port are recorded. This has become dead code over time.

- reactivate the dead code
- make it work with v6 addresses as well
- do not exit on error in writing the record

---

Alternatively we could remove this "feature" and related code.

 src/openvpn/manage.c | 12 +++---------
 1 file changed, 3 insertions(+), 9 deletions(-)
src/openvpn/manage.c +3 -9
index 21c7ccdd..548d3b9a 100644
@@ -1621,23 +1621,19 @@ man_record_peer_info(struct management *man)
     if (man->settings.write_peer_info_file)
     {
         bool success = false;
-#ifdef HAVE_GETSOCKNAME
         if (socket_defined(man->connection.sd_cli))
         {
-            struct sockaddr_in addr;
+            struct sockaddr_storage addr;
             socklen_t addrlen = sizeof(addr);
             int status;
 
-            CLEAR(addr);
             status = getsockname(man->connection.sd_cli, (struct sockaddr *)&addr, &addrlen);
-            if (!status && addrlen == sizeof(addr))
+            if (!status)
             {
-                const in_addr_t a = ntohl(addr.sin_addr.s_addr);
-                const int p = ntohs(addr.sin_port);
                 FILE *fp = platform_fopen(man->settings.write_peer_info_file, "w");
                 if (fp)
                 {
-                    fprintf(fp, "%s\n%d\n", print_in_addr_t(a, 0, &gc), p);
+                    fprintf(fp, "%s\n", print_sockaddr((struct sockaddr *)&addr, &gc));
                     if (!fclose(fp))
                     {
                         success = true;
@@ -1645,12 +1641,10 @@ man_record_peer_info(struct management *man)
                 }
             }
         }
-#endif /* ifdef HAVE_GETSOCKNAME */
         if (!success)
         {
             msg(D_MANAGEMENT, "MANAGEMENT: failed to write peer info to file %s",
                 man->settings.write_peer_info_file);
-            throw_signal_soft(SIGTERM, "management-connect-failed");
         }
     }
     gc_free(&gc);
Signature · 1 line
2.30.2
#2 lore raw

Re: [Openvpn-devel] [PATCH 2/3] Reactivate record_peer_info in manage.c

Am 30.06.2022 um 21:05 schrieb [email protected]...:

Quoted 9 lines

From: Selva Nair <[email protected]...>

--management-client has an obscure and undocumented feature to take a file argument where the peer's address and port are recorded. This has become dead code over time.

- reactivate the dead code
- make it work with v6 addresses as well
- do not exit on error in writing the record

This is feature actually useful? If it is dead for a long time and nobody noticed, maybe we can just remove it?

Arne

#3 lore raw

PATCH 3/3 Log address of management client on accept

In reply to [email protected]...

From: Selva Nair <[email protected]...>

Currently when we are listening on the management interface, the local address/port is logged as that of the connecting client.

Fix it.

---
 src/openvpn/manage.c | 24 +++++++++++++++++++++---
 1 file changed, 21 insertions(+), 3 deletions(-)
src/openvpn/manage.c +21 -3
index 548d3b9a..7947c906 100644
@@ -1685,9 +1685,27 @@ man_new_connection_post(struct management *man, const char *description)
     }
     else
 #endif
-    msg(D_MANAGEMENT, "MANAGEMENT: %s %s",
-        description,
-        print_sockaddr(man->settings.local->ai_addr, &gc));
+    if (man->settings.flags & MF_CONNECT_AS_CLIENT)
+    {
+        msg(D_MANAGEMENT, "MANAGEMENT: %s %s",
+            description,
+            print_sockaddr(man->settings.local->ai_addr, &gc));
+    }
+    else
+    {
+        struct sockaddr_storage addr;
+        socklen_t addrlen = sizeof(addr);
+        if (!getpeername(man->connection.sd_cli, (struct sockaddr *) &addr,
+                         &addrlen))
+        {
+            msg(D_MANAGEMENT, "MANAGEMENT: %s %s", description,
+                print_sockaddr((struct sockaddr *) &addr, &gc));
+        }
+        else
+        {
+            msg(D_MANAGEMENT, "MANAGEMENT: %s %s", description, "unknown");
+        }
+    }
 
     buffer_list_reset(man->connection.out);
 
Signature · 1 line
2.30.2
Acked-by on v1 3/3
#4 lore raw

[Openvpn-devel] [PATCH applied] Re: Log address of management client on accept

Silly code, this...

Before:

2022-07-14 20:50:12 us=826150 MANAGEMENT: Client connected from [AF_INET]127.0.0.1:0

After:

2022-07-14 20:51:18 us=100211 MANAGEMENT: TCP Socket listening on [AF_INET]127.0.0.1:49334
2022-07-14 20:51:22 us=443453 MANAGEMENT: Client connected from [AF_INET]127.0.0.1:50184

.. and also dual-stacked...

2022-07-14 20:52:03 us=916048 MANAGEMENT: Client connected from [AF_INET6]::1:29800
2022-07-14 20:52:09 us=636212 MANAGEMENT: Client connected from [AF_INET6]::ffff:127.0.0.1:30335

(Yeah, that one could be printed more pretty... we might have a _ex() thingie for it... but it's already much more useful than before)

New code looks good, and does what it promises.

Your patch has been applied to the master branch.

commit dd98c38b31c02cbca7f88ed0a709e68f24361195

Author: Selva Nair
Date:   Thu Jun 30 15:05:49 2022 -0400
     Log address of management client on accept

     Signed-off-by: Selva Nair <[email protected]...>
     Acked-by: Gert Doering <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg24619.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering

Acked-by on v1 1/3
#5 lore raw
In reply to [email protected]...

This looks like a useful addition, and it actually works :-)

$ openvpn --verb 4 --dev tun --management :: 0 --management-hold
..
2022-07-14 20:43:01 us=644866 setsockopt(IPV6_V6ONLY=0)
2022-07-14 20:43:01 us=644907 MANAGEMENT: TCP Socket listening on [AF_INET6][undef]:59291
2022-07-14 20:43:01 us=644918 Need hold release from management interface, waiting...

of course it also works for legacy IP...

2022-07-14 20:43:39 us=245584 MANAGEMENT: TCP Socket listening on [AF_INET]127.0.0.1:63545

Tested on FreeBSD and on OpenSolaris (because that's always the oddball "it is a real unix, but more pain than the rest" OS).

Your patch has been applied to the master branch.

commit 298f3bedbc42578d39055990669f7dda0f787d68

Author: Selva Nair
Date:   Thu Jun 30 15:05:47 2022 -0400
     Log the actual management interface port in use

     Signed-off-by: Selva Nair <[email protected]...>
     Acked-by: Gert Doering <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg24617.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering