* DNS: resolve host names from DHCP: improve

#1956

Squashed commit of the following:

commit efeacd92b8b82a9a0a0cce8c5648f2d024b4bc9e
Author: Simon Zolin <s.zolin@adguard.com>
Date:   Tue Aug 18 13:54:15 2020 +0300

    * DNS: resolve host names from DHCP: improve

    . Require a valid host name from DHCP lease
    . Use lower-case names
This commit is contained in:
Simon Zolin 2020-08-18 17:40:36 +03:00
parent 8d0c8ad438
commit c3123473cf
2 changed files with 38 additions and 6 deletions

View File

@ -22,7 +22,8 @@ Contents:
* Update client * Update client
* Delete client * Delete client
* API: Find clients by IP * API: Find clients by IP
* Enable DHCP server * DHCP server
* DHCP server in DNS
* "Show DHCP status" command * "Show DHCP status" command
* "Check DHCP" command * "Check DHCP" command
* "Enable DHCP" command * "Enable DHCP" command
@ -375,9 +376,9 @@ Error response:
UI shows error message "Auto-update has failed" UI shows error message "Auto-update has failed"
## Enable DHCP server ## DHCP server
Algorithm: Enable DHCP server algorithm:
* UI shows DHCP configuration screen with "Enabled DHCP" button disabled, and "Check DHCP" button enabled * UI shows DHCP configuration screen with "Enabled DHCP" button disabled, and "Check DHCP" button enabled
* User clicks on "Check DHCP"; UI sends request to server * User clicks on "Check DHCP"; UI sends request to server
@ -389,6 +390,21 @@ Algorithm:
* UI shows the status * UI shows the status
### DHCP server in DNS
DHCP leases are used in several ways by DNS module.
* For "A" DNS reqeust we reply with an IP address leased by our DHCP server.
< A bills-notebook.lan.
> A bills-notebook.lan. = 192.168.1.100
* For "PTR" DNS request we reply with a hostname from an active DHCP lease.
< PTR 100.1.168.192.in-addr.arpa.
> PTR 100.1.168.192.in-addr.arpa. = bills-notebook.
### "Show DHCP status" command ### "Show DHCP status" command
Request: Request:

View File

@ -94,6 +94,20 @@ func processInitial(ctx *dnsContext) int {
return resultDone return resultDone
} }
// Return TRUE if host names doesn't contain disallowed characters
func isHostnameOK(hostname string) bool {
for _, c := range hostname {
if !((c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9') ||
c == '.' || c == '-') {
log.Debug("DNS: skipping invalid hostname %s from DHCP", hostname)
return false
}
}
return true
}
func (s *Server) onDHCPLeaseChanged(flags int) { func (s *Server) onDHCPLeaseChanged(flags int) {
switch flags { switch flags {
case dhcpd.LeaseChangedAdded, case dhcpd.LeaseChangedAdded,
@ -110,15 +124,17 @@ func (s *Server) onDHCPLeaseChanged(flags int) {
ll := s.dhcpServer.Leases(dhcpd.LeasesAll) ll := s.dhcpServer.Leases(dhcpd.LeasesAll)
for _, l := range ll { for _, l := range ll {
if len(l.Hostname) == 0 { if len(l.Hostname) == 0 || !isHostnameOK(l.Hostname) {
continue continue
} }
m[l.IP.String()] = l.Hostname lowhost := strings.ToLower(l.Hostname)
m[l.IP.String()] = lowhost
ip := make(net.IP, 4) ip := make(net.IP, 4)
copy(ip, l.IP.To4()) copy(ip, l.IP.To4())
hostToIP[l.Hostname] = ip hostToIP[lowhost] = ip
} }
log.Debug("DNS: added %d A/PTR entries from DHCP", len(m)) log.Debug("DNS: added %d A/PTR entries from DHCP", len(m))