[3/4] unbound-dhcp-leases-bridge: Don't overwrite static leases

Message ID 20241021163849.1265183-3-michael.tremer@ipfire.org
State New
Headers
Series [1/4] unbound-dhcp-leases-bridge: Don't export expired leases to Unbound |

Commit Message

Michael Tremer Oct. 21, 2024, 4:38 p.m. UTC
  When we import all static leases, their remark will be used as hostname
(because WTF?) and might be overwritten if the device is not sending any
or even the same hostname.

This patch avoids that static leases will be modified.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
---
 config/unbound/unbound-dhcp-leases-bridge | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
  

Comments

Bernhard Bitsch Oct. 21, 2024, 4:47 p.m. UTC | #1
Reviewed-by: Bernhard Bitsch <bbitsch@ipfire.org>

Am 21.10.2024 um 18:38 schrieb Michael Tremer:
> When we import all static leases, their remark will be used as hostname
> (because WTF?) and might be overwritten if the device is not sending any
> or even the same hostname.
> 
> This patch avoids that static leases will be modified.
> 
> Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
> ---
>   config/unbound/unbound-dhcp-leases-bridge | 10 ++++++++--
>   1 file changed, 8 insertions(+), 2 deletions(-)
> 
> diff --git a/config/unbound/unbound-dhcp-leases-bridge b/config/unbound/unbound-dhcp-leases-bridge
> index 2cbdaa004..66ea28054 100644
> --- a/config/unbound/unbound-dhcp-leases-bridge
> +++ b/config/unbound/unbound-dhcp-leases-bridge
> @@ -216,6 +216,11 @@ class UnboundDHCPLeasesBridge(object):
>   			# Find the old lease
>   			old_lease = self._find_lease(address)
>   
> +			# Don't update fixed leases as they might clear the hostname
> +			if old_lease and old_lease.fixed:
> +				log.debug("Won't update fixed lease %s" % old_lease)
> +				return
> +
>   			# Create a new lease
>   			lease = Lease(address, {
>   				"client-hostname" : name,
> @@ -581,19 +586,20 @@ class FixLeases(object):
>   					"client-hostname" : hostname,
>   					"starts"          : now.strftime("%w %Y/%m/%d %H:%M:%S"),
>   					"ends"            : "never",
> -				})
> +				}, fixed=True)
>   				leases.append(l)
>   
>   		return leases
>   
>   
>   class Lease(object):
> -	def __init__(self, ipaddr, properties):
> +	def __init__(self, ipaddr, properties, fixed=False):
>   		if not isinstance(ipaddr, ipaddress.IPv4Address):
>   			ipaddr = ipaddress.IPv4Address(ipaddr)
>   
>   		self.ipaddr = ipaddr
>   		self._properties = properties
> +		self.fixed = fixed
>   
>   	def __repr__(self):
>   		return "<%s for %s (%s)>" % (self.__class__.__name__, self.ipaddr, self.hostname)
  

Patch

diff --git a/config/unbound/unbound-dhcp-leases-bridge b/config/unbound/unbound-dhcp-leases-bridge
index 2cbdaa004..66ea28054 100644
--- a/config/unbound/unbound-dhcp-leases-bridge
+++ b/config/unbound/unbound-dhcp-leases-bridge
@@ -216,6 +216,11 @@  class UnboundDHCPLeasesBridge(object):
 			# Find the old lease
 			old_lease = self._find_lease(address)
 
+			# Don't update fixed leases as they might clear the hostname
+			if old_lease and old_lease.fixed:
+				log.debug("Won't update fixed lease %s" % old_lease)
+				return
+
 			# Create a new lease
 			lease = Lease(address, {
 				"client-hostname" : name,
@@ -581,19 +586,20 @@  class FixLeases(object):
 					"client-hostname" : hostname,
 					"starts"          : now.strftime("%w %Y/%m/%d %H:%M:%S"),
 					"ends"            : "never",
-				})
+				}, fixed=True)
 				leases.append(l)
 
 		return leases
 
 
 class Lease(object):
-	def __init__(self, ipaddr, properties):
+	def __init__(self, ipaddr, properties, fixed=False):
 		if not isinstance(ipaddr, ipaddress.IPv4Address):
 			ipaddr = ipaddress.IPv4Address(ipaddr)
 
 		self.ipaddr = ipaddr
 		self._properties = properties
+		self.fixed = fixed
 
 	def __repr__(self):
 		return "<%s for %s (%s)>" % (self.__class__.__name__, self.ipaddr, self.hostname)