DNS over TLS and HTTPS set up in one step
jarl encrypted-dns setup (and the Encrypted DNS page) finds or obtains a certificate for the host name, checks that nothing else listens on the DoT and DoH ports at the chosen addresses (offering a free DoH port), and stages named's tls statement and listeners as one change, keeping port 53 where it was when there was no listen-on, plus the host name's records in the private views (local to the view in a linked zone). Status shows what to give Android, Linux, Firefox, Chrome and Windows clients. A tls statement with missing files is refused (tls_file_missing), and a certificate named uses can't be deleted. docs/encrypted-dns.md, e2e with real DoT and DoH queries. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ebd3a3dfbe
commit
66dcf53684
21 files changed
+1790
-8
No files matched your search
@@ -0,0 +1,395 @@
|
||||
// Package encdns sets up DNS over TLS (DoT) and DNS over HTTPS (DoH) in
|
||||
// named (docs/encrypted-dns.md): a tls statement naming a certificate Jarl
|
||||
// manages, and listen-on statements for port 853 (DoT) and the DoH port.
|
||||
// Its state is read from named's configuration, so what the setup stages
|
||||
// stays ordinary, visible configuration.
|
||||
package encdns
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/netip"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"jarl/internal/engine"
|
||||
"jarl/internal/model"
|
||||
"jarl/internal/namedconf"
|
||||
)
|
||||
|
||||
// Prefix starts the names of the tls statements the setup writes.
|
||||
const Prefix = "jarl-"
|
||||
|
||||
// DoTPort is the port for DNS over TLS (RFC 7858).
|
||||
const DoTPort = 853
|
||||
|
||||
// Listener is a listen-on or listen-on-v6 statement.
|
||||
type Listener struct {
|
||||
OptionID int64 `json:"option_id"`
|
||||
V6 bool `json:"v6"`
|
||||
Port int `json:"port"` // as written; 0 when it isn't
|
||||
TLS string `json:"tls,omitempty"` // tls statement ("" for plain DNS)
|
||||
HTTP string `json:"http,omitempty"` // http endpoints: DNS over HTTPS
|
||||
Addrs []string `json:"addresses"` // the address match list
|
||||
}
|
||||
|
||||
// EffectivePort is the port named listens on: as written, or its
|
||||
// default for the kind of listener.
|
||||
func (l Listener) EffectivePort() int {
|
||||
switch {
|
||||
case l.Port != 0:
|
||||
return l.Port
|
||||
case l.HTTP != "" && l.TLS != "" && l.TLS != "none":
|
||||
return 443
|
||||
case l.HTTP != "":
|
||||
return 80
|
||||
case l.TLS != "":
|
||||
return DoTPort
|
||||
}
|
||||
return 53
|
||||
}
|
||||
|
||||
// Kind is plain, dot or doh.
|
||||
func (l Listener) Kind() string {
|
||||
switch {
|
||||
case l.HTTP != "":
|
||||
return "doh"
|
||||
case l.TLS != "" && l.TLS != "none":
|
||||
return "dot"
|
||||
}
|
||||
return "plain"
|
||||
}
|
||||
|
||||
// Managed reports whether the setup wrote the listener.
|
||||
func (l Listener) Managed() bool { return strings.HasPrefix(l.TLS, Prefix) }
|
||||
|
||||
// Listeners returns the server's listen-on and listen-on-v6 statements.
|
||||
func Listeners(st *model.State) []Listener {
|
||||
var out []Listener
|
||||
for _, o := range st.ScopedOptions(model.ScopeOptions, 0) {
|
||||
if o.Name != "listen-on" && o.Name != "listen-on-v6" {
|
||||
continue
|
||||
}
|
||||
l, err := parseListener(o.Name, o.Value)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
l.OptionID, l.V6 = o.ID, o.Name == "listen-on-v6"
|
||||
out = append(out, l)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func parseListener(name, value string) (Listener, error) {
|
||||
var l Listener
|
||||
stmt, err := namedconf.ParseOptionValue(name, value)
|
||||
if err != nil {
|
||||
return l, err
|
||||
}
|
||||
items := stmt.Items[1:]
|
||||
for i := 0; i < len(items); i++ {
|
||||
it := items[i]
|
||||
if it.Block != nil {
|
||||
for _, s := range it.Block.Stmts {
|
||||
l.Addrs = append(l.Addrs, namedconf.Compact(s.Items))
|
||||
}
|
||||
continue
|
||||
}
|
||||
if it.Value == nil || i+1 >= len(items) || items[i+1].Value == nil {
|
||||
continue
|
||||
}
|
||||
arg := items[i+1].Value.Text
|
||||
switch strings.ToLower(it.Value.Text) {
|
||||
case "port":
|
||||
l.Port, _ = strconv.Atoi(arg)
|
||||
case "tls":
|
||||
l.TLS = arg
|
||||
case "http":
|
||||
l.HTTP = arg
|
||||
}
|
||||
i++
|
||||
}
|
||||
return l, nil
|
||||
}
|
||||
|
||||
// TLSBlock is a top-level tls statement.
|
||||
type TLSBlock struct {
|
||||
OptionID int64 `json:"option_id"`
|
||||
Name string `json:"name"`
|
||||
CertFile string `json:"cert_file"`
|
||||
KeyFile string `json:"key_file"`
|
||||
}
|
||||
|
||||
// TLSBlocks returns the top-level tls statements.
|
||||
func TLSBlocks(st *model.State) []TLSBlock {
|
||||
var out []TLSBlock
|
||||
for _, o := range st.ScopedOptions(model.ScopeTopLevel, 0) {
|
||||
if o.Name != "tls" {
|
||||
continue
|
||||
}
|
||||
stmt, err := namedconf.ParseOptionValue("tls", o.Value)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
b := TLSBlock{OptionID: o.ID, Name: stmt.Arg(0)}
|
||||
if blk := stmt.FirstBlock(); blk != nil {
|
||||
for _, s := range blk.Stmts {
|
||||
switch s.Keyword() {
|
||||
case "cert-file":
|
||||
b.CertFile = s.Arg(0)
|
||||
case "key-file":
|
||||
b.KeyFile = s.Arg(0)
|
||||
}
|
||||
}
|
||||
}
|
||||
out = append(out, b)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// Endpoint is an address and port named serves DoT or DoH on.
|
||||
type Endpoint struct {
|
||||
Address string `json:"address"`
|
||||
Port int `json:"port"`
|
||||
}
|
||||
|
||||
// Status is what is set up.
|
||||
type Status struct {
|
||||
Enabled bool `json:"enabled"`
|
||||
// TLS is the tls statement the setup wrote, and Certificate the
|
||||
// certificate it names (a Jarl certificate's name, when it is one).
|
||||
TLS *TLSBlock `json:"tls,omitempty"`
|
||||
Certificate string `json:"certificate,omitempty"`
|
||||
DoT []Endpoint `json:"dot"`
|
||||
DoH []Endpoint `json:"doh"`
|
||||
// Listeners are all listen-on statements, the setup's and others.
|
||||
Listeners []Listener `json:"listeners"`
|
||||
// Other are DoT or DoH listeners written by hand.
|
||||
Other bool `json:"other,omitempty"`
|
||||
}
|
||||
|
||||
// GetStatus reads the setup from the configuration. certDir is where Jarl
|
||||
// writes certificates for named (certificates.dir).
|
||||
func GetStatus(st *model.State, certDir string) Status {
|
||||
s := Status{Listeners: Listeners(st), DoT: []Endpoint{}, DoH: []Endpoint{}}
|
||||
if s.Listeners == nil {
|
||||
s.Listeners = []Listener{}
|
||||
}
|
||||
for _, b := range TLSBlocks(st) {
|
||||
if strings.HasPrefix(b.Name, Prefix) {
|
||||
b := b
|
||||
s.TLS = &b
|
||||
if dir := filepath.Dir(b.CertFile); filepath.Dir(dir) == filepath.Clean(certDir) {
|
||||
s.Certificate = filepath.Base(dir)
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
for _, l := range s.Listeners {
|
||||
if l.Kind() == "plain" {
|
||||
continue
|
||||
}
|
||||
if !l.Managed() {
|
||||
s.Other = true
|
||||
continue
|
||||
}
|
||||
for _, a := range l.Addrs {
|
||||
e := Endpoint{Address: a, Port: l.EffectivePort()}
|
||||
if l.Kind() == "doh" {
|
||||
s.DoH = append(s.DoH, e)
|
||||
} else {
|
||||
s.DoT = append(s.DoT, e)
|
||||
}
|
||||
}
|
||||
}
|
||||
s.Enabled = len(s.DoT)+len(s.DoH) > 0
|
||||
return s
|
||||
}
|
||||
|
||||
// Setup is what to set up.
|
||||
type Setup struct {
|
||||
// Hostname is the name clients connect to (the certificate's).
|
||||
Hostname string `json:"hostname"`
|
||||
// Certificate is the Jarl certificate (with the dns use) to serve.
|
||||
Certificate string `json:"certificate"`
|
||||
// Addresses are the addresses of this server to listen on.
|
||||
Addresses []string `json:"addresses"`
|
||||
DoT bool `json:"dot"`
|
||||
DoH bool `json:"doh"`
|
||||
DoHPort int `json:"doh_port"` // default 443
|
||||
DoTPort int `json:"dot_port"` // default 853 (clients expect it)
|
||||
// Views get the hostname's A/AAAA records (default: none). In a
|
||||
// linked zone they are local to the view.
|
||||
Views []string `json:"views"`
|
||||
}
|
||||
|
||||
// Plan returns the requests that set up encrypted DNS as s says,
|
||||
// replacing an earlier setup, and notes for the user. certDir is where
|
||||
// the certificate's files are.
|
||||
func Plan(st *model.State, s Setup, certDir string) ([]engine.Request, []string, error) {
|
||||
var notes []string
|
||||
if !s.DoT && !s.DoH {
|
||||
return nil, nil, fmt.Errorf("choose DNS over TLS, DNS over HTTPS or both")
|
||||
}
|
||||
if s.DoHPort == 0 {
|
||||
s.DoHPort = 443
|
||||
}
|
||||
if s.DoTPort == 0 {
|
||||
s.DoTPort = DoTPort
|
||||
}
|
||||
for _, p := range []int{s.DoHPort, s.DoTPort} {
|
||||
if p < 1 || p > 65535 || p == 53 {
|
||||
return nil, nil, fmt.Errorf("port %d: use a free port other than 53", p)
|
||||
}
|
||||
}
|
||||
if s.DoT && s.DoH && s.DoHPort == s.DoTPort {
|
||||
return nil, nil, fmt.Errorf("DoT and DoH need different ports (both are %d)", s.DoHPort)
|
||||
}
|
||||
if s.Certificate == "" {
|
||||
return nil, nil, fmt.Errorf("choose the certificate to serve")
|
||||
}
|
||||
host := strings.ToLower(strings.TrimSuffix(strings.TrimSpace(s.Hostname), "."))
|
||||
if host == "" || !strings.Contains(host, ".") {
|
||||
return nil, nil, fmt.Errorf("give the host name clients connect to, e.g. dns.example.com")
|
||||
}
|
||||
var v4, v6 []string
|
||||
for _, a := range s.Addresses {
|
||||
ip, err := netip.ParseAddr(strings.TrimSpace(a))
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("%q isn't an address of this server", a)
|
||||
}
|
||||
ip = ip.Unmap()
|
||||
if ip.IsUnspecified() {
|
||||
return nil, nil, fmt.Errorf("listen on this server's addresses, not on %s (that would include public ones)", ip)
|
||||
}
|
||||
if ip.Is4() {
|
||||
v4 = append(v4, ip.String())
|
||||
} else {
|
||||
v6 = append(v6, ip.String())
|
||||
}
|
||||
}
|
||||
if len(v4)+len(v6) == 0 {
|
||||
return nil, nil, fmt.Errorf("choose at least one address to listen on")
|
||||
}
|
||||
tlsName := Prefix + s.Certificate
|
||||
dir := filepath.Join(certDir, s.Certificate)
|
||||
tlsValue := fmt.Sprintf(`%s { cert-file "%s"; key-file "%s"; protocols { TLSv1.2; TLSv1.3; }; }`,
|
||||
tlsName, filepath.Join(dir, "fullchain.pem"), filepath.Join(dir, "privkey.pem"))
|
||||
|
||||
var reqs []engine.Request
|
||||
// Take out the earlier setup's listeners and tls statements.
|
||||
current := Listeners(st)
|
||||
for _, l := range current {
|
||||
if l.Managed() {
|
||||
reqs = append(reqs, engine.NewRequest(engine.OpOptionDelete, l.OptionID, nil))
|
||||
}
|
||||
}
|
||||
haveTLS := false
|
||||
for _, b := range TLSBlocks(st) {
|
||||
switch {
|
||||
case b.Name == tlsName:
|
||||
haveTLS = true
|
||||
reqs = append(reqs, engine.NewRequest(engine.OpOptionUpdate, b.OptionID, engine.OptionInput{Value: model.Ptr(tlsValue)}))
|
||||
case strings.HasPrefix(b.Name, Prefix):
|
||||
reqs = append(reqs, engine.NewRequest(engine.OpOptionDelete, b.OptionID, nil))
|
||||
}
|
||||
}
|
||||
if !haveTLS {
|
||||
reqs = append(reqs, option(model.ScopeTopLevel, "tls", tlsValue))
|
||||
}
|
||||
// named listens on port 53 everywhere only while there is no
|
||||
// listen-on statement at all: adding the first one for DoT would end
|
||||
// that, so keep it explicitly.
|
||||
plain := func(v6 bool) bool {
|
||||
for _, l := range current {
|
||||
if l.V6 == v6 && !l.Managed() && l.Kind() == "plain" {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
add := func(name string, addrs []string) {
|
||||
if len(addrs) == 0 {
|
||||
return
|
||||
}
|
||||
list := "{ " + strings.Join(addrs, "; ") + "; }"
|
||||
if !plain(name == "listen-on-v6") {
|
||||
reqs = append(reqs, option(model.ScopeOptions, name, "{ any; }"))
|
||||
notes = append(notes, fmt.Sprintf("named listened on port 53 on every address by default; %s { any; } keeps that, as the first %s statement would otherwise end it", name, name))
|
||||
}
|
||||
if s.DoT {
|
||||
reqs = append(reqs, option(model.ScopeOptions, name, fmt.Sprintf("port %d tls %s %s", s.DoTPort, tlsName, list)))
|
||||
}
|
||||
if s.DoH {
|
||||
reqs = append(reqs, option(model.ScopeOptions, name, fmt.Sprintf("port %d tls %s http default %s", s.DoHPort, tlsName, list)))
|
||||
}
|
||||
}
|
||||
add("listen-on", v4)
|
||||
add("listen-on-v6", v6)
|
||||
|
||||
// The host name's addresses, in the views asked for.
|
||||
fqdn := host + "."
|
||||
for _, vn := range s.Views {
|
||||
v := st.ViewByName(vn)
|
||||
if v == nil {
|
||||
return nil, nil, fmt.Errorf("view %q does not exist", vn)
|
||||
}
|
||||
z := zoneFor(st, fqdn, v.ID)
|
||||
if z == nil {
|
||||
notes = append(notes, fmt.Sprintf("view %s has no zone for %s, so no address records were added there; clients must find it some other way", v.Name, host))
|
||||
continue
|
||||
}
|
||||
var local *bool
|
||||
if z.LinkGroup != 0 {
|
||||
local = model.Ptr(true)
|
||||
}
|
||||
for _, set := range []struct {
|
||||
typ string
|
||||
addrs []string
|
||||
}{{"A", v4}, {"AAAA", v6}} {
|
||||
if len(set.addrs) == 0 {
|
||||
continue
|
||||
}
|
||||
reqs = append(reqs, engine.NewRequest(engine.OpRRSetReplace, 0, engine.RRSetInput{ZoneID: z.ID, Name: fqdn, Type: set.typ, Data: set.addrs, Local: local}))
|
||||
}
|
||||
}
|
||||
return reqs, notes, nil
|
||||
}
|
||||
|
||||
func option(scope, name, value string) engine.Request {
|
||||
return engine.NewRequest(engine.OpOptionCreate, 0, engine.OptionInput{Scope: model.Ptr(scope), Name: model.Ptr(name), Value: model.Ptr(value)})
|
||||
}
|
||||
|
||||
// zoneFor returns the primary zone in a view that holds name.
|
||||
func zoneFor(st *model.State, name string, viewID int64) *model.Zone {
|
||||
var best *model.Zone
|
||||
for _, z := range st.ZonesInView(viewID) {
|
||||
if z.Type == model.ZonePrimary && (name == z.Name || strings.HasSuffix(name, "."+z.Name)) && (best == nil || len(z.Name) > len(best.Name)) {
|
||||
best = z
|
||||
}
|
||||
}
|
||||
return best
|
||||
}
|
||||
|
||||
// Disable returns the requests that take the setup out: its listeners and
|
||||
// tls statement. Plain listeners and the host name's records stay.
|
||||
func Disable(st *model.State) []engine.Request {
|
||||
var reqs []engine.Request
|
||||
for _, l := range Listeners(st) {
|
||||
if l.Managed() {
|
||||
reqs = append(reqs, engine.NewRequest(engine.OpOptionDelete, l.OptionID, nil))
|
||||
}
|
||||
}
|
||||
for _, b := range TLSBlocks(st) {
|
||||
if strings.HasPrefix(b.Name, Prefix) {
|
||||
reqs = append(reqs, engine.NewRequest(engine.OpOptionDelete, b.OptionID, nil))
|
||||
}
|
||||
}
|
||||
return reqs
|
||||
}
|
||||
|
||||
// Private reports whether an address is private (RFC 1918, ULA, ...).
|
||||
func Private(a netip.Addr) bool {
|
||||
a = a.Unmap()
|
||||
return a.IsPrivate() || a.IsLoopback() || a.IsLinkLocalUnicast() || netip.MustParsePrefix("100.64.0.0/10").Contains(a)
|
||||
}
|
||||
@@ -0,0 +1,188 @@
|
||||
package encdns
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/netip"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"jarl/internal/auth"
|
||||
"jarl/internal/engine"
|
||||
"jarl/internal/model"
|
||||
"jarl/internal/render"
|
||||
"jarl/internal/store"
|
||||
)
|
||||
|
||||
type env struct {
|
||||
t *testing.T
|
||||
eng *engine.Engine
|
||||
st *store.Store
|
||||
certDir string
|
||||
}
|
||||
|
||||
func newEnv(t *testing.T) *env {
|
||||
dir := t.TempDir()
|
||||
st, err := store.Open(filepath.Join(dir, "jarl.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { st.Close() })
|
||||
eng := engine.New(st, nil, engine.Config{
|
||||
Paths: render.Paths{ConfigDir: filepath.Join(dir, "etc"), ZoneDir: filepath.Join(dir, "zones"), CacheDir: filepath.Join(dir, "cache")},
|
||||
StagingDir: filepath.Join(dir, "staging"), NoReload: true, SkipCheck: true,
|
||||
}, slog.New(slog.NewTextHandler(io.Discard, nil)))
|
||||
e := &env{t: t, eng: eng, st: st, certDir: filepath.Join(dir, "tls")}
|
||||
os.MkdirAll(filepath.Join(e.certDir, "dns"), 0o755)
|
||||
os.WriteFile(filepath.Join(e.certDir, "dns", "fullchain.pem"), []byte("x"), 0o644)
|
||||
os.WriteFile(filepath.Join(e.certDir, "dns", "privkey.pem"), []byte("x"), 0o600)
|
||||
e.submit(engine.NewRequest(engine.OpSettingsUpdate, 0, engine.SettingsInput{Nameservers: &[]string{"ns1.example.net."}}),
|
||||
engine.NewRequest(engine.OpViewCreate, 0, engine.ViewInput{Name: model.Ptr("internal"), MatchClients: &[]string{"10.0.0.0/8"}}),
|
||||
engine.NewRequest(engine.OpViewCreate, 0, engine.ViewInput{Name: model.Ptr("external"), MatchClients: &[]string{"any"}}))
|
||||
e.submit(engine.NewRequest(engine.OpZoneCreate, 0, engine.ZoneInput{Name: model.Ptr("example.com"), View: model.Ptr("internal")}))
|
||||
return e
|
||||
}
|
||||
|
||||
func (e *env) submit(reqs ...engine.Request) *engine.Result {
|
||||
e.t.Helper()
|
||||
res, err := e.eng.Submit(context.Background(), reqs, auth.System, "test", "", engine.ApplyOptions{})
|
||||
if err != nil {
|
||||
e.t.Fatalf("%v (%+v)", err, res)
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func (e *env) state() *model.State {
|
||||
st, err := e.st.LoadState(context.Background())
|
||||
if err != nil {
|
||||
e.t.Fatal(err)
|
||||
}
|
||||
return st
|
||||
}
|
||||
|
||||
func values(st *model.State, scope, name string) []string {
|
||||
var out []string
|
||||
for _, o := range st.ScopedOptions(scope, 0) {
|
||||
if o.Name == name {
|
||||
out = append(out, o.Value)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func TestSetup(t *testing.T) {
|
||||
e := newEnv(t)
|
||||
setup := Setup{Hostname: "dns.example.com", Certificate: "dns", Addresses: []string{"10.0.10.5", "2001:db8::5"}, DoT: true, DoH: true, Views: []string{"internal", "external"}}
|
||||
reqs, notes, err := Plan(e.state(), setup, e.certDir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
e.submit(reqs...)
|
||||
st := e.state()
|
||||
tls := values(st, model.ScopeTopLevel, "tls")
|
||||
if len(tls) != 1 || !strings.HasPrefix(tls[0], "jarl-dns {") || !strings.Contains(tls[0], filepath.Join(e.certDir, "dns", "fullchain.pem")) {
|
||||
t.Errorf("tls = %q", tls)
|
||||
}
|
||||
// The plain listeners named had by default are kept explicitly.
|
||||
want4 := []string{"{ any; }", "port 853 tls jarl-dns { 10.0.10.5; }", "port 443 tls jarl-dns http default { 10.0.10.5; }"}
|
||||
if got := values(st, model.ScopeOptions, "listen-on"); !slices.Equal(got, want4) {
|
||||
t.Errorf("listen-on = %q, want %q", got, want4)
|
||||
}
|
||||
if got := values(st, model.ScopeOptions, "listen-on-v6"); len(got) != 3 || got[0] != "{ any; }" {
|
||||
t.Errorf("listen-on-v6 = %q", got)
|
||||
}
|
||||
if len(notes) != 3 || !strings.Contains(strings.Join(notes, " "), "external has no zone") {
|
||||
t.Errorf("notes = %q", notes)
|
||||
}
|
||||
// The host name's addresses in the internal view.
|
||||
z := st.FindZone("example.com.", st.ViewByName("internal").ID)
|
||||
var addrs []string
|
||||
for _, r := range st.ZoneRecords(z.ID) {
|
||||
if r.Name == "dns.example.com." {
|
||||
addrs = append(addrs, r.Type+" "+r.Data)
|
||||
}
|
||||
}
|
||||
if !slices.Equal(addrs, []string{"A 10.0.10.5", "AAAA 2001:db8::5"}) {
|
||||
t.Errorf("records = %q", addrs)
|
||||
}
|
||||
s := GetStatus(st, e.certDir)
|
||||
if !s.Enabled || s.Certificate != "dns" || len(s.DoT) != 2 || len(s.DoH) != 2 || s.DoH[0].Port != 443 || s.Other {
|
||||
t.Errorf("status = %+v", s)
|
||||
}
|
||||
|
||||
// Setting up again replaces the setup: DoT only, one address, and the
|
||||
// plain listeners aren't doubled.
|
||||
reqs, _, err = Plan(st, Setup{Hostname: "dns.example.com", Certificate: "dns", Addresses: []string{"10.0.10.5"}, DoT: true}, e.certDir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
e.submit(reqs...)
|
||||
st = e.state()
|
||||
if got := values(st, model.ScopeOptions, "listen-on"); !slices.Equal(got, []string{"{ any; }", "port 853 tls jarl-dns { 10.0.10.5; }"}) {
|
||||
t.Errorf("after the second setup: %q", got)
|
||||
}
|
||||
if got := values(st, model.ScopeOptions, "listen-on-v6"); !slices.Equal(got, []string{"{ any; }"}) {
|
||||
t.Errorf("listen-on-v6 after the second setup: %q", got)
|
||||
}
|
||||
if got := values(st, model.ScopeTopLevel, "tls"); len(got) != 1 {
|
||||
t.Errorf("tls statements: %q", got)
|
||||
}
|
||||
|
||||
// Off: the setup's listeners and tls statement go, the rest stays.
|
||||
e.submit(Disable(st)...)
|
||||
st = e.state()
|
||||
if got := values(st, model.ScopeOptions, "listen-on"); !slices.Equal(got, []string{"{ any; }"}) || len(values(st, model.ScopeTopLevel, "tls")) != 0 || GetStatus(st, e.certDir).Enabled {
|
||||
t.Errorf("after disabling: listen-on %q tls %q", got, values(st, model.ScopeTopLevel, "tls"))
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetupRefuses(t *testing.T) {
|
||||
e := newEnv(t)
|
||||
st := e.state()
|
||||
for _, s := range []Setup{
|
||||
{Hostname: "dns.example.com", Certificate: "dns", Addresses: []string{"10.0.10.5"}}, // neither DoT nor DoH
|
||||
{Hostname: "dns.example.com", Certificate: "dns", Addresses: []string{"0.0.0.0"}, DoT: true}, // every address
|
||||
{Hostname: "dns.example.com", Certificate: "dns", DoT: true}, // no address
|
||||
{Hostname: "dns.example.com", Certificate: "dns", Addresses: []string{"10.0.10.5"}, DoH: true, DoHPort: 53}, // port 53
|
||||
{Hostname: "dns", Certificate: "dns", Addresses: []string{"10.0.10.5"}, DoT: true}, // not a host name
|
||||
} {
|
||||
if _, _, err := Plan(st, s, e.certDir); err == nil {
|
||||
t.Errorf("accepted %+v", s)
|
||||
}
|
||||
}
|
||||
// A tls statement whose files are missing is refused by validation.
|
||||
_, err := e.eng.Submit(context.Background(), []engine.Request{engine.NewRequest(engine.OpOptionCreate, 0, engine.OptionInput{
|
||||
Scope: model.Ptr(model.ScopeTopLevel), Name: model.Ptr("tls"), Value: model.Ptr(`x { cert-file "/nonexistent/c.pem"; key-file "/nonexistent/k.pem"; }`)})},
|
||||
auth.System, "test", "", engine.ApplyOptions{})
|
||||
if err == nil || !strings.Contains(err.Error(), "doesn't exist") {
|
||||
t.Errorf("missing files: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPorts(t *testing.T) {
|
||||
s, err := parseProcAddr("0100007F:0035")
|
||||
if err != nil || s.Addr != netip.MustParseAddr("127.0.0.1") || s.Port != 53 {
|
||||
t.Errorf("v4: %+v %v", s, err)
|
||||
}
|
||||
s, err = parseProcAddr("00000000000000000000000001000000:01BB")
|
||||
if err != nil || s.Addr != netip.MustParseAddr("::1") || s.Port != 443 {
|
||||
t.Errorf("v6: %+v %v", s, err)
|
||||
}
|
||||
e := newEnv(t)
|
||||
old := ListeningSockets
|
||||
t.Cleanup(func() { ListeningSockets = old })
|
||||
ListeningSockets = func() ([]Socket, error) {
|
||||
return []Socket{{netip.MustParseAddr("0.0.0.0"), 443}, {netip.MustParseAddr("10.0.10.5"), 8443}, {netip.MustParseAddr("::"), 853}}, nil
|
||||
}
|
||||
c, err := PortCheck(e.state(), []string{"10.0.10.5"}, []int{853, 443, 8443}, []string{"0.0.0.0:443"})
|
||||
if err != nil || len(c) != 3 || c[1].What != "jarld (Jarl's web UI and API)" || c[2].What != "another program" {
|
||||
t.Errorf("conflicts = %+v %v", c, err)
|
||||
}
|
||||
if p := FreeDoHPort(e.state(), []string{"10.0.10.5"}, nil); p != 4443 {
|
||||
t.Errorf("free DoH port = %d, want 4443", p)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,149 @@
|
||||
package encdns
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"net/netip"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"jarl/internal/model"
|
||||
)
|
||||
|
||||
// Socket is a listening TCP socket on this host.
|
||||
type Socket struct {
|
||||
Addr netip.Addr
|
||||
Port int
|
||||
}
|
||||
|
||||
// ListeningSockets reads the host's listening TCP sockets from
|
||||
// /proc/net/tcp and tcp6 (Linux; nil elsewhere). It needs no privileges
|
||||
// and binds nothing. Tests replace it.
|
||||
var ListeningSockets = func() ([]Socket, error) {
|
||||
var out []Socket
|
||||
for _, f := range []string{"/proc/net/tcp", "/proc/net/tcp6"} {
|
||||
socks, err := parseProcNet(f)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
continue
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, socks...)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func parseProcNet(path string) ([]Socket, error) {
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
var out []Socket
|
||||
sc := bufio.NewScanner(f)
|
||||
sc.Scan() // header
|
||||
for sc.Scan() {
|
||||
fields := strings.Fields(sc.Text())
|
||||
if len(fields) < 4 || fields[3] != "0A" { // 0A: LISTEN
|
||||
continue
|
||||
}
|
||||
s, err := parseProcAddr(fields[1])
|
||||
if err == nil {
|
||||
out = append(out, s)
|
||||
}
|
||||
}
|
||||
return out, sc.Err()
|
||||
}
|
||||
|
||||
// parseProcAddr parses "0100007F:0035": the address in 32-bit words of
|
||||
// host byte order (little-endian here), then the port.
|
||||
func parseProcAddr(s string) (Socket, error) {
|
||||
ha, hp, ok := strings.Cut(s, ":")
|
||||
if !ok {
|
||||
return Socket{}, fmt.Errorf("bad address %q", s)
|
||||
}
|
||||
b, err := hex.DecodeString(ha)
|
||||
if err != nil || (len(b) != 4 && len(b) != 16) {
|
||||
return Socket{}, fmt.Errorf("bad address %q", s)
|
||||
}
|
||||
for i := 0; i+4 <= len(b); i += 4 {
|
||||
b[i], b[i+1], b[i+2], b[i+3] = b[i+3], b[i+2], b[i+1], b[i]
|
||||
}
|
||||
addr, _ := netip.AddrFromSlice(b)
|
||||
port, err := strconv.ParseUint(hp, 16, 16)
|
||||
if err != nil {
|
||||
return Socket{}, err
|
||||
}
|
||||
return Socket{Addr: addr.Unmap(), Port: int(port)}, nil
|
||||
}
|
||||
|
||||
// Conflict is something else listening where a listener would go.
|
||||
type Conflict struct {
|
||||
Address string `json:"address"`
|
||||
Port int `json:"port"`
|
||||
What string `json:"what"` // who it is, when Jarl knows
|
||||
}
|
||||
|
||||
// PortCheck says which of addresses × ports something else listens on
|
||||
// already. named's own listeners (the setup's, now) and jarld's (known
|
||||
// listen addresses) are told apart.
|
||||
func PortCheck(st *model.State, addresses []string, ports []int, jarld []string) ([]Conflict, error) {
|
||||
socks, err := ListeningSockets()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// What named listens on now with DoT or DoH (it will again).
|
||||
ours := map[string]bool{}
|
||||
for _, l := range Listeners(st) {
|
||||
if l.Kind() != "plain" {
|
||||
for _, a := range l.Addrs {
|
||||
ours[a+"#"+strconv.Itoa(l.EffectivePort())] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
jarldOn := map[string]bool{}
|
||||
for _, l := range jarld {
|
||||
jarldOn[l] = true
|
||||
}
|
||||
var out []Conflict
|
||||
for _, as := range addresses {
|
||||
a, err := netip.ParseAddr(as)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
a = a.Unmap()
|
||||
for _, p := range ports {
|
||||
if ours[a.String()+"#"+strconv.Itoa(p)] {
|
||||
continue
|
||||
}
|
||||
for _, s := range socks {
|
||||
if s.Port != p || !(s.Addr == a || s.Addr.IsUnspecified() && s.Addr.Is4() == a.Is4() || s.Addr.IsUnspecified() && s.Addr.Is6()) {
|
||||
continue
|
||||
}
|
||||
what := "another program"
|
||||
for l := range jarldOn {
|
||||
if strings.HasSuffix(l, ":"+strconv.Itoa(p)) {
|
||||
what = "jarld (Jarl's web UI and API)"
|
||||
}
|
||||
}
|
||||
out = append(out, Conflict{Address: a.String(), Port: p, What: what})
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// FreeDoHPort returns the first of the usual DoH ports nothing listens on
|
||||
// at the addresses (0 when none is free).
|
||||
func FreeDoHPort(st *model.State, addresses []string, jarld []string) int {
|
||||
for _, p := range []int{443, 8443, 4443, 9443} {
|
||||
if c, err := PortCheck(st, addresses, []int{p}, jarld); err == nil && len(c) == 0 {
|
||||
return p
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
Reference in new issue
Block a user