dns: don’t let clients override the hostname

fixes #11
This commit is contained in:
Michael Stapelberg 2018-10-22 21:43:41 +02:00
parent 0bbc1d923d
commit 4288adec69
2 changed files with 51 additions and 0 deletions

View File

@ -134,6 +134,9 @@ func (s *Server) SetLeases(leases []dhcp4d.Lease) {
if l.Expired(now) {
continue
}
if _, ok := s.hostsByName[l.Hostname]; ok {
continue // dont overwrite e.g. the hostname entry
}
s.hostsByName[l.Hostname] = l.Addr.String()
if rev, err := dns.ReverseAddr(l.Addr.String()); err == nil {
s.hostsByIP[rev] = l.Hostname

View File

@ -126,6 +126,54 @@ func TestHostname(t *testing.T) {
t.Skipf("os.Hostname: %v", err)
}
r := &recorder{}
s := NewServer("127.0.0.2:0", "lan")
s.SetLeases([]dhcp4d.Lease{
{
Hostname: hostname,
Addr: net.IP{192, 168, 42, 23},
},
})
t.Run("A", func(t *testing.T) {
m := new(dns.Msg)
m.SetQuestion(hostname+".lan.", dns.TypeA)
s.Mux.ServeDNS(r, m)
if got, want := len(r.response.Answer), 1; got != want {
t.Fatalf("unexpected number of answers for %v: got %d, want %d", m.Question, got, want)
}
a := r.response.Answer[0]
if _, ok := a.(*dns.A); !ok {
t.Fatalf("unexpected response type: got %T, want dns.A", a)
}
if got, want := a.(*dns.A).A, net.ParseIP("127.0.0.2"); !got.Equal(want) {
t.Fatalf("unexpected response IP: got %v, want %v", got, want)
}
})
t.Run("PTR", func(t *testing.T) {
m := new(dns.Msg)
m.SetQuestion("2.0.0.127.in-addr.arpa.", dns.TypePTR)
s.Mux.ServeDNS(r, m)
if got, want := len(r.response.Answer), 1; got != want {
t.Fatalf("unexpected number of answers: got %d, want %d", got, want)
}
a := r.response.Answer[0]
if _, ok := a.(*dns.PTR); !ok {
t.Fatalf("unexpected response type: got %T, want dns.PTR", a)
}
if got, want := a.(*dns.PTR).Ptr, hostname+".lan."; got != want {
t.Fatalf("unexpected response record: got %q, want %q", got, want)
}
})
}
func TestHostnameDHCP(t *testing.T) {
hostname, err := os.Hostname()
if err != nil {
t.Skipf("os.Hostname: %v", err)
}
r := &recorder{}
s := NewServer("127.0.0.2:0", "lan")