diff --git a/bbs.conf.sample b/bbs.conf.sample index b8ba139..a32bce3 100644 --- a/bbs.conf.sample +++ b/bbs.conf.sample @@ -1065,6 +1065,26 @@ address = 127.0.0.1:2513 ;rate = 50 ;burst = 200 +; A DNS record of your own, answered by [dns] as it is (to anyone, before +; the services' names and before anything is asked upstream): a name for a +; machine on your network, a game server, a mail server for your dial-up +; users. Each [dnsrecord NAME] is one record; give a name several for +; several (two A records, an MX and a TXT ...) +;[dnsrecord games] +; the name (default: NAME); *.domain for every name under it +;name = games.bbs.example.org +; A, AAAA, CNAME, TXT, MX, SRV, NS, PTR, CAA ... (default: A or AAAA, by the +; value's address) +;type = A +; as a zone file writes it: an address; a CNAME's name; a TXT's text; an +; MX's preference and host (10 mail.example.org); an SRV's priority, +; weight, port and host (0 5 5060 sip.example.org) +;value = 192.168.77.20 +; seconds (default: [dns] ttl). An A or AAAA record's address answers +; reverse lookups (PTR) with its name, unless a [dnsrecord] gives that +; address a PTR of its own +;ttl = 300 + ; Multi Relay Chat: the BBSes' chat network. Its rooms are the chat rooms ; beginning mrc- here (mrc-lobby is its lobby), on terminals and IRC alike. ; See docs/wiki/MRC.md diff --git a/docs/wiki/Configuration-Reference.md b/docs/wiki/Configuration-Reference.md index 4deaacf..79ed5cc 100644 --- a/docs/wiki/Configuration-Reference.md +++ b/docs/wiki/Configuration-Reference.md @@ -47,6 +47,7 @@ the file is laid out. | [`[marvel]`](#marvel) | The Microsoft Network of 1995 to 1998, for the MSN client that came with Windows 95: users sign in with their BBS names and passwords, the message areas are its Categories, and each area is an MSN bulletin board to read and post in. | | [`[ymsg]`](#ymsg) | Yahoo! Messenger: the chat for Yahoo! Messenger clients (YMSG 9 to 16), beside IRC, AIM and MSN. | | [`[dns]`](#dns) | DNS: a resolving, caching DNS server for callers (dial-up users above all: give it to them as their DNS server, as pppd's ms-dns). | +| [`[dnsrecord NAME]`](#dnsrecord-name) | A DNS record of your own, answered by [dns] as it is (to anyone, before the services' names and before anything is asked upstream): a name for a machine on your network, a game server, a mail server for your dial-up users. | | [`[mrc]`](#mrc) | Multi Relay Chat: the BBSes' chat network. | | [`[finger]`](#finger) | finger: who is on ("finger @host"), and who a user is ("finger name@host"). | | [`[gopher]`](#gopher) | gopher: about the system, who is on, and what areas the sysop opens: no login, so anyone sees them. | @@ -727,6 +728,20 @@ DNS: a resolving, caching DNS server for callers (dial-up users above all: give | `burst` | | | UDP queries a second one address may make (0: no limit), and at once. Examples: `200`. | +## `[dnsrecord NAME]` + +A DNS record of your own, answered by [dns] as it is (to anyone, before the services' names and before anything is asked upstream): a name for a machine on your network, a game server, a mail server for your dial-up users. Each [dnsrecord NAME] is one record; give a name several for several (two A records, an MX and a TXT ...) + +There may be any number of these, each with its own NAME. + +| key | default | live | what it does | +|---|---|---|---| +| `name` | | | the name (default: NAME); *.domain for every name under it. Examples: `games.bbs.example.org`. | +| `type` | | | A, AAAA, CNAME, TXT, MX, SRV, NS, PTR, CAA ... (default: A or AAAA, by the value's address) Examples: `A`. | +| `value` | | | as a zone file writes it: an address; a CNAME's name; a TXT's text; an MX's preference and host (10 mail.example.org); an SRV's priority, weight, port and host (0 5 5060 sip.example.org) Examples: `192.168.77.20`. | +| `ttl` | | | seconds (default: [dns] ttl). An A or AAAA record's address answers reverse lookups (PTR) with its name, unless a [dnsrecord] gives that address a PTR of its own. Examples: `300`. | + + ## `[mrc]` Multi Relay Chat: the BBSes' chat network. Its rooms are the chat rooms beginning mrc- here (mrc-lobby is its lobby), on terminals and IRC alike. See docs/wiki/MRC.md. diff --git a/docs/wiki/Configuration.md b/docs/wiki/Configuration.md index 371c62c..7d45017 100644 --- a/docs/wiki/Configuration.md +++ b/docs/wiki/Configuration.md @@ -121,6 +121,7 @@ list made from what the file and the machine have, a word beside each: | its key (`tls_key`, `key_file`) | the key that goes with the certificate chosen (from another server, or beside it on disk), and the others the file names; not asked for with `acme` or `ca` | | an address a server listens on | every address, this machine only, and each of this machine's IPs, at the port it has (or the usual one); one another server is on is marked *taken by* | | `[dns]` `answer` and `*_answer` | the IPs the services listen on, and this machine's | +| `[dnsrecord]` `type`, `value` (DNS for callers, DNS records) | the record types, each with what its value looks like; an A or AAAA record's address from this machine's and the services' | | `[dns]` `primary`, `backup` | this host's servers, and the well-known public ones | | `[dns]` `services`, `[feeds]` `catalog`, `[time]` `servers`, `[mx]` `dnsbl`, gopher's and Gemini's `areas` | some of them at once (Space on each) | | levels (new users', validated users', a user's, `[mrc]` and `[menu]` `require`) | the `[level N]`s, with their names | diff --git a/docs/wiki/DNS.md b/docs/wiki/DNS.md index 5946231..733c2a9 100644 --- a/docs/wiki/DNS.md +++ b/docs/wiki/DNS.md @@ -186,6 +186,57 @@ configuration editor (`bbs config`, and the sysop's menu) won't take an edit that makes it: the old value is put back and the reason shown. The editor has the DNS server under Internet services, "DNS for callers". +## Records of your own + +A `[dnsrecord NAME]` section is one DNS record, answered as written. These +records are answered to anyone, before the services' names, and nothing +about the name is asked upstream. Use them for a machine on your network, +a game server, or a mail server for your dial-up users. Give a name more +than one section for more than one record: + +```ini +[dnsrecord games.bbs.example.org] +value = 192.168.77.20 + +[dnsrecord bbs-mx] +name = bbs.example.org +type = MX +value = 10 mail.bbs.example.org + +[dnsrecord bbs-txt] +name = bbs.example.org +type = TXT +value = v=spf1 mx -all + +[dnsrecord lan] +name = *.lan.example.org +value = 192.168.77.5 +ttl = 300 +``` + +| key | what it is | +|---|---| +| `name` | the name (default: the section's NAME); `*.domain` is every name under it | +| `type` | A, AAAA, CNAME, TXT, MX, SRV, NS, PTR, CAA, or any other type (default: A or AAAA, from the value's address) | +| `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 +place of the BBS's answer for that type; the other types still get the +BBS's answer. `bbs check` refuses a record it cannot read. In the +configuration screens these records are under Internet services, DNS for +callers, DNS records, with the type picked from a list and an address +picked from this machine's. + ## Who may use it The services' names are answered to anyone who asks. Other names are diff --git a/internal/confui/choices.go b/internal/confui/choices.go index b7e956d..84db9ff 100644 --- a/internal/confui/choices.go +++ b/internal/confui/choices.go @@ -42,6 +42,7 @@ func (o *offer) add(v, note string) { // more are the closed lists (by "kind.key") beside choices: the values // the BBS takes and no others. "" is the default. var more = map[string][]string{ + "dnsrecord.type": {"", "A", "AAAA", "CNAME", "TXT", "MX", "SRV", "NS", "PTR", "CAA"}, "listen.frontend": {"", "bbs", "compuserve", "marvel"}, "acme.challenge": {"", "http", "tls-alpn", "dns"}, "acme.dns_check": {"", "authoritative", "resolvers", "no"}, @@ -61,6 +62,10 @@ var more = map[string][]string{ // notes are words beside some of the closed lists' values. var notes = map[string]map[string]string{ + "dnsrecord.type": {"": "A or AAAA, by the address", "A": "an IPv4 address", "AAAA": "an IPv6 address", + "CNAME": "another name for a name", "TXT": "text", "MX": "a mail server: 10 mail.example.org", + "SRV": "a service: 0 5 5060 sip.example.org", "NS": "a name server", "PTR": "a name for an address", + "CAA": "who may issue certificates: 0 issue \"letsencrypt.org\""}, "listen.frontend": {"bbs": "the BBS", "compuserve": "CompuServe (CIS)", "marvel": "MSN 1995, dial-in"}, "acme.challenge": {"http": "a token on port 80", "tls-alpn": "a certificate on port 443", "dns": "a TXT record (the only one for *.wildcards)"}, @@ -146,6 +151,24 @@ func (e *Editor) offerFor(s confedit.Section, key string) (offer, bool) { o = e.addressOffer(s, key) case kk == "dns.answer" || kk == "dns.answer6" || s.Kind == "dns" && strings.HasSuffix(key, "_answer"): o = e.answerOffer(key == "answer6") + case kk == "dnsrecord.value": + switch t := strings.ToUpper(e.value(s, "type", "")); t { + case "", "A", "AAAA": + o = e.answerOffer(t == "AAAA") + o.values, o.notes = o.values[1:], o.notes[1:] + default: + return offer{}, false + } + case kk == "dnsrecord.name": + o.open = true + o.add("", s.Name) + for _, n := range strings.FieldsFunc(e.value(sec("dns"), "names", ""), func(r rune) bool { return r == ',' || r == ' ' }) { + if !strings.HasPrefix(n, "*.") { + o.add(n, "a name of [dns] names") + } else { + o.add(strings.Replace(n, "*", s.Name, 1), "under "+n[2:]) + } + } case kk == "dns.services": o.multi, o.open = true, true for _, n := range dnsd.ServiceNames() { diff --git a/internal/confui/choices_test.go b/internal/confui/choices_test.go index b166064..fa184be 100644 --- a/internal/confui/choices_test.go +++ b/internal/confui/choices_test.go @@ -197,3 +197,38 @@ func TestPickChoice(t *testing.T) { t.Errorf("%q %v %v", v, ok, err) } } + +// TestDNSRecordsPage: a record is added under DNS for callers, its type +// picked from a list, its address from this machine's. +func TestDNSRecordsPage(t *testing.T) { + fakeAddrs(t) + p := filepath.Join(t.TempDir(), "bbs.conf") + os.WriteFile(p, []byte("[general]\nname = Test\n\n[dns]\naddress = :53\n"), 0o600) + ui := &script{t: t, forms: []map[string]string{{"name": "games.bbs.example"}}, steps: []step{ + open("Internet services"), open("DNS for callers"), open("DNS records"), key("add"), + func(t *testing.T, sh *view.Sheet) view.SheetResult { + ty := sh.Entries[entry(t, sh, "type")] + if ty.Kind != view.EntryChoice || !slices.Contains(ty.Choices, "MX") { + t.Errorf("type: %+v", ty) + } + v := sh.Entries[entry(t, sh, "value")] + if !slices.Contains(v.Choices, "192.0.2.10") { + t.Errorf("value's choices %q", v.Choices) + } + return set("value", "192.0.2.10")(t, sh) + }, + back, + func(t *testing.T, sh *view.Sheet) view.SheetResult { + if it := sh.Entries[entry(t, sh, "games.bbs.example")]; it.Value != "games.bbs.example A 192.0.2.10" { + t.Errorf("summary %q", it.Value) + } + return save(t, sh) + }, + }} + if err := (&Editor{UI: ui, Tools: bbs.ConfigTools{Path: p}}).Run(); err != nil { + t.Fatal(err) + } + if data, _ := os.ReadFile(p); !strings.Contains(string(data), "[dnsrecord games.bbs.example]\ntype = A\nvalue = 192.0.2.10\n") { + t.Errorf("written:\n%s", data) + } +} diff --git a/internal/confui/menus.go b/internal/confui/menus.go index 51ae256..e443cb9 100644 --- a/internal/confui/menus.go +++ b/internal/confui/menus.go @@ -319,7 +319,7 @@ func (e *Editor) servicesMenu() page { e.single("Gopher", "gopher"), e.single("Gemini", "gemini"), e.single("FTP", "ftp"), - e.single("DNS for callers", "dns"), + menu("DNS for callers", e.onOff(sec("dns")), func() error { return e.show(e.dnsPage()) }), menu("News (NNTP)", e.onOff(sec("nntp")), func() error { return e.show(e.newsPage()) }), menu("Web: API, forum, image boards", e.onOff(sec("http")), func() error { return e.show(e.webPage()) }), menu("Certificates (ACME)", e.onOff(sec("acme")), func() error { return e.show(e.acmePage()) }), @@ -480,6 +480,32 @@ func (e *Editor) acmePage() page { }} } +// dnsPage is DNS for callers: [dns], and the records of the sysop's own +// ([dnsrecord NAME]s). +func (e *Editor) dnsPage() page { + return page{title: "DNS for callers", items: func() []item { + out := []item{e.enabled(sec("dns"), "Enabled")} + out = append(out, e.sectionFields(sec("dns"))...) + out = append(out, heading("Records of your own"), menu("DNS records", e.count("dnsrecord", nil, "record"), func() error { + return e.show(e.listPage("DNS records", "dnsrecord", "record", nil, func(s confedit.Section) string { + return e.recordWords(s) + }, map[string]string{"type": "A"})) + })) + return out + }} +} + +// recordWords is a [dnsrecord]'s record in a line: name, type, value. +func (e *Editor) recordWords(s confedit.Section) string { + name := e.value(s, "name", s.Name) + t := e.value(s, "type", "A") + v, _ := e.doc.Get(s, "value") + if v == "" { + v = "(no value yet)" + } + return name + " " + strings.ToUpper(t) + " " + v +} + // tn3270Page is TN3270: the 3270 pictures and logo, and the tn3270 key of // each telnet listener. func (e *Editor) tn3270Page() page { diff --git a/internal/dnsd/records.go b/internal/dnsd/records.go new file mode 100644 index 0000000..de8b4ef --- /dev/null +++ b/internal/dnsd/records.go @@ -0,0 +1,200 @@ +package dnsd + +import ( + "fmt" + "net" + "strings" + + "github.com/miekg/dns" +) + +// 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. 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." +} + +// NewRecord is a record of the sysop's: name, its type (A, AAAA, CNAME, +// TXT, MX, SRV, ... as a zone file names them; "" for an A or AAAA by the +// value's address), its value as a zone file writes it (an MX's "10 +// mail.example.org", a TXT's text), and its TTL. +func NewRecord(name, typ, value string, ttl uint32) (dns.RR, error) { + name = strings.TrimSpace(name) + value = strings.TrimSpace(value) + typ = strings.ToUpper(strings.TrimSpace(typ)) + if name == "" { + return nil, fmt.Errorf("no name") + } + if strings.ContainsAny(name, " \t/@:") || strings.Contains(strings.TrimPrefix(name, "*."), "*") { + return nil, fmt.Errorf("name: %q is not a host name (or *.domain)", name) + } + if value == "" { + return nil, fmt.Errorf("no value") + } + if typ == "" { + ip := net.ParseIP(value) + switch { + case ip == nil: + return nil, fmt.Errorf("type: none given, and %q is not an IP address (an A or AAAA record's)", value) + case ip.To4() != nil: + typ = "A" + default: + typ = "AAAA" + } + } + if _, ok := dns.StringToType[typ]; !ok { + return nil, fmt.Errorf("type: %q is no DNS record type (A, AAAA, CNAME, TXT, MX, SRV ...)", typ) + } + switch typ { + case "TXT", "SPF": + if !strings.HasPrefix(value, `"`) { + value = `"` + strings.ReplaceAll(strings.ReplaceAll(value, `\`, `\\`), `"`, `\"`) + `"` + } + case "CNAME", "NS", "PTR", "DNAME": + value = dns.Fqdn(value) + case "SOA": + return nil, fmt.Errorf("type: SOA is not for a record of your own") + } + rr, err := dns.NewRR(fmt.Sprintf("%s %d IN %s %s", dns.Fqdn(strings.ToLower(name)), ttl, typ, value)) + if err != nil { + msg := err.Error() + if pe, ok := err.(*dns.ParseError); ok { + msg = pe.Error() + } + return nil, fmt.Errorf("value: %q is not a %s record's (%s)", value, typ, msg) + } + if rr == nil { + return nil, fmt.Errorf("value: %q is not a %s record's", value, typ) + } + return rr, nil +} + +// NewRecords is a table of records. +func NewRecords(rrs []dns.RR) *Records { + r := &Records{exact: map[string][]dns.RR{}, suffix: map[string][]dns.RR{}} + for _, rr := range rrs { + n := strings.ToLower(rr.Header().Name) + if strings.HasPrefix(n, "*.") { + r.suffix[n[1:]] = append(r.suffix[n[1:]], rr) + continue + } + 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 { + return 0 + } + return len(r.exact) + len(r.suffix) +} + +// lookup is the records of name (a wildcard's with its name made name's), +// and whether it has any. +func (r *Records) lookup(name string) ([]dns.RR, bool) { + if r == nil { + return nil, false + } + name = strings.ToLower(dns.Fqdn(name)) + if rrs, ok := r.exact[name]; ok { + return rrs, true + } + for i := 0; i < len(name)-1; i++ { + if name[i] != '.' { + continue + } + if rrs, ok := r.suffix[name[i:]]; ok { + out := make([]dns.RR, len(rrs)) + for j, rr := range rrs { + c := dns.Copy(rr) + c.Header().Name = name + out[j] = c + } + return out, true + } + } + return nil, false +} + +// answer is the records answering name and qtype: those of the type (all, +// for ANY), or else a CNAME and what its target has here; has is whether +// the name has records at all. +func (r *Records) answer(name string, qtype uint16) (ans []dns.RR, has bool) { + rrs, has := r.lookup(name) + if !has { + return nil, false + } + for _, rr := range rrs { + if t := rr.Header().Rrtype; t == qtype || qtype == dns.TypeANY { + ans = append(ans, rr) + } + } + if len(ans) > 0 || qtype == dns.TypeCNAME { + return ans, true + } + for _, rr := range rrs { + if c, ok := rr.(*dns.CNAME); ok { + ans = append(ans, c) + // its target's, when they are here too (one step: no loops) + if more, ok := r.lookup(c.Target); ok { + for _, m := range more { + if m.Header().Rrtype == qtype { + ans = append(ans, m) + } + } + } + return ans, true + } + } + return nil, true +} + +// fixName gives rrs the question's own spelling of the name (as the +// client asked: 0x20 case), for the records of that name. +func fixName(rrs []dns.RR, asked string) []dns.RR { + out := make([]dns.RR, len(rrs)) + for i, rr := range rrs { + c := dns.Copy(rr) + if strings.EqualFold(c.Header().Name, asked) { + c.Header().Name = asked + } + out[i] = c + } + return out +} diff --git a/internal/dnsd/records_test.go b/internal/dnsd/records_test.go new file mode 100644 index 0000000..861d4c8 --- /dev/null +++ b/internal/dnsd/records_test.go @@ -0,0 +1,95 @@ +package dnsd + +import ( + "net" + "testing" + + "github.com/miekg/dns" +) + +// TestRecords: the sysop's records are answered as they are, to anyone, +// before the services' names and before anything upstream; a CNAME is +// followed to a target here; a wildcard takes the name asked. +func TestRecords(t *testing.T) { + up, n := fakeUpstream(t) + s := newTest(t, up) + var rrs []dns.RR + for _, r := range []struct{ name, typ, value string }{ + {"games.bbs.example", "", "192.168.77.20"}, + {"games.bbs.example", "", "fd00::20"}, + {"games.bbs.example", "TXT", `Play "LORD" here`}, + {"bbs.example", "MX", "10 mail.bbs.example"}, + {"www.bbs.example", "CNAME", "games.bbs.example"}, + {"*.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 { + t.Fatalf("%v: %v", r, err) + } + rrs = append(rrs, rr) + } + s.S.Records = rrs + s.recs = NewRecords(rrs) + outsider := net.IPv4(203, 0, 113, 9) // not allowed to recurse + ask := func(name string, qtype uint16) *dns.Msg { + t.Helper() + r := s.Answer(query(name, qtype), outsider, local) + if r.Rcode != dns.RcodeSuccess || !r.Authoritative { + t.Fatalf("%s %s: %v", name, dns.TypeToString[qtype], r) + } + return r + } + if r := ask("Games.BBS.example", dns.TypeA); len(r.Answer) != 1 || !r.Answer[0].(*dns.A).A.Equal(net.IPv4(192, 168, 77, 20)) || + r.Answer[0].Header().Name != "Games.BBS.example." { + t.Errorf("games A: %v", r.Answer) + } + if r := ask("games.bbs.example", dns.TypeAAAA); len(r.Answer) != 1 { + t.Errorf("games AAAA: %v", r.Answer) + } + if r := ask("games.bbs.example", dns.TypeTXT); len(r.Answer) != 1 || r.Answer[0].(*dns.TXT).Txt[0] != `Play \"LORD\" here` { + t.Errorf("games TXT: %v", r.Answer) + } + if r := ask("bbs.example", dns.TypeMX); len(r.Answer) != 1 || r.Answer[0].(*dns.MX).Mx != "mail.bbs.example." { + t.Errorf("MX: %v", r.Answer) + } + if r := ask("bbs.example", dns.TypeA); len(r.Answer) != 0 { + t.Errorf("bbs.example A: %v, want none", r.Answer) + } + if r := ask("www.bbs.example", dns.TypeA); len(r.Answer) != 2 || r.Answer[0].Header().Rrtype != dns.TypeCNAME { + t.Errorf("www A: %v, want the CNAME and its A", r.Answer) + } + if r := ask("pc1.lan.example", dns.TypeA); len(r.Answer) != 1 || r.Answer[0].Header().Name != "pc1.lan.example." { + t.Errorf("wildcard: %v", r.Answer) + } + if r := ask("messenger.hotmail.com", dns.TypeA); !r.Answer[0].(*dns.A).A.Equal(net.IPv4(192, 168, 77, 30)) { + t.Errorf("messenger: %v, want the record's", r.Answer) + } + 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()) + } + for _, bad := range []struct{ name, typ, value string }{ + {"x.example", "", "not-an-ip"}, {"x.example", "BOGUS", "1"}, {"x.example", "MX", "mail.example"}, + {"a b.example", "A", "10.0.0.1"}, {"x.example", "A", ""}, {"x.example", "SOA", "a b 1 2 3 4 5"}, + } { + if _, err := NewRecord(bad.name, bad.typ, bad.value, 60); err == nil { + t.Errorf("%v: taken", bad) + } + } +} diff --git a/internal/dnsd/server.go b/internal/dnsd/server.go index 3f03194..27dc5eb 100644 --- a/internal/dnsd/server.go +++ b/internal/dnsd/server.go @@ -40,6 +40,9 @@ type Settings struct { ServiceAnswers map[string][]net.IP Services []string // whose names are answered (keys of Services) Names []string // more names answered with the BBS's address + // Records are the sysop's own ([dnsrecord NAME]), answered as they + // are before all else + Records []dns.RR // Allow are the networks whose clients may resolve other names (the // services' names are answered to anyone) Allow []*net.IPNet @@ -62,6 +65,7 @@ type Server struct { S Settings Log *log.Logger table *Table + recs *Records cache *cache up *upstreams group singleflight.Group @@ -88,7 +92,7 @@ func New(s Settings, lg *log.Logger) *Server { if s.Timeout == 0 { s.Timeout = 2 * time.Second } - srv := &Server{S: s, Log: lg, table: NewTable(s.Services, s.Names), + srv := &Server{S: s, Log: lg, table: NewTable(s.Services, s.Names), recs: NewRecords(s.Records), cache: newCache(s.CacheSize, s.MaxTTL, s.NegTTL), up: &upstreams{fixed: s.Upstreams, self: []string{s.Address}}} if s.Rate > 0 { @@ -173,6 +177,16 @@ func (s *Server) Answer(req *dns.Msg, client net.IP, local net.Addr) *dns.Msg { } q := req.Question[0] allowed := s.allowed(client) + if ans, has := s.recs.answer(q.Name, q.Qtype); has && q.Qclass == dns.ClassINET && + (len(ans) > 0 || s.table.Match(q.Name) == "") { + // the sysop's own: as they are (none of the type: no records, + // NOERROR, so nothing upstream answers for the name) + r.SetReply(req) + r.Authoritative = true + r.RecursionAvailable = allowed + r.Answer = fixName(ans, q.Name) + return r + } if who := s.table.Match(q.Name); who != "" && q.Qclass == dns.ClassINET { r.SetReply(req) r.Authoritative = true @@ -446,7 +460,18 @@ func (s Settings) String() string { } return "dns: " + s.Address + ", " + strconv.Itoa(len(NewTable(s.Services, s.Names).exact)) + " names (" + fmt.Sprint(s.Services) + ") answered with " + ans + perService(s.ServiceAnswers) + ", the rest from " + up + - ", " + strconv.Itoa(s.CacheSize) + " kept" + records(len(s.Records)) + ", " + strconv.Itoa(s.CacheSize) + " kept" +} + +// records is how many records of the sysop's there are, for String. +func records(n int) string { + switch n { + case 0: + return "" + case 1: + return ", 1 record of your own" + } + return ", " + strconv.Itoa(n) + " records of your own" } // perService describes the services' own addresses, for String. diff --git a/internal/server/config.go b/internal/server/config.go index e2905f3..4a25d7c 100644 --- a/internal/server/config.go +++ b/internal/server/config.go @@ -273,7 +273,7 @@ func load(cf *config.File) (*Config, error) { } for _, s := range cf.Sections { switch s.Kind { - case "general", "security", "acme", "backup", "modemmenu", "event", "listen", "ftp", "news", "nntp", "peer", "qwknet", "fido", "binkp", "link", "door", "finger", "gopher", "gemini", "time", "ntp", "nts", "ptp", "dns", "irc", "oscar", "aol", "msn", "marvel", "ymsg", "mrc", "irclink", "ircpeer", "menu", "strings", "http", "level", "chan", + case "general", "security", "acme", "backup", "modemmenu", "event", "listen", "ftp", "news", "nntp", "peer", "qwknet", "fido", "binkp", "link", "door", "finger", "gopher", "gemini", "time", "ntp", "nts", "ptp", "dns", "dnsrecord", "irc", "oscar", "aol", "msn", "marvel", "ymsg", "mrc", "irclink", "ircpeer", "menu", "strings", "http", "level", "chan", "feeds", "feed", "weather", "quotes", "mail", "smarthost", "pop3", "imap", "smtp", "mx", "lmtp": default: diff --git a/internal/server/dnsconfig.go b/internal/server/dnsconfig.go index 0728095..e6a7423 100644 --- a/internal/server/dnsconfig.go +++ b/internal/server/dnsconfig.go @@ -8,6 +8,8 @@ import ( "strings" "time" + "github.com/miekg/dns" + "github.com/D-Modem/bbs/internal/config" "github.com/D-Modem/bbs/internal/dnsd" "github.com/D-Modem/bbs/internal/vhost" @@ -19,6 +21,9 @@ import ( func (c *Config) dnsSection(cf *config.File) error { s := cf.Get("dns", "") if s == nil { + if len(cf.All("dnsrecord")) > 0 { + c.Warnings = append(c.Warnings, "[dnsrecord] without a [dns] section: no DNS server answers it") + } return nil } err := func() error { @@ -141,6 +146,9 @@ func (c *Config) dnsSection(cf *config.File) error { return err } d.TTL = uint32(ttl) + if d.Records, err = dnsRecords(cf, d.TTL); err != nil { + return err + } if d.Timeout, err = s.Seconds("timeout", 2, 1, 30); err != nil { return err } @@ -370,3 +378,21 @@ func (c *Config) sharedPortsCheck() error { } return nil } + +// dnsRecords reads the [dnsrecord NAME]s: the sysop's own records, each a +// name (NAME by default), type, value and ttl (by default [dns]'s). +func dnsRecords(cf *config.File, ttl uint32) ([]dns.RR, error) { + var out []dns.RR + for _, rs := range cf.All("dnsrecord") { + t, err := rs.Int("ttl", int(ttl), 0, 7*86400) + if err != nil { + return nil, fmt.Errorf("[dnsrecord %s] %w", rs.Name, err) + } + rr, err := dnsd.NewRecord(rs.String("name", rs.Name), rs.String("type", ""), rs.String("value", ""), uint32(t)) + if err != nil { + return nil, fmt.Errorf("[dnsrecord %s] %w", rs.Name, err) + } + out = append(out, rr) + } + return out, nil +} diff --git a/internal/server/dnsconfig_test.go b/internal/server/dnsconfig_test.go index 39b65cd..e8756b4 100644 --- a/internal/server/dnsconfig_test.go +++ b/internal/server/dnsconfig_test.go @@ -130,3 +130,31 @@ func TestDNSServiceAnswers(t *testing.T) { t.Errorf("two IPv4 answers: %v", err) } } + +// TestDNSRecords: the [dnsrecord NAME]s, by NAME or their name, a bad one +// refused, and one without [dns] warned of. +func TestDNSRecords(t *testing.T) { + dir := t.TempDir() + load := func(body string) (*Config, error) { + p := filepath.Join(dir, "bbs.conf") + os.WriteFile(p, []byte("[listen telnet]\ntype = telnet\naddress = :2300\n"+body), 0600) + return Load(p) + } + c, err := load("[dns]\nttl = 90\n[dnsrecord games.bbs.example]\nvalue = 192.168.77.20\n" + + "[dnsrecord mail]\nname = bbs.example\ntype = mx\nvalue = 10 mail.bbs.example\nttl = 3600\n") + if err != nil { + t.Fatal(err) + } + if r := c.DNS.Records; len(r) != 2 || r[0].Header().Name != "games.bbs.example." || r[0].Header().Ttl != 90 || + r[1].Header().Name != "bbs.example." || r[1].Header().Ttl != 3600 { + t.Errorf("records %v", r) + } + if _, err := load("[dns]\n[dnsrecord x.example]\ntype = MX\nvalue = nope\n"); err == nil || + !strings.Contains(err.Error(), "[dnsrecord x.example]") { + t.Errorf("bad MX: %v", err) + } + c, err = load("[dnsrecord x.example]\nvalue = 10.0.0.1\n") + if err != nil || !slices.ContainsFunc(c.Warnings, func(w string) bool { return strings.Contains(w, "without a [dns]") }) { + t.Errorf("no [dns]: %v %v", err, c.Warnings) + } +}