diff --git a/bbs.conf.sample b/bbs.conf.sample index 8191d4c..208a84f 100644 --- a/bbs.conf.sample +++ b/bbs.conf.sample @@ -1031,6 +1031,16 @@ address = 127.0.0.1:2513 ; IPv6 (default: likewise, for a caller that came by IPv6) ;answer = 192.168.77.1 ;answer6 = +; a service's own address, given for its names instead (an IPv4 and an +; IPv6 one at most): for an HTTPS server on an IP of its own, with a +; self-signed certificate of its own. Default: the IP its https_address +; (or [oscar] tls_address, or address) is given, when it is one of its own +;msn_answer = 192.168.77.11 +;ymsg_answer = 192.168.77.12 +;oscar_answer = 192.168.77.13 +;aol_answer = +;marvel_answer = +;compuserve_answer = ; whose names: auto (the front ends set up here), none, or some of msn, ; marvel, oscar, aol, ymsg, compuserve ;services = auto diff --git a/docs/wiki/Configuration-Reference.md b/docs/wiki/Configuration-Reference.md index 6f1a8a7..4b8c66d 100644 --- a/docs/wiki/Configuration-Reference.md +++ b/docs/wiki/Configuration-Reference.md @@ -709,6 +709,12 @@ DNS: a resolving, caching DNS server for callers (dial-up users above all: give | `backup` | | | the servers names are asked of, by IP address (port 53 unless said); none: the host's (its /etc/resolv.conf, or systemd-resolved's own list) Examples: `8.8.8.8`. | | `answer` | | | the address the services' names are given (default: the BBS's address as each caller reaches it: the PPP link's for a dial-up user); answer6 for IPv6 (default: likewise, for a caller that came by IPv6) Examples: `192.168.77.1`. | | `answer6` | | | the address the services' names are given (default: the BBS's address as each caller reaches it: the PPP link's for a dial-up user); answer6 for IPv6 (default: likewise, for a caller that came by IPv6) | +| `msn_answer` | | | a service's own address, given for its names instead (an IPv4 and an IPv6 one at most): for an HTTPS server on an IP of its own, with a self-signed certificate of its own. Default: the IP its https_address (or [oscar] tls_address, or address) is given, when it is one of its own. Examples: `192.168.77.11`. | +| `ymsg_answer` | | | a service's own address, given for its names instead (an IPv4 and an IPv6 one at most): for an HTTPS server on an IP of its own, with a self-signed certificate of its own. Default: the IP its https_address (or [oscar] tls_address, or address) is given, when it is one of its own. Examples: `192.168.77.12`. | +| `oscar_answer` | | | a service's own address, given for its names instead (an IPv4 and an IPv6 one at most): for an HTTPS server on an IP of its own, with a self-signed certificate of its own. Default: the IP its https_address (or [oscar] tls_address, or address) is given, when it is one of its own. Examples: `192.168.77.13`. | +| `aol_answer` | | | a service's own address, given for its names instead (an IPv4 and an IPv6 one at most): for an HTTPS server on an IP of its own, with a self-signed certificate of its own. Default: the IP its https_address (or [oscar] tls_address, or address) is given, when it is one of its own. | +| `marvel_answer` | | | a service's own address, given for its names instead (an IPv4 and an IPv6 one at most): for an HTTPS server on an IP of its own, with a self-signed certificate of its own. Default: the IP its https_address (or [oscar] tls_address, or address) is given, when it is one of its own. | +| `compuserve_answer` | | | a service's own address, given for its names instead (an IPv4 and an IPv6 one at most): for an HTTPS server on an IP of its own, with a self-signed certificate of its own. Default: the IP its https_address (or [oscar] tls_address, or address) is given, when it is one of its own. | | `services` | | | whose names: auto (the front ends set up here), none, or some of msn, marvel, oscar, aol, ymsg, compuserve. Examples: `auto`. | | `names` | | | more names answered with the BBS's address (*.domain: every name under it) Examples: `bbs.example.org`. | | `allow` | | | the networks that may resolve other names (default: this machine and private networks). The services' names are answered to anyone. Examples: `127.0.0.0/8, 192.168.77.0/24`. | diff --git a/docs/wiki/DNS.md b/docs/wiki/DNS.md index 300d83a..aec396d 100644 --- a/docs/wiki/DNS.md +++ b/docs/wiki/DNS.md @@ -30,6 +30,7 @@ backup = 8.8.8.8 | `primary`, `backup` | the host's | the servers other names are asked of, by IP address (`:port` if not 53); the backup is asked when the primary fails | | `answer` | the BBS's address as the caller reaches it | the IPv4 address the services' names are given | | `answer6` | likewise, for a caller that came by IPv6 | the IPv6 address they are given | +| `msn_answer`, `ymsg_answer`, `oscar_answer`, `aol_answer`, `marvel_answer`, `compuserve_answer` | the IP the service's HTTPS (TLS) server or port is given, when it has one of its own | that service's own address (an IPv4 and an IPv6 one at most), given for its names instead of `answer` | | `services` | `auto` | whose names: `auto` (the front ends set up here), `none`, or some of `msn`, `marvel`, `oscar`, `aol`, `ymsg`, `compuserve` | | `names` | none | more names answered with the BBS's address; `*.example.org` is every name under it | | `allow` | this machine and private networks | the networks that may resolve other names | @@ -133,6 +134,50 @@ openssl x509 -req -in legacy.csr -CA ca.crt -CAkey ca.key -CAcreateserial \ of their own. A port that is TLS for one front end and plain for another can't be shared, and is refused. +### An IP for each HTTPS service + +When each service needs a self-signed certificate of its own (an old +client that checks only the certificate's common name, not a SAN list), +give each its own IP instead of sharing one. Put the addresses on the +BBS machine (a dummy interface, or more addresses on the LAN one), and +give each front end's HTTPS server one: + +```ini +[msn] +address = :1863 +https_address = 192.168.77.11:443 +tls_cert = /etc/bbs/passport.crt +tls_key = /etc/bbs/passport.key + +[ymsg] +address = :5050 +http_address = :80 +https_address = 192.168.77.12:443 +tls_cert = /etc/bbs/login.yahoo.com.crt +tls_key = /etc/bbs/login.yahoo.com.key + +[oscar] +address = :5190 +tls_address = 192.168.77.13:443 +tls_cert = /etc/bbs/aim.crt +tls_key = /etc/bbs/aim.key + +[dns] +``` + +`[dns]` then gives each service's names its own address (MSN's names +192.168.77.11, Yahoo!'s 192.168.77.12, AIM's 192.168.77.13), with no +more settings: a front end's HTTPS (or TLS) address, when it names an IP, +is the one its names get. `msn_answer` and the like set it otherwise (an +address behind NAT, say). Keep the plain ports (`address`, `http_address`) +on every address, as above, so a client reaches them at the service's IP +too. Dial-up callers reach the extra addresses through the BBS machine, +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. + ## Who may use it The services' names are answered to anyone who asks. Other names are diff --git a/internal/dnsd/dnsd_test.go b/internal/dnsd/dnsd_test.go index ba35a0c..f2898c9 100644 --- a/internal/dnsd/dnsd_test.go +++ b/internal/dnsd/dnsd_test.go @@ -250,3 +250,19 @@ func TestServerAddress(t *testing.T) { t.Error("a name taken") } } + +func TestServiceAnswers(t *testing.T) { + s := New(Settings{Address: ":53", Services: []string{"msn", "ymsg"}, Answer: net.IPv4(192, 168, 77, 1), + ServiceAnswers: map[string][]net.IP{"msn": {net.IPv4(192, 168, 77, 11), net.ParseIP("fd00::11")}}}, nil) + for name, want := range map[string]string{"login.live.com": "192.168.77.11", "messenger.hotmail.com": "192.168.77.11", + "login.yahoo.com": "192.168.77.1"} { + r := s.Answer(query(name, dns.TypeA), net.IPv4(192, 168, 77, 2), local) + if len(r.Answer) != 1 || r.Answer[0].(*dns.A).A.String() != want { + t.Errorf("%s: %v, want %s", name, r, want) + } + } + r := s.Answer(query("login.live.com", dns.TypeAAAA), net.IPv4(192, 168, 77, 2), local) + if len(r.Answer) != 1 || r.Answer[0].(*dns.AAAA).AAAA.String() != "fd00::11" { + t.Errorf("AAAA: %v", r) + } +} diff --git a/internal/dnsd/server.go b/internal/dnsd/server.go index ade72f8..3f03194 100644 --- a/internal/dnsd/server.go +++ b/internal/dnsd/server.go @@ -33,8 +33,13 @@ type Settings struct { // BBS's address as the asker reaches it); Answer6 their IPv6 one (nil: // likewise, when the asker came by IPv6; none otherwise) Answer, Answer6 net.IP - Services []string // whose names are answered (keys of Services) - Names []string // more names answered with the BBS's address + // ServiceAnswers are services' own addresses (an IPv4 and an IPv6 one + // at most), given for their names instead of Answer and Answer6: for a + // service whose HTTPS server is on an IP of its own, with a + // certificate of its own + ServiceAnswers map[string][]net.IP + Services []string // whose names are answered (keys of Services) + Names []string // more names answered with the BBS's address // Allow are the networks whose clients may resolve other names (the // services' names are answered to anyone) Allow []*net.IPNet @@ -172,7 +177,7 @@ func (s *Server) Answer(req *dns.Msg, client net.IP, local net.Addr) *dns.Msg { r.SetReply(req) r.Authoritative = true r.RecursionAvailable = allowed - if ip := s.answerFor(q.Qtype, client, local); ip != nil { + if ip := s.answerFor(who, q.Qtype, client, local); ip != nil { hdr := dns.RR_Header{Name: q.Name, Rrtype: q.Qtype, Class: dns.ClassINET, Ttl: s.S.TTL} if q.Qtype == dns.TypeA { r.Answer = append(r.Answer, &dns.A{Hdr: hdr, A: ip.To4()}) @@ -235,7 +240,19 @@ func (s *Server) resolve(req *dns.Msg) (*dns.Msg, error) { // answerFor is the address the services' names are given, for an A or // AAAA query (nil: none). -func (s *Server) answerFor(qtype uint16, client net.IP, local net.Addr) net.IP { +func (s *Server) answerFor(who string, qtype uint16, client net.IP, local net.Addr) net.IP { + if ips := s.S.ServiceAnswers[who]; len(ips) > 0 { + // the service's own address (its HTTPS server on an IP of its own) + for _, ip := range ips { + if qtype == dns.TypeA && ip.To4() != nil { + return ip.To4() + } + if qtype == dns.TypeAAAA && ip.To4() == nil { + return ip + } + } + return nil + } switch qtype { case dns.TypeA: if s.S.Answer != nil { @@ -428,6 +445,17 @@ func (s Settings) String() string { ans = s.Answer.String() } return "dns: " + s.Address + ", " + strconv.Itoa(len(NewTable(s.Services, s.Names).exact)) + - " names (" + fmt.Sprint(s.Services) + ") answered with " + ans + ", the rest from " + up + + " names (" + fmt.Sprint(s.Services) + ") answered with " + ans + perService(s.ServiceAnswers) + ", the rest from " + up + ", " + strconv.Itoa(s.CacheSize) + " kept" } + +// perService describes the services' own addresses, for String. +func perService(m map[string][]net.IP) string { + out := "" + for _, k := range ServiceNames() { + if ips := m[k]; len(ips) > 0 { + out += fmt.Sprintf(" (%s: %v)", k, ips) + } + } + return out +} diff --git a/internal/server/dnsconfig.go b/internal/server/dnsconfig.go index 77457ea..79f1b1c 100644 --- a/internal/server/dnsconfig.go +++ b/internal/server/dnsconfig.go @@ -85,6 +85,32 @@ func (c *Config) dnsSection(cf *config.File) error { } } } + // each service's own address: _answer, or the IP its + // HTTPS (TLS) server or its port is given + for _, svc := range dnsd.ServiceNames() { + key := svc + "_answer" + var ips []net.IP + if v := s.String(key, ""); v != "" { + for _, f := range splitList(v) { + ip := net.ParseIP(f) + if ip == nil { + return fmt.Errorf("%s: an IP address (an IPv4 and an IPv6 one at most)", key) + } + ips = append(ips, ip) + } + if len(ips) > 2 || len(ips) == 2 && (ips[0].To4() != nil) == (ips[1].To4() != nil) { + return fmt.Errorf("%s: an IPv4 address, an IPv6 one, or one of each", key) + } + } else if ip := c.serviceIP(svc); ip != nil { + ips = []net.IP{ip} + } + if len(ips) > 0 && slices.Contains(d.Services, svc) { + if d.ServiceAnswers == nil { + d.ServiceAnswers = map[string][]net.IP{} + } + d.ServiceAnswers[svc] = ips + } + } for _, n := range splitList(s.String("names", "")) { n = strings.ToLower(strings.TrimSuffix(n, ".")) if strings.ContainsAny(n, "/:@") || !strings.Contains(n, ".") || strings.Contains(n[1:], "*") || @@ -167,6 +193,45 @@ func (c *Config) dnsServicesOn() []string { return on } +// serviceIP is the IP a service's server is given, when it is one of +// its own (not every address, nor loopback): its HTTPS or TLS address's, +// else its address's. nil: none. +func (c *Config) serviceIP(svc string) net.IP { + var addrs []string + switch svc { + case "msn": + if m := c.MSN; m != nil { + addrs = []string{m.HTTPSAddress, m.Address} + } + case "ymsg": + if y := c.YMSG; y != nil { + addrs = []string{y.HTTPSAddress, y.Address} + } + case "oscar": + if o := c.OSCAR; o != nil { + addrs = []string{o.TLSAddress, o.Address} + } + case "aol": + if a := c.AOL; a != nil { + addrs = []string{a.Address} + } + case "marvel": + if m := c.Marvel; m != nil { + addrs = []string{m.Address} + } + } + for _, a := range addrs { + h, _, err := net.SplitHostPort(a) + if err != nil { + continue + } + if ip := net.ParseIP(h); ip != nil && !ip.IsUnspecified() && !ip.IsLoopback() { + return ip + } + } + return nil +} + // dnsWarnings: the clients sent here by name come to the old services' // ports, so a front end listening elsewhere is not found. func (c *Config) dnsWarnings() { @@ -248,6 +313,19 @@ func (c *Config) SharedPorts() ([]SharedPort, error) { } out[i].Members = append(out[i].Members, u.who) } + // one port on every address and on an address of its own can't both + // be listened on: give each front end an IP of its own, or one address + for i, p := range out { + ph, pp, _ := net.SplitHostPort(vhost.Normal(p.Address)) + 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, "], [")) + } + } + } var shared []SharedPort for _, p := range out { if len(p.Members) > 1 { diff --git a/internal/server/dnsconfig_test.go b/internal/server/dnsconfig_test.go index abb1600..39b65cd 100644 --- a/internal/server/dnsconfig_test.go +++ b/internal/server/dnsconfig_test.go @@ -97,3 +97,36 @@ func TestSharedPorts(t *testing.T) { t.Errorf("TLS and plain on one port: %v", err) } } + +// TestDNSServiceAnswers: a front end's HTTPS server on an IP of its own is +// what [dns] gives for its names; _answer says otherwise. +func TestDNSServiceAnswers(t *testing.T) { + dir := t.TempDir() + cert, key := selfSigned(t, dir) + tlsKeys := "tls_cert = " + cert + "\ntls_key = " + key + "\n" + 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("[msn]\nhttps_address = 192.168.77.11:443\n" + tlsKeys + "[ymsg]\nhttps_address = 192.168.77.12:443\n" + + tlsKeys + "[oscar]\ntls_address = 192.168.77.13:443\n" + tlsKeys + "[dns]\nprimary = 1.1.1.1\nymsg_answer = 10.0.0.12 fd00::12\n") + if err != nil { + t.Fatal(err) + } + if ports, err := c.SharedPorts(); err != nil || len(ports) != 0 { + t.Errorf("an IP each: shared %v %v", ports, err) + } + a := c.DNS.ServiceAnswers + if a["msn"][0].String() != "192.168.77.11" || a["oscar"][0].String() != "192.168.77.13" || + len(a["ymsg"]) != 2 || a["ymsg"][0].String() != "10.0.0.12" { + t.Errorf("answers %v", a) + } + if _, err := load("[msn]\nhttps_address = 192.168.77.11:443\n" + tlsKeys + "[ymsg]\nhttps_address = :443\n" + + tlsKeys); err == nil || !strings.Contains(err.Error(), "an IP of its own") { + t.Errorf("every address and one IP on one port: %v", err) + } + if _, err := load("[msn]\n[dns]\nmsn_answer = 1.2.3.4 5.6.7.8\n"); err == nil || !strings.Contains(err.Error(), "msn_answer") { + t.Errorf("two IPv4 answers: %v", err) + } +}