Pull request: all: imp dhcp client hostname normalization
Updates #2952. Updates #2978. Squashed commit of the following: commit 20e379b94ccf8140fd9056429315945c17f711a5 Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Mon Apr 19 15:58:37 2021 +0300 all: imp naming commit ed300e0563fa37e161406a97991b26a89e23903a Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Mon Apr 19 15:43:09 2021 +0300 all: imp dhcp client hostname normalization
This commit is contained in:
@@ -558,34 +558,45 @@ 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) {
|
||||
// normalizeHostname normalizes a hostname sent by the client.
|
||||
func 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"
|
||||
parts := strings.FieldsFunc(name, func(c rune) (ok bool) {
|
||||
return c != '.' && !aghnet.IsValidHostOuterRune(c)
|
||||
})
|
||||
|
||||
if len(parts) == 0 {
|
||||
return "", fmt.Errorf("normalizing hostname %q: no valid parts", name)
|
||||
}
|
||||
|
||||
err = aghnet.ValidateDomainName(norm)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("validating %s hostname: %w", state, err)
|
||||
}
|
||||
norm = strings.Join(parts, "-")
|
||||
norm = strings.TrimSuffix(norm, "-")
|
||||
|
||||
return norm, nil
|
||||
}
|
||||
|
||||
// validateHostname validates a hostname sent by the client.
|
||||
func (s *v4Server) validateHostname(name string) (err error) {
|
||||
if name == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
err = aghnet.ValidateDomainName(name)
|
||||
if err != nil {
|
||||
return fmt.Errorf("validating hostname: %w", err)
|
||||
}
|
||||
|
||||
// TODO(a.garipov): Add client hostname uniqueness validation either
|
||||
// here or into method processRequest. This is not as easy as it might
|
||||
// look like, because the process of adding and releasing a lease is
|
||||
// currently non-straightforward.
|
||||
|
||||
return 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 *Lease, ok bool) {
|
||||
@@ -634,16 +645,29 @@ func (s *v4Server) processRequest(req, resp *dhcpv4.DHCPv4) (lease *Lease, ok bo
|
||||
}
|
||||
|
||||
if !lease.IsStatic() {
|
||||
cliHostname := req.HostName()
|
||||
|
||||
var hostname string
|
||||
hostname, err = s.normalizeHostname(req.HostName())
|
||||
hostname, err = normalizeHostname(cliHostname)
|
||||
if err != nil {
|
||||
log.Error("dhcpv4: cannot normalize hostname for %s: %s", mac, err)
|
||||
|
||||
return nil, false
|
||||
// Go on and assign a hostname made from the IP.
|
||||
}
|
||||
|
||||
if hostname != "" && cliHostname != hostname {
|
||||
log.Debug("dhcpv4: normalized hostname %q into %q", cliHostname, hostname)
|
||||
}
|
||||
|
||||
err = s.validateHostname(hostname)
|
||||
if err != nil {
|
||||
log.Error("dhcpv4: validating hostname for %s: %s", mac, err)
|
||||
|
||||
// Go on and assign a hostname made from the IP.
|
||||
}
|
||||
|
||||
if hostname == "" {
|
||||
hostname = aghnet.GenerateHostName(reqIP)
|
||||
hostname = aghnet.GenerateHostname(reqIP)
|
||||
}
|
||||
|
||||
lease.Hostname = hostname
|
||||
|
||||
@@ -290,7 +290,7 @@ func TestV4DynamicLease_Get(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestV4Server_normalizeHostname(t *testing.T) {
|
||||
func TestNormalizeHostname(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
hostname string
|
||||
@@ -312,24 +312,35 @@ func TestV4Server_normalizeHostname(t *testing.T) {
|
||||
wantErrMsg: "",
|
||||
want: "my-device-01",
|
||||
}, {
|
||||
name: "error",
|
||||
hostname: "!!!",
|
||||
wantErrMsg: `validating non-normalized hostname: ` +
|
||||
`invalid domain name label at index 0: ` +
|
||||
`invalid char '!' at index 0 in "!!!"`,
|
||||
want: "",
|
||||
name: "success_underscores",
|
||||
hostname: "my_device_01",
|
||||
wantErrMsg: "",
|
||||
want: "my-device-01",
|
||||
}, {
|
||||
name: "error_spaces",
|
||||
hostname: "! ! !",
|
||||
wantErrMsg: `validating normalized hostname: ` +
|
||||
`invalid domain name label at index 0: ` +
|
||||
`invalid char '!' at index 0 in "!-!-!"`,
|
||||
want: "",
|
||||
name: "error_part",
|
||||
hostname: "device !!!",
|
||||
wantErrMsg: "",
|
||||
want: "device",
|
||||
}, {
|
||||
name: "error_part_spaces",
|
||||
hostname: "device ! ! !",
|
||||
wantErrMsg: "",
|
||||
want: "device",
|
||||
}, {
|
||||
name: "error",
|
||||
hostname: "!!!",
|
||||
wantErrMsg: `normalizing hostname "!!!": no valid parts`,
|
||||
want: "",
|
||||
}, {
|
||||
name: "error_spaces",
|
||||
hostname: "! ! !",
|
||||
wantErrMsg: `normalizing hostname "! ! !": no valid parts`,
|
||||
want: "",
|
||||
}}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
got, err := (&v4Server{}).normalizeHostname(tc.hostname)
|
||||
got, err := normalizeHostname(tc.hostname)
|
||||
if tc.wantErrMsg == "" {
|
||||
assert.NoError(t, err)
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user