diff --git a/src/opnsense/scripts/dhcp/dnsmasq_watcher.py b/src/opnsense/scripts/dhcp/dnsmasq_watcher.py index 5f372cdcf..2e9e6f2a7 100755 --- a/src/opnsense/scripts/dhcp/dnsmasq_watcher.py +++ b/src/opnsense/scripts/dhcp/dnsmasq_watcher.py @@ -35,6 +35,7 @@ import time import argparse import syslog import signal +import re from configparser import ConfigParser sys.path.insert(0, "/usr/local/opnsense/site-python") from daemonize import Daemonize @@ -48,6 +49,7 @@ def run_watcher(target_filename, default_domain, watch_file, service_pid): # initiate lease watcher and setup cache dhcpdleases = watchers.dhcpd.DHCPDLease(watch_file) cached_leases = dict() + hostname_pattern = re.compile("(?!-)[A-Z0-9-]*(? time.time() \ and 'client-hostname' in lease and 'address' in lease and lease['client-hostname']: - address = ipaddress.ip_address(lease['address']) - lease['domain'] = default_domain - cached_leases[lease['address']] = lease - dhcpd_changed = True + if all(hostname_pattern.match(part) for part in lease['client-hostname'].strip().split('.')): + address = ipaddress.ip_address(lease['address']) + lease['domain'] = default_domain + cached_leases[lease['address']] = lease + dhcpd_changed = True + else: + syslog.syslog( + syslog.LOG_WARNING, + "dhcpd leases: %s not a valid hostname, ignoring" % lease['client-hostname'] + ) if time.time() - last_cleanup > cleanup_interval: # cleanup every x seconds diff --git a/src/opnsense/scripts/dhcp/unbound_watcher.py b/src/opnsense/scripts/dhcp/unbound_watcher.py index ba55cff26..20376501b 100755 --- a/src/opnsense/scripts/dhcp/unbound_watcher.py +++ b/src/opnsense/scripts/dhcp/unbound_watcher.py @@ -63,11 +63,6 @@ def unbound_control(commands, input=None, output_stream=None): if output_stream: output_stream.seek(0) -def valid_hostname(hostname): - hostname = hostname.rstrip('.') - correct = re.compile("(?!-)[A-Z0-9-]{1,63}(? time.time() \ and 'client-hostname' in lease and 'address' in lease and lease['client-hostname']: - if valid_hostname(lease['client-hostname']): + if all(hostname_pattern.match(part) for part in lease['client-hostname'].strip().split('.')): address = ipaddress.ip_address(lease['address']) lease['domain'] = default_domain for lease_config in lease_configs: