diff --git a/docs/wiki/DNS.md b/docs/wiki/DNS.md index 24f886d..733c2a9 100644 --- a/docs/wiki/DNS.md +++ b/docs/wiki/DNS.md @@ -221,6 +221,13 @@ ttl = 300 | `value` | as a zone file writes it: an address, a CNAME's target, a TXT's text, an MX's preference and host, an SRV's priority, weight, port and host | | `ttl` | seconds (default: `[dns]` `ttl`) | +Reverse lookups work too. Each A or AAAA record's address answers a PTR +query (`20.77.168.192.in-addr.arpa`, or its `ip6.arpa` name) with the +record's name, so `192.168.77.20` gives back `games.bbs.example.org`. +Wildcards don't make PTRs. To give an address another name, write its PTR +yourself (`name = 20.77.168.192.in-addr.arpa`, `type = PTR`, +`value = arcade.bbs.example.org`). Your own PTR replaces the automatic one. + A query for a type the name has no record of gets an empty answer, and is not asked upstream. A CNAME is followed to its target when the target has records here too. A record for one of the old services' names takes the diff --git a/internal/dnsd/records.go b/internal/dnsd/records.go index 967a2c6..de8b4ef 100644 --- a/internal/dnsd/records.go +++ b/internal/dnsd/records.go @@ -10,7 +10,9 @@ import ( // Records are the sysop's own DNS records ([dnsrecord NAME]): answered // as they are, to anyone, before the services' names and before anything -// is asked upstream. A name beginning "*." is every name under it. +// is asked upstream. A name beginning "*." is every name under it. An +// A or AAAA record's address answers a reverse (PTR) query with its name, +// unless there are PTR records of the sysop's for it. type Records struct { exact map[string][]dns.RR // fqdn, lower case suffix map[string][]dns.RR // ".domain." @@ -82,9 +84,39 @@ func NewRecords(rrs []dns.RR) *Records { } r.exact[n] = append(r.exact[n], rr) } + // each A and AAAA's address answers back with its name (a PTR), unless + // the sysop wrote the address's PTRs + for _, rr := range rrs { + var ip net.IP + switch a := rr.(type) { + case *dns.A: + ip = a.A + case *dns.AAAA: + ip = a.AAAA + default: + continue + } + name := rr.Header().Name + rev, err := dns.ReverseAddr(ip.String()) + if err != nil || strings.HasPrefix(name, "*.") || r.wrotePTR(rev, rrs) { + continue + } + r.exact[rev] = append(r.exact[rev], &dns.PTR{Hdr: dns.RR_Header{Name: rev, Rrtype: dns.TypePTR, + Class: dns.ClassINET, Ttl: rr.Header().Ttl}, Ptr: strings.ToLower(name)}) + } return r } +// wrotePTR is whether the sysop's records (rrs) have a PTR for rev. +func (r *Records) wrotePTR(rev string, rrs []dns.RR) bool { + for _, rr := range rrs { + if rr.Header().Rrtype == dns.TypePTR && strings.EqualFold(rr.Header().Name, rev) { + return true + } + } + return false +} + // Len is how many names (and wildcards) have records. func (r *Records) Len() int { if r == nil { diff --git a/internal/dnsd/records_test.go b/internal/dnsd/records_test.go index b6c4b35..861d4c8 100644 --- a/internal/dnsd/records_test.go +++ b/internal/dnsd/records_test.go @@ -23,6 +23,8 @@ func TestRecords(t *testing.T) { {"*.lan.example", "A", "10.0.0.9"}, {"messenger.hotmail.com", "", "192.168.77.30"}, // over the service's own answer {"login.live.com", "TXT", "hello"}, // a type the service has not: its A stays + {"pc2.lan.example", "", "10.0.0.2"}, + {"2.0.0.10.in-addr.arpa", "PTR", "server.lan.example"}, // the sysop's own PTR wins } { rr, err := NewRecord(r.name, r.typ, r.value, 300) if err != nil { @@ -69,6 +71,16 @@ func TestRecords(t *testing.T) { if r := ask("login.live.com", dns.TypeA); len(r.Answer) != 1 || !r.Answer[0].(*dns.A).A.Equal(net.IPv4(127, 0, 0, 1)) { t.Errorf("login.live.com A: %v, want the service's", r.Answer) } + // reverse: an A or AAAA's address gives back its name + if r := ask("20.77.168.192.in-addr.arpa", dns.TypePTR); len(r.Answer) != 1 || r.Answer[0].(*dns.PTR).Ptr != "games.bbs.example." { + t.Errorf("PTR of 192.168.77.20: %v", r.Answer) + } + if r := ask("0.2.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.d.f.ip6.arpa", dns.TypePTR); len(r.Answer) != 1 { + t.Errorf("PTR of fd00::20: %v", r.Answer) + } + if r := ask("2.0.0.10.in-addr.arpa", dns.TypePTR); len(r.Answer) != 1 || r.Answer[0].(*dns.PTR).Ptr != "server.lan.example." { + t.Errorf("PTR of 10.0.0.2: %v, want only the sysop's", r.Answer) + } if n.Load() != 0 { t.Errorf("%d asked upstream", n.Load()) }