diff --git a/docs/wiki/DNS.md b/docs/wiki/DNS.md index aec396d..040a9fd 100644 --- a/docs/wiki/DNS.md +++ b/docs/wiki/DNS.md @@ -176,7 +176,10 @@ which is their PPP link's far end. One port can't be both on every address and on an IP of its own: with `https_address = 192.168.77.11:443` for one front end, give the others an -IP of their own too (not `:443`). `bbs check` refuses the mix. +IP of their own too (not `:443`). `bbs check` refuses the mix, and the +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". ## Who may use it diff --git a/internal/confui/fields.go b/internal/confui/fields.go index abf790e..38bed42 100644 --- a/internal/confui/fields.go +++ b/internal/confui/fields.go @@ -1,6 +1,7 @@ package confui import ( + "errors" "fmt" "sort" "strconv" @@ -9,6 +10,7 @@ import ( "github.com/D-Modem/bbs/internal/bbs" "github.com/D-Modem/bbs/internal/confedit" "github.com/D-Modem/bbs/internal/forum" + "github.com/D-Modem/bbs/internal/vhost" "github.com/D-Modem/bbs/internal/view" ) @@ -141,17 +143,42 @@ func (e *Editor) field(s confedit.Section, key string, label ...string) item { if it.Kind == view.EntryChoice && nv != "" && !contains(it.Choices, nv) { return fmt.Errorf("%s: one of %s", it.Label, strings.Join(it.Choices, ", ")) } + old, had := e.doc.Get(s, key) + wasMixed := e.refuseMixed() != nil if nv == "" { e.doc.Unset(s, key) } else if err := e.doc.Set(s, key, nv); err != nil { return err } + if err := e.refuseMixed(); err != nil && !wasMixed { + // put it back: the BBS could not listen on both + if had { + e.doc.Set(s, key, old) + } else { + e.doc.Unset(s, key) + } + return err + } e.checkDoc() return nil } return it } +// refuseMixed is the error, if the file now has one port both on every +// address and on an IP of its own (the chat's HTTPS services: an IP each, +// or all on one), for the edit that made it to be refused. A file that +// had it already is left to be put right an edit at a time. +func (e *Editor) refuseMixed() error { + if e.Tools.Check == nil { + return nil + } + if _, err := e.Tools.Check(e.doc.Bytes()); errors.Is(err, vhost.ErrMixedAddresses) { + return err + } + return nil +} + // isBool is whether a key is a yes or no: its default and examples are all // yes, no or nothing, and one is yes or no. func isBool(x *confedit.Key) bool { diff --git a/internal/confui/menus.go b/internal/confui/menus.go index ea46998..51ae256 100644 --- a/internal/confui/menus.go +++ b/internal/confui/menus.go @@ -319,6 +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("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()) }), diff --git a/internal/confui/mixed_test.go b/internal/confui/mixed_test.go new file mode 100644 index 0000000..ea84ee4 --- /dev/null +++ b/internal/confui/mixed_test.go @@ -0,0 +1,55 @@ +package confui + +import ( + "fmt" + "strings" + "testing" + + bbssec "github.com/D-Modem/bbs" + "github.com/D-Modem/bbs/internal/bbs" + "github.com/D-Modem/bbs/internal/confedit" + "github.com/D-Modem/bbs/internal/vhost" +) + +// mixedCheck is the BBS's check, as far as it goes here: [msn] on an IP of +// its own and [ymsg] on every address of the same port can't both listen. +func mixedCheck(data []byte) ([]string, error) { + s := string(data) + if strings.Contains(s, "https_address = 192.168.77.11:443") && strings.Contains(s, "https_address = :443") { + return nil, fmt.Errorf("%w: port 443 is on every address for [ymsg] and on 192.168.77.11:443 for [msn]", + vhost.ErrMixedAddresses) + } + return nil, nil +} + +// TestRefuseMixedAddresses: with MSN's HTTPS server on an IP of its own, +// putting Yahoo!'s on every address of the same port is refused and put +// back; on an IP of its own it is taken. +func TestRefuseMixedAddresses(t *testing.T) { + doc, err := confedit.Parse([]byte("[msn]\nhttps_address = 192.168.77.11:443\n[ymsg]\nhttps_address = 192.168.77.12:443\n")) + if err != nil { + t.Fatal(err) + } + orig, _ := confedit.Parse(doc.Bytes()) + e := &Editor{UI: &script{t: t}, Tools: bbs.ConfigTools{Check: mixedCheck}, doc: doc, orig: orig, + cat: confedit.NewCatalog(bbssec.Sample)} + it := e.field(sec("ymsg"), "https_address") + err = it.set(":443") + if err == nil || !strings.Contains(err.Error(), "on every address") { + t.Fatalf("every address beside an IP of its own: %v", err) + } + if v, _ := e.doc.Get(sec("ymsg"), "https_address"); v != "192.168.77.12:443" { + t.Errorf("not put back: %q", v) + } + if err := it.set("192.168.77.13:443"); err != nil { + t.Fatalf("an IP of its own: %v", err) + } + if v, _ := e.doc.Get(sec("ymsg"), "https_address"); v != "192.168.77.13:443" { + t.Errorf("kept %q", v) + } + // a file mixed already is put right an edit at a time + e.doc.Set(sec("ymsg"), "https_address", ":443") + if err := e.field(sec("ymsg"), "max_clients").set("100"); err != nil { + t.Errorf("an edit beside a mix already there: %v", err) + } +} diff --git a/internal/server/dnsconfig.go b/internal/server/dnsconfig.go index 79f1b1c..0728095 100644 --- a/internal/server/dnsconfig.go +++ b/internal/server/dnsconfig.go @@ -320,9 +320,13 @@ func (c *Config) SharedPorts() ([]SharedPort, error) { for _, q := range out[i+1:] { qh, qp, _ := net.SplitHostPort(vhost.Normal(q.Address)) if pp == qp && ph != qh && (ph == "" || qh == "") { - return nil, fmt.Errorf("port %s is on every address for [%s] and on %s for [%s]: give each "+ - "front end an IP of its own (all of them), or the same address", pp, strings.Join(p.Members, "], ["), - q.Address, strings.Join(q.Members, "], [")) + all, own := p, q + if qh == "" { + all, own = q, p + } + return nil, fmt.Errorf("%w: port %s is on every address for [%s] and on %s for [%s]: give each "+ + "front end an IP of its own (all of them), or the same address", vhost.ErrMixedAddresses, pp, + strings.Join(all.Members, "], ["), own.Address, strings.Join(own.Members, "], [")) } } } diff --git a/internal/vhost/vhost.go b/internal/vhost/vhost.go index d5bc678..400ee57 100644 --- a/internal/vhost/vhost.go +++ b/internal/vhost/vhost.go @@ -311,6 +311,12 @@ func (l *connListener) Close() error { func (l *connListener) Addr() net.Addr { return &net.TCPAddr{} } +// ErrMixedAddresses is a port on every address for one front end and on +// an IP of its own for another, which can't both be listened on: each +// HTTPS service on an IP of its own (a certificate of its own each) means +// all of them on one. +var ErrMixedAddresses = errors.New("a port on every address and on an IP of its own") + // claimed are the addresses a Port serves, which the front ends leave // alone. var (