Pull request: dhcpd: normalize client host

Updates #2946.

Squashed commit of the following:

commit f830c03ddee65f7e86c43baa00a7dcc022091d93
Merge: df6c83d7 327e76cd
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Tue Apr 13 15:55:58 2021 +0300

    Merge branch 'master' into 2946-norm-dhcp-host

commit df6c83d7d117b718110a035216e708d5131bf71c
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Tue Apr 13 15:49:03 2021 +0300

    dhcpd: imp docs

commit 1407e9bd7b7694bd3069349faba3ec9ff5eb468b
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Tue Apr 13 15:27:38 2021 +0300

    dhcpd: normalize client host
This commit is contained in:
Ainar Garipov
2021-04-13 16:00:09 +03:00
parent 327e76cd65
commit 773f02cf7d
4 changed files with 99 additions and 6 deletions

View File

@@ -6,6 +6,7 @@ import (
"bytes"
"fmt"
"net"
"strings"
"sync"
"time"
@@ -557,10 +558,39 @@ func (o *optFQDN) ToBytes() []byte {
return b
}
// normalizeHostname normalizes and validates a hostname sent by the client.
//
// TODO(a.garipov): Add client hostname uniqueness validations and rename the
// method to validateHostname.
func (s *v4Server) normalizeHostname(name string) (norm string, err error) {
if name == "" {
return "", nil
}
// Some devices send hostnames with spaces, but we still want to accept
// them, so replace them with dashes and issue a warning.
//
// See https://github.com/AdguardTeam/AdGuardHome/issues/2946.
norm = strings.ReplaceAll(name, " ", "-")
state := "non-normalized"
if name != norm {
log.Debug("dhcpv4: normalized hostname %q into %q", name, norm)
state = "normalized"
}
err = aghnet.ValidateDomainName(norm)
if err != nil {
return "", fmt.Errorf("validating %s hostname: %w", state, err)
}
return norm, nil
}
// Process Request request and return lease
// Return false if we don't need to reply
func (s *v4Server) processRequest(req, resp *dhcpv4.DHCPv4) (*Lease, bool) {
var lease *Lease
func (s *v4Server) processRequest(req, resp *dhcpv4.DHCPv4) (lease *Lease, ok bool) {
var err error
mac := req.ClientHWAddr
reqIP := req.RequestedIPAddress()
if reqIP == nil {
@@ -604,7 +634,13 @@ func (s *v4Server) processRequest(req, resp *dhcpv4.DHCPv4) (*Lease, bool) {
}
if !lease.IsStatic() {
lease.Hostname = req.HostName()
lease.Hostname, err = s.normalizeHostname(req.HostName())
if err != nil {
log.Error("dhcpv4: cannot normalize hostname for %s: %s", mac, err)
return nil, false
}
s.commitLease(lease)
} else if len(lease.Hostname) != 0 {
o := &optFQDN{