[1/3] openvpn-authenticator: Avoid infinite loop when losing socket connection

Message ID 20221206100144.4150532-1-michael.tremer@ipfire.org
State Accepted
Commit 91abc6660a4f26b2ba5e6623fc29c4c1297f6303
Headers
Series [1/3] openvpn-authenticator: Avoid infinite loop when losing socket connection |

Commit Message

Michael Tremer Dec. 6, 2022, 10:01 a.m. UTC
  This patch will gracefully terminate the daemon when it loses its
connection to the OpenVPN daemon.

Fixes: #12963
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
---
 config/ovpn/openvpn-authenticator | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
  

Comments

Adolf Belka Dec. 7, 2022, 12:21 p.m. UTC | #1
Tested-by: Adolf Belka <adolf.belka@ipfire.org>

On 06/12/2022 11:01, Michael Tremer wrote:
> This patch will gracefully terminate the daemon when it loses its
> connection to the OpenVPN daemon.
>
> Fixes: #12963
> Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
> ---
>   config/ovpn/openvpn-authenticator | 15 ++++++++++-----
>   1 file changed, 10 insertions(+), 5 deletions(-)
>
> diff --git a/config/ovpn/openvpn-authenticator b/config/ovpn/openvpn-authenticator
> index 65844012b..5d9348d7e 100644
> --- a/config/ovpn/openvpn-authenticator
> +++ b/config/ovpn/openvpn-authenticator
> @@ -116,11 +116,16 @@ class OpenVPNAuthenticator(object):
>   
>   		log.info("OpenVPN Authenticator started")
>   
> -		while True:
> -			line = self._read_line()
> +		try:
> +			while True:
> +				line = self._read_line()
>   
> -			if line.startswith(">CLIENT"):
> -				self._client_event(line)
> +				if line.startswith(">CLIENT"):
> +					self._client_event(line)
> +
> +		# Terminate the daemon when it loses its connection to the OpenVPN daemon
> +		except ConnectionResetError as e:
> +			log.error("Connection to OpenVPN has been lost: %s" % e)
>   
>   		log.info("OpenVPN Authenticator terminated")
>   
> @@ -269,7 +274,7 @@ class OpenVPNAuthenticator(object):
>   	@staticmethod
>   	def _b64decode(s):
>   		return base64.b64decode(s.encode()).decode()
> -		
> +
>   	@staticmethod
>   	def _escape(s):
>   		return s.replace(" ", "\ ")
  

Patch

diff --git a/config/ovpn/openvpn-authenticator b/config/ovpn/openvpn-authenticator
index 65844012b..5d9348d7e 100644
--- a/config/ovpn/openvpn-authenticator
+++ b/config/ovpn/openvpn-authenticator
@@ -116,11 +116,16 @@  class OpenVPNAuthenticator(object):
 
 		log.info("OpenVPN Authenticator started")
 
-		while True:
-			line = self._read_line()
+		try:
+			while True:
+				line = self._read_line()
 
-			if line.startswith(">CLIENT"):
-				self._client_event(line)
+				if line.startswith(">CLIENT"):
+					self._client_event(line)
+
+		# Terminate the daemon when it loses its connection to the OpenVPN daemon
+		except ConnectionResetError as e:
+			log.error("Connection to OpenVPN has been lost: %s" % e)
 
 		log.info("OpenVPN Authenticator terminated")
 
@@ -269,7 +274,7 @@  class OpenVPNAuthenticator(object):
 	@staticmethod
 	def _b64decode(s):
 		return base64.b64decode(s.encode()).decode()
-		
+
 	@staticmethod
 	def _escape(s):
 		return s.replace(" ", "\ ")