Skip to contentSkip to conversation

Allow killing of client instances by cn with wildcards

PATCH Superseded Vladislav Grishenko 3 participants 7 messages Jul 27, 2020 – Aug 14, 2020

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH 2/2 Allow killing of client instances by cn with wildcards

In case of some permanent part of common name (ex. domain) and/or long complex common name consisting of multiple x509 fields, it's handly to kill client instances via management interface with just part of common name, not by exact match only.

Patch allows to use wildcard placeholder '*' as the last trailing symbol of kill command parameter.
Single '*' wildcard would be too greedy and can be too harmful, therefore not allowed. Wildcards in the middle of parameter string are not supported to keep the the things simple at the moment.

---
 doc/management-notes.txt | 2 ++
 src/openvpn/multi.c      | 6 +++++-
 2 files changed, 7 insertions(+), 1 deletion(-)
doc/management-notes.txt +2 -0
index 61daaf07..91073693 100644
@@ -195,6 +195,8 @@ Command examples:
 
   kill Test-Client -- kill the client instance having a
                       common name of "Test-Client".
+  kill Test-Cli*   -- kill the client instances having a
+                      common name starting with "Test-Cli".
   kill 1.2.3.4:4000 -- kill the client instance having a
                        source address and port of 1.2.3.4:4000
 
src/openvpn/multi.c +5 -1
index 9bda38b0..8952658a 100644
@@ -3772,6 +3772,10 @@ management_callback_kill_by_cn(void *arg, const char *del_cn)
     struct hash_element *he;
     int count = 0;
 
+    /* Allow trailing wildcard */
+    int len = strlen(del_cn);
+    len += (len > 1 && del_cn[len-1] == '*') ? -1 : 1;
+
     hash_iterator_init(m->iter, &hi);
     while ((he = hash_iterator_next(&hi)))
     {
@@ -3779,7 +3783,7 @@ management_callback_kill_by_cn(void *arg, const char *del_cn)
         if (!mi->halt)
         {
             const char *cn = tls_common_name(mi->context.c2.tls_multi, false);
-            if (cn && !strcmp(cn, del_cn))
+            if (cn && !strncmp(cn, del_cn, len))
             {
                 multi_signal_instance(m, mi, SIGTERM);
                 ++count;
Signature · 1 line
2.17.1
#1 lore raw

src/openvpn/multi.c management_callback_kill_by_cn(void *arg, const char *del_cn) · line 3777

Quoted 6 lines
     int count = 0;
src/openvpn/multi.c +3 -0
+    /* Allow trailing wildcard */
+    int len = strlen(del_cn);
+    len += (len > 1 && del_cn[len-1] == '*') ? -1 : 1;

This is very compact and not very readable

A comment that says why you are adding +1 would be good.

I first thought it was incorrect and went through godbolt to figure out the '*' string is longer so its prefix is short and normal string needs the \0 instead: https://godbolt.org/z/vqsPeW

Quoted 6 lines
src/openvpn/multi.c +1 -0
+
     hash_iterator_init(m->iter, &hi);
     while ((he = hash_iterator_next(&hi)))
     {

src/openvpn/multi.c management_callback_kill_by_cn(void *arg, const char *del_cn) · line 3789

Quoted 8 lines
src/openvpn/multi.c +1 -1
@@ -3779,7 +3783,7 @@ management_callback_kill_by_cn(void *arg, const char *del_cn)
         if (!mi->halt)
         {
             const char *cn = tls_common_name(mi->context.c2.tls_multi, false);
-            if (cn && !strcmp(cn, del_cn))
+            if (cn && !strncmp(cn, del_cn, len))
             {
                 multi_signal_instance(m, mi, SIGTERM);
                 ++count;

Feature-Ack. But I would like the string len magic be documented/written in a better understandable way.

Arne

#2 lore raw

PATCH v2 1/1 Allow management to kill client instances by CN wildcard

In case of some permanent part of common name (ex. domain) and/or long complex common name consisting of multiple x509 fields, it's handly to kill client instances via management interface with just prefix of common name, not by exact match only.

Patch allows to use asterisk as wildcard placeholder in the last trailing symbol of kill command parameter.
Single asterisk - empty prefix would be too greedy and can be too harmful, therefore not allowed. Wildcards in the middle of parameter string are not supported to keep the the things simple at the moment.

v2: fine tune comments

---
 doc/management-notes.txt |  2 ++
 src/openvpn/multi.c      | 15 ++++++++++++++-
 2 files changed, 16 insertions(+), 1 deletion(-)
doc/management-notes.txt +2 -0
index 61daaf07..91073693 100644
@@ -195,6 +195,8 @@ Command examples:
 
   kill Test-Client -- kill the client instance having a
                       common name of "Test-Client".
+  kill Test-Cli*   -- kill the client instances having a
+                      common name starting with "Test-Cli".
   kill 1.2.3.4:4000 -- kill the client instance having a
                        source address and port of 1.2.3.4:4000
 
src/openvpn/multi.c +14 -1
index 13738180..36be5de2 100644
@@ -3820,6 +3820,19 @@ management_callback_kill_by_cn(void *arg, const char *del_cn)
     struct hash_element *he;
     int count = 0;
 
+    /* Check passed string for non-empty prefix with trailing asterisk */
+    size_t len = strlen(del_cn);
+    if (len > 1 && del_cn[len - 1] == '*')
+    {
+        /* Exclude trailing asterisk from string comparison */
+        len--;
+    }
+    else
+    {
+        /* Include terminating NUL char to perform exact string comparison */
+        len++;
+    }
+
     hash_iterator_init(m->iter, &hi);
     while ((he = hash_iterator_next(&hi)))
     {
@@ -3827,7 +3840,7 @@ management_callback_kill_by_cn(void *arg, const char *del_cn)
         if (!mi->halt)
         {
             const char *cn = tls_common_name(mi->context.c2.tls_multi, false);
-            if (cn && !strcmp(cn, del_cn))
+            if (cn && !strncmp(cn, del_cn, len))
             {
                 multi_signal_instance(m, mi, SIGTERM);
                 ++count;
Signature · 1 line
2.17.1
#3 lore raw

Re: [Openvpn-devel] [PATCH v2] Allow management to kill client instances by CN wildcard

Am 14.08.20 um 19:12 schrieb Vladislav Grishenko:

Quoted 5 lines

In case of some permanent part of common name (ex. domain) and/or long complex common name consisting of multiple x509 fields, it's handly to kill client instances via management interface with just prefix of common name, not by exact match only.

Quoted 8 lines

Patch allows to use asterisk as wildcard placeholder in the last trailing symbol of kill command parameter.
Single asterisk - empty prefix would be too greedy and can be too harmful, therefore not allowed. Wildcards in the middle of parameter string are not supported to keep the the things simple at the moment.

v2: fine tune comments

Thanks for v2,

Acked-By; Arne Schwabe <[email protected]...>

#4 lore raw

Re: [Openvpn-devel] [PATCH v2] Allow management to kill client instances by CN wildcard

Hi

Quoted 13 lines · Arne Schwabe

Am 14.08.20 um 19:12 schrieb Vladislav Grishenko:

Quoted 11 lines

In case of some permanent part of common name (ex. domain) and/or long complex common name consisting of multiple x509 fields, it's handly to kill client instances via management interface with just prefix of common name, not by exact match only.

Patch allows to use asterisk as wildcard placeholder in the last trailing symbol of kill command parameter.
Single asterisk - empty prefix would be too greedy and can be too harmful, therefore not allowed. Wildcards in the middle of parameter string are not supported to keep the the things simple at the moment.

Quoted 6 lines
Quoted 2 lines

v2: fine tune comments

Thanks for v2,

Acked-By; Arne Schwabe <[email protected]...>

'*' is an allowed character in x509 common name unless we explicitly forbid it. So killing a client with name ending in * would get tricky if not impossible without side effects.

Selva

#5 lore raw

Re: [Openvpn-devel] [PATCH v2] Allow management to kill client instances by CN wildcard

Hi,

Yes, killing a client with cn ending in * will also lead to killing all the clients whose cn starts with that prefix.
Use other char would no-intuitive (ex. +).
What about optional "prefix" mode word for explicit mode (can be also enhanced one day with suffix/regexp/etc).

kill cn [mode]: Kill the client instance(s) having common name cn.

--
Best Regards, Vladislav Grishenko

-----Original Message-----
From: Selva Nair <[email protected]...>
Sent: Friday, August 14, 2020 11:22 PM
To: openvpn-devel <[email protected]>
Subject: Re: [Openvpn-devel] [PATCH v2] Allow management to kill client instances by CN wildcard

Hi

Quoted 13 lines · Arne Schwabe

Am 14.08.20 um 19:12 schrieb Vladislav Grishenko:

Quoted 11 lines

In case of some permanent part of common name (ex. domain) and/or long complex common name consisting of multiple x509 fields, it's handly to kill client instances via management interface with just prefix of common name, not by exact match only.

Patch allows to use asterisk as wildcard placeholder in the last trailing symbol of kill command parameter.
Single asterisk - empty prefix would be too greedy and can be too harmful, therefore not allowed. Wildcards in the middle of parameter string are not supported to keep the the things simple at the moment.

Quoted 6 lines
Quoted 2 lines

v2: fine tune comments

Thanks for v2,

Acked-By; Arne Schwabe <[email protected]...>

'*' is an allowed character in x509 common name unless we explicitly forbid it. So killing a client with name ending in * would get tricky if not impossible without side effects.

Selva

_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel

#6 lore raw

Re: [Openvpn-devel] [PATCH v2] Allow management to kill client instances by CN wildcard

Hi

Quoted 10 lines · Vladislav Grishenko

Hi,

Yes, killing a client with cn ending in * will also lead to killing all the clients whose cn starts with that prefix.
Use other char would no-intuitive (ex. +).
What about optional "prefix" mode word for explicit mode (can be also enhanced one day with suffix/regexp/etc).

kill cn [mode]: Kill the client instance(s) having common name cn.

That sounds good to me -- avoids the use of any special character.

Also, updating the "help" command of management interface was missed in the previous version of the patch.

Selva

Quoted 46 lines

--
Best Regards, Vladislav Grishenko

-----Original Message-----
From: Selva Nair <[email protected]...>
Sent: Friday, August 14, 2020 11:22 PM
To: openvpn-devel <[email protected]>
Subject: Re: [Openvpn-devel] [PATCH v2] Allow management to kill client instances by CN wildcard

Hi

Quoted 13 lines · Arne Schwabe

Am 14.08.20 um 19:12 schrieb Vladislav Grishenko:

Quoted 11 lines

In case of some permanent part of common name (ex. domain) and/or long complex common name consisting of multiple x509 fields, it's handly to kill client instances via management interface with just prefix of common name, not by exact match only.

Patch allows to use asterisk as wildcard placeholder in the last trailing symbol of kill command parameter.
Single asterisk - empty prefix would be too greedy and can be too harmful, therefore not allowed. Wildcards in the middle of parameter string are not supported to keep the the things simple at the moment.

Quoted 6 lines
Quoted 2 lines

v2: fine tune comments

Thanks for v2,

Acked-By; Arne Schwabe <[email protected]...>

'*' is an allowed character in x509 common name unless we explicitly forbid it. So killing a client with name ending in * would get tricky if not impossible without side effects.

Selva

_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel